ein-completer.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ;;; ein-completer.el --- Completion module
  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-completer.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-completer.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-completer.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (declare-function ac-cursor-on-diable-face-p "auto-complete")
  19. (eval-when-compile (require 'cl))
  20. (require 'ein-core)
  21. (require 'ein-log)
  22. (require 'ein-subpackages)
  23. (require 'ein-kernel)
  24. (defun ein:completer-choose ()
  25. (when (require 'auto-complete nil t)
  26. (require 'ein-ac))
  27. (cond
  28. ((and (or ein:use-auto-complete
  29. ein:use-auto-complete-superpack)
  30. (ein:eval-if-bound 'auto-complete-mode)
  31. (fboundp 'ein:completer-finish-completing-ac))
  32. #'ein:completer-finish-completing-ac)
  33. (t
  34. #'ein:completer-finish-completing-default)))
  35. (defun ein:completer-beginning (matched-text)
  36. (save-excursion
  37. (re-search-backward (concat matched-text "\\="))))
  38. (defun ein:completer-finish-completing (args content -metadata-not-used-)
  39. (ein:log 'debug "COMPLETER-FINISH-COMPLETING: content=%S" content)
  40. (let ((matched-text (plist-get content :matched_text))
  41. (matches (plist-get content :matches))
  42. (completer (ein:completer-choose)))
  43. (ein:log 'debug "COMPLETER-FINISH-COMPLETING: completer=%s" completer)
  44. (apply completer matched-text matches args)))
  45. (defun ein:completer-finish-completing-default (matched-text matches
  46. &rest -ignore-)
  47. (let* ((end (point))
  48. (beg (ein:completer-beginning matched-text))
  49. (word (if (and beg matches)
  50. (completing-read "Complete: " matches
  51. nil nil matched-text))))
  52. (when word
  53. (delete-region beg end)
  54. (insert word))))
  55. (defun* ein:completer-complete
  56. (kernel &rest args &key callbacks &allow-other-keys)
  57. "Start completion for the code at point.
  58. .. It sends `:complete_request' to KERNEL.
  59. CALLBACKS is passed to `ein:kernel-complete'.
  60. If you specify CALLBACKS explicitly (i.e., you are not using
  61. `ein:completer-finish-completing'), keyword argument will be
  62. ignored. Otherwise, ARGS are passed as additional arguments
  63. to completer callback functions. ARGS **must** be keyword
  64. arguments.
  65. EXPAND keyword argument is supported by
  66. `ein:completer-finish-completing-ac'. When it is specified,
  67. it overrides `ac-expand-on-auto-complete' when calling
  68. `auto-complete'."
  69. (interactive (list (ein:get-kernel)))
  70. (unless callbacks
  71. (setq callbacks
  72. (list :complete_reply
  73. (cons #'ein:completer-finish-completing
  74. (ein:plist-exclude args '(:callbacks))))))
  75. (ein:kernel-complete kernel
  76. (thing-at-point 'line)
  77. (current-column)
  78. callbacks))
  79. (defun ein:completer-dot-complete ()
  80. "Insert dot and request completion."
  81. (interactive)
  82. (insert ".")
  83. (ein:and-let* ((kernel (ein:get-kernel))
  84. ((not (ac-cursor-on-diable-face-p)))
  85. ((ein:kernel-live-p kernel)))
  86. (ein:completer-complete kernel :expand nil)))
  87. (defcustom ein:complete-on-dot t
  88. "Start completion when inserting a dot. Note that
  89. `ein:use-auto-complete' (or `ein:use-auto-complete-superpack')
  90. must be `t' to enable this option. This variable has effect on
  91. notebook buffers and connected buffers."
  92. :type 'boolean
  93. :group 'ein)
  94. (defun ein:complete-on-dot-install (map &optional func)
  95. (if (and ein:complete-on-dot
  96. (featurep 'auto-complete)
  97. (or ein:use-auto-complete
  98. ein:use-auto-complete-superpack))
  99. (define-key map "." (or func #'ein:completer-dot-complete))
  100. (define-key map "." nil)))
  101. (provide 'ein-completer)
  102. ;;; ein-completer.el ends here