recentf.el 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. ;;; recentf.el --- setup a menu of recently opened files
  2. ;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. ;; Author: David Ponce <david@dponce.com>
  4. ;; Created: July 19 1999
  5. ;; Keywords: files
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package maintains a menu for visiting files that were operated
  19. ;; on recently. When enabled a new "Open Recent" sub menu is
  20. ;; displayed in the "File" menu. The recent files list is
  21. ;; automatically saved across Emacs sessions. You can customize the
  22. ;; number of recent files displayed, the location of the menu and
  23. ;; others options (see the source code for details).
  24. ;; To enable this package, add the following to your .emacs:
  25. ;; (recentf-mode 1)
  26. ;;; History:
  27. ;;
  28. ;;; Code:
  29. (require 'easymenu)
  30. (require 'tree-widget)
  31. (require 'timer)
  32. ;;; Internal data
  33. ;;
  34. (defvar recentf-list nil
  35. "List of recently opened files.")
  36. (defsubst recentf-enabled-p ()
  37. "Return non-nil if recentf mode is currently enabled."
  38. (memq 'recentf-save-list kill-emacs-hook))
  39. ;;; Customization
  40. ;;
  41. (defgroup recentf nil
  42. "Maintain a menu of recently opened files."
  43. :version "21.1"
  44. :group 'files)
  45. (defgroup recentf-filters nil
  46. "Group to customize recentf menu filters.
  47. You should define the options of your own filters in this group."
  48. :group 'recentf)
  49. (defcustom recentf-max-saved-items 20
  50. "Maximum number of items of the recent list that will be saved.
  51. A nil value means to save the whole list.
  52. See the command `recentf-save-list'."
  53. :group 'recentf
  54. :type 'integer)
  55. (defcustom recentf-save-file (locate-user-emacs-file "recentf" ".recentf")
  56. "File to save the recent list into."
  57. :group 'recentf
  58. :version "24.4"
  59. :type 'file
  60. :initialize 'custom-initialize-default
  61. :set (lambda (symbol value)
  62. (let ((oldvalue (eval symbol)))
  63. (custom-set-default symbol value)
  64. (and (not (equal value oldvalue))
  65. recentf-mode
  66. (recentf-load-list)))))
  67. (defcustom recentf-save-file-modes 384 ;; 0600
  68. "Mode bits of recentf save file, as an integer, or nil.
  69. If non-nil, after writing `recentf-save-file', set its mode bits to
  70. this value. By default give R/W access only to the user who owns that
  71. file. See also the function `set-file-modes'."
  72. :group 'recentf
  73. :type '(choice (const :tag "Don't change" nil)
  74. integer))
  75. (defcustom recentf-exclude nil
  76. "List of regexps and predicates for filenames excluded from the recent list.
  77. When a filename matches any of the regexps or satisfies any of the
  78. predicates it is excluded from the recent list.
  79. A predicate is a function that is passed a filename to check and that
  80. must return non-nil to exclude it."
  81. :group 'recentf
  82. :type '(repeat (choice regexp function)))
  83. (defun recentf-keep-default-predicate (file)
  84. "Return non-nil if FILE should be kept in the recent list.
  85. It handles the case of remote files as well."
  86. (cond
  87. ((file-remote-p file nil t) (file-readable-p file))
  88. ((file-remote-p file))
  89. ((file-readable-p file))))
  90. (defcustom recentf-keep
  91. '(recentf-keep-default-predicate)
  92. "List of regexps and predicates for filenames kept in the recent list.
  93. Regexps and predicates are tried in the specified order.
  94. When nil all filenames are kept in the recent list.
  95. When a filename matches any of the regexps or satisfies any of the
  96. predicates it is kept in the recent list.
  97. The default is to keep readable files. Remote files are checked
  98. for readability only in case a connection is established to that
  99. remote system, otherwise they are kept in the recent list without
  100. checking their readability.
  101. A predicate is a function that is passed a filename to check and that
  102. must return non-nil to keep it."
  103. :group 'recentf
  104. :type '(repeat (choice regexp function)))
  105. (defun recentf-menu-customization-changed (variable value)
  106. "Function called when the recentf menu customization has changed.
  107. Set VARIABLE with VALUE, and force a rebuild of the recentf menu."
  108. (if (and (featurep 'recentf) (recentf-enabled-p))
  109. (progn
  110. ;; Unavailable until recentf has been loaded.
  111. (recentf-hide-menu)
  112. (set-default variable value)
  113. (recentf-show-menu))
  114. (set-default variable value)))
  115. (defcustom recentf-menu-title "Open Recent"
  116. "Name of the recentf menu."
  117. :group 'recentf
  118. :type 'string
  119. :set 'recentf-menu-customization-changed)
  120. (defcustom recentf-menu-path '("File")
  121. "Path where to add the recentf menu.
  122. If nil add it at top level (see also `easy-menu-add-item')."
  123. :group 'recentf
  124. :type '(choice (const :tag "Top Level" nil)
  125. (sexp :tag "Menu Path"))
  126. :set 'recentf-menu-customization-changed)
  127. (defcustom recentf-menu-before "Open File..."
  128. "Name of the menu before which the recentf menu will be added.
  129. If nil add it at end of menu (see also `easy-menu-add-item')."
  130. :group 'recentf
  131. :type '(choice (string :tag "Name")
  132. (const :tag "Last" nil))
  133. :set 'recentf-menu-customization-changed)
  134. (defcustom recentf-menu-action 'find-file
  135. "Function to invoke with a filename item of the recentf menu.
  136. The default is to call `find-file' to edit the selected file."
  137. :group 'recentf
  138. :type 'function)
  139. (defcustom recentf-max-menu-items 10
  140. "Maximum number of items in the recentf menu."
  141. :group 'recentf
  142. :type 'integer)
  143. (defcustom recentf-menu-filter nil
  144. "Function used to filter files displayed in the recentf menu.
  145. A nil value means no filter. The following functions are predefined:
  146. - `recentf-sort-ascending'
  147. Sort menu items in ascending order.
  148. - `recentf-sort-descending'
  149. Sort menu items in descending order.
  150. - `recentf-sort-basenames-ascending'
  151. Sort menu items by filenames sans directory in ascending order.
  152. - `recentf-sort-basenames-descending'
  153. Sort menu items by filenames sans directory in descending order.
  154. - `recentf-sort-directories-ascending'
  155. Sort menu items by directories in ascending order.
  156. - `recentf-sort-directories-descending'
  157. Sort menu items by directories in descending order.
  158. - `recentf-show-basenames'
  159. Show filenames sans directory in menu items.
  160. - `recentf-show-basenames-ascending'
  161. Show filenames sans directory in ascending order.
  162. - `recentf-show-basenames-descending'
  163. Show filenames sans directory in descending order.
  164. - `recentf-relative-filter'
  165. Show filenames relative to `default-directory'.
  166. - `recentf-arrange-by-rule'
  167. Show sub-menus following user defined rules.
  168. - `recentf-arrange-by-mode'
  169. Show a sub-menu for each major mode.
  170. - `recentf-arrange-by-dir'
  171. Show a sub-menu for each directory.
  172. - `recentf-filter-changer'
  173. Manage a menu of filters.
  174. The filter function is called with one argument, the list of menu
  175. elements used to build the menu and must return a new list of menu
  176. elements (see `recentf-make-menu-element' for menu element form)."
  177. :group 'recentf
  178. :type '(radio (const nil)
  179. (function-item recentf-sort-ascending)
  180. (function-item recentf-sort-descending)
  181. (function-item recentf-sort-basenames-ascending)
  182. (function-item recentf-sort-basenames-descending)
  183. (function-item recentf-sort-directories-ascending)
  184. (function-item recentf-sort-directories-descending)
  185. (function-item recentf-show-basenames)
  186. (function-item recentf-show-basenames-ascending)
  187. (function-item recentf-show-basenames-descending)
  188. (function-item recentf-relative-filter)
  189. (function-item recentf-arrange-by-rule)
  190. (function-item recentf-arrange-by-mode)
  191. (function-item recentf-arrange-by-dir)
  192. (function-item recentf-filter-changer)
  193. function))
  194. (defcustom recentf-menu-open-all-flag nil
  195. "Non-nil means to show an \"All...\" item in the menu.
  196. This item will replace the \"More...\" item."
  197. :group 'recentf
  198. :type 'boolean)
  199. (define-obsolete-variable-alias 'recentf-menu-append-commands-p
  200. 'recentf-menu-append-commands-flag
  201. "22.1")
  202. (defcustom recentf-menu-append-commands-flag t
  203. "Non-nil means to append command items to the menu."
  204. :group 'recentf
  205. :type 'boolean)
  206. (defcustom recentf-auto-cleanup 'mode
  207. "Define when to automatically cleanup the recent list.
  208. The following values can be set:
  209. - `mode'
  210. Cleanup when turning the mode on (default).
  211. - `never'
  212. Never cleanup the list automatically.
  213. - A number
  214. Cleanup each time Emacs has been idle that number of seconds.
  215. - A time string
  216. Cleanup at specified time string, for example at \"11:00pm\".
  217. Setting this variable directly does not take effect;
  218. use \\[customize].
  219. See also the command `recentf-cleanup', that can be used to manually
  220. cleanup the list."
  221. :group 'recentf
  222. :type '(radio (const :tag "When mode enabled"
  223. :value mode)
  224. (const :tag "Never"
  225. :value never)
  226. (number :tag "When idle that seconds"
  227. :value 300)
  228. (string :tag "At time"
  229. :value "11:00pm"))
  230. :set (lambda (variable value)
  231. (set-default variable value)
  232. (when (featurep 'recentf)
  233. ;; Unavailable until recentf has been loaded.
  234. (recentf-auto-cleanup))))
  235. (defcustom recentf-initialize-file-name-history t
  236. "Non-nil means to initialize `file-name-history' with the recent list.
  237. If `file-name-history' is not empty, do nothing."
  238. :group 'recentf
  239. :type 'boolean)
  240. (defcustom recentf-load-hook nil
  241. "Normal hook run at end of loading the `recentf' package."
  242. :group 'recentf
  243. :type 'hook)
  244. (defcustom recentf-filename-handlers nil
  245. "Functions to post process recent file names.
  246. They are successively passed a file name to transform it."
  247. :group 'recentf
  248. :type '(choice
  249. (const :tag "None" nil)
  250. (repeat :tag "Functions"
  251. (choice
  252. (const file-truename)
  253. (const abbreviate-file-name)
  254. (function :tag "Other function")))))
  255. (defcustom recentf-show-file-shortcuts-flag t
  256. "Whether to show \"[N]\" for the Nth item up to 10.
  257. If non-nil, `recentf-open-files' will show labels for keys that can be
  258. used as shortcuts to open the Nth file."
  259. :group 'recentf
  260. :type 'boolean)
  261. ;;; Utilities
  262. ;;
  263. (defconst recentf-case-fold-search
  264. (memq system-type '(windows-nt cygwin))
  265. "Non-nil if recentf searches and matches should ignore case.")
  266. (defsubst recentf-string-equal (s1 s2)
  267. "Return non-nil if strings S1 and S2 have identical contents.
  268. Ignore case if `recentf-case-fold-search' is non-nil."
  269. (if recentf-case-fold-search
  270. (string-equal (downcase s1) (downcase s2))
  271. (string-equal s1 s2)))
  272. (defsubst recentf-string-lessp (s1 s2)
  273. "Return non-nil if string S1 is less than S2 in lexicographic order.
  274. Ignore case if `recentf-case-fold-search' is non-nil."
  275. (if recentf-case-fold-search
  276. (string-lessp (downcase s1) (downcase s2))
  277. (string-lessp s1 s2)))
  278. (defun recentf-string-member (elt list)
  279. "Return non-nil if ELT is an element of LIST.
  280. The value is actually the tail of LIST whose car is ELT.
  281. ELT must be a string and LIST a list of strings.
  282. Ignore case if `recentf-case-fold-search' is non-nil."
  283. (while (and list (not (recentf-string-equal elt (car list))))
  284. (setq list (cdr list)))
  285. list)
  286. (defsubst recentf-trunc-list (l n)
  287. "Return from L the list of its first N elements."
  288. (let (nl)
  289. (while (and l (> n 0))
  290. (setq nl (cons (car l) nl)
  291. n (1- n)
  292. l (cdr l)))
  293. (nreverse nl)))
  294. (defun recentf-dump-variable (variable &optional limit)
  295. "Insert a \"(setq VARIABLE value)\" in the current buffer.
  296. When the value of VARIABLE is a list, optional argument LIMIT
  297. specifies a maximum number of elements to insert. By default insert
  298. the full list."
  299. (let ((value (symbol-value variable)))
  300. (if (atom value)
  301. (insert (format "\n(setq %S '%S)\n" variable value))
  302. (when (and (integerp limit) (> limit 0))
  303. (setq value (recentf-trunc-list value limit)))
  304. (insert (format "\n(setq %S\n '(" variable))
  305. (dolist (e value)
  306. (insert (format "\n %S" e)))
  307. (insert "\n ))\n"))))
  308. (defvar recentf-auto-cleanup-timer nil
  309. "Timer used to automatically cleanup the recent list.
  310. See also the option `recentf-auto-cleanup'.")
  311. (defun recentf-auto-cleanup ()
  312. "Automatic cleanup of the recent list."
  313. (when (timerp recentf-auto-cleanup-timer)
  314. (cancel-timer recentf-auto-cleanup-timer))
  315. (when recentf-mode
  316. (setq recentf-auto-cleanup-timer
  317. (cond
  318. ((eq 'mode recentf-auto-cleanup)
  319. (recentf-cleanup)
  320. nil)
  321. ((numberp recentf-auto-cleanup)
  322. (run-with-idle-timer
  323. recentf-auto-cleanup t 'recentf-cleanup))
  324. ((stringp recentf-auto-cleanup)
  325. (run-at-time
  326. recentf-auto-cleanup nil 'recentf-cleanup))))))
  327. ;;; File functions
  328. ;;
  329. (defsubst recentf-push (filename)
  330. "Push FILENAME into the recent list, if it isn't there yet.
  331. If it is there yet, move it at the beginning of the list.
  332. If `recentf-case-fold-search' is non-nil, ignore case when comparing
  333. filenames."
  334. (let ((m (recentf-string-member filename recentf-list)))
  335. (and m (setq recentf-list (delq (car m) recentf-list)))
  336. (push filename recentf-list)))
  337. (defun recentf-apply-filename-handlers (name)
  338. "Apply `recentf-filename-handlers' to file NAME.
  339. Return the transformed file name, or NAME if any handler failed, or
  340. returned nil."
  341. (or (condition-case nil
  342. (let ((handlers recentf-filename-handlers)
  343. (filename name))
  344. (while (and filename handlers)
  345. (setq filename (funcall (car handlers) filename)
  346. handlers (cdr handlers)))
  347. filename)
  348. (error nil))
  349. name))
  350. (defsubst recentf-expand-file-name (name)
  351. "Convert file NAME to absolute, and canonicalize it.
  352. NAME is first passed to the function `expand-file-name', then to
  353. `recentf-filename-handlers' to post process it."
  354. (recentf-apply-filename-handlers (expand-file-name name)))
  355. (defun recentf-include-p (filename)
  356. "Return non-nil if FILENAME should be included in the recent list.
  357. That is, if it doesn't match any of the `recentf-exclude' checks."
  358. (let ((case-fold-search recentf-case-fold-search)
  359. (checks recentf-exclude)
  360. (keepit t))
  361. (while (and checks keepit)
  362. ;; If there was an error in a predicate, err on the side of
  363. ;; keeping the file. (Bug#5843)
  364. (setq keepit (not (ignore-errors
  365. (if (stringp (car checks))
  366. ;; A regexp
  367. (string-match (car checks) filename)
  368. ;; A predicate
  369. (funcall (car checks) filename))))
  370. checks (cdr checks)))
  371. keepit))
  372. (defun recentf-keep-p (filename)
  373. "Return non-nil if FILENAME should be kept in the recent list.
  374. That is, if it matches any of the `recentf-keep' checks."
  375. (let* ((case-fold-search recentf-case-fold-search)
  376. (checks recentf-keep)
  377. (keepit (null checks)))
  378. (while (and checks (not keepit))
  379. (setq keepit (condition-case nil
  380. (if (stringp (car checks))
  381. ;; A regexp
  382. (string-match (car checks) filename)
  383. ;; A predicate
  384. (funcall (car checks) filename))
  385. (error nil))
  386. checks (cdr checks)))
  387. keepit))
  388. (defsubst recentf-add-file (filename)
  389. "Add or move FILENAME at the beginning of the recent list.
  390. Does nothing if the name satisfies any of the `recentf-exclude'
  391. regexps or predicates."
  392. (setq filename (recentf-expand-file-name filename))
  393. (when (recentf-include-p filename)
  394. (recentf-push filename)))
  395. (defsubst recentf-remove-if-non-kept (filename)
  396. "Remove FILENAME from the recent list, if file is not kept.
  397. Return non-nil if FILENAME has been removed."
  398. (unless (recentf-keep-p filename)
  399. (let ((m (recentf-string-member
  400. (recentf-expand-file-name filename) recentf-list)))
  401. (and m (setq recentf-list (delq (car m) recentf-list))))))
  402. (defsubst recentf-directory-compare (f1 f2)
  403. "Compare absolute filenames F1 and F2.
  404. First compare directories, then filenames sans directory.
  405. Return non-nil if F1 is less than F2."
  406. (let ((d1 (file-name-directory f1))
  407. (d2 (file-name-directory f2)))
  408. (if (recentf-string-equal d1 d2)
  409. (recentf-string-lessp (file-name-nondirectory f1)
  410. (file-name-nondirectory f2))
  411. (recentf-string-lessp d1 d2))))
  412. ;;; Menu building
  413. ;;
  414. (defsubst recentf-digit-shortcut-command-name (n)
  415. "Return a command name to open the Nth most recent file.
  416. See also the command `recentf-open-most-recent-file'."
  417. (intern (format "recentf-open-most-recent-file-%d" n)))
  418. (defvar recentf--shortcuts-keymap
  419. (let ((km (make-sparse-keymap)))
  420. (dolist (k '(0 9 8 7 6 5 4 3 2 1))
  421. (let ((cmd (recentf-digit-shortcut-command-name k)))
  422. ;; Define a shortcut command.
  423. (defalias cmd
  424. `(lambda ()
  425. (interactive)
  426. (recentf-open-most-recent-file ,k)))
  427. ;; Bind it to a digit key.
  428. (define-key km (vector (+ k ?0)) cmd)))
  429. km)
  430. "Digit shortcuts keymap.")
  431. (defvar recentf-menu-items-for-commands
  432. (list
  433. ["Cleanup list"
  434. recentf-cleanup
  435. :help "Remove duplicates, and obsoletes files from the recent list"
  436. :active t]
  437. ["Edit list..."
  438. recentf-edit-list
  439. :help "Manually remove files from the recent list"
  440. :active t]
  441. ["Save list now"
  442. recentf-save-list
  443. :help "Save the list of recently opened files now"
  444. :active t]
  445. ["Options..."
  446. (customize-group "recentf")
  447. :help "Customize recently opened files menu and options"
  448. :active t]
  449. )
  450. "List of menu items for recentf commands.")
  451. (defvar recentf-menu-filter-commands nil
  452. "This variable can be used by menu filters to setup their own command menu.
  453. If non-nil it must contain a list of valid menu-items to be appended
  454. to the recent file list part of the menu. Before calling a menu
  455. filter function this variable is reset to nil.")
  456. (defsubst recentf-elements (n)
  457. "Return a list of the first N elements of the recent list."
  458. (recentf-trunc-list recentf-list n))
  459. (defsubst recentf-make-menu-element (menu-item menu-value)
  460. "Create a new menu-element.
  461. A menu element is a pair (MENU-ITEM . MENU-VALUE), where MENU-ITEM is
  462. the menu item string displayed. MENU-VALUE is the file to be open
  463. when the corresponding MENU-ITEM is selected. Or it is a
  464. pair (SUB-MENU-TITLE . MENU-ELEMENTS) where SUB-MENU-TITLE is a
  465. sub-menu title and MENU-ELEMENTS is the list of menu elements in the
  466. sub-menu."
  467. (cons menu-item menu-value))
  468. (defsubst recentf-menu-element-item (e)
  469. "Return the item part of the menu-element E."
  470. (car e))
  471. (defsubst recentf-menu-element-value (e)
  472. "Return the value part of the menu-element E."
  473. (cdr e))
  474. (defsubst recentf-set-menu-element-item (e item)
  475. "Change the item part of menu-element E to ITEM."
  476. (setcar e item))
  477. (defsubst recentf-set-menu-element-value (e value)
  478. "Change the value part of menu-element E to VALUE."
  479. (setcdr e value))
  480. (defsubst recentf-sub-menu-element-p (e)
  481. "Return non-nil if menu-element E defines a sub-menu."
  482. (consp (recentf-menu-element-value e)))
  483. (defsubst recentf-make-default-menu-element (file)
  484. "Make a new default menu element with FILE.
  485. This a menu element (FILE . FILE)."
  486. (recentf-make-menu-element file file))
  487. (defsubst recentf-menu-elements (n)
  488. "Return a list of the first N default menu elements from the recent list.
  489. See also `recentf-make-default-menu-element'."
  490. (mapcar 'recentf-make-default-menu-element
  491. (recentf-elements n)))
  492. (defun recentf-apply-menu-filter (filter l)
  493. "Apply function FILTER to the list of menu-elements L.
  494. It takes care of sub-menu elements in L and recursively apply FILTER
  495. to them. It is guaranteed that FILTER receives only a list of single
  496. menu-elements (no sub-menu)."
  497. (if (and l (functionp filter))
  498. (let ((case-fold-search recentf-case-fold-search)
  499. elts others)
  500. ;; split L into two sub-lists, one of sub-menus elements and
  501. ;; another of single menu elements.
  502. (dolist (elt l)
  503. (if (recentf-sub-menu-element-p elt)
  504. (push elt elts)
  505. (push elt others)))
  506. ;; Apply FILTER to single elements.
  507. (when others
  508. (setq others (funcall filter (nreverse others))))
  509. ;; Apply FILTER to sub-menu elements.
  510. (setq l nil)
  511. (dolist (elt elts)
  512. (recentf-set-menu-element-value
  513. elt (recentf-apply-menu-filter
  514. filter (recentf-menu-element-value elt)))
  515. (push elt l))
  516. ;; Return the new filtered menu element list.
  517. (nconc l others))
  518. l))
  519. ;; Count the number of assigned menu shortcuts.
  520. (defvar recentf-menu-shortcuts)
  521. (defun recentf-make-menu-items (&optional _menu)
  522. "Make menu items from the recent list.
  523. This is a menu filter function which ignores the MENU argument."
  524. (setq recentf-menu-filter-commands nil)
  525. (let* ((recentf-menu-shortcuts 0)
  526. (file-items
  527. (condition-case err
  528. (mapcar 'recentf-make-menu-item
  529. (recentf-apply-menu-filter
  530. recentf-menu-filter
  531. (recentf-menu-elements recentf-max-menu-items)))
  532. (error
  533. (message "recentf update menu failed: %s"
  534. (error-message-string err))))))
  535. (append
  536. (or file-items
  537. '(["No files" t
  538. :help "No recent file to open"
  539. :active nil]))
  540. (if recentf-menu-open-all-flag
  541. '(["All..." recentf-open-files
  542. :help "Open recent files through a dialog"
  543. :active t])
  544. (and (< recentf-max-menu-items (length recentf-list))
  545. '(["More..." recentf-open-more-files
  546. :help "Open files not in the menu through a dialog"
  547. :active t])))
  548. (and recentf-menu-filter-commands '("---"))
  549. recentf-menu-filter-commands
  550. (and recentf-menu-items-for-commands '("---"))
  551. recentf-menu-items-for-commands)))
  552. (defun recentf-menu-value-shortcut (name)
  553. "Return a shortcut digit for file NAME.
  554. Return nil if file NAME is not one of the ten more recent."
  555. (let ((i 0) k)
  556. (while (and (not k) (< i 10))
  557. (if (string-equal name (nth i recentf-list))
  558. (progn
  559. (setq recentf-menu-shortcuts (1+ recentf-menu-shortcuts))
  560. (setq k (% (1+ i) 10)))
  561. (setq i (1+ i))))
  562. k))
  563. (defun recentf-make-menu-item (elt)
  564. "Make a menu item from menu element ELT."
  565. (let ((item (recentf-menu-element-item elt))
  566. (value (recentf-menu-element-value elt)))
  567. (if (recentf-sub-menu-element-p elt)
  568. (cons item (mapcar 'recentf-make-menu-item value))
  569. (let ((k (and (< recentf-menu-shortcuts 10)
  570. (recentf-menu-value-shortcut value))))
  571. (vector item
  572. ;; If the file name is one of the ten more recent, use
  573. ;; a digit shortcut command to open it, else use an
  574. ;; anonymous command.
  575. (if k
  576. (recentf-digit-shortcut-command-name k)
  577. `(lambda ()
  578. (interactive)
  579. (,recentf-menu-action ,value)))
  580. :help (concat "Open " value)
  581. :active t)))))
  582. (defsubst recentf-menu-bar ()
  583. "Return the keymap of the global menu bar."
  584. (lookup-key global-map [menu-bar]))
  585. (defun recentf-show-menu ()
  586. "Show the menu of recently opened files."
  587. (easy-menu-add-item
  588. (recentf-menu-bar) recentf-menu-path
  589. (list recentf-menu-title :filter 'recentf-make-menu-items)
  590. recentf-menu-before))
  591. (defun recentf-hide-menu ()
  592. "Hide the menu of recently opened files."
  593. (easy-menu-remove-item (recentf-menu-bar) recentf-menu-path
  594. recentf-menu-title))
  595. ;;; Predefined menu filters
  596. ;;
  597. (defsubst recentf-sort-ascending (l)
  598. "Sort the list of menu elements L in ascending order.
  599. The MENU-ITEM part of each menu element is compared."
  600. (sort (copy-sequence l)
  601. #'(lambda (e1 e2)
  602. (recentf-string-lessp
  603. (recentf-menu-element-item e1)
  604. (recentf-menu-element-item e2)))))
  605. (defsubst recentf-sort-descending (l)
  606. "Sort the list of menu elements L in descending order.
  607. The MENU-ITEM part of each menu element is compared."
  608. (sort (copy-sequence l)
  609. #'(lambda (e1 e2)
  610. (recentf-string-lessp
  611. (recentf-menu-element-item e2)
  612. (recentf-menu-element-item e1)))))
  613. (defsubst recentf-sort-basenames-ascending (l)
  614. "Sort the list of menu elements L in ascending order.
  615. Only filenames sans directory are compared."
  616. (sort (copy-sequence l)
  617. #'(lambda (e1 e2)
  618. (recentf-string-lessp
  619. (file-name-nondirectory (recentf-menu-element-value e1))
  620. (file-name-nondirectory (recentf-menu-element-value e2))))))
  621. (defsubst recentf-sort-basenames-descending (l)
  622. "Sort the list of menu elements L in descending order.
  623. Only filenames sans directory are compared."
  624. (sort (copy-sequence l)
  625. #'(lambda (e1 e2)
  626. (recentf-string-lessp
  627. (file-name-nondirectory (recentf-menu-element-value e2))
  628. (file-name-nondirectory (recentf-menu-element-value e1))))))
  629. (defsubst recentf-sort-directories-ascending (l)
  630. "Sort the list of menu elements L in ascending order.
  631. Compares directories then filenames to order the list."
  632. (sort (copy-sequence l)
  633. #'(lambda (e1 e2)
  634. (recentf-directory-compare
  635. (recentf-menu-element-value e1)
  636. (recentf-menu-element-value e2)))))
  637. (defsubst recentf-sort-directories-descending (l)
  638. "Sort the list of menu elements L in descending order.
  639. Compares directories then filenames to order the list."
  640. (sort (copy-sequence l)
  641. #'(lambda (e1 e2)
  642. (recentf-directory-compare
  643. (recentf-menu-element-value e2)
  644. (recentf-menu-element-value e1)))))
  645. (defun recentf-show-basenames (l &optional no-dir)
  646. "Filter the list of menu elements L to show filenames sans directory.
  647. When a filename is duplicated, it is appended a sequence number if
  648. optional argument NO-DIR is non-nil, or its directory otherwise."
  649. (let (filtered-names filtered-list full name counters sufx)
  650. (dolist (elt l (nreverse filtered-list))
  651. (setq full (recentf-menu-element-value elt)
  652. name (file-name-nondirectory full))
  653. (if (not (member name filtered-names))
  654. (push name filtered-names)
  655. (if no-dir
  656. (if (setq sufx (assoc name counters))
  657. (setcdr sufx (1+ (cdr sufx)))
  658. (setq sufx 1)
  659. (push (cons name sufx) counters))
  660. (setq sufx (file-name-directory full)))
  661. (setq name (format "%s(%s)" name sufx)))
  662. (push (recentf-make-menu-element name full) filtered-list))))
  663. (defsubst recentf-show-basenames-ascending (l)
  664. "Filter the list of menu elements L to show filenames sans directory.
  665. Filenames are sorted in ascending order.
  666. This filter combines the `recentf-sort-basenames-ascending' and
  667. `recentf-show-basenames' filters."
  668. (recentf-show-basenames (recentf-sort-basenames-ascending l)))
  669. (defsubst recentf-show-basenames-descending (l)
  670. "Filter the list of menu elements L to show filenames sans directory.
  671. Filenames are sorted in descending order.
  672. This filter combines the `recentf-sort-basenames-descending' and
  673. `recentf-show-basenames' filters."
  674. (recentf-show-basenames (recentf-sort-basenames-descending l)))
  675. (defun recentf-relative-filter (l)
  676. "Filter the list of menu-elements L to show relative filenames.
  677. Filenames are relative to the `default-directory'."
  678. (mapcar #'(lambda (menu-element)
  679. (let* ((ful (recentf-menu-element-value menu-element))
  680. (rel (file-relative-name ful default-directory)))
  681. (if (string-match "^\\.\\." rel)
  682. menu-element
  683. (recentf-make-menu-element rel ful))))
  684. l))
  685. ;;; Rule based menu filters
  686. ;;
  687. (defcustom recentf-arrange-rules
  688. '(
  689. ("Elisp files (%d)" ".\\.el\\'")
  690. ("Java files (%d)" ".\\.java\\'")
  691. ("C/C++ files (%d)" "c\\(pp\\)?\\'")
  692. )
  693. "List of rules used by `recentf-arrange-by-rule' to build sub-menus.
  694. A rule is a pair (SUB-MENU-TITLE . MATCHER). SUB-MENU-TITLE is the
  695. displayed title of the sub-menu where a `%d' `format' pattern is
  696. replaced by the number of items in the sub-menu. MATCHER is a regexp
  697. or a list of regexps. Items matching one of the regular expressions in
  698. MATCHER are added to the corresponding sub-menu.
  699. SUB-MENU-TITLE can be a function. It is passed every items that
  700. matched the corresponding MATCHER, and it must return a
  701. pair (SUB-MENU-TITLE . ITEM). SUB-MENU-TITLE is a computed sub-menu
  702. title that can be another function. ITEM is the received item which
  703. may have been modified to match another rule."
  704. :group 'recentf-filters
  705. :type '(repeat (cons (choice string function)
  706. (repeat regexp))))
  707. (defcustom recentf-arrange-by-rule-others "Other files (%d)"
  708. "Title of the `recentf-arrange-by-rule' sub-menu.
  709. This is for the menu where items that don't match any
  710. `recentf-arrange-rules' are displayed. If nil these items are
  711. displayed in the main recent files menu. A `%d' `format' pattern in
  712. the title is replaced by the number of items in the sub-menu."
  713. :group 'recentf-filters
  714. :type '(choice (const :tag "Main menu" nil)
  715. (string :tag "Title")))
  716. (defcustom recentf-arrange-by-rules-min-items 0
  717. "Minimum number of items in a `recentf-arrange-by-rule' sub-menu.
  718. If the number of items in a sub-menu is less than this value the
  719. corresponding sub-menu items are displayed in the main recent files
  720. menu or in the `recentf-arrange-by-rule-others' sub-menu if
  721. defined."
  722. :group 'recentf-filters
  723. :type 'number)
  724. (defcustom recentf-arrange-by-rule-subfilter nil
  725. "Function called by a rule based filter to filter sub-menu elements.
  726. A nil value means no filter. See also `recentf-menu-filter'.
  727. You can't use another rule based filter here."
  728. :group 'recentf-filters
  729. :type '(choice (const nil) function)
  730. :set (lambda (variable value)
  731. (when (memq value '(recentf-arrange-by-rule
  732. recentf-arrange-by-mode
  733. recentf-arrange-by-dir))
  734. (error "Recursive use of a rule based filter"))
  735. (set-default variable value)))
  736. (defun recentf-match-rule (file)
  737. "Return the rule that match FILE."
  738. (let ((rules recentf-arrange-rules)
  739. match found)
  740. (while (and (not found) rules)
  741. (setq match (cdar rules))
  742. (when (stringp match)
  743. (setq match (list match)))
  744. (while (and match (not (string-match (car match) file)))
  745. (setq match (cdr match)))
  746. (if match
  747. (setq found (cons (caar rules) file))
  748. (setq rules (cdr rules))))
  749. found))
  750. (defun recentf-arrange-by-rule (l)
  751. "Filter the list of menu-elements L.
  752. Arrange them in sub-menus following rules in `recentf-arrange-rules'."
  753. (when recentf-arrange-rules
  754. (let (menus others menu file min count)
  755. ;; Put menu items into sub-menus as defined by rules.
  756. (dolist (elt l)
  757. (setq file (recentf-menu-element-value elt)
  758. menu (recentf-match-rule file))
  759. (while (functionp (car menu))
  760. (setq menu (funcall (car menu) (cdr menu))))
  761. (if (not (stringp (car menu)))
  762. (push elt others)
  763. (setq menu (or (assoc (car menu) menus)
  764. (car (push (list (car menu)) menus))))
  765. (recentf-set-menu-element-value
  766. menu (cons elt (recentf-menu-element-value menu)))))
  767. ;; Finalize each sub-menu:
  768. ;; - truncate it depending on the value of
  769. ;; `recentf-arrange-by-rules-min-items',
  770. ;; - replace %d by the number of menu items,
  771. ;; - apply `recentf-arrange-by-rule-subfilter' to menu items.
  772. (setq min (if (natnump recentf-arrange-by-rules-min-items)
  773. recentf-arrange-by-rules-min-items 0)
  774. l nil)
  775. (dolist (elt menus)
  776. (setq menu (recentf-menu-element-value elt)
  777. count (length menu))
  778. (if (< count min)
  779. (setq others (nconc menu others))
  780. (recentf-set-menu-element-item
  781. elt (format (recentf-menu-element-item elt) count))
  782. (recentf-set-menu-element-value
  783. elt (recentf-apply-menu-filter
  784. recentf-arrange-by-rule-subfilter (nreverse menu)))
  785. (push elt l)))
  786. ;; Add the menu items remaining in the `others' bin.
  787. (when (setq others (nreverse others))
  788. (setq l (nconc
  789. l
  790. ;; Put items in an sub menu.
  791. (if (stringp recentf-arrange-by-rule-others)
  792. (list
  793. (recentf-make-menu-element
  794. (format recentf-arrange-by-rule-others
  795. (length others))
  796. (recentf-apply-menu-filter
  797. recentf-arrange-by-rule-subfilter others)))
  798. ;; Append items to the main menu.
  799. (recentf-apply-menu-filter
  800. recentf-arrange-by-rule-subfilter others)))))))
  801. l)
  802. ;;; Predefined rule based menu filters
  803. ;;
  804. (defun recentf-indirect-mode-rule (file)
  805. "Apply a second level `auto-mode-alist' regexp to FILE."
  806. (recentf-match-rule (substring file 0 (match-beginning 0))))
  807. (defun recentf-build-mode-rules ()
  808. "Convert `auto-mode-alist' to menu filter rules.
  809. Rules obey `recentf-arrange-rules' format."
  810. (let ((case-fold-search recentf-case-fold-search)
  811. regexp rule-name rule rules)
  812. (dolist (mode auto-mode-alist)
  813. (setq regexp (car mode)
  814. mode (cdr mode))
  815. (when mode
  816. (cond
  817. ;; Build a special "strip suffix" rule from entries of the
  818. ;; form (REGEXP FUNCTION NON-NIL). Notice that FUNCTION is
  819. ;; ignored by the menu filter. So in some corner cases a
  820. ;; wrong mode could be guessed.
  821. ((and (consp mode) (cadr mode))
  822. (setq rule-name 'recentf-indirect-mode-rule))
  823. ((and mode (symbolp mode))
  824. (setq rule-name (symbol-name mode))
  825. (if (string-match "\\(.*\\)-mode$" rule-name)
  826. (setq rule-name (match-string 1 rule-name)))
  827. (setq rule-name (concat rule-name " (%d)"))))
  828. (setq rule (assoc rule-name rules))
  829. (if rule
  830. (setcdr rule (cons regexp (cdr rule)))
  831. (push (list rule-name regexp) rules))))
  832. ;; It is important to preserve auto-mode-alist order
  833. ;; to ensure the right file <-> mode association
  834. (nreverse rules)))
  835. (defun recentf-arrange-by-mode (l)
  836. "Split the list of menu-elements L into sub-menus by major mode."
  837. (let ((recentf-arrange-rules (recentf-build-mode-rules))
  838. (recentf-arrange-by-rule-others "others (%d)"))
  839. (recentf-arrange-by-rule l)))
  840. (defun recentf-file-name-nondir (l)
  841. "Filter the list of menu-elements L to show filenames sans directory.
  842. This simplified version of `recentf-show-basenames' does not handle
  843. duplicates. It is used by `recentf-arrange-by-dir' as its
  844. `recentf-arrange-by-rule-subfilter'."
  845. (mapcar #'(lambda (e)
  846. (recentf-make-menu-element
  847. (file-name-nondirectory (recentf-menu-element-value e))
  848. (recentf-menu-element-value e)))
  849. l))
  850. (defun recentf-dir-rule (file)
  851. "Return as a sub-menu, the directory FILE belongs to."
  852. (cons (file-name-directory file) file))
  853. (defun recentf-arrange-by-dir (l)
  854. "Split the list of menu-elements L into sub-menus by directory."
  855. (let ((recentf-arrange-rules '((recentf-dir-rule . ".*")))
  856. (recentf-arrange-by-rule-subfilter 'recentf-file-name-nondir)
  857. recentf-arrange-by-rule-others)
  858. (recentf-arrange-by-rule l)))
  859. ;;; Menu of menu filters
  860. ;;
  861. (defvar recentf-filter-changer-current nil
  862. "Current filter used by `recentf-filter-changer'.")
  863. (defcustom recentf-filter-changer-alist
  864. '(
  865. (recentf-arrange-by-mode . "Grouped by Mode")
  866. (recentf-arrange-by-dir . "Grouped by Directory")
  867. (recentf-arrange-by-rule . "Grouped by Custom Rules")
  868. )
  869. "List of filters managed by `recentf-filter-changer'.
  870. Each filter is defined by a pair (FUNCTION . LABEL), where FUNCTION is
  871. the filter function, and LABEL is the menu item displayed to select
  872. that filter."
  873. :group 'recentf-filters
  874. :type '(repeat (cons function string))
  875. :set (lambda (variable value)
  876. (setq recentf-filter-changer-current nil)
  877. (set-default variable value)))
  878. (defun recentf-filter-changer-select (filter)
  879. "Select FILTER as the current menu filter.
  880. See `recentf-filter-changer'."
  881. (setq recentf-filter-changer-current filter))
  882. (defun recentf-filter-changer (l)
  883. "Manage a sub-menu of menu filters.
  884. `recentf-filter-changer-alist' defines the filters in the menu.
  885. Filtering of L is delegated to the selected filter in the menu."
  886. (unless recentf-filter-changer-current
  887. (setq recentf-filter-changer-current
  888. (caar recentf-filter-changer-alist)))
  889. (if (not recentf-filter-changer-current)
  890. l
  891. (setq recentf-menu-filter-commands
  892. (list
  893. `("Show files"
  894. ,@(mapcar
  895. #'(lambda (f)
  896. `[,(cdr f)
  897. (setq recentf-filter-changer-current ',(car f))
  898. ;;:active t
  899. :style radio ;;radio Don't work with GTK :-(
  900. :selected (eq recentf-filter-changer-current
  901. ',(car f))
  902. ;;:help ,(cdr f)
  903. ])
  904. recentf-filter-changer-alist))))
  905. (recentf-apply-menu-filter recentf-filter-changer-current l)))
  906. ;;; Hooks
  907. ;;
  908. (defun recentf-track-opened-file ()
  909. "Insert the name of the file just opened or written into the recent list."
  910. (and buffer-file-name
  911. (recentf-add-file buffer-file-name))
  912. ;; Must return nil because it is run from `write-file-functions'.
  913. nil)
  914. (defun recentf-track-closed-file ()
  915. "Update the recent list when a buffer is killed.
  916. That is, remove a non kept file from the recent list."
  917. (and buffer-file-name
  918. (recentf-remove-if-non-kept buffer-file-name)))
  919. (defconst recentf-used-hooks
  920. '(
  921. (find-file-hook recentf-track-opened-file)
  922. (write-file-functions recentf-track-opened-file)
  923. (kill-buffer-hook recentf-track-closed-file)
  924. (kill-emacs-hook recentf-save-list)
  925. )
  926. "Hooks used by recentf.")
  927. ;;; Commands
  928. ;;
  929. ;;; Common dialog stuff
  930. ;;
  931. (defun recentf-cancel-dialog (&rest _ignore)
  932. "Cancel the current dialog.
  933. IGNORE arguments."
  934. (interactive)
  935. (kill-buffer (current-buffer))
  936. (message "Dialog canceled"))
  937. (defun recentf-dialog-goto-first (widget-type)
  938. "Move the cursor to the first WIDGET-TYPE in current dialog.
  939. Go to the beginning of buffer if not found."
  940. (goto-char (point-min))
  941. (condition-case nil
  942. (let (done)
  943. (widget-move 1)
  944. (while (not done)
  945. (if (eq widget-type (widget-type (widget-at (point))))
  946. (setq done t)
  947. (widget-move 1))))
  948. (error
  949. (goto-char (point-min)))))
  950. (defvar recentf-dialog-mode-map
  951. (let ((km (copy-keymap recentf--shortcuts-keymap)))
  952. (set-keymap-parent km widget-keymap)
  953. (define-key km "q" 'recentf-cancel-dialog)
  954. (define-key km "n" 'next-line)
  955. (define-key km "p" 'previous-line)
  956. (define-key km [follow-link] "\C-m")
  957. km)
  958. "Keymap used in recentf dialogs.")
  959. (define-derived-mode recentf-dialog-mode nil "recentf-dialog"
  960. "Major mode of recentf dialogs.
  961. \\{recentf-dialog-mode-map}"
  962. :syntax-table nil
  963. :abbrev-table nil
  964. (setq truncate-lines t))
  965. (defmacro recentf-dialog (name &rest forms)
  966. "Show a dialog buffer with NAME, setup with FORMS."
  967. (declare (indent 1) (debug t))
  968. `(with-current-buffer (get-buffer-create ,name)
  969. ;; Cleanup buffer
  970. (let ((inhibit-read-only t)
  971. (ol (overlay-lists)))
  972. (mapc 'delete-overlay (car ol))
  973. (mapc 'delete-overlay (cdr ol))
  974. (erase-buffer))
  975. (recentf-dialog-mode)
  976. ,@forms
  977. (widget-setup)
  978. (switch-to-buffer (current-buffer))))
  979. ;;; Edit list dialog
  980. ;;
  981. (defvar recentf-edit-list nil)
  982. (defun recentf-edit-list-select (widget &rest _ignore)
  983. "Toggle a file selection based on the checkbox WIDGET state.
  984. IGNORE other arguments."
  985. (let ((value (widget-get widget :tag))
  986. (check (widget-value widget)))
  987. (if check
  988. (add-to-list 'recentf-edit-list value)
  989. (setq recentf-edit-list (delq value recentf-edit-list)))
  990. (message "%s %sselected" value (if check "" "un"))))
  991. (defun recentf-edit-list-validate (&rest _ignore)
  992. "Process the recent list when the edit list dialog is committed.
  993. IGNORE arguments."
  994. (if recentf-edit-list
  995. (let ((i 0))
  996. (dolist (e recentf-edit-list)
  997. (setq recentf-list (delq e recentf-list)
  998. i (1+ i)))
  999. (kill-buffer (current-buffer))
  1000. (message "%S file(s) removed from the list" i))
  1001. (message "No file selected")))
  1002. (defun recentf-edit-list ()
  1003. "Show a dialog to delete selected files from the recent list."
  1004. (interactive)
  1005. (unless recentf-list
  1006. (error "The list of recent files is empty"))
  1007. (recentf-dialog (format "*%s - Edit list*" recentf-menu-title)
  1008. (set (make-local-variable 'recentf-edit-list) nil)
  1009. (widget-insert
  1010. "Click on OK to delete selected files from the recent list.
  1011. Click on Cancel or type `q' to cancel.\n")
  1012. ;; Insert the list of files as checkboxes
  1013. (dolist (item recentf-list)
  1014. (widget-create 'checkbox
  1015. :value nil ; unselected checkbox
  1016. :format "\n %[%v%] %t"
  1017. :tag item
  1018. :notify 'recentf-edit-list-select))
  1019. (widget-insert "\n\n")
  1020. (widget-create
  1021. 'push-button
  1022. :notify 'recentf-edit-list-validate
  1023. :help-echo "Delete selected files from the recent list"
  1024. "Ok")
  1025. (widget-insert " ")
  1026. (widget-create
  1027. 'push-button
  1028. :notify 'recentf-cancel-dialog
  1029. "Cancel")
  1030. (recentf-dialog-goto-first 'checkbox)))
  1031. ;;; Open file dialog
  1032. ;;
  1033. (defun recentf-open-files-action (widget &rest _ignore)
  1034. "Open the file stored in WIDGET's value when notified.
  1035. IGNORE other arguments."
  1036. (kill-buffer (current-buffer))
  1037. (funcall recentf-menu-action (widget-value widget)))
  1038. ;; List of files associated to a digit shortcut key.
  1039. (defvar recentf--files-with-key nil)
  1040. (defun recentf-show-digit-shortcut-filter (l)
  1041. "Filter the list of menu-elements L to show digit shortcuts."
  1042. (let ((i 0))
  1043. (dolist (e l)
  1044. (setq i (1+ i))
  1045. (recentf-set-menu-element-item
  1046. e (format "[%d] %s" (% i 10) (recentf-menu-element-item e))))
  1047. l))
  1048. (defun recentf-open-files-item (menu-element)
  1049. "Return a widget to display MENU-ELEMENT in a dialog buffer."
  1050. (if (consp (cdr menu-element))
  1051. ;; Represent a sub-menu with a tree widget
  1052. `(tree-widget
  1053. :open t
  1054. :match ignore
  1055. :node (item :tag ,(car menu-element)
  1056. :sample-face bold
  1057. :format "%{%t%}:\n")
  1058. ,@(mapcar 'recentf-open-files-item
  1059. (cdr menu-element)))
  1060. ;; Represent a single file with a link widget
  1061. `(link :tag ,(car menu-element)
  1062. :button-prefix ""
  1063. :button-suffix ""
  1064. :button-face default
  1065. :format "%[%t\n%]"
  1066. :help-echo ,(concat "Open " (cdr menu-element))
  1067. :action recentf-open-files-action
  1068. ,(cdr menu-element))))
  1069. (defun recentf-open-files-items (files)
  1070. "Return a list of widgets to display FILES in a dialog buffer."
  1071. (set (make-local-variable 'recentf--files-with-key)
  1072. (recentf-trunc-list files 10))
  1073. (mapcar 'recentf-open-files-item
  1074. (append
  1075. ;; When requested group the files with shortcuts together
  1076. ;; at the top of the list.
  1077. (when recentf-show-file-shortcuts-flag
  1078. (setq files (nthcdr 10 files))
  1079. (recentf-apply-menu-filter
  1080. 'recentf-show-digit-shortcut-filter
  1081. (mapcar 'recentf-make-default-menu-element
  1082. recentf--files-with-key)))
  1083. ;; Then the other files.
  1084. (recentf-apply-menu-filter
  1085. recentf-menu-filter
  1086. (mapcar 'recentf-make-default-menu-element
  1087. files)))))
  1088. (defun recentf-open-files (&optional files buffer-name)
  1089. "Show a dialog to open a recent file.
  1090. If optional argument FILES is non-nil, it is a list of recently-opened
  1091. files to choose from. It defaults to the whole recent list.
  1092. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1093. use for the dialog. It defaults to \"*`recentf-menu-title'*\"."
  1094. (interactive)
  1095. (unless (or files recentf-list)
  1096. (error "There is no recent file to open"))
  1097. (recentf-dialog (or buffer-name (format "*%s*" recentf-menu-title))
  1098. (widget-insert "Click on a file"
  1099. (if recentf-show-file-shortcuts-flag
  1100. ", or type the corresponding digit key,"
  1101. "")
  1102. " to open it.\n"
  1103. (format-message "Click on Cancel or type ‘q’ to cancel.\n"))
  1104. ;; Use a L&F that looks like the recentf menu.
  1105. (tree-widget-set-theme "folder")
  1106. (apply 'widget-create
  1107. `(group
  1108. :indent 2
  1109. :format "\n%v\n"
  1110. ,@(recentf-open-files-items (or files recentf-list))))
  1111. (widget-create
  1112. 'push-button
  1113. :notify 'recentf-cancel-dialog
  1114. "Cancel")
  1115. (recentf-dialog-goto-first 'link)))
  1116. (defun recentf-open-more-files ()
  1117. "Show a dialog to open a recent file that is not in the menu."
  1118. (interactive)
  1119. (recentf-open-files (nthcdr recentf-max-menu-items recentf-list)
  1120. (format "*%s - More*" recentf-menu-title)))
  1121. (defun recentf-open-most-recent-file (&optional n)
  1122. "Open the Nth most recent file.
  1123. Optional argument N must be a valid digit number. It defaults to 1.
  1124. 1 opens the most recent file, 2 the second most recent one, etc..
  1125. 0 opens the tenth most recent file."
  1126. (interactive "p")
  1127. (cond
  1128. ((zerop n) (setq n 10))
  1129. ((and (> n 0) (< n 10)))
  1130. ((error "Recent file number out of range [0-9], %d" n)))
  1131. (let ((file (nth (1- n) (or recentf--files-with-key recentf-list))))
  1132. (unless file (error "Not that many recent files"))
  1133. ;; Close the open files dialog.
  1134. (when recentf--files-with-key
  1135. (kill-buffer (current-buffer)))
  1136. (funcall recentf-menu-action file)))
  1137. ;;; Save/load/cleanup the recent list
  1138. ;;
  1139. (defconst recentf-save-file-header
  1140. ";;; Automatically generated by `recentf' on %s.\n"
  1141. "Header to be written into the `recentf-save-file'.")
  1142. (defconst recentf-save-file-coding-system
  1143. (if (coding-system-p 'utf-8-emacs)
  1144. 'utf-8-emacs
  1145. 'emacs-mule)
  1146. "Coding system of the file `recentf-save-file'.")
  1147. (defun recentf-save-list ()
  1148. "Save the recent list.
  1149. Write data into the file specified by `recentf-save-file'."
  1150. (interactive)
  1151. (condition-case error
  1152. (with-temp-buffer
  1153. (erase-buffer)
  1154. (set-buffer-file-coding-system recentf-save-file-coding-system)
  1155. (insert (format-message recentf-save-file-header
  1156. (current-time-string)))
  1157. (recentf-dump-variable 'recentf-list recentf-max-saved-items)
  1158. (recentf-dump-variable 'recentf-filter-changer-current)
  1159. (insert "\n \n;; Local Variables:\n"
  1160. (format ";; coding: %s\n" recentf-save-file-coding-system)
  1161. ";; End:\n")
  1162. (write-file (expand-file-name recentf-save-file))
  1163. (when recentf-save-file-modes
  1164. (set-file-modes recentf-save-file recentf-save-file-modes))
  1165. nil)
  1166. (error
  1167. (warn "recentf mode: %s" (error-message-string error)))))
  1168. (defun recentf-load-list ()
  1169. "Load a previously saved recent list.
  1170. Read data from the file specified by `recentf-save-file'.
  1171. When `recentf-initialize-file-name-history' is non-nil, initialize an
  1172. empty `file-name-history' with the recent list."
  1173. (interactive)
  1174. (let ((file (expand-file-name recentf-save-file)))
  1175. (when (file-readable-p file)
  1176. (load-file file)
  1177. (and recentf-initialize-file-name-history
  1178. (not file-name-history)
  1179. (setq file-name-history (mapcar 'abbreviate-file-name
  1180. recentf-list))))))
  1181. (defun recentf-cleanup ()
  1182. "Cleanup the recent list.
  1183. That is, remove duplicates, non-kept, and excluded files."
  1184. (interactive)
  1185. (message "Cleaning up the recentf list...")
  1186. (let ((n 0)
  1187. (ht (make-hash-table
  1188. :size recentf-max-saved-items
  1189. :test 'equal))
  1190. newlist key)
  1191. (dolist (f recentf-list)
  1192. (setq f (recentf-expand-file-name f)
  1193. key (if recentf-case-fold-search (downcase f) f))
  1194. (if (and (recentf-include-p f)
  1195. (recentf-keep-p f)
  1196. (not (gethash key ht)))
  1197. (progn
  1198. (push f newlist)
  1199. (puthash key t ht))
  1200. (setq n (1+ n))
  1201. (message "File %s removed from the recentf list" f)))
  1202. (message "Cleaning up the recentf list...done (%d removed)" n)
  1203. (setq recentf-list (nreverse newlist))))
  1204. ;;; The minor mode
  1205. ;;
  1206. (defvar recentf-mode-map (make-sparse-keymap)
  1207. "Keymap to use in recentf mode.")
  1208. ;;;###autoload
  1209. (define-minor-mode recentf-mode
  1210. "Toggle \"Open Recent\" menu (Recentf mode).
  1211. With a prefix argument ARG, enable Recentf mode if ARG is
  1212. positive, and disable it otherwise. If called from Lisp, enable
  1213. Recentf mode if ARG is omitted or nil.
  1214. When Recentf mode is enabled, a \"Open Recent\" submenu is
  1215. displayed in the \"File\" menu, containing a list of files that
  1216. were operated on recently."
  1217. :global t
  1218. :group 'recentf
  1219. :keymap recentf-mode-map
  1220. (unless (and recentf-mode (recentf-enabled-p))
  1221. (if recentf-mode
  1222. (progn
  1223. (recentf-load-list)
  1224. (recentf-show-menu))
  1225. (recentf-hide-menu)
  1226. (recentf-save-list))
  1227. (recentf-auto-cleanup)
  1228. (let ((hook-setup (if recentf-mode 'add-hook 'remove-hook)))
  1229. (dolist (hook recentf-used-hooks)
  1230. (apply hook-setup hook)))))
  1231. (defun recentf-unload-function ()
  1232. "Unload the recentf library."
  1233. (recentf-mode -1)
  1234. ;; continue standard unloading
  1235. nil)
  1236. (provide 'recentf)
  1237. (run-hooks 'recentf-load-hook)
  1238. ;;; recentf.el ends here