Last active
December 10, 2015 11:18
-
-
Save knmkr/4426171 to your computer and use it in GitHub Desktop.
elisp / Quote selected region as python-list.
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
(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