Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2163 lines
73 KiB

  1. ;;; auto-complete.el --- Auto Completion for GNU Emacs
  2. ;; Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Tomohiro Matsuyama
  3. ;; Author: Tomohiro Matsuyama <m2ym.pub@gmail.com>
  4. ;; URL: https://github.com/auto-complete/auto-complete
  5. ;; Keywords: completion, convenience
  6. ;; Version: 1.5.1
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This extension provides a way to complete with popup menu like:
  20. ;;
  21. ;; def-!-
  22. ;; +-----------------+
  23. ;; |defun::::::::::::|
  24. ;; |defvar |
  25. ;; |defmacro |
  26. ;; | ... |
  27. ;; +-----------------+
  28. ;;
  29. ;; You can complete by typing and selecting menu.
  30. ;;
  31. ;; Entire documents are located in doc/ directory.
  32. ;; Take a look for information.
  33. ;;
  34. ;; Enjoy!
  35. ;;; Code:
  36. (defconst ac-version "1.5.1"
  37. "Version of auto-complete in string format.
  38. Use `version-to-list' to get version component.")
  39. (defconst ac-version-major (car (version-to-list ac-version))
  40. "Major version number of auto-complete")
  41. (defconst ac-version-minor (cadr (version-to-list ac-version))
  42. "Minor version number of auto-complete")
  43. (require 'cl-lib)
  44. (require 'popup)
  45. ;;;; Global stuff
  46. (defun ac-error (&optional var)
  47. "Report an error and disable `auto-complete-mode'."
  48. (ignore-errors
  49. (message "auto-complete error: %s" var)
  50. (auto-complete-mode -1)
  51. var))
  52. ;;;; Customization
  53. (defgroup auto-complete nil
  54. "Auto completion."
  55. :group 'completion
  56. :prefix "ac-")
  57. (defcustom ac-delay 0.1
  58. "Delay to completions will be available."
  59. :type 'float
  60. :group 'auto-complete)
  61. (defcustom ac-auto-show-menu 0.8
  62. "Non-nil means completion menu will be automatically shown."
  63. :type '(choice (const :tag "Yes" t)
  64. (const :tag "Never" nil)
  65. (float :tag "Timer"))
  66. :group 'auto-complete)
  67. (defcustom ac-show-menu-immediately-on-auto-complete t
  68. "Non-nil means menu will be showed immediately on `auto-complete'."
  69. :type 'boolean
  70. :group 'auto-complete)
  71. (defcustom ac-expand-on-auto-complete t
  72. "Non-nil means expand whole common part on first time `auto-complete'."
  73. :type 'boolean
  74. :group 'auto-complete)
  75. (defcustom ac-disable-faces '(font-lock-comment-face font-lock-string-face font-lock-doc-face)
  76. "Non-nil means disable automatic completion on specified faces."
  77. :type '(repeat symbol)
  78. :group 'auto-complete)
  79. (defcustom ac-stop-flymake-on-completing t
  80. "Non-nil means disble flymake temporarily on completing."
  81. :type 'boolean
  82. :group 'auto-complete)
  83. (defcustom ac-flycheck-poll-completion-end-interval 0.5
  84. "Polling interval to restart automatically flycheck's checking after completion is end."
  85. :type 'float
  86. :group 'auto-complete)
  87. (defcustom ac-use-fuzzy (and (locate-library "fuzzy") t)
  88. "Non-nil means use fuzzy matching."
  89. :type 'boolean
  90. :group 'auto-complete)
  91. (defcustom ac-fuzzy-cursor-color "red"
  92. "Cursor color in fuzzy mode."
  93. :type 'string
  94. :group 'auto-complete)
  95. (defcustom ac-use-comphist t
  96. "Non-nil means use intelligent completion history."
  97. :type 'boolean
  98. :group 'auto-complete)
  99. (defcustom ac-comphist-threshold 0.7
  100. "Percentage of ignoring low scored candidates."
  101. :type 'float
  102. :group 'auto-complete)
  103. (defcustom ac-comphist-file
  104. (expand-file-name (concat (if (boundp 'user-emacs-directory)
  105. user-emacs-directory
  106. "~/.emacs.d/")
  107. "/ac-comphist.dat"))
  108. "Completion history file name."
  109. :type 'string
  110. :group 'auto-complete)
  111. (defcustom ac-user-dictionary nil
  112. "User defined dictionary"
  113. :type '(repeat string)
  114. :group 'auto-complete)
  115. (defcustom ac-dictionary-files '("~/.dict")
  116. "Dictionary files."
  117. :type '(repeat string)
  118. :group 'auto-complete)
  119. (defvaralias 'ac-user-dictionary-files 'ac-dictionary-files)
  120. (defcustom ac-dictionary-directories
  121. (ignore-errors
  122. (when load-file-name
  123. (let ((installed-dir (file-name-directory load-file-name)))
  124. (cl-loop for name in '("ac-dict" "dict")
  125. for dir = (concat installed-dir name)
  126. if (file-directory-p dir)
  127. collect dir))))
  128. "Dictionary directories."
  129. :type '(repeat string)
  130. :group 'auto-complete)
  131. (defcustom ac-use-quick-help t
  132. "Non-nil means use quick help."
  133. :type 'boolean
  134. :group 'auto-complete)
  135. (defcustom ac-quick-help-delay 1.5
  136. "Delay to show quick help."
  137. :type 'float
  138. :group 'auto-complete)
  139. (defcustom ac-menu-height 10
  140. "Max height of candidate menu."
  141. :type 'integer
  142. :group 'auto-complete)
  143. (defvaralias 'ac-candidate-menu-height 'ac-menu-height)
  144. (defcustom ac-quick-help-height 20
  145. "Max height of quick help."
  146. :type 'integer
  147. :group 'auto-complete)
  148. (defcustom ac-quick-help-prefer-pos-tip t
  149. "Prefer native tooltip with pos-tip than overlay popup for displaying quick help."
  150. :type 'boolean
  151. :group 'auto-complete)
  152. (defvaralias 'ac-quick-help-prefer-x 'ac-quick-help-prefer-pos-tip)
  153. (defcustom ac-candidate-limit nil
  154. "Limit number of candidates. Non-integer means no limit."
  155. :type 'integer
  156. :group 'auto-complete)
  157. (defvaralias 'ac-candidate-max 'ac-candidate-limit)
  158. (defcustom ac-modes
  159. '(emacs-lisp-mode lisp-mode lisp-interaction-mode
  160. slime-repl-mode
  161. nim-mode c-mode cc-mode c++-mode go-mode
  162. java-mode malabar-mode clojure-mode clojurescript-mode scala-mode
  163. scheme-mode
  164. ocaml-mode tuareg-mode coq-mode haskell-mode agda-mode agda2-mode
  165. perl-mode cperl-mode python-mode ruby-mode lua-mode tcl-mode
  166. ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode scss-mode less-css-mode
  167. makefile-mode sh-mode fortran-mode f90-mode ada-mode
  168. xml-mode sgml-mode web-mode
  169. ts-mode
  170. sclang-mode
  171. verilog-mode
  172. qml-mode
  173. apples-mode)
  174. "Major modes `auto-complete-mode' can run on."
  175. :type '(repeat symbol)
  176. :group 'auto-complete)
  177. (defcustom ac-compatible-packages-regexp
  178. "^ac-"
  179. "Regexp to indicate what packages can work with auto-complete."
  180. :type 'string
  181. :group 'auto-complete)
  182. (defcustom ac-non-trigger-commands
  183. '(*table--cell-self-insert-command
  184. electric-buffer-list)
  185. "Commands that can't be used as triggers of `auto-complete'."
  186. :type '(repeat symbol)
  187. :group 'auto-complete)
  188. (defcustom ac-trigger-commands
  189. '(self-insert-command)
  190. "Trigger commands that specify whether `auto-complete' should start or not."
  191. :type '(repeat symbol)
  192. :group 'auto-complete)
  193. (defcustom ac-trigger-commands-on-completing
  194. '(delete-backward-char
  195. backward-delete-char
  196. backward-delete-char-untabify
  197. ;; autopair
  198. autopair-backspace
  199. ;; paredit
  200. paredit-backward-delete
  201. paredit-backward-delete-word)
  202. "Trigger commands that specify whether `auto-complete' should continue or not."
  203. :type '(repeat symbol)
  204. :group 'auto-complete)
  205. (defcustom ac-trigger-key nil
  206. "Non-nil means `auto-complete' will start by typing this key.
  207. If you specify this TAB, for example, `auto-complete' will start by typing TAB,
  208. and if there is no completions, an original command will be fallbacked."
  209. :type '(choice (const :tag "None" nil)
  210. (string :tag "Key"))
  211. :group 'auto-complete
  212. :set (lambda (symbol value)
  213. (set-default symbol value)
  214. (when (and value
  215. (fboundp 'ac-set-trigger-key))
  216. (ac-set-trigger-key value))))
  217. (defcustom ac-auto-start 2
  218. "Non-nil means completion will be started automatically.
  219. Positive integer means if a length of a word you entered is larger than the value,
  220. completion will be started automatically.
  221. If you specify `nil', never be started automatically."
  222. :type '(choice (const :tag "Yes" t)
  223. (const :tag "Never" nil)
  224. (integer :tag "Require"))
  225. :group 'auto-complete)
  226. (defcustom ac-stop-words nil
  227. "List of string to stop completion."
  228. :type '(repeat string)
  229. :group 'auto-complete)
  230. (defvaralias 'ac-ignores 'ac-stop-words)
  231. (defcustom ac-use-dictionary-as-stop-words t
  232. "Non-nil means a buffer related dictionary will be thought of as stop words."
  233. :type 'boolean
  234. :group 'auto-complete)
  235. (defcustom ac-ignore-case 'smart
  236. "Non-nil means auto-complete ignores case.
  237. If this value is `smart', auto-complete ignores case only when
  238. a prefix doesn't contain any upper case letters."
  239. :type '(choice (const :tag "Yes" t)
  240. (const :tag "Smart" smart)
  241. (const :tag "No" nil))
  242. :group 'auto-complete)
  243. (defcustom ac-dwim t
  244. "Non-nil means `auto-complete' works based on Do What I Mean."
  245. :type 'boolean
  246. :group 'auto-complete)
  247. (defcustom ac-use-menu-map nil
  248. "Non-nil means a special keymap `ac-menu-map' on completing menu will be used."
  249. :type 'boolean
  250. :group 'auto-complete)
  251. (defcustom ac-use-overriding-local-map nil
  252. "Non-nil means `overriding-local-map' will be used to hack for overriding key events on auto-completion."
  253. :type 'boolean
  254. :group 'auto-complete)
  255. (defcustom ac-disable-inline nil
  256. "Non-nil disable inline completion visibility"
  257. :type 'boolean
  258. :group 'auto-complete)
  259. (defcustom ac-candidate-menu-min 1
  260. "Number of candidates required to display menu"
  261. :type 'integer
  262. :group 'auto-complete)
  263. (defcustom ac-max-width nil
  264. "Maximum width for auto-complete menu to have"
  265. :type '(choice (const :tag "No limit" nil)
  266. (const :tag "Character Limit" 25)
  267. (const :tag "Window Ratio Limit" 0.5))
  268. :group 'auto-complete)
  269. (defface ac-completion-face
  270. '((t (:foreground "darkgray" :underline t)))
  271. "Face for inline completion"
  272. :group 'auto-complete)
  273. (defface ac-candidate-face
  274. '((t (:inherit popup-face)))
  275. "Face for candidate."
  276. :group 'auto-complete)
  277. (defface ac-candidate-mouse-face
  278. '((t (:inherit popup-menu-mouse-face)))
  279. "Mouse face for candidate."
  280. :group 'auto-complete)
  281. (defface ac-selection-face
  282. '((t (:inherit popup-menu-selection-face)))
  283. "Face for selected candidate."
  284. :group 'auto-complete)
  285. (defvar auto-complete-mode-hook nil
  286. "Hook for `auto-complete-mode'.")
  287. ;;;; Internal variables
  288. (defvar auto-complete-mode nil
  289. "Dummy variable to suppress compiler warnings.")
  290. (defvar ac-cursor-color nil
  291. "Old cursor color.")
  292. (defvar ac-inline nil
  293. "Inline completion instance.")
  294. (defvar ac-menu nil
  295. "Menu instance.")
  296. (defvar ac-show-menu nil
  297. "Flag to show menu on timer tick.")
  298. (defvar ac-last-completion nil
  299. "Cons of prefix marker and selected item of last completion.")
  300. (defvar ac-quick-help nil
  301. "Quick help instance")
  302. (defvar ac-completing nil
  303. "Non-nil means `auto-complete-mode' is now working on completion.")
  304. (defvar ac-buffer nil
  305. "Buffer where auto-complete is started.")
  306. (defvar ac-point nil
  307. "Start point of prefix.")
  308. (defvar ac-last-point nil
  309. "Last point of updating pattern.")
  310. (defvar ac-prefix nil
  311. "Prefix string.")
  312. (defvaralias 'ac-target 'ac-prefix)
  313. (defvar ac-selected-candidate nil
  314. "Last selected candidate.")
  315. (defvar ac-common-part nil
  316. "Common part string of meaningful candidates.
  317. If there is no common part, this will be nil.")
  318. (defvar ac-whole-common-part nil
  319. "Common part string of whole candidates.
  320. If there is no common part, this will be nil.")
  321. (defvar ac-prefix-overlay nil
  322. "Overlay for prefix string.")
  323. (defvar ac-timer nil
  324. "Completion idle timer.")
  325. (defvar ac-show-menu-timer nil
  326. "Show menu idle timer.")
  327. (defvar ac-quick-help-timer nil
  328. "Quick help idle timer.")
  329. (defvar ac-triggered nil
  330. "Flag to update.")
  331. (defvar ac-limit nil
  332. "Limit number of candidates for each sources.")
  333. (defvar ac-candidates nil
  334. "Current candidates.")
  335. (defvar ac-candidates-cache nil
  336. "Candidates cache for individual sources.")
  337. (defvar ac-fuzzy-enable nil
  338. "Non-nil means fuzzy matching is enabled.")
  339. (defvar ac-dwim-enable nil
  340. "Non-nil means DWIM completion will be allowed.")
  341. (defvar ac-mode-map (make-sparse-keymap)
  342. "Auto-complete mode map. It is also used for trigger key command. See also `ac-trigger-key'.")
  343. (defvar ac-completing-map
  344. (let ((map (make-sparse-keymap)))
  345. (define-key map "\t" 'ac-expand)
  346. (define-key map [tab] 'ac-expand)
  347. (define-key map "\r" 'ac-complete)
  348. (define-key map (kbd "M-TAB") 'auto-complete)
  349. (define-key map "\M-n" 'ac-next)
  350. (define-key map "\M-p" 'ac-previous)
  351. (define-key map [down] 'ac-next)
  352. (define-key map [up] 'ac-previous)
  353. (define-key map [f1] 'ac-help)
  354. (define-key map [M-f1] 'ac-persist-help)
  355. (define-key map (kbd "C-?") 'ac-help)
  356. (define-key map (kbd "C-M-?") 'ac-persist-help)
  357. (define-key map [C-down] 'ac-quick-help-scroll-down)
  358. (define-key map [C-up] 'ac-quick-help-scroll-up)
  359. (define-key map "\C-\M-n" 'ac-quick-help-scroll-down)
  360. (define-key map "\C-\M-p" 'ac-quick-help-scroll-up)
  361. (dotimes (i 9)
  362. (let ((symbol (intern (format "ac-complete-select-%d" (1+ i)))))
  363. (fset symbol
  364. `(lambda ()
  365. (interactive)
  366. (when (and (ac-menu-live-p) (popup-select ac-menu ,i))
  367. (ac-complete))))
  368. (define-key map (read-kbd-macro (format "M-%s" (1+ i))) symbol)))
  369. map)
  370. "Keymap for completion.")
  371. (defvaralias 'ac-complete-mode-map 'ac-completing-map)
  372. (defvar ac-menu-map
  373. (let ((map (make-sparse-keymap)))
  374. (set-keymap-parent map ac-completing-map)
  375. (define-key map (kbd "RET") 'ac-complete)
  376. (define-key map "\C-n" 'ac-next)
  377. (define-key map "\C-p" 'ac-previous)
  378. (define-key map "\C-s" 'ac-isearch)
  379. (define-key map [mouse-1] 'ac-mouse-1)
  380. (define-key map [down-mouse-1] 'ac-ignore)
  381. (define-key map [mouse-4] 'ac-mouse-4)
  382. (define-key map [mouse-5] 'ac-mouse-5)
  383. map)
  384. "Keymap for completion on completing menu.")
  385. (defvar ac-current-map
  386. (let ((map (make-sparse-keymap)))
  387. (set-keymap-parent map ac-completing-map)
  388. map))
  389. (defvar ac-match-function 'all-completions
  390. "Default match function.")
  391. (defvar ac-prefix-definitions
  392. '((symbol . ac-prefix-symbol)
  393. (file . ac-prefix-file)
  394. (valid-file . ac-prefix-valid-file)
  395. (c-dot . ac-prefix-c-dot)
  396. (c-dot-ref . ac-prefix-c-dot-ref)
  397. (cc-member . ac-prefix-cc-member))
  398. "Prefix definitions for common use.")
  399. (defvar ac-sources '(ac-source-words-in-same-mode-buffers)
  400. "Sources for completion.")
  401. (make-variable-buffer-local 'ac-sources)
  402. (defvar ac-compiled-sources nil
  403. "Compiled source of `ac-sources'.")
  404. (defvar ac-current-sources nil
  405. "Current working sources. This is sublist of `ac-compiled-sources'.")
  406. (defvar ac-omni-completion-sources nil
  407. "Do not use this anymore.")
  408. (defvar ac-current-prefix-def nil)
  409. (defvar ac-ignoring-prefix-def nil)
  410. ;;;; Intelligent completion history
  411. (defvar ac-comphist nil
  412. "Database of completion history.")
  413. (defsubst ac-comphist-make-tab ()
  414. (make-hash-table :test 'equal))
  415. (defsubst ac-comphist-tab (db)
  416. (nth 0 db))
  417. (defsubst ac-comphist-cache (db)
  418. (nth 1 db))
  419. (defun ac-comphist-make (&optional tab)
  420. (list (or tab (ac-comphist-make-tab)) (make-hash-table :test 'equal :weakness t)))
  421. (defun ac-comphist-get (db string &optional create)
  422. (let* ((tab (ac-comphist-tab db))
  423. (index (gethash string tab)))
  424. (when (and create (null index))
  425. (setq index (make-vector (length string) 0))
  426. (puthash string index tab))
  427. index))
  428. (defun ac-comphist-add (db string prefix)
  429. (setq prefix (min prefix (1- (length string))))
  430. (when (<= 0 prefix)
  431. (setq string (substring-no-properties string))
  432. (let ((stat (ac-comphist-get db string t)))
  433. (cl-incf (aref stat prefix))
  434. (remhash string (ac-comphist-cache db)))))
  435. (defun ac-comphist-score (db string prefix)
  436. (setq prefix (min prefix (1- (length string))))
  437. (if (<= 0 prefix)
  438. (let ((cache (gethash string (ac-comphist-cache db))))
  439. (or (and cache (aref cache prefix))
  440. (let ((stat (ac-comphist-get db string))
  441. (score 0.0))
  442. (when stat
  443. (cl-loop for p from 0 below (length string)
  444. ;; sigmoid function
  445. with a = 5
  446. with b = (/ 700.0 a) ; bounds for avoiding range error in `exp'
  447. with d = (/ 6.0 a)
  448. for x = (max (- b) (min b (- d (abs (- prefix p)))))
  449. for r = (/ 1.0 (1+ (exp (* (- a) x))))
  450. do
  451. (cl-incf score (* (aref stat p) r))))
  452. ;; Weight by distance
  453. (cl-incf score (max 0.0 (- 0.3 (/ (- (length string) prefix) 100.0))))
  454. (unless cache
  455. (setq cache (make-vector (length string) nil))
  456. (puthash string cache (ac-comphist-cache db)))
  457. (aset cache prefix score)
  458. score)))
  459. 0.0))
  460. (defun ac-comphist-sort (db collection prefix &optional threshold)
  461. (let (result
  462. (n 0)
  463. (total 0)
  464. (cur 0))
  465. (setq result (mapcar (lambda (a)
  466. (when (and cur threshold)
  467. (if (>= cur (* total threshold))
  468. (setq cur nil)
  469. (cl-incf n)
  470. (cl-incf cur (cdr a))))
  471. (car a))
  472. (sort (mapcar (lambda (string)
  473. (let ((score (ac-comphist-score db string prefix)))
  474. (cl-incf total score)
  475. (cons string score)))
  476. collection)
  477. (lambda (a b) (< (cdr b) (cdr a))))))
  478. (if threshold
  479. (cons n result)
  480. result)))
  481. (defun ac-comphist-serialize (db)
  482. (let (alist)
  483. (maphash (lambda (k v)
  484. (push (cons k v) alist))
  485. (ac-comphist-tab db))
  486. (list alist)))
  487. (defun ac-comphist-deserialize (sexp)
  488. (condition-case nil
  489. (ac-comphist-make (let ((tab (ac-comphist-make-tab)))
  490. (mapc (lambda (cons)
  491. (puthash (car cons) (cdr cons) tab))
  492. (nth 0 sexp))
  493. tab))
  494. (error (message "Invalid comphist db.") nil)))
  495. (defun ac-comphist-init ()
  496. (ac-comphist-load)
  497. (add-hook 'kill-emacs-hook 'ac-comphist-save))
  498. (defun ac-comphist-load ()
  499. (interactive)
  500. (let ((db (if (file-exists-p ac-comphist-file)
  501. (ignore-errors
  502. (with-temp-buffer
  503. (insert-file-contents ac-comphist-file)
  504. (goto-char (point-min))
  505. (ac-comphist-deserialize (read (current-buffer))))))))
  506. (setq ac-comphist (or db (ac-comphist-make)))))
  507. (defun ac-comphist-save ()
  508. (interactive)
  509. (require 'pp)
  510. (ignore-errors
  511. (with-temp-buffer
  512. (pp (ac-comphist-serialize ac-comphist) (current-buffer))
  513. (write-region (point-min) (point-max) ac-comphist-file))))
  514. ;;;; Dictionary
  515. (defvar ac-buffer-dictionary nil)
  516. (defvar ac-file-dictionary (make-hash-table :test 'equal))
  517. (defun ac-clear-dictionary-cache ()
  518. (interactive)
  519. (dolist (buffer (buffer-list))
  520. (with-current-buffer buffer
  521. (if (local-variable-p 'ac-buffer-dictionary)
  522. (kill-local-variable 'ac-buffer-dictionary))))
  523. (clrhash ac-file-dictionary))
  524. (defun ac-file-dictionary (filename)
  525. (let ((cache (gethash filename ac-file-dictionary 'none)))
  526. (if (and cache (not (eq cache 'none)))
  527. cache
  528. (let (result)
  529. (ignore-errors
  530. (with-temp-buffer
  531. (insert-file-contents filename)
  532. (setq result (split-string (buffer-string) "\n" t))))
  533. (puthash filename result ac-file-dictionary)
  534. result))))
  535. (defun ac-mode-dictionary (mode)
  536. (cl-loop for name in (cons (symbol-name mode)
  537. (ignore-errors (list (file-name-extension (buffer-file-name)))))
  538. append (cl-loop for dir in ac-dictionary-directories
  539. for file = (concat dir "/" name)
  540. if (file-exists-p file)
  541. append (ac-file-dictionary file))))
  542. (defun ac-buffer-dictionary (&optional buffer)
  543. (with-current-buffer (or buffer (current-buffer))
  544. (if (local-variable-p 'ac-buffer-dictionary)
  545. ac-buffer-dictionary
  546. (make-local-variable 'ac-buffer-dictionary)
  547. (setq ac-buffer-dictionary
  548. (apply 'append
  549. ac-user-dictionary
  550. (ac-mode-dictionary major-mode)
  551. (mapcar 'ac-file-dictionary ac-dictionary-files))))))
  552. ;;;; Auto completion internals
  553. (defun ac-menu-at-wrapper-line-p ()
  554. "Return non-nil if current line is long and wrapped to next visual line."
  555. (and (not truncate-lines)
  556. (eq (line-beginning-position)
  557. (save-excursion
  558. (vertical-motion 1)
  559. (line-beginning-position)))))
  560. (defun ac-stop-word-p (word)
  561. (or (member word ac-stop-words)
  562. (if ac-use-dictionary-as-stop-words
  563. (member word (ac-buffer-dictionary)))))
  564. (defun ac-prefix-default ()
  565. "Same as `ac-prefix-symbol' but ignore a number prefix."
  566. (let ((start (ac-prefix-symbol)))
  567. (when start
  568. (cl-loop with end = (point)
  569. for pos from start below end
  570. for c = (char-after pos)
  571. if (not (and (<= ?0 c) (<= c ?9)))
  572. return start))))
  573. (defun ac-prefix-symbol ()
  574. "Default prefix definition function."
  575. (require 'thingatpt)
  576. (car-safe (bounds-of-thing-at-point 'symbol)))
  577. (defun ac-prefix-file ()
  578. "File prefix."
  579. (let ((point (re-search-backward "[\"<>' \t\r\n]" nil t)))
  580. (if point (1+ point))))
  581. (defsubst ac-windows-remote-file-p (file)
  582. (and (memq system-type '(ms-dos windows-nt cygwin))
  583. (string-match-p "\\`\\(?://\\|\\\\\\\\\\)" file)))
  584. (defun ac-prefix-valid-file ()
  585. "Existed (or to be existed) file prefix."
  586. (let* ((line-beg (line-beginning-position))
  587. (end (point))
  588. (start (or (let ((point (re-search-backward "[\"<>'= \t\r\n]" line-beg t)))
  589. (if point (1+ point)))
  590. line-beg))
  591. (file (buffer-substring start end)))
  592. (if (and file (or (string-match "^/" file)
  593. (and (setq file (and (string-match "^[^/]*/" file)
  594. (match-string 0 file)))
  595. (file-directory-p file))))
  596. (unless (ac-windows-remote-file-p file)
  597. start))))
  598. (defun ac-prefix-c-dot ()
  599. "C-like languages dot(.) prefix."
  600. (if (re-search-backward "\\.\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
  601. (match-beginning 1)))
  602. (defun ac-prefix-c-dot-ref ()
  603. "C-like languages dot(.) and reference(->) prefix."
  604. (if (re-search-backward "\\(?:\\.\\|->\\)\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
  605. (match-beginning 1)))
  606. (defun ac-prefix-cc-member ()
  607. "C-like languages member(.)(->)(::) prefix."
  608. (when (re-search-backward "\\(?:\\.\\|->\\|::\\)\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
  609. (match-beginning 1)))
  610. (defun ac-define-prefix (name prefix)
  611. "Define new prefix definition.
  612. You can not use it in source definition like (prefix . `NAME')."
  613. (push (cons name prefix) ac-prefix-definitions))
  614. (defun ac-match-substring (prefix candidates)
  615. (cl-loop with regexp = (regexp-quote prefix)
  616. for candidate in candidates
  617. if (string-match regexp candidate)
  618. collect candidate))
  619. (defsubst ac-source-entity (source)
  620. (if (symbolp source)
  621. (symbol-value source)
  622. source))
  623. (defun ac-source-available-p (source)
  624. (if (and (symbolp source)
  625. (get source 'available))
  626. (eq (get source 'available) t)
  627. (let* ((src (ac-source-entity source))
  628. (avail-pair (assq 'available src))
  629. (avail-cond (cdr avail-pair))
  630. (available (and (if avail-pair
  631. (cond
  632. ((symbolp avail-cond)
  633. (funcall avail-cond))
  634. ((listp avail-cond)
  635. (eval avail-cond)))
  636. t)
  637. (cl-loop for feature in (assoc-default 'depends src)
  638. unless (require feature nil t) return nil
  639. finally return t))))
  640. (if (symbolp source)
  641. (put source 'available (if available t 'no)))
  642. available)))
  643. (defun ac-compile-sources (sources)
  644. "Compiled `SOURCES' into expanded sources style."
  645. (cl-loop for source in sources
  646. if (ac-source-available-p source)
  647. do
  648. (setq source (ac-source-entity source))
  649. ;; prefix
  650. (let* ((prefix (assoc 'prefix source))
  651. (real (assoc-default (cdr prefix) ac-prefix-definitions)))
  652. (cond
  653. (real
  654. (add-to-list 'source (cons 'prefix real)))
  655. ((null prefix)
  656. (add-to-list 'source (cons 'prefix 'ac-prefix-default)))))
  657. ;; match
  658. (let ((match (assq 'match source)))
  659. (cond
  660. ((eq (cdr match) 'substring)
  661. (setcdr match 'ac-match-substring))))
  662. and collect source))
  663. (defun ac-compiled-sources ()
  664. (or ac-compiled-sources
  665. (setq ac-compiled-sources
  666. (ac-compile-sources ac-sources))))
  667. (defsubst ac-menu-live-p ()
  668. (popup-live-p ac-menu))
  669. (defun ac-menu-create (point width height)
  670. (setq ac-menu
  671. (popup-create point width height
  672. :around t
  673. :face 'ac-candidate-face
  674. :max-width ac-max-width
  675. :mouse-face 'ac-candidate-mouse-face
  676. :selection-face 'ac-selection-face
  677. :symbol t
  678. :scroll-bar t
  679. :margin-left 1
  680. :keymap ac-menu-map
  681. )))
  682. (defun ac-menu-delete ()
  683. (when ac-menu
  684. (popup-delete ac-menu)
  685. (setq ac-menu nil)))
  686. (defsubst ac-inline-overlay ()
  687. (nth 0 ac-inline))
  688. (defsubst ac-inline-live-p ()
  689. (and ac-inline (ac-inline-overlay) t))
  690. (defun ac-inline-show (point string)
  691. (unless ac-inline
  692. (setq ac-inline (list nil)))
  693. (save-excursion
  694. (let ((overlay (ac-inline-overlay))
  695. (width 0)
  696. (string-width (string-width string))
  697. (length 0)
  698. (original-string string))
  699. ;; Calculate string space to show completion
  700. (goto-char point)
  701. (let (c)
  702. (while (and (not (eolp))
  703. (< width string-width)
  704. (setq c (char-after))
  705. (not (eq c ?\t))) ; special case for tab
  706. (cl-incf width (char-width c))
  707. (cl-incf length)
  708. (forward-char)))
  709. ;; Show completion
  710. (goto-char point)
  711. (cond
  712. ((= width 0)
  713. ;; End-of-line
  714. ;; Do nothing
  715. )
  716. ((<= width string-width)
  717. ;; No space to show
  718. ;; Do nothing
  719. )
  720. ((> width string-width)
  721. ;; Need to fill space
  722. (setq string (concat string (make-string (- width string-width) ? )))))
  723. (setq string (propertize string 'face 'ac-completion-face))
  724. (if overlay
  725. (progn
  726. (move-overlay overlay point (+ point length))
  727. (overlay-put overlay 'invisible nil))
  728. (setq overlay (make-overlay point (+ point length)))
  729. (setf (nth 0 ac-inline) overlay)
  730. (overlay-put overlay 'priority 9999)
  731. ;; Help prefix-overlay in some cases
  732. (overlay-put overlay 'keymap ac-current-map))
  733. ;; TODO no width but char
  734. (if (eq length 0)
  735. ;; Case: End-of-line
  736. (progn
  737. (put-text-property 0 1 'cursor t string)
  738. (overlay-put overlay 'after-string string))
  739. (let ((display (substring string 0 1))
  740. (after-string (substring string 1)))
  741. (overlay-put overlay 'display display)
  742. (overlay-put overlay 'after-string after-string)))
  743. (overlay-put overlay 'string original-string))))
  744. (defun ac-inline-delete ()
  745. (when (ac-inline-live-p)
  746. (ac-inline-hide)
  747. (delete-overlay (ac-inline-overlay))
  748. (setq ac-inline nil)))
  749. (defun ac-inline-hide ()
  750. (when (ac-inline-live-p)
  751. (let ((overlay (ac-inline-overlay))
  752. (buffer-undo-list t))
  753. (when overlay
  754. (move-overlay overlay (point-min) (point-min))
  755. (overlay-put overlay 'invisible t)
  756. (overlay-put overlay 'display nil)
  757. (overlay-put overlay 'after-string nil)))))
  758. (defun ac-inline-update ()
  759. (if (and ac-completing ac-prefix (stringp ac-common-part))
  760. (let ((common-part-length (length ac-common-part))
  761. (prefix-length (length ac-prefix)))
  762. (if (> common-part-length prefix-length)
  763. (progn
  764. (ac-inline-hide)
  765. (ac-inline-show (point) (substring ac-common-part prefix-length)))
  766. (ac-inline-delete)))
  767. (ac-inline-delete)))
  768. (defun ac-put-prefix-overlay ()
  769. (unless ac-prefix-overlay
  770. (let (newline)
  771. ;; Insert newline to make sure that cursor always on the overlay
  772. (when (eobp)
  773. (popup-save-buffer-state
  774. (insert "\n"))
  775. (setq newline t))
  776. (setq ac-prefix-overlay (make-overlay ac-point (1+ (point)) nil t t))
  777. (overlay-put ac-prefix-overlay 'priority 9999)
  778. (overlay-put ac-prefix-overlay 'keymap (make-sparse-keymap))
  779. (overlay-put ac-prefix-overlay 'newline newline))))
  780. (defun ac-remove-prefix-overlay ()
  781. (when ac-prefix-overlay
  782. (when (overlay-get ac-prefix-overlay 'newline)
  783. ;; Remove inserted newline
  784. (popup-save-buffer-state
  785. (goto-char (point-max))
  786. (if (eq (char-before) ?\n)
  787. (delete-char -1))))
  788. (delete-overlay ac-prefix-overlay)))
  789. (defun ac-activate-completing-map ()
  790. (if (and ac-show-menu ac-use-menu-map)
  791. (set-keymap-parent ac-current-map ac-menu-map))
  792. (when (and ac-use-overriding-local-map
  793. (null overriding-terminal-local-map))
  794. (setq overriding-terminal-local-map ac-current-map))
  795. (when ac-prefix-overlay
  796. (set-keymap-parent (overlay-get ac-prefix-overlay 'keymap) ac-current-map)))
  797. (defun ac-deactivate-completing-map ()
  798. (set-keymap-parent ac-current-map ac-completing-map)
  799. (when (and ac-use-overriding-local-map
  800. (eq overriding-terminal-local-map ac-current-map))
  801. (setq overriding-terminal-local-map nil))
  802. (when ac-prefix-overlay
  803. (set-keymap-parent (overlay-get ac-prefix-overlay 'keymap) nil)))
  804. (defsubst ac-selected-candidate ()
  805. (if ac-menu
  806. (popup-selected-item ac-menu)))
  807. (defun ac-prefix (requires ignore-list)
  808. (cl-loop with current = (point)
  809. with point
  810. with point-def
  811. with prefix-def
  812. with sources
  813. for source in (ac-compiled-sources)
  814. for prefix = (assoc-default 'prefix source)
  815. for req = (or (assoc-default 'requires source) requires 1)
  816. do
  817. (unless (member prefix ignore-list)
  818. (save-excursion
  819. (setq point (cond
  820. ((symbolp prefix)
  821. (funcall prefix))
  822. ((stringp prefix)
  823. (and (re-search-backward (concat prefix "\\=") nil t)
  824. (or (match-beginning 1) (match-beginning 0))))
  825. ((stringp (car-safe prefix))
  826. (let ((regexp (nth 0 prefix))
  827. (end (nth 1 prefix))
  828. (group (nth 2 prefix)))
  829. (and (re-search-backward (concat regexp "\\=") nil t)
  830. (funcall (if end 'match-end 'match-beginning)
  831. (or group 0)))))
  832. (t
  833. (eval prefix))))
  834. (if (and point
  835. (integerp req)
  836. (< (- current point) req))
  837. (setq point nil))
  838. (when point
  839. (if (null prefix-def)
  840. (setq prefix-def prefix
  841. point-def point))
  842. (if (equal point point-def)
  843. (push source sources)))))
  844. finally return
  845. (and point-def (list prefix-def point-def (nreverse sources)))))
  846. (defun ac-init ()
  847. "Initialize current sources to start completion."
  848. (setq ac-candidates-cache nil)
  849. (cl-loop for source in ac-current-sources
  850. for function = (assoc-default 'init source)
  851. if function do
  852. (save-excursion
  853. (cond
  854. ((functionp function)
  855. (funcall function))
  856. (t
  857. (eval function))))))
  858. (defun ac-candidates-1 (source)
  859. (let* ((do-cache (assq 'cache source))
  860. (function (assoc-default 'candidates source))
  861. (action (assoc-default 'action source))
  862. (document (assoc-default 'document source))
  863. (symbol (assoc-default 'symbol source))
  864. (ac-limit (or (assoc-default 'limit source) ac-limit))
  865. (face (or (assoc-default 'face source) (assoc-default 'candidate-face source)))
  866. (selection-face (assoc-default 'selection-face source))
  867. (cache (and do-cache (assq source ac-candidates-cache)))
  868. (candidates (cdr cache)))
  869. (unless cache
  870. (setq candidates (save-excursion
  871. (cond
  872. ((functionp function)
  873. (funcall function))
  874. (t
  875. (eval function)))))
  876. ;; Convert (name value) format candidates into name with text properties.
  877. (setq candidates (mapcar (lambda (candidate)
  878. (if (consp candidate)
  879. (propertize (car candidate) 'value (cdr candidate))
  880. candidate))
  881. candidates))
  882. (when do-cache
  883. (push (cons source candidates) ac-candidates-cache)))
  884. (setq candidates (funcall (or (assoc-default 'match source)
  885. ac-match-function)
  886. ac-prefix candidates))
  887. ;; Remove extra items regarding to ac-limit
  888. (if (and (integerp ac-limit) (> ac-limit 1) (> (length candidates) ac-limit))
  889. (setcdr (nthcdr (1- ac-limit) candidates) nil))
  890. ;; Put candidate properties
  891. (setq candidates (mapcar (lambda (candidate)
  892. (popup-item-propertize candidate
  893. 'action action
  894. 'symbol symbol
  895. 'document document
  896. 'popup-face face
  897. 'selection-face selection-face))
  898. candidates))
  899. candidates))
  900. (defun ac-delete-duplicated-candidates (candidates)
  901. (cl-delete-duplicates
  902. candidates
  903. :test (lambda (x y)
  904. ;; We assume two candidates are same if their titles are
  905. ;; equal and their actions are equal.
  906. (and (equal x y)
  907. (eq (popup-item-property x 'action)
  908. (popup-item-property y 'action))))
  909. :from-end t))
  910. (defun ac-reduce-candidates (candidates)
  911. ;; Call `ac-delete-duplicated-candidates' on first portion of
  912. ;; candidate list for speed.
  913. (let ((size 20))
  914. (if (< (length candidates) size)
  915. (ac-delete-duplicated-candidates candidates)
  916. (cl-loop for c on candidates by 'cdr
  917. repeat (1- size)
  918. finally return
  919. (let ((rest (cdr c)))
  920. (setcdr c nil)
  921. (append (ac-delete-duplicated-candidates candidates) (copy-sequence rest)))))))
  922. (defun ac-candidates ()
  923. "Produce candidates for current sources."
  924. (cl-loop with completion-ignore-case = (or (eq ac-ignore-case t)
  925. (and (eq ac-ignore-case 'smart)
  926. (let ((case-fold-search nil)) (not (string-match "[[:upper:]]" ac-prefix)))))
  927. with case-fold-search = completion-ignore-case
  928. with prefix-len = (length ac-prefix)
  929. for source in ac-current-sources
  930. append (ac-candidates-1 source) into candidates
  931. finally return
  932. (progn
  933. (if (and ac-use-comphist ac-comphist)
  934. (if ac-show-menu
  935. (let* ((pair (ac-comphist-sort ac-comphist candidates prefix-len ac-comphist-threshold))
  936. (n (car pair))
  937. (result (ac-reduce-candidates (cdr pair)))
  938. (cons (if (> n 0) (nthcdr (1- n) result)))
  939. (cdr (cdr cons)))
  940. ;; XXX ugly
  941. (if cons (setcdr cons nil))
  942. (setq ac-common-part (try-completion ac-prefix result))
  943. (setq ac-whole-common-part (try-completion ac-prefix candidates))
  944. (if cons (setcdr cons cdr))
  945. result)
  946. (setq candidates (ac-comphist-sort ac-comphist candidates prefix-len))
  947. (setq ac-common-part (if candidates (popup-x-to-string (car candidates))))
  948. (setq ac-whole-common-part (try-completion ac-prefix candidates))
  949. candidates)
  950. (when ac-show-menu
  951. (setq candidates (ac-reduce-candidates candidates)))
  952. (setq ac-common-part (try-completion ac-prefix candidates))
  953. (setq ac-whole-common-part ac-common-part)
  954. candidates))))
  955. (defun ac-update-candidates (cursor scroll-top)
  956. "Update candidates of menu to `ac-candidates' and redraw it."
  957. (setf (popup-cursor ac-menu) cursor
  958. (popup-scroll-top ac-menu) scroll-top)
  959. (setq ac-dwim-enable (= (length ac-candidates) 1))
  960. (if ac-candidates
  961. (progn
  962. (setq ac-completing t)
  963. (ac-activate-completing-map))
  964. (setq ac-completing nil)
  965. (ac-deactivate-completing-map))
  966. (unless ac-disable-inline
  967. (ac-inline-update))
  968. (popup-set-list ac-menu ac-candidates)
  969. (if (and (not ac-fuzzy-enable)
  970. (<= (length ac-candidates) ac-candidate-menu-min))
  971. (popup-hide ac-menu)
  972. (if ac-show-menu
  973. (popup-draw ac-menu))))
  974. (defun ac-reposition ()
  975. "Force to redraw candidate menu with current `ac-candidates'."
  976. (let ((cursor (popup-cursor ac-menu))
  977. (scroll-top (popup-scroll-top ac-menu))
  978. (height (popup-height ac-menu)))
  979. (ac-menu-delete)
  980. (ac-menu-create ac-point (popup-preferred-width ac-candidates) height)
  981. (ac-update-candidates cursor scroll-top)))
  982. (defun ac-cleanup ()
  983. "Cleanup auto completion."
  984. (if ac-cursor-color
  985. (set-cursor-color ac-cursor-color))
  986. (when (and ac-use-comphist ac-comphist)
  987. (when (and (null ac-selected-candidate)
  988. (member ac-prefix ac-candidates))
  989. ;; Assume candidate is selected by just typing
  990. (setq ac-selected-candidate ac-prefix)
  991. (setq ac-last-point ac-point))
  992. (when ac-selected-candidate
  993. (ac-comphist-add ac-comphist
  994. ac-selected-candidate
  995. (if ac-last-point
  996. (- ac-last-point ac-point)
  997. (length ac-prefix)))))
  998. (ac-deactivate-completing-map)
  999. (ac-remove-prefix-overlay)
  1000. (ac-remove-quick-help)
  1001. (ac-inline-delete)
  1002. (ac-menu-delete)
  1003. (ac-cancel-timer)
  1004. (ac-cancel-show-menu-timer)
  1005. (ac-cancel-quick-help-timer)
  1006. (setq ac-cursor-color nil
  1007. ac-inline nil
  1008. ac-show-menu nil
  1009. ac-menu nil
  1010. ac-completing nil
  1011. ac-point nil
  1012. ac-last-point nil
  1013. ac-prefix nil
  1014. ac-prefix-overlay nil
  1015. ac-selected-candidate nil
  1016. ac-common-part nil
  1017. ac-whole-common-part nil
  1018. ac-triggered nil
  1019. ac-limit nil
  1020. ac-candidates nil
  1021. ac-candidates-cache nil
  1022. ac-fuzzy-enable nil
  1023. ac-dwim-enable nil
  1024. ac-compiled-sources nil
  1025. ac-current-sources nil
  1026. ac-current-prefix-def nil
  1027. ac-ignoring-prefix-def nil))
  1028. (defsubst ac-abort ()
  1029. "Abort completion."
  1030. (ac-cleanup))
  1031. (defun ac-extend-region-to-delete (string)
  1032. "Determine the boundary of the region to delete before
  1033. inserting the completed string. This will be either the position
  1034. of current point, or the end of the symbol at point, if the text
  1035. from point to end of symbol is the right part of the completed
  1036. string."
  1037. (let* ((end-of-symbol (or (cdr-safe (bounds-of-thing-at-point 'symbol))
  1038. (point)))
  1039. (remaindar (buffer-substring-no-properties (point) end-of-symbol))
  1040. (remaindar-length (length remaindar)))
  1041. (if (and (>= (length string) remaindar-length)
  1042. (string= (substring-no-properties string (- remaindar-length))
  1043. remaindar))
  1044. end-of-symbol
  1045. (point))))
  1046. (defun ac-expand-string (string &optional remove-undo-boundary)
  1047. "Expand `STRING' into the buffer and update `ac-prefix' to `STRING'.
  1048. This function records deletion and insertion sequences by `undo-boundary'.
  1049. If `remove-undo-boundary' is non-nil, this function also removes `undo-boundary'
  1050. that have been made before in this function. When `buffer-undo-list' is
  1051. `t', `remove-undo-boundary' has no effect."
  1052. (when (eq buffer-undo-list t)
  1053. (setq remove-undo-boundary nil))
  1054. (when (not (equal string (buffer-substring ac-point (point))))
  1055. (undo-boundary)
  1056. ;; We can't use primitive-undo since it undoes by
  1057. ;; groups, divided by boundaries.
  1058. ;; We don't want boundary between deletion and insertion.
  1059. ;; So do it manually.
  1060. ;; Delete region silently for undo:
  1061. (if remove-undo-boundary
  1062. (progn
  1063. (let (buffer-undo-list)
  1064. (save-excursion
  1065. (delete-region ac-point (ac-extend-region-to-delete string))))
  1066. (setq buffer-undo-list
  1067. (nthcdr 2 buffer-undo-list)))
  1068. (delete-region ac-point (ac-extend-region-to-delete string)))
  1069. (insert (substring-no-properties string))
  1070. ;; Sometimes, possible when omni-completion used, (insert) added
  1071. ;; to buffer-undo-list strange record about position changes.
  1072. ;; Delete it here:
  1073. (when (and remove-undo-boundary
  1074. (integerp (cadr buffer-undo-list)))
  1075. (setcdr buffer-undo-list (nthcdr 2 buffer-undo-list)))
  1076. (undo-boundary)
  1077. (setq ac-selected-candidate string)
  1078. (setq ac-prefix string)))
  1079. (defun ac-set-trigger-key (key)
  1080. "Set `ac-trigger-key' to `KEY'. It is recommemded to use this function instead of calling `setq'."
  1081. ;; Remove old mapping
  1082. (when ac-trigger-key
  1083. (define-key ac-mode-map (read-kbd-macro ac-trigger-key) nil))
  1084. ;; Make new mapping
  1085. (setq ac-trigger-key key)
  1086. (when key
  1087. (define-key ac-mode-map (read-kbd-macro key) 'ac-trigger-key-command)))
  1088. (defun ac-set-timer ()
  1089. (unless ac-timer
  1090. (setq ac-timer (run-with-idle-timer ac-delay ac-delay 'ac-update-greedy))))
  1091. (defun ac-cancel-timer ()
  1092. (when (timerp ac-timer)
  1093. (cancel-timer ac-timer)
  1094. (setq ac-timer nil)))
  1095. (defun ac-update (&optional force)
  1096. (when (and auto-complete-mode
  1097. ac-prefix
  1098. (or ac-triggered
  1099. force)
  1100. (not isearch-mode))
  1101. (ac-put-prefix-overlay)
  1102. (setq ac-candidates (ac-candidates))
  1103. (let ((preferred-width (popup-preferred-width ac-candidates)))
  1104. ;; Reposition if needed
  1105. (when (or (null ac-menu)
  1106. (>= (popup-width ac-menu) preferred-width)
  1107. (<= (popup-width ac-menu) (- preferred-width 10))
  1108. (and (> (popup-direction ac-menu) 0)
  1109. (ac-menu-at-wrapper-line-p)))
  1110. (ac-inline-hide) ; Hide overlay to calculate correct column
  1111. (ac-remove-quick-help)
  1112. (ac-menu-delete)
  1113. (ac-menu-create ac-point preferred-width ac-menu-height)))
  1114. (ac-update-candidates 0 0)
  1115. t))
  1116. (defun ac-update-greedy (&optional force)
  1117. (let (result)
  1118. (while (when (and (setq result (ac-update force))
  1119. (null ac-candidates))
  1120. (add-to-list 'ac-ignoring-prefix-def ac-current-prefix-def)
  1121. (ac-start :force-init t)
  1122. ac-current-prefix-def))
  1123. result))
  1124. (defun ac-set-show-menu-timer ()
  1125. (when (and (or (integerp ac-auto-show-menu) (floatp ac-auto-show-menu))
  1126. (null ac-show-menu-timer))
  1127. (setq ac-show-menu-timer (run-with-idle-timer ac-auto-show-menu ac-auto-show-menu 'ac-show-menu))))
  1128. (defun ac-cancel-show-menu-timer ()
  1129. (when (timerp ac-show-menu-timer)
  1130. (cancel-timer ac-show-menu-timer)
  1131. (setq ac-show-menu-timer nil)))
  1132. (defun ac-show-menu ()
  1133. (when (not (eq ac-show-menu t))
  1134. (setq ac-show-menu t)
  1135. (ac-inline-hide)
  1136. (ac-remove-quick-help)
  1137. (ac-update t)))
  1138. (defun ac-help (&optional persist)
  1139. (interactive "P")
  1140. (when ac-menu
  1141. (popup-menu-show-help ac-menu persist)))
  1142. (defun ac-persist-help ()
  1143. (interactive)
  1144. (ac-help t))
  1145. (defun ac-last-help (&optional persist)
  1146. (interactive "P")
  1147. (when ac-last-completion
  1148. (popup-item-show-help (cdr ac-last-completion) persist)))
  1149. (defun ac-last-persist-help ()
  1150. (interactive)
  1151. (ac-last-help t))
  1152. (defun ac-set-quick-help-timer ()
  1153. (when (and ac-use-quick-help
  1154. (null ac-quick-help-timer))
  1155. (setq ac-quick-help-timer (run-with-idle-timer ac-quick-help-delay ac-quick-help-delay 'ac-quick-help))))
  1156. (defun ac-cancel-quick-help-timer ()
  1157. (when (timerp ac-quick-help-timer)
  1158. (cancel-timer ac-quick-help-timer)
  1159. (setq ac-quick-help-timer nil)))
  1160. (defun ac-pos-tip-show-quick-help (menu &optional item &rest args)
  1161. (let* ((point (plist-get args :point))
  1162. (around nil)
  1163. (parent-offset (popup-offset menu))
  1164. (doc (popup-menu-documentation menu item)))
  1165. (when (stringp doc)
  1166. (if (popup-hidden-p menu)
  1167. (setq around t)
  1168. (setq point nil))
  1169. (with-no-warnings
  1170. (pos-tip-show doc
  1171. 'popup-tip-face
  1172. (or point
  1173. (and menu
  1174. (popup-child-point menu parent-offset))
  1175. (point))
  1176. nil 300
  1177. popup-tip-max-width
  1178. nil nil
  1179. (and (not around) 0))
  1180. (unless (plist-get args :nowait)
  1181. (clear-this-command-keys)
  1182. (unwind-protect
  1183. (push (read-event (plist-get args :prompt)) unread-command-events)
  1184. (pos-tip-hide))
  1185. t)))))
  1186. (defun ac-quick-help-use-pos-tip-p ()
  1187. (and ac-quick-help-prefer-pos-tip
  1188. window-system
  1189. (featurep 'pos-tip)))
  1190. (defun ac-quick-help (&optional force)
  1191. (interactive)
  1192. ;; TODO don't use FORCE
  1193. (when (and (or force
  1194. (with-no-warnings
  1195. ;; called-interactively-p can take no args
  1196. (called-interactively-p))
  1197. ;; ac-isearch'ing
  1198. (null this-command))
  1199. (ac-menu-live-p)
  1200. (null ac-quick-help))
  1201. (setq ac-quick-help
  1202. (funcall (if (ac-quick-help-use-pos-tip-p)
  1203. 'ac-pos-tip-show-quick-help
  1204. 'popup-menu-show-quick-help)
  1205. ac-menu nil
  1206. :point ac-point
  1207. :height ac-quick-help-height
  1208. :nowait t))))
  1209. (defun ac-remove-quick-help ()
  1210. (when (ac-quick-help-use-pos-tip-p)
  1211. (with-no-warnings
  1212. (pos-tip-hide)))
  1213. (when ac-quick-help
  1214. (popup-delete ac-quick-help)
  1215. (setq ac-quick-help nil)))
  1216. (defun ac-last-quick-help ()
  1217. (interactive)
  1218. (when (and ac-last-completion
  1219. (eq (marker-buffer (car ac-last-completion))
  1220. (current-buffer)))
  1221. (let ((doc (popup-item-documentation (cdr ac-last-completion)))
  1222. (point (marker-position (car ac-last-completion))))
  1223. (when (stringp doc)
  1224. (if (ac-quick-help-use-pos-tip-p)
  1225. (with-no-warnings (pos-tip-show doc nil point nil 300))
  1226. (popup-tip doc
  1227. :point point
  1228. :around t
  1229. :scroll-bar t
  1230. :margin t))))))
  1231. (defmacro ac-define-quick-help-command (name arglist &rest body)
  1232. (declare (indent 2))
  1233. `(progn
  1234. (defun ,name ,arglist ,@body)
  1235. (put ',name 'ac-quick-help-command t)))
  1236. (ac-define-quick-help-command ac-quick-help-scroll-down ()
  1237. (interactive)
  1238. (when ac-quick-help
  1239. (popup-scroll-down ac-quick-help)))
  1240. (ac-define-quick-help-command ac-quick-help-scroll-up ()
  1241. (interactive)
  1242. (when ac-quick-help
  1243. (popup-scroll-up ac-quick-help)))
  1244. ;;;; Auto completion isearch
  1245. (defun ac-isearch-callback (list)
  1246. (setq ac-dwim-enable (eq (length list) 1)))
  1247. (defun ac-isearch ()
  1248. (interactive)
  1249. (when (ac-menu-live-p)
  1250. (ac-cancel-show-menu-timer)
  1251. (ac-show-menu)
  1252. (if ac-use-quick-help
  1253. (let ((popup-menu-show-quick-help-function
  1254. (if (ac-quick-help-use-pos-tip-p)
  1255. 'ac-pos-tip-show-quick-help
  1256. 'popup-menu-show-quick-help)))
  1257. (popup-isearch ac-menu
  1258. :callback 'ac-isearch-callback
  1259. :help-delay ac-quick-help-delay))
  1260. (popup-isearch ac-menu :callback 'ac-isearch-callback))))
  1261. ;;;; Auto completion commands
  1262. (cl-defun auto-complete-1 (&key sources (triggered 'command))
  1263. (let ((menu-live (ac-menu-live-p))
  1264. (inline-live (ac-inline-live-p))
  1265. started)
  1266. (ac-abort)
  1267. (let ((ac-sources (or sources ac-sources)))
  1268. (if (or ac-show-menu-immediately-on-auto-complete
  1269. inline-live)
  1270. (setq ac-show-menu t))
  1271. (setq started (ac-start :triggered triggered)))
  1272. (when (ac-update-greedy t)
  1273. ;; TODO Not to cause inline completion to be disrupted.
  1274. (if (ac-inline-live-p)
  1275. (ac-inline-hide))
  1276. ;; Not to expand when it is first time to complete
  1277. (when (and (or (and (not ac-expand-on-auto-complete)
  1278. (> (length ac-candidates) 1)
  1279. (not menu-live))
  1280. (not (let ((ac-common-part ac-whole-common-part))
  1281. (ac-expand-common))))
  1282. ac-use-fuzzy
  1283. (null ac-candidates))
  1284. (ac-fuzzy-complete)))
  1285. started))
  1286. ;;;###autoload
  1287. (defun auto-complete (&optional sources)
  1288. "Start auto-completion at current point."
  1289. (interactive)
  1290. (auto-complete-1 :sources sources))
  1291. (defun ac-fuzzy-complete ()
  1292. "Start fuzzy completion at current point."
  1293. (interactive)
  1294. (if (not (require 'fuzzy nil t))
  1295. (message "Please install fuzzy.el if you use fuzzy completion")
  1296. (unless (ac-menu-live-p)
  1297. (ac-start))
  1298. (let ((ac-match-function 'fuzzy-all-completions))
  1299. (when ac-fuzzy-cursor-color
  1300. (unless ac-cursor-color
  1301. (setq ac-cursor-color (frame-parameter (selected-frame) 'cursor-color)))
  1302. (set-cursor-color ac-fuzzy-cursor-color))
  1303. (setq ac-show-menu t)
  1304. (setq ac-fuzzy-enable t)
  1305. (setq ac-triggered nil)
  1306. (ac-update t)))
  1307. t)
  1308. (defun ac-next ()
  1309. "Select next candidate."
  1310. (interactive)
  1311. (when (ac-menu-live-p)
  1312. (when (popup-hidden-p ac-menu)
  1313. (ac-show-menu))
  1314. (popup-next ac-menu)
  1315. (if (eq this-command 'ac-next)
  1316. (setq ac-dwim-enable t))))
  1317. (defun ac-previous ()
  1318. "Select previous candidate."
  1319. (interactive)
  1320. (when (ac-menu-live-p)
  1321. (when (popup-hidden-p ac-menu)
  1322. (ac-show-menu))
  1323. (popup-previous ac-menu)
  1324. (if (eq this-command 'ac-previous)
  1325. (setq ac-dwim-enable t))))
  1326. (defun ac-expand (arg)
  1327. "Try expand, and if expanded twice, select next candidate.
  1328. If given a prefix argument, select the previous candidate."
  1329. (interactive "P")
  1330. (unless (ac-expand-common)
  1331. (let ((string (ac-selected-candidate)))
  1332. (when string
  1333. (when (equal ac-prefix string)
  1334. (if (not arg)
  1335. (ac-next)
  1336. (ac-previous))
  1337. (setq string (ac-selected-candidate)))
  1338. (ac-expand-string string
  1339. (or (eq last-command 'ac-expand)
  1340. (eq last-command 'ac-expand-previous)))
  1341. ;; Do reposition if menu at long line
  1342. (if (and (> (popup-direction ac-menu) 0)
  1343. (ac-menu-at-wrapper-line-p))
  1344. (ac-reposition))
  1345. (setq ac-show-menu t)
  1346. string))))
  1347. (defun ac-expand-previous (arg)
  1348. "Like `ac-expand', but select previous candidate."
  1349. (interactive "P")
  1350. (ac-expand (not arg)))
  1351. (defun ac-expand-common ()
  1352. "Try to expand meaningful common part."
  1353. (interactive)
  1354. (if (and ac-dwim ac-dwim-enable)
  1355. (ac-complete)
  1356. (when (and (ac-inline-live-p)
  1357. ac-common-part)
  1358. (ac-inline-hide)
  1359. (ac-expand-string ac-common-part (eq last-command this-command))
  1360. (setq ac-common-part nil)
  1361. t)))
  1362. (defun ac-complete-1 (candidate)
  1363. (let ((action (popup-item-property candidate 'action))
  1364. (fallback nil))
  1365. (when candidate
  1366. (unless (ac-expand-string candidate)
  1367. (setq fallback t))
  1368. ;; Remember to show help later
  1369. (when (and ac-point candidate)
  1370. (unless ac-last-completion
  1371. (setq ac-last-completion (cons (make-marker) nil)))
  1372. (set-marker (car ac-last-completion) ac-point ac-buffer)
  1373. (setcdr ac-last-completion candidate)))
  1374. (ac-abort)
  1375. (cond
  1376. (action
  1377. (funcall action))
  1378. (fallback
  1379. (ac-fallback-command)))
  1380. candidate))
  1381. (defun ac-complete ()
  1382. "Try complete."
  1383. (interactive)
  1384. (ac-complete-1 (ac-selected-candidate)))
  1385. (cl-defun ac-start (&key
  1386. requires
  1387. force-init
  1388. (triggered (or ac-triggered t)))
  1389. "Start completion."
  1390. (interactive)
  1391. (if (not auto-complete-mode)
  1392. (message "auto-complete-mode is not enabled")
  1393. (let* ((info (ac-prefix requires ac-ignoring-prefix-def))
  1394. (prefix-def (nth 0 info))
  1395. (point (nth 1 info))
  1396. (sources (nth 2 info))
  1397. prefix
  1398. (init (or force-init (not (eq ac-point point)))))
  1399. (if (or (null point)
  1400. (progn
  1401. (setq prefix (buffer-substring-no-properties point (point)))
  1402. (and (not (eq triggered 'command))
  1403. (ac-stop-word-p prefix))))
  1404. (prog1 nil
  1405. (ac-abort))
  1406. (when (and ac-use-fuzzy ac-fuzzy-cursor-color)
  1407. (unless ac-cursor-color
  1408. (setq ac-cursor-color (frame-parameter (selected-frame) 'cursor-color))))
  1409. (setq ac-show-menu (or ac-show-menu (if (eq ac-auto-show-menu t) t))
  1410. ac-current-sources sources
  1411. ac-buffer (current-buffer)
  1412. ac-point point
  1413. ac-prefix prefix
  1414. ac-limit ac-candidate-limit
  1415. ac-triggered triggered
  1416. ac-current-prefix-def prefix-def)
  1417. (when (or init (null ac-prefix-overlay))
  1418. (ac-init))
  1419. (ac-set-timer)
  1420. (ac-set-show-menu-timer)
  1421. (ac-set-quick-help-timer)
  1422. (ac-put-prefix-overlay)
  1423. t))))
  1424. (defun ac-stop ()
  1425. "Stop completing."
  1426. (interactive)
  1427. (setq ac-selected-candidate nil)
  1428. (ac-abort))
  1429. (defun ac-ignore (&rest ignore)
  1430. "Same as `ignore'."
  1431. (interactive))
  1432. (defun ac-mouse-1 (event)
  1433. (interactive "e")
  1434. (popup-awhen (popup-menu-item-of-mouse-event event)
  1435. (ac-complete-1 it)))
  1436. (defun ac-mouse-4 (event)
  1437. (interactive "e")
  1438. (ac-previous))
  1439. (defun ac-mouse-5 (event)
  1440. (interactive "e")
  1441. (ac-next))
  1442. (defun ac-trigger-key-command (&optional force)
  1443. (interactive "P")
  1444. (let (started)
  1445. (when (or force (ac-trigger-command-p last-command))
  1446. (setq started (auto-complete-1 :triggered 'trigger-key)))
  1447. (unless started
  1448. (ac-fallback-command 'ac-trigger-key-command))))
  1449. ;;;; Basic cache facility
  1450. (defvar ac-clear-variables-every-minute-timer nil)
  1451. (defvar ac-clear-variables-after-save nil)
  1452. (defvar ac-clear-variables-every-minute nil)
  1453. (defvar ac-minutes-counter 0)
  1454. (defun ac-clear-variable-after-save (variable &optional pred)
  1455. (add-to-list 'ac-clear-variables-after-save (cons variable pred)))
  1456. (defun ac-clear-variables-after-save ()
  1457. (dolist (pair ac-clear-variables-after-save)
  1458. (if (or (null (cdr pair))
  1459. (funcall (cdr pair)))
  1460. (set (car pair) nil))))
  1461. (defun ac-clear-variable-every-minutes (variable minutes)
  1462. (add-to-list 'ac-clear-variables-every-minute (cons variable minutes)))
  1463. (defun ac-clear-variable-every-minute (variable)
  1464. (ac-clear-variable-every-minutes variable 1))
  1465. (defun ac-clear-variable-every-10-minutes (variable)
  1466. (ac-clear-variable-every-minutes variable 10))
  1467. (defun ac-clear-variables-every-minute ()
  1468. (cl-incf ac-minutes-counter)
  1469. (dolist (pair ac-clear-variables-every-minute)
  1470. (if (eq (% ac-minutes-counter (cdr pair)) 0)
  1471. (set (car pair) nil))))
  1472. ;;;; Auto complete mode
  1473. (defun ac-cursor-on-diable-face-p (&optional point)
  1474. (memq (get-text-property (or point (point)) 'face) ac-disable-faces))
  1475. (defun ac-trigger-command-p (command)
  1476. "Return non-nil if `COMMAND' is a trigger command."
  1477. (and (symbolp command)
  1478. (not (memq command ac-non-trigger-commands))
  1479. (or (memq command ac-trigger-commands)
  1480. (string-match "self-insert-command" (symbol-name command))
  1481. (string-match "electric" (symbol-name command)))))
  1482. (defun ac-fallback-key-sequence ()
  1483. (setq unread-command-events
  1484. (append (this-single-command-raw-keys)
  1485. unread-command-events))
  1486. (read-key-sequence-vector ""))
  1487. (defun ac-fallback-command (&optional except-command)
  1488. (let* ((auto-complete-mode nil)
  1489. (keys (ac-fallback-key-sequence))
  1490. (command (and keys (key-binding keys))))
  1491. (when (and (commandp command)
  1492. (not (eq command except-command)))
  1493. (setq this-command command)
  1494. (call-interactively command))))
  1495. (defun ac-compatible-package-command-p (command)
  1496. "Return non-nil if `COMMAND' is compatible with auto-complete."
  1497. (and (symbolp command)
  1498. (string-match ac-compatible-packages-regexp (symbol-name command))))
  1499. (defun ac-handle-pre-command ()
  1500. (condition-case var
  1501. (if (or (setq ac-triggered (and (not ac-fuzzy-enable) ; ignore key storkes in fuzzy mode
  1502. (or (eq this-command 'auto-complete) ; special case
  1503. (ac-trigger-command-p this-command)
  1504. (and ac-completing
  1505. (memq this-command ac-trigger-commands-on-completing)))
  1506. (not (ac-cursor-on-diable-face-p))
  1507. (or ac-triggered t)))
  1508. (ac-compatible-package-command-p this-command))
  1509. (progn
  1510. (if (or (not (symbolp this-command))
  1511. (not (get this-command 'ac-quick-help-command)))
  1512. (ac-remove-quick-help))
  1513. ;; Not to cause inline completion to be disrupted.
  1514. (ac-inline-hide))
  1515. (ac-abort))
  1516. (error (ac-error var))))
  1517. (defun ac-handle-post-command ()
  1518. (condition-case var
  1519. (when (and ac-triggered
  1520. (or ac-auto-start
  1521. ac-completing)
  1522. (not isearch-mode))
  1523. (setq ac-last-point (point))
  1524. (ac-start :requires (unless ac-completing ac-auto-start))
  1525. (unless ac-disable-inline
  1526. (ac-inline-update)))
  1527. (error (ac-error var))))
  1528. (defvar ac-flycheck-poll-completion-end-timer nil
  1529. "Timer to poll end of completion.")
  1530. (defun ac-syntax-checker-workaround ()
  1531. (if ac-stop-flymake-on-completing
  1532. (progn
  1533. (make-local-variable 'ac-flycheck-poll-completion-end-timer)
  1534. (when (require 'flymake nil t)
  1535. (defadvice flymake-on-timer-event (around ac-flymake-stop-advice activate)
  1536. (unless ac-completing
  1537. ad-do-it)))
  1538. (when (require 'flycheck nil t)
  1539. (defadvice flycheck-handle-idle-change (around ac-flycheck-stop-advice activate)
  1540. (if ac-completing
  1541. (setq ac-flycheck-poll-completion-end-timer
  1542. (run-at-time ac-flycheck-poll-completion-end-interval
  1543. nil
  1544. #'flycheck-handle-idle-change))
  1545. ad-do-it))))
  1546. (when (featurep 'flymake)
  1547. (ad-disable-advice 'flymake-on-timer-event 'around 'ac-flymake-stop-advice))
  1548. (when (featurep 'flycheck)
  1549. (ad-disable-advice 'flycheck-handle-idle-change 'around 'ac-flycheck-stop-advice))))
  1550. (defun ac-setup ()
  1551. (if ac-trigger-key
  1552. (ac-set-trigger-key ac-trigger-key))
  1553. (if ac-use-comphist
  1554. (ac-comphist-init))
  1555. (unless ac-clear-variables-every-minute-timer
  1556. (setq ac-clear-variables-every-minute-timer (run-with-timer 60 60 'ac-clear-variables-every-minute)))
  1557. (ac-syntax-checker-workaround))
  1558. ;;;###autoload
  1559. (define-minor-mode auto-complete-mode
  1560. "AutoComplete mode"
  1561. :lighter " AC"
  1562. :keymap ac-mode-map
  1563. :group 'auto-complete
  1564. (if auto-complete-mode
  1565. (progn
  1566. (ac-setup)
  1567. (add-hook 'pre-command-hook 'ac-handle-pre-command nil t)
  1568. (add-hook 'post-command-hook 'ac-handle-post-command nil t)
  1569. (add-hook 'after-save-hook 'ac-clear-variables-after-save nil t)
  1570. (run-hooks 'auto-complete-mode-hook))
  1571. (remove-hook 'pre-command-hook 'ac-handle-pre-command t)
  1572. (remove-hook 'post-command-hook 'ac-handle-post-command t)
  1573. (remove-hook 'after-save-hook 'ac-clear-variables-after-save t)
  1574. (ac-abort)))
  1575. (defun auto-complete-mode-maybe ()
  1576. "What buffer `auto-complete-mode' prefers."
  1577. (if (and (not (minibufferp (current-buffer)))
  1578. (memq major-mode ac-modes))
  1579. (auto-complete-mode 1)))
  1580. ;;;###autoload
  1581. (define-global-minor-mode global-auto-complete-mode
  1582. auto-complete-mode auto-complete-mode-maybe
  1583. :group 'auto-complete)
  1584. ;;;; Compatibilities with other extensions
  1585. (defun ac-flyspell-workaround ()
  1586. "Flyspell uses `sit-for' for delaying its process. Unfortunatelly,
  1587. it stops auto completion which is trigger with `run-with-idle-timer'.
  1588. This workaround avoid flyspell processes when auto completion is being started."
  1589. (interactive)
  1590. (defadvice flyspell-post-command-hook (around ac-flyspell-workaround activate)
  1591. (unless ac-triggered
  1592. ad-do-it)))
  1593. (defun ac-linum-workaround ()
  1594. "linum-mode tries to display the line numbers even for the
  1595. completion menu. This workaround stops that annoying behavior."
  1596. (interactive)
  1597. (defadvice linum-update (around ac-linum-update-workaround activate)
  1598. (unless ac-completing
  1599. ad-do-it)))
  1600. ;;;; Standard sources
  1601. (defmacro ac-define-source (name source)
  1602. "Source definition macro. It defines a complete command also."
  1603. (declare (indent 1))
  1604. `(progn
  1605. (defvar ,(intern (format "ac-source-%s" name)))
  1606. ;; Use `setq' to reset ac-source-NAME every time
  1607. ;; `ac-define-source' is called. This is useful, for example
  1608. ;; when evaluating `ac-define-source' using C-M-x (`eval-defun').
  1609. (setq ,(intern (format "ac-source-%s" name)) ,source)
  1610. (defun ,(intern (format "ac-complete-%s" name)) ()
  1611. (interactive)
  1612. (auto-complete '(,(intern (format "ac-source-%s" name)))))))
  1613. ;; Words in buffer source
  1614. (defvar ac-word-index nil)
  1615. (defun ac-candidate-words-in-buffer (point prefix limit)
  1616. (let ((i 0)
  1617. candidate
  1618. candidates
  1619. (regexp (concat "\\_<" (regexp-quote prefix) "\\(\\sw\\|\\s_\\)+\\_>")))
  1620. (save-excursion
  1621. ;; Search backward
  1622. (goto-char point)
  1623. (while (and (or (not (integerp limit)) (< i limit))
  1624. (re-search-backward regexp nil t))
  1625. (setq candidate (match-string-no-properties 0))
  1626. (unless (member candidate candidates)
  1627. (push candidate candidates)
  1628. (cl-incf i)))
  1629. ;; Search backward
  1630. (goto-char (+ point (length prefix)))
  1631. (while (and (or (not (integerp limit)) (< i limit))
  1632. (re-search-forward regexp nil t))
  1633. (setq candidate (match-string-no-properties 0))
  1634. (unless (member candidate candidates)
  1635. (push candidate candidates)
  1636. (cl-incf i)))
  1637. (nreverse candidates))))
  1638. (defun ac-incremental-update-word-index ()
  1639. (unless (local-variable-p 'ac-word-index)
  1640. (make-local-variable 'ac-word-index))
  1641. (if (null ac-word-index)
  1642. (setq ac-word-index (cons nil nil)))
  1643. ;; Mark incomplete
  1644. (if (car ac-word-index)
  1645. (setcar ac-word-index nil))
  1646. (let ((index (cdr ac-word-index))
  1647. (words (ac-candidate-words-in-buffer ac-point ac-prefix (or (and (integerp ac-limit) ac-limit) 10))))
  1648. (dolist (word words)
  1649. (unless (member word index)
  1650. (push word index)
  1651. (setcdr ac-word-index index)))))
  1652. (defun ac-update-word-index-1 ()
  1653. (unless (local-variable-p 'ac-word-index)
  1654. (make-local-variable 'ac-word-index))
  1655. (when (and (not (car ac-word-index))
  1656. (< (buffer-size) 1048576))
  1657. ;; Complete index
  1658. (setq ac-word-index
  1659. (cons t
  1660. (split-string (buffer-substring-no-properties (point-min) (point-max))
  1661. "\\(?:^\\|\\_>\\).*?\\(?:\\_<\\|$\\)")))))
  1662. (defun ac-update-word-index ()
  1663. (dolist (buffer (buffer-list))
  1664. (when (or ac-fuzzy-enable
  1665. (not (eq buffer (current-buffer))))
  1666. (with-current-buffer buffer
  1667. (ac-update-word-index-1)))))
  1668. (defun ac-word-candidates (&optional buffer-pred)
  1669. (cl-loop initially (unless ac-fuzzy-enable (ac-incremental-update-word-index))
  1670. for buffer in (buffer-list)
  1671. if (and (or (not (integerp ac-limit)) (< (length candidates) ac-limit))
  1672. (if buffer-pred (funcall buffer-pred buffer) t))
  1673. append (funcall ac-match-function
  1674. ac-prefix
  1675. (and (local-variable-p 'ac-word-index buffer)
  1676. (cdr (buffer-local-value 'ac-word-index buffer))))
  1677. into candidates
  1678. finally return (delete-dups candidates)))
  1679. (ac-define-source words-in-buffer
  1680. '((candidates . ac-word-candidates)))
  1681. (ac-define-source words-in-all-buffer
  1682. '((init . ac-update-word-index)
  1683. (candidates . ac-word-candidates)))
  1684. (ac-define-source words-in-same-mode-buffers
  1685. '((init . ac-update-word-index)
  1686. (candidates . (ac-word-candidates
  1687. (lambda (buffer)
  1688. (derived-mode-p (buffer-local-value 'major-mode buffer)))))))
  1689. ;; Lisp symbols source
  1690. (defvar ac-symbols-cache nil)
  1691. (ac-clear-variable-every-10-minutes 'ac-symbols-cache)
  1692. (defun ac-symbol-file (symbol type)
  1693. (if (fboundp 'find-lisp-object-file-name)
  1694. (find-lisp-object-file-name symbol type)
  1695. (let ((file-name (with-no-warnings
  1696. (describe-simplify-lib-file-name
  1697. (symbol-file symbol type)))))
  1698. (when (equal file-name "loaddefs.el")
  1699. ;; Find the real def site of the preloaded object.
  1700. (let ((location (condition-case nil
  1701. (if (eq type 'defun)
  1702. (find-function-search-for-symbol symbol nil
  1703. "loaddefs.el")
  1704. (find-variable-noselect symbol file-name))
  1705. (error nil))))
  1706. (when location
  1707. (with-current-buffer (car location)
  1708. (when (cdr location)
  1709. (goto-char (cdr location)))
  1710. (when (re-search-backward
  1711. "^;;; Generated autoloads from \\(.*\\)" nil t)
  1712. (setq file-name (match-string 1)))))))
  1713. (if (and (null file-name)
  1714. (or (eq type 'defun)
  1715. (integerp (get symbol 'variable-documentation))))
  1716. ;; It's a object not defined in Elisp but in C.
  1717. (if (get-buffer " *DOC*")
  1718. (if (eq type 'defun)
  1719. (help-C-file-name (symbol-function symbol) 'subr)
  1720. (help-C-file-name symbol 'var))
  1721. 'C-source)
  1722. file-name))))
  1723. (defun ac-symbol-documentation (symbol)
  1724. (if (stringp symbol)
  1725. (setq symbol (intern-soft symbol)))
  1726. (ignore-errors
  1727. (with-temp-buffer
  1728. (let ((standard-output (current-buffer)))
  1729. (prin1 symbol)
  1730. (princ " is ")
  1731. (cond
  1732. ((fboundp symbol)
  1733. ;; import help-xref-following
  1734. (require 'help-mode)
  1735. (let ((help-xref-following t)
  1736. (major-mode 'help-mode)) ; avoid error in Emacs 24
  1737. (describe-function-1 symbol))
  1738. (buffer-string))
  1739. ((boundp symbol)
  1740. (let ((file-name (ac-symbol-file symbol 'defvar)))
  1741. (princ "a variable")
  1742. (when file-name
  1743. (princ " defined in `")
  1744. (princ (if (eq file-name 'C-source)
  1745. "C source code"
  1746. (file-name-nondirectory file-name))))
  1747. (princ "'.\n\n")
  1748. (princ (or (documentation-property symbol 'variable-documentation t)
  1749. "Not documented."))
  1750. (buffer-string)))
  1751. ((facep symbol)
  1752. (let ((file-name (ac-symbol-file symbol 'defface)))
  1753. (princ "a face")
  1754. (when file-name
  1755. (princ " defined in `")
  1756. (princ (if (eq file-name 'C-source)
  1757. "C source code"
  1758. (file-name-nondirectory file-name))))
  1759. (princ "'.\n\n")
  1760. (princ (or (documentation-property symbol 'face-documentation t)
  1761. "Not documented."))
  1762. (buffer-string)))
  1763. (t
  1764. (let ((doc (documentation-property symbol 'group-documentation t)))
  1765. (when doc
  1766. (princ "a group.\n\n")
  1767. (princ doc)
  1768. (buffer-string)))))))))
  1769. (defun ac-symbol-candidates ()
  1770. (or ac-symbols-cache
  1771. (setq ac-symbols-cache
  1772. (cl-loop for x being the symbols
  1773. if (or (fboundp x)
  1774. (boundp x)
  1775. (symbol-plist x))
  1776. collect (symbol-name x)))))
  1777. (ac-define-source symbols
  1778. '((candidates . ac-symbol-candidates)
  1779. (document . ac-symbol-documentation)
  1780. (symbol . "s")
  1781. (cache)))
  1782. ;; Lisp functions source
  1783. (defvar ac-functions-cache nil)
  1784. (ac-clear-variable-every-10-minutes 'ac-functions-cache)
  1785. (defun ac-function-candidates ()
  1786. (or ac-functions-cache
  1787. (setq ac-functions-cache
  1788. (cl-loop for x being the symbols
  1789. if (fboundp x)
  1790. collect (symbol-name x)))))
  1791. (ac-define-source functions
  1792. '((candidates . ac-function-candidates)
  1793. (document . ac-symbol-documentation)
  1794. (symbol . "f")
  1795. (prefix . "(\\(\\(?:\\sw\\|\\s_\\)+\\)")
  1796. (cache)))
  1797. ;; Lisp variables source
  1798. (defvar ac-variables-cache nil)
  1799. (ac-clear-variable-every-10-minutes 'ac-variables-cache)
  1800. (defun ac-variable-candidates ()
  1801. (or ac-variables-cache
  1802. (setq ac-variables-cache
  1803. (cl-loop for x being the symbols
  1804. if (boundp x)
  1805. collect (symbol-name x)))))
  1806. (ac-define-source variables
  1807. '((candidates . ac-variable-candidates)
  1808. (document . ac-symbol-documentation)
  1809. (symbol . "v")
  1810. (cache)))
  1811. ;; Lisp features source
  1812. (defvar ac-emacs-lisp-features nil)
  1813. (ac-clear-variable-every-10-minutes 'ac-emacs-lisp-features)
  1814. (defun ac-emacs-lisp-feature-candidates ()
  1815. (or ac-emacs-lisp-features
  1816. (if (fboundp 'find-library-suffixes)
  1817. (let ((suffix (concat (regexp-opt (find-library-suffixes) t) "\\'")))
  1818. (setq ac-emacs-lisp-features
  1819. (append (mapcar 'prin1-to-string features)
  1820. (cl-loop for dir in load-path
  1821. if (file-directory-p dir)
  1822. append (cl-loop for file in (directory-files dir)
  1823. if (string-match suffix file)
  1824. collect (substring file 0 (match-beginning 0))))))))))
  1825. (ac-define-source features
  1826. '((depends find-func)
  1827. (candidates . ac-emacs-lisp-feature-candidates)
  1828. (prefix . "require +'\\(\\(?:\\sw\\|\\s_\\)*\\)")
  1829. (requires . 0)))
  1830. (defvaralias 'ac-source-emacs-lisp-features 'ac-source-features)
  1831. ;; Abbrev source
  1832. (ac-define-source abbrev
  1833. '((candidates . (mapcar 'popup-x-to-string (append (vconcat local-abbrev-table global-abbrev-table) nil)))
  1834. (action . expand-abbrev)
  1835. (symbol . "a")
  1836. (cache)))
  1837. ;; Files in current directory source
  1838. (ac-define-source files-in-current-dir
  1839. '((candidates . (directory-files default-directory))
  1840. (cache)))
  1841. ;; Filename source
  1842. (defvar ac-filename-cache nil)
  1843. (defun ac-filename-candidate ()
  1844. (let (file-name-handler-alist)
  1845. (unless (or (and comment-start-skip
  1846. (string-match comment-start-skip ac-prefix))
  1847. (file-regular-p ac-prefix))
  1848. (ignore-errors
  1849. (cl-loop with dir = (file-name-directory ac-prefix)
  1850. with files = (or (assoc-default dir ac-filename-cache)
  1851. (let ((files (directory-files dir nil "^[^.]")))
  1852. (push (cons dir files) ac-filename-cache)
  1853. files))
  1854. for file in files
  1855. for path = (concat dir file)
  1856. collect (if (file-directory-p path)
  1857. (concat path "/")
  1858. path))))))
  1859. (ac-define-source filename
  1860. '((init . (setq ac-filename-cache nil))
  1861. (candidates . ac-filename-candidate)
  1862. (prefix . valid-file)
  1863. (requires . 0)
  1864. (action . ac-start)
  1865. (limit . nil)))
  1866. ;; Dictionary source
  1867. (ac-define-source dictionary
  1868. '((candidates . ac-buffer-dictionary)
  1869. (symbol . "d")))
  1870. (provide 'auto-complete)
  1871. ;;; auto-complete.el ends here