hippie-exp.el 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. ;;; hippie-exp.el --- expand text trying various ways to find its expansion
  2. ;; Copyright (C) 1992, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Anders Holst <aho@sans.kth.se>
  4. ;; Maintainer: emacs-devel@gnu.org
  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 `elisp-completion-at-point', 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 `completion-at-point', 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. ;; Acknowledgment
  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. ;;; Autoloaded for historical reasons (bug#12982)
  179. ;;;###autoload
  180. (defcustom hippie-expand-try-functions-list
  181. '(try-complete-file-name-partially
  182. try-complete-file-name
  183. try-expand-all-abbrevs
  184. try-expand-list
  185. try-expand-line
  186. try-expand-dabbrev
  187. try-expand-dabbrev-all-buffers
  188. try-expand-dabbrev-from-kill
  189. try-complete-lisp-symbol-partially
  190. try-complete-lisp-symbol)
  191. "The list of expansion functions tried in order by `hippie-expand'.
  192. To change the behavior of `hippie-expand', remove, change the order of,
  193. or insert functions in this list."
  194. :type '(repeat function)
  195. :group 'hippie-expand)
  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. (defcustom hippie-expand-dabbrev-skip-space nil
  201. "Non-nil means tolerate trailing spaces in the abbreviation to expand."
  202. :group 'hippie-expand
  203. :type 'boolean)
  204. (defcustom hippie-expand-dabbrev-as-symbol t
  205. "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
  206. :group 'hippie-expand
  207. :type 'boolean)
  208. (defcustom hippie-expand-no-restriction t
  209. "Non-nil means that narrowed buffers are widened during search."
  210. :group 'hippie-expand
  211. :type 'boolean)
  212. (defcustom hippie-expand-max-buffers ()
  213. "The maximum number of buffers (apart from the current) searched.
  214. If nil, all buffers are searched."
  215. :type '(choice (const :tag "All" nil)
  216. integer)
  217. :group 'hippie-expand)
  218. (defcustom hippie-expand-ignore-buffers '("^ \\*.*\\*$" dired-mode)
  219. "A list specifying which buffers not to search (if not current).
  220. Can contain both regexps matching buffer names (as strings) and major modes
  221. \(as atoms)"
  222. :type '(repeat (choice regexp (symbol :tag "Major Mode")))
  223. :group 'hippie-expand)
  224. (defcustom hippie-expand-only-buffers ()
  225. "A list specifying the only buffers to search (in addition to current).
  226. Can contain both regexps matching buffer names (as strings) and major modes
  227. \(as atoms). If non-nil, this variable overrides the variable
  228. `hippie-expand-ignore-buffers'."
  229. :type '(repeat (choice regexp (symbol :tag "Major Mode")))
  230. :group 'hippie-expand)
  231. ;;;###autoload
  232. (defun hippie-expand (arg)
  233. "Try to expand text before point, using multiple methods.
  234. The expansion functions in `hippie-expand-try-functions-list' are
  235. tried in order, until a possible expansion is found. Repeated
  236. application of `hippie-expand' inserts successively possible
  237. expansions.
  238. With a positive numeric argument, jumps directly to the ARG next
  239. function in this list. With a negative argument or just \\[universal-argument],
  240. undoes the expansion."
  241. (interactive "P")
  242. (if (or (not arg)
  243. (and (integerp arg) (> arg 0)))
  244. (let ((first (or (= he-num -1)
  245. (not (equal this-command last-command)))))
  246. (if first
  247. (progn
  248. (setq he-num -1)
  249. (setq he-tried-table nil)))
  250. (if arg
  251. (if (not first) (he-reset-string))
  252. (setq arg 0))
  253. (let ((i (max (+ he-num arg) 0)))
  254. (while (not (or (>= i (length hippie-expand-try-functions-list))
  255. (apply (nth i hippie-expand-try-functions-list)
  256. (list (= he-num i)))))
  257. (setq i (1+ i)))
  258. (setq he-num i))
  259. (if (>= he-num (length hippie-expand-try-functions-list))
  260. (progn
  261. (setq he-num -1)
  262. (if first
  263. (message "No expansion found")
  264. (message "No further expansions found"))
  265. (ding))
  266. (if (and hippie-expand-verbose
  267. (not (window-minibuffer-p)))
  268. (message "Using %s"
  269. (nth he-num hippie-expand-try-functions-list)))))
  270. (if (and (>= he-num 0)
  271. (eq (marker-buffer he-string-beg) (current-buffer)))
  272. (progn
  273. (setq he-num -1)
  274. (he-reset-string)
  275. (if (and hippie-expand-verbose
  276. (not (window-minibuffer-p)))
  277. (message "Undoing expansions"))))))
  278. ;; Initializes the region to expand (to between BEG and END).
  279. (defun he-init-string (beg end)
  280. (set-marker he-string-beg beg)
  281. (set-marker he-string-end end)
  282. (setq he-search-string (buffer-substring-no-properties beg end)))
  283. ;; Resets the expanded region to its original contents.
  284. (defun he-reset-string ()
  285. (let ((newpos (point-marker)))
  286. (goto-char he-string-beg)
  287. (insert he-search-string)
  288. (delete-region (point) he-string-end)
  289. (goto-char newpos)))
  290. ;; Substitutes an expansion STR into the correct region (the region
  291. ;; initialized with `he-init-string').
  292. ;; An optional argument TRANS-CASE means that it is ok to transfer case
  293. ;; from the abbreviation to the expansion if that is possible, and is
  294. ;; enabled in the buffer.
  295. (defun he-substitute-string (str &optional trans-case)
  296. (let ((trans-case (and trans-case
  297. case-replace
  298. case-fold-search))
  299. (newpos (point-marker))
  300. (subst ()))
  301. (goto-char he-string-beg)
  302. (setq subst (if trans-case (he-transfer-case he-search-string str) str))
  303. (setq he-tried-table (cons subst he-tried-table))
  304. (insert subst)
  305. (delete-region (point) he-string-end)
  306. (goto-char newpos)))
  307. (defun he-capitalize-first (str)
  308. (save-match-data
  309. (if (string-match "\\Sw*\\(\\sw\\).*" str)
  310. (let ((res (downcase str))
  311. (no (match-beginning 1)))
  312. (aset res no (upcase (aref str no)))
  313. res)
  314. str)))
  315. (defun he-ordinary-case-p (str)
  316. (or (string= str (downcase str))
  317. (string= str (upcase str))
  318. (string= str (capitalize str))
  319. (string= str (he-capitalize-first str))))
  320. (defun he-transfer-case (from-str to-str)
  321. (cond ((string= from-str (substring to-str 0 (min (length from-str)
  322. (length to-str))))
  323. to-str)
  324. ((not (he-ordinary-case-p to-str))
  325. to-str)
  326. ((string= from-str (downcase from-str))
  327. (downcase to-str))
  328. ((string= from-str (upcase from-str))
  329. (upcase to-str))
  330. ((string= from-str (he-capitalize-first from-str))
  331. (he-capitalize-first to-str))
  332. ((string= from-str (capitalize from-str))
  333. (capitalize to-str))
  334. (t
  335. to-str)))
  336. ;; Check if STR is a member of LST.
  337. ;; Transform to the final case if optional TRANS-CASE is non-nil.
  338. (defun he-string-member (str lst &optional trans-case)
  339. (if str
  340. (member (if (and trans-case
  341. case-replace
  342. case-fold-search)
  343. (he-transfer-case he-search-string str)
  344. str)
  345. lst)))
  346. ;; Check if current buffer matches any atom or regexp in LST.
  347. ;; Atoms are interpreted as major modes, strings as regexps matching the name.
  348. (defun he-buffer-member (lst)
  349. (or (memq major-mode lst)
  350. (progn
  351. (while (and lst
  352. (or (not (stringp (car lst)))
  353. (not (string-match (car lst) (buffer-name)))))
  354. (setq lst (cdr lst)))
  355. lst)))
  356. ;; For the real hippie-expand enthusiast: A macro that makes it
  357. ;; possible to use many functions like hippie-expand, but with
  358. ;; different try-functions-lists.
  359. ;; Usage is for example:
  360. ;; (fset 'my-complete-file (make-hippie-expand-function
  361. ;; '(try-complete-file-name-partially
  362. ;; try-complete-file-name)))
  363. ;; (fset 'my-complete-line (make-hippie-expand-function
  364. ;; '(try-expand-line
  365. ;; try-expand-line-all-buffers)))
  366. ;;
  367. ;;;###autoload
  368. (defmacro make-hippie-expand-function (try-list &optional verbose)
  369. "Construct a function similar to `hippie-expand'.
  370. Make it use the expansion functions in TRY-LIST. An optional second
  371. argument VERBOSE non-nil makes the function verbose."
  372. `(function (lambda (arg)
  373. ,(concat
  374. "Try to expand text before point, using the following functions: \n"
  375. (mapconcat 'prin1-to-string (eval try-list) ", "))
  376. (interactive "P")
  377. (let ((hippie-expand-try-functions-list ,try-list)
  378. (hippie-expand-verbose ,verbose))
  379. (hippie-expand arg)))))
  380. ;;; Here follows the try-functions and their requisites:
  381. (defun try-complete-file-name (old)
  382. "Try to complete text as a file name.
  383. The argument OLD has to be nil the first call of this function, and t
  384. for subsequent calls (for further possible completions of the same
  385. string). It returns t if a new completion is found, nil otherwise."
  386. (if (not old)
  387. (progn
  388. (he-init-string (he-file-name-beg) (point))
  389. (let ((name-part (file-name-nondirectory he-search-string))
  390. (dir-part (expand-file-name (or (file-name-directory
  391. he-search-string) ""))))
  392. (if (not (he-string-member name-part he-tried-table))
  393. (setq he-tried-table (cons name-part he-tried-table)))
  394. (if (and (not (equal he-search-string ""))
  395. (file-directory-p dir-part))
  396. (setq he-expand-list (sort (file-name-all-completions
  397. name-part
  398. dir-part)
  399. 'string-lessp))
  400. (setq he-expand-list ())))))
  401. (while (and he-expand-list
  402. (he-string-member (car he-expand-list) he-tried-table))
  403. (setq he-expand-list (cdr he-expand-list)))
  404. (if (null he-expand-list)
  405. (progn
  406. (if old (he-reset-string))
  407. ())
  408. (let ((filename (he-concat-directory-file-name
  409. (file-name-directory he-search-string)
  410. (car he-expand-list))))
  411. (he-substitute-string filename)
  412. (setq he-tried-table (cons (car he-expand-list) (cdr he-tried-table)))
  413. (setq he-expand-list (cdr he-expand-list))
  414. t)))
  415. (defun try-complete-file-name-partially (old)
  416. "Try to complete text as a file name, as many characters as unique.
  417. The argument OLD has to be nil the first call of this function. It
  418. returns t if a unique, possibly partial, completion is found, nil
  419. otherwise."
  420. (let ((expansion ()))
  421. (if (not old)
  422. (progn
  423. (he-init-string (he-file-name-beg) (point))
  424. (let ((name-part (file-name-nondirectory he-search-string))
  425. (dir-part (expand-file-name (or (file-name-directory
  426. he-search-string) ""))))
  427. (if (and (not (equal he-search-string ""))
  428. (file-directory-p dir-part))
  429. (setq expansion (file-name-completion name-part
  430. dir-part)))
  431. (if (or (eq expansion t)
  432. (string= expansion name-part)
  433. (he-string-member expansion he-tried-table))
  434. (setq expansion ())))))
  435. (if (not expansion)
  436. (progn
  437. (if old (he-reset-string))
  438. ())
  439. (let ((filename (he-concat-directory-file-name
  440. (file-name-directory he-search-string)
  441. expansion)))
  442. (he-substitute-string filename)
  443. (setq he-tried-table (cons expansion (cdr he-tried-table)))
  444. t))))
  445. (defvar he-file-name-chars
  446. (cond ((memq system-type '(ms-dos windows-nt cygwin))
  447. "-a-zA-Z0-9_/.,~^#$+=:\\\\")
  448. (t ;; More strange file formats ?
  449. "-a-zA-Z0-9_/.,~^#$+="))
  450. "Characters that are considered part of the file name to expand.")
  451. (defun he-file-name-beg ()
  452. (let ((op (point)))
  453. (save-excursion
  454. (skip-chars-backward he-file-name-chars)
  455. (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars
  456. op
  457. (point)))))
  458. ;; Thanks go to David Hughes <ukchugd@ukpmr.cs.philips.nl> who
  459. ;; helped to make it work on PC.
  460. (defun he-concat-directory-file-name (dir-part name-part)
  461. "Try to slam together two parts of a file specification, system dependently."
  462. (cond ((null dir-part) name-part)
  463. ((eq system-type 'ms-dos)
  464. (if (and (string-match "\\\\" dir-part)
  465. (not (string-match "/" dir-part))
  466. (= (aref name-part (1- (length name-part))) ?/))
  467. (aset name-part (1- (length name-part)) ?\\))
  468. (concat dir-part name-part))
  469. (t
  470. (concat dir-part name-part))))
  471. (defun try-complete-lisp-symbol (old)
  472. "Try to complete word as an Emacs Lisp symbol.
  473. The argument OLD has to be nil the first call of this function, and t
  474. for subsequent calls (for further possible completions of the same
  475. string). It returns t if a new completion is found, nil otherwise."
  476. (if (not old)
  477. (progn
  478. (he-init-string (he-lisp-symbol-beg) (point))
  479. (if (not (he-string-member he-search-string he-tried-table))
  480. (setq he-tried-table (cons he-search-string he-tried-table)))
  481. (setq he-expand-list
  482. (and (not (equal he-search-string ""))
  483. (sort (all-completions he-search-string obarray
  484. (function (lambda (sym)
  485. (or (boundp sym)
  486. (fboundp sym)
  487. (symbol-plist sym)))))
  488. 'string-lessp)))))
  489. (while (and he-expand-list
  490. (he-string-member (car he-expand-list) he-tried-table))
  491. (setq he-expand-list (cdr he-expand-list)))
  492. (if (null he-expand-list)
  493. (progn
  494. (if old (he-reset-string))
  495. ())
  496. (progn
  497. (he-substitute-string (car he-expand-list))
  498. (setq he-expand-list (cdr he-expand-list))
  499. t)))
  500. (defun try-complete-lisp-symbol-partially (old)
  501. "Try to complete as an Emacs Lisp symbol, as many characters as unique.
  502. The argument OLD has to be nil the first call of this function. It
  503. returns t if a unique, possibly partial, completion is found, nil
  504. otherwise."
  505. (let ((expansion ()))
  506. (if (not old)
  507. (progn
  508. (he-init-string (he-lisp-symbol-beg) (point))
  509. (if (not (string= he-search-string ""))
  510. (setq expansion
  511. (try-completion he-search-string obarray
  512. (function (lambda (sym)
  513. (or (boundp sym)
  514. (fboundp sym)
  515. (symbol-plist sym)))))))
  516. (if (or (eq expansion t)
  517. (string= expansion he-search-string)
  518. (he-string-member expansion he-tried-table))
  519. (setq expansion ()))))
  520. (if (not expansion)
  521. (progn
  522. (if old (he-reset-string))
  523. ())
  524. (progn
  525. (he-substitute-string expansion)
  526. t))))
  527. (defun he-lisp-symbol-beg ()
  528. (save-excursion
  529. (skip-syntax-backward "w_")
  530. (point)))
  531. (defun try-expand-line (old)
  532. "Try to complete the current line to an entire line in the buffer.
  533. The argument OLD has to be nil the first call of this function, and t
  534. for subsequent calls (for further possible completions of the same
  535. string). It returns t if a new completion is found, nil otherwise."
  536. (let ((expansion ())
  537. (strip-prompt (and (get-buffer-process (current-buffer))
  538. comint-use-prompt-regexp
  539. comint-prompt-regexp)))
  540. (if (not old)
  541. (progn
  542. (he-init-string (he-line-beg strip-prompt) (point))
  543. (set-marker he-search-loc he-string-beg)
  544. (setq he-search-bw t)))
  545. (if (not (equal he-search-string ""))
  546. (save-excursion
  547. (save-restriction
  548. (if hippie-expand-no-restriction
  549. (widen))
  550. ;; Try looking backward unless inhibited.
  551. (if he-search-bw
  552. (progn
  553. (goto-char he-search-loc)
  554. (setq expansion (he-line-search he-search-string
  555. strip-prompt t))
  556. (set-marker he-search-loc (point))
  557. (if (not expansion)
  558. (progn
  559. (set-marker he-search-loc he-string-end)
  560. (setq he-search-bw ())))))
  561. (if (not expansion) ; Then look forward.
  562. (progn
  563. (goto-char he-search-loc)
  564. (setq expansion (he-line-search he-search-string
  565. strip-prompt nil))
  566. (set-marker he-search-loc (point)))))))
  567. (if (not expansion)
  568. (progn
  569. (if old (he-reset-string))
  570. ())
  571. (progn
  572. (he-substitute-string expansion t)
  573. t))))
  574. (defun try-expand-line-all-buffers (old)
  575. "Try to complete the current line, searching all other buffers.
  576. The argument OLD has to be nil the first call of this function, and t
  577. for subsequent calls (for further possible completions of the same
  578. string). It returns t if a new completion is found, nil otherwise."
  579. (he--all-buffers
  580. old
  581. (lambda () (he-line-beg (and (get-buffer-process (current-buffer))
  582. comint-use-prompt-regexp
  583. comint-prompt-regexp)))
  584. (lambda (string)
  585. (he-line-search string
  586. (and (get-buffer-process (current-buffer))
  587. comint-use-prompt-regexp
  588. comint-prompt-regexp)
  589. nil))))
  590. (defun he--all-buffers (old beg-function search-function)
  591. (let ((expansion ())
  592. (buf (current-buffer))
  593. (only-buffers hippie-expand-only-buffers)
  594. (ignore-buffers hippie-expand-ignore-buffers)
  595. (orig-case-fold-search case-fold-search))
  596. (if (not old)
  597. (progn
  598. (he-init-string (funcall beg-function) (point))
  599. (setq he-search-bufs (buffer-list))
  600. (setq he-searched-n-bufs 0)
  601. (set-marker he-search-loc 1 (car he-search-bufs))))
  602. (if (not (equal he-search-string ""))
  603. (while (and he-search-bufs
  604. (not expansion)
  605. (or (not hippie-expand-max-buffers)
  606. (< he-searched-n-bufs hippie-expand-max-buffers)))
  607. (set-buffer (car he-search-bufs))
  608. (if (and (not (eq (current-buffer) buf))
  609. (if only-buffers
  610. (he-buffer-member only-buffers)
  611. (not (he-buffer-member ignore-buffers))))
  612. (save-excursion
  613. (save-restriction
  614. (if hippie-expand-no-restriction
  615. (widen))
  616. (goto-char he-search-loc)
  617. (setq expansion
  618. (let ((case-fold-search orig-case-fold-search))
  619. (funcall search-function he-search-string)))
  620. (set-marker he-search-loc (point))
  621. (if (not expansion)
  622. (progn
  623. (setq he-search-bufs (cdr he-search-bufs))
  624. (setq he-searched-n-bufs (1+ he-searched-n-bufs))
  625. (set-marker he-search-loc 1 (car he-search-bufs))))))
  626. (setq he-search-bufs (cdr he-search-bufs))
  627. (set-marker he-search-loc 1 (car he-search-bufs)))))
  628. (set-buffer buf)
  629. (if (not expansion)
  630. (progn
  631. (if old (he-reset-string))
  632. ())
  633. (progn
  634. (he-substitute-string expansion t)
  635. t))))
  636. (defun he-line-search (str strip-prompt reverse)
  637. (let ((result ()))
  638. (while (and (not result)
  639. (if reverse
  640. (re-search-backward
  641. (he-line-search-regexp str strip-prompt)
  642. nil t)
  643. (re-search-forward
  644. (he-line-search-regexp str strip-prompt)
  645. nil t)))
  646. (setq result (buffer-substring-no-properties (match-end 1)
  647. (match-end 0)))
  648. (if (he-string-member result he-tried-table t)
  649. (setq result nil))) ; if already in table, ignore
  650. result))
  651. (defun he-line-beg (strip-prompt)
  652. (save-excursion
  653. (if (re-search-backward (he-line-search-regexp "" strip-prompt)
  654. (line-beginning-position) t)
  655. (match-beginning 2)
  656. (point))))
  657. (defun he-line-search-regexp (pat strip-prompt)
  658. (if strip-prompt
  659. (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\(?2:"
  660. (regexp-quote pat)
  661. "[^\n]*[^ \t\n]\\)")
  662. (concat "^\\(\\s-*\\)\\("
  663. (regexp-quote pat)
  664. "[^\n]*[^ \t\n]\\)")))
  665. (defun try-expand-list (old)
  666. "Try to complete the current beginning of a list.
  667. The argument OLD has to be nil the first call of this function, and t
  668. for subsequent calls (for further possible completions of the same
  669. string). It returns t if a new completion is found, nil otherwise."
  670. (let ((expansion ()))
  671. (if (not old)
  672. (progn
  673. (he-init-string (he-list-beg) (point))
  674. (set-marker he-search-loc he-string-beg)
  675. (setq he-search-bw t)))
  676. (if (not (equal he-search-string ""))
  677. (save-excursion
  678. (save-restriction
  679. (if hippie-expand-no-restriction
  680. (widen))
  681. ;; Try looking backward unless inhibited.
  682. (if he-search-bw
  683. (progn
  684. (goto-char he-search-loc)
  685. (setq expansion (he-list-search he-search-string t))
  686. (set-marker he-search-loc (point))
  687. (if (not expansion)
  688. (progn
  689. (set-marker he-search-loc he-string-end)
  690. (setq he-search-bw ())))))
  691. (if (not expansion) ; Then look forward.
  692. (progn
  693. (goto-char he-search-loc)
  694. (setq expansion (he-list-search he-search-string nil))
  695. (set-marker he-search-loc (point)))))))
  696. (if (not expansion)
  697. (progn
  698. (if old (he-reset-string))
  699. ())
  700. (progn
  701. (he-substitute-string expansion t)
  702. t))))
  703. (defun try-expand-list-all-buffers (old)
  704. "Try to complete the current list, searching all other buffers.
  705. The argument OLD has to be nil the first call of this function, and t
  706. for subsequent calls (for further possible completions of the same
  707. string). It returns t if a new completion is found, nil otherwise."
  708. (he--all-buffers old #'he-list-beg #'he-list-search))
  709. (defun he-list-search (str &optional reverse)
  710. (let ((result ())
  711. beg pos err)
  712. (while (and (not result)
  713. (if reverse
  714. (search-backward str nil t)
  715. (search-forward str nil t)))
  716. (setq pos (point))
  717. (setq beg (match-beginning 0))
  718. (goto-char beg)
  719. (setq err ())
  720. (condition-case ()
  721. (forward-list 1)
  722. (error (setq err t)))
  723. (if (and reverse
  724. (> (point) he-string-beg))
  725. (setq err t))
  726. (if (not err)
  727. (progn
  728. (setq result (buffer-substring-no-properties beg (point)))
  729. (if (he-string-member result he-tried-table t)
  730. (setq result nil)))) ; if already in table, ignore
  731. (goto-char pos))
  732. result))
  733. (defun he-list-beg ()
  734. (save-excursion
  735. (condition-case ()
  736. (backward-up-list 1)
  737. (error ()))
  738. (point)))
  739. (defun try-expand-all-abbrevs (old)
  740. "Try to expand word before point according to all abbrev tables.
  741. The argument OLD has to be nil the first call of this function, and t
  742. for subsequent calls (for further possible expansions of the same
  743. string). It returns t if a new expansion is found, nil otherwise."
  744. (if (not old)
  745. (progn
  746. (he-init-string (he-dabbrev-beg) (point))
  747. (setq he-expand-list
  748. (and (not (equal he-search-string ""))
  749. (mapcar (function (lambda (sym)
  750. (if (and (boundp sym) (vectorp (eval sym)))
  751. (abbrev-expansion (downcase he-search-string)
  752. (eval sym)))))
  753. (append '(local-abbrev-table
  754. global-abbrev-table)
  755. abbrev-table-name-list))))))
  756. (while (and he-expand-list
  757. (or (not (car he-expand-list))
  758. (he-string-member (car he-expand-list) he-tried-table t)))
  759. (setq he-expand-list (cdr he-expand-list)))
  760. (if (null he-expand-list)
  761. (progn
  762. (if old (he-reset-string))
  763. ())
  764. (progn
  765. (he-substitute-string (car he-expand-list) t)
  766. (setq he-expand-list (cdr he-expand-list))
  767. t)))
  768. (defun try-expand-dabbrev (old)
  769. "Try to expand word \"dynamically\", searching the current buffer.
  770. The argument OLD has to be nil the first call of this function, and t
  771. for subsequent calls (for further possible expansions of the same
  772. string). It returns t if a new expansion is found, nil otherwise."
  773. (let ((expansion ()))
  774. (if (not old)
  775. (progn
  776. (he-init-string (he-dabbrev-beg) (point))
  777. (set-marker he-search-loc he-string-beg)
  778. (setq he-search-bw t)))
  779. (if (not (equal he-search-string ""))
  780. (save-excursion
  781. (save-restriction
  782. (if hippie-expand-no-restriction
  783. (widen))
  784. ;; Try looking backward unless inhibited.
  785. (if he-search-bw
  786. (progn
  787. (goto-char he-search-loc)
  788. (setq expansion (he-dabbrev-search he-search-string t))
  789. (set-marker he-search-loc (point))
  790. (if (not expansion)
  791. (progn
  792. (set-marker he-search-loc he-string-end)
  793. (setq he-search-bw ())))))
  794. (if (not expansion) ; Then look forward.
  795. (progn
  796. (goto-char he-search-loc)
  797. (setq expansion (he-dabbrev-search he-search-string nil))
  798. (set-marker he-search-loc (point)))))))
  799. (if (not expansion)
  800. (progn
  801. (if old (he-reset-string))
  802. ())
  803. (progn
  804. (he-substitute-string expansion t)
  805. t))))
  806. (defun try-expand-dabbrev-all-buffers (old)
  807. "Try to expand word \"dynamically\", searching all other buffers.
  808. The argument OLD has to be nil the first call of this function, and t
  809. for subsequent calls (for further possible expansions of the same
  810. string). It returns t if a new expansion is found, nil otherwise."
  811. (he--all-buffers old #'he-dabbrev-beg #'he-dabbrev-search))
  812. ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
  813. ;; suggested this one.
  814. (defun try-expand-dabbrev-visible (old)
  815. "Try to expand word \"dynamically\", searching visible window parts.
  816. The argument OLD has to be nil the first call of this function, and t
  817. for subsequent calls (for further possible expansions of the same
  818. string). It returns t if a new expansion is found, nil otherwise."
  819. (let ((expansion ())
  820. (flag (if (frame-visible-p (window-frame))
  821. 'visible t)))
  822. (unless old
  823. (he-init-string (he-dabbrev-beg) (point))
  824. (setq he-search-window (selected-window))
  825. (set-marker he-search-loc
  826. (window-start he-search-window)
  827. (window-buffer he-search-window)))
  828. (while (and (not (equal he-search-string ""))
  829. (marker-position he-search-loc)
  830. (not expansion))
  831. (with-current-buffer (marker-buffer he-search-loc)
  832. (save-excursion
  833. (goto-char he-search-loc)
  834. (setq expansion (he-dabbrev-search he-search-string ()
  835. (window-end he-search-window)))
  836. (if (and expansion
  837. (eq (marker-buffer he-string-beg) (current-buffer))
  838. (eq (marker-position he-string-beg) (match-beginning 0)))
  839. (setq expansion
  840. (he-dabbrev-search he-search-string ()
  841. (window-end he-search-window))))
  842. (set-marker he-search-loc (point) (current-buffer))))
  843. (unless expansion
  844. (setq he-search-window (next-window he-search-window nil flag))
  845. (if (eq he-search-window (selected-window))
  846. (set-marker he-search-loc nil)
  847. (set-marker he-search-loc (window-start he-search-window)
  848. (window-buffer he-search-window)))))
  849. (if (not expansion)
  850. (progn
  851. (if old (he-reset-string))
  852. ())
  853. (progn
  854. (he-substitute-string expansion t)
  855. t))))
  856. (defun he-dabbrev-search (pattern &optional reverse limit)
  857. (let ((result ())
  858. (regpat (cond ((not hippie-expand-dabbrev-as-symbol)
  859. (concat "\\<" (regexp-quote pattern) "\\sw+"))
  860. ((eq (char-syntax (aref pattern 0)) ?_)
  861. (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
  862. (t
  863. (concat "\\<" (regexp-quote pattern)
  864. "\\(\\sw\\|\\s_\\)+")))))
  865. (while (and (not result)
  866. (if reverse
  867. (re-search-backward regpat limit t)
  868. (re-search-forward regpat limit t)))
  869. (setq result (buffer-substring-no-properties (match-beginning 0)
  870. (match-end 0)))
  871. (if (or (and hippie-expand-dabbrev-as-symbol
  872. (> (match-beginning 0) (point-min))
  873. (memq (char-syntax (char-after (1- (match-beginning 0))))
  874. '(?_ ?w)))
  875. (he-string-member result he-tried-table t))
  876. (setq result nil))) ; ignore if bad prefix or already in table
  877. result))
  878. (defun he-dabbrev-beg ()
  879. (let ((op (point)))
  880. (save-excursion
  881. (if hippie-expand-dabbrev-skip-space
  882. (skip-syntax-backward ". "))
  883. (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
  884. "w_" "w"))
  885. 0)
  886. op
  887. (point)))))
  888. (defun try-expand-dabbrev-from-kill (old)
  889. "Try to expand word \"dynamically\", searching the kill ring.
  890. The argument OLD has to be nil the first call of this function, and t
  891. for subsequent calls (for further possible completions of the same
  892. string). It returns t if a new completion is found, nil otherwise."
  893. (let ((expansion ()))
  894. (if (not old)
  895. (progn
  896. (he-init-string (he-dabbrev-beg) (point))
  897. (setq he-expand-list
  898. (if (not (equal he-search-string ""))
  899. kill-ring))
  900. (setq he-search-loc2 0)))
  901. (if (not (equal he-search-string ""))
  902. (setq expansion (he-dabbrev-kill-search he-search-string)))
  903. (if (not expansion)
  904. (progn
  905. (if old (he-reset-string))
  906. ())
  907. (progn
  908. (he-substitute-string expansion t)
  909. t))))
  910. (defun he-dabbrev-kill-search (pattern)
  911. (let ((result ())
  912. (regpat (cond ((not hippie-expand-dabbrev-as-symbol)
  913. (concat "\\<" (regexp-quote pattern) "\\sw+"))
  914. ((eq (char-syntax (aref pattern 0)) ?_)
  915. (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
  916. (t
  917. (concat "\\<" (regexp-quote pattern)
  918. "\\(\\sw\\|\\s_\\)+"))))
  919. (killstr (car he-expand-list)))
  920. (while (and (not result)
  921. he-expand-list)
  922. (while (and (not result)
  923. (string-match regpat killstr he-search-loc2))
  924. (setq result (substring killstr (match-beginning 0) (match-end 0)))
  925. (set-text-properties 0 (length result) () result)
  926. (setq he-search-loc2 (1+ (match-beginning 0)))
  927. (if (or (and hippie-expand-dabbrev-as-symbol
  928. (> (match-beginning 0) 0)
  929. (memq (char-syntax (aref killstr (1- (match-beginning 0))))
  930. '(?_ ?w)))
  931. (he-string-member result he-tried-table t))
  932. (setq result nil))) ; ignore if bad prefix or already in table
  933. (if (and (not result)
  934. he-expand-list)
  935. (progn
  936. (setq he-expand-list (cdr he-expand-list))
  937. (setq killstr (car he-expand-list))
  938. (setq he-search-loc2 0))))
  939. result))
  940. (defun try-expand-whole-kill (old)
  941. "Try to complete text with something from the kill ring.
  942. The argument OLD has to be nil the first call of this function, and t
  943. for subsequent calls (for further possible completions of the same
  944. string). It returns t if a new completion is found, nil otherwise."
  945. (let ((expansion ()))
  946. (if (not old)
  947. (progn
  948. (he-init-string (he-kill-beg) (point))
  949. (if (not (he-string-member he-search-string he-tried-table))
  950. (setq he-tried-table (cons he-search-string he-tried-table)))
  951. (setq he-expand-list
  952. (if (not (equal he-search-string ""))
  953. kill-ring))
  954. (setq he-search-loc2 ())))
  955. (if (not (equal he-search-string ""))
  956. (setq expansion (he-whole-kill-search he-search-string)))
  957. (if (not expansion)
  958. (progn
  959. (if old (he-reset-string))
  960. ())
  961. (progn
  962. (he-substitute-string expansion)
  963. t))))
  964. (defun he-whole-kill-search (str)
  965. (let ((case-fold-search ())
  966. (result ())
  967. (str (regexp-quote str))
  968. (killstr (car he-expand-list))
  969. (pos -1))
  970. (while (and (not result)
  971. he-expand-list)
  972. (if (not he-search-loc2)
  973. (while (setq pos (string-match str killstr (1+ pos)))
  974. (setq he-search-loc2 (cons pos he-search-loc2))))
  975. (while (and (not result)
  976. he-search-loc2)
  977. (setq pos (car he-search-loc2))
  978. (setq he-search-loc2 (cdr he-search-loc2))
  979. (save-excursion
  980. (goto-char he-string-beg)
  981. (if (and (>= (- (point) pos) (point-min)) ; avoid some string GC
  982. (eq (char-after (- (point) pos)) (aref killstr 0))
  983. (search-backward (substring killstr 0 pos)
  984. (- (point) pos) t))
  985. (progn
  986. (setq result (substring killstr pos))
  987. (set-text-properties 0 (length result) () result))))
  988. (if (and result
  989. (he-string-member result he-tried-table))
  990. (setq result nil))) ; ignore if already in table
  991. (if (and (not result)
  992. he-expand-list)
  993. (progn
  994. (setq he-expand-list (cdr he-expand-list))
  995. (setq killstr (car he-expand-list))
  996. (setq pos -1))))
  997. result))
  998. (defun he-kill-beg ()
  999. (let ((op (point)))
  1000. (save-excursion
  1001. (skip-syntax-backward "^w_")
  1002. (if (= (skip-syntax-backward "w_") 0)
  1003. op
  1004. (point)))))
  1005. (provide 'hippie-exp)
  1006. ;;; hippie-exp.el ends here