elec-pair.el 24 KB

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