blob: c04cc3437c0ca6157863695f2a65fd86a781791a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
;;; geiser-completion.el -- tab completion
;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
;; Start date: Mon Feb 09, 2009 22:21
(require 'geiser-impl)
(require 'geiser-eval)
(require 'geiser-log)
(require 'geiser-syntax)
(require 'geiser-base)
(require 'minibuffer)
;;; Minibuffer maps:
(defvar geiser-completion--minibuffer-map
(let ((map (make-keymap)))
(set-keymap-parent map minibuffer-local-completion-map)
(define-key map "?" 'self-insert-command)
map))
(defvar geiser-completion--module-minibuffer-map
(let ((map (make-keymap)))
(set-keymap-parent map minibuffer-local-completion-map)
(define-key map " " 'self-insert-command)
(define-key map "?" 'self-insert-command)
map))
;;; Completion functionality:
(defvar geiser-completion--binding-forms nil)
(geiser-impl--register-local-variable
'geiser-completion--binding-forms 'binding-forms nil
"A list of forms introducing local bindings, a la let or lambda.")
(defvar geiser-completion--binding-forms* nil)
(geiser-impl--register-local-variable
'geiser-completion--binding-forms* 'binding-forms* nil
"A list of forms introducing nested local bindings, a la let*.")
(defsubst geiser-completion--locals ()
(mapcar (lambda (s) (and (symbolp s) (symbol-name s)))
(geiser-syntax--locals-around-point
geiser-completion--binding-forms
geiser-completion--binding-forms*)))
(defun geiser-completion--symbol-list (prefix)
(geiser--del-dups
(append (all-completions prefix (geiser-completion--locals))
(geiser-eval--send/result `(:eval (:ge completions ,prefix))))))
(defsubst geiser-completion--module-list (prefix)
(geiser-eval--send/result `(:eval (:ge module-completions ,prefix))))
(defvar geiser-completion--symbol-list-func
(completion-table-dynamic 'geiser-completion--symbol-list))
(defvar geiser-completion--module-list-func
(completion-table-dynamic 'geiser-completion--module-list))
(defun geiser-completion--complete (prefix modules)
(if modules (geiser-completion--module-list prefix)
(geiser-completion--symbol-list prefix)))
(defvar geiser-completion--symbol-history nil)
(defun geiser-completion--read-symbol (prompt &optional default history)
(let ((minibuffer-local-completion-map geiser-completion--minibuffer-map))
(make-symbol (completing-read prompt
geiser-completion--symbol-list-func
nil nil nil
(or history
geiser-completion--symbol-history)
(or default (symbol-at-point))))))
(defvar geiser-completion--module-history nil)
(defun geiser-completion--read-module (&optional prompt default history)
(let ((minibuffer-local-completion-map
geiser-completion--module-minibuffer-map))
(completing-read (or prompt "Module name: ")
geiser-completion--module-list-func
nil nil nil
(or history geiser-completion--module-history)
default)))
(defvar geiser-completion--symbol-begin-function nil)
(defun geiser-completion--def-symbol-begin (module)
(save-excursion (skip-syntax-backward "^-()>") (point)))
(geiser-impl--register-local-method
'geiser-completion--symbol-begin-function 'find-symbol-begin
'geiser-completion--def-symbol-begin
"An optional function finding the position of the beginning of
the identifier around point. Takes a boolean, indicating whether
we're looking for a module name.")
(defun geiser-completion--symbol-begin (module)
(funcall geiser-completion--symbol-begin-function module))
(defun geiser-completion--module-at-point ()
(save-excursion
(goto-char (geiser-completion--symbol-begin t))
(ignore-errors (thing-at-point 'sexp))))
(defsubst geiser-completion--prefix (module)
(buffer-substring-no-properties (geiser-completion--symbol-begin module)
(point)))
(defsubst geiser-completion--prefix-end (beg)
(unless (or (eq beg (point-max))
(member (char-syntax (char-after beg)) '(?\" ?\( ?\))))
(let ((pos (point)))
(condition-case nil
(save-excursion
(goto-char beg)
(forward-sexp 1)
(when (>= (point) pos)
(point)))
(scan-error pos)))))
(defun geiser-completion--thing-at-point (module &optional predicate)
(with-syntax-table scheme-mode-syntax-table
(let* ((beg (geiser-completion--symbol-begin module))
(end (or (geiser-completion--prefix-end beg) beg))
(prefix (and (> end beg) (buffer-substring-no-properties beg end)))
(prefix (and prefix
(if (string-match "\\([^-]+\\)-" prefix)
(match-string 1 prefix)
prefix)))
(cmps (and prefix (geiser-completion--complete prefix module))))
(and cmps (list beg end cmps)))))
(defun geiser-completion--for-symbol (&optional predicate)
(geiser-completion--thing-at-point nil predicate))
(defun geiser-completion--for-module (&optional predicate)
(geiser-completion--thing-at-point t predicate))
(defun geiser-completion--setup (enable)
(set (make-local-variable 'completion-at-point-functions)
(if enable
'(geiser-completion--for-symbol geiser-completion--for-module)
(default-value 'completion-at-point-functions))))
(defun geiser-completion--complete-module ()
"Complete module name at point."
(interactive)
(let ((completion-at-point-functions '(geiser-completion--for-module)))
(call-interactively 'completion-at-point)))
;;; Smart tab mode:
(make-variable-buffer-local
(defvar geiser-smart-tab-mode-string " SmartTab"
"Modeline indicator for geiser-smart-tab-mode"))
(define-minor-mode geiser-smart-tab-mode
"Toggle smart tab mode.
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.
When this mode is enable, TAB will indent if at point is at
beginning of line or after a white space or closing parenthesis,
and will try completing symbol at point otherwise."
:init-value nil
:lighter geiser-smart-tab-mode-string
:group 'geiser-mode
(set (make-local-variable 'tab-always-indent)
(if geiser-smart-tab-mode
'complete
(default-value 'tab-always-indent))))
(provide 'geiser-completion)
|