*** empty log message ***
[bpt/emacs.git] / lisp / filesets.el
index 489a103..afeae05 100644 (file)
@@ -1,6 +1,6 @@
 ;;; filesets.el --- handle group of files
 
-;; Copyright (C) 2002 Free Software Foundation, Inc.
+;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Thomas Link <t.link@gmx.at>
 ;; Maintainer: FSF
@@ -10,7 +10,7 @@
 
 ;; This program is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; This program is distributed in the hope that it will be useful,
@@ -19,8 +19,8 @@
 ;; GNU General Public License for more details.
 
 ;; A copy of the GNU General Public License can be obtained from this
-;; program's author or from the Free Software Foundation, Inc., 675 Mass
-;; Ave, Cambridge, MA 02139, USA.
+;; program's author or from the Free Software Foundation, Inc.,
+;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
 (defvar filesets-version "1.8.4")
 (defvar filesets-homepage
@@ -132,7 +132,8 @@ Is buffer local variable.")
 
 (defvar filesets-menu-ensure-use-cached
   (and filesets-running-xemacs
-       (not (emacs-version>= 21 5)))
+       (if (fboundp 'emacs-version>=)
+          (not (emacs-version>= 21 5))))
   "Make sure (X)Emacs uses filesets' cache.
 
 Well, if you use XEmacs (prior to 21.5?) custom.el is loaded after
@@ -161,7 +162,7 @@ COND-FN takes one argument: the current element."
        (setq rv (append rv (list elt)))))))
 
 (defun filesets-ormap (fsom-pred lst)
-  "Return the tail of FSOM-LST for the head of which FSOM-PRED is non-nil."
+  "Return the tail of LST for the head of which FSOM-PRED is non-nil."
   (let ((fsom-lst lst)
        (fsom-rv nil))
     (while (and (not (null fsom-lst))
@@ -250,8 +251,15 @@ key is supported."
 ;  (customize-set-variable var val))
 ;  (filesets-build-menu))
 
+;; It seems this is a workaround for the XEmacs issue described in the
+;; doc-string of filesets-menu-ensure-use-cached. Under Emacs this is
+;; essentially just `set-default'.
 (defun filesets-set-default (sym val &optional init-flag)
-  "Set-default wrapper function used in conjunction with `defcustom'."
+  "Set-default wrapper function used in conjunction with `defcustom'.
+If SYM is in the list `filesets-ignore-next-set-default', delete
+it from that list, and return nil.  Otherwise, set the value of
+SYM to VAL and return t.  If INIT-FLAG is non-nil, set with
+`custom-initialize-set', otherwise with `set-default'."
   (let ((ignore-flag (member sym filesets-ignore-next-set-default)))
     (if ignore-flag
        (setq filesets-ignore-next-set-default
@@ -295,7 +303,8 @@ key is supported."
 (defgroup filesets nil
   "The fileset swapper."
   :prefix "filesets-"
-  :group 'convenience)
+  :group 'convenience
+  :version "22.1")
 
 (defcustom filesets-menu-name "Filesets"
   "*Filesets' menu name."
@@ -303,31 +312,26 @@ key is supported."
   :type 'sexp
   :group 'filesets)
 
-(if filesets-running-xemacs
-    (progn
-      (defcustom filesets-menu-path nil
-       "*The menu under which the filesets menu should be inserted.
-XEmacs specific; see `add-submenu' for documentation."
-       :set (function filesets-set-default)
-       :type 'sexp
-       :group 'filesets)
-
-      (defcustom filesets-menu-before "File"
-       "*The name of a menu before which this menu should be added.
-XEmacs specific; see `add-submenu' for documentation."
-       :set (function filesets-set-default)
-       :type 'sexp
-       :group 'filesets)
-
-      (defcustom filesets-menu-in-menu nil
-       "*Use that instead of `current-menubar' as the menu to change.
-XEmacs specific; see `add-submenu' for documentation."
-       :set (function filesets-set-default)
-       :type 'sexp
-       :group 'filesets))
-  (defvar filesets-menu-path nil)
-  (defvar filesets-menu-before nil)
-  (defvar filesets-menu-in-menu nil))
+(defcustom filesets-menu-path nil
+  "*The menu under which the filesets menu should be inserted.
+See `add-submenu' for documentation."
+  :set (function filesets-set-default)
+  :type 'sexp
+  :group 'filesets)
+
+(defcustom filesets-menu-before "File"
+  "*The name of a menu before which this menu should be added.
+See `add-submenu' for documentation."
+  :set (function filesets-set-default)
+  :type 'sexp
+  :group 'filesets)
+
+(defcustom filesets-menu-in-menu nil
+  "*Use that instead of `current-menubar' as the menu to change.
+See `add-submenu' for documentation."
+  :set (function filesets-set-default)
+  :type 'sexp
+  :group 'filesets)
 
 (defcustom filesets-menu-shortcuts-flag t
   "*Non-nil means to prepend menus with hopefully unique shortcuts."
@@ -350,7 +354,7 @@ XEmacs specific; see `add-submenu' for documentation."
 (defcustom filesets-menu-cache-file
   (if filesets-running-xemacs
       "~/.xemacs/filesets-cache.el"
-      "~/.filesets-cache.el")
+      "~/.emacs.d/filesets-cache.el")
   "*File to be used for saving the filesets menu between sessions.
 Set this to \"\", to disable caching of menus.
 Don't forget to check out `filesets-menu-ensure-use-cached'."
@@ -535,7 +539,7 @@ computer environments."
   :group 'filesets)
 
 (defcustom filesets-sort-case-sensitive-flag t
-  "*Non-nil means sorting of the filesete menu is case sensitive."
+  "*Non-nil means sorting of the filesets menu is case sensitive."
   :set (function filesets-set-default)
   :type 'boolean
   :group 'filesets)
@@ -646,8 +650,8 @@ the filename."
 Has the form ((FILE-PATTERN VIEWER PROPERTIES) ...), VIEWER being either a
 function or a command name as string.
 
-Properties is an association list determining filesets' behaviour in
-several conditions. Choose one from this list:
+Properties is an association list determining filesets' behavior in
+several conditions.  Choose one from this list:
 
 :ignore-on-open-all ... Don't open files of this type automatically --
 i.e. on open-all-files-events or when running commands
@@ -1069,9 +1073,7 @@ defined in `filesets-ingroup-patterns'."
 ;;; Emacs compatibility
 (eval-and-compile
   (if filesets-running-xemacs
-      (progn
-       (fset 'filesets-error 'error)
-       (fset 'filesets-add-submenu 'add-submenu))
+      (fset 'filesets-error 'error)
 
     (require 'easymenu)
 
@@ -1079,12 +1081,6 @@ defined in `filesets-ingroup-patterns'."
       "`error' wrapper."
       (error (mapconcat 'identity args " ")))
 
-    ;; This should work for 21.1 Emacs
-    (defun filesets-add-submenu (menu-path submenu &optional
-                                          before in-menu)
-      "`easy-menu-define' wrapper."
-      (easy-menu-define
-       filesets-submenu global-map "Filesets menu" submenu))
     ))
 
 (defun filesets-filter-dir-names (lst &optional negative)
@@ -1100,7 +1096,6 @@ non-nil, remove all directory names."
 (defun filesets-conditional-sort (lst &optional access-fn simply-do-it)
   "Return a sorted copy of LST, LST being a list of strings.
 If `filesets-sort-menu-flag' is nil, return LST itself.
-
 ACCESS-FN ... function to get the string value of LST's elements.
 
 If SIMPLY-DO-IT is non-nil, the list is sorted regardless of
@@ -1355,7 +1350,7 @@ Use the viewer defined in EV-ENTRY (a valid element of
                  (run-hooks 'oh))
                (set-buffer-modified-p nil)
                (setq buffer-read-only t)
-               (beginning-of-buffer))
+               (goto-char (point-min)))
            (when oh
              (run-hooks 'oh))))
       (filesets-error 'error
@@ -1372,7 +1367,7 @@ not be opened."
     (find-file file)))
 
 (defun filesets-find-or-display-file (&optional file viewer)
-  "Visit FILE using an external viewer or open it in an Emacs buffer."
+  "Visit FILE using an external VIEWER or open it in an Emacs buffer."
   (interactive)
   (let* ((file (or file
                   (read-file-name "Find file: " nil nil viewer)))
@@ -1423,24 +1418,24 @@ not be opened."
     something)))
 
 (defun filesets-data-get-name (entry)
-  "Access to `filesets-data'.  Get the entry's name"
+  "Access to `filesets-data'.  Get the ENTRY's name."
   (car entry))
 
 (defun filesets-data-get-data (entry)
-  "Access to `filesets-data'.  Get the entry's data section"
+  "Access to `filesets-data'.  Get the ENTRY's data section."
   (cdr entry))
 
 (defun filesets-alist-get (alist key &optional default carp)
   "Get KEY's value in the association list ALIST.
 Return DEFAULT if not found.  Return (car VALUE) if CARP is non-nil."
-  (let* ((elt (assoc key alist)))
+  (let ((elt (assoc key alist)))
     (cond
-     (elt
-      (if carp
-         (cadr elt)
-       (cdr elt)))
-     (default default)
-     (t nil))))
+      (elt
+       (if carp
+          (cadr elt)
+        (cdr elt)))
+      (default default)
+      (t nil))))
 
 (defun filesets-data-get (entry key &optional default carp)
   "Extract the value for KEY in the data part of fileset ENTRY.
@@ -1448,7 +1443,7 @@ Return DEFAULT if not found.  Return (car VALUE) if CARP is non-nil."
   (filesets-alist-get (filesets-data-get-data entry) key default carp))
 
 (defun filesets-data-set (entry key value)
-  "Set the value for KEY in the data part of fileset ENTRY."
+  "Set the VALUE for KEY in the data part of fileset ENTRY."
   (let* ((alist (filesets-data-get-data entry))
         (elt (assoc key alist)))
     (if elt
@@ -1592,7 +1587,8 @@ SAVE-FUNCTION takes no argument, but works on the current buffer."
 (defun filesets-cmd-show-result (cmd output)
   "Show OUTPUT of CMD (a shell command)."
   (pop-to-buffer "*Filesets: Shell Command Output*")
-  (end-of-buffer)
+  (with-no-warnings
+   (end-of-buffer))
   (insert "*** ")
   (insert cmd)
   (newline)
@@ -1606,7 +1602,7 @@ Replace <file-name> or <<file-name>> with filename."
                      ((equal arg "<file-name>")
                       (buffer-file-name))
                      ((equal arg "<<file-name>>")
-                      (filesets-quote (buffer-file-name)))
+                      (shell-quote-argument (buffer-file-name)))
                      (t
                       arg))))
 
@@ -1637,7 +1633,7 @@ Replace <file-name> or <<file-name>> with filename."
                (save-restriction
                  (let ((buffer (filesets-find-file this)))
                    (when buffer
-                     (beginning-of-buffer)
+                     (goto-char (point-min))
                      (let ()
                        (cond
                         ((stringp fn)
@@ -1674,9 +1670,9 @@ Replace <file-name> or <<file-name>> with filename."
               filesets-commands)))
 
 
-;;; sampe commands
+;;; sample commands
 (defun filesets-cmd-query-replace-getargs ()
-  "Get arguments for `filesets-cmd-query-replace'."
+  "Get arguments for `query-replace' and `query-replace-regexp'."
   (let* ((from-string (read-string "Filesets query replace: "
                                   ""
                                   'query-replace-history))
@@ -1704,7 +1700,7 @@ Replace <file-name> or <<file-name>> with filename."
                  ok)
              t)))
     (when ok
-      (let ((cmd (format txt (buffer-file-name))))
+      (let ((cmd (format txt (shell-quote-argument (buffer-file-name)))))
        (message "Filesets: %s" cmd)
        (filesets-cmd-show-result cmd
                                  (shell-command-to-string cmd))))))
@@ -1798,8 +1794,17 @@ User will be queried, if no fileset name is provided."
         (name   (or name
                     (completing-read
                      (format "Add '%s' to fileset: " buffer)
-                     filesets-data nil t)))
-        (entry  (assoc name filesets-data)))
+                     filesets-data nil)))
+         (entry  (or (assoc name filesets-data)
+                     (when (y-or-n-p
+                            (format "Fileset %s does not exist. Create it? "
+                                    name))
+                       (progn
+      (add-to-list 'filesets-data (list name '(:files)))
+      (message
+       "Fileset %s created.  Call `M-x filesets-save-config' to save."
+       name)
+      (car filesets-data))))))
     (if entry
        (let* ((files  (filesets-entry-get-files entry))
               (this   (buffer-file-name buffer))
@@ -1874,7 +1879,7 @@ User will be queried, if no fileset name is provided."
   (browse-url filesets-homepage))
 
 (defun filesets-remake-shortcut (count submenu)
-  "Remake a submenus shortcut when wrapping long menus."
+  "Remake a submenu's shortcut when wrapping long menus."
   (let* ((name (concat (filesets-get-shortcut count)
                       (substring (elt submenu 0) 2))))
     (if (listp submenu)
@@ -2031,7 +2036,7 @@ LOOKUP-NAME is used as lookup name for retrieving fileset specific settings."
          (lax-plist-put filesets-ingroup-cache emaster this))))
 
 (defun filesets-ingroup-collect-files (fs &optional remdupl-flag master depth)
-  "Helper function for `filesets-ingroup-collect'. Collect file names."
+  "Helper function for `filesets-ingroup-collect'.  Collect file names."
   (let* ((master       (or master
                           (filesets-entry-get-master fs)))
         (remdupl-flag (or remdupl-flag
@@ -2145,7 +2150,7 @@ FS is a fileset's name. FLIST is a list returned by
                          `([,nm (filesets-file-open nil ',master ',fsn)])))))))))
 
 (defun filesets-ingroup-collect (fs remdupl-flag master &optional depth)
-  "Collect names of included files & build submenu."
+  "Collect names of included files and build submenu."
   (filesets-ingroup-cache-put master nil)
   (filesets-message 2 "Filesets: parsing %S" master)
   (filesets-ingroup-collect-build-menu
@@ -2337,7 +2342,7 @@ bottom up, set `filesets-submenus' to nil, first.)"
       (filesets-menu-cache-file-save-maybe)))
   (let ((cb (current-buffer)))
     (when (not (member cb filesets-updated-buffers))
-      (filesets-add-submenu
+      (add-submenu
        filesets-menu-path
        `(,filesets-menu-name
         ("# Filesets"
@@ -2494,6 +2499,7 @@ We apologize for the inconvenience."))
 (defun filesets-exit ()
   (filesets-menu-cache-file-save-maybe))
 
+;;;###autoload
 (defun filesets-init ()
   "Filesets initialization.
 Set up hooks, load the cache file -- if existing -- and build the menu."
@@ -2519,4 +2525,5 @@ Set up hooks, load the cache file -- if existing -- and build the menu."
 ;;; sentence-end-double-space:t
 ;;; End:
 
+;;; arch-tag: 2c03f85f-c3df-4cec-b0a3-b46fd5592d70
 ;;; filesets.el ends here