home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / emacs / sources / 930 < prev    next >
Encoding:
Internet Message Format  |  1993-01-10  |  27.4 KB

  1. Path: sparky!uunet!spool.mu.edu!hri.com!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!kth.se!News.kth.se!aho
  2. From: aho@thalamus.sans.kth.se (Anders Holst)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: hippie-expand.el -- Now Even Better !!
  5. Message-ID: <AHO.93Jan10151608@thalamus.sans.kth.se>
  6. Date: 10 Jan 93 14:16:08 GMT
  7. Sender: usenet@kth.se (Usenet)
  8. Organization: /home/aho/.organization
  9. Lines: 743
  10. Nntp-Posting-Host: thalamus.sans.kth.se
  11.  
  12. Or at least slightly updated and revised.
  13.  
  14. Hippie-expand is a function for text completion trying various ways to
  15. complete in succession. It cycles through expansions of e.g. the
  16. following types: file name completion, dabbrev, dabbrev all buffers,
  17. line completion and elisp symbol completion. Exactly which
  18. "try-functions" (i.e. kind of expansions) to try and in which order is
  19. determined by the variable `hippie-expand-try-functions-list'.
  20.  
  21. New things in this version includes:
  22. * Some new try-functions (try-complete-file-name-partially,
  23.   try-complete-lisp-symbol-partially and try-expand-line-all-buffers) 
  24. * Case handling in (nearly) the same way as dabbrev-expand, i.e. if
  25.   `case-replace' is enabled in the buffer, it tries to give the
  26.   expansion the same case as the abbreviation had.
  27. * A macro `make-hippie-expand-function' that allows different
  28.   hippie-expand-like functions that uses different try-function lists. 
  29. * Hippie-expand can be made to tell which try-function it has used
  30.   (the variable `hippie-expand-verbose').
  31. * Numerical arguments make hippie-expand skip one or more functions in
  32.   `hippie-expand-try-functions-list', if they get tiresome.
  33. Also, the copyright is now transferred to FSF, so please throw away
  34. the last version and replace it with this instead.
  35.  
  36.  
  37.     DIGRESSION ABOUT CASE HANDLING
  38.  
  39. The case handling in hippie-expand is just nearly as in dabbrev-expand.
  40. The difference is that dabbrev never changes the case of the expansion
  41. from uppercase to lowercase (or to capitalized), even if the 
  42. abbreviation is written in lowercase. Hippie-expand treats case
  43. symmetrically: if both the found expansion and the abbreviation has
  44. recognizable cases (not mixed) then the expansion will always get its
  45. case from the abbreviation.  
  46.  
  47. I think that this symmetrical solution gives maximal control over the
  48. case when writing the abbreviation. If I write the word lowercase I
  49. will get it lowercase, regardless of whether it occurred uppercase in
  50. some header the last time, or perhaps as output from some silly
  51. common-lisp that outputs all atoms in uppercase.
  52. If I instead want the word emphasized with uppercase this time I can
  53. begin with uppercase. Even if I explicitly want the same case as the
  54. last time and I don't remember what it was, I can write the abbrev in
  55. some mixed case (like xX) for which no transfer of case occurs.
  56. Still, variables and symbols with mixed case will stay that way, even
  57. if I for convenience write the abbrev in lowercase only. 
  58.  
  59. I would like to know a little of what other people think about this.
  60. Is it better than the old behavior (that of dabbrev) ? Are there any
  61. specific reasons for the old behavior ? 
  62.  
  63. If you have some opinions about this, please mail me: aho@sans.kth.se
  64. (Not to clutter up this group with a lot of talk). 
  65.  
  66.     END OF DIGRESSION
  67.  
  68.  
  69. Enough talk. Here's the code:
  70.  
  71. ;; Expand text trying various ways to find its expansion.
  72. ;; Copyright (C) 1992 Free Software Foundation, Inc.
  73. ;;
  74. ;; This file is part of GNU Emacs.
  75.  
  76. ;; GNU Emacs is free software; you can redistribute it and/or modify
  77. ;; it under the terms of the GNU General Public License as published by
  78. ;; the Free Software Foundation; either version 2, or (at your option)
  79. ;; any later version.
  80.  
  81. ;; GNU Emacs is distributed in the hope that it will be useful,
  82. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  83. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  84. ;; GNU General Public License for more details.
  85.  
  86. ;; You should have received a copy of the GNU General Public License
  87. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  88. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  89. ;; 
  90. ;;  Author: Anders Holst (aho@sans.kth.se)
  91. ;;
  92. ;;  Last change: 4 January 1993
  93.  
  94. ;;  
  95. ;;  DESCRIPTION
  96. ;;  
  97. ;;  `hippie-expand' is a single function for a lot of different kinds
  98. ;;  of completions and expansions.  Called repeatedly it tries all
  99. ;;  possible completions in succession. 
  100. ;;  Which kinds of completions to try, and in which order, is
  101. ;;  determined by the contents of `hippie-expand-try-functions-list'.
  102. ;;  Much customization of `hippie-expand' can be made by changing the
  103. ;;  order of, removing, or inserting new functions in this list.
  104. ;;  Given a positive numeric argument, `hippie-expand' jumps directly
  105. ;;  ARG functions forward in this list.  Given some other argument
  106. ;;  (a negative argument or just Ctrl-U) it undoes the tried
  107. ;;  completion.
  108. ;;  If the variable `hippie-expand-verbose' is non-nil, `hippie-expand'
  109. ;;  outputs in a message which try-function in the list that is used
  110. ;;  currently (ie. was used currently and will be tried first the next
  111. ;;  time).
  112. ;;  See also the macro `make-hippie-expand-function' below.
  113. ;;  
  114. ;;  A short description of the current try-functions in this file:
  115. ;;    `try-complete-file-name' : very convenient to have in any buffer,
  116. ;;      and not just in the minibuffer or (some) shell-mode.  It goes
  117. ;;      through all possible completions instead of just completing as
  118. ;;      much as is unique.
  119. ;;    `try-complete-file-name-partially' : To insert in the list just
  120. ;;      before `try-complete-file-name' for those who want first to get
  121. ;;      a file name completed only as many characters as is unique.
  122. ;;      (NOTE: Not by default in `hippie-expand-try-functions-list'.)
  123. ;;    `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
  124. ;;      Otherwise it looks through all abbrev-tables, starting with
  125. ;;      the local followed by the global. 
  126. ;;    `try-expand-line' : Searches the buffer for an entire line that 
  127. ;;      begins exactly as the current line.  Convenient sometimes, for 
  128. ;;      example as a substitute for (or complement to) the history
  129. ;;      list in shell-like buffers.  Remove it if you find it confusing.
  130. ;;    `try-expand-line-all-buffers' : Like `try-expand-line' but searches
  131. ;;      in all buffers (except the current).  (This may be a little
  132. ;;      slow, don't use it unless you are really fond of `hippie-expand'.
  133. ;;      NOTE: Not by default in hippie-expand-try-functions-list.)
  134. ;;    `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
  135. ;;      course in a way compatible with the other try-functions).
  136. ;;    `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
  137. ;;      like `dabbrev-expand' but searches all Emacs buffers (except the
  138. ;;      current) for matching words.  (No, I don't find this one
  139. ;;      particularly slow.) 
  140. ;;    `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes
  141. ;;      through all possibilities instead of completing what is unique.
  142. ;;      Might be tedious (usually a lot of possible completions) and
  143. ;;      since its function is much like `lisp-complete-symbol', which
  144. ;;      already has a key of its own, you might want to remove this.
  145. ;;    `try-complete-lisp-symbol-partially' : To insert in the list just
  146. ;;      before `try-complete-lisp-symbol' for those who first want to get
  147. ;;      completion of what is unique in the name.  (NOTE: Not by
  148. ;;      default in hippie-expand-try-functions-list.)
  149. ;;
  150. ;;  To write new try-functions, consider the following:
  151. ;;  Each try-function takes one argument OLD which is nil the first
  152. ;;  time the function is called and true in succeeding calls for the
  153. ;;  same string to complete.  The first time the function has to
  154. ;;  extract the string before point to complete, and substitute the
  155. ;;  first completion alternative for it.  On following calls it has to
  156. ;;  substitute the next possible completion for the last tried string.
  157. ;;  The try-function is to return t as long as it finds new
  158. ;;  possible completions.  When there are no more alternatives it has
  159. ;;  to restore the text before point to its original contents, and
  160. ;;  return nil (don't beep or message or anything).
  161. ;;  The try-function can (should) use the following functions:
  162. ;;    `he-init-string' : Initializes the text to substitute to the
  163. ;;      contents of the region BEGIN to END.  Also sets the variable
  164. ;;      `he-search-string' to the text to expand.
  165. ;;    `he-substitute-string' : substitutes STR into the region
  166. ;;      initialized with `he-init-string'.  (An optional second argument
  167. ;;      TRANS-CASE non-nil, means transfer of case from the abbreviation
  168. ;;      to the expansion is ok if that is enabled in the buffer.)
  169. ;;    `he-reset-string' : Resets the initialized region to its original
  170. ;;      contents.
  171. ;;  There is also a variable: `he-tried-table' which is meant to contain
  172. ;;  all tried expansions so far.  The try-function can check this 
  173. ;;  variable to see whether an expansion has already been tried
  174. ;;  (hint: `he-string-member'), and add its own tried expansions to it.
  175. ;;
  176. ;;
  177. ;;  INSTALLATION
  178. ;;
  179. ;;  To install this file, put it in your load-path, and put the
  180. ;;  following or something similar in your .emacs :
  181. ;;  
  182. ;;    (autoload 'hippie-expand "hippie-expand" "Try to expand text.")
  183. ;;    (define-key esc-map " " 'hippie-expand)
  184. ;;
  185. ;;  (Note that hippie-expand must be bound to a key to work properly.)
  186. ;;
  187. ;;  
  188. ;;  KNOWN BUGS
  189. ;;
  190. ;;  It may happen that some completion suggestion occurs twice, in
  191. ;;  spite of the use of `he-tried-table' to prevent that.  This is 
  192. ;;  because different try-functions may try to complete different
  193. ;;  lengths of text, and thus put different amounts of the
  194. ;;  text in `he-try-table'.  Anyway this seems to occur seldom enough not
  195. ;;  to be too disturbing.  Also it should NOT bee possible for the
  196. ;;  opposite situation to occur, that `hippie-expand' misses some
  197. ;;  suggestion because it thinks it has already tried it.
  198. ;;
  199. ;;  
  200. ;;  ACKNOWLEDGEMENT
  201. ;;
  202. ;;  I want to thank Mikael Djurfeldt in discussions with whom the idea
  203. ;;  of this function took form.
  204. ;;  I am also grateful to all those who have given me suggestions on
  205. ;;  how to improve it.
  206. ;;
  207.  
  208.  
  209. (defvar he-num -1)
  210.  
  211. (defvar he-string-beg ())
  212.  
  213. (defvar he-string-end ())
  214.  
  215. (defvar he-search-string ())
  216.  
  217. (defvar he-expand-list ())
  218.  
  219. (defvar he-tried-table ())
  220.  
  221. (defvar he-search-loc ())
  222.  
  223. (defvar he-search-bw ())
  224.  
  225. (defvar he-search-bufs ())
  226.  
  227. (defvar hippie-expand-try-functions-list '(try-complete-file-name
  228.                        try-expand-all-abbrevs
  229.                        try-expand-line
  230.                        try-expand-dabbrev
  231.                        try-expand-dabbrev-all-buffers
  232.                        try-complete-lisp-symbol)
  233.   "The list of expansion functions tried in order by `hippie-expand'.
  234. To change the behavior of `hippie-expand', remove, change the order of,
  235. or insert functions in this list.")
  236.  
  237. (defvar hippie-expand-verbose t
  238.   "*Non-nil makes `hippie-expand' output which function it is trying.")
  239.  
  240. (defun hippie-expand (arg)
  241.   "Try to expand text before point, using multiple methods.
  242. The expansion functions in `hippie-expand-try-functions-list' are
  243. tried in order, until a possible expansion is found.  Repeated
  244. application of `hippie-expand' inserts successively possible
  245. expansions.  
  246. With a positive numeric argument, jumps directly to the ARG next
  247. function in this list.  With a negative argument or just \\[universal-argument],
  248. undoes the expansion." 
  249.   (interactive "P")
  250.   (if (or (not arg) 
  251.       (and (integerp arg) (> arg 0)))
  252.       (let ((first (or (= he-num -1)
  253.                (not (equal this-command last-command)))))
  254.     (if first
  255.         (progn
  256.           (setq he-num -1)
  257.           (setq he-tried-table nil)))
  258.     (if arg
  259.         (if (not first) (he-reset-string))
  260.         (setq arg 0))
  261.     (let ((i (max (+ he-num arg) 0)))
  262.       (while (not (or (>= i (length hippie-expand-try-functions-list))
  263.               (apply (nth i hippie-expand-try-functions-list) 
  264.                  (list (= he-num i)))))
  265.         (setq i (1+ i)))
  266.       (setq he-num i))
  267.     (if (>= he-num (length hippie-expand-try-functions-list))
  268.         (progn
  269.           (setq he-num -1)
  270.           (if first
  271.           (message "No expansion found")
  272.           (message "No further expansions found"))
  273.           (ding))
  274.         (if hippie-expand-verbose
  275.         (message (concat "Using "
  276.                  (prin1-to-string (nth he-num 
  277.                    hippie-expand-try-functions-list)))))))
  278.       (if (>= he-num 0)
  279.       (progn
  280.         (setq he-num -1)
  281.         (he-reset-string)
  282.         (if hippie-expand-verbose
  283.         (message "Undoing expansions"))))))
  284.       
  285. ;; Initializes the region to expand (to between BEG and END).
  286. (defun he-init-string (beg end)
  287.   (setq he-string-beg beg)
  288.   (setq he-string-end end)
  289.   (setq he-search-string (buffer-substring beg end)))
  290.  
  291. ;; Resets the expanded region to its original contents.
  292. (defun he-reset-string ()
  293.   (delete-region he-string-beg he-string-end)
  294.   (insert he-search-string)
  295.   (setq he-string-end (point)))
  296.  
  297. ;; Substitutes an expansion STR into the correct region (the region
  298. ;; initialized with `he-init-string'). 
  299. ;; An optional argument TRANS-CASE means that it is ok to transfer case
  300. ;; from the abbreviation to the expansion if that is possible, and is
  301. ;; enabled in the buffer.
  302. (defun he-substitute-string (str &optional trans-case)
  303.   (let ((trans-case (and trans-case
  304.              case-replace
  305.              case-fold-search
  306.              (he-transfer-case-ok str he-search-string))))
  307.     (he-reset-string)
  308.     (goto-char he-string-beg)
  309.     (search-forward he-search-string)
  310.     (replace-match (if trans-case (downcase str) str)
  311.            (not trans-case)
  312.            'literal)
  313.     (setq he-string-end (point))))
  314.  
  315. (defun he-ordinary-case-p (str)
  316.   (or (string= str (downcase str))
  317.       (string= str (upcase str))
  318.       (string= str (capitalize str))))
  319.  
  320. (defun he-transfer-case-ok (to-str from-str)
  321.   (and (not (string= from-str (substring to-str 0 (length from-str))))
  322.          ;; otherwise transfer is not needed (and this also solves
  323.      ;; some obscure situations)
  324.        (he-ordinary-case-p to-str)
  325.          ;; otherwise case may be significant 
  326.        (he-ordinary-case-p from-str)
  327.          ;; otherwise replace-match wont know what to do
  328.   ))
  329.  
  330. ;; Check if STR is a member of LST.
  331. ;; Ignore case if `case-replace' and `case-fold-search' are both t.
  332. (defun he-string-member (str lst)
  333.   (while (and lst
  334.           (not
  335.            (if (and case-fold-search case-replace)
  336.            (string= (downcase (car lst)) (downcase str))
  337.            (string= (car lst) str))))
  338.     (setq lst (cdr lst)))
  339.   lst)
  340.  
  341. ;;  For the real hippie-expand enthusiast: A macro that makes it
  342. ;;  possible to use many functions like hippie-expand, but with
  343. ;;  different try-functions-lists.
  344. ;;  Usage is for example:
  345. ;;    (fset 'my-complete-file (make-hippie-expand-function
  346. ;;                             '(try-complete-file-name-partially
  347. ;;                               try-complete-file-name)))
  348. ;;    (fset 'my-complete-line (make-hippie-expand-function
  349. ;;                             '(try-expand-line
  350. ;;                               try-expand-line-all-buffers)))
  351. ;;  
  352. (defmacro make-hippie-expand-function (try-list &optional verbose)
  353.   "Construct a function similar to `hippie-expand'.
  354. Make it use the expansion functions in TRY-LIST.  An optional second
  355. argument VERBOSE non-nil makes the function verbose."
  356.   (` '(lambda (arg)
  357.        (, (concat 
  358.            "Try to expand text before point, using the following functions: \n"
  359.        (mapconcat 'prin1-to-string (eval try-list) ", ")))
  360.        (interactive "P")
  361.        (let ((hippie-expand-try-functions-list (, try-list))
  362.          (hippie-expand-verbose (, verbose)))
  363.      (hippie-expand arg)))))
  364.  
  365.  
  366. ;;;  Here follows the try-functions and their requisites:
  367.  
  368. (defun try-complete-file-name (old)
  369.   "Try to complete text as a file name.
  370. The argument OLD has to be nil the first call of this function, and t
  371. for subsequent calls (for further possible completions of the same
  372. string).  It returns t if a new completion is found, nil otherwise."
  373.   (if (not old)
  374.       (progn 
  375.     (he-init-string (he-file-name-beg) (point))
  376.     (let ((name-part (file-name-nondirectory he-search-string))
  377.           (dir-part (expand-file-name (or (file-name-directory
  378.                            he-search-string) ""))))
  379.       (if (not (he-string-member name-part he-tried-table))
  380.           (setq he-tried-table (cons name-part he-tried-table)))
  381.       (if (and (not (equal he-search-string ""))
  382.            (file-directory-p dir-part))
  383.           (setq he-expand-list (sort (file-name-all-completions 
  384.                       name-part
  385.                       dir-part)
  386.                      'string-lessp))
  387.           (setq he-expand-list ())))))
  388.  
  389.   (while (and he-expand-list
  390.           (he-string-member (car he-expand-list) he-tried-table))
  391.     (setq he-expand-list (cdr he-expand-list)))
  392.   (if (null he-expand-list)
  393.       (progn
  394.     (he-reset-string)
  395.     ())
  396.       (let ((filename (concat (file-name-directory he-search-string)
  397.                   (car he-expand-list))))
  398.     (he-substitute-string filename)
  399.     (setq he-tried-table (cons (car he-expand-list) he-tried-table))
  400.     (setq he-expand-list (cdr he-expand-list))
  401.     t)))
  402.  
  403. (defun try-complete-file-name-partially (old)
  404.   "Try to complete text as a file name, as many characters as unique.
  405. The argument OLD has to be nil the first call of this function.  It
  406. returns t if a unique, possibly partial, completion is found, nil 
  407. otherwise."
  408.   (let ((expansion ()))
  409.     (if (not old)
  410.     (progn 
  411.       (he-init-string (he-file-name-beg) (point))
  412.       (let ((name-part (file-name-nondirectory he-search-string))
  413.         (dir-part (expand-file-name (or (file-name-directory
  414.                          he-search-string) ""))))
  415.         (if (and (not (equal he-search-string ""))
  416.              (file-directory-p dir-part))
  417.         (setq expansion (file-name-completion name-part
  418.                               dir-part)))
  419.         (if (or (eq expansion t)
  420.             (string= expansion name-part))
  421.         (setq expansion ())))))
  422.  
  423.     (if (not expansion)
  424.     (progn
  425.       (he-reset-string)
  426.       ())
  427.     (let ((filename (concat (file-name-directory he-search-string)
  428.                 expansion)))
  429.       (he-substitute-string filename)
  430.       (setq he-tried-table (cons expansion he-tried-table))
  431.       t))))
  432.  
  433. (defun he-file-name-beg ()
  434.   (let ((skips "-a-zA-Z0-9_./~^#$"))
  435.     (save-excursion
  436.       (skip-chars-backward skips)
  437.       (point))))
  438.  
  439. (defun try-complete-lisp-symbol (old)
  440.   "Try to complete word as an Emacs Lisp symbol.
  441. The argument OLD has to be nil the first call of this function, and t
  442. for subsequent calls (for further possible completions of the same
  443. string).  It returns t if a new completion is found, nil otherwise."
  444.   (if (not old)
  445.       (progn 
  446.     (he-init-string (he-lisp-symbol-beg) (point))
  447.     (if (not (he-string-member he-search-string he-tried-table))
  448.         (setq he-tried-table (cons he-search-string he-tried-table)))
  449.     (setq he-expand-list 
  450.           (and (not (equal he-search-string ""))
  451.            (sort (all-completions he-search-string obarray
  452.                       (function (lambda (sym)
  453.                         (or (boundp sym)
  454.                         (fboundp sym)
  455.                         (symbol-plist sym)))))
  456.              'string-lessp)))))
  457.   (while (and he-expand-list
  458.           (he-string-member (car he-expand-list) he-tried-table))
  459.     (setq he-expand-list (cdr he-expand-list)))
  460.   (if (null he-expand-list)
  461.       (progn
  462.     (he-reset-string)
  463.     ())
  464.       (progn
  465.     (he-substitute-string (car he-expand-list))
  466.     (setq he-tried-table (cons (car he-expand-list) he-tried-table))
  467.     (setq he-expand-list (cdr he-expand-list))
  468.     t)))
  469.  
  470. (defun try-complete-lisp-symbol-partially (old)
  471.   "Try to complete as an Emacs Lisp symbol, as many characters as unique.
  472. The argument OLD has to be nil the first call of this function.  It
  473. returns t if a unique, possibly partial, completion is found, nil 
  474. otherwise."
  475.   (let ((expansion ()))
  476.     (if (not old)
  477.     (progn 
  478.       (he-init-string (he-lisp-symbol-beg) (point))
  479.       (if (not (string= he-search-string ""))
  480.           (setq expansion 
  481.             (try-completion he-search-string obarray
  482.                     (function (lambda (sym)
  483.                       (or (boundp sym)
  484.                       (fboundp sym)
  485.                       (symbol-plist sym)))))))
  486.       (if (or (eq expansion t)
  487.           (string= expansion he-search-string))
  488.           (setq expansion ()))))
  489.  
  490.   (if (not expansion)
  491.       (progn
  492.     (he-reset-string)
  493.     ())
  494.       (progn
  495.     (he-substitute-string expansion)
  496.     (setq he-tried-table (cons expansion he-tried-table))
  497.     t))))
  498.  
  499. (defun he-lisp-symbol-beg ()
  500.   (let ((skips "-a-zA-Z0-9_."))
  501.     (save-excursion
  502.       (skip-chars-backward skips)
  503.       (point))))
  504.  
  505. (defun try-expand-line (old)
  506.   "Try to complete the current line to an entire line in the buffer.
  507. The argument OLD has to be nil the first call of this function, and t
  508. for subsequent calls (for further possible completions of the same
  509. string).  It returns t if a new completion is found, nil otherwise."
  510.   (let ((expansion ())
  511.     (strip-prompt (and (get-buffer-process (current-buffer))
  512.                shell-prompt-pattern)))
  513.     (if (not old)
  514.     (progn
  515.       (he-init-string (he-line-beg strip-prompt) (point))
  516.       (setq he-search-loc he-string-beg)
  517.       (setq he-search-bw t)))
  518.  
  519.     (if (not (equal he-search-string ""))
  520.     (save-excursion
  521.       ;; Try looking backward unless inhibited.
  522.       (if he-search-bw
  523.           (progn 
  524.         (goto-char he-search-loc)
  525.         (setq expansion (he-line-search he-search-string
  526.                         strip-prompt t))
  527.         (setq he-search-loc (point-marker))
  528.         (if (not expansion)
  529.             (progn
  530.               (setq he-search-loc he-string-end)
  531.               (setq he-search-bw ())))))
  532.       
  533.       (if (not expansion) ; Then look forward.
  534.           (progn 
  535.         (goto-char he-search-loc)
  536.         (setq expansion (he-line-search he-search-string 
  537.                         strip-prompt nil))
  538.         (setq he-search-loc (point-marker))))))
  539.  
  540.     (if (not expansion)
  541.     (progn
  542.       (he-reset-string)
  543.       ())
  544.     (progn
  545.       (he-substitute-string expansion t)
  546.       (setq he-tried-table (cons expansion he-tried-table))
  547.       t))))
  548.  
  549. (defun try-expand-line-all-buffers (old)
  550.   "Try to complete the current line, searching all other buffers.
  551. The argument OLD has to be nil the first call of this function, and t
  552. for subsequent calls (for further possible completions of the same
  553. string).  It returns t if a new completion is found, nil otherwise."
  554.   (let ((expansion ())
  555.     (strip-prompt (and (get-buffer-process (current-buffer))
  556.                shell-prompt-pattern))
  557.     (buf (current-buffer)))
  558.     (if (not old)
  559.     (progn
  560.       (he-init-string (he-line-beg strip-prompt) (point))
  561.       (setq he-search-loc 0)
  562.       (setq he-search-bufs (buffer-list))))
  563.  
  564.     (if (not (equal he-search-string ""))
  565.     (while (and he-search-bufs (not expansion))
  566.       (set-buffer (car he-search-bufs))
  567.       (if (and (not (eq (current-buffer) buf))
  568.            (not (eq major-mode 'dired-mode)))
  569.            ;; dont search dired buffers
  570.           (save-excursion
  571.         (goto-char he-search-loc)
  572.         (setq expansion (he-line-search he-search-string
  573.                         strip-prompt nil))
  574.         (setq he-search-loc (point-marker))))
  575.       (if expansion
  576.           (setq he-tried-table (cons expansion he-tried-table))
  577.           (progn
  578.         (setq he-search-loc 0)
  579.         (setq he-search-bufs (cdr he-search-bufs))))))
  580.  
  581.     (set-buffer buf)
  582.     (if (not expansion)
  583.     (progn
  584.       (he-reset-string)
  585.       ())
  586.     (progn
  587.       (he-substitute-string expansion t)
  588.       t))))
  589.  
  590. (defun he-line-search (str strip-prompt reverse) 
  591.   (let ((result ()))
  592.     (while (and (not result)
  593.         (if reverse
  594.             (re-search-backward 
  595.              (he-line-search-regexp str strip-prompt)
  596.              nil t)
  597.             (re-search-forward
  598.              (he-line-search-regexp str strip-prompt)
  599.              nil t)))
  600.       (setq result (buffer-substring (match-beginning 2) (match-end 2)))
  601.       (if (he-string-member result he-tried-table)
  602.       (setq result nil)))                ; if already in table, ignore
  603.     result))
  604.  
  605. (defun he-line-beg (strip-prompt)
  606.   (save-excursion
  607.     (end-of-line)
  608.     (if (re-search-backward (he-line-search-regexp "" strip-prompt) 
  609.                 (save-excursion (beginning-of-line)
  610.                         (point)) t)
  611.     (match-beginning 2)
  612.       (beginning-of-line)
  613.       (point))))
  614.  
  615. (defun he-line-search-regexp (pat strip-prompt)
  616.   (if strip-prompt
  617.       (concat "\\(" shell-prompt-pattern "\\|^\\s-*\\)\\("
  618.           (regexp-quote pat)
  619.           "[^\n]*[^ \t\n]\\)")
  620.       (concat "^\\(\\s-*\\)\\(" 
  621.           (regexp-quote pat)
  622.           "[^\n]*[^ \t\n]\\)")))
  623.  
  624. (defun try-expand-all-abbrevs (old)
  625.   "Try to expand word before point according to all abbrev tables.
  626. The argument OLD has to be nil the first call of this function, and t
  627. for subsequent calls (for further possible expansions of the same
  628. string).  It returns t if a new expansion is found, nil otherwise."
  629.   (if (not old)
  630.       (progn
  631.     (he-init-string (he-dabbrev-beg) (point))
  632.     (setq he-expand-list 
  633.           (and (not (equal he-search-string ""))
  634.            (mapcar (function (lambda (sym)
  635.                  (abbrev-expansion he-search-string 
  636.                            (eval sym))))
  637.                (append '(local-abbrev-table 
  638.                      global-abbrev-table)
  639.                    abbrev-table-name-list))))))
  640.   (while (and he-expand-list
  641.           (or (not (car he-expand-list))
  642.           (he-string-member (car he-expand-list) he-tried-table)))
  643.     (setq he-expand-list (cdr he-expand-list)))
  644.   (if (null he-expand-list)
  645.       (progn
  646.     (he-reset-string)
  647.     ())
  648.       (progn
  649.     (he-substitute-string (car he-expand-list))
  650.     (setq he-tried-table (cons (car he-expand-list) he-tried-table))
  651.     (setq he-expand-list (cdr he-expand-list))
  652.     t)))
  653.  
  654. (defun try-expand-dabbrev (old)
  655.   "Try to expand word \"dynamically\", searching the current buffer.
  656. The argument OLD has to be nil the first call of this function, and t
  657. for subsequent calls (for further possible expansions of the same
  658. string).  It returns t if a new expansion is found, nil otherwise."
  659.   (let ((expansion ()))
  660.     (if (not old)
  661.     (progn
  662.       (he-init-string (he-dabbrev-beg) (point))
  663.       (setq he-search-loc he-string-beg)
  664.       (setq he-search-bw t)))
  665.  
  666.     (if (not (equal he-search-string ""))
  667.     (save-excursion
  668.       ;; Try looking backward unless inhibited.
  669.       (if he-search-bw
  670.           (progn 
  671.         (goto-char he-search-loc)
  672.         (setq expansion (he-dab-search he-search-string t))
  673.         (setq he-search-loc (point-marker))
  674.         (if (not expansion)
  675.             (progn
  676.               (setq he-search-loc he-string-end)
  677.               (setq he-search-bw ())))))
  678.       
  679.       (if (not expansion) ; Then look forward.
  680.           (progn 
  681.         (goto-char he-search-loc)
  682.         (setq expansion (he-dab-search he-search-string nil))
  683.         (setq he-search-loc (point-marker))))))
  684.     
  685.     (if (not expansion)
  686.     (progn
  687.       (he-reset-string)
  688.       ())
  689.     (progn
  690.       (he-substitute-string expansion t)
  691.       (setq he-tried-table (cons expansion he-tried-table))
  692.       t))))
  693.  
  694. (defun try-expand-dabbrev-all-buffers (old)
  695.   "Tries to expand word \"dynamically\", searching all other buffers.
  696. The argument OLD has to be nil the first call of this function, and t
  697. for subsequent calls (for further possible expansions of the same
  698. string).  It returns t if a new expansion is found, nil otherwise."
  699.   (let ((expansion ())
  700.     (buf (current-buffer)))
  701.     (if (not old)
  702.     (progn
  703.       (he-init-string (he-dabbrev-beg) (point))
  704.       (setq he-search-loc 0)
  705.       (setq he-search-bufs (buffer-list))))
  706.  
  707.     (if (not (equal he-search-string ""))
  708.     (while (and he-search-bufs (not expansion))
  709.       (set-buffer (car he-search-bufs))
  710.       (if (and (not (eq (current-buffer) buf))
  711.            (not (eq major-mode 'dired-mode))) 
  712.            ;; dont search dired buffers
  713.           (save-excursion
  714.         (goto-char he-search-loc)
  715.         (setq expansion (he-dab-search he-search-string nil))
  716.         (setq he-search-loc (point-marker))))
  717.       (if expansion
  718.           (setq he-tried-table (cons expansion he-tried-table))
  719.           (progn
  720.         (setq he-search-loc 0)
  721.         (setq he-search-bufs (cdr he-search-bufs))))))
  722.  
  723.     (set-buffer buf)
  724.     (if (not expansion)
  725.     (progn
  726.       (he-reset-string)
  727.       ())
  728.     (progn
  729.       (he-substitute-string expansion t)
  730.       t))))
  731.  
  732. (defun he-dab-search-regexp (pat)
  733.   (concat "\\b" (regexp-quote pat) 
  734.       "\\(\\sw\\|\\s_\\)+"))
  735.  
  736. (defun he-dab-search (pattern reverse)
  737.   (let ((result ()))
  738.     (while (and (not result) 
  739.         (if reverse
  740.              (re-search-backward (he-dab-search-regexp pattern)
  741.                      nil t)
  742.              (re-search-forward (he-dab-search-regexp pattern)
  743.                     nil t)))
  744.       (setq result (buffer-substring (match-beginning 0) (match-end 0)))
  745.       (if (he-string-member result he-tried-table)
  746.       (setq result nil)))                ; if already in table, ignore
  747.     result))
  748.  
  749. (defun he-dabbrev-beg ()
  750.   (let ((skips "-a-zA-Z0-9_."))
  751.     (save-excursion
  752.       (skip-chars-backward skips)
  753.       (skip-chars-forward "-_.")
  754.       (point))))
  755.