skeleton.el 22 KB

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