Skip to content

Instantly share code, notes, and snippets.

@notcancername
Created November 20, 2024 14:36
Show Gist options
  • Save notcancername/04cf2dcd0bce1a9b03e82bc4197e31a3 to your computer and use it in GitHub Desktop.
Save notcancername/04cf2dcd0bce1a9b03e82bc4197e31a3 to your computer and use it in GitHub Desktop.
elisp python-on-region
(defvar
python-on-region-template
"#! /usr/bin/env python3\nimport sys\nfor line in sys.stdin:\n sys.stdout.write(line$$)\n"
"The template to use for python-on-region. $$ indicates the point.")
(defun python-on-region--run ()
(interactive)
(save-buffer)
(delete-window)
(switch-to-buffer python-on-region--buffer)
(chmod python-on-region--file (logior (file-modes python-on-region--file) #o111))
(if (region-active-p)
(shell-command-on-region (region-beginning) (region-end) python-on-region--file (current-buffer) 'no-mark)
(shell-command-on-region 0 -1 python-on-region--file (current-buffer) 'no-mark)))
(defun python-on-region ()
"Run a python script on the current region."
(interactive)
(setq python-on-region--buffer (current-buffer))
(setq python-on-region--file (make-temp-file "" nil ".py" python-on-region-template))
(split-window-sensibly)
(other-window 1)
(find-file python-on-region--file)
(local-set-key (kbd "C-c C-c") 'python-on-region--run)
(replace-string "$$" ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment