annotate lisp/utils/lib-complete.el @ 70:131b0175ea99 r20-0b30

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