dabbrev.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. ;;;###autoload
  292. (defun dabbrev-completion (&optional arg)
  293. "Completion on current word.
  294. Like \\[dabbrev-expand] but finds all expansions in the current buffer
  295. and presents suggestions for completion.
  296. With a prefix argument ARG, it searches all buffers accepted by the
  297. function pointed out by `dabbrev-friend-buffer-function' to find the
  298. completions.
  299. If the prefix argument is 16 (which comes from \\[universal-argument] \\[universal-argument]),
  300. then it searches *all* buffers."
  301. (interactive "*P")
  302. (dabbrev--reset-global-variables)
  303. (let* ((dabbrev-check-other-buffers (and arg t))
  304. (dabbrev-check-all-buffers
  305. (and arg (= (prefix-numeric-value arg) 16)))
  306. (abbrev (dabbrev--abbrev-at-point))
  307. (beg (progn (search-backward abbrev) (point)))
  308. (end (progn (search-forward abbrev) (point)))
  309. (ignore-case-p
  310. (and (if (eq dabbrev-case-fold-search 'case-fold-search)
  311. case-fold-search
  312. dabbrev-case-fold-search)
  313. (or (not dabbrev-upcase-means-case-search)
  314. (string= abbrev (downcase abbrev)))))
  315. (list 'uninitialized)
  316. (table
  317. (lambda (s p a)
  318. (if (eq a 'metadata)
  319. `(metadata (cycle-sort-function . ,#'identity)
  320. (category . dabbrev))
  321. (when (eq list 'uninitialized)
  322. (save-excursion
  323. ;;--------------------------------
  324. ;; New abbreviation to expand.
  325. ;;--------------------------------
  326. (setq dabbrev--last-abbreviation abbrev)
  327. ;; Find all expansion
  328. (let ((completion-list
  329. (dabbrev--find-all-expansions abbrev ignore-case-p))
  330. (completion-ignore-case ignore-case-p))
  331. (or (consp completion-list)
  332. (error "No dynamic expansion for \"%s\" found%s"
  333. abbrev
  334. (if dabbrev--check-other-buffers
  335. "" " in this-buffer")))
  336. (setq list
  337. (cond
  338. ((not (and ignore-case-p dabbrev-case-replace))
  339. completion-list)
  340. ((string= abbrev (upcase abbrev))
  341. (mapcar #'upcase completion-list))
  342. ((string= (substring abbrev 0 1)
  343. (upcase (substring abbrev 0 1)))
  344. (mapcar #'capitalize completion-list))
  345. (t
  346. (mapcar #'downcase completion-list)))))))
  347. (complete-with-action a list s p)))))
  348. (completion-in-region beg end table)))
  349. ;;;###autoload
  350. (defun dabbrev-expand (arg)
  351. "Expand previous word \"dynamically\".
  352. Expands to the most recent, preceding word for which this is a prefix.
  353. If no suitable preceding word is found, words following point are
  354. considered. If still no suitable word is found, then look in the
  355. buffers accepted by the function pointed out by variable
  356. `dabbrev-friend-buffer-function'.
  357. A positive prefix argument, N, says to take the Nth backward *distinct*
  358. possibility. A negative argument says search forward.
  359. If the cursor has not moved from the end of the previous expansion and
  360. no argument is given, replace the previously-made expansion
  361. with the next possible expansion not yet tried.
  362. The variable `dabbrev-backward-only' may be used to limit the
  363. direction of search to backward if set non-nil.
  364. See also `dabbrev-abbrev-char-regexp' and \\[dabbrev-completion]."
  365. (interactive "*P")
  366. (let (abbrev record-case-pattern
  367. expansion old direction (orig-point (point)))
  368. ;; abbrev -- the abbrev to expand
  369. ;; expansion -- the expansion found (eventually) or nil until then
  370. ;; old -- the text currently in the buffer
  371. ;; (the abbrev, or the previously-made expansion)
  372. (save-excursion
  373. (if (and (null arg)
  374. (markerp dabbrev--last-abbrev-location)
  375. (marker-position dabbrev--last-abbrev-location)
  376. (or (eq last-command this-command)
  377. (and (window-minibuffer-p (selected-window))
  378. (= dabbrev--last-abbrev-location
  379. (point)))))
  380. ;; Find a different expansion for the same abbrev as last time.
  381. (progn
  382. (setq abbrev dabbrev--last-abbreviation)
  383. (setq old dabbrev--last-expansion)
  384. (setq direction dabbrev--last-direction))
  385. ;; If the user inserts a space after expanding
  386. ;; and then asks to expand again, always fetch the next word.
  387. (if (and (eq (preceding-char) ?\s)
  388. (markerp dabbrev--last-abbrev-location)
  389. (marker-position dabbrev--last-abbrev-location)
  390. (= (point) (1+ dabbrev--last-abbrev-location)))
  391. (progn
  392. ;; The "abbrev" to expand is just the space.
  393. (setq abbrev " ")
  394. (save-excursion
  395. (save-restriction
  396. (widen)
  397. (if dabbrev--last-buffer
  398. (set-buffer dabbrev--last-buffer))
  399. ;; Find the end of the last "expansion" word.
  400. (if (or (eq dabbrev--last-direction 1)
  401. (and (eq dabbrev--last-direction 0)
  402. (< dabbrev--last-expansion-location (point))))
  403. (setq dabbrev--last-expansion-location
  404. (+ dabbrev--last-expansion-location
  405. (length dabbrev--last-expansion))))
  406. (goto-char dabbrev--last-expansion-location)
  407. ;; Take the following word, with intermediate separators,
  408. ;; as our expansion this time.
  409. (re-search-forward
  410. (concat "\\(?:" dabbrev--abbrev-char-regexp "\\)+"))
  411. (setq expansion (buffer-substring-no-properties
  412. dabbrev--last-expansion-location (point)))
  413. ;; Record the end of this expansion, in case we repeat this.
  414. (setq dabbrev--last-expansion-location (point))))
  415. ;; Indicate that dabbrev--last-expansion-location is
  416. ;; at the end of the expansion.
  417. (setq dabbrev--last-direction -1))
  418. ;; We have a different abbrev to expand.
  419. (dabbrev--reset-global-variables)
  420. (setq direction (if (null arg)
  421. (if dabbrev-backward-only 1 0)
  422. (prefix-numeric-value arg)))
  423. (setq abbrev (dabbrev--abbrev-at-point))
  424. (setq record-case-pattern t)
  425. (setq old nil)))
  426. ;;--------------------------------
  427. ;; Find the expansion
  428. ;;--------------------------------
  429. (or expansion
  430. (setq expansion
  431. (dabbrev--find-expansion
  432. abbrev direction
  433. (and (if (eq dabbrev-case-fold-search 'case-fold-search)
  434. case-fold-search
  435. dabbrev-case-fold-search)
  436. (or (not dabbrev-upcase-means-case-search)
  437. (string= abbrev (downcase abbrev))))))))
  438. (cond
  439. ((not expansion)
  440. (dabbrev--reset-global-variables)
  441. (if old
  442. (save-excursion
  443. (setq buffer-undo-list (cons orig-point buffer-undo-list))
  444. ;; Put back the original abbrev with its original case pattern.
  445. (search-backward old)
  446. (insert abbrev)
  447. (delete-region (point) (+ (point) (length old)))))
  448. (error "No%s dynamic expansion for `%s' found"
  449. (if old " further" "") abbrev))
  450. (t
  451. (if (not (or (eq dabbrev--last-buffer dabbrev--last-buffer-found)
  452. (minibuffer-window-active-p (selected-window))))
  453. (progn
  454. (message "Expansion found in '%s'"
  455. (buffer-name dabbrev--last-buffer))
  456. (setq dabbrev--last-buffer-found dabbrev--last-buffer))
  457. (message nil))
  458. (if (and (or (eq (current-buffer) dabbrev--last-buffer)
  459. (null dabbrev--last-buffer))
  460. (numberp dabbrev--last-expansion-location)
  461. (and (> dabbrev--last-expansion-location (point))))
  462. (setq dabbrev--last-expansion-location
  463. (copy-marker dabbrev--last-expansion-location)))
  464. ;; Success: stick it in and return.
  465. (setq buffer-undo-list (cons orig-point buffer-undo-list))
  466. (dabbrev--substitute-expansion old abbrev expansion
  467. record-case-pattern)
  468. ;; Save state for re-expand.
  469. (setq dabbrev--last-expansion expansion)
  470. (setq dabbrev--last-abbreviation abbrev)
  471. (setq dabbrev--last-abbrev-location (point-marker))))))
  472. ;;----------------------------------------------------------------
  473. ;; Local functions
  474. ;;----------------------------------------------------------------
  475. (defun dabbrev--same-major-mode-p (other-buffer)
  476. "Check if OTHER-BUFFER has the same major mode as current buffer."
  477. (eq major-mode
  478. (with-current-buffer other-buffer
  479. major-mode)))
  480. (defun dabbrev--goto-start-of-abbrev ()
  481. "Back over all abbrev type characters and then moves forward over
  482. all skip characters."
  483. ;; Move backwards over abbrev chars
  484. (save-match-data
  485. (when (> (point) (minibuffer-prompt-end))
  486. (forward-char -1)
  487. (while (and (looking-at dabbrev--abbrev-char-regexp)
  488. (> (point) (minibuffer-prompt-end))
  489. (not (= (point) (field-beginning (point) nil
  490. (1- (point))))))
  491. (forward-char -1))
  492. (or (looking-at dabbrev--abbrev-char-regexp)
  493. (forward-char 1)))
  494. (and dabbrev-abbrev-skip-leading-regexp
  495. (while (looking-at dabbrev-abbrev-skip-leading-regexp)
  496. (forward-char 1)))))
  497. (defun dabbrev--abbrev-at-point ()
  498. "Extract the symbol at point to serve as abbreviation."
  499. ;; Check for error
  500. (if (bobp)
  501. (error "No possible abbreviation preceding point"))
  502. ;; Return abbrev at point
  503. (save-excursion
  504. ;; Record the end of the abbreviation.
  505. (setq dabbrev--last-abbrev-location (point))
  506. ;; If we aren't right after an abbreviation,
  507. ;; move point back to just after one.
  508. ;; This is so the user can get successive words
  509. ;; by typing the punctuation followed by M-/.
  510. (save-match-data
  511. (if (save-excursion
  512. (forward-char -1)
  513. (not (looking-at (or dabbrev-abbrev-char-regexp
  514. "\\sw\\|\\s_"))))
  515. (if (re-search-backward (or dabbrev-abbrev-char-regexp
  516. "\\sw\\|\\s_")
  517. nil t)
  518. (forward-char 1)
  519. (error "No possible abbreviation preceding point"))))
  520. ;; Now find the beginning of that one.
  521. (dabbrev--goto-start-of-abbrev)
  522. (buffer-substring-no-properties
  523. dabbrev--last-abbrev-location (point))))
  524. (defun dabbrev--reset-global-variables ()
  525. "Initialize all global variables."
  526. (setq dabbrev--last-table nil
  527. dabbrev--last-abbreviation nil
  528. dabbrev--last-abbrev-location nil
  529. dabbrev--last-direction nil
  530. dabbrev--last-expansion nil
  531. dabbrev--last-expansion-location nil
  532. dabbrev--friend-buffer-list nil
  533. dabbrev--last-buffer nil
  534. dabbrev--last-buffer-found nil
  535. dabbrev--abbrev-char-regexp (or dabbrev-abbrev-char-regexp
  536. "\\sw\\|\\s_")
  537. dabbrev--check-other-buffers dabbrev-check-other-buffers))
  538. (defun dabbrev--select-buffers ()
  539. "Return a list of other buffers to search for a possible abbrev.
  540. The current buffer is not included in the list.
  541. This function makes a list of all the buffers returned by `buffer-list',
  542. then discards buffers whose names match `dabbrev-ignored-buffer-names'
  543. or `dabbrev-ignored-buffer-regexps'. It also discards buffers for which
  544. `dabbrev-friend-buffer-function', if it is bound, returns nil when called
  545. with the buffer as argument.
  546. It returns the list of the buffers that are not discarded."
  547. (dabbrev-filter-elements
  548. buffer (buffer-list)
  549. (and (not (eq (current-buffer) buffer))
  550. (not (dabbrev--ignore-buffer-p buffer))
  551. (boundp 'dabbrev-friend-buffer-function)
  552. (funcall dabbrev-friend-buffer-function buffer))))
  553. (defun dabbrev--try-find (abbrev reverse n ignore-case)
  554. "Search for ABBREV, backwards if REVERSE, N times.
  555. If IGNORE-CASE is non-nil, ignore case while searching.
  556. Return the expansion found, and save the location of the start
  557. of the expansion in `dabbrev--last-expansion-location'."
  558. (save-excursion
  559. (save-restriction
  560. (widen)
  561. (let ((expansion nil))
  562. (and dabbrev--last-expansion-location
  563. (goto-char dabbrev--last-expansion-location))
  564. (let ((case-fold-search ignore-case)
  565. (count n))
  566. (while (and (> count 0)
  567. (setq expansion (dabbrev--search
  568. abbrev reverse
  569. (and ignore-case
  570. (if (eq dabbrev-case-distinction
  571. 'case-replace)
  572. case-replace
  573. dabbrev-case-distinction)))))
  574. (setq count (1- count))))
  575. (and expansion
  576. (setq dabbrev--last-expansion-location (point)))
  577. expansion))))
  578. (defun dabbrev--find-all-expansions (abbrev ignore-case)
  579. "Return a list of all possible expansions of ABBREV.
  580. If IGNORE-CASE is non-nil, accept matches which differ in case."
  581. (let ((all-expansions nil)
  582. expansion)
  583. (save-excursion
  584. (goto-char (point-min))
  585. (while (setq expansion (dabbrev--find-expansion abbrev -1 ignore-case))
  586. (setq all-expansions (cons expansion all-expansions))))
  587. all-expansions))
  588. (defun dabbrev--ignore-buffer-p (buffer)
  589. "Return non-nil if BUFFER should be ignored by dabbrev."
  590. (let ((bn (buffer-name buffer)))
  591. (or (member bn dabbrev-ignored-buffer-names)
  592. (let ((tail dabbrev-ignored-buffer-regexps)
  593. (match nil))
  594. (while (and tail (not match))
  595. (setq match (string-match (car tail) bn)
  596. tail (cdr tail)))
  597. match))))
  598. (defun dabbrev--find-expansion (abbrev direction ignore-case)
  599. "Find one occurrence of ABBREV, and return the expansion.
  600. DIRECTION > 0 means look that many times backwards.
  601. DIRECTION < 0 means look that many times forward.
  602. DIRECTION = 0 means try both backward and forward.
  603. IGNORE-CASE non-nil means ignore case when searching.
  604. This sets `dabbrev--last-direction' to 1 or -1 according
  605. to the direction in which the occurrence was actually found.
  606. It sets `dabbrev--last-expansion-location' to the location
  607. of the start of the occurrence."
  608. (save-excursion
  609. ;; If we were scanning something other than the current buffer,
  610. ;; continue scanning there.
  611. (when dabbrev--last-buffer
  612. (set-buffer dabbrev--last-buffer))
  613. (or
  614. ;; ------------------------------------------
  615. ;; Look backward in current buffer.
  616. ;; ------------------------------------------
  617. (and (not dabbrev-search-these-buffers-only)
  618. (>= direction 0)
  619. (setq dabbrev--last-direction (min 1 direction))
  620. (dabbrev--try-find abbrev t
  621. (max 1 direction)
  622. ignore-case))
  623. ;; ------------------------------------------
  624. ;; Look forward in current buffer
  625. ;; or whatever buffer we were last scanning.
  626. ;; ------------------------------------------
  627. (and (or (not dabbrev-search-these-buffers-only)
  628. dabbrev--last-buffer)
  629. (<= direction 0)
  630. (setq dabbrev--last-direction -1)
  631. (dabbrev--try-find abbrev nil
  632. (max 1 (- direction))
  633. ignore-case))
  634. ;; ------------------------------------------
  635. ;; Look in other buffers.
  636. ;; Always start at (point-min) and look forward.
  637. ;; ------------------------------------------
  638. (progn
  639. (setq dabbrev--last-direction -1)
  640. (unless dabbrev--last-buffer
  641. ;; If we have just now begun to search other buffers,
  642. ;; determine which other buffers we should check.
  643. ;; Put that list in dabbrev--friend-buffer-list.
  644. (unless dabbrev--friend-buffer-list
  645. (setq dabbrev--friend-buffer-list
  646. (dabbrev--make-friend-buffer-list))
  647. (setq dabbrev--progress-reporter
  648. (make-progress-reporter
  649. "Scanning for dabbrevs..."
  650. (- (length dabbrev--friend-buffer-list)) 0 0 1 1.5))))
  651. ;; Walk through the buffers till we find a match.
  652. (let (expansion)
  653. (while (and (not expansion) dabbrev--friend-buffer-list)
  654. (setq dabbrev--last-buffer (pop dabbrev--friend-buffer-list))
  655. (set-buffer dabbrev--last-buffer)
  656. (progress-reporter-update dabbrev--progress-reporter
  657. (- (length dabbrev--friend-buffer-list)))
  658. (setq dabbrev--last-expansion-location (point-min))
  659. (setq expansion (dabbrev--try-find abbrev nil 1 ignore-case)))
  660. (progress-reporter-done dabbrev--progress-reporter)
  661. expansion)))))
  662. ;; Compute the list of buffers to scan.
  663. ;; If dabbrev-search-these-buffers-only, then the current buffer
  664. ;; is included in this list if it should be searched.
  665. ;; Otherwise, the current buffer is searched first specially.,
  666. ;; and it is not included in this list.
  667. (defun dabbrev--make-friend-buffer-list ()
  668. (let ((list (mapcar (function get-buffer)
  669. dabbrev-search-these-buffers-only)))
  670. (when (and (null dabbrev-search-these-buffers-only)
  671. dabbrev--check-other-buffers
  672. (or (eq dabbrev--check-other-buffers t)
  673. (setq dabbrev--check-other-buffers
  674. (y-or-n-p "Scan other buffers also? "))))
  675. (setq list (funcall dabbrev-select-buffers-function))
  676. ;; If dabbrev-check-all-buffers, tack on all the other
  677. ;; buffers at the end of the list, except those which are
  678. ;; specifically to be ignored.
  679. (if dabbrev-check-all-buffers
  680. (setq list
  681. (append list
  682. (dabbrev-filter-elements
  683. buffer (buffer-list)
  684. (and (not (memq buffer list))
  685. (not (dabbrev--ignore-buffer-p buffer)))))))
  686. ;; Remove the current buffer.
  687. (setq list (delq (current-buffer) list)))
  688. ;; Move buffers in the list that are visible on the screen
  689. ;; to the front of the list, but don't add anything to the list.
  690. (if list
  691. (walk-windows (lambda (w)
  692. (unless (eq w (selected-window))
  693. (if (memq (window-buffer w) list)
  694. (setq list
  695. (cons (window-buffer w)
  696. (delq (window-buffer w)
  697. list))))))))
  698. ;; In a minibuffer, search the buffer it was activated from,
  699. ;; first after the minibuffer itself. Unless we aren't supposed
  700. ;; to search the current buffer either.
  701. (if (and (window-minibuffer-p (selected-window))
  702. (not dabbrev-search-these-buffers-only))
  703. (setq list
  704. (cons (dabbrev--minibuffer-origin)
  705. (delq (dabbrev--minibuffer-origin) list))))
  706. list))
  707. (defun dabbrev--safe-replace-match (string &optional fixedcase literal)
  708. (if (eq major-mode 'picture-mode)
  709. (with-no-warnings
  710. (picture-replace-match string fixedcase literal))
  711. (replace-match string fixedcase literal)))
  712. ;;;----------------------------------------------------------------
  713. (defun dabbrev--substitute-expansion (old abbrev expansion record-case-pattern)
  714. "Replace OLD with EXPANSION in the buffer.
  715. OLD is text currently in the buffer, perhaps the abbreviation
  716. or perhaps another expansion that was tried previously.
  717. ABBREV is the abbreviation we are expanding.
  718. It is \" \" if we are copying subsequent words.
  719. EXPANSION is the expansion substring to be used this time.
  720. RECORD-CASE-PATTERN, if non-nil, means set `dabbrev--last-case-pattern'
  721. to record whether we upcased the expansion, downcased it, or did neither."
  722. ;;(undo-boundary)
  723. (let ((use-case-replace
  724. (and (if (eq dabbrev-case-fold-search 'case-fold-search)
  725. case-fold-search
  726. dabbrev-case-fold-search)
  727. (or (not dabbrev-upcase-means-case-search)
  728. (string= abbrev (downcase abbrev)))
  729. (if (eq dabbrev-case-replace 'case-replace)
  730. case-replace
  731. dabbrev-case-replace))))
  732. ;; If we upcased or downcased the original expansion,
  733. ;; do likewise for the subsequent words when we copy them.
  734. ;; Don't do any of the usual case processing, though.
  735. (when (equal abbrev " ")
  736. (if dabbrev--last-case-pattern
  737. (setq expansion
  738. (funcall dabbrev--last-case-pattern expansion)))
  739. (setq use-case-replace nil))
  740. ;; If the expansion has mixed case
  741. ;; and it is not simply a capitalized word,
  742. ;; or if the abbrev has mixed case,
  743. ;; and if the given abbrev's case pattern
  744. ;; matches the start of the expansion,
  745. ;; copy the expansion's case
  746. ;; instead of downcasing all the rest.
  747. ;;
  748. ;; Treat a one-capital-letter (possibly with preceding non-letter
  749. ;; characters) abbrev as "not all upper case", so as to force
  750. ;; preservation of the expansion's pattern if the expansion starts
  751. ;; with a capital letter.
  752. (let ((expansion-rest (substring expansion 1))
  753. (first-letter-position (string-match "[[:alpha:]]" abbrev)))
  754. (if (or (null first-letter-position)
  755. (and (not
  756. (and (or (string= expansion-rest (downcase expansion-rest))
  757. (string= expansion-rest (upcase expansion-rest)))
  758. (or (string= abbrev (downcase abbrev))
  759. (and (string= abbrev (upcase abbrev))
  760. (> (- (length abbrev) first-letter-position)
  761. 1)))))
  762. (string= abbrev
  763. (substring expansion 0 (length abbrev)))))
  764. (setq use-case-replace nil)))
  765. ;; If the abbrev and the expansion are both all-lower-case
  766. ;; then don't do any conversion. The conversion would be a no-op
  767. ;; for this replacement, but it would carry forward to subsequent words.
  768. ;; The goal of this is to prevent that carrying forward.
  769. (if (and (string= expansion (downcase expansion))
  770. (string= abbrev (downcase abbrev)))
  771. (setq use-case-replace nil))
  772. (if use-case-replace
  773. (setq expansion (downcase expansion)))
  774. ;; In case we insert subsequent words,
  775. ;; record if we upcased or downcased the first word,
  776. ;; in order to do likewise for subsequent words.
  777. (and record-case-pattern
  778. (setq dabbrev--last-case-pattern
  779. (and use-case-replace
  780. (cond ((equal abbrev (upcase abbrev)) 'upcase)
  781. ((equal abbrev (downcase abbrev)) 'downcase)))))
  782. ;; Convert whitespace to single spaces.
  783. (if dabbrev-eliminate-newlines
  784. (let ((pos
  785. (if (equal abbrev " ") 0 (length abbrev))))
  786. ;; If ABBREV is real, search after the end of it.
  787. ;; If ABBREV is space and we are copying successive words,
  788. ;; search starting at the front.
  789. (while (string-match "[\n \t]+" expansion pos)
  790. (setq pos (1+ (match-beginning 0)))
  791. (setq expansion (replace-match " " nil nil expansion)))))
  792. (if old
  793. (save-excursion
  794. (search-backward old))
  795. ;;(set-match-data (list (point-marker) (point-marker)))
  796. (search-backward abbrev)
  797. (search-forward abbrev))
  798. ;; Make case of replacement conform to case of abbreviation
  799. ;; provided (1) that kind of thing is enabled in this buffer
  800. ;; and (2) the replacement itself is all lower case.
  801. (dabbrev--safe-replace-match expansion
  802. (not use-case-replace)
  803. t)))
  804. ;;;----------------------------------------------------------------
  805. ;;; Search function used by dabbrevs library.
  806. (defun dabbrev--search (abbrev reverse ignore-case)
  807. "Search for something that could be used to expand ABBREV.
  808. Second arg, REVERSE, is t for reverse search, nil for forward.
  809. The variable `dabbrev-limit' controls the maximum search region size.
  810. Third argument IGNORE-CASE non-nil means treat case as insignificant while
  811. looking for a match and when comparing with previous matches. Also if
  812. that's non-nil and the match is found at the beginning of a sentence
  813. and is in lower case except for the initial then it is converted to
  814. all lower case for return.
  815. Table of expansions already seen is examined in buffer
  816. `dabbrev--last-table' so that only distinct possibilities are found
  817. by dabbrev-re-expand.
  818. Returns the expansion found, or nil if not found.
  819. Leaves point at the location of the start of the expansion."
  820. (save-match-data
  821. (let ((pattern1 (concat (regexp-quote abbrev)
  822. "\\(" dabbrev--abbrev-char-regexp "\\)"))
  823. (pattern2 (concat (regexp-quote abbrev)
  824. "\\(\\(" dabbrev--abbrev-char-regexp "\\)+\\)"))
  825. ;; This makes it possible to find matches in minibuffer prompts
  826. ;; even when they are "inviolable".
  827. (inhibit-point-motion-hooks t)
  828. found-string result)
  829. ;; Limited search.
  830. (save-restriction
  831. (and dabbrev-limit
  832. (narrow-to-region
  833. dabbrev--last-expansion-location
  834. (+ (point) (if reverse (- dabbrev-limit) dabbrev-limit))))
  835. ;;--------------------------------
  836. ;; Look for a distinct expansion, using dabbrev--last-table.
  837. ;;--------------------------------
  838. (while (and (not found-string)
  839. (if reverse
  840. (re-search-backward pattern1 nil t)
  841. (re-search-forward pattern1 nil t)))
  842. (goto-char (match-beginning 0))
  843. ;; In case we matched in the middle of a word,
  844. ;; back up to start of word and verify we still match.
  845. (dabbrev--goto-start-of-abbrev)
  846. (if (not (looking-at pattern1))
  847. nil
  848. ;; We have a truly valid match. Find the end.
  849. (re-search-forward pattern2)
  850. (setq found-string (match-string-no-properties 0))
  851. (setq result found-string)
  852. (and ignore-case (setq found-string (downcase found-string)))
  853. ;; Ignore this match if it's already in the table.
  854. (if (dabbrev-filter-elements
  855. table-string dabbrev--last-table
  856. (string= found-string table-string))
  857. (setq found-string nil)))
  858. ;; Prepare to continue searching.
  859. (goto-char (if reverse (match-beginning 0) (match-end 0))))
  860. ;; If we found something, use it.
  861. (when found-string
  862. ;; Put it into `dabbrev--last-table'
  863. ;; and return it (either downcased, or as is).
  864. (setq dabbrev--last-table
  865. (cons found-string dabbrev--last-table))
  866. result)))))
  867. (dolist (mess '("^No dynamic expansion for .* found"
  868. "^No further dynamic expansion for .* found$"
  869. "^No possible abbreviation preceding point$"))
  870. (add-to-list 'debug-ignored-errors mess))
  871. (provide 'dabbrev)
  872. ;;; dabbrev.el ends here