From: Juanma Barranquero Date: Sat, 26 Apr 2003 23:41:59 +0000 (+0000) Subject: Major rewrite. The code is reordered, cleaner and faster. X-Git-Tag: ttn-vms-21-2-B4~10409 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=be9e7056daaf9112afc394fea96fe3fe67b26070;p=emacs.git Major rewrite. The code is reordered, cleaner and faster. Introduced new options to automatically cleanup the recent list, and to handle filename transformation (for example to use true filenames). (recentf-version): New constant. (recentf-save-file-header): Moved. (recentf-data-cache): New variable. (recentf-update-menu-p, recentf-initialized-p): Removed. (recentf-menu-customization-changed): Moved. Doc fix. (recentf-max-saved-items): Doc fix. (recentf-save-file): Doc fix. No more expand filename here. (recentf-exclude, recentf-menu-action) (recentf-menu-filter): Doc fix. (recentf-menu-append-commands-flag): Renamed from... (recentf-menu-append-commands-p): Made obsolete. (recentf-keep-non-readable-files-flag): Renamed from... (recentf-keep-non-readable-files-p): Made obsolete. (recentf-auto-cleanup, recentf-filename-handler): New options. (recentf-string-equal, recentf-string-lessp) (recentf-string-member): New functions. (recentf-trunc-list): Moved. (recentf-dump-variable): Moved. Better code and output format. (recentf-auto-cleanup-timer): New variable. (recentf-auto-cleanup): New function. (recentf-push, recentf-expand-file-name): New functions. (recentf-add-file): In-lined. Use above functions. (recentf-remove-if-non-readable): In-lined. Expand file name. (recentf-find-file): Use `recentf-remove-if-non-readable'. (recentf-directory-compare): Moved. Use `recentf-string-equal' and `recentf-string-lessp' to do comparisons. (recentf-menu-items-for-commands) (recentf-menu-filter-commands): Moved. (recentf-elements, recentf-make-menu-element) (recentf-menu-element-item, recentf-menu-element-value) (recentf-set-menu-element-item, recentf-set-menu-element-value) (recentf-sub-menu-element-p, recentf-make-default-menu-element) (recentf-menu-elements): In-lined. Some doc fix. (recentf-apply-menu-filter): Better code. (recentf-make-menu-items): Doc fix. Use `recentf-menu-append-commands-flag'. (recentf-make-menu-item): In-lined. Better code. (recentf-clear-data): New function. (recentf-sort-ascending, recentf-sort-descending) (recentf-sort-basenames-ascending) (recentf-sort-basenames-descending) (recentf-sort-directories-ascending) (recentf-sort-directories-descending) (recentf-show-basenames-ascending) (recentf-show-basenames-descending: In-lined. Better code. Some doc fix. (recentf-show-basenames) (recentf-relative-filter): Better code. Doc fix. (recentf-arrange-by-rule-subfilter): Doc fix. Improved :set code. (recentf-match-rule-p): Use filename instead of file-path. (recentf-arrange-by-rule, recentf-build-mode-rules) (recentf-arrange-by-mode, recentf-build-dir-rules) (recentf-file-name-nondir) (recentf-filter-changer-alist): Some doc fix and code cleanup. (recentf-filter-changer-goto-next): Doc fix. Call `recentf-clear-data'. (recentf-filter-changer-get-current) (recentf-filter-changer-get-next): In-lined. Doc fix and better code. (recentf-filter-changer): Doc fix and better code. (recentf-cancel-dialog): Doc fix. (recentf-dialog-mode-map): Initialized in defvar. (recentf-dialog-mode): Doc fix. (recentf-track-opened-file): Renamed from... (recentf-add-file-hook): Removed. (recentf-track-closed-file): Renamed from... (recentf-remove-file-hook): Removed. (recentf-update-menu-hook): Removed. Replaced by... (recentf-update-menu): New. Better catch unnecessary updates. Display a message on error. (recentf-used-hooks): New constant. (recentf-enabled-p): New function. (recentf-edit-selected-items) (recentf-open-files-action) (recentf-open-files-item-shift): Doc fix. (recentf-edit-list-action) (recentf-open-files-item): Doc fix. Code cleanup. (recentf-edit-list, recentf-open-files) (recentf-open-more-files): Likewise. Removed autoload cookie. (recentf-save-list, recentf-cleanup): Likewise. Moved. (recentf-load-list): New command. (recentf-mode): Better code. Does nothing if enabling the already enabled mode. --- diff --git a/lisp/recentf.el b/lisp/recentf.el index f7683e30698..698e848ce6a 100644 --- a/lisp/recentf.el +++ b/lisp/recentf.el @@ -1,17 +1,21 @@ ;;; recentf.el --- setup a menu of recently opened files -;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. +;; Copyright (C) 1999, 2000, 2001, 2002, 2003 +;; Free Software Foundation, Inc. ;; Author: David Ponce ;; Created: July 19 1999 -;; Keywords: customization +;; Maintainer: FSF +;; Keywords: files + +(defconst recentf-version "$Revision$") ;; 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. +;; 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 @@ -26,49 +30,31 @@ ;;; 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 +;; 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: +;; the source code for details). + +;;; History: ;; -;; (require 'recentf) -;; (recentf-mode 1) ;;; Code: - (require 'easymenu) (require 'wid-edit) +(require 'timer) -(defconst recentf-save-file-header - ";;; Automatically generated by `recentf' on %s.\n" - "Header to be written into the `recentf-save-file'.") - +;;; Internal data +;; (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)) - +(defvar recentf-data-cache nil + "Cache of data used to build the recentf menu. +The menu is rebuilt when this data has changed.") + +;;; Customization +;; (defgroup recentf nil "Maintain a menu of recently opened files." :version "21.1" @@ -80,20 +66,30 @@ 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'." + "*Maximum number of items of the recent list that will be saved. +nil means to save the whole list. +See the command `recentf-save-list'." :group 'recentf :type 'integer) -(defcustom recentf-save-file (expand-file-name "~/.recentf") - "*File to save `recentf-list' into." +(defcustom recentf-save-file "~/.recentf" + "*File to save the recent list into." :group 'recentf :type 'file) (defcustom recentf-exclude nil - "*List of regexps for filenames excluded from `recentf-list'." + "*List of regexps for filenames excluded from the recent list." :group 'recentf :type '(repeat regexp)) +(defun recentf-menu-customization-changed (variable value) + "Function called when the recentf menu customization has changed. +Set VARIABLE with VALUE, and force a rebuild of the recentf menu." + (when (featurep 'recentf) + ;; Unavailable until recentf has been loaded. + (recentf-clear-data)) + (set-default variable value)) + (defcustom recentf-menu-title "Open Recent" "*Name of the recentf menu." :group 'recentf @@ -118,11 +114,7 @@ If nil add it at end of menu (see also `easy-menu-change')." (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." +The default is to call `recentf-find-file' to edit the selected file." :group 'recentf :type 'function :set 'recentf-menu-customization-changed) @@ -137,73 +129,220 @@ list of recently opened files." "*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)." +- `recentf-sort-ascending' + Sort menu items in ascending order. +- `recentf-sort-descending' + Sort menu items in descending order. +- `recentf-sort-basenames-ascending' + Sort menu items by filenames sans directory in ascending order. +- `recentf-sort-basenames-descending' + Sort menu items by filenames sans directory in descending order. +- `recentf-sort-directories-ascending' + Sort menu items by directories in ascending order. +- `recentf-sort-directories-descending' + Sort menu items by directories in descending order. +- `recentf-show-basenames' + Show filenames sans directory in menu items. +- `recentf-show-basenames-ascending' + Show filenames sans directory in ascending order. +- `recentf-show-basenames-descending' + Show filenames sans directory in descending order. +- `recentf-relative-filter' + Show filenames relative to `default-directory'. +- `recentf-arrange-by-rule' + Show sub-menus following user defined rules. +- `recentf-arrange-by-mode' + Show a sub-menu for each major mode. +- `recentf-arrange-by-dir' + Show a sub-menu for each directory. +- `recentf-filter-changer' + 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 '(radio (const nil) - (function-item recentf-sort-ascending) - (function-item recentf-sort-descending) - (function-item recentf-sort-basenames-ascending) - (function-item recentf-sort-basenames-descending) - (function-item recentf-sort-directories-ascending) - (function-item recentf-sort-directories-descending) - (function-item recentf-show-basenames) - (function-item recentf-show-basenames-ascending) - (function-item recentf-show-basenames-descending) - (function-item recentf-relative-filter) - (function-item recentf-arrange-by-rule) - (function-item recentf-arrange-by-mode) - (function-item recentf-arrange-by-dir) - (function-item recentf-filter-changer) - function) + (function-item recentf-sort-ascending) + (function-item recentf-sort-descending) + (function-item recentf-sort-basenames-ascending) + (function-item recentf-sort-basenames-descending) + (function-item recentf-sort-directories-ascending) + (function-item recentf-sort-directories-descending) + (function-item recentf-show-basenames) + (function-item recentf-show-basenames-ascending) + (function-item recentf-show-basenames-descending) + (function-item recentf-relative-filter) + (function-item recentf-arrange-by-rule) + (function-item recentf-arrange-by-mode) + (function-item recentf-arrange-by-dir) + (function-item recentf-filter-changer) + function) :set 'recentf-menu-customization-changed) -(defcustom recentf-menu-append-commands-p t - "*If not-nil command items are appended to the menu." +(defcustom recentf-menu-append-commands-flag t + "*non-nil means to append command items 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'." +(defvaralias 'recentf-menu-append-commands-p + 'recentf-menu-append-commands-flag) +(make-obsolete-variable 'recentf-menu-append-commands-p + 'recentf-menu-append-commands-flag + "21.4") + +(defcustom recentf-keep-non-readable-files-flag nil + "*non-nil means to keep non readable files in the recent 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))) + :type 'boolean) + +(defvaralias 'recentf-keep-non-readable-files-p + 'recentf-keep-non-readable-files-flag) +(make-obsolete-variable 'recentf-keep-non-readable-files-p + 'recentf-keep-non-readable-files-flag + "21.4") + +(defcustom recentf-auto-cleanup 'mode + "*Define when to automatically cleanup the recent list. +The following values can be set: + +- `mode' + Cleanup when turning the mode on (default). +- `never' + Never cleanup the list automatically. +- A number + Cleanup each time Emacs has been idle that number of seconds. +- A time string + Cleanup at specified time string, for example at \"11:00pm\". + +Setting this variable directly does not take effect; +use \\[customize]. + +See also the command `recentf-cleanup', that can be used to manually +cleanup the list." + :group 'recentf + :type '(radio (const :tag "When mode enabled" + :value mode) + (const :tag "Never" + :value never) + (number :tag "When idle that seconds" + :value 300) + (string :tag "At time" + :value "11:00pm")) + :set (lambda (variable value) + (set-default variable value) + (when (featurep 'recentf) + ;; Unavailable until recentf has been loaded. + (recentf-auto-cleanup)))) (defcustom recentf-load-hook nil "*Normal hook run at end of loading the `recentf' package." :group 'recentf :type 'hook) -;;;; -;;;; Common functions -;;;; +(defcustom recentf-filename-handler nil + "Function to call to process filename handled by recentf. +It is passed a filename to give a chance to transform it. +If it returns nil, the filename is left unchanged." + :group 'recentf + :type 'function) + +;;; Utilities +;; (defconst recentf-case-fold-search (memq system-type '(vax-vms windows-nt cygwin)) "Non-nil if recentf searches and matches should ignore case.") +(defsubst recentf-string-equal (s1 s2) + "Return non-nil if strings S1 and S2 have identical contents. +Ignore case if `recentf-case-fold-search' is non-nil." + (if recentf-case-fold-search + (string-equal (downcase s1) (downcase s2)) + (string-equal s1 s2))) + +(defsubst recentf-string-lessp (s1 s2) + "Return non-nil if string S1 is less than S2 in lexicographic order. +Ignore case if `recentf-case-fold-search' is non-nil." + (if recentf-case-fold-search + (string-lessp (downcase s1) (downcase s2)) + (string-lessp s1 s2))) + +(defun recentf-string-member (elt list) + "Return non-nil if ELT is an element of LIST. +The value is actually the tail of LIST whose car is ELT. +ELT must be a string and LIST a list of strings. +Ignore case if `recentf-case-fold-search' is non-nil." + (while (and list (not (recentf-string-equal elt (car list)))) + (setq list (cdr list))) + list) + +(defsubst recentf-trunc-list (l n) + "Return from L the list of its first N elements." + (let (nl) + (while (and l (> n 0)) + (setq nl (cons (car l) nl) + n (1- n) + l (cdr l))) + (nreverse nl))) + +(defun recentf-dump-variable (variable &optional limit) + "Insert a \"(setq VARIABLE value)\" in the current buffer. +When the value of VARIABLE is a list, optional argument LIMIT +specifies a maximum number of elements to insert. By default insert +the full list." + (let ((value (symbol-value variable))) + (if (atom value) + (insert (format "\n(setq %S %S)\n" variable value)) + (when (and (integerp limit) (> limit 0)) + (setq value (recentf-trunc-list value limit))) + (insert (format "\n(setq %S\n '(" variable)) + (dolist (e value) + (insert (format "\n %S" e))) + (insert "\n ))\n")))) + +(defvar recentf-auto-cleanup-timer nil + "Timer used to automatically cleanup the recent list. +See also the option `recentf-auto-cleanup'.") + +(defun recentf-auto-cleanup () + "Automatic cleanup of the recent list." + (when (timerp recentf-auto-cleanup-timer) + (cancel-timer recentf-auto-cleanup-timer)) + (when recentf-mode + (setq recentf-auto-cleanup-timer + (cond + ((eq 'mode recentf-auto-cleanup) + (recentf-cleanup) + nil) + ((numberp recentf-auto-cleanup) + (run-with-idle-timer + recentf-auto-cleanup t 'recentf-cleanup)) + ((stringp recentf-auto-cleanup) + (run-at-time + recentf-auto-cleanup nil 'recentf-cleanup)))))) + +;;; File functions +;; +(defsubst recentf-push (filename) + "Push FILENAME into the recent list, if it isn't there yet. +If it is there yet, move it at the beginning of the list. +If `recentf-case-fold-search' is non-nil, ignore case when comparing +filenames." + (let ((m (recentf-string-member filename recentf-list))) + (and m (setq recentf-list (delq (car m) recentf-list))) + (push filename recentf-list))) + +(defsubst recentf-expand-file-name (name) + "Convert filename NAME to absolute, and canonicalize it. +See also the function `expand-file-name'. +If defined, call the function `recentf-filename-handler' to post +process the canonical name." + (let* ((filename (expand-file-name name))) + (or (and recentf-filename-handler + (funcall recentf-filename-handler filename)) + filename))) + (defun recentf-include-p (filename) "Return t if FILENAME match none of the `recentf-exclude' regexps." (let ((case-fold-search recentf-case-fold-search) @@ -212,86 +351,111 @@ used to build the menu and must return a new list of menu elements (see (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)))) +(defsubst recentf-add-file (filename) + "Add or move FILENAME at the beginning of the recent list. +Does nothing it if it matches any of the `recentf-exclude' regexps." + (setq filename (recentf-expand-file-name filename)) + (when (recentf-include-p filename) + (recentf-push filename))) -(defun recentf-remove-if-non-readable (filename) - "Remove FILENAME from `recentf-list' if not readable." +(defsubst recentf-remove-if-non-readable (filename) + "Remove FILENAME from the recent list, if file is not readable. +Return non-nil if FILENAME has been removed." (unless (file-readable-p filename) - (setq recentf-list (delete filename recentf-list)) - (setq recentf-update-menu-p t))) + (let ((m (recentf-string-member + (recentf-expand-file-name filename) recentf-list))) + (and m (setq recentf-list (delq (car m) recentf-list)))))) (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))) +If the file does not exist or is non readable, and +`recentf-keep-non-readable-files-flag' is nil, it is not edited and +its name is removed from the recent list." + (if (and (not recentf-keep-non-readable-files-flag) + (recentf-remove-if-non-readable filename)) + (message "File `%s' not found" filename) + (find-file filename))) + +(defsubst recentf-directory-compare (f1 f2) + "Compare absolute filenames F1 and F2. +First compare directories, then filenames sans directory. +Return non-nil if F1 is less than F2." + (let ((d1 (file-name-directory f1)) + (d2 (file-name-directory f2))) + (if (recentf-string-equal d1 d2) + (recentf-string-lessp (file-name-nondirectory f1) + (file-name-nondirectory f2)) + (recentf-string-lessp d1 d2)))) + +;;; Menu building +;; +(defvar recentf-menu-items-for-commands + (list ["Cleanup list" + recentf-cleanup + :help "Remove all non-readable and excluded files from the recent list" + :active t] + ["Edit list..." + recentf-edit-list + :help "Edit the files that are kept in the recent list" + :active t] + ["Save list now" + recentf-save-list + :help "Save the list of recently opened files now" + :active t] + ["Options..." + (customize-group "recentf") + :help "Customize recently opened files menu and options" + :active t] + ) + "List of menu items for recentf commands.") -(defun recentf-elements (n) - "Return a list of the first N elements of `recentf-list'." +(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.") + +(defsubst recentf-elements (n) + "Return a list of the first N elements of the recent list." (recentf-trunc-list recentf-list n)) -(defun recentf-make-menu-element (menu-item menu-value) +(defsubst 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." +A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is +the menu item string displayed. MENU-VALUE is the file to be open +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) +(defsubst recentf-menu-element-item (e) "Return the item part of the menu-element E." (car e)) -(defun recentf-menu-element-value (e) +(defsubst recentf-menu-element-value (e) "Return the value part of the menu-element E." (cdr e)) -(defun recentf-set-menu-element-item (e item) +(defsubst 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) +(defsubst 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) +(defsubst 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). -Do so 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)) +(defsubst recentf-make-default-menu-element (file) + "Make a new default menu element with FILE. +This a menu element (FILE . FILE)." + (recentf-make-menu-element file file)) -(defun recentf-menu-elements (n) - "Return a list of the first N default menu elements from `recentf-list'. +(defsubst recentf-menu-elements (n) + "Return a list of the first N default menu elements from the recent list. See also `recentf-make-default-menu-element'." (mapcar 'recentf-make-default-menu-element (recentf-elements n))) @@ -301,69 +465,31 @@ See also `recentf-make-default-menu-element'." It takes care of sub-menu elements in L and recursively apply FILTER to them. It is guaranteed that FILTER receives only a list of single menu-elements (no sub-menu)." - (if (and (functionp filter) l) + (if (and l (functionp filter)) (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)) + elts others) + ;; split L into two sub-listes, one of sub-menus elements and + ;; another of single menu elements. + (dolist (elt l) + (if (recentf-sub-menu-element-p elt) + (push elt elts) + (push elt others))) + ;; Apply FILTER to single elements. + (when others + (setq others (funcall filter (nreverse others)))) + ;; Apply FILTER to sub-menu elements. + (setq l nil) + (dolist (elt elts) (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)) + elt (recentf-apply-menu-filter + filter (recentf-menu-element-value elt))) + (push elt l)) + ;; Return the new filtered menu element list. + (nconc l others)) l)) -(defvar recentf-menu-items-for-commands - (list ["Cleanup list" - recentf-cleanup - :help "Remove all non-readable and excluded files from the recent list" - :active t] - ["Edit list..." - recentf-edit-list - :help "Edit the files that are kept in the recent list" - :active t] - ["Save list now" - recentf-save-list - :help "Save the list of recently opened files now" - :active t] - ["Options..." - (customize-group "recentf") - :help "Customize recently opened files menu and options" - :active 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'." + "Make menu items from the recent list." (setq recentf-menu-filter-commands nil) (let ((file-items (mapcar 'recentf-make-menu-item @@ -380,149 +506,128 @@ filter function this variable is reset to nil.") (and recentf-menu-filter-commands (cons "---" recentf-menu-filter-commands)) - (and recentf-menu-append-commands-p + (and recentf-menu-append-commands-flag (cons "---" recentf-menu-items-for-commands))))) -(defun recentf-make-menu-item (menu-element) - "Make a menu item from 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) - :help (concat "Open " menu-value) +(defsubst recentf-make-menu-item (elt) + "Make a menu item from menu element ELT." + (let ((item (recentf-menu-element-item elt)) + (value (recentf-menu-element-value elt))) + (if (recentf-sub-menu-element-p elt) + (cons item (mapcar 'recentf-make-menu-item value)) + (vector item (list recentf-menu-action value) + :help (concat "Open " value) :active t)))) -;;;; -;;;; Predefined menu filter functions -;;;; - -(defun recentf-sort-ascending (l) +(defun recentf-clear-data () + "Clear data used to build the recentf menu. +This force a rebuild of the menu." + (easy-menu-remove-item nil recentf-menu-path recentf-menu-title) + (setq recentf-data-cache nil)) + +;;; Predefined menu filters +;; +(defsubst 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)))))) + #'(lambda (e1 e2) + (recentf-string-lessp + (recentf-menu-element-item e1) + (recentf-menu-element-item e2))))) -(defun recentf-sort-descending (l) +(defsubst 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)))))) + #'(lambda (e1 e2) + (recentf-string-lessp + (recentf-menu-element-item e2) + (recentf-menu-element-item e1))))) -(defun recentf-sort-basenames-ascending (l) +(defsubst recentf-sort-basenames-ascending (l) "Sort the list of menu elements L in ascending order. -Only file names (without directories) are compared." +Only filenames sans directory 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))))))) + #'(lambda (e1 e2) + (recentf-string-lessp + (file-name-nondirectory (recentf-menu-element-value e1)) + (file-name-nondirectory (recentf-menu-element-value e2)))))) -(defun recentf-sort-basenames-descending (l) +(defsubst recentf-sort-basenames-descending (l) "Sort the list of menu elements L in descending order. -Only file names (without directories) are compared." +Only filenames sans directory 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 paths P1 and P2. -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) + #'(lambda (e1 e2) + (recentf-string-lessp + (file-name-nondirectory (recentf-menu-element-value e2)) + (file-name-nondirectory (recentf-menu-element-value e1)))))) + +(defsubst 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)))))) + #'(lambda (e1 e2) + (recentf-directory-compare + (recentf-menu-element-value e1) + (recentf-menu-element-value e2))))) -(defun recentf-sort-directories-descending (l) +(defsubst 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. -Show only file names in the menu, sorted in ascending order. This -filter combines the `recentf-sort-basenames-ascending' and + #'(lambda (e1 e2) + (recentf-directory-compare + (recentf-menu-element-value e2) + (recentf-menu-element-value e1))))) + +(defun recentf-show-basenames (l &optional no-dir) + "Filter the list of menu elements L to show filenames sans directory. +When a filename is duplicated, it is appended a sequence number if +optional argument NO-DIR is non-nil, or its directory otherwise." + (let (filtered-names filtered-list full name counters sufx) + (dolist (elt l (nreverse filtered-list)) + (setq full (recentf-menu-element-value elt) + name (file-name-nondirectory full)) + (if (not (member name filtered-names)) + (push name filtered-names) + (if no-dir + (if (setq sufx (assoc name counters)) + (setcdr sufx (1+ (cdr sufx))) + (setq sufx 1) + (push (cons name sufx) counters)) + (setq sufx (file-name-directory full))) + (setq name (format "%s(%s)" name sufx))) + (push (recentf-make-menu-element name full) filtered-list)))) + +(defsubst recentf-show-basenames-ascending (l) + "Filter the list of menu elements L to show filenames sans directory. +Filenames are 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. -Show only file names in the menu, sorted in descending order. This -filter combines the `recentf-sort-basenames-descending' and +(defsubst recentf-show-basenames-descending (l) + "Filter the list of menu elements L to show filenames sans directory. +Filenames are 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. -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))))) + "Filter the list of menu-elements L to show relative filenames. +Filenames are relative to the `default-directory'." + (mapcar #'(lambda (menu-element) + (let* ((ful (recentf-menu-element-value menu-element)) + (rel (file-relative-name ful default-directory))) + (if (string-match "^\\.\\." rel) + menu-element + (recentf-make-menu-element rel ful)))) l)) - + +;;; Rule based menu filters +;; (defcustom recentf-arrange-rules '( ("Elisp files (%d)" ".\\.el$") @@ -561,163 +666,144 @@ defined." :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!" + "*Function called by a rule based filter to filter sub-menu elements. +nil means no filter. See also `recentf-menu-filter'. +You can't use another rule based filter here." :group 'recentf-filters :type '(choice (const nil) 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. + :set (lambda (variable value) + (when (memq value '(recentf-arrange-by-rule + recentf-arrange-by-mode + recentf-arrange-by-dir)) + (error "Recursive use of a rule based filter")) + (recentf-menu-customization-changed variable value))) + +(defun recentf-match-rule-p (matcher filename) + "Return non-nil if the rule specified by MATCHER match FILENAME. See `recentf-arrange-rules' for details on MATCHER." (if (stringp matcher) - (string-match matcher file-path) + (string-match matcher filename) (while (and (consp matcher) - (not (string-match (car matcher) file-path))) + (not (string-match (car matcher) filename))) (setq matcher (cdr matcher))) matcher)) (defun recentf-arrange-by-rule (l) "Filter the list of menu-elements L. 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)) - + (if (not recentf-arrange-rules) + l + (let ((menus (mapcar #'(lambda (r) (list (car r))) + recentf-arrange-rules)) + menu others min file rules elts count) + (dolist (elt l) + (setq file (recentf-menu-element-value elt) + rules recentf-arrange-rules + elts menus + menu nil) + (while (and (not menu) rules) + (when (recentf-match-rule-p (cdar rules) file) + (setq menu (car elts)) + (recentf-set-menu-element-value + menu (cons elt (recentf-menu-element-value menu)))) + (setq rules (cdr rules) + elts (cdr elts))) + (unless menu + (push elt others))) + + (setq l nil + min (if (natnump recentf-arrange-by-rules-min-items) + recentf-arrange-by-rules-min-items 0)) + (dolist (menu menus) + (when (setq elts (recentf-menu-element-value menu)) + (setq count (length elts)) + (if (< count min) + (setq others (nconc elts others)) + (recentf-set-menu-element-item + menu (format (recentf-menu-element-item menu) count)) + (recentf-set-menu-element-value + menu (recentf-apply-menu-filter + recentf-arrange-by-rule-subfilter (nreverse elts))) + (push menu l)))) + + (if (and (stringp recentf-arrange-by-rule-others) others) + (nreverse + (cons + (recentf-make-menu-element + (format recentf-arrange-by-rule-others (length others)) + (recentf-apply-menu-filter + recentf-arrange-by-rule-subfilter (nreverse others))) + l)) + (nconc + (nreverse l) + (recentf-apply-menu-filter + recentf-arrange-by-rule-subfilter (nreverse others))))) + )) + +;;; Predefined rule based menu filters +;; (defun recentf-build-mode-rules () - "Convert `auto-mode-alist' to `recentf-arrange-rules' format." + "Convert `auto-mode-alist' to menu filter rules. +Rules obey `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)) + regexp rule-name rule rules) + (dolist (mode auto-mode-alist) + (setq regexp (car mode) + mode (cdr mode)) (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)) + (setq rule-name (concat rule-name " (%d)") + 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))) + (push (list rule-name regexp) rules)))) ;; 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." + "Split the list of menu-elements L into sub-menus by 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 menu-elements L to rules in `recentf-arrange-rules' format." + "Convert directories in menu-elements L to menu filter rules. +Rules obey `recentf-arrange-rules' format." (let (dirs) - (mapc (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))))) + (mapcar #'(lambda (e) + (let ((dir (file-name-directory + (recentf-menu-element-value e)))) + (or (recentf-string-member dir dirs) + (push dir dirs)))) + l) + (mapcar #'(lambda (d) + (cons (concat d " (%d)") + (concat "\\`" d))) + (nreverse (sort dirs 'recentf-string-lessp))))) (defun recentf-file-name-nondir (l) - "Filter the list of menu-elements L to show only filenames. + "Filter the list of menu-elements L to show filenames sans directory. This simplified version of `recentf-show-basenames' does 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)))) + (mapcar #'(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." + "Split the list of menu-elements L into sub-menus by 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)))) - + +;;; Ring of menu filters +;; (defvar recentf-filter-changer-state nil "Used by `recentf-filter-changer' to hold its state.") @@ -728,153 +814,140 @@ duplicates. It is used by `recentf-arrange-by-dir' as its (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" +Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is +the filter function, and LABEL is the menu item displayed to select +that filter." :group 'recentf-filters :type '(repeat (cons function string)) - :set (lambda (sym val) + :set (lambda (variable value) (setq recentf-filter-changer-state nil) - (recentf-menu-customization-changed sym val))) + (recentf-menu-customization-changed variable value))) (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)))) + "Go to the next filter available. +See `recentf-filter-changer'." + (setq recentf-filter-changer-state (cdr recentf-filter-changer-state)) + (recentf-clear-data)) + +(defsubst recentf-filter-changer-get-current () + "Get the current filter available. +See `recentf-filter-changer'." + (unless recentf-filter-changer-state + (setq recentf-filter-changer-state recentf-filter-changer-alist)) + (car recentf-filter-changer-state)) + +(defsubst recentf-filter-changer-get-next () + "Get the next filter available. +See `recentf-filter-changer'." + ;; At this point the current filter is the first element of + ;; `recentf-filter-changer-state'. + (car (or (cdr recentf-filter-changer-state) + ;; There is no next element in + ;; `recentf-filter-changer-state', so loop back to the + ;; first element of `recentf-filter-changer-alist'. + recentf-filter-changer-alist))) (defun recentf-filter-changer (l) - "Manage a ring of filters. + "Manage a ring of menu 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 +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) - :active t))))) + (let ((filter (recentf-filter-changer-get-current))) + (when filter + (setq l (recentf-apply-menu-filter (car filter) l) + filter (recentf-filter-changer-get-next)) + (when filter + (setq recentf-menu-filter-commands + (list (vector (cdr filter) + '(recentf-filter-changer-goto-next) + t))))) l)) - -;;;; -;;;; Dialogs stuff -;;;; - + +;;; Common dialog stuff +;; (defun recentf-cancel-dialog (&rest ignore) "Cancel the current dialog. -Used by `recentf-edit-list' and `recentf-open-files' dialogs." +Used internally by recentf dialogs. +IGNORE arguments." (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) - (define-key recentf-dialog-mode-map [down-mouse-1] 'widget-button-click) - (set-keymap-parent recentf-dialog-mode-map widget-keymap)) +(defvar recentf-dialog-mode-map + (let ((km (make-sparse-keymap))) + (define-key km "q" 'recentf-cancel-dialog) + (define-key km [down-mouse-1] 'widget-button-click) + (set-keymap-parent km widget-keymap) + km) + "Keymap used in recentf dialogs.") (defun recentf-dialog-mode () - "Major mode used in recentf dialogs. + "Major mode of recentf dialogs. -These are the special commands of `recentf-dialog-mode' mode: - q -- cancel this dialog." +\\{recentf-dialog-mode-map}" (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)) + +;;; Hooks +;; +(defun recentf-track-opened-file () + "Insert the name of the file just opened or written into the recent list." + (and buffer-file-name + (recentf-add-file buffer-file-name)) + ;; Must return nil because it is run from `write-file-functions'. 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) +(defun recentf-track-closed-file () + "Update the recent list when a buffer is killed. +That is, remove a non readable file from the recent list, if +`recentf-keep-non-readable-files-flag' is nil." + (and buffer-file-name + (not recentf-keep-non-readable-files-flag) + (recentf-remove-if-non-readable buffer-file-name))) + +(defun recentf-update-menu () + "Update the recentf menu from the current recent list." + (let ((cache (cons default-directory recentf-list))) + ;; Does nothing, if nothing has changed. + (unless (equal recentf-data-cache cache) + (setq recentf-data-cache cache) + (condition-case err (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))) - (if (listp value) - (progn - (when (and (integerp limit) (> limit 0)) - (setq value (recentf-trunc-list value limit))) - (insert (format "(setq %S '(" variable)) - (mapc (lambda (e) (insert (format "\n%S" e))) value) - (insert "))\n")) - (insert (format "(setq %S %S)\n" variable value))))) + recentf-menu-before) + (error + (message "recentf update menu failed: %s" + (error-message-string err))))))) -;;;###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) +(defconst recentf-used-hooks + '( + (find-file-hook recentf-track-opened-file) + (write-file-functions recentf-track-opened-file) + (kill-buffer-hook recentf-track-closed-file) + (menu-bar-update-hook recentf-update-menu) + (kill-emacs-hook recentf-save-list) + ) + "Hooks used by recentf.") +(defsubst recentf-enabled-p () + "Return non-nil if recentf mode is currently enabled." + (memq 'recentf-update-menu menu-bar-update-hook)) + +;;; Commands +;; (defvar recentf-edit-selected-items nil - "Used by `recentf-edit-list'. -Holds list of files to be deleted from `recentf-list'.") + "List of files to be deleted from the recent list. +Used internally by `recentf-edit-list'.") (defun recentf-edit-list-action (widget &rest ignore) - "Checkbox WIDGET action used by `recentf-edit-list' to select/unselect a file." + "Checkbox WIDGET action that toogles a file selection. +Used internally by `recentf-edit-list'. +IGNORE other arguments." (let ((value (widget-get widget ':tag))) ;; if value is already in the selected items (if (memq value recentf-edit-selected-items) @@ -882,136 +955,124 @@ Holds list of files to be deleted from `recentf-list'.") (progn (setq recentf-edit-selected-items (delq value recentf-edit-selected-items)) - (message "%s removed from selection." value)) + (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))))) + (push 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." + "Show a dialog buffer to edit the recent list. +That is to select files to be deleted from the recent list." (interactive) - (with-current-buffer (get-buffer-create (concat "*" recentf-menu-title " - Edit list*")) + (with-current-buffer + (get-buffer-create (format "*%s - Edit list*" recentf-menu-title)) (switch-to-buffer (current-buffer)) + ;; Cleanup buffer (kill-all-local-variables) - (let ((inhibit-read-only t)) - (erase-buffer)) - (let ((all (overlay-lists))) + (let ((inhibit-read-only t) + (ol (overlay-lists))) + (erase-buffer) ;; Delete all the overlays. - (mapc 'delete-overlay (car all)) - (mapc 'delete-overlay (cdr all))) + (mapc 'delete-overlay (car ol)) + (mapc 'delete-overlay (cdr ol))) (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") + (widget-insert + "\ +Select the files to be deleted from the recent list.\n\n\ +Click on Ok to update the list. \ +Click on Cancel or type \"q\" to quit.\n") ;; Insert the list of files as checkboxes - (mapc (function - (lambda (item) - (widget-create 'checkbox - :value nil ; unselected checkbox - :format "\n %[%v%] %t" - :tag item - :notify 'recentf-edit-list-action))) - recentf-list) + (dolist (item recentf-list) + (widget-create + 'checkbox + :value nil ; unselected checkbox + :format "\n %[%v%] %t" + :tag item + :notify 'recentf-edit-list-action)) (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)) - (mapc (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-create + 'push-button + :notify (lambda (&rest ignore) + (if recentf-edit-selected-items + (let ((i 0)) + (kill-buffer (current-buffer)) + (dolist (e recentf-edit-selected-items) + (setq recentf-list (delq e recentf-list) + i (1+ i))) + (message "%S file(s) removed from the list" i)) + (message "No file selected"))) + "Ok") (widget-insert " ") ;; Insert the Cancel button - (widget-create 'push-button - :notify 'recentf-cancel-dialog - "Cancel") + (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." + "Button WIDGET action that open a file. +Used internally by `recentf-open-files'. +IGNORE other arguments." (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.") + "Amount of space to shift right sub-menu items. +Used internally by `recentf-open-files'.") (defun recentf-open-files-item (menu-element) - "Insert 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 + "Insert an item widget for MENU-ELEMENT in the current dialog buffer. +Used internally by `recentf-open-files'." + (let ((item (car menu-element)) + (file (cdr menu-element))) + (if (consp file) ; 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")) - (mapc 'recentf-open-files-item - file-path) + (widget-create + 'item + :tag item + :sample-face 'bold + :format (concat shift "%{%t%}:\n")) + (mapc 'recentf-open-files-item file) (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-create + 'push-button + :button-face 'default + :tag item + :help-echo (concat "Open " file) + :format (concat recentf-open-files-item-shift "%[%t%]") + :notify 'recentf-open-files-action + file) (widget-insert "\n")))) -;;;###autoload (defun recentf-open-files (&optional files buffer-name) - "Display buffer allowing user to choose a file from recently-opened list. -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." + "Show a dialog buffer to open a recent file. +If optional argument FILES is non-nil, it specifies the list of +recently-opened files to choose from. It is the whole recent list +otherwise. +If optional argument BUFFER-NAME is non-nil, it specifies which buffer +name to use for the interaction. It is \"*`recentf-menu-title'*\" by +default." (interactive) - (if (null files) - (setq files recentf-list)) - (if (null buffer-name) - (setq buffer-name (concat "*" recentf-menu-title "*"))) + (unless files + (setq files recentf-list)) + (unless buffer-name + (setq buffer-name (format "*%s*" recentf-menu-title))) (with-current-buffer (get-buffer-create buffer-name) (switch-to-buffer (current-buffer)) + ;; Cleanup buffer (kill-all-local-variables) - (let ((inhibit-read-only t)) - (erase-buffer)) - (let ((all (overlay-lists))) + (let ((inhibit-read-only t) + (ol (overlay-lists))) + (erase-buffer) ;; Delete all the overlays. - (mapc 'delete-overlay (car all)) - (mapc 'delete-overlay (cdr all))) + (mapc 'delete-overlay (car ol)) + (mapc 'delete-overlay (cdr ol))) ;; 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" ) @@ -1023,54 +1084,78 @@ which buffer to use for the interaction." (mapcar 'recentf-make-default-menu-element files)))) (widget-insert "\n") ;; Insert the Cancel button - (widget-create 'push-button - :notify 'recentf-cancel-dialog - "Cancel") + (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." + "Show a dialog buffer to open a recent file that is not in the menu." (interactive) (recentf-open-files (nthcdr recentf-max-menu-items recentf-list) - (concat "*" recentf-menu-title " - More*"))) + (format "*%s - More*" recentf-menu-title))) +(defconst recentf-save-file-header + ";;; Automatically generated by `recentf' on %s.\n" + "Header to be written into the `recentf-save-file'.") + +(defun recentf-save-list () + "Save the recent list. +Write data into the file specified by `recentf-save-file'." + (interactive) + (with-temp-file (expand-file-name recentf-save-file) + (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) + nil)) + +(defun recentf-load-list () + "Load a previously saved recent list. +Read data from the file specified by `recentf-save-file'." + (interactive) + (let ((file (expand-file-name recentf-save-file))) + (when (file-readable-p file) + (load-file file)))) + +(defun recentf-cleanup () + "Remove all non-readable and excluded files from the recent list." + (interactive) + (message "Cleaning up the recentf list...") + (let (newlist) + (dolist (f recentf-list) + (if (and (file-readable-p f) (recentf-include-p f)) + (push f newlist) + (message "File %s removed from the recentf list" f))) + (setq recentf-list (nreverse newlist)) + (message "Cleaning up the recentf list...done"))) -;;; Note this definition must be at the end of the file, because -;;; `define-minor-mode' actually calls the mode-function if the -;;; associated variable is non-nil, which requires that all needed -;;; functions be already defined. [This is arguably a bug in d-m-m] ;;;###autoload (define-minor-mode recentf-mode "Toggle recentf mode. With prefix argument ARG, turn on if positive, otherwise off. Returns non-nil if the new state is enabled. -When recentf mode is enabled, it maintains a menu for visiting files that -were operated on recently." +When recentf mode is enabled, it maintains a menu for visiting files +that were operated on recently." :global t :group 'recentf - (if recentf-mode - (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)))) - + (unless (and recentf-mode (recentf-enabled-p)) + (if recentf-mode + (recentf-load-list) + (recentf-save-list)) + (recentf-auto-cleanup) + (recentf-clear-data) + (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook))) + (dolist (hook recentf-used-hooks) + (apply hook-setup hook))) + (run-hooks 'recentf-mode-hook) + (when (interactive-p) + (message "Recentf mode %sabled" (if recentf-mode "en" "dis")))) + recentf-mode) (provide 'recentf)