ein-ac.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ;;; ein-ac.el --- Auto-complete extension
  2. ;; Copyright (C) 2012- Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-ac.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-ac.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-ac.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (require 'auto-complete nil t)
  20. (require 'ein-core)
  21. (eval-when-compile (require 'ein-notebook)
  22. (defvar ein:mumamo-codecell-mode))
  23. ;;; Configuration
  24. (defvar ein:ac-sources (and (boundp 'ac-sources)
  25. (default-value 'ac-sources))
  26. "Extra `ac-sources' used in notebook.")
  27. (make-obsolete-variable 'ein:ac-max-cache nil "0.1.2")
  28. (defcustom ein:ac-max-cache 1000
  29. "[This value is not used anymore!]
  30. Maximum number of cache to store."
  31. :type 'integer
  32. :group 'ein)
  33. ;;; Chunk (adapted from auto-complete-chunk.el)
  34. (defvar ein:ac-chunk-regex
  35. (rx (group (| (syntax whitespace)
  36. (syntax open-parenthesis)
  37. (syntax close-parenthesis)
  38. (syntax string-quote) ; Complete files for `open("path/..`
  39. bol))
  40. (? (syntax punctuation)) ; to complete ``~/PATH/...``
  41. (* (+ (| (syntax word) (syntax symbol)))
  42. (syntax punctuation))
  43. (+ (| (syntax word) (syntax symbol)))
  44. (? (syntax punctuation))
  45. point)
  46. "A regexp that matches to a \"chunk\" containing words and dots.")
  47. (defun ein:ac-chunk-beginning ()
  48. "Return the position where the chunk begins."
  49. (ignore-errors
  50. (save-excursion
  51. (+ (re-search-backward ein:ac-chunk-regex) (length (match-string 1))))))
  52. (defun ein:ac-chunk-candidates-from-list (chunk-list)
  53. "Return matched candidates in CHUNK-LIST."
  54. (let* ((start (ein:ac-chunk-beginning)))
  55. (when start
  56. (loop with prefix = (buffer-substring start (point))
  57. for cc in chunk-list
  58. when (string-prefix-p prefix cc)
  59. collect cc))))
  60. ;;; AC Source
  61. (defvar ein:ac-direct-matches nil
  62. "Variable to store completion candidates for `auto-completion'.")
  63. ;; FIXME: Maybe this should be buffer-local?
  64. (defun ein:ac-direct-get-matches ()
  65. (ein:ac-chunk-candidates-from-list ein:ac-direct-matches))
  66. (ac-define-source ein-direct
  67. '((candidates . ein:ac-direct-get-matches)
  68. (requires . 0)
  69. (prefix . ein:ac-chunk-beginning)
  70. (symbol . "s")))
  71. (ac-define-source ein-async
  72. '((candidates . ein:ac-direct-get-matches)
  73. (requires . 0)
  74. (prefix . ein:ac-chunk-beginning)
  75. (init . ein:ac-request-in-background)
  76. (symbol . "c")))
  77. (define-obsolete-function-alias 'ac-complete-ein-cached 'ac-complete-ein-async
  78. "0.2.1")
  79. (define-obsolete-variable-alias 'ac-source-ein-cached 'ac-source-ein-async
  80. "0.2.1")
  81. (defun ein:ac-request-in-background ()
  82. (ein:and-let* ((kernel (ein:get-kernel))
  83. ((ein:kernel-live-p kernel)))
  84. (ein:completer-complete
  85. kernel
  86. :callbacks
  87. (list :complete_reply
  88. (cons (lambda (_ content __)
  89. (ein:ac-prepare-completion (plist-get content :matches)))
  90. nil)))))
  91. ;;; Completer interface
  92. (defun ein:ac-prepare-completion (matches)
  93. "Prepare `ac-source-ein-direct' using MATCHES from kernel.
  94. Call this function before calling `auto-complete'."
  95. (when matches
  96. (setq ein:ac-direct-matches matches))) ; let-binding won't work
  97. (defun* ein:completer-finish-completing-ac
  98. (matched-text
  99. matches
  100. &key (expand ac-expand-on-auto-complete)
  101. &allow-other-keys)
  102. "Invoke completion using `auto-complete'.
  103. Only the argument MATCHES is used. MATCHED-TEXT is for
  104. compatibility with `ein:completer-finish-completing-default'."
  105. ;; I don't need to check if the point is at right position, as in
  106. ;; `ein:completer-finish-completing-default' because `auto-complete'
  107. ;; checks it anyway.
  108. (ein:log 'debug "COMPLETER-FINISH-COMPLETING-AC: matched-text=%S matches=%S"
  109. matched-text matches)
  110. (ein:ac-prepare-completion matches)
  111. (when matches ; No auto-complete drop-down list when no matches
  112. (let ((ac-expand-on-auto-complete expand))
  113. (ac-start))))
  114. ;; Why `ac-start'? See: `jedi:complete'.
  115. ;;; Async document request hack
  116. (defun ein:ac-request-document-for-selected-candidate ()
  117. "Request object information for the candidate at point.
  118. This is called via `ac-next'/`ac-previous'/`ac-update' and set
  119. `document' property of the current candidate string. If server
  120. replied within `ac-quick-help-delay' seconds, auto-complete will
  121. popup help string."
  122. (let* ((candidate (ac-selected-candidate))
  123. (kernel (ein:get-kernel))
  124. (callbacks (list :object_info_reply
  125. (cons #'ein:ac-set-document candidate))))
  126. (when (and candidate
  127. (ein:kernel-live-p kernel)
  128. (not (get-text-property 0 'document candidate)))
  129. (ein:log 'debug "Requesting object info for AC candidate %S"
  130. candidate)
  131. (ein:kernel-object-info-request kernel candidate callbacks))))
  132. (defun ein:ac-set-document (candidate content -metadata-not-used-)
  133. (ein:log 'debug "EIN:AC-SET-DOCUMENT candidate=%S content=%S"
  134. candidate content)
  135. (put-text-property 0 (length candidate)
  136. 'document (ein:kernel-construct-help-string content)
  137. candidate))
  138. (defadvice ac-next (after ein:ac-next-request)
  139. "Monkey patch `auto-complete' internal function to request
  140. help documentation asynchronously."
  141. (ein:ac-request-document-for-selected-candidate))
  142. (defadvice ac-previous (after ein:ac-previous-request)
  143. "Monkey patch `auto-complete' internal function to request
  144. help documentation asynchronously."
  145. (ein:ac-request-document-for-selected-candidate))
  146. (defadvice ac-update (after ein:ac-update-request)
  147. "Monkey patch `auto-complete' internal function to request help
  148. documentation asynchronously. This will request info for the
  149. first candidate when the `ac-menu' pops up."
  150. (ein:ac-request-document-for-selected-candidate))
  151. ;;; Setup
  152. (defun ein:ac-superpack ()
  153. "Enable richer auto-completion.
  154. * Enable auto-completion help by monkey patching `ac-next'/`ac-previous'"
  155. (interactive)
  156. (ad-enable-advice 'ac-next 'after 'ein:ac-next-request)
  157. (ad-enable-advice 'ac-previous 'after 'ein:ac-previous-request)
  158. (ad-enable-advice 'ac-update 'after 'ein:ac-update-request)
  159. (ad-activate 'ac-next)
  160. (ad-activate 'ac-previous)
  161. (ad-activate 'ac-update))
  162. (defun ein:ac-setup ()
  163. "Call this function from mode hook (see `ein:ac-config')."
  164. (setq ac-sources (append '(ac-source-ein-async) ein:ac-sources)))
  165. (defun ein:ac-setup-maybe ()
  166. "Setup `ac-sources' for mumamo.
  167. .. note:: Setting `ein:notebook-mumamo-mode-hook' does not work
  168. because `ac-sources' in `ein:notebook-mumamo-mode'-enabled
  169. buffer is *chunk local*, rather than buffer local.
  170. Making `ac-sources' permanent-local also addresses issue of
  171. MuMaMo discarding `ac-sources'. However, it effects to entire
  172. Emacs setting. So this is not the right way to do it.
  173. Using `mumamo-make-variable-buffer-permanent' (i.e., adding
  174. `ac-sources' to `mumamo-per-buffer-local-vars' or
  175. `mumamo-per-main-major-local-vars') is also not appropriate.
  176. Adding `ac-sources' to them makes it impossible to different
  177. `ac-sources' between chunks, which is good for EIN but may not
  178. for other package."
  179. (and ein:%notebook%
  180. (ein:eval-if-bound 'ein:notebook-mumamo-mode)
  181. (eql major-mode ein:mumamo-codecell-mode)
  182. (ein:ac-setup)))
  183. (defun ein:ac-config (&optional superpack)
  184. "Install auto-complete-mode for notebook modes.
  185. Specifying non-`nil' to SUPERPACK enables richer auto-completion
  186. \(see `ein:ac-superpack')."
  187. (add-hook 'after-change-major-mode-hook 'ein:ac-setup-maybe)
  188. (add-hook 'ein:notebook-mode-hook 'ein:ac-setup)
  189. (when superpack
  190. (ein:ac-superpack)))
  191. (defvar ein:ac-config-once-called nil)
  192. (defun ein:ac-config-once (&optional superpack)
  193. (unless ein:ac-config-once-called
  194. (setq ein:ac-config-once-called t)
  195. (ein:ac-config superpack)))
  196. (provide 'ein-ac)
  197. ;;; ein-ac.el ends here