Do not set geiser last-prompt-end beyond of point-max
[geiser.git] / elisp / geiser-company.el
blob368afb35b528ec52d819656217be7474f8885b47
1 ;; geiser-company.el -- integration with company-mode
3 ;; Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2016 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)
18 (require 'geiser-doc)
20 (eval-when-compile (require 'cl))
23 ;;; Helpers:
25 (make-variable-buffer-local
26 (defvar geiser-company--enabled-flag nil))
28 (make-variable-buffer-local
29 (defvar geiser-company--autodoc-flag nil))
31 (make-variable-buffer-local
32 (defvar geiser-company--completions nil))
34 (defun geiser-company--candidates (prefix)
35 (and (equal prefix (car geiser-company--completions))
36 (cdr geiser-company--completions)))
38 (defun geiser-company--doc (id)
39 (ignore-errors
40 (when (not (geiser-autodoc--inhibit))
41 (let ((help (geiser-autodoc--autodoc `((,id 0)))))
42 (and help (substring-no-properties help))))))
44 (defun geiser-company--doc-buffer (id)
45 (let* ((impl geiser-impl--implementation)
46 (module (geiser-doc-module (geiser-eval--get-module) impl))
47 (symbol (make-symbol id))
48 (ds (geiser-doc--get-docstring symbol module)))
49 (if (or (not ds) (not (listp ds)))
50 (message "No documentation available for '%s'" symbol)
51 (with-current-buffer (get-buffer-create "*company-documentation*")
52 (geiser-doc--render-docstring ds symbol module impl)
53 (current-buffer)))))
55 (defun geiser-company--location (id)
56 (ignore-errors
57 (when (not (geiser-autodoc--inhibit))
58 (let ((id (make-symbol id)))
59 (condition-case nil
60 (geiser-edit-module id 'noselect)
61 (error (geiser-edit-symbol id 'noselect)))))))
63 (defun geiser-company--prefix-at-point ()
64 (ignore-errors
65 (when (and (not (geiser-autodoc--inhibit)) geiser-company--enabled-flag)
66 (if (nth 8 (syntax-ppss)) 'stop
67 (let* ((prefix (and (looking-at-p "\\_>")
68 (geiser-completion--prefix nil)))
69 (cmps1 (and prefix
70 (geiser-completion--complete prefix nil)))
71 (cmps2 (and prefix
72 (geiser-completion--complete prefix t)))
73 (mprefix (and (not cmps1) (not cmps2)
74 (geiser-completion--prefix t)))
75 (cmps3 (and mprefix (geiser-completion--complete mprefix t)))
76 (cmps (or cmps3 (append cmps1 cmps2)))
77 (prefix (or mprefix prefix)))
78 (setq geiser-company--completions (cons prefix cmps))
79 prefix)))))
82 ;;; Activation
84 (defun geiser-company--setup (enable)
85 (setq geiser-company--enabled-flag enable)
86 (when (fboundp 'geiser-company--setup-company)
87 (geiser-company--setup-company enable)))
89 (defun geiser-company--inhibit-autodoc (ignored)
90 (when (setq geiser-company--autodoc-flag geiser-autodoc-mode)
91 (geiser-autodoc-mode -1)))
93 (defun geiser-company--restore-autodoc (&optional ignored)
94 (when geiser-company--autodoc-flag
95 (geiser-autodoc-mode 1)))
98 ;;; Company activation
100 (declare-function company-begin-backend "ext:company")
101 (declare-function company-cancel "ext:company")
102 (declare-function company-mode "ext:company")
103 (defvar company-backends)
104 (defvar company-active-map)
105 (eval-after-load "company"
106 '(progn
107 (defun geiser-company-backend (command &optional arg &rest ignored)
108 "A `company-mode' completion back-end for `geiser-mode'."
109 (interactive (list 'interactive))
110 (case command
111 ('interactive (company-begin-backend 'geiser-company-backend))
112 ('prefix (geiser-company--prefix-at-point))
113 ('candidates (geiser-company--candidates arg))
114 ('meta (geiser-company--doc arg))
115 ('doc-buffer (geiser-company--doc-buffer arg))
116 ('location (geiser-company--location arg))
117 ('sorted t)))
118 (defun geiser-company--setup-company (enable)
119 (when enable
120 (set (make-local-variable 'company-backends)
121 (add-to-list 'company-backends 'geiser-company-backend)))
122 (company-mode (if enable 1 -1)))
123 (add-hook 'company-completion-finished-hook
124 'geiser-company--restore-autodoc)
125 (add-hook 'company-completion-cancelled-hook
126 'geiser-company--restore-autodoc)
127 (add-hook 'company-completion-started-hook
128 'geiser-company--inhibit-autodoc)
129 (define-key company-active-map (kbd "M-`")
130 (lambda ()
131 (interactive)
132 (company-cancel)
133 (call-interactively 'geiser-completion--complete-module)))))
137 (provide 'geiser-company)