Skip to content

Instantly share code, notes, and snippets.

@jl2
Created November 10, 2011 17:45
Show Gist options
  • Save jl2/1355548 to your computer and use it in GitHub Desktop.
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.
(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