Created
February 28, 2012 19:16
-
-
Save edmccard/1934483 to your computer and use it in GitHub Desktop.
Python mark inside/outside quotes
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
(if (fboundp 'python-beginning-of-string) | |
;; python.el | |
(defun er--python-string-start-pos () | |
"Returns character address of start of string, nil if not inside. " | |
(let ((pt (point))) | |
(save-excursion | |
;; python-beginning-of-string returns the current | |
;; position if point is not in a string, so we have | |
;; to check if point moved, and if not, we check if | |
;; we weren't already on the first quote of a string | |
(python-beginning-of-string) | |
(cond ((/= pt (point)) (point)) | |
((looking-at "\"\\|'") (point)) | |
(t nil))))) | |
;; python-mode.el | |
(defalias 'er--python-string-start-pos 'py-in-string-p)) | |
(defun er/mark-inside-python-quotes () | |
"Mark the inside of the current string, not including the quotation marks." | |
(interactive) | |
(let ((beg (er--python-string-start-pos))) | |
(when beg | |
(goto-char beg) | |
;; Skipping quote characters one-by-one will do the wrong thing | |
;; if a triple-quoted string ends with an escaped quote, e.g. | |
;; """The last word is \"quoted.\"""" | |
(let ((q-skip (if (looking-at "\"\"\"\\|'''") 3 1))) | |
(forward-sexp) | |
(backward-char q-skip) | |
(set-mark (point)) | |
(goto-char beg) | |
(forward-char q-skip))))) | |
(defun er/mark-outside-python-quotes () | |
"Mark the current string, including the quotation marks." | |
(interactive) | |
(let ((beg (er--python-string-start-pos))) | |
(when beg | |
(goto-char beg) | |
(set-mark (point)) | |
(forward-sexp) | |
(exchange-point-and-mark)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment