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