hippie-exp.el 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. ;;; hippie-exp.el --- expand text trying various ways to find its expansion
  2. ;; Copyright (C) 1992, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Anders Holst <aho@sans.kth.se>
  4. ;; Last change: 3 March 1998
  5. ;; Version: 1.6
  6. ;; Keywords: abbrev convenience
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; `hippie-expand' is a single function for a lot of different kinds
  20. ;; of completions and expansions. Called repeatedly it tries all
  21. ;; possible completions in succession.
  22. ;; Which kinds of completions to try, and in which order, is
  23. ;; determined by the contents of `hippie-expand-try-functions-list'.
  24. ;; Much customization of `hippie-expand' can be made by changing the
  25. ;; order of, removing, or inserting new functions in this list.
  26. ;; Given a positive numeric argument, `hippie-expand' jumps directly
  27. ;; ARG functions forward in this list. Given some other argument
  28. ;; (a negative argument or just Ctrl-U) it undoes the tried
  29. ;; completion.
  30. ;;
  31. ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand'
  32. ;; outputs in a message which try-function in the list that is used
  33. ;; currently (ie. was used currently and will be tried first the next
  34. ;; time).
  35. ;; The variable `hippie-expand-max-buffers' determines in how many
  36. ;; buffers, apart from the current, to search for expansions in. It
  37. ;; is used by the try-functions named "-all-buffers".
  38. ;; The variable `hippie-expand-ignore-buffers' is a list of regexps
  39. ;; matching buffer names (as strings) or major modes (as atoms) of
  40. ;; buffers that should not be searched by the try-functions named
  41. ;; "-all-buffers".
  42. ;; If set, the variable `hippie-expand-only-buffers' does the opposite
  43. ;; of `hippie-expand-ignore-buffers', in that the search is restricted
  44. ;; to only the kind of buffers listed.
  45. ;; If the variable `hippie-expand-no-restriction' is non-nil, narrowed
  46. ;; buffers are widened before they are searched.
  47. ;; The variable `hippie-expand-dabbrev-skip-space' controls whether
  48. ;; trailing spaces will be included in the abbreviation to search for,
  49. ;; which then gives the same behavior as the original `dabbrev-expand'.
  50. ;; The variable `hippie-expand-dabbrev-as-symbol' controls whether
  51. ;; characters of syntax '_' is considered part of the words to expand
  52. ;; dynamically.
  53. ;; See also the macro `make-hippie-expand-function' below.
  54. ;;
  55. ;; A short description of the current try-functions in this file:
  56. ;; `try-complete-file-name' : very convenient to have in any buffer,
  57. ;; and not just in the minibuffer or (some) shell-mode. It goes
  58. ;; through all possible completions instead of just completing as
  59. ;; much as is unique.
  60. ;; `try-complete-file-name-partially' : To insert in the list just
  61. ;; before `try-complete-file-name' for those who want first to get
  62. ;; a file name completed only as many characters as is unique.
  63. ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
  64. ;; Otherwise it looks through all abbrev-tables, starting with
  65. ;; the local followed by the global.
  66. ;; `try-expand-line' : Searches the buffer for an entire line that
  67. ;; begins exactly as the current line. Convenient sometimes, for
  68. ;; example as a substitute for (or complement to) the history
  69. ;; list in shell-like buffers. At other times, only confusing.
  70. ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches
  71. ;; in all buffers (except the current). (This may be a little
  72. ;; slow, don't use it unless you are really fond of `hippie-expand'.)
  73. ;; `try-expand-list' : Tries to expand the text back to the nearest
  74. ;; open delimiter, to a whole list from the buffer. Convenient for
  75. ;; example when writing Lisp or TeX.
  76. ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches
  77. ;; in all buffers (except the current).
  78. ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
  79. ;; course in a way compatible with the other try-functions).
  80. ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
  81. ;; like `dabbrev-expand' but searches all Emacs buffers (except the
  82. ;; current) for matching words. (No, I don't find this one
  83. ;; particularly slow.)
  84. ;; `try-expand-dabbrev-visible': Searches the currently visible parts of
  85. ;; all windows. Can be put before `try-expand-dabbrev-all-buffers' to
  86. ;; first try the expansions you can see.
  87. ;; `try-expand-dabbrev-from-kill': Searches the kill ring for a suitable
  88. ;; completion of the word. Good to have, just in case the word was not
  89. ;; found elsewhere.
  90. ;; `try-expand-whole-kill' : Tries to complete text with a whole entry
  91. ;; from the kill ring. May be good if you don't know how far up in
  92. ;; the kill-ring the required entry is, and don't want to mess with
  93. ;; "Choose Next Paste".
  94. ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes
  95. ;; through all possibilities instead of completing what is unique.
  96. ;; Might be tedious (usually a lot of possible completions) and
  97. ;; since its function is much like `lisp-complete-symbol', which
  98. ;; already has a key of its own, you might want to remove this.
  99. ;; `try-complete-lisp-symbol-partially' : To insert in the list just
  100. ;; before `try-complete-lisp-symbol' for those who first want to get
  101. ;; completion of what is unique in the name.
  102. ;;
  103. ;; Not all of the above functions are by default in
  104. ;; `hippie-expand-try-functions-list'. This variable is better set
  105. ;; in ".emacs" to make `hippie-expand' behave maximally convenient
  106. ;; according to personal taste. Also, instead of loading the
  107. ;; variable with all kinds of try-functions above, it might be an
  108. ;; idea to use `make-hippie-expand-function' to construct different
  109. ;; `hippie-expand'-like functions, with different try-lists and bound
  110. ;; to different keys. It is also possible to make
  111. ;; `hippie-expand-try-functions-list' a buffer local variable, and
  112. ;; let it depend on the mode (by setting it in the mode-hooks).
  113. ;;
  114. ;; To write new try-functions, consider the following:
  115. ;; Each try-function takes one argument OLD which is nil the first
  116. ;; time the function is called and true in succeeding calls for the
  117. ;; same string to complete. The first time the function has to
  118. ;; extract the string before point to complete, and substitute the
  119. ;; first completion alternative for it. On following calls it has to
  120. ;; substitute the next possible completion for the last tried string.
  121. ;; The try-function is to return t as long as it finds new
  122. ;; possible completions. When there are no more alternatives it has
  123. ;; to restore the text before point to its original contents, and
  124. ;; return nil (don't beep or message or anything).
  125. ;; The try-function can (should) use the following functions:
  126. ;; `he-init-string' : Initializes the text to substitute to the
  127. ;; contents of the region BEGIN to END. Also sets the variable
  128. ;; `he-search-string' to the text to expand.
  129. ;; `he-substitute-string' : substitutes STR into the region
  130. ;; initialized with `he-init-string'. (An optional second argument
  131. ;; TRANS-CASE non-nil, means transfer of case from the abbreviation
  132. ;; to the expansion is ok if that is enabled in the buffer.)
  133. ;; `he-reset-string' : Resets the initialized region to its original
  134. ;; contents.
  135. ;; There is also a variable: `he-tried-table' which is meant to contain
  136. ;; all tried expansions so far. The try-function can check this
  137. ;; variable to see whether an expansion has already been tried
  138. ;; (hint: `he-string-member').
  139. ;;
  140. ;; Known bugs
  141. ;;
  142. ;; It may happen that some completion suggestion occurs twice, in
  143. ;; spite of the use of `he-tried-table' to prevent that. This is
  144. ;; because different try-functions may try to complete different
  145. ;; lengths of text, and thus put different amounts of the
  146. ;; text in `he-tried-table'. Anyway this seems to occur seldom enough
  147. ;; not to be too disturbing. Also it should NOT be possible for the
  148. ;; opposite situation to occur, that `hippie-expand' misses some
  149. ;; suggestion because it thinks it has already tried it.
  150. ;;
  151. ;; Acknowledgement
  152. ;;
  153. ;; I want to thank Mikael Djurfeldt in discussions with whom the idea
  154. ;; of this function took form.
  155. ;; I am also grateful to all those who have given me suggestions on
  156. ;; how to improve it, and all those who helped to find and remove bugs.
  157. ;;
  158. ;;; Code:
  159. (require 'comint)
  160. (defgroup hippie-expand nil
  161. "Expand text trying various ways to find its expansion."
  162. :link '(custom-manual "(autotype)Hippie Expand")
  163. :link '(emacs-commentary-link "hippie-exp")
  164. :group 'abbrev
  165. :group 'convenience)
  166. (defvar he-num -1)
  167. (defvar he-string-beg (make-marker))
  168. (defvar he-string-end (make-marker))
  169. (defvar he-search-string ())
  170. (defvar he-expand-list ())
  171. (defvar he-tried-table ())
  172. (defvar he-search-loc (make-marker))
  173. (defvar he-search-loc2 ())
  174. (defvar he-search-bw ())
  175. (defvar he-search-bufs ())
  176. (defvar he-searched-n-bufs ())
  177. (defvar he-search-window ())
  178. ;;;###autoload
  179. (defcustom hippie-expand-try-functions-list
  180. '(try-complete-file-name-partially
  181. try-complete-file-name
  182. try-expand-all-abbrevs
  183. try-expand-list
  184. try-expand-line
  185. try-expand-dabbrev
  186. try-expand-dabbrev-all-buffers
  187. try-expand-dabbrev-from-kill
  188. try-complete-lisp-symbol-partially
  189. try-complete-lisp-symbol)
  190. "The list of expansion functions tried in order by `hippie-expand'.
  191. To change the behavior of `hippie-expand', remove, change the order of,
  192. or insert functions in this list."
  193. :type '(repeat function)
  194. :group 'hippie-expand)
  195. ;;;###autoload
  196. (defcustom hippie-expand-verbose t
  197. "Non-nil makes `hippie-expand' output which function it is trying."
  198. :type 'boolean
  199. :group 'hippie-expand)
  200. ;;;###autoload
  201. (defcustom hippie-expand-dabbrev-skip-space nil
  202. "Non-nil means tolerate trailing spaces in the abbreviation to expand."
  203. :group 'hippie-expand
  204. :type 'boolean)
  205. ;;;###autoload
  206. (defcustom hippie-expand-dabbrev-as-symbol t
  207. "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
  208. :group 'hippie-expand
  209. :type 'boolean)
  210. ;;;###autoload
  211. (defcustom hippie-expand-no-restriction t
  212. "Non-nil means that narrowed buffers are widened during search."
  213. :group 'hippie-expand
  214. :type 'boolean)
  215. ;;;###autoload
  216. (defcustom hippie-expand-max-buffers ()
  217. "The maximum number of buffers (apart from the current) searched.
  218. If nil, all buffers are searched."
  219. :type '(choice (const :tag "All" nil)
  220. integer)
  221. :group 'hippie-expand)
  222. ;;;###autoload
  223. (defcustom hippie-expand-ignore-buffers (list (purecopy "^ \\*.*\\*$") 'dired-mode)
  224. "A list specifying which buffers not to search (if not current).
  225. Can contain both regexps matching buffer names (as strings) and major modes
  226. \(as atoms)"
  227. :type '(repeat (choice regexp (symbol :tag "Major Mode")))
  228. :group 'hippie-expand)
  229. ;;;###autoload
  230. (defcustom hippie-expand-only-buffers ()
  231. "A list specifying the only buffers to search (in addition to current).
  232. Can contain both regexps matching buffer names (as strings) and major modes
  233. \(as atoms). If non-nil, this variable overrides the variable
  234. `hippie-expand-ignore-buffers'."
  235. :type '(repeat (choice regexp (symbol :tag "Major Mode")))
  236. :group 'hippie-expand)
  237. ;;;###autoload
  238. (defun hippie-expand (arg)
  239. "Try to expand text before point, using multiple methods.
  240. The expansion functions in `hippie-expand-try-functions-list' are
  241. tried in order, until a possible expansion is found. Repeated
  242. application of `hippie-expand' inserts successively possible
  243. expansions.
  244. With a positive numeric argument, jumps directly to the ARG next
  245. function in this list. With a negative argument or just \\[universal-argument],
  246. undoes the expansion."
  247. (interactive "P")
  248. (if (or (not arg)
  249. (and (integerp arg) (> arg 0)))
  250. (let ((first (or (= he-num -1)
  251. (not (equal this-command last-command)))))
  252. (if first
  253. (progn
  254. (setq he-num -1)
  255. (setq he-tried-table nil)))
  256. (if arg
  257. (if (not first) (he-reset-string))
  258. (setq arg 0))
  259. (let ((i (max (+ he-num arg) 0)))
  260. (while (not (or (>= i (length hippie-expand-try-functions-list))
  261. (apply (nth i hippie-expand-try-functions-list)
  262. (list (= he-num i)))))
  263. (setq i (1+ i)))
  264. (setq he-num i))
  265. (if (>= he-num (length hippie-expand-try-functions-list))
  266. (progn
  267. (setq he-num -1)
  268. (if first
  269. (message "No expansion found")
  270. (message "No further expansions found"))
  271. (ding))
  272. (if (and hippie-expand-verbose
  273. (not (window-minibuffer-p (selected-window))))
  274. (message "Using %s"
  275. (nth he-num hippie-expand-try-functions-list)))))
  276. (if (and (>= he-num 0)
  277. (eq (marker-buffer he-string-beg) (current-buffer)))
  278. (progn
  279. (setq he-num -1)
  280. (he-reset-string)
  281. (if (and hippie-expand-verbose
  282. (not (window-minibuffer-p (selected-window))))
  283. (message "Undoing expansions"))))))
  284. ;; Initializes the region to expand (to between BEG and END).
  285. (defun he-init-string (beg end)
  286. (set-marker he-string-beg beg)
  287. (set-marker he-string-end end)
  288. (setq he-search-string (buffer-substring-no-properties beg end)))
  289. ;; Resets the expanded region to its original contents.
  290. (defun he-reset-string ()
  291. (let ((newpos (point-marker)))
  292. (goto-char he-string-beg)
  293. (insert he-search-string)
  294. (delete-region (point) he-string-end)
  295. (goto-char newpos)))
  296. ;; Substitutes an expansion STR into the correct region (the region
  297. ;; initialized with `he-init-string').
  298. ;; An optional argument TRANS-CASE means that it is ok to transfer case
  299. ;; from the abbreviation to the expansion if that is possible, and is
  300. ;; enabled in the buffer.
  301. (defun he-substitute-string (str &optional trans-case)
  302. (let ((trans-case (and trans-case
  303. case-replace
  304. case-fold-search))
  305. (newpos (point-marker))
  306. (subst ()))
  307. (goto-char he-string-beg)
  308. (setq subst (if trans-case (he-transfer-case he-search-string str) str))
  309. (setq he-tried-table (cons subst he-tried-table))
  310. (insert subst)
  311. (delete-region (point) he-string-end)
  312. (goto-char newpos)))
  313. (defun he-capitalize-first (str)
  314. (save-match-data
  315. (if (string-match "\\Sw*\\(\\sw\\).*" str)
  316. (let ((res (downcase str))
  317. (no (match-beginning 1)))
  318. (aset res no (upcase (aref str no)))
  319. res)
  320. str)))
  321. (defun he-ordinary-case-p (str)
  322. (or (string= str (downcase str))
  323. (string= str (upcase str))
  324. (string= str (capitalize str))
  325. (string= str (he-capitalize-first str))))
  326. (defun he-transfer-case (from-str to-str)
  327. (cond ((string= from-str (substring to-str 0 (min (length from-str)
  328. (length to-str))))
  329. to-str)
  330. ((not (he-ordinary-case-p to-str))
  331. to-str)
  332. ((string= from-str (downcase from-str))
  333. (downcase to-str))
  334. ((string= from-str (upcase from-str))
  335. (upcase to-str))
  336. ((string= from-str (he-capitalize-first from-str))
  337. (he-capitalize-first to-str))
  338. ((string= from-str (capitalize from-str))
  339. (capitalize to-str))
  340. (t
  341. to-str)))
  342. ;; Check if STR is a member of LST.
  343. ;; Transform to the final case if optional TRANS-CASE is non-nil.
  344. (defun he-string-member (str lst &optional trans-case)
  345. (if str
  346. (member (if (and trans-case
  347. case-replace
  348. case-fold-search)
  349. (he-transfer-case he-search-string str)
  350. str)
  351. lst)))
  352. ;; Check if current buffer matches any atom or regexp in LST.
  353. ;; Atoms are interpreted as major modes, strings as regexps matching the name.
  354. (defun he-buffer-member (lst)
  355. (or (memq major-mode lst)
  356. (progn
  357. (while (and lst
  358. (or (not (stringp (car lst)))
  359. (not (string-match (car lst) (buffer-name)))))
  360. (setq lst (cdr lst)))
  361. lst)))
  362. ;; For the real hippie-expand enthusiast: A macro that makes it
  363. ;; possible to use many functions like hippie-expand, but with
  364. ;; different try-functions-lists.
  365. ;; Usage is for example:
  366. ;; (fset 'my-complete-file (make-hippie-expand-function
  367. ;; '(try-complete-file-name-partially
  368. ;; try-complete-file-name)))
  369. ;; (fset 'my-complete-line (make-hippie-expand-function
  370. ;; '(try-expand-line
  371. ;; try-expand-line-all-buffers)))
  372. ;;
  373. ;;;###autoload
  374. (defmacro make-hippie-expand-function (try-list &optional verbose)
  375. "Construct a function similar to `hippie-expand'.
  376. Make it use the expansion functions in TRY-LIST. An optional second
  377. argument VERBOSE non-nil makes the function verbose."
  378. `(function (lambda (arg)
  379. ,(concat
  380. "Try to expand text before point, using the following functions: \n"
  381. (mapconcat 'prin1-to-string (eval try-list) ", "))
  382. (interactive "P")
  383. (let ((hippie-expand-try-functions-list ,try-list)
  384. (hippie-expand-verbose ,verbose))
  385. (hippie-expand arg)))))
  386. ;;; Here follows the try-functions and their requisites:
  387. (defun try-complete-file-name (old)
  388. "Try to complete text as a file name.
  389. The argument OLD has to be nil the first call of this function, and t
  390. for subsequent calls (for further possible completions of the same
  391. string). It returns t if a new completion is found, nil otherwise."
  392. (if (not old)
  393. (progn
  394. (he-init-string (he-file-name-beg) (point))
  395. (let ((name-part (file-name-nondirectory he-search-string))
  396. (dir-part (expand-file-name (or (file-name-directory
  397. he-search-string) ""))))
  398. (if (not (he-string-member name-part he-tried-table))
  399. (setq he-tried-table (cons name-part he-tried-table)))
  400. (if (and (not (equal he-search-string ""))
  401. (file-directory-p dir-part))
  402. (setq he-expand-list (sort (file-name-all-completions
  403. name-part
  404. dir-part)
  405. 'string-lessp))
  406. (setq he-expand-list ())))))
  407. (while (and he-expand-list
  408. (he-string-member (car he-expand-list) he-tried-table))
  409. (setq he-expand-list (cdr he-expand-list)))
  410. (if (null he-expand-list)
  411. (progn
  412. (if old (he-reset-string))
  413. ())
  414. (let ((filename (he-concat-directory-file-name
  415. (file-name-directory he-search-string)
  416. (car he-expand-list))))
  417. (he-substitute-string filename)
  418. (setq he-tried-table (cons (car he-expand-list) (cdr he-tried-table)))
  419. (setq he-expand-list (cdr he-expand-list))
  420. t)))
  421. (defun try-complete-file-name-partially (old)
  422. "Try to complete text as a file name, as many characters as unique.
  423. The argument OLD has to be nil the first call of this function. It
  424. returns t if a unique, possibly partial, completion is found, nil
  425. otherwise."
  426. (let ((expansion ()))
  427. (if (not old)
  428. (progn
  429. (he-init-string (he-file-name-beg) (point))
  430. (let ((name-part (file-name-nondirectory he-search-string))
  431. (dir-part (expand-file-name (or (file-name-directory
  432. he-search-string) ""))))
  433. (if (and (not (equal he-search-string ""))
  434. (file-directory-p dir-part))
  435. (setq expansion (file-name-completion name-part
  436. dir-part)))
  437. (if (or (eq expansion t)
  438. (string= expansion name-part)
  439. (he-string-member expansion he-tried-table))
  440. (setq expansion ())))))
  441. (if (not expansion)
  442. (progn
  443. (if old (he-reset-string))
  444. ())
  445. (let ((filename (he-concat-directory-file-name
  446. (file-name-directory he-search-string)
  447. expansion)))
  448. (he-substitute-string filename)
  449. (setq he-tried-table (cons expansion (cdr he-tried-table)))
  450. t))))
  451. (defvar he-file-name-chars
  452. (cond ((memq system-type '(ms-dos windows-nt cygwin))
  453. "-a-zA-Z0-9_/.,~^#$+=:\\\\")
  454. (t ;; More strange file formats ?
  455. "-a-zA-Z0-9_/.,~^#$+="))
  456. "Characters that are considered part of the file name to expand.")
  457. (defun he-file-name-beg ()
  458. (let ((op (point)))
  459. (save-excursion
  460. (skip-chars-backward he-file-name-chars)
  461. (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars
  462. op
  463. (point)))))
  464. ;; Thanks go to David Hughes <ukchugd@ukpmr.cs.philips.nl> who
  465. ;; helped to make it work on PC.
  466. (defun he-concat-directory-file-name (dir-part name-part)
  467. "Try to slam together two parts of a file specification, system dependently."
  468. (cond ((null dir-part) name-part)
  469. ((eq system-type 'ms-dos)
  470. (if (and (string-match "\\\\" dir-part)
  471. (not (string-match "/" dir-part))
  472. (= (aref name-part (1- (length name-part))) ?/))
  473. (aset name-part (1- (length name-part)) ?\\))
  474. (concat dir-part name-part))
  475. (t
  476. (concat dir-part name-part))))
  477. (defun try-complete-lisp-symbol (old)
  478. "Try to complete word as an Emacs Lisp symbol.
  479. The argument OLD has to be nil the first call of this function, and t
  480. for subsequent calls (for further possible completions of the same
  481. string). It returns t if a new completion is found, nil otherwise."
  482. (if (not old)
  483. (progn
  484. (he-init-string (he-lisp-symbol-beg) (point))
  485. (if (not (he-string-member he-search-string he-tried-table))
  486. (setq he-tried-table (cons he-search-string he-tried-table)))
  487. (setq he-expand-list
  488. (and (not (equal he-search-string ""))
  489. (sort (all-completions he-search-string obarray
  490. (function (lambda (sym)
  491. (or (boundp sym)
  492. (fboundp sym)
  493. (symbol-plist sym)))))
  494. 'string-lessp)))))
  495. (while (and he-expand-list
  496. (he-string-member (car he-expand-list) he-tried-table))
  497. (setq he-expand-list (cdr he-expand-list)))
  498. (if (null he-expand-list)
  499. (progn
  500. (if old (he-reset-string))
  501. ())
  502. (progn
  503. (he-substitute-string (car he-expand-list))
  504. (setq he-expand-list (cdr he-expand-list))
  505. t)))
  506. (defun try-complete-lisp-symbol-partially (old)
  507. "Try to complete as an Emacs Lisp symbol, as many characters as unique.
  508. The argument OLD has to be nil the first call of this function. It
  509. returns t if a unique, possibly partial, completion is found, nil
  510. otherwise."
  511. (let ((expansion ()))
  512. (if (not old)
  513. (progn
  514. (he-init-string (he-lisp-symbol-beg) (point))
  515. (if (not (string= he-search-string ""))
  516. (setq expansion
  517. (try-completion he-search-string obarray
  518. (function (lambda (sym)
  519. (or (boundp sym)
  520. (fboundp sym)
  521. (symbol-plist sym)))))))
  522. (if (or (eq expansion t)
  523. (string= expansion he-search-string)
  524. (he-string-member expansion he-tried-table))
  525. (setq expansion ()))))
  526. (if (not expansion)
  527. (progn
  528. (if old (he-reset-string))
  529. ())
  530. (progn
  531. (he-substitute-string expansion)
  532. t))))
  533. (defun he-lisp-symbol-beg ()
  534. (save-excursion
  535. (skip-syntax-backward "w_")
  536. (point)))
  537. (defun try-expand-line (old)
  538. "Try to complete the current line to an entire line in the buffer.
  539. The argument OLD has to be nil the first call of this function, and t
  540. for subsequent calls (for further possible completions of the same
  541. string). It returns t if a new completion is found, nil otherwise."
  542. (let ((expansion ())
  543. (strip-prompt (and (get-buffer-process (current-buffer))
  544. comint-use-prompt-regexp
  545. comint-prompt-regexp)))
  546. (if (not old)
  547. (progn
  548. (he-init-string (he-line-beg strip-prompt) (point))
  549. (set-marker he-search-loc he-string-beg)
  550. (setq he-search-bw t)))
  551. (if (not (equal he-search-string ""))
  552. (save-excursion
  553. (save-restriction
  554. (if hippie-expand-no-restriction
  555. (widen))
  556. ;; Try looking backward unless inhibited.
  557. (if he-search-bw
  558. (progn
  559. (goto-char he-search-loc)
  560. (setq expansion (he-line-search he-search-string
  561. strip-prompt t))
  562. (set-marker he-search-loc (point))
  563. (if (not expansion)
  564. (progn
  565. (set-marker he-search-loc he-string-end)
  566. (setq he-search-bw ())))))
  567. (if (not expansion) ; Then look forward.
  568. (progn
  569. (goto-char he-search-loc)
  570. (setq expansion (he-line-search he-search-string
  571. strip-prompt nil))
  572. (set-marker he-search-loc (point)))))))
  573. (if (not expansion)
  574. (progn
  575. (if old (he-reset-string))
  576. ())
  577. (progn
  578. (he-substitute-string expansion t)
  579. t))))
  580. (defun try-expand-line-all-buffers (old)
  581. "Try to complete the current line, searching all other buffers.
  582. The argument OLD has to be nil the first call of this function, and t
  583. for subsequent calls (for further possible completions of the same
  584. string). It returns t if a new completion is found, nil otherwise."
  585. (let ((expansion ())
  586. (strip-prompt (and (get-buffer-process (current-buffer))
  587. comint-use-prompt-regexp
  588. comint-prompt-regexp))
  589. (buf (current-buffer))
  590. (orig-case-fold-search case-fold-search))
  591. (if (not old)
  592. (progn
  593. (he-init-string (he-line-beg strip-prompt) (point))
  594. (setq he-search-bufs (buffer-list))
  595. (setq he-searched-n-bufs 0)
  596. (set-marker he-search-loc 1 (car he-search-bufs))))
  597. (if (not (equal he-search-string ""))
  598. (while (and he-search-bufs
  599. (not expansion)
  600. (or (not hippie-expand-max-buffers)
  601. (< he-searched-n-bufs hippie-expand-max-buffers)))
  602. (set-buffer (car he-search-bufs))
  603. (if (and (not (eq (current-buffer) buf))
  604. (if hippie-expand-only-buffers
  605. (he-buffer-member hippie-expand-only-buffers)
  606. (not (he-buffer-member hippie-expand-ignore-buffers))))
  607. (save-excursion
  608. (save-restriction
  609. (if hippie-expand-no-restriction
  610. (widen))
  611. (goto-char he-search-loc)
  612. (setq strip-prompt (and (get-buffer-process (current-buffer))
  613. comint-use-prompt-regexp
  614. comint-prompt-regexp))
  615. (setq expansion
  616. (let ((case-fold-search orig-case-fold-search))
  617. (he-line-search he-search-string
  618. strip-prompt nil)))
  619. (set-marker he-search-loc (point))
  620. (if (not expansion)
  621. (progn
  622. (setq he-search-bufs (cdr he-search-bufs))
  623. (setq he-searched-n-bufs (1+ he-searched-n-bufs))
  624. (set-marker he-search-loc 1 (car he-search-bufs))))))
  625. (setq he-search-bufs (cdr he-search-bufs))
  626. (set-marker he-search-loc 1 (car he-search-bufs)))))
  627. (set-buffer buf)
  628. (if (not expansion)
  629. (progn
  630. (if old (he-reset-string))
  631. ())
  632. (progn
  633. (he-substitute-string expansion t)
  634. t))))
  635. (defun he-line-search (str strip-prompt reverse)
  636. (let ((result ()))
  637. (while (and (not result)
  638. (if reverse
  639. (re-search-backward
  640. (he-line-search-regexp str strip-prompt)
  641. nil t)
  642. (re-search-forward
  643. (he-line-search-regexp str strip-prompt)
  644. nil t)))
  645. (setq result (buffer-substring-no-properties (match-end 1)
  646. (match-end 0)))
  647. (if (he-string-member result he-tried-table t)
  648. (setq result nil))) ; if already in table, ignore
  649. result))
  650. (defun he-line-beg (strip-prompt)
  651. (save-excursion
  652. (if (re-search-backward (he-line-search-regexp "" strip-prompt)
  653. (line-beginning-position) t)
  654. (match-beginning 2)
  655. (point))))
  656. (defun he-line-search-regexp (pat strip-prompt)
  657. (if strip-prompt
  658. (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\("
  659. (regexp-quote pat)
  660. "[^\n]*[^ \t\n]\\)")
  661. (concat "^\\(\\s-*\\)\\("
  662. (regexp-quote pat)
  663. "[^\n]*[^ \t\n]\\)")))
  664. (defun try-expand-list (old)
  665. "Try to complete the current beginning of a list.
  666. The argument OLD has to be nil the first call of this function, and t
  667. for subsequent calls (for further possible completions of the same
  668. string). It returns t if a new completion is found, nil otherwise."
  669. (let ((expansion ()))
  670. (if (not old)
  671. (progn
  672. (he-init-string (he-list-beg) (point))
  673. (set-marker he-search-loc he-string-beg)
  674. (setq he-search-bw t)))
  675. (if (not (equal he-search-string ""))
  676. (save-excursion
  677. (save-restriction
  678. (if hippie-expand-no-restriction
  679. (widen))
  680. ;; Try looking backward unless inhibited.
  681. (if he-search-bw
  682. (progn
  683. (goto-char he-search-loc)
  684. (setq expansion (he-list-search he-search-string t))
  685. (set-marker he-search-loc (point))
  686. (if (not expansion)
  687. (progn
  688. (set-marker he-search-loc he-string-end)
  689. (setq he-search-bw ())))))
  690. (if (not expansion) ; Then look forward.
  691. (progn
  692. (goto-char he-search-loc)
  693. (setq expansion (he-list-search he-search-string nil))
  694. (set-marker he-search-loc (point)))))))
  695. (if (not expansion)
  696. (progn
  697. (if old (he-reset-string))
  698. ())
  699. (progn
  700. (he-substitute-string expansion t)
  701. t))))
  702. (defun try-expand-list-all-buffers (old)
  703. "Try to complete the current list, searching all other buffers.
  704. The argument OLD has to be nil the first call of this function, and t
  705. for subsequent calls (for further possible completions of the same
  706. string). It returns t if a new completion is found, nil otherwise."
  707. (let ((expansion ())
  708. (buf (current-buffer))
  709. (orig-case-fold-search case-fold-search))
  710. (if (not old)
  711. (progn
  712. (he-init-string (he-list-beg) (point))
  713. (setq he-search-bufs (buffer-list))
  714. (setq he-searched-n-bufs 0)
  715. (set-marker he-search-loc 1 (car he-search-bufs))))
  716. (if (not (equal he-search-string ""))
  717. (while (and he-search-bufs
  718. (not expansion)
  719. (or (not hippie-expand-max-buffers)
  720. (< he-searched-n-bufs hippie-expand-max-buffers)))
  721. (set-buffer (car he-search-bufs))
  722. (if (and (not (eq (current-buffer) buf))
  723. (if hippie-expand-only-buffers
  724. (he-buffer-member hippie-expand-only-buffers)
  725. (not (he-buffer-member hippie-expand-ignore-buffers))))
  726. (save-excursion
  727. (save-restriction
  728. (if hippie-expand-no-restriction
  729. (widen))
  730. (goto-char he-search-loc)
  731. (setq expansion
  732. (let ((case-fold-search orig-case-fold-search))
  733. (he-list-search he-search-string nil)))
  734. (set-marker he-search-loc (point))
  735. (if (not expansion)
  736. (progn
  737. (setq he-search-bufs (cdr he-search-bufs))
  738. (setq he-searched-n-bufs (1+ he-searched-n-bufs))
  739. (set-marker he-search-loc 1 (car he-search-bufs))))))
  740. (setq he-search-bufs (cdr he-search-bufs))
  741. (set-marker he-search-loc 1 (car he-search-bufs)))))
  742. (set-buffer buf)
  743. (if (not expansion)
  744. (progn
  745. (if old (he-reset-string))
  746. ())
  747. (progn
  748. (he-substitute-string expansion t)
  749. t))))
  750. (defun he-list-search (str reverse)
  751. (let ((result ())
  752. beg pos err)
  753. (while (and (not result)
  754. (if reverse
  755. (search-backward str nil t)
  756. (search-forward str nil t)))
  757. (setq pos (point))
  758. (setq beg (match-beginning 0))
  759. (goto-char beg)
  760. (setq err ())
  761. (condition-case ()
  762. (forward-list 1)
  763. (error (setq err t)))
  764. (if (and reverse
  765. (> (point) he-string-beg))
  766. (setq err t))
  767. (if (not err)
  768. (progn
  769. (setq result (buffer-substring-no-properties beg (point)))
  770. (if (he-string-member result he-tried-table t)
  771. (setq result nil)))) ; if already in table, ignore
  772. (goto-char pos))
  773. result))
  774. (defun he-list-beg ()
  775. (save-excursion
  776. (condition-case ()
  777. (backward-up-list 1)
  778. (error ()))
  779. (point)))
  780. (defun try-expand-all-abbrevs (old)
  781. "Try to expand word before point according to all abbrev tables.
  782. The argument OLD has to be nil the first call of this function, and t
  783. for subsequent calls (for further possible expansions of the same
  784. string). It returns t if a new expansion is found, nil otherwise."
  785. (if (not old)
  786. (progn
  787. (he-init-string (he-dabbrev-beg) (point))
  788. (setq he-expand-list
  789. (and (not (equal he-search-string ""))
  790. (mapcar (function (lambda (sym)
  791. (if (and (boundp sym) (vectorp (eval sym)))
  792. (abbrev-expansion (downcase he-search-string)
  793. (eval sym)))))
  794. (append '(local-abbrev-table
  795. global-abbrev-table)
  796. abbrev-table-name-list))))))
  797. (while (and he-expand-list
  798. (or (not (car he-expand-list))
  799. (he-string-member (car he-expand-list) he-tried-table t)))
  800. (setq he-expand-list (cdr he-expand-list)))
  801. (if (null he-expand-list)
  802. (progn
  803. (if old (he-reset-string))
  804. ())
  805. (progn
  806. (he-substitute-string (car he-expand-list) t)
  807. (setq he-expand-list (cdr he-expand-list))
  808. t)))
  809. (defun try-expand-dabbrev (old)
  810. "Try to expand word \"dynamically\", searching the current buffer.
  811. The argument OLD has to be nil the first call of this function, and t
  812. for subsequent calls (for further possible expansions of the same
  813. string). It returns t if a new expansion is found, nil otherwise."
  814. (let ((expansion ()))
  815. (if (not old)
  816. (progn
  817. (he-init-string (he-dabbrev-beg) (point))
  818. (set-marker he-search-loc he-string-beg)
  819. (setq he-search-bw t)))
  820. (if (not (equal he-search-string ""))
  821. (save-excursion
  822. (save-restriction
  823. (if hippie-expand-no-restriction
  824. (widen))
  825. ;; Try looking backward unless inhibited.
  826. (if he-search-bw
  827. (progn
  828. (goto-char he-search-loc)
  829. (setq expansion (he-dabbrev-search he-search-string t))
  830. (set-marker he-search-loc (point))
  831. (if (not expansion)
  832. (progn
  833. (set-marker he-search-loc he-string-end)
  834. (setq he-search-bw ())))))
  835. (if (not expansion) ; Then look forward.
  836. (progn
  837. (goto-char he-search-loc)
  838. (setq expansion (he-dabbrev-search he-search-string nil))
  839. (set-marker he-search-loc (point)))))))
  840. (if (not expansion)
  841. (progn
  842. (if old (he-reset-string))
  843. ())
  844. (progn
  845. (he-substitute-string expansion t)
  846. t))))
  847. (defun try-expand-dabbrev-all-buffers (old)
  848. "Try to expand word \"dynamically\", searching all other buffers.
  849. The argument OLD has to be nil the first call of this function, and t
  850. for subsequent calls (for further possible expansions of the same
  851. string). It returns t if a new expansion is found, nil otherwise."
  852. (let ((expansion ())
  853. (buf (current-buffer))
  854. (orig-case-fold-search case-fold-search))
  855. (if (not old)
  856. (progn
  857. (he-init-string (he-dabbrev-beg) (point))
  858. (setq he-search-bufs (buffer-list))
  859. (setq he-searched-n-bufs 0)
  860. (set-marker he-search-loc 1 (car he-search-bufs))))
  861. (if (not (equal he-search-string ""))
  862. (while (and he-search-bufs
  863. (not expansion)
  864. (or (not hippie-expand-max-buffers)
  865. (< he-searched-n-bufs hippie-expand-max-buffers)))
  866. (set-buffer (car he-search-bufs))
  867. (if (and (not (eq (current-buffer) buf))
  868. (if hippie-expand-only-buffers
  869. (he-buffer-member hippie-expand-only-buffers)
  870. (not (he-buffer-member hippie-expand-ignore-buffers))))
  871. (save-excursion
  872. (save-restriction
  873. (if hippie-expand-no-restriction
  874. (widen))
  875. (goto-char he-search-loc)
  876. (setq expansion
  877. (let ((case-fold-search orig-case-fold-search))
  878. (he-dabbrev-search he-search-string nil)))
  879. (set-marker he-search-loc (point))
  880. (if (not expansion)
  881. (progn
  882. (setq he-search-bufs (cdr he-search-bufs))
  883. (setq he-searched-n-bufs (1+ he-searched-n-bufs))
  884. (set-marker he-search-loc 1 (car he-search-bufs))))))
  885. (setq he-search-bufs (cdr he-search-bufs))
  886. (set-marker he-search-loc 1 (car he-search-bufs)))))
  887. (set-buffer buf)
  888. (if (not expansion)
  889. (progn
  890. (if old (he-reset-string))
  891. ())
  892. (progn
  893. (he-substitute-string expansion t)
  894. t))))
  895. ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
  896. ;; suggested this one.
  897. (defun try-expand-dabbrev-visible (old)
  898. "Try to expand word \"dynamically\", searching visible window parts.
  899. The argument OLD has to be nil the first call of this function, and t
  900. for subsequent calls (for further possible expansions of the same
  901. string). It returns t if a new expansion is found, nil otherwise."
  902. (let ((expansion ())
  903. (flag (if (frame-visible-p (window-frame (selected-window)))
  904. 'visible t)))
  905. (unless old
  906. (he-init-string (he-dabbrev-beg) (point))
  907. (setq he-search-window (selected-window))
  908. (set-marker he-search-loc
  909. (window-start he-search-window)
  910. (window-buffer he-search-window)))
  911. (while (and (not (equal he-search-string ""))
  912. (marker-position he-search-loc)
  913. (not expansion))
  914. (with-current-buffer (marker-buffer he-search-loc)
  915. (save-excursion
  916. (goto-char he-search-loc)
  917. (setq expansion (he-dabbrev-search he-search-string ()
  918. (window-end he-search-window)))
  919. (if (and expansion
  920. (eq (marker-buffer he-string-beg) (current-buffer))
  921. (eq (marker-position he-string-beg) (match-beginning 0)))
  922. (setq expansion
  923. (he-dabbrev-search he-search-string ()
  924. (window-end he-search-window))))
  925. (set-marker he-search-loc (point) (current-buffer))))
  926. (unless expansion
  927. (setq he-search-window (next-window he-search-window nil flag))
  928. (if (eq he-search-window (selected-window))
  929. (set-marker he-search-loc nil)
  930. (set-marker he-search-loc (window-start he-search-window)
  931. (window-buffer he-search-window)))))
  932. (if (not expansion)
  933. (progn
  934. (if old (he-reset-string))
  935. ())
  936. (progn
  937. (he-substitute-string expansion t)
  938. t))))
  939. (defun he-dabbrev-search (pattern &optional reverse limit)
  940. (let ((result ())
  941. (regpat (cond ((not hippie-expand-dabbrev-as-symbol)
  942. (concat "\\<" (regexp-quote pattern) "\\sw+"))
  943. ((eq (char-syntax (aref pattern 0)) ?_)
  944. (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
  945. (t
  946. (concat "\\<" (regexp-quote pattern)
  947. "\\(\\sw\\|\\s_\\)+")))))
  948. (while (and (not result)
  949. (if reverse
  950. (re-search-backward regpat limit t)
  951. (re-search-forward regpat limit t)))
  952. (setq result (buffer-substring-no-properties (match-beginning 0)
  953. (match-end 0)))
  954. (if (or (and hippie-expand-dabbrev-as-symbol
  955. (> (match-beginning 0) (point-min))
  956. (memq (char-syntax (char-after (1- (match-beginning 0))))
  957. '(?_ ?w)))
  958. (he-string-member result he-tried-table t))
  959. (setq result nil))) ; ignore if bad prefix or already in table
  960. result))
  961. (defun he-dabbrev-beg ()
  962. (let ((op (point)))
  963. (save-excursion
  964. (if hippie-expand-dabbrev-skip-space
  965. (skip-syntax-backward ". "))
  966. (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
  967. "w_" "w"))
  968. 0)
  969. op
  970. (point)))))
  971. (defun try-expand-dabbrev-from-kill (old)
  972. "Try to expand word \"dynamically\", searching the kill ring.
  973. The argument OLD has to be nil the first call of this function, and t
  974. for subsequent calls (for further possible completions of the same
  975. string). It returns t if a new completion is found, nil otherwise."
  976. (let ((expansion ()))
  977. (if (not old)
  978. (progn
  979. (he-init-string (he-dabbrev-beg) (point))
  980. (setq he-expand-list
  981. (if (not (equal he-search-string ""))
  982. kill-ring))
  983. (setq he-search-loc2 0)))
  984. (if (not (equal he-search-string ""))
  985. (setq expansion (he-dabbrev-kill-search he-search-string)))
  986. (if (not expansion)
  987. (progn
  988. (if old (he-reset-string))
  989. ())
  990. (progn
  991. (he-substitute-string expansion t)
  992. t))))
  993. (defun he-dabbrev-kill-search (pattern)
  994. (let ((result ())
  995. (regpat (cond ((not hippie-expand-dabbrev-as-symbol)
  996. (concat "\\<" (regexp-quote pattern) "\\sw+"))
  997. ((eq (char-syntax (aref pattern 0)) ?_)
  998. (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
  999. (t
  1000. (concat "\\<" (regexp-quote pattern)
  1001. "\\(\\sw\\|\\s_\\)+"))))
  1002. (killstr (car he-expand-list)))
  1003. (while (and (not result)
  1004. he-expand-list)
  1005. (while (and (not result)
  1006. (string-match regpat killstr he-search-loc2))
  1007. (setq result (substring killstr (match-beginning 0) (match-end 0)))
  1008. (set-text-properties 0 (length result) () result)
  1009. (setq he-search-loc2 (1+ (match-beginning 0)))
  1010. (if (or (and hippie-expand-dabbrev-as-symbol
  1011. (> (match-beginning 0) 0)
  1012. (memq (char-syntax (aref killstr (1- (match-beginning 0))))
  1013. '(?_ ?w)))
  1014. (he-string-member result he-tried-table t))
  1015. (setq result nil))) ; ignore if bad prefix or already in table
  1016. (if (and (not result)
  1017. he-expand-list)
  1018. (progn
  1019. (setq he-expand-list (cdr he-expand-list))
  1020. (setq killstr (car he-expand-list))
  1021. (setq he-search-loc2 0))))
  1022. result))
  1023. (defun try-expand-whole-kill (old)
  1024. "Try to complete text with something from the kill ring.
  1025. The argument OLD has to be nil the first call of this function, and t
  1026. for subsequent calls (for further possible completions of the same
  1027. string). It returns t if a new completion is found, nil otherwise."
  1028. (let ((expansion ()))
  1029. (if (not old)
  1030. (progn
  1031. (he-init-string (he-kill-beg) (point))
  1032. (if (not (he-string-member he-search-string he-tried-table))
  1033. (setq he-tried-table (cons he-search-string he-tried-table)))
  1034. (setq he-expand-list
  1035. (if (not (equal he-search-string ""))
  1036. kill-ring))
  1037. (setq he-search-loc2 ())))
  1038. (if (not (equal he-search-string ""))
  1039. (setq expansion (he-whole-kill-search he-search-string)))
  1040. (if (not expansion)
  1041. (progn
  1042. (if old (he-reset-string))
  1043. ())
  1044. (progn
  1045. (he-substitute-string expansion)
  1046. t))))
  1047. (defun he-whole-kill-search (str)
  1048. (let ((case-fold-search ())
  1049. (result ())
  1050. (str (regexp-quote str))
  1051. (killstr (car he-expand-list))
  1052. (pos -1))
  1053. (while (and (not result)
  1054. he-expand-list)
  1055. (if (not he-search-loc2)
  1056. (while (setq pos (string-match str killstr (1+ pos)))
  1057. (setq he-search-loc2 (cons pos he-search-loc2))))
  1058. (while (and (not result)
  1059. he-search-loc2)
  1060. (setq pos (car he-search-loc2))
  1061. (setq he-search-loc2 (cdr he-search-loc2))
  1062. (save-excursion
  1063. (goto-char he-string-beg)
  1064. (if (and (>= (- (point) pos) (point-min)) ; avoid some string GC
  1065. (eq (char-after (- (point) pos)) (aref killstr 0))
  1066. (search-backward (substring killstr 0 pos)
  1067. (- (point) pos) t))
  1068. (progn
  1069. (setq result (substring killstr pos))
  1070. (set-text-properties 0 (length result) () result))))
  1071. (if (and result
  1072. (he-string-member result he-tried-table))
  1073. (setq result nil))) ; ignore if already in table
  1074. (if (and (not result)
  1075. he-expand-list)
  1076. (progn
  1077. (setq he-expand-list (cdr he-expand-list))
  1078. (setq killstr (car he-expand-list))
  1079. (setq pos -1))))
  1080. result))
  1081. (defun he-kill-beg ()
  1082. (let ((op (point)))
  1083. (save-excursion
  1084. (skip-syntax-backward "^w_")
  1085. (if (= (skip-syntax-backward "w_") 0)
  1086. op
  1087. (point)))))
  1088. (provide 'hippie-exp)
  1089. ;;; hippie-exp.el ends here