aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes
diff options
context:
space:
mode:
authorStefan Monnier2009-12-07 20:06:26 +0000
committerStefan Monnier2009-12-07 20:06:26 +0000
commit51ef56c47fc0b02b20f44b673f8b60350c03b4e1 (patch)
treef3c898b5885cfc9cdca8681966bd223d3ee19ff7 /lisp/progmodes
parent5e7a90229a1c32ded160a6d27f4ad9f3c66f60c3 (diff)
downloademacs-51ef56c47fc0b02b20f44b673f8b60350c03b4e1.tar.gz
emacs-51ef56c47fc0b02b20f44b673f8b60350c03b4e1.zip
* minibuffer.el (completion-at-point-functions): New var.
(completion-at-point): New command. * indent.el (indent-for-tab-command): Handle the new `complete' behavior. * progmodes/python.el (python-mode-map): Use completion-at-point. (python-completion-at-point): Rename from python-partial-symbol and adjust for use in completion-at-point-functions. (python-mode): Setup completion-at-point for Python completion. * emacs-lisp/lisp.el (lisp-completion-at-point): New function extracted from lisp-complete-symbol. (lisp-complete-symbol): Use it. * emacs-lisp/lisp-mode.el (emacs-lisp-mode): Use define-derived-mode, setup completion-at-point for Elisp completion. (emacs-lisp-mode-map, lisp-interaction-mode-map): Use completion-at-point. * ielm.el (ielm-map): Use completion-at-point. (inferior-emacs-lisp-mode): Setup completion-at-point for Elisp completion. * progmodes/sym-comp.el: Move to... * obsolete/sym-comp.el: Move from progmodes.
Diffstat (limited to 'lisp/progmodes')
-rw-r--r--lisp/progmodes/python.el23
-rw-r--r--lisp/progmodes/sym-comp.el230
2 files changed, 8 insertions, 245 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index eff599c77a5..c401cdfbf54 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -268,7 +268,7 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)."
268 (define-key map "\C-c\C-z" 'python-switch-to-python) 268 (define-key map "\C-c\C-z" 'python-switch-to-python)
269 (define-key map "\C-c\C-m" 'python-load-file) 269 (define-key map "\C-c\C-m" 'python-load-file)
270 (define-key map "\C-c\C-l" 'python-load-file) ; a la cmuscheme 270 (define-key map "\C-c\C-l" 'python-load-file) ; a la cmuscheme
271 (substitute-key-definition 'complete-symbol 'symbol-complete 271 (substitute-key-definition 'complete-symbol 'completion-at-point
272 map global-map) 272 map global-map)
273 (define-key map "\C-c\C-i" 'python-find-imports) 273 (define-key map "\C-c\C-i" 'python-find-imports)
274 (define-key map "\C-c\C-t" 'python-expand-template) 274 (define-key map "\C-c\C-t" 'python-expand-template)
@@ -319,7 +319,7 @@ Used for syntactic keywords. N is the match number (1, 2 or 3)."
319 "-" 319 "-"
320 ["Help on symbol" python-describe-symbol 320 ["Help on symbol" python-describe-symbol
321 :help "Use pydoc on symbol at point"] 321 :help "Use pydoc on symbol at point"]
322 ["Complete symbol" symbol-complete 322 ["Complete symbol" completion-at-point
323 :help "Complete (qualified) symbol before point"] 323 :help "Complete (qualified) symbol before point"]
324 ["Find function" python-find-function 324 ["Find function" python-find-function
325 :help "Try to find source definition of function at point"] 325 :help "Try to find source definition of function at point"]
@@ -2159,8 +2159,7 @@ Uses `python-imports' to load modules against which to complete."
2159 (delete-dups completions) 2159 (delete-dups completions)
2160 #'string<)))) 2160 #'string<))))
2161 2161
2162(defun python-partial-symbol () 2162(defun python-completion-at-point ()
2163 "Return the partial symbol before point (for completion)."
2164 (let ((end (point)) 2163 (let ((end (point))
2165 (start (save-excursion 2164 (start (save-excursion
2166 (and (re-search-backward 2165 (and (re-search-backward
@@ -2168,7 +2167,9 @@ Uses `python-imports' to load modules against which to complete."
2168 (group (1+ (regexp "[[:alnum:]._]"))) point) 2167 (group (1+ (regexp "[[:alnum:]._]"))) point)
2169 nil t) 2168 nil t)
2170 (match-beginning 1))))) 2169 (match-beginning 1)))))
2171 (if start (buffer-substring-no-properties start end)))) 2170 (when start
2171 (list start end
2172 (completion-table-dynamic 'python-symbol-completions)))))
2172 2173
2173;;;; FFAP support 2174;;;; FFAP support
2174 2175
@@ -2471,10 +2472,8 @@ with skeleton expansions for compound statement templates.
2471 (add-hook 'eldoc-mode-hook 2472 (add-hook 'eldoc-mode-hook
2472 (lambda () (run-python nil t)) ; need it running 2473 (lambda () (run-python nil t)) ; need it running
2473 nil t) 2474 nil t)
2474 (set (make-local-variable 'symbol-completion-symbol-function) 2475 (add-hook 'completion-at-point-functions
2475 'python-partial-symbol) 2476 'python-completion-at-point nil 'local)
2476 (set (make-local-variable 'symbol-completion-completions-function)
2477 'python-symbol-completions)
2478 ;; Fixme: should be in hideshow. This seems to be of limited use 2477 ;; Fixme: should be in hideshow. This seems to be of limited use
2479 ;; since it isn't (can't be) indentation-based. Also hide-level 2478 ;; since it isn't (can't be) indentation-based. Also hide-level
2480 ;; doesn't seem to work properly. 2479 ;; doesn't seem to work properly.
@@ -2488,12 +2487,6 @@ with skeleton expansions for compound statement templates.
2488 '((< '(backward-delete-char-untabify (min python-indent 2487 '((< '(backward-delete-char-untabify (min python-indent
2489 (current-column)))) 2488 (current-column))))
2490 (^ '(- (1+ (current-indentation)))))) 2489 (^ '(- (1+ (current-indentation))))))
2491 ;; Let's not mess with hippie-expand. Symbol-completion should rather be
2492 ;; bound to another key, since it has different performance requirements.
2493 ;; (if (featurep 'hippie-exp)
2494 ;; (set (make-local-variable 'hippie-expand-try-functions-list)
2495 ;; (cons 'symbol-completion-try-complete
2496 ;; hippie-expand-try-functions-list)))
2497 ;; Python defines TABs as being 8-char wide. 2490 ;; Python defines TABs as being 8-char wide.
2498 (set (make-local-variable 'tab-width) 8) 2491 (set (make-local-variable 'tab-width) 8)
2499 (unless font-lock-mode (font-lock-mode 1)) 2492 (unless font-lock-mode (font-lock-mode 1))
diff --git a/lisp/progmodes/sym-comp.el b/lisp/progmodes/sym-comp.el
deleted file mode 100644
index a0f572266bd..00000000000
--- a/lisp/progmodes/sym-comp.el
+++ /dev/null
@@ -1,230 +0,0 @@
1;;; sym-comp.el --- mode-dependent symbol completion
2
3;; Copyright (C) 2004, 2008, 2009 Free Software Foundation, Inc.
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: extensions
7;; URL: http://www.loveshack.ukfsn.org/emacs
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24;;; Commentary:
25
26;; This defines `symbol-complete', which is a generalization of the
27;; old `lisp-complete-symbol'. It provides the following hooks to
28;; allow major modes to set up completion appropriate for the mode:
29;; `symbol-completion-symbol-function',
30;; `symbol-completion-completions-function',
31;; `symbol-completion-predicate-function',
32;; `symbol-completion-transform-function'. Typically it is only
33;; necessary for a mode to set
34;; `symbol-completion-completions-function' locally and to bind
35;; `symbol-complete' appropriately.
36
37;; It's unfortunate that there doesn't seem to be a good way of
38;; combining this with `complete-symbol'.
39
40;; There is also `symbol-completion-try-complete', for use with
41;; Hippie-exp.
42
43;;; Code:
44
45;;;; Mode-dependent symbol completion.
46
47(defun symbol-completion-symbol ()
48 "Default `symbol-completion-symbol-function'.
49Uses `current-word' with the buffer narrowed to the part before
50point."
51 (save-restriction
52 ;; Narrow in case point is in the middle of a symbol -- we want
53 ;; just the preceeding part.
54 (narrow-to-region (point-min) (point))
55 (current-word)))
56
57(defvar symbol-completion-symbol-function 'symbol-completion-symbol
58 "Function to return a partial symbol before point for completion.
59The value it returns should be a string (or nil).
60Major modes may set this locally if the default isn't appropriate.
61
62Beware: the length of the string STR returned need to be equal to the length
63of text before point that's subject to completion. Typically, this amounts
64to saying that STR is equal to
65\(buffer-substring (- (point) (length STR)) (point)).")
66
67(defvar symbol-completion-completions-function nil
68 "Function to return possible symbol completions.
69It takes an argument which is the string to be completed and
70returns a value suitable for the second argument of
71`try-completion'. This value need not use the argument, i.e. it
72may be all possible completions, such as `obarray' in the case of
73Emacs Lisp.
74
75Major modes may set this locally to allow them to support
76`symbol-complete'. See also `symbol-completion-symbol-function',
77`symbol-completion-predicate-function' and
78`symbol-completion-transform-function'.")
79
80(defvar symbol-completion-predicate-function nil
81 "If non-nil, function to return a predicate for selecting symbol completions.
82The function gets two args, the positions of the beginning and
83end of the symbol to be completed.
84
85Major modes may set this locally if the default isn't
86appropriate. This is a function returning a predicate so that
87the predicate can be context-dependent, e.g. to select only
88function names if point is at a function call position. The
89function's args may be useful for determining the context.")
90
91(defvar symbol-completion-transform-function nil
92 "If non-nil, function to transform symbols in the symbol-completion buffer.
93E.g., for Lisp, it may annotate the symbol as being a function,
94not a variable.
95
96The function takes the symbol name as argument. If it needs to
97annotate this, it should return a value suitable as an element of
98the list passed to `display-completion-list'.
99
100The predicate being used for selecting completions (from
101`symbol-completion-predicate-function') is available
102dynamically-bound as `symbol-completion-predicate' in case the
103transform needs it.")
104
105(defvar symbol-completion-predicate)
106
107;;;###autoload
108(defun symbol-complete (&optional predicate)
109 "Perform completion of the symbol preceding point.
110This is done in a way appropriate to the current major mode,
111perhaps by interrogating an inferior interpreter. Compare
112`complete-symbol'.
113If no characters can be completed, display a list of possible completions.
114Repeating the command at that point scrolls the list.
115
116When called from a program, optional arg PREDICATE is a predicate
117determining which symbols are considered.
118
119This function requires `symbol-completion-completions-function'
120to be set buffer-locally. Variables `symbol-completion-symbol-function',
121`symbol-completion-predicate-function' and
122`symbol-completion-transform-function' are also consulted."
123 (interactive)
124 ;; Fixme: Punt to `complete-symbol' in this case?
125 (unless (functionp symbol-completion-completions-function)
126 (error "symbol-completion-completions-function not defined"))
127 (let* ((pattern (or (funcall symbol-completion-symbol-function)
128 (error "No preceding symbol to complete")))
129 ;; FIXME: We assume below that `pattern' holds the text just
130 ;; before point. This is a problem in the way
131 ;; symbol-completion-symbol-function was defined.
132 (predicate (or predicate
133 (if symbol-completion-predicate-function
134 (funcall symbol-completion-predicate-function
135 (- (point) (length pattern))
136 (point)))))
137 (completions (funcall symbol-completion-completions-function
138 pattern))
139 ;; In case the transform needs to access it.
140 (symbol-completion-predicate predicate)
141 (completion-annotate-function
142 (if (functionp symbol-completion-transform-function)
143 (lambda (str)
144 (car-safe (cdr-safe
145 (funcall symbol-completion-transform-function
146 str)))))))
147 (completion-in-region (- (point) (length pattern)) (point)
148 completions predicate)))
149
150(eval-when-compile (require 'hippie-exp))
151
152;;;###autoload
153(defun symbol-completion-try-complete (old)
154 "Completion function for use with `hippie-expand'.
155Uses `symbol-completion-symbol-function' and
156`symbol-completion-completions-function'. It is intended to be
157used something like this in a major mode which provides symbol
158completion:
159
160 (if (featurep 'hippie-exp)
161 (set (make-local-variable 'hippie-expand-try-functions-list)
162 (cons 'symbol-completion-try-complete
163 hippie-expand-try-functions-list)))"
164 (when (and symbol-completion-symbol-function
165 symbol-completion-completions-function)
166 (unless old
167 (let ((symbol (funcall symbol-completion-symbol-function)))
168 (he-init-string (- (point) (length symbol)) (point))
169 (if (not (he-string-member he-search-string he-tried-table))
170 (push he-search-string he-tried-table))
171 (setq he-expand-list
172 (and symbol
173 (funcall symbol-completion-completions-function symbol)))))
174 (while (and he-expand-list
175 (he-string-member (car he-expand-list) he-tried-table))
176 (pop he-expand-list))
177 (if he-expand-list
178 (progn
179 (he-substitute-string (pop he-expand-list))
180 t)
181 (if old (he-reset-string))
182 nil)))
183
184;;; Emacs Lisp symbol completion.
185
186(defun lisp-completion-symbol ()
187 "`symbol-completion-symbol-function' for Lisp."
188 (let ((end (point))
189 (beg (with-syntax-table emacs-lisp-mode-syntax-table
190 (save-excursion
191 (backward-sexp 1)
192 (while (= (char-syntax (following-char)) ?\')
193 (forward-char 1))
194 (point)))))
195 (buffer-substring-no-properties beg end)))
196
197(defun lisp-completion-predicate (beg end)
198 "`symbol-completion-predicate-function' for Lisp."
199 (save-excursion
200 (goto-char beg)
201 (if (not (eq (char-before) ?\())
202 (lambda (sym) ;why not just nil ? -sm
203 ;To avoid interned symbols with
204 ;no slots. -- fx
205 (or (boundp sym) (fboundp sym)
206 (symbol-plist sym)))
207 ;; Looks like a funcall position. Let's double check.
208 (if (condition-case nil
209 (progn (up-list -2) (forward-char 1)
210 (eq (char-after) ?\())
211 (error nil))
212 ;; If the first element of the parent list is an open
213 ;; parenthesis we are probably not in a funcall position.
214 ;; Maybe a `let' varlist or something.
215 nil
216 ;; Else, we assume that a function name is expected.
217 'fboundp))))
218
219(defun lisp-symbol-completion-transform ()
220 "`symbol-completion-transform-function' for Lisp."
221 (lambda (elt)
222 (if (and (not (eq 'fboundp symbol-completion-predicate))
223 (fboundp (intern elt)))
224 (list elt " <f>")
225 elt)))
226
227(provide 'sym-comp)
228
229;; arch-tag: 6fcce616-f3c4-4751-94b4-710e83144124
230;;; sym-comp.el ends here