expand.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. ;;; expand.el --- make abbreviations more usable
  2. ;; Copyright (C) 1995-1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Frederic Lepied <Frederic.Lepied@sugix.frmug.org>
  4. ;; Maintainer: Frederic Lepied <Frederic.Lepied@sugix.frmug.org>
  5. ;; Keywords: abbrev
  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. ;;
  19. ;; This package defines abbrevs which expand into structured constructs
  20. ;; for certain languages. The construct is indented for you,
  21. ;; and contains slots for you to fill in other text.
  22. ;; These abbrevs expand only at the end of a line and when not in a comment
  23. ;; or a string.
  24. ;;
  25. ;; Look at the Sample: section for emacs-lisp, perl and c expand lists.
  26. ;; For example for c-mode, you could declare your abbrev table with :
  27. ;;
  28. ;; (defconst c-expand-list
  29. ;; '(("if" "if () {\n \n} else {\n \n}" (5 10 21))
  30. ;; ("ifn" "if () {}" (5 8))
  31. ;; ("uns" "unsigned ")
  32. ;; ("for" "for(; ; ) {\n\n}" (5 7 9 13))
  33. ;; ("switch" "switch () {\n\n}" (9 13))
  34. ;; ("case" "case :\n\nbreak;\n" (6 8 16))
  35. ;; ("do" "do {\n\n} while ();" (6 16))
  36. ;; ("while" "while () {\n\n}" (8 12))
  37. ;; ("default" "default:\n\nbreak;" 10)
  38. ;; ("main" "int\nmain(int argc, char * argv[])\n{\n\n}\n" 37))
  39. ;; "Expansions for C mode")
  40. ;;
  41. ;; and enter Abbrev mode with the following hook :
  42. ;;
  43. ;; (add-hook 'c-mode-hook
  44. ;; (lambda ()
  45. ;; (expand-add-abbrevs c-mode-abbrev-table c-expand-list)
  46. ;; (abbrev-mode 1)))
  47. ;;
  48. ;; you can also init some post-process hooks :
  49. ;;
  50. ;; (add-hook 'expand-load-hook
  51. ;; (lambda ()
  52. ;; (add-hook 'expand-expand-hook 'indent-according-to-mode)
  53. ;; (add-hook 'expand-jump-hook 'indent-according-to-mode)))
  54. ;;
  55. ;; Remarks:
  56. ;;
  57. ;; Many thanks to Heddy Boubaker <boubaker@cenatls.cena.dgac.fr>,
  58. ;; Jerome Santini <santini@chambord.univ-orleans.fr>,
  59. ;; Jari Aalto <jaalto@tre.tele.nokia.fi>.
  60. ;;
  61. ;; Please send me a word to give me your feeling about this feature or
  62. ;; to explain me how you use it (your expansions table for example) using
  63. ;; the function expand-submit-report.
  64. ;;; Code:
  65. ;;; Constants:
  66. (defgroup expand nil
  67. "Make abbreviations more usable."
  68. :group 'abbrev)
  69. (defcustom expand-load-hook nil
  70. "Hooks run when `expand.el' is loaded."
  71. :type 'hook
  72. :group 'expand)
  73. (defcustom expand-expand-hook nil
  74. "Hooks run when an abbrev made by `expand-add-abbrevs' is expanded."
  75. :type 'hook
  76. :group 'expand)
  77. (defcustom expand-jump-hook nil
  78. "Hooks run by `expand-jump-to-previous-slot' and `expand-jump-to-next-slot'."
  79. :type 'hook
  80. :group 'expand)
  81. ;;; Samples:
  82. (define-skeleton expand-c-for-skeleton "For loop skeleton"
  83. "Loop var: "
  84. "for(" str _ @ "=0; " str @ "; " str @ ") {" \n
  85. @ _ \n
  86. "}" > \n)
  87. (defconst expand-c-sample-expand-list
  88. '(("if" "if () {\n \n} else {\n \n}" (5 10 21))
  89. ("ifn" "if () {}" (5 8))
  90. ("uns" "unsigned ")
  91. ("for" expand-c-for-skeleton)
  92. ("switch" "switch () {\n\n}" (9 13))
  93. ("case" "case :\n\nbreak;\n" (6 8 16))
  94. ("do" "do {\n\n} while ();" (6 16))
  95. ("while" "while () {\n\n}" (8 12))
  96. ("default" "default:\n\nbreak;" 10)
  97. ("main" "int\nmain(int argc, char * argv[])\n{\n\n}\n" 37))
  98. "Expansions for C mode. See `expand-add-abbrevs'.")
  99. ;; lisp example from Jari Aalto <jaalto@tre.tele.nokia.fi>
  100. (defconst expand-sample-lisp-mode-expand-list
  101. (list
  102. (list
  103. "defu"
  104. (concat
  105. "(defun ()\n"
  106. " \"\"\n"
  107. " (interactive)\n"
  108. " (let* (\n"
  109. " )\n"
  110. " \n"
  111. " ))")
  112. (list 8 11 16 32 43 59))
  113. (list
  114. "defs"
  115. (concat
  116. "(defsubst ()\n"
  117. " \"\"\n"
  118. " (interactive)\n"
  119. " )")
  120. (list 11 14 19 23 39))
  121. (list
  122. "defm"
  123. (concat
  124. "(defmacro ()\n"
  125. " \"\"\n"
  126. " `( \n"
  127. " ))")
  128. (list 11 13 18 25))
  129. (list
  130. "defa"
  131. (concat
  132. "(defadvice (around act)\n"
  133. " \"\"\n"
  134. " \n"
  135. " )")
  136. (list 12 22 32 36))
  137. (list
  138. "defc"
  139. "(defconst nil\n \"\")\n"
  140. (list 11 13 20))
  141. (list
  142. "defv"
  143. "(defvar nil\n \"\")\n"
  144. (list 9 11 18))
  145. (list
  146. "let"
  147. "(let* (\n)\n "
  148. (list 8 13))
  149. (list
  150. "sav"
  151. "(save-excursion\n \n)"
  152. (list 18))
  153. (list
  154. "aut"
  155. "(autoload ' \"\" t t)\n"
  156. (list 12 14))
  157. )
  158. "Expansions for Lisp mode. See `expand-add-abbrevs'.")
  159. ;; perl example from Jari Aalto <jaalto@tre.tele.nokia.fi>
  160. (defconst expand-sample-perl-mode-expand-list
  161. (list
  162. (list
  163. ;; This is default perl4 subroutine template
  164. ;;
  165. "sub"
  166. (concat
  167. "#" (make-string 70 ?-) "\n"
  168. "sub {\n"
  169. " # DESCRIPTION\n"
  170. " # \n"
  171. " # \n"
  172. " # INPUT\n"
  173. " # \n"
  174. " # \n"
  175. " # RETURN\n"
  176. " # \n"
  177. "\n"
  178. " local( $f ) = \"$lib.\";\n" ;; Function name AFTER period
  179. " local() = @_;\n" ;; func arguments here
  180. " \n"
  181. " \n}\n"
  182. )
  183. (list 77 88 120 146 159 176))
  184. (list
  185. "for" ; foreach
  186. (concat
  187. "for ( )\n"
  188. "{\n\n\}"
  189. )
  190. (list 7 12))
  191. (list
  192. "whi" ; foreach
  193. (concat
  194. "while ( )\n"
  195. "{\n\n\}"
  196. )
  197. (list 9 15))
  198. ;; The normal "if" can be used like
  199. ;; print $F "xxxxxx" if defined @arr;
  200. ;;
  201. (list
  202. "iff"
  203. (concat
  204. "if ( )\n"
  205. "{\n\n\}"
  206. )
  207. (list 6 12))
  208. (list "loc" "local( $ );" (list 9))
  209. (list "my" "my( $ );" (list 6))
  210. (list "ope" "open(,\"\")\t|| die \"$f: Can't open [$]\";" (list 6 8 36))
  211. (list "clo" "close ;" 7)
  212. (list "def" "defined " (list 9))
  213. (list "und" "undef ;" (list 7))
  214. ;; There is no ending colon, because they can be in statement
  215. ;; defined $REXP_NOT_NEW && (print "xxxxx" );
  216. ;;
  217. (list "pr" "print " 7)
  218. (list "pf" "printf " 8)
  219. (list "gre" "grep( //, );" (list 8 11))
  220. (list "pus" "push( , );" (list 7 9))
  221. (list "joi" "join( '', );" (list 7 11))
  222. (list "rtu" "return ;" (list 8))
  223. )
  224. "Expansions for Perl mode. See `expand-add-abbrevs'.")
  225. ;;; Code:
  226. ;;;###autoload
  227. (defun expand-add-abbrevs (table abbrevs)
  228. "Add a list of abbreviations to abbrev table TABLE.
  229. ABBREVS is a list of abbrev definitions; each abbrev description entry
  230. has the form (ABBREV EXPANSION ARG).
  231. ABBREV is the abbreviation to replace.
  232. EXPANSION is the replacement string or a function which will make the
  233. expansion. For example, you could use the DMacros or skeleton packages
  234. to generate such functions.
  235. ARG is an optional argument which can be a number or a list of
  236. numbers. If ARG is a number, point is placed ARG chars from the
  237. beginning of the expanded text.
  238. If ARG is a list of numbers, point is placed according to the first
  239. member of the list, but you can visit the other specified positions
  240. cyclically with the functions `expand-jump-to-previous-slot' and
  241. `expand-jump-to-next-slot'.
  242. If ARG is omitted, point is placed at the end of the expanded text."
  243. (if (null abbrevs)
  244. table
  245. (expand-add-abbrev table (nth 0 (car abbrevs)) (nth 1 (car abbrevs))
  246. (nth 2 (car abbrevs)))
  247. (expand-add-abbrevs table (cdr abbrevs))))
  248. (defvar expand-list nil "Temporary variable used by the Expand package.")
  249. (defvar expand-pos nil
  250. "If non-nil, stores a vector containing markers to positions defined by the last expansion.")
  251. (make-variable-buffer-local 'expand-pos)
  252. (defvar expand-index 0
  253. "Index of the last marker used in `expand-pos'.")
  254. (make-variable-buffer-local 'expand-index)
  255. (defvar expand-point nil
  256. "End of the expanded region.")
  257. (make-variable-buffer-local 'expand-point)
  258. (defun expand-add-abbrev (table abbrev expansion arg)
  259. "Add one abbreviation and provide the hook to move to the specified positions."
  260. (let* ((string-exp (if (and (symbolp expansion) (fboundp expansion))
  261. nil
  262. expansion))
  263. (position (if (and arg string-exp)
  264. (if (listp arg)
  265. (- (length expansion) (1- (car arg)))
  266. (- (length expansion) (1- arg)))
  267. 0)))
  268. (define-abbrev
  269. table
  270. abbrev
  271. (vector string-exp
  272. position
  273. (if (and (listp arg)
  274. (not (null arg)))
  275. (cons (length string-exp) arg)
  276. nil)
  277. (if (and (symbolp expansion) (fboundp expansion))
  278. expansion
  279. nil)
  280. )
  281. 'expand-abbrev-hook)))
  282. (put 'expand-abbrev-hook 'no-self-insert t)
  283. ;;;###autoload
  284. (defun expand-abbrev-hook ()
  285. "Abbrev hook used to do the expansion job of expand abbrevs.
  286. See `expand-add-abbrevs'. Value is non-nil if expansion was done."
  287. ;; Expand only at the end of a line if we are near a word that has
  288. ;; an abbrev built from expand-add-abbrev.
  289. (if (and (eolp)
  290. (not (expand-in-literal)))
  291. (let ((p (point)))
  292. (setq expand-point nil)
  293. ;; don't expand if the preceding char isn't a word constituent
  294. (if (and (eq (char-syntax (preceding-char))
  295. ?w)
  296. (expand-do-expansion))
  297. (progn
  298. ;; expand-point tells us if we have inserted the text
  299. ;; ourself or if it is the hook which has done the job.
  300. (if expand-point
  301. (progn
  302. (if (vectorp expand-list)
  303. (expand-build-marks expand-point))
  304. (indent-region p expand-point nil))
  305. ;; an outside function can set expand-list to a list of
  306. ;; markers in reverse order.
  307. (if (listp expand-list)
  308. (setq expand-index 0
  309. expand-pos (expand-list-to-markers expand-list)
  310. expand-list nil)))
  311. (run-hooks 'expand-expand-hook)
  312. t)
  313. nil))
  314. nil))
  315. (defun expand-do-expansion ()
  316. (delete-char (- (length last-abbrev-text)))
  317. (let* ((vect (symbol-value last-abbrev))
  318. (text (aref vect 0))
  319. (position (aref vect 1))
  320. (jump-args (aref vect 2))
  321. (hook (aref vect 3)))
  322. (cond (text
  323. (insert text)
  324. (setq expand-point (point))))
  325. (if jump-args
  326. (funcall 'expand-build-list (car jump-args) (cdr jump-args)))
  327. (if position
  328. (backward-char position))
  329. (if hook
  330. (funcall hook))
  331. t)
  332. )
  333. (defun expand-abbrev-from-expand (word)
  334. "Test if an abbrev has a hook."
  335. (or
  336. (and (intern-soft word local-abbrev-table)
  337. (symbol-function (intern-soft word local-abbrev-table)))
  338. (and (intern-soft word global-abbrev-table)
  339. (symbol-function (intern-soft word global-abbrev-table)))))
  340. (defun expand-previous-word ()
  341. "Return the previous word."
  342. (save-excursion
  343. (let ((p (point)))
  344. (backward-word 1)
  345. (buffer-substring p (point)))))
  346. ;;;###autoload
  347. (defun expand-jump-to-previous-slot ()
  348. "Move the cursor to the previous slot in the last abbrev expansion.
  349. This is used only in conjunction with `expand-add-abbrevs'."
  350. (interactive)
  351. (if expand-pos
  352. (progn
  353. (setq expand-index (1- expand-index))
  354. (if (< expand-index 0)
  355. (setq expand-index (1- (length expand-pos))))
  356. (goto-char (aref expand-pos expand-index))
  357. (run-hooks 'expand-jump-hook))))
  358. ;;;###autoload
  359. (defun expand-jump-to-next-slot ()
  360. "Move the cursor to the next slot in the last abbrev expansion.
  361. This is used only in conjunction with `expand-add-abbrevs'."
  362. (interactive)
  363. (if expand-pos
  364. (progn
  365. (setq expand-index (1+ expand-index))
  366. (if (>= expand-index (length expand-pos))
  367. (setq expand-index 0))
  368. (goto-char (aref expand-pos expand-index))
  369. (run-hooks 'expand-jump-hook))))
  370. ;;;###autoload (define-key abbrev-map "p" 'expand-jump-to-previous-slot)
  371. ;;;###autoload (define-key abbrev-map "n" 'expand-jump-to-next-slot)
  372. (defun expand-build-list (len l)
  373. "Build a vector of offset positions from the list of positions."
  374. (expand-clear-markers)
  375. (setq expand-list (vconcat l))
  376. (let ((i 0)
  377. (lenlist (length expand-list)))
  378. (while (< i lenlist)
  379. (aset expand-list i (- len (1- (aref expand-list i))))
  380. (setq i (1+ i))))
  381. )
  382. (defun expand-build-marks (p)
  383. "Transform the offsets vector into a marker vector."
  384. (if expand-list
  385. (progn
  386. (setq expand-index 0)
  387. (setq expand-pos (make-vector (length expand-list) nil))
  388. (let ((i (1- (length expand-list))))
  389. (while (>= i 0)
  390. (aset expand-pos i (copy-marker (- p (aref expand-list i))))
  391. (setq i (1- i))))
  392. (setq expand-list nil))))
  393. (defun expand-clear-markers ()
  394. "Make the markers point nowhere."
  395. (if expand-pos
  396. (progn
  397. (let ((i (1- (length expand-pos))))
  398. (while (>= i 0)
  399. (set-marker (aref expand-pos i) nil)
  400. (setq i (1- i))))
  401. (setq expand-pos nil))))
  402. (defun expand-in-literal ()
  403. "Test if we are in a comment or in a string."
  404. (save-excursion
  405. (let* ((lim (or (save-excursion
  406. (beginning-of-defun)
  407. (point))
  408. (point-min)))
  409. (state (parse-partial-sexp lim (point))))
  410. (cond
  411. ((nth 3 state) 'string)
  412. ((nth 4 state) 'comment)
  413. (t nil)))))
  414. ;; support functions to add marks to jump from outside function
  415. (defun expand-list-to-markers (l)
  416. "Transform a list of markers in reverse order into a vector in the correct order."
  417. (let* ((len (1- (length l)))
  418. (loop len)
  419. (v (make-vector (+ len 1) nil)))
  420. (while (>= loop 0)
  421. (aset v loop (if (markerp (car l)) (car l) (copy-marker (car l))))
  422. (setq l (cdr l)
  423. loop (1- loop)))
  424. v))
  425. ;; integration with skeleton.el
  426. ;; Used in `skeleton-end-hook' to fetch the positions for @ skeleton tags.
  427. ;; See `skeleton-insert'.
  428. (defun expand-skeleton-end-hook ()
  429. (if skeleton-positions
  430. (setq expand-list skeleton-positions)))
  431. (add-hook 'skeleton-end-hook (function expand-skeleton-end-hook))
  432. (provide 'expand)
  433. ;; run load hooks
  434. (run-hooks 'expand-load-hook)
  435. ;;; expand.el ends here