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

543 lines
19 KiB

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