Emacsist RSS --- If never, make it ever: C-h C-h

Emacs Lisp: Run Current File


这里 查看更多 Emacs 相关推荐文章最新 Emacs 圈的动态. 欢迎关注微信公众账号: Emacsist

This page shows a emacs lisp command that execute the current programing language file.


I want to be able to press a button, and have the current file executed or compiled. The file may be a Perl, Python, Ruby, PHP, Bash.

This way is even better than using a interactive command line, because you can write and edit normally with full emacs power. And you can run it by just pressing a key, such as F8.


Here's the solution:

(defun xah-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.
File suffix is used to determine what program to run.

If the file is modified, ask if you want to save first.

URL `http://ergoemacs.org/emacs/elisp_run_current_file.html'
version 2015-08-21"
  (let* (
          ;; (?extension? . ?shell program name?)
            ("php" . "php")
            ("pl" . "perl")
            ("py" . "python")
            ("py3" . ,(if (string-equal system-type "windows-nt") "c:/Python32/python.exe" "python3"))
            ("rb" . "ruby")
            ("js" . "node") ; node.js
            ("sh" . "bash")
            ("clj" . "java -cp /home/xah/apps/clojure-1.6.0/clojure-1.6.0.jar clojure.main")
            ("ml" . "ocaml")
            ("vbs" . "cscript")
            ("tex" . "pdflatex")
            ;; ("pov" . "/usr/local/bin/povray +R2 +A0.1 +J1.2 +Am2 +Q9 +H480 +W640")
         (ξfname (buffer-file-name))
         (ξfSuffix (file-name-extension ξfname))
         (ξprog-name (cdr (assoc ξfSuffix ξsuffix-map)))
         (ξcmd-str (concat ξprog-name " \""   ξfname "\"")))

    (when (buffer-modified-p)
      (when (y-or-n-p "Buffer modified. Do you want to save first?")

    (if (string-equal ξfSuffix "el") ; special case for emacs lisp
        (load ξfname)
      (if ξprog-name
            (message "Running…")
            (shell-command ξcmd-str "*xah-run-current-file output*" ))
        (message "No recognized program file suffix for this file.")))))

The code is very easy to understand.

(info "(elisp) Association Lists")

(info "(elisp) Buffer File Name")

(info "(elisp) File Name Components")

Now, we can define a keyboard shortcut for this:

(global-set-key (kbd "<f8>") 'xah-run-current-file)

〔? Emacs: How to Define Keys

So now, doesn't matter we are writing in Perl, Python, elisp, …, we can just press a button and have the file executed or compiled.

Determine Language by Major Mode

If you don't have file extension, you might use the current major mode to determine what language it is.

The major mode's name is stored in the variable major-mode.

You also need to redefine your associative list to store major mode names corresponding to program paths.

(thanks to Matic Bojan for suggesting major mode for determining language.)

原文出处: 李杀
原文地址: http://ergoemacs.org/emacs/elisp_run_current_file.html
原文时间: 2015-08-22 03:36
本文地址: http://emacsist.com/10421
整理时间: 2015-08-23 00:16

本文由 Hick 整理,转载请保留以上信息;
COPYLEFTThe articles on this site come from Internet, thanks to all the original authors.
      If anything about COPYRIGHT, or LEFT, please contact Emacsist at gmail dot com .