1 ;;; geiser-completion.el -- tab completion
3 ;; Copyright (C) 2009, 2010, 2011 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 Feb 09, 2009 22:21
14 (require 'geiser-impl
)
15 (require 'geiser-eval
)
17 (require 'geiser-syntax
)
18 (require 'geiser-base
)
25 (defvar geiser-completion--minibuffer-map
26 (let ((map (make-keymap)))
27 (set-keymap-parent map minibuffer-local-completion-map
)
28 (define-key map
"?" 'self-insert-command
)
31 (defvar geiser-completion--module-minibuffer-map
32 (let ((map (make-keymap)))
33 (set-keymap-parent map minibuffer-local-completion-map
)
34 (define-key map
" " 'self-insert-command
)
35 (define-key map
"?" 'self-insert-command
)
39 ;;; Completion functionality:
41 (defvar geiser-completion--binding-forms nil
)
42 (geiser-impl--register-local-variable
43 'geiser-completion--binding-forms
'binding-forms nil
44 "A list of forms introducing local bindings, a la let or lambda.")
46 (defvar geiser-completion--binding-forms
* nil
)
47 (geiser-impl--register-local-variable
48 'geiser-completion--binding-forms
* 'binding-forms
* nil
49 "A list of forms introducing nested local bindings, a la let*.")
51 (defsubst geiser-completion--locals
()
52 (geiser-syntax--locals-around-point geiser-completion--binding-forms
53 geiser-completion--binding-forms
*))
55 (defun geiser-completion--symbol-list (prefix)
57 (append (all-completions prefix
(geiser-completion--locals))
58 (geiser-eval--send/result
`(:eval
(:ge completions
,prefix
))))))
60 (defsubst geiser-completion--module-list
(prefix)
61 (geiser-eval--send/result
`(:eval
(:ge module-completions
,prefix
))))
63 (defvar geiser-completion--symbol-list-func
64 (completion-table-dynamic 'geiser-completion--symbol-list
))
66 (defvar geiser-completion--module-list-func
67 (completion-table-dynamic 'geiser-completion--module-list
))
69 (defun geiser-completion--complete (prefix modules
)
70 (if modules
(geiser-completion--module-list prefix
)
71 (geiser-completion--symbol-list prefix
)))
73 (defvar geiser-completion--symbol-history nil
)
75 (defun geiser-completion--read-symbol (prompt &optional default history
)
76 (let ((minibuffer-local-completion-map geiser-completion--minibuffer-map
))
77 (make-symbol (completing-read prompt
78 geiser-completion--symbol-list-func
81 geiser-completion--symbol-history
)
82 (or default
(geiser--symbol-at-point))))))
84 (defvar geiser-completion--module-history nil
)
86 (defun geiser-completion--read-module (&optional prompt default history
)
87 (let ((minibuffer-local-completion-map
88 geiser-completion--module-minibuffer-map
))
89 (completing-read (or prompt
"Module name: ")
90 geiser-completion--module-list-func
92 (or history geiser-completion--module-history
)
95 (defvar geiser-completion--symbol-begin-function nil
)
97 (defun geiser-completion--def-symbol-begin (module)
98 (save-excursion (skip-syntax-backward "^-()>") (point)))
100 (geiser-impl--register-local-method
101 'geiser-completion--symbol-begin-function
'find-symbol-begin
102 'geiser-completion--def-symbol-begin
103 "An optional function finding the position of the beginning of
104 the identifier around point. Takes a boolean, indicating whether
105 we're looking for a module name.")
107 (defun geiser-completion--symbol-begin (module)
108 (funcall geiser-completion--symbol-begin-function module
))
110 (defun geiser-completion--module-at-point ()
112 (goto-char (geiser-completion--symbol-begin t
))
113 (ignore-errors (thing-at-point 'sexp
))))
115 (defsubst geiser-completion--prefix
(module)
116 (buffer-substring-no-properties (geiser-completion--symbol-begin module
)
119 (defsubst geiser-completion--prefix-end
(beg mod
)
120 (unless (or (eq beg
(point-max))
121 (member (char-syntax (char-after beg
))
122 (if mod
'(?
\" ?\
)) '(?
\" ?\
( ?\
)))))
128 (when (>= (point) pos
)
132 (defun geiser-completion--thing-at-point (module &optional predicate
)
133 (with-syntax-table scheme-mode-syntax-table
134 (let* ((beg (geiser-completion--symbol-begin module
))
135 (end (or (geiser-completion--prefix-end beg module
) beg
))
136 (prefix (and (> end beg
) (buffer-substring-no-properties beg end
)))
138 (if (string-match "\\([^-]+\\)-" prefix
)
139 (match-string 1 prefix
)
141 (cmps (and prefix
(geiser-completion--complete prefix module
))))
142 (and cmps
(list beg end cmps
)))))
144 (defun geiser-completion--for-symbol (&optional predicate
)
145 (geiser-completion--thing-at-point nil predicate
))
147 (defun geiser-completion--for-module (&optional predicate
)
148 (geiser-completion--thing-at-point t predicate
))
150 (defun geiser-completion--setup (enable)
151 (set (make-local-variable 'completion-at-point-functions
)
153 '(geiser-completion--for-symbol geiser-completion--for-module
)
154 (default-value 'completion-at-point-functions
))))
156 (defun geiser-completion--complete-module ()
157 "Complete module name at point."
159 (let ((completion-at-point-functions '(geiser-completion--for-module)))
160 (call-interactively 'completion-at-point
)))
165 (make-variable-buffer-local
166 (defvar geiser-smart-tab-mode-string
" SmartTab"
167 "Modeline indicator for geiser-smart-tab-mode"))
169 (define-minor-mode geiser-smart-tab-mode
170 "Toggle smart tab mode.
171 With no argument, this command toggles the mode.
172 Non-null prefix argument turns on the mode.
173 Null prefix argument turns off the mode.
175 When this mode is enable, TAB will indent if at point is at
176 beginning of line or after a white space or closing parenthesis,
177 and will try completing symbol at point otherwise."
179 :lighter geiser-smart-tab-mode-string
181 (set (make-local-variable 'tab-always-indent
)
182 (if geiser-smart-tab-mode
184 (default-value 'tab-always-indent
))))
187 (provide 'geiser-completion
)