-;; recentf.el --- setup a menu of recently opened files\r
-\r
-;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.\r
-\r
-;; Author: David Ponce <david@dponce.com>\r
-;; Created: July 19 1999\r
-;; Keywords: customization\r
-\r
-;; This file is part of GNU Emacs.\r
-\r
-;; GNU Emacs is free software; you can redistribute it and/or modify\r
-;; it under the terms of the GNU General Public License as published by\r
-;; the Free Software Foundation; either version 2, or (at your option)\r
-;; any later version.\r
-\r
-;; GNU Emacs is distributed in the hope that it will be useful,\r
-;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-;; GNU General Public License for more details.\r
-\r
-;; You should have received a copy of the GNU General Public License\r
-;; along with GNU Emacs; see the file COPYING. If not, write to the\r
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
-;; Boston, MA 02111-1307, USA.\r
-\r
-;;; Commentary:\r
-\r
-;; This package maintains a menu for visiting files that were operated\r
-;; on recently. When enabled a new "Open Recent" submenu is displayed\r
-;; in the "Files" menu. The recent files list is automatically saved\r
-;; across Emacs sessions. You can customize the number of recent\r
-;; files displayed, the location of the menu and others options (see\r
-;; the source code for details). To install and use, put the file on\r
-;; your Emacs-Lisp load path and add the following into your ~/.emacs\r
-;; startup file:\r
-;;\r
-;; (require 'recentf)\r
-;; (recentf-mode 1) \r
-\r
-;;; Code:\r
-\r
-(require 'easymenu)\r
-(require 'wid-edit)\r
-\r
-(defconst recentf-save-file-header\r
- ";;; Automatically generated by `recentf' on %s.\n"\r
- "Header to be written into the `recentf-save-file'.")\r
-\r
-(defvar recentf-list nil\r
- "List of recently opened files.")\r
-\r
-(defvar recentf-update-menu-p t\r
- "Non-nil if the recentf menu must be updated.")\r
-\r
-(defvar recentf-initialized-p nil\r
- "Non-nil if recentf already initialized.")\r
-\r
-;; IMPORTANT: This function must be defined before the following defcustoms\r
-;; because it is used in their :set clause. To avoid byte-compiler warnings\r
-;; the `symbol-value' function is used to access the `recentf-menu-path'\r
-;; and `recentf-menu-title' values.\r
-(defun recentf-menu-customization-changed (sym val)\r
- "Function called when menu customization has changed.\r
-It removes the recentf menu and forces its complete redrawing."\r
- (when recentf-initialized-p\r
- (easy-menu-remove-item nil \r
- (symbol-value 'recentf-menu-path)\r
- (symbol-value 'recentf-menu-title))\r
- (setq recentf-update-menu-p t))\r
- (custom-set-default sym val))\r
-\r
-(defgroup recentf nil\r
- "Maintain a menu of recently opened files."\r
- :version "21.1"\r
- :group 'files)\r
-\r
-(defgroup recentf-filters nil\r
- "Group to customize recentf menu filters.\r
-You should define the options of your own filters in this group."\r
- :group 'recentf)\r
-\r
-(defcustom recentf-max-saved-items 20\r
- "*Maximum number of items saved to `recentf-save-file'."\r
- :group 'recentf\r
- :type 'integer)\r
-\r
-(defcustom recentf-save-file (expand-file-name "~/.recentf")\r
- "*File to save `recentf-list' into."\r
- :group 'recentf\r
- :type 'file)\r
-\r
-(defcustom recentf-exclude nil\r
- "*List of regexps for filenames excluded from `recentf-list'."\r
- :group 'recentf\r
- :type '(repeat regexp))\r
-\r
-(defcustom recentf-menu-title "Open Recent"\r
- "*Name of the recentf menu."\r
- :group 'recentf\r
- :type 'string\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-menu-path '("files")\r
- "*Path where to add the recentf menu.\r
-If nil add it at top-level (see also `easy-menu-change')."\r
- :group 'recentf\r
- :type '(choice (const :tag "Top Level" nil)\r
- (sexp :tag "Menu Path"))\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-menu-before "open-file"\r
- "*Name of the menu before which the recentf menu will be added.\r
-If nil add it at end of menu (see also `easy-menu-change')."\r
- :group 'recentf\r
- :type '(choice (string :tag "Name")\r
- (const :tag "Last" nil))\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-menu-action 'recentf-find-file\r
- "*Function to invoke with a filename item of the recentf menu.\r
-The default action `recentf-find-file' calls `find-file' to edit an\r
-existing file. If the file does not exist or is not readable, it is\r
-not edited and its name is removed from `recentf-list'. You can use\r
-`find-file' instead to open non-existing files and keep them in the\r
-list of recently opened files."\r
- :group 'recentf\r
- :type 'function\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-max-menu-items 10\r
- "*Maximum number of items in the recentf menu."\r
- :group 'recentf\r
- :type 'integer\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-menu-filter nil\r
- "*Function used to filter files displayed in the recentf menu.\r
-Nil means no filter. The following functions are predefined:\r
-\r
-- - `recentf-sort-ascending' to sort menu items in ascending order.\r
-- - `recentf-sort-descending' to sort menu items in descending order.\r
-- - `recentf-sort-basenames-ascending' to sort file names in descending order.\r
-- - `recentf-sort-basenames-descending' to sort file names in descending order.\r
-- - `recentf-sort-directories-ascending' to sort directories in ascending order.\r
-- - `recentf-sort-directories-descending' to sort directories in descending order.\r
-- - `recentf-show-basenames' to show file names (no directories) in menu items.\r
-- - `recentf-show-basenames-ascending' to show file names in ascending order.\r
-- - `recentf-show-basenames-descending' to show file names in descending order.\r
-- - `recentf-relative-filter' to show file names relative to `default-directory'.\r
-- - `recentf-arrange-by-rule' to show sub-menus following user defined rules.\r
-- - `recentf-arrange-by-mode' to show a sub-menu for each major mode.\r
-- - `recentf-arrange-by-dir' to show a sub-menu for each directory.\r
-- - `recentf-filter-changer' to manage a ring of filters.\r
-\r
-The filter function is called with one argument, the list of menu elements\r
-used to build the menu and must return a new list of menu elements (see\r
-`recentf-make-menu-element' for menu element form)."\r
- :group 'recentf\r
- :type 'function\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-menu-append-commands-p t\r
- "*If not-nil command items are appended to the menu."\r
- :group 'recentf\r
- :type 'boolean\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-keep-non-readable-files-p nil\r
- "*If nil (default), non-readable files are not kept in `recentf-list'."\r
- :group 'recentf\r
- :type 'boolean\r
- :require 'recentf\r
- :initialize 'custom-initialize-default\r
- :set (lambda (sym val)\r
- (if val\r
- (remove-hook 'kill-buffer-hook 'recentf-remove-file-hook)\r
- (add-hook 'kill-buffer-hook 'recentf-remove-file-hook))\r
- (custom-set-default sym val)))\r
-\r
-(defcustom recentf-mode nil\r
- "Toggle recentf mode.\r
-When recentf mode is enabled, it maintains a menu for visiting files that\r
-were operated on recently.\r
-Setting this variable directly does not take effect;\r
-use either \\[customize] or the function `recentf-mode'."\r
- :set (lambda (symbol value)\r
- (recentf-mode (or value 0)))\r
- :initialize 'custom-initialize-default\r
- :type 'boolean\r
- :group 'recentf\r
- :require 'recentf)\r
-\r
-(defcustom recentf-load-hook nil\r
- "*Normal hook run at end of loading the `recentf' package."\r
- :group 'recentf\r
- :type 'hook)\r
-\r
-;;;\r
-;;; Common functions\r
-;;;\r
-(defconst recentf-case-fold-search\r
- (memq system-type '(vax-vms windows-nt))\r
- "Non-nil if recentf searches and matches should ignore case.")\r
-\r
-(defun recentf-include-p (filename)\r
- "Return t if FILENAME matches none of the `recentf-exclude' regexps."\r
- (let ((case-fold-search recentf-case-fold-search)\r
- (rl recentf-exclude))\r
- (while (and rl (not (string-match (car rl) filename)))\r
- (setq rl (cdr rl)))\r
- (null rl)))\r
-\r
-(defun recentf-add-file (filename)\r
- "Add or move FILENAME at the beginning of `recentf-list'.\r
-Does nothing if FILENAME matches one of the `recentf-exclude' regexps."\r
- (let ((filename (expand-file-name filename)))\r
- (when (recentf-include-p filename)\r
- (setq recentf-list (cons filename (delete filename recentf-list)))\r
- (setq recentf-update-menu-p t))))\r
-\r
-(defun recentf-remove-if-non-readable (filename)\r
- "Remove FILENAME from `recentf-list' if not readable."\r
- (unless (file-readable-p filename)\r
- (setq recentf-list (delete filename recentf-list))\r
- (setq recentf-update-menu-p t)))\r
-\r
-(defun recentf-find-file (filename)\r
- "Edit file FILENAME using `find-file'.\r
-If FILENAME is not readable it is removed from `recentf-list'."\r
- (if (file-readable-p filename)\r
- (find-file filename)\r
- (progn\r
- (message "File `%s' not found." filename)\r
- (setq recentf-list (delete filename recentf-list))\r
- (setq recentf-update-menu-p t))))\r
-\r
-(defun recentf-trunc-list (l n)\r
- "Return a list of the first N elements of L."\r
- (let ((lh nil))\r
- (while (and l (> n 0))\r
- (setq lh (cons (car l) lh))\r
- (setq n (1- n))\r
- (setq l (cdr l)))\r
- (nreverse lh)))\r
-\r
-(defun recentf-elements (n)\r
- "Return a list of the first N elements of `recentf-list'."\r
- (recentf-trunc-list recentf-list n))\r
-\r
-(defun recentf-make-menu-element (menu-item menu-value)\r
- "Create a new menu-element.\r
-\r
-A menu element is a pair (MENU-ITEM . MENU-VALUE) where:\r
-\r
-- - MENU-ITEM is the menu item string displayed.\r
-- - MENU-VALUE is the path used to open the file when the\r
- corresponding MENU-ITEM is selected. Or it is\r
- a pair (SUB-MENU-TITLE . MENU-ELEMENTS) where\r
- SUB-MENU-TITLE is a sub-menu title and\r
- MENU-ELEMENTS is the list of menu elements in\r
- the sub-menu."\r
- (cons menu-item menu-value))\r
-\r
-(defun recentf-menu-element-item (e)\r
- "Return the item part of the menu-element E."\r
- (car e))\r
-\r
-(defun recentf-menu-element-value (e)\r
- "Return the value part of the menu-element E."\r
- (cdr e))\r
-\r
-(defun recentf-set-menu-element-item (e item)\r
- "Change the item part of menu-element E to ITEM."\r
- (setcar e item))\r
-\r
-(defun recentf-set-menu-element-value (e value)\r
- "Change the value part of menu-element E to VALUE."\r
- (setcdr e value))\r
-\r
-(defun recentf-sub-menu-element-p (e)\r
- "Return non-nil if menu-element E defines a sub-menu."\r
- (consp (recentf-menu-element-value e)))\r
-\r
-(defun recentf-make-default-menu-element (file-path)\r
- "Make a new default menu element (MENU-ITEM . MENU-VALUE) for the\r
-given recent file path FILE-PATH. MENU-ITEM and MENU-VALUE are set to\r
-FILE-PATH. See also `recentf-make-menu-element'."\r
- (recentf-make-menu-element file-path file-path))\r
-\r
-(defun recentf-menu-elements (n)\r
- "Return a list of the first N default menu elements from\r
-`recentf-list'. See also `recentf-make-default-menu-element'."\r
- (mapcar 'recentf-make-default-menu-element\r
- (recentf-elements n)))\r
-\r
-(defun recentf-apply-menu-filter (filter l)\r
- "Convenient funtion to apply the function FILTER to the list of\r
-menu-elements L. It takes care of sub-menu elements in L and\r
-recursively apply FILTER to them. It is guarantee than FILTER receives\r
-only a list of single menu-elements (no sub-menu)."\r
- (if (and (functionp filter) l)\r
- (let ((case-fold-search recentf-case-fold-search)\r
- menu-element sub-menu-elements single-elements)\r
- ;; split L in two sub-listes:\r
- ;; one of sub-menus elements and\r
- ;; one of single menu elements\r
- (while l\r
- (setq menu-element (car l))\r
- (if (recentf-sub-menu-element-p menu-element)\r
- (setq sub-menu-elements\r
- (cons menu-element sub-menu-elements))\r
- (setq single-elements\r
- (cons menu-element single-elements)))\r
- (setq l (cdr l)))\r
- ;; apply FILTER to the list of single menu elements\r
- (if single-elements\r
- (setq single-elements (funcall filter\r
- (nreverse single-elements))))\r
- ;; apply FILTER to sub-menu menu element list\r
- (setq l sub-menu-elements)\r
- (setq sub-menu-elements nil)\r
- (while l\r
- (setq menu-element (car l))\r
- (recentf-set-menu-element-value\r
- menu-element\r
- (recentf-apply-menu-filter\r
- filter\r
- (recentf-menu-element-value menu-element)))\r
- (setq sub-menu-elements (cons menu-element sub-menu-elements))\r
- (setq l (cdr l)))\r
- ;; build and return the new filtered menu element list\r
- (nconc sub-menu-elements single-elements))\r
- l))\r
-\r
-(defvar recentf-menu-items-for-commands\r
- (list ["Cleanup list" recentf-cleanup t]\r
- ["Edit list..." recentf-edit-list t]\r
- ["Save list now" recentf-save-list t]\r
- (vector "Recentf Options..." '(customize-group "recentf") t))\r
- "List of menu items for recentf commands.")\r
-\r
-(defvar recentf-menu-filter-commands nil\r
- "This variable can be used by menu filters to setup their own command menu.\r
-\r
-If non-nil it must contain a list of valid menu-items to be appended\r
-to the recent file list part of the menu. Before calling a menu\r
-filter function this variable is reset to nil.")\r
-\r
-(defun recentf-make-menu-items ()\r
- "Make menu items from `recentf-list'."\r
- (setq recentf-menu-filter-commands nil)\r
- (let ((file-items\r
- (mapcar 'recentf-make-menu-item\r
- (recentf-apply-menu-filter\r
- recentf-menu-filter\r
- (recentf-menu-elements recentf-max-menu-items)))))\r
- (append (or file-items (list ["No files" t nil]))\r
- (and (< recentf-max-menu-items (length recentf-list))\r
- (list ["More..." recentf-open-more-files t]))\r
- (and recentf-menu-filter-commands\r
- (cons "---"\r
- recentf-menu-filter-commands))\r
- (and recentf-menu-append-commands-p\r
- (cons "---"\r
- recentf-menu-items-for-commands)))))\r
-\r
-(defun recentf-make-menu-item (menu-element)\r
- "Make a menu item from a menu element (see `recentf-make-menu-element')."\r
- (let ((menu-item (recentf-menu-element-item menu-element))\r
- (menu-value (recentf-menu-element-value menu-element)))\r
- (if (recentf-sub-menu-element-p menu-element)\r
- (cons menu-item (mapcar 'recentf-make-menu-item menu-value))\r
- (vector menu-item\r
- (list recentf-menu-action menu-value)\r
- t))))\r
-\r
-;;;\r
-;;; Predefined menu filter functions\r
-;;;\r
-\r
-(defun recentf-sort-ascending (l)\r
- "Sort the list of menu elements L in ascending order.\r
-The MENU-ITEM part of each menu element is compared."\r
- (sort (copy-sequence l)\r
- (function\r
- (lambda (e1 e2)\r
- (string-lessp (recentf-menu-element-item e1)\r
- (recentf-menu-element-item e2))))))\r
-\r
-(defun recentf-sort-descending (l)\r
- "Sort the list of menu elements L in descending order.\r
-The MENU-ITEM part of each menu element is compared."\r
- (sort (copy-sequence l)\r
- (function\r
- (lambda (e1 e2)\r
- (string-lessp (recentf-menu-element-item e2)\r
- (recentf-menu-element-item e1))))))\r
-\r
-(defun recentf-sort-basenames-ascending (l)\r
- "Sort the list of menu elements L in ascending order.\r
-Only file names (without directories) are compared."\r
- (sort (copy-sequence l)\r
- (function\r
- (lambda (e1 e2)\r
- (string-lessp\r
- (file-name-nondirectory (recentf-menu-element-value e1))\r
- (file-name-nondirectory (recentf-menu-element-value e2)))))))\r
-\r
-(defun recentf-sort-basenames-descending (l)\r
- "Sort the list of menu elements L in descending order.\r
-Only file names (without directories) are compared."\r
- (sort (copy-sequence l)\r
- (function\r
- (lambda (e1 e2)\r
- (string-lessp\r
- (file-name-nondirectory (recentf-menu-element-value e2))\r
- (file-name-nondirectory (recentf-menu-element-value e1)))))))\r
-\r
-(defun recentf-directory-compare (p1 p2)\r
- "Compare directories then filenames in pathes P1 and P2 and return\r
-non-nil if P1 is less than P2."\r
- (let ((d1 (file-name-directory p1))\r
- (f1 (file-name-nondirectory p1))\r
- (d2 (file-name-directory p2))\r
- (f2 (file-name-nondirectory p2)))\r
- (if (string= d1 d2)\r
- (string-lessp f1 f2)\r
- (string-lessp d1 d2))))\r
-\r
-(defun recentf-sort-directories-ascending (l)\r
- "Sort the list of menu elements L in ascending order.\r
-Compares directories then filenames to order the list."\r
- (sort (copy-sequence l)\r
- (function\r
- (lambda (e1 e2)\r
- (recentf-directory-compare (recentf-menu-element-value e1)\r
- (recentf-menu-element-value e2))))))\r
-\r
-(defun recentf-sort-directories-descending (l)\r
- "Sort the list of menu elements L in descending order.\r
-Compares directories then filenames to order the list."\r
- (sort (copy-sequence l)\r
- (function\r
- (lambda (e1 e2)\r
- (recentf-directory-compare (recentf-menu-element-value e2)\r
- (recentf-menu-element-value e1))))))\r
-\r
-(defun recentf-show-basenames (l)\r
- "Filter the list of menu elements L to show only file names (no directories)\r
-in the menu. When file names are duplicated their directory component is added."\r
- (let ((names (mapcar (function\r
- (lambda (item)\r
- (file-name-nondirectory\r
- (recentf-menu-element-value item))))\r
- l))\r
- (dirs (mapcar (function\r
- (lambda (item)\r
- (file-name-directory\r
- (recentf-menu-element-value item))))\r
- l))\r
- (pathes (mapcar 'recentf-menu-element-value l))\r
- (pos -1)\r
- item filtered-items filtered-list)\r
- (while names\r
- (setq item (car names))\r
- (setq names (cdr names))\r
- (setq pos (1+ pos))\r
- (setq filtered-list\r
- (cons (recentf-make-menu-element\r
- (if (or (member item names) (member item filtered-items))\r
- (concat item " (" (nth pos dirs) ")")\r
- item)\r
- (nth pos pathes))\r
- filtered-list))\r
- (setq filtered-items (cons item filtered-items)))\r
- (nreverse filtered-list)))\r
-\r
-(defun recentf-show-basenames-ascending (l)\r
- "Filter the list of menu elements L to show only file names in the menu,\r
-sorted in ascending order. This filter combines the `recentf-sort-basenames-ascending'\r
-and `recentf-show-basenames' filters."\r
- (recentf-show-basenames (recentf-sort-basenames-ascending l)))\r
-\r
-(defun recentf-show-basenames-descending (l)\r
- "Filter the list of menu elements L to show only file names in the menu,\r
-sorted in descending order. This filter combines the `recentf-sort-basenames-descending'\r
-and `recentf-show-basenames' filters."\r
- (recentf-show-basenames (recentf-sort-basenames-descending l)))\r
-\r
-(defun recentf-relative-filter (l)\r
- "Filter the list of `recentf-menu-elements' L to show filenames\r
-relative to `default-directory'."\r
- (setq recentf-update-menu-p t) ; force menu update\r
- (mapcar (function\r
- (lambda (menu-element)\r
- (let* ((ful-path (recentf-menu-element-value menu-element))\r
- (rel-path (file-relative-name ful-path)))\r
- (if (string-match "^\\.\\." rel-path)\r
- menu-element\r
- (recentf-make-menu-element rel-path ful-path)))))\r
- l))\r
-\r
-(defcustom recentf-arrange-rules\r
- '(\r
- ("Elisp files (%d)" ".\\.el$")\r
- ("Java files (%d)" ".\\.java$")\r
- ("C/C++ files (%d)" "c\\(pp\\)?$")\r
- )\r
- "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.\r
-A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the\r
-displayed title of the sub-menu where a '%d' `format' pattern is\r
-replaced by the number of items in the sub-menu. MATCHER is a regexp\r
-or a list of regexps. Items matching one of the regular expressions in\r
-MATCHER are added to the corresponding sub-menu."\r
- :group 'recentf-filters\r
- :type '(repeat (cons string (repeat regexp)))\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-arrange-by-rule-others "Other files (%d)"\r
- "*Title of the `recentf-arrange-by-rule' sub-menu where items that\r
-don't match any `recentf-arrange-rules' are displayed. If nil\r
-these items are displayed in the main recent files menu. A '%d'\r
-`format' pattern in the title is replaced by the number of items in\r
-the sub-menu."\r
- :group 'recentf-filters\r
- :type '(choice (const :tag "Main menu" nil)\r
- (string :tag "Title"))\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-arrange-by-rules-min-items 0\r
- "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.\r
-If the number of items in a sub-menu is less than this value the\r
-corresponding sub-menu items are displayed in the main recent files\r
-menu or in the `recentf-arrange-by-rule-others' sub-menu if\r
-defined."\r
- :group 'recentf-filters\r
- :type 'number\r
- :set 'recentf-menu-customization-changed)\r
-\r
-(defcustom recentf-arrange-by-rule-subfilter nil\r
- "*Function used by `recentf-arrange-by-rule' to filter sub-menu elements.\r
-Nil means no filter. See also `recentf-menu-filter'. You can't use\r
-`recentf-arrange-by-rule' itself here!"\r
- :group 'recentf-filters\r
- :type 'function\r
- :set (lambda (sym val)\r
- (if (eq val 'recentf-arrange-by-rule)\r
- (error "Can't use `recentf-arrange-by-rule' itself here!")\r
- (recentf-menu-customization-changed sym val))))\r
-\r
-(defun recentf-match-rule-p (matcher file-path)\r
- "Return non-nil if FILE-PATH match the rule specified by MATCHER.\r
-See `recentf-arrange-rules' for details on MATCHER."\r
- (if (stringp matcher)\r
- (string-match matcher file-path)\r
- (while (and (consp matcher)\r
- (not (string-match (car matcher) file-path)))\r
- (setq matcher (cdr matcher)))\r
- matcher))\r
-\r
-(defun recentf-arrange-by-rule (l)\r
- "Filter the list of menu-elements L to arrange them in sub-menus\r
-following rules in `recentf-arrange-rules'."\r
- (let ((sub-menus-number (length recentf-arrange-rules)))\r
- (if (> sub-menus-number 0)\r
- (let ((sub-menus (apply 'vector\r
- (mapcar (function\r
- (lambda (pair)\r
- (list (car pair))))\r
- recentf-arrange-rules)))\r
- other-menu-elements index min-size)\r
- (while l\r
- (let* ((menu-element (car l))\r
- (file-path (recentf-menu-element-value menu-element))\r
- (rules recentf-arrange-rules)\r
- (found nil))\r
- (setq index 0)\r
- (while (and (not found) rules)\r
- (if (recentf-match-rule-p (cdar rules) file-path)\r
- (let ((sub-menu (aref sub-menus index)))\r
- (setq found t)\r
- (recentf-set-menu-element-value\r
- sub-menu\r
- (cons menu-element (recentf-menu-element-value sub-menu)))\r
- ))\r
- (setq index (1+ index))\r
- (setq rules (cdr rules)))\r
- (or found\r
- (setq other-menu-elements\r
- (cons menu-element other-menu-elements)))\r
- (setq l (cdr l))))\r
- (setq index 0)\r
- (setq l nil)\r
- (setq min-size (if (integerp recentf-arrange-by-rules-min-items)\r
- (max 0 recentf-arrange-by-rules-min-items)\r
- 0))\r
- (while (< index sub-menus-number)\r
- (let* ((sub-menu (aref sub-menus index))\r
- (sub-menu-title (recentf-menu-element-item sub-menu))\r
- (sub-menu-elements (recentf-menu-element-value sub-menu))\r
- (sub-menu-length (length sub-menu-elements)))\r
- (if (> sub-menu-length 0)\r
- (cond\r
- ((< sub-menu-length min-size)\r
- (setq other-menu-elements\r
- (nconc sub-menu-elements other-menu-elements)))\r
- ((>= sub-menu-length min-size)\r
- (recentf-set-menu-element-item\r
- sub-menu\r
- (format sub-menu-title sub-menu-length))\r
- (recentf-set-menu-element-value\r
- sub-menu\r
- (recentf-apply-menu-filter\r
- recentf-arrange-by-rule-subfilter\r
- (nreverse sub-menu-elements)))\r
- (setq l (cons sub-menu l)))))\r
- (setq index (1+ index))))\r
- (if (and (stringp recentf-arrange-by-rule-others)\r
- other-menu-elements)\r
- (setq l\r
- (nreverse\r
- (cons (recentf-make-menu-element\r
- (format recentf-arrange-by-rule-others\r
- (length other-menu-elements))\r
- (recentf-apply-menu-filter\r
- recentf-arrange-by-rule-subfilter\r
- (nreverse other-menu-elements)))\r
- l)))\r
- (setq l (nconc (nreverse l)\r
- (recentf-apply-menu-filter\r
- recentf-arrange-by-rule-subfilter\r
- (nreverse other-menu-elements)))))))\r
- l))\r
-\r
-(defun recentf-build-mode-rules ()\r
- "Convert `auto-mode-alist' to `recentf-arrange-rules' format."\r
- (let ((case-fold-search recentf-case-fold-search)\r
- (modes auto-mode-alist)\r
- regexp mode rule-name rule rules)\r
- (while modes\r
- (setq regexp (caar modes))\r
- (setq mode (cdar modes))\r
- (when (symbolp mode)\r
- (setq rule-name (symbol-name mode))\r
- (if (string-match "\\(.*\\)-mode$" rule-name)\r
- (setq rule-name (match-string 1 rule-name)))\r
- (setq rule-name (concat rule-name " (%d)"))\r
- (setq rule (assoc rule-name rules))\r
- (if rule\r
- (setcdr rule (cons regexp (cdr rule)))\r
- (setq rules (cons (list rule-name regexp) rules))))\r
- (setq modes (cdr modes)))\r
- ;; It is important to preserve auto-mode-alist order\r
- ;; to ensure the right file <-> mode association\r
- (nreverse rules)))\r
- \r
-(defun recentf-arrange-by-mode (l)\r
- "Filter the list of menu-elements L to build sub-menus for each\r
-major mode."\r
- (let ((recentf-arrange-rules (recentf-build-mode-rules))\r
- (recentf-arrange-by-rule-others "others (%d)"))\r
- (recentf-arrange-by-rule l)))\r
-\r
-(defun recentf-build-dir-rules (l)\r
- "Convert directories in the list of menu-elements L to rules in\r
-`recentf-arrange-rules' format."\r
- (let (dirs)\r
- (mapcar (function\r
- (lambda (e)\r
- (let ((dir (file-name-directory\r
- (recentf-menu-element-value e))))\r
- (or (member dir dirs)\r
- (setq dirs (cons dir dirs))))))\r
- l)\r
- (mapcar (function\r
- (lambda (d)\r
- (cons (concat d " (%d)")\r
- (concat "\\`" d))))\r
- (nreverse (sort dirs 'string-lessp)))))\r
-\r
-(defun recentf-file-name-nondir (l)\r
- "Filter the list of menu-elements L to show only filenames. This\r
-simplified version of `recentf-show-basenames' do not handle\r
-duplicates. It is used by `recentf-arrange-by-dir' as its\r
-`recentf-arrange-by-rule-subfilter'."\r
- (mapcar (function\r
- (lambda (e)\r
- (recentf-make-menu-element\r
- (file-name-nondirectory (recentf-menu-element-value e))\r
- (recentf-menu-element-value e))))\r
- l))\r
-\r
-(defun recentf-arrange-by-dir (l)\r
- "Filter the list of menu-elements L to build sub-menus for each\r
-directory."\r
- (let ((recentf-arrange-rules (recentf-build-dir-rules l))\r
- (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)\r
- recentf-arrange-by-rule-others)\r
- (nreverse (recentf-arrange-by-rule l))))\r
-\r
-(defvar recentf-filter-changer-state nil\r
- "Used by `recentf-filter-changer' to hold its state.")\r
-\r
-(defcustom recentf-filter-changer-alist\r
- '(\r
- (recentf-arrange-by-mode . "*Files by Mode*")\r
- (recentf-arrange-by-dir . "*Files by Directory*")\r
- (recentf-arrange-by-rule . "*Files by User Rule*")\r
- )\r
- "*List of filters managed by `recentf-filter-changer'.\r
-Each filter is defined by a pair (FILTER-FUN . FILTER-LBL) where:\r
-\r
-- - FILTER-FUN is the function that filters menu-elements\r
-- - FILTER-LBL is the menu item used to activate the filter"\r
- :group 'recentf-filters\r
- :type '(repeat (cons function string))\r
- :set (lambda (sym val)\r
- (setq recentf-filter-changer-state nil)\r
- (recentf-menu-customization-changed sym val)))\r
-\r
-(defun recentf-filter-changer-goto-next ()\r
- "Go to the next filter available (see `recentf-filter-changer')."\r
- (and (consp recentf-filter-changer-state)\r
- (setq recentf-filter-changer-state\r
- (cdr recentf-filter-changer-state)))\r
- (setq recentf-update-menu-p t))\r
-\r
-(defun recentf-filter-changer-get-current ()\r
- "Get the current filter available (see `recentf-filter-changer')."\r
- (if (null recentf-filter-changer-state)\r
- (setq recentf-filter-changer-state recentf-filter-changer-alist))\r
- (and (consp recentf-filter-changer-state)\r
- (car recentf-filter-changer-state)))\r
-\r
-(defun recentf-filter-changer-get-next ()\r
- "Get the next filter available (see `recentf-filter-changer')."\r
- (let ((filters recentf-filter-changer-state))\r
- (cond ((consp filters)\r
- (setq filters (cdr filters))\r
- (if (null filters)\r
- (setq filters recentf-filter-changer-alist)))\r
- (t\r
- (setq filters recentf-filter-changer-alist)\r
- (if (consp filters)\r
- (setq filters (cdr filters)))))\r
- (if (consp filters)\r
- (car filters))))\r
- \r
-(defun recentf-filter-changer (l)\r
- "Manage a ring of filters. `recentf-filter-changer-alist' defines\r
-the filters in the ring. Actual filtering of L is delegated to the\r
-current filter in the ring. A filter menu item is displayed allowing\r
-to dynamically activate the next filter in the ring. If the filter\r
-ring is empty L is left unchanged."\r
- (let ((current-filter-item (recentf-filter-changer-get-current))\r
- (next-filter-item (recentf-filter-changer-get-next)))\r
- (when current-filter-item\r
- (setq l (recentf-apply-menu-filter (car current-filter-item) l))\r
- (if next-filter-item\r
- (setq recentf-menu-filter-commands\r
- (list (vector (cdr next-filter-item)\r
- '(recentf-filter-changer-goto-next)\r
- t)))))\r
- l))\r
-\r
-;;;\r
-;;; Dialogs stuff\r
-;;;\r
-\r
-(defun recentf-cancel-dialog (&rest ignore)\r
- "Cancel the current dialog. Used by `recentf-edit-list' and\r
-`recentf-open-files' dialogs."\r
- (interactive)\r
- (kill-buffer (current-buffer))\r
- (message "Dialog canceled."))\r
-\r
-(defvar recentf-dialog-mode-map nil\r
- "`recentf-dialog-mode' keymap.")\r
-\r
-(if recentf-dialog-mode-map\r
- ()\r
- (setq recentf-dialog-mode-map (make-sparse-keymap))\r
- (define-key recentf-dialog-mode-map "q" 'recentf-cancel-dialog)\r
- (set-keymap-parent recentf-dialog-mode-map widget-keymap))\r
-\r
-(defun recentf-dialog-mode ()\r
- "Major mode used in recentf dialogs.\r
-\r
-These are the special commands of recentf-dialog-mode mode:\r
- q -- cancel this dialog."\r
- (interactive)\r
- (setq major-mode 'recentf-dialog-mode)\r
- (setq mode-name "recentf-dialog")\r
- (use-local-map recentf-dialog-mode-map))\r
-\r
-;;;\r
-;;; Hooks and Commands\r
-;;;\r
-\r
-(defun recentf-add-file-hook ()\r
- "Insert the name of the file just opened or written into `recentf-list'."\r
- (and buffer-file-name (recentf-add-file buffer-file-name))\r
- nil)\r
-\r
-(defun recentf-remove-file-hook ()\r
- "When a buffer is killed remove a non readable file from `recentf-list'."\r
- (and buffer-file-name (recentf-remove-if-non-readable buffer-file-name))\r
- nil)\r
-\r
-(defun recentf-update-menu-hook ()\r
- "Update the recentf menu from the current `recentf-list'."\r
- (when recentf-update-menu-p\r
- (condition-case nil\r
- (progn\r
- (setq recentf-update-menu-p nil)\r
- (easy-menu-change recentf-menu-path\r
- recentf-menu-title\r
- (recentf-make-menu-items)\r
- recentf-menu-before))\r
- (error nil))))\r
-\r
-(defun recentf-dump-variable (variable &optional limit)\r
- "Insert a \"(setq VARIABLE value)\" in the current buffer. Optional\r
-argument LIMIT specifies a maximum length when VARIABLE value is a\r
-list (default to the full list)."\r
- (let ((value (symbol-value variable)))\r
- (insert (format "(setq %S\n '(\n" variable))\r
- (cond ((consp value)\r
- (if (and (integerp limit) (> limit 0))\r
- (setq value (recentf-trunc-list value limit)))\r
- (mapcar (function\r
- (lambda (e)\r
- (insert (format " %S\n" e))))\r
- value))\r
- (t\r
- (insert (format " %S\n" value))))\r
- (insert " ))\n")\r
- ))\r
-\r
-;;;###autoload\r
-(defun recentf-save-list ()\r
- "Save the current `recentf-list' to the file `recentf-save-file'."\r
- (interactive)\r
- (with-temp-buffer\r
- (erase-buffer)\r
- (insert (format recentf-save-file-header (current-time-string)))\r
- (recentf-dump-variable 'recentf-list recentf-max-saved-items)\r
- (recentf-dump-variable 'recentf-filter-changer-state)\r
- (if (file-writable-p recentf-save-file)\r
- (write-region (point-min) (point-max) recentf-save-file))\r
- (kill-buffer (current-buffer)))\r
- nil)\r
-\r
-(defvar recentf-edit-selected-items nil\r
- "Used by `recentf-edit-list' to hold the list of files to be deleted\r
-from `recentf-list'.")\r
-\r
-(defun recentf-edit-list-action (widget &rest ignore)\r
- "Checkbox widget action used by `recentf-edit-list' to select/unselect a file."\r
- (let ((value (widget-get widget ':tag)))\r
- ;; if value is already in the selected items\r
- (if (memq value recentf-edit-selected-items)\r
- ;; then remove it\r
- (progn\r
- (setq recentf-edit-selected-items\r
- (delq value recentf-edit-selected-items))\r
- (message "%s removed from selection." value))\r
- ;; else add it\r
- (progn\r
- (setq recentf-edit-selected-items\r
- (nconc (list value) recentf-edit-selected-items))\r
- (message "%s added to selection." value)))))\r
- \r
-;;;###autoload\r
-(defun recentf-edit-list ()\r
- "Allow the user to edit the files that are kept in the recent list."\r
- (interactive)\r
- (with-current-buffer (get-buffer-create (concat "*" recentf-menu-title " - Edit list*"))\r
- (switch-to-buffer (current-buffer))\r
- (kill-all-local-variables)\r
- (let ((inhibit-read-only t))\r
- (erase-buffer))\r
- (let ((all (overlay-lists)))\r
- ;; Delete all the overlays.\r
- (mapcar 'delete-overlay (car all))\r
- (mapcar 'delete-overlay (cdr all)))\r
- (setq recentf-edit-selected-items nil)\r
- ;; Insert the dialog header\r
- (widget-insert "Select the files to be deleted from the 'recentf-list'.\n\n")\r
- (widget-insert "Click on Ok to update the list. ")\r
- (widget-insert "Click on Cancel or type \"q\" to quit.\n")\r
- ;; Insert the list of files as checkboxes\r
- (mapcar (function\r
- (lambda (item)\r
- (widget-create 'checkbox\r
- :value nil ; unselected checkbox\r
- :format "\n %[%v%] %t"\r
- :tag item\r
- :notify 'recentf-edit-list-action)))\r
- recentf-list)\r
- (widget-insert "\n\n")\r
- ;; Insert the Ok button\r
- (widget-create 'push-button\r
- :notify (lambda (&rest ignore)\r
- (if recentf-edit-selected-items\r
- (progn (kill-buffer (current-buffer))\r
- (mapcar (function\r
- (lambda (item)\r
- (setq recentf-list\r
- (delq item recentf-list))))\r
- recentf-edit-selected-items)\r
- (message "%S file(s) removed from the list"\r
- (length recentf-edit-selected-items))\r
- (setq recentf-update-menu-p t))\r
- (message "No file selected.")))\r
- "Ok")\r
- (widget-insert " ")\r
- ;; Insert the Cancel button\r
- (widget-create 'push-button\r
- :notify 'recentf-cancel-dialog\r
- "Cancel")\r
- (recentf-dialog-mode)\r
- (widget-setup)\r
- (goto-char (point-min))))\r
-\r
-;;;###autoload\r
-(defun recentf-cleanup ()\r
- "Remove all non-readable and excluded files from `recentf-list'."\r
- (interactive)\r
- (let ((count (length recentf-list)))\r
- (setq recentf-list\r
- (delq nil\r
- (mapcar (function\r
- (lambda (filename)\r
- (and (file-readable-p filename)\r
- (recentf-include-p filename)\r
- filename)))\r
- recentf-list)))\r
- (setq count (- count (length recentf-list)))\r
- (message "%s removed from the list"\r
- (cond ((= count 0) "No file")\r
- ((= count 1) "One file")\r
- (t (format "%d files" count)))))\r
- (setq recentf-update-menu-p t))\r
-\r
-(defun recentf-open-files-action (widget &rest ignore)\r
- "Button widget action used by `recentf-open-files' to open a file."\r
- (kill-buffer (current-buffer))\r
- (funcall recentf-menu-action (widget-value widget)))\r
-\r
-(defvar recentf-open-files-item-shift ""\r
- "String used by `recentf-open-files' to shift right sub-menu\r
-items.")\r
-\r
-(defun recentf-open-files-item (menu-element)\r
- "Function called by `recentf-open-files' to insert a menu-element\r
-item in the current interaction buffer."\r
- (let ((menu-item (car menu-element))\r
- (file-path (cdr menu-element)))\r
- (if (consp file-path) ; This is a sub-menu\r
- (let* ((shift recentf-open-files-item-shift)\r
- (recentf-open-files-item-shift (concat shift " ")))\r
- (widget-create 'item\r
- :tag menu-item\r
- :sample-face 'bold\r
- :format (concat shift "%{%t%}:\n"))\r
- (mapcar 'recentf-open-files-item\r
- file-path)\r
- (widget-insert "\n"))\r
- (widget-create 'push-button\r
- :button-face 'default\r
- :tag menu-item\r
- :help-echo (concat "Open " file-path)\r
- :format (concat recentf-open-files-item-shift "%[%t%]")\r
- :notify 'recentf-open-files-action\r
- file-path)\r
- (widget-insert "\n"))))\r
-\r
-;;;###autoload\r
-(defun recentf-open-files (&optional files buffer-name)\r
- "Open a buffer that allows the user to choose a file to open from\r
-the list of recently opened files. The optional argument FILES may be\r
-used to specify the list, otherwise recentf-list is used. The optional\r
-argument BUFFER-NAME specifies which buffer to use for the interaction."\r
- (interactive)\r
- (if (null files)\r
- (setq files recentf-list))\r
- (if (null buffer-name)\r
- (setq buffer-name (concat "*" recentf-menu-title "*")))\r
- (with-current-buffer (get-buffer-create buffer-name)\r
- (switch-to-buffer (current-buffer))\r
- (kill-all-local-variables)\r
- (let ((inhibit-read-only t))\r
- (erase-buffer))\r
- (let ((all (overlay-lists)))\r
- ;; Delete all the overlays.\r
- (mapcar 'delete-overlay (car all))\r
- (mapcar 'delete-overlay (cdr all)))\r
- ;; Insert the dialog header\r
- (widget-insert "Click on a file to open it. ")\r
- (widget-insert "Click on Cancel or type \"q\" to quit.\n\n" )\r
- ;; Insert the list of files as buttons\r
- (let ((recentf-open-files-item-shift ""))\r
- (mapcar 'recentf-open-files-item\r
- (recentf-apply-menu-filter\r
- recentf-menu-filter\r
- (mapcar 'recentf-make-default-menu-element files))))\r
- (widget-insert "\n")\r
- ;; Insert the Cancel button\r
- (widget-create 'push-button\r
- :notify 'recentf-cancel-dialog\r
- "Cancel")\r
- (recentf-dialog-mode)\r
- (widget-setup)\r
- (goto-char (point-min))))\r
-\r
-;;;###autoload\r
-(defun recentf-open-more-files ()\r
- "Allow the user to open files that are not in the menu."\r
- (interactive)\r
- (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)\r
- (concat "*" recentf-menu-title " - More*")))\r
-\r
-;;;###autoload\r
-(defun recentf-mode (&optional arg)\r
- "Toggle recentf mode.\r
-With prefix ARG, turn recentf mode on if and only if ARG is positive.\r
-Returns the new status of recentf mode (non-nil means on).\r
-\r
-When recentf mode is enabled, it maintains a menu for visiting files that\r
-were operated on recently."\r
- (interactive "P")\r
- (let ((on-p (if arg\r
- (> (prefix-numeric-value arg) 0)\r
- (not recentf-mode))))\r
- (if on-p\r
- (unless recentf-initialized-p\r
- (setq recentf-initialized-p t)\r
- (if (file-readable-p recentf-save-file)\r
- (load-file recentf-save-file))\r
- (setq recentf-update-menu-p t)\r
- (add-hook 'find-file-hooks 'recentf-add-file-hook)\r
- (add-hook 'write-file-hooks 'recentf-add-file-hook)\r
- (add-hook 'menu-bar-update-hook 'recentf-update-menu-hook)\r
- (add-hook 'kill-emacs-hook 'recentf-save-list))\r
- (when recentf-initialized-p\r
- (setq recentf-initialized-p nil)\r
- (recentf-save-list)\r
- (easy-menu-remove-item nil recentf-menu-path recentf-menu-title)\r
- (remove-hook 'find-file-hooks 'recentf-add-file-hook)\r
- (remove-hook 'write-file-hooks 'recentf-add-file-hook)\r
- (remove-hook 'menu-bar-update-hook 'recentf-update-menu-hook)\r
- (remove-hook 'kill-emacs-hook 'recentf-save-list)))\r
- (setq recentf-mode on-p)))\r
-\r
-(provide 'recentf)\r
-\r
-(run-hooks 'recentf-load-hook)\r
-\r
-;;; recentf.el ends here.\r
+;; recentf.el --- setup a menu of recently opened files
+
+;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
+
+;; Author: David Ponce <david@dponce.com>
+;; Created: July 19 1999
+;; Keywords: customization
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs 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)
+;; any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING. If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
+
+;;; Commentary:
+
+;; This package maintains a menu for visiting files that were operated
+;; on recently. When enabled a new "Open Recent" submenu is displayed
+;; in the "Files" menu. The recent files list is automatically saved
+;; across Emacs sessions. You can customize the number of recent
+;; files displayed, the location of the menu and others options (see
+;; the source code for details). To install and use, put the file on
+;; your Emacs-Lisp load path and add the following into your ~/.emacs
+;; startup file:
+;;
+;; (require 'recentf)
+;; (recentf-mode 1)
+
+;;; Code:
+
+(require 'easymenu)
+(require 'wid-edit)
+
+(defconst recentf-save-file-header
+ ";;; Automatically generated by `recentf' on %s.\n"
+ "Header to be written into the `recentf-save-file'.")
+
+(defvar recentf-list nil
+ "List of recently opened files.")
+
+(defvar recentf-update-menu-p t
+ "Non-nil if the recentf menu must be updated.")
+
+(defvar recentf-initialized-p nil
+ "Non-nil if recentf already initialized.")
+
+;; IMPORTANT: This function must be defined before the following defcustoms
+;; because it is used in their :set clause. To avoid byte-compiler warnings
+;; the `symbol-value' function is used to access the `recentf-menu-path'
+;; and `recentf-menu-title' values.
+(defun recentf-menu-customization-changed (sym val)
+ "Function called when menu customization has changed.
+It removes the recentf menu and forces its complete redrawing."
+ (when recentf-initialized-p
+ (easy-menu-remove-item nil
+ (symbol-value 'recentf-menu-path)
+ (symbol-value 'recentf-menu-title))
+ (setq recentf-update-menu-p t))
+ (custom-set-default sym val))
+
+(defgroup recentf nil
+ "Maintain a menu of recently opened files."
+ :version "21.1"
+ :group 'files)
+
+(defgroup recentf-filters nil
+ "Group to customize recentf menu filters.
+You should define the options of your own filters in this group."
+ :group 'recentf)
+
+(defcustom recentf-max-saved-items 20
+ "*Maximum number of items saved to `recentf-save-file'."
+ :group 'recentf
+ :type 'integer)
+
+(defcustom recentf-save-file (expand-file-name "~/.recentf")
+ "*File to save `recentf-list' into."
+ :group 'recentf
+ :type 'file)
+
+(defcustom recentf-exclude nil
+ "*List of regexps for filenames excluded from `recentf-list'."
+ :group 'recentf
+ :type '(repeat regexp))
+
+(defcustom recentf-menu-title "Open Recent"
+ "*Name of the recentf menu."
+ :group 'recentf
+ :type 'string
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-menu-path '("files")
+ "*Path where to add the recentf menu.
+If nil add it at top-level (see also `easy-menu-change')."
+ :group 'recentf
+ :type '(choice (const :tag "Top Level" nil)
+ (sexp :tag "Menu Path"))
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-menu-before "open-file"
+ "*Name of the menu before which the recentf menu will be added.
+If nil add it at end of menu (see also `easy-menu-change')."
+ :group 'recentf
+ :type '(choice (string :tag "Name")
+ (const :tag "Last" nil))
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-menu-action 'recentf-find-file
+ "*Function to invoke with a filename item of the recentf menu.
+The default action `recentf-find-file' calls `find-file' to edit an
+existing file. If the file does not exist or is not readable, it is
+not edited and its name is removed from `recentf-list'. You can use
+`find-file' instead to open non-existing files and keep them in the
+list of recently opened files."
+ :group 'recentf
+ :type 'function
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-max-menu-items 10
+ "*Maximum number of items in the recentf menu."
+ :group 'recentf
+ :type 'integer
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-menu-filter nil
+ "*Function used to filter files displayed in the recentf menu.
+Nil means no filter. The following functions are predefined:
+
+- - `recentf-sort-ascending' to sort menu items in ascending order.
+- - `recentf-sort-descending' to sort menu items in descending order.
+- - `recentf-sort-basenames-ascending' to sort file names in descending order.
+- - `recentf-sort-basenames-descending' to sort file names in descending order.
+- - `recentf-sort-directories-ascending' to sort directories in ascending order.
+- - `recentf-sort-directories-descending' to sort directories in descending order.
+- - `recentf-show-basenames' to show file names (no directories) in menu items.
+- - `recentf-show-basenames-ascending' to show file names in ascending order.
+- - `recentf-show-basenames-descending' to show file names in descending order.
+- - `recentf-relative-filter' to show file names relative to `default-directory'.
+- - `recentf-arrange-by-rule' to show sub-menus following user defined rules.
+- - `recentf-arrange-by-mode' to show a sub-menu for each major mode.
+- - `recentf-arrange-by-dir' to show a sub-menu for each directory.
+- - `recentf-filter-changer' to manage a ring of filters.
+
+The filter function is called with one argument, the list of menu elements
+used to build the menu and must return a new list of menu elements (see
+`recentf-make-menu-element' for menu element form)."
+ :group 'recentf
+ :type 'function
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-menu-append-commands-p t
+ "*If not-nil command items are appended to the menu."
+ :group 'recentf
+ :type 'boolean
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-keep-non-readable-files-p nil
+ "*If nil (default), non-readable files are not kept in `recentf-list'."
+ :group 'recentf
+ :type 'boolean
+ :require 'recentf
+ :initialize 'custom-initialize-default
+ :set (lambda (sym val)
+ (if val
+ (remove-hook 'kill-buffer-hook 'recentf-remove-file-hook)
+ (add-hook 'kill-buffer-hook 'recentf-remove-file-hook))
+ (custom-set-default sym val)))
+
+(defcustom recentf-mode nil
+ "Toggle recentf mode.
+When recentf mode is enabled, it maintains a menu for visiting files that
+were operated on recently.
+Setting this variable directly does not take effect;
+use either \\[customize] or the function `recentf-mode'."
+ :set (lambda (symbol value)
+ (recentf-mode (or value 0)))
+ :initialize 'custom-initialize-default
+ :type 'boolean
+ :group 'recentf
+ :require 'recentf)
+
+(defcustom recentf-load-hook nil
+ "*Normal hook run at end of loading the `recentf' package."
+ :group 'recentf
+ :type 'hook)
+
+;;;
+;;; Common functions
+;;;
+(defconst recentf-case-fold-search
+ (memq system-type '(vax-vms windows-nt))
+ "Non-nil if recentf searches and matches should ignore case.")
+
+(defun recentf-include-p (filename)
+ "Return t if FILENAME matches none of the `recentf-exclude' regexps."
+ (let ((case-fold-search recentf-case-fold-search)
+ (rl recentf-exclude))
+ (while (and rl (not (string-match (car rl) filename)))
+ (setq rl (cdr rl)))
+ (null rl)))
+
+(defun recentf-add-file (filename)
+ "Add or move FILENAME at the beginning of `recentf-list'.
+Does nothing if FILENAME matches one of the `recentf-exclude' regexps."
+ (let ((filename (expand-file-name filename)))
+ (when (recentf-include-p filename)
+ (setq recentf-list (cons filename (delete filename recentf-list)))
+ (setq recentf-update-menu-p t))))
+
+(defun recentf-remove-if-non-readable (filename)
+ "Remove FILENAME from `recentf-list' if not readable."
+ (unless (file-readable-p filename)
+ (setq recentf-list (delete filename recentf-list))
+ (setq recentf-update-menu-p t)))
+
+(defun recentf-find-file (filename)
+ "Edit file FILENAME using `find-file'.
+If FILENAME is not readable it is removed from `recentf-list'."
+ (if (file-readable-p filename)
+ (find-file filename)
+ (progn
+ (message "File `%s' not found." filename)
+ (setq recentf-list (delete filename recentf-list))
+ (setq recentf-update-menu-p t))))
+
+(defun recentf-trunc-list (l n)
+ "Return a list of the first N elements of L."
+ (let ((lh nil))
+ (while (and l (> n 0))
+ (setq lh (cons (car l) lh))
+ (setq n (1- n))
+ (setq l (cdr l)))
+ (nreverse lh)))
+
+(defun recentf-elements (n)
+ "Return a list of the first N elements of `recentf-list'."
+ (recentf-trunc-list recentf-list n))
+
+(defun recentf-make-menu-element (menu-item menu-value)
+ "Create a new menu-element.
+
+A menu element is a pair (MENU-ITEM . MENU-VALUE) where:
+
+- - MENU-ITEM is the menu item string displayed.
+- - MENU-VALUE is the path used to open the file when the
+ corresponding MENU-ITEM is selected. Or it is
+ a pair (SUB-MENU-TITLE . MENU-ELEMENTS) where
+ SUB-MENU-TITLE is a sub-menu title and
+ MENU-ELEMENTS is the list of menu elements in
+ the sub-menu."
+ (cons menu-item menu-value))
+
+(defun recentf-menu-element-item (e)
+ "Return the item part of the menu-element E."
+ (car e))
+
+(defun recentf-menu-element-value (e)
+ "Return the value part of the menu-element E."
+ (cdr e))
+
+(defun recentf-set-menu-element-item (e item)
+ "Change the item part of menu-element E to ITEM."
+ (setcar e item))
+
+(defun recentf-set-menu-element-value (e value)
+ "Change the value part of menu-element E to VALUE."
+ (setcdr e value))
+
+(defun recentf-sub-menu-element-p (e)
+ "Return non-nil if menu-element E defines a sub-menu."
+ (consp (recentf-menu-element-value e)))
+
+(defun recentf-make-default-menu-element (file-path)
+ "Make a new default menu element (MENU-ITEM . MENU-VALUE) for the
+given recent file path FILE-PATH. MENU-ITEM and MENU-VALUE are set to
+FILE-PATH. See also `recentf-make-menu-element'."
+ (recentf-make-menu-element file-path file-path))
+
+(defun recentf-menu-elements (n)
+ "Return a list of the first N default menu elements from
+`recentf-list'. See also `recentf-make-default-menu-element'."
+ (mapcar 'recentf-make-default-menu-element
+ (recentf-elements n)))
+
+(defun recentf-apply-menu-filter (filter l)
+ "Convenient funtion to apply the function FILTER to the list of
+menu-elements L. It takes care of sub-menu elements in L and
+recursively apply FILTER to them. It is guarantee than FILTER receives
+only a list of single menu-elements (no sub-menu)."
+ (if (and (functionp filter) l)
+ (let ((case-fold-search recentf-case-fold-search)
+ menu-element sub-menu-elements single-elements)
+ ;; split L in two sub-listes:
+ ;; one of sub-menus elements and
+ ;; one of single menu elements
+ (while l
+ (setq menu-element (car l))
+ (if (recentf-sub-menu-element-p menu-element)
+ (setq sub-menu-elements
+ (cons menu-element sub-menu-elements))
+ (setq single-elements
+ (cons menu-element single-elements)))
+ (setq l (cdr l)))
+ ;; apply FILTER to the list of single menu elements
+ (if single-elements
+ (setq single-elements (funcall filter
+ (nreverse single-elements))))
+ ;; apply FILTER to sub-menu menu element list
+ (setq l sub-menu-elements)
+ (setq sub-menu-elements nil)
+ (while l
+ (setq menu-element (car l))
+ (recentf-set-menu-element-value
+ menu-element
+ (recentf-apply-menu-filter
+ filter
+ (recentf-menu-element-value menu-element)))
+ (setq sub-menu-elements (cons menu-element sub-menu-elements))
+ (setq l (cdr l)))
+ ;; build and return the new filtered menu element list
+ (nconc sub-menu-elements single-elements))
+ l))
+
+(defvar recentf-menu-items-for-commands
+ (list ["Cleanup list" recentf-cleanup t]
+ ["Edit list..." recentf-edit-list t]
+ ["Save list now" recentf-save-list t]
+ (vector "Recentf Options..." '(customize-group "recentf") t))
+ "List of menu items for recentf commands.")
+
+(defvar recentf-menu-filter-commands nil
+ "This variable can be used by menu filters to setup their own command menu.
+
+If non-nil it must contain a list of valid menu-items to be appended
+to the recent file list part of the menu. Before calling a menu
+filter function this variable is reset to nil.")
+
+(defun recentf-make-menu-items ()
+ "Make menu items from `recentf-list'."
+ (setq recentf-menu-filter-commands nil)
+ (let ((file-items
+ (mapcar 'recentf-make-menu-item
+ (recentf-apply-menu-filter
+ recentf-menu-filter
+ (recentf-menu-elements recentf-max-menu-items)))))
+ (append (or file-items (list ["No files" t nil]))
+ (and (< recentf-max-menu-items (length recentf-list))
+ (list ["More..." recentf-open-more-files t]))
+ (and recentf-menu-filter-commands
+ (cons "---"
+ recentf-menu-filter-commands))
+ (and recentf-menu-append-commands-p
+ (cons "---"
+ recentf-menu-items-for-commands)))))
+
+(defun recentf-make-menu-item (menu-element)
+ "Make a menu item from a menu element (see `recentf-make-menu-element')."
+ (let ((menu-item (recentf-menu-element-item menu-element))
+ (menu-value (recentf-menu-element-value menu-element)))
+ (if (recentf-sub-menu-element-p menu-element)
+ (cons menu-item (mapcar 'recentf-make-menu-item menu-value))
+ (vector menu-item
+ (list recentf-menu-action menu-value)
+ t))))
+
+;;;
+;;; Predefined menu filter functions
+;;;
+
+(defun recentf-sort-ascending (l)
+ "Sort the list of menu elements L in ascending order.
+The MENU-ITEM part of each menu element is compared."
+ (sort (copy-sequence l)
+ (function
+ (lambda (e1 e2)
+ (string-lessp (recentf-menu-element-item e1)
+ (recentf-menu-element-item e2))))))
+
+(defun recentf-sort-descending (l)
+ "Sort the list of menu elements L in descending order.
+The MENU-ITEM part of each menu element is compared."
+ (sort (copy-sequence l)
+ (function
+ (lambda (e1 e2)
+ (string-lessp (recentf-menu-element-item e2)
+ (recentf-menu-element-item e1))))))
+
+(defun recentf-sort-basenames-ascending (l)
+ "Sort the list of menu elements L in ascending order.
+Only file names (without directories) are compared."
+ (sort (copy-sequence l)
+ (function
+ (lambda (e1 e2)
+ (string-lessp
+ (file-name-nondirectory (recentf-menu-element-value e1))
+ (file-name-nondirectory (recentf-menu-element-value e2)))))))
+
+(defun recentf-sort-basenames-descending (l)
+ "Sort the list of menu elements L in descending order.
+Only file names (without directories) are compared."
+ (sort (copy-sequence l)
+ (function
+ (lambda (e1 e2)
+ (string-lessp
+ (file-name-nondirectory (recentf-menu-element-value e2))
+ (file-name-nondirectory (recentf-menu-element-value e1)))))))
+
+(defun recentf-directory-compare (p1 p2)
+ "Compare directories then filenames in pathes P1 and P2 and return
+non-nil if P1 is less than P2."
+ (let ((d1 (file-name-directory p1))
+ (f1 (file-name-nondirectory p1))
+ (d2 (file-name-directory p2))
+ (f2 (file-name-nondirectory p2)))
+ (if (string= d1 d2)
+ (string-lessp f1 f2)
+ (string-lessp d1 d2))))
+
+(defun recentf-sort-directories-ascending (l)
+ "Sort the list of menu elements L in ascending order.
+Compares directories then filenames to order the list."
+ (sort (copy-sequence l)
+ (function
+ (lambda (e1 e2)
+ (recentf-directory-compare (recentf-menu-element-value e1)
+ (recentf-menu-element-value e2))))))
+
+(defun recentf-sort-directories-descending (l)
+ "Sort the list of menu elements L in descending order.
+Compares directories then filenames to order the list."
+ (sort (copy-sequence l)
+ (function
+ (lambda (e1 e2)
+ (recentf-directory-compare (recentf-menu-element-value e2)
+ (recentf-menu-element-value e1))))))
+
+(defun recentf-show-basenames (l)
+ "Filter the list of menu elements L to show only file names (no directories)
+in the menu. When file names are duplicated their directory component is added."
+ (let ((names (mapcar (function
+ (lambda (item)
+ (file-name-nondirectory
+ (recentf-menu-element-value item))))
+ l))
+ (dirs (mapcar (function
+ (lambda (item)
+ (file-name-directory
+ (recentf-menu-element-value item))))
+ l))
+ (pathes (mapcar 'recentf-menu-element-value l))
+ (pos -1)
+ item filtered-items filtered-list)
+ (while names
+ (setq item (car names))
+ (setq names (cdr names))
+ (setq pos (1+ pos))
+ (setq filtered-list
+ (cons (recentf-make-menu-element
+ (if (or (member item names) (member item filtered-items))
+ (concat item " (" (nth pos dirs) ")")
+ item)
+ (nth pos pathes))
+ filtered-list))
+ (setq filtered-items (cons item filtered-items)))
+ (nreverse filtered-list)))
+
+(defun recentf-show-basenames-ascending (l)
+ "Filter the list of menu elements L to show only file names in the menu,
+sorted in ascending order. This filter combines the `recentf-sort-basenames-ascending'
+and `recentf-show-basenames' filters."
+ (recentf-show-basenames (recentf-sort-basenames-ascending l)))
+
+(defun recentf-show-basenames-descending (l)
+ "Filter the list of menu elements L to show only file names in the menu,
+sorted in descending order. This filter combines the `recentf-sort-basenames-descending'
+and `recentf-show-basenames' filters."
+ (recentf-show-basenames (recentf-sort-basenames-descending l)))
+
+(defun recentf-relative-filter (l)
+ "Filter the list of `recentf-menu-elements' L to show filenames
+relative to `default-directory'."
+ (setq recentf-update-menu-p t) ; force menu update
+ (mapcar (function
+ (lambda (menu-element)
+ (let* ((ful-path (recentf-menu-element-value menu-element))
+ (rel-path (file-relative-name ful-path)))
+ (if (string-match "^\\.\\." rel-path)
+ menu-element
+ (recentf-make-menu-element rel-path ful-path)))))
+ l))
+
+(defcustom recentf-arrange-rules
+ '(
+ ("Elisp files (%d)" ".\\.el$")
+ ("Java files (%d)" ".\\.java$")
+ ("C/C++ files (%d)" "c\\(pp\\)?$")
+ )
+ "*List of rules used by `recentf-arrange-by-rule' to build sub-menus.
+A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
+displayed title of the sub-menu where a '%d' `format' pattern is
+replaced by the number of items in the sub-menu. MATCHER is a regexp
+or a list of regexps. Items matching one of the regular expressions in
+MATCHER are added to the corresponding sub-menu."
+ :group 'recentf-filters
+ :type '(repeat (cons string (repeat regexp)))
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-arrange-by-rule-others "Other files (%d)"
+ "*Title of the `recentf-arrange-by-rule' sub-menu where items that
+don't match any `recentf-arrange-rules' are displayed. If nil
+these items are displayed in the main recent files menu. A '%d'
+`format' pattern in the title is replaced by the number of items in
+the sub-menu."
+ :group 'recentf-filters
+ :type '(choice (const :tag "Main menu" nil)
+ (string :tag "Title"))
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-arrange-by-rules-min-items 0
+ "*Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
+If the number of items in a sub-menu is less than this value the
+corresponding sub-menu items are displayed in the main recent files
+menu or in the `recentf-arrange-by-rule-others' sub-menu if
+defined."
+ :group 'recentf-filters
+ :type 'number
+ :set 'recentf-menu-customization-changed)
+
+(defcustom recentf-arrange-by-rule-subfilter nil
+ "*Function used by `recentf-arrange-by-rule' to filter sub-menu elements.
+Nil means no filter. See also `recentf-menu-filter'. You can't use
+`recentf-arrange-by-rule' itself here!"
+ :group 'recentf-filters
+ :type 'function
+ :set (lambda (sym val)
+ (if (eq val 'recentf-arrange-by-rule)
+ (error "Can't use `recentf-arrange-by-rule' itself here!")
+ (recentf-menu-customization-changed sym val))))
+
+(defun recentf-match-rule-p (matcher file-path)
+ "Return non-nil if FILE-PATH match the rule specified by MATCHER.
+See `recentf-arrange-rules' for details on MATCHER."
+ (if (stringp matcher)
+ (string-match matcher file-path)
+ (while (and (consp matcher)
+ (not (string-match (car matcher) file-path)))
+ (setq matcher (cdr matcher)))
+ matcher))
+
+(defun recentf-arrange-by-rule (l)
+ "Filter the list of menu-elements L to arrange them in sub-menus
+following rules in `recentf-arrange-rules'."
+ (let ((sub-menus-number (length recentf-arrange-rules)))
+ (if (> sub-menus-number 0)
+ (let ((sub-menus (apply 'vector
+ (mapcar (function
+ (lambda (pair)
+ (list (car pair))))
+ recentf-arrange-rules)))
+ other-menu-elements index min-size)
+ (while l
+ (let* ((menu-element (car l))
+ (file-path (recentf-menu-element-value menu-element))
+ (rules recentf-arrange-rules)
+ (found nil))
+ (setq index 0)
+ (while (and (not found) rules)
+ (if (recentf-match-rule-p (cdar rules) file-path)
+ (let ((sub-menu (aref sub-menus index)))
+ (setq found t)
+ (recentf-set-menu-element-value
+ sub-menu
+ (cons menu-element (recentf-menu-element-value sub-menu)))
+ ))
+ (setq index (1+ index))
+ (setq rules (cdr rules)))
+ (or found
+ (setq other-menu-elements
+ (cons menu-element other-menu-elements)))
+ (setq l (cdr l))))
+ (setq index 0)
+ (setq l nil)
+ (setq min-size (if (integerp recentf-arrange-by-rules-min-items)
+ (max 0 recentf-arrange-by-rules-min-items)
+ 0))
+ (while (< index sub-menus-number)
+ (let* ((sub-menu (aref sub-menus index))
+ (sub-menu-title (recentf-menu-element-item sub-menu))
+ (sub-menu-elements (recentf-menu-element-value sub-menu))
+ (sub-menu-length (length sub-menu-elements)))
+ (if (> sub-menu-length 0)
+ (cond
+ ((< sub-menu-length min-size)
+ (setq other-menu-elements
+ (nconc sub-menu-elements other-menu-elements)))
+ ((>= sub-menu-length min-size)
+ (recentf-set-menu-element-item
+ sub-menu
+ (format sub-menu-title sub-menu-length))
+ (recentf-set-menu-element-value
+ sub-menu
+ (recentf-apply-menu-filter
+ recentf-arrange-by-rule-subfilter
+ (nreverse sub-menu-elements)))
+ (setq l (cons sub-menu l)))))
+ (setq index (1+ index))))
+ (if (and (stringp recentf-arrange-by-rule-others)
+ other-menu-elements)
+ (setq l
+ (nreverse
+ (cons (recentf-make-menu-element
+ (format recentf-arrange-by-rule-others
+ (length other-menu-elements))
+ (recentf-apply-menu-filter
+ recentf-arrange-by-rule-subfilter
+ (nreverse other-menu-elements)))
+ l)))
+ (setq l (nconc (nreverse l)
+ (recentf-apply-menu-filter
+ recentf-arrange-by-rule-subfilter
+ (nreverse other-menu-elements)))))))
+ l))
+
+(defun recentf-build-mode-rules ()
+ "Convert `auto-mode-alist' to `recentf-arrange-rules' format."
+ (let ((case-fold-search recentf-case-fold-search)
+ (modes auto-mode-alist)
+ regexp mode rule-name rule rules)
+ (while modes
+ (setq regexp (caar modes))
+ (setq mode (cdar modes))
+ (when (symbolp mode)
+ (setq rule-name (symbol-name mode))
+ (if (string-match "\\(.*\\)-mode$" rule-name)
+ (setq rule-name (match-string 1 rule-name)))
+ (setq rule-name (concat rule-name " (%d)"))
+ (setq rule (assoc rule-name rules))
+ (if rule
+ (setcdr rule (cons regexp (cdr rule)))
+ (setq rules (cons (list rule-name regexp) rules))))
+ (setq modes (cdr modes)))
+ ;; It is important to preserve auto-mode-alist order
+ ;; to ensure the right file <-> mode association
+ (nreverse rules)))
+
+(defun recentf-arrange-by-mode (l)
+ "Filter the list of menu-elements L to build sub-menus for each
+major mode."
+ (let ((recentf-arrange-rules (recentf-build-mode-rules))
+ (recentf-arrange-by-rule-others "others (%d)"))
+ (recentf-arrange-by-rule l)))
+
+(defun recentf-build-dir-rules (l)
+ "Convert directories in the list of menu-elements L to rules in
+`recentf-arrange-rules' format."
+ (let (dirs)
+ (mapcar (function
+ (lambda (e)
+ (let ((dir (file-name-directory
+ (recentf-menu-element-value e))))
+ (or (member dir dirs)
+ (setq dirs (cons dir dirs))))))
+ l)
+ (mapcar (function
+ (lambda (d)
+ (cons (concat d " (%d)")
+ (concat "\\`" d))))
+ (nreverse (sort dirs 'string-lessp)))))
+
+(defun recentf-file-name-nondir (l)
+ "Filter the list of menu-elements L to show only filenames. This
+simplified version of `recentf-show-basenames' do not handle
+duplicates. It is used by `recentf-arrange-by-dir' as its
+`recentf-arrange-by-rule-subfilter'."
+ (mapcar (function
+ (lambda (e)
+ (recentf-make-menu-element
+ (file-name-nondirectory (recentf-menu-element-value e))
+ (recentf-menu-element-value e))))
+ l))
+
+(defun recentf-arrange-by-dir (l)
+ "Filter the list of menu-elements L to build sub-menus for each
+directory."
+ (let ((recentf-arrange-rules (recentf-build-dir-rules l))
+ (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
+ recentf-arrange-by-rule-others)
+ (nreverse (recentf-arrange-by-rule l))))
+
+(defvar recentf-filter-changer-state nil
+ "Used by `recentf-filter-changer' to hold its state.")
+
+(defcustom recentf-filter-changer-alist
+ '(
+ (recentf-arrange-by-mode . "*Files by Mode*")
+ (recentf-arrange-by-dir . "*Files by Directory*")
+ (recentf-arrange-by-rule . "*Files by User Rule*")
+ )
+ "*List of filters managed by `recentf-filter-changer'.
+Each filter is defined by a pair (FILTER-FUN . FILTER-LBL) where:
+
+- - FILTER-FUN is the function that filters menu-elements
+- - FILTER-LBL is the menu item used to activate the filter"
+ :group 'recentf-filters
+ :type '(repeat (cons function string))
+ :set (lambda (sym val)
+ (setq recentf-filter-changer-state nil)
+ (recentf-menu-customization-changed sym val)))
+
+(defun recentf-filter-changer-goto-next ()
+ "Go to the next filter available (see `recentf-filter-changer')."
+ (and (consp recentf-filter-changer-state)
+ (setq recentf-filter-changer-state
+ (cdr recentf-filter-changer-state)))
+ (setq recentf-update-menu-p t))
+
+(defun recentf-filter-changer-get-current ()
+ "Get the current filter available (see `recentf-filter-changer')."
+ (if (null recentf-filter-changer-state)
+ (setq recentf-filter-changer-state recentf-filter-changer-alist))
+ (and (consp recentf-filter-changer-state)
+ (car recentf-filter-changer-state)))
+
+(defun recentf-filter-changer-get-next ()
+ "Get the next filter available (see `recentf-filter-changer')."
+ (let ((filters recentf-filter-changer-state))
+ (cond ((consp filters)
+ (setq filters (cdr filters))
+ (if (null filters)
+ (setq filters recentf-filter-changer-alist)))
+ (t
+ (setq filters recentf-filter-changer-alist)
+ (if (consp filters)
+ (setq filters (cdr filters)))))
+ (if (consp filters)
+ (car filters))))
+
+(defun recentf-filter-changer (l)
+ "Manage a ring of filters. `recentf-filter-changer-alist' defines
+the filters in the ring. Actual filtering of L is delegated to the
+current filter in the ring. A filter menu item is displayed allowing
+to dynamically activate the next filter in the ring. If the filter
+ring is empty L is left unchanged."
+ (let ((current-filter-item (recentf-filter-changer-get-current))
+ (next-filter-item (recentf-filter-changer-get-next)))
+ (when current-filter-item
+ (setq l (recentf-apply-menu-filter (car current-filter-item) l))
+ (if next-filter-item
+ (setq recentf-menu-filter-commands
+ (list (vector (cdr next-filter-item)
+ '(recentf-filter-changer-goto-next)
+ t)))))
+ l))
+
+;;;
+;;; Dialogs stuff
+;;;
+
+(defun recentf-cancel-dialog (&rest ignore)
+ "Cancel the current dialog. Used by `recentf-edit-list' and
+`recentf-open-files' dialogs."
+ (interactive)
+ (kill-buffer (current-buffer))
+ (message "Dialog canceled."))
+
+(defvar recentf-dialog-mode-map nil
+ "`recentf-dialog-mode' keymap.")
+
+(if recentf-dialog-mode-map
+ ()
+ (setq recentf-dialog-mode-map (make-sparse-keymap))
+ (define-key recentf-dialog-mode-map "q" 'recentf-cancel-dialog)
+ (set-keymap-parent recentf-dialog-mode-map widget-keymap))
+
+(defun recentf-dialog-mode ()
+ "Major mode used in recentf dialogs.
+
+These are the special commands of recentf-dialog-mode mode:
+ q -- cancel this dialog."
+ (interactive)
+ (setq major-mode 'recentf-dialog-mode)
+ (setq mode-name "recentf-dialog")
+ (use-local-map recentf-dialog-mode-map))
+
+;;;
+;;; Hooks and Commands
+;;;
+
+(defun recentf-add-file-hook ()
+ "Insert the name of the file just opened or written into `recentf-list'."
+ (and buffer-file-name (recentf-add-file buffer-file-name))
+ nil)
+
+(defun recentf-remove-file-hook ()
+ "When a buffer is killed remove a non readable file from `recentf-list'."
+ (and buffer-file-name (recentf-remove-if-non-readable buffer-file-name))
+ nil)
+
+(defun recentf-update-menu-hook ()
+ "Update the recentf menu from the current `recentf-list'."
+ (when recentf-update-menu-p
+ (condition-case nil
+ (progn
+ (setq recentf-update-menu-p nil)
+ (easy-menu-change recentf-menu-path
+ recentf-menu-title
+ (recentf-make-menu-items)
+ recentf-menu-before))
+ (error nil))))
+
+(defun recentf-dump-variable (variable &optional limit)
+ "Insert a \"(setq VARIABLE value)\" in the current buffer. Optional
+argument LIMIT specifies a maximum length when VARIABLE value is a
+list (default to the full list)."
+ (let ((value (symbol-value variable)))
+ (insert (format "(setq %S\n '(\n" variable))
+ (cond ((consp value)
+ (if (and (integerp limit) (> limit 0))
+ (setq value (recentf-trunc-list value limit)))
+ (mapcar (function
+ (lambda (e)
+ (insert (format " %S\n" e))))
+ value))
+ (t
+ (insert (format " %S\n" value))))
+ (insert " ))\n")
+ ))
+
+;;;###autoload
+(defun recentf-save-list ()
+ "Save the current `recentf-list' to the file `recentf-save-file'."
+ (interactive)
+ (with-temp-buffer
+ (erase-buffer)
+ (insert (format recentf-save-file-header (current-time-string)))
+ (recentf-dump-variable 'recentf-list recentf-max-saved-items)
+ (recentf-dump-variable 'recentf-filter-changer-state)
+ (if (file-writable-p recentf-save-file)
+ (write-region (point-min) (point-max) recentf-save-file))
+ (kill-buffer (current-buffer)))
+ nil)
+
+(defvar recentf-edit-selected-items nil
+ "Used by `recentf-edit-list' to hold the list of files to be deleted
+from `recentf-list'.")
+
+(defun recentf-edit-list-action (widget &rest ignore)
+ "Checkbox widget action used by `recentf-edit-list' to select/unselect a file."
+ (let ((value (widget-get widget ':tag)))
+ ;; if value is already in the selected items
+ (if (memq value recentf-edit-selected-items)
+ ;; then remove it
+ (progn
+ (setq recentf-edit-selected-items
+ (delq value recentf-edit-selected-items))
+ (message "%s removed from selection." value))
+ ;; else add it
+ (progn
+ (setq recentf-edit-selected-items
+ (nconc (list value) recentf-edit-selected-items))
+ (message "%s added to selection." value)))))
+
+;;;###autoload
+(defun recentf-edit-list ()
+ "Allow the user to edit the files that are kept in the recent list."
+ (interactive)
+ (with-current-buffer (get-buffer-create (concat "*" recentf-menu-title " - Edit list*"))
+ (switch-to-buffer (current-buffer))
+ (kill-all-local-variables)
+ (let ((inhibit-read-only t))
+ (erase-buffer))
+ (let ((all (overlay-lists)))
+ ;; Delete all the overlays.
+ (mapcar 'delete-overlay (car all))
+ (mapcar 'delete-overlay (cdr all)))
+ (setq recentf-edit-selected-items nil)
+ ;; Insert the dialog header
+ (widget-insert "Select the files to be deleted from the 'recentf-list'.\n\n")
+ (widget-insert "Click on Ok to update the list. ")
+ (widget-insert "Click on Cancel or type \"q\" to quit.\n")
+ ;; Insert the list of files as checkboxes
+ (mapcar (function
+ (lambda (item)
+ (widget-create 'checkbox
+ :value nil ; unselected checkbox
+ :format "\n %[%v%] %t"
+ :tag item
+ :notify 'recentf-edit-list-action)))
+ recentf-list)
+ (widget-insert "\n\n")
+ ;; Insert the Ok button
+ (widget-create 'push-button
+ :notify (lambda (&rest ignore)
+ (if recentf-edit-selected-items
+ (progn (kill-buffer (current-buffer))
+ (mapcar (function
+ (lambda (item)
+ (setq recentf-list
+ (delq item recentf-list))))
+ recentf-edit-selected-items)
+ (message "%S file(s) removed from the list"
+ (length recentf-edit-selected-items))
+ (setq recentf-update-menu-p t))
+ (message "No file selected.")))
+ "Ok")
+ (widget-insert " ")
+ ;; Insert the Cancel button
+ (widget-create 'push-button
+ :notify 'recentf-cancel-dialog
+ "Cancel")
+ (recentf-dialog-mode)
+ (widget-setup)
+ (goto-char (point-min))))
+
+;;;###autoload
+(defun recentf-cleanup ()
+ "Remove all non-readable and excluded files from `recentf-list'."
+ (interactive)
+ (let ((count (length recentf-list)))
+ (setq recentf-list
+ (delq nil
+ (mapcar (function
+ (lambda (filename)
+ (and (file-readable-p filename)
+ (recentf-include-p filename)
+ filename)))
+ recentf-list)))
+ (setq count (- count (length recentf-list)))
+ (message "%s removed from the list"
+ (cond ((= count 0) "No file")
+ ((= count 1) "One file")
+ (t (format "%d files" count)))))
+ (setq recentf-update-menu-p t))
+
+(defun recentf-open-files-action (widget &rest ignore)
+ "Button widget action used by `recentf-open-files' to open a file."
+ (kill-buffer (current-buffer))
+ (funcall recentf-menu-action (widget-value widget)))
+
+(defvar recentf-open-files-item-shift ""
+ "String used by `recentf-open-files' to shift right sub-menu
+items.")
+
+(defun recentf-open-files-item (menu-element)
+ "Function called by `recentf-open-files' to insert a menu-element
+item in the current interaction buffer."
+ (let ((menu-item (car menu-element))
+ (file-path (cdr menu-element)))
+ (if (consp file-path) ; This is a sub-menu
+ (let* ((shift recentf-open-files-item-shift)
+ (recentf-open-files-item-shift (concat shift " ")))
+ (widget-create 'item
+ :tag menu-item
+ :sample-face 'bold
+ :format (concat shift "%{%t%}:\n"))
+ (mapcar 'recentf-open-files-item
+ file-path)
+ (widget-insert "\n"))
+ (widget-create 'push-button
+ :button-face 'default
+ :tag menu-item
+ :help-echo (concat "Open " file-path)
+ :format (concat recentf-open-files-item-shift "%[%t%]")
+ :notify 'recentf-open-files-action
+ file-path)
+ (widget-insert "\n"))))
+
+;;;###autoload
+(defun recentf-open-files (&optional files buffer-name)
+ "Open a buffer that allows the user to choose a file to open from
+the list of recently opened files. The optional argument FILES may be
+used to specify the list, otherwise recentf-list is used. The optional
+argument BUFFER-NAME specifies which buffer to use for the interaction."
+ (interactive)
+ (if (null files)
+ (setq files recentf-list))
+ (if (null buffer-name)
+ (setq buffer-name (concat "*" recentf-menu-title "*")))
+ (with-current-buffer (get-buffer-create buffer-name)
+ (switch-to-buffer (current-buffer))
+ (kill-all-local-variables)
+ (let ((inhibit-read-only t))
+ (erase-buffer))
+ (let ((all (overlay-lists)))
+ ;; Delete all the overlays.
+ (mapcar 'delete-overlay (car all))
+ (mapcar 'delete-overlay (cdr all)))
+ ;; Insert the dialog header
+ (widget-insert "Click on a file to open it. ")
+ (widget-insert "Click on Cancel or type \"q\" to quit.\n\n" )
+ ;; Insert the list of files as buttons
+ (let ((recentf-open-files-item-shift ""))
+ (mapcar 'recentf-open-files-item
+ (recentf-apply-menu-filter
+ recentf-menu-filter
+ (mapcar 'recentf-make-default-menu-element files))))
+ (widget-insert "\n")
+ ;; Insert the Cancel button
+ (widget-create 'push-button
+ :notify 'recentf-cancel-dialog
+ "Cancel")
+ (recentf-dialog-mode)
+ (widget-setup)
+ (goto-char (point-min))))
+
+;;;###autoload
+(defun recentf-open-more-files ()
+ "Allow the user to open files that are not in the menu."
+ (interactive)
+ (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
+ (concat "*" recentf-menu-title " - More*")))
+
+;;;###autoload
+(defun recentf-mode (&optional arg)
+ "Toggle recentf mode.
+With prefix ARG, turn recentf mode on if and only if ARG is positive.
+Returns the new status of recentf mode (non-nil means on).
+
+When recentf mode is enabled, it maintains a menu for visiting files that
+were operated on recently."
+ (interactive "P")
+ (let ((on-p (if arg
+ (> (prefix-numeric-value arg) 0)
+ (not recentf-mode))))
+ (if on-p
+ (unless recentf-initialized-p
+ (setq recentf-initialized-p t)
+ (if (file-readable-p recentf-save-file)
+ (load-file recentf-save-file))
+ (setq recentf-update-menu-p t)
+ (add-hook 'find-file-hooks 'recentf-add-file-hook)
+ (add-hook 'write-file-hooks 'recentf-add-file-hook)
+ (add-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
+ (add-hook 'kill-emacs-hook 'recentf-save-list))
+ (when recentf-initialized-p
+ (setq recentf-initialized-p nil)
+ (recentf-save-list)
+ (easy-menu-remove-item nil recentf-menu-path recentf-menu-title)
+ (remove-hook 'find-file-hooks 'recentf-add-file-hook)
+ (remove-hook 'write-file-hooks 'recentf-add-file-hook)
+ (remove-hook 'menu-bar-update-hook 'recentf-update-menu-hook)
+ (remove-hook 'kill-emacs-hook 'recentf-save-list)))
+ (setq recentf-mode on-p)))
+
+(provide 'recentf)
+
+(run-hooks 'recentf-load-hook)
+
+;;; recentf.el ends here.