Klimi's new dotfiles with stow.
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.

2164 lines
73 KiB

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