Disabling company calls while the REPL is working
[geiser.git] / elisp / geiser-company.el
blob90b1cbb9bb94e58b6751aa40a563471bae2bcaf4
1 ;; geiser-company.el -- integration with company-mode
3 ;; Copyright (C) 2009, 2010, 2011, 2012, 2013 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Mon Aug 24, 2009 12:44
14 (require 'geiser-autodoc)
15 (require 'geiser-completion)
16 (require 'geiser-edit)
17 (require 'geiser-base)
20 ;;; Helpers:
22 (make-variable-buffer-local
23 (defvar geiser-company--enabled-flag nil))
25 (make-variable-buffer-local
26 (defvar geiser-company--autodoc-flag nil))
28 (make-variable-buffer-local
29 (defvar geiser-company--completions nil))
31 (defun geiser-company--candidates (prefix)
32 (and (equal prefix (car geiser-company--completions))
33 (cdr geiser-company--completions)))
35 (defun geiser-company--doc (id)
36 (ignore-errors
37 (when (not (geiser-autodoc--inhibit))
38 (let ((help (geiser-autodoc--autodoc `((,id 0)))))
39 (and help (substring-no-properties help))))))
41 (defsubst geiser-company--doc-buffer (id) nil)
43 (defun geiser-company--location (id)
44 (ignore-errors
45 (when (not (geiser-autodoc--inhibit))
46 (let ((id (make-symbol id)))
47 (condition-case nil
48 (geiser-edit-module id 'noselect)
49 (error (geiser-edit-symbol id 'noselect)))))))
51 (defun geiser-company--prefix-at-point ()
52 (when (and (not (geiser-autodoc--inhibit)) geiser-company--enabled-flag)
53 (if (nth 8 (syntax-ppss)) 'stop
54 (let* ((prefix (and (looking-at-p "\\_>")
55 (geiser-completion--prefix nil)))
56 (cmps1 (and prefix
57 (geiser-completion--complete prefix nil)))
58 (cmps2 (and prefix
59 (geiser-completion--complete prefix t)))
60 (mprefix (and (not cmps1) (not cmps2)
61 (geiser-completion--prefix t)))
62 (cmps3 (and mprefix (geiser-completion--complete mprefix t)))
63 (cmps (or cmps3 (append cmps1 cmps2)))
64 (prefix (or mprefix prefix)))
65 (setq geiser-company--completions (cons prefix cmps))
66 prefix))))
69 ;;; Activation
71 (defun geiser-company--setup (enable)
72 (setq geiser-company--enabled-flag enable)
73 (when (fboundp 'geiser-company--setup-company)
74 (geiser-company--setup-company enable)))
76 (defun geiser-company--inhibit-autodoc (ignored)
77 (when (setq geiser-company--autodoc-flag geiser-autodoc-mode)
78 (geiser-autodoc-mode -1)))
80 (defun geiser-company--restore-autodoc (&optional ignored)
81 (when geiser-company--autodoc-flag
82 (geiser-autodoc-mode 1)))
85 ;;; Company activation
87 (eval-after-load "company"
88 '(progn
89 (defun geiser-company-backend (command &optional arg &rest ignored)
90 "A `company-mode' completion back-end for `geiser-mode'."
91 (interactive (list 'interactive))
92 (case command
93 ('interactive (company-begin-backend 'geiser-company-backend))
94 ('prefix (geiser-company--prefix-at-point))
95 ('candidates (geiser-company--candidates arg))
96 ('meta (geiser-company--doc arg))
97 ('doc-buffer (geiser-company--doc-buffer arg))
98 ('location (geiser-company--location arg))
99 ('sorted t)))
100 (defun geiser-company--setup-company (enable)
101 (set (make-local-variable 'company-default-lighter) "/C")
102 (set (make-local-variable 'company-echo-delay) 0.01)
103 (set (make-local-variable 'company-backends)
104 (and enable '(geiser-company-backend)))
105 (company-mode (if enable 1 -1)))
106 (add-hook 'company-completion-finished-hook
107 'geiser-company--restore-autodoc)
108 (add-hook 'company-completion-cancelled-hook
109 'geiser-company--restore-autodoc)
110 (add-hook 'company-completion-started-hook
111 'geiser-company--inhibit-autodoc)
112 (define-key company-active-map (kbd "M-`")
113 (lambda ()
114 (interactive)
115 (company-cancel)
116 (call-interactively 'geiser-completion--complete-module)))))
120 (provide 'geiser-company)