comparison lisp/progmodes/sym-comp.el @ 104760:1d5c75e6a226

(displayed-completions): Remove. (symbol-complete): Use minibuffer-complete.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Mon, 31 Aug 2009 05:47:06 +0000
parents a9dc0e7c3f2b
children aef9a4af6024
comparison
equal deleted inserted replaced
104759:42d63191b7cc 104760:1d5c75e6a226
55 (current-word))) 55 (current-word)))
56 56
57 (defvar symbol-completion-symbol-function 'symbol-completion-symbol 57 (defvar symbol-completion-symbol-function 'symbol-completion-symbol
58 "Function to return a partial symbol before point for completion. 58 "Function to return a partial symbol before point for completion.
59 The value it returns should be a string (or nil). 59 The value it returns should be a string (or nil).
60 Major modes may set this locally if the default isn't appropriate.") 60 Major modes may set this locally if the default isn't appropriate.
61
62 Beware: the length of the string STR returned need to be equal to the length
63 of text before point that's subject to completion. Typically, this amounts
64 to saying that STR is equal to
65 \(buffer-substring (- (point) (length STR)) (point)).")
61 66
62 (defvar symbol-completion-completions-function nil 67 (defvar symbol-completion-completions-function nil
63 "Function to return possible symbol completions. 68 "Function to return possible symbol completions.
64 It takes an argument which is the string to be completed and 69 It takes an argument which is the string to be completed and
65 returns a value suitable for the second argument of 70 returns a value suitable for the second argument of
95 The predicate being used for selecting completions (from 100 The predicate being used for selecting completions (from
96 `symbol-completion-predicate-function') is available 101 `symbol-completion-predicate-function') is available
97 dynamically-bound as `symbol-completion-predicate' in case the 102 dynamically-bound as `symbol-completion-predicate' in case the
98 transform needs it.") 103 transform needs it.")
99 104
100 (defvar displayed-completions) 105 (defvar symbol-completion-predicate)
101 106
102 ;;;###autoload 107 ;;;###autoload
103 (defun symbol-complete (&optional predicate) 108 (defun symbol-complete (&optional predicate)
104 "Perform completion of the symbol preceding point. 109 "Perform completion of the symbol preceding point.
105 This is done in a way appropriate to the current major mode, 110 This is done in a way appropriate to the current major mode,
117 `symbol-completion-transform-function' are also consulted." 122 `symbol-completion-transform-function' are also consulted."
118 (interactive) 123 (interactive)
119 ;; Fixme: Punt to `complete-symbol' in this case? 124 ;; Fixme: Punt to `complete-symbol' in this case?
120 (unless (functionp symbol-completion-completions-function) 125 (unless (functionp symbol-completion-completions-function)
121 (error "symbol-completion-completions-function not defined")) 126 (error "symbol-completion-completions-function not defined"))
122 (let ((window (get-buffer-window "*Completions*"))) 127 (let* ((pattern (or (funcall symbol-completion-symbol-function)
123 (let* ((pattern (or (funcall symbol-completion-symbol-function) 128 (error "No preceding symbol to complete")))
124 (error "No preceding symbol to complete"))) 129 ;; FIXME: We assume below that `pattern' holds the text just
125 (predicate (or predicate 130 ;; before point. This is a problem in the way
126 (if symbol-completion-predicate-function 131 ;; symbol-completion-symbol-function was defined.
127 (funcall symbol-completion-predicate-function 132 (predicate (or predicate
128 (- (point) (length pattern)) 133 (if symbol-completion-predicate-function
129 (point))))) 134 (funcall symbol-completion-predicate-function
130 (completions (funcall symbol-completion-completions-function 135 (- (point) (length pattern))
131 pattern)) 136 (point)))))
132 (completion (try-completion pattern completions predicate))) 137 (completions (funcall symbol-completion-completions-function
133 ;; If this command was repeated, and there's a fresh completion 138 pattern))
134 ;; window with a live buffer and a displayed completion list 139 ;; In case the transform needs to access it.
135 ;; matching the current completions, then scroll the window. 140 (symbol-completion-predicate predicate)
136 (unless (and (eq last-command this-command) 141 (completion-annotate-function
137 window (window-live-p window) (window-buffer window) 142 (if (functionp symbol-completion-transform-function)
138 (buffer-name (window-buffer window)) 143 (lambda (str)
139 (with-current-buffer (window-buffer window) 144 (car-safe (cdr-safe
140 (if (equal displayed-completions 145 (funcall symbol-completion-transform-function
141 (all-completions pattern completions predicate)) 146 str))))))
142 (progn 147 (minibuffer-completion-table completions)
143 (if (pos-visible-in-window-p (point-max) window) 148 (minibuffer-completion-predicate predicate)
144 (set-window-start window (point-min)) 149 (ol (make-overlay (- (point) (length pattern)) (point) nil nil t)))
145 (save-selected-window 150 (overlay-put ol 'field 'sym-comp)
146 (select-window window) 151 (unwind-protect
147 (scroll-up))) 152 (call-interactively 'minibuffer-complete)
148 t)))) 153 (delete-overlay ol))))
149 ;; Otherwise, do completion.
150 (cond ((eq completion t))
151 ((null completion)
152 (message "Can't find completion for \"%s\"" pattern)
153 (ding))
154 ((not (string= pattern completion))
155 (delete-region (- (point) (length pattern)) (point))
156 (insert completion))
157 (t
158 (message "Making completion list...")
159 (let* ((list (all-completions pattern completions predicate))
160 ;; In case the transform needs to access it.
161 (symbol-completion-predicate predicate)
162 ;; Copy since list is side-effected by sorting.
163 (copy (copy-sequence list)))
164 (setq list (sort list 'string<))
165 (if (functionp symbol-completion-transform-function)
166 (setq list
167 (mapcar (funcall
168 symbol-completion-transform-function)
169 list)))
170 (with-output-to-temp-buffer "*Completions*"
171 (condition-case ()
172 (display-completion-list list pattern) ; Emacs 22
173 (error (display-completion-list list))))
174 ;; Record the list for determining whether to scroll
175 ;; (above).
176 (with-current-buffer "*Completions*"
177 (set (make-local-variable 'displayed-completions) copy)))
178 (message "Making completion list...%s" "done")))))))
179 154
180 (eval-when-compile (require 'hippie-exp)) 155 (eval-when-compile (require 'hippie-exp))
181 156
182 ;;;###autoload 157 ;;;###autoload
183 (defun symbol-completion-try-complete (old) 158 (defun symbol-completion-try-complete (old)
244 ;; Maybe a `let' varlist or something. 219 ;; Maybe a `let' varlist or something.
245 nil 220 nil
246 ;; Else, we assume that a function name is expected. 221 ;; Else, we assume that a function name is expected.
247 'fboundp)))) 222 'fboundp))))
248 223
249 (defvar symbol-completion-predicate)
250
251 (defun lisp-symbol-completion-transform () 224 (defun lisp-symbol-completion-transform ()
252 "`symbol-completion-transform-function' for Lisp." 225 "`symbol-completion-transform-function' for Lisp."
253 (lambda (elt) 226 (lambda (elt)
254 (if (and (not (eq 'fboundp symbol-completion-predicate)) 227 (if (and (not (eq 'fboundp symbol-completion-predicate))
255 (fboundp (intern elt))) 228 (fboundp (intern elt)))