dabbrev.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. ;;; dabbrev.el --- dynamic abbreviation package -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1985-1986, 1992, 1994, 1996-1997, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Don Morrison
  5. ;; Lars Lindberg
  6. ;; (according to ack.texi)
  7. ;; Maintainer: Lars Lindberg <Lars.Lindberg@sypro.cap.se>
  8. ;; Created: 16 Mars 1992
  9. ;; Lindberg's last update version: 5.7
  10. ;; Keywords: abbrev expand completion convenience
  11. ;; This file is part of GNU Emacs.
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;; The purpose with this package is to let you write just a few
  24. ;; characters of words you've written earlier to be able to expand
  25. ;; them.
  26. ;;
  27. ;; To expand a word, just put the point right after the word and press
  28. ;; M-/ (dabbrev-expand) or M-C-/ (dabbrev-completion).
  29. ;;
  30. ;; Check out the customizable variables below to learn about all the
  31. ;; features of this package.
  32. ;;; Hints and tips for major modes writers:
  33. ;; Recommended values C/Lisp etc text
  34. ;; dabbrev-case-fold-search nil t
  35. ;; dabbrev-case-replace nil t
  36. ;;
  37. ;; Set the variables you want special for your mode like this:
  38. ;; (set (make-local-variable 'dabbrev-case-replace) nil)
  39. ;; Then you don't interfere with other modes.
  40. ;;
  41. ;; If your mode handles buffers that refers to other buffers
  42. ;; (i.e. compilation-mode, gud-mode), then try to set
  43. ;; `dabbrev-select-buffers-function' or `dabbrev-friend-buffer-function'
  44. ;; to a function that point out those buffers.
  45. ;; Same goes for major-modes that are connected to other modes. There
  46. ;; are for instance a number of mail-modes. One for reading, one for
  47. ;; creating a new mail etc. Maybe those should be connected.
  48. ;; Example for GNUS (when we write a reply, we want dabbrev to look in
  49. ;; the article for expansion):
  50. ;; (set (make-local-variable 'dabbrev-friend-buffer-function)
  51. ;; (lambda (buffer)
  52. ;; (with-current-buffer buffer
  53. ;; (memq major-mode '(news-reply-mode gnus-article-mode)))))
  54. ;; Known bugs and limitations.
  55. ;; - Possible to do several levels of `dabbrev-completion' in the
  56. ;; minibuffer.
  57. ;; - dabbrev-completion doesn't handle resetting the globals variables
  58. ;; right. It resets them after finding the abbrev.
  59. ;; Future enhancements
  60. ;; - Check the tags-files? Like tags-complete?
  61. ;; - Add the possibility of searching both forward and backward to
  62. ;; the nearest expansion.
  63. ;; - Check the kill-ring when everything else fails. (Maybe something
  64. ;; for hippie-expand?). [Bng] <boris@cs.rochester.edu>
  65. ;;; These people gave suggestions:
  66. ;; [hymie] Hyman Rosen <marks!hymie@jyacc.jyacc.com>
  67. ;; [burgett] Steve Burgett <burgett@bizet.eecs.berkeley.edu>
  68. ;; [jules] Julian Gosnell <jules@x.co.uk>
  69. ;; [kifer] Michael Kifer <kifer@sbcs.sunysb.edu>
  70. ;; [ake] Ake Stenhoff <extaksf@aom.ericsson.se>
  71. ;; [alon] Alon Albert <al%imercury@uunet.uu.net>
  72. ;; [tromey] Tom Tromey <tromey@busco.lanl.gov>
  73. ;; [Rolf] Rolf Schreiber <rolf@mathematik.uni-stuttgart.de>
  74. ;; [Petri] Petri Raitio <per@tekla.fi>
  75. ;; [ejb] Jay Berkenbilt <ejb@ql.org>
  76. ;; [hawley] Bob Hawley <rth1@quartet.mt.att.com>
  77. ;; ... and to all the people who have participated in the beta tests.
  78. ;;; Code:
  79. ;;----------------------------------------------------------------
  80. ;; Customization variables
  81. ;;----------------------------------------------------------------
  82. (defgroup dabbrev nil
  83. "Dynamic Abbreviations."
  84. :tag "Dynamic Abbreviations"
  85. :group 'abbrev
  86. :group 'convenience)
  87. (defcustom dabbrev-backward-only nil
  88. "If non-nil, `dabbrev-expand' only looks backwards."
  89. :type 'boolean
  90. :group 'dabbrev)
  91. (defcustom dabbrev-limit nil
  92. "Limits region searched by `dabbrev-expand' to this many chars away."
  93. :type '(choice (const :tag "off" nil)
  94. integer)
  95. :group 'dabbrev)
  96. (defcustom dabbrev-abbrev-skip-leading-regexp nil
  97. "Regexp for skipping leading characters of an abbreviation.
  98. Example: Set this to \"\\\\$\" for programming languages
  99. in which variable names may appear with or without a leading `$'.
  100. \(For example, in Makefiles.\)
  101. Set this to nil if no characters should be skipped."
  102. :type '(choice regexp
  103. (const :tag "off" nil))
  104. :group 'dabbrev)
  105. (defcustom dabbrev-eliminate-newlines t
  106. "Non-nil means dabbrev should not insert newlines.
  107. Instead it converts them to spaces."
  108. :type 'boolean
  109. :group 'dabbrev)
  110. (defcustom dabbrev-case-fold-search 'case-fold-search
  111. "Control whether dabbrev searches should ignore case.
  112. A value of nil means case is significant.
  113. A value of `case-fold-search' means case is significant
  114. if `case-fold-search' is nil.
  115. Any other non-nil version means case is not significant."
  116. :type '(choice (const :tag "off" nil)
  117. (const :tag "like search" case-fold-search)
  118. (other :tag "on" t))
  119. :group 'dabbrev)
  120. ;;;###autoload(put 'dabbrev-case-fold-search 'risky-local-variable t)
  121. (defcustom dabbrev-upcase-means-case-search nil
  122. "The significance of an uppercase character in an abbreviation.
  123. A nil value means case fold search when searching for possible expansions;
  124. non-nil means case sensitive search.
  125. This variable has an effect only when the value of
  126. `dabbrev-case-fold-search' says to ignore case."
  127. :type 'boolean
  128. :group 'dabbrev)
  129. (defcustom dabbrev-case-distinction 'case-replace
  130. "Whether dabbrev treats expansions as the same if they differ in case.
  131. A value of nil means treat them as different.
  132. A value of `case-replace' means distinguish them if `case-replace' is nil.
  133. Any other non-nil value means to treat them as the same.
  134. This variable has an effect only when the value of
  135. `dabbrev-case-fold-search' specifies to ignore case."
  136. :type '(choice (const :tag "off" nil)
  137. (const :tag "based on `case-replace'" case-replace)
  138. (other :tag "on" t))
  139. :group 'dabbrev
  140. :version "22.1")
  141. (defcustom dabbrev-case-replace 'case-replace
  142. "Whether dabbrev applies the abbreviations's case pattern to the expansion.
  143. A value of nil means preserve the expansion's case pattern.
  144. A value of `case-replace' means preserve it if `case-replace' is nil.
  145. Any other non-nil value means modify the expansion
  146. by applying the abbreviation's case pattern to it.
  147. This variable has an effect only when the value of
  148. `dabbrev-case-fold-search' specifies to ignore case."
  149. :type '(choice (const :tag "off" nil)
  150. (const :tag "based on `case-replace'" case-replace)
  151. (other :tag "on" t))
  152. :group 'dabbrev)
  153. ;;;###autoload(put 'dabbrev-case-replace 'risky-local-variable t)
  154. (defcustom dabbrev-abbrev-char-regexp nil
  155. "Regexp to recognize a character in an abbreviation or expansion.
  156. This regexp will be surrounded with \\\\( ... \\\\) when actually used.
  157. Set this variable to \"\\\\sw\" if you want ordinary words or
  158. \"\\\\sw\\\\|\\\\s_\" if you want symbols (including characters whose
  159. syntax is \"symbol\" as well as those whose syntax is \"word\".
  160. The value nil has a special meaning: the abbreviation is from point to
  161. previous word-start, but the search is for symbols.
  162. For instance, if you are programming in Lisp, `yes-or-no-p' is a symbol,
  163. while `yes', `or', `no' and `p' are considered words. If this
  164. variable is nil, then expanding `yes-or-no-' looks for a symbol
  165. starting with or containing `no-'. If you set this variable to
  166. \"\\\\sw\\\\|\\\\s_\", that expansion looks for a symbol starting with
  167. `yes-or-no-'. Finally, if you set this variable to \"\\\\sw\", then
  168. expanding `yes-or-no-' signals an error because `-' is not part of a word;
  169. but expanding `yes-or-no' looks for a word starting with `no'.
  170. The recommended value is nil, which will make dabbrev default to
  171. using \"\\\\sw\\\\|\\\\s_\"."
  172. :type '(choice (const nil)
  173. regexp)
  174. :group 'dabbrev)
  175. (defcustom dabbrev-check-all-buffers t
  176. "Non-nil means dabbrev package should search *all* buffers.
  177. Dabbrev always searches the current buffer first. Then, if
  178. `dabbrev-check-other-buffers' says so, it searches the buffers
  179. designated by `dabbrev-select-buffers-function'.
  180. Then, if `dabbrev-check-all-buffers' is non-nil, dabbrev searches
  181. all the other buffers, except those named in `dabbrev-ignored-buffer-names',
  182. or matched by `dabbrev-ignored-regexps'."
  183. :type 'boolean
  184. :group 'dabbrev)
  185. (defcustom dabbrev-ignored-buffer-names '("*Messages*" "*Buffer List*")
  186. "List of buffer names that dabbrev should not check.
  187. See also `dabbrev-ignored-buffer-regexps'."
  188. :type '(repeat (string :tag "Buffer name"))
  189. :group 'dabbrev
  190. :version "20.3")
  191. (defcustom dabbrev-ignored-buffer-regexps nil
  192. "List of regexps matching names of buffers that dabbrev should not check.
  193. See also `dabbrev-ignored-buffer-names'."
  194. :type '(repeat regexp)
  195. :group 'dabbrev
  196. :version "21.1")
  197. (defcustom dabbrev-check-other-buffers t
  198. "Should \\[dabbrev-expand] look in other buffers?\
  199. nil: Don't look in other buffers.
  200. t: Also look for expansions in the buffers pointed out by
  201. `dabbrev-select-buffers-function'.
  202. Anything else: When we can't find any more expansions in
  203. the current buffer, then ask the user whether to look in other
  204. buffers too.
  205. The default value is t."
  206. :type '(choice (const :tag "off" nil)
  207. (const :tag "on" t)
  208. (other :tag "ask" other))
  209. :group 'dabbrev)
  210. ;; I guess setting this to a function that selects all C- or C++-
  211. ;; mode buffers would be a good choice for a debugging buffer,
  212. ;; when debugging C- or C++-code.
  213. (defvar dabbrev-select-buffers-function 'dabbrev--select-buffers
  214. "A function that selects buffers that should be searched by dabbrev.
  215. The function should take no arguments and return a list of buffers to
  216. search for expansions. See the source of `dabbrev--select-buffers'
  217. for an example.
  218. A mode setting this variable should make it buffer local.")
  219. (defcustom dabbrev-friend-buffer-function 'dabbrev--same-major-mode-p
  220. "A function to decide whether dabbrev should search OTHER-BUFFER.
  221. The function should take one argument, OTHER-BUFFER, and return
  222. non-nil if that buffer should be searched. Have a look at
  223. `dabbrev--same-major-mode-p' for an example.
  224. The value of `dabbrev-friend-buffer-function' has an effect only if
  225. the value of `dabbrev-select-buffers-function' uses it. The function
  226. `dabbrev--select-buffers' is one function you can use here.
  227. A mode setting this variable should make it buffer local."
  228. :type 'function
  229. :group 'dabbrev)
  230. (defcustom dabbrev-search-these-buffers-only nil
  231. "If non-nil, a list of buffers which dabbrev should search.
  232. If this variable is non-nil, dabbrev will only look in these buffers.
  233. It will not even look in the current buffer if it is not a member of
  234. this list."
  235. :group 'dabbrev)
  236. ;;----------------------------------------------------------------
  237. ;; Internal variables
  238. ;;----------------------------------------------------------------
  239. ;; Table of expansions seen so far
  240. (defvar dabbrev--last-table nil)
  241. ;; Last string we tried to expand.
  242. (defvar dabbrev--last-abbreviation nil)
  243. ;; Location last abbreviation began
  244. (defvar dabbrev--last-abbrev-location nil)
  245. ;; Direction of last dabbrevs search
  246. (defvar dabbrev--last-direction 0)
  247. ;; Last expansion of an abbreviation.
  248. (defvar dabbrev--last-expansion nil)
  249. ;; Location the last expansion was found.
  250. (defvar dabbrev--last-expansion-location nil)
  251. ;; The list of remaining buffers with the same mode as current buffer.
  252. (defvar dabbrev--friend-buffer-list nil)
  253. ;; The buffer we looked in last, not counting the current buffer.
  254. (defvar dabbrev--last-buffer nil)
  255. ;; The buffer we found the expansion last time.
  256. (defvar dabbrev--last-buffer-found nil)
  257. ;; If non-nil, a function to use when copying successive words.
  258. ;; It should be `upcase' or `downcase'.
  259. (defvar dabbrev--last-case-pattern nil)
  260. ;; Same as dabbrev-check-other-buffers, but is set for every expand.
  261. (defvar dabbrev--check-other-buffers dabbrev-check-other-buffers)
  262. ;; The regexp for recognizing a character in an abbreviation.
  263. (defvar dabbrev--abbrev-char-regexp nil)
  264. ;; The progress reporter for buffer-scanning progress.
  265. (defvar dabbrev--progress-reporter nil)
  266. ;;----------------------------------------------------------------
  267. ;; Macros
  268. ;;----------------------------------------------------------------
  269. (defsubst dabbrev--minibuffer-origin ()
  270. "Get the buffer from which mini-buffer."
  271. (window-buffer (minibuffer-selected-window)))
  272. ;; Make a list of some of the elements of LIST.
  273. ;; Check each element of LIST, storing it temporarily in the
  274. ;; variable ELEMENT, and include it in the result
  275. ;; if CONDITION evaluates non-nil.
  276. (defmacro dabbrev-filter-elements (element list condition)
  277. `(let (dabbrev-result dabbrev-tail ,element)
  278. (setq dabbrev-tail ,list)
  279. (while dabbrev-tail
  280. (setq ,element (car dabbrev-tail))
  281. (if ,condition
  282. (setq dabbrev-result (cons ,element dabbrev-result)))
  283. (setq dabbrev-tail (cdr dabbrev-tail)))
  284. (nreverse dabbrev-result)))
  285. ;;----------------------------------------------------------------
  286. ;; Exported functions
  287. ;;----------------------------------------------------------------
  288. ;;;###autoload (define-key esc-map "/" 'dabbrev-expand)
  289. ;;??? Do we want this?
  290. ;;;###autoload (define-key esc-map [?\C-/] 'dabbrev-completion)
  291. (defun dabbrev--ignore-case-p (abbrev)
  292. (and (if (eq dabbrev-case-fold-search 'case-fold-search)
  293. case-fold-search
  294. dabbrev-case-fold-search)
  295. (or (not dabbrev-upcase-means-case-search)
  296. (string= abbrev (downcase abbrev)))))
  297. ;;;###autoload
  298. (defun dabbrev-completion (&optional arg)
  299. "Completion on current word.
  300. Like \\[dabbrev-expand] but finds all expansions in the current buffer
  301. and presents suggestions for completion.
  302. With a prefix argument ARG, it searches all buffers accepted by the
  303. function pointed out by `dabbrev-friend-buffer-function' to find the
  304. completions.
  305. If the prefix argument is 16 (which comes from \\[universal-argument] \\[universal-argument]),
  306. then it searches *all* buffers."
  307. (interactive "*P")
  308. (dabbrev--reset-global-variables)
  309. (let* ((dabbrev-check-other-buffers (and arg t))
  310. (dabbrev-check-all-buffers
  311. (and arg (= (prefix-numeric-value arg) 16)))
  312. (abbrev (dabbrev--abbrev-at-point))
  313. (beg (progn (search-backward abbrev) (point)))
  314. (end (progn (search-forward abbrev) (point)))
  315. (ignore-case-p (dabbrev--ignore-case-p abbrev))
  316. (list 'uninitialized)
  317. (table
  318. (lambda (s p a)
  319. (if (eq a 'metadata)
  320. `(metadata (cycle-sort-function . ,#'identity)
  321. (category . dabbrev))
  322. (when (eq list 'uninitialized)
  323. (save-excursion
  324. ;;--------------------------------
  325. ;; New abbreviation to expand.
  326. ;;--------------------------------
  327. (setq dabbrev--last-abbreviation abbrev)
  328. ;; Find all expansion
  329. (let ((completion-list
  330. (dabbrev--find-all-expansions abbrev ignore-case-p))
  331. (completion-ignore-case ignore-case-p))
  332. (or (consp completion-list)
  333. (user-error "No dynamic expansion for \"%s\" found%s"
  334. abbrev
  335. (if dabbrev--check-other-buffers
  336. "" " in this-buffer")))
  337. (setq list
  338. (cond
  339. ((not (and ignore-case-p dabbrev-case-replace))
  340. completion-list)
  341. ((string= abbrev (upcase abbrev))
  342. (mapcar #'upcase completion-list))
  343. ((string= (substring abbrev 0 1)
  344. (upcase (substring abbrev 0 1)))
  345. (mapcar #'capitalize completion-list))
  346. (t
  347. (mapcar #'downcase completion-list)))))))
  348. (complete-with-action a list s p)))))
  349. (completion-in-region beg end table)))
  350. ;;;###autoload
  351. (defun dabbrev-expand (arg)
  352. "Expand previous word \"dynamically\".
  353. Expands to the most recent, preceding word for which this is a prefix.
  354. If no suitable preceding word is found, words following point are
  355. considered. If still no suitable word is found, then look in the
  356. buffers accepted by the function pointed out by variable
  357. `dabbrev-friend-buffer-function'.
  358. A positive prefix argument, N, says to take the Nth backward *distinct*
  359. possibility. A negative argument says search forward.
  360. If the cursor has not moved from the end of the previous expansion and
  361. no argument is given, replace the previously-made expansion
  362. with the next possible expansion not yet tried.
  363. The variable `dabbrev-backward-only' may be used to limit the
  364. direction of search to backward if set non-nil.
  365. See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
  366. (interactive "*P")
  367. (let (abbrev record-case-pattern
  368. expansion old direction (orig-point (point)))
  369. ;; abbrev -- the abbrev to expand
  370. ;; expansion -- the expansion found (eventually) or nil until then
  371. ;; old -- the text currently in the buffer
  372. ;; (the abbrev, or the previously-made expansion)
  373. (save-excursion
  374. (if (and (null arg)
  375. (markerp dabbrev--last-abbrev-location)
  376. (marker-position dabbrev--last-abbrev-location)
  377. (or (eq last-command this-command)
  378. (and (window-minibuffer-p (selected-window))
  379. (= dabbrev--last-abbrev-location
  380. (point)))))
  381. ;; Find a different expansion for the same abbrev as last time.
  382. (progn
  383. (setq abbrev dabbrev--last-abbreviation)
  384. (setq old dabbrev--last-expansion)
  385. (setq direction dabbrev--last-direction))
  386. ;; If the user inserts a space after expanding
  387. ;; and then asks to expand again, always fetch the next word.
  388. (if (and (eq (preceding-char) ?\s)
  389. (markerp dabbrev--last-abbrev-location)
  390. (marker-position dabbrev--last-abbrev-location)
  391. (= (point) (1+ dabbrev--last-abbrev-location)))
  392. (progn
  393. ;; The "abbrev" to expand is just the space.
  394. (setq abbrev " ")
  395. (save-excursion
  396. (save-restriction
  397. (widen)
  398. (if dabbrev--last-buffer
  399. (set-buffer dabbrev--last-buffer))
  400. ;; Find the end of the last "expansion" word.
  401. (if (or (eq dabbrev--last-direction 1)
  402. (and (eq dabbrev--last-direction 0)
  403. (< dabbrev--last-expansion-location (point))))
  404. (setq dabbrev--last-expansion-location
  405. (+ dabbrev--last-expansion-location
  406. (length dabbrev--last-expansion))))
  407. (goto-char dabbrev--last-expansion-location)
  408. ;; Take the following word, with intermediate separators,
  409. ;; as our expansion this time.
  410. (re-search-forward
  411. (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
  412. (setq expansion (buffer-substring-no-properties
  413. dabbrev--last-expansion-location (point)))
  414. ;; Record the end of this expansion, in case we repeat this.
  415. (setq dabbrev--last-expansion-location (point))))
  416. ;; Indicate that dabbrev--last-expansion-location is
  417. ;; at the end of the expansion.
  418. (setq dabbrev--last-direction -1))
  419. ;; We have a different abbrev to expand.
  420. (dabbrev--reset-global-variables)
  421. (setq direction (if (null arg)
  422. (if dabbrev-backward-only 1 0)
  423. (prefix-numeric-value arg)))
  424. (setq abbrev (dabbrev--abbrev-at-point))
  425. (setq record-case-pattern t)
  426. (setq old nil)))
  427. ;;--------------------------------
  428. ;; Find the expansion
  429. ;;--------------------------------
  430. (or expansion
  431. (setq expansion
  432. (dabbrev--find-expansion
  433. abbrev direction
  434. (dabbrev--ignore-case-p abbrev)))))
  435. (cond
  436. ((not expansion)
  437. (dabbrev--reset-global-variables)
  438. (if old
  439. (save-excursion
  440. (setq buffer-undo-list (cons orig-point buffer-undo-list))
  441. ;; Put back the original abbrev with its original case pattern.
  442. (search-backward old)
  443. (insert abbrev)
  444. (delete-region (point) (+ (point) (length old)))))
  445. (user-error "No%s dynamic expansion for `%s' found"
  446. (if old " further" "") abbrev))
  447. (t
  448. (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
  449. (minibuffer-window-active-p (selected-window))))
  450. (progn
  451. (message "Expansion found in '%s'"
  452. (buffer-name dabbrev--last-buffer))
  453. (setq dabbrev--last-buffer-found dabbrev--last-buffer))
  454. (message nil))
  455. (if (and (or (eq (current-buffer) dabbrev--last-buffer)
  456. (null dabbrev--last-buffer))
  457. (numberp dabbrev--last-expansion-location)
  458. (and (> dabbrev--last-expansion-location (point))))
  459. (setq dabbrev--last-expansion-location
  460. (copy-marker dabbrev--last-expansion-location)))
  461. ;; Success: stick it in and return.
  462. (setq buffer-undo-list (cons orig-point buffer-undo-list))
  463. (dabbrev--substitute-expansion old abbrev expansion
  464. record-case-pattern)
  465. ;; Save state for re-expand.
  466. (setq dabbrev--last-expansion expansion)
  467. (setq dabbrev--last-abbreviation abbrev)
  468. (setq dabbrev--last-abbrev-location (point-marker))))))
  469. ;;----------------------------------------------------------------
  470. ;; Local functions
  471. ;;----------------------------------------------------------------
  472. (defun dabbrev--same-major-mode-p (other-buffer)
  473. "Check if OTHER-BUFFER has the same major mode as current buffer."
  474. (eq major-mode
  475. (with-current-buffer other-buffer
  476. major-mode)))
  477. (defun dabbrev--goto-start-of-abbrev ()
  478. "Back over all abbrev type characters and then moves forward over
  479. all skip characters."
  480. ;; Move backwards over abbrev chars
  481. (save-match-data
  482. (when (> (point) (minibuffer-prompt-end))
  483. (forward-char -1)
  484. (while (and (looking-at dabbrev--abbrev-char-regexp)
  485. (> (point) (minibuffer-prompt-end))
  486. (not (= (point) (field-beginning (point) nil
  487. (1- (point))))))
  488. (forward-char -1))
  489. (or (looking-at dabbrev--abbrev-char-regexp)
  490. (forward-char 1)))
  491. (and dabbrev-abbrev-skip-leading-regexp
  492. (while (looking-at dabbrev-abbrev-skip-leading-regexp)
  493. (forward-char 1)))))
  494. (defun dabbrev--abbrev-at-point ()
  495. "Extract the symbol at point to serve as abbreviation."
  496. ;; Check for error
  497. (if (bobp)
  498. (user-error "No possible abbreviation preceding point"))
  499. ;; Return abbrev at point
  500. (save-excursion
  501. ;; Record the end of the abbreviation.
  502. (setq dabbrev--last-abbrev-location (point))
  503. ;; If we aren't right after an abbreviation,
  504. ;; move point back to just after one.
  505. ;; This is so the user can get successive words
  506. ;; by typing the punctuation followed by M-/.
  507. (save-match-data
  508. (if (save-excursion
  509. (forward-char -1)
  510. (not (looking-at (or dabbrev-abbrev-char-regexp
  511. "\\sw\\|\\s_"))))
  512. (if (re-search-backward (or dabbrev-abbrev-char-regexp
  513. "\\sw\\|\\s_")
  514. nil t)
  515. (forward-char 1)
  516. (user-error "No possible abbreviation preceding point"))))
  517. ;; Now find the beginning of that one.
  518. (dabbrev--goto-start-of-abbrev)
  519. (buffer-substring-no-properties
  520. dabbrev--last-abbrev-location (point))))
  521. (defun dabbrev--reset-global-variables ()
  522. "Initialize all global variables."
  523. (setq dabbrev--last-table nil
  524. dabbrev--last-abbreviation nil
  525. dabbrev--last-abbrev-location nil
  526. dabbrev--last-direction nil
  527. dabbrev--last-expansion nil
  528. dabbrev--last-expansion-location nil
  529. dabbrev--friend-buffer-list nil
  530. dabbrev--last-buffer nil
  531. dabbrev--last-buffer-found nil
  532. dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
  533. "\\sw\\|\\s_")
  534. dabbrev--check-other-buffers dabbrev-check-other-buffers))
  535. (defun dabbrev--select-buffers ()
  536. "Return a list of other buffers to search for a possible abbrev.
  537. The current buffer is not included in the list.
  538. This function makes a list of all the buffers returned by `buffer-list',
  539. then discards buffers whose names match `dabbrev-ignored-buffer-names'
  540. or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
  541. `dabbrev-friend-buffer-function', if it is bound, returns nil when called
  542. with the buffer as argument.
  543. It returns the list of the buffers that are not discarded."
  544. (dabbrev-filter-elements
  545. buffer (buffer-list)
  546. (and (not (eq (current-buffer) buffer))
  547. (not (dabbrev--ignore-buffer-p buffer))
  548. (boundp 'dabbrev-friend-buffer-function)
  549. (funcall dabbrev-friend-buffer-function buffer))))
  550. (defun dabbrev--try-find (abbrev reverse n ignore-case)
  551. "Search for ABBREV, backwards if REVERSE, N times.
  552. If IGNORE-CASE is non-nil, ignore case while searching.
  553. Return the expansion found, and save the location of the start
  554. of the expansion in `dabbrev--last-expansion-location'."
  555. (save-excursion
  556. (save-restriction
  557. (widen)
  558. (let ((expansion nil))
  559. (and dabbrev--last-expansion-location
  560. (goto-char dabbrev--last-expansion-location))
  561. (let ((case-fold-search ignore-case)
  562. (count n))
  563. (while (and (> count 0)
  564. (setq expansion (dabbrev--search
  565. abbrev reverse
  566. (and ignore-case
  567. (if (eq dabbrev-case-distinction
  568. 'case-replace)
  569. case-replace
  570. dabbrev-case-distinction)))))
  571. (setq count (1- count))))
  572. (and expansion
  573. (setq dabbrev--last-expansion-location (point)))
  574. expansion))))
  575. (defun dabbrev--find-all-expansions (abbrev ignore-case)
  576. "Return a list of all possible expansions of ABBREV.
  577. If IGNORE-CASE is non-nil, accept matches which differ in case."
  578. (let ((all-expansions nil)
  579. expansion)
  580. (save-excursion
  581. (goto-char (point-min))
  582. (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
  583. (setq all-expansions (cons expansion all-expansions))))
  584. all-expansions))
  585. (defun dabbrev--ignore-buffer-p (buffer)
  586. "Return non-nil if BUFFER should be ignored by dabbrev."
  587. (let ((bn (buffer-name buffer)))
  588. (or (member bn dabbrev-ignored-buffer-names)
  589. (let ((tail dabbrev-ignored-buffer-regexps)
  590. (match nil))
  591. (while (and tail (not match))
  592. (setq match (string-match (car tail) bn)
  593. tail (cdr tail)))
  594. match))))
  595. (defun dabbrev--find-expansion (abbrev direction ignore-case)
  596. "Find one occurrence of ABBREV, and return the expansion.
  597. DIRECTION > 0 means look that many times backwards.
  598. DIRECTION < 0 means look that many times forward.
  599. DIRECTION = 0 means try both backward and forward.
  600. IGNORE-CASE non-nil means ignore case when searching.
  601. This sets `dabbrev--last-direction' to 1 or -1 according
  602. to the direction in which the occurrence was actually found.
  603. It sets `dabbrev--last-expansion-location' to the location
  604. of the start of the occurrence."
  605. (save-excursion
  606. ;; If we were scanning something other than the current buffer,
  607. ;; continue scanning there.
  608. (when dabbrev--last-buffer
  609. (set-buffer dabbrev--last-buffer))
  610. (or
  611. ;; ------------------------------------------
  612. ;; Look backward in current buffer.
  613. ;; ------------------------------------------
  614. (and (not dabbrev-search-these-buffers-only)
  615. (>= direction 0)
  616. (setq dabbrev--last-direction (min 1 direction))
  617. (dabbrev--try-find abbrev t
  618. (max 1 direction)
  619. ignore-case))
  620. ;; ------------------------------------------
  621. ;; Look forward in current buffer
  622. ;; or whatever buffer we were last scanning.
  623. ;; ------------------------------------------
  624. (and (or (not dabbrev-search-these-buffers-only)
  625. dabbrev--last-buffer)
  626. (<= direction 0)
  627. (setq dabbrev--last-direction -1)
  628. (dabbrev--try-find abbrev nil
  629. (max 1 (- direction))
  630. ignore-case))
  631. ;; ------------------------------------------
  632. ;; Look in other buffers.
  633. ;; Always start at (point-min) and look forward.
  634. ;; ------------------------------------------
  635. (progn
  636. (setq dabbrev--last-direction -1)
  637. (unless dabbrev--last-buffer
  638. ;; If we have just now begun to search other buffers,
  639. ;; determine which other buffers we should check.
  640. ;; Put that list in dabbrev--friend-buffer-list.
  641. (unless dabbrev--friend-buffer-list
  642. (setq dabbrev--friend-buffer-list
  643. (dabbrev--make-friend-buffer-list))
  644. (setq dabbrev--progress-reporter
  645. (make-progress-reporter
  646. "Scanning for dabbrevs..."
  647. (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5))))
  648. ;; Walk through the buffers till we find a match.
  649. (let (expansion)
  650. (while (and (not expansion) dabbrev--friend-buffer-list)
  651. (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
  652. (set-buffer dabbrev--last-buffer)
  653. (progress-reporter-update dabbrev--progress-reporter
  654. (- (length dabbrev--friend-buffer-list)))
  655. (setq dabbrev--last-expansion-location (point-min))
  656. (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
  657. (progress-reporter-done dabbrev--progress-reporter)
  658. expansion)))))
  659. ;; Compute the list of buffers to scan.
  660. ;; If dabbrev-search-these-buffers-only, then the current buffer
  661. ;; is included in this list if it should be searched.
  662. ;; Otherwise, the current buffer is searched first specially.,
  663. ;; and it is not included in this list.
  664. (defun dabbrev--make-friend-buffer-list ()
  665. (let ((list (mapcar (function get-buffer)
  666. dabbrev-search-these-buffers-only)))
  667. (when (and (null dabbrev-search-these-buffers-only)
  668. dabbrev--check-other-buffers
  669. (or (eq dabbrev--check-other-buffers t)
  670. (setq dabbrev--check-other-buffers
  671. (y-or-n-p "Scan other buffers also? "))))
  672. (setq list (funcall dabbrev-select-buffers-function))
  673. ;; If dabbrev-check-all-buffers, tack on all the other
  674. ;; buffers at the end of the list, except those which are
  675. ;; specifically to be ignored.
  676. (if dabbrev-check-all-buffers
  677. (setq list
  678. (append list
  679. (dabbrev-filter-elements
  680. buffer (buffer-list)
  681. (and (not (memq buffer list))
  682. (not (dabbrev--ignore-buffer-p buffer)))))))
  683. ;; Remove the current buffer.
  684. (setq list (delq (current-buffer) list)))
  685. ;; Move buffers in the list that are visible on the screen
  686. ;; to the front of the list, but don't add anything to the list.
  687. (if list
  688. (walk-windows (lambda (w)
  689. (unless (eq w (selected-window))
  690. (if (memq (window-buffer w) list)
  691. (setq list
  692. (cons (window-buffer w)
  693. (delq (window-buffer w)
  694. list))))))))
  695. ;; In a minibuffer, search the buffer it was activated from,
  696. ;; first after the minibuffer itself. Unless we aren't supposed
  697. ;; to search the current buffer either.
  698. (if (and (window-minibuffer-p (selected-window))
  699. (not dabbrev-search-these-buffers-only))
  700. (setq list
  701. (cons (dabbrev--minibuffer-origin)
  702. (delq (dabbrev--minibuffer-origin) list))))
  703. list))
  704. (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
  705. (if (eq major-mode 'picture-mode)
  706. (with-no-warnings
  707. (picture-replace-match string fixedcase literal))
  708. (replace-match string fixedcase literal)))
  709. ;;;----------------------------------------------------------------
  710. (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
  711. "Replace OLD with EXPANSION in the buffer.
  712. OLD is text currently in the buffer, perhaps the abbreviation
  713. or perhaps another expansion that was tried previously.
  714. ABBREV is the abbreviation we are expanding.
  715. It is \" \" if we are copying subsequent words.
  716. EXPANSION is the expansion substring to be used this time.
  717. RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
  718. to record whether we upcased the expansion, downcased it, or did neither."
  719. ;;(undo-boundary)
  720. (let ((use-case-replace
  721. (and (dabbrev--ignore-case-p abbrev)
  722. (if (eq dabbrev-case-replace 'case-replace)
  723. case-replace
  724. dabbrev-case-replace))))
  725. ;; If we upcased or downcased the original expansion,
  726. ;; do likewise for the subsequent words when we copy them.
  727. ;; Don't do any of the usual case processing, though.
  728. (when (equal abbrev " ")
  729. (if dabbrev--last-case-pattern
  730. (setq expansion
  731. (funcall dabbrev--last-case-pattern expansion)))
  732. (setq use-case-replace nil))
  733. ;; If the expansion has mixed case
  734. ;; and it is not simply a capitalized word,
  735. ;; or if the abbrev has mixed case,
  736. ;; and if the given abbrev's case pattern
  737. ;; matches the start of the expansion,
  738. ;; copy the expansion's case
  739. ;; instead of downcasing all the rest.
  740. ;;
  741. ;; Treat a one-capital-letter (possibly with preceding non-letter
  742. ;; characters) abbrev as "not all upper case", so as to force
  743. ;; preservation of the expansion's pattern if the expansion starts
  744. ;; with a capital letter.
  745. (let ((expansion-rest (substring expansion 1))
  746. (first-letter-position (string-match "[[:alpha:]]" abbrev)))
  747. (if (or (null first-letter-position)
  748. (and (not
  749. (and (or (string= expansion-rest (downcase expansion-rest))
  750. (string= expansion-rest (upcase expansion-rest)))
  751. (or (string= abbrev (downcase abbrev))
  752. (and (string= abbrev (upcase abbrev))
  753. (> (- (length abbrev) first-letter-position)
  754. 1)))))
  755. (string= abbrev
  756. (substring expansion 0 (length abbrev)))))
  757. (setq use-case-replace nil)))
  758. ;; If the abbrev and the expansion are both all-lower-case
  759. ;; then don't do any conversion. The conversion would be a no-op
  760. ;; for this replacement, but it would carry forward to subsequent words.
  761. ;; The goal of this is to prevent that carrying forward.
  762. (if (and (string= expansion (downcase expansion))
  763. (string= abbrev (downcase abbrev)))
  764. (setq use-case-replace nil))
  765. (if use-case-replace
  766. (setq expansion (downcase expansion)))
  767. ;; In case we insert subsequent words,
  768. ;; record if we upcased or downcased the first word,
  769. ;; in order to do likewise for subsequent words.
  770. (and record-case-pattern
  771. (setq dabbrev--last-case-pattern
  772. (and use-case-replace
  773. (cond ((equal abbrev (upcase abbrev)) 'upcase)
  774. ((equal abbrev (downcase abbrev)) 'downcase)))))
  775. ;; Convert whitespace to single spaces.
  776. (if dabbrev-eliminate-newlines
  777. (let ((pos
  778. (if (equal abbrev " ") 0 (length abbrev))))
  779. ;; If ABBREV is real, search after the end of it.
  780. ;; If ABBREV is space and we are copying successive words,
  781. ;; search starting at the front.
  782. (while (string-match "[\n \t]+" expansion pos)
  783. (setq pos (1+ (match-beginning 0)))
  784. (setq expansion (replace-match " " nil nil expansion)))))
  785. (if old
  786. (save-excursion
  787. (search-backward old))
  788. ;;(set-match-data (list (point-marker) (point-marker)))
  789. (search-backward abbrev)
  790. (search-forward abbrev))
  791. ;; Make case of replacement conform to case of abbreviation
  792. ;; provided (1) that kind of thing is enabled in this buffer
  793. ;; and (2) the replacement itself is all lower case.
  794. (dabbrev--safe-replace-match expansion
  795. (not use-case-replace)
  796. t)))
  797. ;;;----------------------------------------------------------------
  798. ;;; Search function used by dabbrevs library.
  799. (defun dabbrev--search (abbrev reverse ignore-case)
  800. "Search for something that could be used to expand ABBREV.
  801. Second arg, REVERSE, is t for reverse search, nil for forward.
  802. The variable `dabbrev-limit' controls the maximum search region size.
  803. Third argument IGNORE-CASE non-nil means treat case as insignificant while
  804. looking for a match and when comparing with previous matches. Also if
  805. that's non-nil and the match is found at the beginning of a sentence
  806. and is in lower case except for the initial then it is converted to
  807. all lower case for return.
  808. Table of expansions already seen is examined in buffer
  809. `dabbrev--last-table' so that only distinct possibilities are found
  810. by dabbrev-re-expand.
  811. Returns the expansion found, or nil if not found.
  812. Leaves point at the location of the start of the expansion."
  813. (save-match-data
  814. (let ((pattern1 (concat (regexp-quote abbrev)
  815. "\\(" dabbrev--abbrev-char-regexp "\\)"))
  816. (pattern2 (concat (regexp-quote abbrev)
  817. "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
  818. ;; This makes it possible to find matches in minibuffer prompts
  819. ;; even when they are "inviolable".
  820. (inhibit-point-motion-hooks t)
  821. found-string result)
  822. ;; Limited search.
  823. (save-restriction
  824. (and dabbrev-limit
  825. (narrow-to-region
  826. dabbrev--last-expansion-location
  827. (+ (point) (if reverse (- dabbrev-limit) dabbrev-limit))))
  828. ;;--------------------------------
  829. ;; Look for a distinct expansion, using dabbrev--last-table.
  830. ;;--------------------------------
  831. (while (and (not found-string)
  832. (if reverse
  833. (re-search-backward pattern1 nil t)
  834. (re-search-forward pattern1 nil t)))
  835. (goto-char (match-beginning 0))
  836. ;; In case we matched in the middle of a word,
  837. ;; back up to start of word and verify we still match.
  838. (dabbrev--goto-start-of-abbrev)
  839. (if (not (looking-at pattern1))
  840. nil
  841. ;; We have a truly valid match. Find the end.
  842. (re-search-forward pattern2)
  843. (setq found-string (match-string-no-properties 0))
  844. (setq result found-string)
  845. (and ignore-case (setq found-string (downcase found-string)))
  846. ;; Ignore this match if it's already in the table.
  847. (if (dabbrev-filter-elements
  848. table-string dabbrev--last-table
  849. (string= found-string table-string))
  850. (setq found-string nil)))
  851. ;; Prepare to continue searching.
  852. (goto-char (if reverse (match-beginning 0) (match-end 0))))
  853. ;; If we found something, use it.
  854. (when found-string
  855. ;; Put it into `dabbrev--last-table'
  856. ;; and return it (either downcased, or as is).
  857. (setq dabbrev--last-table
  858. (cons found-string dabbrev--last-table))
  859. result)))))
  860. (provide 'dabbrev)
  861. ;;; dabbrev.el ends here