Commit | Line | Data |
---|---|---|
7514d3f8 SM |
1 | ;;; elec-pair.el --- Automatic parenthesis pairing -*- lexical-binding:t -*- |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2013-2014 Free Software Foundation, Inc. |
7514d3f8 SM |
4 | |
5 | ;; Author: João Távora <joaotavora@gmail.com> | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
9 | ;; GNU Emacs 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 | ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;;; Code: | |
25 | ||
26 | (require 'electric) | |
27 | ||
28 | ;;; Electric pairing. | |
29 | ||
30 | (defcustom electric-pair-pairs | |
31 | '((?\" . ?\")) | |
32 | "Alist of pairs that should be used regardless of major mode. | |
33 | ||
34 | Pairs of delimiters in this list are a fallback in case they have | |
35 | no syntax relevant to `electric-pair-mode' in the mode's syntax | |
36 | table. | |
37 | ||
38 | See also the variable `electric-pair-text-pairs'." | |
39 | :version "24.1" | |
40 | :group 'electricity | |
41 | :type '(repeat (cons character character))) | |
42 | ||
43 | ;;;###autoload | |
44 | (defcustom electric-pair-text-pairs | |
45 | '((?\" . ?\" )) | |
46 | "Alist of pairs that should always be used in comments and strings. | |
47 | ||
48 | Pairs of delimiters in this list are a fallback in case they have | |
49 | no syntax relevant to `electric-pair-mode' in the syntax table | |
50 | defined in `electric-pair-text-syntax-table'" | |
51 | :version "24.4" | |
52 | :group 'electricity | |
53 | :type '(repeat (cons character character))) | |
54 | ||
55 | (defcustom electric-pair-skip-self #'electric-pair-default-skip-self | |
56 | "If non-nil, skip char instead of inserting a second closing paren. | |
57 | ||
58 | When inserting a closing paren character right before the same character, | |
59 | just skip that character instead, so that hitting ( followed by ) results | |
60 | in \"()\" rather than \"())\". | |
61 | ||
62 | This can be convenient for people who find it easier to hit ) than C-f. | |
63 | ||
64 | Can also be a function of one argument (the closer char just | |
65 | inserted), in which case that function's return value is | |
66 | considered instead." | |
67 | :version "24.1" | |
68 | :group 'electricity | |
69 | :type '(choice | |
70 | (const :tag "Never skip" nil) | |
71 | (const :tag "Help balance" electric-pair-default-skip-self) | |
72 | (const :tag "Always skip" t) | |
73 | function)) | |
74 | ||
75 | (defcustom electric-pair-inhibit-predicate | |
76 | #'electric-pair-default-inhibit | |
77 | "Predicate to prevent insertion of a matching pair. | |
78 | ||
79 | The function is called with a single char (the opening char just inserted). | |
80 | If it returns non-nil, then `electric-pair-mode' will not insert a matching | |
81 | closer." | |
82 | :version "24.4" | |
83 | :group 'electricity | |
84 | :type '(choice | |
85 | (const :tag "Conservative" electric-pair-conservative-inhibit) | |
86 | (const :tag "Help balance" electric-pair-default-inhibit) | |
87 | (const :tag "Always pair" ignore) | |
88 | function)) | |
89 | ||
90 | (defcustom electric-pair-preserve-balance t | |
91 | "Non-nil if default pairing and skipping should help balance parentheses. | |
92 | ||
93 | The default values of `electric-pair-inhibit-predicate' and | |
94 | `electric-pair-skip-self' check this variable before delegating to other | |
7e99158a PE |
95 | predicates responsible for making decisions on whether to pair/skip some |
96 | characters based on the actual state of the buffer's parentheses and | |
7514d3f8 SM |
97 | quotes." |
98 | :version "24.4" | |
99 | :group 'electricity | |
100 | :type 'boolean) | |
101 | ||
102 | (defcustom electric-pair-delete-adjacent-pairs t | |
103 | "If non-nil, backspacing an open paren also deletes adjacent closer. | |
104 | ||
105 | Can also be a function of no arguments, in which case that function's | |
106 | return value is considered instead." | |
107 | :version "24.4" | |
108 | :group 'electricity | |
109 | :type '(choice | |
110 | (const :tag "Yes" t) | |
111 | (const :tag "No" nil) | |
112 | function)) | |
113 | ||
114 | (defcustom electric-pair-open-newline-between-pairs t | |
115 | "If non-nil, a newline between adjacent parentheses opens an extra one. | |
116 | ||
117 | Can also be a function of no arguments, in which case that function's | |
118 | return value is considered instead." | |
119 | :version "24.4" | |
120 | :group 'electricity | |
121 | :type '(choice | |
122 | (const :tag "Yes" t) | |
123 | (const :tag "No" nil) | |
124 | function)) | |
125 | ||
126 | (defcustom electric-pair-skip-whitespace t | |
127 | "If non-nil skip whitespace when skipping over closing parens. | |
128 | ||
129 | The specific kind of whitespace skipped is given by the variable | |
130 | `electric-pair-skip-whitespace-chars'. | |
131 | ||
132 | The symbol `chomp' specifies that the skipped-over whitespace | |
133 | should be deleted. | |
134 | ||
135 | Can also be a function of no arguments, in which case that function's | |
136 | return value is considered instead." | |
137 | :version "24.4" | |
138 | :group 'electricity | |
139 | :type '(choice | |
140 | (const :tag "Yes, jump over whitespace" t) | |
9c61f806 | 141 | (const :tag "Yes, and delete whitespace" chomp) |
7514d3f8 SM |
142 | (const :tag "No, no whitespace skipping" nil) |
143 | function)) | |
144 | ||
145 | (defcustom electric-pair-skip-whitespace-chars (list ?\t ?\s ?\n) | |
146 | "Whitespace characters considered by `electric-pair-skip-whitespace'." | |
147 | :version "24.4" | |
148 | :group 'electricity | |
149 | :type '(choice (set (const :tag "Space" ?\s) | |
150 | (const :tag "Tab" ?\t) | |
151 | (const :tag "Newline" ?\n)) | |
152 | (list character))) | |
153 | ||
154 | (defun electric-pair--skip-whitespace () | |
155 | "Skip whitespace forward, not crossing comment or string boundaries." | |
156 | (let ((saved (point)) | |
157 | (string-or-comment (nth 8 (syntax-ppss)))) | |
158 | (skip-chars-forward (apply #'string electric-pair-skip-whitespace-chars)) | |
159 | (unless (eq string-or-comment (nth 8 (syntax-ppss))) | |
160 | (goto-char saved)))) | |
161 | ||
162 | (defvar electric-pair-text-syntax-table prog-mode-syntax-table | |
163 | "Syntax table used when pairing inside comments and strings. | |
164 | ||
165 | `electric-pair-mode' considers this syntax table only when point in inside | |
025f2c81 | 166 | quotes or comments. If lookup fails here, `electric-pair-text-pairs' will |
7514d3f8 SM |
167 | be considered.") |
168 | ||
169 | (defun electric-pair-backward-delete-char (n &optional killflag untabify) | |
170 | "Delete characters backward, and maybe also two adjacent paired delimiters. | |
171 | ||
da5ecfa9 | 172 | Remaining behavior is given by `backward-delete-char' or, if UNTABIFY is |
7514d3f8 SM |
173 | non-nil, `backward-delete-char-untabify'." |
174 | (interactive "*p\nP") | |
175 | (let* ((prev (char-before)) | |
176 | (next (char-after)) | |
5dca7759 JT |
177 | (syntax-info (and prev |
178 | (electric-pair-syntax-info prev))) | |
7514d3f8 SM |
179 | (syntax (car syntax-info)) |
180 | (pair (cadr syntax-info))) | |
5dca7759 JT |
181 | (when (and next pair |
182 | (if (functionp electric-pair-delete-adjacent-pairs) | |
7514d3f8 SM |
183 | (funcall electric-pair-delete-adjacent-pairs) |
184 | electric-pair-delete-adjacent-pairs) | |
7514d3f8 SM |
185 | (memq syntax '(?\( ?\" ?\$)) |
186 | (eq pair next)) | |
187 | (delete-char 1 killflag)) | |
188 | (if untabify | |
189 | (backward-delete-char-untabify n killflag) | |
190 | (backward-delete-char n killflag)))) | |
191 | ||
192 | (defun electric-pair-backward-delete-char-untabify (n &optional killflag) | |
193 | "Delete characters backward, and maybe also two adjacent paired delimiters. | |
194 | ||
da5ecfa9 | 195 | Remaining behavior is given by `backward-delete-char-untabify'." |
7514d3f8 SM |
196 | (interactive "*p\nP") |
197 | (electric-pair-backward-delete-char n killflag t)) | |
198 | ||
199 | (defun electric-pair-conservative-inhibit (char) | |
200 | (or | |
201 | ;; I find it more often preferable not to pair when the | |
202 | ;; same char is next. | |
203 | (eq char (char-after)) | |
204 | ;; Don't pair up when we insert the second of "" or of ((. | |
205 | (and (eq char (char-before)) | |
206 | (eq char (char-before (1- (point))))) | |
207 | ;; I also find it often preferable not to pair next to a word. | |
208 | (eq (char-syntax (following-char)) ?w))) | |
209 | ||
210 | (defun electric-pair-syntax-info (command-event) | |
211 | "Calculate a list (SYNTAX PAIR UNCONDITIONAL STRING-OR-COMMENT-START). | |
212 | ||
213 | SYNTAX is COMMAND-EVENT's syntax character. PAIR is | |
214 | COMMAND-EVENT's pair. UNCONDITIONAL indicates the variables | |
215 | `electric-pair-pairs' or `electric-pair-text-pairs' were used to | |
216 | lookup syntax. STRING-OR-COMMENT-START indicates that point is | |
025f2c81 | 217 | inside a comment or string." |
ee0761ca JB |
218 | (let* ((pre-string-or-comment (or (bobp) |
219 | (nth 8 (save-excursion | |
220 | (syntax-ppss (1- (point))))))) | |
7514d3f8 SM |
221 | (post-string-or-comment (nth 8 (syntax-ppss (point)))) |
222 | (string-or-comment (and post-string-or-comment | |
223 | pre-string-or-comment)) | |
224 | (table (if string-or-comment | |
225 | electric-pair-text-syntax-table | |
226 | (syntax-table))) | |
227 | (table-syntax-and-pair (with-syntax-table table | |
228 | (list (char-syntax command-event) | |
229 | (or (matching-paren command-event) | |
230 | command-event)))) | |
231 | (fallback (if string-or-comment | |
232 | (append electric-pair-text-pairs | |
233 | electric-pair-pairs) | |
234 | electric-pair-pairs)) | |
235 | (direct (assq command-event fallback)) | |
236 | (reverse (rassq command-event fallback))) | |
237 | (cond | |
238 | ((memq (car table-syntax-and-pair) | |
239 | '(?\" ?\( ?\) ?\$)) | |
240 | (append table-syntax-and-pair (list nil string-or-comment))) | |
241 | (direct (if (eq (car direct) (cdr direct)) | |
242 | (list ?\" command-event t string-or-comment) | |
243 | (list ?\( (cdr direct) t string-or-comment))) | |
244 | (reverse (list ?\) (car reverse) t string-or-comment))))) | |
245 | ||
246 | (defun electric-pair--insert (char) | |
247 | (let ((last-command-event char) | |
248 | (blink-matching-paren nil) | |
249 | (electric-pair-mode nil)) | |
250 | (self-insert-command 1))) | |
251 | ||
252 | (defun electric-pair--syntax-ppss (&optional pos where) | |
253 | "Like `syntax-ppss', but sometimes fallback to `parse-partial-sexp'. | |
254 | ||
025f2c81 | 255 | WHERE is a list defaulting to '(string comment) and indicates |
7514d3f8 SM |
256 | when to fallback to `parse-partial-sexp'." |
257 | (let* ((pos (or pos (point))) | |
258 | (where (or where '(string comment))) | |
259 | (quick-ppss (syntax-ppss)) | |
260 | (quick-ppss-at-pos (syntax-ppss pos))) | |
261 | (if (or (and (nth 3 quick-ppss) (memq 'string where)) | |
262 | (and (nth 4 quick-ppss) (memq 'comment where))) | |
263 | (with-syntax-table electric-pair-text-syntax-table | |
264 | (parse-partial-sexp (1+ (nth 8 quick-ppss)) pos)) | |
265 | ;; HACK! cc-mode apparently has some `syntax-ppss' bugs | |
266 | (if (memq major-mode '(c-mode c++ mode)) | |
267 | (parse-partial-sexp (point-min) pos) | |
268 | quick-ppss-at-pos)))) | |
269 | ||
025f2c81 JB |
270 | ;; Balancing means controlling pairing and skipping of parentheses |
271 | ;; so that, if possible, the buffer ends up at least as balanced as | |
272 | ;; before, if not more. The algorithm is slightly complex because | |
273 | ;; some situations like "()))" need pairing to occur at the end but | |
274 | ;; not at the beginning. Balancing should also happen independently | |
275 | ;; for different types of parentheses, so that having your {}'s | |
276 | ;; unbalanced doesn't keep `electric-pair-mode' from balancing your | |
277 | ;; ()'s and your []'s. | |
7514d3f8 | 278 | (defun electric-pair--balance-info (direction string-or-comment) |
7e99158a | 279 | "Examine lists forward or backward according to DIRECTION's sign. |
7514d3f8 SM |
280 | |
281 | STRING-OR-COMMENT is info suitable for running `parse-partial-sexp'. | |
282 | ||
7e99158a | 283 | Return a cons of two descriptions (MATCHED-P . PAIR) for the |
025f2c81 | 284 | innermost and outermost lists that enclose point. The outermost |
7514d3f8 | 285 | list enclosing point is either the first top-level or first |
7e99158a | 286 | mismatched list found by listing up. |
7514d3f8 | 287 | |
025f2c81 JB |
288 | If the outermost list is matched, don't rely on its PAIR. |
289 | If point is not enclosed by any lists, return ((t) . (t))." | |
7514d3f8 SM |
290 | (let* (innermost |
291 | outermost | |
292 | (table (if string-or-comment | |
293 | electric-pair-text-syntax-table | |
294 | (syntax-table))) | |
295 | (at-top-level-or-equivalent-fn | |
025f2c81 JB |
296 | ;; called when `scan-sexps' ran perfectly, when it found |
297 | ;; a parenthesis pointing in the direction of travel. | |
298 | ;; Also when travel started inside a comment and exited it. | |
7514d3f8 SM |
299 | #'(lambda () |
300 | (setq outermost (list t)) | |
301 | (unless innermost | |
302 | (setq innermost (list t))))) | |
303 | (ended-prematurely-fn | |
304 | ;; called when `scan-sexps' crashed against a parenthesis | |
025f2c81 | 305 | ;; pointing opposite the direction of travel. After |
7514d3f8 SM |
306 | ;; traversing that character, the idea is to travel one sexp |
307 | ;; in the opposite direction looking for a matching | |
308 | ;; delimiter. | |
309 | #'(lambda () | |
310 | (let* ((pos (point)) | |
311 | (matched | |
312 | (save-excursion | |
313 | (cond ((< direction 0) | |
314 | (condition-case nil | |
315 | (eq (char-after pos) | |
316 | (with-syntax-table table | |
317 | (matching-paren | |
318 | (char-before | |
319 | (scan-sexps (point) 1))))) | |
320 | (scan-error nil))) | |
321 | (t | |
322 | ;; In this case, no need to use | |
323 | ;; `scan-sexps', we can use some | |
324 | ;; `electric-pair--syntax-ppss' in this | |
325 | ;; case (which uses the quicker | |
326 | ;; `syntax-ppss' in some cases) | |
327 | (let* ((ppss (electric-pair--syntax-ppss | |
328 | (1- (point)))) | |
329 | (start (car (last (nth 9 ppss)))) | |
330 | (opener (char-after start))) | |
331 | (and start | |
332 | (eq (char-before pos) | |
333 | (or (with-syntax-table table | |
334 | (matching-paren opener)) | |
335 | opener)))))))) | |
336 | (actual-pair (if (> direction 0) | |
337 | (char-before (point)) | |
338 | (char-after (point))))) | |
339 | (unless innermost | |
340 | (setq innermost (cons matched actual-pair))) | |
341 | (unless matched | |
342 | (setq outermost (cons matched actual-pair))))))) | |
343 | (save-excursion | |
344 | (while (not outermost) | |
345 | (condition-case err | |
346 | (with-syntax-table table | |
347 | (scan-sexps (point) (if (> direction 0) | |
348 | (point-max) | |
349 | (- (point-max)))) | |
350 | (funcall at-top-level-or-equivalent-fn)) | |
351 | (scan-error | |
352 | (cond ((or | |
353 | ;; some error happened and it is not of the "ended | |
354 | ;; prematurely" kind"... | |
355 | (not (string-match "ends prematurely" (nth 1 err))) | |
356 | ;; ... or we were in a comment and just came out of | |
357 | ;; it. | |
358 | (and string-or-comment | |
359 | (not (nth 8 (syntax-ppss))))) | |
360 | (funcall at-top-level-or-equivalent-fn)) | |
361 | (t | |
362 | ;; exit the sexp | |
363 | (goto-char (nth 3 err)) | |
364 | (funcall ended-prematurely-fn))))))) | |
365 | (cons innermost outermost))) | |
366 | ||
367 | (defun electric-pair--looking-at-unterminated-string-p (char) | |
025f2c81 | 368 | "Return non-nil if following string starts with CHAR and is unterminated." |
7514d3f8 SM |
369 | ;; FIXME: ugly/naive |
370 | (save-excursion | |
371 | (skip-chars-forward (format "^%c" char)) | |
372 | (while (not (zerop (% (save-excursion (skip-syntax-backward "\\")) 2))) | |
373 | (unless (eobp) | |
374 | (forward-char 1) | |
375 | (skip-chars-forward (format "^%c" char)))) | |
376 | (and (not (eobp)) | |
377 | (condition-case nil | |
378 | (progn (forward-sexp) nil) | |
379 | (scan-error t))))) | |
380 | ||
381 | (defun electric-pair--inside-string-p (char) | |
025f2c81 | 382 | "Return non-nil if point is inside a string started by CHAR. |
7514d3f8 SM |
383 | |
384 | A comments text is parsed with `electric-pair-text-syntax-table'. | |
385 | Also consider strings within comments, but not strings within | |
386 | strings." | |
387 | ;; FIXME: could also consider strings within strings by examining | |
388 | ;; delimiters. | |
025f2c81 | 389 | (let ((ppss (electric-pair--syntax-ppss (point) '(comment)))) |
7514d3f8 SM |
390 | (memq (nth 3 ppss) (list t char)))) |
391 | ||
392 | (defun electric-pair-inhibit-if-helps-balance (char) | |
393 | "Return non-nil if auto-pairing of CHAR would hurt parentheses' balance. | |
394 | ||
395 | Works by first removing the character from the buffer, then doing | |
396 | some list calculations, finally restoring the situation as if nothing | |
397 | happened." | |
398 | (pcase (electric-pair-syntax-info char) | |
399 | (`(,syntax ,pair ,_ ,s-or-c) | |
400 | (unwind-protect | |
401 | (progn | |
402 | (delete-char -1) | |
403 | (cond ((eq ?\( syntax) | |
404 | (let* ((pair-data | |
405 | (electric-pair--balance-info 1 s-or-c)) | |
406 | (outermost (cdr pair-data))) | |
407 | (cond ((car outermost) | |
408 | nil) | |
409 | (t | |
410 | (eq (cdr outermost) pair))))) | |
411 | ((eq syntax ?\") | |
412 | (electric-pair--looking-at-unterminated-string-p char)))) | |
413 | (insert-char char))))) | |
414 | ||
415 | (defun electric-pair-skip-if-helps-balance (char) | |
416 | "Return non-nil if skipping CHAR would benefit parentheses' balance. | |
417 | ||
418 | Works by first removing the character from the buffer, then doing | |
419 | some list calculations, finally restoring the situation as if nothing | |
420 | happened." | |
421 | (pcase (electric-pair-syntax-info char) | |
422 | (`(,syntax ,pair ,_ ,s-or-c) | |
423 | (unwind-protect | |
424 | (progn | |
425 | (delete-char -1) | |
426 | (cond ((eq syntax ?\)) | |
427 | (let* ((pair-data | |
428 | (electric-pair--balance-info | |
429 | -1 s-or-c)) | |
430 | (innermost (car pair-data)) | |
431 | (outermost (cdr pair-data))) | |
432 | (and | |
433 | (cond ((car outermost) | |
434 | (car innermost)) | |
435 | ((car innermost) | |
436 | (not (eq (cdr outermost) pair))))))) | |
437 | ((eq syntax ?\") | |
438 | (electric-pair--inside-string-p char)))) | |
439 | (insert-char char))))) | |
440 | ||
441 | (defun electric-pair-default-skip-self (char) | |
442 | (if electric-pair-preserve-balance | |
443 | (electric-pair-skip-if-helps-balance char) | |
444 | t)) | |
445 | ||
446 | (defun electric-pair-default-inhibit (char) | |
447 | (if electric-pair-preserve-balance | |
448 | (electric-pair-inhibit-if-helps-balance char) | |
449 | (electric-pair-conservative-inhibit char))) | |
450 | ||
451 | (defun electric-pair-post-self-insert-function () | |
452 | (let* ((pos (and electric-pair-mode (electric--after-char-pos))) | |
453 | (skip-whitespace-info)) | |
454 | (pcase (electric-pair-syntax-info last-command-event) | |
455 | (`(,syntax ,pair ,unconditional ,_) | |
456 | (cond | |
457 | ((null pos) nil) | |
458 | ;; Wrap a pair around the active region. | |
459 | ;; | |
460 | ((and (memq syntax '(?\( ?\) ?\" ?\$)) (use-region-p)) | |
461 | ;; FIXME: To do this right, we'd need a post-self-insert-function | |
462 | ;; so we could add-function around it and insert the closer after | |
463 | ;; all the rest of the hook has run. | |
464 | (if (or (eq syntax ?\") | |
465 | (and (eq syntax ?\)) | |
466 | (>= (point) (mark))) | |
467 | (and (not (eq syntax ?\))) | |
468 | (>= (mark) (point)))) | |
469 | (save-excursion | |
470 | (goto-char (mark)) | |
471 | (electric-pair--insert pair)) | |
472 | (delete-region pos (1- pos)) | |
473 | (electric-pair--insert pair) | |
474 | (goto-char (mark)) | |
475 | (electric-pair--insert last-command-event))) | |
476 | ;; Backslash-escaped: no pairing, no skipping. | |
477 | ((save-excursion | |
478 | (goto-char (1- pos)) | |
479 | (not (zerop (% (skip-syntax-backward "\\") 2)))) | |
480 | nil) | |
481 | ;; Skip self. | |
482 | ((and (memq syntax '(?\) ?\" ?\$)) | |
483 | (and (or unconditional | |
484 | (if (functionp electric-pair-skip-self) | |
485 | (funcall electric-pair-skip-self last-command-event) | |
486 | electric-pair-skip-self)) | |
487 | (save-excursion | |
488 | (when (setq skip-whitespace-info | |
489 | (if (functionp electric-pair-skip-whitespace) | |
490 | (funcall electric-pair-skip-whitespace) | |
491 | electric-pair-skip-whitespace)) | |
492 | (electric-pair--skip-whitespace)) | |
493 | (eq (char-after) last-command-event)))) | |
494 | ;; This is too late: rather than insert&delete we'd want to only | |
495 | ;; skip (or insert in overwrite mode). The difference is in what | |
496 | ;; goes in the undo-log and in the intermediate state which might | |
497 | ;; be visible to other post-self-insert-hook. We'll just have to | |
498 | ;; live with it for now. | |
499 | (when skip-whitespace-info | |
500 | (electric-pair--skip-whitespace)) | |
501 | (delete-region (1- pos) (if (eq skip-whitespace-info 'chomp) | |
502 | (point) | |
503 | pos)) | |
504 | (forward-char)) | |
505 | ;; Insert matching pair. | |
506 | ((and (memq syntax `(?\( ?\" ?\$)) | |
507 | (not overwrite-mode) | |
508 | (or unconditional | |
509 | (not (funcall electric-pair-inhibit-predicate | |
510 | last-command-event)))) | |
511 | (save-excursion (electric-pair--insert pair))))) | |
512 | (t | |
513 | (when (and (if (functionp electric-pair-open-newline-between-pairs) | |
514 | (funcall electric-pair-open-newline-between-pairs) | |
515 | electric-pair-open-newline-between-pairs) | |
516 | (eq last-command-event ?\n) | |
e9d6c46a | 517 | (< (1+ (point-min)) (point) (point-max)) |
7514d3f8 SM |
518 | (eq (save-excursion |
519 | (skip-chars-backward "\t\s") | |
520 | (char-before (1- (point)))) | |
521 | (matching-paren (char-after)))) | |
522 | (save-excursion (newline 1 t))))))) | |
523 | ||
524 | (put 'electric-pair-post-self-insert-function 'priority 20) | |
525 | ||
526 | (defun electric-pair-will-use-region () | |
527 | (and (use-region-p) | |
528 | (memq (car (electric-pair-syntax-info last-command-event)) | |
529 | '(?\( ?\) ?\" ?\$)))) | |
530 | ||
531 | (defvar electric-pair-mode-map | |
532 | (let ((map (make-sparse-keymap))) | |
533 | (define-key map [remap backward-delete-char-untabify] | |
534 | 'electric-pair-backward-delete-char-untabify) | |
535 | (define-key map [remap backward-delete-char] | |
536 | 'electric-pair-backward-delete-char) | |
537 | (define-key map [remap delete-backward-char] | |
538 | 'electric-pair-backward-delete-char) | |
539 | map) | |
540 | "Keymap used by `electric-pair-mode'.") | |
541 | ||
542 | ;;;###autoload | |
543 | (define-minor-mode electric-pair-mode | |
544 | "Toggle automatic parens pairing (Electric Pair mode). | |
545 | With a prefix argument ARG, enable Electric Pair mode if ARG is | |
546 | positive, and disable it otherwise. If called from Lisp, enable | |
547 | the mode if ARG is omitted or nil. | |
548 | ||
549 | Electric Pair mode is a global minor mode. When enabled, typing | |
550 | an open parenthesis automatically inserts the corresponding | |
025f2c81 | 551 | closing parenthesis. (Likewise for brackets, etc.)." |
7514d3f8 SM |
552 | :global t :group 'electricity |
553 | (if electric-pair-mode | |
554 | (progn | |
555 | (add-hook 'post-self-insert-hook | |
556 | #'electric-pair-post-self-insert-function) | |
557 | (electric--sort-post-self-insertion-hook) | |
558 | (add-hook 'self-insert-uses-region-functions | |
559 | #'electric-pair-will-use-region)) | |
560 | (remove-hook 'post-self-insert-hook | |
561 | #'electric-pair-post-self-insert-function) | |
562 | (remove-hook 'self-insert-uses-region-functions | |
563 | #'electric-pair-will-use-region))) | |
564 | ||
565 | (provide 'elec-pair) | |
566 | ||
567 | ;;; elec-pair.el ends here |