annotate lisp/progmodes/sym-comp.el @ 95257:c20af13dd9f0

*** empty log message ***
author Juanma Barranquero <lekktu@gmail.com>
date Fri, 23 May 2008 10:30:18 +0000
parents 52b7a8c22af5
children a9dc0e7c3f2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
1 ;;; sym-comp.el --- mode-dependent symbol completion
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
2
92250
a2c5eb229cdf Change copyright to FSF, per the author's statement:
Glenn Morris <rgm@gnu.org>
parents: 92229
diff changeset
3 ;; Copyright (C) 2004, 2008 Free Software Foundation, Inc.
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
4
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
5 ;; Author: Dave Love <fx@gnu.org>
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
6 ;; Keywords: extensions
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
7 ;; URL: http://www.loveshack.ukfsn.org/emacs
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
8
92256
bae32624018f Standardize license text.
Glenn Morris <rgm@gnu.org>
parents: 92250
diff changeset
9 ;; This file is part of GNU Emacs.
bae32624018f Standardize license text.
Glenn Morris <rgm@gnu.org>
parents: 92250
diff changeset
10
94673
52b7a8c22af5 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92256
diff changeset
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
94673
52b7a8c22af5 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92256
diff changeset
13 ;; the Free Software Foundation, either version 3 of the License, or
52b7a8c22af5 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92256
diff changeset
14 ;; (at your option) any later version.
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
15
92256
bae32624018f Standardize license text.
Glenn Morris <rgm@gnu.org>
parents: 92250
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
19 ;; GNU General Public License for more details.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
20
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
94673
52b7a8c22af5 Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents: 92256
diff changeset
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
23
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
24 ;;; Commentary:
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
25
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
26 ;; This defines `symbol-complete', which is a generalization of the
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
27 ;; old `lisp-complete-symbol'. It provides the following hooks to
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
28 ;; allow major modes to set up completion appropriate for the mode:
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
29 ;; `symbol-completion-symbol-function',
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
30 ;; `symbol-completion-completions-function',
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
31 ;; `symbol-completion-predicate-function',
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
32 ;; `symbol-completion-transform-function'. Typically it is only
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
33 ;; necessary for a mode to set
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
34 ;; `symbol-completion-completions-function' locally and to bind
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
35 ;; `symbol-complete' appropriately.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
36
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
37 ;; It's unfortunate that there doesn't seem to be a good way of
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
38 ;; combining this with `complete-symbol'.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
39
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
40 ;; There is also `symbol-completion-try-complete', for use with
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
41 ;; Hippie-exp.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
42
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
43 ;;; Code:
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
44
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
45 ;;;; Mode-dependent symbol completion.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
46
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
47 (defun symbol-completion-symbol ()
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
48 "Default `symbol-completion-symbol-function'.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
49 Uses `current-word' with the buffer narrowed to the part before
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
50 point."
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
51 (save-restriction
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
52 ;; Narrow in case point is in the middle of a symbol -- we want
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
53 ;; just the preceeding part.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
54 (narrow-to-region (point-min) (point))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
55 (current-word)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
56
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
57 (defvar symbol-completion-symbol-function 'symbol-completion-symbol
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
58 "Function to return a partial symbol before point for completion.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
59 The value it returns should be a string (or nil).
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
60 Major modes may set this locally if the default isn't appropriate.")
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
61
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
62 (defvar symbol-completion-completions-function nil
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
63 "Function to return possible symbol completions.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
64 It takes an argument which is the string to be completed and
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
65 returns a value suitable for the second argument of
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
66 `try-completion'. This value need not use the argument, i.e. it
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
67 may be all possible completions, such as `obarray' in the case of
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
68 Emacs Lisp.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
69
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
70 Major modes may set this locally to allow them to support
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
71 `symbol-complete'. See also `symbol-completion-symbol-function',
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
72 `symbol-completion-predicate-function' and
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
73 `symbol-completion-transform-function'.")
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
74
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
75 (defvar symbol-completion-predicate-function nil
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
76 "If non-nil, function to return a predicate for selecting symbol completions.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
77 The function gets two args, the positions of the beginning and
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
78 end of the symbol to be completed.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
79
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
80 Major modes may set this locally if the default isn't
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
81 appropriate. This is a function returning a predicate so that
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
82 the predicate can be context-dependent, e.g. to select only
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
83 function names if point is at a function call position. The
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
84 function's args may be useful for determining the context.")
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
85
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
86 (defvar symbol-completion-transform-function nil
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
87 "If non-nil, function to transform symbols in the symbol-completion buffer.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
88 E.g., for Lisp, it may annotate the symbol as being a function,
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
89 not a variable.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
90
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
91 The function takes the symbol name as argument. If it needs to
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
92 annotate this, it should return a value suitable as an element of
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
93 the list passed to `display-completion-list'.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
94
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
95 The predicate being used for selecting completions (from
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
96 `symbol-completion-predicate-function') is available
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
97 dynamically-bound as `symbol-completion-predicate' in case the
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
98 transform needs it.")
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
99
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
100 (defvar displayed-completions)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
101
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
102 ;;;###autoload
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
103 (defun symbol-complete (&optional predicate)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
104 "Perform completion of the symbol preceding point.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
105 This is done in a way appropriate to the current major mode,
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
106 perhaps by interrogating an inferior interpreter. Compare
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
107 `complete-symbol'.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
108 If no characters can be completed, display a list of possible completions.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
109 Repeating the command at that point scrolls the list.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
110
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
111 When called from a program, optional arg PREDICATE is a predicate
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
112 determining which symbols are considered.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
113
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
114 This function requires `symbol-completion-completions-function'
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
115 to be set buffer-locally. Variables `symbol-completion-symbol-function',
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
116 `symbol-completion-predicate-function' and
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
117 `symbol-completion-transform-function' are also consulted."
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
118 (interactive)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
119 ;; Fixme: Punt to `complete-symbol' in this case?
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
120 (unless (functionp symbol-completion-completions-function)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
121 (error "symbol-completion-completions-function not defined"))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
122 (let ((window (get-buffer-window "*Completions*")))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
123 (let* ((pattern (or (funcall symbol-completion-symbol-function)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
124 (error "No preceding symbol to complete")))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
125 (predicate (or predicate
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
126 (if symbol-completion-predicate-function
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
127 (funcall symbol-completion-predicate-function
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
128 (- (point) (length pattern))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
129 (point)))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
130 (completions (funcall symbol-completion-completions-function
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
131 pattern))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
132 (completion (try-completion pattern completions predicate)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
133 ;; If this command was repeated, and there's a fresh completion
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
134 ;; window with a live buffer and a displayed completion list
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
135 ;; matching the current completions, then scroll the window.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
136 (unless (and (eq last-command this-command)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
137 window (window-live-p window) (window-buffer window)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
138 (buffer-name (window-buffer window))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
139 (with-current-buffer (window-buffer window)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
140 (if (equal displayed-completions
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
141 (all-completions pattern completions predicate))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
142 (progn
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
143 (if (pos-visible-in-window-p (point-max) window)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
144 (set-window-start window (point-min))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
145 (save-selected-window
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
146 (select-window window)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
147 (scroll-up)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
148 t))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
149 ;; Otherwise, do completion.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
150 (cond ((eq completion t))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
151 ((null completion)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
152 (message "Can't find completion for \"%s\"" pattern)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
153 (ding))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
154 ((not (string= pattern completion))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
155 (delete-region (- (point) (length pattern)) (point))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
156 (insert completion))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
157 (t
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
158 (message "Making completion list...")
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
159 (let* ((list (all-completions pattern completions predicate))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
160 ;; In case the transform needs to access it.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
161 (symbol-completion-predicate predicate)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
162 ;; Copy since list is side-effected by sorting.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
163 (copy (copy-sequence list)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
164 (setq list (sort list 'string<))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
165 (if (functionp symbol-completion-transform-function)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
166 (setq list
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
167 (mapcar (funcall
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
168 symbol-completion-transform-function)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
169 list)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
170 (with-output-to-temp-buffer "*Completions*"
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
171 (condition-case ()
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
172 (display-completion-list list pattern) ; Emacs 22
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
173 (error (display-completion-list list))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
174 ;; Record the list for determining whether to scroll
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
175 ;; (above).
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
176 (with-current-buffer "*Completions*"
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
177 (set (make-local-variable 'displayed-completions) copy)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
178 (message "Making completion list...%s" "done")))))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
179
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
180 (eval-when-compile (require 'hippie-exp))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
181
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
182 ;;;###autoload
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
183 (defun symbol-completion-try-complete (old)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
184 "Completion function for use with `hippie-expand'.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
185 Uses `symbol-completion-symbol-function' and
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
186 `symbol-completion-completions-function'. It is intended to be
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
187 used something like this in a major mode which provides symbol
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
188 completion:
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
189
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
190 (if (featurep 'hippie-exp)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
191 (set (make-local-variable 'hippie-expand-try-functions-list)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
192 (cons 'symbol-completion-try-complete
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
193 hippie-expand-try-functions-list)))"
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
194 (when (and symbol-completion-symbol-function
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
195 symbol-completion-completions-function)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
196 (unless old
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
197 (let ((symbol (funcall symbol-completion-symbol-function)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
198 (he-init-string (- (point) (length symbol)) (point))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
199 (if (not (he-string-member he-search-string he-tried-table))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
200 (push he-search-string he-tried-table))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
201 (setq he-expand-list
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
202 (and symbol
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
203 (funcall symbol-completion-completions-function symbol)))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
204 (while (and he-expand-list
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
205 (he-string-member (car he-expand-list) he-tried-table))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
206 (pop he-expand-list))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
207 (if he-expand-list
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
208 (progn
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
209 (he-substitute-string (pop he-expand-list))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
210 t)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
211 (if old (he-reset-string))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
212 nil)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
213
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
214 ;;; Emacs Lisp symbol completion.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
215
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
216 (defun lisp-completion-symbol ()
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
217 "`symbol-completion-symbol-function' for Lisp."
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
218 (let ((end (point))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
219 (beg (with-syntax-table emacs-lisp-mode-syntax-table
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
220 (save-excursion
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
221 (backward-sexp 1)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
222 (while (= (char-syntax (following-char)) ?\')
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
223 (forward-char 1))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
224 (point)))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
225 (buffer-substring-no-properties beg end)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
226
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
227 (defun lisp-completion-predicate (beg end)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
228 "`symbol-completion-predicate-function' for Lisp."
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
229 (save-excursion
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
230 (goto-char beg)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
231 (if (not (eq (char-before) ?\())
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
232 (lambda (sym) ;why not just nil ? -sm
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
233 ;To avoid interned symbols with
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
234 ;no slots. -- fx
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
235 (or (boundp sym) (fboundp sym)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
236 (symbol-plist sym)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
237 ;; Looks like a funcall position. Let's double check.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
238 (if (condition-case nil
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
239 (progn (up-list -2) (forward-char 1)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
240 (eq (char-after) ?\())
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
241 (error nil))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
242 ;; If the first element of the parent list is an open
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
243 ;; parenthesis we are probably not in a funcall position.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
244 ;; Maybe a `let' varlist or something.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
245 nil
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
246 ;; Else, we assume that a function name is expected.
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
247 'fboundp))))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
248
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
249 (defvar symbol-completion-predicate)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
250
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
251 (defun lisp-symbol-completion-transform ()
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
252 "`symbol-completion-transform-function' for Lisp."
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
253 (lambda (elt)
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
254 (if (and (not (eq 'fboundp symbol-completion-predicate))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
255 (fboundp (intern elt)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
256 (list elt " <f>")
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
257 elt)))
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
258
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
259 (provide 'sym-comp)
92123
e670b615aad9 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 92054
diff changeset
260
e670b615aad9 Add arch tagline
Miles Bader <miles@gnu.org>
parents: 92054
diff changeset
261 ;; arch-tag: 6fcce616-f3c4-4751-94b4-710e83144124
92054
6d77acc71bc0 New file.
Chong Yidong <cyd@stupidchicken.com>
parents:
diff changeset
262 ;;; sym-comp.el ends here