]> code.delx.au - gnu-emacs-elpa/blob - packages/cycle-quotes/cycle-quotes.el
Fix some quoting problems in doc strings
[gnu-emacs-elpa] / packages / cycle-quotes / cycle-quotes.el
1 ;;; cycle-quotes.el --- Cycle between quote styles -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
4
5 ;; Author: Simen Heggestøyl <simenheg@gmail.com>
6 ;; Keywords: convenience
7 ;; Version: 0.1
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
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:
29 ;;
30 ;; --> "Hi, it's me!" --> `Hi, it's me!` --> 'Hi, it\'s me!' --
31 ;; | |
32 ;; ------------------------------------------------------------
33 ;;
34 ;; As seen in the above example, `cycle-quotes' tries to escape and
35 ;; unescape quote characters intelligently.
36
37 ;;; Code:
38
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.")
42
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
46 to be recomputed.")
47
48 (defun cycle-quotes--set-quote-chars ()
49 "Set the quote chars for the current syntax table."
50 (let ((syntax-table (syntax-table)))
51 (while syntax-table
52 (map-char-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)))
60 (add-to-list
61 'cycle-quotes--quote-chars char-code-or-range))))
62 syntax-table)
63 (setq syntax-table (char-table-parent syntax-table)))
64 (setq-local cycle-quotes--quote-chars-mode major-mode)))
65
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)))
69 (when list-from-char
70 (if (= (length list-from-char) 1)
71 (car cycle-quotes--quote-chars)
72 (cadr list-from-char)))))
73
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
78 character removed."
79 (let ((escape-string (string escape-char))
80 (unescape-string (string unescape-char)))
81 (save-excursion
82 (goto-char end)
83 (while (search-backward (concat "\\" unescape-string) beg t)
84 (replace-match unescape-string nil t)))
85 (save-excursion
86 (goto-char end)
87 (while (search-backward escape-string beg t)
88 (replace-match (concat "\\" escape-string) nil t)
89 (forward-char -1)))))
90
91 ;;;###autoload
92 (defun cycle-quotes ()
93 "Cycle between string quote styles."
94 (interactive)
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))))
100 (if (not quote-char)
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.
105 (start-pos (point)))
106 (when inside-generic-string
107 (skip-syntax-backward "^|")
108 (forward-char -1)
109 (setq quote-char (char-after)))
110 (let ((new-quote-char
111 (cycle-quotes--next-quote-char
112 (if inside-generic-string
113 (char-after)
114 quote-char))))
115 (unless inside-generic-string
116 (search-backward-regexp
117 (concat "\\([^\\]" (string quote-char) "\\)\\|"
118 "^" (string quote-char)))
119 (when (match-beginning 1)
120 (forward-char)))
121 (let ((repeat
122 ;; Handle multiple quotes, such as Python's triple
123 ;; quotes.
124 (save-excursion
125 (search-forward-regexp
126 (format "%c+" quote-char))
127 (- (match-end 0) (match-beginning 0)))))
128 (save-excursion
129 (let ((beg (point)))
130 (forward-sexp)
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))
137 (dotimes (_ repeat)
138 (insert new-quote-char)))
139 (delete-char repeat)
140 (dotimes (_ repeat)
141 (insert new-quote-char))))
142 (goto-char start-pos))))))
143
144 (provide 'cycle-quotes)
145 ;;; cycle-quotes.el ends here