recentf.el 50 KB

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