Klimi's new dotfiles with stow.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

551 rader
20 KiB

5 år sedan
  1. ;;; auto-complete-config.el --- auto-complete additional configuations
  2. ;; Copyright (C) 2009, 2010 Tomohiro Matsuyama
  3. ;; Author: Tomohiro Matsuyama <m2ym.pub@gmail.com>
  4. ;; Keywords: convenience
  5. ;; Version: 1.5.0
  6. ;; This program is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. (require 'cl-lib)
  20. (require 'auto-complete)
  21. (declare-function semantic-analyze-current-context "semantic/analyze")
  22. (declare-function semantic-tag-class "semantic/tag")
  23. (declare-function semantic-tag-function-arguments "semantic/tag")
  24. (declare-function semantic-format-tag-type "semantic/format")
  25. (declare-function semantic-format-tag-name "semantic/format")
  26. (declare-function yas-expand-snippet "yasnippet")
  27. (declare-function oref "eieio" (obj slot))
  28. ;;;; Additional sources
  29. ;; imenu
  30. (defvar ac-imenu-index nil)
  31. (ac-clear-variable-every-10-minutes 'ac-imenu-index)
  32. (defun ac-imenu-candidates ()
  33. (cl-loop with i = 0
  34. with stack = (progn
  35. (unless (local-variable-p 'ac-imenu-index)
  36. (make-local-variable 'ac-imenu-index))
  37. (or ac-imenu-index
  38. (setq ac-imenu-index
  39. (ignore-errors
  40. (with-no-warnings
  41. (imenu--make-index-alist))))))
  42. with result
  43. while (and stack (or (not (integerp ac-limit))
  44. (< i ac-limit)))
  45. for node = (pop stack)
  46. if (consp node)
  47. do
  48. (let ((car (car node))
  49. (cdr (cdr node)))
  50. (if (consp cdr)
  51. (mapc (lambda (child)
  52. (push child stack))
  53. cdr)
  54. (when (and (stringp car)
  55. (string-match (concat "^" (regexp-quote ac-prefix)) car))
  56. ;; Remove extra characters
  57. (if (string-match "^.*\\(()\\|=\\|<>\\)$" car)
  58. (setq car (substring car 0 (match-beginning 1))))
  59. (push car result)
  60. (cl-incf i))))
  61. finally return (nreverse result)))
  62. (ac-define-source imenu
  63. '((depends imenu)
  64. (candidates . ac-imenu-candidates)
  65. (symbol . "s")))
  66. ;; gtags
  67. (defface ac-gtags-candidate-face
  68. '((t (:inherit ac-candidate-face :foreground "navy")))
  69. "Face for gtags candidate"
  70. :group 'auto-complete)
  71. (defface ac-gtags-selection-face
  72. '((t (:inherit ac-selection-face :background "navy")))
  73. "Face for the gtags selected candidate."
  74. :group 'auto-complete)
  75. (defun ac-gtags-candidate ()
  76. (ignore-errors
  77. (split-string (shell-command-to-string (format "global -ciq %s" ac-prefix)) "\n")))
  78. (ac-define-source gtags
  79. '((candidates . ac-gtags-candidate)
  80. (candidate-face . ac-gtags-candidate-face)
  81. (selection-face . ac-gtags-selection-face)
  82. (requires . 3)
  83. (symbol . "s")))
  84. ;; yasnippet
  85. (defface ac-yasnippet-candidate-face
  86. '((t (:inherit ac-candidate-face
  87. :background "sandybrown" :foreground "black")))
  88. "Face for yasnippet candidate."
  89. :group 'auto-complete)
  90. (defface ac-yasnippet-selection-face
  91. '((t (:inherit ac-selection-face :background "coral3")))
  92. "Face for the yasnippet selected candidate."
  93. :group 'auto-complete)
  94. (defun ac-yasnippet-table-hash (table)
  95. (cond
  96. ((fboundp 'yas/snippet-table-hash)
  97. (yas/snippet-table-hash table))
  98. ((fboundp 'yas/table-hash)
  99. (yas/table-hash table))))
  100. (defun ac-yasnippet-table-parent (table)
  101. (cond
  102. ((fboundp 'yas/snippet-table-parent)
  103. (yas/snippet-table-parent table))
  104. ((fboundp 'yas/table-parent)
  105. (yas/table-parent table))))
  106. (defun ac-yasnippet-candidate-1 (table)
  107. (with-no-warnings
  108. (let ((hashtab (ac-yasnippet-table-hash table))
  109. (parent (ac-yasnippet-table-parent table))
  110. candidates)
  111. (maphash (lambda (key value)
  112. (push key candidates))
  113. hashtab)
  114. (setq candidates (all-completions ac-prefix (nreverse candidates)))
  115. (if parent
  116. (setq candidates
  117. (append candidates (ac-yasnippet-candidate-1 parent))))
  118. candidates)))
  119. (defun ac-yasnippet-candidates ()
  120. (with-no-warnings
  121. (cond (;; 0.8 onwards
  122. (fboundp 'yas-active-keys)
  123. (all-completions ac-prefix (yas-active-keys)))
  124. (;; >0.6.0
  125. (fboundp 'yas/get-snippet-tables)
  126. (apply 'append (mapcar 'ac-yasnippet-candidate-1
  127. (condition-case nil
  128. (yas/get-snippet-tables major-mode)
  129. (wrong-number-of-arguments
  130. (yas/get-snippet-tables)))))
  131. )
  132. (t
  133. (let ((table
  134. (if (fboundp 'yas/snippet-table)
  135. ;; <0.6.0
  136. (yas/snippet-table major-mode)
  137. ;; 0.6.0
  138. (yas/current-snippet-table))))
  139. (if table
  140. (ac-yasnippet-candidate-1 table)))))))
  141. (ac-define-source yasnippet
  142. '((depends yasnippet)
  143. (candidates . ac-yasnippet-candidates)
  144. (action . yas/expand)
  145. (candidate-face . ac-yasnippet-candidate-face)
  146. (selection-face . ac-yasnippet-selection-face)
  147. (symbol . "a")))
  148. ;; semantic
  149. (defun ac-semantic-candidates (prefix)
  150. (with-no-warnings
  151. (delete "" ; semantic sometimes returns an empty string
  152. (mapcar (lambda (elem)
  153. (cons (semantic-tag-name elem)
  154. (semantic-tag-clone elem)))
  155. (ignore-errors
  156. (or (semantic-analyze-possible-completions
  157. (semantic-analyze-current-context))
  158. (senator-find-tag-for-completion prefix)))))))
  159. (defun ac-semantic-doc (symbol)
  160. (with-no-warnings
  161. (let* ((proto (semantic-format-tag-summarize-with-file symbol nil t))
  162. (doc (semantic-documentation-for-tag symbol))
  163. (res proto))
  164. (when doc
  165. (setq res (concat res "\n\n" doc)))
  166. res)))
  167. (defun ac-semantic-action ()
  168. (when (and (boundp 'yas-minor-mode) yas-minor-mode)
  169. (let* ((tag (car (last (oref (semantic-analyze-current-context) prefix))))
  170. (class (semantic-tag-class tag))
  171. (args))
  172. (when (eq class 'function)
  173. (setq args (semantic-tag-function-arguments tag))
  174. (yas-expand-snippet
  175. (concat "("
  176. (mapconcat
  177. (lambda (arg)
  178. (let ((arg-type (semantic-format-tag-type arg nil))
  179. (arg-name (semantic-format-tag-name arg nil)))
  180. (concat "${"
  181. (if (string= arg-name "")
  182. arg-type
  183. (concat arg-type " " arg-name))
  184. "}")))
  185. args
  186. ", ")
  187. ")$0"))))))
  188. (ac-define-source semantic
  189. '((available . (or (require 'semantic-ia nil t)
  190. (require 'semantic/ia nil t)))
  191. (candidates . (ac-semantic-candidates ac-prefix))
  192. (document . ac-semantic-doc)
  193. (action . ac-semantic-action)
  194. (prefix . cc-member)
  195. (requires . 0)
  196. (symbol . "m")))
  197. (ac-define-source semantic-raw
  198. '((available . (or (require 'semantic-ia nil t)
  199. (require 'semantic/ia nil t)))
  200. (candidates . (ac-semantic-candidates ac-prefix))
  201. (document . ac-semantic-doc)
  202. (action . ac-semantic-action)
  203. (symbol . "s")))
  204. ;; eclim
  205. (defun ac-eclim-candidates ()
  206. (with-no-warnings
  207. (cl-loop for c in (eclim/java-complete)
  208. collect (nth 1 c))))
  209. (ac-define-source eclim
  210. '((candidates . ac-eclim-candidates)
  211. (prefix . c-dot)
  212. (requires . 0)
  213. (symbol . "f")))
  214. ;; css
  215. ;; Copied from company-css.el
  216. (defconst ac-css-property-alist
  217. ;; see http://www.w3.org/TR/CSS21/propidx.html
  218. '(("azimuth" angle "left-side" "far-left" "left" "center-left" "center"
  219. "center-right" "right" "far-right" "right-side" "behind" "leftwards"
  220. "rightwards")
  221. ("background" background-color background-image background-repeat
  222. background-attachment background-position)
  223. ("background-attachment" "scroll" "fixed")
  224. ("background-color" color "transparent")
  225. ("background-image" uri "none")
  226. ("background-position" percentage length "left" "center" "right" percentage
  227. length "top" "center" "bottom" "left" "center" "right" "top" "center"
  228. "bottom")
  229. ("background-repeat" "repeat" "repeat-x" "repeat-y" "no-repeat")
  230. ("border" border-width border-style border-color)
  231. ("border-bottom" border)
  232. ("border-bottom-color" border-color)
  233. ("border-bottom-style" border-style)
  234. ("border-bottom-width" border-width)
  235. ("border-collapse" "collapse" "separate")
  236. ("border-color" color "transparent")
  237. ("border-left" border)
  238. ("border-left-color" border-color)
  239. ("border-left-style" border-style)
  240. ("border-left-width" border-width)
  241. ("border-right" border)
  242. ("border-right-color" border-color)
  243. ("border-right-style" border-style)
  244. ("border-right-width" border-width)
  245. ("border-spacing" length length)
  246. ("border-style" border-style)
  247. ("border-top" border)
  248. ("border-top-color" border-color)
  249. ("border-top-style" border-style)
  250. ("border-top-width" border-width)
  251. ("border-width" border-width)
  252. ("bottom" length percentage "auto")
  253. ("caption-side" "top" "bottom")
  254. ("clear" "none" "left" "right" "both")
  255. ("clip" shape "auto")
  256. ("color" color)
  257. ("content" "normal" "none" string uri counter "attr()" "open-quote"
  258. "close-quote" "no-open-quote" "no-close-quote")
  259. ("counter-increment" identifier integer "none")
  260. ("counter-reset" identifier integer "none")
  261. ("cue" cue-before cue-after)
  262. ("cue-after" uri "none")
  263. ("cue-before" uri "none")
  264. ("cursor" uri "*" "auto" "crosshair" "default" "pointer" "move" "e-resize"
  265. "ne-resize" "nw-resize" "n-resize" "se-resize" "sw-resize" "s-resize"
  266. "w-resize" "text" "wait" "help" "progress")
  267. ("direction" "ltr" "rtl")
  268. ("display" "inline" "block" "list-item" "run-in" "inline-block" "table"
  269. "inline-table" "table-row-group" "table-header-group" "table-footer-group"
  270. "table-row" "table-column-group" "table-column" "table-cell"
  271. "table-caption" "none")
  272. ("elevation" angle "below" "level" "above" "higher" "lower")
  273. ("empty-cells" "show" "hide")
  274. ("float" "left" "right" "none")
  275. ("font" font-style font-variant font-weight font-size "/" line-height
  276. font-family "caption" "icon" "menu" "message-box" "small-caption"
  277. "status-bar")
  278. ("font-family" family-name generic-family)
  279. ("font-size" absolute-size relative-size length percentage)
  280. ("font-style" "normal" "italic" "oblique")
  281. ("font-variant" "normal" "small-caps")
  282. ("font-weight" "normal" "bold" "bolder" "lighter" "100" "200" "300" "400"
  283. "500" "600" "700" "800" "900")
  284. ("height" length percentage "auto")
  285. ("left" length percentage "auto")
  286. ("letter-spacing" "normal" length)
  287. ("line-height" "normal" number length percentage)
  288. ("list-style" list-style-type list-style-position list-style-image)
  289. ("list-style-image" uri "none")
  290. ("list-style-position" "inside" "outside")
  291. ("list-style-type" "disc" "circle" "square" "decimal" "decimal-leading-zero"
  292. "lower-roman" "upper-roman" "lower-greek" "lower-latin" "upper-latin"
  293. "armenian" "georgian" "lower-alpha" "upper-alpha" "none")
  294. ("margin" margin-width)
  295. ("margin-bottom" margin-width)
  296. ("margin-left" margin-width)
  297. ("margin-right" margin-width)
  298. ("margin-top" margin-width)
  299. ("max-height" length percentage "none")
  300. ("max-width" length percentage "none")
  301. ("min-height" length percentage)
  302. ("min-width" length percentage)
  303. ("orphans" integer)
  304. ("outline" outline-color outline-style outline-width)
  305. ("outline-color" color "invert")
  306. ("outline-style" border-style)
  307. ("outline-width" border-width)
  308. ("overflow" "visible" "hidden" "scroll" "auto")
  309. ("padding" padding-width)
  310. ("padding-bottom" padding-width)
  311. ("padding-left" padding-width)
  312. ("padding-right" padding-width)
  313. ("padding-top" padding-width)
  314. ("page-break-after" "auto" "always" "avoid" "left" "right")
  315. ("page-break-before" "auto" "always" "avoid" "left" "right")
  316. ("page-break-inside" "avoid" "auto")
  317. ("pause" time percentage)
  318. ("pause-after" time percentage)
  319. ("pause-before" time percentage)
  320. ("pitch" frequency "x-low" "low" "medium" "high" "x-high")
  321. ("pitch-range" number)
  322. ("play-during" uri "mix" "repeat" "auto" "none")
  323. ("position" "static" "relative" "absolute" "fixed")
  324. ("quotes" string string "none")
  325. ("richness" number)
  326. ("right" length percentage "auto")
  327. ("speak" "normal" "none" "spell-out")
  328. ("speak-header" "once" "always")
  329. ("speak-numeral" "digits" "continuous")
  330. ("speak-punctuation" "code" "none")
  331. ("speech-rate" number "x-slow" "slow" "medium" "fast" "x-fast" "faster"
  332. "slower")
  333. ("stress" number)
  334. ("table-layout" "auto" "fixed")
  335. ("text-align" "left" "right" "center" "justify")
  336. ("text-decoration" "none" "underline" "overline" "line-through" "blink")
  337. ("text-indent" length percentage)
  338. ("text-transform" "capitalize" "uppercase" "lowercase" "none")
  339. ("top" length percentage "auto")
  340. ("unicode-bidi" "normal" "embed" "bidi-override")
  341. ("vertical-align" "baseline" "sub" "super" "top" "text-top" "middle"
  342. "bottom" "text-bottom" percentage length)
  343. ("visibility" "visible" "hidden" "collapse")
  344. ("voice-family" specific-voice generic-voice "*" specific-voice
  345. generic-voice)
  346. ("volume" number percentage "silent" "x-soft" "soft" "medium" "loud"
  347. "x-loud")
  348. ("white-space" "normal" "pre" "nowrap" "pre-wrap" "pre-line")
  349. ("widows" integer)
  350. ("width" length percentage "auto")
  351. ("word-spacing" "normal" length)
  352. ("z-index" "auto" integer))
  353. "A list of CSS properties and their possible values.")
  354. (defconst ac-css-value-classes
  355. '((absolute-size "xx-small" "x-small" "small" "medium" "large" "x-large"
  356. "xx-large")
  357. (border-style "none" "hidden" "dotted" "dashed" "solid" "double" "groove"
  358. "ridge" "inset" "outset")
  359. (color "aqua" "black" "blue" "fuchsia" "gray" "green" "lime" "maroon" "navy"
  360. "olive" "orange" "purple" "red" "silver" "teal" "white" "yellow"
  361. "rgb")
  362. (counter "counter")
  363. (family-name "Courier" "Helvetica" "Times")
  364. (generic-family "serif" "sans-serif" "cursive" "fantasy" "monospace")
  365. (generic-voice "male" "female" "child")
  366. (margin-width "auto") ;; length percentage
  367. (relative-size "larger" "smaller")
  368. (shape "rect")
  369. (uri "url"))
  370. "A list of CSS property value classes and their contents.")
  371. (defconst ac-css-pseudo-classes
  372. '("active" "after" "before" "first" "first-child" "first-letter" "first-line"
  373. "focus" "hover" "lang" "left" "link" "right" "visited")
  374. "Identifiers for CSS pseudo-elements and pseudo-classes.")
  375. (defvar ac-css-property nil
  376. "Current editing property.")
  377. (defun ac-css-prefix ()
  378. (when (save-excursion (re-search-backward "\\_<\\(.+?\\)\\_>\\s *:[^;]*\\=" nil t))
  379. (setq ac-css-property (match-string 1))
  380. (or (ac-prefix-symbol) (point))))
  381. (defun ac-css-property-candidates ()
  382. (let ((list (assoc-default ac-css-property ac-css-property-alist)))
  383. (if list
  384. (cl-loop with seen
  385. with value
  386. while (setq value (pop list))
  387. if (symbolp value)
  388. do (unless (memq value seen)
  389. (push value seen)
  390. (setq list
  391. (append list
  392. (or (assoc-default value ac-css-value-classes)
  393. (assoc-default (symbol-name value) ac-css-property-alist)))))
  394. else collect value)
  395. ac-css-pseudo-classes)))
  396. (ac-define-source css-property
  397. '((candidates . ac-css-property-candidates)
  398. (prefix . ac-css-prefix)
  399. (requires . 0)))
  400. ;; slime
  401. (ac-define-source slime
  402. '((depends slime)
  403. (candidates . (car (slime-simple-completions ac-prefix)))
  404. (symbol . "s")
  405. (cache)))
  406. ;; ghc-mod
  407. (ac-define-source ghc-mod
  408. '((depends ghc)
  409. (candidates . (ghc-select-completion-symbol))
  410. (symbol . "s")
  411. (cache)))
  412. ;;;; Not maintained sources
  413. ;; ropemacs
  414. (defvar ac-ropemacs-loaded nil)
  415. (defun ac-ropemacs-require ()
  416. (with-no-warnings
  417. (unless ac-ropemacs-loaded
  418. (pymacs-load "ropemacs" "rope-")
  419. (if (boundp 'ropemacs-enable-autoimport)
  420. (setq ropemacs-enable-autoimport t))
  421. (setq ac-ropemacs-loaded t))))
  422. (defun ac-ropemacs-setup ()
  423. (ac-ropemacs-require)
  424. ;(setq ac-sources (append (list 'ac-source-ropemacs) ac-sources))
  425. (setq ac-omni-completion-sources '(("\\." ac-source-ropemacs))))
  426. (defun ac-ropemacs-initialize ()
  427. (autoload 'pymacs-apply "pymacs")
  428. (autoload 'pymacs-call "pymacs")
  429. (autoload 'pymacs-eval "pymacs" nil t)
  430. (autoload 'pymacs-exec "pymacs" nil t)
  431. (autoload 'pymacs-load "pymacs" nil t)
  432. (add-hook 'python-mode-hook 'ac-ropemacs-setup)
  433. t)
  434. (defvar ac-ropemacs-completions-cache nil)
  435. (defvar ac-source-ropemacs
  436. '((init
  437. . (lambda ()
  438. (setq ac-ropemacs-completions-cache
  439. (mapcar
  440. (lambda (completion)
  441. (concat ac-prefix completion))
  442. (ignore-errors
  443. (rope-completions))))))
  444. (candidates . ac-ropemacs-completions-cache)))
  445. ;; rcodetools
  446. (defvar ac-source-rcodetools
  447. '((init . (lambda ()
  448. (require 'rcodetools)
  449. (condition-case x
  450. (save-excursion
  451. (rct-exec-and-eval rct-complete-command-name "--completion-emacs-icicles"))
  452. (error) (setq rct-method-completion-table nil))))
  453. (candidates . (lambda ()
  454. (all-completions
  455. ac-prefix
  456. (mapcar
  457. (lambda (completion)
  458. (replace-regexp-in-string "\t.*$" "" (car completion)))
  459. rct-method-completion-table))))))
  460. ;;;; Default settings
  461. (defun ac-common-setup ()
  462. ;(add-to-list 'ac-sources 'ac-source-filename)
  463. )
  464. (defun ac-emacs-lisp-mode-setup ()
  465. (setq ac-sources (append '(ac-source-features ac-source-functions ac-source-yasnippet ac-source-variables ac-source-symbols) ac-sources)))
  466. (defun ac-cc-mode-setup ()
  467. (setq ac-sources (append '(ac-source-yasnippet ac-source-gtags) ac-sources)))
  468. (defun ac-ruby-mode-setup ())
  469. (defun ac-css-mode-setup ()
  470. (setq ac-sources (append '(ac-source-css-property) ac-sources)))
  471. ;;;###autoload
  472. (defun ac-config-default ()
  473. (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  474. (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
  475. (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
  476. (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
  477. (add-hook 'css-mode-hook 'ac-css-mode-setup)
  478. (add-hook 'auto-complete-mode-hook 'ac-common-setup)
  479. (global-auto-complete-mode t))
  480. (provide 'auto-complete-config)
  481. ;;; auto-complete-config.el ends here