dabbrev.el 38 KB

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