Mercurial > emacs
annotate lisp/filesets.el @ 45123:7648ca9b6024
(mwheel-event-button): Translate mouse-wheel events.
(mwheel-scroll): Recognize nil scroll amount as documented.
author | Jason Rumney <jasonr@gnu.org> |
---|---|
date | Sat, 04 May 2002 22:16:04 +0000 |
parents | 161015ebadc0 |
children | cb1d1e1eb678 |
rev | line source |
---|---|
44934 | 1 ;;; filesets.el --- handle group of files |
2 | |
3 ;; Copyright (C) 2002 Free Software Foundation, Inc. | |
4 | |
44959 | 5 ;; Author: Thomas Link <t.link@gmx.at> |
44934 | 6 ;; Keywords: filesets convenience |
7 | |
44959 | 8 ;; This file is part of GNU Emacs. |
9 | |
44934 | 10 ;; This program is free software; you can redistribute it and/or modify |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; This program is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; A copy of the GNU General Public License can be obtained from this | |
21 ;; program's author or from the Free Software Foundation, Inc., 675 Mass | |
22 ;; Ave, Cambridge, MA 02139, USA. | |
23 | |
24 (defvar filesets-version "1.8.1") | |
25 (defvar filesets-homepage | |
26 "http://members.a1.net/t.link/CompEmacsFilesets.html") | |
27 | |
28 ;;; Commentary: | |
29 | |
44973 | 30 ;; Define filesets, which can be opened or saved with the power of one or |
44959 | 31 ;; two mouse clicks only. A fileset is either a list of files, a file |
32 ;; pattern, a base directory and a search pattern (for files), or an | |
33 ;; inclusion group (i.e. a base file including other files). | |
44934 | 34 |
44959 | 35 ;; Usage: 1. Put (require 'filesets) into your start-up file. 2. Type |
36 ;; M-x filesets-edit or choose "Edit Filesets" from the menu. 3. Save | |
37 ;; your customizations. | |
44934 | 38 |
44959 | 39 ;; Caveat: Fileset names have to be unique. |
44934 | 40 |
44959 | 41 ;; Filesets.el adds a nifty filesets menu to your menubar. If you change |
42 ;; your filesets on the fly, don't forget to select "Save Filesets" from | |
43 ;; the menu. | |
44934 | 44 |
44959 | 45 ;; Pressing on the first item in the submenu will open all files at once. |
46 ;; Define your own function, e.g. browse-url, for opening a fileset's | |
47 ;; files. Or define external viewers for opening files with other | |
48 ;; programs. See `filesets-external-viewers'. | |
44934 | 49 |
44959 | 50 ;; BTW, if you close a fileset, files, which have been changed, will |
51 ;; be silently saved. Change this behaviour by setting | |
52 ;; `filesets-save-buffer-fn'. | |
44934 | 53 |
54 ;;; Supported modes for inclusion groups (`filesets-ingroup-patterns'): | |
55 ;; - Elisp | |
56 ;; - Emacs-Wiki (simple names only) | |
57 ;; - LaTeX | |
58 | |
59 | |
60 | |
61 ;;; Known bugs: | |
62 | |
63 | |
64 ;;; To do: | |
65 | |
66 ;;- better handling of different customization scenarios | |
67 | |
68 | |
69 ;;; Credits: | |
70 | |
71 ;; Helpful suggestions (but no significant code) were contributed by | |
72 | |
73 ;;- Christoph Conrad (at gmx de) | |
74 ;;- Christian Ohler (at Informatik Uni-Oldenburg DE) | |
75 ;;- Richard Stallman aka RMS (at gnu org) | |
76 ;;- Per Abrahamsen aka abraham (at dina kvl dk) | |
77 | |
78 | |
79 ;;; Code: | |
80 | |
81 (eval-when-compile | |
82 (require 'cl)) | |
83 | |
84 | |
85 ;;; Some variables | |
44959 | 86 (eval-and-compile |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
87 (defvar filesets-running-xemacs (string-match "XEmacs\\|Lucid" emacs-version) |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
88 "Non-nil means we are running XEmacs.")) |
44934 | 89 |
90 (defvar filesets-menu-cache nil | |
91 "The whole filesets menu.") | |
92 (defvar filesets-cache-version nil | |
93 "Filesets' cached version number.") | |
94 (defvar filesets-cache-hostname nil | |
95 "Filesets' cached system name.") | |
96 | |
97 (defvar filesets-ingroup-cache nil | |
98 "A plist containing files and their ingroup data.") | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
99 (defvar filesets-ingroup-files nil |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
100 "List of files already processed when searching for included files.") |
44934 | 101 |
102 (defvar filesets-has-changed-flag t | |
103 "Non-nil means some fileset definition has changed.") | |
104 (defvar filesets-submenus nil | |
105 "An association list with filesets menu data.") | |
106 (defvar filesets-updated-buffers nil | |
107 "A list of buffers with updated menu bars.") | |
108 (defvar filesets-menu-use-cached-flag nil | |
109 "Use cached data. See `filesets-menu-ensure-use-cached' for details.") | |
110 (defvar filesets-update-cache-file-flag nil | |
111 "Non-nil means the cache needs updating.") | |
112 (defvar filesets-ignore-next-set-default nil | |
113 "A list of custom variables for which the next `set-default' will be | |
114 ignored.") | |
115 | |
116 (defvar filesets-output-buffer-flag nil | |
117 "Non-nil means the current buffer is an output buffer created by filesets. | |
118 Is buffer local variable.") | |
119 | |
120 (defvar filesets-verbosity 1 | |
121 "An integer defining the level of verbosity. 0 means no messages | |
122 at all.") | |
123 | |
124 (defvar filesets-menu-ensure-use-cached | |
125 (and filesets-running-xemacs | |
126 (not (emacs-version>= 21 5))) | |
127 "Make sure (X)Emacs uses filesets' cache. | |
128 | |
129 Well, if you use XEmacs (prior to 21.5?) custom.el is loaded after | |
130 init.el. This means that settings saved in the cache file (see | |
131 `filesets-menu-cache-file') will be overwritten by custom.el. In order | |
132 to ensure the use of the cache file, set this variable to t -- which is | |
133 the default for XEmacs prior to 21.5. If you want to change this value | |
134 put \"(setq filesets-menu-ensure-use-cached VALUE)\" into your startup | |
135 file -- before loading filesets.el. | |
136 | |
137 So, when should you think about setting this value to t? If filesets.el | |
138 is loaded before user customizations. Thus, if (require 'filesets) | |
139 precedes the custom-set-variables command or, for XEmacs, if init.el is | |
140 loaded before custom.el, set this variable to t.") | |
141 | |
142 | |
143 ;;; utils | |
144 (defun filesets-filter-list (lst cond-fn) | |
145 "Remove all elements not conforming to COND-FN from list LST. | |
146 COND-FN takes one argument: the current element." | |
147 ; (remove* 'dummy lst :test (lambda (dummy elt) | |
148 ; (not (funcall cond-fn elt))))) | |
149 (let ((rv nil)) | |
150 (dolist (elt lst rv) | |
151 (when (funcall cond-fn elt) | |
152 (setq rv (append rv (list elt))))))) | |
153 | |
154 (defun filesets-sublist (lst beg &optional end) | |
155 "Get the sublist of LST from BEG to END - 1." | |
156 (let ((rv nil) | |
157 (i beg) | |
158 (top (or end | |
159 (length lst)))) | |
160 (while (< i top) | |
161 (setq rv (append rv (list (nth i lst)))) | |
162 (setq i (+ i 1))) | |
163 rv)) | |
164 | |
165 (defun filesets-select-command (cmd-list) | |
166 "Select one command from CMD-LIST -- a string with space separated names." | |
167 (let ((this (shell-command-to-string | |
168 (format "which --skip-alias %s 2> /dev/null | head -n 1" | |
169 cmd-list)))) | |
170 (if (equal this "") | |
171 nil | |
172 (file-name-nondirectory (substring this 0 (- (length this) 1)))))) | |
173 | |
174 (defun filesets-which-command (cmd) | |
175 "Calls \"which CMD\"." | |
176 (shell-command-to-string (format "which %s" cmd))) | |
177 | |
178 (defun filesets-which-command-p (cmd) | |
179 "Calls \"which CMD\" and returns non-nil if the command was found." | |
180 (when (string-match (format "\\(/[^/]+\\)?/%s" cmd) | |
181 (filesets-which-command cmd)) | |
182 cmd)) | |
183 | |
184 (defun filesets-message (level &rest args) | |
185 "Show a message only if LEVEL is greater or equal then `filesets-verbosity'." | |
186 (when (<= level (abs filesets-verbosity)) | |
187 (apply 'message args))) | |
188 | |
189 | |
190 ;;; config file | |
191 (defun filesets-save-config () | |
192 "Save filesets' customizations." | |
193 (interactive) | |
194 (customize-save-customized)) | |
195 | |
196 (defun filesets-reset-fileset (&optional fileset no-cache) | |
197 "Reset the cached values for one or all filesets." | |
198 (if fileset | |
199 (setq filesets-submenus (lax-plist-put filesets-submenus fileset nil)) | |
200 (setq filesets-submenus nil)) | |
201 (setq filesets-has-changed-flag t) | |
202 (setq filesets-update-cache-file-flag (or filesets-update-cache-file-flag | |
203 (not no-cache)))) | |
204 | |
205 (defun filesets-set-config (fileset var val) | |
206 "Set-default wrapper function." | |
207 (filesets-reset-fileset fileset) | |
208 (set-default var val)) | |
209 ; (customize-set-variable var val)) | |
210 ; (filesets-build-menu)) | |
211 | |
212 (defun filesets-set-default (sym val &optional init-flag) | |
213 "Set-default wrapper function used in conjunction with `defcustom'." | |
214 (let ((ignore-flag (member sym filesets-ignore-next-set-default))) | |
215 (if ignore-flag | |
216 (setq filesets-ignore-next-set-default | |
217 (delete sym filesets-ignore-next-set-default)) | |
218 (if init-flag | |
219 (custom-initialize-set sym val) | |
220 (set-default sym val))) | |
221 (not ignore-flag))) | |
222 | |
223 (defun filesets-set-default! (sym val) | |
224 "Call `filestes-set-default' and reset cached data (i.e. rebuild menu)." | |
225 (when (filesets-set-default sym val) | |
226 (filesets-reset-fileset))) | |
227 | |
228 (defun filesets-set-default+ (sym val) | |
229 "Call `filestes-set-default' and reset filesets' standard menu." | |
230 (when (filesets-set-default sym val) | |
231 (setq filesets-has-changed-flag t))) | |
232 ; (filesets-reset-fileset nil t))) | |
233 | |
234 (defun filesets-data-set-default (sym val) | |
235 "Set the default for `filesets-data'." | |
236 (if filesets-menu-use-cached-flag | |
237 (setq filesets-menu-use-cached-flag nil) | |
238 (when (default-boundp 'filesets-data) | |
239 (let ((modified-filesets | |
240 (filesets-filter-list val | |
241 (lambda (x) | |
242 (let ((name (car x)) | |
243 (data (cdr x))) | |
244 (let ((elt (assoc name filesets-data))) | |
245 (or (not elt) | |
246 (not (equal data (cdr elt)))))))))) | |
247 (dolist (x modified-filesets) | |
248 (filesets-reset-fileset (car x)))))) | |
249 (filesets-set-default sym val)) | |
250 | |
251 | |
252 | |
253 ;;; configuration | |
254 (defgroup filesets nil | |
255 "The fileset swapper." | |
256 :prefix "filesets-" | |
257 :group 'convenience) | |
258 | |
259 (defcustom filesets-menu-name "Filesets" | |
260 "*Filesets' menu name." | |
261 :set (function filesets-set-default) | |
262 :type 'sexp | |
263 :group 'filesets) | |
264 | |
265 (defcustom filesets-menu-path nil | |
266 "*The menu under which the filesets menu should be inserted. | |
267 XEmacs specific; see `add-submenu' for documentation." | |
268 :set (function filesets-set-default) | |
269 :type 'sexp | |
270 :group 'filesets) | |
271 | |
272 (defcustom filesets-menu-before "File" | |
273 "*The name of a menu before which this menu should be added. | |
274 XEmacs specific; see `add-submenu' for documentation." | |
275 :set (function filesets-set-default) | |
276 :type 'sexp | |
277 :group 'filesets) | |
278 | |
279 (defcustom filesets-menu-in-menu nil | |
280 "*Use that instead of `current-menubar' as the menu to change. | |
281 XEmacs specific; see `add-submenu' for documentation." | |
282 :set (function filesets-set-default) | |
283 :type 'sexp | |
284 :group 'filesets) | |
285 | |
286 (defcustom filesets-menu-shortcuts-flag t | |
287 "*Non-nil means to prepend menus with hopefully unique shortcuts." | |
288 :set (function filesets-set-default!) | |
289 :type 'boolean | |
290 :group 'filesets) | |
291 | |
292 (defcustom filesets-menu-shortcuts-marker "%_" | |
293 "*String for marking menu shortcuts." | |
294 :set (function filesets-set-default!) | |
295 :type 'string | |
296 :group 'filesets) | |
297 | |
298 ;(defcustom filesets-menu-cnvfp-flag nil | |
299 ; "*Non-nil means show \"Convert :pattern to :files\" entry for :pattern menus." | |
300 ; :set (function filesets-set-default!) | |
301 ; :type 'boolean | |
302 ; :group 'filesets) | |
303 | |
304 (defcustom filesets-menu-cache-file | |
305 (if filesets-running-xemacs | |
306 "~/.xemacs/filesets-cache.el" | |
307 "~/.filesets-cache.el") | |
308 "*File to be used for saving the filesets menu between (X)Emacs | |
309 sessions. Set this to \"\", to disable caching of menus. | |
310 Don't forget to check out `filesets-menu-ensure-use-cached'." | |
311 :set (function filesets-set-default) | |
312 :type 'file | |
313 :group 'filesets) | |
314 | |
315 (defcustom filesets-menu-cache-contents | |
316 '(filesets-be-docile-flag | |
317 filesets-submenus | |
318 filesets-menu-cache | |
319 filesets-ingroup-cache) | |
320 "*Stuff we want to save in `filesets-menu-cache-file'. | |
321 | |
322 Possible uses: don't save configuration data in the main startup files | |
323 but in filesets's own cache. In this case add `filesets-data' to this | |
324 list. | |
325 | |
326 There is a second reason for putting `filesets-data' on this list. If | |
327 you frequently add and remove buffers on the fly to :files filesets, you | |
328 don't need to save your customizations if `filesets-data' is being | |
329 mirrored in the cache file. In this case the version in the cache file | |
330 is the current one, and the version in your startup file will be | |
331 silently updated later on. | |
332 | |
333 If you want caching to work properly, at least `filesets-submenus', | |
334 `filesets-menu-cache', and `filesets-ingroup-cache' should be in this | |
335 list. | |
336 | |
337 Don't forget to check out `filesets-menu-ensure-use-cached'." | |
338 :set (function filesets-set-default) | |
339 :type '(repeat | |
340 (choice :tag "Variable" | |
341 (const :tag "filesets-submenus" | |
342 :value filesets-submenus) | |
343 (const :tag "filesets-menu-cache" | |
344 :value filesets-menu-cache) | |
345 (const :tag "filesets-ingroup-cache" | |
346 :value filesets-ingroup-cache) | |
347 (const :tag "filesets-data" | |
348 :value filesets-data) | |
349 (const :tag "filesets-external-viewers" | |
350 :value filesets-external-viewers) | |
351 (const :tag "filesets-ingroup-patterns" | |
352 :value filesets-ingroup-patterns) | |
353 (const :tag "filesets-be-docile-flag" | |
354 :value filesets-be-docile-flag) | |
355 (sexp :tag "Other" :value nil))) | |
356 :group 'filesets) | |
357 | |
358 (defcustom filesets-cache-fill-content-hooks nil | |
359 "*Hooks to run when writing the contents of filesets' cache file. | |
360 | |
361 The hook is called with the cache file as current buffer and the cursor | |
362 at the last position. I.e. each hook has to make sure that the cursor is | |
363 at the last position. | |
364 | |
365 Possible uses: If you don't want to save `filesets-data' in your normal | |
366 configuration file, you can add a something like this | |
367 | |
368 \(lambda () | |
369 \(insert (format \"(setq-default filesets-data '%S)\" | |
370 filesets-data)) | |
371 \(newline 2)) | |
372 | |
373 to this hook. | |
374 | |
375 Don't forget to check out `filesets-menu-ensure-use-cached'." | |
376 :set (function filesets-set-default) | |
377 :type 'hook | |
378 :group 'filesets) | |
379 | |
380 (defcustom filesets-cache-hostname-flag nil | |
381 "*Non-nil means cache the hostname. If the current name differs from | |
382 the cached one, rebuild the menu and create a new cache file." | |
383 :set (function filesets-set-default) | |
384 :type 'boolean | |
385 :group 'filesets) | |
386 | |
387 (defcustom filesets-cache-save-often-flag nil | |
388 "*Non-nil means save buffer on every change of the filesets menu. | |
389 If this variable is set to nil and if Emacs crashes, the cache and | |
390 filesets-data could get out of sync. Set this to t if this happens from | |
391 time to time or if the fileset cache causes troubles." | |
392 :set (function filesets-set-default) | |
393 :type 'boolean | |
394 :group 'filesets) | |
395 | |
396 (defcustom filesets-max-submenu-length 25 | |
397 "*Maximum length of submenus. | |
398 Set this value to 0 to turn menu splitting off. BTW, parts of submenus | |
399 will not be rewrapped if their length exceeds this value." | |
400 :set (function filesets-set-default) | |
401 :type 'integer | |
402 :group 'filesets) | |
403 | |
404 (defcustom filesets-max-entry-length 50 | |
405 "*Truncate names of splitted submenus to this length." | |
406 :set (function filesets-set-default) | |
407 :type 'integer | |
408 :group 'filesets) | |
409 | |
410 (defcustom filesets-browse-dir-fn 'dired | |
411 "*A function or command used for browsing directories. | |
412 When using an external command, \"%s\" will be replaced with the | |
413 directory's name. | |
414 | |
415 Note: You have to manually rebuild the menu if you change this value." | |
416 :set (function filesets-set-default) | |
417 :type '(choice :tag "Function:" | |
418 (const :tag "dired" | |
419 :value dired) | |
420 (list :tag "Command" | |
421 :value ("" "%s") | |
422 (string :tag "Name") | |
423 (string :tag "Arguments")) | |
424 (function :tag "Function" | |
425 :value nil)) | |
426 :group 'filesets) | |
427 | |
428 (defcustom filesets-open-file-fn 'filesets-find-or-display-file | |
429 "*The function used for opening files. | |
430 | |
431 `filesets-find-or-display-file' ... Filesets' default function for | |
432 visiting files. This function checks if an external viewer is defined | |
433 for a specific file type. Either this viewer, if defined, or | |
434 `find-file' will be used to visit a file. | |
435 | |
436 `filesets-find-file' ... An alternative function that always uses | |
437 `find-file'. If `filesets-be-docile-flag' is true, a file, which isn't | |
438 readable, will not be opened. | |
439 | |
440 Caveat: Changes will take effect only after rebuilding the menu." | |
441 :set (function filesets-set-default) | |
442 :type '(choice :tag "Function:" | |
443 (const :tag "filesets-find-or-display-file" | |
444 :value filesets-find-or-display-file) | |
445 (const :tag "filesets-find-file" | |
446 :value filesets-find-file) | |
447 (function :tag "Function" | |
448 :value nil)) | |
449 :group 'filesets) | |
450 | |
451 (defcustom filesets-save-buffer-fn 'save-buffer | |
452 "*The function used to save a buffer. | |
453 Caveat: Changes will take effect after rebuilding the menu." | |
454 :set (function filesets-set-default) | |
455 :type '(choice :tag "Function:" | |
456 (const :tag "save-buffer" | |
457 :value save-buffer) | |
458 (function :tag "Function" | |
459 :value nil)) | |
460 :group 'filesets) | |
461 | |
462 (defcustom filesets-find-file-delay | |
463 (if (and filesets-running-xemacs gutter-buffers-tab-visible-p) | |
464 0.5 | |
465 0) | |
466 "*Delay before calling find-file. | |
467 This is for calls via `filesets-find-or-display-file' | |
468 or `filesets-find-file'. | |
469 | |
470 Set this to 0, if you don't use XEmacs' buffer tabs." | |
471 :set (function filesets-set-default) | |
472 :type 'number | |
473 :group 'filesets) | |
474 | |
475 (defcustom filesets-be-docile-flag nil | |
476 "*Non-nil means don't complain if a file or a directory doesn't exist. | |
477 This is useful if you want to use the same startup files in different | |
478 computer environments." | |
479 :set (function filesets-set-default) | |
480 :type 'boolean | |
481 :group 'filesets) | |
482 | |
483 (defcustom filesets-sort-menu-flag t | |
484 "*Non-nil means sort the filesets menu alphabetically." | |
485 :set (function filesets-set-default) | |
486 :type 'boolean | |
487 :group 'filesets) | |
488 | |
489 (defcustom filesets-sort-case-sensitive-flag t | |
490 "*Non-nil means sorting of the filesete menu is case sensitive." | |
491 :set (function filesets-set-default) | |
492 :type 'boolean | |
493 :group 'filesets) | |
494 | |
495 (defcustom filesets-tree-max-level 3 | |
496 "*Maximum scan depth for directory trees. | |
497 A :tree fileset is defined by a base directory the contents of which | |
498 will be recursively added to the menu. filesets-tree-max-level tells up | |
499 to which level the directory structure should be scanned/listed, | |
500 i.e. how deep the menu should be. Try something like | |
501 | |
502 \(\"HOME -- only one level\" | |
503 \(:tree \"~\" \"^[^.].*[^~]$\") | |
504 \(:tree-max-level 1) | |
505 \(:filter-dirs-flag t)) | |
506 \(\"HOME -- up to 3 levels\" | |
507 \(:tree \"~\" \"^[^.].*[^~]$\") | |
508 \(:tree-max-level 3) | |
509 \(:filter-dirs-flag t)) | |
510 | |
511 and it should become clear what this option is about. In any case, | |
512 including directory trees to the menu can take a lot of memory." | |
513 :set (function filesets-set-default) | |
514 :type 'integer | |
515 :group 'filesets) | |
516 | |
517 (defcustom filesets-commands | |
518 `(("Query Replace" | |
519 query-replace | |
520 (filesets-cmd-query-replace-getargs)) | |
521 ("Query Replace (regexp)" | |
522 query-replace-regexp | |
523 (filesets-cmd-query-replace-getargs)) | |
524 ("Grep <<selection>>" | |
525 "grep" | |
526 ("-n " filesets-get-quoted-selection " " "<<file-name>>")) | |
527 ("Run Shell Command" | |
528 filesets-cmd-shell-command | |
529 (filesets-cmd-shell-command-getargs))) | |
530 "*Commands to run on filesets. | |
531 An association list of names, functions, and an argument list (or a | |
532 function that returns one) to be run on a filesets' files. | |
533 | |
534 The argument <file-name> or <<file-name>> (quoted) will be replaced with | |
535 the filename." | |
536 :set (function filesets-set-default+) | |
537 :type '(repeat :tag "Commands" | |
538 (list :tag "Definition" :value ("") | |
539 (string "Name") | |
540 (choice :tag "Command" | |
541 (string :tag "String") | |
542 (function :tag "Function")) | |
543 (repeat :tag "Argument List" | |
544 (choice :tag "Arguments" | |
545 (sexp :tag "Sexp" | |
546 :value nil) | |
547 (string :tag "File Name" | |
548 :value "<file-name>") | |
549 (string :tag "Quoted File Name" | |
550 :value "<<file-name>>") | |
551 (function :tag "Function" | |
552 :value nil))))) | |
553 :group 'filesets) | |
554 | |
555 (defcustom filesets-external-viewers | |
556 (let | |
557 ; ((ps-cmd (or (and (boundp 'my-ps-viewer) my-ps-viewer) | |
558 ; (filesets-select-command "ggv gv"))) | |
559 ; (pdf-cmd (or (and (boundp 'my-ps-viewer) my-pdf-viewer) | |
560 ; (filesets-select-command "xpdf acroread"))) | |
561 ; (dvi-cmd (or (and (boundp 'my-ps-viewer) my-dvi-viewer) | |
562 ; (filesets-select-command "xdvi tkdvi"))) | |
563 ; (doc-cmd (or (and (boundp 'my-ps-viewer) my-doc-viewer) | |
564 ; (filesets-select-command "antiword"))) | |
565 ; (pic-cmd (or (and (boundp 'my-ps-viewer) my-pic-viewer) | |
566 ; (filesets-select-command "gqview ee display")))) | |
567 ((ps-cmd "ggv") | |
568 (pdf-cmd "xpdf") | |
569 (dvi-cmd "xdvi") | |
570 (doc-cmd "antiword") | |
571 (pic-cmd "gqview")) | |
572 `(("^.+\\..?html?$" browse-url | |
573 ((:ignore-on-open-all t))) | |
574 ("^.+\\.pdf$" ,pdf-cmd | |
575 ((:ignore-on-open-all t) | |
576 (:ignore-on-read-text t) | |
577 (:constraint-flag ,pdf-cmd))) | |
578 ("^.+\\.e?ps\\(.gz\\)?$" ,ps-cmd | |
579 ((:ignore-on-open-all t) | |
580 (:ignore-on-read-text t) | |
581 (:constraint-flag ,ps-cmd))) | |
582 ("^.+\\.dvi$" ,dvi-cmd | |
583 ((:ignore-on-open-all t) | |
584 (:ignore-on-read-text t) | |
585 (:constraint-flag ,dvi-cmd))) | |
586 ("^.+\\.doc$" ,doc-cmd | |
587 ((:capture-output t) | |
588 (:ignore-on-read-text t) | |
589 (:constraint-flag ,doc-cmd))) | |
590 ("^.+\\.\\(tiff\\|xpm\\|gif\\|pgn\\)$" ,pic-cmd | |
591 ((:ignore-on-open-all t) | |
592 (:ignore-on-read-text t) | |
593 (:constraint-flag ,pic-cmd))))) | |
594 "*Association list of file patterns and external viewers for use with | |
595 `filesets-find-or-display-file'. | |
596 | |
597 Has the form ((FILE-PATTERN VIEWER PROPERTIES) ...), VIEWER being either a | |
598 function or a command name as string. | |
599 | |
600 Properties is an association list determining filesets' behaviour in | |
601 several conditions. Choose one from this list: | |
602 | |
603 :ignore-on-open-all ... Don't open files of this type automatically -- | |
604 i.e. on open-all-files-events or when running commands | |
605 | |
606 :capture-output ... capture an external viewer output | |
607 | |
608 :constraintp FUNCTION ... use this viewer only if FUNCTION returns non-nil | |
609 | |
610 :constraint-flag SYMBOL ... use this viewer only if SYMBOL is non-nil | |
611 | |
612 :open-hook HOOK ... run hooks after spawning the viewer -- mainly useful | |
613 in conjunction with :capture-output | |
614 | |
615 :args (FORMAT-STRING or SYMBOL or FUNCTION) ... a list of arguments | |
616 \(defaults to (list \"%S\")) when using shell commands | |
617 | |
618 Avoid modifying this variable and achieve minor speed-ups by setting the | |
619 variables my-ps-viewer, my-pdf-viewer, my-dvi-viewer, my-pic-viewer. | |
620 | |
621 In order to view pdf or rtf files in an Emacs buffer, you could use these: | |
622 | |
623 | |
624 \(\"^.+\\.pdf$\" \"pdftotext\" | |
625 \((:capture-output t) | |
626 \(:args (\"%S - | fmt -w \" window-width)) | |
627 \(:ignore-on-read-text t) | |
628 \(:constraintp (lambda () | |
629 \(and \(filesets-which-command-p \"pdftotext\") | |
630 \(filesets-which-command-p \"fmt\")))))) | |
631 \(\"^.+\\.rtf$\" \"rtf2htm\" | |
632 \((:capture-output t) | |
633 \(:args (\"%S 2> /dev/null | w3m -dump -T text/html\")) | |
634 \(:ignore-on-read-text t) | |
635 \(:constraintp (lambda () | |
636 \(and (filesets-which-command-p \"rtf2htm\") | |
637 \(filesets-which-command-p \"w3m\")))))) | |
638 " | |
639 :set (function filesets-set-default) | |
640 :type '(repeat :tag "Viewer" | |
641 (list :tag "Definition" | |
642 :value ("^.+\\.suffix$" "") | |
643 (regexp :tag "Pattern") | |
644 (choice :tag "Viewer" | |
645 (symbol :tag "Function" :value nil) | |
646 (string :tag "Program" :value "")) | |
647 (repeat :tag "Properties" | |
648 (choice | |
649 (list :tag ":constraintp" | |
650 :value (:constraintp) | |
651 (const :format "" | |
652 :value :constraintp) | |
653 (function :tag "Function")) | |
654 (list :tag ":constraint-flag" | |
655 :value (:constraint-flag) | |
656 (const :format "" | |
657 :value :constraint-flag) | |
658 (symbol :tag "Symbol")) | |
659 (list :tag ":ignore-on-open-all" | |
660 :value (:ignore-on-open-all t) | |
661 (const :format "" | |
662 :value :ignore-on-open-all) | |
663 (boolean :tag "Boolean")) | |
664 (list :tag ":ignore-on-read-text" | |
665 :value (:ignore-on-read-text t) | |
666 (const :format "" | |
667 :value :ignore-on-read-text) | |
668 (boolean :tag "Boolean")) | |
669 (list :tag ":args" | |
670 :value (:args) | |
671 (const :format "" | |
672 :value :args) | |
673 (repeat :tag "List" | |
674 (choice :tag "Arguments" | |
675 (string :tag "String" | |
676 :value "") | |
677 (symbol :tag "Symbol" | |
678 :value nil) | |
679 (function :tag "Function" | |
680 :value nil)))) | |
681 (list :tag ":open-hook" | |
682 :value (:open-hook) | |
683 (const :format "" | |
684 :value :open-hook) | |
685 (hook :tag "Hook")) | |
686 ; (list :tag ":close-hook" | |
687 ; :value (:close-hook) | |
688 ; (const :format "" | |
689 ; :value :close-hook) | |
690 ; (hook :tag "Hook")) | |
691 (list :tag ":capture-output" | |
692 :value (:capture-output t) | |
693 (const :format "" | |
694 :value :capture-output) | |
695 (boolean :tag "Boolean")))))) | |
696 :group 'filesets) | |
697 | |
698 | |
699 (defcustom filesets-ingroup-patterns | |
700 '(("^.+\\.tex$" t | |
701 (((:name "Package") | |
702 (:pattern "\\\\usepackage\\W*\\(\\[[^\]]*\\]\\W*\\)?{\\W*\\(.+\\)\\W*}") | |
703 (:match-number 2) | |
704 (:stub-flag t) | |
705 (:get-file-name (lambda (master file) | |
706 (filesets-which-file master | |
707 (concat file ".sty") | |
708 (filesets-convert-path-list | |
709 (or (getenv "MY_TEXINPUTS") | |
710 (getenv "TEXINPUTS"))))))) | |
711 ((:name "Include") | |
712 (:pattern "\\\\include\\W*{\\W*\\(.+\\)\\W*}") | |
713 (:get-file-name (lambda (master file) | |
714 (filesets-which-file master | |
715 (concat file ".tex") | |
716 (filesets-convert-path-list | |
717 (or (getenv "MY_TEXINPUTS") | |
718 (getenv "TEXINPUTS")))))) | |
719 (:scan-depth 5)) | |
720 ((:name "Input") | |
721 (:pattern "\\\\input\\W*{\\W*\\(.+\\)\\W*}") | |
722 (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b)))) | |
723 (:get-file-name (lambda (master file) | |
724 (filesets-which-file master | |
725 (concat file ".tex") | |
726 (filesets-convert-path-list | |
727 (or (getenv "MY_TEXINPUTS") | |
728 (getenv "TEXINPUTS")))))) | |
729 (:scan-depth 5)) | |
730 ((:name "Bibliography") | |
731 (:pattern "\\\\bibliography\\W*{\\W*\\(.+\\)\\W*}") | |
732 (:get-file-name (lambda (master file) | |
733 (filesets-which-file master | |
734 (concat file ".bib") | |
735 (filesets-convert-path-list | |
736 (or (getenv "MY_BIBINPUTS") | |
737 (getenv "BIBINPUTS"))))))))) | |
738 ("^.+\\.el$" t | |
739 (((:name "Require") | |
740 (:pattern "(require\\W+'\\(.+\\))") | |
741 (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b)))) | |
742 (:get-file-name (lambda (master file) | |
743 (filesets-which-file master | |
744 (concat file ".el") | |
745 load-path)))) | |
746 ((:name "Load") | |
747 (:pattern "(load\\(-library\\)?\\W+\"\\(.+\\)\")") | |
748 (:match-number 2) | |
749 (:get-file-name (lambda (master file) | |
750 (filesets-which-file master file load-path)))))) | |
751 ("^\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)$" t | |
752 (((:pattern "\\<\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)\\>") | |
753 (:scan-depth 5) | |
754 (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b)))) | |
755 (:case-sensitive t) | |
756 (:get-file-name (lambda (master file) | |
757 (filesets-which-file | |
758 master | |
759 file | |
760 (if (boundp 'emacs-wiki-directories) | |
761 emacs-wiki-directories | |
762 nil)))))))) | |
763 | |
764 "*Inclusion group definitions. | |
765 | |
766 Define how to find included file according to a file's mode (being | |
767 defined by a file pattern). | |
768 | |
769 A valid entry has the form (FILE-PATTERN REMOVE-DUPLICATES-FLAG | |
770 CMD-DEF1 ...), CMD-DEF1 being a plist containing the fields :pattern | |
771 \(mandatory), :name, :get-file-name, :match-number, :scan-depth, | |
772 :preprocess, :case-sensitive. | |
773 | |
774 File Pattern ... A regexp matching the file's name for which the | |
775 following rules should be applied. | |
776 | |
777 Remove Duplicates ... If t, only the first occurrence of an included | |
778 file is retained. (See below for a full explanation.) | |
779 | |
780 :name STRING ... This pattern's name. | |
781 | |
782 :pattern REGEXP ... A regexp matching the command. This regexp has to | |
783 include a group that holds the name of the included file. | |
784 | |
785 :get-file-name FUNCTION (default: `filesets-which-file') ... A function | |
786 that takes two arguments (the path of the master file and the name | |
787 of the included file) and returns a valid path or nil -- if the | |
788 subfile can't be found. | |
789 | |
790 :match-number INTEGER (default: 1) ... The number of the match/group | |
791 in the pattern holding the subfile's name. 0 refers the whole | |
792 match, 1 to the first group. | |
793 | |
794 :stubp FUNCTION ... if (FUNCTION MASTER INCLUDED-FILE) returns non-nil, | |
795 INCLUDED-FILE is a stub -- see below. | |
796 | |
797 :stub-flag ... files of this type are stubs -- see below. | |
798 | |
799 :scan-depth INTEGER (default: 0) ... Whether included files should be | |
800 rescanned. Set this to 0 to disable re-scanning of included file. | |
801 | |
802 :preprocess FUNCTION ... A function modifying a buffer holding the | |
803 master file so that pattern matching becomes easier. This is usually | |
804 used to narrow a buffer to the relevant region. This function could also | |
805 be destructive and simply delete non-relevant text. | |
806 | |
807 :case-sensitive BOOLEAN (default: nil) ... Whether a pattern is | |
808 case-sensitive or not. | |
809 | |
810 | |
811 Stubs: | |
812 | |
813 First, a stub is a file that shows up in the menu but will not be | |
814 included in an ingroup's file listing -- i.e. filesets will never | |
815 operate on this file automatically. Secondly, in opposition to normal | |
816 files stubs are not scanned for new inclusion groups. This is useful if | |
817 you want to have quick access to library headers. | |
818 | |
819 In the menu, an asterisk is appended to the stub's name. | |
820 | |
821 | |
822 Remove Duplicates: | |
823 | |
824 E.g. File A and file B refer to file X; X refers to A. If | |
825 you choose not to remove duplicates the tree would look like: | |
826 | |
827 M + A - X - A ... | |
828 B - X - A ... | |
829 | |
830 As you can see, there is some chance that you run in circles. | |
831 Nevertheless, up to some degree this could still be what you want. | |
832 | |
833 With duplicates removed, it would be: | |
834 | |
835 M + A - X | |
836 B" | |
837 :set (function filesets-set-default) | |
838 :type '(repeat | |
839 :tag "Include" | |
840 (list | |
841 :tag "Definition" :value ("^.+\\.suffix$" t) | |
842 (regexp :tag "File Pattern" :value "^.+\\.suffix$") | |
843 (boolean :tag "Remove Duplicates" :value t) | |
844 (repeat :tag "Commands" | |
845 (repeat :tag "Command" | |
846 (choice | |
847 :tag "Definition" | |
848 (list :tag ":name" | |
849 :value (:name "") | |
850 (const :format "" :value :name) | |
851 (string :tag "String")) | |
852 (list :tag ":pattern" | |
853 :value (:pattern "\\<CMD\\W*\\(.+\\)\\>") | |
854 (const :format "" :value :pattern) | |
855 (regexp :tag "RegExp")) | |
856 (list :tag ":get-file-name" | |
857 :value (:get-file-name) | |
858 (const :format "" :value :get-file-name) | |
859 (function :tag "Function")) | |
860 (list :tag ":match-number" | |
861 :value (:match-number 1) | |
862 (const :format "" :value :match-number) | |
863 (integer :tag "Integer")) | |
864 (list :tag ":stub-flag" | |
865 :value (:stub-flag t) | |
866 (const :format "" :value :stub-flag) | |
867 (boolean :tag "Boolean")) | |
868 (list :tag ":stubp" | |
869 :value (:stubp) | |
870 (const :format "" :value :stubp) | |
871 (function :tag "Function")) | |
872 (list :tag ":scan-depth" | |
873 :value (:scan-depth 0) | |
874 (const :format "" :value :scan-depth) | |
875 (integer :tag "Integer")) | |
876 (list :tag ":case-sensitive" | |
877 :value (:case-sensitive) | |
878 (const :format "" :value :case-sensitive) | |
879 (boolean :tag "Boolean")) | |
880 (list :tag ":preprocess" | |
881 :value (:preprocess) | |
882 (const :format "" :value :preprocess) | |
883 (function :tag "Function"))))))) | |
884 :group 'filesets) | |
885 | |
886 (defcustom filesets-data | |
887 nil | |
888 "*Fileset definitions. | |
889 | |
890 A fileset is either a list of files, a file pattern, a base directory | |
891 and a search pattern (for files), or a base file. Changes to this | |
892 variable will take effect after rebuilding the menu. | |
893 | |
894 Caveat: Fileset names have to be unique. | |
895 | |
896 Example definition: | |
897 '\(\(\"My Wiki\" | |
898 \(:ingroup \"~/Etc/My-Wiki/WikiContents\")) | |
899 \(\"My Homepage\" | |
900 \(:pattern \"~/public_html/\" \"^.+\\\\.html$\") | |
901 \(:open filesets-find-file)) | |
902 \(\"User Configuration\" | |
903 \(:files \"~/.xinitrc\" | |
904 \"~/.bashrc\" | |
905 \"~/.bash_profile\")) | |
906 \(\"HOME\" | |
907 \(:tree \"~\" \"^[^.].*[^~]$\") | |
908 \(:filter-dirs-flag t))) | |
909 | |
910 `filesets-data' is a list of (NAME-AS-STRING . DEFINITION), DEFINITION | |
911 being an association list with the fields: | |
912 | |
913 :files FILE-1 .. FILE-N ... a list of files belonging to a fileset | |
914 | |
915 :ingroup FILE-NAME ... an inclusion group's base file. | |
916 | |
917 :tree ROOT-DIR PATTERN ... a base directory and a file pattern | |
918 | |
919 :pattern DIR PATTERN ... PATTERN is a regular expression comprising path | |
920 and file pattern -- e.g. 'PATH/^REGEXP$'. Note the `^' at the beginning | |
921 of the file name pattern. | |
922 | |
923 :filter-dirs-flag BOOLEAN ... is only used in conjunction with :tree. | |
924 | |
925 :tree-max-level INTEGER ... recurse into directories this many levels | |
44959 | 926 \(see `filesets-tree-max-level' for a full explanation) |
44934 | 927 |
928 :dormant-flag BOOLEAN ... non-nil means don't show this item in the | |
929 menu; dormant filesets can still be manipulated via commands available | |
930 from the minibuffer -- e.g. `filesets-open', `filesets-close', or | |
931 `filesets-run-cmd' | |
932 | |
933 :dormant-p FUNCTION ... a function returning :dormant-flag | |
934 | |
935 :open FUNCTION ... the function used to open file belonging to this | |
936 fileset. The function takes a file name as argument | |
937 | |
938 :save FUNCTION ... the function used to save file belonging to this | |
939 fileset; it takes no arguments, but works on the current buffer. | |
940 | |
941 Either :files, :pattern, :tree, or :ingroup must be supplied. :files | |
942 overrules :tree, :tree overrules :pattern, :pattern overrules :ingroup, | |
943 i.e. these tags are mutually exclusive. The fields :open and :save are | |
944 optional. | |
945 | |
946 In conjunction with the :tree tag, :save is void. :open refers to the | |
947 function used for opening files in a directory, not for opening the | |
948 directory. For browsing directories, `filesets-browse-dir-fn' is used. | |
949 | |
950 Before using :ingroup, make sure that the file type is already | |
951 defined in `filesets-ingroup-patterns'." | |
952 :group 'filesets | |
953 :set (function filesets-data-set-default) | |
954 :type '(repeat | |
955 (cons :tag "Fileset" | |
956 (string :tag "Name" :value "") | |
957 (repeat :tag "Data" | |
958 (choice | |
959 :tag "Type" :value nil | |
960 (list :tag "Pattern" | |
961 :value (:pattern "~/" "^.+\\.suffix$") | |
962 (const :format "" :value :pattern) | |
963 (directory :tag "Dir") | |
964 (regexp :tag "Pattern")) | |
965 (cons :tag "Files" | |
966 :value (:files) | |
967 (const :format "" :value :files) | |
968 (repeat :tag "Files" file)) | |
969 (list :tag "Single File" | |
970 :value (:file "~/") | |
971 (const :format "" :value :file) | |
972 (file :tag "File")) | |
973 (list :tag "Inclusion group" | |
974 :value (:ingroup "~/") | |
975 (const :format "" :value :ingroup) | |
976 (file :tag "File" :value "~/")) | |
977 (list :tag "Directory Tree" | |
978 :value (:tree "~/" "^.+\\.suffix$") | |
979 (const :format "" :value :tree) | |
980 (directory :tag "Dir") | |
981 (regexp :tag "Pattern")) | |
982 (list :tag "Filter directories" | |
983 :value (:filter-dirs-flag) | |
984 (const :format "" :value :filter-dirs-flag) | |
985 (boolean :tag "Boolean" :value nil)) | |
986 (list :tag "Scanning depth" | |
987 :value (:tree-max-level 3) | |
988 (const :format "" :value :tree-max-level) | |
989 (integer :tag "Integer")) | |
990 (list :tag "Verbosity" | |
991 :value (:verbosity 1) | |
992 (const :format "" :value :verbosity) | |
993 (integer :tag "Integer")) | |
994 (list :tag "Conceal fileset (Flag)" | |
995 :value (:dormant-flag) | |
996 (const :format "" :value :dormant-flag) | |
997 (boolean :tag "Boolean")) | |
998 (list :tag "Conceal fileset (Function)" | |
999 :value (:dormant-p) | |
1000 (const :format "" :value :dormant-p) | |
1001 (function :tag "Function")) | |
1002 (list :tag "Save function" | |
1003 :value (:save) | |
1004 (const :format "" :value :save) | |
1005 (function :tag "Function")) | |
1006 (list :tag "Open function" | |
1007 :value (:open) | |
1008 (const :format "" :value :open) | |
1009 (function :tag "Function"))))))) | |
1010 | |
1011 | |
1012 (defcustom filesets-query-user-limit 15 | |
1013 "*Query the user before opening a fileset with that many files." | |
1014 :set (function filesets-set-default) | |
1015 :type 'integer | |
1016 :group 'filesets) | |
1017 | |
1018 | |
1019 ;;; Emacs compatibility | |
1020 (eval-and-compile | |
1021 (if filesets-running-xemacs | |
1022 (progn | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1023 (fset 'filesets-error 'error) |
44934 | 1024 (fset 'filesets-add-submenu 'add-submenu)) |
1025 | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1026 (require 'easymenu) |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1027 |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1028 (defun filesets-error (class &rest args) |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1029 "`error' wrapper." |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1030 (error (mapconcat 'identity args " "))) |
44934 | 1031 |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1032 ;; This should work for 21.1 Emacs |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1033 (defun filesets-add-submenu (menu-path submenu &optional |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1034 before in-menu) |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1035 "`easy-menu-define' wrapper." |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1036 (easy-menu-define |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1037 filesets-submenu global-map "Filesets menu" submenu)) |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1038 )) |
44934 | 1039 |
1040 (defun filesets-filter-dir-names (lst &optional negative) | |
1041 "Remove non-directory names from a list of strings. If NEGATIVE is | |
1042 non-nil, remove all directory names." | |
1043 (filesets-filter-list lst | |
1044 (lambda (x) | |
1045 (and (not (string-match "^\\.+/$" x)) | |
1046 (if negative | |
1047 (not (string-match "[:/\\]$" x)) | |
1048 (string-match "[:/\\]$" x)))))) | |
1049 | |
1050 (defun filesets-conditional-sort (lst &optional access-fn simply-do-it) | |
1051 "Return a sorted copy of LST, LST being a list of strings. | |
1052 If `filesets-sort-menu-flag' is nil, return LST itself. | |
1053 | |
1054 ACCESS-FN ... function to get the string value of LST's elements. | |
1055 | |
1056 If SIMPLY-DO-IT is non-nil, the list is sorted regardless of | |
1057 `filesets-sort-menu-flag'." | |
1058 (if filesets-sort-menu-flag | |
1059 (let* ((fni (or access-fn | |
1060 (function identity))) | |
1061 (fn (if filesets-sort-case-sensitive-flag | |
1062 (lambda (a b) | |
1063 (string< (funcall fni a) | |
1064 (funcall fni b))) | |
1065 (lambda (a b) | |
1066 (string< (upcase (funcall fni a)) | |
1067 (upcase (funcall fni b))))))) | |
1068 (sort (copy-list lst) fn)) | |
1069 lst)) | |
1070 | |
1071 (defun filesets-directory-files (dir &optional | |
1072 pattern what full-flag match-dirs-flag) | |
1073 "Get WHAT (:files or :dirs) in DIR. If PATTERN is provided return only | |
1074 those entries matching this regular expression. If MATCH-DIRS-FLAG is | |
1075 non-nil, also match directory entries. Return full path if FULL-FLAG is | |
1076 non-nil." | |
1077 (filesets-message 2 "Filesets: scanning %S" dir) | |
1078 (cond | |
1079 ((file-exists-p dir) | |
1080 (let ((files nil) | |
1081 (dirs nil)) | |
1082 (dolist (this (file-name-all-completions "" dir)) | |
1083 (cond | |
1084 ((string-match "^\\.+/$" this) | |
1085 nil) | |
1086 ((string-match "[:/\\]$" this) | |
1087 (when (or (not match-dirs-flag) | |
1088 (not pattern) | |
1089 (string-match pattern this)) | |
1090 (filesets-message 5 "Filesets: matched dir %S with pattern %S" | |
1091 this pattern) | |
1092 (setq dirs (cons this dirs)))) | |
1093 (t | |
1094 (when (or (not pattern) | |
1095 (string-match pattern this)) | |
1096 (filesets-message 5 "Filesets: matched file %S with pattern %S" | |
1097 this pattern) | |
1098 (setq files (cons (if full-flag | |
1099 (concat (file-name-as-directory dir) this) | |
1100 this) | |
1101 files)))))) | |
1102 (cond | |
1103 ((equal what ':dirs) | |
1104 (filesets-conditional-sort dirs)) | |
1105 ((equal what ':files) | |
1106 (filesets-conditional-sort files)) | |
1107 (t | |
1108 (append (filesets-conditional-sort files) | |
1109 (filesets-conditional-sort dirs)))))) | |
1110 (filesets-be-docile-flag | |
1111 (filesets-message 1 "Filesets: %S doesn't exist" dir) | |
1112 nil) | |
1113 (t | |
1114 (filesets-error 'error "Filesets: " dir " does not exist")))) | |
1115 | |
1116 (defun filesets-quote (txt) | |
1117 "Return TXT in quotes." | |
1118 (concat "\"" txt "\"")) | |
1119 | |
1120 (defun filesets-get-selection () | |
1121 "Get the text between mark and point -- i.e. the selection or region." | |
1122 (let ((m (mark)) | |
1123 (p (point))) | |
1124 (if m | |
1125 (buffer-substring (min m p) (max m p)) | |
1126 (filesets-error 'error "No selection.")))) | |
1127 | |
1128 (defun filesets-get-quoted-selection () | |
1129 "Return the currently selected text in quotes." | |
1130 (filesets-quote (filesets-get-selection))) | |
1131 | |
1132 (defun filesets-get-shortcut (n) | |
1133 "Create menu shortcuts based on number N." | |
1134 (let ((n (mod (- n 1) 51))) | |
1135 (cond | |
1136 ((not filesets-menu-shortcuts-flag) | |
1137 "") | |
1138 ((<= n 9) | |
1139 (concat (number-to-string n) " ")) | |
1140 ((<= n 35) | |
1141 (format "%c " (+ 87 n))) | |
1142 ((<= n 51) | |
1143 (format "%c " (+ -3 n)))))) | |
1144 | |
1145 (defun filesets-files-equalp (a b) | |
1146 "Compare two filenames A and B after expansion." | |
1147 (equal (expand-file-name a) (expand-file-name b))) | |
1148 | |
1149 (defun filesets-files-in-same-directory-p (a b) | |
1150 "Compare two filenames A and B after expansion." | |
1151 (let ((ad (file-name-directory (expand-file-name a))) | |
1152 (bd (file-name-directory (expand-file-name b)))) | |
1153 (equal ad bd))) | |
1154 | |
1155 (defun filesets-convert-path-list (string) | |
1156 "Return a path-list given as STRING as list." | |
1157 (if string | |
1158 (mapcar (lambda (x) (file-name-as-directory x)) | |
1159 (split-string string path-separator)) | |
1160 nil)) | |
1161 | |
1162 (defun filesets-which-file (master filename &optional path-list) | |
1163 "Search for a FILENAME relative to a MASTER file in PATH-LIST." | |
1164 (let ((f (concat (file-name-directory master) | |
1165 filename))) | |
1166 (if (file-exists-p f) | |
1167 f | |
1168 (some (lambda (dir) | |
1169 (let ((dir (file-name-as-directory dir)) | |
1170 (files (if (file-exists-p dir) | |
1171 (filesets-directory-files dir nil ':files) | |
1172 nil))) | |
1173 (some (lambda (file) | |
1174 (if (equal filename (file-name-nondirectory file)) | |
1175 (concat dir file) | |
1176 nil)) | |
1177 files))) | |
1178 path-list)))) | |
1179 | |
1180 | |
1181 (defun filesets-eviewer-get-props (entry) | |
1182 "Get ENTRY's (representing an external viewer) properties." | |
1183 (nth 2 entry)) | |
1184 | |
1185 (defun filesets-eviewer-constraint-p (entry) | |
1186 (let* ((props (filesets-eviewer-get-props entry)) | |
1187 (constraint (assoc ':constraintp props)) | |
1188 (constraint-flag (assoc ':constraint-flag props))) | |
1189 (cond | |
1190 (constraint | |
1191 (funcall (cadr constraint))) | |
1192 (constraint-flag | |
1193 (eval (cadr constraint-flag))) | |
1194 (t | |
1195 t)))) | |
1196 | |
1197 (defun filesets-get-external-viewer (file) | |
1198 "Find an external viewer for FILE." | |
1199 (let ((filename (file-name-nondirectory file))) | |
1200 (some | |
1201 (lambda (entry) | |
1202 (when (and (string-match (nth 0 entry) filename) | |
1203 (filesets-eviewer-constraint-p entry)) | |
1204 entry)) | |
1205 filesets-external-viewers))) | |
1206 | |
1207 (defun filesets-get-external-viewer-by-name (name) | |
1208 "Get the external viewer definition called NAME." | |
1209 (when name | |
1210 (some | |
1211 (lambda (entry) | |
1212 (when (and (string-equal (nth 1 entry) name) | |
1213 (filesets-eviewer-constraint-p entry)) | |
1214 entry)) | |
1215 filesets-external-viewers))) | |
1216 | |
1217 (defun filesets-filetype-property (filename event &optional entry) | |
1218 "Returns non-nil if a file of a specific type has special flags/tags. | |
1219 | |
1220 Events (corresponding tag): | |
1221 | |
1222 on-open-all (:ignore-on-open-all) ... Exclude files of this when opening | |
1223 a fileset | |
1224 | |
1225 on-grep (:ignore-on-read-text) ... Exclude files of this when running | |
1226 the \"Grep <<selection>>\" command | |
1227 | |
1228 on-capture-output (:capture-output) ... Capture output of an external viewer | |
1229 | |
1230 on-ls ... not used | |
1231 | |
1232 on-cmd ... not used | |
1233 | |
1234 on-close-all ... not used" | |
1235 (let ((def (filesets-eviewer-get-props | |
1236 (or entry | |
1237 (filesets-get-external-viewer filename))))) | |
1238 (filesets-alist-get def | |
1239 (case event | |
1240 ((on-open-all) ':ignore-on-open-all) | |
1241 ((on-grep) ':ignore-on-read-text) | |
1242 ((on-cmd) nil) | |
1243 ((on-close-all) nil)) | |
1244 nil t))) | |
1245 | |
1246 (defun filesets-filetype-get-prop (property filename &optional entry) | |
1247 "Returns PROPERTY for filename -- use ENTRY if provided." | |
1248 (let ((def (filesets-eviewer-get-props | |
1249 (or entry | |
1250 (filesets-get-external-viewer filename))))) | |
1251 (when def | |
1252 (filesets-alist-get def property nil t)))) | |
1253 | |
1254 (defun filesets-reset-filename-on-change () | |
1255 "Reset a buffer's filename if the buffer is being modified." | |
1256 (when filesets-output-buffer-flag | |
1257 (set-visited-file-name nil t))) | |
1258 | |
1259 (defun filesets-spawn-external-viewer (file &optional ev-entry) | |
1260 "Start an external viewer for FILE. | |
1261 Use the viewer defined in EV-ENTRY (a valid element of | |
1262 `filesets-external-viewers') if provided." | |
1263 (let* ((file (expand-file-name file)) | |
1264 (entry (or ev-entry | |
1265 (filesets-get-external-viewer file)))) | |
1266 (if entry | |
1267 (let* ((vwr (cadr entry)) | |
1268 (co-flag (filesets-filetype-get-prop ':capture-output file entry)) | |
1269 (oh (filesets-filetype-get-prop ':open-hook file entry)) | |
1270 (args (let ((fmt (filesets-filetype-get-prop ':args file entry))) | |
1271 (if fmt | |
1272 (let ((rv "")) | |
1273 (dolist (this fmt rv) | |
1274 (setq rv (concat rv | |
1275 (cond | |
1276 ((stringp this) | |
1277 (format this file)) | |
1278 ((and (symbolp this) | |
1279 (fboundp this)) | |
1280 (format "%S" (funcall this))) | |
1281 (t | |
1282 (format "%S" this))))))) | |
1283 (format "%S" file)))) | |
1284 (output | |
1285 (cond | |
1286 ((and (functionp vwr) co-flag) | |
1287 (funcall vwr file)) | |
1288 ((functionp vwr) | |
1289 (funcall vwr file) | |
1290 nil) | |
1291 (co-flag | |
1292 (shell-command-to-string (format "%s %s" vwr args))) | |
1293 (t | |
1294 (shell-command (format "%s %s&" vwr args)) | |
1295 nil)))) | |
1296 (if co-flag | |
1297 (progn | |
1298 (switch-to-buffer (format "Filesets: %s %s" vwr file)) | |
1299 (insert output) | |
1300 (make-local-variable 'filesets-output-buffer-flag) | |
1301 (setq filesets-output-buffer-flag t) | |
1302 (set-visited-file-name file t) | |
1303 (when oh | |
1304 (run-hooks 'oh)) | |
1305 (set-buffer-modified-p nil) | |
1306 (setq buffer-read-only t) | |
1307 (beginning-of-buffer)) | |
1308 (when oh | |
1309 (run-hooks 'oh)))) | |
1310 (filesets-error 'error | |
1311 "Filesets: general error when spawning external viewer")))) | |
1312 | |
1313 (defun filesets-find-file (file) | |
1314 "Call `find-file' after a possible delay (see `filesets-find-file-delay'). | |
1315 If `filesets-be-docile-flag' is true, a file, which isn't readable, will | |
1316 not be opened." | |
1317 ; (sleep-for filesets-find-file-delay) | |
1318 (when (or (file-readable-p file) | |
1319 (not filesets-be-docile-flag)) | |
1320 (sit-for filesets-find-file-delay) | |
1321 (find-file file))) | |
1322 | |
1323 (defun filesets-find-or-display-file (&optional file viewer) | |
1324 "Visit FILE using an external viewer or open it in an Emacs buffer." | |
1325 (interactive) | |
1326 (let* ((file (or file | |
1327 (read-file-name "Find file: " nil nil viewer))) | |
1328 (external-viewer-def (or | |
1329 (filesets-get-external-viewer-by-name viewer) | |
1330 (filesets-get-external-viewer file)))) | |
1331 (filesets-message 3 "Filesets: view %S using %s" file external-viewer-def) | |
1332 (if external-viewer-def | |
1333 (filesets-spawn-external-viewer file external-viewer-def) | |
1334 (filesets-find-file file)))) | |
1335 | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
1336 (defun filesets-find-file-using () |
44934 | 1337 "Select a viewer and call `filesets-find-or-display-file'." |
1338 (interactive) | |
1339 (let* ((lst (mapcar (lambda (this) | |
1340 (let ((a (cadr this))) | |
1341 (list (format "%s" a) a))) | |
1342 filesets-external-viewers)) | |
1343 (viewer (completing-read "Using viewer: " lst nil t))) | |
1344 (when viewer | |
1345 (filesets-find-or-display-file nil (cadr (assoc viewer lst)))))) | |
1346 | |
1347 (defun filesets-browser-name () | |
1348 "Get the directory browser's name as defined in `filesets-browse-dir-fn'." | |
1349 (cond | |
1350 ((listp filesets-browse-dir-fn) | |
1351 (car filesets-browse-dir-fn)) | |
1352 (t | |
1353 filesets-browse-dir-fn))) | |
1354 | |
1355 (defun filesets-browse-dir (dir) | |
1356 "Browse DIR using `filesets-browse-dir-fn'." | |
1357 (if (functionp filesets-browse-dir-fn) | |
1358 (funcall filesets-browse-dir-fn dir) | |
1359 (let ((name (car filesets-browse-dir-fn)) | |
1360 (args (format (cadr filesets-browse-dir-fn) (expand-file-name dir)))) | |
1361 (with-temp-buffer | |
1362 (start-process (concat "Filesets:" name) | |
1363 "*Filesets external directory browser*" | |
1364 name args))))) | |
1365 | |
1366 (defun filesets-get-fileset-name (something) | |
1367 "Get SOMETHING's name. (Don't ask.)" | |
1368 (cond | |
1369 ((listp something) | |
1370 (car something)) | |
1371 (t | |
1372 something))) | |
1373 | |
1374 (defun filesets-data-get-name (entry) | |
1375 "Access to `filesets-data'. Get the entry's name" | |
1376 (car entry)) | |
1377 | |
1378 (defun filesets-data-get-data (entry) | |
1379 "Access to `filesets-data'. Get the entry's data section" | |
1380 (cdr entry)) | |
1381 | |
1382 (defun filesets-alist-get (alist key &optional default carp) | |
1383 "Get KEY's value in the association list ALIST. | |
1384 Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil." | |
1385 (let* ((elt (assoc key alist))) | |
1386 (cond | |
1387 (elt | |
1388 (if carp | |
1389 (cadr elt) | |
1390 (cdr elt))) | |
1391 (default default) | |
1392 (t nil)))) | |
1393 | |
1394 (defun filesets-data-get (entry key &optional default carp) | |
1395 "Extract the value for KEY in the data part of fileset ENTRY. | |
1396 Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil." | |
1397 (filesets-alist-get (filesets-data-get-data entry) key default carp)) | |
1398 | |
1399 (defun filesets-data-set (entry key value) | |
1400 "Set the value for KEY in the data part of fileset ENTRY." | |
1401 (let* ((alist (filesets-data-get-data entry)) | |
1402 (elt (assoc key alist))) | |
1403 (if elt | |
1404 (setcdr elt value) | |
1405 (setcdr entry (cons (cons key value) alist))))) | |
1406 | |
1407 (defun filesets-entry-mode (entry) | |
1408 "Return fileset ENTRY's mode: :files, :file, :tree, :pattern, or :ingroup. | |
1409 See `filesets-data'." | |
1410 (let ((data (filesets-data-get-data entry))) | |
1411 (some (lambda (x) | |
1412 (if (assoc x data) | |
1413 x)) | |
1414 '(:files :tree :pattern :ingroup :file)))) | |
1415 | |
1416 (defun filesets-entry-get-open-fn (fileset-name &optional fileset-entry) | |
1417 "Get the open-function for FILESET-NAME. | |
1418 Use FILESET-ENTRY for finding the open function, if provided." | |
1419 (filesets-data-get (or fileset-entry | |
1420 (filesets-get-fileset-from-name fileset-name)) | |
1421 ':open filesets-open-file-fn t)) | |
1422 | |
1423 (defun filesets-entry-get-save-fn (fileset-name &optional fileset-entry) | |
1424 "Get the save-function for FILESET-NAME. | |
1425 Use FILESET-ENTRY for finding the save function, if provided." | |
1426 (filesets-data-get (or fileset-entry | |
1427 (filesets-get-fileset-from-name fileset-name)) | |
1428 ':save filesets-save-buffer-fn t)) | |
1429 | |
1430 (defun filesets-entry-get-files (entry) | |
1431 "Get the file list for fileset ENTRY." | |
1432 (filesets-data-get entry ':files)) | |
1433 | |
1434 (defun filesets-entry-set-files (entry data &optional anyways) | |
1435 "Set the file list for fileset ENTRY." | |
1436 (let ((files (filesets-entry-get-files entry))) | |
1437 (if (or anyways files) | |
1438 (filesets-data-set entry ':files data)))) | |
1439 | |
1440 (defun filesets-entry-get-verbosity (entry) | |
1441 "Get verbosity level for fileset ENTRY." | |
1442 (filesets-data-get entry ':verbosity 1 t)) | |
1443 | |
1444 (defun filesets-entry-get-file (entry) | |
1445 "Get the single file for fileset ENTRY." | |
1446 (filesets-data-get entry ':file nil t)) | |
1447 | |
1448 (defun filesets-entry-get-pattern (entry) | |
1449 "Get the base directory + file pattern for fileset ENTRY." | |
1450 ; (filesets-data-get entry ':pattern nil t)) | |
1451 (filesets-data-get entry ':pattern)) | |
1452 | |
1453 (defun filesets-entry-get-pattern--pattern (list) | |
1454 "Get the file pattern for LIST." | |
1455 (if (= (length list) 1) ;; for compatibility with filesets < v1.5.5 | |
1456 (file-name-nondirectory (car list)) | |
1457 (cadr list))) | |
1458 | |
1459 (defun filesets-entry-get-pattern--dir (list) | |
1460 "Get a file pattern's base directory for LIST." | |
1461 (if (= (length list) 1) ;; for compatibility with filesets < v1.5.5 | |
1462 (file-name-directory (car list)) | |
1463 (car list))) | |
1464 | |
1465 (defun filesets-entry-get-tree (entry) | |
1466 "Get the tree pattern for fileset ENTRY." | |
1467 (filesets-data-get entry ':tree)) | |
1468 | |
1469 (defun filesets-entry-get-dormant-flag (entry) | |
1470 "Get dormant flag for fileset ENTRY." | |
1471 (let ((fn (filesets-data-get entry ':dormant-p nil t))) | |
1472 (if fn | |
1473 (funcall fn) | |
1474 (filesets-data-get entry ':dormant-flag nil t)))) | |
1475 | |
1476 (defun filesets-entry-get-filter-dirs-flag (entry) | |
1477 "Get filter-dirs-flag for fileset ENTRY." | |
1478 (filesets-data-get entry ':filter-dirs-flag nil t)) | |
1479 | |
1480 (defun filesets-entry-get-tree-max-level (entry) | |
1481 "Get maximal tree scanning depth for fileset ENTRY." | |
1482 (filesets-data-get entry ':tree-max-level nil t)) | |
1483 | |
1484 (defun filesets-entry-get-master (entry) | |
1485 "Get the base file for fileset ENTRY." | |
1486 (filesets-data-get entry ':ingroup nil t)) | |
1487 | |
1488 (defun filesets-file-open (open-function file-name &optional fileset-name) | |
1489 "Open FILE-NAME using OPEN-FUNCTION. If OPEN-FUNCTION is nil, it's | |
1490 value will be deduced from FILESET-NAME" | |
1491 (let ((open-function (or open-function | |
1492 (filesets-entry-get-open-fn fileset-name)))) | |
1493 (if (file-readable-p file-name) | |
1494 (funcall open-function file-name) | |
1495 (message "Filesets: Couldn't open `%s'" file-name)))) | |
1496 | |
1497 (defun filesets-file-close (save-function buffer) | |
1498 "Close BUFFER. | |
1499 First, save the buffer's contents using SAVE-FUNCTION. Then, kill buffer | |
1500 if `buffer-modified-p' returns nil. | |
1501 | |
1502 SAVE-FUNCTION takes no argument, but works on the current buffer." | |
1503 (save-excursion | |
1504 (set-buffer buffer) | |
1505 (if (buffer-modified-p) | |
1506 (funcall save-function)) | |
1507 (if (not (buffer-modified-p)) | |
1508 (kill-buffer buffer)))) | |
1509 | |
1510 (defun filesets-get-fileset-from-name (name &optional mode) | |
1511 "Get fileset definition for NAME." | |
1512 (case mode | |
1513 ((:ingroup :tree) | |
1514 name) | |
1515 (t | |
1516 (assoc name filesets-data)))) | |
1517 | |
1518 | |
1519 ;;; commands | |
1520 (defun filesets-cmd-get-def (cmd-name) | |
1521 "Get `filesets-commands' entry for CMD-NAME." | |
1522 (assoc cmd-name filesets-commands)) | |
1523 | |
1524 (defun filesets-cmd-get-args (cmd-name) | |
1525 (let ((args (let ((def (filesets-cmd-get-def cmd-name))) | |
1526 (nth 2 def))) | |
1527 (rv nil)) | |
1528 (dolist (this args rv) | |
1529 (cond | |
1530 ((and (symbolp this) (fboundp this)) | |
1531 (let ((x (funcall this))) | |
1532 (setq rv (append rv (if (listp x) x (list x)))))) | |
1533 (t | |
1534 (setq rv (append rv (list this)))))))) | |
1535 | |
1536 (defun filesets-cmd-get-fn (cmd-name) | |
1537 (let ((def (filesets-cmd-get-def cmd-name))) | |
1538 (nth 1 def))) | |
1539 | |
1540 (defun filesets-cmd-show-result (cmd output) | |
1541 "Show OUTPUT of CMD (a shell command)." | |
1542 (pop-to-buffer "*Filesets: Shell Command Output*") | |
1543 (end-of-buffer) | |
1544 (insert "*** ") | |
1545 (insert cmd) | |
1546 (newline) | |
1547 (insert output) | |
1548 (newline)) | |
1549 | |
1550 (defun filesets-run-cmd--repl-fn (arg &optional format-fn) | |
1551 "Helper function for `filesets-run-cmd'. Apply FORMAT-FN to arg. | |
1552 Replace <file-name> or <<file-name>> with filename." | |
1553 (funcall format-fn (cond | |
1554 ((equal arg "<file-name>") | |
1555 (buffer-file-name)) | |
1556 ((equal arg "<<file-name>>") | |
1557 (filesets-quote (buffer-file-name))) | |
1558 (t | |
1559 arg)))) | |
1560 | |
1561 (defun filesets-run-cmd (&optional cmd-name fileset mode) | |
1562 "Run CMD-NAME (see `filesets-commands') on FILESET." | |
1563 (interactive) | |
1564 (let* ((cmd-name (or cmd-name | |
1565 (completing-read "Select command: " filesets-commands | |
1566 nil t))) | |
1567 (name (or fileset | |
1568 (completing-read "Select fileset: " filesets-data nil t)))) | |
1569 (when (and cmd-name name) | |
1570 (let* ((event (if (equal cmd-name "Grep <<selection>>") | |
1571 'on-grep | |
1572 'on-cmd)) | |
1573 (files (if (and fileset | |
1574 (or (equal mode ':ingroup) | |
1575 (equal mode ':tree))) | |
1576 (filesets-get-filelist fileset mode event) | |
1577 (filesets-get-filelist | |
1578 (filesets-get-fileset-from-name name) | |
1579 mode event)))) | |
1580 (when files | |
1581 (let ((fn (filesets-cmd-get-fn cmd-name)) | |
1582 (args (filesets-cmd-get-args cmd-name))) | |
1583 (dolist (this files nil) | |
1584 (save-excursion | |
1585 (save-restriction | |
1586 (let ((buffer (filesets-find-file this))) | |
1587 (when buffer | |
1588 (beginning-of-buffer) | |
1589 (let () | |
1590 (cond | |
1591 ((stringp fn) | |
1592 (let* ((args | |
1593 (let ((txt "")) | |
1594 (dolist (this args txt) | |
1595 (setq txt | |
1596 (concat txt | |
1597 (filesets-run-cmd--repl-fn | |
1598 this | |
1599 (lambda (this) | |
1600 (if (equal txt "") "" " ") | |
1601 (format "%s" this)))))))) | |
1602 (cmd (concat fn " " args))) | |
1603 (filesets-cmd-show-result | |
1604 cmd (shell-command-to-string cmd)))) | |
1605 ((symbolp fn) | |
1606 (let ((args | |
1607 (let ((argl nil)) | |
1608 (dolist (this args argl) | |
1609 (setq argl | |
1610 (append argl | |
1611 (filesets-run-cmd--repl-fn | |
1612 this | |
1613 'list))))))) | |
1614 (apply fn args)))))))))))))))) | |
1615 | |
1616 (defun filesets-get-cmd-menu () | |
1617 "Create filesets command menu." | |
1618 `("+ Commands" | |
1619 . ,(mapcar (lambda (this) | |
1620 (let ((name (car this))) | |
1621 `[,name (filesets-run-cmd ,name)])) | |
1622 filesets-commands))) | |
1623 | |
1624 | |
1625 ;;; sampe commands | |
1626 (defun filesets-cmd-query-replace-getargs () | |
1627 "Get arguments for `filesets-cmd-query-replace'." | |
1628 (let* ((from-string (read-string "Filesets query replace: " | |
1629 "" | |
1630 'query-replace-history)) | |
1631 (to-string (read-string | |
1632 (format "Filesets query replace %s with: " from-string) | |
1633 "" | |
1634 'query-replace-history)) | |
1635 (delimited (y-or-n-p | |
1636 "Filesets query replace: respect word boundaries? "))) | |
1637 (list from-string to-string delimited))) | |
1638 | |
1639 (defun filesets-cmd-shell-command-getargs () | |
1640 "Get arguments for `filesets-cmd-shell-command'." | |
1641 (let* ((arg (read-string "Shell command (%s = file): " | |
1642 "%s" | |
1643 'shell-command-history))) | |
1644 arg)) | |
1645 | |
1646 (defun filesets-cmd-shell-command (txt) | |
1647 "Wrapper function for `shell-command'." | |
1648 (let ((ok (if (buffer-modified-p) | |
1649 (let ((ok (y-or-n-p "Save buffer? "))) | |
1650 (when ok | |
1651 (save-buffer)) | |
1652 ok) | |
1653 t))) | |
1654 (when ok | |
1655 (let ((cmd (format txt (buffer-file-name)))) | |
1656 (message "Filesets: %s" cmd) | |
1657 (filesets-cmd-show-result cmd | |
1658 (shell-command-to-string cmd)))))) | |
1659 | |
1660 | |
1661 ;;; body | |
1662 (defun filesets-get-filelist (entry &optional mode event) | |
1663 "Get all files for fileset ENTRY. | |
1664 Assume MODE (see `filesets-entry-mode'), if provided." | |
1665 (let* ((mode (or mode | |
1666 (filesets-entry-mode entry))) | |
1667 (fl (case mode | |
1668 ((:files) | |
1669 (filesets-entry-get-files entry)) | |
1670 ((:file) | |
1671 (list (filesets-entry-get-file entry))) | |
1672 ((:ingroup) | |
1673 (let ((entry (expand-file-name | |
1674 (if (stringp entry) | |
1675 entry | |
1676 (filesets-entry-get-master entry))))) | |
1677 (cons entry (filesets-ingroup-cache-get entry)))) | |
1678 ((:tree) | |
1679 (let ((dir (nth 0 entry)) | |
1680 (patt (nth 1 entry))) | |
1681 (filesets-directory-files dir patt ':files t))) | |
1682 ((:pattern) | |
1683 (let ((dirpatt (filesets-entry-get-pattern entry))) | |
1684 (if dirpatt | |
1685 (let ((dir (filesets-entry-get-pattern--dir dirpatt)) | |
1686 (patt (filesets-entry-get-pattern--pattern dirpatt))) | |
1687 ;;(filesets-message 3 "Filesets: scanning %s" dirpatt) | |
1688 (filesets-directory-files dir patt ':files t)) | |
1689 ;; (message "Filesets: malformed entry: %s" entry))))))) | |
1690 (filesets-error 'error "Filesets: malformed entry: " | |
1691 entry))))))) | |
1692 (filesets-filter-list fl | |
1693 (lambda (file) | |
1694 (not (filesets-filetype-property file event)))))) | |
1695 | |
1696 (defun filesets-open (&optional mode name lookup-name) | |
1697 "Open the fileset called NAME. | |
1698 Use LOOKUP-NAME for searching additional data if provided." | |
1699 (interactive) | |
1700 (let* ((name (or name | |
1701 (completing-read "Open fileset: " filesets-data nil t))) | |
1702 (fileset (filesets-get-fileset-from-name name mode)) | |
1703 (lookup-fs (if lookup-name | |
1704 (filesets-get-fileset-from-name lookup-name) | |
1705 fileset)) | |
1706 (mode (or mode (filesets-entry-mode lookup-fs)))) | |
1707 (if fileset | |
1708 (let* ((files (filesets-get-filelist fileset mode 'on-open-all)) | |
1709 (n (length files)) | |
1710 (open-function (filesets-entry-get-open-fn nil lookup-fs))) | |
1711 (if (or (<= n filesets-query-user-limit) | |
1712 (y-or-n-p (format "Filesets: Open all %d files in %s? " | |
1713 n name))) | |
1714 (dolist (this files nil) | |
1715 (filesets-file-open open-function this)) | |
1716 (message "Filesets: cancelled"))) | |
1717 (filesets-error 'error "Filesets: Unknown fileset: " name)))) | |
1718 | |
1719 (defun filesets-close (&optional mode name lookup-name) | |
1720 "Close all buffers belonging to the fileset called NAME. | |
1721 Use LOOKUP-NAME for deducing the save-function, if provided." | |
1722 (interactive) | |
1723 (let* ((name (or name | |
1724 (completing-read "Close fileset: " filesets-data nil t))) | |
1725 (fileset (filesets-get-fileset-from-name name mode)) | |
1726 (lookup-fs (if lookup-name | |
1727 (filesets-get-fileset-from-name lookup-name) | |
1728 fileset)) | |
1729 (mode (or mode (filesets-entry-mode lookup-fs)))) | |
1730 (if fileset | |
1731 (let ((files (filesets-get-filelist fileset mode 'on-close-all)) | |
1732 (save-function (filesets-entry-get-save-fn nil lookup-fs))) | |
1733 (dolist (file-name files nil) | |
1734 (let* ((buffer (get-file-buffer file-name))) | |
1735 (if buffer | |
1736 (filesets-file-close save-function buffer))))) | |
1737 ; (message "Filesets: Unknown fileset: `%s'" name)))) | |
1738 (filesets-error 'error "Filesets: Unknown fileset: " name)))) | |
1739 | |
1740 (defun filesets-add-buffer (&optional name buffer) | |
1741 "Add BUFFER (or current-buffer) to the fileset called NAME. | |
1742 User will be queried, if no fileset name is provided." | |
1743 (interactive) | |
1744 (let* ((buffer (or buffer | |
1745 (current-buffer))) | |
1746 (name (or name | |
1747 (completing-read | |
1748 (format "Add '%s' to fileset: " buffer) | |
1749 filesets-data nil t))) | |
1750 (entry (assoc name filesets-data))) | |
1751 (if entry | |
1752 (let* ((files (filesets-entry-get-files entry)) | |
1753 (this (buffer-file-name buffer)) | |
1754 (inlist (member* this files :test 'filesets-files-equalp))) | |
1755 (cond | |
1756 (inlist | |
1757 (message "Filesets: '%s' is already in '%s'" this name)) | |
1758 ((and (equal (filesets-entry-mode entry) ':files) | |
1759 this) | |
1760 (filesets-entry-set-files entry (cons this files) t) | |
1761 (filesets-set-config name 'filesets-data filesets-data)) | |
1762 (t | |
1763 (message "Filesets: Can't add '%s' to fileset '%s'" this name))))))) | |
1764 | |
1765 (defun filesets-remove-buffer (&optional name buffer) | |
1766 "Remove BUFFER (or current-buffer) to fileset NAME. | |
1767 User will be queried, if no fileset name is provided." | |
1768 (interactive) | |
1769 (let* ((buffer (or buffer | |
1770 (current-buffer))) | |
1771 (name (or name | |
1772 (completing-read | |
1773 (format "Remove '%s' from fileset: " buffer) | |
1774 filesets-data nil t))) | |
1775 (entry (assoc name filesets-data))) | |
1776 (if entry | |
1777 (let* ((files (filesets-entry-get-files entry)) | |
1778 (this (buffer-file-name buffer)) | |
1779 (inlist (member* this files :test 'filesets-files-equalp))) | |
1780 ;;(message "%s %s %s" files this inlist) | |
1781 (if (and files this inlist) | |
1782 (let ((new (list (cons ':files (delete (car inlist) files))))) | |
1783 (setcdr entry new) | |
1784 (filesets-set-config name 'filesets-data filesets-data)) | |
1785 (message "Filesets: Can't remove '%s' from fileset '%s'" | |
1786 this | |
1787 name)))))) | |
1788 | |
1789 (defun filesets-convert-patterns (name) | |
1790 "Change fileset NAME's mode from :pattern to :files." | |
1791 (interactive) | |
1792 (let ((entry (assoc name filesets-data))) | |
1793 (if entry | |
1794 (let ((pattern (filesets-entry-get-pattern entry)) | |
1795 (patfiles (filesets-get-filelist entry ':pattern))) | |
1796 (if pattern | |
1797 (progn | |
1798 (filesets-entry-set-files entry patfiles t) | |
1799 (filesets-set-config name 'filesets-data filesets-data))))))) | |
1800 | |
1801 (defun filesets-edit () | |
1802 "Customize `filesets-data'." | |
1803 (interactive) | |
1804 (customize-variable 'filesets-data)) | |
1805 | |
1806 (defun filesets-customize () | |
1807 "Customize the filesets group." | |
1808 (interactive) | |
1809 (customize-group 'filesets)) | |
1810 | |
1811 (defun filesets-info () | |
1812 "Display filesets's version information." | |
1813 (interactive) | |
1814 (if (y-or-n-p (format "Filesets v%s: visit homepage? " filesets-version)) | |
1815 (filesets-goto-homepage))) | |
1816 | |
1817 (defun filesets-goto-homepage () | |
1818 "Show filesets's homepage." | |
1819 (interactive) | |
1820 (browse-url filesets-homepage)) | |
1821 | |
1822 (defun filesets-remake-shortcut (count submenu) | |
1823 "Remake a submenus shortcut when wrapping long menus." | |
1824 (let* ((name (concat (filesets-get-shortcut count) | |
1825 (substring (elt submenu 0) 2)))) | |
1826 (if (listp submenu) | |
1827 (cons name (cdr submenu)) | |
1828 (apply 'vector (list name (cdr (append submenu nil))))))) | |
1829 ; (vconcat `[,name] (subseq submenu 1))))) | |
1830 | |
1831 (defun filesets-wrap-submenu (submenu-body) | |
1832 "Split long submenus." | |
1833 (let ((bl (length submenu-body))) | |
1834 (if (or (= filesets-max-submenu-length 0) | |
1835 (<= bl filesets-max-submenu-length)) | |
1836 submenu-body | |
1837 (let* ((result nil) | |
1838 (factor (ceiling (/ (float bl) | |
1839 filesets-max-submenu-length)))) | |
1840 (do ((data submenu-body (cdr data)) | |
1841 (n 1 (+ n 1)) | |
1842 (count 0 (+ count factor))) | |
1843 ((or (> count bl) | |
1844 (null data))) | |
1845 ; (let ((sl (subseq submenu-body count | |
1846 (let ((sl (filesets-sublist submenu-body count | |
1847 (let ((x (+ count factor))) | |
1848 (if (>= bl x) | |
1849 x | |
1850 nil))))) | |
1851 (when sl | |
1852 (setq result | |
1853 (append | |
1854 result | |
1855 (if (= (length sl) 1) | |
1856 (if filesets-menu-shortcuts-flag | |
1857 (list (filesets-remake-shortcut n (car sl))) | |
1858 sl) | |
1859 `((,(concat | |
1860 (filesets-get-shortcut n) | |
1861 (let ((rv "")) | |
1862 (do ((x sl (cdr x))) | |
1863 ((null x)) | |
1864 (let ((y (concat (elt (car x) 0) | |
1865 (if (null (cdr x)) | |
1866 "" | |
1867 ", ")))) | |
1868 (setq rv | |
1869 (concat | |
1870 rv | |
1871 (if filesets-menu-shortcuts-flag | |
1872 (substring y 2) | |
1873 y))))) | |
1874 (if (> (length rv) | |
1875 filesets-max-entry-length) | |
1876 (concat | |
1877 (substring rv 0 filesets-max-entry-length) | |
1878 " ...") | |
1879 rv))) | |
1880 ,@sl)))))))) | |
1881 result)))) | |
1882 | |
1883 (defun filesets-get-menu-epilog (something &optional | |
1884 mode lookup-name rebuild-flag) | |
1885 "Get submenu epilog for SOMETHING (usually a fileset). | |
1886 If mode is :tree or :ingroup, SOMETHING is some weird construct and | |
1887 LOOKUP-NAME is used as lookup name for retrieving fileset specific settings." | |
1888 (case mode | |
1889 ((:tree) | |
1890 `("---" | |
1891 ["Close all files" (filesets-close ',mode ',something ',lookup-name)] | |
1892 ["Run Command" (filesets-run-cmd nil ',something ',mode)] | |
1893 [,(format "Browse with `%s'" (filesets-browser-name)) | |
1894 (filesets-browse-dir ',(car something))] | |
1895 ,@(when rebuild-flag | |
1896 `(["Rebuild this submenu" | |
1897 (filesets-rebuild-this-submenu ',lookup-name)])))) | |
1898 ((:ingroup) | |
1899 `("---" | |
1900 ["Close all files" (filesets-close ',mode ',something ',lookup-name)] | |
1901 ["Run Command" (filesets-run-cmd nil ',something ',mode)] | |
1902 ,@(when rebuild-flag | |
1903 `(["Rebuild this submenu" | |
1904 (filesets-rebuild-this-submenu ',lookup-name)])))) | |
1905 ((:pattern) | |
1906 `("---" | |
1907 ["Close all files" (filesets-close ',mode ',something)] | |
1908 ["Run Command" (filesets-run-cmd nil ',something ',mode)] | |
1909 [,(format "Browse with `%s'" (filesets-browser-name)) | |
1910 ,(list 'filesets-browse-dir | |
1911 (filesets-entry-get-pattern--dir | |
1912 (filesets-entry-get-pattern | |
1913 (filesets-get-fileset-from-name something ':pattern))))] | |
1914 ; [,(concat (if filesets-menu-shortcuts-flag | |
1915 ; (concat "Con" filesets-menu-shortcuts-marker "vert") | |
1916 ; "Convert") | |
1917 ; " :pattern to :files") | |
1918 ; ,(list (function filesets-convert-patterns) something)] | |
1919 ,@(when rebuild-flag | |
1920 `(["Rebuild this submenu" | |
1921 (filesets-rebuild-this-submenu ',lookup-name)])))) | |
1922 ((:files) | |
1923 `("---" | |
1924 [,(concat "Close all files") (filesets-close ',mode ',something)] | |
1925 ["Run Command" (filesets-run-cmd nil ',something ',mode)] | |
1926 ["Add current buffer" | |
1927 (filesets-add-buffer ',something (current-buffer))] | |
1928 ["Remove current buffer" | |
1929 (filesets-remove-buffer ',something (current-buffer))] | |
1930 ,@(when rebuild-flag | |
1931 `(["Rebuild this submenu" | |
1932 (filesets-rebuild-this-submenu ',lookup-name)])))) | |
1933 (t | |
1934 (filesets-error 'error "Filesets: malformed definition of " something)))) | |
1935 | |
1936 (defun filesets-ingroup-get-data (master pos &optional fun) | |
1937 "Access to `filesets-ingroup-patterns'. Extract data section." | |
1938 (let ((masterfile (file-name-nondirectory master)) | |
1939 (fn (or fun (lambda (a b) | |
1940 (and (stringp a) | |
1941 (stringp b) | |
1942 (string-match a b)))))) | |
1943 (some (lambda (x) | |
1944 (if (funcall fn (car x) masterfile) | |
1945 (nth pos x) | |
1946 nil)) | |
1947 filesets-ingroup-patterns))) | |
1948 | |
1949 (defun filesets-ingroup-get-pattern (master) | |
1950 "Access to `filesets-ingroup-patterns'. Extract patterns." | |
1951 (filesets-ingroup-get-data master 2)) | |
1952 | |
1953 (defun filesets-ingroup-get-remdupl-p (master) | |
1954 "Access to `filesets-ingroup-patterns'. Extract remove-duplicates-flag." | |
1955 (filesets-ingroup-get-data master 1)) | |
1956 | |
1957 (defun filesets-ingroup-collect-finder (patt case-sencitivep) | |
1958 "Helper function for `filesets-ingroup-collect'. Find pattern PATT." | |
1959 (let ((cfs case-fold-search) | |
1960 (rv (progn | |
1961 (setq case-fold-search (not case-sencitivep)) | |
1962 (re-search-forward patt nil t)))) | |
1963 (setq case-fold-search cfs) | |
1964 rv)) | |
1965 | |
1966 (defun filesets-ingroup-cache-get (master) | |
1967 "Access to `filesets-ingroup-cache'." | |
1968 (lax-plist-get filesets-ingroup-cache master)) | |
1969 | |
1970 (defun filesets-ingroup-cache-put (master file) | |
1971 "Access to `filesets-ingroup-cache'." | |
1972 (let* ((emaster (expand-file-name master)) | |
1973 (this (if file | |
1974 (cons file (filesets-ingroup-cache-get emaster)) | |
1975 nil))) | |
1976 (setq filesets-ingroup-cache | |
1977 (lax-plist-put filesets-ingroup-cache emaster this)))) | |
1978 | |
1979 (defun filesets-ingroup-collect-files (fs &optional remdupl-flag master depth) | |
1980 "Helper function for `filesets-ingroup-collect'. Collect file names." | |
1981 (let* ((master (or master | |
1982 (filesets-entry-get-master fs))) | |
1983 (remdupl-flag (or remdupl-flag | |
1984 (filesets-ingroup-get-remdupl-p master)))) | |
1985 (filesets-ingroup-cache-put master nil) | |
1986 (filesets-message 2 "Filesets: parsing %S" master) | |
1987 (let ((cmdpatts (filesets-ingroup-get-pattern master)) | |
1988 (count 0) | |
1989 (rv nil)) | |
1990 (if cmdpatts | |
1991 (dolist (this-def cmdpatts rv) | |
1992 (let* ((this-patt (filesets-alist-get this-def ':pattern nil t)) | |
1993 (this-name (filesets-alist-get this-def ':name "" t)) | |
1994 (this-pp (filesets-alist-get this-def ':preprocess nil t)) | |
1995 (this-mn (filesets-alist-get this-def ':match-number 1 t)) | |
1996 (this-sd (or depth | |
1997 (filesets-alist-get this-def ':scan-depth 0 t))) | |
1998 (this-csp (filesets-alist-get this-def ':case-sensitive nil t)) | |
1999 (this-fn (filesets-alist-get | |
2000 this-def ':get-file-name 'filesets-which-file t)) | |
2001 (this-stubp (filesets-alist-get this-def ':stubp nil t)) | |
2002 (this-stub-flag (filesets-alist-get this-def ':stub-flag nil t)) | |
2003 (flist nil) | |
2004 (lst nil)) | |
2005 (cond | |
2006 ((not this-patt) | |
2007 (filesets-error 'error "Filesets: malformed :ingroup definition " | |
2008 this-def)) | |
2009 ((<= this-sd 0) | |
2010 (setq rv (nconc rv `(((,master ,this-name)))))) | |
2011 (t | |
2012 (with-temp-buffer | |
2013 (insert-file-contents master) | |
2014 (goto-char (point-min)) | |
2015 (when this-pp | |
2016 (funcall this-pp)) | |
2017 (while (filesets-ingroup-collect-finder this-patt this-csp) | |
2018 (let* ((txt (match-string this-mn)) | |
2019 (f (funcall this-fn master txt))) | |
2020 (when (and f | |
2021 (not (member f flist)) | |
2022 (or (not remdupl-flag) | |
2023 (not (member* | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
2024 f filesets-ingroup-files |
44934 | 2025 :test 'filesets-files-equalp)))) |
2026 (let ((no-stub-flag | |
2027 (and (not this-stub-flag) | |
2028 (if this-stubp | |
2029 (not (funcall this-stubp master f)) | |
2030 t)))) | |
2031 (setq count (+ count 1)) | |
2032 (setq flist (cons f flist)) | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
2033 (setq filesets-ingroup-files |
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
2034 (cons f filesets-ingroup-files)) |
44934 | 2035 (when no-stub-flag |
2036 (filesets-ingroup-cache-put master f)) | |
2037 (setq lst (append lst (list f)))))))) | |
2038 (when lst | |
2039 (setq rv | |
2040 (nconc rv | |
2041 (mapcar (lambda (this) | |
2042 `((,this ,this-name) | |
2043 ,@(filesets-ingroup-collect-files | |
2044 fs remdupl-flag this | |
2045 (- this-sd 1)))) | |
2046 lst)))))))) | |
2047 (filesets-message 2 "Filesets: no patterns defined for %S" master))))) | |
2048 | |
2049 (defun filesets-ingroup-collect-build-menu (fs flist &optional other-count) | |
2050 "Helper function for `filesets-ingroup-collect'. Build the menu. | |
2051 FS is a fileset's name. FLIST is a list returned by | |
2052 `filesets-ingroup-collect-files'." | |
2053 (if (null flist) | |
2054 nil | |
2055 (let ((count 0) | |
2056 (fsn fs) | |
2057 (rv nil)) | |
2058 (dolist (this flist rv) | |
2059 (setq count (+ count 1)) | |
2060 (let* ((def (if (listp this) (car this) (list this ""))) | |
2061 (files (if (listp this) (cdr this) nil)) | |
2062 (master (nth 0 def)) | |
2063 (name (nth 1 def)) | |
2064 (nm (concat (filesets-get-shortcut (if (or (not other-count) files) | |
2065 count other-count)) | |
2066 (if (or (null name) (equal name "")) | |
2067 "" | |
2068 (format "%s: " name)) | |
2069 (file-name-nondirectory master)))) | |
2070 (setq rv | |
2071 (append rv | |
2072 (if files | |
2073 `((,nm | |
2074 [,(concat "Inclusion Group: " | |
2075 (file-name-nondirectory master)) | |
2076 (filesets-open ':ingroup ',master ',fsn)] | |
2077 "---" | |
2078 [,master (filesets-file-open nil ',master ',fsn)] | |
2079 "---" | |
2080 ,@(let ((count 0)) | |
2081 (mapcar | |
2082 (lambda (this) | |
2083 (setq count (+ count 1)) | |
2084 (let ((ff (filesets-ingroup-collect-build-menu | |
2085 fs (list this) count))) | |
2086 (if (= (length ff) 1) | |
2087 (car ff) | |
2088 ff))) | |
2089 files)) | |
2090 ,@(filesets-get-menu-epilog master ':ingroup fsn))) | |
2091 `([,nm (filesets-file-open nil ',master ',fsn)]))))))))) | |
2092 | |
2093 (defun filesets-ingroup-collect (fs remdupl-flag master &optional depth) | |
2094 "Collect names of included files & build submenu." | |
2095 (filesets-ingroup-cache-put master nil) | |
2096 (filesets-message 2 "Filesets: parsing %S" master) | |
2097 (filesets-ingroup-collect-build-menu | |
2098 fs | |
2099 (filesets-ingroup-collect-files fs remdupl-flag master))) | |
2100 | |
2101 (defun filesets-build-ingroup-submenu (lookup-name master) | |
2102 "Build a :ingroup submenu for file MASTER." | |
2103 (if (file-readable-p master) | |
2104 (let ((remdupl-flag (filesets-ingroup-get-remdupl-p master))) | |
45012
161015ebadc0
(filesets-running-xemacs): Make defvar unconditional.
Richard M. Stallman <rms@gnu.org>
parents:
44973
diff
changeset
|
2105 (setq filesets-ingroup-files (list master)) |
44934 | 2106 (filesets-ingroup-collect lookup-name remdupl-flag master)) |
2107 (if filesets-be-docile-flag | |
2108 (progn | |
2109 (message "Filesets: can't parse %s" master) | |
2110 nil) | |
2111 (filesets-error 'error "Filesets: can't parse " master)))) | |
2112 | |
2113 (defun filesets-build-dir-submenu-now (level depth entry lookup-name dir patt fd | |
2114 &optional rebuild-flag) | |
2115 "Helper function for `filesets-build-dir-submenu'." | |
2116 ;;(filesets-message 3 "Filesets: scanning %s" dir) | |
2117 (if (or (= depth 0) | |
2118 (< level depth)) | |
2119 (let* ((dir (file-name-as-directory dir)) | |
2120 (header `([,(concat "Tree: " | |
2121 (if (= level 0) | |
2122 dir | |
2123 (concat ".../" | |
2124 (file-name-as-directory | |
2125 (file-name-nondirectory | |
2126 (directory-file-name dir)))))) | |
2127 ,(list (function filesets-open) | |
2128 ':tree | |
2129 `(quote (,dir ,patt)) | |
2130 lookup-name)] | |
2131 "---")) | |
2132 (dirlist (filesets-directory-files dir patt nil nil fd)) | |
2133 (subdirs (filesets-filter-dir-names dirlist)) | |
2134 (count 0) | |
2135 (dirsmenu (mapcar | |
2136 (lambda (x) | |
2137 (setq count (+ count 1)) | |
2138 (let* ((x (file-name-as-directory x)) | |
2139 (xx (concat dir x)) | |
2140 (dd (filesets-build-dir-submenu-now | |
2141 (+ level 1) depth entry | |
2142 lookup-name xx patt fd)) | |
2143 (nm (concat (filesets-get-shortcut count) | |
2144 x))) | |
2145 (if dd | |
2146 `(,nm ,@dd) | |
2147 `[,nm ,(list 'filesets-browse-dir xx)]))) | |
2148 subdirs)) | |
2149 (files (filesets-filter-dir-names dirlist t)) | |
2150 (filesmenu (mapcar (lambda (x) | |
2151 (setq count (+ count 1)) | |
2152 `[,(concat (filesets-get-shortcut count) | |
2153 x) | |
2154 (filesets-file-open nil | |
2155 (quote ,(concat dir x)) | |
2156 (quote ,lookup-name))]) | |
2157 files))) | |
2158 (append header | |
2159 (filesets-wrap-submenu | |
2160 (append | |
2161 dirsmenu | |
2162 filesmenu)) | |
2163 (filesets-get-menu-epilog `(,dir ,patt) ':tree | |
2164 lookup-name rebuild-flag))) | |
2165 nil)) | |
2166 | |
2167 (defun filesets-build-dir-submenu (entry lookup-name dir patt) | |
2168 "Build a :tree submenu named LOOKUP-NAME with base directory DIR including | |
2169 all files matching PATT for filesets ENTRY." | |
2170 (let ((fd (filesets-entry-get-filter-dirs-flag entry)) | |
2171 (depth (or (filesets-entry-get-tree-max-level entry) | |
2172 filesets-tree-max-level))) | |
2173 (filesets-build-dir-submenu-now 0 depth entry lookup-name dir patt fd t))) | |
2174 | |
2175 (defun filesets-build-submenu (count lookup-name entry) | |
2176 "Build submenu for the fileset ENTRY named LOOKUP-NAME. | |
2177 Construct a shortcut from COUNT." | |
2178 (let ((lookup-name (or lookup-name | |
2179 (filesets-data-get-name entry)))) | |
2180 (message "Filesets: %s" lookup-name) | |
2181 (let ((mode (filesets-entry-mode entry)) | |
2182 (filesets-verbosity (filesets-entry-get-verbosity entry)) | |
2183 (this-lookup-name (concat (filesets-get-shortcut count) | |
2184 lookup-name))) | |
2185 (case mode | |
2186 ((:file) | |
2187 (let* ((file (filesets-entry-get-file entry))) | |
2188 `[,this-lookup-name | |
2189 (filesets-file-open nil ',file ',lookup-name)])) | |
2190 (t | |
2191 `(,this-lookup-name | |
2192 ,@(case mode | |
2193 ((:pattern) | |
2194 (let* ((files (filesets-get-filelist entry mode 'on-ls)) | |
2195 (dirpatt (filesets-entry-get-pattern entry)) | |
2196 (pattname (apply 'concat (cons "Pattern: " dirpatt))) | |
2197 (count 0)) | |
2198 ;;(filesets-message 3 "Filesets: scanning %S" pattname) | |
2199 `([,pattname | |
2200 ,(list (function filesets-open) mode lookup-name)] | |
2201 "---" | |
2202 ,@(filesets-wrap-submenu | |
2203 (mapcar | |
2204 (lambda (x) | |
2205 (setq count (+ count 1)) | |
2206 `[,(concat (filesets-get-shortcut count) | |
2207 (file-name-nondirectory x)) | |
2208 (filesets-file-open nil ',x ',lookup-name)]) | |
2209 files)) | |
2210 ,@(filesets-get-menu-epilog lookup-name mode | |
2211 lookup-name t)))) | |
2212 ((:ingroup) | |
2213 (let* ((master (filesets-entry-get-master entry))) | |
2214 ;;(filesets-message 3 "Filesets: parsing %S" master) | |
2215 `([,(concat "Inclusion Group: " | |
2216 (file-name-nondirectory master)) | |
2217 (filesets-open ',mode ',master ',lookup-name)] | |
2218 "---" | |
2219 [,master (filesets-file-open nil ',master ',lookup-name)] | |
2220 "---" | |
2221 ,@(filesets-wrap-submenu | |
2222 (filesets-build-ingroup-submenu lookup-name master)) | |
2223 ,@(filesets-get-menu-epilog master mode lookup-name t)))) | |
2224 ((:tree) | |
2225 (let* ((dirpatt (filesets-entry-get-tree entry)) | |
2226 (dir (car dirpatt)) | |
2227 (patt (cadr dirpatt))) | |
2228 (filesets-build-dir-submenu entry lookup-name dir patt))) | |
2229 ((:files) | |
2230 (let ((files (filesets-get-filelist entry mode 'on-open-all)) | |
2231 (count 0)) | |
2232 `([,(concat "Files: " lookup-name) | |
2233 (filesets-open ',mode ',lookup-name)] | |
2234 "---" | |
2235 ,@(filesets-wrap-submenu | |
2236 (mapcar | |
2237 (lambda (x) | |
2238 (setq count (+ count 1)) | |
2239 `[,(concat (filesets-get-shortcut count) | |
2240 (file-name-nondirectory x)) | |
2241 (filesets-file-open nil ',x ',lookup-name)]) | |
2242 (filesets-conditional-sort | |
2243 files | |
2244 (function file-name-nondirectory)))) | |
2245 ,@(filesets-get-menu-epilog lookup-name mode | |
2246 lookup-name t))))))))))) | |
2247 | |
2248 (defun filesets-remove-from-ubl (&optional buffer) | |
2249 "BUFFER or current-buffer require update of the filesets menu." | |
2250 (let ((b (or buffer | |
2251 (current-buffer)))) | |
2252 (if (member b filesets-updated-buffers) | |
2253 (setq filesets-updated-buffers | |
2254 (delete b filesets-updated-buffers))))) | |
2255 | |
2256 (defun filesets-build-menu-now (from-scratch-flag) | |
2257 "Update the filesets menu. | |
2258 Build all new if FROM-SCRATCH-FLAG is non-nil. (To really build from the | |
2259 bottom up, set `filesets-submenus' to nil, first.)" | |
2260 (when (or from-scratch-flag | |
2261 filesets-has-changed-flag | |
2262 (not filesets-menu-cache)) | |
2263 (setq filesets-menu-cache nil) | |
2264 (setq filesets-has-changed-flag nil) | |
2265 (setq filesets-updated-buffers nil) | |
2266 (setq filesets-update-cache-file-flag t) | |
2267 (do ((data (filesets-conditional-sort filesets-data (function car)) | |
2268 (cdr data)) | |
2269 (count 1 (+ count 1))) | |
2270 ((null data)) | |
2271 (let* ((this (car data)) | |
2272 (name (filesets-data-get-name this)) | |
2273 (cached (lax-plist-get filesets-submenus name)) | |
2274 (submenu (or cached | |
2275 (filesets-build-submenu count name this)))) | |
2276 (unless cached | |
2277 (setq filesets-submenus | |
2278 (lax-plist-put filesets-submenus name submenu))) | |
2279 (unless (filesets-entry-get-dormant-flag this) | |
2280 (setq filesets-menu-cache | |
2281 (append filesets-menu-cache (list submenu)))))) | |
2282 (when filesets-cache-save-often-flag | |
2283 (filesets-menu-cache-file-save-maybe))) | |
2284 (let ((cb (current-buffer))) | |
2285 (when (not (member cb filesets-updated-buffers)) | |
2286 (filesets-add-submenu | |
2287 filesets-menu-path | |
2288 `(,filesets-menu-name | |
2289 ("# Filesets" | |
2290 ["Edit Filesets" filesets-edit] | |
2291 ["Save Filesets" filesets-save-config] | |
2292 ["Save Menu Cache" filesets-menu-cache-file-save] | |
2293 ["Rebuild Menu" filesets-build-menu] | |
2294 ["Customize" filesets-customize] | |
2295 ["About" filesets-info]) | |
2296 ,(filesets-get-cmd-menu) | |
2297 "---" | |
2298 ,@filesets-menu-cache) | |
2299 filesets-menu-before | |
2300 filesets-menu-in-menu) | |
2301 (setq filesets-updated-buffers | |
2302 (cons cb filesets-updated-buffers)) | |
2303 (message nil) | |
2304 ;;(message "Filesets updated: %s" cb) | |
2305 ))) | |
2306 | |
2307 (defun filesets-build-menu-maybe () | |
2308 "Update the filesets menu." | |
2309 (interactive) | |
2310 (filesets-build-menu-now nil)) | |
2311 | |
2312 (defun filesets-build-menu () | |
2313 "Force rebuild of the filesets menu." | |
2314 (interactive) | |
2315 ;(setq filesets-submenus nil) | |
2316 (filesets-reset-fileset) | |
2317 (filesets-build-menu-now t) | |
2318 (filesets-menu-cache-file-save-maybe)) | |
2319 | |
2320 (defun filesets-rebuild-this-submenu (fileset) | |
2321 "Force rebuild of FILESET submenu." | |
2322 (filesets-reset-fileset fileset) | |
2323 (filesets-build-menu-now t)) | |
2324 | |
2325 (defun filesets-menu-cache-file-save-maybe (&optional simply-do-it) | |
2326 "Write filesets' cache file. | |
2327 If SIMPLY-DO-IT is non-nil, the cache file will be written no matter if | |
2328 fileset thinks this is necessary or not." | |
2329 (when (and (not (equal filesets-menu-cache-file "")) | |
2330 (or simply-do-it | |
2331 filesets-update-cache-file-flag)) | |
2332 (when (file-exists-p filesets-menu-cache-file) | |
2333 (delete-file filesets-menu-cache-file)) | |
2334 ;;(message "Filesets: saving menu cache") | |
2335 (with-temp-buffer | |
2336 (dolist (this filesets-menu-cache-contents) | |
2337 (if (get this 'custom-type) | |
2338 (progn | |
2339 (insert (format "(setq-default %s '%S)" this (eval this))) | |
2340 (when filesets-menu-ensure-use-cached | |
2341 (newline) | |
2342 (insert (format "(setq %s (cons '%s %s))" | |
2343 'filesets-ignore-next-set-default | |
2344 this | |
2345 'filesets-ignore-next-set-default)))) | |
2346 (insert (format "(setq %s '%S)" this (eval this)))) | |
2347 (newline 2)) | |
2348 (insert (format "(setq filesets-cache-version %S)" filesets-version)) | |
2349 (newline 2) | |
2350 (when filesets-cache-hostname-flag | |
2351 (insert (format "(setq filesets-cache-hostname %S)" (system-name))) | |
2352 (newline 2)) | |
2353 (run-hooks 'filesets-cache-fill-content-hooks) | |
2354 (write-file filesets-menu-cache-file)) | |
2355 (setq filesets-has-changed-flag nil) | |
2356 (setq filesets-update-cache-file-flag nil))) | |
2357 | |
2358 (defun filesets-menu-cache-file-save () | |
2359 "Save filesets' menu cache file." | |
2360 (interactive) | |
2361 (filesets-menu-cache-file-save-maybe t)) | |
2362 | |
2363 (defun filesets-update-cleanup () | |
2364 "Rebuild the menu and save the cache file after updating user data." | |
2365 (interactive) | |
2366 (message "Filesets v%s: updating menu & cache from version %s" | |
2367 filesets-version (or filesets-cache-version "???")) | |
2368 (filesets-build-menu) | |
2369 (filesets-menu-cache-file-save-maybe) | |
2370 (filesets-menu-cache-file-load)) | |
2371 | |
2372 (defun filesets-update-pre010505 () | |
2373 (let ((msg | |
2374 "Filesets: manual editing of user data required! | |
2375 | |
2376 Filesets has detected that you were using an older version before, | |
2377 which requires some manual updating. Type 'y' for editing the startup | |
2378 file now. | |
2379 | |
2380 The layout of `filesets-data' has changed. Please delete your cache file | |
2381 and edit your startup file as shown below: | |
2382 | |
2383 1. `filesets-data': Edit all :pattern filesets in your startup file and | |
2384 transform all entries as shown in this example: | |
2385 | |
2386 \(\"Test\" (:pattern \"~/dir/^pattern$\")) | |
2387 --> \(\"Test\" (:pattern \"~/dir/\" \"^pattern$\")) | |
2388 | |
2389 2. `filesets-data': Change all occurances of \":document\" to \":ingroup\": | |
2390 | |
2391 \(\(\"Test\" \(:document \"~/dir/file\")) | |
2392 --> \(\(\"Test\" \(:ingroup \"~/dir/file\")) | |
2393 | |
2394 3. `filesets-subdocument-patterns': If you already modified the variable | |
2395 previously called `filesets-subdocument-patterns', change its name to | |
2396 `filesets-ingroup-patterns'. | |
2397 | |
2398 4. `filesets-menu-cache-contents': If you already modified this | |
2399 variable, change the entry `filesets-subdocument--cache' to | |
2400 `filesets-ingroup-cache'. | |
2401 | |
2402 5. Type M-x filesets-update-cleanup and restart Emacs. | |
2403 | |
2404 We apologize for the inconvenience.")) | |
2405 (let* ((cf (or custom-file user-init-file))) | |
2406 (switch-to-buffer-other-frame "*Filesets update*") | |
2407 (insert msg) | |
2408 (when (y-or-n-p (format "Edit startup (%s) file now? " cf)) | |
2409 (find-file-other-window cf)) | |
2410 (filesets-error 'error msg)))) | |
2411 | |
2412 (defun filesets-update (version cached-version) | |
2413 "Do some cleanup after updating filesets.el." | |
2414 (cond | |
2415 ((or (not cached-version) | |
2416 (string< cached-version "1.5.5") | |
2417 (boundp 'filesets-subdocument-patterns)) | |
2418 (filesets-update-pre010505))) | |
2419 (filesets-update-cleanup)) | |
2420 | |
2421 (defun filesets-menu-cache-file-load () | |
2422 "Load filesets' menu cache file." | |
2423 (cond | |
2424 ((and (not (equal filesets-menu-cache-file "")) | |
2425 (file-readable-p filesets-menu-cache-file)) | |
2426 (load-file filesets-menu-cache-file) | |
2427 (if (and (equal filesets-cache-version filesets-version) | |
2428 (if filesets-cache-hostname-flag | |
2429 (equal filesets-cache-hostname (system-name)) | |
2430 t)) | |
2431 (progn | |
2432 (setq filesets-update-cache-file-flag nil) | |
2433 t) | |
2434 (filesets-update filesets-version filesets-cache-version))) | |
2435 (t | |
2436 (setq filesets-update-cache-file-flag t) | |
2437 nil))) | |
2438 | |
2439 (defun filesets-exit () | |
2440 (filesets-menu-cache-file-save-maybe)) | |
2441 | |
2442 (defun filesets-init () | |
2443 "Filesets initialization. | |
2444 Set up hooks, load the cache file -- if existing -- and build the menu." | |
2445 (add-hook (if filesets-running-xemacs 'activate-menubar-hook 'menu-bar-update-hook) | |
2446 (function filesets-build-menu-maybe)) | |
2447 (add-hook 'kill-buffer-hook (function filesets-remove-from-ubl)) | |
2448 (add-hook 'first-change-hook (function filesets-reset-filename-on-change)) | |
2449 (add-hook 'kill-emacs-hook (function filesets-exit)) | |
2450 (if (filesets-menu-cache-file-load) | |
2451 (progn | |
2452 (filesets-build-menu-maybe) | |
2453 ;;Well, normally when we use XEmacs <= 21.4, custom.el is loaded | |
2454 ;;after init.el. This more or less ignores the next | |
2455 ;;`filesets-data-set-default' | |
2456 (if filesets-menu-ensure-use-cached | |
2457 (setq filesets-menu-use-cached-flag t))) | |
2458 (filesets-build-menu))) | |
2459 | |
2460 | |
2461 ;;; run | |
2462 (filesets-init) | |
2463 | |
2464 (provide 'filesets) | |
2465 | |
2466 ;;; Local Variables: | |
2467 ;;; time-stamp-format:"%:y-%02m-%02d" | |
2468 ;;; sentence-end-double-space:t | |
2469 ;;; End: | |
2470 | |
2471 ;;; filesets.el ends here |