annotate lisp/utils/lib-complete.el @ 155:43dd3413c7c7 r20-3b4

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