skeleton.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. ;;; skeleton.el --- Lisp language extension for writing statement skeletons
  2. ;; Copyright (C) 1993-1996, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: extensions, abbrev, languages, tools
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; A very concise language extension for writing structured statement
  19. ;; skeleton insertion commands for programming language modes. This
  20. ;; originated in shell-script mode and was applied to ada-mode's
  21. ;; commands which shrunk to one third. And these commands are now
  22. ;; user configurable.
  23. ;;; Code:
  24. (eval-when-compile (require 'cl-lib))
  25. ;; page 1: statement skeleton language definition & interpreter
  26. ;; page 2: paired insertion
  27. ;; page 3: mirror-mode, an example for setting up paired insertion
  28. (defvar skeleton-transformation-function 'identity
  29. "If non-nil, function applied to literal strings before they are inserted.
  30. It should take strings and characters and return them transformed, or nil
  31. which means no transformation.
  32. Typical examples might be `upcase' or `capitalize'.")
  33. (defvaralias 'skeleton-transformation 'skeleton-transformation-function)
  34. ; this should be a fourth argument to defvar
  35. (put 'skeleton-transformation-function 'variable-interactive
  36. "aTransformation function: ")
  37. (defvar skeleton-autowrap t
  38. "Controls wrapping behavior of functions created with `define-skeleton'.
  39. When the region is visible (due to `transient-mark-mode' or marking a region
  40. with the mouse) and this is non-nil and the function was called without an
  41. explicit ARG, then the ARG defaults to -1, i.e. wrapping around the visible
  42. region.")
  43. (make-obsolete-variable 'skeleton-autowrap nil "24.5")
  44. (defvar skeleton-end-newline t
  45. "If non-nil, make sure that the skeleton inserted ends with a newline.")
  46. (defvar skeleton-end-hook nil
  47. "Hook called at end of skeleton but before going to point of interest.
  48. The variables `v1' and `v2' are still set when calling this.")
  49. ;;;###autoload
  50. (defvar skeleton-filter-function 'identity
  51. "Function for transforming a skeleton proxy's aliases' variable value.")
  52. (defvaralias 'skeleton-filter 'skeleton-filter-function)
  53. (defvar skeleton-untabify nil ; bug#12223
  54. "When non-nil untabifies when deleting backwards with element -ARG.")
  55. (defvar skeleton-newline-indent-rigidly nil
  56. "When non-nil, indent rigidly under current line for element `\\n'.
  57. Else use mode's `indent-line-function'.")
  58. (defvar-local skeleton-further-elements ()
  59. "A buffer-local varlist (see `let') of mode specific skeleton elements.
  60. These variables are bound while interpreting a skeleton. Their value may
  61. in turn be any valid skeleton element if they are themselves to be used as
  62. skeleton elements.")
  63. (defvar skeleton-subprompt
  64. (substitute-command-keys
  65. "RET, \\<minibuffer-local-map>\\[abort-recursive-edit] or \\[help-command]")
  66. "Replacement for %s in prompts of recursive subskeletons.")
  67. (defvar skeleton-debug nil
  68. "If non-nil `define-skeleton' will override previous definition.")
  69. (defvar skeleton-positions nil
  70. "List of positions marked with @, after skeleton insertion.
  71. The list describes the most recent skeleton insertion, and its elements
  72. are integer buffer positions in the reverse order of the insertion order.")
  73. ;; reduce the number of compiler warnings
  74. (defvar skeleton-il)
  75. (defvar skeleton-modified)
  76. (defvar skeleton-point)
  77. (defvar skeleton-regions)
  78. (def-edebug-spec skeleton-edebug-spec
  79. ([&or null stringp (stringp &rest stringp) [[&not atom] def-form]]
  80. &rest &or "n" "_" "-" ">" "@" "&" "!" "resume:"
  81. ("quote" def-form) skeleton-edebug-spec def-form))
  82. ;;;###autoload
  83. (defmacro define-skeleton (command documentation &rest skeleton)
  84. "Define a user-configurable COMMAND that enters a statement skeleton.
  85. DOCUMENTATION is that of the command.
  86. SKELETON is as defined under `skeleton-insert'."
  87. (declare (doc-string 2) (debug (&define name stringp skeleton-edebug-spec)))
  88. (if skeleton-debug
  89. (set command skeleton))
  90. `(progn
  91. ;; Tell self-insert-command that this function, if called by an
  92. ;; abbrev, should cause the self-insert to be skipped.
  93. (put ',command 'no-self-insert t)
  94. (defun ,command (&optional str arg)
  95. ,(concat documentation
  96. (if (string-match "\n\\'" documentation)
  97. "" "\n")
  98. "\n"
  99. "This is a skeleton command (see `skeleton-insert').
  100. Normally the skeleton text is inserted at point, with nothing \"inside\".
  101. If there is a highlighted region, the skeleton text is wrapped
  102. around the region text.
  103. A prefix argument ARG says to wrap the skeleton around the next ARG words.
  104. A prefix argument of -1 says to wrap around region, even if not highlighted.
  105. A prefix argument of zero says to wrap around zero words---that is, nothing.
  106. This is a way of overriding the use of a highlighted region.")
  107. (interactive "*P\nP")
  108. (skeleton-proxy-new ',skeleton str arg))))
  109. ;;;###autoload
  110. (defun skeleton-proxy-new (skeleton &optional str arg)
  111. "Insert SKELETON.
  112. Prefix ARG allows wrapping around words or regions (see `skeleton-insert').
  113. If no ARG was given, but the region is visible, ARG defaults to -1 depending
  114. on `skeleton-autowrap'. An ARG of M-0 will prevent this just for once.
  115. This command can also be an abbrev expansion (3rd and 4th columns in
  116. \\[edit-abbrevs] buffer: \"\" command-name).
  117. Optional second argument STR may also be a string which will be the value
  118. of `str' whereas the skeleton's interactor is then ignored."
  119. (skeleton-insert (funcall skeleton-filter-function skeleton)
  120. ;; Pretend C-x a e passed its prefix arg to us
  121. (if (or arg current-prefix-arg)
  122. (prefix-numeric-value (or arg
  123. current-prefix-arg))
  124. (and skeleton-autowrap
  125. (or (eq last-command 'mouse-drag-region)
  126. (and transient-mark-mode mark-active))
  127. ;; Deactivate the mark, in case one of the
  128. ;; elements of the skeleton is sensitive
  129. ;; to such situations (e.g. it is itself a
  130. ;; skeleton).
  131. (progn (deactivate-mark)
  132. -1)))
  133. (if (stringp str)
  134. str))
  135. ;; Return non-nil to tell expand-abbrev that expansion has happened.
  136. ;; Otherwise the no-self-insert is ignored.
  137. t)
  138. ;;;###autoload
  139. (defun skeleton-insert (skeleton &optional regions str)
  140. "Insert the complex statement skeleton SKELETON describes very concisely.
  141. With optional second argument REGIONS, wrap first interesting point
  142. \(`_') in skeleton around next REGIONS words, if REGIONS is positive.
  143. If REGIONS is negative, wrap REGIONS preceding interregions into first
  144. REGIONS interesting positions (successive `_'s) in skeleton.
  145. An interregion is the stretch of text between two contiguous marked
  146. points. If you marked A B C [] (where [] is the cursor) in
  147. alphabetical order, the 3 interregions are simply the last 3 regions.
  148. But if you marked B A [] C, the interregions are B-A, A-[], []-C.
  149. The optional third argument STR, if specified, is the value for the
  150. variable `str' within the skeleton. When this is non-nil, the
  151. interactor gets ignored, and this should be a valid skeleton element.
  152. When done with skeleton, but before going back to `_'-point, add
  153. a newline (unless `skeleton-end-newline' is nil) and run the hook
  154. `skeleton-end-hook'.
  155. SKELETON is made up as (INTERACTOR ELEMENT ...). INTERACTOR may be nil if
  156. not needed, a prompt-string or an expression for complex read functions.
  157. If ELEMENT is a string or a character it gets inserted (see also
  158. `skeleton-transformation-function'). Other possibilities are:
  159. \\n go to next line and indent according to mode, unless
  160. this is the first/last element of a skeleton and point
  161. is at bol/eol
  162. _ interesting point, interregion here
  163. - interesting point, no interregion interaction, overrides
  164. interesting point set by _
  165. > indent line (or interregion if > _) according to major mode
  166. @ add position to `skeleton-positions'
  167. & do next ELEMENT if previous moved point
  168. | do next ELEMENT if previous didn't move point
  169. -NUM delete NUM preceding characters (see `skeleton-untabify')
  170. resume: skipped, continue here if quit is signaled
  171. nil skipped
  172. After termination, point will be positioned at the last occurrence of -
  173. or at the first occurrence of _ or at the end of the inserted text.
  174. Note that \\n as the last element of the skeleton only inserts a
  175. newline if not at eol. If you want to unconditionally insert a newline
  176. at the end of the skeleton, use \"\\n\" instead. Likewise with \\n
  177. as the first element when at bol.
  178. Further elements can be defined via `skeleton-further-elements'.
  179. ELEMENT may itself be a SKELETON with an INTERACTOR. The user is prompted
  180. repeatedly for different inputs. The SKELETON is processed as often as
  181. the user enters a non-empty string. \\[keyboard-quit] terminates skeleton insertion, but
  182. continues after `resume:' and positions at `_' if any. If INTERACTOR in
  183. such a subskeleton is a prompt-string which contains a \".. %s ..\" it is
  184. formatted with `skeleton-subprompt'. Such an INTERACTOR may also be a list
  185. of strings with the subskeleton being repeated once for each string.
  186. Quoted Lisp expressions are evaluated for their side-effects.
  187. Other Lisp expressions are evaluated and the value treated as above.
  188. Note that expressions may not return t since this implies an
  189. endless loop. Modes can define other symbols by locally setting them
  190. to any valid skeleton element. The following local variables are
  191. available:
  192. str first time: read a string according to INTERACTOR
  193. then: insert previously read string once more
  194. help help-form during interaction with the user or nil
  195. input initial input (string or cons with index) while reading str
  196. v1, v2 local variables for memorizing anything you want"
  197. (let ((skeleton-regions regions))
  198. (and skeleton-regions
  199. (setq skeleton-regions
  200. (if (> skeleton-regions 0)
  201. (list (copy-marker (point) t)
  202. (save-excursion (forward-word-strictly
  203. skeleton-regions)
  204. (point-marker)))
  205. (setq skeleton-regions (- skeleton-regions))
  206. ;; copy skeleton-regions - 1 elements from `mark-ring'
  207. (let ((l1 (cons (mark-marker) mark-ring))
  208. (l2 (list (copy-marker (point) t))))
  209. (while (and l1 (> skeleton-regions 0))
  210. (push (copy-marker (pop l1) t) l2)
  211. (setq skeleton-regions (1- skeleton-regions)))
  212. (sort l2 '<))))
  213. (goto-char (car skeleton-regions))
  214. (setq skeleton-regions (cdr skeleton-regions)))
  215. (let ((beg (point))
  216. skeleton-modified skeleton-point resume: help input v1 v2)
  217. (setq skeleton-positions nil)
  218. (unwind-protect
  219. (cl-progv
  220. (mapcar #'car skeleton-further-elements)
  221. (mapcar (lambda (x) (eval (cadr x))) skeleton-further-elements)
  222. (skeleton-internal-list skeleton str))
  223. (or (eolp) (not skeleton-end-newline) (newline-and-indent))
  224. (run-hooks 'skeleton-end-hook)
  225. (sit-for 0)
  226. (or (pos-visible-in-window-p beg)
  227. (progn
  228. (goto-char beg)
  229. (recenter 0)))
  230. (if skeleton-point
  231. (goto-char skeleton-point))))))
  232. (defun skeleton-read (prompt &optional initial-input recursive)
  233. "Function for reading a string from the minibuffer within skeletons.
  234. PROMPT must be a string or a function that evaluates to a string.
  235. It may also be a form that evaluates to a string (deprecated).
  236. It may contain a `%s' which will be replaced by `skeleton-subprompt'.
  237. If non-nil second arg INITIAL-INPUT or variable `input' is a string or
  238. cons with index to insert before reading. If third arg RECURSIVE is non-nil
  239. i.e. we are handling the iterator of a subskeleton, returns empty string if
  240. user didn't modify input.
  241. While reading, the value of `minibuffer-help-form' is variable `help' if that
  242. is non-nil or a default string."
  243. (let ((minibuffer-help-form (or (if (boundp 'help) (symbol-value 'help))
  244. (if recursive "\
  245. As long as you provide input you will insert another subskeleton.
  246. If you enter the empty string, the loop inserting subskeletons is
  247. left, and the current one is removed as far as it has been entered.
  248. If you quit, the current subskeleton is removed as far as it has been
  249. entered. No more of the skeleton will be inserted, except maybe for a
  250. syntactically necessary termination."
  251. "\
  252. You are inserting a skeleton. Standard text gets inserted into the buffer
  253. automatically, and you are prompted to fill in the variable parts.")))
  254. (eolp (eolp)))
  255. ;; since Emacs doesn't show main window's cursor, do something noticeable
  256. (or eolp
  257. ;; We used open-line before, but that can do a lot more than we want,
  258. ;; since it runs self-insert-command. E.g. it may remove spaces
  259. ;; before point.
  260. (save-excursion (insert "\n")))
  261. (unwind-protect
  262. (setq prompt (cond ((stringp prompt)
  263. (read-string (format prompt skeleton-subprompt)
  264. (setq initial-input
  265. (or initial-input
  266. (symbol-value 'input)))))
  267. ((functionp prompt)
  268. (funcall prompt))
  269. (t (eval prompt))))
  270. (or eolp
  271. (delete-char 1))))
  272. (if (and recursive
  273. (or (null prompt)
  274. (string= prompt "")
  275. (equal prompt initial-input)
  276. (equal prompt (car-safe initial-input))))
  277. (signal 'quit t)
  278. prompt))
  279. (defun skeleton-internal-list (skeleton-il &optional str recursive)
  280. (let* ((start (line-beginning-position))
  281. (column (current-column))
  282. (line (buffer-substring start (line-end-position)))
  283. opoint)
  284. (or str
  285. (setq str `(setq str
  286. (skeleton-read ',(car skeleton-il) nil ,recursive))))
  287. (when (and (eq (cadr skeleton-il) '\n) (not recursive)
  288. (save-excursion (skip-chars-backward " \t") (bolp)))
  289. (setq skeleton-il (cons nil (cons '> (cddr skeleton-il)))))
  290. (while (setq skeleton-modified (eq opoint (point))
  291. opoint (point)
  292. skeleton-il (cdr skeleton-il))
  293. (condition-case quit
  294. (skeleton-internal-1 (car skeleton-il) nil recursive)
  295. (quit
  296. (if (eq (cdr quit) 'recursive)
  297. (setq recursive 'quit
  298. skeleton-il (memq 'resume: skeleton-il))
  299. ;; Remove the subskeleton as far as it has been shown
  300. ;; the subskeleton shouldn't have deleted outside current line.
  301. (end-of-line)
  302. (delete-region start (point))
  303. (insert line)
  304. (move-to-column column)
  305. (if (cdr quit)
  306. (setq skeleton-il ()
  307. recursive nil)
  308. (signal 'quit 'recursive)))))))
  309. ;; maybe continue loop or go on to next outer resume: section
  310. (if (eq recursive 'quit)
  311. (signal 'quit 'recursive)
  312. recursive))
  313. (defun skeleton-internal-1 (element &optional literal recursive)
  314. (cond
  315. ((or (integerp element) (stringp element))
  316. (if (and (integerp element) ; -num
  317. (< element 0))
  318. (if skeleton-untabify
  319. (backward-delete-char-untabify (- element))
  320. (delete-char element))
  321. (insert (if (not literal)
  322. (funcall skeleton-transformation-function element)
  323. element))))
  324. ((or (eq element '\n) ; actually (eq '\n 'n)
  325. ;; The sequence `> \n' is handled specially so as to indent the first
  326. ;; line after inserting the newline (to get the proper indentation).
  327. (and (eq element '>) (eq (nth 1 skeleton-il) '\n) (pop skeleton-il)))
  328. (let ((pos (if (eq element '>) (point))))
  329. (cond
  330. ((and skeleton-regions (eq (nth 1 skeleton-il) '_))
  331. (or (eolp) (insert "\n"))
  332. (if pos (save-excursion (goto-char pos) (indent-according-to-mode)))
  333. (indent-region (line-beginning-position)
  334. (car skeleton-regions) nil))
  335. ;; \n as last element only inserts \n if not at eol.
  336. ((and (null (cdr skeleton-il)) (not recursive) (eolp))
  337. (if pos (indent-according-to-mode)))
  338. (skeleton-newline-indent-rigidly
  339. (let ((pt (point)))
  340. (insert "\n")
  341. (indent-to (save-excursion
  342. (goto-char pt)
  343. (if pos (indent-according-to-mode))
  344. (current-indentation)))))
  345. (t (if pos (reindent-then-newline-and-indent)
  346. (insert "\n")
  347. (indent-according-to-mode))))))
  348. ((eq element '>)
  349. (if (and skeleton-regions (eq (nth 1 skeleton-il) '_))
  350. (indent-region (line-beginning-position)
  351. (car skeleton-regions) nil)
  352. (indent-according-to-mode)))
  353. ((eq element '_)
  354. (if skeleton-regions
  355. (progn
  356. (goto-char (pop skeleton-regions))
  357. (and (<= (current-column) (current-indentation))
  358. (eq (nth 1 skeleton-il) '\n)
  359. (end-of-line 0)))
  360. (or skeleton-point
  361. (setq skeleton-point (point)))))
  362. ((eq element '-)
  363. (setq skeleton-point (point)))
  364. ((eq element '&)
  365. (when skeleton-modified (pop skeleton-il)))
  366. ((eq element '|)
  367. (unless skeleton-modified (pop skeleton-il)))
  368. ((eq element '@)
  369. (push (point) skeleton-positions))
  370. ((eq 'quote (car-safe element))
  371. (eval (nth 1 element)))
  372. ((and (consp element)
  373. (or (stringp (car element)) (listp (car element))))
  374. ;; Don't forget: `symbolp' is also true for nil.
  375. (if (symbolp (car-safe (car element)))
  376. (while (and (skeleton-internal-list element nil t)
  377. ;; If the interactor is nil, don't infinite loop.
  378. (car element)))
  379. (setq literal (car element))
  380. (while literal
  381. (skeleton-internal-list element (car literal))
  382. (setq literal (cdr literal)))))
  383. ((null element))
  384. (t (skeleton-internal-1 (eval element) t recursive))))
  385. ;; Maybe belongs into simple.el or elsewhere
  386. ;; ;;;###autoload
  387. ;; (define-skeleton local-variables-section
  388. ;; "Insert a local variables section. Use current comment syntax if any."
  389. ;; (completing-read "Mode: " obarray
  390. ;; (lambda (symbol)
  391. ;; (if (commandp symbol)
  392. ;; (string-match "-mode$" (symbol-name symbol))))
  393. ;; t)
  394. ;; '(save-excursion
  395. ;; (if (re-search-forward page-delimiter nil t)
  396. ;; (error "Not on last page")))
  397. ;; comment-start "Local Variables:" comment-end \n
  398. ;; comment-start "mode: " str
  399. ;; & -5 | '(kill-line 0) & -1 | comment-end \n
  400. ;; ( (completing-read (format "Variable, %s: " skeleton-subprompt)
  401. ;; obarray
  402. ;; (lambda (symbol)
  403. ;; (or (eq symbol 'eval)
  404. ;; (custom-variable-p symbol)))
  405. ;; t)
  406. ;; comment-start str ": "
  407. ;; (read-from-minibuffer "Expression: " nil read-expression-map nil
  408. ;; 'read-expression-history) | _
  409. ;; comment-end \n)
  410. ;; resume:
  411. ;; comment-start "End:" comment-end \n)
  412. ;; Variables and command for automatically inserting pairs like () or "".
  413. (defvar skeleton-pair nil
  414. "If this is nil pairing is turned off, no matter what else is set.
  415. Otherwise modes with `skeleton-pair-insert-maybe' on some keys
  416. will attempt to insert pairs of matching characters.")
  417. (defvar skeleton-pair-on-word nil
  418. "If this is nil, paired insertion is inhibited before or inside a word.")
  419. (defvar skeleton-pair-filter-function (lambda () nil)
  420. "Attempt paired insertion if this function returns nil, before inserting.
  421. This allows for context-sensitive checking whether pairing is appropriate.")
  422. (defvar skeleton-pair-alist ()
  423. "An override alist of pairing partners matched against `last-command-event'.
  424. Each alist element, which looks like (ELEMENT ...), is passed to
  425. `skeleton-insert' with no interactor. Variable `str' does nothing.
  426. Elements might be (?\\=` ?\\=` _ \"\\='\\='\"), (?\\( ? _ \" )\") or (?{ \\n > _ \\n ?} >).")
  427. (defvar skeleton-pair-default-alist '((?\( _ ?\)) (?\))
  428. (?\[ _ ?\]) (?\])
  429. (?{ _ ?}) (?\})
  430. (?< _ ?>) (?\>)
  431. (?« _ ?») (?\»)
  432. (?` _ ?')))
  433. ;;;###autoload
  434. (defun skeleton-pair-insert-maybe (arg)
  435. "Insert the character you type ARG times.
  436. With no ARG, if `skeleton-pair' is non-nil, pairing can occur. If the region
  437. is visible the pair is wrapped around it depending on `skeleton-autowrap'.
  438. Else, if `skeleton-pair-on-word' is non-nil or we are not before or inside a
  439. word, and if `skeleton-pair-filter-function' returns nil, pairing is performed.
  440. Pairing is also prohibited if we are right after a quoting character
  441. such as backslash.
  442. If a match is found in `skeleton-pair-alist', that is inserted, else
  443. the defaults are used. These are (), [], {}, <> and (grave
  444. accent, apostrophe) for the paired ones, and the same character
  445. twice for the others."
  446. (interactive "*P")
  447. (if (or arg (not skeleton-pair))
  448. (self-insert-command (prefix-numeric-value arg))
  449. (let* ((mark (and skeleton-autowrap
  450. (or (eq last-command 'mouse-drag-region)
  451. (and transient-mark-mode mark-active))))
  452. (char last-command-event)
  453. (skeleton (or (assq char skeleton-pair-alist)
  454. (assq char skeleton-pair-default-alist)
  455. `(,char _ ,char))))
  456. (if (or (memq (char-syntax (preceding-char)) '(?\\ ?/))
  457. (and (not mark)
  458. (or overwrite-mode
  459. (if (not skeleton-pair-on-word) (looking-at "\\w"))
  460. (funcall skeleton-pair-filter-function))))
  461. (self-insert-command (prefix-numeric-value arg))
  462. ;; Newlines not desirable for inserting pairs. See bug#16138.
  463. (let ((skeleton-end-newline nil))
  464. (skeleton-insert (cons nil skeleton) (if mark -1)))))))
  465. ;; A more serious example can be found in sh-script.el
  466. ;; (defun mirror-mode ()
  467. ;; "This major mode is an amusing little example of paired insertion.
  468. ;;All printable characters do a paired self insert, while the other commands
  469. ;;work normally."
  470. ;; (interactive)
  471. ;; (kill-all-local-variables)
  472. ;; (make-local-variable 'skeleton-pair)
  473. ;; (make-local-variable 'skeleton-pair-on-word)
  474. ;; (make-local-variable 'skeleton-pair-filter-function)
  475. ;; (make-local-variable 'skeleton-pair-alist)
  476. ;; (setq major-mode 'mirror-mode
  477. ;; mode-name "Mirror"
  478. ;; skeleton-pair-on-word t
  479. ;; ;; in the middle column insert one or none if odd window-width
  480. ;; skeleton-pair-filter-function (lambda ()
  481. ;; (if (>= (current-column)
  482. ;; (/ (window-width) 2))
  483. ;; ;; insert both on next line
  484. ;; (next-line 1)
  485. ;; ;; insert one or both?
  486. ;; (= (* 2 (1+ (current-column)))
  487. ;; (window-width))))
  488. ;; ;; mirror these the other way round as well
  489. ;; skeleton-pair-alist '((?) _ ?()
  490. ;; (?] _ ?[)
  491. ;; (?} _ ?{)
  492. ;; (?> _ ?<)
  493. ;; (?/ _ ?\\)
  494. ;; (?\\ _ ?/)
  495. ;; (?` ?` _ "''")
  496. ;; (?' ?' _ "``"))
  497. ;; ;; in this mode we exceptionally ignore the user, else it's no fun
  498. ;; skeleton-pair t)
  499. ;; (let ((map (make-vector 256 'skeleton-pair-insert-maybe))
  500. ;; (i 0))
  501. ;; (use-local-map `(keymap ,map))
  502. ;; (while (< i ? )
  503. ;; (aset map i nil)
  504. ;; (aset map (+ i 128) nil)
  505. ;; (setq i (1+ i))))
  506. ;; (run-mode-hooks 'mirror-mode-hook))
  507. (provide 'skeleton)
  508. ;;; skeleton.el ends here