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