1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| ;;; -----------liu233w/run-current-file------------------- (defun liu233w/run-current-file () "Execute the current file. For example, if the current buffer is the file x.py, then it'll call「python x.py」in a shell. The file can be Emacs Lisp, PHP, Perl, Python, Ruby, JavaScript, Bash, Ocaml, Visual Basic, TeX, Java, Clojure. File suffix is used to determine what program to run.
If the file is modified or not saved, save it automatically before run.
URL `http://ergoemacs.org/emacs/elisp_run_current_file.html' version 2016-01-28" (interactive) (let ( (-suffix-map ;; (‹extension› . ‹shell program name›) `( ("php" . "php") ("pl" . "perl") ("py" . "python") ("py3" . ,(if (string-equal system-type "windows-nt") "python.exe" "python3")) ("rb" . "ruby") ("go" . "go run") ("js" . "node") ; node.js ("sh" . "bash") ("clj" . "java -cp /home/xah/apps/clojure-1.6.0/clojure-1.6.0.jar clojure.main") ("rkt" . "racket") ("ml" . "ocaml") ("vbs" . "cscript") ("tex" . "pdflatex") ("latex" . "pdflatex") ("java" . "javac") ("cpp" . ,(or (executable-find "myclang") (executable-find "clang") "g++")) ;; ("pov" . "/usr/local/bin/povray +R2 +A0.1 +J1.2 +Am2 +Q9 +H480 +W640") ))
-fname -fSuffix -prog-name -cmd-str)
(when (null (buffer-file-name)) (save-buffer)) (when (buffer-modified-p) (save-buffer))
(setq -fname (buffer-file-name)) (setq -fSuffix (file-name-extension -fname)) (setq -prog-name (cdr (assoc -fSuffix -suffix-map))) (setq -cmd-str (concat -prog-name " \"" -fname "\""))
(cond ((string-equal -fSuffix "el") (load -fname)) ((string-equal -fSuffix "java") (liu233w//compile-and-run -cmd-str (format "java %s" (file-name-sans-extension (file-name-nondirectory -fname))))) ((string-equal -fSuffix "cpp") (liu233w//compile-and-run (format "%s --std=c++11" -cmd-str) "a")) (t (if -prog-name (progn (message "Running…") (async-shell-command -cmd-str "*liu233w/run-current-file output*" )) (message "No recognized program file suffix for this file."))))))
(defun liu233w//compile-and-run (cmp-cmd run-cmd) "Run cmp-cmd, if success, then run run-cmd and print the result. Or just print the error message.
When it's compiling a file, this function may cause error behavior." (setf liu233w//compile-status (cons run-cmd compile-command)) (compile cmp-cmd) )
(defvar liu233w//compile-status nil "doc")
(defun liu233w//run-after-compile (buffer string) (when (and (string-match "compilation" (buffer-name buffer)) (string-match "finished" string) liu233w//compile-status) ;; 防止 liu233w/bury-compile-buffer-if-successful 删除 compilation buffer ;; 这样 async-shell-command 命令就能覆盖 compilation buffer 而不是源代码的 ;; buffer 了。 (with-current-buffer "*compilation*" (insert "warning LOL")) (async-shell-command (car liu233w//compile-status) "*liu233w/run-current-file output*") (setf compile-command (cdr liu233w//compile-status) liu233w//compile-status nil))) (add-hook 'compilation-finish-functions #'liu233w//run-after-compile) ;;; END -----------liu233w/run-current-file-------------------
|