Using implementation-specific keywords also in REPL
[geiser.git] / elisp / geiser-company.el
blob476cf39ebef2a4124ebddbf481fda25630ba622f
1 ;; geiser-company.el -- integration with company-mode
3 ;; Copyright (C) 2009, 2010, 2011, 2012 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 (let ((help (geiser-autodoc--autodoc `((,id 0)))))
38 (and help (substring-no-properties help)))))
40 (defsubst geiser-company--doc-buffer (id) nil)
42 (defun geiser-company--location (id)
43 (ignore-errors
44 (let ((id (make-symbol id)))
45 (condition-case nil
46 (geiser-edit-module id 'noselect)
47 (error (geiser-edit-symbol id 'noselect))))))
49 (defun geiser-company--prefix-at-point ()
50 (when geiser-company--enabled-flag
51 (if (nth 8 (syntax-ppss)) 'stop
52 (let* ((prefix (and (looking-at-p "\\_>")
53 (geiser-completion--prefix nil)))
54 (cmps1 (and prefix
55 (geiser-completion--complete prefix nil)))
56 (cmps2 (and prefix
57 (geiser-completion--complete prefix t)))
58 (mprefix (and (not cmps1) (not cmps2)
59 (geiser-completion--prefix t)))
60 (cmps3 (and mprefix (geiser-completion--complete mprefix t)))
61 (cmps (or cmps3 (append cmps1 cmps2)))
62 (prefix (or mprefix prefix)))
63 (setq geiser-company--completions (cons prefix cmps))
64 prefix))))
67 ;;; Activation
69 (defun geiser-company--setup (enable)
70 (setq geiser-company--enabled-flag enable)
71 (when (fboundp 'geiser-company--setup-company)
72 (geiser-company--setup-company enable)))
74 (defun geiser-company--inhibit-autodoc (ignored)
75 (when (setq geiser-company--autodoc-flag geiser-autodoc-mode)
76 (geiser-autodoc-mode -1)))
78 (defun geiser-company--restore-autodoc (&optional ignored)
79 (when geiser-company--autodoc-flag
80 (geiser-autodoc-mode 1)))
83 ;;; Company activation
85 (eval-after-load "company"
86 '(progn
87 (defun geiser-company-backend (command &optional arg &rest ignored)
88 "A `company-mode' completion back-end for `geiser-mode'."
89 (interactive (list 'interactive))
90 (case command
91 ('interactive (company-begin-backend 'geiser-company-backend))
92 ('prefix (geiser-company--prefix-at-point))
93 ('candidates (geiser-company--candidates arg))
94 ('meta (geiser-company--doc arg))
95 ('doc-buffer (geiser-company--doc-buffer arg))
96 ('location (geiser-company--location arg))
97 ('sorted t)))
98 (defun geiser-company--setup-company (enable)
99 (set (make-local-variable 'company-default-lighter) "/C")
100 (set (make-local-variable 'company-echo-delay) 0.01)
101 (set (make-local-variable 'company-backends)
102 (and enable '(geiser-company-backend)))
103 (company-mode (if enable 1 -1)))
104 (add-hook 'company-completion-finished-hook
105 'geiser-company--restore-autodoc)
106 (add-hook 'company-completion-cancelled-hook
107 'geiser-company--restore-autodoc)
108 (add-hook 'company-completion-started-hook
109 'geiser-company--inhibit-autodoc)
110 (define-key company-active-map (kbd "M-`")
111 (lambda ()
112 (interactive)
113 (company-cancel)
114 (call-interactively 'geiser-completion--complete-module)))))
118 (provide 'geiser-company)