Skip to content

Instantly share code, notes, and snippets.

@knmkr
Last active December 10, 2015 11:18
Show Gist options
  • Save knmkr/4426171 to your computer and use it in GitHub Desktop.
Save knmkr/4426171 to your computer and use it in GitHub Desktop.
elisp / Quote selected region as python-list.
(eval-when-compile (require 'cl))
;;; Quote selected region as python-list.
(defun quote-as-list ()
;; a t g c
;; -> ["a", "t", "g", "c"]
(interactive)
;; cut region as `seq`
(setq seq (buffer-substring (region-beginning) (region-end)))
(delete-region (region-beginning) (region-end))
;; split `seq` as `subseqs`
(setq subseqs (split-string seq))
;; print quoted subseqs as python-list: ["a", "t", "g", "c"]
(insert-string "[")
(let ((i 0))
(setq n (length subseqs))
(dolist (subseq subseqs)
(insert-string (format "\"%s\"" subseq))
(incf i)
(if (not (equal i n))
(insert-string ", "))))
(insert-string "]")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment