Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autopair.el 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. ;;; autopair.el --- Automagically pair braces and quotes like TextMate
  2. ;; Copyright (C) 2009,2010 Joao Tavora
  3. ;; Author: Joao Tavora <joaotavora [at] gmail.com>
  4. ;; Keywords: convenience, emulations
  5. ;; X-URL: http://autopair.googlecode.com
  6. ;; URL: http://autopair.googlecode.com
  7. ;; EmacsWiki: AutoPairs
  8. ;; Package-Requires: ((cl-lib "0.3"))
  9. ;; Version: 0.6.1
  10. ;; This program is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (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.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;
  22. ;; Another stab at making braces and quotes pair like in
  23. ;; TextMate:
  24. ;;
  25. ;; * Opening braces/quotes are autopaired;
  26. ;; * Closing braces/quotes are autoskipped;
  27. ;; * Backspacing an opening brace/quote autodeletes its adjacent pair.
  28. ;; * Newline between newly-opened brace pairs open an extra indented line.
  29. ;;
  30. ;; Autopair deduces from the current syntax table which characters to
  31. ;; pair, skip or delete.
  32. ;;
  33. ;;; Installation:
  34. ;;
  35. ;; (require 'autopair)
  36. ;; (autopair-global-mode) ;; to enable in all buffers
  37. ;;
  38. ;; To enable autopair in just some types of buffers, comment out the
  39. ;; `autopair-global-mode' and put autopair-mode in some major-mode
  40. ;; hook, like:
  41. ;;
  42. ;; (add-hook 'c-mode-common-hook #'(lambda () (autopair-mode)))
  43. ;;
  44. ;; Alternatively, do use `autopair-global-mode' and create
  45. ;; *exceptions* using the `autopair-dont-activate' local variable (for
  46. ;; emacs < 24), or just using (autopair-mode -1) (for emacs >= 24)
  47. ;; like:
  48. ;;
  49. ;; (add-hook 'c-mode-common-hook
  50. ;; #'(lambda ()
  51. ;; (setq autopair-dont-activate t)
  52. ;; (autopair-mode -1)))
  53. ;;
  54. ;;
  55. ;;; Use:
  56. ;;
  57. ;; The extension works by rebinding the braces and quotes keys, but
  58. ;; can still be minimally intrusive, since the original binding is
  59. ;; always called as if autopair did not exist.
  60. ;;
  61. ;; The decision of which keys to actually rebind is taken at
  62. ;; minor-mode activation time, based on the current major mode's
  63. ;; syntax tables. To achieve this kind of behaviour, an emacs
  64. ;; variable `emulation-mode-map-alists' was used.
  65. ;;
  66. ;; If you set `autopair-pair-criteria' and `autopair-skip-criteria' to
  67. ;; 'help-balance (which, by the way, is the default), braces are not
  68. ;; autopaired/autoskiped in all situations; the decision to autopair
  69. ;; or autoskip a brace is taken according to the following table:
  70. ;;
  71. ;; +---------+------------+-----------+-------------------+
  72. ;; | 1234567 | autopair? | autoskip? | notes |
  73. ;; +---------+------------+-----------+-------------------+
  74. ;; | (()) | yyyyyyy | ---yy-- | balanced |
  75. ;; +---------+------------+-----------+-------------------+
  76. ;; | (())) | ------y | ---yyy- | too many closings |
  77. ;; +---------+------------+-----------+-------------------+
  78. ;; | ((()) | yyyyyyy | ------- | too many openings |
  79. ;; +---------+------------+-----------+-------------------+
  80. ;;
  81. ;; The table is read like this: in a buffer with 7 characters laid out
  82. ;; like the first column, an "y" marks points where an opening brace
  83. ;; is autopaired and in which places would a closing brace be
  84. ;; autoskipped.
  85. ;;
  86. ;; Quote pairing tries to support similar "intelligence", but is less
  87. ;; deterministic. Some inside-string or inside-comment situations may
  88. ;; not always behave how you intend them to.
  89. ;;
  90. ;; The variable `autopair-autowrap' tells autopair to automatically
  91. ;; wrap the selection region with the delimiters you're trying to
  92. ;; insert. This is done conditionally based of syntaxes of the two
  93. ;; ends of the selection region. It is compatible with `cua-mode's
  94. ;; typing-deletes-selection behaviour.
  95. ;;
  96. ;; If you find the paren-blinking annoying, turn `autopair-blink' to
  97. ;; nil.
  98. ;;
  99. ;; For lisp-programming you might also like `autopair-skip-whitespace'.
  100. ;;
  101. ;; For further customization have a look at `autopair-dont-pair',
  102. ;; `autopair-handle-action-fns' and `autopair-extra-pairs'.
  103. ;;
  104. ;; `autopair-dont-pair' lets you define special cases of characters
  105. ;; you don't want paired. Its default value skips pairing
  106. ;; single-quote characters when inside a comment literal, even if the
  107. ;; language syntax tables does pair these characters.
  108. ;;
  109. ;; (defvar autopair-dont-pair `(:string (?') :comment (?'))
  110. ;;
  111. ;; As a further example, to also prevent the '{' (opening brace)
  112. ;; character from being autopaired in C++ comments use this in your
  113. ;; .emacs.
  114. ;;
  115. ;; (add-hook 'c++-mode-hook
  116. ;; #'(lambda ()
  117. ;; (push ?{
  118. ;; (cl-getf autopair-dont-pair :comment))))
  119. ;;
  120. ;; `autopair-handle-action-fns' lets you override/extend the actions
  121. ;; taken by autopair after it decides something must be paired,skipped
  122. ;; or deleted. To work with triple quoting in python mode, you can use
  123. ;; this for example:
  124. ;;
  125. ;; (add-hook 'python-mode-hook
  126. ;; #'(lambda ()
  127. ;; (setq autopair-handle-action-fns
  128. ;; (list #'autopair-default-handle-action
  129. ;; #'autopair-python-triple-quote-action))))
  130. ;;
  131. ;; It's also useful to deal with latex's mode use of the "paired
  132. ;; delimiter" syntax class.
  133. ;;
  134. ;; (add-hook 'latex-mode-hook
  135. ;; #'(lambda ()
  136. ;; (set (make-local-variable 'autopair-handle-action-fns)
  137. ;; (list #'autopair-default-handle-action
  138. ;; #'autopair-latex-mode-paired-delimiter-action))))
  139. ;;
  140. ;; `autopair-extra-pairs' lets you define extra pairing and skipping
  141. ;; behaviour for pairs not programmed into the syntax table. Watch
  142. ;; out, this is work-in-progress, a little unstable and does not help
  143. ;; balancing at all. To have '<' and '>' pair in c++-mode buffers, but
  144. ;; only in code, use:
  145. ;;
  146. ;; (add-hook 'c++-mode-hook
  147. ;; #'(lambda ()
  148. ;; (push '(?< . ?>)
  149. ;; (cl-getf autopair-extra-pairs :code))))
  150. ;;
  151. ;; if you program in emacs-lisp you might also like the following to
  152. ;; pair backtick and quote
  153. ;;
  154. ;; (add-hook 'emacs-lisp-mode-hook
  155. ;; #'(lambda ()
  156. ;; (push '(?` . ?')
  157. ;; (cl-getf autopair-extra-pairs :comment))
  158. ;; (push '(?` . ?')
  159. ;; (cl-getf autopair-extra-pairs :string))))
  160. ;;
  161. ;;; Bugs:
  162. ;;
  163. ;; * Quote pairing/skipping inside comments is not perfect...
  164. ;;
  165. ;; * See the last section on monkey-patching for the `defadvice'
  166. ;; tricks used to make `autopair-autowrap' work with `cua-mode' and
  167. ;; `delete-selection-mode'.
  168. ;;
  169. ;;; Credit:
  170. ;;
  171. ;; Thanks Ed Singleton for early testing.
  172. ;;
  173. ;;; Code:
  174. ;; requires
  175. (require 'cl-lib)
  176. (require 'paren)
  177. (defgroup autopair nil
  178. "Automagically pair braces and quotes"
  179. :group 'convenience)
  180. ;; variables
  181. (defcustom autopair-pair-criteria 'help-balance
  182. "How to decide whether to pair opening brackets or quotes.
  183. Set this to 'always to always pair, or 'help-balance to be more
  184. criterious when pairing."
  185. :group 'autopair
  186. :type '(choice (const :tag "Help balance" help-balance)
  187. (const :tag "Always pair" always)))
  188. (defcustom autopair-skip-criteria 'help-balance
  189. "How to decide whether to skip closing brackets or quotes.
  190. Set this to 'always to always skip, or 'help-balance to be more
  191. criterious when skipping."
  192. :group 'autopair
  193. :type '(choice (const :tag "Help balance" help-balance)
  194. (const :tag "Always skip" always)))
  195. (defcustom autopair-autowrap 'help-balance
  196. "If non-nil autopair attempts to wrap the selected region.
  197. This is also done in an optimistic \"try-to-balance\" fashion.
  198. Set this to to 'help-balance to be more criterious when
  199. wrapping."
  200. :group 'autopair
  201. :type '(choice (const :tag "Do wrap" t)
  202. (const :tag "Do not wrap" nil)
  203. (const :tag "Help Balance" 'help-balance)))
  204. (defvar autopair--emulation-alist nil
  205. "A keymap alist for adding to `emulation-mode-map-alists'.
  206. The alist contains single (t MAP) association, where MAP is a
  207. dynamic keymap set mostly from the major mode's syntax table.")
  208. (unless (eval-when-compile (> emacs-major-version 23))
  209. (defvar autopair-dont-activate nil
  210. "Control activation of `autopair-global-mode'.
  211. Set this to a non-nil value to skip activation of `autopair-mode'
  212. in certain contexts. If however the value satisfies `functionp'
  213. and is a function of no arguments, the function is called and it is
  214. the return value that decides.")
  215. (make-variable-buffer-local 'autopair-dont-activate))
  216. (defvar autopair-extra-pairs nil
  217. "Extra pairs for which to use pairing.
  218. It's a Common-lisp-style even-numbered property list, each pair
  219. of elements being of the form (TYPE , PAIRS). PAIRS is a mixed
  220. list whose elements are cons cells, which look like cells look
  221. like (OPENING . CLOSING). Autopair pairs these like
  222. parenthesis.
  223. TYPE can be one of:
  224. :string : whereby PAIRS will be considered only when inside a
  225. string literal
  226. :comment : whereby PAIRS will be considered only when inside a comment
  227. :code : whereby PAIRS will be considered only when outisde a
  228. string and a comment.
  229. :everywhere : whereby PAIRS will be considered in all situations
  230. In Emacs-lisp, this might be useful
  231. (add-hook 'emacs-lisp-mode-hook
  232. #'(lambda ()
  233. (setq autopair-extra-pairs `(:comment ((?`. ?'))))))
  234. Note that this does *not* work for single characters,
  235. e.x. characters you want to behave as quotes. See the
  236. docs/source comments for more details.")
  237. (make-variable-buffer-local 'autopair-extra-pairs)
  238. (defvar autopair-dont-pair `(:string (?') :comment (?'))
  239. "Characters for which to skip any pairing behaviour.
  240. This variable overrides `autopair-pair-criteria' and
  241. `autopair-extra-pairs'. It does not
  242. (currently) affect the skipping behaviour.
  243. It's a Common-lisp-style even-numbered property list, each pair
  244. of elements being of the form (TYPE , CHARS). CHARS is a list of
  245. characters and TYPE can be one of:
  246. :string : whereby characters in CHARS will not be autopaired when
  247. inside a string literal
  248. :comment : whereby characters in CHARS will not be autopaired when
  249. inside a comment
  250. :never : whereby characters in CHARS won't even have their
  251. bindings replaced by autopair's. This particular option
  252. should be used for troubleshooting and requires
  253. `autopair-mode' to be restarted to have any effect.")
  254. (make-variable-buffer-local 'autopair-dont-pair)
  255. (defvar autopair-action nil
  256. "Autopair action decided on by last interactive autopair command, or nil.
  257. When autopair decides on an action this is a list whose first
  258. three elements are (ACTION PAIR POS-BEFORE).
  259. ACTION is one of `opening', `insert-quote', `skip-quote',
  260. `backspace', `newline' or `paired-delimiter'. PAIR is the pair of
  261. the `autopair--inserted' character, if applicable. POS-BEFORE is
  262. value of point before action command took place .")
  263. (defvar autopair-wrap-action nil
  264. "Autowrap action decided on by autopair, if any.
  265. When autopair decides on an action this is a list whose first
  266. three elements are (ACTION PAIR POS-BEFORE REGION-BEFORE).
  267. ACTION can only be `wrap' currently. PAIR and POS-BEFORE
  268. delimiter are as in `autopair-action'. REGION-BEFORE is a cons
  269. cell with the bounds of the region before the command takes
  270. place")
  271. (defvar autopair-handle-action-fns '()
  272. "Autopair handlers to run *instead* of the default handler.
  273. Each element is a function taking three arguments (ACTION, PAIR
  274. and POS-BEFORE), which are the three elements of the
  275. `autopair-action' variable, which see.
  276. If non-nil, these functions are called *instead* of the single
  277. function `autopair-default-handle-action', so use this variable
  278. to specify special behaviour. To also run the default behaviour,
  279. be sure to include `autopair-default-handle-action' in the
  280. list, or call it from your handlers.")
  281. (make-variable-buffer-local 'autopair-handle-action-fns)
  282. (defvar autopair-handle-wrap-action-fns '()
  283. "Autopair wrap handlers to run *instead* of the default handler.
  284. Each element is a function taking four arguments (ACTION, PAIR,
  285. POS-BEFORE and REGION-BEFORE), which are the three elements of the
  286. `autopair-wrap-action' variable, which see.
  287. If non-nil, these functions are called *instead* of the single
  288. function `autopair-default-handle-wrap-action', so use this
  289. variable to specify special behaviour. To also run the default
  290. behaviour, be sure to include `autopair-default-handle-wrap-action' in
  291. the list, or call it in your handlers.")
  292. (make-variable-buffer-local 'autopair-handle-wrap-action-fns)
  293. (defvar autopair-inserted nil
  294. "Delimiter inserted by last interactive autopair command.
  295. This is calculated with `autopair-calculate-inserted', which see.")
  296. (defun autopair--calculate-inserted ()
  297. "Attempts to guess the delimiter the current command is inserting.
  298. For now, simply returns `last-command-event'"
  299. last-command-event)
  300. ;; minor mode and global mode
  301. ;;
  302. ;;;###autoload
  303. (define-minor-mode autopair-mode
  304. "Automagically pair braces and quotes like in TextMate."
  305. nil " pair" nil
  306. (cond (autopair-mode
  307. ;; Setup the dynamic emulation keymap, i.e. sets `autopair--emulation-alist'
  308. ;;
  309. (autopair--set-emulation-bindings)
  310. (add-to-list 'emulation-mode-map-alists 'autopair--emulation-alist 'append)
  311. ;; Init important vars
  312. ;;
  313. (setq autopair-action nil)
  314. (setq autopair-wrap-action nil)
  315. ;; Add the post command handler
  316. ;;
  317. (add-hook 'post-command-hook 'autopair--post-command-handler nil 'local))
  318. (t
  319. (set (make-local-variable 'autopair--emulation-alist) nil)
  320. (remove-hook 'post-command-hook 'autopair--post-command-handler 'local))))
  321. ;;;###autoload
  322. (define-globalized-minor-mode autopair-global-mode autopair-mode autopair-on)
  323. (when (eval-when-compile (>= emacs-major-version 24))
  324. (defvar autopair--global-mode-emacs24-hack-flag nil)
  325. (defadvice autopair-global-mode-enable-in-buffers (before autopairs-global-mode-emacs24-hack activate)
  326. "Monkey patch for recent emacsen 24.
  327. It's impossible for a globalized minor-mode to see variables set
  328. by major-mode-hooks. However, the auto-generated
  329. `autopair-global-mode-enable-in-buffers' does run after the
  330. major-mode-hooks.
  331. This advice makes sure the emulation keybindings are (re)set
  332. there. It relies on the fact that
  333. `autopair-global-mode-enable-in-buffers' is still called again in
  334. `after-change-major-mode-hook' (but the autopair-mode has already
  335. been turned on before the major-mode hooks kicked in).
  336. We want this advice to only kick in the *second* call to
  337. `autopair-global-mode-enable-in-buffers'."
  338. (dolist (buf autopair-global-mode-buffers)
  339. (when (buffer-live-p buf)
  340. (with-current-buffer buf
  341. (when (and autopair-mode
  342. (not autopair--global-mode-emacs24-hack-flag))
  343. (autopair--set-emulation-bindings)
  344. (set (make-local-variable 'autopair--global-mode-emacs24-hack-flag) t)))))))
  345. (defun autopair-on ()
  346. (unless (or buffer-read-only
  347. (and (not (minibufferp))
  348. (string-match "^ \\*" (buffer-name)))
  349. (eq major-mode 'sldb-mode)
  350. (and (eval-when-compile (< emacs-major-version 24))
  351. (boundp 'autopair-dont-activate)
  352. autopair-dont-activate)
  353. (autopair-mode 1))))
  354. (defun autopair--set-emulation-bindings ()
  355. "Setup keymap MAP with keybindings based on the major-mode's
  356. syntax table and the local value of `autopair-extra-pairs'."
  357. (let ((map (make-sparse-keymap)))
  358. (define-key map [remap delete-backward-char] 'autopair-backspace)
  359. (define-key map [remap backward-delete-char-untabify] 'autopair-backspace)
  360. (define-key map "\177" 'autopair-backspace)
  361. (define-key map "\r" 'autopair-newline)
  362. (dotimes (char 256) ;; only searches the first 256 chars,
  363. ;; TODO: is this enough/toomuch/stupid?
  364. (unless (member char
  365. (cl-getf autopair-dont-pair :never))
  366. (let* ((syntax-entry (aref (syntax-table) char))
  367. (class (and syntax-entry
  368. (syntax-class syntax-entry)))
  369. (pair (and syntax-entry
  370. (cdr syntax-entry))))
  371. (cond ((and (eq class (car (string-to-syntax "(")))
  372. pair)
  373. ;; syntax classes "opening parens" and "close parens"
  374. (define-key map (string char) 'autopair-insert-opening)
  375. (define-key map (string pair) 'autopair-skip-close-maybe))
  376. ((eq class (car (string-to-syntax "\"")))
  377. ;; syntax class "string quote
  378. (define-key map (string char) 'autopair-insert-or-skip-quote))
  379. ((eq class (car (string-to-syntax "$")))
  380. ;; syntax class "paired-delimiter"
  381. ;;
  382. ;; Apropos this class, see Issues 18, 25 and
  383. ;; elisp info node "35.2.1 Table of Syntax
  384. ;; Classes". The fact that it supresses
  385. ;; syntatic properties in the delimited region
  386. ;; dictates that deciding to autopair/autoskip
  387. ;; can't really be as clean as the string
  388. ;; delimiter.
  389. ;;
  390. ;; Apparently, only `TeX-mode' uses this, so
  391. ;; the best is to bind this to
  392. ;; `autopair-insert-or-skip-paired-delimiter'
  393. ;; which defers any decision making to
  394. ;; mode-specific post-command handler
  395. ;; functions.
  396. ;;
  397. (define-key map (string char) 'autopair-insert-or-skip-paired-delimiter))))))
  398. ;; read `autopair-extra-pairs'
  399. ;;
  400. (dolist (pairs-list (cl-remove-if-not #'listp autopair-extra-pairs))
  401. (dolist (pair pairs-list)
  402. (define-key map (string (car pair)) 'autopair-extra-insert-opening)
  403. (define-key map (string (cdr pair)) 'autopair-extra-skip-close-maybe)))
  404. (set (make-local-variable 'autopair--emulation-alist) (list (cons t map)))))
  405. ;; helper functions
  406. ;;
  407. (defun autopair--syntax-ppss ()
  408. "Calculate syntax info relevant to autopair.
  409. A list of four elements is returned:
  410. - SYNTAX-INFO is either the result `syntax-ppss' or the result of
  411. calling `parse-partial-sexp' with the appropriate
  412. bounds (previously calculated with `syntax-ppss'.
  413. - WHERE-SYM can be one of the symbols :string, :comment or :code.
  414. - QUICK-SYNTAX-INFO is always the result returned by `syntax-ppss'.
  415. - BOUNDS are the boudaries of the current string or comment if
  416. we're currently inside one."
  417. (let* ((quick-syntax-info (syntax-ppss))
  418. (string-or-comment-start (nth 8 quick-syntax-info)))
  419. (cond (;; inside a string, recalculate
  420. (nth 3 quick-syntax-info)
  421. (list (parse-partial-sexp (1+ string-or-comment-start) (point))
  422. :string
  423. quick-syntax-info
  424. (cons string-or-comment-start
  425. (condition-case nil
  426. (scan-sexps string-or-comment-start 1)
  427. (scan-error nil)))))
  428. ((nth 4 quick-syntax-info)
  429. (list (parse-partial-sexp (1+ (nth 8 quick-syntax-info)) (point))
  430. :comment
  431. quick-syntax-info))
  432. (t
  433. (list quick-syntax-info
  434. :code
  435. quick-syntax-info)))))
  436. (defun autopair--pair-of (delim &optional closing)
  437. (when (and delim
  438. (integerp delim))
  439. (let ((syntax-entry (aref (syntax-table) delim)))
  440. (cond ((eq (syntax-class syntax-entry) (car (string-to-syntax "(")))
  441. (cdr syntax-entry))
  442. ((or (eq (syntax-class syntax-entry) (car (string-to-syntax "\"")))
  443. (eq (syntax-class syntax-entry) (car (string-to-syntax "$"))))
  444. delim)
  445. ((and (not closing)
  446. (eq (syntax-class syntax-entry) (car (string-to-syntax ")"))))
  447. (cdr syntax-entry))
  448. (autopair-extra-pairs
  449. (cl-some #'(lambda (pair-list)
  450. (cl-some #'(lambda (pair)
  451. (cond ((eq (cdr pair) delim) (car pair))
  452. ((eq (car pair) delim) (cdr pair))))
  453. pair-list))
  454. (cl-remove-if-not #'listp autopair-extra-pairs)))))))
  455. (defun autopair--calculate-wrap-action ()
  456. (when (and transient-mark-mode mark-active)
  457. (when (> (point) (mark))
  458. (exchange-point-and-mark))
  459. (save-excursion
  460. (let* ((region-before (cons (region-beginning)
  461. (region-end)))
  462. (point-before (point))
  463. (start-syntax (syntax-ppss (car region-before)))
  464. (end-syntax (syntax-ppss (cdr region-before))))
  465. (when (or (not (eq autopair-autowrap 'help-balance))
  466. (and (eq (nth 0 start-syntax) (nth 0 end-syntax))
  467. (eq (nth 3 start-syntax) (nth 3 end-syntax))))
  468. (list 'wrap (or (cl-second autopair-action)
  469. (autopair--pair-of autopair-inserted))
  470. point-before
  471. region-before))))))
  472. (defun autopair--original-binding (fallback-keys)
  473. (or (key-binding `[,autopair-inserted])
  474. (key-binding (this-single-command-keys))
  475. (key-binding fallback-keys)))
  476. (defvar autopair--this-command nil)
  477. (defun autopair--fallback (&optional fallback-keys)
  478. (let* ((autopair--emulation-alist nil)
  479. (beyond-cua (let ((cua--keymap-alist nil))
  480. (autopair--original-binding fallback-keys)))
  481. (beyond-autopair (autopair--original-binding fallback-keys)))
  482. (when autopair-autowrap
  483. (setq autopair-wrap-action (autopair--calculate-wrap-action)))
  484. (setq autopair--this-command this-command)
  485. (setq this-original-command beyond-cua)
  486. ;; defer to "paredit-mode" if that is installed and running
  487. (when (and (featurep 'paredit)
  488. (symbolp beyond-cua)
  489. (string-match "paredit" (symbol-name beyond-cua)))
  490. (setq autopair-action nil))
  491. (let ((cua-delete-selection (not autopair-autowrap))
  492. (blink-matching-paren (not autopair-action)))
  493. (call-interactively beyond-autopair))))
  494. (defcustom autopair-skip-whitespace nil
  495. "If non-nil also skip over whitespace when skipping closing delimiters.
  496. If set to 'chomp, this will be most useful in lisp-like languages
  497. where you want lots of )))))...."
  498. :group 'autopair
  499. :type 'boolean)
  500. (defcustom autopair-blink (if (boundp 'blink-matching-paren)
  501. blink-matching-paren
  502. t)
  503. "If non-nil autopair blinks matching delimiters."
  504. :group 'autopair
  505. :type 'boolean)
  506. (defcustom autopair-blink-delay 0.1
  507. "Autopair's blink-the-delimiter delay."
  508. :group 'autopair
  509. :type 'float)
  510. (defun autopair--document-bindings (&optional fallback-keys)
  511. (concat
  512. "Works by scheduling possible autopair behaviour, then calls
  513. original command as if autopair didn't exist"
  514. (when (eq this-command 'describe-key)
  515. (let* ((autopair--emulation-alist nil)
  516. (command (or (key-binding (this-single-command-keys))
  517. (key-binding fallback-keys))))
  518. (when command
  519. (format ", which in this case is `%s'" command))))
  520. "."))
  521. (defun autopair--escaped-p (syntax-info)
  522. (nth 5 syntax-info))
  523. (defun autopair--exception-p (where-sym exception-where-sym blacklist &optional fn)
  524. (and (or (eq exception-where-sym :everywhere)
  525. (eq exception-where-sym where-sym))
  526. (member autopair-inserted
  527. (if fn
  528. (mapcar fn (cl-getf blacklist exception-where-sym))
  529. (cl-getf blacklist exception-where-sym)))))
  530. (defun autopair--find-pair (direction)
  531. "Compute (MATCHED START END) for the pair of the delimiter at point.
  532. With positive DIRECTION consider the delimiter after point and
  533. travel forward, otherwise consider the delimiter is just before
  534. point and travel backward."
  535. (let* ((show-paren-data (and nil
  536. (funcall show-paren-data-function)))
  537. (here (point)))
  538. (cond (show-paren-data
  539. (cl-destructuring-bind (here-beg here-end there-beg there-end mismatch)
  540. show-paren-data
  541. (if (cl-plusp direction)
  542. (list (not mismatch) there-end here-beg)
  543. (list (not mismatch) there-beg here-end))))
  544. (t
  545. (condition-case move-err
  546. (save-excursion
  547. (forward-sexp (if (cl-plusp direction) 1 -1))
  548. (list (if (cl-plusp direction)
  549. (eq (char-after here)
  550. (autopair--pair-of (char-before (point))))
  551. (eq (char-before here)
  552. (autopair--pair-of (char-after (point)))))
  553. (point) here))
  554. (scan-error
  555. (list nil (nth 2 move-err) here)))))))
  556. (defun autopair--up-list (&optional n)
  557. "Try to up-list forward as much as N lists.
  558. With negative N, up-list backward.
  559. Return a cons of two descritions (MATCHED START END) for the
  560. innermost and outermost lists that enclose point. The outermost
  561. list enclosing point is either the first top-level or mismatched
  562. list found by uplisting."
  563. (save-excursion
  564. (cl-loop with n = (or n (point-max))
  565. for i from 0 below (abs n)
  566. with outermost
  567. with innermost
  568. until outermost
  569. do
  570. (condition-case forward-err
  571. (progn
  572. (scan-sexps (point) (if (cl-plusp n)
  573. (point-max)
  574. (- (point-max))))
  575. (unless innermost
  576. (setq innermost (list t)))
  577. (setq outermost (list t)))
  578. (scan-error
  579. (goto-char
  580. (if (cl-plusp n)
  581. ;; HACK: the reason for this `max' is that some
  582. ;; modes like ruby-mode sometimes mis-report the
  583. ;; scan error when `forward-sexp'eeing too-much, its
  584. ;; (nth 3) should at least one greater than its (nth
  585. ;; 2). We really need to move out of the sexp so
  586. ;; detect this and add 1. If this were fixed we
  587. ;; could move to (nth 3 forward-err) in all
  588. ;; situations.
  589. ;;
  590. (max (1+ (nth 2 forward-err))
  591. (nth 3 forward-err))
  592. (nth 3 forward-err)))
  593. (let ((pair-data (autopair--find-pair (- n))))
  594. (unless innermost
  595. (setq innermost pair-data))
  596. (unless (cl-first pair-data)
  597. (setq outermost pair-data)))))
  598. finally (return (cons innermost outermost)))))
  599. ;; interactive commands and their associated predicates
  600. ;;
  601. (defun autopair-insert-or-skip-quote ()
  602. (interactive)
  603. (setq autopair-inserted (autopair--calculate-inserted))
  604. (let* ((syntax-triplet (autopair--syntax-ppss))
  605. (syntax-info (cl-first syntax-triplet))
  606. (where-sym (cl-second syntax-triplet))
  607. (orig-info (cl-third syntax-triplet))
  608. ;; inside-string may the quote character itself or t if this
  609. ;; is a "generically terminated string"
  610. (inside-string (and (eq where-sym :string)
  611. (cl-fourth orig-info)))
  612. (escaped-p (autopair--escaped-p syntax-info))
  613. )
  614. (cond (;; decides whether to skip the quote...
  615. ;;
  616. (and (not escaped-p)
  617. (eq autopair-inserted (char-after (point)))
  618. (or
  619. ;; ... if we're already inside a string and the
  620. ;; string starts with the character just inserted,
  621. ;; or it's a generically terminated string
  622. (and inside-string
  623. (or (eq inside-string t)
  624. (eq autopair-inserted inside-string)))
  625. ;; ... if we're in a comment and ending a string
  626. ;; (the inside-string criteria does not work
  627. ;; here...)
  628. (and (eq where-sym :comment)
  629. (condition-case nil
  630. (eq autopair-inserted (char-after (scan-sexps (1+ (point)) -1)))
  631. (scan-error nil)))))
  632. (setq autopair-action (list 'skip-quote autopair-inserted (point))))
  633. (;; decides whether to pair, i.e do *not* pair the quote if...
  634. ;;
  635. (not
  636. (or
  637. escaped-p
  638. ;; ... inside a generic string
  639. (eq inside-string t)
  640. ;; ... inside an unterminated string started by this char
  641. (autopair--in-unterminated-string-p syntax-triplet)
  642. ;; ... the position at the end of buffer is inside an
  643. ;; unterminated string
  644. (autopair--in-unterminated-string-p (save-excursion
  645. (goto-char (point-max))
  646. (autopair--syntax-ppss)))
  647. ;; ... comment-disable or string-disable are true at
  648. ;; point. The latter is only useful if we're in a string
  649. ;; terminated by a character other than
  650. ;; `autopair-inserted'.
  651. (cl-some #'(lambda (sym)
  652. (autopair--exception-p where-sym sym autopair-dont-pair))
  653. '(:comment :string))))
  654. (setq autopair-action (list 'insert-quote autopair-inserted (point)))))
  655. (autopair--fallback)))
  656. (put 'autopair-insert-or-skip-quote 'function-documentation
  657. '(concat "Insert or possibly skip over a quoting character.\n\n"
  658. (autopair--document-bindings)))
  659. (defun autopair--in-unterminated-string-p (autopair-triplet)
  660. (let* ((relevant-ppss (cl-third autopair-triplet))
  661. (string-delim (cl-fourth relevant-ppss)))
  662. (and (or (eq t string-delim)
  663. (eq autopair-inserted string-delim))
  664. (condition-case nil (progn (scan-sexps (cl-ninth relevant-ppss) 1) nil) (scan-error t)))))
  665. (defun autopair-insert-opening ()
  666. (interactive)
  667. (setq autopair-inserted (autopair--calculate-inserted))
  668. (when (autopair--pair-p)
  669. (setq autopair-action (list 'opening (autopair--pair-of autopair-inserted) (point))))
  670. (autopair--fallback))
  671. (put 'autopair-insert-opening 'function-documentation
  672. '(concat "Insert opening delimiter and possibly automatically close it.\n\n"
  673. (autopair--document-bindings)))
  674. (defun autopair-skip-close-maybe ()
  675. (interactive)
  676. (setq autopair-inserted (autopair--calculate-inserted))
  677. (when (autopair--skip-p)
  678. (setq autopair-action (list 'closing (autopair--pair-of autopair-inserted) (point))))
  679. (autopair--fallback))
  680. (put 'autopair-skip-close-maybe 'function-documentation
  681. '(concat "Insert or possibly skip over a closing delimiter.\n\n"
  682. (autopair--document-bindings)))
  683. (defun autopair-backspace ()
  684. (interactive)
  685. (setq autopair-inserted (autopair--calculate-inserted))
  686. (when (char-before)
  687. (setq autopair-action (list 'backspace (autopair--pair-of (char-before) 'closing) (point))))
  688. (autopair--fallback (kbd "DEL")))
  689. (put 'autopair-backspace 'function-documentation
  690. '(concat "Possibly delete a pair of paired delimiters.\n\n"
  691. (autopair--document-bindings (kbd "DEL"))))
  692. (defun autopair-newline ()
  693. (interactive)
  694. (setq autopair-inserted (autopair--calculate-inserted))
  695. (let ((pair (autopair--pair-of (char-before))))
  696. (when (and pair
  697. (eq (char-syntax pair) ?\))
  698. (eq (char-after) pair))
  699. (setq autopair-action (list 'newline pair (point))))
  700. (autopair--fallback (kbd "RET"))))
  701. (put 'autopair-newline 'function-documentation
  702. '(concat "Do a smart newline when right between parenthesis.\n
  703. In other words, insert an extra newline along with the one inserted normally
  704. by this command. Then place point after the first, indented.\n\n"
  705. (autopair--document-bindings (kbd "RET"))))
  706. (defun autopair--skip-p ()
  707. (let* ((syntax-triplet (autopair--syntax-ppss))
  708. (syntax-info (cl-first syntax-triplet))
  709. (orig-point (point)))
  710. (cond ((eq autopair-skip-criteria 'help-balance)
  711. (cl-destructuring-bind (innermost . outermost)
  712. (autopair--up-list (- (point-max)))
  713. (cond ((cl-first outermost)
  714. (cl-first innermost))
  715. ((cl-first innermost)
  716. (not (eq (autopair--pair-of (char-after (cl-third outermost)))
  717. autopair-inserted))))))
  718. ((eq autopair-skip-criteria 'need-opening)
  719. (save-excursion
  720. (condition-case err
  721. (progn
  722. (backward-list)
  723. t)
  724. (scan-error nil))))
  725. (t
  726. t))))
  727. (defun autopair--pair-p ()
  728. (let* ((syntax-triplet (autopair--syntax-ppss))
  729. (syntax-info (cl-first syntax-triplet))
  730. (where-sym (cl-second syntax-triplet))
  731. (orig-point (point)))
  732. (and (not (cl-some #'(lambda (sym)
  733. (autopair--exception-p where-sym sym autopair-dont-pair))
  734. '(:string :comment :code :everywhere)))
  735. (cond ((eq autopair-pair-criteria 'help-balance)
  736. (and (not (autopair--escaped-p syntax-info))
  737. (cl-destructuring-bind (innermost . outermost)
  738. (autopair--up-list (point-max))
  739. (cond ((cl-first outermost)
  740. t)
  741. ((not (cl-first innermost))
  742. (not (eq (autopair--pair-of (char-before (cl-third outermost)))
  743. autopair-inserted)))))))
  744. ((eq autopair-pair-criteria 'always)
  745. t)
  746. (t
  747. (not (autopair--escaped-p syntax-info)))))))
  748. ;; post-command-hook stuff
  749. ;;
  750. (defun autopair--post-command-handler ()
  751. "Performs pairing and wrapping based on `autopair-action' and
  752. `autopair-wrap-action'. "
  753. (when (and autopair-wrap-action
  754. (cl-notany #'null autopair-wrap-action))
  755. (if autopair-handle-wrap-action-fns
  756. (condition-case err
  757. (mapc #'(lambda (fn)
  758. (apply fn autopair-wrap-action))
  759. autopair-handle-wrap-action-fns)
  760. (scan-error (progn
  761. (message "[autopair] error running custom `autopair-handle-wrap-action-fns', switching autopair off")
  762. (autopair-mode -1))))
  763. (apply #'autopair-default-handle-wrap-action autopair-wrap-action))
  764. (setq autopair-wrap-action nil))
  765. (when (and autopair-action
  766. (cl-notany #'null autopair-action))
  767. (if autopair-handle-action-fns
  768. (condition-case err
  769. (mapc #'(lambda (fn)
  770. (funcall fn (cl-first autopair-action) (cl-second autopair-action) (cl-third autopair-action)))
  771. autopair-handle-action-fns)
  772. (scan-error (progn
  773. (message "[autopair] error running custom `autopair-handle-action-fns', switching autopair off")
  774. (autopair-mode -1))))
  775. (apply #'autopair-default-handle-action autopair-action))
  776. (setq autopair-action nil)))
  777. (defun autopair--blink-matching-open ()
  778. (let ((blink-matching-paren autopair-blink)
  779. (show-paren-mode nil)
  780. (blink-matching-delay autopair-blink-delay))
  781. (blink-matching-open)))
  782. (defun autopair--blink (&optional pos)
  783. (when autopair-blink
  784. (if pos
  785. (save-excursion
  786. (goto-char pos)
  787. (sit-for autopair-blink-delay))
  788. (sit-for autopair-blink-delay))))
  789. (defun autopair-default-handle-action (action pair pos-before)
  790. ;;(message "action is %s" action)
  791. (condition-case err
  792. (cond (;; automatically insert closing delimiter
  793. (and (eq 'opening action)
  794. (not (eq pair (char-before))))
  795. (insert pair)
  796. (autopair--blink)
  797. (backward-char 1))
  798. (;; automatically insert closing quote delimiter
  799. (eq 'insert-quote action)
  800. (insert pair)
  801. (autopair--blink)
  802. (backward-char 1))
  803. (;; automatically skip oper closer quote delimiter
  804. (and (eq 'skip-quote action)
  805. (eq pair (char-after (point))))
  806. (delete-char 1)
  807. (autopair--blink-matching-open))
  808. (;; skip over newly-inserted-but-existing closing delimiter
  809. ;; (normal case)
  810. (eq 'closing action)
  811. (let ((skipped 0))
  812. (when autopair-skip-whitespace
  813. (setq skipped (save-excursion (skip-chars-forward "\s\n\t"))))
  814. (when (eq autopair-inserted (char-after (+ (point) skipped)))
  815. (backward-delete-char 1)
  816. (unless (zerop skipped) (autopair--blink (+ (point) skipped)))
  817. (if (eq autopair-skip-whitespace 'chomp)
  818. (delete-char skipped)
  819. (forward-char skipped))
  820. (forward-char))
  821. (autopair--blink-matching-open)))
  822. (;; autodelete closing delimiter
  823. (and (eq 'backspace action)
  824. (eq pair (char-after (point))))
  825. (delete-char 1))
  826. (;; opens an extra line after point, then indents
  827. (and (eq 'newline action)
  828. (eq pair (char-after (point))))
  829. (save-excursion
  830. (newline-and-indent))
  831. (indent-according-to-mode)))
  832. (error
  833. (message "[autopair] Ignored error in `autopair-default-handle-action'"))))
  834. (defun autopair-default-handle-wrap-action (action pair pos-before region-before)
  835. "Default handler for the wrapping action in `autopair-wrap'"
  836. (condition-case err
  837. (when (eq 'wrap action)
  838. (let ((delete-active-region nil))
  839. (cond
  840. ((member autopair--this-command '(autopair-insert-opening
  841. autopair-extra-insert-opening))
  842. (goto-char (1+ (cdr region-before)))
  843. (insert pair)
  844. (autopair--blink)
  845. (goto-char (1+ (car region-before))))
  846. (;; wraps
  847. (member autopair--this-command '(autopair-skip-close-maybe
  848. autopair-extra-skip-close-maybe))
  849. (delete-char -1)
  850. (insert pair)
  851. (goto-char (1+ (cdr region-before)))
  852. (insert autopair-inserted))
  853. ((member autopair--this-command '(autopair-insert-or-skip-quote
  854. autopair-insert-or-skip-paired-delimiter))
  855. (goto-char (1+ (cdr region-before)))
  856. (insert pair)
  857. (autopair--blink))
  858. (t
  859. (delete-char -1)
  860. (goto-char (cdr region-before))
  861. (insert autopair-inserted)))
  862. (setq autopair-action nil)))
  863. (error
  864. (message "[autopair] Ignored error in `autopair-default-handle-wrap-action'"))))
  865. ;; example python triple quote helper
  866. ;;
  867. (defun autopair-python-triple-quote-action (action pair pos-before)
  868. (cond ((and (eq 'insert-quote action)
  869. (>= (point) 3)
  870. (string= (buffer-substring (- (point) 3)
  871. (point))
  872. (make-string 3 pair)))
  873. (save-excursion (insert (make-string 2 pair))))
  874. ((and (eq 'backspace action)
  875. (>= (point) 2)
  876. (<= (point) (- (point-max) 2))
  877. (string= (buffer-substring (- (point) 2)
  878. (+ (point) 2))
  879. (make-string 4 pair)))
  880. (delete-region (- (point) 2)
  881. (+ (point) 2)))
  882. ((and (eq 'skip-quote action)
  883. (<= (point) (- (point-max) 2))
  884. (string= (buffer-substring (point)
  885. (+ (point) 2))
  886. (make-string 2 pair)))
  887. (forward-char 2))
  888. (t
  889. t)))
  890. ;; example latex paired-delimiter helper
  891. ;;
  892. (defun autopair-latex-mode-paired-delimiter-action (action pair pos-before)
  893. "Pair or skip latex's \"paired delimiter\" syntax in math mode. Added AucText support, thanks Massimo Lauria"
  894. (when (eq action 'paired-delimiter)
  895. (when (eq (char-before) pair)
  896. (if (and (or
  897. (eq (get-text-property pos-before 'face) 'tex-math)
  898. (eq (get-text-property (- pos-before 1) 'face) 'font-latex-math-face)
  899. (member 'font-latex-math-face (get-text-property (- pos-before 1) 'face)))
  900. (eq (char-after) pair))
  901. (cond ((and (eq (char-after) pair)
  902. (eq (char-after (1+ (point))) pair))
  903. ;; double skip
  904. (delete-char 1)
  905. (forward-char))
  906. ((eq (char-before pos-before) pair)
  907. ;; doube insert
  908. (insert pair)
  909. (backward-char))
  910. (t
  911. ;; simple skip
  912. (delete-char 1)))
  913. (insert pair)
  914. (backward-char)))))
  915. ;; Commands and predicates for the autopair-extra* feature
  916. ;;
  917. (defun autopair-extra-insert-opening ()
  918. (interactive)
  919. (setq autopair-inserted (autopair--calculate-inserted))
  920. (when (autopair--extra-pair-p)
  921. (setq autopair-action (list 'opening (autopair--pair-of autopair-inserted) (point))))
  922. (autopair--fallback))
  923. (put 'autopair-extra-insert-opening 'function-documentation
  924. '(concat "Insert (an extra) opening delimiter and possibly automatically close it.\n\n"
  925. (autopair--document-bindings)))
  926. (defun autopair-extra-skip-close-maybe ()
  927. (interactive)
  928. (setq autopair-inserted (autopair--calculate-inserted))
  929. (when (autopair--extra-skip-p)
  930. (setq autopair-action (list 'closing autopair-inserted (point))))
  931. (autopair--fallback))
  932. (put 'autopair-extra-skip-close-maybe 'function-documentation
  933. '(concat "Insert or possibly skip over a (and extra) closing delimiter.\n\n"
  934. (autopair--document-bindings)))
  935. (defun autopair--extra-pair-p ()
  936. (let* ((syntax-triplet (autopair--syntax-ppss))
  937. (syntax-info (cl-first syntax-triplet))
  938. (where-sym (cl-second syntax-triplet)))
  939. (cl-some #'(lambda (sym)
  940. (autopair--exception-p where-sym sym autopair-extra-pairs #'car))
  941. '(:everywhere :comment :string :code))))
  942. (defun autopair--extra-skip-p ()
  943. (let* ((syntax-triplet (autopair--syntax-ppss))
  944. (syntax-info (cl-first syntax-triplet))
  945. (where-sym (cl-second syntax-triplet))
  946. (orig-point (point)))
  947. (and (eq (char-after (point)) autopair-inserted)
  948. (cl-some #'(lambda (sym)
  949. (autopair--exception-p where-sym sym autopair-extra-pairs #'cdr))
  950. '(:comment :string :code :everywhere))
  951. (save-excursion
  952. (condition-case err
  953. (backward-sexp (point-max))
  954. (scan-error
  955. (goto-char (cl-third err))))
  956. (search-forward (make-string 1 (autopair--pair-of autopair-inserted))
  957. orig-point
  958. 'noerror)))))
  959. ;; Commands and tex-mode specific handler functions for the "paired
  960. ;; delimiter" syntax class.
  961. ;;
  962. (defun autopair-insert-or-skip-paired-delimiter ()
  963. " insert or skip a character paired delimiter"
  964. (interactive)
  965. (setq autopair-inserted (autopair--calculate-inserted))
  966. (setq autopair-action (list 'paired-delimiter autopair-inserted (point)))
  967. (autopair--fallback))
  968. (put 'autopair-insert-or-skip-paired-delimiter 'function-documentation
  969. '(concat "Insert or possibly skip over a character with a syntax-class of \"paired delimiter\"."
  970. (autopair--document-bindings)))
  971. ;; monkey-patching: Compatibility with delete-selection-mode and cua-mode
  972. ;;
  973. ;; Ideally one would be able to use functions as the value of the
  974. ;; 'delete-selection properties of the autopair commands. The function
  975. ;; would return non-nil when no wrapping should/could be performed.
  976. ;;
  977. ;; Until then use some `defadvice' i.e. monkey-patching, which relies
  978. ;; on these features' implementation details.
  979. ;;
  980. (put 'autopair-insert-opening 'delete-selection t)
  981. (put 'autopair-skip-close-maybe 'delete-selection t)
  982. (put 'autopair-insert-or-skip-quote 'delete-selection t)
  983. (put 'autopair-extra-insert-opening 'delete-selection t)
  984. (put 'autopair-extra-skip-close-maybe 'delete-selection t)
  985. (put 'autopair-backspace 'delete-selection 'supersede)
  986. (put 'autopair-newline 'delete-selection t)
  987. (defun autopair--should-autowrap ()
  988. (and autopair-mode
  989. (not (eq this-command 'autopair-backspace))
  990. (symbolp this-command)
  991. (string-match "^autopair" (symbol-name this-command))
  992. (autopair--calculate-wrap-action)))
  993. (defadvice cua--pre-command-handler-1 (around autopair-override activate)
  994. "Don't actually do anything if autopair is about to autowrap. "
  995. (unless (autopair--should-autowrap) ad-do-it))
  996. (defadvice delete-selection-pre-hook (around autopair-override activate)
  997. "Don't actually do anything if autopair is about to autowrap. "
  998. (unless (autopair--should-autowrap) ad-do-it))
  999. (provide 'autopair)
  1000. ;; Local Variables:
  1001. ;; coding: utf-8
  1002. ;; End:
  1003. ;;; autopair.el ends here