Created
November 10, 2011 17:45
-
-
Save jl2/1355548 to your computer and use it in GitHub Desktop.
Emacs Lisp to run the current region through Python, then replace the region with the output of the script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun replace-python-exp () | |
"Run the region as a Python script, kill the region, insert the output of the Python script." | |
(interactive) | |
(let* ((tmp-file (make-temp-file "tmppl")) | |
(rb (region-beginning)) | |
(re (region-end)) | |
(str (buffer-substring-no-properties rb re))) | |
(if (string-match "print" str) | |
(progn | |
(write-region rb re tmp-file nil 1) | |
(kill-region rb re)) | |
(progn | |
(goto-char rb) | |
(insert "print(") | |
(goto-char (+ 6 re)) | |
(insert ")") | |
(setq re (point)) | |
(write-region rb re tmp-file nil 1) | |
(kill-region rb re) | |
(message str))) | |
(insert (shell-command-to-string (concat "py " tmp-file))) | |
(delete-file tmp-file))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment