home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / dabbrev.el < prev    next >
Encoding:
Text File  |  1995-08-26  |  31.9 KB  |  854 lines

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