1 ;;; filesets.el --- handle group of files
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 ;; Author: Thomas Link <sanobast-emacs@yahoo.de>
7 ;; Keywords: filesets convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs 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 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 (defvar filesets-version
"1.8.4")
27 (defvar filesets-homepage
28 "http://members.a1.net/t.link/CompEmacsFilesets.html")
32 ;; Define filesets, which can be opened or saved with the power of one or
33 ;; two mouse clicks only. A fileset is either a list of files, a file
34 ;; pattern, a base directory and a search pattern (for files), or an
35 ;; inclusion group (i.e. a base file including other files).
38 ;; 1. Put (require 'filesets) and (filesets-init) in your .emacs file.
39 ;; 2. Type ;; M-x filesets-edit or choose "Edit Filesets" from the menu.
40 ;; 3. Save your customizations.
42 ;; Caveat: Fileset names have to be unique.
44 ;; Filesets.el adds a nifty filesets menu to your menubar. If you change
45 ;; your filesets on the fly, don't forget to select "Save Filesets" from
48 ;; Pressing on the first item in the submenu will open all files at once.
49 ;; Define your own function, e.g. browse-url, for opening a fileset's
50 ;; files. Or define external viewers for opening files with other
51 ;; programs. See `filesets-external-viewers'.
53 ;; BTW, if you close a fileset, files, which have been changed, will
54 ;; be silently saved. Change this behavior by setting
55 ;; `filesets-save-buffer-fn'.
57 ;;; Supported modes for inclusion groups (`filesets-ingroup-patterns'):
59 ;; - Emacs-Wiki (simple names only)
69 ;;- better handling of different customization scenarios
71 ;; Data gathering should be better separated from building the menu
72 ;; so that one could (1) use filesets without installing the menu
73 ;; and (2) create new "frontends" to speedbar and others.
75 ;; The functionality to call external viewers should be isolated in
76 ;; an extra package and possibly integrated with the MIME
81 ;; Helpful suggestions (but no significant code) were contributed by
83 ;;- Christoph Conrad (at gmx de)
84 ;;- Christian Ohler (at Informatik Uni-Oldenburg DE)
85 ;;- Richard Stallman aka RMS (at gnu org)
86 ;;- Per Abrahamsen aka abraham (at dina kvl dk)
91 (eval-when-compile (require 'cl-lib
))
95 (defvar filesets-menu-cache nil
96 "The whole filesets menu.")
97 (defvar filesets-cache-version nil
98 "Filesets' cached version number.")
99 (defvar filesets-cache-hostname nil
100 "Filesets' cached system name.")
102 (defvar filesets-ingroup-cache nil
103 "A plist containing files and their ingroup data.")
104 (defvar filesets-ingroup-files nil
105 "List of files already processed when searching for included files.")
107 (defvar filesets-has-changed-flag t
108 "Non-nil means some fileset definition has changed.")
109 (defvar filesets-submenus nil
110 "An association list with filesets menu data.")
111 (defvar filesets-updated-buffers nil
112 "A list of buffers with updated menu bars.")
113 (defvar filesets-menu-use-cached-flag nil
114 "Use cached data. See `filesets-menu-ensure-use-cached' for details.")
115 (defvar filesets-update-cache-file-flag nil
116 "Non-nil means the cache needs updating.")
117 (defvar filesets-ignore-next-set-default nil
118 "List of custom variables for which the next `set-default' will be ignored.")
120 (defvar filesets-output-buffer-flag nil
121 "Non-nil means the current buffer is an output buffer created by filesets.
122 Is buffer local variable.")
124 (defvar filesets-verbosity
1
125 "An integer defining the level of verbosity.
126 0 means no messages at all.")
128 (defvar filesets-menu-ensure-use-cached
129 (and (featurep 'xemacs
)
130 (if (fboundp 'emacs-version
>=)
131 (not (emacs-version>= 21 5))))
132 "Make sure (X)Emacs uses filesets' cache.
134 Well, if you use XEmacs (prior to 21.5?) custom.el is loaded after
135 init.el. This means that settings saved in the cache file (see
136 `filesets-menu-cache-file') will be overwritten by custom.el. In order
137 to ensure the use of the cache file, set this variable to t -- which is
138 the default for XEmacs prior to 21.5. If you want to change this value
139 put \"(setq filesets-menu-ensure-use-cached VALUE)\" into your startup
140 file -- before loading filesets.el.
142 So, when should you think about setting this value to t? If filesets.el
143 is loaded before user customizations. Thus, if (require 'filesets)
144 precedes the `custom-set-variables' command or, for XEmacs, if init.el
145 is loaded before custom.el, set this variable to t.")
149 (defun filesets-filter-list (lst cond-fn
)
150 "Remove all elements not conforming to COND-FN from list LST.
151 COND-FN takes one argument: the current element."
152 ; (remove* 'dummy lst :test (lambda (dummy elt)
153 ; (not (funcall cond-fn elt)))))
156 (when (funcall cond-fn elt
)
157 (setq rv
(append rv
(list elt
)))))))
159 (defun filesets-ormap (fsom-pred lst
)
160 "Return the tail of LST for the head of which FSOM-PRED is non-nil."
163 (while (and (not (null fsom-lst
))
165 (if (funcall fsom-pred
(car fsom-lst
))
166 (setq fsom-rv fsom-lst
)
167 (setq fsom-lst
(cdr fsom-lst
))))
170 (defun filesets-some (fss-pred fss-lst
)
171 "Return non-nil if FSS-PRED is non-nil for any element of FSS-LST.
172 Like `some', return the first value of FSS-PRED that is non-nil."
174 (dolist (fss-this fss-lst nil
)
175 (let ((fss-rv (funcall fss-pred fss-this
)))
177 (throw 'exit fss-rv
))))))
178 ;(fset 'filesets-some 'some) ;; or use the cl function
180 (defun filesets-member (fsm-item fsm-lst
&rest fsm-keys
)
181 "Find the first occurrence of FSM-ITEM in FSM-LST.
182 It is supposed to work like cl's `member*'. At the moment only the :test
184 (let ((fsm-test (or (plist-get fsm-keys
':test
)
186 (filesets-ormap (lambda (fsm-this)
187 (funcall fsm-test fsm-item fsm-this
))
189 ;(fset 'filesets-member 'member*) ;; or use the cl function
191 (defun filesets-sublist (lst beg
&optional end
)
192 "Get the sublist of LST from BEG to END - 1."
198 (setq rv
(append rv
(list (nth i lst
))))
202 (defun filesets-select-command (cmd-list)
203 "Select one command from CMD-LIST -- a string with space separated names."
204 (let ((this (shell-command-to-string
205 (format "which --skip-alias %s 2> /dev/null | head -n 1"
209 (file-name-nondirectory (substring this
0 (- (length this
) 1))))))
211 (defun filesets-which-command (cmd)
212 "Call \"which CMD\"."
213 (shell-command-to-string (format "which %s" cmd
)))
215 (defun filesets-which-command-p (cmd)
216 "Call \"which CMD\" and return non-nil if the command was found."
217 (when (string-match (format "\\(/[^/]+\\)?/%s" cmd
)
218 (filesets-which-command cmd
))
221 (defun filesets-message (level &rest args
)
222 "Show a message only if LEVEL is greater or equal then `filesets-verbosity'."
223 (when (<= level
(abs filesets-verbosity
))
224 (apply 'message args
)))
228 (defun filesets-save-config ()
229 "Save filesets' customizations."
231 (customize-save-customized))
233 (defun filesets-reset-fileset (&optional fileset no-cache
)
234 "Reset the cached values for one or all filesets."
236 (setq filesets-submenus
(lax-plist-put filesets-submenus fileset nil
))
237 (setq filesets-submenus nil
))
238 (setq filesets-has-changed-flag t
)
239 (setq filesets-update-cache-file-flag
(or filesets-update-cache-file-flag
242 (defun filesets-set-config (fileset var val
)
243 "Set-default wrapper function."
244 (filesets-reset-fileset fileset
)
245 (set-default var val
))
246 ; (customize-set-variable var val))
247 ; (filesets-build-menu))
249 ;; It seems this is a workaround for the XEmacs issue described in the
250 ;; doc-string of filesets-menu-ensure-use-cached. Under Emacs this is
251 ;; essentially just `set-default'.
252 (defun filesets-set-default (sym val
&optional init-flag
)
253 "Set-default wrapper function used in conjunction with `defcustom'.
254 If SYM is in the list `filesets-ignore-next-set-default', delete
255 it from that list, and return nil. Otherwise, set the value of
256 SYM to VAL and return t. If INIT-FLAG is non-nil, set with
257 `custom-initialize-set', otherwise with `set-default'."
258 (let ((ignore-flag (member sym filesets-ignore-next-set-default
)))
260 (setq filesets-ignore-next-set-default
261 (delete sym filesets-ignore-next-set-default
))
263 (custom-initialize-set sym val
)
264 (set-default sym val
)))
267 (defun filesets-set-default! (sym val
)
268 "Call `filesets-set-default' and reset cached data (i.e. rebuild menu)."
269 (when (filesets-set-default sym val
)
270 (filesets-reset-fileset)))
272 (defun filesets-set-default+ (sym val
)
273 "Call `filesets-set-default' and reset filesets' standard menu."
274 (when (filesets-set-default sym val
)
275 (setq filesets-has-changed-flag t
)))
276 ; (filesets-reset-fileset nil t)))
278 (defvar filesets-data
)
280 (defun filesets-data-set-default (sym val
)
281 "Set the default for `filesets-data'."
282 (if filesets-menu-use-cached-flag
283 (setq filesets-menu-use-cached-flag nil
)
284 (when (default-boundp 'filesets-data
)
285 (let ((modified-filesets
286 (filesets-filter-list val
290 (let ((elt (assoc name filesets-data
)))
292 (not (equal data
(cdr elt
))))))))))
293 (dolist (x modified-filesets
)
294 (filesets-reset-fileset (car x
))))))
295 (filesets-set-default sym val
))
298 (defgroup filesets nil
299 "The fileset swapper."
304 (defcustom filesets-menu-name
"Filesets"
305 "Filesets' menu name."
306 :set
(function filesets-set-default
)
310 (defcustom filesets-menu-path
'("File") ; cf recentf-menu-path
311 "The menu under which the filesets menu should be inserted.
312 See `add-submenu' for documentation."
313 :set
(function filesets-set-default
)
314 :type
'(choice (const :tag
"Top Level" nil
)
315 (sexp :tag
"Menu Path"))
316 :version
"23.1" ; was nil
319 (defcustom filesets-menu-before
"Open File..." ; cf recentf-menu-before
320 "The name of a menu before which this menu should be added.
321 See `add-submenu' for documentation."
322 :set
(function filesets-set-default
)
323 :type
'(choice (string :tag
"Name")
324 (const :tag
"Last" nil
))
325 :version
"23.1" ; was "File"
328 (defcustom filesets-menu-in-menu nil
329 "Use that instead of `current-menubar' as the menu to change.
330 See `add-submenu' for documentation."
331 :set
(function filesets-set-default
)
335 (defcustom filesets-menu-shortcuts-flag t
336 "Non-nil means to prepend menus with hopefully unique shortcuts."
337 :set
(function filesets-set-default
!)
341 (defcustom filesets-menu-shortcuts-marker
"%_"
342 "String for marking menu shortcuts."
343 :set
(function filesets-set-default
!)
347 ;;(defcustom filesets-menu-cnvfp-flag nil
348 ;; "Non-nil means show \"Convert :pattern to :files\" entry for :pattern menus."
349 ;; :set (function filesets-set-default!)
353 (defcustom filesets-menu-cache-file
354 (locate-user-emacs-file "filesets-cache.el")
355 "File to be used for saving the filesets menu between sessions.
356 Set this to \"\", to disable caching of menus.
357 Don't forget to check out `filesets-menu-ensure-use-cached'."
358 :set
(function filesets-set-default
)
361 (put 'filesets-menu-cache-file
'risky-local-variable t
)
363 (defcustom filesets-menu-cache-contents
364 '(filesets-be-docile-flag
367 filesets-ingroup-cache
)
368 "Stuff we want to save in `filesets-menu-cache-file'.
370 Possible uses: don't save configuration data in the main startup files
371 but in filesets's own cache. In this case add `filesets-data' to this
374 There is a second reason for putting `filesets-data' on this list. If
375 you frequently add and remove buffers on the fly to :files filesets, you
376 don't need to save your customizations if `filesets-data' is being
377 mirrored in the cache file. In this case the version in the cache file
378 is the current one, and the version in your startup file will be
379 silently updated later on.
381 If you want caching to work properly, at least `filesets-submenus',
382 `filesets-menu-cache', and `filesets-ingroup-cache' should be in this
385 Don't forget to check out `filesets-menu-ensure-use-cached'."
386 :set
(function filesets-set-default
)
388 (choice :tag
"Variable"
389 (const :tag
"filesets-submenus"
390 :value filesets-submenus
)
391 (const :tag
"filesets-menu-cache"
392 :value filesets-menu-cache
)
393 (const :tag
"filesets-ingroup-cache"
394 :value filesets-ingroup-cache
)
395 (const :tag
"filesets-data"
396 :value filesets-data
)
397 (const :tag
"filesets-external-viewers"
398 :value filesets-external-viewers
)
399 (const :tag
"filesets-ingroup-patterns"
400 :value filesets-ingroup-patterns
)
401 (const :tag
"filesets-be-docile-flag"
402 :value filesets-be-docile-flag
)
403 (sexp :tag
"Other" :value nil
)))
406 (defcustom filesets-cache-fill-content-hooks nil
407 "Hooks to run when writing the contents of filesets' cache file.
409 The hook is called with the cache file as current buffer and the cursor
410 at the last position. I.e. each hook has to make sure that the cursor is
411 at the last position.
413 Possible uses: If you don't want to save `filesets-data' in your normal
414 configuration file, you can add a something like this
417 \(insert (format \"(setq-default filesets-data '%S)\"
423 Don't forget to check out `filesets-menu-ensure-use-cached'."
424 :set
(function filesets-set-default
)
428 (defcustom filesets-cache-hostname-flag nil
429 "Non-nil means cache the hostname.
430 If the current name differs from the cached one,
431 rebuild the menu and create a new cache file."
432 :set
(function filesets-set-default
)
436 (defcustom filesets-cache-save-often-flag nil
437 "Non-nil means save buffer on every change of the filesets menu.
438 If this variable is set to nil and if Emacs crashes, the cache and
439 filesets-data could get out of sync. Set this to t if this happens from
440 time to time or if the fileset cache causes troubles."
441 :set
(function filesets-set-default
)
445 (defcustom filesets-max-submenu-length
25
446 "Maximum length of submenus.
447 Set this value to 0 to turn menu splitting off. BTW, parts of submenus
448 will not be rewrapped if their length exceeds this value."
449 :set
(function filesets-set-default
)
453 (defcustom filesets-max-entry-length
50
454 "Truncate names of split submenus to this length."
455 :set
(function filesets-set-default
)
459 (defcustom filesets-browse-dir-function
'dired
460 "A function or command used for browsing directories.
461 When using an external command, \"%s\" will be replaced with the
464 Note: You have to manually rebuild the menu if you change this value."
465 :set
(function filesets-set-default
)
466 :type
'(choice :tag
"Function:"
472 (string :tag
"Arguments"))
473 (function :tag
"Function"
477 (defcustom filesets-open-file-function
'filesets-find-or-display-file
478 "The function used for opening files.
480 `filesets-find-or-display-file' ... Filesets' default function for
481 visiting files. This function checks if an external viewer is defined
482 for a specific file type. Either this viewer, if defined, or
483 `find-file' will be used to visit a file.
485 `filesets-find-file' ... An alternative function that always uses
486 `find-file'. If `filesets-be-docile-flag' is true, a file, which isn't
487 readable, will not be opened.
489 Caveat: Changes will take effect only after rebuilding the menu."
490 :set
(function filesets-set-default
)
491 :type
'(choice :tag
"Function:"
492 (const :tag
"filesets-find-or-display-file"
493 :value filesets-find-or-display-file
)
494 (const :tag
"filesets-find-file"
495 :value filesets-find-file
)
496 (function :tag
"Function"
500 (defcustom filesets-save-buffer-function
'save-buffer
501 "The function used to save a buffer.
502 Caveat: Changes will take effect after rebuilding the menu."
503 :set
(function filesets-set-default
)
504 :type
'(choice :tag
"Function:"
505 (const :tag
"save-buffer"
507 (function :tag
"Function"
511 (defcustom filesets-find-file-delay
512 (if (and (featurep 'xemacs
) gutter-buffers-tab-visible-p
)
515 "Delay before calling `find-file'.
516 This is for calls via `filesets-find-or-display-file'
517 or `filesets-find-file'.
519 Set this to 0, if you don't use XEmacs's buffer tabs."
520 :set
(function filesets-set-default
)
524 (defcustom filesets-be-docile-flag nil
525 "Non-nil means don't complain if a file or a directory doesn't exist.
526 This is useful if you want to use the same startup files in different
527 computer environments."
528 :set
(function filesets-set-default
)
532 (defcustom filesets-sort-menu-flag t
533 "Non-nil means sort the filesets menu alphabetically."
534 :set
(function filesets-set-default
)
538 (defcustom filesets-sort-case-sensitive-flag t
539 "Non-nil means sorting of the filesets menu is case sensitive."
540 :set
(function filesets-set-default
)
544 (defcustom filesets-tree-max-level
3
545 "Maximum scan depth for directory trees.
546 A :tree fileset is defined by a base directory the contents of which
547 will be recursively added to the menu. `filesets-tree-max-level' tells up
548 to which level the directory structure should be scanned/listed,
549 i.e. how deep the menu should be. Try something like
551 \(\"HOME -- only one level\"
552 \(:tree \"~\" \"^[^.].*[^~]$\")
554 \(:filter-dirs-flag t))
555 \(\"HOME -- up to 3 levels\"
556 \(:tree \"~\" \"^[^.].*[^~]$\")
558 \(:filter-dirs-flag t))
560 and it should become clear what this option is about. In any case,
561 including directory trees to the menu can take a lot of memory."
562 :set
(function filesets-set-default
)
566 (defcustom filesets-commands
569 (filesets-cmd-isearch-getargs))
571 multi-isearch-files-regexp
572 (filesets-cmd-isearch-getargs))
575 (filesets-cmd-query-replace-getargs))
576 ("Query Replace (regexp)"
578 (filesets-cmd-query-replace-regexp-getargs))
579 ("Grep <<selection>>"
581 ("-n " filesets-get-quoted-selection
" " "<<file-name>>"))
583 filesets-cmd-shell-command
584 (filesets-cmd-shell-command-getargs)))
585 "Commands to run on filesets.
586 An association list of names, functions, and an argument list (or a
587 function that returns one) to be run on a filesets' files.
589 The argument <file-name> or <<file-name>> (quoted) will be replaced with
591 :set
(function filesets-set-default
+)
592 :type
'(repeat :tag
"Commands"
593 (list :tag
"Definition" :value
("")
595 (choice :tag
"Command"
596 (string :tag
"String")
597 (function :tag
"Function"))
598 (repeat :tag
"Argument List"
599 (choice :tag
"Arguments"
602 (string :tag
"File Name"
603 :value
"<file-name>")
604 (string :tag
"Quoted File Name"
605 :value
"<<file-name>>")
606 (function :tag
"Function"
609 (put 'filesets-commands
'risky-local-variable t
)
611 (defcustom filesets-external-viewers
613 ;; ((ps-cmd (or (and (boundp 'my-ps-viewer) my-ps-viewer)
614 ;; (filesets-select-command "ggv gv")))
615 ;; (pdf-cmd (or (and (boundp 'my-ps-viewer) my-pdf-viewer)
616 ;; (filesets-select-command "xpdf acroread")))
617 ;; (dvi-cmd (or (and (boundp 'my-ps-viewer) my-dvi-viewer)
618 ;; (filesets-select-command "xdvi tkdvi")))
619 ;; (doc-cmd (or (and (boundp 'my-ps-viewer) my-doc-viewer)
620 ;; (filesets-select-command "antiword")))
621 ;; (pic-cmd (or (and (boundp 'my-ps-viewer) my-pic-viewer)
622 ;; (filesets-select-command "gqview ee display"))))
628 `(("^.+\\..?html?$" browse-url
629 ((:ignore-on-open-all t
)))
630 ("^.+\\.pdf$" ,pdf-cmd
631 ((:ignore-on-open-all t
)
632 (:ignore-on-read-text t
)
633 (:constraint-flag
,pdf-cmd
)))
634 ("^.+\\.e?ps\\(.gz\\)?$" ,ps-cmd
635 ((:ignore-on-open-all t
)
636 (:ignore-on-read-text t
)
637 (:constraint-flag
,ps-cmd
)))
638 ("^.+\\.dvi$" ,dvi-cmd
639 ((:ignore-on-open-all t
)
640 (:ignore-on-read-text t
)
641 (:constraint-flag
,dvi-cmd
)))
642 ("^.+\\.doc$" ,doc-cmd
644 (:ignore-on-read-text t
)
645 (:constraint-flag
,doc-cmd
)))
646 ("^.+\\.\\(tiff\\|xpm\\|gif\\|pgn\\)$" ,pic-cmd
647 ((:ignore-on-open-all t
)
648 (:ignore-on-read-text t
)
649 (:constraint-flag
,pic-cmd
)))))
650 "Association list of file patterns and external viewers for use with
651 `filesets-find-or-display-file'.
653 Has the form ((FILE-PATTERN VIEWER PROPERTIES) ...), VIEWER being either a
654 function or a command name as string.
656 Properties is an association list determining filesets' behavior in
657 several conditions. Choose one from this list:
659 :ignore-on-open-all ... Don't open files of this type automatically --
660 i.e. on open-all-files-events or when running commands
662 :capture-output ... capture an external viewer output
664 :constraintp FUNCTION ... use this viewer only if FUNCTION returns non-nil
666 :constraint-flag SEXP ... use this viewer only if SEXP evaluates to non-nil
668 :open-hook HOOK ... run hooks after spawning the viewer -- mainly useful
669 in conjunction with :capture-output
671 :args (FORMAT-STRING or SYMBOL or FUNCTION) ... a list of arguments
672 \(defaults to (list \"%S\")) when using shell commands
674 Avoid modifying this variable and achieve minor speed-ups by setting the
675 variables my-ps-viewer, my-pdf-viewer, my-dvi-viewer, my-pic-viewer.
677 In order to view pdf or rtf files in an Emacs buffer, you could use these:
680 \(\"^.+\\\\.pdf\\\\'\" \"pdftotext\"
681 \((:capture-output t)
682 \(:args (\"%S - | fmt -w \" window-width))
683 \(:ignore-on-read-text t)
684 \(:constraintp (lambda ()
685 \(and \(filesets-which-command-p \"pdftotext\")
686 \(filesets-which-command-p \"fmt\"))))))
687 \(\"^.+\\\\.rtf\\\\'\" \"rtf2htm\"
688 \((:capture-output t)
689 \(:args (\"%S 2> /dev/null | w3m -dump -T text/html\"))
690 \(:ignore-on-read-text t)
691 \(:constraintp (lambda ()
692 \(and (filesets-which-command-p \"rtf2htm\")
693 \(filesets-which-command-p \"w3m\"))))))"
694 :set
(function filesets-set-default
)
695 :type
'(repeat :tag
"Viewer"
696 (list :tag
"Definition"
697 :value
("^.+\\.suffix$" "")
698 (regexp :tag
"Pattern")
699 (choice :tag
"Viewer"
700 (symbol :tag
"Function" :value nil
)
701 (string :tag
"Program" :value
""))
702 (repeat :tag
"Properties"
704 (list :tag
":constraintp"
705 :value
(:constraintp
)
708 (function :tag
"Function"))
709 (list :tag
":constraint-flag"
710 :value
(:constraint-flag
)
712 :value
:constraint-flag
)
713 (sexp :tag
"Symbol"))
714 (list :tag
":ignore-on-open-all"
715 :value
(:ignore-on-open-all t
)
717 :value
:ignore-on-open-all
)
718 (boolean :tag
"Boolean"))
719 (list :tag
":ignore-on-read-text"
720 :value
(:ignore-on-read-text t
)
722 :value
:ignore-on-read-text
)
723 (boolean :tag
"Boolean"))
729 (choice :tag
"Arguments"
730 (string :tag
"String"
732 (symbol :tag
"Symbol"
734 (function :tag
"Function"
736 (list :tag
":open-hook"
741 ; (list :tag ":close-hook"
742 ; :value (:close-hook)
744 ; :value :close-hook)
745 ; (hook :tag "Hook"))
746 (list :tag
":capture-output"
747 :value
(:capture-output t
)
749 :value
:capture-output
)
750 (boolean :tag
"Boolean"))))))
752 (put 'filesets-external-viewers
'risky-local-variable t
)
754 (defcustom filesets-ingroup-patterns
757 (:pattern
"\\\\usepackage\\W*\\(\\[[^\]]*\\]\\W*\\)?{\\W*\\(.+\\)\\W*}")
760 (:get-file-name
(lambda (master file
)
761 (filesets-which-file master
763 (filesets-convert-path-list
764 (or (getenv "MY_TEXINPUTS")
765 (getenv "TEXINPUTS")))))))
767 (:pattern
"\\\\include\\W*{\\W*\\(.+\\)\\W*}")
768 (:get-file-name
(lambda (master file
)
769 (filesets-which-file master
771 (filesets-convert-path-list
772 (or (getenv "MY_TEXINPUTS")
773 (getenv "TEXINPUTS"))))))
776 (:pattern
"\\\\input\\W*{\\W*\\(.+\\)\\W*}")
777 (:stubp
(lambda (a b
) (not (filesets-files-in-same-directory-p a b
))))
778 (:get-file-name
(lambda (master file
)
779 (filesets-which-file master
781 (filesets-convert-path-list
782 (or (getenv "MY_TEXINPUTS")
783 (getenv "TEXINPUTS"))))))
785 ((:name
"Bibliography")
786 (:pattern
"\\\\bibliography\\W*{\\W*\\(.+\\)\\W*}")
787 (:get-file-name
(lambda (master file
)
788 (filesets-which-file master
790 (filesets-convert-path-list
791 (or (getenv "MY_BIBINPUTS")
792 (getenv "BIBINPUTS")))))))))
795 (:pattern
"(require\\W+'\\(.+\\))")
796 (:stubp
(lambda (a b
) (not (filesets-files-in-same-directory-p a b
))))
797 (:get-file-name
(lambda (master file
)
798 (filesets-which-file master
802 (:pattern
"(load\\(-library\\)?\\W+\"\\(.+\\)\")")
804 (:get-file-name
(lambda (master file
)
805 (filesets-which-file master file load-path
))))))
806 ("^\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)$" t
807 (((:pattern
"\\<\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)\\>")
809 (:stubp
(lambda (a b
) (not (filesets-files-in-same-directory-p a b
))))
811 (:get-file-name
(lambda (master file
)
815 (if (boundp 'emacs-wiki-directories
)
816 emacs-wiki-directories
819 "Inclusion group definitions.
821 Define how to find included file according to a file's mode (being
822 defined by a file pattern).
824 A valid entry has the form (FILE-PATTERN REMOVE-DUPLICATES-FLAG
825 CMD-DEF1 ...), CMD-DEF1 being a plist containing the fields :pattern
826 \(mandatory), :name, :get-file-name, :match-number, :scan-depth,
827 :preprocess, :case-sensitive.
829 File Pattern ... A regexp matching the file's name for which the
830 following rules should be applied.
832 Remove Duplicates ... If t, only the first occurrence of an included
833 file is retained. (See below for a full explanation.)
835 :name STRING ... This pattern's name.
837 :pattern REGEXP ... A regexp matching the command. This regexp has to
838 include a group that holds the name of the included file.
840 :get-file-name FUNCTION (default: `filesets-which-file') ... A function
841 that takes two arguments (the path of the master file and the name
842 of the included file) and returns a valid path or nil -- if the
843 subfile can't be found.
845 :match-number INTEGER (default: 1) ... The number of the match/group
846 in the pattern holding the subfile's name. 0 refers the whole
847 match, 1 to the first group.
849 :stubp FUNCTION ... If (FUNCTION MASTER INCLUDED-FILE) returns non-nil,
850 INCLUDED-FILE is a stub -- see below.
852 :stub-flag ... Files of this type are stubs -- see below.
854 :scan-depth INTEGER (default: 0) ... Whether included files should be
855 rescanned. Set this to 0 to disable re-scanning of included file.
857 :preprocess FUNCTION ... A function modifying a buffer holding the
858 master file so that pattern matching becomes easier. This is usually
859 used to narrow a buffer to the relevant region. This function could also
860 be destructive and simply delete non-relevant text.
862 :case-sensitive BOOLEAN (default: nil) ... Whether a pattern is
863 case-sensitive or not.
868 First, a stub is a file that shows up in the menu but will not be
869 included in an ingroup's file listing -- i.e. filesets will never
870 operate on this file automatically. Secondly, in opposition to normal
871 files stubs are not scanned for new inclusion groups. This is useful if
872 you want to have quick access to library headers.
874 In the menu, an asterisk is appended to the stub's name.
879 E.g. File A and file B refer to file X; X refers to A. If
880 you choose not to remove duplicates the tree would look like:
885 As you can see, there is some chance that you run in circles.
886 Nevertheless, up to some degree this could still be what you want.
888 With duplicates removed, it would be:
892 :set
(function filesets-set-default
)
896 :tag
"Definition" :value
("^.+\\.suffix$" t
)
897 (regexp :tag
"File Pattern" :value
"^.+\\.suffix$")
898 (boolean :tag
"Remove Duplicates" :value t
)
899 (repeat :tag
"Commands"
900 (repeat :tag
"Command"
905 (const :format
"" :value
:name
)
906 (string :tag
"String"))
907 (list :tag
":pattern"
908 :value
(:pattern
"\\<CMD\\W*\\(.+\\)\\>")
909 (const :format
"" :value
:pattern
)
910 (regexp :tag
"RegExp"))
911 (list :tag
":get-file-name"
912 :value
(:get-file-name
)
913 (const :format
"" :value
:get-file-name
)
914 (function :tag
"Function"))
915 (list :tag
":match-number"
916 :value
(:match-number
1)
917 (const :format
"" :value
:match-number
)
918 (integer :tag
"Integer"))
919 (list :tag
":stub-flag"
920 :value
(:stub-flag t
)
921 (const :format
"" :value
:stub-flag
)
922 (boolean :tag
"Boolean"))
925 (const :format
"" :value
:stubp
)
926 (function :tag
"Function"))
927 (list :tag
":scan-depth"
928 :value
(:scan-depth
0)
929 (const :format
"" :value
:scan-depth
)
930 (integer :tag
"Integer"))
931 (list :tag
":case-sensitive"
932 :value
(:case-sensitive
)
933 (const :format
"" :value
:case-sensitive
)
934 (boolean :tag
"Boolean"))
935 (list :tag
":preprocess"
937 (const :format
"" :value
:preprocess
)
938 (function :tag
"Function")))))))
940 (put 'filesets-ingroup-patterns
'risky-local-variable t
)
942 (defcustom filesets-data nil
943 "Fileset definitions.
945 A fileset is either a list of files, a file pattern, a base directory
946 and a search pattern (for files), or a base file. Changes to this
947 variable will take effect after rebuilding the menu.
949 Caveat: Fileset names have to be unique.
953 \(:ingroup \"~/Etc/My-Wiki/WikiContents\"))
955 \(:pattern \"~/public_html/\" \"^.+\\\\.html$\")
956 \(:open filesets-find-file))
957 \(\"User Configuration\"
958 \(:files \"~/.xinitrc\"
960 \"~/.bash_profile\"))
962 \(:tree \"~\" \"^[^.].*[^~]$\")
963 \(:filter-dirs-flag t)))
965 `filesets-data' is a list of (NAME-AS-STRING . DEFINITION), DEFINITION
966 being an association list with the fields:
968 :files FILE-1 .. FILE-N ... a list of files belonging to a fileset
970 :ingroup FILE-NAME ... an inclusion group's base file.
972 :tree ROOT-DIR PATTERN ... a base directory and a file pattern
974 :pattern DIR PATTERN ... a base directory and a regexp matching
975 files in that directory. Usually,
976 PATTERN has the form '^REGEXP$'. Unlike
977 :tree, this form does not descend
978 recursively into subdirectories.
980 :filter-dirs-flag BOOLEAN ... is only used in conjunction with :tree.
982 :tree-max-level INTEGER ... recurse into directories this many levels
983 \(see `filesets-tree-max-level' for a full explanation)
985 :dormant-flag BOOLEAN ... non-nil means don't show this item in the
986 menu; dormant filesets can still be manipulated via commands available
987 from the minibuffer -- e.g. `filesets-open', `filesets-close', or
990 :dormant-p FUNCTION ... a function returning :dormant-flag
992 :open FUNCTION ... the function used to open file belonging to this
993 fileset. The function takes a file name as argument
995 :save FUNCTION ... the function used to save file belonging to this
996 fileset; it takes no arguments, but works on the current buffer.
998 Either :files, :pattern, :tree, or :ingroup must be supplied. :files
999 overrules :tree, :tree overrules :pattern, :pattern overrules :ingroup,
1000 i.e. these tags are mutually exclusive. The fields :open and :save are
1003 In conjunction with the :tree tag, :save is void. :open refers to the
1004 function used for opening files in a directory, not for opening the
1005 directory. For browsing directories, `filesets-browse-dir-function' is used.
1007 Before using :ingroup, make sure that the file type is already
1008 defined in `filesets-ingroup-patterns'."
1010 :set
(function filesets-data-set-default
)
1012 (cons :tag
"Fileset"
1013 (string :tag
"Name" :value
"")
1016 :tag
"Type" :value nil
1017 (list :tag
"Pattern"
1018 :value
(:pattern
"~/" "^.+\\.suffix$")
1019 (const :format
"" :value
:pattern
)
1020 (directory :tag
"Dir")
1021 (regexp :tag
"Pattern"))
1024 (const :format
"" :value
:files
)
1025 (repeat :tag
"Files" file
))
1026 (list :tag
"Single File"
1028 (const :format
"" :value
:file
)
1030 (list :tag
"Inclusion group"
1031 :value
(:ingroup
"~/")
1032 (const :format
"" :value
:ingroup
)
1033 (file :tag
"File" :value
"~/"))
1034 (list :tag
"Directory Tree"
1035 :value
(:tree
"~/" "^.+\\.suffix$")
1036 (const :format
"" :value
:tree
)
1037 (directory :tag
"Dir")
1038 (regexp :tag
"Pattern"))
1039 (list :tag
"Filter directories"
1040 :value
(:filter-dirs-flag
)
1041 (const :format
"" :value
:filter-dirs-flag
)
1042 (boolean :tag
"Boolean" :value nil
))
1043 (list :tag
"Scanning depth"
1044 :value
(:tree-max-level
3)
1045 (const :format
"" :value
:tree-max-level
)
1046 (integer :tag
"Integer"))
1047 (list :tag
"Verbosity"
1048 :value
(:verbosity
1)
1049 (const :format
"" :value
:verbosity
)
1050 (integer :tag
"Integer"))
1051 (list :tag
"Conceal fileset (Flag)"
1052 :value
(:dormant-flag
)
1053 (const :format
"" :value
:dormant-flag
)
1054 (boolean :tag
"Boolean"))
1055 (list :tag
"Conceal fileset (Function)"
1057 (const :format
"" :value
:dormant-p
)
1058 (function :tag
"Function"))
1059 (list :tag
"Save function"
1061 (const :format
"" :value
:save
)
1062 (function :tag
"Function"))
1063 (list :tag
"Open function"
1065 (const :format
"" :value
:open
)
1066 (function :tag
"Function")))))))
1067 (put 'filesets-data
'risky-local-variable t
)
1070 (defcustom filesets-query-user-limit
15
1071 "Query the user before opening a fileset with that many files."
1072 :set
(function filesets-set-default
)
1076 ;;; Emacs compatibility
1078 (if (featurep 'xemacs
)
1079 (fset 'filesets-error
'error
)
1083 (defun filesets-error (class &rest args
)
1085 (error "%s" (mapconcat 'identity args
" ")))
1089 (defun filesets-filter-dir-names (lst &optional negative
)
1090 "Remove non-directory names from a list of strings.
1091 If NEGATIVE is non-nil, remove all directory names."
1092 (filesets-filter-list lst
1094 (and (not (string-match "^\\.+/$" x
))
1096 (not (string-match "[:/\\]$" x
))
1097 (string-match "[:/\\]$" x
))))))
1099 (defun filesets-conditional-sort (lst &optional access-fn
)
1100 "Return a sorted copy of LST, LST being a list of strings.
1101 If `filesets-sort-menu-flag' is nil, return LST itself.
1103 ACCESS-FN ... function to get the string value of LST's elements."
1104 (if filesets-sort-menu-flag
1105 (let* ((fni (or access-fn
1106 (function identity
)))
1107 (fn (if filesets-sort-case-sensitive-flag
1109 (string< (funcall fni a
)
1112 (string< (upcase (funcall fni a
))
1113 (upcase (funcall fni b
)))))))
1114 (sort (copy-sequence lst
) fn
))
1117 (defun filesets-directory-files (dir &optional
1118 pattern what full-flag match-dirs-flag
)
1119 "Get WHAT (:files or :dirs) in DIR.
1120 If PATTERN is provided return only those entries matching this
1122 If MATCH-DIRS-FLAG is non-nil, also match directory entries.
1123 Return full path if FULL-FLAG is non-nil."
1124 (filesets-message 2 "Filesets: scanning %S" dir
)
1126 ((file-exists-p dir
)
1129 (dolist (this (file-name-all-completions "" dir
))
1131 ((string-match "^\\.+/$" this
)
1133 ((string-match "[:/\\]$" this
)
1134 (when (or (not match-dirs-flag
)
1136 (string-match pattern this
))
1137 (filesets-message 5 "Filesets: matched dir %S with pattern %S"
1139 (setq dirs
(cons this dirs
))))
1141 (when (or (not pattern
)
1142 (string-match pattern this
))
1143 (filesets-message 5 "Filesets: matched file %S with pattern %S"
1145 (setq files
(cons (if full-flag
1146 (concat (file-name-as-directory dir
) this
)
1150 ((equal what
':dirs
)
1151 (filesets-conditional-sort dirs
))
1152 ((equal what
':files
)
1153 (filesets-conditional-sort files
))
1155 (append (filesets-conditional-sort files
)
1156 (filesets-conditional-sort dirs
))))))
1157 (filesets-be-docile-flag
1158 (filesets-message 1 "Filesets: %S doesn't exist" dir
)
1161 (filesets-error 'error
"Filesets: " dir
" does not exist"))))
1163 (defun filesets-quote (txt)
1164 "Return TXT in quotes."
1165 (concat "\"" txt
"\""))
1167 (defun filesets-get-selection ()
1168 "Get the text between mark and point -- i.e. the selection or region."
1172 (buffer-substring (min m p
) (max m p
))
1173 (filesets-error 'error
"No selection."))))
1175 (defun filesets-get-quoted-selection ()
1176 "Return the currently selected text in quotes."
1177 (filesets-quote (filesets-get-selection)))
1179 (defun filesets-get-shortcut (n)
1180 "Create menu shortcuts based on number N."
1181 (let ((n (mod (- n
1) 51)))
1183 ((not filesets-menu-shortcuts-flag
)
1186 (concat (number-to-string n
) " "))
1188 (format "%c " (+ 87 n
)))
1190 (format "%c " (+ -
3 n
))))))
1192 (defun filesets-files-equalp (a b
)
1193 "Compare two filenames A and B after expansion."
1194 (equal (expand-file-name a
) (expand-file-name b
)))
1196 (defun filesets-files-in-same-directory-p (a b
)
1197 "Compare two filenames A and B after expansion."
1198 (let ((ad (file-name-directory (expand-file-name a
)))
1199 (bd (file-name-directory (expand-file-name b
))))
1202 (defun filesets-convert-path-list (string)
1203 "Return a path-list given as STRING as list."
1205 (mapcar (lambda (x) (file-name-as-directory x
))
1206 (split-string string path-separator
))
1209 (defun filesets-which-file (master filename
&optional path-list
)
1210 "Search for a FILENAME relative to a MASTER file in PATH-LIST."
1211 (let ((f (concat (file-name-directory master
)
1213 (if (file-exists-p f
)
1217 (let ((dir (file-name-as-directory dir
))
1218 (files (if (file-exists-p dir
)
1219 (filesets-directory-files dir nil
':files
)
1221 (filesets-some (lambda (file)
1222 (if (equal filename
(file-name-nondirectory file
))
1229 (defun filesets-eviewer-get-props (entry)
1230 "Get ENTRY's (representing an external viewer) properties."
1233 (defun filesets-eviewer-constraint-p (entry)
1234 (let* ((props (filesets-eviewer-get-props entry
))
1235 (constraint (assoc ':constraintp props
))
1236 (constraint-flag (assoc ':constraint-flag props
)))
1239 (funcall (cadr constraint
)))
1241 (eval (cadr constraint-flag
)))
1245 (defun filesets-get-external-viewer (file)
1246 "Find an external viewer for FILE."
1247 (let ((filename (file-name-nondirectory file
)))
1250 (when (and (string-match (nth 0 entry
) filename
)
1251 (filesets-eviewer-constraint-p entry
))
1253 filesets-external-viewers
)))
1255 (defun filesets-get-external-viewer-by-name (name)
1256 "Get the external viewer definition called NAME."
1260 (when (and (string-equal (nth 1 entry
) name
)
1261 (filesets-eviewer-constraint-p entry
))
1263 filesets-external-viewers
)))
1265 (defun filesets-filetype-property (filename event
&optional entry
)
1266 "Return non-nil if a file of a specific type has special flags/tags.
1268 Events (corresponding tag):
1270 on-open-all (:ignore-on-open-all) ... Exclude files of this when opening
1273 on-grep (:ignore-on-read-text) ... Exclude files of this when running
1274 the \"Grep <<selection>>\" command
1276 on-capture-output (:capture-output) ... Capture output of an external viewer
1282 on-close-all ... Not used"
1283 (let ((def (filesets-eviewer-get-props
1285 (filesets-get-external-viewer filename
)))))
1286 (filesets-alist-get def
1288 (`on-open-all
':ignore-on-open-all
)
1289 (`on-grep
':ignore-on-read-text
)
1291 (`on-close-all nil
))
1294 (defun filesets-filetype-get-prop (property filename
&optional entry
)
1295 "Return PROPERTY for filename -- use ENTRY if provided."
1296 (let ((def (filesets-eviewer-get-props
1298 (filesets-get-external-viewer filename
)))))
1300 (filesets-alist-get def property nil t
))))
1302 (defun filesets-reset-filename-on-change ()
1303 "Reset a buffer's filename if the buffer is being modified."
1304 (when filesets-output-buffer-flag
1305 (set-visited-file-name nil t
)))
1307 (defun filesets-spawn-external-viewer (file &optional ev-entry
)
1308 "Start an external viewer for FILE.
1309 Use the viewer defined in EV-ENTRY (a valid element of
1310 `filesets-external-viewers') if provided."
1311 (let* ((file (expand-file-name file
))
1313 (filesets-get-external-viewer file
))))
1315 (let* ((vwr (cadr entry
))
1316 (co-flag (filesets-filetype-get-prop ':capture-output file entry
))
1317 (oh (filesets-filetype-get-prop ':open-hook file entry
))
1318 (args (let ((fmt (filesets-filetype-get-prop ':args file entry
)))
1321 (dolist (this fmt rv
)
1326 ((and (symbolp this
)
1328 (format "%S" (funcall this
)))
1330 (format "%S" this
)))))))
1331 (format "%S" file
))))
1334 ((and (functionp vwr
) co-flag
)
1340 (shell-command-to-string (format "%s %s" vwr args
)))
1342 (shell-command (format "%s %s&" vwr args
))
1346 (switch-to-buffer (format "Filesets: %s %s" vwr file
))
1348 (make-local-variable 'filesets-output-buffer-flag
)
1349 (setq filesets-output-buffer-flag t
)
1350 (set-visited-file-name file t
)
1353 (set-buffer-modified-p nil
)
1354 (setq buffer-read-only t
)
1355 (goto-char (point-min)))
1358 (filesets-error 'error
1359 "Filesets: general error when spawning external viewer"))))
1361 (defun filesets-find-file (file)
1362 "Call `find-file' after a possible delay (see `filesets-find-file-delay').
1363 If `filesets-be-docile-flag' is true, a file, which isn't readable, will
1365 ; (sleep-for filesets-find-file-delay)
1366 (when (or (file-readable-p file
)
1367 (not filesets-be-docile-flag
))
1368 (sit-for filesets-find-file-delay
)
1371 (defun filesets-find-or-display-file (&optional file viewer
)
1372 "Visit FILE using an external VIEWER or open it in an Emacs buffer."
1374 (let* ((file (or file
1375 (read-file-name "Find file: " nil nil viewer
)))
1376 (external-viewer-def (or
1377 (filesets-get-external-viewer-by-name viewer
)
1378 (filesets-get-external-viewer file
))))
1379 (filesets-message 3 "Filesets: view %S using %s" file external-viewer-def
)
1380 (if external-viewer-def
1381 (filesets-spawn-external-viewer file external-viewer-def
)
1382 (filesets-find-file file
))))
1384 (defun filesets-find-file-using ()
1385 "Select a viewer and call `filesets-find-or-display-file'."
1387 (let* ((lst (mapcar (lambda (this)
1388 (let ((a (cadr this
)))
1389 (list (format "%s" a
) a
)))
1390 filesets-external-viewers
))
1391 (viewer (completing-read "Using viewer: " lst nil t
)))
1393 (filesets-find-or-display-file nil
(cadr (assoc viewer lst
))))))
1395 (defun filesets-browser-name ()
1396 "Get the directory browser's name as defined in `filesets-browse-dir-function'."
1398 ((listp filesets-browse-dir-function
)
1399 (car filesets-browse-dir-function
))
1401 filesets-browse-dir-function
)))
1403 (defun filesets-browse-dir (dir)
1404 "Browse DIR using `filesets-browse-dir-function'."
1405 (if (functionp filesets-browse-dir-function
)
1406 (funcall filesets-browse-dir-function dir
)
1407 (let ((name (car filesets-browse-dir-function
))
1408 (args (format (cadr filesets-browse-dir-function
) (expand-file-name dir
))))
1410 (start-process (concat "Filesets:" name
)
1411 "*Filesets external directory browser*"
1414 (defun filesets-get-fileset-name (something)
1415 "Get SOMETHING's name (Don't ask)."
1422 (defun filesets-data-get-name (entry)
1423 "Access to `filesets-data'. Get the ENTRY's name."
1426 (defun filesets-data-get-data (entry)
1427 "Access to `filesets-data'. Get the ENTRY's data section."
1430 (defun filesets-alist-get (alist key
&optional default carp
)
1431 "Get KEY's value in the association list ALIST.
1432 Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
1433 (let ((elt (assoc key alist
)))
1442 (defun filesets-data-get (entry key
&optional default carp
)
1443 "Extract the value for KEY in the data part of fileset ENTRY.
1444 Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
1445 (filesets-alist-get (filesets-data-get-data entry
) key default carp
))
1447 (defun filesets-data-set (entry key value
)
1448 "Set the VALUE for KEY in the data part of fileset ENTRY."
1449 (let* ((alist (filesets-data-get-data entry
))
1450 (elt (assoc key alist
)))
1453 (setcdr entry
(cons (cons key value
) alist
)))))
1455 (defun filesets-entry-mode (entry)
1456 "Return fileset ENTRY's mode: :files, :file, :tree, :pattern, or :ingroup.
1457 See `filesets-data'."
1458 (let ((data (filesets-data-get-data entry
)))
1463 '(:files
:tree
:pattern
:ingroup
:file
))))
1465 (defun filesets-entry-get-open-fn (fileset-name &optional fileset-entry
)
1466 "Get the open-function for FILESET-NAME.
1467 Use FILESET-ENTRY for finding the open function, if provided."
1468 (filesets-data-get (or fileset-entry
1469 (filesets-get-fileset-from-name fileset-name
))
1470 ':open filesets-open-file-function t
))
1472 (defun filesets-entry-get-save-fn (fileset-name &optional fileset-entry
)
1473 "Get the save-function for FILESET-NAME.
1474 Use FILESET-ENTRY for finding the save function, if provided."
1475 (filesets-data-get (or fileset-entry
1476 (filesets-get-fileset-from-name fileset-name
))
1477 ':save filesets-save-buffer-function t
))
1479 (defun filesets-entry-get-files (entry)
1480 "Get the file list for fileset ENTRY."
1481 (filesets-data-get entry
':files
))
1483 (defun filesets-entry-set-files (entry data
&optional anyways
)
1484 "Set the file list for fileset ENTRY."
1485 (let ((files (filesets-entry-get-files entry
)))
1486 (if (or anyways files
)
1487 (filesets-data-set entry
':files data
))))
1489 (defun filesets-entry-get-verbosity (entry)
1490 "Get verbosity level for fileset ENTRY."
1491 (filesets-data-get entry
':verbosity
1 t
))
1493 (defun filesets-entry-get-file (entry)
1494 "Get the single file for fileset ENTRY."
1495 (filesets-data-get entry
':file nil t
))
1497 (defun filesets-entry-get-pattern (entry)
1498 "Get the base directory + file pattern for fileset ENTRY."
1499 ; (filesets-data-get entry ':pattern nil t))
1500 (filesets-data-get entry
':pattern
))
1502 (defun filesets-entry-get-pattern--pattern (list)
1503 "Get the file pattern for LIST."
1504 (if (= (length list
) 1) ;; for compatibility with filesets < v1.5.5
1505 (file-name-nondirectory (car list
))
1508 (defun filesets-entry-get-pattern--dir (list)
1509 "Get a file pattern's base directory for LIST."
1510 (if (= (length list
) 1) ;; for compatibility with filesets < v1.5.5
1511 (file-name-directory (car list
))
1514 (defun filesets-entry-get-tree (entry)
1515 "Get the tree pattern for fileset ENTRY."
1516 (filesets-data-get entry
':tree
))
1518 (defun filesets-entry-get-dormant-flag (entry)
1519 "Get dormant flag for fileset ENTRY."
1520 (let ((fn (filesets-data-get entry
':dormant-p nil t
)))
1523 (filesets-data-get entry
':dormant-flag nil t
))))
1525 (defun filesets-entry-get-filter-dirs-flag (entry)
1526 "Get filter-dirs-flag for fileset ENTRY."
1527 (filesets-data-get entry
':filter-dirs-flag nil t
))
1529 (defun filesets-entry-get-tree-max-level (entry)
1530 "Get maximal tree scanning depth for fileset ENTRY."
1531 (filesets-data-get entry
':tree-max-level nil t
))
1533 (defun filesets-entry-get-master (entry)
1534 "Get the base file for fileset ENTRY."
1535 (filesets-data-get entry
':ingroup nil t
))
1537 (defun filesets-file-open (open-function file-name
&optional fileset-name
)
1538 "Open FILE-NAME using OPEN-FUNCTION.
1539 If OPEN-FUNCTION is nil, its value will be deduced from FILESET-NAME."
1540 (let ((open-function (or open-function
1541 (filesets-entry-get-open-fn fileset-name
))))
1542 (if (file-readable-p file-name
)
1543 (funcall open-function file-name
)
1544 (message "Filesets: Couldn't open `%s'" file-name
))))
1546 (defun filesets-file-close (save-function buffer
)
1548 First, save the buffer's contents using SAVE-FUNCTION. Then, kill buffer
1549 if `buffer-modified-p' returns nil.
1551 SAVE-FUNCTION takes no argument, but works on the current buffer."
1552 (with-current-buffer buffer
1553 (if (buffer-modified-p)
1554 (funcall save-function
))
1555 (if (not (buffer-modified-p))
1556 (kill-buffer buffer
))))
1558 (defun filesets-get-fileset-from-name (name &optional mode
)
1559 "Get fileset definition for NAME."
1561 ((or `:ingroup
`:tree
) name
)
1562 (_ (assoc name filesets-data
))))
1566 (defun filesets-cmd-get-def (cmd-name)
1567 "Get `filesets-commands' entry for CMD-NAME."
1568 (assoc cmd-name filesets-commands
))
1570 (defun filesets-cmd-get-args (cmd-name)
1571 (let ((args (let ((def (filesets-cmd-get-def cmd-name
)))
1574 (dolist (this args rv
)
1576 ((and (symbolp this
) (fboundp this
))
1577 (let ((x (funcall this
)))
1578 (setq rv
(append rv
(if (listp x
) x
(list x
))))))
1580 (setq rv
(append rv
(list this
))))))))
1582 (defun filesets-cmd-get-fn (cmd-name)
1583 (let ((def (filesets-cmd-get-def cmd-name
)))
1586 (defun filesets-cmd-show-result (cmd output
)
1587 "Show OUTPUT of CMD (a shell command)."
1588 (pop-to-buffer "*Filesets: Shell Command Output*")
1597 (defun filesets-run-cmd--repl-fn (arg &optional format-fn
)
1598 "Helper function for `filesets-run-cmd'. Apply FORMAT-FN to arg.
1599 Replace <file-name> or <<file-name>> with filename."
1600 (funcall format-fn
(cond
1601 ((equal arg
"<file-name>")
1603 ((equal arg
"<<file-name>>")
1604 (shell-quote-argument (buffer-file-name)))
1608 (defun filesets-run-cmd (&optional cmd-name fileset mode
)
1609 "Run CMD-NAME (see `filesets-commands') on FILESET."
1611 (let* ((cmd-name (or cmd-name
1612 (completing-read "Select command: " filesets-commands
1615 (completing-read "Select fileset: " filesets-data nil t
))))
1616 (when (and cmd-name name
)
1617 (let* ((event (if (equal cmd-name
"Grep <<selection>>")
1620 (files (if (and fileset
1621 (or (equal mode
':ingroup
)
1622 (equal mode
':tree
)))
1623 (filesets-get-filelist fileset mode event
)
1624 (filesets-get-filelist
1625 (filesets-get-fileset-from-name name
)
1628 (let ((fn (filesets-cmd-get-fn cmd-name
))
1629 (args (filesets-cmd-get-args cmd-name
)))
1630 (if (memq fn
'(multi-isearch-files multi-isearch-files-regexp
))
1632 (dolist (this files nil
)
1635 (let ((buffer (filesets-find-file this
)))
1637 (goto-char (point-min))
1643 (dolist (this args txt
)
1646 (filesets-run-cmd--repl-fn
1649 (if (equal txt
"") "" " ")
1650 (format "%s" this
))))))))
1651 (cmd (concat fn
" " args
)))
1652 (filesets-cmd-show-result
1653 cmd
(shell-command-to-string cmd
))))
1657 (dolist (this args argl
)
1660 (filesets-run-cmd--repl-fn
1663 (apply fn args
)))))))))))))))))
1665 (defun filesets-get-cmd-menu ()
1666 "Create filesets command menu."
1668 .
,(mapcar (lambda (this)
1669 (let ((name (car this
)))
1670 `[,name
(filesets-run-cmd ,name
)]))
1671 filesets-commands
)))
1675 (defun filesets-cmd-query-replace-getargs ()
1676 "Get arguments for `query-replace' and `query-replace-regexp'."
1677 (let ((common (query-replace-read-args "Filesets query replace" nil t
)))
1678 (list (nth 0 common
) (nth 1 common
) t nil
(nth 2 common
) nil
1679 multi-query-replace-map
)))
1681 (defun filesets-cmd-query-replace-regexp-getargs ()
1682 "Get arguments for `query-replace' and `query-replace-regexp'."
1683 (let ((common (query-replace-read-args "Filesets query replace" t t
)))
1684 (list (nth 0 common
) (nth 1 common
) t t
(nth 2 common
) nil
1685 multi-query-replace-map
)))
1687 (defun filesets-cmd-isearch-getargs ()
1688 "Get arguments for `multi-isearch-files' and `multi-isearch-files-regexp'."
1689 (and (boundp 'files
) (list files
)))
1691 (defun filesets-cmd-shell-command-getargs ()
1692 "Get arguments for `filesets-cmd-shell-command'."
1693 (let* ((arg (read-string "Shell command (%s = file): "
1695 'shell-command-history
)))
1698 (defun filesets-cmd-shell-command (txt)
1699 "Wrapper function for `shell-command'."
1700 (let ((ok (if (buffer-modified-p)
1701 (let ((ok (y-or-n-p "Save buffer? ")))
1707 (let ((cmd (format txt
(shell-quote-argument (buffer-file-name)))))
1708 (message "Filesets: %s" cmd
)
1709 (filesets-cmd-show-result cmd
1710 (shell-command-to-string cmd
))))))
1714 (defun filesets-get-filelist (entry &optional mode event
)
1715 "Get all files for fileset ENTRY.
1716 Assume MODE (see `filesets-entry-mode'), if provided."
1717 (let* ((mode (or mode
1718 (filesets-entry-mode entry
)))
1721 (filesets-entry-get-files entry
))
1723 (list (filesets-entry-get-file entry
)))
1725 (let ((entry (expand-file-name
1728 (filesets-entry-get-master entry
)))))
1729 (cons entry
(filesets-ingroup-cache-get entry
))))
1731 (let ((dir (nth 0 entry
))
1732 (patt (nth 1 entry
)))
1733 (filesets-directory-files dir patt
':files t
)))
1735 (let ((dirpatt (filesets-entry-get-pattern entry
)))
1737 (let ((dir (filesets-entry-get-pattern--dir dirpatt
))
1738 (patt (filesets-entry-get-pattern--pattern dirpatt
)))
1739 ;;(filesets-message 3 "Filesets: scanning %s" dirpatt)
1740 (filesets-directory-files dir patt
':files t
))
1741 ;; (message "Filesets: malformed entry: %s" entry)))))))
1742 (filesets-error 'error
"Filesets: malformed entry: "
1744 (filesets-filter-list fl
1746 (not (filesets-filetype-property file event
))))))
1748 (defun filesets-open (&optional mode name lookup-name
)
1749 "Open the fileset called NAME.
1750 Use LOOKUP-NAME for searching additional data if provided."
1752 (let* ((name (or name
1753 (completing-read "Open fileset: " filesets-data nil t
)))
1754 (fileset (filesets-get-fileset-from-name name mode
))
1755 (lookup-fs (if lookup-name
1756 (filesets-get-fileset-from-name lookup-name
)
1758 (mode (or mode
(filesets-entry-mode lookup-fs
))))
1760 (let* ((files (filesets-get-filelist fileset mode
'on-open-all
))
1762 (open-function (filesets-entry-get-open-fn nil lookup-fs
)))
1763 (if (or (<= n filesets-query-user-limit
)
1764 (y-or-n-p (format "Filesets: Open all %d files in %s? "
1766 (dolist (this files nil
)
1767 (filesets-file-open open-function this
))
1768 (message "Filesets: cancelled")))
1769 (filesets-error 'error
"Filesets: Unknown fileset: " name
))))
1771 (defun filesets-close (&optional mode name lookup-name
)
1772 "Close all buffers belonging to the fileset called NAME.
1773 Use LOOKUP-NAME for deducing the save-function, if provided."
1775 (let* ((name (or name
1776 (completing-read "Close fileset: " filesets-data nil t
)))
1777 (fileset (filesets-get-fileset-from-name name mode
))
1778 (lookup-fs (if lookup-name
1779 (filesets-get-fileset-from-name lookup-name
)
1781 (mode (or mode
(filesets-entry-mode lookup-fs
))))
1783 (let ((files (filesets-get-filelist fileset mode
'on-close-all
))
1784 (save-function (filesets-entry-get-save-fn nil lookup-fs
)))
1785 (dolist (file-name files nil
)
1786 (let* ((buffer (get-file-buffer file-name
)))
1788 (filesets-file-close save-function buffer
)))))
1789 ; (message "Filesets: Unknown fileset: `%s'" name))))
1790 (filesets-error 'error
"Filesets: Unknown fileset: " name
))))
1792 (defun filesets-add-buffer (&optional name buffer
)
1793 "Add BUFFER (or current buffer) to the fileset called NAME.
1794 User will be queried, if no fileset name is provided."
1796 (let* ((buffer (or buffer
1800 (format "Add '%s' to fileset: " buffer
)
1801 filesets-data nil
)))
1802 (entry (or (assoc name filesets-data
)
1804 (format "Fileset %s does not exist. Create it? "
1807 (add-to-list 'filesets-data
(list name
'(:files
)))
1809 "Fileset %s created. Call `M-x filesets-save-config' to save."
1811 (car filesets-data
))))))
1813 (let* ((files (filesets-entry-get-files entry
))
1814 (this (buffer-file-name buffer
))
1815 (inlist (filesets-member this files
1816 :test
'filesets-files-equalp
)))
1819 (message "Filesets: '%s' is already in '%s'" this name
))
1820 ((and (equal (filesets-entry-mode entry
) ':files
)
1822 (filesets-entry-set-files entry
(cons this files
) t
)
1823 (filesets-set-config name
'filesets-data filesets-data
))
1825 (message "Filesets: Can't add '%s' to fileset '%s'" this name
)))))))
1827 (defun filesets-remove-buffer (&optional name buffer
)
1828 "Remove BUFFER (or current buffer) to fileset NAME.
1829 User will be queried, if no fileset name is provided."
1831 (let* ((buffer (or buffer
1835 (format "Remove '%s' from fileset: " buffer
)
1836 filesets-data nil t
)))
1837 (entry (assoc name filesets-data
)))
1839 (let* ((files (filesets-entry-get-files entry
))
1840 (this (buffer-file-name buffer
))
1841 (inlist (filesets-member this files
1842 :test
'filesets-files-equalp
)))
1843 ;;(message "%s %s %s" files this inlist)
1844 (if (and files this inlist
)
1845 (let ((new (list (cons ':files
(delete (car inlist
) files
)))))
1847 (filesets-set-config name
'filesets-data filesets-data
))
1848 (message "Filesets: Can't remove '%s' from fileset '%s'"
1852 (defun filesets-convert-patterns (name)
1853 "Change fileset NAME's mode from :pattern to :files."
1855 (let ((entry (assoc name filesets-data
)))
1857 (let ((pattern (filesets-entry-get-pattern entry
))
1858 (patfiles (filesets-get-filelist entry
':pattern
)))
1861 (filesets-entry-set-files entry patfiles t
)
1862 (filesets-set-config name
'filesets-data filesets-data
)))))))
1864 (defun filesets-edit ()
1865 "Customize `filesets-data'."
1867 (customize-variable 'filesets-data
))
1869 (defun filesets-customize ()
1870 "Customize the filesets group."
1872 (customize-group 'filesets
))
1874 (defun filesets-info ()
1875 "Display filesets's version information."
1877 (if (y-or-n-p (format "Filesets v%s: visit homepage? " filesets-version
))
1878 (filesets-goto-homepage)))
1880 (defun filesets-goto-homepage ()
1881 "Show filesets's homepage."
1883 (browse-url filesets-homepage
))
1885 (defun filesets-remake-shortcut (count submenu
)
1886 "Remake a submenu's shortcut when wrapping long menus."
1887 (let* ((name (concat (filesets-get-shortcut count
)
1888 (substring (elt submenu
0) 2))))
1890 (cons name
(cdr submenu
))
1891 (apply 'vector
(list name
(cdr (append submenu nil
)))))))
1892 ; (vconcat `[,name] (subseq submenu 1)))))
1894 (defun filesets-wrap-submenu (submenu-body)
1895 "Split long submenus."
1896 (let ((bl (length submenu-body
)))
1897 (if (or (= filesets-max-submenu-length
0)
1898 (<= bl filesets-max-submenu-length
))
1901 (factor (ceiling (/ (float bl
)
1902 filesets-max-submenu-length
))))
1903 (cl-do ((data submenu-body
(cdr data
))
1905 (count 0 (+ count factor
)))
1908 ;; (let ((sl (subseq submenu-body count
1909 (let ((sl (filesets-sublist submenu-body count
1910 (let ((x (+ count factor
)))
1918 (if (= (length sl
) 1)
1919 (if filesets-menu-shortcuts-flag
1920 (list (filesets-remake-shortcut n
(car sl
)))
1923 (filesets-get-shortcut n
)
1925 (cl-do ((x sl
(cdr x
)))
1927 (let ((y (concat (elt (car x
) 0)
1934 (if filesets-menu-shortcuts-flag
1938 filesets-max-entry-length
)
1940 (substring rv
0 filesets-max-entry-length
)
1946 (defun filesets-get-menu-epilog (something &optional
1947 mode lookup-name rebuild-flag
)
1948 "Get submenu epilog for SOMETHING (usually a fileset).
1949 If mode is :tree or :ingroup, SOMETHING is some weird construct and
1950 LOOKUP-NAME is used as lookup name for retrieving fileset specific settings."
1954 ["Close all files" (filesets-close ',mode
',something
',lookup-name
)]
1955 ["Run Command" (filesets-run-cmd nil
',something
',mode
)]
1956 [,(format "Browse with `%s'" (filesets-browser-name))
1957 (filesets-browse-dir ',(car something
))]
1958 ,@(when rebuild-flag
1959 `(["Rebuild this submenu"
1960 (filesets-rebuild-this-submenu ',lookup-name
)]))))
1963 ["Close all files" (filesets-close ',mode
',something
',lookup-name
)]
1964 ["Run Command" (filesets-run-cmd nil
',something
',mode
)]
1965 ,@(when rebuild-flag
1966 `(["Rebuild this submenu"
1967 (filesets-rebuild-this-submenu ',lookup-name
)]))))
1970 ["Close all files" (filesets-close ',mode
',something
)]
1971 ["Run Command" (filesets-run-cmd nil
',something
',mode
)]
1972 [,(format "Browse with `%s'" (filesets-browser-name))
1973 ,(list 'filesets-browse-dir
1974 (filesets-entry-get-pattern--dir
1975 (filesets-entry-get-pattern
1976 (filesets-get-fileset-from-name something
':pattern
))))]
1977 ; [,(concat (if filesets-menu-shortcuts-flag
1978 ; (concat "Con" filesets-menu-shortcuts-marker "vert")
1980 ; " :pattern to :files")
1981 ; ,(list (function filesets-convert-patterns) something)]
1982 ,@(when rebuild-flag
1983 `(["Rebuild this submenu"
1984 (filesets-rebuild-this-submenu ',lookup-name
)]))))
1987 [,(concat "Close all files") (filesets-close ',mode
',something
)]
1988 ["Run Command" (filesets-run-cmd nil
',something
',mode
)]
1989 ["Add current buffer"
1990 (filesets-add-buffer ',something
(current-buffer))]
1991 ["Remove current buffer"
1992 (filesets-remove-buffer ',something
(current-buffer))]
1993 ,@(when rebuild-flag
1994 `(["Rebuild this submenu"
1995 (filesets-rebuild-this-submenu ',lookup-name
)]))))
1997 (filesets-error 'error
"Filesets: malformed definition of " something
))))
1999 (defun filesets-ingroup-get-data (master pos
&optional fun
)
2000 "Access to `filesets-ingroup-patterns'. Extract data section."
2001 (let ((masterfile (file-name-nondirectory master
))
2002 (fn (or fun
(lambda (a b
)
2005 (string-match a b
))))))
2006 (filesets-some (lambda (x)
2007 (if (funcall fn
(car x
) masterfile
)
2010 filesets-ingroup-patterns
)))
2012 (defun filesets-ingroup-get-pattern (master)
2013 "Access to `filesets-ingroup-patterns'. Extract patterns."
2014 (filesets-ingroup-get-data master
2))
2016 (defun filesets-ingroup-get-remdupl-p (master)
2017 "Access to `filesets-ingroup-patterns'. Extract remove-duplicates-flag."
2018 (filesets-ingroup-get-data master
1))
2020 (defun filesets-ingroup-collect-finder (patt case-sensitivep
)
2021 "Helper function for `filesets-ingroup-collect'. Find pattern PATT."
2022 (let ((cfs case-fold-search
)
2024 (setq case-fold-search
(not case-sensitivep
))
2025 (re-search-forward patt nil t
))))
2026 (setq case-fold-search cfs
)
2029 (defun filesets-ingroup-cache-get (master)
2030 "Access to `filesets-ingroup-cache'."
2031 (lax-plist-get filesets-ingroup-cache master
))
2033 (defun filesets-ingroup-cache-put (master file
)
2034 "Access to `filesets-ingroup-cache'."
2035 (let* ((emaster (expand-file-name master
))
2037 (cons file
(filesets-ingroup-cache-get emaster
))
2039 (setq filesets-ingroup-cache
2040 (lax-plist-put filesets-ingroup-cache emaster this
))))
2042 (defun filesets-ingroup-collect-files (fs &optional remdupl-flag master depth
)
2043 "Helper function for `filesets-ingroup-collect'. Collect file names."
2044 (let* ((master (or master
2045 (filesets-entry-get-master fs
)))
2046 (remdupl-flag (or remdupl-flag
2047 (filesets-ingroup-get-remdupl-p master
))))
2048 (filesets-ingroup-cache-put master nil
)
2049 (filesets-message 2 "Filesets: parsing %S" master
)
2050 (let ((cmdpatts (filesets-ingroup-get-pattern master
))
2054 (dolist (this-def cmdpatts rv
)
2055 (let* ((this-patt (filesets-alist-get this-def
':pattern nil t
))
2056 (this-name (filesets-alist-get this-def
':name
"" t
))
2057 (this-pp (filesets-alist-get this-def
':preprocess nil t
))
2058 (this-mn (filesets-alist-get this-def
':match-number
1 t
))
2060 (filesets-alist-get this-def
':scan-depth
0 t
)))
2061 (this-csp (filesets-alist-get this-def
':case-sensitive nil t
))
2062 (this-fn (filesets-alist-get
2063 this-def
':get-file-name
'filesets-which-file t
))
2064 (this-stubp (filesets-alist-get this-def
':stubp nil t
))
2065 (this-stub-flag (filesets-alist-get this-def
':stub-flag nil t
))
2070 (filesets-error 'error
"Filesets: malformed :ingroup definition "
2076 (insert-file-contents master
)
2077 (goto-char (point-min))
2080 (while (filesets-ingroup-collect-finder this-patt this-csp
)
2081 (let* ((txt (match-string this-mn
))
2082 (f (funcall this-fn master txt
)))
2084 (not (member f flist
))
2085 (or (not remdupl-flag
)
2086 (not (filesets-member
2087 f filesets-ingroup-files
2088 :test
'filesets-files-equalp
))))
2090 (and (not this-stub-flag
)
2092 (not (funcall this-stubp master f
))
2094 (setq count
(+ count
1))
2095 (setq flist
(cons f flist
))
2096 (setq filesets-ingroup-files
2097 (cons f filesets-ingroup-files
))
2099 (filesets-ingroup-cache-put master f
))
2100 (setq lst
(append lst
(list f
))))))))
2104 (mapcar (lambda (this)
2105 `((,this
,this-name
)
2106 ,@(filesets-ingroup-collect-files
2107 fs remdupl-flag this
2110 (filesets-message 2 "Filesets: no patterns defined for %S" master
)))))
2112 (defun filesets-ingroup-collect-build-menu (fs flist
&optional other-count
)
2113 "Helper function for `filesets-ingroup-collect'. Build the menu.
2114 FS is a fileset's name. FLIST is a list returned by
2115 `filesets-ingroup-collect-files'."
2121 (dolist (this flist rv
)
2122 (setq count
(+ count
1))
2123 (let* ((def (if (listp this
) (car this
) (list this
"")))
2124 (files (if (listp this
) (cdr this
) nil
))
2125 (master (nth 0 def
))
2127 (nm (concat (filesets-get-shortcut (if (or (not other-count
) files
)
2129 (if (or (null name
) (equal name
""))
2131 (format "%s: " name
))
2132 (file-name-nondirectory master
))))
2137 [,(concat "Inclusion Group: "
2138 (file-name-nondirectory master
))
2139 (filesets-open ':ingroup
',master
',fsn
)]
2141 [,master
(filesets-file-open nil
',master
',fsn
)]
2146 (setq count
(+ count
1))
2147 (let ((ff (filesets-ingroup-collect-build-menu
2148 fs
(list this
) count
)))
2149 (if (= (length ff
) 1)
2153 ,@(filesets-get-menu-epilog master
':ingroup fsn
)))
2154 `([,nm
(filesets-file-open nil
',master
',fsn
)])))))))))
2156 (defun filesets-ingroup-collect (fs remdupl-flag master
)
2157 "Collect names of included files and build submenu."
2158 (filesets-ingroup-cache-put master nil
)
2159 (filesets-message 2 "Filesets: parsing %S" master
)
2160 (filesets-ingroup-collect-build-menu
2162 (filesets-ingroup-collect-files fs remdupl-flag master
)))
2164 (defun filesets-build-ingroup-submenu (lookup-name master
)
2165 "Build a :ingroup submenu for file MASTER."
2166 (if (file-readable-p master
)
2167 (let ((remdupl-flag (filesets-ingroup-get-remdupl-p master
)))
2168 (setq filesets-ingroup-files
(list master
))
2169 (filesets-ingroup-collect lookup-name remdupl-flag master
))
2170 (if filesets-be-docile-flag
2172 (message "Filesets: can't parse %s" master
)
2174 (filesets-error 'error
"Filesets: can't parse " master
))))
2176 (defun filesets-build-dir-submenu-now (level depth entry lookup-name dir patt fd
2177 &optional rebuild-flag
)
2178 "Helper function for `filesets-build-dir-submenu'."
2179 ;;(filesets-message 3 "Filesets: scanning %s" dir)
2182 (let* ((dir (file-name-as-directory dir
))
2183 (header `([,(concat "Tree: "
2187 (file-name-as-directory
2188 (file-name-nondirectory
2189 (directory-file-name dir
))))))
2190 ,(list (function filesets-open
)
2192 `(quote (,dir
,patt
))
2195 (dirlist (filesets-directory-files dir patt nil nil fd
))
2196 (subdirs (filesets-filter-dir-names dirlist
))
2200 (setq count
(+ count
1))
2201 (let* ((x (file-name-as-directory x
))
2203 (dd (filesets-build-dir-submenu-now
2204 (+ level
1) depth entry
2205 lookup-name xx patt fd
))
2206 (nm (concat (filesets-get-shortcut count
)
2210 `[,nm
,(list 'filesets-browse-dir xx
)])))
2212 (files (filesets-filter-dir-names dirlist t
))
2213 (filesmenu (mapcar (lambda (x)
2214 (setq count
(+ count
1))
2215 `[,(concat (filesets-get-shortcut count
)
2217 (filesets-file-open nil
2218 (quote ,(concat dir x
))
2219 (quote ,lookup-name
))])
2222 (filesets-wrap-submenu
2226 (filesets-get-menu-epilog `(,dir
,patt
) ':tree
2227 lookup-name rebuild-flag
)))
2230 (defun filesets-build-dir-submenu (entry lookup-name dir patt
)
2231 "Build a :tree submenu named LOOKUP-NAME with base directory DIR including
2232 all files matching PATT for filesets ENTRY."
2233 (let ((fd (filesets-entry-get-filter-dirs-flag entry
))
2234 (depth (or (filesets-entry-get-tree-max-level entry
)
2235 filesets-tree-max-level
)))
2236 (filesets-build-dir-submenu-now 0 depth entry lookup-name dir patt fd t
)))
2238 (defun filesets-build-submenu (count lookup-name entry
)
2239 "Build submenu for the fileset ENTRY named LOOKUP-NAME.
2240 Construct a shortcut from COUNT."
2241 (let ((lookup-name (or lookup-name
2242 (filesets-data-get-name entry
))))
2243 (message "Filesets: %s" lookup-name
)
2244 (let ((mode (filesets-entry-mode entry
))
2245 (filesets-verbosity (filesets-entry-get-verbosity entry
))
2246 (this-lookup-name (concat (filesets-get-shortcut count
)
2250 (let* ((file (filesets-entry-get-file entry
)))
2252 (filesets-file-open nil
',file
',lookup-name
)]))
2257 (let* ((files (filesets-get-filelist entry mode
'on-ls
))
2258 (dirpatt (filesets-entry-get-pattern entry
))
2259 (pattname (apply 'concat
(cons "Pattern: " dirpatt
)))
2261 ;;(filesets-message 3 "Filesets: scanning %S" pattname)
2263 ,(list (function filesets-open
) mode lookup-name
)]
2265 ,@(filesets-wrap-submenu
2268 (setq count
(+ count
1))
2269 `[,(concat (filesets-get-shortcut count
)
2270 (file-name-nondirectory x
))
2271 (filesets-file-open nil
',x
',lookup-name
)])
2273 ,@(filesets-get-menu-epilog lookup-name mode
2276 (let* ((master (filesets-entry-get-master entry
)))
2277 ;;(filesets-message 3 "Filesets: parsing %S" master)
2278 `([,(concat "Inclusion Group: "
2279 (file-name-nondirectory master
))
2280 (filesets-open ',mode
',master
',lookup-name
)]
2282 [,master
(filesets-file-open nil
',master
',lookup-name
)]
2284 ,@(filesets-wrap-submenu
2285 (filesets-build-ingroup-submenu lookup-name master
))
2286 ,@(filesets-get-menu-epilog master mode lookup-name t
))))
2288 (let* ((dirpatt (filesets-entry-get-tree entry
))
2290 (patt (cadr dirpatt
)))
2291 (filesets-build-dir-submenu entry lookup-name dir patt
)))
2293 (let ((files (filesets-get-filelist entry mode
'on-open-all
))
2295 `([,(concat "Files: " lookup-name
)
2296 (filesets-open ',mode
',lookup-name
)]
2298 ,@(filesets-wrap-submenu
2301 (setq count
(+ count
1))
2302 `[,(concat (filesets-get-shortcut count
)
2303 (file-name-nondirectory x
))
2304 (filesets-file-open nil
',x
',lookup-name
)])
2305 (filesets-conditional-sort
2307 (function file-name-nondirectory
))))
2308 ,@(filesets-get-menu-epilog lookup-name mode
2309 lookup-name t
)))))))))))
2311 (defun filesets-remove-from-ubl (&optional buffer
)
2312 "BUFFER or current buffer require update of the filesets menu."
2315 (if (member b filesets-updated-buffers
)
2316 (setq filesets-updated-buffers
2317 (delete b filesets-updated-buffers
)))))
2319 (defun filesets-build-menu-now (from-scratch-flag)
2320 "Update the filesets menu.
2321 Build all new if FROM-SCRATCH-FLAG is non-nil. (To really build from the
2322 bottom up, set `filesets-submenus' to nil, first.)"
2323 (when (or from-scratch-flag
2324 filesets-has-changed-flag
2325 (not filesets-menu-cache
))
2326 (setq filesets-menu-cache nil
)
2327 (setq filesets-has-changed-flag nil
)
2328 (setq filesets-updated-buffers nil
)
2329 (setq filesets-update-cache-file-flag t
)
2330 (cl-do ((data (filesets-conditional-sort filesets-data
(function car
))
2332 (count 1 (+ count
1)))
2334 (let* ((this (car data
))
2335 (name (filesets-data-get-name this
))
2336 (cached (lax-plist-get filesets-submenus name
))
2338 (filesets-build-submenu count name this
))))
2340 (setq filesets-submenus
2341 (lax-plist-put filesets-submenus name submenu
)))
2342 (unless (filesets-entry-get-dormant-flag this
)
2343 (setq filesets-menu-cache
2344 (append filesets-menu-cache
(list submenu
))))))
2345 (when filesets-cache-save-often-flag
2346 (filesets-menu-cache-file-save-maybe)))
2347 (let ((cb (current-buffer)))
2348 (when (not (member cb filesets-updated-buffers
))
2351 `(,filesets-menu-name
2353 ["Edit Filesets" filesets-edit
]
2354 ["Save Filesets" filesets-save-config
]
2355 ["Save Menu Cache" filesets-menu-cache-file-save
]
2356 ["Rebuild Menu" filesets-build-menu
]
2357 ["Customize" filesets-customize
]
2358 ["About" filesets-info
])
2359 ,(filesets-get-cmd-menu)
2361 ,@filesets-menu-cache
)
2362 filesets-menu-before
2363 filesets-menu-in-menu
)
2364 (setq filesets-updated-buffers
2365 (cons cb filesets-updated-buffers
))
2366 ;; This wipes out other messages in the echo area.
2368 ;;(message "Filesets updated: %s" cb)
2371 (defun filesets-build-menu-maybe ()
2372 "Update the filesets menu."
2374 (filesets-build-menu-now nil
))
2376 (defun filesets-build-menu ()
2377 "Force rebuild of the filesets menu."
2379 ;(setq filesets-submenus nil)
2380 (filesets-reset-fileset)
2381 (filesets-build-menu-now t
)
2382 (filesets-menu-cache-file-save-maybe))
2384 (defun filesets-rebuild-this-submenu (fileset)
2385 "Force rebuild of FILESET submenu."
2386 (filesets-reset-fileset fileset
)
2387 (filesets-build-menu-now t
))
2389 (defun filesets-menu-cache-file-save-maybe (&optional simply-do-it
)
2390 "Write filesets' cache file.
2391 If SIMPLY-DO-IT is non-nil, the cache file will be written no matter if
2392 fileset thinks this is necessary or not."
2393 (when (and (not (equal filesets-menu-cache-file
""))
2395 filesets-update-cache-file-flag
))
2396 (when (file-exists-p filesets-menu-cache-file
)
2397 (delete-file filesets-menu-cache-file
))
2398 ;;(message "Filesets: saving menu cache")
2400 (dolist (this filesets-menu-cache-contents
)
2401 (if (get this
'custom-type
)
2403 (insert (format "(setq-default %s '%S)" this
(eval this
)))
2404 (when filesets-menu-ensure-use-cached
2406 (insert (format "(setq %s (cons '%s %s))"
2407 'filesets-ignore-next-set-default
2409 'filesets-ignore-next-set-default
))))
2410 (insert (format "(setq %s '%S)" this
(eval this
))))
2412 (insert (format "(setq filesets-cache-version %S)" filesets-version
))
2414 (when filesets-cache-hostname-flag
2415 (insert (format "(setq filesets-cache-hostname %S)" (system-name)))
2417 (run-hooks 'filesets-cache-fill-content-hooks
)
2418 (write-file filesets-menu-cache-file
))
2419 (setq filesets-has-changed-flag nil
)
2420 (setq filesets-update-cache-file-flag nil
)))
2422 (defun filesets-menu-cache-file-save ()
2423 "Save filesets' menu cache file."
2425 (filesets-menu-cache-file-save-maybe t
))
2427 (defun filesets-update-cleanup ()
2428 "Rebuild the menu and save the cache file after updating user data."
2430 (message "Filesets v%s: updating menu & cache from version %s"
2431 filesets-version
(or filesets-cache-version
"???"))
2432 (filesets-build-menu)
2433 (filesets-menu-cache-file-save-maybe)
2434 (filesets-menu-cache-file-load))
2436 (defun filesets-update-pre010505 ()
2438 "Filesets: manual editing of user data required!
2440 Filesets has detected that you were using an older version before,
2441 which requires some manual updating. Type 'y' for editing the startup
2444 The layout of `filesets-data' has changed. Please delete your cache file
2445 and edit your startup file as shown below:
2447 1. `filesets-data': Edit all :pattern filesets in your startup file and
2448 transform all entries as shown in this example:
2450 \(\"Test\" (:pattern \"~/dir/^pattern$\"))
2451 --> \(\"Test\" (:pattern \"~/dir/\" \"^pattern$\"))
2453 2. `filesets-data': Change all occurrences of \":document\" to \":ingroup\":
2455 \(\(\"Test\" \(:document \"~/dir/file\"))
2456 --> \(\(\"Test\" \(:ingroup \"~/dir/file\"))
2458 3. `filesets-subdocument-patterns': If you already modified the variable
2459 previously called `filesets-subdocument-patterns', change its name to
2460 `filesets-ingroup-patterns'.
2462 4. `filesets-menu-cache-contents': If you already modified this
2463 variable, change the entry `filesets-subdocument--cache' to
2464 `filesets-ingroup-cache'.
2466 5. Type M-x filesets-update-cleanup and restart Emacs.
2468 We apologize for the inconvenience."))
2469 (let* ((cf (or custom-file user-init-file
)))
2470 (switch-to-buffer-other-frame "*Filesets update*")
2472 (when (y-or-n-p (format "Edit startup (%s) file now? " cf
))
2473 (find-file-other-window cf
))
2474 (filesets-error 'error msg
))))
2476 (defun filesets-update (cached-version)
2477 "Do some cleanup after updating filesets.el."
2479 ((or (not cached-version
)
2480 (string< cached-version
"1.5.5")
2481 (boundp 'filesets-subdocument-patterns
))
2482 (filesets-update-pre010505)))
2483 (filesets-update-cleanup))
2485 (defun filesets-menu-cache-file-load ()
2486 "Load filesets' menu cache file."
2488 ((and (not (equal filesets-menu-cache-file
""))
2489 (file-readable-p filesets-menu-cache-file
))
2490 (load-file filesets-menu-cache-file
)
2491 (if (and (equal filesets-cache-version filesets-version
)
2492 (if filesets-cache-hostname-flag
2493 (equal filesets-cache-hostname
(system-name))
2496 (setq filesets-update-cache-file-flag nil
)
2498 (filesets-update filesets-cache-version
)))
2500 (setq filesets-update-cache-file-flag t
)
2503 (defun filesets-exit ()
2504 (filesets-menu-cache-file-save-maybe))
2507 (defun filesets-init ()
2508 "Filesets initialization.
2509 Set up hooks, load the cache file -- if existing -- and build the menu."
2510 (add-hook (if (featurep 'xemacs
) 'activate-menubar-hook
'menu-bar-update-hook
)
2511 (function filesets-build-menu-maybe
))
2512 (add-hook 'kill-buffer-hook
(function filesets-remove-from-ubl
))
2513 (add-hook 'first-change-hook
(function filesets-reset-filename-on-change
))
2514 (add-hook 'kill-emacs-hook
(function filesets-exit
))
2515 (if (filesets-menu-cache-file-load)
2517 (filesets-build-menu-maybe)
2518 ;;Well, normally when we use XEmacs <= 21.4, custom.el is loaded
2519 ;;after init.el. This more or less ignores the next
2520 ;;`filesets-data-set-default'
2521 (if filesets-menu-ensure-use-cached
2522 (setq filesets-menu-use-cached-flag t
)))
2523 (filesets-build-menu)))
2529 ;; sentence-end-double-space:t
2532 ;;; filesets.el ends here