filesets.el 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. ;;; filesets.el --- handle group of files
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Thomas Link <sanobast-emacs@yahoo.de>
  4. ;; Maintainer: FSF
  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 .emacs 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
  62. (require 'cl))
  63. ;;; Some variables
  64. (defvar filesets-menu-cache nil
  65. "The whole filesets menu.")
  66. (defvar filesets-cache-version nil
  67. "Filesets' cached version number.")
  68. (defvar filesets-cache-hostname nil
  69. "Filesets' cached system name.")
  70. (defvar filesets-ingroup-cache nil
  71. "A plist containing files and their ingroup data.")
  72. (defvar filesets-ingroup-files nil
  73. "List of files already processed when searching for included files.")
  74. (defvar filesets-has-changed-flag t
  75. "Non-nil means some fileset definition has changed.")
  76. (defvar filesets-submenus nil
  77. "An association list with filesets menu data.")
  78. (defvar filesets-updated-buffers nil
  79. "A list of buffers with updated menu bars.")
  80. (defvar filesets-menu-use-cached-flag nil
  81. "Use cached data. See `filesets-menu-ensure-use-cached' for details.")
  82. (defvar filesets-update-cache-file-flag nil
  83. "Non-nil means the cache needs updating.")
  84. (defvar filesets-ignore-next-set-default nil
  85. "List of custom variables for which the next `set-default' will be ignored.")
  86. (defvar filesets-output-buffer-flag nil
  87. "Non-nil means the current buffer is an output buffer created by filesets.
  88. Is buffer local variable.")
  89. (defvar filesets-verbosity 1
  90. "An integer defining the level of verbosity.
  91. 0 means no messages at all.")
  92. (defvar filesets-menu-ensure-use-cached
  93. (and (featurep 'xemacs)
  94. (if (fboundp 'emacs-version>=)
  95. (not (emacs-version>= 21 5))))
  96. "Make sure (X)Emacs uses filesets' cache.
  97. Well, if you use XEmacs (prior to 21.5?) custom.el is loaded after
  98. init.el. This means that settings saved in the cache file (see
  99. `filesets-menu-cache-file') will be overwritten by custom.el. In order
  100. to ensure the use of the cache file, set this variable to t -- which is
  101. the default for XEmacs prior to 21.5. If you want to change this value
  102. put \"(setq filesets-menu-ensure-use-cached VALUE)\" into your startup
  103. file -- before loading filesets.el.
  104. So, when should you think about setting this value to t? If filesets.el
  105. is loaded before user customizations. Thus, if (require 'filesets)
  106. precedes the `custom-set-variables' command or, for XEmacs, if init.el
  107. is loaded before custom.el, set this variable to t.")
  108. ;;; utils
  109. (defun filesets-filter-list (lst cond-fn)
  110. "Remove all elements not conforming to COND-FN from list LST.
  111. COND-FN takes one argument: the current element."
  112. ; (remove* 'dummy lst :test (lambda (dummy elt)
  113. ; (not (funcall cond-fn elt)))))
  114. (let ((rv nil))
  115. (dolist (elt lst rv)
  116. (when (funcall cond-fn elt)
  117. (setq rv (append rv (list elt)))))))
  118. (defun filesets-ormap (fsom-pred lst)
  119. "Return the tail of LST for the head of which FSOM-PRED is non-nil."
  120. (let ((fsom-lst lst)
  121. (fsom-rv nil))
  122. (while (and (not (null fsom-lst))
  123. (null fsom-rv))
  124. (if (funcall fsom-pred (car fsom-lst))
  125. (setq fsom-rv fsom-lst)
  126. (setq fsom-lst (cdr fsom-lst))))
  127. fsom-rv))
  128. (defun filesets-some (fss-pred fss-lst)
  129. "Return non-nil if FSS-PRED is non-nil for any element of FSS-LST.
  130. Like `some', return the first value of FSS-PRED that is non-nil."
  131. (catch 'exit
  132. (dolist (fss-this fss-lst nil)
  133. (let ((fss-rv (funcall fss-pred fss-this)))
  134. (when fss-rv
  135. (throw 'exit fss-rv))))))
  136. ;(fset 'filesets-some 'some) ;; or use the cl function
  137. (defun filesets-member (fsm-item fsm-lst &rest fsm-keys)
  138. "Find the first occurrence of FSM-ITEM in FSM-LST.
  139. It is supposed to work like cl's `member*'. At the moment only the :test
  140. key is supported."
  141. (let ((fsm-test (or (plist-get fsm-keys ':test)
  142. (function equal))))
  143. (filesets-ormap (lambda (fsm-this)
  144. (funcall fsm-test fsm-item fsm-this))
  145. fsm-lst)))
  146. ;(fset 'filesets-member 'member*) ;; or use the cl function
  147. (defun filesets-sublist (lst beg &optional end)
  148. "Get the sublist of LST from BEG to END - 1."
  149. (let ((rv nil)
  150. (i beg)
  151. (top (or end
  152. (length lst))))
  153. (while (< i top)
  154. (setq rv (append rv (list (nth i lst))))
  155. (setq i (+ i 1)))
  156. rv))
  157. (defun filesets-select-command (cmd-list)
  158. "Select one command from CMD-LIST -- a string with space separated names."
  159. (let ((this (shell-command-to-string
  160. (format "which --skip-alias %s 2> /dev/null | head -n 1"
  161. cmd-list))))
  162. (if (equal this "")
  163. nil
  164. (file-name-nondirectory (substring this 0 (- (length this) 1))))))
  165. (defun filesets-which-command (cmd)
  166. "Call \"which CMD\"."
  167. (shell-command-to-string (format "which %s" cmd)))
  168. (defun filesets-which-command-p (cmd)
  169. "Call \"which CMD\" and return non-nil if the command was found."
  170. (when (string-match (format "\\(/[^/]+\\)?/%s" cmd)
  171. (filesets-which-command cmd))
  172. cmd))
  173. (defun filesets-message (level &rest args)
  174. "Show a message only if LEVEL is greater or equal then `filesets-verbosity'."
  175. (when (<= level (abs filesets-verbosity))
  176. (apply 'message args)))
  177. ;;; config file
  178. (defun filesets-save-config ()
  179. "Save filesets' customizations."
  180. (interactive)
  181. (customize-save-customized))
  182. (defun filesets-reset-fileset (&optional fileset no-cache)
  183. "Reset the cached values for one or all filesets."
  184. (if fileset
  185. (setq filesets-submenus (lax-plist-put filesets-submenus fileset nil))
  186. (setq filesets-submenus nil))
  187. (setq filesets-has-changed-flag t)
  188. (setq filesets-update-cache-file-flag (or filesets-update-cache-file-flag
  189. (not no-cache))))
  190. (defun filesets-set-config (fileset var val)
  191. "Set-default wrapper function."
  192. (filesets-reset-fileset fileset)
  193. (set-default var val))
  194. ; (customize-set-variable var val))
  195. ; (filesets-build-menu))
  196. ;; It seems this is a workaround for the XEmacs issue described in the
  197. ;; doc-string of filesets-menu-ensure-use-cached. Under Emacs this is
  198. ;; essentially just `set-default'.
  199. (defun filesets-set-default (sym val &optional init-flag)
  200. "Set-default wrapper function used in conjunction with `defcustom'.
  201. If SYM is in the list `filesets-ignore-next-set-default', delete
  202. it from that list, and return nil. Otherwise, set the value of
  203. SYM to VAL and return t. If INIT-FLAG is non-nil, set with
  204. `custom-initialize-set', otherwise with `set-default'."
  205. (let ((ignore-flag (member sym filesets-ignore-next-set-default)))
  206. (if ignore-flag
  207. (setq filesets-ignore-next-set-default
  208. (delete sym filesets-ignore-next-set-default))
  209. (if init-flag
  210. (custom-initialize-set sym val)
  211. (set-default sym val)))
  212. (not ignore-flag)))
  213. (defun filesets-set-default! (sym val)
  214. "Call `filesets-set-default' and reset cached data (i.e. rebuild menu)."
  215. (when (filesets-set-default sym val)
  216. (filesets-reset-fileset)))
  217. (defun filesets-set-default+ (sym val)
  218. "Call `filesets-set-default' and reset filesets' standard menu."
  219. (when (filesets-set-default sym val)
  220. (setq filesets-has-changed-flag t)))
  221. ; (filesets-reset-fileset nil t)))
  222. (defvar filesets-data)
  223. (defun filesets-data-set-default (sym val)
  224. "Set the default for `filesets-data'."
  225. (if filesets-menu-use-cached-flag
  226. (setq filesets-menu-use-cached-flag nil)
  227. (when (default-boundp 'filesets-data)
  228. (let ((modified-filesets
  229. (filesets-filter-list val
  230. (lambda (x)
  231. (let ((name (car x))
  232. (data (cdr x)))
  233. (let ((elt (assoc name filesets-data)))
  234. (or (not elt)
  235. (not (equal data (cdr elt))))))))))
  236. (dolist (x modified-filesets)
  237. (filesets-reset-fileset (car x))))))
  238. (filesets-set-default sym val))
  239. ;;; configuration
  240. (defgroup filesets nil
  241. "The fileset swapper."
  242. :prefix "filesets-"
  243. :group 'convenience
  244. :version "22.1")
  245. (defcustom filesets-menu-name "Filesets"
  246. "Filesets' menu name."
  247. :set (function filesets-set-default)
  248. :type 'string
  249. :group 'filesets)
  250. (defcustom filesets-menu-path '("File") ; cf recentf-menu-path
  251. "The menu under which the filesets menu should be inserted.
  252. See `add-submenu' for documentation."
  253. :set (function filesets-set-default)
  254. :type '(choice (const :tag "Top Level" nil)
  255. (sexp :tag "Menu Path"))
  256. :version "23.1" ; was nil
  257. :group 'filesets)
  258. (defcustom filesets-menu-before "Open File..." ; cf recentf-menu-before
  259. "The name of a menu before which this menu should be added.
  260. See `add-submenu' for documentation."
  261. :set (function filesets-set-default)
  262. :type '(choice (string :tag "Name")
  263. (const :tag "Last" nil))
  264. :version "23.1" ; was "File"
  265. :group 'filesets)
  266. (defcustom filesets-menu-in-menu nil
  267. "Use that instead of `current-menubar' as the menu to change.
  268. See `add-submenu' for documentation."
  269. :set (function filesets-set-default)
  270. :type 'sexp
  271. :group 'filesets)
  272. (defcustom filesets-menu-shortcuts-flag t
  273. "Non-nil means to prepend menus with hopefully unique shortcuts."
  274. :set (function filesets-set-default!)
  275. :type 'boolean
  276. :group 'filesets)
  277. (defcustom filesets-menu-shortcuts-marker "%_"
  278. "String for marking menu shortcuts."
  279. :set (function filesets-set-default!)
  280. :type 'string
  281. :group 'filesets)
  282. ;;(defcustom filesets-menu-cnvfp-flag nil
  283. ;; "Non-nil means show \"Convert :pattern to :files\" entry for :pattern menus."
  284. ;; :set (function filesets-set-default!)
  285. ;; :type 'boolean
  286. ;; :group 'filesets)
  287. (defcustom filesets-menu-cache-file
  288. (locate-user-emacs-file "filesets-cache.el")
  289. "File to be used for saving the filesets menu between sessions.
  290. Set this to \"\", to disable caching of menus.
  291. Don't forget to check out `filesets-menu-ensure-use-cached'."
  292. :set (function filesets-set-default)
  293. :type 'file
  294. :group 'filesets)
  295. (put 'filesets-menu-cache-file 'risky-local-variable t)
  296. (defcustom filesets-menu-cache-contents
  297. '(filesets-be-docile-flag
  298. filesets-submenus
  299. filesets-menu-cache
  300. filesets-ingroup-cache)
  301. "Stuff we want to save in `filesets-menu-cache-file'.
  302. Possible uses: don't save configuration data in the main startup files
  303. but in filesets's own cache. In this case add `filesets-data' to this
  304. list.
  305. There is a second reason for putting `filesets-data' on this list. If
  306. you frequently add and remove buffers on the fly to :files filesets, you
  307. don't need to save your customizations if `filesets-data' is being
  308. mirrored in the cache file. In this case the version in the cache file
  309. is the current one, and the version in your startup file will be
  310. silently updated later on.
  311. If you want caching to work properly, at least `filesets-submenus',
  312. `filesets-menu-cache', and `filesets-ingroup-cache' should be in this
  313. list.
  314. Don't forget to check out `filesets-menu-ensure-use-cached'."
  315. :set (function filesets-set-default)
  316. :type '(repeat
  317. (choice :tag "Variable"
  318. (const :tag "filesets-submenus"
  319. :value filesets-submenus)
  320. (const :tag "filesets-menu-cache"
  321. :value filesets-menu-cache)
  322. (const :tag "filesets-ingroup-cache"
  323. :value filesets-ingroup-cache)
  324. (const :tag "filesets-data"
  325. :value filesets-data)
  326. (const :tag "filesets-external-viewers"
  327. :value filesets-external-viewers)
  328. (const :tag "filesets-ingroup-patterns"
  329. :value filesets-ingroup-patterns)
  330. (const :tag "filesets-be-docile-flag"
  331. :value filesets-be-docile-flag)
  332. (sexp :tag "Other" :value nil)))
  333. :group 'filesets)
  334. (defcustom filesets-cache-fill-content-hooks nil
  335. "Hooks to run when writing the contents of filesets' cache file.
  336. The hook is called with the cache file as current buffer and the cursor
  337. at the last position. I.e. each hook has to make sure that the cursor is
  338. at the last position.
  339. Possible uses: If you don't want to save `filesets-data' in your normal
  340. configuration file, you can add a something like this
  341. \(lambda ()
  342. \(insert (format \"(setq-default filesets-data '%S)\"
  343. filesets-data))
  344. \(newline 2))
  345. to this hook.
  346. Don't forget to check out `filesets-menu-ensure-use-cached'."
  347. :set (function filesets-set-default)
  348. :type 'hook
  349. :group 'filesets)
  350. (defcustom filesets-cache-hostname-flag nil
  351. "Non-nil means cache the hostname.
  352. If the current name differs from the cached one,
  353. rebuild the menu and create a new cache file."
  354. :set (function filesets-set-default)
  355. :type 'boolean
  356. :group 'filesets)
  357. (defcustom filesets-cache-save-often-flag nil
  358. "Non-nil means save buffer on every change of the filesets menu.
  359. If this variable is set to nil and if Emacs crashes, the cache and
  360. filesets-data could get out of sync. Set this to t if this happens from
  361. time to time or if the fileset cache causes troubles."
  362. :set (function filesets-set-default)
  363. :type 'boolean
  364. :group 'filesets)
  365. (defcustom filesets-max-submenu-length 25
  366. "Maximum length of submenus.
  367. Set this value to 0 to turn menu splitting off. BTW, parts of submenus
  368. will not be rewrapped if their length exceeds this value."
  369. :set (function filesets-set-default)
  370. :type 'integer
  371. :group 'filesets)
  372. (defcustom filesets-max-entry-length 50
  373. "Truncate names of split submenus to this length."
  374. :set (function filesets-set-default)
  375. :type 'integer
  376. :group 'filesets)
  377. (defcustom filesets-browse-dir-function 'dired
  378. "A function or command used for browsing directories.
  379. When using an external command, \"%s\" will be replaced with the
  380. directory's name.
  381. Note: You have to manually rebuild the menu if you change this value."
  382. :set (function filesets-set-default)
  383. :type '(choice :tag "Function:"
  384. (const :tag "dired"
  385. :value dired)
  386. (list :tag "Command"
  387. :value ("" "%s")
  388. (string :tag "Name")
  389. (string :tag "Arguments"))
  390. (function :tag "Function"
  391. :value nil))
  392. :group 'filesets)
  393. (defcustom filesets-open-file-function 'filesets-find-or-display-file
  394. "The function used for opening files.
  395. `filesets-find-or-display-file' ... Filesets' default function for
  396. visiting files. This function checks if an external viewer is defined
  397. for a specific file type. Either this viewer, if defined, or
  398. `find-file' will be used to visit a file.
  399. `filesets-find-file' ... An alternative function that always uses
  400. `find-file'. If `filesets-be-docile-flag' is true, a file, which isn't
  401. readable, will not be opened.
  402. Caveat: Changes will take effect only after rebuilding the menu."
  403. :set (function filesets-set-default)
  404. :type '(choice :tag "Function:"
  405. (const :tag "filesets-find-or-display-file"
  406. :value filesets-find-or-display-file)
  407. (const :tag "filesets-find-file"
  408. :value filesets-find-file)
  409. (function :tag "Function"
  410. :value nil))
  411. :group 'filesets)
  412. (defcustom filesets-save-buffer-function 'save-buffer
  413. "The function used to save a buffer.
  414. Caveat: Changes will take effect after rebuilding the menu."
  415. :set (function filesets-set-default)
  416. :type '(choice :tag "Function:"
  417. (const :tag "save-buffer"
  418. :value save-buffer)
  419. (function :tag "Function"
  420. :value nil))
  421. :group 'filesets)
  422. (defcustom filesets-find-file-delay
  423. (if (and (featurep 'xemacs) gutter-buffers-tab-visible-p)
  424. 0.5
  425. 0)
  426. "Delay before calling `find-file'.
  427. This is for calls via `filesets-find-or-display-file'
  428. or `filesets-find-file'.
  429. Set this to 0, if you don't use XEmacs's buffer tabs."
  430. :set (function filesets-set-default)
  431. :type 'number
  432. :group 'filesets)
  433. (defcustom filesets-be-docile-flag nil
  434. "Non-nil means don't complain if a file or a directory doesn't exist.
  435. This is useful if you want to use the same startup files in different
  436. computer environments."
  437. :set (function filesets-set-default)
  438. :type 'boolean
  439. :group 'filesets)
  440. (defcustom filesets-sort-menu-flag t
  441. "Non-nil means sort the filesets menu alphabetically."
  442. :set (function filesets-set-default)
  443. :type 'boolean
  444. :group 'filesets)
  445. (defcustom filesets-sort-case-sensitive-flag t
  446. "Non-nil means sorting of the filesets menu is case sensitive."
  447. :set (function filesets-set-default)
  448. :type 'boolean
  449. :group 'filesets)
  450. (defcustom filesets-tree-max-level 3
  451. "Maximum scan depth for directory trees.
  452. A :tree fileset is defined by a base directory the contents of which
  453. will be recursively added to the menu. `filesets-tree-max-level' tells up
  454. to which level the directory structure should be scanned/listed,
  455. i.e. how deep the menu should be. Try something like
  456. \(\"HOME -- only one level\"
  457. \(:tree \"~\" \"^[^.].*[^~]$\")
  458. \(:tree-max-level 1)
  459. \(:filter-dirs-flag t))
  460. \(\"HOME -- up to 3 levels\"
  461. \(:tree \"~\" \"^[^.].*[^~]$\")
  462. \(:tree-max-level 3)
  463. \(:filter-dirs-flag t))
  464. and it should become clear what this option is about. In any case,
  465. including directory trees to the menu can take a lot of memory."
  466. :set (function filesets-set-default)
  467. :type 'integer
  468. :group 'filesets)
  469. (defcustom filesets-commands
  470. `(("Isearch"
  471. multi-isearch-files
  472. (filesets-cmd-isearch-getargs))
  473. ("Isearch (regexp)"
  474. multi-isearch-files-regexp
  475. (filesets-cmd-isearch-getargs))
  476. ("Query Replace"
  477. perform-replace
  478. (filesets-cmd-query-replace-getargs))
  479. ("Query Replace (regexp)"
  480. perform-replace
  481. (filesets-cmd-query-replace-regexp-getargs))
  482. ("Grep <<selection>>"
  483. "grep"
  484. ("-n " filesets-get-quoted-selection " " "<<file-name>>"))
  485. ("Run Shell Command"
  486. filesets-cmd-shell-command
  487. (filesets-cmd-shell-command-getargs)))
  488. "Commands to run on filesets.
  489. An association list of names, functions, and an argument list (or a
  490. function that returns one) to be run on a filesets' files.
  491. The argument <file-name> or <<file-name>> (quoted) will be replaced with
  492. the filename."
  493. :set (function filesets-set-default+)
  494. :type '(repeat :tag "Commands"
  495. (list :tag "Definition" :value ("")
  496. (string "Name")
  497. (choice :tag "Command"
  498. (string :tag "String")
  499. (function :tag "Function"))
  500. (repeat :tag "Argument List"
  501. (choice :tag "Arguments"
  502. (sexp :tag "Sexp"
  503. :value nil)
  504. (string :tag "File Name"
  505. :value "<file-name>")
  506. (string :tag "Quoted File Name"
  507. :value "<<file-name>>")
  508. (function :tag "Function"
  509. :value nil)))))
  510. :group 'filesets)
  511. (put 'filesets-commands 'risky-local-variable t)
  512. (defcustom filesets-external-viewers
  513. (let
  514. ;; ((ps-cmd (or (and (boundp 'my-ps-viewer) my-ps-viewer)
  515. ;; (filesets-select-command "ggv gv")))
  516. ;; (pdf-cmd (or (and (boundp 'my-ps-viewer) my-pdf-viewer)
  517. ;; (filesets-select-command "xpdf acroread")))
  518. ;; (dvi-cmd (or (and (boundp 'my-ps-viewer) my-dvi-viewer)
  519. ;; (filesets-select-command "xdvi tkdvi")))
  520. ;; (doc-cmd (or (and (boundp 'my-ps-viewer) my-doc-viewer)
  521. ;; (filesets-select-command "antiword")))
  522. ;; (pic-cmd (or (and (boundp 'my-ps-viewer) my-pic-viewer)
  523. ;; (filesets-select-command "gqview ee display"))))
  524. ((ps-cmd "ggv")
  525. (pdf-cmd "xpdf")
  526. (dvi-cmd "xdvi")
  527. (doc-cmd "antiword")
  528. (pic-cmd "gqview"))
  529. `(("^.+\\..?html?$" browse-url
  530. ((:ignore-on-open-all t)))
  531. ("^.+\\.pdf$" ,pdf-cmd
  532. ((:ignore-on-open-all t)
  533. (:ignore-on-read-text t)
  534. (:constraint-flag ,pdf-cmd)))
  535. ("^.+\\.e?ps\\(.gz\\)?$" ,ps-cmd
  536. ((:ignore-on-open-all t)
  537. (:ignore-on-read-text t)
  538. (:constraint-flag ,ps-cmd)))
  539. ("^.+\\.dvi$" ,dvi-cmd
  540. ((:ignore-on-open-all t)
  541. (:ignore-on-read-text t)
  542. (:constraint-flag ,dvi-cmd)))
  543. ("^.+\\.doc$" ,doc-cmd
  544. ((:capture-output t)
  545. (:ignore-on-read-text t)
  546. (:constraint-flag ,doc-cmd)))
  547. ("^.+\\.\\(tiff\\|xpm\\|gif\\|pgn\\)$" ,pic-cmd
  548. ((:ignore-on-open-all t)
  549. (:ignore-on-read-text t)
  550. (:constraint-flag ,pic-cmd)))))
  551. "Association list of file patterns and external viewers for use with
  552. `filesets-find-or-display-file'.
  553. Has the form ((FILE-PATTERN VIEWER PROPERTIES) ...), VIEWER being either a
  554. function or a command name as string.
  555. Properties is an association list determining filesets' behavior in
  556. several conditions. Choose one from this list:
  557. :ignore-on-open-all ... Don't open files of this type automatically --
  558. i.e. on open-all-files-events or when running commands
  559. :capture-output ... capture an external viewer output
  560. :constraintp FUNCTION ... use this viewer only if FUNCTION returns non-nil
  561. :constraint-flag SEXP ... use this viewer only if SEXP evaluates to non-nil
  562. :open-hook HOOK ... run hooks after spawning the viewer -- mainly useful
  563. in conjunction with :capture-output
  564. :args (FORMAT-STRING or SYMBOL or FUNCTION) ... a list of arguments
  565. \(defaults to (list \"%S\")) when using shell commands
  566. Avoid modifying this variable and achieve minor speed-ups by setting the
  567. variables my-ps-viewer, my-pdf-viewer, my-dvi-viewer, my-pic-viewer.
  568. In order to view pdf or rtf files in an Emacs buffer, you could use these:
  569. \(\"^.+\\\\.pdf\\\\'\" \"pdftotext\"
  570. \((:capture-output t)
  571. \(:args (\"%S - | fmt -w \" window-width))
  572. \(:ignore-on-read-text t)
  573. \(:constraintp (lambda ()
  574. \(and \(filesets-which-command-p \"pdftotext\")
  575. \(filesets-which-command-p \"fmt\"))))))
  576. \(\"^.+\\\\.rtf\\\\'\" \"rtf2htm\"
  577. \((:capture-output t)
  578. \(:args (\"%S 2> /dev/null | w3m -dump -T text/html\"))
  579. \(:ignore-on-read-text t)
  580. \(:constraintp (lambda ()
  581. \(and (filesets-which-command-p \"rtf2htm\")
  582. \(filesets-which-command-p \"w3m\"))))))"
  583. :set (function filesets-set-default)
  584. :type '(repeat :tag "Viewer"
  585. (list :tag "Definition"
  586. :value ("^.+\\.suffix$" "")
  587. (regexp :tag "Pattern")
  588. (choice :tag "Viewer"
  589. (symbol :tag "Function" :value nil)
  590. (string :tag "Program" :value ""))
  591. (repeat :tag "Properties"
  592. (choice
  593. (list :tag ":constraintp"
  594. :value (:constraintp)
  595. (const :format ""
  596. :value :constraintp)
  597. (function :tag "Function"))
  598. (list :tag ":constraint-flag"
  599. :value (:constraint-flag)
  600. (const :format ""
  601. :value :constraint-flag)
  602. (sexp :tag "Symbol"))
  603. (list :tag ":ignore-on-open-all"
  604. :value (:ignore-on-open-all t)
  605. (const :format ""
  606. :value :ignore-on-open-all)
  607. (boolean :tag "Boolean"))
  608. (list :tag ":ignore-on-read-text"
  609. :value (:ignore-on-read-text t)
  610. (const :format ""
  611. :value :ignore-on-read-text)
  612. (boolean :tag "Boolean"))
  613. (list :tag ":args"
  614. :value (:args)
  615. (const :format ""
  616. :value :args)
  617. (repeat :tag "List"
  618. (choice :tag "Arguments"
  619. (string :tag "String"
  620. :value "")
  621. (symbol :tag "Symbol"
  622. :value nil)
  623. (function :tag "Function"
  624. :value nil))))
  625. (list :tag ":open-hook"
  626. :value (:open-hook)
  627. (const :format ""
  628. :value :open-hook)
  629. (hook :tag "Hook"))
  630. ; (list :tag ":close-hook"
  631. ; :value (:close-hook)
  632. ; (const :format ""
  633. ; :value :close-hook)
  634. ; (hook :tag "Hook"))
  635. (list :tag ":capture-output"
  636. :value (:capture-output t)
  637. (const :format ""
  638. :value :capture-output)
  639. (boolean :tag "Boolean"))))))
  640. :group 'filesets)
  641. (put 'filesets-external-viewers 'risky-local-variable t)
  642. (defcustom filesets-ingroup-patterns
  643. '(("^.+\\.tex$" t
  644. (((:name "Package")
  645. (:pattern "\\\\usepackage\\W*\\(\\[[^\]]*\\]\\W*\\)?{\\W*\\(.+\\)\\W*}")
  646. (:match-number 2)
  647. (:stub-flag t)
  648. (:get-file-name (lambda (master file)
  649. (filesets-which-file master
  650. (concat file ".sty")
  651. (filesets-convert-path-list
  652. (or (getenv "MY_TEXINPUTS")
  653. (getenv "TEXINPUTS")))))))
  654. ((:name "Include")
  655. (:pattern "\\\\include\\W*{\\W*\\(.+\\)\\W*}")
  656. (:get-file-name (lambda (master file)
  657. (filesets-which-file master
  658. (concat file ".tex")
  659. (filesets-convert-path-list
  660. (or (getenv "MY_TEXINPUTS")
  661. (getenv "TEXINPUTS"))))))
  662. (:scan-depth 5))
  663. ((:name "Input")
  664. (:pattern "\\\\input\\W*{\\W*\\(.+\\)\\W*}")
  665. (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b))))
  666. (:get-file-name (lambda (master file)
  667. (filesets-which-file master
  668. (concat file ".tex")
  669. (filesets-convert-path-list
  670. (or (getenv "MY_TEXINPUTS")
  671. (getenv "TEXINPUTS"))))))
  672. (:scan-depth 5))
  673. ((:name "Bibliography")
  674. (:pattern "\\\\bibliography\\W*{\\W*\\(.+\\)\\W*}")
  675. (:get-file-name (lambda (master file)
  676. (filesets-which-file master
  677. (concat file ".bib")
  678. (filesets-convert-path-list
  679. (or (getenv "MY_BIBINPUTS")
  680. (getenv "BIBINPUTS")))))))))
  681. ("^.+\\.el$" t
  682. (((:name "Require")
  683. (:pattern "(require\\W+'\\(.+\\))")
  684. (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b))))
  685. (:get-file-name (lambda (master file)
  686. (filesets-which-file master
  687. (concat file ".el")
  688. load-path))))
  689. ((:name "Load")
  690. (:pattern "(load\\(-library\\)?\\W+\"\\(.+\\)\")")
  691. (:match-number 2)
  692. (:get-file-name (lambda (master file)
  693. (filesets-which-file master file load-path))))))
  694. ("^\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)$" t
  695. (((:pattern "\\<\\([A-ZÄÖÜ][a-zäöüß]+\\([A-ZÄÖÜ][a-zäöüß]+\\)+\\)\\>")
  696. (:scan-depth 5)
  697. (:stubp (lambda (a b) (not (filesets-files-in-same-directory-p a b))))
  698. (:case-sensitive t)
  699. (:get-file-name (lambda (master file)
  700. (filesets-which-file
  701. master
  702. file
  703. (if (boundp 'emacs-wiki-directories)
  704. emacs-wiki-directories
  705. nil))))))))
  706. "Inclusion group definitions.
  707. Define how to find included file according to a file's mode (being
  708. defined by a file pattern).
  709. A valid entry has the form (FILE-PATTERN REMOVE-DUPLICATES-FLAG
  710. CMD-DEF1 ...), CMD-DEF1 being a plist containing the fields :pattern
  711. \(mandatory), :name, :get-file-name, :match-number, :scan-depth,
  712. :preprocess, :case-sensitive.
  713. File Pattern ... A regexp matching the file's name for which the
  714. following rules should be applied.
  715. Remove Duplicates ... If t, only the first occurrence of an included
  716. file is retained. (See below for a full explanation.)
  717. :name STRING ... This pattern's name.
  718. :pattern REGEXP ... A regexp matching the command. This regexp has to
  719. include a group that holds the name of the included file.
  720. :get-file-name FUNCTION (default: `filesets-which-file') ... A function
  721. that takes two arguments (the path of the master file and the name
  722. of the included file) and returns a valid path or nil -- if the
  723. subfile can't be found.
  724. :match-number INTEGER (default: 1) ... The number of the match/group
  725. in the pattern holding the subfile's name. 0 refers the whole
  726. match, 1 to the first group.
  727. :stubp FUNCTION ... If (FUNCTION MASTER INCLUDED-FILE) returns non-nil,
  728. INCLUDED-FILE is a stub -- see below.
  729. :stub-flag ... Files of this type are stubs -- see below.
  730. :scan-depth INTEGER (default: 0) ... Whether included files should be
  731. rescanned. Set this to 0 to disable re-scanning of included file.
  732. :preprocess FUNCTION ... A function modifying a buffer holding the
  733. master file so that pattern matching becomes easier. This is usually
  734. used to narrow a buffer to the relevant region. This function could also
  735. be destructive and simply delete non-relevant text.
  736. :case-sensitive BOOLEAN (default: nil) ... Whether a pattern is
  737. case-sensitive or not.
  738. Stubs:
  739. First, a stub is a file that shows up in the menu but will not be
  740. included in an ingroup's file listing -- i.e. filesets will never
  741. operate on this file automatically. Secondly, in opposition to normal
  742. files stubs are not scanned for new inclusion groups. This is useful if
  743. you want to have quick access to library headers.
  744. In the menu, an asterisk is appended to the stub's name.
  745. Remove Duplicates:
  746. E.g. File A and file B refer to file X; X refers to A. If
  747. you choose not to remove duplicates the tree would look like:
  748. M + A - X - A ...
  749. B - X - A ...
  750. As you can see, there is some chance that you run in circles.
  751. Nevertheless, up to some degree this could still be what you want.
  752. With duplicates removed, it would be:
  753. M + A - X
  754. B"
  755. :set (function filesets-set-default)
  756. :type '(repeat
  757. :tag "Include"
  758. (list
  759. :tag "Definition" :value ("^.+\\.suffix$" t)
  760. (regexp :tag "File Pattern" :value "^.+\\.suffix$")
  761. (boolean :tag "Remove Duplicates" :value t)
  762. (repeat :tag "Commands"
  763. (repeat :tag "Command"
  764. (choice
  765. :tag "Definition"
  766. (list :tag ":name"
  767. :value (:name "")
  768. (const :format "" :value :name)
  769. (string :tag "String"))
  770. (list :tag ":pattern"
  771. :value (:pattern "\\<CMD\\W*\\(.+\\)\\>")
  772. (const :format "" :value :pattern)
  773. (regexp :tag "RegExp"))
  774. (list :tag ":get-file-name"
  775. :value (:get-file-name)
  776. (const :format "" :value :get-file-name)
  777. (function :tag "Function"))
  778. (list :tag ":match-number"
  779. :value (:match-number 1)
  780. (const :format "" :value :match-number)
  781. (integer :tag "Integer"))
  782. (list :tag ":stub-flag"
  783. :value (:stub-flag t)
  784. (const :format "" :value :stub-flag)
  785. (boolean :tag "Boolean"))
  786. (list :tag ":stubp"
  787. :value (:stubp)
  788. (const :format "" :value :stubp)
  789. (function :tag "Function"))
  790. (list :tag ":scan-depth"
  791. :value (:scan-depth 0)
  792. (const :format "" :value :scan-depth)
  793. (integer :tag "Integer"))
  794. (list :tag ":case-sensitive"
  795. :value (:case-sensitive)
  796. (const :format "" :value :case-sensitive)
  797. (boolean :tag "Boolean"))
  798. (list :tag ":preprocess"
  799. :value (:preprocess)
  800. (const :format "" :value :preprocess)
  801. (function :tag "Function")))))))
  802. :group 'filesets)
  803. (put 'filesets-ingroup-patterns 'risky-local-variable t)
  804. (defcustom filesets-data nil
  805. "Fileset definitions.
  806. A fileset is either a list of files, a file pattern, a base directory
  807. and a search pattern (for files), or a base file. Changes to this
  808. variable will take effect after rebuilding the menu.
  809. Caveat: Fileset names have to be unique.
  810. Example definition:
  811. '\(\(\"My Wiki\"
  812. \(:ingroup \"~/Etc/My-Wiki/WikiContents\"))
  813. \(\"My Homepage\"
  814. \(:pattern \"~/public_html/\" \"^.+\\\\.html$\")
  815. \(:open filesets-find-file))
  816. \(\"User Configuration\"
  817. \(:files \"~/.xinitrc\"
  818. \"~/.bashrc\"
  819. \"~/.bash_profile\"))
  820. \(\"HOME\"
  821. \(:tree \"~\" \"^[^.].*[^~]$\")
  822. \(:filter-dirs-flag t)))
  823. `filesets-data' is a list of (NAME-AS-STRING . DEFINITION), DEFINITION
  824. being an association list with the fields:
  825. :files FILE-1 .. FILE-N ... a list of files belonging to a fileset
  826. :ingroup FILE-NAME ... an inclusion group's base file.
  827. :tree ROOT-DIR PATTERN ... a base directory and a file pattern
  828. :pattern DIR PATTERN ... a base directory and a regexp matching
  829. files in that directory. Usually,
  830. PATTERN has the form '^REGEXP$'. Unlike
  831. :tree, this form does not descend
  832. recursively into subdirectories.
  833. :filter-dirs-flag BOOLEAN ... is only used in conjunction with :tree.
  834. :tree-max-level INTEGER ... recurse into directories this many levels
  835. \(see `filesets-tree-max-level' for a full explanation)
  836. :dormant-flag BOOLEAN ... non-nil means don't show this item in the
  837. menu; dormant filesets can still be manipulated via commands available
  838. from the minibuffer -- e.g. `filesets-open', `filesets-close', or
  839. `filesets-run-cmd'
  840. :dormant-p FUNCTION ... a function returning :dormant-flag
  841. :open FUNCTION ... the function used to open file belonging to this
  842. fileset. The function takes a file name as argument
  843. :save FUNCTION ... the function used to save file belonging to this
  844. fileset; it takes no arguments, but works on the current buffer.
  845. Either :files, :pattern, :tree, or :ingroup must be supplied. :files
  846. overrules :tree, :tree overrules :pattern, :pattern overrules :ingroup,
  847. i.e. these tags are mutually exclusive. The fields :open and :save are
  848. optional.
  849. In conjunction with the :tree tag, :save is void. :open refers to the
  850. function used for opening files in a directory, not for opening the
  851. directory. For browsing directories, `filesets-browse-dir-function' is used.
  852. Before using :ingroup, make sure that the file type is already
  853. defined in `filesets-ingroup-patterns'."
  854. :group 'filesets
  855. :set (function filesets-data-set-default)
  856. :type '(repeat
  857. (cons :tag "Fileset"
  858. (string :tag "Name" :value "")
  859. (repeat :tag "Data"
  860. (choice
  861. :tag "Type" :value nil
  862. (list :tag "Pattern"
  863. :value (:pattern "~/" "^.+\\.suffix$")
  864. (const :format "" :value :pattern)
  865. (directory :tag "Dir")
  866. (regexp :tag "Pattern"))
  867. (cons :tag "Files"
  868. :value (:files)
  869. (const :format "" :value :files)
  870. (repeat :tag "Files" file))
  871. (list :tag "Single File"
  872. :value (:file "~/")
  873. (const :format "" :value :file)
  874. (file :tag "File"))
  875. (list :tag "Inclusion group"
  876. :value (:ingroup "~/")
  877. (const :format "" :value :ingroup)
  878. (file :tag "File" :value "~/"))
  879. (list :tag "Directory Tree"
  880. :value (:tree "~/" "^.+\\.suffix$")
  881. (const :format "" :value :tree)
  882. (directory :tag "Dir")
  883. (regexp :tag "Pattern"))
  884. (list :tag "Filter directories"
  885. :value (:filter-dirs-flag)
  886. (const :format "" :value :filter-dirs-flag)
  887. (boolean :tag "Boolean" :value nil))
  888. (list :tag "Scanning depth"
  889. :value (:tree-max-level 3)
  890. (const :format "" :value :tree-max-level)
  891. (integer :tag "Integer"))
  892. (list :tag "Verbosity"
  893. :value (:verbosity 1)
  894. (const :format "" :value :verbosity)
  895. (integer :tag "Integer"))
  896. (list :tag "Conceal fileset (Flag)"
  897. :value (:dormant-flag)
  898. (const :format "" :value :dormant-flag)
  899. (boolean :tag "Boolean"))
  900. (list :tag "Conceal fileset (Function)"
  901. :value (:dormant-p)
  902. (const :format "" :value :dormant-p)
  903. (function :tag "Function"))
  904. (list :tag "Save function"
  905. :value (:save)
  906. (const :format "" :value :save)
  907. (function :tag "Function"))
  908. (list :tag "Open function"
  909. :value (:open)
  910. (const :format "" :value :open)
  911. (function :tag "Function")))))))
  912. (put 'filesets-data 'risky-local-variable t)
  913. (defcustom filesets-query-user-limit 15
  914. "Query the user before opening a fileset with that many files."
  915. :set (function filesets-set-default)
  916. :type 'integer
  917. :group 'filesets)
  918. ;;; Emacs compatibility
  919. (eval-and-compile
  920. (if (featurep 'xemacs)
  921. (fset 'filesets-error 'error)
  922. (require 'easymenu)
  923. (defun filesets-error (class &rest args)
  924. "`error' wrapper."
  925. (error "%s" (mapconcat 'identity args " ")))
  926. ))
  927. (defun filesets-filter-dir-names (lst &optional negative)
  928. "Remove non-directory names from a list of strings.
  929. If NEGATIVE is non-nil, remove all directory names."
  930. (filesets-filter-list lst
  931. (lambda (x)
  932. (and (not (string-match "^\\.+/$" x))
  933. (if negative
  934. (not (string-match "[:/\\]$" x))
  935. (string-match "[:/\\]$" x))))))
  936. (defun filesets-conditional-sort (lst &optional access-fn)
  937. "Return a sorted copy of LST, LST being a list of strings.
  938. If `filesets-sort-menu-flag' is nil, return LST itself.
  939. ACCESS-FN ... function to get the string value of LST's elements."
  940. (if filesets-sort-menu-flag
  941. (let* ((fni (or access-fn
  942. (function identity)))
  943. (fn (if filesets-sort-case-sensitive-flag
  944. (lambda (a b)
  945. (string< (funcall fni a)
  946. (funcall fni b)))
  947. (lambda (a b)
  948. (string< (upcase (funcall fni a))
  949. (upcase (funcall fni b)))))))
  950. (sort (copy-sequence lst) fn))
  951. lst))
  952. (defun filesets-directory-files (dir &optional
  953. pattern what full-flag match-dirs-flag)
  954. "Get WHAT (:files or :dirs) in DIR.
  955. If PATTERN is provided return only those entries matching this
  956. regular expression.
  957. If MATCH-DIRS-FLAG is non-nil, also match directory entries.
  958. Return full path if FULL-FLAG is non-nil."
  959. (filesets-message 2 "Filesets: scanning %S" dir)
  960. (cond
  961. ((file-exists-p dir)
  962. (let ((files nil)
  963. (dirs nil))
  964. (dolist (this (file-name-all-completions "" dir))
  965. (cond
  966. ((string-match "^\\.+/$" this)
  967. nil)
  968. ((string-match "[:/\\]$" this)
  969. (when (or (not match-dirs-flag)
  970. (not pattern)
  971. (string-match pattern this))
  972. (filesets-message 5 "Filesets: matched dir %S with pattern %S"
  973. this pattern)
  974. (setq dirs (cons this dirs))))
  975. (t
  976. (when (or (not pattern)
  977. (string-match pattern this))
  978. (filesets-message 5 "Filesets: matched file %S with pattern %S"
  979. this pattern)
  980. (setq files (cons (if full-flag
  981. (concat (file-name-as-directory dir) this)
  982. this)
  983. files))))))
  984. (cond
  985. ((equal what ':dirs)
  986. (filesets-conditional-sort dirs))
  987. ((equal what ':files)
  988. (filesets-conditional-sort files))
  989. (t
  990. (append (filesets-conditional-sort files)
  991. (filesets-conditional-sort dirs))))))
  992. (filesets-be-docile-flag
  993. (filesets-message 1 "Filesets: %S doesn't exist" dir)
  994. nil)
  995. (t
  996. (filesets-error 'error "Filesets: " dir " does not exist"))))
  997. (defun filesets-quote (txt)
  998. "Return TXT in quotes."
  999. (concat "\"" txt "\""))
  1000. (defun filesets-get-selection ()
  1001. "Get the text between mark and point -- i.e. the selection or region."
  1002. (let ((m (mark))
  1003. (p (point)))
  1004. (if m
  1005. (buffer-substring (min m p) (max m p))
  1006. (filesets-error 'error "No selection."))))
  1007. (defun filesets-get-quoted-selection ()
  1008. "Return the currently selected text in quotes."
  1009. (filesets-quote (filesets-get-selection)))
  1010. (defun filesets-get-shortcut (n)
  1011. "Create menu shortcuts based on number N."
  1012. (let ((n (mod (- n 1) 51)))
  1013. (cond
  1014. ((not filesets-menu-shortcuts-flag)
  1015. "")
  1016. ((<= n 9)
  1017. (concat (number-to-string n) " "))
  1018. ((<= n 35)
  1019. (format "%c " (+ 87 n)))
  1020. ((<= n 51)
  1021. (format "%c " (+ -3 n))))))
  1022. (defun filesets-files-equalp (a b)
  1023. "Compare two filenames A and B after expansion."
  1024. (equal (expand-file-name a) (expand-file-name b)))
  1025. (defun filesets-files-in-same-directory-p (a b)
  1026. "Compare two filenames A and B after expansion."
  1027. (let ((ad (file-name-directory (expand-file-name a)))
  1028. (bd (file-name-directory (expand-file-name b))))
  1029. (equal ad bd)))
  1030. (defun filesets-convert-path-list (string)
  1031. "Return a path-list given as STRING as list."
  1032. (if string
  1033. (mapcar (lambda (x) (file-name-as-directory x))
  1034. (split-string string path-separator))
  1035. nil))
  1036. (defun filesets-which-file (master filename &optional path-list)
  1037. "Search for a FILENAME relative to a MASTER file in PATH-LIST."
  1038. (let ((f (concat (file-name-directory master)
  1039. filename)))
  1040. (if (file-exists-p f)
  1041. f
  1042. (filesets-some
  1043. (lambda (dir)
  1044. (let ((dir (file-name-as-directory dir))
  1045. (files (if (file-exists-p dir)
  1046. (filesets-directory-files dir nil ':files)
  1047. nil)))
  1048. (filesets-some (lambda (file)
  1049. (if (equal filename (file-name-nondirectory file))
  1050. (concat dir file)
  1051. nil))
  1052. files)))
  1053. path-list))))
  1054. (defun filesets-eviewer-get-props (entry)
  1055. "Get ENTRY's (representing an external viewer) properties."
  1056. (nth 2 entry))
  1057. (defun filesets-eviewer-constraint-p (entry)
  1058. (let* ((props (filesets-eviewer-get-props entry))
  1059. (constraint (assoc ':constraintp props))
  1060. (constraint-flag (assoc ':constraint-flag props)))
  1061. (cond
  1062. (constraint
  1063. (funcall (cadr constraint)))
  1064. (constraint-flag
  1065. (eval (cadr constraint-flag)))
  1066. (t
  1067. t))))
  1068. (defun filesets-get-external-viewer (file)
  1069. "Find an external viewer for FILE."
  1070. (let ((filename (file-name-nondirectory file)))
  1071. (filesets-some
  1072. (lambda (entry)
  1073. (when (and (string-match (nth 0 entry) filename)
  1074. (filesets-eviewer-constraint-p entry))
  1075. entry))
  1076. filesets-external-viewers)))
  1077. (defun filesets-get-external-viewer-by-name (name)
  1078. "Get the external viewer definition called NAME."
  1079. (when name
  1080. (filesets-some
  1081. (lambda (entry)
  1082. (when (and (string-equal (nth 1 entry) name)
  1083. (filesets-eviewer-constraint-p entry))
  1084. entry))
  1085. filesets-external-viewers)))
  1086. (defun filesets-filetype-property (filename event &optional entry)
  1087. "Return non-nil if a file of a specific type has special flags/tags.
  1088. Events (corresponding tag):
  1089. on-open-all (:ignore-on-open-all) ... Exclude files of this when opening
  1090. a fileset
  1091. on-grep (:ignore-on-read-text) ... Exclude files of this when running
  1092. the \"Grep <<selection>>\" command
  1093. on-capture-output (:capture-output) ... Capture output of an external viewer
  1094. on-ls ... Not used
  1095. on-cmd ... Not used
  1096. on-close-all ... Not used"
  1097. (let ((def (filesets-eviewer-get-props
  1098. (or entry
  1099. (filesets-get-external-viewer filename)))))
  1100. (filesets-alist-get def
  1101. (case event
  1102. ((on-open-all) ':ignore-on-open-all)
  1103. ((on-grep) ':ignore-on-read-text)
  1104. ((on-cmd) nil)
  1105. ((on-close-all) nil))
  1106. nil t)))
  1107. (defun filesets-filetype-get-prop (property filename &optional entry)
  1108. "Return PROPERTY for filename -- use ENTRY if provided."
  1109. (let ((def (filesets-eviewer-get-props
  1110. (or entry
  1111. (filesets-get-external-viewer filename)))))
  1112. (when def
  1113. (filesets-alist-get def property nil t))))
  1114. (defun filesets-reset-filename-on-change ()
  1115. "Reset a buffer's filename if the buffer is being modified."
  1116. (when filesets-output-buffer-flag
  1117. (set-visited-file-name nil t)))
  1118. (defun filesets-spawn-external-viewer (file &optional ev-entry)
  1119. "Start an external viewer for FILE.
  1120. Use the viewer defined in EV-ENTRY (a valid element of
  1121. `filesets-external-viewers') if provided."
  1122. (let* ((file (expand-file-name file))
  1123. (entry (or ev-entry
  1124. (filesets-get-external-viewer file))))
  1125. (if entry
  1126. (let* ((vwr (cadr entry))
  1127. (co-flag (filesets-filetype-get-prop ':capture-output file entry))
  1128. (oh (filesets-filetype-get-prop ':open-hook file entry))
  1129. (args (let ((fmt (filesets-filetype-get-prop ':args file entry)))
  1130. (if fmt
  1131. (let ((rv ""))
  1132. (dolist (this fmt rv)
  1133. (setq rv (concat rv
  1134. (cond
  1135. ((stringp this)
  1136. (format this file))
  1137. ((and (symbolp this)
  1138. (fboundp this))
  1139. (format "%S" (funcall this)))
  1140. (t
  1141. (format "%S" this)))))))
  1142. (format "%S" file))))
  1143. (output
  1144. (cond
  1145. ((and (functionp vwr) co-flag)
  1146. (funcall vwr file))
  1147. ((functionp vwr)
  1148. (funcall vwr file)
  1149. nil)
  1150. (co-flag
  1151. (shell-command-to-string (format "%s %s" vwr args)))
  1152. (t
  1153. (shell-command (format "%s %s&" vwr args))
  1154. nil))))
  1155. (if co-flag
  1156. (progn
  1157. (switch-to-buffer (format "Filesets: %s %s" vwr file))
  1158. (insert output)
  1159. (make-local-variable 'filesets-output-buffer-flag)
  1160. (setq filesets-output-buffer-flag t)
  1161. (set-visited-file-name file t)
  1162. (when oh
  1163. (run-hooks 'oh))
  1164. (set-buffer-modified-p nil)
  1165. (setq buffer-read-only t)
  1166. (goto-char (point-min)))
  1167. (when oh
  1168. (run-hooks 'oh))))
  1169. (filesets-error 'error
  1170. "Filesets: general error when spawning external viewer"))))
  1171. (defun filesets-find-file (file)
  1172. "Call `find-file' after a possible delay (see `filesets-find-file-delay').
  1173. If `filesets-be-docile-flag' is true, a file, which isn't readable, will
  1174. not be opened."
  1175. ; (sleep-for filesets-find-file-delay)
  1176. (when (or (file-readable-p file)
  1177. (not filesets-be-docile-flag))
  1178. (sit-for filesets-find-file-delay)
  1179. (find-file file)))
  1180. (defun filesets-find-or-display-file (&optional file viewer)
  1181. "Visit FILE using an external VIEWER or open it in an Emacs buffer."
  1182. (interactive)
  1183. (let* ((file (or file
  1184. (read-file-name "Find file: " nil nil viewer)))
  1185. (external-viewer-def (or
  1186. (filesets-get-external-viewer-by-name viewer)
  1187. (filesets-get-external-viewer file))))
  1188. (filesets-message 3 "Filesets: view %S using %s" file external-viewer-def)
  1189. (if external-viewer-def
  1190. (filesets-spawn-external-viewer file external-viewer-def)
  1191. (filesets-find-file file))))
  1192. (defun filesets-find-file-using ()
  1193. "Select a viewer and call `filesets-find-or-display-file'."
  1194. (interactive)
  1195. (let* ((lst (mapcar (lambda (this)
  1196. (let ((a (cadr this)))
  1197. (list (format "%s" a) a)))
  1198. filesets-external-viewers))
  1199. (viewer (completing-read "Using viewer: " lst nil t)))
  1200. (when viewer
  1201. (filesets-find-or-display-file nil (cadr (assoc viewer lst))))))
  1202. (defun filesets-browser-name ()
  1203. "Get the directory browser's name as defined in `filesets-browse-dir-function'."
  1204. (cond
  1205. ((listp filesets-browse-dir-function)
  1206. (car filesets-browse-dir-function))
  1207. (t
  1208. filesets-browse-dir-function)))
  1209. (defun filesets-browse-dir (dir)
  1210. "Browse DIR using `filesets-browse-dir-function'."
  1211. (if (functionp filesets-browse-dir-function)
  1212. (funcall filesets-browse-dir-function dir)
  1213. (let ((name (car filesets-browse-dir-function))
  1214. (args (format (cadr filesets-browse-dir-function) (expand-file-name dir))))
  1215. (with-temp-buffer
  1216. (start-process (concat "Filesets:" name)
  1217. "*Filesets external directory browser*"
  1218. name args)))))
  1219. (defun filesets-get-fileset-name (something)
  1220. "Get SOMETHING's name (Don't ask)."
  1221. (cond
  1222. ((listp something)
  1223. (car something))
  1224. (t
  1225. something)))
  1226. (defun filesets-data-get-name (entry)
  1227. "Access to `filesets-data'. Get the ENTRY's name."
  1228. (car entry))
  1229. (defun filesets-data-get-data (entry)
  1230. "Access to `filesets-data'. Get the ENTRY's data section."
  1231. (cdr entry))
  1232. (defun filesets-alist-get (alist key &optional default carp)
  1233. "Get KEY's value in the association list ALIST.
  1234. Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
  1235. (let ((elt (assoc key alist)))
  1236. (cond
  1237. (elt
  1238. (if carp
  1239. (cadr elt)
  1240. (cdr elt)))
  1241. (default default)
  1242. (t nil))))
  1243. (defun filesets-data-get (entry key &optional default carp)
  1244. "Extract the value for KEY in the data part of fileset ENTRY.
  1245. Return DEFAULT if not found. Return (car VALUE) if CARP is non-nil."
  1246. (filesets-alist-get (filesets-data-get-data entry) key default carp))
  1247. (defun filesets-data-set (entry key value)
  1248. "Set the VALUE for KEY in the data part of fileset ENTRY."
  1249. (let* ((alist (filesets-data-get-data entry))
  1250. (elt (assoc key alist)))
  1251. (if elt
  1252. (setcdr elt value)
  1253. (setcdr entry (cons (cons key value) alist)))))
  1254. (defun filesets-entry-mode (entry)
  1255. "Return fileset ENTRY's mode: :files, :file, :tree, :pattern, or :ingroup.
  1256. See `filesets-data'."
  1257. (let ((data (filesets-data-get-data entry)))
  1258. (filesets-some
  1259. (lambda (x)
  1260. (if (assoc x data)
  1261. x))
  1262. '(:files :tree :pattern :ingroup :file))))
  1263. (defun filesets-entry-get-open-fn (fileset-name &optional fileset-entry)
  1264. "Get the open-function for FILESET-NAME.
  1265. Use FILESET-ENTRY for finding the open function, if provided."
  1266. (filesets-data-get (or fileset-entry
  1267. (filesets-get-fileset-from-name fileset-name))
  1268. ':open filesets-open-file-function t))
  1269. (defun filesets-entry-get-save-fn (fileset-name &optional fileset-entry)
  1270. "Get the save-function for FILESET-NAME.
  1271. Use FILESET-ENTRY for finding the save function, if provided."
  1272. (filesets-data-get (or fileset-entry
  1273. (filesets-get-fileset-from-name fileset-name))
  1274. ':save filesets-save-buffer-function t))
  1275. (defun filesets-entry-get-files (entry)
  1276. "Get the file list for fileset ENTRY."
  1277. (filesets-data-get entry ':files))
  1278. (defun filesets-entry-set-files (entry data &optional anyways)
  1279. "Set the file list for fileset ENTRY."
  1280. (let ((files (filesets-entry-get-files entry)))
  1281. (if (or anyways files)
  1282. (filesets-data-set entry ':files data))))
  1283. (defun filesets-entry-get-verbosity (entry)
  1284. "Get verbosity level for fileset ENTRY."
  1285. (filesets-data-get entry ':verbosity 1 t))
  1286. (defun filesets-entry-get-file (entry)
  1287. "Get the single file for fileset ENTRY."
  1288. (filesets-data-get entry ':file nil t))
  1289. (defun filesets-entry-get-pattern (entry)
  1290. "Get the base directory + file pattern for fileset ENTRY."
  1291. ; (filesets-data-get entry ':pattern nil t))
  1292. (filesets-data-get entry ':pattern))
  1293. (defun filesets-entry-get-pattern--pattern (list)
  1294. "Get the file pattern for LIST."
  1295. (if (= (length list) 1) ;; for compatibility with filesets < v1.5.5
  1296. (file-name-nondirectory (car list))
  1297. (cadr list)))
  1298. (defun filesets-entry-get-pattern--dir (list)
  1299. "Get a file pattern's base directory for LIST."
  1300. (if (= (length list) 1) ;; for compatibility with filesets < v1.5.5
  1301. (file-name-directory (car list))
  1302. (car list)))
  1303. (defun filesets-entry-get-tree (entry)
  1304. "Get the tree pattern for fileset ENTRY."
  1305. (filesets-data-get entry ':tree))
  1306. (defun filesets-entry-get-dormant-flag (entry)
  1307. "Get dormant flag for fileset ENTRY."
  1308. (let ((fn (filesets-data-get entry ':dormant-p nil t)))
  1309. (if fn
  1310. (funcall fn)
  1311. (filesets-data-get entry ':dormant-flag nil t))))
  1312. (defun filesets-entry-get-filter-dirs-flag (entry)
  1313. "Get filter-dirs-flag for fileset ENTRY."
  1314. (filesets-data-get entry ':filter-dirs-flag nil t))
  1315. (defun filesets-entry-get-tree-max-level (entry)
  1316. "Get maximal tree scanning depth for fileset ENTRY."
  1317. (filesets-data-get entry ':tree-max-level nil t))
  1318. (defun filesets-entry-get-master (entry)
  1319. "Get the base file for fileset ENTRY."
  1320. (filesets-data-get entry ':ingroup nil t))
  1321. (defun filesets-file-open (open-function file-name &optional fileset-name)
  1322. "Open FILE-NAME using OPEN-FUNCTION.
  1323. If OPEN-FUNCTION is nil, its value will be deduced from FILESET-NAME."
  1324. (let ((open-function (or open-function
  1325. (filesets-entry-get-open-fn fileset-name))))
  1326. (if (file-readable-p file-name)
  1327. (funcall open-function file-name)
  1328. (message "Filesets: Couldn't open `%s'" file-name))))
  1329. (defun filesets-file-close (save-function buffer)
  1330. "Close BUFFER.
  1331. First, save the buffer's contents using SAVE-FUNCTION. Then, kill buffer
  1332. if `buffer-modified-p' returns nil.
  1333. SAVE-FUNCTION takes no argument, but works on the current buffer."
  1334. (with-current-buffer buffer
  1335. (if (buffer-modified-p)
  1336. (funcall save-function))
  1337. (if (not (buffer-modified-p))
  1338. (kill-buffer buffer))))
  1339. (defun filesets-get-fileset-from-name (name &optional mode)
  1340. "Get fileset definition for NAME."
  1341. (case mode
  1342. ((:ingroup :tree)
  1343. name)
  1344. (t
  1345. (assoc name filesets-data))))
  1346. ;;; commands
  1347. (defun filesets-cmd-get-def (cmd-name)
  1348. "Get `filesets-commands' entry for CMD-NAME."
  1349. (assoc cmd-name filesets-commands))
  1350. (defun filesets-cmd-get-args (cmd-name)
  1351. (let ((args (let ((def (filesets-cmd-get-def cmd-name)))
  1352. (nth 2 def)))
  1353. (rv nil))
  1354. (dolist (this args rv)
  1355. (cond
  1356. ((and (symbolp this) (fboundp this))
  1357. (let ((x (funcall this)))
  1358. (setq rv (append rv (if (listp x) x (list x))))))
  1359. (t
  1360. (setq rv (append rv (list this))))))))
  1361. (defun filesets-cmd-get-fn (cmd-name)
  1362. (let ((def (filesets-cmd-get-def cmd-name)))
  1363. (nth 1 def)))
  1364. (defun filesets-cmd-show-result (cmd output)
  1365. "Show OUTPUT of CMD (a shell command)."
  1366. (pop-to-buffer "*Filesets: Shell Command Output*")
  1367. (with-no-warnings
  1368. (end-of-buffer))
  1369. (insert "*** ")
  1370. (insert cmd)
  1371. (newline)
  1372. (insert output)
  1373. (newline))
  1374. (defun filesets-run-cmd--repl-fn (arg &optional format-fn)
  1375. "Helper function for `filesets-run-cmd'. Apply FORMAT-FN to arg.
  1376. Replace <file-name> or <<file-name>> with filename."
  1377. (funcall format-fn (cond
  1378. ((equal arg "<file-name>")
  1379. (buffer-file-name))
  1380. ((equal arg "<<file-name>>")
  1381. (shell-quote-argument (buffer-file-name)))
  1382. (t
  1383. arg))))
  1384. (defun filesets-run-cmd (&optional cmd-name fileset mode)
  1385. "Run CMD-NAME (see `filesets-commands') on FILESET."
  1386. (interactive)
  1387. (let* ((cmd-name (or cmd-name
  1388. (completing-read "Select command: " filesets-commands
  1389. nil t)))
  1390. (name (or fileset
  1391. (completing-read "Select fileset: " filesets-data nil t))))
  1392. (when (and cmd-name name)
  1393. (let* ((event (if (equal cmd-name "Grep <<selection>>")
  1394. 'on-grep
  1395. 'on-cmd))
  1396. (files (if (and fileset
  1397. (or (equal mode ':ingroup)
  1398. (equal mode ':tree)))
  1399. (filesets-get-filelist fileset mode event)
  1400. (filesets-get-filelist
  1401. (filesets-get-fileset-from-name name)
  1402. mode event))))
  1403. (when files
  1404. (let ((fn (filesets-cmd-get-fn cmd-name))
  1405. (args (filesets-cmd-get-args cmd-name)))
  1406. (if (memq fn '(multi-isearch-files multi-isearch-files-regexp))
  1407. (apply fn args)
  1408. (dolist (this files nil)
  1409. (save-excursion
  1410. (save-restriction
  1411. (let ((buffer (filesets-find-file this)))
  1412. (when buffer
  1413. (goto-char (point-min))
  1414. (progn
  1415. (cond
  1416. ((stringp fn)
  1417. (let* ((args
  1418. (let ((txt ""))
  1419. (dolist (this args txt)
  1420. (setq txt
  1421. (concat txt
  1422. (filesets-run-cmd--repl-fn
  1423. this
  1424. (lambda (this)
  1425. (if (equal txt "") "" " ")
  1426. (format "%s" this))))))))
  1427. (cmd (concat fn " " args)))
  1428. (filesets-cmd-show-result
  1429. cmd (shell-command-to-string cmd))))
  1430. ((symbolp fn)
  1431. (let ((args
  1432. (let ((argl nil))
  1433. (dolist (this args argl)
  1434. (setq argl
  1435. (append argl
  1436. (filesets-run-cmd--repl-fn
  1437. this
  1438. 'list)))))))
  1439. (apply fn args)))))))))))))))))
  1440. (defun filesets-get-cmd-menu ()
  1441. "Create filesets command menu."
  1442. `("+ Commands"
  1443. . ,(mapcar (lambda (this)
  1444. (let ((name (car this)))
  1445. `[,name (filesets-run-cmd ,name)]))
  1446. filesets-commands)))
  1447. ;;; sample commands
  1448. (defun filesets-cmd-query-replace-getargs ()
  1449. "Get arguments for `query-replace' and `query-replace-regexp'."
  1450. (let ((common (query-replace-read-args "Filesets query replace" nil t)))
  1451. (list (nth 0 common) (nth 1 common) t nil (nth 2 common) nil
  1452. multi-query-replace-map)))
  1453. (defun filesets-cmd-query-replace-regexp-getargs ()
  1454. "Get arguments for `query-replace' and `query-replace-regexp'."
  1455. (let ((common (query-replace-read-args "Filesets query replace" t t)))
  1456. (list (nth 0 common) (nth 1 common) t t (nth 2 common) nil
  1457. multi-query-replace-map)))
  1458. (defun filesets-cmd-isearch-getargs ()
  1459. "Get arguments for `multi-isearch-files' and `multi-isearch-files-regexp'."
  1460. (and (boundp 'files) (list files)))
  1461. (defun filesets-cmd-shell-command-getargs ()
  1462. "Get arguments for `filesets-cmd-shell-command'."
  1463. (let* ((arg (read-string "Shell command (%s = file): "
  1464. "%s"
  1465. 'shell-command-history)))
  1466. arg))
  1467. (defun filesets-cmd-shell-command (txt)
  1468. "Wrapper function for `shell-command'."
  1469. (let ((ok (if (buffer-modified-p)
  1470. (let ((ok (y-or-n-p "Save buffer? ")))
  1471. (when ok
  1472. (save-buffer))
  1473. ok)
  1474. t)))
  1475. (when ok
  1476. (let ((cmd (format txt (shell-quote-argument (buffer-file-name)))))
  1477. (message "Filesets: %s" cmd)
  1478. (filesets-cmd-show-result cmd
  1479. (shell-command-to-string cmd))))))
  1480. ;;; body
  1481. (defun filesets-get-filelist (entry &optional mode event)
  1482. "Get all files for fileset ENTRY.
  1483. Assume MODE (see `filesets-entry-mode'), if provided."
  1484. (let* ((mode (or mode
  1485. (filesets-entry-mode entry)))
  1486. (fl (case mode
  1487. ((:files)
  1488. (filesets-entry-get-files entry))
  1489. ((:file)
  1490. (list (filesets-entry-get-file entry)))
  1491. ((:ingroup)
  1492. (let ((entry (expand-file-name
  1493. (if (stringp entry)
  1494. entry
  1495. (filesets-entry-get-master entry)))))
  1496. (cons entry (filesets-ingroup-cache-get entry))))
  1497. ((:tree)
  1498. (let ((dir (nth 0 entry))
  1499. (patt (nth 1 entry)))
  1500. (filesets-directory-files dir patt ':files t)))
  1501. ((:pattern)
  1502. (let ((dirpatt (filesets-entry-get-pattern entry)))
  1503. (if dirpatt
  1504. (let ((dir (filesets-entry-get-pattern--dir dirpatt))
  1505. (patt (filesets-entry-get-pattern--pattern dirpatt)))
  1506. ;;(filesets-message 3 "Filesets: scanning %s" dirpatt)
  1507. (filesets-directory-files dir patt ':files t))
  1508. ;; (message "Filesets: malformed entry: %s" entry)))))))
  1509. (filesets-error 'error "Filesets: malformed entry: "
  1510. entry)))))))
  1511. (filesets-filter-list fl
  1512. (lambda (file)
  1513. (not (filesets-filetype-property file event))))))
  1514. (defun filesets-open (&optional mode name lookup-name)
  1515. "Open the fileset called NAME.
  1516. Use LOOKUP-NAME for searching additional data if provided."
  1517. (interactive)
  1518. (let* ((name (or name
  1519. (completing-read "Open fileset: " filesets-data nil t)))
  1520. (fileset (filesets-get-fileset-from-name name mode))
  1521. (lookup-fs (if lookup-name
  1522. (filesets-get-fileset-from-name lookup-name)
  1523. fileset))
  1524. (mode (or mode (filesets-entry-mode lookup-fs))))
  1525. (if fileset
  1526. (let* ((files (filesets-get-filelist fileset mode 'on-open-all))
  1527. (n (length files))
  1528. (open-function (filesets-entry-get-open-fn nil lookup-fs)))
  1529. (if (or (<= n filesets-query-user-limit)
  1530. (y-or-n-p (format "Filesets: Open all %d files in %s? "
  1531. n name)))
  1532. (dolist (this files nil)
  1533. (filesets-file-open open-function this))
  1534. (message "Filesets: cancelled")))
  1535. (filesets-error 'error "Filesets: Unknown fileset: " name))))
  1536. (defun filesets-close (&optional mode name lookup-name)
  1537. "Close all buffers belonging to the fileset called NAME.
  1538. Use LOOKUP-NAME for deducing the save-function, if provided."
  1539. (interactive)
  1540. (let* ((name (or name
  1541. (completing-read "Close fileset: " filesets-data nil t)))
  1542. (fileset (filesets-get-fileset-from-name name mode))
  1543. (lookup-fs (if lookup-name
  1544. (filesets-get-fileset-from-name lookup-name)
  1545. fileset))
  1546. (mode (or mode (filesets-entry-mode lookup-fs))))
  1547. (if fileset
  1548. (let ((files (filesets-get-filelist fileset mode 'on-close-all))
  1549. (save-function (filesets-entry-get-save-fn nil lookup-fs)))
  1550. (dolist (file-name files nil)
  1551. (let* ((buffer (get-file-buffer file-name)))
  1552. (if buffer
  1553. (filesets-file-close save-function buffer)))))
  1554. ; (message "Filesets: Unknown fileset: `%s'" name))))
  1555. (filesets-error 'error "Filesets: Unknown fileset: " name))))
  1556. (defun filesets-add-buffer (&optional name buffer)
  1557. "Add BUFFER (or current buffer) to the fileset called NAME.
  1558. User will be queried, if no fileset name is provided."
  1559. (interactive)
  1560. (let* ((buffer (or buffer
  1561. (current-buffer)))
  1562. (name (or name
  1563. (completing-read
  1564. (format "Add '%s' to fileset: " buffer)
  1565. filesets-data nil)))
  1566. (entry (or (assoc name filesets-data)
  1567. (when (y-or-n-p
  1568. (format "Fileset %s does not exist. Create it? "
  1569. name))
  1570. (progn
  1571. (add-to-list 'filesets-data (list name '(:files)))
  1572. (message
  1573. "Fileset %s created. Call `M-x 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 "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. (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. (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. (case 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. (t
  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 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. (case mode
  1991. ((:file)
  1992. (let* ((file (filesets-entry-get-file entry)))
  1993. `[,this-lookup-name
  1994. (filesets-file-open nil ',file ',lookup-name)]))
  1995. (t
  1996. `(,this-lookup-name
  1997. ,@(case 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. (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-hooks)
  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
  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