0
|
1 ;; ========================================================================
|
|
2 ;; lib-complete.el -- Completion on a search path
|
|
3 ;; Author : Mike Williams <mike-w@cs.aukuni.ac.nz>
|
|
4 ;; Created On : Sat Apr 20 17:47:21 1991
|
|
5 ;; Last Modified By: Heiko M|nkel <muenkel@tnt.uni-hannover.de>
|
|
6 ;; Additional XEmacs integration By: Chuck Thompson <cthomp@cs.uiuc.edu>
|
|
7 ;; Last Modified On: Thu Jul 1 14:23:00 1994
|
70
|
8 ;; RCS Info : $Revision: 1.1.1.1 $ $Locker: $
|
0
|
9 ;; ========================================================================
|
|
10 ;; NOTE: this file must be recompiled if changed.
|
|
11 ;;
|
|
12 ;; Copyright (C) Mike Williams <mike-w@cs.aukuni.ac.nz> 1991
|
|
13 ;;
|
|
14 ;; Keywords: utility, lisp
|
|
15
|
|
16 ;; This file is part of XEmacs.
|
|
17
|
|
18 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
19 ;; under the terms of the GNU General Public License as published by
|
|
20 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
21 ;; any later version.
|
|
22
|
|
23 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
26 ;; General Public License for more details.
|
|
27
|
|
28 ;; You should have received a copy of the GNU General Public License
|
16
|
29 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
31 ;; Boston, MA 02111-1307, USA.
|
0
|
32
|
|
33 ;;; Synched up with: Not in FSF.
|
|
34
|
|
35 ;; Many thanks to Hallvard Furuseth <hallvard@ifi.uio.no> for his
|
|
36 ;; helpful suggestions.
|
|
37
|
|
38 ;; The function locate-file is removed, because of its incompatibility
|
|
39 ;; with the buildin function of the lemacs 19.10 (Heiko M|nkel).
|
|
40
|
|
41 ;; There is now the new function find-library in this package.
|
|
42
|
|
43 (provide 'lib-complete)
|
|
44
|
|
45 ;;=== Usage ===============================================================
|
|
46 ;;
|
|
47 ;; (autoload (fmakunbound 'load-library) "lib-complete" nil t)
|
|
48 ;; (autoload 'library-all-completions "lib-complete")
|
|
49 ;; (autoload 'read-library "lib-complete")
|
|
50 ;; (autoload 'find-library "lib-complete"
|
|
51 ;; "Find and edit the source for the library named LIBRARY.
|
|
52 ;; The extension of the LIBRARY must be omitted.")
|
|
53
|
|
54 ;;=== Locate a file in a search path ======================================
|
|
55
|
|
56 ;(defun locate-file (FILE SEARCH-PATH &optional SUFFIX-LIST PRED)
|
|
57 ; "Search for FILE on SEARCH-PATH (list). If optional SUFFIX-LIST is
|
|
58 ;provided, allow file to be followed by one of the suffixes.
|
|
59 ;Optional second argument PRED restricts the number of files which
|
|
60 ;may match. The default is file-exists-p."
|
|
61 ; (if (not SUFFIX-LIST) (setq SUFFIX-LIST '("")))
|
|
62 ; (if (not PRED) (setq PRED 'file-exists-p))
|
|
63 ; (if (file-name-absolute-p FILE) (setq SEARCH-PATH '(nil)))
|
|
64 ; (if (equal FILE "") (error "Empty filename"))
|
|
65 ; (let ((filelist
|
|
66 ; (mapcar
|
|
67 ; (function (lambda (ext) (concat FILE ext)))
|
|
68 ; SUFFIX-LIST)))
|
|
69 ; ;; Search SEARCH-PATH for a readable file in filelist
|
|
70 ; (catch 'found
|
|
71 ; (while SEARCH-PATH
|
|
72 ; (let ((filelist filelist))
|
|
73 ; (while filelist
|
|
74 ; (let ((filepath (expand-file-name (car filelist)
|
|
75 ; (car SEARCH-PATH))))
|
|
76 ; (if (funcall PRED filepath)
|
|
77 ; (throw 'found filepath)))
|
|
78 ; (setq filelist (cdr filelist))))
|
|
79 ; (setq SEARCH-PATH (cdr SEARCH-PATH))))
|
|
80 ; ))
|
|
81
|
|
82 ;;=== Determine completions for filename in search path ===================
|
|
83
|
|
84 (defun library-all-completions (FILE SEARCH-PATH &optional FULL FAST)
|
|
85 "Return all completions for FILE in any directory on SEARCH-PATH.
|
|
86 If optional third argument FULL is non-nil, returned pathnames should be
|
|
87 absolute rather than relative to some directory on the SEARCH-PATH.
|
|
88 If optional fourth argument FAST is non-nil, don't sort the completions,
|
|
89 or remove duplicates."
|
|
90 (setq FILE (or FILE ""))
|
|
91 (if (file-name-absolute-p FILE)
|
|
92 ;; It's an absolute file name, so don't need SEARCH-PATH
|
|
93 (progn
|
|
94 (setq FILE (expand-file-name FILE))
|
|
95 (file-name-all-completions
|
|
96 (file-name-nondirectory FILE) (file-name-directory FILE)))
|
|
97 (let ((subdir (file-name-directory FILE))
|
|
98 (file (file-name-nondirectory FILE))
|
|
99 all-completions)
|
|
100 ;; Make list of completions in each directory on SEARCH-PATH
|
|
101 (while SEARCH-PATH
|
|
102 (let* ((dir (concat (file-name-as-directory
|
|
103 (expand-file-name (car SEARCH-PATH)))
|
|
104 subdir))
|
|
105 (dir-prefix (if FULL dir subdir)))
|
|
106 (if (file-directory-p dir)
|
|
107 (let ((subdir-completions
|
|
108 (file-name-all-completions file dir)))
|
|
109 (while subdir-completions
|
|
110 (setq all-completions
|
|
111 (cons (concat dir-prefix (car subdir-completions))
|
|
112 all-completions))
|
|
113 (setq subdir-completions (cdr subdir-completions))))))
|
|
114 (setq SEARCH-PATH (cdr SEARCH-PATH)))
|
|
115 (if FAST all-completions
|
|
116 (let ((sorted (nreverse (sort all-completions 'string<)))
|
|
117 compressed)
|
|
118 (while sorted
|
|
119 (if (equal (car sorted) (car compressed)) nil
|
|
120 (setq compressed (cons (car sorted) compressed)))
|
|
121 (setq sorted (cdr sorted)))
|
|
122 compressed)))))
|
|
123
|
|
124 ;;=== Utilities ===========================================================
|
|
125
|
|
126 (defmacro progn-with-message (MESSAGE &rest FORMS)
|
|
127 "(progn-with-message MESSAGE FORMS ...)
|
|
128 Display MESSAGE and evaluate FORMS, returning value of the last one."
|
|
129 ;; based on Hallvard Furuseth's funcall-with-message
|
|
130 (`
|
|
131 (if (eq (selected-window) (minibuffer-window))
|
|
132 (save-excursion
|
|
133 (goto-char (point-max))
|
|
134 (let ((orig-pmax (point-max)))
|
|
135 (unwind-protect
|
|
136 (progn
|
|
137 (insert " " (, MESSAGE)) (goto-char orig-pmax)
|
|
138 (sit-for 0) ; Redisplay
|
|
139 (,@ FORMS))
|
|
140 (delete-region orig-pmax (point-max)))))
|
|
141 (prog2
|
|
142 (message "%s" (, MESSAGE))
|
|
143 (progn (,@ FORMS))
|
|
144 (message "")))))
|
|
145
|
|
146 (put 'progn-with-message 'lisp-indent-hook 1)
|
|
147
|
|
148 ;;=== Completion caching ==================================================
|
|
149
|
|
150 (defconst lib-complete:cache nil
|
|
151 "Used within read-library and read-library-internal to prevent
|
|
152 costly repeated calls to library-all-completions.
|
|
153 Format is a list of lists of the form
|
|
154
|
|
155 ([<path> <subdir>] <cache-record> <cache-record> ...)
|
|
156
|
|
157 where each <cache-record> has the form
|
|
158
|
|
159 (<root> <modtimes> <completion-table>)")
|
|
160
|
|
161 (defun lib-complete:better-root (ROOT1 ROOT2)
|
|
162 "Return non-nil if ROOT1 is a superset of ROOT2."
|
|
163 (and (equal (file-name-directory ROOT1) (file-name-directory ROOT2))
|
|
164 (string-match
|
|
165 (concat "^" (regexp-quote (file-name-nondirectory ROOT1)))
|
|
166 ROOT2)))
|
|
167
|
|
168 (defun lib-complete:get-completion-table (FILE PATH FILTER)
|
|
169 (let* ((subdir (file-name-directory FILE))
|
|
170 (root (file-name-nondirectory FILE))
|
|
171 (PATH
|
|
172 (mapcar
|
|
173 (function (lambda (dir) (file-name-as-directory
|
|
174 (expand-file-name (or dir "")))))
|
|
175 PATH))
|
|
176 (key (vector PATH subdir FILTER))
|
|
177 (real-dirs
|
|
178 (if subdir
|
|
179 (mapcar (function (lambda (dir) (concat dir subdir))) PATH)
|
|
180 PATH))
|
|
181 (path-modtimes
|
|
182 (mapcar
|
|
183 (function (lambda (fn) (if fn (nth 5 (file-attributes fn)))))
|
|
184 real-dirs))
|
|
185 (cache-entry (assoc key lib-complete:cache))
|
|
186 (cache-records (cdr cache-entry)))
|
|
187 ;; Look for cached entry
|
|
188 (catch 'table
|
|
189 (while cache-records
|
|
190 (if (and
|
|
191 (lib-complete:better-root (nth 0 (car cache-records)) root)
|
|
192 (equal (nth 1 (car cache-records)) path-modtimes))
|
|
193 (throw 'table (nth 2 (car cache-records))))
|
|
194 (setq cache-records (cdr cache-records)))
|
|
195 ;; Otherwise build completions
|
|
196 (let ((completion-list
|
|
197 (progn-with-message "(building completion table...)"
|
|
198 (library-all-completions FILE PATH nil 'fast)))
|
|
199 (completion-table (make-vector 127 0)))
|
|
200 (while completion-list
|
|
201 (let ((completion
|
|
202 (if (or (not FILTER)
|
|
203 (file-directory-p (car completion-list)))
|
|
204 (car completion-list)
|
|
205 (funcall FILTER (car completion-list)))))
|
|
206 (if completion
|
|
207 (intern completion completion-table)))
|
|
208 (setq completion-list (cdr completion-list)))
|
|
209 ;; Cache the completions
|
|
210 (lib-complete:cache-completions key root
|
|
211 path-modtimes completion-table)
|
|
212 completion-table))))
|
|
213
|
|
214 (defvar lib-complete:max-cache-size 20
|
|
215 "*Maximum number of search paths which are cached.")
|
|
216
|
|
217 (defun lib-complete:cache-completions (key root modtimes table)
|
|
218 (let* ((cache-entry (assoc key lib-complete:cache))
|
|
219 (cache-records (cdr cache-entry))
|
|
220 (new-cache-records (list (list root modtimes table))))
|
|
221 (if (not cache-entry) nil
|
|
222 ;; Remove old cache entry
|
|
223 (setq lib-complete:cache (delq cache-entry lib-complete:cache))
|
|
224 ;; Copy non-redundant entries from old cache entry
|
|
225 (while cache-records
|
|
226 (if (or (equal root (nth 0 (car cache-records)))
|
|
227 (lib-complete:better-root root (nth 0 (car cache-records))))
|
|
228 nil
|
|
229 (setq new-cache-records
|
|
230 (cons (car cache-records) new-cache-records)))
|
|
231 (setq cache-records (cdr cache-records))))
|
|
232 ;; Add entry to front of cache
|
|
233 (setq lib-complete:cache
|
|
234 (cons (cons key (nreverse new-cache-records)) lib-complete:cache))
|
|
235 ;; Trim cache
|
|
236 (let ((tail (nthcdr lib-complete:max-cache-size lib-complete:cache)))
|
|
237 (if tail (setcdr tail nil)))))
|
|
238
|
|
239 ;;=== Read a filename, with completion in a search path ===================
|
|
240
|
|
241 (defun read-library-internal (FILE FILTER FLAG)
|
|
242 "Don't call this."
|
|
243 ;; Relies on read-library-internal-search-path being let-bound
|
|
244 (let ((completion-table
|
|
245 (lib-complete:get-completion-table
|
|
246 FILE read-library-internal-search-path FILTER)))
|
|
247 (cond
|
|
248 ((not completion-table) nil)
|
|
249 ;; Completion table is filtered before use, so the PREDICATE
|
|
250 ;; argument is redundant.
|
|
251 ((eq FLAG nil) (try-completion FILE completion-table nil))
|
|
252 ((eq FLAG t) (all-completions FILE completion-table nil))
|
|
253 ((eq FLAG 'lambda) (and (intern-soft FILE completion-table) t))
|
|
254 )))
|
|
255
|
|
256 (defun read-library (PROMPT SEARCH-PATH &optional DEFAULT MUST-MATCH
|
|
257 FULL FILTER)
|
|
258 "Read library name, prompting with PROMPT and completing in directories
|
|
259 from SEARCH-PATH. A nil in the search path represents the current
|
|
260 directory. Completions for a given search-path are cached, with the
|
|
261 cache being invalidated whenever one of the directories on the path changes.
|
|
262 Default to DEFAULT if user enters a null string.
|
|
263 Optional fourth arg MUST-MATCH non-nil means require existing file's name.
|
|
264 Non-nil and non-t means also require confirmation after completion.
|
|
265 Optional fifth argument FULL non-nil causes a full pathname, rather than a
|
|
266 relative pathname, to be returned. Note that FULL implies MUST-MATCH.
|
|
267 Optional sixth argument FILTER can be used to provide a function to
|
|
268 filter the completions. This function is passed the filename, and should
|
|
269 return a transformed filename (possibly a null transformation) or nil,
|
|
270 indicating that the filename should not be included in the completions."
|
|
271 (let* ((read-library-internal-search-path SEARCH-PATH)
|
|
272 (library (completing-read PROMPT 'read-library-internal
|
|
273 FILTER (or MUST-MATCH FULL) nil)))
|
|
274 (cond
|
|
275 ((equal library "") DEFAULT)
|
70
|
276 (FULL (locate-file library read-library-internal-search-path ".el:.elc"))
|
0
|
277 (t library))))
|
|
278
|
|
279 ;; NOTE: as a special case, read-library may be used to read a filename
|
|
280 ;; relative to the current directory, returning a *relative* pathname
|
|
281 ;; (read-file-name returns a full pathname).
|
|
282 ;;
|
|
283 ;; eg. (read-library "Local header: " '(nil) nil)
|
|
284
|
|
285 (defun get-library-path ()
|
|
286 "Front end to read-library"
|
|
287 (read-library "Find Library file: " load-path nil t t
|
|
288 (function (lambda (fn)
|
70
|
289 (cond
|
|
290 ((string-match "\\.el$" fn)
|
0
|
291 (substring fn 0 (match-beginning 0))))))
|
|
292 ))
|
|
293
|
|
294 ;;=== Replacement for load-library with completion ========================
|
|
295
|
|
296 (defun load-library (library)
|
|
297 "Load the library named LIBRARY.
|
|
298 This is an interface to the function `load'."
|
|
299 (interactive
|
|
300 (list (read-library "Load Library: " load-path nil nil nil
|
|
301 (function (lambda (fn)
|
|
302 (cond
|
|
303 ((string-match "\\.elc?$" fn)
|
|
304 (substring fn 0 (match-beginning 0))))))
|
|
305 )))
|
|
306 (load library))
|
|
307
|
|
308 ;;=== find-library with completion (Author: Heiko Muenkel) ===================
|
|
309
|
|
310 (defun find-library (library)
|
|
311 "Find and edit the source for the library named LIBRARY.
|
|
312 The extension of the LIBRARY must be omitted."
|
|
313 (interactive
|
|
314 (list
|
|
315 (get-library-path)))
|
|
316 (find-file library))
|
|
317
|
|
318 (defun find-library-other-window (library)
|
|
319 "Load the library named LIBRARY in another window."
|
|
320 (interactive
|
|
321 (list (get-library-path)))
|
|
322 (find-file-other-window library))
|
|
323
|
|
324 (defun find-library-other-frame (library)
|
|
325 "Load the library named LIBRARY in a newly-created frame."
|
|
326 (interactive
|
|
327 (list (get-library-path)))
|
|
328 (find-file-other-frame library))
|
|
329
|
|
330 ; This conflicts with an existing binding
|
|
331 ;(define-key global-map "\C-xl" 'find-library)
|
|
332 (define-key global-map "\C-x4l" 'find-library-other-window)
|
|
333 (define-key global-map "\C-x5l" 'find-library-other-frame)
|