dired-x.el 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. ;;; dired-x.el --- extra Dired functionality -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1993-1994, 1997, 2001-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
  5. ;; Lawrence R. Dodd <dodd@roebling.poly.edu>
  6. ;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
  7. ;; Keywords: dired extensions files
  8. ;; Package: emacs
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This is based on Sebastian Kremer's excellent dired-x.el (Dired Extra),
  22. ;; version 1.191, adapted for GNU Emacs. See the `dired-x' info pages.
  23. ;; At load time dired-x.el will install itself and bind some dired keys.
  24. ;; Some dired.el and dired-aux.el functions have extra features if
  25. ;; dired-x is loaded.
  26. ;; User customization: M-x customize-group RET dired-x RET.
  27. ;; *Please* see the `dired-x' info pages for more details.
  28. ;;; Code:
  29. ;; This is a no-op if dired-x is being loaded via `dired-load-hook',
  30. ;; but maybe not if a dired-x function is being autoloaded.
  31. (require 'dired)
  32. ;;; User-defined variables.
  33. (defgroup dired-x nil
  34. "Extended directory editing (dired-x)."
  35. :group 'dired)
  36. (defgroup dired-keys nil
  37. "Dired keys customizations."
  38. :prefix "dired-"
  39. :group 'dired-x)
  40. (defcustom dired-bind-vm nil
  41. "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
  42. RMAIL files in the old Babyl format (used before before Emacs 23.1)
  43. contain \"-*- rmail -*-\" at the top, so `dired-find-file'
  44. will run `rmail' on these files. New RMAIL files use the standard
  45. mbox format, and so cannot be distinguished in this way."
  46. :type 'boolean
  47. :group 'dired-keys)
  48. (defcustom dired-bind-jump t
  49. "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not.
  50. Setting this variable directly after dired-x is loaded has no effect -
  51. use \\[customize]."
  52. :type 'boolean
  53. :set (lambda (sym val)
  54. (if (set sym val)
  55. (progn
  56. (define-key ctl-x-map "\C-j" 'dired-jump)
  57. (define-key ctl-x-4-map "\C-j" 'dired-jump-other-window))
  58. (if (eq 'dired-jump (lookup-key ctl-x-map "\C-j"))
  59. (define-key ctl-x-map "\C-j" nil))
  60. (if (eq 'dired-jump-other-window (lookup-key ctl-x-4-map "\C-j"))
  61. (define-key ctl-x-4-map "\C-j" nil))))
  62. :group 'dired-keys)
  63. (defcustom dired-bind-man t
  64. "Non-nil means bind `dired-man' to \"N\" in Dired, otherwise do not.
  65. Setting this variable directly after dired-x is loaded has no effect -
  66. use \\[customize]."
  67. :type 'boolean
  68. :set (lambda (sym val)
  69. (if (set sym val)
  70. (define-key dired-mode-map "N" 'dired-man)
  71. (if (eq 'dired-man (lookup-key dired-mode-map "N"))
  72. (define-key dired-mode-map "N" nil))))
  73. :group 'dired-keys)
  74. (defcustom dired-bind-info t
  75. "Non-nil means bind `dired-info' to \"I\" in Dired, otherwise do not.
  76. Setting this variable directly after dired-x is loaded has no effect -
  77. use \\[customize]."
  78. :type 'boolean
  79. :set (lambda (sym val)
  80. (if (set sym val)
  81. (define-key dired-mode-map "I" 'dired-info)
  82. (if (eq 'dired-info (lookup-key dired-mode-map "I"))
  83. (define-key dired-mode-map "I" nil))))
  84. :group 'dired-keys)
  85. (defcustom dired-vm-read-only-folders nil
  86. "If non-nil, \\[dired-vm] will visit all folders read-only.
  87. If neither nil nor t, e.g. the symbol `if-file-read-only', only
  88. files not writable by you are visited read-only."
  89. :type '(choice (const :tag "off" nil)
  90. (const :tag "on" t)
  91. (other :tag "non-writable only" if-file-read-only))
  92. :group 'dired-x)
  93. (defcustom dired-omit-size-limit 30000
  94. "Maximum size for the \"omitting\" feature.
  95. If nil, there is no maximum size."
  96. :type '(choice (const :tag "no maximum" nil) integer)
  97. :group 'dired-x)
  98. (defcustom dired-omit-case-fold 'filesystem
  99. "Determine whether \"omitting\" patterns are case-sensitive.
  100. When nil, always be case-sensitive; when t, always be
  101. case-insensitive; the default value, `filesystem', causes case
  102. folding to be used on case-insensitive filesystems only."
  103. :type '(choice (const :tag "Always case-sensitive" nil)
  104. (const :tag "Always case-insensitive" t)
  105. (const :tag "According to filesystem" filesystem))
  106. :group 'dired-x
  107. :version "26.1")
  108. (declare-function file-name-case-insensitive-p "fileio.c" (filename))
  109. (defun dired-omit-case-fold-p (dir)
  110. "Non-nil if `dired-omit-mode' should be case-insensitive in DIR."
  111. (if (eq dired-omit-case-fold 'filesystem)
  112. (file-name-case-insensitive-p dir)
  113. dired-omit-case-fold))
  114. ;; For backward compatibility
  115. (define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
  116. (define-minor-mode dired-omit-mode
  117. "Toggle omission of uninteresting files in Dired (Dired-Omit mode).
  118. With a prefix argument ARG, enable Dired-Omit mode if ARG is
  119. positive, and disable it otherwise. If called from Lisp, enable
  120. the mode if ARG is omitted or nil.
  121. Dired-Omit mode is a buffer-local minor mode. When enabled in a
  122. Dired buffer, Dired does not list files whose filenames match
  123. regexp `dired-omit-files', nor files ending with extensions in
  124. `dired-omit-extensions'.
  125. To enable omitting in every Dired buffer, you can put this in
  126. your init file:
  127. (add-hook \\='dired-mode-hook (lambda () (dired-omit-mode)))
  128. See Info node `(dired-x) Omitting Variables' for more information."
  129. :group 'dired-x
  130. (if dired-omit-mode
  131. ;; This will mention how many lines were omitted:
  132. (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
  133. (revert-buffer)))
  134. (put 'dired-omit-mode 'safe-local-variable 'booleanp)
  135. (defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
  136. "Filenames matching this regexp will not be displayed.
  137. This only has effect when `dired-omit-mode' is t. See interactive function
  138. `dired-omit-mode' (\\[dired-omit-mode]) and variable
  139. `dired-omit-extensions'. The default is to omit `.', `..', auto-save
  140. files and lock files."
  141. :type 'regexp
  142. :group 'dired-x)
  143. (defcustom dired-omit-verbose t
  144. "When non-nil, show messages when omitting files.
  145. When nil, don't show messages."
  146. :version "24.1"
  147. :type 'boolean
  148. :group 'dired-x)
  149. (defcustom dired-find-subdir nil ; t is pretty near to DWIM...
  150. "If non-nil, Dired always finds a directory in a buffer of its own.
  151. If nil, Dired finds the directory as a subdirectory in some other buffer
  152. if it is present as one.
  153. If there are several Dired buffers for a directory, the most recently
  154. used is chosen.
  155. Dired avoids switching to the current buffer, so that if you have
  156. a normal and a wildcard buffer for the same directory, \\[dired] will
  157. toggle between those two."
  158. :type 'boolean
  159. :group 'dired-x)
  160. (defcustom dired-enable-local-variables t
  161. "Control use of local-variables lists in Dired.
  162. This temporarily overrides the value of `enable-local-variables' when
  163. listing a directory. See also `dired-local-variables-file'."
  164. :risky t
  165. :type '(choice (const :tag "Query Unsafe" t)
  166. (const :tag "Safe Only" :safe)
  167. (const :tag "Do all" :all)
  168. (const :tag "Ignore" nil)
  169. (other :tag "Query" other))
  170. :group 'dired-x)
  171. (make-obsolete-variable 'dired-enable-local-variables
  172. "use a standard `dir-locals-file' instead." "24.1")
  173. (defcustom dired-guess-shell-gnutar
  174. (catch 'found
  175. (dolist (exe '("tar" "gtar"))
  176. (if (with-temp-buffer
  177. (ignore-errors (call-process exe nil t nil "--version"))
  178. (and (re-search-backward "GNU tar" nil t) t))
  179. (throw 'found exe))))
  180. "If non-nil, name of GNU tar executable.
  181. \(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
  182. compressed or gzip'ed tar files. If you don't have GNU tar, set this
  183. to nil: a pipe using `zcat' or `gunzip -c' will be used."
  184. ;; Changed from system-type test to testing --version output.
  185. ;; Maybe test --help for -z instead?
  186. :version "24.1"
  187. :type '(choice (const :tag "Not GNU tar" nil)
  188. (string :tag "Command name"))
  189. :group 'dired-x)
  190. (defcustom dired-guess-shell-gzip-quiet t
  191. "Non-nil says pass -q to gzip overriding verbose GZIP environment."
  192. :type 'boolean
  193. :group 'dired-x)
  194. (defcustom dired-guess-shell-znew-switches nil
  195. "If non-nil, then string of switches passed to `znew', example: \"-K\"."
  196. :type '(choice (const :tag "None" nil)
  197. (string :tag "Switches"))
  198. :group 'dired-x)
  199. (defcustom dired-clean-up-buffers-too t
  200. "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
  201. :type 'boolean
  202. :group 'dired-x)
  203. ;;; KEY BINDINGS.
  204. (define-key dired-mode-map "\C-x\M-o" 'dired-omit-mode)
  205. (define-key dired-mode-map "*O" 'dired-mark-omitted)
  206. (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
  207. (define-key dired-mode-map "*(" 'dired-mark-sexp)
  208. (define-key dired-mode-map "*." 'dired-mark-extension)
  209. (define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
  210. (define-key dired-mode-map "\M-G" 'dired-goto-subdir)
  211. (define-key dired-mode-map "F" 'dired-do-find-marked-files)
  212. (define-key dired-mode-map "Y" 'dired-do-relsymlink)
  213. (define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
  214. (define-key dired-mode-map "V" 'dired-do-run-mail)
  215. ;;; MENU BINDINGS
  216. (require 'easymenu)
  217. (let ((menu (lookup-key dired-mode-map [menu-bar])))
  218. (easy-menu-add-item menu '("Operate")
  219. ["Find Files" dired-do-find-marked-files
  220. :help "Find current or marked files"]
  221. "Shell Command...")
  222. (easy-menu-add-item menu '("Operate")
  223. ["Relative Symlink to..." dired-do-relsymlink
  224. :visible (fboundp 'make-symbolic-link)
  225. :help "Make relative symbolic links for current or \
  226. marked files"]
  227. "Hardlink to...")
  228. (easy-menu-add-item menu '("Mark")
  229. ["Flag Extension..." dired-flag-extension
  230. :help "Flag files with a certain extension for deletion"]
  231. "Mark Executables")
  232. (easy-menu-add-item menu '("Mark")
  233. ["Mark Extension..." dired-mark-extension
  234. :help "Mark files with a certain extension"]
  235. "Unmark All")
  236. (easy-menu-add-item menu '("Mark")
  237. ["Mark Omitted" dired-mark-omitted
  238. :help "Mark files matching `dired-omit-files' \
  239. and `dired-omit-extensions'"]
  240. "Unmark All")
  241. (easy-menu-add-item menu '("Regexp")
  242. ["Relative Symlink..." dired-do-relsymlink-regexp
  243. :visible (fboundp 'make-symbolic-link)
  244. :help "Make relative symbolic links for files \
  245. matching regexp"]
  246. "Hardlink...")
  247. (easy-menu-add-item menu '("Immediate")
  248. ["Omit Mode" dired-omit-mode
  249. :style toggle :selected dired-omit-mode
  250. :help "Enable or disable omitting \"uninteresting\" \
  251. files"]
  252. "Refresh"))
  253. ;; Install into appropriate hooks.
  254. (add-hook 'dired-mode-hook 'dired-extra-startup)
  255. (add-hook 'dired-after-readin-hook 'dired-omit-expunge)
  256. (defun dired-extra-startup ()
  257. "Automatically put on `dired-mode-hook' to get extra Dired features:
  258. \\<dired-mode-map>
  259. \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
  260. \\[dired-info]\t-- run info on file
  261. \\[dired-man]\t-- run man on file
  262. \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
  263. \\[dired-omit-mode]\t-- toggle omitting of files
  264. \\[dired-mark-sexp]\t-- mark by Lisp expression
  265. To see the options you can set, use M-x customize-group RET dired-x RET.
  266. See also the functions:
  267. `dired-flag-extension'
  268. `dired-virtual'
  269. `dired-jump'
  270. `dired-man'
  271. `dired-vm'
  272. `dired-rmail'
  273. `dired-info'
  274. `dired-do-find-marked-files'"
  275. (interactive)
  276. ;; These must be done in each new dired buffer.
  277. (dired-hack-local-variables)
  278. (dired-omit-startup))
  279. ;;; EXTENSION MARKING FUNCTIONS.
  280. (defun dired--mark-suffix-interactive-spec ()
  281. (let* ((default
  282. (let ((file (dired-get-filename nil t)))
  283. (when file
  284. (file-name-extension file))))
  285. (suffix
  286. (read-string (format "%s extension%s: "
  287. (if (equal current-prefix-arg '(4))
  288. "UNmarking"
  289. "Marking")
  290. (if default
  291. (format " (default %s)" default)
  292. "")) nil nil default))
  293. (marker
  294. (pcase current-prefix-arg
  295. ('(4) ?\s)
  296. ('(16)
  297. (let* ((dflt (char-to-string dired-marker-char))
  298. (input (read-string
  299. (format
  300. "Marker character to use (default %s): " dflt)
  301. nil nil dflt)))
  302. (aref input 0)))
  303. (_ dired-marker-char))))
  304. (list suffix marker)))
  305. ;; Mark files with some extension.
  306. (defun dired-mark-extension (extension &optional marker-char)
  307. "Mark all files with a certain EXTENSION for use in later commands.
  308. A `.' is automatically prepended to EXTENSION when not present.
  309. EXTENSION may also be a list of extensions instead of a single one.
  310. Optional MARKER-CHAR is marker to use.
  311. Interactively, ask for EXTENSION.
  312. Prefixed with one C-u, unmark files instead.
  313. Prefixed with two C-u's, prompt for MARKER-CHAR and mark files with it."
  314. (interactive (dired--mark-suffix-interactive-spec))
  315. (unless (listp extension)
  316. (setq extension (list extension)))
  317. (dired-mark-files-regexp
  318. (concat ".";; don't match names with nothing but an extension
  319. "\\("
  320. (mapconcat
  321. (lambda (x)
  322. (regexp-quote
  323. (if (string-prefix-p "." x) x (concat "." x))))
  324. extension "\\|")
  325. "\\)$")
  326. marker-char))
  327. ;; Mark files ending with some suffix.
  328. (defun dired-mark-suffix (suffix &optional marker-char)
  329. "Mark all files with a certain SUFFIX for use in later commands.
  330. A `.' is *not* automatically prepended to the string entered; see
  331. also `dired-mark-extension', which is similar but automatically
  332. prepends `.' when not present.
  333. SUFFIX may also be a list of suffixes instead of a single one.
  334. Optional MARKER-CHAR is marker to use.
  335. Interactively, ask for SUFFIX.
  336. Prefixed with one C-u, unmark files instead.
  337. Prefixed with two C-u's, prompt for MARKER-CHAR and mark files with it."
  338. (interactive (dired--mark-suffix-interactive-spec))
  339. (unless (listp suffix)
  340. (setq suffix (list suffix)))
  341. (dired-mark-files-regexp
  342. (concat ".";; don't match names with nothing but an extension
  343. "\\("
  344. (mapconcat 'regexp-quote suffix "\\|")
  345. "\\)$")
  346. marker-char))
  347. (defun dired-flag-extension (extension)
  348. "In Dired, flag all files with a certain EXTENSION for deletion.
  349. A `.' is *not* automatically prepended to the string entered."
  350. (interactive "sFlagging extension: ")
  351. (dired-mark-extension extension dired-del-marker))
  352. ;; Define some unpopular file extensions. Used for cleaning and omitting.
  353. (defvar dired-patch-unclean-extensions
  354. '(".rej" ".orig")
  355. "List of extensions of dispensable files created by the `patch' program.")
  356. (defvar dired-tex-unclean-extensions
  357. '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
  358. "List of extensions of dispensable files created by TeX.")
  359. (defvar dired-latex-unclean-extensions
  360. '(".idx" ".lof" ".lot" ".glo")
  361. "List of extensions of dispensable files created by LaTeX.")
  362. (defvar dired-bibtex-unclean-extensions
  363. '(".blg" ".bbl")
  364. "List of extensions of dispensable files created by BibTeX.")
  365. (defvar dired-texinfo-unclean-extensions
  366. '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
  367. ".tp" ".tps" ".vr" ".vrs")
  368. "List of extensions of dispensable files created by texinfo.")
  369. (defun dired-clean-patch ()
  370. "Flag dispensable files created by patch for deletion.
  371. See variable `dired-patch-unclean-extensions'."
  372. (interactive)
  373. (dired-flag-extension dired-patch-unclean-extensions))
  374. (defun dired-clean-tex ()
  375. "Flag dispensable files created by [La]TeX etc. for deletion.
  376. See variables `dired-tex-unclean-extensions',
  377. `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
  378. `dired-texinfo-unclean-extensions'."
  379. (interactive)
  380. (dired-flag-extension (append dired-texinfo-unclean-extensions
  381. dired-latex-unclean-extensions
  382. dired-bibtex-unclean-extensions
  383. dired-tex-unclean-extensions)))
  384. (defun dired-very-clean-tex ()
  385. "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
  386. See variables `dired-texinfo-unclean-extensions',
  387. `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
  388. `dired-texinfo-unclean-extensions'."
  389. (interactive)
  390. (dired-flag-extension (append dired-texinfo-unclean-extensions
  391. dired-latex-unclean-extensions
  392. dired-bibtex-unclean-extensions
  393. dired-tex-unclean-extensions
  394. (list ".dvi"))))
  395. (defvar tar-superior-buffer)
  396. ;;; JUMP.
  397. ;;;###autoload
  398. (defun dired-jump (&optional other-window file-name)
  399. "Jump to Dired buffer corresponding to current buffer.
  400. If in a file, Dired the current directory and move to file's line.
  401. If in Dired already, pop up a level and goto old directory's line.
  402. In case the proper Dired file line cannot be found, refresh the dired
  403. buffer and try again.
  404. When OTHER-WINDOW is non-nil, jump to Dired buffer in other window.
  405. When FILE-NAME is non-nil, jump to its line in Dired.
  406. Interactively with prefix argument, read FILE-NAME."
  407. (interactive
  408. (list nil (and current-prefix-arg
  409. (read-file-name "Jump to Dired file: "))))
  410. (if (bound-and-true-p tar-subfile-mode)
  411. (switch-to-buffer tar-superior-buffer)
  412. ;; Expand file-name before `dired-goto-file' call:
  413. ;; `dired-goto-file' requires its argument to be an absolute
  414. ;; file name; the result of `read-file-name' could be
  415. ;; an abbreviated file name (Bug#24409).
  416. (let* ((file (or (and file-name (expand-file-name file-name))
  417. buffer-file-name))
  418. (dir (if file (file-name-directory file) default-directory)))
  419. (if (and (eq major-mode 'dired-mode) (null file-name))
  420. (progn
  421. (setq dir (dired-current-directory))
  422. (dired-up-directory other-window)
  423. (unless (dired-goto-file dir)
  424. ;; refresh and try again
  425. (dired-insert-subdir (file-name-directory dir))
  426. (dired-goto-file dir)))
  427. (if other-window
  428. (dired-other-window dir)
  429. (dired dir))
  430. (if file
  431. (or (dired-goto-file file)
  432. ;; refresh and try again
  433. (progn
  434. (dired-insert-subdir (file-name-directory file))
  435. (dired-goto-file file))
  436. ;; Toggle omitting, if it is on, and try again.
  437. (when dired-omit-mode
  438. (dired-omit-mode)
  439. (dired-goto-file file))))))))
  440. ;;;###autoload
  441. (defun dired-jump-other-window (&optional file-name)
  442. "Like \\[dired-jump] (`dired-jump') but in other window."
  443. (interactive
  444. (list (and current-prefix-arg
  445. (read-file-name "Jump to Dired file: "))))
  446. (dired-jump t file-name))
  447. ;;; OMITTING.
  448. ;; Enhanced omitting of lines from directory listings.
  449. ;; Marked files are never omitted.
  450. ;; should probably get rid of this and always use 'no-dir.
  451. ;; sk 28-Aug-1991 09:37
  452. (defvar dired-omit-localp 'no-dir
  453. "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
  454. If it is `no-dir', omitting is much faster, but you can only match
  455. against the non-directory part of the file name. Set it to nil if you
  456. need to match the entire file name.")
  457. ;; \017=^O for Omit - other packages can chose other control characters.
  458. (defvar dired-omit-marker-char ?\017
  459. "Temporary marker used by Dired-Omit.
  460. Should never be used as marker by the user or other packages.")
  461. (defun dired-omit-startup ()
  462. (or (assq 'dired-omit-mode minor-mode-alist)
  463. (setq minor-mode-alist
  464. (append '((dired-omit-mode
  465. (:eval (if (eq major-mode 'dired-mode)
  466. " Omit" ""))))
  467. minor-mode-alist))))
  468. (defun dired-mark-omitted ()
  469. "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
  470. (interactive)
  471. (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
  472. (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp
  473. (dired-omit-case-fold-p (if (stringp dired-directory)
  474. dired-directory
  475. (car dired-directory)))))
  476. (defcustom dired-omit-extensions
  477. (append completion-ignored-extensions
  478. dired-latex-unclean-extensions
  479. dired-bibtex-unclean-extensions
  480. dired-texinfo-unclean-extensions)
  481. "If non-nil, a list of extensions (strings) to omit from Dired listings.
  482. Defaults to elements of `completion-ignored-extensions',
  483. `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
  484. `dired-texinfo-unclean-extensions'.
  485. See interactive function `dired-omit-mode' (\\[dired-omit-mode]) and
  486. variables `dired-omit-mode' and `dired-omit-files'."
  487. :type '(repeat string)
  488. :group 'dired-x)
  489. (defun dired-omit-expunge (&optional regexp)
  490. "Erases all unmarked files matching REGEXP.
  491. Does nothing if global variable `dired-omit-mode' is nil, or if called
  492. non-interactively and buffer is bigger than `dired-omit-size-limit'.
  493. If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
  494. filenames ending in `dired-omit-extensions'.
  495. If REGEXP is the empty string, this function is a no-op.
  496. This functions works by temporarily binding `dired-marker-char' to
  497. `dired-omit-marker-char' and calling `dired-do-kill-lines'."
  498. (interactive "sOmit files (regexp): ")
  499. (if (and dired-omit-mode
  500. (or (called-interactively-p 'interactive)
  501. (not dired-omit-size-limit)
  502. (< (buffer-size) dired-omit-size-limit)
  503. (progn
  504. (when dired-omit-verbose
  505. (message "Not omitting: directory larger than %d characters."
  506. dired-omit-size-limit))
  507. (setq dired-omit-mode nil)
  508. nil)))
  509. (let ((omit-re (or regexp (dired-omit-regexp)))
  510. (old-modified-p (buffer-modified-p))
  511. count)
  512. (or (string= omit-re "")
  513. (let ((dired-marker-char dired-omit-marker-char))
  514. (when dired-omit-verbose (message "Omitting..."))
  515. (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp
  516. (dired-omit-case-fold-p (if (stringp dired-directory)
  517. dired-directory
  518. (car dired-directory))))
  519. (progn
  520. (setq count (dired-do-kill-lines
  521. nil
  522. (if dired-omit-verbose "Omitted %d line%s." "")))
  523. (force-mode-line-update))
  524. (when dired-omit-verbose (message "(Nothing to omit)")))))
  525. ;; Try to preserve modified state of buffer. So `%*' doesn't appear
  526. ;; in mode-line of omitted buffers.
  527. (set-buffer-modified-p (and old-modified-p
  528. (save-excursion
  529. (goto-char (point-min))
  530. (re-search-forward dired-re-mark nil t))))
  531. count)))
  532. (defun dired-omit-regexp ()
  533. (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
  534. (if (and dired-omit-files dired-omit-extensions) "\\|" "")
  535. (if dired-omit-extensions
  536. (concat ".";; a non-extension part should exist
  537. "\\("
  538. (mapconcat 'regexp-quote dired-omit-extensions "\\|")
  539. "\\)$")
  540. "")))
  541. ;; Returns t if any work was done, nil otherwise.
  542. (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp case-fold-p)
  543. "Mark unmarked files matching REGEXP, displaying MSG.
  544. REGEXP is matched against the entire file name. When called
  545. interactively, prompt for REGEXP.
  546. With prefix argument, unflag all those files.
  547. Optional fourth argument LOCALP is as in `dired-get-filename'.
  548. Optional fifth argument CASE-FOLD-P specifies the value of
  549. `case-fold-search' used for matching REGEXP."
  550. (interactive
  551. (list (read-regexp
  552. "Mark unmarked files matching regexp (default all): "
  553. nil 'dired-regexp-history)
  554. nil current-prefix-arg nil))
  555. (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
  556. (dired-mark-if
  557. (and
  558. ;; not already marked
  559. (= (following-char) ?\s)
  560. ;; uninteresting
  561. (let ((fn (dired-get-filename localp t))
  562. ;; Match patterns case-insensitively on case-insensitive
  563. ;; systems
  564. (case-fold-search case-fold-p))
  565. (and fn (string-match-p regexp fn))))
  566. msg)))
  567. ;;; VIRTUAL DIRED MODE.
  568. ;; For browsing `ls -lR' listings in a dired-like fashion.
  569. (defalias 'virtual-dired 'dired-virtual)
  570. (defun dired-virtual (dirname &optional switches)
  571. "Put this buffer into Virtual Dired mode.
  572. In Virtual Dired mode, all commands that do not actually consult the
  573. filesystem will work.
  574. This is useful if you want to peruse and move around in an ls -lR
  575. output file, for example one you got from an ftp server. With
  576. ange-ftp, you can even Dired a directory containing an ls-lR file,
  577. visit that file and turn on Virtual Dired mode. But don't try to save
  578. this file, as dired-virtual indents the listing and thus changes the
  579. buffer.
  580. If you have save a Dired buffer in a file you can use \\[dired-virtual] to
  581. resume it in a later session.
  582. Type \\<dired-mode-map>\\[revert-buffer] \
  583. in the Virtual Dired buffer and answer `y' to convert
  584. the virtual to a real Dired buffer again. You don't have to do this, though:
  585. you can relist single subdirs using \\[dired-do-redisplay]."
  586. ;; DIRNAME is the top level directory of the buffer. It will become
  587. ;; its `default-directory'. If nil, the old value of
  588. ;; default-directory is used.
  589. ;; Optional SWITCHES are the ls switches to use.
  590. ;; Shell wildcards will be used if there already is a `wildcard'
  591. ;; line in the buffer (thus it is a saved Dired buffer), but there
  592. ;; is no other way to get wildcards. Insert a `wildcard' line by
  593. ;; hand if you want them.
  594. (interactive
  595. (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
  596. (goto-char (point-min))
  597. (or (looking-at-p " ")
  598. ;; if not already indented, do it now:
  599. (indent-region (point-min) (point-max) 2))
  600. (or dirname (setq dirname default-directory))
  601. (setq dirname (expand-file-name (file-name-as-directory dirname)))
  602. (setq default-directory dirname) ; contains no wildcards
  603. (let ((wildcard (save-excursion
  604. (goto-char (point-min))
  605. (forward-line 1)
  606. (and (looking-at "^ wildcard ")
  607. (buffer-substring (match-end 0)
  608. (line-end-position))))))
  609. (if wildcard
  610. (setq dirname (expand-file-name wildcard default-directory))))
  611. ;; If raw ls listing (not a saved old dired buffer), give it a
  612. ;; decent subdir headerline:
  613. (goto-char (point-min))
  614. (or (looking-at-p dired-subdir-regexp)
  615. (insert " "
  616. (directory-file-name (file-name-directory default-directory))
  617. ":\n"))
  618. (dired-mode dirname (or switches dired-listing-switches))
  619. (setq mode-name "Virtual Dired"
  620. revert-buffer-function 'dired-virtual-revert)
  621. (set (make-local-variable 'dired-subdir-alist) nil)
  622. (dired-build-subdir-alist)
  623. (goto-char (point-min))
  624. (dired-initial-position dirname))
  625. (defun dired-virtual-guess-dir ()
  626. "Guess and return appropriate working directory of this buffer.
  627. The buffer is assumed to be in Dired or ls -lR format. The guess is
  628. based upon buffer contents. If nothing could be guessed, returns
  629. nil."
  630. (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
  631. (subexpr 2))
  632. (goto-char (point-min))
  633. (cond ((looking-at regexp)
  634. ;; If a saved dired buffer, look to which dir and
  635. ;; perhaps wildcard it belongs:
  636. (let ((dir (buffer-substring (match-beginning subexpr)
  637. (match-end subexpr))))
  638. (file-name-as-directory dir)))
  639. ;; Else no match for headerline found. It's a raw ls listing.
  640. ;; In raw ls listings the directory does not have a headerline
  641. ;; try parent of first subdir, if any
  642. ((re-search-forward regexp nil t)
  643. (file-name-directory
  644. (directory-file-name
  645. (file-name-as-directory
  646. (buffer-substring (match-beginning subexpr)
  647. (match-end subexpr))))))
  648. (t ; if all else fails
  649. nil))))
  650. (defun dired-virtual-revert (&optional _arg _noconfirm)
  651. (if (not
  652. (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
  653. (error "Cannot revert a Virtual Dired buffer")
  654. (setq mode-name "Dired"
  655. revert-buffer-function 'dired-revert)
  656. (revert-buffer)))
  657. ;; A zero-arg version of dired-virtual.
  658. (defun dired-virtual-mode ()
  659. "Put current buffer into Virtual Dired mode (see `dired-virtual').
  660. Useful on `magic-mode-alist' with the regexp
  661. \"^ \\\\(/[^ /]+\\\\)+/?:$\"
  662. to put saved Dired buffers automatically into Virtual Dired mode.
  663. Also useful for `auto-mode-alist' like this:
  664. (add-to-list \\='auto-mode-alist
  665. \\='(\"[^/]\\\\.dired\\\\\\='\" . dired-virtual-mode))"
  666. (interactive)
  667. (dired-virtual (dired-virtual-guess-dir)))
  668. ;;; SMART SHELL.
  669. ;; An Emacs buffer can have but one working directory, stored in the
  670. ;; buffer-local variable `default-directory'. A Dired buffer may have
  671. ;; several subdirectories inserted, but still has but one working directory:
  672. ;; that of the top level Dired directory in that buffer. For some commands
  673. ;; it is appropriate that they use the current Dired directory instead of
  674. ;; `default-directory', e.g., `find-file' and `compile'. This is a general
  675. ;; mechanism is provided for special handling of the working directory in
  676. ;; special major modes.
  677. (define-obsolete-variable-alias 'default-directory-alist
  678. 'dired-default-directory-alist "24.1")
  679. ;; It's easier to add to this alist than redefine function
  680. ;; default-directory while keeping the old information.
  681. (defconst dired-default-directory-alist
  682. '((dired-mode . (if (fboundp 'dired-current-directory)
  683. (dired-current-directory)
  684. default-directory)))
  685. "Alist of major modes and their opinion on `default-directory'.
  686. Each element has the form (MAJOR . EXPRESSION).
  687. The function `dired-default-directory' evaluates EXPRESSION to
  688. determine a default directory.")
  689. (put 'dired-default-directory-alist 'risky-local-variable t) ; gets eval'd
  690. (make-obsolete-variable 'dired-default-directory-alist
  691. "this feature is due to be removed." "24.1")
  692. (defun dired-default-directory ()
  693. "Return the `dired-default-directory-alist' entry for the current major-mode.
  694. If none, return `default-directory'."
  695. ;; It looks like this was intended to be something of a "general"
  696. ;; feature, but it only ever seems to have been used in
  697. ;; dired-smart-shell-command, and doesn't seem worth keeping around.
  698. (declare (obsolete nil "24.1"))
  699. (or (eval (cdr (assq major-mode dired-default-directory-alist)))
  700. default-directory))
  701. (defun dired-smart-shell-command (command &optional output-buffer error-buffer)
  702. "Like function `shell-command', but in the current Virtual Dired directory."
  703. (interactive
  704. (list
  705. (read-shell-command "Shell command: " nil nil
  706. (cond
  707. (buffer-file-name (file-relative-name buffer-file-name))
  708. ((eq major-mode 'dired-mode) (dired-get-filename t t))))
  709. current-prefix-arg
  710. shell-command-default-error-buffer))
  711. (let ((default-directory (or (and (eq major-mode 'dired-mode)
  712. (dired-current-directory))
  713. default-directory)))
  714. (shell-command command output-buffer error-buffer)))
  715. ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
  716. ;; Brief Description (This feature is obsolete as of Emacs 24.1)
  717. ;;
  718. ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
  719. ;;
  720. ;; * `dired-extra-startup' calls `dired-hack-local-variables'
  721. ;;
  722. ;; * `dired-hack-local-variables' checks the value of
  723. ;; `dired-local-variables-file'
  724. ;;
  725. ;; * Check if `dired-local-variables-file' is a non-nil string and is a
  726. ;; filename found in the directory of the Dired Buffer being created.
  727. ;;
  728. ;; * If `dired-local-variables-file' satisfies the above, then temporarily
  729. ;; include it in the Dired Buffer at the bottom.
  730. ;;
  731. ;; * Set `enable-local-variables' temporarily to the user variable
  732. ;; `dired-enable-local-variables' and run `hack-local-variables' on the
  733. ;; Dired Buffer.
  734. (defcustom dired-local-variables-file (convert-standard-filename ".dired")
  735. "Filename, as string, containing local Dired buffer variables to be hacked.
  736. If this file found in current directory, then it will be inserted into dired
  737. buffer and `hack-local-variables' will be run. See Info node
  738. `(emacs)File Variables' for more information on local variables.
  739. See also `dired-enable-local-variables'."
  740. :type 'file
  741. :group 'dired)
  742. (make-obsolete-variable 'dired-local-variables-file 'dir-locals-file "24.1")
  743. (defun dired-hack-local-variables ()
  744. "Evaluate local variables in `dired-local-variables-file' for Dired buffer."
  745. (declare (obsolete hack-dir-local-variables-non-file-buffer "24.1"))
  746. (and (stringp dired-local-variables-file)
  747. (file-exists-p dired-local-variables-file)
  748. (let ((opoint (point-max))
  749. (inhibit-read-only t)
  750. ;; In case user has `enable-local-variables' set to nil we
  751. ;; override it locally with dired's variable.
  752. (enable-local-variables dired-enable-local-variables))
  753. ;; Insert 'em.
  754. (save-excursion
  755. (goto-char opoint)
  756. (insert "\^L\n")
  757. (insert-file-contents dired-local-variables-file))
  758. ;; Hack 'em.
  759. (unwind-protect
  760. (let ((buffer-file-name dired-local-variables-file))
  761. (hack-local-variables))
  762. ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
  763. (delete-region opoint (point-max)))
  764. ;; Make sure that the mode line shows the proper information.
  765. (dired-sort-set-mode-line))))
  766. ;; Does not seem worth a dedicated command.
  767. ;; See the more general features in files-x.el.
  768. (defun dired-omit-here-always ()
  769. "Create `dir-locals-file' setting `dired-omit-mode' to t in `dired-mode'.
  770. If in a Dired buffer, reverts it."
  771. (declare (obsolete add-dir-local-variable "24.1"))
  772. (interactive)
  773. (if (file-exists-p dired-local-variables-file)
  774. (error "Old-style dired-local-variables-file `./%s' found;
  775. replace it with a dir-locals-file `./%s'"
  776. dired-local-variables-file
  777. dir-locals-file))
  778. (if (file-exists-p dir-locals-file)
  779. (message "File `./%s' already exists." dir-locals-file)
  780. (add-dir-local-variable 'dired-mode 'subdirs nil)
  781. (add-dir-local-variable 'dired-mode 'dired-omit-mode t)
  782. ;; Run extra-hooks and revert directory.
  783. (when (derived-mode-p 'dired-mode)
  784. (hack-dir-local-variables-non-file-buffer)
  785. (dired-extra-startup)
  786. (dired-revert))))
  787. ;;; GUESS SHELL COMMAND.
  788. ;; Brief Description:
  789. ;;;
  790. ;; * `dired-do-shell-command' is bound to `!' by dired.el.
  791. ;;;
  792. ;; * `dired-guess-shell-command' provides smarter defaults for
  793. ;;; dired-aux.el's `dired-read-shell-command'.
  794. ;;;
  795. ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
  796. ;;; marked files.
  797. ;;;
  798. ;; * Parse `dired-guess-shell-alist-user' and
  799. ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
  800. ;;; that matches the first file in the file list.
  801. ;;;
  802. ;; * If the REGEXP matches all the entries of the file list then evaluate
  803. ;;; COMMAND, which is either a string or a Lisp expression returning a
  804. ;;; string. COMMAND may be a list of commands.
  805. ;;;
  806. ;; * Return this command to `dired-guess-shell-command' which prompts user
  807. ;;; with it. The list of commands is put into the list of default values.
  808. ;;; If a command is used successfully then it is stored permanently in
  809. ;;; `dired-shell-command-history'.
  810. ;; Guess what shell command to apply to a file.
  811. (defvar dired-shell-command-history nil
  812. "History list for commands that read dired-shell commands.")
  813. ;; Default list of shell commands.
  814. ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
  815. ;; install GNU zip's version of zcat.
  816. (autoload 'Man-support-local-filenames "man")
  817. (defvar dired-guess-shell-alist-default
  818. (list
  819. (list "\\.tar\\'"
  820. '(if dired-guess-shell-gnutar
  821. (concat dired-guess-shell-gnutar " xvf")
  822. "tar xvf")
  823. ;; Extract files into a separate subdirectory
  824. '(if dired-guess-shell-gnutar
  825. (concat "mkdir " (file-name-sans-extension file)
  826. "; " dired-guess-shell-gnutar " -C "
  827. (file-name-sans-extension file) " -xvf")
  828. (concat "mkdir " (file-name-sans-extension file)
  829. "; tar -C " (file-name-sans-extension file) " -xvf"))
  830. ;; List archive contents.
  831. '(if dired-guess-shell-gnutar
  832. (concat dired-guess-shell-gnutar " tvf")
  833. "tar tvf"))
  834. ;; REGEXPS for compressed archives must come before the .Z rule to
  835. ;; be recognized:
  836. (list "\\.tar\\.Z\\'"
  837. ;; Untar it.
  838. '(if dired-guess-shell-gnutar
  839. (concat dired-guess-shell-gnutar " zxvf")
  840. (concat "zcat * | tar xvf -"))
  841. ;; Optional conversion to gzip format.
  842. '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
  843. " " dired-guess-shell-znew-switches))
  844. ;; gzip'ed archives
  845. (list "\\.t\\(ar\\.\\)?gz\\'"
  846. '(if dired-guess-shell-gnutar
  847. (concat dired-guess-shell-gnutar " zxvf")
  848. (concat "gunzip -qc * | tar xvf -"))
  849. ;; Extract files into a separate subdirectory
  850. '(if dired-guess-shell-gnutar
  851. (concat "mkdir " (file-name-sans-extension file)
  852. "; " dired-guess-shell-gnutar " -C "
  853. (file-name-sans-extension file) " -zxvf")
  854. (concat "mkdir " (file-name-sans-extension file)
  855. "; gunzip -qc * | tar -C "
  856. (file-name-sans-extension file) " -xvf -"))
  857. ;; Optional decompression.
  858. '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
  859. ;; List archive contents.
  860. '(if dired-guess-shell-gnutar
  861. (concat dired-guess-shell-gnutar " ztvf")
  862. (concat "gunzip -qc * | tar tvf -")))
  863. ;; bzip2'ed archives
  864. (list "\\.t\\(ar\\.bz2\\|bz\\)\\'"
  865. "bunzip2 -c * | tar xvf -"
  866. ;; Extract files into a separate subdirectory
  867. '(concat "mkdir " (file-name-sans-extension file)
  868. "; bunzip2 -c * | tar -C "
  869. (file-name-sans-extension file) " -xvf -")
  870. ;; Optional decompression.
  871. "bunzip2")
  872. ;; xz'ed archives
  873. (list "\\.t\\(ar\\.\\)?xz\\'"
  874. "unxz -c * | tar xvf -"
  875. ;; Extract files into a separate subdirectory
  876. '(concat "mkdir " (file-name-sans-extension file)
  877. "; unxz -c * | tar -C "
  878. (file-name-sans-extension file) " -xvf -")
  879. ;; Optional decompression.
  880. "unxz")
  881. '("\\.shar\\.Z\\'" "zcat * | unshar")
  882. '("\\.shar\\.g?z\\'" "gunzip -qc * | unshar")
  883. '("\\.e?ps\\'" "ghostview" "xloadimage" "lpr")
  884. (list "\\.e?ps\\.g?z\\'" "gunzip -qc * | ghostview -"
  885. ;; Optional decompression.
  886. '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
  887. (list "\\.e?ps\\.Z\\'" "zcat * | ghostview -"
  888. ;; Optional conversion to gzip format.
  889. '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
  890. " " dired-guess-shell-znew-switches))
  891. '("\\.patch\\'" "cat * | patch")
  892. (list "\\.patch\\.g?z\\'" "gunzip -qc * | patch"
  893. ;; Optional decompression.
  894. '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
  895. (list "\\.patch\\.Z\\'" "zcat * | patch"
  896. ;; Optional conversion to gzip format.
  897. '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
  898. " " dired-guess-shell-znew-switches))
  899. ;; The following four extensions are useful with dired-man ("N" key)
  900. ;; FIXME "man ./" does not work with dired-do-shell-command,
  901. ;; because there seems to be no way for us to modify the filename,
  902. ;; only the command. Hmph. `dired-man' works though.
  903. (list "\\.\\(?:[0-9]\\|man\\)\\'"
  904. '(let ((loc (Man-support-local-filenames)))
  905. (cond ((eq loc 'man-db) "man -l")
  906. ((eq loc 'man) "man ./")
  907. (t
  908. "cat * | tbl | nroff -man -h | col -b"))))
  909. (list "\\.\\(?:[0-9]\\|man\\)\\.g?z\\'"
  910. '(let ((loc (Man-support-local-filenames)))
  911. (cond ((eq loc 'man-db)
  912. "man -l")
  913. ((eq loc 'man)
  914. "man ./")
  915. (t "gunzip -qc * | tbl | nroff -man -h | col -b")))
  916. ;; Optional decompression.
  917. '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
  918. (list "\\.[0-9]\\.Z\\'"
  919. '(let ((loc (Man-support-local-filenames)))
  920. (cond ((eq loc 'man-db) "man -l")
  921. ((eq loc 'man) "man ./")
  922. (t "zcat * | tbl | nroff -man -h | col -b")))
  923. ;; Optional conversion to gzip format.
  924. '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
  925. " " dired-guess-shell-znew-switches))
  926. '("\\.pod\\'" "perldoc" "pod2man * | nroff -man")
  927. '("\\.dvi\\'" "xdvi" "dvips") ; preview and printing
  928. '("\\.au\\'" "play") ; play Sun audiofiles
  929. '("\\.mpe?g\\'\\|\\.avi\\'" "xine -p")
  930. '("\\.ogg\\'" "ogg123")
  931. '("\\.mp3\\'" "mpg123")
  932. '("\\.wav\\'" "play")
  933. '("\\.uu\\'" "uudecode") ; for uudecoded files
  934. '("\\.hqx\\'" "mcvert")
  935. '("\\.sh\\'" "sh") ; execute shell scripts
  936. '("\\.xbm\\'" "bitmap") ; view X11 bitmaps
  937. '("\\.gp\\'" "gnuplot")
  938. '("\\.p[bgpn]m\\'" "xloadimage")
  939. '("\\.gif\\'" "xloadimage") ; view gif pictures
  940. '("\\.tif\\'" "xloadimage")
  941. '("\\.png\\'" "display") ; xloadimage 4.1 doesn't grok PNG
  942. '("\\.jpe?g\\'" "xloadimage")
  943. '("\\.fig\\'" "xfig") ; edit fig pictures
  944. '("\\.out\\'" "xgraph") ; for plotting purposes.
  945. '("\\.tex\\'" "latex" "tex")
  946. '("\\.texi\\(nfo\\)?\\'" "makeinfo" "texi2dvi")
  947. '("\\.pdf\\'" "xpdf")
  948. '("\\.doc\\'" "antiword" "strings")
  949. '("\\.rpm\\'" "rpm -qilp" "rpm -ivh")
  950. '("\\.dia\\'" "dia")
  951. '("\\.mgp\\'" "mgp")
  952. ;; Some other popular archivers.
  953. (list "\\.zip\\'" "unzip" "unzip -l"
  954. ;; Extract files into a separate subdirectory
  955. '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
  956. " -d " (file-name-sans-extension file)))
  957. '("\\.zoo\\'" "zoo x//")
  958. '("\\.lzh\\'" "lharc x")
  959. '("\\.arc\\'" "arc x")
  960. '("\\.shar\\'" "unshar")
  961. '("\\.rar\\'" "unrar x")
  962. '("\\.7z\\'" "7z x")
  963. ;; Compression.
  964. (list "\\.g?z\\'" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
  965. (list "\\.dz\\'" "dictunzip")
  966. (list "\\.bz2\\'" "bunzip2")
  967. (list "\\.xz\\'" "unxz")
  968. (list "\\.Z\\'" "uncompress"
  969. ;; Optional conversion to gzip format.
  970. '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
  971. " " dired-guess-shell-znew-switches))
  972. '("\\.sign?\\'" "gpg --verify"))
  973. "Default alist used for shell command guessing.
  974. See `dired-guess-shell-alist-user'.")
  975. (defcustom dired-guess-shell-alist-user nil
  976. "User-defined alist of rules for suggested commands.
  977. These rules take precedence over the predefined rules in the variable
  978. `dired-guess-shell-alist-default' (to which they are prepended).
  979. Each element of this list looks like
  980. (REGEXP COMMAND...)
  981. where each COMMAND can either be a string or a Lisp expression that evaluates
  982. to a string. This expression can access the file name as the variable `file'.
  983. If several COMMANDs are given, the first one will be the default
  984. and the rest will be added temporarily to the history and can be retrieved
  985. with \\[previous-history-element] (M-p) .
  986. The variable `dired-guess-shell-case-fold-search' controls whether
  987. REGEXP is matched case-sensitively."
  988. :group 'dired-x
  989. :type '(alist :key-type regexp :value-type (repeat sexp)))
  990. (defcustom dired-guess-shell-case-fold-search t
  991. "If non-nil, `dired-guess-shell-alist-default' and
  992. `dired-guess-shell-alist-user' are matched case-insensitively."
  993. :group 'dired-x
  994. :type 'boolean)
  995. (defun dired-guess-default (files)
  996. "Return a shell command, or a list of commands, appropriate for FILES.
  997. See `dired-guess-shell-alist-user'."
  998. (let* ((case-fold-search dired-guess-shell-case-fold-search)
  999. ;; Prepend the user's alist to the default alist.
  1000. (alist (append dired-guess-shell-alist-user
  1001. dired-guess-shell-alist-default))
  1002. (file (car files))
  1003. (flist (cdr files))
  1004. elt regexp cmds)
  1005. ;; Find the first match in the alist for first file in FILES.
  1006. (while alist
  1007. (setq elt (car alist)
  1008. regexp (car elt)
  1009. alist (cdr alist))
  1010. (if (string-match-p regexp file)
  1011. (setq cmds (cdr elt)
  1012. alist nil)))
  1013. ;; If more than one file, see if all of FILES match regular expression.
  1014. (while (and flist
  1015. (string-match-p regexp (car flist)))
  1016. (setq flist (cdr flist)))
  1017. ;; If flist is still non-nil, then do not guess since this means that not
  1018. ;; all the files in FILES were matched by the regexp.
  1019. (setq cmds (and (not flist) cmds))
  1020. ;; Return commands or nil if flist is still non-nil.
  1021. ;; Evaluate the commands in order that any logical testing will be done.
  1022. (if (cdr cmds)
  1023. (delete-dups (mapcar (lambda (cmd) (eval cmd `((file . ,file)))) cmds))
  1024. (eval (car cmds) `((file . ,file)))))) ; single command
  1025. (defun dired-guess-shell-command (prompt files)
  1026. "Ask user with PROMPT for a shell command, guessing a default from FILES."
  1027. (let ((default (dired-guess-default files))
  1028. default-list val)
  1029. (if (null default)
  1030. ;; Nothing to guess
  1031. (read-shell-command prompt nil 'dired-shell-command-history)
  1032. (setq prompt (replace-regexp-in-string ": $" " " prompt))
  1033. (if (listp default)
  1034. ;; More than one guess
  1035. (setq default-list default
  1036. default (car default)
  1037. prompt (concat
  1038. prompt
  1039. (format "{%d guesses} " (length default-list))))
  1040. ;; Just one guess
  1041. (setq default-list (list default)))
  1042. ;; Put the first guess in the prompt but not in the initial value.
  1043. (setq prompt (concat prompt (format "[%s]: " default)))
  1044. ;; All guesses can be retrieved with M-n
  1045. (setq val (read-shell-command prompt nil
  1046. 'dired-shell-command-history
  1047. default-list))
  1048. ;; If we got a return, then return default.
  1049. (if (equal val "") default val))))
  1050. ;;; RELATIVE SYMBOLIC LINKS.
  1051. (declare-function make-symbolic-link "fileio.c")
  1052. (defvar dired-keep-marker-relsymlink ?S
  1053. "See variable `dired-keep-marker-move'.")
  1054. (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
  1055. "Make a symbolic link (pointing to FILE1) in FILE2.
  1056. The link is relative (if possible), for example
  1057. \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
  1058. results in
  1059. \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
  1060. (interactive "FRelSymLink: \nFRelSymLink %s: \np")
  1061. (let (name1 name2 len1 len2 (index 0) sub)
  1062. (setq file1 (expand-file-name file1)
  1063. file2 (expand-file-name file2)
  1064. len1 (length file1)
  1065. len2 (length file2))
  1066. ;; Find common initial file name components:
  1067. (let (next)
  1068. (while (and (setq next (string-match "/" file1 index))
  1069. (< (setq next (1+ next)) (min len1 len2))
  1070. ;; For the comparison, both substrings must end in
  1071. ;; `/', so NEXT is *one plus* the result of the
  1072. ;; string-match.
  1073. ;; E.g., consider the case of linking "/tmp/a/abc"
  1074. ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
  1075. ;; of "/tmp/" as common initial component
  1076. (string-equal (substring file1 0 next)
  1077. (substring file2 0 next)))
  1078. (setq index next))
  1079. (setq name2 file2
  1080. sub (substring file1 0 index)
  1081. name1 (substring file1 index)))
  1082. (if (string-equal sub "/")
  1083. ;; No common initial file name found
  1084. (setq name1 file1)
  1085. ;; Else they have a common parent directory
  1086. (let ((tem (substring file2 index))
  1087. (start 0)
  1088. (count 0))
  1089. ;; Count number of slashes we must compensate for ...
  1090. (while (setq start (string-match "/" tem start))
  1091. (setq count (1+ count)
  1092. start (1+ start)))
  1093. ;; ... and prepend a "../" for each slash found:
  1094. (dotimes (_ count)
  1095. (setq name1 (concat "../" name1)))))
  1096. (make-symbolic-link
  1097. (directory-file-name name1) ; must not link to foo/
  1098. ; (trailing slash!)
  1099. name2 ok-if-already-exists)))
  1100. (autoload 'dired-do-create-files "dired-aux")
  1101. ;;;###autoload
  1102. (defun dired-do-relsymlink (&optional arg)
  1103. "Relative symlink all marked (or next ARG) files into a directory.
  1104. Otherwise make a relative symbolic link to the current file.
  1105. This creates relative symbolic links like
  1106. foo -> ../bar/foo
  1107. not absolute ones like
  1108. foo -> /ugly/file/name/that/may/change/any/day/bar/foo
  1109. For absolute symlinks, use \\[dired-do-symlink]."
  1110. (interactive "P")
  1111. (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
  1112. "RelSymLink" arg dired-keep-marker-relsymlink))
  1113. (autoload 'dired-mark-read-regexp "dired-aux")
  1114. (autoload 'dired-do-create-files-regexp "dired-aux")
  1115. (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
  1116. "RelSymlink all marked files containing REGEXP to NEWNAME.
  1117. See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
  1118. for more info."
  1119. (interactive (dired-mark-read-regexp "RelSymLink"))
  1120. (dired-do-create-files-regexp
  1121. #'dired-make-relative-symlink
  1122. "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
  1123. ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
  1124. ;; Brief Description:
  1125. ;;;
  1126. ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
  1127. ;;;
  1128. ;; * Use `dired-get-marked-files' to collect the marked files in the current
  1129. ;;; Dired Buffer into a list of filenames `FILE-LIST'.
  1130. ;;;
  1131. ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
  1132. ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
  1133. ;;;
  1134. ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
  1135. ;;; list each time.
  1136. ;;;
  1137. ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
  1138. ;;; element of FILE-LIST.
  1139. ;;;
  1140. ;; * If NOSELECT is nil then calculate the `size' of the window for each file
  1141. ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
  1142. ;;; cognizant of the window-configuration.
  1143. ;;;
  1144. ;; * If `size' is too small abort, otherwise run `find-file' on each element
  1145. ;;; of FILE-LIST giving each a window of height `size'.
  1146. (defun dired-do-find-marked-files (&optional noselect)
  1147. "Find all marked files displaying all of them simultaneously.
  1148. With optional NOSELECT just find files but do not select them.
  1149. The current window is split across all files marked, as evenly as possible.
  1150. Remaining lines go to bottom-most window. The number of files that can be
  1151. displayed this way is restricted by the height of the current window and
  1152. `window-min-height'.
  1153. To keep Dired buffer displayed, type \\[split-window-below] first.
  1154. To display just marked files, type \\[delete-other-windows] first."
  1155. (interactive "P")
  1156. (dired-simultaneous-find-file (dired-get-marked-files) noselect))
  1157. (defun dired-simultaneous-find-file (file-list noselect)
  1158. "Visit all files in FILE-LIST and display them simultaneously.
  1159. The current window is split across all files in FILE-LIST, as evenly as
  1160. possible. Remaining lines go to the bottom-most window. The number of
  1161. files that can be displayed this way is restricted by the height of the
  1162. current window and the variable `window-min-height'. With non-nil
  1163. NOSELECT the files are merely found but not selected."
  1164. ;; We don't make this function interactive because it is usually too clumsy
  1165. ;; to specify FILE-LIST interactively unless via dired.
  1166. (let (size)
  1167. (if noselect
  1168. ;; Do not select the buffer.
  1169. (find-file-noselect (car file-list))
  1170. ;; We will have to select the buffer. Calculate and check window size.
  1171. (setq size (/ (window-height) (length file-list)))
  1172. (or (<= window-min-height size)
  1173. (error "Too many files to visit simultaneously. Try C-u prefix"))
  1174. (find-file (car file-list)))
  1175. ;; Decrement.
  1176. (dolist (file (cdr file-list))
  1177. (if noselect
  1178. ;; Do not select the buffer.
  1179. (find-file-noselect file)
  1180. ;; Vertically split off a window of desired size. Upper window will
  1181. ;; have SIZE lines. Select lower (larger) window. We split it again.
  1182. (select-window (split-window nil size))
  1183. (find-file file)))))
  1184. ;;; MISCELLANEOUS COMMANDS.
  1185. ;; Run man on files.
  1186. (declare-function Man-getpage-in-background "man" (topic))
  1187. (defvar manual-program) ; from man.el
  1188. (defun dired-man ()
  1189. "Run `man' on this file."
  1190. ;; Used also to say: "Display old buffer if buffer name matches filename."
  1191. ;; but I have no idea what that means.
  1192. (interactive)
  1193. (require 'man)
  1194. (let* ((file (dired-get-filename))
  1195. (manual-program (replace-regexp-in-string "\\*" "%s"
  1196. (dired-guess-shell-command
  1197. "Man command: " (list file)))))
  1198. (Man-getpage-in-background file)))
  1199. ;; Run Info on files.
  1200. (defun dired-info ()
  1201. "Run `info' on this file."
  1202. (interactive)
  1203. (info (dired-get-filename)))
  1204. ;; Run mail on mail folders.
  1205. (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
  1206. (defvar vm-folder-directory)
  1207. (defun dired-vm (&optional read-only)
  1208. "Run VM on this file.
  1209. With optional prefix argument, visits the folder read-only.
  1210. Otherwise obeys the value of `dired-vm-read-only-folders'."
  1211. (interactive "P")
  1212. (let ((dir (dired-current-directory))
  1213. (fil (dired-get-filename)))
  1214. (vm-visit-folder fil (or read-only
  1215. (eq t dired-vm-read-only-folders)
  1216. (and dired-vm-read-only-folders
  1217. (not (file-writable-p fil)))))
  1218. ;; So that pressing `v' inside VM does prompt within current directory:
  1219. (set (make-local-variable 'vm-folder-directory) dir)))
  1220. (defun dired-rmail ()
  1221. "Run RMAIL on this file."
  1222. (interactive)
  1223. (rmail (dired-get-filename)))
  1224. (defun dired-do-run-mail ()
  1225. "Visit the current file as a mailbox, using VM or RMAIL.
  1226. Prompt for confirmation first; if the user says yes, call
  1227. `dired-vm' if `dired-bind-vm' is non-nil, `dired-rmail'
  1228. otherwise."
  1229. (interactive)
  1230. (let ((file (dired-get-filename t)))
  1231. (if dired-bind-vm
  1232. (if (y-or-n-p (format-message
  1233. "Visit `%s' as a mail folder with VM?" file))
  1234. (dired-vm))
  1235. ;; Read mail folder using rmail.
  1236. (if (y-or-n-p (format-message
  1237. "Visit `%s' as a mailbox with RMAIL?" file))
  1238. (dired-rmail)))))
  1239. ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
  1240. ;; This should be a builtin
  1241. (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
  1242. "Return t if BUFFER1 is more recently used than BUFFER2.
  1243. Considers buffers closer to the car of `buffer-list' to be more recent."
  1244. (and (not (equal buffer1 buffer2))
  1245. (memq buffer1 (buffer-list))
  1246. (not (memq buffer1 (memq buffer2 (buffer-list))))))
  1247. ;; Needed if ls -lh is supported and also for GNU ls -ls.
  1248. (defun dired-x--string-to-number (str)
  1249. "Like `string-to-number' but recognize a trailing unit prefix.
  1250. For example, 2K is expanded to 2048.0. The caller should make
  1251. sure that a trailing letter in STR is one of BKkMGTPEZY."
  1252. (let* ((val (string-to-number str))
  1253. (u (unless (zerop val)
  1254. (aref str (1- (length str))))))
  1255. (when (and u (> u ?9))
  1256. (when (= u ?k)
  1257. (setq u ?K))
  1258. (let ((units '(?B ?K ?M ?G ?T ?P ?E ?Z ?Y)))
  1259. (while (and units (/= (pop units) u))
  1260. (setq val (* 1024.0 val)))))
  1261. val))
  1262. (defun dired-mark-sexp (predicate &optional unflag-p)
  1263. "Mark files for which PREDICATE returns non-nil.
  1264. With a prefix arg, unmark or unflag those files instead.
  1265. PREDICATE is a lisp expression that can refer to the following symbols:
  1266. inode [integer] the inode of the file (only for ls -i output)
  1267. s [integer] the size of the file for ls -s output
  1268. (usually in blocks or, with -k, in KByte)
  1269. mode [string] file permission bits, e.g. \"-rw-r--r--\"
  1270. nlink [integer] number of links to file
  1271. uid [string] owner
  1272. gid [string] group (If the gid is not displayed by ls,
  1273. this will still be set (to the same as uid))
  1274. size [integer] file size in bytes
  1275. time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
  1276. name [string] the name of the file
  1277. sym [string] if file is a symbolic link, the linked-to name, else \"\"
  1278. For example, use
  1279. (equal 0 size)
  1280. to mark all zero length files.
  1281. There's an ambiguity when a single integer not followed by a unit
  1282. prefix precedes the file mode: It is then parsed as inode number
  1283. and not as block size (this always works for GNU coreutils ls).
  1284. Another limitation is that the uid field is needed for the
  1285. function to work correctly. In particular, the field is not
  1286. present for some values of `ls-lisp-emulation'.
  1287. This function operates only on the buffer content and does not
  1288. refer at all to the underlying file system. Contrast this with
  1289. `find-dired', which might be preferable for the task at hand."
  1290. ;; Using sym="" instead of nil avoids the trap of
  1291. ;; (string-match "foo" sym) into which a user would soon fall.
  1292. ;; Give `equal' instead of `=' in the example, as this works on
  1293. ;; integers and strings.
  1294. (interactive
  1295. (list (read--expression
  1296. (format "%s if (lisp expr): "
  1297. (if current-prefix-arg
  1298. "UNmark"
  1299. "Mark")))
  1300. current-prefix-arg))
  1301. (message "%s" predicate)
  1302. (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
  1303. inode s mode nlink uid gid size time name sym)
  1304. (dired-mark-if
  1305. (save-excursion
  1306. (and
  1307. ;; Sets vars
  1308. ;; inode s mode nlink uid gid size time name sym
  1309. ;; according to current file line. Returns t for success, nil if
  1310. ;; there is no file line. Upon success, all variables are set, either
  1311. ;; to nil or the appropriate value, so they need not be initialized.
  1312. ;; Moves point within the current line.
  1313. (dired-move-to-filename)
  1314. (let ((mode-len 10) ; length of mode string
  1315. ;; like in dired.el, but with subexpressions \1=inode, \2=s:
  1316. ;; GNU ls -hs suffixes the block count with a unit and
  1317. ;; prints it as a float, FreeBSD does neither.
  1318. (dired-re-inode-size "\\=\\s *\\([0-9]+\\s +\\)?\
  1319. \\(?:\\([0-9]+\\(?:\\.[0-9]*\\)?[BkKMGTPEZY]?\\)? ?\\)"))
  1320. (beginning-of-line)
  1321. (forward-char 2)
  1322. (search-forward-regexp dired-re-inode-size nil t)
  1323. ;; XXX Might be a size not followed by a unit prefix.
  1324. ;; We could set s to inode if it were otherwise nil,
  1325. ;; with a similar reasoning as below for setting gid to uid,
  1326. ;; but it would be even more whimsical.
  1327. (setq inode (when (match-string 1)
  1328. (string-to-number (match-string 1))))
  1329. (setq s (when (match-string 2)
  1330. (dired-x--string-to-number (match-string 2))))
  1331. (setq mode (buffer-substring (point) (+ mode-len (point))))
  1332. (forward-char mode-len)
  1333. ;; Skip any extended attributes marker ("." or "+").
  1334. (or (= (following-char) ?\s)
  1335. (forward-char 1))
  1336. (setq nlink (read (current-buffer)))
  1337. ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
  1338. ;; Another issue is that GNU ls -n right-justifies numerical
  1339. ;; UIDs and GIDs, while FreeBSD left-justifies them, so
  1340. ;; don't rely on a specific whitespace layout. Both of them
  1341. ;; right-justify all other numbers, though.
  1342. ;; XXX Return a number if the uid or gid seems to be
  1343. ;; numerical?
  1344. (setq uid (buffer-substring (progn
  1345. (skip-chars-forward " \t")
  1346. (point))
  1347. (progn
  1348. (skip-chars-forward "^ \t")
  1349. (point))))
  1350. (dired-move-to-filename)
  1351. (save-excursion
  1352. (setq time
  1353. ;; The regexp below tries to match from the last
  1354. ;; digit of the size field through a space after the
  1355. ;; date. Also, dates may have different formats
  1356. ;; depending on file age, so the date column need
  1357. ;; not be aligned to the right.
  1358. (buffer-substring (save-excursion
  1359. (skip-chars-backward " \t")
  1360. (point))
  1361. (progn
  1362. (re-search-backward
  1363. directory-listing-before-filename-regexp)
  1364. (skip-chars-forward "^ \t")
  1365. (1+ (point))))
  1366. size (dired-x--string-to-number
  1367. ;; We know that there's some kind of number
  1368. ;; before point because the regexp search
  1369. ;; above succeeded. I don't think it's worth
  1370. ;; doing an extra check for leading garbage.
  1371. (buffer-substring (point)
  1372. (progn
  1373. (skip-chars-backward "^ \t")
  1374. (point))))
  1375. ;; If no gid is displayed, gid will be set to uid
  1376. ;; but the user will then not reference it anyway in
  1377. ;; PREDICATE.
  1378. gid (buffer-substring (progn
  1379. (skip-chars-backward " \t")
  1380. (point))
  1381. (progn
  1382. (skip-chars-backward "^ \t")
  1383. (point)))))
  1384. (setq name (buffer-substring (point)
  1385. (or
  1386. (dired-move-to-end-of-filename t)
  1387. (point)))
  1388. sym (if (looking-at " -> ")
  1389. (buffer-substring (progn (forward-char 4) (point))
  1390. (line-end-position))
  1391. ""))
  1392. t)
  1393. (eval predicate
  1394. `((inode . ,inode)
  1395. (s . ,s)
  1396. (mode . ,mode)
  1397. (nlink . ,nlink)
  1398. (uid . ,uid)
  1399. (gid . ,gid)
  1400. (size . ,size)
  1401. (time . ,time)
  1402. (name . ,name)
  1403. (sym . ,sym)))))
  1404. (format "'%s file" predicate))))
  1405. ;;; FIND FILE AT POINT.
  1406. (defcustom dired-x-hands-off-my-keys t
  1407. "Non-nil means don't remap `find-file' to `dired-x-find-file'.
  1408. Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
  1409. If you change this variable without using \\[customize] after `dired-x.el'
  1410. is loaded then call \\[dired-x-bind-find-file]."
  1411. :type 'boolean
  1412. :initialize 'custom-initialize-default
  1413. :set (lambda (symbol value)
  1414. (set symbol value)
  1415. (dired-x-bind-find-file))
  1416. :group 'dired-x)
  1417. (defun dired-x-bind-find-file ()
  1418. "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
  1419. Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
  1420. Binding direction based on `dired-x-hands-off-my-keys'."
  1421. (interactive)
  1422. (if (called-interactively-p 'interactive)
  1423. (setq dired-x-hands-off-my-keys
  1424. (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
  1425. (unless dired-x-hands-off-my-keys
  1426. (define-key (current-global-map) [remap find-file]
  1427. 'dired-x-find-file)
  1428. (define-key (current-global-map) [remap find-file-other-window]
  1429. 'dired-x-find-file-other-window)))
  1430. ;; Now call it so binding is correct. This could go in the :initialize
  1431. ;; slot, but then dired-x-bind-find-file has to be defined before the
  1432. ;; defcustom, and we get free variable warnings.
  1433. (dired-x-bind-find-file)
  1434. (defun dired-x-find-file (filename)
  1435. "Edit file FILENAME.
  1436. Like `find-file', except that when called interactively with a
  1437. prefix argument, it offers the filename near point as a default."
  1438. (interactive (list (dired-x-read-filename-at-point "Find file: ")))
  1439. (find-file filename))
  1440. (defun dired-x-find-file-other-window (filename)
  1441. "Edit file FILENAME, in another window.
  1442. Like `find-file-other-window', except that when called interactively with
  1443. a prefix argument, when it offers the filename near point as a default."
  1444. (interactive (list (dired-x-read-filename-at-point "Find file: ")))
  1445. (find-file-other-window filename))
  1446. ;;; Internal functions.
  1447. ;; Fixme: This should probably use `thing-at-point'. -- fx
  1448. (defun dired-filename-at-point ()
  1449. "Return the filename closest to point, expanded.
  1450. Point should be in or after a filename."
  1451. (save-excursion
  1452. ;; First see if just past a filename.
  1453. (or (eobp) ; why?
  1454. (when (looking-at-p "[] \t\n[{}()]") ; whitespace or some parens
  1455. (skip-chars-backward " \n\t\r({[]})")
  1456. (or (bobp) (backward-char 1))))
  1457. (let ((filename-chars "-.[:alnum:]_/:$+@")
  1458. start prefix)
  1459. (if (looking-at-p (format "[%s]" filename-chars))
  1460. (progn
  1461. (skip-chars-backward filename-chars)
  1462. (setq start (point)
  1463. prefix
  1464. ;; This is something to do with ange-ftp filenames.
  1465. ;; It convert foo@bar to /foo@bar.
  1466. ;; But when does the former occur in dired buffers?
  1467. (and (string-match-p
  1468. "^\\w+@"
  1469. (buffer-substring start (line-end-position)))
  1470. "/"))
  1471. (if (string-match-p "[/~]" (char-to-string (preceding-char)))
  1472. (setq start (1- start)))
  1473. (skip-chars-forward filename-chars))
  1474. (error "No file found around point!"))
  1475. ;; Return string.
  1476. (expand-file-name (concat prefix (buffer-substring start (point)))))))
  1477. (defun dired-x-read-filename-at-point (prompt)
  1478. "Return filename prompting with PROMPT with completion.
  1479. If `current-prefix-arg' is non-nil, uses name at point as guess."
  1480. (if current-prefix-arg
  1481. (let ((guess (dired-filename-at-point)))
  1482. (read-file-name prompt
  1483. (file-name-directory guess)
  1484. guess
  1485. nil (file-name-nondirectory guess)))
  1486. (read-file-name prompt default-directory)))
  1487. (define-obsolete-function-alias 'read-filename-at-point
  1488. 'dired-x-read-filename-at-point "24.1") ; is this even needed?
  1489. ;;; BUG REPORTS
  1490. (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
  1491. ;; As Barry Warsaw would say: "This might be useful..."
  1492. (provide 'dired-x)
  1493. ;; Local Variables:
  1494. ;; byte-compile-dynamic: t
  1495. ;; generated-autoload-file: "dired-loaddefs.el"
  1496. ;; End:
  1497. ;;; dired-x.el ends here