Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Last active February 26, 2017 07:02
Show Gist options
  • Save v2e4lisp/4233179 to your computer and use it in GitHub Desktop.
Save v2e4lisp/4233179 to your computer and use it in GitHub Desktop.
run current script (py, rb, el, js, php...)
;;; THIS gist is from Xah Emacs Tutorial
;;; http://ergoemacs.org/emacs/emacs.html
(defun run-current-file ()
"Execute or compile the current file.
For example, if the current buffer is the file x.pl,
then it'll call “perl x.pl” in a shell.
The file can be php, perl, python, ruby, javascript, bash, ocaml, vb, elisp.
File suffix is used to determine what program to run.
If the file is modified, ask if you want to save first. (This command always run the saved version.)
If the file is emacs lisp, run the byte compiled version if exist."
(interactive)
(let (suffixMap fName fSuffix progName cmdStr)
;; a keyed list of file suffix to comand-line program path/name
(setq suffixMap
'(("php" . "php")
;; ("coffee" . "coffee -p")
("pl" . "perl")
("py" . "python")
("rb" . "ruby")
("js" . "node") ; node.js
("sh" . "bash")
("ml" . "ocaml")
("vbs" . "cscript")))
(setq fName (buffer-file-name))
(setq fSuffix (file-name-extension fName))
(setq progName (cdr (assoc fSuffix suffixMap)))
(setq cmdStr (concat progName " \"" fName "\""))
(when (buffer-modified-p)
(progn
(when (y-or-n-p "Buffer modified. Do you want to save first?")
(save-buffer) ) ) )
(if (string-equal fSuffix "el") ; special case for emacs lisp
(progn
(load (file-name-sans-extension fName)))
(if progName
(progn
(message "Running…")
;; (message progName)
(shell-command cmdStr "*run-current-file output*" ))
(message "No recognized program file suffix for this file.")))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment