filesets.el 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  1. ;;; filesets.el --- handle group of files -*- coding: utf-8 -*-
  2. ;; Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. ;; Author: Thomas Link <sanobast-emacs@yahoo.de>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: filesets convenience
  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. ;;; Code:
  18. (defvar filesets-version "1.8.4")
  19. (defvar filesets-homepage
  20. "http://members.a1.net/t.link/CompEmacsFilesets.html")
  21. ;;; Commentary:
  22. ;; Define filesets, which can be opened or saved with the power of one or
  23. ;; two mouse clicks only. A fileset is either a list of files, a file
  24. ;; pattern, a base directory and a search pattern (for files), or an
  25. ;; inclusion group (i.e. a base file including other files).
  26. ;; Usage:
  27. ;; 1. Put (require 'filesets) and (filesets-init) in your init file.
  28. ;; 2. Type ;; M-x filesets-edit or choose "Edit Filesets" from the menu.
  29. ;; 3. Save your customizations.
  30. ;; Caveat: Fileset names have to be unique.
  31. ;; Filesets.el adds a nifty filesets menu to your menubar. If you change
  32. ;; your filesets on the fly, don't forget to select "Save Filesets" from
  33. ;; the menu.
  34. ;; Pressing on the first item in the submenu will open all files at once.
  35. ;; Define your own function, e.g. browse-url, for opening a fileset's
  36. ;; files. Or define external viewers for opening files with other
  37. ;; programs. See `filesets-external-viewers'.
  38. ;; BTW, if you close a fileset, files, which have been changed, will
  39. ;; be silently saved. Change this behavior by setting
  40. ;; `filesets-save-buffer-fn'.
  41. ;;; Supported modes for inclusion groups (`filesets-ingroup-patterns'):
  42. ;; - Elisp
  43. ;; - Emacs-Wiki (simple names only)
  44. ;; - LaTeX
  45. ;;; Known bugs:
  46. ;;; To do:
  47. ;;- better handling of different customization scenarios
  48. ;; Data gathering should be better separated from building the menu
  49. ;; so that one could (1) use filesets without installing the menu
  50. ;; and (2) create new "frontends" to speedbar and others.
  51. ;; The functionality to call external viewers should be isolated in
  52. ;; an extra package and possibly integrated with the MIME
  53. ;; handling.
  54. ;;; Credits:
  55. ;; Helpful suggestions (but no significant code) were contributed by
  56. ;;- Christoph Conrad (at gmx de)
  57. ;;- Christian Ohler (at Informatik Uni-Oldenburg DE)
  58. ;;- Richard Stallman aka RMS (at gnu org)
  59. ;;- Per Abrahamsen aka abraham (at dina kvl dk)
  60. ;;; Code:
  61. (eval-when-compile (require 'cl-lib))
  62. ;;; Some variables
  63. (defvar filesets-menu-cache nil
  64. "The whole filesets menu.")
  65. (defvar filesets-cache-version nil
  66. "Filesets' cached version number.")
  67. (defvar filesets-cache-hostname nil
  68. "Filesets' cached system name.")
  69. (defvar filesets-ingroup-cache nil
  70. "A plist containing files and their ingroup data.")
  71. (defvar filesets-ingroup-files nil
  72. "List of files already processed when searching for included files.")
  73. (defvar filesets-has-changed-flag t
  74. "Non-nil means some fileset definition has changed.")
  75. (defvar filesets-submenus nil
  76. "An association list with filesets menu data.")
  77. (defvar filesets-updated-buffers nil
  78. "A list of buffers with updated menu bars.")
  79. (defvar filesets-menu-use-cached-flag nil
  80. "Use cached data. See `filesets-menu-ensure-use-cached' for details.")
  81. (defvar filesets-update-cache-file-flag nil
  82. "Non-nil means the cache needs updating.")
  83. (defvar filesets-ignore-next-set-default nil
  84. "List of custom variables for which the next `set-default' will be ignored.")
  85. (defvar filesets-output-buffer-flag nil
  86. "Non-nil means the current buffer is an output buffer created by filesets.
  87. Is buffer local variable.")
  88. (defvar filesets-verbosity 1
  89. "An integer defining the level of verbosity.
  90. 0 means no messages at all.")
  91. (defvar filesets-menu-ensure-use-cached
  92. (and (featurep 'xemacs)
  93. (if (fboundp 'emacs-version>=)
  94. (not (emacs-version>= 21 5))))
  95. "Make sure (X)Emacs uses filesets' cache.
  96. Well, if you use XEmacs (prior to 21.5?) custom.el is loaded after
  97. init.el. This means that settings saved in the cache file (see
  98. `filesets-menu-cache-file') will be overwritten by custom.el. In order
  99. to ensure the use of the cache file, set this variable to t -- which is
  100. the default for XEmacs prior to 21.5. If you want to change this value
  101. put \"(setq filesets-menu-ensure-use-cached VALUE)\" into your startup
  102. file -- before loading filesets.el.
  103. So, when should you think about setting this value to t? If filesets.el
  104. is loaded before user customizations. Thus, if (require 'filesets)
  105. precedes the `custom-set-variables' command or, for XEmacs, if init.el
  106. is loaded before custom.el, set this variable to t.")
  107. ;;; utils
  108. (defun filesets-filter-list (lst cond-fn)
  109. "Remove all elements not conforming to COND-FN from list LST.
  110. COND-FN takes one argument: the current element."
  111. ; (cl-remove 'dummy lst :test (lambda (dummy elt)
  112. ; (not (funcall cond-fn elt)))))
  113. (let ((rv nil))
  114. (dolist (elt lst rv)
  115. (when (funcall cond-fn elt)
  116. (setq rv (append rv (list elt)))))))
  117. (defun filesets-ormap (fsom-pred lst)
  118. "Return the tail of LST for the head of which FSOM-PRED is non-nil."
  119. (let ((fsom-lst lst)
  120. (fsom-rv nil))
  121. (while (and (not (null fsom-lst))
  122. (null fsom-rv))
  123. (if (funcall fsom-pred (car fsom-lst))
  124. (setq fsom-rv fsom-lst)
  125. (setq fsom-lst (cdr fsom-lst))))
  126. fsom-rv))
  127. (defun filesets-some (fss-pred fss-lst)
  128. "Return non-nil if FSS-PRED is non-nil for any element of FSS-LST.
  129. Like `some', return the first value of FSS-PRED that is non-nil."
  130. (catch 'exit
  131. (dolist (fss-this fss-lst nil)
  132. (let ((fss-rv (funcall fss-pred fss-this)))
  133. (when fss-rv
  134. (throw 'exit fss-rv))))))
  135. ;(fset 'filesets-some 'cl-some) ;; or use the cl function
  136. (defun filesets-member (fsm-item fsm-lst &rest fsm-keys)
  137. "Find the first occurrence of FSM-ITEM in FSM-LST.
  138. It is supposed to work like cl's `member*'. At the moment only the :test
  139. key is supported."
  140. (let ((fsm-test (or (plist-get fsm-keys ':test)
  141. (function equal))))
  142. (filesets-ormap (lambda (fsm-this)
  143. (funcall fsm-test fsm-item fsm-this))
  144. fsm-lst)))
  145. ;(fset 'filesets-member 'cl-member) ;; or use the cl function
  146. (defun filesets-sublist (lst beg &optional end)
  147. "Get the sublist of LST from BEG to END - 1."
  148. (let ((rv nil)
  149. (i beg)
  150. (top (or end
  151. (length lst))))
  152. (while (< i top)
  153. (setq rv (append rv (list (nth i lst))))
  154. (setq i (+ i 1)))
  155. rv))
  156. (defun filesets-select-command (cmd-list)
  157. "Select one command from CMD-LIST -- a string with space separated names."
  158. (let ((this (shell-command-to-string
  159. (format "which --skip-alias %s 2> /dev/null | head -n 1"
  160. cmd-list))))
  161. (if (equal this "")
  162. nil
  163. (file-name-nondirectory (substring this 0 (- (length this) 1))))))
  164. (defun filesets-which-command (cmd)
  165. "Call \"which CMD\"."
  166. (shell-command-to-string (format "which %s" cmd)))
  167. (defun filesets-which-command-p (cmd)
  168. "Call \"which CMD\" and return non-nil if the command was found."
  169. (when (string-match-p (format "\\(/[^/]+\\)?/%s" cmd)
  170. (filesets-which-command cmd))
  171. cmd))
  172. (defun filesets-message (level &rest args)
  173. "Show a message only if LEVEL is greater or equal then `filesets-verbosity'."
  174. (when (<= level (abs filesets-verbosity))
  175. (apply 'message args)))
  176. ;;; config file
  177. (defun filesets-save-config ()
  178. "Save filesets' customizations."
  179. (interactive)
  180. (customize-save-customized))
  181. (defun filesets-reset-fileset (&optional fileset no-cache)
  182. "Reset the cached values for one or all filesets."
  183. (if fileset
  184. (setq filesets-submenus (lax-plist-put filesets-submenus fileset nil))
  185. (setq filesets-submenus nil))
  186. (setq filesets-has-changed-flag t)
  187. (setq filesets-update-cache-file-flag (or filesets-update-cache-file-flag
  188. (not no-cache))))
  189. (defun filesets-set-config (fileset var val)
  190. "Set-default wrapper function."
  191. (filesets-reset-fileset fileset)
  192. (set-default var val))
  193. ; (customize-set-variable var val))
  194. ; (filesets-build-menu))
  195. ;; It seems this is a workaround for the XEmacs issue described in the
  196. ;; doc-string of filesets-menu-ensure-use-cached. Under Emacs this is
  197. ;; essentially just `set-default'.
  198. (defun filesets-set-default (sym val &optional init-flag)
  199. "Set-default wrapper function used in conjunction with `defcustom'.
  200. If SYM is in the list `filesets-ignore-next-set-default', delete
  201. it from that list, and return nil. Otherwise, set the value of
  202. SYM to VAL and return t. If INIT-FLAG is non-nil, set with
  203. `custom-initialize-set', otherwise with `set-default'."
  204. (let ((ignore-flag (member sym filesets-ignore-next-set-default)))
  205. (if ignore-flag
  206. (setq filesets-ignore-next-set-default
  207. (delete sym filesets-ignore-next-set-default))
  208. (if init-flag
  209. (custom-initialize-set sym val)
  210. (set-default sym val)))
  211. (not ignore-flag)))
  212. (defun filesets-set-default! (sym val)
  213. "Call `filesets-set-default' and reset cached data (i.e. rebuild menu)."
  214. (when (filesets-set-default sym val)
  215. (filesets-reset-fileset)))
  216. (defun filesets-set-default+ (sym val)
  217. "Call `filesets-set-default' and reset filesets' standard menu."
  218. (when (filesets-set-default sym val)
  219. (setq filesets-has-changed-flag t)))
  220. ; (filesets-reset-fileset nil t)))
  221. (defvar filesets-data)
  222. (defun filesets-data-set-default (sym val)
  223. "Set the default for `filesets-data'."
  224. (if filesets-menu-use-cached-flag
  225. (setq filesets-menu-use-cached-flag nil)
  226. (when (default-boundp 'filesets-data)
  227. (let ((modified-filesets
  228. (filesets-filter-list val
  229. (lambda (x)
  230. (let ((name (car x))
  231. (data (cdr x)))
  232. (let ((elt (assoc name filesets-data)))
  233. (or (not elt)
  234. (not (equal data (cdr elt))))))))))
  235. (dolist (x modified-filesets)
  236. (filesets-reset-fileset (car x))))))
  237. (filesets-set-default sym val))
  238. ;;; configuration
  239. (defgroup filesets nil
  240. "The fileset swapper."
  241. :prefix "filesets-"
  242. :group 'convenience
  243. :version "22.1")
  244. (defcustom filesets-menu-name "Filesets"
  245. "Filesets' menu name."
  246. :set (function filesets-set-default)
  247. :type 'string
  248. :group 'filesets)
  249. (defcustom filesets-menu-path '("File") ; cf recentf-menu-path
  250. "The menu under which the filesets menu should be inserted.
  251. See `add-submenu' for documentation."
  252. :set (function filesets-set-default)
  253. :type '(choice (const :tag "Top Level" nil)
  254. (sexp :tag "Menu Path"))
  255. :version "23.1" ; was nil
  256. :group 'filesets)
  257. (defcustom filesets-menu-before "Open File..." ; cf recentf-menu-before
  258. "The name of a menu before which this menu should be added.
  259. See `add-submenu' for documentation."
  260. :set (function filesets-set-default)
  261. :type '(choice (string :tag "Name")
  262. (const :tag "Last" nil))
  263. :version "23.1" ; was "File"
  264. :group 'filesets)
  265. (defcustom filesets-menu-in-menu nil
  266. "Use that instead of `current-menubar' as the menu to change.
  267. See `add-submenu' for documentation."
  268. :set (function filesets-set-default)
  269. :type 'sexp
  270. :group 'filesets)
  271. (defcustom filesets-menu-shortcuts-flag t
  272. "Non-nil means to prepend menus with hopefully unique shortcuts."
  273. :set (function filesets-set-default!)
  274. :type 'boolean
  275. :group 'filesets)
  276. (defcustom filesets-menu-shortcuts-marker "%_"
  277. "String for marking menu shortcuts."
  278. :set (function filesets-set-default!)
  279. :type 'string
  280. :group 'filesets)
  281. ;;(defcustom filesets-menu-cnvfp-flag nil
  282. ;; "Non-nil means show \"Convert :pattern to :files\" entry for :pattern menus."
  283. ;; :set (function filesets-set-default!)
  284. ;; :type 'boolean
  285. ;; :group 'filesets)
  286. (defcustom filesets-menu-cache-file
  287. (locate-user-emacs-file "filesets-cache.el")
  288. "File to be used for saving the filesets menu between sessions.
  289. Set this to \"\", to disable caching of menus.
  290. Don't forget to check out `filesets-menu-ensure-use-cached'."
  291. :set (function filesets-set-default)
  292. :type 'file
  293. :group 'filesets)
  294. (put 'filesets-menu-cache-file 'risky-local-variable t)
  295. (defcustom filesets-menu-cache-contents
  296. '(filesets-be-docile-flag
  297. filesets-submenus
  298. filesets-menu-cache
  299. filesets-ingroup-cache)
  300. "Stuff we want to save in `filesets-menu-cache-file'.
  301. Possible uses: don't save configuration data in the main startup files
  302. but in filesets's own cache. In this case add `filesets-data' to this
  303. list.
  304. There is a second reason for putting `filesets-data' on this list. If
  305. you frequently add and remove buffers on the fly to :files filesets, you
  306. don't need to save your customizations if `filesets-data' is being
  307. mirrored in the cache file. In this case the version in the cache file
  308. is the current one, and the version in your startup file will be
  309. silently updated later on.
  310. If you want caching to work properly, at least `filesets-submenus',
  311. `filesets-menu-cache', and `filesets-ingroup-cache' should be in this
  312. list.
  313. Don't forget to check out `filesets-menu-ensure-use-cached'."
  314. :set (function filesets-set-default)
  315. :type '(repeat
  316. (choice :tag "Variable"
  317. (const :tag "filesets-submenus"
  318. :value filesets-submenus)
  319. (const :tag "filesets-menu-cache"
  320. :value filesets-menu-cache)
  321. (const :tag "filesets-ingroup-cache"
  322. :value filesets-ingroup-cache)
  323. (const :tag "filesets-data"
  324. :value filesets-data)
  325. (const :tag "filesets-external-viewers"
  326. :value filesets-external-viewers)
  327. (const :tag "filesets-ingroup-patterns"
  328. :value filesets-ingroup-patterns)
  329. (const :tag "filesets-be-docile-flag"
  330. :value filesets-be-docile-flag)
  331. (sexp :tag "Other" :value nil)))
  332. :group 'filesets)
  333. (define-obsolete-variable-alias 'filesets-cache-fill-content-hooks
  334. 'filesets-cache-fill-content-hook "24.3")
  335. (defcustom filesets-cache-fill-content-hook nil
  336. "Hook run when writing the contents of filesets' cache file.
  337. The hook is called with the cache file as current buffer and the cursor
  338. at the last position. I.e. each hook has to make sure that the cursor is
  339. at the last position.
  340. Possible uses: If you don't want to save `filesets-data' in your normal
  341. configuration file, you can add a something like this
  342. \(lambda ()
  343. \(insert (format \"(setq-default filesets-data \\='%S)\"
  344. filesets-data))
  345. \(newline 2))
  346. to this hook.
  347. Don't forget to check out `filesets-menu-ensure-use-cached'."
  348. :set (function filesets-set-default)
  349. :type 'hook
  350. :group 'filesets)
  351. (defcustom filesets-cache-hostname-flag nil
  352. "Non-nil means cache the hostname.
  353. If the current name differs from the cached one,
  354. rebuild the menu and create a new cache file."
  355. :set (function filesets-set-default)
  356. :type 'boolean
  357. :group 'filesets)
  358. (defcustom filesets-cache-save-often-flag nil
  359. "Non-nil means save buffer on every change of the filesets menu.
  360. If this variable is set to nil and if Emacs crashes, the cache and
  361. filesets-data could get out of sync. Set this to t if this happens from
  362. time to time or if the fileset cache causes troubles."
  363. :set (function filesets-set-default)
  364. :type 'boolean
  365. :group 'filesets)
  366. (defcustom filesets-max-submenu-length 25
  367. "Maximum length of submenus.
  368. Set this value to 0 to turn menu splitting off. BTW, parts of submenus
  369. will not be rewrapped if their length exceeds this value."
  370. :set (function filesets-set-default)
  371. :type 'integer
  372. :group 'filesets)
  373. (defcustom filesets-max-entry-length 50
  374. "Truncate names of split submenus to this length."
  375. :set (function filesets-set-default)
  376. :type 'integer
  377. :group 'filesets)
  378. (defcustom filesets-browse-dir-function 'dired
  379. "A function or command used for browsing directories.
  380. When using an external command, \"%s\" will be replaced with the
  381. directory's name.
  382. Note: You have to manually rebuild the menu if you change this value."
  383. :set (function filesets-set-default)
  384. :type '(choice :tag "Function:"
  385. (const :tag "dired"
  386. :value dired)
  387. (list :tag "Command"
  388. :value ("" "%s")
  389. (string :tag "Name")
  390. (string :tag "Arguments"))
  391. (function :tag "Function"
  392. :value nil))
  393. :group 'filesets)
  394. (defcustom filesets-open-file-function 'filesets-find-or-display-file
  395. "The function used for opening files.
  396. `filesets-find-or-display-file' ... Filesets' default function for
  397. visiting files. This function checks if an external viewer is defined
  398. for a specific file type. Either this viewer, if defined, or
  399. `find-file' will be used to visit a file.
  400. `filesets-find-file' ... An alternative function that always uses
  401. `find-file'. If `filesets-be-docile-flag' is true, a file, which isn't
  402. readable, will not be opened.
  403. Caveat: Changes will take effect only after rebuilding the menu."
  404. :set (function filesets-set-default)
  405. :type '(choice :tag "Function:"
  406. (const :tag "filesets-find-or-display-file"
  407. :value filesets-find-or-display-file)
  408. (const :tag "filesets-find-file"
  409. :value filesets-find-file)
  410. (function :tag "Function"
  411. :value nil))
  412. :group 'filesets)
  413. (defcustom filesets-save-buffer-function 'save-buffer
  414. "The function used to save a buffer.
  415. Caveat: Changes will take effect after rebuilding the menu."
  416. :set (function filesets-set-default)
  417. :type '(choice :tag "Function:"
  418. (const :tag "save-buffer"
  419. :value save-buffer)
  420. (function :tag "Function"
  421. :value nil))
  422. :group 'filesets)
  423. (defcustom filesets-find-file-delay
  424. (if (and (featurep 'xemacs) gutter-buffers-tab-visible-p)
  425. 0.5
  426. 0)
  427. "Delay before calling `find-file'.
  428. This is for calls via `filesets-find-or-display-file'
  429. or `filesets-find-file'.
  430. Set this to 0, if you don't use XEmacs's buffer tabs."
  431. :set (function filesets-set-default)
  432. :type 'number
  433. :group 'filesets)
  434. (defcustom filesets-be-docile-flag nil
  435. "Non-nil means don't complain if a file or a directory doesn't exist.
  436. This is useful if you want to use the same startup files in different
  437. computer environments."
  438. :set (function filesets-set-default)
  439. :type 'boolean
  440. :group 'filesets)
  441. (defcustom filesets-sort-menu-flag t
  442. "Non-nil means sort the filesets menu alphabetically."
  443. :set (function filesets-set-default)
  444. :type 'boolean
  445. :group 'filesets)
  446. (defcustom filesets-sort-case-sensitive-flag t
  447. "Non-nil means sorting of the filesets menu is case sensitive."
  448. :set (function filesets-set-default)
  449. :type 'boolean
  450. :group 'filesets)
  451. (defcustom filesets-tree-max-level 3
  452. "Maximum scan depth for directory trees.
  453. A :tree fileset is defined by a base directory the contents of which
  454. will be recursively added to the menu. `filesets-tree-max-level' tells up
  455. to which level the directory structure should be scanned/listed,
  456. i.e. how deep the menu should be. Try something like
  457. \(\"HOME -- only one level\"
  458. \(:tree \"~\" \"^[^.].*[^~]$\")
  459. \(:tree-max-level 1)
  460. \(:filter-dirs-flag t))
  461. \(\"HOME -- up to 3 levels\"
  462. \(:tree \"~\" \"^[^.].*[^~]$\")
  463. \(:tree-max-level 3)
  464. \(:filter-dirs-flag t))
  465. and it should become clear what this option is about. In any case,
  466. including directory trees to the menu can take a lot of memory."
  467. :set (function filesets-set-default)
  468. :type 'integer
  469. :group 'filesets)
  470. (defcustom filesets-commands
  471. `(("Isearch"
  472. multi-isearch-files
  473. (filesets-cmd-isearch-getargs))
  474. ("Isearch (regexp)"
  475. multi-isearch-files-regexp
  476. (filesets-cmd-isearch-getargs))
  477. ("Query Replace"
  478. perform-replace
  479. (filesets-cmd-query-replace-getargs))
  480. ("Query Replace (regexp)"
  481. perform-replace
  482. (filesets-cmd-query-replace-regexp-getargs))
  483. ("Grep <<selection>>"
  484. "grep"
  485. ("-n " filesets-get-quoted-selection " " "<<file-name>>"))
  486. ("Run Shell Command"
  487. filesets-cmd-shell-command
  488. (filesets-cmd-shell-command-getargs)))
  489. "Commands to run on filesets.
  490. An association list of names, functions, and an argument list (or a
  491. function that returns one) to be run on a filesets' files.
  492. The argument <file-name> or <<file-name>> (quoted) will be replaced with
  493. the filename."
  494. :set (function filesets-set-default+)
  495. :type '(repeat :tag "Commands"
  496. (list :tag "Definition" :value ("")
  497. (string "Name")
  498. (choice :tag "Command"
  499. (string :tag "String")
  500. (function :tag "Function"))
  501. (repeat :tag "Argument List"
  502. (choice :tag "Arguments"
  503. (sexp :tag "Sexp"
  504. :value nil)
  505. (string :tag "File Name"
  506. :value "<file-name>")
  507. (string :tag "Quoted File Name"
  508. :value "<<file-name>>")
  509. (function :tag "Function"
  510. :value nil)))))
  511. :group 'filesets)
  512. (put 'filesets-commands 'risky-local-variable t)
  513. (defcustom filesets-external-viewers
  514. (let
  515. ;; ((ps-cmd (or (and (boundp 'my-ps-viewer) my-ps-viewer)
  516. ;; (filesets-select-command "ggv gv")))
  517. ;; (pdf-cmd (or (and (boundp 'my-ps-viewer) my-pdf-viewer)
  518. ;; (filesets-select-command "xpdf acroread")))
  519. ;; (dvi-cmd (or (and (boundp 'my-ps-viewer) my-dvi-viewer)
  520. ;; (filesets-select-command "xdvi tkdvi")))
  521. ;; (doc-cmd (or (and (boundp 'my-ps-viewer) my-doc-viewer)
  522. ;; (filesets-select-command "antiword")))
  523. ;; (pic-cmd (or (and (boundp 'my-ps-viewer) my-pic-viewer)
  524. ;; (filesets-select-command "gqview ee display"))))
  525. ((ps-cmd "ggv")
  526. (pdf-cmd "xpdf")
  527. (dvi-cmd "xdvi")
  528. (doc-cmd "antiword")
  529. (pic-cmd "gqview"))
  530. `(("^.+\\..?html?$" browse-url
  531. ((:ignore-on-open-all t)))
  532. ("^.+\\.pdf$" ,pdf-cmd
  533. ((:ignore-on-open-all t)
  534. (:ignore-on-read-text t)
  535. (:constraint-flag ,pdf-cmd)))
  536. ("^.+\\.e?ps\\(.gz\\)?$" ,ps-cmd
  537. ((:ignore-on-open-all t)
  538. (:ignore-on-read-text t)
  539. (:constraint-flag ,ps-cmd)))
  540. ("^.+\\.dvi$" ,dvi-cmd
  541. ((:ignore-on-open-all t)
  542. (:ignore-on-read-text t)
  543. (:constraint-flag ,dvi-cmd)))
  544. ("^.+\\.doc$" ,doc-cmd
  545. ((:capture-output t)
  546. (:ignore-on-read-text t)
  547. (:constraint-flag ,doc-cmd)))
  548. ("^.+\\.\\(tiff\\|xpm\\|gif\\|pgn\\)$" ,pic-cmd
  549. ((:ignore-on-open-all t)
  550. (:ignore-on-read-text t)
  551. (:constraint-flag ,pic-cmd)))))
  552. "Association list of file patterns and external viewers for use with
  553. `filesets-find-or-display-file'.
  554. Has the form ((FILE-PATTERN VIEWER PROPERTIES) ...), VIEWER being either a
  555. function or a command name as string.
  556. Properties is an association list determining filesets' behavior in
  557. several conditions. Choose one from this list:
  558. :ignore-on-open-all ... Don't open files of this type automatically --
  559. i.e. on open-all-files-events or when running commands
  560. :capture-output ... capture an external viewer output
  561. :constraintp FUNCTION ... use this viewer only if FUNCTION returns non-nil
  562. :constraint-flag SEXP ... use this viewer only if SEXP evaluates to non-nil
  563. :open-hook HOOK ... run hooks after spawning the viewer -- mainly useful
  564. in conjunction with :capture-output
  565. :args (FORMAT-STRING or SYMBOL or FUNCTION) ... a list of arguments
  566. \(defaults to (list \"%S\")) when using shell commands
  567. Avoid modifying this variable and achieve minor speed-ups by setting the
  568. variables my-ps-viewer, my-pdf-viewer, my-dvi-viewer, my-pic-viewer.
  569. In order to view pdf or rtf files in an Emacs buffer, you could use these:
  570. \(\"^.+\\\\.pdf\\\\'\" \"pdftotext\"
  571. \((:capture-output t)
  572. \(:args (\"%S - | fmt -w \" window-width))
  573. \(:ignore-on-read-text t)
  574. \(:constraintp (lambda ()
  575. \(and \(filesets-which-command-p \"pdftotext\")
  576. \(filesets-which-command-p \"fmt\"))))))
  577. \(\"^.+\\\\.rtf\\\\'\" \"rtf2htm\"
  578. \((:capture-output t)
  579. \(:args (\"%S 2> /dev/null | w3m -dump -T text/html\"))
  580. \(:ignore-on-read-text t)
  581. \(:constraintp (lambda ()
  582. \(and (filesets-which-command-p \"rtf2htm\")
  583. \(filesets-which-command-p \"w3m\"))))))"
  584. :set (function filesets-set-default)
  585. :type '(repeat :tag "Viewer"
  586. (list :tag "Definition"
  587. :value ("^.+\\.suffix$" "")
  588. (regexp :tag "Pattern")
  589. (choice :tag "Viewer"
  590. (symbol :tag "Function" :value nil)
  591. (string :tag "Program" :value ""))
  592. (repeat :tag "Properties"
  593. (choice
  594. (list :tag ":constraintp"
  595. :value (:constraintp)
  596. (const :format ""
  597. :value :constraintp)
  598. (function :tag "Function"))
  599. (list :tag ":constraint-flag"
  600. :value (:constraint-flag)
  601. (const :format ""
  602. :value :constraint-flag)
  603. (sexp :tag "Symbol"))
  604. (list :tag ":ignore-on-open-all"
  605. :value (:ignore-on-open-all t)
  606. (const :format ""
  607. :value :ignore-on-open-all)
  608. (boolean :tag "Boolean"))
  609. (list :tag ":ignore-on-read-text"
  610. :value (:ignore-on-read-text t)
  611. (const :format ""
  612. :value :ignore-on-read-text)
  613. (boolean :tag "Boolean"))
  614. (list :tag ":args"
  615. :value (:args)
  616. (const :format ""
  617. :value :args)
  618. (repeat :tag "List"
  619. (choice :tag "Arguments"
  620. (string :tag "String"
  621. :value "")
  622. (symbol :tag "Symbol"
  623. :value nil)
  624. (function :tag "Function"
  625. :value nil))))
  626. (list :tag ":open-hook"
  627. :value (:open-hook)
  628. (const :format ""
  629. :value :open-hook)
  630. (hook :tag "Hook"))
  631. ; (list :tag ":close-hook"
  632. ; :value (:close-hook)
  633. ; (const :format ""
  634. ; :value :close-hook)
  635. ; (hook :tag "Hook"))
  636. (list :tag ":capture-output"
  637. :value (:capture-output t)
  638. (const :format ""
  639. :value :capture-output)
  640. (boolean :tag "Boolean"))))))
  641. :group 'filesets)
  642. (put 'filesets-external-viewers 'risky-local-variable t)
  643. (defcustom filesets-ingroup-patterns
  644. '(("^.+\\.tex$" t
  645. (((:name "Package")
  646. (:pattern "\\\\usepackage\\W*\\(\\[[^\]]*\\]\\W*\\)?{\\W*\\(.+\\)\\W*}")
  647. (:match-number 2)
  648. (:stub-flag t)
  649. (:get-file-name (lambda (master file)
  650. (filesets-which-file master
  651. (concat file ".sty")
  652. (filesets-convert-path-list
  653. (or (getenv "MY_TEXINPUTS")
  654. (getenv "TEXINPUTS")))))))
  655. ((:name "Include")
  656. (:pattern "\\\\include\\W*{\\W*\\(.+\\)\\W*}")
  657. (:get-file-name (lambda (master file)
  658. (filesets-which-file master
  659. (concat file ".tex")
  660. (filesets-convert-path-list
  661. (or (getenv "MY_TEXINPUTS")
  662. (getenv "TEXINPUTS"))))))
  663. (:scan-depth 5))
  664. ((:name "Input")
  665. (:pattern "\\\\input\\W*{\\W*\\(.+\\)\\W*}")
  666. (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b))))
  667. (:get-file-name (lambda (master file)
  668. (filesets-which-file master
  669. (concat file ".tex")
  670. (filesets-convert-path-list
  671. (or (getenv "MY_TEXINPUTS")
  672. (getenv "TEXINPUTS"))))))
  673. (:scan-depth 5))
  674. ((:name "Bibliography")
  675. (:pattern "\\\\bibliography\\W*{\\W*\\(.+\\)\\W*}")
  676. (:get-file-name (lambda (master file)
  677. (filesets-which-file master
  678. (concat file ".bib")
  679. (filesets-convert-path-list
  680. (or (getenv "MY_BIBINPUTS")
  681. (getenv "BIBINPUTS")))))))))
  682. ("^.+\\.el$" t
  683. (((:name "Require")
  684. (:pattern "(require\\W+'\\(.+\\))")
  685. (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b))))
  686. (:get-file-name (lambda (master file)
  687. (filesets-which-file master
  688. (concat file ".el")
  689. load-path))))
  690. ((:name "Load")
  691. (:pattern "(load\\(-library\\)?\\W+\"\\(.+\\)\")")
  692. (:match-number 2)
  693. (:get-file-name (lambda (master file)
  694. (filesets-which-file master file load-path))))))
  695. ("^\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)$" t
  696. (((:pattern "\\<\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)\\>")
  697. (:scan-depth 5)
  698. (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b))))
  699. (:case-sensitive t)
  700. (:get-file-name (lambda (master file)
  701. (filesets-which-file
  702. master
  703. file
  704. (if (boundp 'emacs-wiki-directories)
  705. emacs-wiki-directories
  706. nil))))))))
  707. "Inclusion group definitions.
  708. Define how to find included file according to a file's mode (being
  709. defined by a file pattern).
  710. A valid entry has the form (FILE-PATTERN REMOVE-DUPLICATES-FLAG
  711. CMD-DEF1 ...), CMD-DEF1 being a plist containing the fields :pattern
  712. \(mandatory), :name, :get-file-name, :match-number, :scan-depth,
  713. :preprocess, :case-sensitive.
  714. File Pattern ... A regexp matching the file's name for which the
  715. following rules should be applied.
  716. Remove Duplicates ... If t, only the first occurrence of an included
  717. file is retained. (See below for a full explanation.)
  718. :name STRING ... This pattern's name.
  719. :pattern REGEXP ... A regexp matching the command. This regexp has to
  720. include a group that holds the name of the included file.
  721. :get-file-name FUNCTION (default: `filesets-which-file') ... A function
  722. that takes two arguments (the path of the master file and the name
  723. of the included file) and returns a valid path or nil -- if the
  724. subfile can't be found.
  725. :match-number INTEGER (default: 1) ... The number of the match/group
  726. in the pattern holding the subfile's name. 0 refers the whole
  727. match, 1 to the first group.
  728. :stubp FUNCTION ... If (FUNCTION MASTER INCLUDED-FILE) returns non-nil,
  729. INCLUDED-FILE is a stub -- see below.
  730. :stub-flag ... Files of this type are stubs -- see below.
  731. :scan-depth INTEGER (default: 0) ... Whether included files should be
  732. rescanned. Set this to 0 to disable re-scanning of included file.
  733. :preprocess FUNCTION ... A function modifying a buffer holding the
  734. master file so that pattern matching becomes easier. This is usually
  735. used to narrow a buffer to the relevant region. This function could also
  736. be destructive and simply delete non-relevant text.
  737. :case-sensitive BOOLEAN (default: nil) ... Whether a pattern is
  738. case-sensitive or not.
  739. Stubs:
  740. First, a stub is a file that shows up in the menu but will not be
  741. included in an ingroup's file listing -- i.e. filesets will never
  742. operate on this file automatically. Secondly, in opposition to normal
  743. files stubs are not scanned for new inclusion groups. This is useful if
  744. you want to have quick access to library headers.
  745. In the menu, an asterisk is appended to the stub's name.
  746. Remove Duplicates:
  747. E.g. File A and file B refer to file X; X refers to A. If
  748. you choose not to remove duplicates the tree would look like:
  749. M + A - X - A ...
  750. B - X - A ...
  751. As you can see, there is some chance that you run in circles.
  752. Nevertheless, up to some degree this could still be what you want.
  753. With duplicates removed, it would be:
  754. M + A - X
  755. B"
  756. :set (function filesets-set-default)
  757. :type '(repeat
  758. :tag "Include"
  759. (list
  760. :tag "Definition" :value ("^.+\\.suffix$" t)
  761. (regexp :tag "File Pattern" :value "^.+\\.suffix$")
  762. (boolean :tag "Remove Duplicates" :value t)
  763. (repeat :tag "Commands"
  764. (repeat :tag "Command"
  765. (choice
  766. :tag "Definition"
  767. (list :tag ":name"
  768. :value (:name "")
  769. (const :format "" :value :name)
  770. (string :tag "String"))
  771. (list :tag ":pattern"
  772. :value (:pattern "\\<CMD\\W*\\(.+\\)\\>")
  773. (const :format "" :value :pattern)
  774. (regexp :tag "RegExp"))
  775. (list :tag ":get-file-name"
  776. :value (:get-file-name)
  777. (const :format "" :value :get-file-name)
  778. (function :tag "Function"))
  779. (list :tag ":match-number"
  780. :value (:match-number 1)
  781. (const :format "" :value :match-number)
  782. (integer :tag "Integer"))
  783. (list :tag ":stub-flag"
  784. :value (:stub-flag t)
  785. (const :format "" :value :stub-flag)
  786. (boolean :tag "Boolean"))
  787. (list :tag ":stubp"
  788. :value (:stubp)
  789. (const :format "" :value :stubp)
  790. (function :tag "Function"))
  791. (list :tag ":scan-depth"
  792. :value (:scan-depth 0)
  793. (const :format "" :value :scan-depth)
  794. (integer :tag "Integer"))
  795. (list :tag ":case-sensitive"
  796. :value (:case-sensitive)
  797. (const :format "" :value :case-sensitive)
  798. (boolean :tag "Boolean"))
  799. (list :tag ":preprocess"
  800. :value (:preprocess)
  801. (const :format "" :value :preprocess)
  802. (function :tag "Function")))))))
  803. :group 'filesets)
  804. (put 'filesets-ingroup-patterns 'risky-local-variable t)
  805. (defcustom filesets-data nil
  806. "Fileset definitions.
  807. A fileset is either a list of files, a file pattern, a base directory
  808. and a search pattern (for files), or a base file. Changes to this
  809. variable will take effect after rebuilding the menu.
  810. Caveat: Fileset names have to be unique.
  811. Example definition:
  812. '\(\(\"My Wiki\"
  813. \(:ingroup \"~/Etc/My-Wiki/WikiContents\"))
  814. \(\"My Homepage\"
  815. \(:pattern \"~/public_html/\" \"^.+\\\\.html$\")
  816. \(:open filesets-find-file))
  817. \(\"User Configuration\"
  818. \(:files \"~/.xinitrc\"
  819. \"~/.bashrc\"
  820. \"~/.bash_profile\"))
  821. \(\"HOME\"
  822. \(:tree \"~\" \"^[^.].*[^~]$\")
  823. \(:filter-dirs-flag t)))
  824. `filesets-data' is a list of (NAME-AS-STRING . DEFINITION), DEFINITION
  825. being an association list with the fields:
  826. :files FILE-1 .. FILE-N ... a list of files belonging to a fileset
  827. :ingroup FILE-NAME ... an inclusion group's base file.
  828. :tree ROOT-DIR PATTERN ... a base directory and a file pattern
  829. :pattern DIR PATTERN ... a base directory and a regexp matching
  830. files in that directory. Usually,
  831. PATTERN has the form `^REGEXP$'. Unlike
  832. :tree, this form does not descend
  833. recursively into subdirectories.
  834. :filter-dirs-flag BOOLEAN ... is only used in conjunction with :tree.
  835. :tree-max-level INTEGER ... recurse into directories this many levels
  836. \(see `filesets-tree-max-level' for a full explanation)
  837. :dormant-flag BOOLEAN ... non-nil means don't show this item in the
  838. menu; dormant filesets can still be manipulated via commands available
  839. from the minibuffer -- e.g. `filesets-open', `filesets-close', or
  840. `filesets-run-cmd'
  841. :dormant-p FUNCTION ... a function returning :dormant-flag
  842. :open FUNCTION ... the function used to open file belonging to this
  843. fileset. The function takes a file name as argument
  844. :save FUNCTION ... the function used to save file belonging to this
  845. fileset; it takes no arguments, but works on the current buffer.
  846. Either :files, :pattern, :tree, or :ingroup must be supplied. :files
  847. overrules :tree, :tree overrules :pattern, :pattern overrules :ingroup,
  848. i.e. these tags are mutually exclusive. The fields :open and :save are
  849. optional.
  850. In conjunction with the :tree tag, :save is void. :open refers to the
  851. function used for opening files in a directory, not for opening the
  852. directory. For browsing directories, `filesets-browse-dir-function' is used.
  853. Before using :ingroup, make sure that the file type is already
  854. defined in `filesets-ingroup-patterns'."
  855. :group 'filesets
  856. :set (function filesets-data-set-default)
  857. :type '(repeat
  858. (cons :tag "Fileset"
  859. (string :tag "Name" :value "")
  860. (repeat :tag "Data"
  861. (choice
  862. :tag "Type" :value nil
  863. (list :tag "Pattern"
  864. :value (:pattern "~/" "^.+\\.suffix$")
  865. (const :format "" :value :pattern)
  866. (directory :tag "Dir")
  867. (regexp :tag "Pattern"))
  868. (cons :tag "Files"
  869. :value (:files)
  870. (const :format "" :value :files)
  871. (repeat :tag "Files" file))
  872. (list :tag "Single File"
  873. :value (:file "~/")
  874. (const :format "" :value :file)
  875. (file :tag "File"))
  876. (list :tag "Inclusion group"
  877. :value (:ingroup "~/")
  878. (const :format "" :value :ingroup)
  879. (file :tag "File" :value "~/"))
  880. (list :tag "Directory Tree"
  881. :value (:tree "~/" "^.+\\.suffix$")
  882. (const :format "" :value :tree)
  883. (directory :tag "Dir")
  884. (regexp :tag "Pattern"))
  885. (list :tag "Filter directories"
  886. :value (:filter-dirs-flag)
  887. (const :format "" :value :filter-dirs-flag)
  888. (boolean :tag "Boolean" :value nil))
  889. (list :tag "Scanning depth"
  890. :value (:tree-max-level 3)
  891. (const :format "" :value :tree-max-level)
  892. (integer :tag "Integer"))
  893. (list :tag "Verbosity"
  894. :value (:verbosity 1)
  895. (const :format "" :value :verbosity)
  896. (integer :tag "Integer"))
  897. (list :tag "Conceal fileset (Flag)"
  898. :value (:dormant-flag)
  899. (const :format "" :value :dormant-flag)
  900. (boolean :tag "Boolean"))
  901. (list :tag "Conceal fileset (Function)"
  902. :value (:dormant-p)
  903. (const :format "" :value :dormant-p)
  904. (function :tag "Function"))
  905. (list :tag "Save function"
  906. :value (:save)
  907. (const :format "" :value :save)
  908. (function :tag "Function"))
  909. (list :tag "Open function"
  910. :value (:open)
  911. (const :format "" :value :open)
  912. (function :tag "Function")))))))
  913. (put 'filesets-data 'risky-local-variable t)
  914. (defcustom filesets-query-user-limit 15
  915. "Query the user before opening a fileset with that many files."
  916. :set (function filesets-set-default)
  917. :type 'integer
  918. :group 'filesets)
  919. ;;; Emacs compatibility
  920. (eval-and-compile
  921. (if (featurep 'xemacs)
  922. (fset 'filesets-error 'error)
  923. (require 'easymenu)
  924. (defun filesets-error (_class &rest args)
  925. "`error' wrapper."
  926. (error "%s" (mapconcat 'identity args " ")))
  927. ))
  928. (defun filesets-filter-dir-names (lst &optional negative)
  929. "Remove non-directory names from a list of strings.
  930. If NEGATIVE is non-nil, remove all directory names."
  931. (filesets-filter-list lst
  932. (lambda (x)
  933. (and (not (string-match-p "^\\.+/$" x))
  934. (if negative
  935. (not (string-match-p "[:/\\]$" x))
  936. (string-match-p "[:/\\]$" x))))))
  937. (defun filesets-conditional-sort (lst &optional access-fn)
  938. "Return a sorted copy of LST, LST being a list of strings.
  939. If `filesets-sort-menu-flag' is nil, return LST itself.
  940. ACCESS-FN ... function to get the string value of LST's elements."
  941. (if filesets-sort-menu-flag
  942. (let* ((fni (or access-fn
  943. (function identity)))
  944. (fn (if filesets-sort-case-sensitive-flag
  945. (lambda (a b)
  946. (string< (funcall fni a)
  947. (funcall fni b)))
  948. (lambda (a b)
  949. (string< (upcase (funcall fni a))
  950. (upcase (funcall fni b)))))))
  951. (sort (copy-sequence lst) fn))
  952. lst))
  953. (defun filesets-directory-files (dir &optional
  954. pattern what full-flag match-dirs-flag)
  955. "Get WHAT (:files or :dirs) in DIR.
  956. If PATTERN is provided return only those entries matching this
  957. regular expression.
  958. If MATCH-DIRS-FLAG is non-nil, also match directory entries.
  959. Return full path if FULL-FLAG is non-nil."
  960. (filesets-message 2 "Filesets: scanning %S" dir)
  961. (cond
  962. ((file-exists-p dir)
  963. (let ((files nil)
  964. (dirs nil))
  965. (dolist (this (file-name-all-completions "" dir))
  966. (cond
  967. ((string-match-p "^\\.+/$" this)
  968. nil)
  969. ((string-match-p "[:/\\]$" this)
  970. (when (or (not match-dirs-flag)
  971. (not pattern)
  972. (string-match-p pattern this))
  973. (filesets-message 5 "Filesets: matched dir %S with pattern %S"
  974. this pattern)
  975. (setq dirs (cons this dirs))))
  976. (t
  977. (when (or (not pattern)
  978. (string-match-p pattern this))
  979. (filesets-message 5 "Filesets: matched file %S with pattern %S"
  980. this pattern)
  981. (setq files (cons (if full-flag
  982. (concat (file-name-as-directory dir) this)
  983. this)
  984. files))))))
  985. (cond
  986. ((equal what ':dirs)
  987. (filesets-conditional-sort dirs))
  988. ((equal what ':files)
  989. (filesets-conditional-sort files))
  990. (t
  991. (append (filesets-conditional-sort files)
  992. (filesets-conditional-sort dirs))))))
  993. (filesets-be-docile-flag
  994. (filesets-message 1 "Filesets: %S doesn't exist" dir)
  995. nil)
  996. (t
  997. (filesets-error 'error "Filesets: " dir " does not exist"))))
  998. (defun filesets-quote (txt)
  999. "Return TXT in quotes."
  1000. (concat "\"" txt "\""))
  1001. (defun filesets-get-selection ()
  1002. "Get the text between mark and point -- i.e. the selection or region."
  1003. (let ((m (mark))
  1004. (p (point)))
  1005. (if m
  1006. (buffer-substring (min m p) (max m p))
  1007. (filesets-error 'error "No selection."))))
  1008. (defun filesets-get-quoted-selection ()
  1009. "Return the currently selected text in quotes."
  1010. (filesets-quote (filesets-get-selection)))
  1011. (defun filesets-get-shortcut (n)
  1012. "Create menu shortcuts based on number N."
  1013. (let ((n (mod (- n 1) 51)))
  1014. (cond
  1015. ((not filesets-menu-shortcuts-flag)
  1016. "")
  1017. ((<= n 9)
  1018. (concat (number-to-string n) " "))
  1019. ((<= n 35)
  1020. (format "%c " (+ 87 n)))
  1021. ((<= n 51)
  1022. (format "%c " (+ -3 n))))))
  1023. (defun filesets-files-equalp (a b)
  1024. "Compare two filenames A and B after expansion."
  1025. (equal (expand-file-name a) (expand-file-name b)))
  1026. (defun filesets-files-in-same-directory-p (a b)
  1027. "Compare two filenames A and B after expansion."
  1028. (let ((ad (file-name-directory (expand-file-name a)))
  1029. (bd (file-name-directory (expand-file-name b))))
  1030. (equal ad bd)))
  1031. (defun filesets-convert-path-list (string)
  1032. "Return a path-list given as STRING as list."
  1033. (if string
  1034. (mapcar (lambda (x) (file-name-as-directory x))
  1035. (split-string string path-separator))
  1036. nil))
  1037. (defun filesets-which-file (master filename &optional path-list)
  1038. "Search for a FILENAME relative to a MASTER file in PATH-LIST."
  1039. (let ((f (concat (file-name-directory master)
  1040. filename)))
  1041. (if (file-exists-p f)
  1042. f
  1043. (filesets-some
  1044. (lambda (dir)
  1045. (let ((dir (file-name-as-directory dir))
  1046. (files (if (file-exists-p dir)
  1047. (filesets-directory-files dir nil ':files)
  1048. nil)))
  1049. (filesets-some (lambda (file)
  1050. (if (equal filename (file-name-nondirectory file))
  1051. (concat dir file)
  1052. nil))
  1053. files)))
  1054. path-list))))
  1055. (defun filesets-eviewer-get-props (entry)
  1056. "Get ENTRY's (representing an external viewer) properties."
  1057. (nth 2 entry))
  1058. (defun filesets-eviewer-constraint-p (entry)
  1059. (let* ((props (filesets-eviewer-get-props entry))
  1060. (constraint (assoc ':constraintp props))
  1061. (constraint-flag (assoc ':constraint-flag props)))
  1062. (cond
  1063. (constraint
  1064. (funcall (cadr constraint)))
  1065. (constraint-flag
  1066. (eval (cadr constraint-flag)))
  1067. (t
  1068. t))))
  1069. (defun filesets-get-external-viewer (file)
  1070. "Find an external viewer for FILE."
  1071. (let ((filename (file-name-nondirectory file)))
  1072. (filesets-some
  1073. (lambda (entry)
  1074. (when (and (string-match-p (nth 0 entry) filename)
  1075. (filesets-eviewer-constraint-p entry))
  1076. entry))
  1077. filesets-external-viewers)))
  1078. (defun filesets-get-external-viewer-by-name (name)
  1079. "Get the external viewer definition called NAME."
  1080. (when name
  1081. (filesets-some
  1082. (lambda (entry)
  1083. (when (and (string-equal (nth 1 entry) name)
  1084. (filesets-eviewer-constraint-p entry))
  1085. entry))
  1086. filesets-external-viewers)))
  1087. (defun filesets-filetype-property (filename event &optional entry)
  1088. "Return non-nil if a file of a specific type has special flags/tags.
  1089. Events (corresponding tag):
  1090. on-open-all (:ignore-on-open-all) ... Exclude files of this when opening
  1091. a fileset
  1092. on-grep (:ignore-on-read-text) ... Exclude files of this when running
  1093. the \"Grep <<selection>>\" command
  1094. on-capture-output (:capture-output) ... Capture output of an external viewer
  1095. on-ls ... Not used
  1096. on-cmd ... Not used
  1097. on-close-all ... Not used"
  1098. (let ((def (filesets-eviewer-get-props
  1099. (or entry
  1100. (filesets-get-external-viewer filename)))))
  1101. (filesets-alist-get def
  1102. (pcase event
  1103. (`on-open-all ':ignore-on-open-all)
  1104. (`on-grep ':ignore-on-read-text)
  1105. (`on-cmd nil)
  1106. (`on-close-all nil))
  1107. nil t)))
  1108. (defun filesets-filetype-get-prop (property filename &optional entry)
  1109. "Return PROPERTY for filename -- use ENTRY if provided."
  1110. (let ((def (filesets-eviewer-get-props
  1111. (or entry
  1112. (filesets-get-external-viewer filename)))))
  1113. (when def
  1114. (filesets-alist-get def property nil t))))
  1115. (defun filesets-reset-filename-on-change ()
  1116. "Reset a buffer's filename if the buffer is being modified."
  1117. (when filesets-output-buffer-flag
  1118. (set-visited-file-name nil t)))
  1119. (defun filesets-spawn-external-viewer (file &optional ev-entry)
  1120. "Start an external viewer for FILE.
  1121. Use the viewer defined in EV-ENTRY (a valid element of
  1122. `filesets-external-viewers') if provided."
  1123. (let* ((file (expand-file-name file))
  1124. (entry (or ev-entry
  1125. (filesets-get-external-viewer file))))
  1126. (if entry
  1127. (let* ((vwr (cadr entry))
  1128. (co-flag (filesets-filetype-get-prop ':capture-output file entry))
  1129. (oh (filesets-filetype-get-prop ':open-hook file entry))
  1130. (args (let ((fmt (filesets-filetype-get-prop ':args file entry)))
  1131. (if fmt
  1132. (let ((rv ""))
  1133. (dolist (this fmt rv)
  1134. (setq rv (concat rv
  1135. (cond
  1136. ((stringp this)
  1137. (format this file))
  1138. ((and (symbolp this)
  1139. (fboundp this))
  1140. (format "%S" (funcall this)))
  1141. (t
  1142. (format "%S" this)))))))
  1143. (format "%S" file))))
  1144. (output
  1145. (cond
  1146. ((and (functionp vwr) co-flag)
  1147. (funcall vwr file))
  1148. ((functionp vwr)
  1149. (funcall vwr file)
  1150. nil)
  1151. (co-flag
  1152. (shell-command-to-string (format "%s %s" vwr args)))
  1153. (t
  1154. (shell-command (format "%s %s&" vwr args))
  1155. nil))))
  1156. (if co-flag
  1157. (progn
  1158. (switch-to-buffer (format "Filesets: %s %s" vwr file))
  1159. (insert output)
  1160. (make-local-variable 'filesets-output-buffer-flag)
  1161. (setq filesets-output-buffer-flag t)
  1162. (set-visited-file-name file t)
  1163. (when oh
  1164. (run-hooks 'oh))
  1165. (set-buffer-modified-p nil)
  1166. (setq buffer-read-only t)
  1167. (goto-char (point-min)))
  1168. (when oh
  1169. (run-hooks 'oh))))
  1170. (filesets-error 'error
  1171. "Filesets: general error when spawning external viewer"))))
  1172. (defun filesets-find-file (file)
  1173. "Call `find-file' after a possible delay (see `filesets-find-file-delay').
  1174. If `filesets-be-docile-flag' is true, a file, which isn't readable, will
  1175. not be opened."
  1176. ; (sleep-for filesets-find-file-delay)
  1177. (when (or (file-readable-p file)
  1178. (not filesets-be-docile-flag))
  1179. (sit-for filesets-find-file-delay)
  1180. (find-file file)))
  1181. (defun filesets-find-or-display-file (&optional file viewer)
  1182. "Visit FILE using an external VIEWER or open it in an Emacs buffer."
  1183. (interactive)
  1184. (let* ((file (or file
  1185. (read-file-name "Find file: " nil nil viewer)))
  1186. (external-viewer-def (or
  1187. (filesets-get-external-viewer-by-name viewer)
  1188. (filesets-get-external-viewer file))))
  1189. (filesets-message 3 "Filesets: view %S using %s" file external-viewer-def)
  1190. (if external-viewer-def
  1191. (filesets-spawn-external-viewer file external-viewer-def)
  1192. (filesets-find-file file))))
  1193. (defun filesets-find-file-using ()
  1194. "Select a viewer and call `filesets-find-or-display-file'."
  1195. (interactive)
  1196. (let* ((lst (mapcar (lambda (this)
  1197. (let ((a (cadr this)))
  1198. (list (format "%s" a) a)))
  1199. filesets-external-viewers))
  1200. (viewer (completing-read "Using viewer: " lst nil t)))
  1201. (when viewer
  1202. (filesets-find-or-display-file nil (cadr (assoc viewer lst))))))
  1203. (defun filesets-browser-name ()
  1204. "Get the directory browser's name as defined in `filesets-browse-dir-function'."
  1205. (cond
  1206. ((listp filesets-browse-dir-function)
  1207. (car filesets-browse-dir-function))
  1208. (t
  1209. filesets-browse-dir-function)))
  1210. (defun filesets-browse-dir (dir)
  1211. "Browse DIR using `filesets-browse-dir-function'."
  1212. (if (functionp filesets-browse-dir-function)
  1213. (funcall filesets-browse-dir-function dir)
  1214. (let ((name (car filesets-browse-dir-function))
  1215. (args (format (cadr filesets-browse-dir-function) (expand-file-name dir))))
  1216. (with-temp-buffer
  1217. (start-process (concat "Filesets:" name)
  1218. "*Filesets external directory browser*"
  1219. name args)))))
  1220. (defun filesets-get-fileset-name (something)
  1221. "Get SOMETHING's name (Don't ask)."
  1222. (cond
  1223. ((listp something)
  1224. (car something))
  1225. (t
  1226. something)))
  1227. (defun filesets-data-get-name (entry)
  1228. "Access to `filesets-data'. Get the ENTRY's name."
  1229. (car entry))
  1230. (defun filesets-data-get-data (entry)
  1231. "Access to `filesets-data'. Get the ENTRY's data section."
  1232. (cdr entry))
  1233. (defun filesets-alist-get (alist key &optional default carp)
  1234. "Get KEY's value in the association list ALIST.
  1235. Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
  1236. (let ((elt (assoc key alist)))
  1237. (cond
  1238. (elt
  1239. (if carp
  1240. (cadr elt)
  1241. (cdr elt)))
  1242. (default default)
  1243. (t nil))))
  1244. (defun filesets-data-get (entry key &optional default carp)
  1245. "Extract the value for KEY in the data part of fileset ENTRY.
  1246. Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
  1247. (filesets-alist-get (filesets-data-get-data entry) key default carp))
  1248. (defun filesets-data-set (entry key value)
  1249. "Set the VALUE for KEY in the data part of fileset ENTRY."
  1250. (let* ((alist (filesets-data-get-data entry))
  1251. (elt (assoc key alist)))
  1252. (if elt
  1253. (setcdr elt value)
  1254. (setcdr entry (cons (cons key value) alist)))))
  1255. (defun filesets-entry-mode (entry)
  1256. "Return fileset ENTRY's mode: :files, :file, :tree, :pattern, or :ingroup.
  1257. See `filesets-data'."
  1258. (let ((data (filesets-data-get-data entry)))
  1259. (filesets-some
  1260. (lambda (x)
  1261. (if (assoc x data)
  1262. x))
  1263. '(:files :tree :pattern :ingroup :file))))
  1264. (defun filesets-entry-get-open-fn (fileset-name &optional fileset-entry)
  1265. "Get the open-function for FILESET-NAME.
  1266. Use FILESET-ENTRY for finding the open function, if provided."
  1267. (filesets-data-get (or fileset-entry
  1268. (filesets-get-fileset-from-name fileset-name))
  1269. ':open filesets-open-file-function t))
  1270. (defun filesets-entry-get-save-fn (fileset-name &optional fileset-entry)
  1271. "Get the save-function for FILESET-NAME.
  1272. Use FILESET-ENTRY for finding the save function, if provided."
  1273. (filesets-data-get (or fileset-entry
  1274. (filesets-get-fileset-from-name fileset-name))
  1275. ':save filesets-save-buffer-function t))
  1276. (defun filesets-entry-get-files (entry)
  1277. "Get the file list for fileset ENTRY."
  1278. (filesets-data-get entry ':files))
  1279. (defun filesets-entry-set-files (entry data &optional anyways)
  1280. "Set the file list for fileset ENTRY."
  1281. (let ((files (filesets-entry-get-files entry)))
  1282. (if (or anyways files)
  1283. (filesets-data-set entry ':files data))))
  1284. (defun filesets-entry-get-verbosity (entry)
  1285. "Get verbosity level for fileset ENTRY."
  1286. (filesets-data-get entry ':verbosity 1 t))
  1287. (defun filesets-entry-get-file (entry)
  1288. "Get the single file for fileset ENTRY."
  1289. (filesets-data-get entry ':file nil t))
  1290. (defun filesets-entry-get-pattern (entry)
  1291. "Get the base directory + file pattern for fileset ENTRY."
  1292. ; (filesets-data-get entry ':pattern nil t))
  1293. (filesets-data-get entry ':pattern))
  1294. (defun filesets-entry-get-pattern--pattern (list)
  1295. "Get the file pattern for LIST."
  1296. (if (= (length list) 1) ;; for compatibility with filesets < v1.5.5
  1297. (file-name-nondirectory (car list))
  1298. (cadr list)))
  1299. (defun filesets-entry-get-pattern--dir (list)
  1300. "Get a file pattern's base directory for LIST."
  1301. (if (= (length list) 1) ;; for compatibility with filesets < v1.5.5
  1302. (file-name-directory (car list))
  1303. (car list)))
  1304. (defun filesets-entry-get-tree (entry)
  1305. "Get the tree pattern for fileset ENTRY."
  1306. (filesets-data-get entry ':tree))
  1307. (defun filesets-entry-get-dormant-flag (entry)
  1308. "Get dormant flag for fileset ENTRY."
  1309. (let ((fn (filesets-data-get entry ':dormant-p nil t)))
  1310. (if fn
  1311. (funcall fn)
  1312. (filesets-data-get entry ':dormant-flag nil t))))
  1313. (defun filesets-entry-get-filter-dirs-flag (entry)
  1314. "Get filter-dirs-flag for fileset ENTRY."
  1315. (filesets-data-get entry ':filter-dirs-flag nil t))
  1316. (defun filesets-entry-get-tree-max-level (entry)
  1317. "Get maximal tree scanning depth for fileset ENTRY."
  1318. (filesets-data-get entry ':tree-max-level nil t))
  1319. (defun filesets-entry-get-master (entry)
  1320. "Get the base file for fileset ENTRY."
  1321. (filesets-data-get entry ':ingroup nil t))
  1322. (defun filesets-file-open (open-function file-name &optional fileset-name)
  1323. "Open FILE-NAME using OPEN-FUNCTION.
  1324. If OPEN-FUNCTION is nil, its value will be deduced from FILESET-NAME."
  1325. (let ((open-function (or open-function
  1326. (filesets-entry-get-open-fn fileset-name))))
  1327. (if (file-readable-p file-name)
  1328. (funcall open-function file-name)
  1329. (message "Filesets: Couldn't open `%s'" file-name))))
  1330. (defun filesets-file-close (save-function buffer)
  1331. "Close BUFFER.
  1332. First, save the buffer's contents using SAVE-FUNCTION. Then, kill buffer
  1333. if `buffer-modified-p' returns nil.
  1334. SAVE-FUNCTION takes no argument, but works on the current buffer."
  1335. (with-current-buffer buffer
  1336. (if (buffer-modified-p)
  1337. (funcall save-function))
  1338. (if (not (buffer-modified-p))
  1339. (kill-buffer buffer))))
  1340. (defun filesets-get-fileset-from-name (name &optional mode)
  1341. "Get fileset definition for NAME."
  1342. (pcase mode
  1343. ((or `:ingroup `:tree) name)
  1344. (_ (assoc name filesets-data))))
  1345. ;;; commands
  1346. (defun filesets-cmd-get-def (cmd-name)
  1347. "Get `filesets-commands' entry for CMD-NAME."
  1348. (assoc cmd-name filesets-commands))
  1349. (defun filesets-cmd-get-args (cmd-name)
  1350. (let ((args (let ((def (filesets-cmd-get-def cmd-name)))
  1351. (nth 2 def)))
  1352. (rv nil))
  1353. (dolist (this args rv)
  1354. (cond
  1355. ((and (symbolp this) (fboundp this))
  1356. (let ((x (funcall this)))
  1357. (setq rv (append rv (if (listp x) x (list x))))))
  1358. (t
  1359. (setq rv (append rv (list this))))))))
  1360. (defun filesets-cmd-get-fn (cmd-name)
  1361. (let ((def (filesets-cmd-get-def cmd-name)))
  1362. (nth 1 def)))
  1363. (defun filesets-cmd-show-result (cmd output)
  1364. "Show OUTPUT of CMD (a shell command)."
  1365. (pop-to-buffer "*Filesets: Shell Command Output*")
  1366. (with-no-warnings
  1367. (end-of-buffer))
  1368. (insert "*** ")
  1369. (insert cmd)
  1370. (newline)
  1371. (insert output)
  1372. (newline))
  1373. (defun filesets-run-cmd--repl-fn (arg &optional format-fn)
  1374. "Helper function for `filesets-run-cmd'. Apply FORMAT-FN to arg.
  1375. Replace <file-name> or <<file-name>> with filename."
  1376. (funcall format-fn (cond
  1377. ((equal arg "<file-name>")
  1378. (buffer-file-name))
  1379. ((equal arg "<<file-name>>")
  1380. (shell-quote-argument (buffer-file-name)))
  1381. (t
  1382. arg))))
  1383. (defun filesets-run-cmd (&optional cmd-name fileset mode)
  1384. "Run CMD-NAME (see `filesets-commands') on FILESET."
  1385. (interactive)
  1386. (let* ((cmd-name (or cmd-name
  1387. (completing-read "Select command: " filesets-commands
  1388. nil t)))
  1389. (name (or fileset
  1390. (completing-read "Select fileset: " filesets-data nil t))))
  1391. (when (and cmd-name name)
  1392. (let* ((event (if (equal cmd-name "Grep <<selection>>")
  1393. 'on-grep
  1394. 'on-cmd))
  1395. (files (if (and fileset
  1396. (or (equal mode ':ingroup)
  1397. (equal mode ':tree)))
  1398. (filesets-get-filelist fileset mode event)
  1399. (filesets-get-filelist
  1400. (filesets-get-fileset-from-name name)
  1401. mode event))))
  1402. (when files
  1403. (let ((fn (filesets-cmd-get-fn cmd-name))
  1404. (args (filesets-cmd-get-args cmd-name)))
  1405. (if (memq fn '(multi-isearch-files multi-isearch-files-regexp))
  1406. (apply fn args)
  1407. (dolist (this files nil)
  1408. (save-excursion
  1409. (save-restriction
  1410. (let ((buffer (filesets-find-file this)))
  1411. (when buffer
  1412. (goto-char (point-min))
  1413. (progn
  1414. (cond
  1415. ((stringp fn)
  1416. (let* ((args
  1417. (let ((txt ""))
  1418. (dolist (this args txt)
  1419. (setq txt
  1420. (concat txt
  1421. (filesets-run-cmd--repl-fn
  1422. this
  1423. (lambda (this)
  1424. (if (equal txt "") "" " ")
  1425. (format "%s" this))))))))
  1426. (cmd (concat fn " " args)))
  1427. (filesets-cmd-show-result
  1428. cmd (shell-command-to-string cmd))))
  1429. ((symbolp fn)
  1430. (let ((args
  1431. (let ((argl nil))
  1432. (dolist (this args argl)
  1433. (setq argl
  1434. (append argl
  1435. (filesets-run-cmd--repl-fn
  1436. this
  1437. 'list)))))))
  1438. (apply fn args)))))))))))))))))
  1439. (defun filesets-get-cmd-menu ()
  1440. "Create filesets command menu."
  1441. `("+ Commands"
  1442. . ,(mapcar (lambda (this)
  1443. (let ((name (car this)))
  1444. `[,name (filesets-run-cmd ,name)]))
  1445. filesets-commands)))
  1446. ;;; sample commands
  1447. (defun filesets-cmd-query-replace-getargs ()
  1448. "Get arguments for `query-replace' and `query-replace-regexp'."
  1449. (let ((common (query-replace-read-args "Filesets query replace" nil t)))
  1450. (list (nth 0 common) (nth 1 common) t nil (nth 2 common) nil
  1451. multi-query-replace-map)))
  1452. (defun filesets-cmd-query-replace-regexp-getargs ()
  1453. "Get arguments for `query-replace' and `query-replace-regexp'."
  1454. (let ((common (query-replace-read-args "Filesets query replace" t t)))
  1455. (list (nth 0 common) (nth 1 common) t t (nth 2 common) nil
  1456. multi-query-replace-map)))
  1457. (defun filesets-cmd-isearch-getargs ()
  1458. "Get arguments for `multi-isearch-files' and `multi-isearch-files-regexp'."
  1459. (and (boundp 'files) (list files)))
  1460. (defun filesets-cmd-shell-command-getargs ()
  1461. "Get arguments for `filesets-cmd-shell-command'."
  1462. (let* ((arg (read-string "Shell command (%s = file): "
  1463. "%s"
  1464. 'shell-command-history)))
  1465. arg))
  1466. (defun filesets-cmd-shell-command (txt)
  1467. "Wrapper function for `shell-command'."
  1468. (let ((ok (if (buffer-modified-p)
  1469. (let ((ok (y-or-n-p "Save buffer? ")))
  1470. (when ok
  1471. (save-buffer))
  1472. ok)
  1473. t)))
  1474. (when ok
  1475. (let ((cmd (format txt (shell-quote-argument (buffer-file-name)))))
  1476. (message "Filesets: %s" cmd)
  1477. (filesets-cmd-show-result cmd
  1478. (shell-command-to-string cmd))))))
  1479. ;;; body
  1480. (defun filesets-get-filelist (entry &optional mode event)
  1481. "Get all files for fileset ENTRY.
  1482. Assume MODE (see `filesets-entry-mode'), if provided."
  1483. (let* ((mode (or mode
  1484. (filesets-entry-mode entry)))
  1485. (fl (pcase mode
  1486. (:files
  1487. (filesets-entry-get-files entry))
  1488. (:file
  1489. (list (filesets-entry-get-file entry)))
  1490. (:ingroup
  1491. (let ((entry (expand-file-name
  1492. (if (stringp entry)
  1493. entry
  1494. (filesets-entry-get-master entry)))))
  1495. (cons entry (filesets-ingroup-cache-get entry))))
  1496. (:tree
  1497. (let ((dir (nth 0 entry))
  1498. (patt (nth 1 entry)))
  1499. (filesets-directory-files dir patt ':files t)))
  1500. (:pattern
  1501. (let ((dirpatt (filesets-entry-get-pattern entry)))
  1502. (if dirpatt
  1503. (let ((dir (filesets-entry-get-pattern--dir dirpatt))
  1504. (patt (filesets-entry-get-pattern--pattern dirpatt)))
  1505. ;;(filesets-message 3 "Filesets: scanning %s" dirpatt)
  1506. (filesets-directory-files dir patt ':files t))
  1507. ;; (message "Filesets: malformed entry: %s" entry)))))))
  1508. (filesets-error 'error "Filesets: malformed entry: "
  1509. entry)))))))
  1510. (filesets-filter-list fl
  1511. (lambda (file)
  1512. (not (filesets-filetype-property file event))))))
  1513. (defun filesets-open (&optional mode name lookup-name)
  1514. "Open the fileset called NAME.
  1515. Use LOOKUP-NAME for searching additional data if provided."
  1516. (interactive)
  1517. (let* ((name (or name
  1518. (completing-read "Open fileset: " filesets-data nil t)))
  1519. (fileset (filesets-get-fileset-from-name name mode))
  1520. (lookup-fs (if lookup-name
  1521. (filesets-get-fileset-from-name lookup-name)
  1522. fileset))
  1523. (mode (or mode (filesets-entry-mode lookup-fs))))
  1524. (if fileset
  1525. (let* ((files (filesets-get-filelist fileset mode 'on-open-all))
  1526. (n (length files))
  1527. (open-function (filesets-entry-get-open-fn nil lookup-fs)))
  1528. (if (or (<= n filesets-query-user-limit)
  1529. (y-or-n-p (format "Filesets: Open all %d files in %s? "
  1530. n name)))
  1531. (dolist (this files nil)
  1532. (filesets-file-open open-function this))
  1533. (message "Filesets: canceled")))
  1534. (filesets-error 'error "Filesets: Unknown fileset: " name))))
  1535. (defun filesets-close (&optional mode name lookup-name)
  1536. "Close all buffers belonging to the fileset called NAME.
  1537. Use LOOKUP-NAME for deducing the save-function, if provided."
  1538. (interactive)
  1539. (let* ((name (or name
  1540. (completing-read "Close fileset: " filesets-data nil t)))
  1541. (fileset (filesets-get-fileset-from-name name mode))
  1542. (lookup-fs (if lookup-name
  1543. (filesets-get-fileset-from-name lookup-name)
  1544. fileset))
  1545. (mode (or mode (filesets-entry-mode lookup-fs))))
  1546. (if fileset
  1547. (let ((files (filesets-get-filelist fileset mode 'on-close-all))
  1548. (save-function (filesets-entry-get-save-fn nil lookup-fs)))
  1549. (dolist (file-name files nil)
  1550. (let* ((buffer (get-file-buffer file-name)))
  1551. (if buffer
  1552. (filesets-file-close save-function buffer)))))
  1553. ; (message "Filesets: Unknown fileset: `%s'" name))))
  1554. (filesets-error 'error "Filesets: Unknown fileset: " name))))
  1555. (defun filesets-add-buffer (&optional name buffer)
  1556. "Add BUFFER (or current buffer) to the fileset called NAME.
  1557. User will be queried, if no fileset name is provided."
  1558. (interactive)
  1559. (let* ((buffer (or buffer
  1560. (current-buffer)))
  1561. (name (or name
  1562. (completing-read
  1563. (format-message "Add ‘%s’ to fileset: " buffer)
  1564. filesets-data nil)))
  1565. (entry (or (assoc name filesets-data)
  1566. (when (y-or-n-p
  1567. (format "Fileset %s does not exist. Create it? "
  1568. name))
  1569. (progn
  1570. (add-to-list 'filesets-data (list name '(:files)))
  1571. (message
  1572. (substitute-command-keys
  1573. "Fileset %s created. Call `\\[filesets-save-config]' to save.")
  1574. name)
  1575. (car filesets-data))))))
  1576. (if entry
  1577. (let* ((files (filesets-entry-get-files entry))
  1578. (this (buffer-file-name buffer))
  1579. (inlist (filesets-member this files
  1580. :test 'filesets-files-equalp)))
  1581. (cond
  1582. (inlist
  1583. (message "Filesets: ‘%s’ is already in ‘%s’" this name))
  1584. ((and (equal (filesets-entry-mode entry) ':files)
  1585. this)
  1586. (filesets-entry-set-files entry (cons this files) t)
  1587. (filesets-set-config name 'filesets-data filesets-data))
  1588. (t
  1589. (message "Filesets: Can't add ‘%s’ to fileset ‘%s’" this name)))))))
  1590. (defun filesets-remove-buffer (&optional name buffer)
  1591. "Remove BUFFER (or current buffer) to fileset NAME.
  1592. User will be queried, if no fileset name is provided."
  1593. (interactive)
  1594. (let* ((buffer (or buffer
  1595. (current-buffer)))
  1596. (name (or name
  1597. (completing-read
  1598. (format-message "Remove ‘%s’ from fileset: " buffer)
  1599. filesets-data nil t)))
  1600. (entry (assoc name filesets-data)))
  1601. (if entry
  1602. (let* ((files (filesets-entry-get-files entry))
  1603. (this (buffer-file-name buffer))
  1604. (inlist (filesets-member this files
  1605. :test 'filesets-files-equalp)))
  1606. ;;(message "%s %s %s" files this inlist)
  1607. (if (and files this inlist)
  1608. (let ((new (list (cons ':files (delete (car inlist) files)))))
  1609. (setcdr entry new)
  1610. (filesets-set-config name 'filesets-data filesets-data))
  1611. (message "Filesets: Can't remove ‘%s’ from fileset ‘%s’"
  1612. this
  1613. name))))))
  1614. (defun filesets-convert-patterns (name)
  1615. "Change fileset NAME's mode from :pattern to :files."
  1616. (interactive)
  1617. (let ((entry (assoc name filesets-data)))
  1618. (if entry
  1619. (let ((pattern (filesets-entry-get-pattern entry))
  1620. (patfiles (filesets-get-filelist entry ':pattern)))
  1621. (if pattern
  1622. (progn
  1623. (filesets-entry-set-files entry patfiles t)
  1624. (filesets-set-config name 'filesets-data filesets-data)))))))
  1625. (defun filesets-edit ()
  1626. "Customize `filesets-data'."
  1627. (interactive)
  1628. (customize-variable 'filesets-data))
  1629. (defun filesets-customize ()
  1630. "Customize the filesets group."
  1631. (interactive)
  1632. (customize-group 'filesets))
  1633. (defun filesets-info ()
  1634. "Display filesets's version information."
  1635. (interactive)
  1636. (if (y-or-n-p (format "Filesets v%s: visit homepage? " filesets-version))
  1637. (filesets-goto-homepage)))
  1638. (defun filesets-goto-homepage ()
  1639. "Show filesets's homepage."
  1640. (interactive)
  1641. (browse-url filesets-homepage))
  1642. (defun filesets-remake-shortcut (count submenu)
  1643. "Remake a submenu's shortcut when wrapping long menus."
  1644. (let* ((name (concat (filesets-get-shortcut count)
  1645. (substring (elt submenu 0) 2))))
  1646. (if (listp submenu)
  1647. (cons name (cdr submenu))
  1648. (apply 'vector (list name (cdr (append submenu nil)))))))
  1649. ; (vconcat `[,name] (subseq submenu 1)))))
  1650. (defun filesets-wrap-submenu (submenu-body)
  1651. "Split long submenus."
  1652. (let ((bl (length submenu-body)))
  1653. (if (or (= filesets-max-submenu-length 0)
  1654. (<= bl filesets-max-submenu-length))
  1655. submenu-body
  1656. (let* ((result nil)
  1657. (factor (ceiling (/ (float bl)
  1658. filesets-max-submenu-length))))
  1659. (cl-do ((data submenu-body (cdr data))
  1660. (n 1 (+ n 1))
  1661. (count 0 (+ count factor)))
  1662. ((or (> count bl)
  1663. (null data)))
  1664. ;; (let ((sl (subseq submenu-body count
  1665. (let ((sl (filesets-sublist submenu-body count
  1666. (let ((x (+ count factor)))
  1667. (if (>= bl x)
  1668. x
  1669. nil)))))
  1670. (when sl
  1671. (setq result
  1672. (append
  1673. result
  1674. (if (= (length sl) 1)
  1675. (if filesets-menu-shortcuts-flag
  1676. (list (filesets-remake-shortcut n (car sl)))
  1677. sl)
  1678. `((,(concat
  1679. (filesets-get-shortcut n)
  1680. (let ((rv ""))
  1681. (cl-do ((x sl (cdr x)))
  1682. ((null x))
  1683. (let ((y (concat (elt (car x) 0)
  1684. (if (null (cdr x))
  1685. ""
  1686. ", "))))
  1687. (setq rv
  1688. (concat
  1689. rv
  1690. (if filesets-menu-shortcuts-flag
  1691. (substring y 2)
  1692. y)))))
  1693. (if (> (length rv)
  1694. filesets-max-entry-length)
  1695. (concat
  1696. (substring rv 0 filesets-max-entry-length)
  1697. " ...")
  1698. rv)))
  1699. ,@sl))))))))
  1700. result))))
  1701. (defun filesets-get-menu-epilog (something &optional
  1702. mode lookup-name rebuild-flag)
  1703. "Get submenu epilog for SOMETHING (usually a fileset).
  1704. If mode is :tree or :ingroup, SOMETHING is some weird construct and
  1705. LOOKUP-NAME is used as lookup name for retrieving fileset specific settings."
  1706. (pcase mode
  1707. (:tree
  1708. `("---"
  1709. ["Close all files" (filesets-close ',mode ',something ',lookup-name)]
  1710. ["Run Command" (filesets-run-cmd nil ',something ',mode)]
  1711. [,(format "Browse with `%s'" (filesets-browser-name))
  1712. (filesets-browse-dir ',(car something))]
  1713. ,@(when rebuild-flag
  1714. `(["Rebuild this submenu"
  1715. (filesets-rebuild-this-submenu ',lookup-name)]))))
  1716. (:ingroup
  1717. `("---"
  1718. ["Close all files" (filesets-close ',mode ',something ',lookup-name)]
  1719. ["Run Command" (filesets-run-cmd nil ',something ',mode)]
  1720. ,@(when rebuild-flag
  1721. `(["Rebuild this submenu"
  1722. (filesets-rebuild-this-submenu ',lookup-name)]))))
  1723. (:pattern
  1724. `("---"
  1725. ["Close all files" (filesets-close ',mode ',something)]
  1726. ["Run Command" (filesets-run-cmd nil ',something ',mode)]
  1727. [,(format "Browse with `%s'" (filesets-browser-name))
  1728. ,(list 'filesets-browse-dir
  1729. (filesets-entry-get-pattern--dir
  1730. (filesets-entry-get-pattern
  1731. (filesets-get-fileset-from-name something ':pattern))))]
  1732. ; [,(concat (if filesets-menu-shortcuts-flag
  1733. ; (concat "Con" filesets-menu-shortcuts-marker "vert")
  1734. ; "Convert")
  1735. ; " :pattern to :files")
  1736. ; ,(list (function filesets-convert-patterns) something)]
  1737. ,@(when rebuild-flag
  1738. `(["Rebuild this submenu"
  1739. (filesets-rebuild-this-submenu ',lookup-name)]))))
  1740. (:files
  1741. `("---"
  1742. [,(concat "Close all files") (filesets-close ',mode ',something)]
  1743. ["Run Command" (filesets-run-cmd nil ',something ',mode)]
  1744. ["Add current buffer"
  1745. (filesets-add-buffer ',something (current-buffer))]
  1746. ["Remove current buffer"
  1747. (filesets-remove-buffer ',something (current-buffer))]
  1748. ,@(when rebuild-flag
  1749. `(["Rebuild this submenu"
  1750. (filesets-rebuild-this-submenu ',lookup-name)]))))
  1751. (_
  1752. (filesets-error 'error "Filesets: malformed definition of " something))))
  1753. (defun filesets-ingroup-get-data (master pos &optional fun)
  1754. "Access to `filesets-ingroup-patterns'. Extract data section."
  1755. (let ((masterfile (file-name-nondirectory master))
  1756. (fn (or fun (lambda (a b)
  1757. (and (stringp a)
  1758. (stringp b)
  1759. (string-match-p a b))))))
  1760. (filesets-some (lambda (x)
  1761. (if (funcall fn (car x) masterfile)
  1762. (nth pos x)
  1763. nil))
  1764. filesets-ingroup-patterns)))
  1765. (defun filesets-ingroup-get-pattern (master)
  1766. "Access to `filesets-ingroup-patterns'. Extract patterns."
  1767. (filesets-ingroup-get-data master 2))
  1768. (defun filesets-ingroup-get-remdupl-p (master)
  1769. "Access to `filesets-ingroup-patterns'. Extract remove-duplicates-flag."
  1770. (filesets-ingroup-get-data master 1))
  1771. (defun filesets-ingroup-collect-finder (patt case-sensitivep)
  1772. "Helper function for `filesets-ingroup-collect'. Find pattern PATT."
  1773. (let ((cfs case-fold-search)
  1774. (rv (progn
  1775. (setq case-fold-search (not case-sensitivep))
  1776. (re-search-forward patt nil t))))
  1777. (setq case-fold-search cfs)
  1778. rv))
  1779. (defun filesets-ingroup-cache-get (master)
  1780. "Access to `filesets-ingroup-cache'."
  1781. (lax-plist-get filesets-ingroup-cache master))
  1782. (defun filesets-ingroup-cache-put (master file)
  1783. "Access to `filesets-ingroup-cache'."
  1784. (let* ((emaster (expand-file-name master))
  1785. (this (if file
  1786. (cons file (filesets-ingroup-cache-get emaster))
  1787. nil)))
  1788. (setq filesets-ingroup-cache
  1789. (lax-plist-put filesets-ingroup-cache emaster this))))
  1790. (defun filesets-ingroup-collect-files (fs &optional remdupl-flag master depth)
  1791. "Helper function for `filesets-ingroup-collect'. Collect file names."
  1792. (let* ((master (or master
  1793. (filesets-entry-get-master fs)))
  1794. (remdupl-flag (or remdupl-flag
  1795. (filesets-ingroup-get-remdupl-p master))))
  1796. (filesets-ingroup-cache-put master nil)
  1797. (filesets-message 2 "Filesets: parsing %S" master)
  1798. (let ((cmdpatts (filesets-ingroup-get-pattern master))
  1799. (count 0)
  1800. (rv nil))
  1801. (if cmdpatts
  1802. (dolist (this-def cmdpatts rv)
  1803. (let* ((this-patt (filesets-alist-get this-def ':pattern nil t))
  1804. (this-name (filesets-alist-get this-def ':name "" t))
  1805. (this-pp (filesets-alist-get this-def ':preprocess nil t))
  1806. (this-mn (filesets-alist-get this-def ':match-number 1 t))
  1807. (this-sd (or depth
  1808. (filesets-alist-get this-def ':scan-depth 0 t)))
  1809. (this-csp (filesets-alist-get this-def ':case-sensitive nil t))
  1810. (this-fn (filesets-alist-get
  1811. this-def ':get-file-name 'filesets-which-file t))
  1812. (this-stubp (filesets-alist-get this-def ':stubp nil t))
  1813. (this-stub-flag (filesets-alist-get this-def ':stub-flag nil t))
  1814. (flist nil)
  1815. (lst nil))
  1816. (cond
  1817. ((not this-patt)
  1818. (filesets-error 'error "Filesets: malformed :ingroup definition "
  1819. this-def))
  1820. ((< this-sd 0)
  1821. nil)
  1822. (t
  1823. (with-temp-buffer
  1824. (insert-file-contents master)
  1825. (goto-char (point-min))
  1826. (when this-pp
  1827. (funcall this-pp))
  1828. (while (filesets-ingroup-collect-finder this-patt this-csp)
  1829. (let* ((txt (match-string this-mn))
  1830. (f (funcall this-fn master txt)))
  1831. (when (and f
  1832. (not (member f flist))
  1833. (or (not remdupl-flag)
  1834. (not (filesets-member
  1835. f filesets-ingroup-files
  1836. :test 'filesets-files-equalp))))
  1837. (let ((no-stub-flag
  1838. (and (not this-stub-flag)
  1839. (if this-stubp
  1840. (not (funcall this-stubp master f))
  1841. t))))
  1842. (setq count (+ count 1))
  1843. (setq flist (cons f flist))
  1844. (setq filesets-ingroup-files
  1845. (cons f filesets-ingroup-files))
  1846. (when no-stub-flag
  1847. (filesets-ingroup-cache-put master f))
  1848. (setq lst (append lst (list f))))))))
  1849. (when lst
  1850. (setq rv
  1851. (nconc rv
  1852. (mapcar (lambda (this)
  1853. `((,this ,this-name)
  1854. ,@(filesets-ingroup-collect-files
  1855. fs remdupl-flag this
  1856. (- this-sd 1))))
  1857. lst))))))))
  1858. (filesets-message 2 "Filesets: no patterns defined for %S" master)))))
  1859. (defun filesets-ingroup-collect-build-menu (fs flist &optional other-count)
  1860. "Helper function for `filesets-ingroup-collect'. Build the menu.
  1861. FS is a fileset's name. FLIST is a list returned by
  1862. `filesets-ingroup-collect-files'."
  1863. (if (null flist)
  1864. nil
  1865. (let ((count 0)
  1866. (fsn fs)
  1867. (rv nil))
  1868. (dolist (this flist rv)
  1869. (setq count (+ count 1))
  1870. (let* ((def (if (listp this) (car this) (list this "")))
  1871. (files (if (listp this) (cdr this) nil))
  1872. (master (nth 0 def))
  1873. (name (nth 1 def))
  1874. (nm (concat (filesets-get-shortcut (if (or (not other-count) files)
  1875. count other-count))
  1876. (if (or (null name) (equal name ""))
  1877. ""
  1878. (format "%s: " name))
  1879. (file-name-nondirectory master))))
  1880. (setq rv
  1881. (append rv
  1882. (if files
  1883. `((,nm
  1884. [,(concat "Inclusion Group: "
  1885. (file-name-nondirectory master))
  1886. (filesets-open ':ingroup ',master ',fsn)]
  1887. "---"
  1888. [,master (filesets-file-open nil ',master ',fsn)]
  1889. "---"
  1890. ,@(let ((count 0))
  1891. (mapcar
  1892. (lambda (this)
  1893. (setq count (+ count 1))
  1894. (let ((ff (filesets-ingroup-collect-build-menu
  1895. fs (list this) count)))
  1896. (if (= (length ff) 1)
  1897. (car ff)
  1898. ff)))
  1899. files))
  1900. ,@(filesets-get-menu-epilog master ':ingroup fsn)))
  1901. `([,nm (filesets-file-open nil ',master ',fsn)])))))))))
  1902. (defun filesets-ingroup-collect (fs remdupl-flag master)
  1903. "Collect names of included files and build submenu."
  1904. (filesets-ingroup-cache-put master nil)
  1905. (filesets-message 2 "Filesets: parsing %S" master)
  1906. (filesets-ingroup-collect-build-menu
  1907. fs
  1908. (filesets-ingroup-collect-files fs remdupl-flag master)))
  1909. (defun filesets-build-ingroup-submenu (lookup-name master)
  1910. "Build a :ingroup submenu for file MASTER."
  1911. (if (file-readable-p master)
  1912. (let ((remdupl-flag (filesets-ingroup-get-remdupl-p master)))
  1913. (setq filesets-ingroup-files (list master))
  1914. (filesets-ingroup-collect lookup-name remdupl-flag master))
  1915. (if filesets-be-docile-flag
  1916. (progn
  1917. (message "Filesets: can't parse %s" master)
  1918. nil)
  1919. (filesets-error 'error "Filesets: can't parse " master))))
  1920. (defun filesets-build-dir-submenu-now (level depth entry lookup-name dir patt fd
  1921. &optional rebuild-flag)
  1922. "Helper function for `filesets-build-dir-submenu'."
  1923. ;;(filesets-message 3 "Filesets: scanning %s" dir)
  1924. (if (or (= depth 0)
  1925. (< level depth))
  1926. (let* ((dir (file-name-as-directory dir))
  1927. (header `([,(concat "Tree: "
  1928. (if (= level 0)
  1929. dir
  1930. (concat ".../"
  1931. (file-name-as-directory
  1932. (file-name-nondirectory
  1933. (directory-file-name dir))))))
  1934. ,(list (function filesets-open)
  1935. ':tree
  1936. `(quote (,dir ,patt))
  1937. lookup-name)]
  1938. "---"))
  1939. (dirlist (filesets-directory-files dir patt nil nil fd))
  1940. (subdirs (filesets-filter-dir-names dirlist))
  1941. (count 0)
  1942. (dirsmenu (mapcar
  1943. (lambda (x)
  1944. (setq count (+ count 1))
  1945. (let* ((x (file-name-as-directory x))
  1946. (xx (concat dir x))
  1947. (dd (filesets-build-dir-submenu-now
  1948. (+ level 1) depth entry
  1949. lookup-name xx patt fd))
  1950. (nm (concat (filesets-get-shortcut count)
  1951. x)))
  1952. (if dd
  1953. `(,nm ,@dd)
  1954. `[,nm ,(list 'filesets-browse-dir xx)])))
  1955. subdirs))
  1956. (files (filesets-filter-dir-names dirlist t))
  1957. (filesmenu (mapcar (lambda (x)
  1958. (setq count (+ count 1))
  1959. `[,(concat (filesets-get-shortcut count)
  1960. x)
  1961. (filesets-file-open nil
  1962. (quote ,(concat dir x))
  1963. (quote ,lookup-name))])
  1964. files)))
  1965. (append header
  1966. (filesets-wrap-submenu
  1967. (append
  1968. dirsmenu
  1969. filesmenu))
  1970. (filesets-get-menu-epilog `(,dir ,patt) ':tree
  1971. lookup-name rebuild-flag)))
  1972. nil))
  1973. (defun filesets-build-dir-submenu (entry lookup-name dir patt)
  1974. "Build a :tree submenu named LOOKUP-NAME with base directory DIR including
  1975. all files matching PATT for filesets ENTRY."
  1976. (let ((fd (filesets-entry-get-filter-dirs-flag entry))
  1977. (depth (or (filesets-entry-get-tree-max-level entry)
  1978. filesets-tree-max-level)))
  1979. (filesets-build-dir-submenu-now 0 depth entry lookup-name dir patt fd t)))
  1980. (defun filesets-build-submenu (count lookup-name entry)
  1981. "Build submenu for the fileset ENTRY named LOOKUP-NAME.
  1982. Construct a shortcut from COUNT."
  1983. (let ((lookup-name (or lookup-name
  1984. (filesets-data-get-name entry))))
  1985. (message "Filesets: %s" lookup-name)
  1986. (let ((mode (filesets-entry-mode entry))
  1987. (filesets-verbosity (filesets-entry-get-verbosity entry))
  1988. (this-lookup-name (concat (filesets-get-shortcut count)
  1989. lookup-name)))
  1990. (pcase mode
  1991. (:file
  1992. (let* ((file (filesets-entry-get-file entry)))
  1993. `[,this-lookup-name
  1994. (filesets-file-open nil ',file ',lookup-name)]))
  1995. (_
  1996. `(,this-lookup-name
  1997. ,@(pcase mode
  1998. (:pattern
  1999. (let* ((files (filesets-get-filelist entry mode 'on-ls))
  2000. (dirpatt (filesets-entry-get-pattern entry))
  2001. (pattname (apply 'concat (cons "Pattern: " dirpatt)))
  2002. (count 0))
  2003. ;;(filesets-message 3 "Filesets: scanning %S" pattname)
  2004. `([,pattname
  2005. ,(list (function filesets-open) mode lookup-name)]
  2006. "---"
  2007. ,@(filesets-wrap-submenu
  2008. (mapcar
  2009. (lambda (x)
  2010. (setq count (+ count 1))
  2011. `[,(concat (filesets-get-shortcut count)
  2012. (file-name-nondirectory x))
  2013. (filesets-file-open nil ',x ',lookup-name)])
  2014. files))
  2015. ,@(filesets-get-menu-epilog lookup-name mode
  2016. lookup-name t))))
  2017. (:ingroup
  2018. (let* ((master (filesets-entry-get-master entry)))
  2019. ;;(filesets-message 3 "Filesets: parsing %S" master)
  2020. `([,(concat "Inclusion Group: "
  2021. (file-name-nondirectory master))
  2022. (filesets-open ',mode ',master ',lookup-name)]
  2023. "---"
  2024. [,master (filesets-file-open nil ',master ',lookup-name)]
  2025. "---"
  2026. ,@(filesets-wrap-submenu
  2027. (filesets-build-ingroup-submenu lookup-name master))
  2028. ,@(filesets-get-menu-epilog master mode lookup-name t))))
  2029. (:tree
  2030. (let* ((dirpatt (filesets-entry-get-tree entry))
  2031. (dir (car dirpatt))
  2032. (patt (cadr dirpatt)))
  2033. (filesets-build-dir-submenu entry lookup-name dir patt)))
  2034. (:files
  2035. (let ((files (filesets-get-filelist entry mode 'on-open-all))
  2036. (count 0))
  2037. `([,(concat "Files: " lookup-name)
  2038. (filesets-open ',mode ',lookup-name)]
  2039. "---"
  2040. ,@(filesets-wrap-submenu
  2041. (mapcar
  2042. (lambda (x)
  2043. (setq count (+ count 1))
  2044. `[,(concat (filesets-get-shortcut count)
  2045. (file-name-nondirectory x))
  2046. (filesets-file-open nil ',x ',lookup-name)])
  2047. (filesets-conditional-sort
  2048. files
  2049. (function file-name-nondirectory))))
  2050. ,@(filesets-get-menu-epilog lookup-name mode
  2051. lookup-name t)))))))))))
  2052. (defun filesets-remove-from-ubl (&optional buffer)
  2053. "BUFFER or current buffer require update of the filesets menu."
  2054. (let ((b (or buffer
  2055. (current-buffer))))
  2056. (if (member b filesets-updated-buffers)
  2057. (setq filesets-updated-buffers
  2058. (delete b filesets-updated-buffers)))))
  2059. (defun filesets-build-menu-now (from-scratch-flag)
  2060. "Update the filesets menu.
  2061. Build all new if FROM-SCRATCH-FLAG is non-nil. (To really build from the
  2062. bottom up, set `filesets-submenus' to nil, first.)"
  2063. (when (or from-scratch-flag
  2064. filesets-has-changed-flag
  2065. (not filesets-menu-cache))
  2066. (setq filesets-menu-cache nil)
  2067. (setq filesets-has-changed-flag nil)
  2068. (setq filesets-updated-buffers nil)
  2069. (setq filesets-update-cache-file-flag t)
  2070. (cl-do ((data (filesets-conditional-sort filesets-data (function car))
  2071. (cdr data))
  2072. (count 1 (+ count 1)))
  2073. ((null data))
  2074. (let* ((this (car data))
  2075. (name (filesets-data-get-name this))
  2076. (cached (lax-plist-get filesets-submenus name))
  2077. (submenu (or cached
  2078. (filesets-build-submenu count name this))))
  2079. (unless cached
  2080. (setq filesets-submenus
  2081. (lax-plist-put filesets-submenus name submenu)))
  2082. (unless (filesets-entry-get-dormant-flag this)
  2083. (setq filesets-menu-cache
  2084. (append filesets-menu-cache (list submenu))))))
  2085. (when filesets-cache-save-often-flag
  2086. (filesets-menu-cache-file-save-maybe)))
  2087. (let ((cb (current-buffer)))
  2088. (when (not (member cb filesets-updated-buffers))
  2089. (add-submenu
  2090. filesets-menu-path
  2091. `(,filesets-menu-name
  2092. ("# Filesets"
  2093. ["Edit Filesets" filesets-edit]
  2094. ["Save Filesets" filesets-save-config]
  2095. ["Save Menu Cache" filesets-menu-cache-file-save]
  2096. ["Rebuild Menu" filesets-build-menu]
  2097. ["Customize" filesets-customize]
  2098. ["About" filesets-info])
  2099. ,(filesets-get-cmd-menu)
  2100. "---"
  2101. ,@filesets-menu-cache)
  2102. filesets-menu-before
  2103. filesets-menu-in-menu)
  2104. (setq filesets-updated-buffers
  2105. (cons cb filesets-updated-buffers))
  2106. ;; This wipes out other messages in the echo area.
  2107. ;; (message nil)
  2108. ;;(message "Filesets updated: %s" cb)
  2109. )))
  2110. (defun filesets-build-menu-maybe ()
  2111. "Update the filesets menu."
  2112. (interactive)
  2113. (filesets-build-menu-now nil))
  2114. (defun filesets-build-menu ()
  2115. "Force rebuild of the filesets menu."
  2116. (interactive)
  2117. ;(setq filesets-submenus nil)
  2118. (filesets-reset-fileset)
  2119. (filesets-build-menu-now t)
  2120. (filesets-menu-cache-file-save-maybe))
  2121. (defun filesets-rebuild-this-submenu (fileset)
  2122. "Force rebuild of FILESET submenu."
  2123. (filesets-reset-fileset fileset)
  2124. (filesets-build-menu-now t))
  2125. (defun filesets-menu-cache-file-save-maybe (&optional simply-do-it)
  2126. "Write filesets' cache file.
  2127. If SIMPLY-DO-IT is non-nil, the cache file will be written no matter if
  2128. fileset thinks this is necessary or not."
  2129. (when (and (not (equal filesets-menu-cache-file ""))
  2130. (or simply-do-it
  2131. filesets-update-cache-file-flag))
  2132. (when (file-exists-p filesets-menu-cache-file)
  2133. (delete-file filesets-menu-cache-file))
  2134. ;;(message "Filesets: saving menu cache")
  2135. (with-temp-buffer
  2136. (dolist (this filesets-menu-cache-contents)
  2137. (if (get this 'custom-type)
  2138. (progn
  2139. (insert (format "(setq-default %s '%S)" this (eval this)))
  2140. (when filesets-menu-ensure-use-cached
  2141. (newline)
  2142. (insert (format "(setq %s (cons '%s %s))"
  2143. 'filesets-ignore-next-set-default
  2144. this
  2145. 'filesets-ignore-next-set-default))))
  2146. (insert (format "(setq %s '%S)" this (eval this))))
  2147. (newline 2))
  2148. (insert (format "(setq filesets-cache-version %S)" filesets-version))
  2149. (newline 2)
  2150. (when filesets-cache-hostname-flag
  2151. (insert (format "(setq filesets-cache-hostname %S)" (system-name)))
  2152. (newline 2))
  2153. (run-hooks 'filesets-cache-fill-content-hook)
  2154. (write-file filesets-menu-cache-file))
  2155. (setq filesets-has-changed-flag nil)
  2156. (setq filesets-update-cache-file-flag nil)))
  2157. (defun filesets-menu-cache-file-save ()
  2158. "Save filesets' menu cache file."
  2159. (interactive)
  2160. (filesets-menu-cache-file-save-maybe t))
  2161. (defun filesets-update-cleanup ()
  2162. "Rebuild the menu and save the cache file after updating user data."
  2163. (interactive)
  2164. (message "Filesets v%s: updating menu & cache from version %s"
  2165. filesets-version (or filesets-cache-version "???"))
  2166. (filesets-build-menu)
  2167. (filesets-menu-cache-file-save-maybe)
  2168. (filesets-menu-cache-file-load))
  2169. (defun filesets-update-pre010505 ()
  2170. (let ((msg (format-message
  2171. "Filesets: manual editing of user data required!
  2172. Filesets has detected that you were using an older version before,
  2173. which requires some manual updating. Type ‘y’ for editing the startup
  2174. file now.
  2175. The layout of ‘filesets-data’ has changed. Please delete your cache file
  2176. and edit your startup file as shown below:
  2177. 1. ‘filesets-data’: Edit all :pattern filesets in your startup file and
  2178. transform all entries as shown in this example:
  2179. \(\"Test\" (:pattern \"~/dir/^pattern$\"))
  2180. --> \(\"Test\" (:pattern \"~/dir/\" \"^pattern$\"))
  2181. 2. ‘filesets-data’: Change all occurrences of \":document\" to \":ingroup\":
  2182. \(\(\"Test\" \(:document \"~/dir/file\"))
  2183. --> \(\(\"Test\" \(:ingroup \"~/dir/file\"))
  2184. 3. ‘filesets-subdocument-patterns’: If you already modified the variable
  2185. previously called ‘filesets-subdocument-patterns’, change its name to
  2186. ‘filesets-ingroup-patterns’.
  2187. 4. ‘filesets-menu-cache-contents’: If you already modified this
  2188. variable, change the entry ‘filesets-subdocument--cache’ to
  2189. ‘filesets-ingroup-cache’.
  2190. 5. Type M-x filesets-update-cleanup and restart Emacs.
  2191. We apologize for the inconvenience.")))
  2192. (let* ((cf (or custom-file user-init-file)))
  2193. (switch-to-buffer-other-frame "*Filesets update*")
  2194. (insert msg)
  2195. (when (y-or-n-p (format "Edit startup (%s) file now? " cf))
  2196. (find-file-other-window cf))
  2197. (filesets-error 'error msg))))
  2198. (defun filesets-update (cached-version)
  2199. "Do some cleanup after updating filesets.el."
  2200. (cond
  2201. ((or (not cached-version)
  2202. (string< cached-version "1.5.5")
  2203. (boundp 'filesets-subdocument-patterns))
  2204. (filesets-update-pre010505)))
  2205. (filesets-update-cleanup))
  2206. (defun filesets-menu-cache-file-load ()
  2207. "Load filesets' menu cache file."
  2208. (cond
  2209. ((and (not (equal filesets-menu-cache-file ""))
  2210. (file-readable-p filesets-menu-cache-file))
  2211. (load-file filesets-menu-cache-file)
  2212. (if (and (equal filesets-cache-version filesets-version)
  2213. (if filesets-cache-hostname-flag
  2214. (equal filesets-cache-hostname (system-name))
  2215. t))
  2216. (progn
  2217. (setq filesets-update-cache-file-flag nil)
  2218. t)
  2219. (filesets-update filesets-cache-version)))
  2220. (t
  2221. (setq filesets-update-cache-file-flag t)
  2222. nil)))
  2223. (defun filesets-exit ()
  2224. (filesets-menu-cache-file-save-maybe))
  2225. ;;;###autoload
  2226. (defun filesets-init ()
  2227. "Filesets initialization.
  2228. Set up hooks, load the cache file -- if existing -- and build the menu."
  2229. (add-hook (if (featurep 'xemacs) 'activate-menubar-hook 'menu-bar-update-hook)
  2230. (function filesets-build-menu-maybe))
  2231. (add-hook 'kill-buffer-hook (function filesets-remove-from-ubl))
  2232. (add-hook 'first-change-hook (function filesets-reset-filename-on-change))
  2233. (add-hook 'kill-emacs-hook (function filesets-exit))
  2234. (if (filesets-menu-cache-file-load)
  2235. (progn
  2236. (filesets-build-menu-maybe)
  2237. ;;Well, normally when we use XEmacs <= 21.4, custom.el is loaded
  2238. ;;after init.el. This more or less ignores the next
  2239. ;;`filesets-data-set-default'
  2240. (if filesets-menu-ensure-use-cached
  2241. (setq filesets-menu-use-cached-flag t)))
  2242. (filesets-build-menu)))
  2243. (provide 'filesets)
  2244. ;; Local Variables:
  2245. ;; sentence-end-double-space:t
  2246. ;; End:
  2247. ;;; filesets.el ends here