1 ;;; cycle-quotes.el --- Cycle between quote styles -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 ;; Author: Simen Heggestøyl <simenheg@gmail.com>
6 ;; Keywords: convenience
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;; This package provides the `cycle-quotes' command to cycle between
25 ;; different string quote styles. For instance, in JavaScript, there's
26 ;; three string quote characters: ", ` and '. In a JavaScript buffer,
27 ;; with point located someplace within the string, `cycle-quotes' will
28 ;; cycle between the following quote styles each time it's called:
30 ;; --> "Hi, it's me!" --> `Hi, it's me!` --> 'Hi, it\'s me!' --
32 ;; ------------------------------------------------------------
34 ;; As seen in the above example, `cycle-quotes' tries to escape and
35 ;; unescape quote characters intelligently.
39 (defvar-local cycle-quotes--quote-chars '()
40 "A list of string quote characters for the current mode.
41 Set the first time `cycle-quotes' is called in a buffer.")
43 (defvar-local cycle-quotes--quote-chars-mode nil
44 "The latest mode that the quote char list was last computed for.
45 If this is different from the current mode, the quote chars need
48 (defun cycle-quotes--set-quote-chars ()
49 "Set the quote chars for the current syntax table."
50 (let ((syntax-table (syntax-table)))
53 (lambda (char-code-or-range syntax)
54 (when (equal (syntax-class syntax) 7)
55 (if (consp char-code-or-range)
56 (let ((from (car char-code-or-range))
57 (to (cdr char-code-or-range)))
58 (dolist (char-code (number-sequence from to))
59 (add-to-list 'cycle-quotes--quote-chars char-code)))
61 'cycle-quotes--quote-chars char-code-or-range))))
63 (setq syntax-table (char-table-parent syntax-table)))
64 (setq-local cycle-quotes--quote-chars-mode major-mode)))
66 (defun cycle-quotes--next-quote-char (char)
67 "Return quote char after CHAR."
68 (let ((list-from-char (member char cycle-quotes--quote-chars)))
70 (if (= (length list-from-char) 1)
71 (car cycle-quotes--quote-chars)
72 (cadr list-from-char)))))
74 (defun cycle-quotes--fix-escapes (beg end escape-char unescape-char)
75 "Fix character escapes between BEG and END.
76 Instances of ESCAPE-CHAR will be escaped by `\', while instances
77 where UNESCAPE-CHAR are escaped by `\' will have their escape
79 (let ((escape-string (string escape-char))
80 (unescape-string (string unescape-char)))
83 (while (search-backward (concat "\\" unescape-string) beg t)
84 (replace-match unescape-string nil t)))
87 (while (search-backward escape-string beg t)
88 (replace-match (concat "\\" escape-string) nil t)
92 (defun cycle-quotes ()
93 "Cycle between string quote styles."
95 (unless (eq major-mode cycle-quotes--quote-chars-mode)
96 (cycle-quotes--set-quote-chars))
97 (if (< (length cycle-quotes--quote-chars) 2)
98 (message "The current mode has no alternative quote syntax")
99 (let ((quote-char (nth 3 (syntax-ppss))))
101 (message "Not inside a string")
102 (let ((inside-generic-string (eq quote-char t))
103 ;; Can't use `save-excursion', because the marker will get
104 ;; deleted if point is at the beginning of the string.
106 (when inside-generic-string
107 (skip-syntax-backward "^|")
109 (setq quote-char (char-after)))
110 (let ((new-quote-char
111 (cycle-quotes--next-quote-char
112 (if inside-generic-string
115 (unless inside-generic-string
116 (search-backward-regexp
117 (concat "\\([^\\]" (string quote-char) "\\)\\|"
118 "^" (string quote-char)))
119 (when (match-beginning 1)
122 ;; Handle multiple quotes, such as Python's triple
125 (search-forward-regexp
126 (format "%c+" quote-char))
127 (- (match-end 0) (match-beginning 0)))))
131 ;; `forward-sexp' fails to jump to the matching quote
132 ;; in some modes, for instance `js2-mode'.
133 (skip-syntax-backward "^\"|")
134 (cycle-quotes--fix-escapes
135 (+ beg 1) (+ (point) 1) new-quote-char quote-char))
136 (delete-char (- repeat))
138 (insert new-quote-char)))
141 (insert new-quote-char))))
142 (goto-char start-pos))))))
144 (provide 'cycle-quotes)
145 ;;; cycle-quotes.el ends here