tempo.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. ;;; tempo.el --- Flexible template insertion
  2. ;; Copyright (C) 1994-1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David K}gedal <davidk@lysator.liu.se>
  4. ;; Created: 16 Feb 1994
  5. ;; K}gedal's last version number: 1.2.4
  6. ;; Keywords: extensions, languages, tools
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file provides a simple way to define powerful templates, or
  20. ;; macros, if you wish. It is mainly intended for, but not limited to,
  21. ;; other programmers to be used for creating shortcuts for editing
  22. ;; certain kind of documents. It was originally written to be used by
  23. ;; a HTML editing mode written by Nelson Minar <nelson@santafe.edu>,
  24. ;; and his html-helper-mode.el is probably the best example of how to
  25. ;; use this program.
  26. ;; A template is defined as a list of items to be inserted in the
  27. ;; current buffer at point. Some of the items can be simple strings,
  28. ;; while other can control formatting or define special points of
  29. ;; interest in the inserted text.
  30. ;; If a template defines a "point of interest" that point is inserted
  31. ;; in a buffer-local list of "points of interest" that the user can
  32. ;; jump between with the commands `tempo-backward-mark' and
  33. ;; `tempo-forward-mark'. If the template definer provides a prompt for
  34. ;; the point, and the variable `tempo-interactive' is non-nil, the
  35. ;; user will be prompted for a string to be inserted in the buffer,
  36. ;; using the minibuffer.
  37. ;; The template can also define one point to be replaced with the
  38. ;; current region if the template command is called with a prefix (or
  39. ;; a non-nil argument).
  40. ;; More flexible templates can be created by including lisp symbols,
  41. ;; which will be evaluated as variables, or lists, which will be
  42. ;; evaluated as lisp expressions.
  43. ;; See the documentation for tempo-define-template for the different
  44. ;; items that can be used to define a tempo template.
  45. ;; One of the more powerful features of tempo templates are automatic
  46. ;; completion. With every template can be assigned a special tag that
  47. ;; should be recognized by `tempo-complete-tag' and expanded to the
  48. ;; complete template. By default the tags are added to a global list
  49. ;; of template tags, and are matched against the last word before
  50. ;; point. But if you assign your tags to a specific list, you can also
  51. ;; specify another method for matching text in the buffer against the
  52. ;; tags. In the HTML mode, for instance, the tags are matched against
  53. ;; the text between the last `<' and point.
  54. ;; When defining a template named `foo', a symbol named
  55. ;; `tempo-template-foo' will be created whose value as a variable will
  56. ;; be the template definition, and its function value will be an
  57. ;; interactive function that inserts the template at the point.
  58. ;; The latest tempo.el distribution can be fetched from
  59. ;; ftp.lysator.liu.se in the directory /pub/emacs
  60. ;; There is also a WWW page at
  61. ;; http://www.lysator.liu.se/~davidk/elisp/ which has some information
  62. ;;; Known bugs:
  63. ;; If the 'o is the first element in a template, strange things can
  64. ;; happen when the template is inserted at the beginning of a
  65. ;; line. This is due to strange behavior in open-line. But it should
  66. ;; be easily avoided.
  67. ;; The 'o tag is also a problem when including the region. This will
  68. ;; be looked into.
  69. ;; Clicking mouse-2 in the completion buffer gives strange results.
  70. ;; There is a bug in some emacs versions that prevents completion from
  71. ;; working. If it doesn't work for you, send me a note indicating your
  72. ;; emacs version and your problems.
  73. ;;; Contributors:
  74. ;; These people have given me important feedback and new ideas for
  75. ;; tempo.el. Thanks.
  76. ;; Nelson Minar <nelson@santafe.edu>
  77. ;; Richard Stallman <rms@gnu.org>
  78. ;; Lars Lindberg <Lars.Lindberg@sypro.cap.se>
  79. ;; Glen Whitney <Glen.Whitney@math.lsa.umich.edu>
  80. ;;; Code:
  81. ;;; User options
  82. (defgroup tempo nil
  83. "Flexible template insertion."
  84. :prefix "tempo-"
  85. :group 'tools)
  86. (defcustom tempo-interactive nil
  87. "Prompt user for strings in templates.
  88. If this variable is non-nil, `tempo-insert' prompts the
  89. user for text to insert in the templates."
  90. :type 'boolean
  91. :group 'tempo)
  92. (defcustom tempo-insert-region nil
  93. "Automatically insert current region when there is a `r' in the template
  94. If this variable is nil, `r' elements will be treated just like `p'
  95. elements, unless the template function is given a prefix (or a non-nil
  96. argument). If this variable is non-nil, the behavior is reversed.
  97. In Transient Mark mode, this option is unused."
  98. :type 'boolean
  99. :group 'tempo)
  100. (defcustom tempo-show-completion-buffer t
  101. "If non-nil, show a buffer with possible completions, when only
  102. a partial completion can be found."
  103. :type 'boolean
  104. :group 'tempo)
  105. (defcustom tempo-leave-completion-buffer nil
  106. "If nil, a completion buffer generated by \\[tempo-complete-tag]
  107. disappears at the next keypress; otherwise, it remains forever."
  108. :type 'boolean
  109. :group 'tempo)
  110. ;;; Internal variables
  111. (defvar tempo-insert-string-functions nil
  112. "List of functions to run when inserting a string.
  113. Each function is called with a single arg, STRING and should return
  114. another string. This could be used for making all strings upcase by
  115. setting it to '(upcase), for example.")
  116. (defvar tempo-tags nil
  117. "An association list with tags and corresponding templates.")
  118. (defvar tempo-local-tags '((tempo-tags . nil))
  119. "A list of locally installed tag completion lists.
  120. It is a association list where the car of every element is a symbol
  121. whose variable value is a template list. The cdr part, if non-nil,
  122. is a function or a regexp that defines the string to match. See the
  123. documentation for the function `tempo-complete-tag' for more info.
  124. `tempo-tags' is always in the last position in this list.")
  125. (defvar tempo-collection nil
  126. "A collection of all the tags defined for the current buffer.")
  127. (defvar tempo-dirty-collection t
  128. "Indicates if the tag collection needs to be rebuilt.")
  129. (defvar tempo-marks nil
  130. "A list of marks to jump to with `\\[tempo-forward-mark]' and `\\[tempo-backward-mark]'.")
  131. (defvar tempo-match-finder "\\b\\([[:word:]]+\\)\\="
  132. "The regexp or function used to find the string to match against tags.
  133. If `tempo-match-finder' is a string, it should contain a regular
  134. expression with at least one \\( \\) pair. When searching for tags,
  135. `tempo-complete-tag' calls `re-search-backward' with this string, and
  136. the string between the first \\( and \\) is used for matching against
  137. each string in the tag list. If one is found, the whole text between
  138. the first \\( and the point is replaced with the inserted template.
  139. You will probably want to include \\=\\= at the end of the regexp to
  140. make sure that the string is matched only against text adjacent to the
  141. point.
  142. If `tempo-match-finder' is a symbol, it should be a function that
  143. returns a pair of the form (STRING . POS), where STRING is the string
  144. used for matching and POS is the buffer position after which text
  145. should be replaced with a template.")
  146. (defvar tempo-user-elements nil
  147. "Element handlers for user-defined elements.
  148. A list of symbols which are bound to functions that take one argument.
  149. This function should return something to be sent to `tempo-insert' if
  150. it recognizes the argument, and nil otherwise.")
  151. (defvar tempo-named-insertions nil
  152. "Temporary storage for named insertions.")
  153. (defvar tempo-region-start (make-marker)
  154. "Region start when inserting around the region.")
  155. (defvar tempo-region-stop (make-marker)
  156. "Region stop when inserting around the region.")
  157. ;; Make some variables local to every buffer
  158. (make-variable-buffer-local 'tempo-marks)
  159. (make-variable-buffer-local 'tempo-local-tags)
  160. (make-variable-buffer-local 'tempo-match-finder)
  161. (make-variable-buffer-local 'tempo-collection)
  162. (make-variable-buffer-local 'tempo-dirty-collection)
  163. ;;; Functions
  164. ;;
  165. ;; tempo-define-template
  166. (defun tempo-define-template (name elements &optional tag documentation taglist)
  167. "Define a template.
  168. This function creates a template variable `tempo-template-NAME' and an
  169. interactive function `tempo-template-NAME' that inserts the template
  170. at the point. The created function is returned.
  171. NAME is a string that contains the name of the template, ELEMENTS is a
  172. list of elements in the template, TAG is the tag used for completion,
  173. DOCUMENTATION is the documentation string for the insertion command
  174. created, and TAGLIST (a symbol) is the tag list that TAG (if provided)
  175. should be added to. If TAGLIST is nil and TAG is non-nil, TAG is
  176. added to `tempo-tags'.
  177. The elements in ELEMENTS can be of several types:
  178. - A string: It is sent to the hooks in `tempo-insert-string-functions',
  179. and the result is inserted.
  180. - The symbol `p': This position is saved in `tempo-marks'.
  181. - The symbol `r': If `tempo-insert' is called with ON-REGION non-nil
  182. the current region is placed here. Otherwise it works like `p'.
  183. - (p PROMPT <NAME> <NOINSERT>): If `tempo-interactive' is non-nil, the
  184. user is prompted in the minibuffer with PROMPT for a string to be
  185. inserted. If the optional parameter NAME is non-nil, the text is
  186. saved for later insertion with the `s' tag. If there already is
  187. something saved under NAME that value is used instead and no
  188. prompting is made. If NOINSERT is provided and non-nil, nothing is
  189. inserted, but text is still saved when a NAME is provided. For
  190. clarity, the symbol `noinsert' should be used as argument.
  191. - (P PROMPT <NAME> <NOINSERT>): Works just like the previous tag, but
  192. forces `tempo-interactive' to be true.
  193. - (r PROMPT <NAME> <NOINSERT>): Like the previous tag, but if
  194. `tempo-interactive' is nil and `tempo-insert' is called with
  195. ON-REGION non-nil, the current region is placed here. This usually
  196. happens when you call the template function with a prefix argument.
  197. - (s NAME): Inserts text previously read with the (p ..) construct.
  198. Finds the insertion saved under NAME and inserts it. Acts like `p'
  199. if tempo-interactive is nil.
  200. - `&': If there is only whitespace between the line start and point,
  201. nothing happens. Otherwise a newline is inserted.
  202. - `%': If there is only whitespace between point and end of line,
  203. nothing happens. Otherwise a newline is inserted.
  204. - `n': Inserts a newline.
  205. - `>': The line is indented using `indent-according-to-mode'. Note
  206. that you often should place this item after the text you want on
  207. the line.
  208. - `r>': Like `r', but it also indents the region.
  209. - (r> PROMPT <NAME> <NOINSERT>): Like (r ...), but is also indents
  210. the region.
  211. - `n>': Inserts a newline and indents line.
  212. - `o': Like `%' but leaves the point before the newline.
  213. - nil: It is ignored.
  214. - Anything else: It is evaluated and the result is treated as an
  215. element to be inserted. One additional tag is useful for these
  216. cases. If an expression returns a list '(l foo bar), the elements
  217. after `l' will be inserted according to the usual rules. This makes
  218. it possible to return several elements from one expression."
  219. (let* ((template-name (intern (concat "tempo-template-"
  220. name)))
  221. (command-name template-name))
  222. (set template-name elements)
  223. (fset command-name (list 'lambda (list '&optional 'arg)
  224. (or documentation
  225. (concat "Insert a " name "."))
  226. (list 'interactive "*P")
  227. (list 'tempo-insert-template (list 'quote
  228. template-name)
  229. (list 'if 'tempo-insert-region
  230. (list 'not 'arg) 'arg))))
  231. (and tag
  232. (tempo-add-tag tag template-name taglist))
  233. command-name))
  234. ;;;
  235. ;;; tempo-insert-template
  236. (defun tempo-insert-template (template on-region)
  237. "Insert a template.
  238. TEMPLATE is the template to be inserted. If ON-REGION is non-nil the
  239. `r' elements are replaced with the current region. In Transient Mark
  240. mode, ON-REGION is ignored and assumed true if the region is active."
  241. (unwind-protect
  242. (progn
  243. (if (or (and (boundp 'transient-mark-mode) ; For Emacs
  244. transient-mark-mode
  245. mark-active)
  246. (if (featurep 'xemacs)
  247. (and zmacs-regions (mark))))
  248. (setq on-region t))
  249. (and on-region
  250. (set-marker tempo-region-start (min (mark) (point)))
  251. (set-marker tempo-region-stop (max (mark) (point))))
  252. (if on-region
  253. (goto-char tempo-region-start))
  254. (save-excursion
  255. (tempo-insert-mark (point-marker))
  256. (mapc (function (lambda (elt)
  257. (tempo-insert elt on-region)))
  258. (symbol-value template))
  259. (tempo-insert-mark (point-marker)))
  260. (tempo-forward-mark))
  261. (tempo-forget-insertions)
  262. ;; Should I check for zmacs here too???
  263. (and (boundp 'transient-mark-mode)
  264. transient-mark-mode
  265. (deactivate-mark))))
  266. ;;;
  267. ;;; tempo-insert
  268. (defun tempo-insert (element on-region)
  269. "Insert a template element.
  270. Insert one element from a template. If ON-REGION is non-nil the `r'
  271. elements are replaced with the current region.
  272. See documentation for `tempo-define-template' for the kind of elements
  273. possible."
  274. (cond ((stringp element) (tempo-process-and-insert-string element))
  275. ((and (consp element)
  276. (eq (car element) 'p)) (tempo-insert-prompt-compat
  277. (cdr element)))
  278. ((and (consp element)
  279. (eq (car element) 'P)) (let ((tempo-interactive t))
  280. (tempo-insert-prompt-compat
  281. (cdr element))))
  282. ;;; ((and (consp element)
  283. ;;; (eq (car element) 'v)) (tempo-save-named
  284. ;;; (nth 1 element)
  285. ;;; nil
  286. ;;; (nth 2 element)))
  287. ((and (consp element)
  288. (eq (car element) 'r)) (if on-region
  289. (goto-char tempo-region-stop)
  290. (tempo-insert-prompt-compat
  291. (cdr element))))
  292. ((and (consp element)
  293. (eq (car element) 'r>)) (if on-region
  294. (progn
  295. (goto-char tempo-region-stop)
  296. (indent-region (mark) (point) nil))
  297. (tempo-insert-prompt-compat
  298. (cdr element))))
  299. ((and (consp element)
  300. (eq (car element) 's)) (tempo-insert-named (car (cdr element))))
  301. ((and (consp element)
  302. (eq (car element) 'l)) (mapcar (function
  303. (lambda (elt)
  304. (tempo-insert elt on-region)))
  305. (cdr element)))
  306. ((eq element 'p) (tempo-insert-mark (point-marker)))
  307. ((eq element 'r) (if on-region
  308. (goto-char tempo-region-stop)
  309. (tempo-insert-mark (point-marker))))
  310. ((eq element 'r>) (if on-region
  311. (progn
  312. (goto-char tempo-region-stop)
  313. (indent-region (mark) (point) nil))
  314. (tempo-insert-mark (point-marker))))
  315. ((eq element '>) (indent-according-to-mode))
  316. ((eq element '&) (if (not (or (= (current-column) 0)
  317. (save-excursion
  318. (re-search-backward
  319. "^\\s-*\\=" nil t))))
  320. (insert "\n")))
  321. ((eq element '%) (if (not (or (eolp)
  322. (save-excursion
  323. (re-search-forward
  324. "\\=\\s-*$" nil t))))
  325. (insert "\n")))
  326. ((eq element 'n) (insert "\n"))
  327. ((eq element 'n>) (insert "\n") (indent-according-to-mode))
  328. ;; Bug: If the 'o is the first element in a template, strange
  329. ;; things can happen when the template is inserted at the
  330. ;; beginning of a line.
  331. ((eq element 'o) (if (not (or on-region
  332. (eolp)
  333. (save-excursion
  334. (re-search-forward
  335. "\\=\\s-*$" nil t))))
  336. (open-line 1)))
  337. ((null element))
  338. (t (tempo-insert (or (tempo-is-user-element element)
  339. (eval element))
  340. on-region))))
  341. ;;;
  342. ;;; tempo-insert-prompt
  343. (defun tempo-insert-prompt-compat (prompt)
  344. "Compatibility hack for `tempo-insert-prompt'.
  345. PROMPT can be either a prompt string, or a list of arguments to
  346. `tempo-insert-prompt', or nil."
  347. (if (consp prompt) ; not nil either
  348. (apply 'tempo-insert-prompt prompt)
  349. (tempo-insert-prompt prompt)))
  350. (defun tempo-insert-prompt (prompt &optional save-name no-insert)
  351. "Prompt for a text string and insert it in the current buffer.
  352. If the variable `tempo-interactive' is non-nil the user is prompted
  353. for a string in the minibuffer, which is then inserted in the current
  354. buffer. If `tempo-interactive' is nil, the current point is placed on
  355. `tempo-mark'.
  356. PROMPT is the prompt string, SAVE-NAME is a name to save the inserted
  357. text under. If the optional argument NO-INSERT is non-nil, no text is
  358. inserted. This can be useful when there is a SAVE-NAME.
  359. If there already is a value for SAVE-NAME, it is used and the user is
  360. never prompted."
  361. (let (insertion
  362. (previous (and save-name
  363. (tempo-lookup-named save-name))))
  364. (cond
  365. ;; Insert previous value, unless no-insert is non-nil
  366. ((and previous
  367. (not no-insert))
  368. (tempo-insert-named save-name)) ; A double lookup here, but who
  369. ; cares
  370. ;; If no-insert is non-nil, don't insert the previous value. Just
  371. ;; keep it
  372. (previous
  373. nil)
  374. ;; No previous value. Prompt or insert mark
  375. (tempo-interactive
  376. (if (not (stringp prompt))
  377. (error "tempo: The prompt (%s) is not a string" prompt))
  378. (setq insertion (read-string prompt))
  379. (or no-insert
  380. (insert insertion))
  381. (if save-name
  382. (tempo-save-named save-name insertion)))
  383. (t
  384. (tempo-insert-mark (point-marker))))))
  385. ;;;
  386. ;;; tempo-is-user-element
  387. (defun tempo-is-user-element (element)
  388. "Tries all the user-defined element handlers in `tempo-user-elements'."
  389. ;; Sigh... I need (some list)
  390. (catch 'found
  391. (mapc (function (lambda (handler)
  392. (let ((result (funcall handler element)))
  393. (if result (throw 'found result)))))
  394. tempo-user-elements)
  395. (throw 'found nil)))
  396. ;;;
  397. ;;; tempo-forget-insertions
  398. (defun tempo-forget-insertions ()
  399. "Forget all the saved named insertions."
  400. (setq tempo-named-insertions nil))
  401. ;;;
  402. ;;; tempo-save-named
  403. (defun tempo-save-named (name data) ; Had an optional prompt for 'v
  404. "Save some data for later insertion
  405. The contents of DATA is saved under the name NAME.
  406. The data can later be retrieved with `tempo-lookup-named'.
  407. This function returns nil, so it can be used in a template without
  408. inserting anything."
  409. (setq tempo-named-insertions
  410. (cons (cons name data)
  411. tempo-named-insertions))
  412. nil)
  413. ;;;
  414. ;;; tempo-lookup-named
  415. (defun tempo-lookup-named (name)
  416. "Lookup some saved data under the name NAME.
  417. Returns the data if NAME was found, and nil otherwise."
  418. (cdr (assq name tempo-named-insertions)))
  419. ;;;
  420. ;;; tempo-insert-named
  421. (defun tempo-insert-named (name)
  422. "Insert the previous insertion saved under a named specified in NAME.
  423. If there is no such name saved, a tempo mark is inserted.
  424. Note that if the data is a string, it will not be run through the string
  425. processor."
  426. (let* ((insertion (tempo-lookup-named name)))
  427. (cond ((null insertion)
  428. (tempo-insert-mark (point-marker)))
  429. ((stringp insertion)
  430. (insert insertion))
  431. (t
  432. (tempo-insert insertion nil)))))
  433. ;;;
  434. ;;; tempo-process-and-insert-string
  435. (defun tempo-process-and-insert-string (string)
  436. "Insert a string from a template.
  437. Run a string through the preprocessors in `tempo-insert-string-functions'
  438. and insert the results."
  439. (cond ((null tempo-insert-string-functions)
  440. nil)
  441. ((symbolp tempo-insert-string-functions)
  442. (setq string
  443. (funcall tempo-insert-string-functions string)))
  444. ((listp tempo-insert-string-functions)
  445. (dolist (fn tempo-insert-string-functions)
  446. (setq string (funcall fn string))))
  447. (t
  448. (error "Bogus value in tempo-insert-string-functions: %s"
  449. tempo-insert-string-functions)))
  450. (insert string))
  451. ;;;
  452. ;;; tempo-insert-mark
  453. (defun tempo-insert-mark (mark)
  454. "Insert a mark `tempo-marks' while keeping it sorted."
  455. (cond ((null tempo-marks) (setq tempo-marks (list mark)))
  456. ((< mark (car tempo-marks)) (setq tempo-marks (cons mark tempo-marks)))
  457. (t (let ((lp tempo-marks))
  458. (while (and (cdr lp)
  459. (<= (car (cdr lp)) mark))
  460. (setq lp (cdr lp)))
  461. (if (not (= mark (car lp)))
  462. (setcdr lp (cons mark (cdr lp))))))))
  463. ;;;
  464. ;;; tempo-forward-mark
  465. (defun tempo-forward-mark ()
  466. "Jump to the next mark in `tempo-forward-mark-list'."
  467. (interactive)
  468. (let ((next-mark (catch 'found
  469. (mapc
  470. (function
  471. (lambda (mark)
  472. (if (< (point) mark)
  473. (throw 'found mark))))
  474. tempo-marks)
  475. ;; return nil if not found
  476. nil)))
  477. (if next-mark
  478. (goto-char next-mark))))
  479. ;;;
  480. ;;; tempo-backward-mark
  481. (defun tempo-backward-mark ()
  482. "Jump to the previous mark in `tempo-back-mark-list'."
  483. (interactive)
  484. (let ((prev-mark (catch 'found
  485. (let (last)
  486. (mapc
  487. (function
  488. (lambda (mark)
  489. (if (<= (point) mark)
  490. (throw 'found last))
  491. (setq last mark)))
  492. tempo-marks)
  493. last))))
  494. (if prev-mark
  495. (goto-char prev-mark))))
  496. ;;;
  497. ;;; tempo-add-tag
  498. (defun tempo-add-tag (tag template &optional tag-list)
  499. "Add a template tag.
  500. Add the TAG, that should complete to TEMPLATE to the list in TAG-LIST,
  501. or to `tempo-tags' if TAG-LIST is nil."
  502. (interactive "sTag: \nCTemplate: ")
  503. (if (null tag-list)
  504. (setq tag-list 'tempo-tags))
  505. (if (not (assoc tag (symbol-value tag-list)))
  506. (set tag-list (cons (cons tag template) (symbol-value tag-list))))
  507. (tempo-invalidate-collection))
  508. ;;;
  509. ;;; tempo-use-tag-list
  510. (defun tempo-use-tag-list (tag-list &optional completion-function)
  511. "Install TAG-LIST to be used for template completion in the current buffer.
  512. TAG-LIST is a symbol whose variable value is a tag list created with
  513. `tempo-add-tag'.
  514. COMPLETION-FUNCTION is an obsolete option for specifying an optional
  515. function or string that is used by `\\[tempo-complete-tag]' to find a
  516. string to match the tag against. It has the same definition as the
  517. variable `tempo-match-finder'. In this version, supplying a
  518. COMPLETION-FUNCTION just sets `tempo-match-finder' locally."
  519. (let ((old (assq tag-list tempo-local-tags)))
  520. (if old
  521. (setcdr old completion-function)
  522. (setq tempo-local-tags (cons (cons tag-list completion-function)
  523. tempo-local-tags))))
  524. (if completion-function
  525. (setq tempo-match-finder completion-function))
  526. (tempo-invalidate-collection))
  527. ;;;
  528. ;;; tempo-invalidate-collection
  529. (defun tempo-invalidate-collection ()
  530. "Marks the tag collection as obsolete.
  531. Whenever it is needed again it will be rebuilt."
  532. (setq tempo-dirty-collection t))
  533. ;;;
  534. ;;; tempo-build-collection
  535. (defun tempo-build-collection ()
  536. "Build a collection of all the tags and return it.
  537. If `tempo-dirty-collection' is nil, the old collection is reused."
  538. (prog1
  539. (or (and (not tempo-dirty-collection)
  540. tempo-collection)
  541. (setq tempo-collection
  542. (apply (function append)
  543. (mapcar (function (lambda (tag-list)
  544. ; If the format for
  545. ; tempo-local-tags changes,
  546. ; change this
  547. (eval (car tag-list))))
  548. tempo-local-tags))))
  549. (setq tempo-dirty-collection nil)))
  550. ;;;
  551. ;;; tempo-find-match-string
  552. (defun tempo-find-match-string (finder)
  553. "Find a string to be matched against a tag list.
  554. FINDER is a function or a string. Returns (STRING . POS), or nil
  555. if no reasonable string is found."
  556. (cond ((stringp finder)
  557. (let (successful)
  558. (save-excursion
  559. (or (setq successful (re-search-backward finder nil t))
  560. 0))
  561. (if successful
  562. (cons (buffer-substring (match-beginning 1)
  563. (match-end 1)) ; This seems to be a
  564. ; bug in emacs
  565. (match-beginning 1))
  566. nil)))
  567. (t
  568. (funcall finder))))
  569. ;;;
  570. ;;; tempo-complete-tag
  571. (defun tempo-complete-tag (&optional silent)
  572. "Look for a tag and expand it.
  573. All the tags in the tag lists in `tempo-local-tags'
  574. \(this includes `tempo-tags') are searched for a match for the text
  575. before the point. The way the string to match for is determined can
  576. be altered with the variable `tempo-match-finder'. If
  577. `tempo-match-finder' returns nil, then the results are the same as
  578. no match at all.
  579. If a single match is found, the corresponding template is expanded in
  580. place of the matching string.
  581. If a partial completion or no match at all is found, and SILENT is
  582. non-nil, the function will give a signal.
  583. If a partial completion is found and `tempo-show-completion-buffer' is
  584. non-nil, a buffer containing possible completions is displayed."
  585. ;; This function may look like a hack, but this is how I want it to
  586. ;; work.
  587. (interactive "*")
  588. (let* ((collection (tempo-build-collection))
  589. (match-info (tempo-find-match-string tempo-match-finder))
  590. (match-string (car match-info))
  591. (match-start (cdr match-info))
  592. (exact (assoc match-string collection))
  593. (compl (or (car exact)
  594. (and match-info (try-completion match-string collection)))))
  595. (if compl (delete-region match-start (point)))
  596. (cond ((null match-info) (or silent (ding)))
  597. ((null compl) (or silent (ding)))
  598. ((eq compl t) (tempo-insert-template
  599. (cdr (assoc match-string
  600. collection))
  601. nil))
  602. (t (if (setq exact (assoc compl collection))
  603. (tempo-insert-template (cdr exact) nil)
  604. (insert compl)
  605. (or silent (ding))
  606. (if tempo-show-completion-buffer
  607. (tempo-display-completions match-string
  608. collection)))))))
  609. ;;;
  610. ;;; tempo-display-completions
  611. (defun tempo-display-completions (string tag-list)
  612. "Show a buffer containing possible completions for STRING."
  613. (if tempo-leave-completion-buffer
  614. (with-output-to-temp-buffer "*Completions*"
  615. (display-completion-list
  616. (all-completions string tag-list)
  617. string))
  618. (save-window-excursion
  619. (with-output-to-temp-buffer "*Completions*"
  620. (display-completion-list
  621. (all-completions string tag-list)
  622. string))
  623. (sit-for 32767))))
  624. ;;;
  625. ;;; tempo-expand-if-complete
  626. (defun tempo-expand-if-complete ()
  627. "Expand the tag before point if it is complete.
  628. Returns non-nil if an expansion was made and nil otherwise.
  629. This could as an example be used in a command that is bound to the
  630. space bar, and looks something like this:
  631. \(defun tempo-space ()
  632. (interactive \"*\")
  633. (or (tempo-expand-if-complete)
  634. (insert \" \")))"
  635. (interactive "*")
  636. (let* ((collection (tempo-build-collection))
  637. (match-info (tempo-find-match-string tempo-match-finder))
  638. (match-string (car match-info))
  639. (match-start (cdr match-info))
  640. (exact (assoc match-string collection)))
  641. (if exact
  642. (progn
  643. (delete-region match-start (point))
  644. (tempo-insert-template (cdr exact) nil)
  645. t)
  646. nil)))
  647. (provide 'tempo)
  648. ;;; tempo.el ends here