dired.el 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088
  1. ;;; dired.el --- directory-browsing commands -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1985-1986, 1992-1997, 2000-2017 Free Software
  3. ;; Foundation, Inc.
  4. ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
  5. ;; Maintainer: emacs-devel@gnu.org
  6. ;; Keywords: files
  7. ;; Package: emacs
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This is a major mode for directory browsing and editing.
  21. ;; It is documented in the Emacs manual.
  22. ;; Rewritten in 1990/1991 to add tree features, file marking and
  23. ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
  24. ;; Finished up by rms in 1992.
  25. ;;; Code:
  26. (eval-when-compile (require 'subr-x))
  27. ;; When bootstrapping dired-loaddefs has not been generated.
  28. (require 'dired-loaddefs nil t)
  29. (declare-function dired-buffer-more-recently-used-p
  30. "dired-x" (buffer1 buffer2))
  31. ;;; Customizable variables
  32. (defgroup dired nil
  33. "Directory editing."
  34. :link '(custom-manual "(emacs)Dired")
  35. :group 'files)
  36. (defgroup dired-mark nil
  37. "Handling marks in Dired."
  38. :prefix "dired-"
  39. :group 'dired)
  40. ;;;###autoload
  41. (defcustom dired-listing-switches (purecopy "-al")
  42. "Switches passed to `ls' for Dired. MUST contain the `l' option.
  43. May contain all other options that don't contradict `-l';
  44. may contain even `F', `b', `i' and `s'. See also the variable
  45. `dired-ls-F-marks-symlinks' concerning the `F' switch.
  46. Options that include embedded whitespace must be quoted
  47. like this: \\\"--option=value with spaces\\\"; you can use
  48. `combine-and-quote-strings' to produce the correct quoting of
  49. each option.
  50. On systems such as MS-DOS and MS-Windows, which use `ls' emulation in Lisp,
  51. some of the `ls' switches are not supported; see the doc string of
  52. `insert-directory' in `ls-lisp.el' for more details."
  53. :type 'string
  54. :group 'dired)
  55. (defcustom dired-subdir-switches nil
  56. "If non-nil, switches passed to `ls' for inserting subdirectories.
  57. If nil, `dired-listing-switches' is used."
  58. :group 'dired
  59. :type '(choice (const :tag "Use dired-listing-switches" nil)
  60. (string :tag "Switches")))
  61. (defcustom dired-chown-program
  62. (purecopy (cond ((executable-find "chown") "chown")
  63. ((file-executable-p "/usr/sbin/chown") "/usr/sbin/chown")
  64. ((file-executable-p "/etc/chown") "/etc/chown")
  65. (t "chown")))
  66. "Name of chown command (usually `chown')."
  67. :group 'dired
  68. :type 'file)
  69. (defcustom dired-use-ls-dired 'unspecified
  70. "Non-nil means Dired should pass the \"--dired\" option to \"ls\".
  71. The special value of `unspecified' means to check explicitly, and
  72. save the result in this variable. This is performed the first
  73. time `dired-insert-directory' is called.
  74. Note that if you set this option to nil, either through choice or
  75. because your \"ls\" program does not support \"--dired\", Dired
  76. will fail to parse some \"unusual\" file names, e.g. those with leading
  77. spaces. You might want to install ls from GNU Coreutils, which does
  78. support this option. Alternatively, you might want to use Emacs's
  79. own emulation of \"ls\", by using:
  80. (setq ls-lisp-use-insert-directory-program nil)
  81. (require \\='ls-lisp)
  82. This is used by default on MS Windows, which does not have an \"ls\" program.
  83. Note that `ls-lisp' does not support as many options as GNU ls, though.
  84. For more details, see Info node `(emacs)ls in Lisp'."
  85. :group 'dired
  86. :type '(choice (const :tag "Check for --dired support" unspecified)
  87. (const :tag "Do not use --dired" nil)
  88. (other :tag "Use --dired" t)))
  89. (defcustom dired-chmod-program "chmod"
  90. "Name of chmod command (usually `chmod')."
  91. :group 'dired
  92. :type 'file)
  93. (defcustom dired-touch-program "touch"
  94. "Name of touch command (usually `touch')."
  95. :group 'dired
  96. :type 'file)
  97. (defcustom dired-ls-F-marks-symlinks nil
  98. "Informs Dired about how `ls -lF' marks symbolic links.
  99. Set this to t if `ls' (or whatever program is specified by
  100. `insert-directory-program') with `-lF' marks the symbolic link
  101. itself with a trailing @ (usually the case under Ultrix).
  102. Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
  103. nil (the default), if it gives `bar@ -> foo', set it to t.
  104. Dired checks if there is really a @ appended. Thus, if you have a
  105. marking `ls' program on one host and a non-marking on another host, and
  106. don't care about symbolic links which really end in a @, you can
  107. always set this variable to t."
  108. :type 'boolean
  109. :group 'dired-mark)
  110. (defcustom dired-trivial-filenames (purecopy "\\`\\.\\.?\\'\\|\\`#")
  111. "Regexp of files to skip when finding first file of a directory.
  112. A value of nil means move to the subdir line.
  113. A value of t means move to first file."
  114. :type '(choice (const :tag "Move to subdir" nil)
  115. (const :tag "Move to first" t)
  116. regexp)
  117. :group 'dired)
  118. (defcustom dired-keep-marker-rename t
  119. ;; Use t as default so that moved files "take their markers with them".
  120. "Controls marking of renamed files.
  121. If t, files keep their previous marks when they are renamed.
  122. If a character, renamed files (whether previously marked or not)
  123. are afterward marked with that character.
  124. This option affects only files renamed by `dired-do-rename' and
  125. `dired-do-rename-regexp'. See `wdired-keep-marker-rename'
  126. if you want to do the same for files renamed in WDired mode."
  127. :type '(choice (const :tag "Keep" t)
  128. (character :tag "Mark" :value ?R))
  129. :group 'dired-mark)
  130. (defcustom dired-keep-marker-copy ?C
  131. "Controls marking of copied files.
  132. If t, copied files are marked if and as the corresponding original files were.
  133. If a character, copied files are unconditionally marked with that character."
  134. :type '(choice (const :tag "Keep" t)
  135. (character :tag "Mark"))
  136. :group 'dired-mark)
  137. (defcustom dired-keep-marker-hardlink ?H
  138. "Controls marking of newly made hard links.
  139. If t, they are marked if and as the files linked to were marked.
  140. If a character, new links are unconditionally marked with that character."
  141. :type '(choice (const :tag "Keep" t)
  142. (character :tag "Mark"))
  143. :group 'dired-mark)
  144. (defcustom dired-keep-marker-symlink ?Y
  145. "Controls marking of newly made symbolic links.
  146. If t, they are marked if and as the files linked to were marked.
  147. If a character, new links are unconditionally marked with that character."
  148. :type '(choice (const :tag "Keep" t)
  149. (character :tag "Mark"))
  150. :group 'dired-mark)
  151. (defcustom dired-dwim-target nil
  152. "If non-nil, Dired tries to guess a default target directory.
  153. This means: if there is a Dired buffer displayed in the next
  154. window, use its current directory, instead of this Dired buffer's
  155. current directory.
  156. The target is used in the prompt for file copy, rename etc."
  157. :type 'boolean
  158. :group 'dired)
  159. (defcustom dired-copy-preserve-time t
  160. "If non-nil, Dired preserves the last-modified time in a file copy.
  161. \(This works on only some systems.)"
  162. :type 'boolean
  163. :group 'dired)
  164. ; These variables were deleted and the replacements are on files.el.
  165. ; We leave aliases behind for back-compatibility.
  166. (defvaralias 'dired-free-space-program 'directory-free-space-program)
  167. (defvaralias 'dired-free-space-args 'directory-free-space-args)
  168. ;;; Hook variables
  169. (defcustom dired-load-hook nil
  170. "Run after loading Dired.
  171. You can customize key bindings or load extensions with this."
  172. :group 'dired
  173. :type 'hook)
  174. (defcustom dired-mode-hook nil
  175. "Run at the very end of `dired-mode'."
  176. :group 'dired
  177. :type 'hook)
  178. (defcustom dired-before-readin-hook nil
  179. "This hook is run before a Dired buffer is read in (created or reverted)."
  180. :group 'dired
  181. :type 'hook)
  182. (defcustom dired-after-readin-hook nil
  183. "Hook run after each time a file or directory is read by Dired.
  184. After each listing of a file or directory, this hook is run
  185. with the buffer narrowed to the listing."
  186. :group 'dired
  187. :type 'hook)
  188. ;; Note this can't simply be run inside function `dired-ls' as the hook
  189. ;; functions probably depend on the dired-subdir-alist to be OK.
  190. (defcustom dired-initial-position-hook nil
  191. "This hook is used to position the point.
  192. It is run by the function `dired-initial-position'."
  193. :group 'dired
  194. :type 'hook
  195. :version "24.4")
  196. (defcustom dired-dnd-protocol-alist
  197. '(("^file:///" . dired-dnd-handle-local-file)
  198. ("^file://" . dired-dnd-handle-file)
  199. ("^file:" . dired-dnd-handle-local-file))
  200. "The functions to call when a drop in `dired-mode' is made.
  201. See `dnd-protocol-alist' for more information. When nil, behave
  202. as in other buffers. Changing this option is effective only for
  203. new Dired buffers."
  204. :type '(choice (repeat (cons (regexp) (function)))
  205. (const :tag "Behave as in other buffers" nil))
  206. :version "22.1"
  207. :group 'dired)
  208. (defcustom dired-hide-details-hide-symlink-targets t
  209. "Non-nil means `dired-hide-details-mode' hides symbolic link targets."
  210. :type 'boolean
  211. :version "24.4"
  212. :group 'dired)
  213. (defcustom dired-hide-details-hide-information-lines t
  214. "Non-nil means `dired-hide-details-mode' hides all but header and file lines."
  215. :type 'boolean
  216. :version "24.4"
  217. :group 'dired)
  218. (defcustom dired-always-read-filesystem nil
  219. "Non-nil means revert buffers visiting files before searching them.
  220. By default, commands like `dired-mark-files-containing-regexp' will
  221. search any buffers visiting the marked files without reverting them,
  222. even if they were changed on disk. When this option is non-nil, such
  223. buffers are always reverted in a temporary buffer before searching
  224. them: the search is performed on the temporary buffer, the original
  225. buffer visiting the file is not modified."
  226. :type 'boolean
  227. :version "26.1"
  228. :group 'dired)
  229. ;; Internal variables
  230. (defvar dired-marker-char ?* ; the answer is 42
  231. ;; so that you can write things like
  232. ;; (let ((dired-marker-char ?X))
  233. ;; ;; great code using X markers ...
  234. ;; )
  235. ;; For example, commands operating on two sets of files, A and B.
  236. ;; Or marking files with digits 0-9. This could implicate
  237. ;; concentric sets or an order for the marked files.
  238. ;; The code depends on dynamic scoping on the marker char.
  239. "In Dired, the current mark character.
  240. This is what the do-commands look for, and what the mark-commands store.")
  241. (defvar dired-del-marker ?D
  242. "Character used to flag files for deletion.")
  243. (defvar dired-shrink-to-fit t
  244. ;; I see no reason ever to make this nil -- rms.
  245. ;; (> baud-rate search-slow-speed)
  246. "Non-nil means Dired shrinks the display buffer to fit the marked files.")
  247. (make-obsolete-variable 'dired-shrink-to-fit
  248. "use the Customization interface to add a new rule
  249. to `display-buffer-alist' where condition regexp is \"^ \\*Marked Files\\*$\",
  250. action argument symbol is `window-height' and its value is nil." "24.3")
  251. (defvar dired-file-version-alist)
  252. ;;;###autoload
  253. (defvar dired-directory nil
  254. "The directory name or wildcard spec that this Dired directory lists.
  255. Local to each Dired buffer. May be a list, in which case the car is the
  256. directory name and the cdr is the list of files to mention.
  257. The directory name must be absolute, but need not be fully expanded.")
  258. ;; Beware of "-l;reboot" etc. See bug#3230.
  259. (defun dired-safe-switches-p (switches)
  260. "Return non-nil if string SWITCHES does not look risky for Dired."
  261. (or (not switches)
  262. (and (stringp switches)
  263. (< (length switches) 100) ; arbitrary
  264. (string-match-p "\\` *-[- [:alnum:]]+\\'" switches))))
  265. (defvar dired-actual-switches nil
  266. "The value of `dired-listing-switches' used to make this buffer's text.")
  267. (put 'dired-actual-switches 'safe-local-variable 'dired-safe-switches-p)
  268. (defvar dired-re-inode-size "[0-9 \t]*[.,0-9]*[BkKMGTPEZY]?[ \t]*"
  269. "Regexp for optional initial inode and file size as made by `ls -i -s'.")
  270. ;; These regexps must be tested at beginning-of-line, but are also
  271. ;; used to search for next matches, so neither omitting "^" nor
  272. ;; replacing "^" by "\n" (to make it slightly faster) will work.
  273. (defvar dired-re-mark "^[^ \n]")
  274. ;; "Regexp matching a marked line.
  275. ;; Important: the match ends just after the marker."
  276. (defvar dired-re-maybe-mark "^. ")
  277. ;; The [^:] part after "d" and "l" is to avoid confusion with the
  278. ;; DOS/Windows-style drive letters in directory names, like in "d:/foo".
  279. (defvar dired-re-dir (concat dired-re-maybe-mark dired-re-inode-size "d[^:]"))
  280. (defvar dired-re-sym (concat dired-re-maybe-mark dired-re-inode-size "l[^:]"))
  281. (defvar dired-re-exe;; match ls permission string of an executable file
  282. (mapconcat (lambda (x)
  283. (concat dired-re-maybe-mark dired-re-inode-size x))
  284. '("-[-r][-w][xs][-r][-w].[-r][-w]."
  285. "-[-r][-w].[-r][-w][xs][-r][-w]."
  286. "-[-r][-w].[-r][-w].[-r][-w][xst]")
  287. "\\|"))
  288. (defvar dired-re-perms "[-bcdlps][-r][-w].[-r][-w].[-r][-w].")
  289. (defvar dired-re-dot "^.* \\.\\.?/?$")
  290. ;; The subdirectory names in the next two lists are expanded.
  291. (defvar dired-subdir-alist nil
  292. "Association list of subdirectories and their buffer positions.
  293. Each subdirectory has an element: (DIRNAME . STARTMARKER).
  294. The order of elements is the reverse of the order in the buffer.
  295. In simple cases, this list contains one element.")
  296. (defvar-local dired-switches-alist nil
  297. "Keeps track of which switches to use for inserted subdirectories.
  298. This is an alist of the form (SUBDIR . SWITCHES).")
  299. (defvaralias 'dired-move-to-filename-regexp
  300. 'directory-listing-before-filename-regexp)
  301. (defvar dired-subdir-regexp "^. \\([^\n\r]+\\)\\(:\\)[\n\r]"
  302. "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
  303. Subexpression 1 is the subdirectory proper, no trailing colon.
  304. The match starts at the beginning of the line and ends after the end
  305. of the line (\\n or \\r).
  306. Subexpression 2 must end right before the \\n or \\r.")
  307. (defgroup dired-faces nil
  308. "Faces used by Dired."
  309. :group 'dired
  310. :group 'faces)
  311. (defface dired-header
  312. '((t (:inherit font-lock-type-face)))
  313. "Face used for directory headers."
  314. :group 'dired-faces
  315. :version "22.1")
  316. (defvar dired-header-face 'dired-header
  317. "Face name used for directory headers.")
  318. (defface dired-mark
  319. '((t (:inherit font-lock-constant-face)))
  320. "Face used for Dired marks."
  321. :group 'dired-faces
  322. :version "22.1")
  323. (defvar dired-mark-face 'dired-mark
  324. "Face name used for Dired marks.")
  325. (defface dired-marked
  326. '((t (:inherit warning)))
  327. "Face used for marked files."
  328. :group 'dired-faces
  329. :version "22.1")
  330. (defvar dired-marked-face 'dired-marked
  331. "Face name used for marked files.")
  332. (defface dired-flagged
  333. '((t (:inherit error)))
  334. "Face used for files flagged for deletion."
  335. :group 'dired-faces
  336. :version "22.1")
  337. (defvar dired-flagged-face 'dired-flagged
  338. "Face name used for files flagged for deletion.")
  339. (defface dired-warning
  340. ;; Inherit from font-lock-warning-face since with min-colors 8
  341. ;; font-lock-comment-face is not colored any more.
  342. '((t (:inherit font-lock-warning-face)))
  343. "Face used to highlight a part of a buffer that needs user attention."
  344. :group 'dired-faces
  345. :version "22.1")
  346. (defvar dired-warning-face 'dired-warning
  347. "Face name used for a part of a buffer that needs user attention.")
  348. (defface dired-perm-write
  349. '((((type w32 pc)) :inherit default) ;; These default to rw-rw-rw.
  350. ;; Inherit from font-lock-comment-delimiter-face since with min-colors 8
  351. ;; font-lock-comment-face is not colored any more.
  352. (t (:inherit font-lock-comment-delimiter-face)))
  353. "Face used to highlight permissions of group- and world-writable files."
  354. :group 'dired-faces
  355. :version "22.2")
  356. (defvar dired-perm-write-face 'dired-perm-write
  357. "Face name used for permissions of group- and world-writable files.")
  358. (defface dired-directory
  359. '((t (:inherit font-lock-function-name-face)))
  360. "Face used for subdirectories."
  361. :group 'dired-faces
  362. :version "22.1")
  363. (defvar dired-directory-face 'dired-directory
  364. "Face name used for subdirectories.")
  365. (defface dired-symlink
  366. '((t (:inherit font-lock-keyword-face)))
  367. "Face used for symbolic links."
  368. :group 'dired-faces
  369. :version "22.1")
  370. (defvar dired-symlink-face 'dired-symlink
  371. "Face name used for symbolic links.")
  372. (defface dired-ignored
  373. '((t (:inherit shadow)))
  374. "Face used for files suffixed with `completion-ignored-extensions'."
  375. :group 'dired-faces
  376. :version "22.1")
  377. (defvar dired-ignored-face 'dired-ignored
  378. "Face name used for files suffixed with `completion-ignored-extensions'.")
  379. (defvar dired-font-lock-keywords
  380. (list
  381. ;;
  382. ;; Dired marks.
  383. (list dired-re-mark '(0 dired-mark-face))
  384. ;;
  385. ;; We make heavy use of MATCH-ANCHORED, since the regexps don't identify the
  386. ;; file name itself. We search for Dired defined regexps, and then use the
  387. ;; Dired defined function `dired-move-to-filename' before searching for the
  388. ;; simple regexp ".+". It is that regexp which matches the file name.
  389. ;;
  390. ;; Marked files.
  391. (list (concat "^[" (char-to-string dired-marker-char) "]")
  392. '(".+" (dired-move-to-filename) nil (0 dired-marked-face)))
  393. ;;
  394. ;; Flagged files.
  395. (list (concat "^[" (char-to-string dired-del-marker) "]")
  396. '(".+" (dired-move-to-filename) nil (0 dired-flagged-face)))
  397. ;; People who are paranoid about security would consider this more
  398. ;; important than other things such as whether it is a directory.
  399. ;; But we don't want to encourage paranoia, so our default
  400. ;; should be what's most useful for non-paranoids. -- rms.
  401. ;;; ;;
  402. ;;; ;; Files that are group or world writable.
  403. ;;; (list (concat dired-re-maybe-mark dired-re-inode-size
  404. ;;; "\\([-d]\\(....w....\\|.......w.\\)\\)")
  405. ;;; '(1 dired-warning-face)
  406. ;;; '(".+" (dired-move-to-filename) nil (0 dired-warning-face)))
  407. ;; However, we don't need to highlight the file name, only the
  408. ;; permissions, to win generally. -- fx.
  409. ;; Fixme: we could also put text properties on the permission
  410. ;; fields with keymaps to frob the permissions, somewhat a la XEmacs.
  411. (list (concat dired-re-maybe-mark dired-re-inode-size
  412. "[-d]....\\(w\\)....") ; group writable
  413. '(1 dired-perm-write-face))
  414. (list (concat dired-re-maybe-mark dired-re-inode-size
  415. "[-d].......\\(w\\).") ; world writable
  416. '(1 dired-perm-write-face))
  417. ;;
  418. ;; Subdirectories.
  419. (list dired-re-dir
  420. '(".+" (dired-move-to-filename) nil (0 dired-directory-face)))
  421. ;;
  422. ;; Symbolic links.
  423. (list dired-re-sym
  424. '(".+" (dired-move-to-filename) nil (0 dired-symlink-face)))
  425. ;;
  426. ;; Files suffixed with `completion-ignored-extensions'.
  427. '(eval .
  428. ;; It is quicker to first find just an extension, then go back to the
  429. ;; start of that file name. So we do this complex MATCH-ANCHORED form.
  430. (list (concat "\\(" (regexp-opt completion-ignored-extensions) "\\|#\\)$")
  431. '(".+" (dired-move-to-filename) nil (0 dired-ignored-face))))
  432. ;;
  433. ;; Files suffixed with `completion-ignored-extensions'
  434. ;; plus a character put in by -F.
  435. '(eval .
  436. (list (concat "\\(" (regexp-opt completion-ignored-extensions)
  437. "\\|#\\)[*=|]$")
  438. '(".+" (progn
  439. (end-of-line)
  440. ;; If the last character is not part of the filename,
  441. ;; move back to the start of the filename
  442. ;; so it can be fontified.
  443. ;; Otherwise, leave point at the end of the line;
  444. ;; that way, nothing is fontified.
  445. (unless (get-text-property (1- (point)) 'mouse-face)
  446. (dired-move-to-filename)))
  447. nil (0 dired-ignored-face))))
  448. ;;
  449. ;; Explicitly put the default face on file names ending in a colon to
  450. ;; avoid fontifying them as directory header.
  451. (list (concat dired-re-maybe-mark dired-re-inode-size dired-re-perms ".*:$")
  452. '(".+" (dired-move-to-filename) nil (0 'default)))
  453. ;;
  454. ;; Directory headers.
  455. (list dired-subdir-regexp '(1 dired-header-face))
  456. )
  457. "Additional expressions to highlight in Dired mode.")
  458. (defvar dnd-protocol-alist)
  459. ;;; Macros must be defined before they are used, for the byte compiler.
  460. (defmacro dired-mark-if (predicate msg)
  461. "Mark all files for which PREDICATE evals to non-nil.
  462. PREDICATE is evaluated on each line, with point at beginning of line.
  463. MSG is a noun phrase for the type of files being marked.
  464. It should end with a noun that can be pluralized by adding `s'.
  465. Return value is the number of files marked, or nil if none were marked."
  466. `(let ((inhibit-read-only t) count)
  467. (save-excursion
  468. (setq count 0)
  469. (when ,msg
  470. (message "%s %ss%s..."
  471. (cond ((eq dired-marker-char ?\040) "Unmarking")
  472. ((eq dired-del-marker dired-marker-char)
  473. "Flagging")
  474. (t "Marking"))
  475. ,msg
  476. (if (eq dired-del-marker dired-marker-char)
  477. " for deletion"
  478. "")))
  479. (goto-char (point-min))
  480. (while (not (eobp))
  481. (if ,predicate
  482. (progn
  483. (delete-char 1)
  484. (insert dired-marker-char)
  485. (setq count (1+ count))))
  486. (forward-line 1))
  487. (if ,msg (message "%s %s%s %s%s."
  488. count
  489. ,msg
  490. (dired-plural-s count)
  491. (if (eq dired-marker-char ?\040) "un" "")
  492. (if (eq dired-marker-char dired-del-marker)
  493. "flagged" "marked"))))
  494. (and (> count 0) count)))
  495. (defmacro dired-map-over-marks (body arg &optional show-progress
  496. distinguish-one-marked)
  497. "Eval BODY with point on each marked line. Return a list of BODY's results.
  498. If no marked file could be found, execute BODY on the current
  499. line. ARG, if non-nil, specifies the files to use instead of the
  500. marked files.
  501. If ARG is an integer, use the next ARG (or previous -ARG, if
  502. ARG<0) files. In that case, point is dragged along. This is so
  503. that commands on the next ARG (instead of the marked) files can
  504. be chained easily.
  505. For any other non-nil value of ARG, use the current file.
  506. If optional third arg SHOW-PROGRESS evaluates to non-nil,
  507. redisplay the dired buffer after each file is processed.
  508. No guarantee is made about the position on the marked line.
  509. BODY must ensure this itself if it depends on this.
  510. Search starts at the beginning of the buffer, thus the car of the
  511. list corresponds to the line nearest to the buffer's bottom.
  512. This is also true for (positive and negative) integer values of
  513. ARG.
  514. BODY should not be too long as it is expanded four times.
  515. If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one
  516. marked file, return (t FILENAME) instead of (FILENAME)."
  517. ;;
  518. ;;Warning: BODY must not add new lines before point - this may cause an
  519. ;;endless loop.
  520. ;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
  521. `(prog1
  522. (let ((inhibit-read-only t) case-fold-search found results)
  523. (if ,arg
  524. (if (integerp ,arg)
  525. (progn ;; no save-excursion, want to move point.
  526. (dired-repeat-over-lines
  527. ,arg
  528. (lambda ()
  529. (if ,show-progress (sit-for 0))
  530. (setq results (cons ,body results))))
  531. (if (< ,arg 0)
  532. (nreverse results)
  533. results))
  534. ;; non-nil, non-integer ARG means use current file:
  535. (list ,body))
  536. (let ((regexp (dired-marker-regexp)) next-position)
  537. (save-excursion
  538. (goto-char (point-min))
  539. ;; remember position of next marked file before BODY
  540. ;; can insert lines before the just found file,
  541. ;; confusing us by finding the same marked file again
  542. ;; and again and...
  543. (setq next-position (and (re-search-forward regexp nil t)
  544. (point-marker))
  545. found (not (null next-position)))
  546. (while next-position
  547. (goto-char next-position)
  548. (if ,show-progress (sit-for 0))
  549. (setq results (cons ,body results))
  550. ;; move after last match
  551. (goto-char next-position)
  552. (forward-line 1)
  553. (set-marker next-position nil)
  554. (setq next-position (and (re-search-forward regexp nil t)
  555. (point-marker)))))
  556. (if (and ,distinguish-one-marked (= (length results) 1))
  557. (setq results (cons t results)))
  558. (if found
  559. results
  560. (list ,body)))))
  561. ;; save-excursion loses, again
  562. (dired-move-to-filename)))
  563. (defun dired-get-marked-files (&optional localp arg filter distinguish-one-marked)
  564. "Return the marked files' names as list of strings.
  565. The list is in the same order as the buffer, that is, the car is the
  566. first marked file.
  567. Values returned are normally absolute file names.
  568. Optional arg LOCALP as in `dired-get-filename'.
  569. Optional second argument ARG, if non-nil, specifies files near
  570. point instead of marked files. It usually comes from the prefix
  571. argument.
  572. If ARG is an integer, use the next ARG files.
  573. If ARG is any other non-nil value, return the current file name.
  574. If no files are marked, and ARG is nil, also return the current file name.
  575. Optional third argument FILTER, if non-nil, is a function to select
  576. some of the files--those for which (funcall FILTER FILENAME) is non-nil.
  577. If DISTINGUISH-ONE-MARKED is non-nil, then if we find just one marked file,
  578. return (t FILENAME) instead of (FILENAME).
  579. Don't use that together with FILTER."
  580. (let ((all-of-them
  581. (save-excursion
  582. (delq nil (dired-map-over-marks
  583. (dired-get-filename localp 'no-error-if-not-filep)
  584. arg nil distinguish-one-marked))))
  585. result)
  586. (when (equal all-of-them '(t))
  587. (setq all-of-them nil))
  588. (if (not filter)
  589. (if (and distinguish-one-marked (eq (car all-of-them) t))
  590. all-of-them
  591. (nreverse all-of-them))
  592. (dolist (file all-of-them)
  593. (if (funcall filter file)
  594. (push file result)))
  595. result)))
  596. ;; The dired command
  597. (defun dired-read-dir-and-switches (str)
  598. ;; For use in interactive.
  599. (reverse (list
  600. (if current-prefix-arg
  601. (read-string "Dired listing switches: "
  602. dired-listing-switches))
  603. ;; If a dialog is used, call `read-directory-name' so the
  604. ;; dialog code knows we want directories. Some dialogs
  605. ;; can only select directories or files when popped up,
  606. ;; not both. If no dialog is used, call `read-file-name'
  607. ;; because the user may want completion of file names for
  608. ;; use in a wildcard pattern.
  609. (if (next-read-file-uses-dialog-p)
  610. (read-directory-name (format "Dired %s(directory): " str)
  611. nil default-directory nil)
  612. (read-file-name (format "Dired %s(directory): " str)
  613. nil default-directory nil)))))
  614. ;; We want to switch to a more sophisticated version of
  615. ;; dired-read-dir-and-switches like the following, if there is a way
  616. ;; to make it more intuitive. See bug#1285.
  617. ;; (defun dired-read-dir-and-switches (str)
  618. ;; ;; For use in interactive.
  619. ;; (reverse
  620. ;; (list
  621. ;; (if current-prefix-arg
  622. ;; (read-string "Dired listing switches: "
  623. ;; dired-listing-switches))
  624. ;; ;; If a dialog is about to be used, call read-directory-name so
  625. ;; ;; the dialog code knows we want directories. Some dialogs can
  626. ;; ;; only select directories or files when popped up, not both.
  627. ;; (if (next-read-file-uses-dialog-p)
  628. ;; (read-directory-name (format "Dired %s(directory): " str)
  629. ;; nil default-directory nil)
  630. ;; (let ((cie ()))
  631. ;; (dolist (ext completion-ignored-extensions)
  632. ;; (if (eq ?/ (aref ext (1- (length ext)))) (push ext cie)))
  633. ;; (setq cie (concat (regexp-opt cie "\\(?:") "\\'"))
  634. ;; (let* ((default (and buffer-file-name
  635. ;; (abbreviate-file-name buffer-file-name)))
  636. ;; (cie cie)
  637. ;; (completion-table
  638. ;; ;; We need a mix of read-file-name and
  639. ;; ;; read-directory-name so that completion to directories
  640. ;; ;; is preferred, but if the user wants to enter a global
  641. ;; ;; pattern, he can still use completion on filenames to
  642. ;; ;; help him write the pattern.
  643. ;; ;; Essentially, we want to use
  644. ;; ;; (completion-table-with-predicate
  645. ;; ;; 'read-file-name-internal 'file-directory-p nil)
  646. ;; ;; but that doesn't work because read-file-name-internal
  647. ;; ;; does not obey its `predicate' argument.
  648. ;; (completion-table-in-turn
  649. ;; (lambda (str pred action)
  650. ;; (let ((read-file-name-predicate
  651. ;; (lambda (f)
  652. ;; (and (not (member f '("./" "../")))
  653. ;; ;; Hack! Faster than file-directory-p!
  654. ;; (eq (aref f (1- (length f))) ?/)
  655. ;; (not (string-match cie f))))))
  656. ;; (complete-with-action
  657. ;; action 'read-file-name-internal str nil)))
  658. ;; 'read-file-name-internal)))
  659. ;; (minibuffer-with-setup-hook
  660. ;; (lambda ()
  661. ;; (setq minibuffer-default default)
  662. ;; (setq minibuffer-completion-table completion-table))
  663. ;; (read-file-name (format "Dired %s(directory): " str)
  664. ;; nil default-directory nil))))))))
  665. (defun dired-file-name-at-point ()
  666. "Try to get a file name at point in the current dired buffer.
  667. This hook is intended to be put in `file-name-at-point-functions'.
  668. Note that it returns an abbreviated name that can't be used
  669. as an argument to `dired-goto-file'."
  670. (let ((filename (dired-get-filename nil t)))
  671. (when filename
  672. (if (file-directory-p filename)
  673. (file-name-as-directory (abbreviate-file-name filename))
  674. (abbreviate-file-name filename)))))
  675. ;;;###autoload (define-key ctl-x-map "d" 'dired)
  676. ;;;###autoload
  677. (defun dired (dirname &optional switches)
  678. "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
  679. Optional second argument SWITCHES specifies the `ls' options used.
  680. \(Interactively, use a prefix argument to be able to specify SWITCHES.)
  681. If DIRNAME is a string, Dired displays a list of files in DIRNAME (which
  682. may also have shell wildcards appended to select certain files).
  683. If DIRNAME is a cons, its first element is taken as the directory name
  684. and the rest as an explicit list of files to make directory entries for.
  685. In this case, SWITCHES are applied to each of the files separately, and
  686. therefore switches that control the order of the files in the produced
  687. listing have no effect.
  688. \\<dired-mode-map>\
  689. You can flag files for deletion with \\[dired-flag-file-deletion] and then
  690. delete them by typing \\[dired-do-flagged-delete].
  691. Type \\[describe-mode] after entering Dired for more info.
  692. If DIRNAME is already in a Dired buffer, that buffer is used without refresh."
  693. ;; Cannot use (interactive "D") because of wildcards.
  694. (interactive (dired-read-dir-and-switches ""))
  695. (pop-to-buffer-same-window (dired-noselect dirname switches)))
  696. ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
  697. ;;;###autoload
  698. (defun dired-other-window (dirname &optional switches)
  699. "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
  700. (interactive (dired-read-dir-and-switches "in other window "))
  701. (switch-to-buffer-other-window (dired-noselect dirname switches)))
  702. ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
  703. ;;;###autoload
  704. (defun dired-other-frame (dirname &optional switches)
  705. "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
  706. (interactive (dired-read-dir-and-switches "in other frame "))
  707. (switch-to-buffer-other-frame (dired-noselect dirname switches)))
  708. ;;;###autoload
  709. (defun dired-noselect (dir-or-list &optional switches)
  710. "Like `dired' but returns the Dired buffer as value, does not select it."
  711. (or dir-or-list (setq dir-or-list default-directory))
  712. ;; This loses the distinction between "/foo/*/" and "/foo/*" that
  713. ;; some shells make:
  714. (let (dirname initially-was-dirname)
  715. (if (consp dir-or-list)
  716. (setq dirname (car dir-or-list))
  717. (setq dirname dir-or-list))
  718. (setq initially-was-dirname
  719. (string= (file-name-as-directory dirname) dirname))
  720. (setq dirname (abbreviate-file-name
  721. (expand-file-name (directory-file-name dirname))))
  722. (if find-file-visit-truename
  723. (setq dirname (file-truename dirname)))
  724. ;; If the argument was syntactically a directory name not a file name,
  725. ;; or if it happens to name a file that is a directory,
  726. ;; convert it syntactically to a directory name.
  727. ;; The reason for checking initially-was-dirname
  728. ;; and not just file-directory-p
  729. ;; is that file-directory-p is slow over ftp.
  730. (if (or initially-was-dirname (file-directory-p dirname))
  731. (setq dirname (file-name-as-directory dirname)))
  732. (if (consp dir-or-list)
  733. (setq dir-or-list (cons dirname (cdr dir-or-list)))
  734. (setq dir-or-list dirname))
  735. (dired-internal-noselect dir-or-list switches)))
  736. ;; The following is an internal dired function. It returns non-nil if
  737. ;; the directory visited by the current dired buffer has changed on
  738. ;; disk. DIRNAME should be the directory name of that directory.
  739. (defun dired-directory-changed-p (dirname)
  740. (not (let ((attributes (file-attributes dirname))
  741. (modtime (visited-file-modtime)))
  742. (or (eq modtime 0)
  743. (not (eq (car attributes) t))
  744. (equal (nth 5 attributes) modtime)))))
  745. (defun dired-buffer-stale-p (&optional noconfirm)
  746. "Return non-nil if current Dired buffer needs updating.
  747. If NOCONFIRM is non-nil, then this function always returns nil
  748. for a remote directory. This feature is used by Auto Revert mode."
  749. (let ((dirname
  750. (if (consp dired-directory) (car dired-directory) dired-directory)))
  751. (and (stringp dirname)
  752. (not (when noconfirm (file-remote-p dirname)))
  753. (file-readable-p dirname)
  754. ;; Do not auto-revert when the dired buffer can be currently
  755. ;; written by the user as in `wdired-mode'.
  756. buffer-read-only
  757. (dired-directory-changed-p dirname))))
  758. (defcustom dired-auto-revert-buffer nil
  759. "Automatically revert Dired buffer on revisiting.
  760. If t, revisiting an existing Dired buffer automatically reverts it.
  761. If its value is a function, call this function with the directory
  762. name as single argument and revert the buffer if it returns non-nil.
  763. Otherwise, a message offering to revert the changed dired buffer
  764. is displayed.
  765. Note that this is not the same as `auto-revert-mode' that
  766. periodically reverts at specified time intervals."
  767. :type '(choice
  768. (const :tag "Don't revert" nil)
  769. (const :tag "Always revert visited Dired buffer" t)
  770. (const :tag "Revert changed Dired buffer" dired-directory-changed-p)
  771. (function :tag "Predicate function"))
  772. :group 'dired
  773. :version "23.2")
  774. (defun dired--need-align-p ()
  775. "Return non-nil if some file names are misaligned.
  776. The return value is the target column for the file names."
  777. (save-excursion
  778. (goto-char (point-min))
  779. (dired-goto-next-file)
  780. ;; Use point difference instead of `current-column', because
  781. ;; the former works when `dired-hide-details-mode' is enabled.
  782. (let* ((first (- (point) (point-at-bol)))
  783. (target first))
  784. (while (and (not (eobp))
  785. (progn
  786. (forward-line)
  787. (dired-move-to-filename)))
  788. (when-let* ((distance (- (point) (point-at-bol)))
  789. (higher (> distance target)))
  790. (setq target distance)))
  791. (and (/= first target) target))))
  792. (defun dired--align-all-files ()
  793. "Align all files adding spaces in front of the size column."
  794. (let ((target (dired--need-align-p))
  795. (regexp directory-listing-before-filename-regexp))
  796. (when target
  797. (save-excursion
  798. (goto-char (point-min))
  799. (dired-goto-next-file)
  800. (while (dired-move-to-filename)
  801. ;; Use point difference instead of `current-column', because
  802. ;; the former works when `dired-hide-details-mode' is enabled.
  803. (let ((distance (- target (- (point) (point-at-bol))))
  804. (inhibit-read-only t))
  805. (unless (zerop distance)
  806. (re-search-backward regexp nil t)
  807. (goto-char (match-beginning 0))
  808. (search-backward-regexp "[[:space:]]" nil t)
  809. (skip-chars-forward "[:space:]")
  810. (insert-char ?\s distance 'inherit))
  811. (forward-line)))))))
  812. (defun dired-internal-noselect (dir-or-list &optional switches mode)
  813. ;; If DIR-OR-LIST is a string and there is an existing dired buffer
  814. ;; for it, just leave buffer as it is (don't even call dired-revert).
  815. ;; This saves time especially for deep trees or with ange-ftp.
  816. ;; The user can type `g' easily, and it is more consistent with find-file.
  817. ;; But if SWITCHES are given they are probably different from the
  818. ;; buffer's old value, so call dired-sort-other, which does
  819. ;; revert the buffer.
  820. ;; Revert the buffer if DIR-OR-LIST is a cons or `dired-directory'
  821. ;; is a cons and DIR-OR-LIST is a string.
  822. ;; A pity we can't possibly do "Directory has changed - refresh? "
  823. ;; like find-file does.
  824. ;; Optional argument MODE is passed to dired-find-buffer-nocreate,
  825. ;; see there.
  826. (let* ((old-buf (current-buffer))
  827. (dirname (if (consp dir-or-list) (car dir-or-list) dir-or-list))
  828. ;; Look for an existing buffer.
  829. (buffer (dired-find-buffer-nocreate dirname mode))
  830. ;; Note that buffer already is in dired-mode, if found.
  831. (new-buffer-p (null buffer)))
  832. (or buffer
  833. (setq buffer (create-file-buffer (directory-file-name dirname))))
  834. (set-buffer buffer)
  835. (if (not new-buffer-p) ; existing buffer ...
  836. (cond (switches ; ... but new switches
  837. ;; file list may have changed
  838. (setq dired-directory dir-or-list)
  839. ;; this calls dired-revert
  840. (dired-sort-other switches))
  841. ;; Always revert when `dir-or-list' is a cons. Also revert
  842. ;; if `dired-directory' is a cons but `dir-or-list' is not.
  843. ((or (consp dir-or-list) (consp dired-directory))
  844. (setq dired-directory dir-or-list)
  845. (revert-buffer))
  846. ;; Always revert regardless of whether it has changed or not.
  847. ((eq dired-auto-revert-buffer t)
  848. (revert-buffer))
  849. ;; Revert when predicate function returns non-nil.
  850. ((functionp dired-auto-revert-buffer)
  851. (when (funcall dired-auto-revert-buffer dirname)
  852. (revert-buffer)
  853. (message "Changed directory automatically updated")))
  854. ;; If directory has changed on disk, offer to revert.
  855. ((when (dired-directory-changed-p dirname)
  856. (message "%s"
  857. (substitute-command-keys
  858. "Directory has changed on disk; type \\[revert-buffer] to update Dired")))))
  859. ;; Else a new buffer
  860. (setq default-directory
  861. (or (car-safe (insert-directory-wildcard-in-dir-p dirname))
  862. ;; We can do this unconditionally
  863. ;; because dired-noselect ensures that the name
  864. ;; is passed in directory name syntax
  865. ;; if it was the name of a directory at all.
  866. (file-name-directory dirname)))
  867. (or switches (setq switches dired-listing-switches))
  868. (if mode (funcall mode)
  869. (dired-mode dir-or-list switches))
  870. ;; default-directory and dired-actual-switches are set now
  871. ;; (buffer-local), so we can call dired-readin:
  872. (let ((failed t))
  873. (unwind-protect
  874. (progn (dired-readin)
  875. (setq failed nil))
  876. ;; dired-readin can fail if parent directories are inaccessible.
  877. ;; Don't leave an empty buffer around in that case.
  878. (if failed (kill-buffer buffer))))
  879. (goto-char (point-min))
  880. (dired-initial-position dirname))
  881. (when (consp dired-directory)
  882. (dired--align-all-files))
  883. (set-buffer old-buf)
  884. buffer))
  885. (defvar dired-buffers nil
  886. ;; Enlarged by dired-advertise
  887. ;; Queried by function dired-buffers-for-dir. When this detects a
  888. ;; killed buffer, it is removed from this list.
  889. "Alist of expanded directories and their associated Dired buffers.")
  890. (defvar dired-find-subdir)
  891. ;; FIXME add a doc-string, and document dired-x extensions.
  892. (defun dired-find-buffer-nocreate (dirname &optional mode)
  893. ;; This differs from dired-buffers-for-dir in that it does not consider
  894. ;; subdirs of default-directory and searches for the first match only.
  895. ;; Also, the major mode must be MODE.
  896. (if (and (featurep 'dired-x)
  897. dired-find-subdir
  898. ;; Don't try to find a wildcard as a subdirectory.
  899. (string-equal dirname (file-name-directory dirname)))
  900. (let* ((cur-buf (current-buffer))
  901. (buffers (nreverse
  902. (dired-buffers-for-dir (expand-file-name dirname))))
  903. (cur-buf-matches (and (memq cur-buf buffers)
  904. ;; Wildcards must match, too:
  905. (equal dired-directory dirname))))
  906. ;; We don't want to switch to the same buffer---
  907. (setq buffers (delq cur-buf buffers))
  908. (or (car (sort buffers #'dired-buffer-more-recently-used-p))
  909. ;; ---unless it's the only possibility:
  910. (and cur-buf-matches cur-buf)))
  911. ;; No dired-x, or dired-find-subdir nil.
  912. (setq dirname (expand-file-name dirname))
  913. (let (found (blist dired-buffers)) ; was (buffer-list)
  914. (or mode (setq mode 'dired-mode))
  915. (while blist
  916. (if (null (buffer-name (cdr (car blist))))
  917. (setq blist (cdr blist))
  918. (with-current-buffer (cdr (car blist))
  919. (if (and (eq major-mode mode)
  920. dired-directory ;; nil during find-alternate-file
  921. (equal dirname
  922. (expand-file-name
  923. (if (consp dired-directory)
  924. (car dired-directory)
  925. dired-directory))))
  926. (setq found (cdr (car blist))
  927. blist nil)
  928. (setq blist (cdr blist))))))
  929. found)))
  930. ;; Read in a new dired buffer
  931. (defun dired-readin ()
  932. "Read in a new Dired buffer.
  933. Differs from `dired-insert-subdir' in that it accepts
  934. wildcards, erases the buffer, and builds the subdir-alist anew
  935. \(including making it buffer-local and clearing it first)."
  936. ;; default-directory and dired-actual-switches must be buffer-local
  937. ;; and initialized by now.
  938. (let (dirname
  939. ;; This makes readin much much faster.
  940. ;; In particular, it prevents the font lock hook from running
  941. ;; until the directory is all read in.
  942. (inhibit-modification-hooks t))
  943. (if (consp dired-directory)
  944. (setq dirname (car dired-directory))
  945. (setq dirname dired-directory))
  946. (setq dirname (expand-file-name dirname))
  947. (save-excursion
  948. ;; This hook which may want to modify dired-actual-switches
  949. ;; based on dired-directory, e.g. with ange-ftp to a SysV host
  950. ;; where ls won't understand -Al switches.
  951. (run-hooks 'dired-before-readin-hook)
  952. (if (consp buffer-undo-list)
  953. (setq buffer-undo-list nil))
  954. (setq-local file-name-coding-system
  955. (or coding-system-for-read file-name-coding-system))
  956. (let ((inhibit-read-only t)
  957. ;; Don't make undo entries for readin.
  958. (buffer-undo-list t))
  959. (widen)
  960. (erase-buffer)
  961. (dired-readin-insert))
  962. (goto-char (point-min))
  963. ;; Must first make alist buffer local and set it to nil because
  964. ;; dired-build-subdir-alist will call dired-clear-alist first
  965. (setq-local dired-subdir-alist nil)
  966. (dired-build-subdir-alist)
  967. (let ((attributes (file-attributes dirname)))
  968. (if (eq (car attributes) t)
  969. (set-visited-file-modtime (nth 5 attributes))))
  970. (set-buffer-modified-p nil)
  971. ;; No need to narrow since the whole buffer contains just
  972. ;; dired-readin's output, nothing else. The hook can
  973. ;; successfully use dired functions (e.g. dired-get-filename)
  974. ;; as the subdir-alist has been built in dired-readin.
  975. (run-hooks 'dired-after-readin-hook))))
  976. ;; Subroutines of dired-readin
  977. (defun dired-readin-insert ()
  978. ;; Insert listing for the specified dir (and maybe file list)
  979. ;; already in dired-directory, assuming a clean buffer.
  980. (let (dir file-list)
  981. (if (consp dired-directory)
  982. (setq dir (car dired-directory)
  983. file-list (cdr dired-directory))
  984. (setq dir dired-directory
  985. file-list nil))
  986. (setq dir (expand-file-name dir))
  987. (if (and (equal "" (file-name-nondirectory dir))
  988. (not file-list))
  989. ;; If we are reading a whole single directory...
  990. (dired-insert-directory dir dired-actual-switches nil nil t)
  991. (if (and (not (insert-directory-wildcard-in-dir-p dir))
  992. (not (file-readable-p
  993. (directory-file-name (file-name-directory dir)))))
  994. (error "Directory %s inaccessible or nonexistent" dir))
  995. ;; Else treat it as a wildcard spec
  996. ;; unless we have an explicit list of files.
  997. (dired-insert-directory dir dired-actual-switches
  998. file-list (not file-list) t))))
  999. (defun dired-align-file (beg end)
  1000. "Align the fields of a file to the ones of surrounding lines.
  1001. BEG..END is the line where the file info is located."
  1002. ;; Some versions of ls try to adjust the size of each field so as to just
  1003. ;; hold the largest element ("largest" in the current invocation, of
  1004. ;; course). So when a single line is output, the size of each field is
  1005. ;; just big enough for that one output. Thus when dired refreshes one
  1006. ;; line, the alignment if this line w.r.t the rest is messed up because
  1007. ;; the fields of that one line will generally be smaller.
  1008. ;;
  1009. ;; To work around this problem, we here add spaces to try and
  1010. ;; re-align the fields as needed. Since this is purely aesthetic,
  1011. ;; it is of utmost importance that it doesn't mess up anything like
  1012. ;; `dired-move-to-filename'. To this end, we limit ourselves to
  1013. ;; adding spaces only, and to only add them at places where there
  1014. ;; was already at least one space. This way, as long as
  1015. ;; `directory-listing-before-filename-regexp' always matches spaces
  1016. ;; with "*" or "+", we know we haven't made anything worse. There
  1017. ;; is one spot where the exact number of spaces is important, which
  1018. ;; is just before the actual filename, so we refrain from adding
  1019. ;; spaces there (and within the filename as well, of course).
  1020. (save-excursion
  1021. (let (file file-col other other-col)
  1022. ;; Check that there is indeed a file, and that there is another adjacent
  1023. ;; file with which to align, and that additional spaces are needed to
  1024. ;; align the filenames.
  1025. (when (and (setq file (progn (goto-char beg)
  1026. (dired-move-to-filename nil end)))
  1027. (setq file-col (current-column))
  1028. (setq other
  1029. (or (and (goto-char beg)
  1030. (zerop (forward-line -1))
  1031. (dired-move-to-filename))
  1032. (and (goto-char beg)
  1033. (zerop (forward-line 1))
  1034. (dired-move-to-filename))))
  1035. (setq other-col (current-column))
  1036. (/= file other)
  1037. ;; Make sure there is some work left to do.
  1038. (> other-col file-col))
  1039. ;; If we've only looked at the line above, check to see if the line
  1040. ;; below exists as well and if so, align with the shorter one.
  1041. (when (and (< other file)
  1042. (goto-char beg)
  1043. (zerop (forward-line 1))
  1044. (dired-move-to-filename))
  1045. (let ((alt-col (current-column)))
  1046. (when (< alt-col other-col)
  1047. (setq other-col alt-col)
  1048. (setq other (point)))))
  1049. ;; Keep positions uptodate when we insert stuff.
  1050. (if (> other file) (setq other (copy-marker other)))
  1051. (setq file (copy-marker file))
  1052. ;; Main loop.
  1053. (goto-char beg)
  1054. (skip-chars-forward " ") ;Skip to the first field.
  1055. (while (and (> other-col file-col)
  1056. ;; Don't touch anything just before (and after) the
  1057. ;; beginning of the filename.
  1058. (> file (point)))
  1059. ;; We're now just in front of a field, with a space behind us.
  1060. (let* ((curcol (current-column))
  1061. ;; Nums are right-aligned.
  1062. (num-align (looking-at-p "[0-9]"))
  1063. ;; Let's look at the other line, in the same column: we
  1064. ;; should be either near the end of the previous field, or
  1065. ;; in the space between that field and the next.
  1066. ;; [ Of course, it's also possible that we're already within
  1067. ;; the next field or even past it, but that's unlikely since
  1068. ;; other-col > file-col. ]
  1069. ;; Let's find the distance to the alignment-point (either
  1070. ;; the beginning or the end of the next field, depending on
  1071. ;; whether this field is left or right aligned).
  1072. (align-pt-offset
  1073. (save-excursion
  1074. (goto-char other)
  1075. (move-to-column curcol)
  1076. (when (looking-at
  1077. (concat
  1078. (if (eq (char-before) ?\s) " *" "[^ ]* *")
  1079. (if num-align "[0-9][^ ]*")))
  1080. (- (match-end 0) (match-beginning 0)))))
  1081. ;; Now, the number of spaces to insert is align-pt-offset
  1082. ;; minus the distance to the equivalent point on the
  1083. ;; current line.
  1084. (spaces
  1085. (if (not num-align)
  1086. align-pt-offset
  1087. (and align-pt-offset
  1088. (save-excursion
  1089. (skip-chars-forward "^ ")
  1090. (- align-pt-offset (- (current-column) curcol)))))))
  1091. (when (and spaces (> spaces 0))
  1092. (setq file-col (+ spaces file-col))
  1093. (if (> file-col other-col)
  1094. (setq spaces (- spaces (- file-col other-col))))
  1095. (insert-char ?\s spaces 'inherit)
  1096. ;; Let's just make really sure we did not mess up.
  1097. (unless (save-excursion
  1098. (eq (dired-move-to-filename) (marker-position file)))
  1099. ;; Damn! We messed up: let's revert the change.
  1100. (delete-char (- spaces)))))
  1101. ;; Now skip to next field.
  1102. (skip-chars-forward "^ ") (skip-chars-forward " "))
  1103. (set-marker file nil)))))
  1104. (defvar ls-lisp-use-insert-directory-program)
  1105. (defun dired-check-switches (switches short &optional long)
  1106. "Return non-nil if the string SWITCHES matches LONG or SHORT format."
  1107. (let (case-fold-search)
  1108. (and (stringp switches)
  1109. (string-match-p (concat "\\(\\`\\| \\)-[[:alnum:]]*" short
  1110. (if long (concat "\\|--" long "\\>") ""))
  1111. switches))))
  1112. (defun dired-switches-escape-p (switches)
  1113. "Return non-nil if the string SWITCHES contains -b or --escape."
  1114. ;; Do not match things like "--block-size" that happen to contain "b".
  1115. (dired-check-switches switches "b" "escape"))
  1116. (defun dired-switches-recursive-p (switches)
  1117. "Return non-nil if the string SWITCHES contains -R or --recursive."
  1118. (dired-check-switches switches "R" "recursive"))
  1119. (defun dired-insert-directory (dir switches &optional file-list wildcard hdr)
  1120. "Insert a directory listing of DIR, Dired style.
  1121. Use SWITCHES to make the listings.
  1122. If FILE-LIST is non-nil, list only those files.
  1123. Otherwise, if WILDCARD is non-nil, expand wildcards;
  1124. in that case, DIR should be a file name that uses wildcards.
  1125. In other cases, DIR should be a directory name or a directory filename.
  1126. If HDR is non-nil, insert a header line with the directory name."
  1127. (let ((opoint (point))
  1128. (process-environment (copy-sequence process-environment))
  1129. end)
  1130. (if (and
  1131. ;; Don't try to invoke `ls' if we are on DOS/Windows where
  1132. ;; ls-lisp emulation is used, except if they want to use `ls'
  1133. ;; as indicated by `ls-lisp-use-insert-directory-program'.
  1134. (not (and (featurep 'ls-lisp)
  1135. (null ls-lisp-use-insert-directory-program)))
  1136. (not (and (featurep 'eshell)
  1137. (bound-and-true-p eshell-ls-use-in-dired)))
  1138. (or (file-remote-p dir)
  1139. (if (eq dired-use-ls-dired 'unspecified)
  1140. ;; Check whether "ls --dired" gives exit code 0, and
  1141. ;; save the answer in `dired-use-ls-dired'.
  1142. (or (setq dired-use-ls-dired
  1143. (eq 0 (call-process insert-directory-program
  1144. nil nil nil "--dired")))
  1145. (progn
  1146. (message "ls does not support --dired; \
  1147. see `dired-use-ls-dired' for more details.")
  1148. nil))
  1149. dired-use-ls-dired)))
  1150. (setq switches (concat "--dired " switches)))
  1151. ;; Expand directory wildcards and fill file-list.
  1152. (let ((dir-wildcard (insert-directory-wildcard-in-dir-p dir)))
  1153. (cond (dir-wildcard
  1154. (setq switches (concat "-d " switches))
  1155. ;; We don't know whether the remote ls supports
  1156. ;; "--dired", so we cannot add it to the `process-file'
  1157. ;; call for wildcards.
  1158. (when (file-remote-p dir)
  1159. (setq switches (dired-replace-in-string "--dired" "" switches)))
  1160. (let* ((default-directory (car dir-wildcard))
  1161. (script (format "ls %s %s" switches (cdr dir-wildcard)))
  1162. (remotep (file-remote-p dir))
  1163. (sh (or (and remotep "/bin/sh")
  1164. (and (bound-and-true-p explicit-shell-file-name)
  1165. (executable-find explicit-shell-file-name))
  1166. (executable-find "sh")))
  1167. (switch (if remotep "-c" shell-command-switch)))
  1168. (unless
  1169. (zerop
  1170. (process-file sh nil (current-buffer) nil switch script))
  1171. (user-error
  1172. "%s: No files matching wildcard" (cdr dir-wildcard)))
  1173. (insert-directory-clean (point) switches)))
  1174. (t
  1175. ;; We used to specify the C locale here, to force English
  1176. ;; month names; but this should not be necessary any
  1177. ;; more, with the new value of
  1178. ;; `directory-listing-before-filename-regexp'.
  1179. (if file-list
  1180. (dolist (f file-list)
  1181. (let ((beg (point)))
  1182. (insert-directory f switches nil nil)
  1183. ;; Re-align fields, if necessary.
  1184. (dired-align-file beg (point))))
  1185. (insert-directory dir switches wildcard (not wildcard))))))
  1186. ;; Quote certain characters, unless ls quoted them for us.
  1187. (if (not (dired-switches-escape-p dired-actual-switches))
  1188. (save-excursion
  1189. (setq end (point-marker))
  1190. (goto-char opoint)
  1191. (while (search-forward "\\" end t)
  1192. (replace-match (apply #'propertize
  1193. "\\\\"
  1194. (text-properties-at (match-beginning 0)))
  1195. nil t))
  1196. (goto-char opoint)
  1197. (while (search-forward "\^m" end t)
  1198. (replace-match (apply #'propertize
  1199. "\\015"
  1200. (text-properties-at (match-beginning 0)))
  1201. nil t))
  1202. (set-marker end nil))
  1203. ;; Replace any newlines in DIR with literal "\n"s, for the sake
  1204. ;; of the header line. To disambiguate a literal "\n" in the
  1205. ;; actual dirname, we also replace "\" with "\\".
  1206. ;; Personally, I think this should always be done, irrespective
  1207. ;; of the value of dired-actual-switches, because:
  1208. ;; i) Dired simply does not work with an unescaped newline in
  1209. ;; the directory name used in the header (bug=10469#28), and
  1210. ;; ii) "\" is always replaced with "\\" in the listing, so doing
  1211. ;; it in the header as well makes things consistent.
  1212. ;; But at present it is only done if "-b" is in ls-switches,
  1213. ;; because newlines in dirnames are uncommon, and people may
  1214. ;; have gotten used to seeing unescaped "\" in the headers.
  1215. ;; Note: adjust dired-build-subdir-alist if you change this.
  1216. (setq dir (replace-regexp-in-string "\\\\" "\\\\" dir nil t)
  1217. dir (replace-regexp-in-string "\n" "\\n" dir nil t)))
  1218. ;; If we used --dired and it worked, the lines are already indented.
  1219. ;; Otherwise, indent them.
  1220. (unless (save-excursion
  1221. (goto-char opoint)
  1222. (looking-at-p " "))
  1223. (let ((indent-tabs-mode nil))
  1224. (indent-rigidly opoint (point) 2)))
  1225. ;; Insert text at the beginning to standardize things.
  1226. (let ((content-point opoint))
  1227. (save-excursion
  1228. (goto-char opoint)
  1229. (when (and (or hdr wildcard)
  1230. (not (and (looking-at "^ \\(.*\\):$")
  1231. (file-name-absolute-p (match-string 1)))))
  1232. ;; Note that dired-build-subdir-alist will replace the name
  1233. ;; by its expansion, so it does not matter whether what we insert
  1234. ;; here is fully expanded, but it should be absolute.
  1235. (insert " " (or (car-safe (insert-directory-wildcard-in-dir-p dir))
  1236. (directory-file-name (file-name-directory dir))) ":\n")
  1237. (setq content-point (point)))
  1238. (when wildcard
  1239. ;; Insert "wildcard" line where "total" line would be for a full dir.
  1240. (insert " wildcard " (or (cdr-safe (insert-directory-wildcard-in-dir-p dir))
  1241. (file-name-nondirectory dir))
  1242. "\n")))
  1243. (dired-insert-set-properties content-point (point)))))
  1244. (defun dired-insert-set-properties (beg end)
  1245. "Add various text properties to the lines in the region."
  1246. (save-excursion
  1247. (goto-char beg)
  1248. (while (< (point) end)
  1249. (ignore-errors
  1250. (if (not (dired-move-to-filename))
  1251. (unless (or (looking-at-p "^$")
  1252. (looking-at-p dired-subdir-regexp))
  1253. (put-text-property (line-beginning-position)
  1254. (1+ (line-end-position))
  1255. 'invisible 'dired-hide-details-information))
  1256. (put-text-property (+ (line-beginning-position) 1) (1- (point))
  1257. 'invisible 'dired-hide-details-detail)
  1258. (add-text-properties
  1259. (point)
  1260. (progn
  1261. (dired-move-to-end-of-filename)
  1262. (point))
  1263. '(mouse-face
  1264. highlight
  1265. dired-filename t
  1266. help-echo "mouse-2: visit this file in other window"))
  1267. (when (< (+ (point) 4) (line-end-position))
  1268. (put-text-property (+ (point) 4) (line-end-position)
  1269. 'invisible 'dired-hide-details-link))))
  1270. (forward-line 1))))
  1271. ;; Reverting a dired buffer
  1272. (defun dired-revert (&optional _arg _noconfirm)
  1273. "Reread the Dired buffer.
  1274. Must also be called after `dired-actual-switches' have changed.
  1275. Should not fail even on completely garbaged buffers.
  1276. Preserves old cursor, marks/flags, hidden-p.
  1277. Dired sets `revert-buffer-function' to this function. The args
  1278. ARG and NOCONFIRM, passed from `revert-buffer', are ignored."
  1279. (widen) ; just in case user narrowed
  1280. (let ((modflag (buffer-modified-p))
  1281. (positions (dired-save-positions))
  1282. (mark-alist nil) ; save marked files
  1283. (hidden-subdirs (dired-remember-hidden))
  1284. (old-subdir-alist (cdr (reverse dired-subdir-alist))) ; except pwd
  1285. (case-fold-search nil) ; we check for upper case ls flags
  1286. (inhibit-read-only t))
  1287. (goto-char (point-min))
  1288. (setq mark-alist;; only after dired-remember-hidden since this unhides:
  1289. (dired-remember-marks (point-min) (point-max)))
  1290. ;; treat top level dir extra (it may contain wildcards)
  1291. (if (not (consp dired-directory))
  1292. (dired-uncache dired-directory)
  1293. (dired-uncache (car dired-directory))
  1294. (dolist (dir (cdr dired-directory))
  1295. (if (file-name-absolute-p dir)
  1296. (dired-uncache dir))))
  1297. ;; Run dired-after-readin-hook just once, below.
  1298. (let ((dired-after-readin-hook nil))
  1299. (dired-readin)
  1300. (dired-insert-old-subdirs old-subdir-alist))
  1301. (dired-mark-remembered mark-alist) ; mark files that were marked
  1302. ;; ... run the hook for the whole buffer, and only after markers
  1303. ;; have been reinserted (else omitting in dired-x would omit marked files)
  1304. (run-hooks 'dired-after-readin-hook) ; no need to narrow
  1305. (dired-restore-positions positions)
  1306. (save-excursion ; hide subdirs that were hidden
  1307. (dolist (dir hidden-subdirs)
  1308. (if (dired-goto-subdir dir)
  1309. (dired-hide-subdir 1))))
  1310. (unless modflag (restore-buffer-modified-p nil)))
  1311. ;; outside of the let scope
  1312. ;;; Might as well not override the user if the user changed this.
  1313. ;;; (setq buffer-read-only t)
  1314. )
  1315. ;; Subroutines of dired-revert
  1316. ;; Some of these are also used when inserting subdirs.
  1317. (defun dired-save-positions ()
  1318. "Return current positions in the buffer and all windows with this directory.
  1319. The positions have the form (BUFFER-POSITION WINDOW-POSITIONS).
  1320. BUFFER-POSITION is the point position in the current Dired buffer.
  1321. It has the form (BUFFER DIRED-FILENAME BUFFER-LINE-NUMBER).
  1322. WINDOW-POSITIONS are current positions in all windows displaying
  1323. this dired buffer. The window positions have the form (WINDOW
  1324. DIRED-FILENAME WINDOW-LINE-NUMBER).
  1325. We store line numbers instead of point positions because the header
  1326. lines might change as well: when this happen the line number doesn't
  1327. change; the point does."
  1328. (list
  1329. (list (current-buffer) (dired-get-filename nil t) (line-number-at-pos))
  1330. (mapcar (lambda (w)
  1331. (with-selected-window w
  1332. (list w
  1333. (dired-get-filename nil t)
  1334. (line-number-at-pos (window-point w)))))
  1335. (get-buffer-window-list nil 0 t))))
  1336. (defun dired-restore-positions (positions)
  1337. "Restore POSITIONS saved with `dired-save-positions'."
  1338. (let* ((buf-file-pos (nth 0 positions))
  1339. (buffer (nth 0 buf-file-pos)))
  1340. (unless (and (nth 1 buf-file-pos)
  1341. (dired-goto-file (nth 1 buf-file-pos)))
  1342. (goto-char (point-min))
  1343. (forward-line (1- (nth 2 buf-file-pos)))
  1344. (dired-move-to-filename))
  1345. (dolist (win-file-pos (nth 1 positions))
  1346. ;; Ensure that window still displays the original buffer.
  1347. (when (eq (window-buffer (nth 0 win-file-pos)) buffer)
  1348. (with-selected-window (nth 0 win-file-pos)
  1349. (unless (and (nth 1 win-file-pos)
  1350. (dired-goto-file (nth 1 win-file-pos)))
  1351. (goto-char (point-min))
  1352. (forward-line (1- (nth 2 win-file-pos)))
  1353. (dired-move-to-filename)))))))
  1354. (defun dired-remember-marks (beg end)
  1355. "Return alist of files and their marks, from BEG to END."
  1356. (if selective-display ; must unhide to make this work.
  1357. (let ((inhibit-read-only t))
  1358. (subst-char-in-region beg end ?\r ?\n)))
  1359. (let (fil chr alist)
  1360. (save-excursion
  1361. (goto-char beg)
  1362. (while (re-search-forward dired-re-mark end t)
  1363. (if (setq fil (dired-get-filename nil t))
  1364. (setq chr (preceding-char)
  1365. alist (cons (cons fil chr) alist)))))
  1366. alist))
  1367. (defun dired-mark-remembered (alist)
  1368. "Mark all files remembered in ALIST.
  1369. Each element of ALIST looks like (FILE . MARKERCHAR)."
  1370. (let (elt fil chr)
  1371. (save-excursion
  1372. (while alist
  1373. (setq elt (car alist)
  1374. alist (cdr alist)
  1375. fil (car elt)
  1376. chr (cdr elt))
  1377. (when (dired-goto-file fil)
  1378. (beginning-of-line)
  1379. (delete-char 1)
  1380. (insert chr))))))
  1381. (defun dired-remember-hidden ()
  1382. "Return a list of names of subdirs currently hidden."
  1383. (let ((l dired-subdir-alist) dir pos result)
  1384. (while l
  1385. (setq dir (car (car l))
  1386. pos (cdr (car l))
  1387. l (cdr l))
  1388. (goto-char pos)
  1389. (skip-chars-forward "^\r\n")
  1390. (if (eq (following-char) ?\r)
  1391. (setq result (cons dir result))))
  1392. result))
  1393. (defun dired-insert-old-subdirs (old-subdir-alist)
  1394. "Try to insert all subdirs that were displayed before.
  1395. Do so according to the former subdir alist OLD-SUBDIR-ALIST."
  1396. (or (dired-switches-recursive-p dired-actual-switches)
  1397. (let (elt dir)
  1398. (while old-subdir-alist
  1399. (setq elt (car old-subdir-alist)
  1400. old-subdir-alist (cdr old-subdir-alist)
  1401. dir (car elt))
  1402. (ignore-errors
  1403. (dired-uncache dir)
  1404. (dired-insert-subdir dir))))))
  1405. (defun dired-uncache (dir)
  1406. "Remove directory DIR from any directory cache."
  1407. (let ((handler (find-file-name-handler dir 'dired-uncache)))
  1408. (if handler
  1409. (funcall handler 'dired-uncache dir))))
  1410. ;; dired mode key bindings and initialization
  1411. (defvar dired-mode-map
  1412. ;; This looks ugly when substitute-command-keys uses C-d instead d:
  1413. ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
  1414. (let ((map (make-keymap)))
  1415. (set-keymap-parent map special-mode-map)
  1416. (define-key map [mouse-2] 'dired-mouse-find-file-other-window)
  1417. (define-key map [follow-link] 'mouse-face)
  1418. ;; Commands to mark or flag certain categories of files
  1419. (define-key map "#" 'dired-flag-auto-save-files)
  1420. (define-key map "." 'dired-clean-directory)
  1421. (define-key map "~" 'dired-flag-backup-files)
  1422. ;; Upper case keys (except !) for operating on the marked files
  1423. (define-key map "A" 'dired-do-find-regexp)
  1424. (define-key map "C" 'dired-do-copy)
  1425. (define-key map "B" 'dired-do-byte-compile)
  1426. (define-key map "D" 'dired-do-delete)
  1427. (define-key map "G" 'dired-do-chgrp)
  1428. (define-key map "H" 'dired-do-hardlink)
  1429. (define-key map "L" 'dired-do-load)
  1430. (define-key map "M" 'dired-do-chmod)
  1431. (define-key map "O" 'dired-do-chown)
  1432. (define-key map "P" 'dired-do-print)
  1433. (define-key map "Q" 'dired-do-find-regexp-and-replace)
  1434. (define-key map "R" 'dired-do-rename)
  1435. (define-key map "S" 'dired-do-symlink)
  1436. (define-key map "T" 'dired-do-touch)
  1437. (define-key map "X" 'dired-do-shell-command)
  1438. (define-key map "Z" 'dired-do-compress)
  1439. (define-key map "c" 'dired-do-compress-to)
  1440. (define-key map "!" 'dired-do-shell-command)
  1441. (define-key map "&" 'dired-do-async-shell-command)
  1442. ;; Comparison commands
  1443. (define-key map "=" 'dired-diff)
  1444. ;; Tree Dired commands
  1445. (define-key map "\M-\C-?" 'dired-unmark-all-files)
  1446. (define-key map "\M-\C-d" 'dired-tree-down)
  1447. (define-key map "\M-\C-u" 'dired-tree-up)
  1448. (define-key map "\M-\C-n" 'dired-next-subdir)
  1449. (define-key map "\M-\C-p" 'dired-prev-subdir)
  1450. ;; move to marked files
  1451. (define-key map "\M-{" 'dired-prev-marked-file)
  1452. (define-key map "\M-}" 'dired-next-marked-file)
  1453. ;; Make all regexp commands share a `%' prefix:
  1454. ;; We used to get to the submap via a symbol dired-regexp-prefix,
  1455. ;; but that seems to serve little purpose, and copy-keymap
  1456. ;; does a better job without it.
  1457. (define-key map "%" nil)
  1458. (define-key map "%u" 'dired-upcase)
  1459. (define-key map "%l" 'dired-downcase)
  1460. (define-key map "%d" 'dired-flag-files-regexp)
  1461. (define-key map "%g" 'dired-mark-files-containing-regexp)
  1462. (define-key map "%m" 'dired-mark-files-regexp)
  1463. (define-key map "%r" 'dired-do-rename-regexp)
  1464. (define-key map "%C" 'dired-do-copy-regexp)
  1465. (define-key map "%H" 'dired-do-hardlink-regexp)
  1466. (define-key map "%R" 'dired-do-rename-regexp)
  1467. (define-key map "%S" 'dired-do-symlink-regexp)
  1468. (define-key map "%&" 'dired-flag-garbage-files)
  1469. ;; Commands for marking and unmarking.
  1470. (define-key map "*" nil)
  1471. (define-key map "**" 'dired-mark-executables)
  1472. (define-key map "*/" 'dired-mark-directories)
  1473. (define-key map "*@" 'dired-mark-symlinks)
  1474. (define-key map "*%" 'dired-mark-files-regexp)
  1475. (define-key map "*c" 'dired-change-marks)
  1476. (define-key map "*s" 'dired-mark-subdir-files)
  1477. (define-key map "*m" 'dired-mark)
  1478. (define-key map "*u" 'dired-unmark)
  1479. (define-key map "*?" 'dired-unmark-all-files)
  1480. (define-key map "*!" 'dired-unmark-all-marks)
  1481. (define-key map "U" 'dired-unmark-all-marks)
  1482. (define-key map "*\177" 'dired-unmark-backward)
  1483. (define-key map "*\C-n" 'dired-next-marked-file)
  1484. (define-key map "*\C-p" 'dired-prev-marked-file)
  1485. (define-key map "*t" 'dired-toggle-marks)
  1486. ;; Lower keys for commands not operating on all the marked files
  1487. (define-key map "a" 'dired-find-alternate-file)
  1488. (define-key map "d" 'dired-flag-file-deletion)
  1489. (define-key map "e" 'dired-find-file)
  1490. (define-key map "f" 'dired-find-file)
  1491. (define-key map "\C-m" 'dired-find-file)
  1492. (put 'dired-find-file :advertised-binding "\C-m")
  1493. (define-key map "g" 'revert-buffer)
  1494. (define-key map "i" 'dired-maybe-insert-subdir)
  1495. (define-key map "j" 'dired-goto-file)
  1496. (define-key map "k" 'dired-do-kill-lines)
  1497. (define-key map "l" 'dired-do-redisplay)
  1498. (define-key map "m" 'dired-mark)
  1499. (define-key map "n" 'dired-next-line)
  1500. (define-key map "o" 'dired-find-file-other-window)
  1501. (define-key map "\C-o" 'dired-display-file)
  1502. (define-key map "p" 'dired-previous-line)
  1503. (define-key map "s" 'dired-sort-toggle-or-edit)
  1504. (define-key map "t" 'dired-toggle-marks)
  1505. (define-key map "u" 'dired-unmark)
  1506. (define-key map "v" 'dired-view-file)
  1507. (define-key map "w" 'dired-copy-filename-as-kill)
  1508. (define-key map "W" 'browse-url-of-dired-file)
  1509. (define-key map "x" 'dired-do-flagged-delete)
  1510. (define-key map "y" 'dired-show-file-type)
  1511. (define-key map "+" 'dired-create-directory)
  1512. ;; moving
  1513. (define-key map "<" 'dired-prev-dirline)
  1514. (define-key map ">" 'dired-next-dirline)
  1515. (define-key map "^" 'dired-up-directory)
  1516. (define-key map " " 'dired-next-line)
  1517. (define-key map [?\S-\ ] 'dired-previous-line)
  1518. (define-key map [remap next-line] 'dired-next-line)
  1519. (define-key map [remap previous-line] 'dired-previous-line)
  1520. ;; hiding
  1521. (define-key map "$" 'dired-hide-subdir)
  1522. (define-key map "\M-$" 'dired-hide-all)
  1523. (define-key map "(" 'dired-hide-details-mode)
  1524. ;; isearch
  1525. (define-key map (kbd "M-s a C-s") 'dired-do-isearch)
  1526. (define-key map (kbd "M-s a M-C-s") 'dired-do-isearch-regexp)
  1527. (define-key map (kbd "M-s f C-s") 'dired-isearch-filenames)
  1528. (define-key map (kbd "M-s f M-C-s") 'dired-isearch-filenames-regexp)
  1529. ;; misc
  1530. (define-key map [remap read-only-mode] 'dired-toggle-read-only)
  1531. ;; `toggle-read-only' is an obsolete alias for `read-only-mode'
  1532. (define-key map [remap toggle-read-only] 'dired-toggle-read-only)
  1533. (define-key map "?" 'dired-summary)
  1534. (define-key map "\177" 'dired-unmark-backward)
  1535. (define-key map [remap undo] 'dired-undo)
  1536. (define-key map [remap advertised-undo] 'dired-undo)
  1537. ;; thumbnail manipulation (image-dired)
  1538. (define-key map "\C-td" 'image-dired-display-thumbs)
  1539. (define-key map "\C-tt" 'image-dired-tag-files)
  1540. (define-key map "\C-tr" 'image-dired-delete-tag)
  1541. (define-key map "\C-tj" 'image-dired-jump-thumbnail-buffer)
  1542. (define-key map "\C-ti" 'image-dired-dired-display-image)
  1543. (define-key map "\C-tx" 'image-dired-dired-display-external)
  1544. (define-key map "\C-ta" 'image-dired-display-thumbs-append)
  1545. (define-key map "\C-t." 'image-dired-display-thumb)
  1546. (define-key map "\C-tc" 'image-dired-dired-comment-files)
  1547. (define-key map "\C-tf" 'image-dired-mark-tagged-files)
  1548. (define-key map "\C-t\C-t" 'image-dired-dired-toggle-marked-thumbs)
  1549. (define-key map "\C-te" 'image-dired-dired-edit-comment-and-tags)
  1550. ;; encryption and decryption (epa-dired)
  1551. (define-key map ":d" 'epa-dired-do-decrypt)
  1552. (define-key map ":v" 'epa-dired-do-verify)
  1553. (define-key map ":s" 'epa-dired-do-sign)
  1554. (define-key map ":e" 'epa-dired-do-encrypt)
  1555. ;; Make menu bar items.
  1556. ;; No need to fo this, now that top-level items are fewer.
  1557. ;;;;
  1558. ;; Get rid of the Edit menu bar item to save space.
  1559. ;(define-key map [menu-bar edit] 'undefined)
  1560. (define-key map [menu-bar subdir]
  1561. (cons "Subdir" (make-sparse-keymap "Subdir")))
  1562. (define-key map [menu-bar subdir hide-all]
  1563. '(menu-item "Hide All" dired-hide-all
  1564. :help "Hide all subdirectories, leave only header lines"))
  1565. (define-key map [menu-bar subdir hide-subdir]
  1566. '(menu-item "Hide/UnHide Subdir" dired-hide-subdir
  1567. :help "Hide or unhide current directory listing"))
  1568. (define-key map [menu-bar subdir tree-down]
  1569. '(menu-item "Tree Down" dired-tree-down
  1570. :help "Go to first subdirectory header down the tree"))
  1571. (define-key map [menu-bar subdir tree-up]
  1572. '(menu-item "Tree Up" dired-tree-up
  1573. :help "Go to first subdirectory header up the tree"))
  1574. (define-key map [menu-bar subdir up]
  1575. '(menu-item "Up Directory" dired-up-directory
  1576. :help "Edit the parent directory"))
  1577. (define-key map [menu-bar subdir prev-subdir]
  1578. '(menu-item "Prev Subdir" dired-prev-subdir
  1579. :help "Go to previous subdirectory header line"))
  1580. (define-key map [menu-bar subdir next-subdir]
  1581. '(menu-item "Next Subdir" dired-next-subdir
  1582. :help "Go to next subdirectory header line"))
  1583. (define-key map [menu-bar subdir prev-dirline]
  1584. '(menu-item "Prev Dirline" dired-prev-dirline
  1585. :help "Move to next directory-file line"))
  1586. (define-key map [menu-bar subdir next-dirline]
  1587. '(menu-item "Next Dirline" dired-next-dirline
  1588. :help "Move to previous directory-file line"))
  1589. (define-key map [menu-bar subdir insert]
  1590. '(menu-item "Insert This Subdir" dired-maybe-insert-subdir
  1591. :help "Insert contents of subdirectory"
  1592. :enable (let ((f (dired-get-filename nil t)))
  1593. (and f (file-directory-p f)))))
  1594. (define-key map [menu-bar immediate]
  1595. (cons "Immediate" (make-sparse-keymap "Immediate")))
  1596. (define-key map
  1597. [menu-bar immediate image-dired-dired-display-external]
  1598. '(menu-item "Display Image Externally" image-dired-dired-display-external
  1599. :help "Display image in external viewer"))
  1600. (define-key map
  1601. [menu-bar immediate image-dired-dired-display-image]
  1602. '(menu-item "Display Image" image-dired-dired-display-image
  1603. :help "Display sized image in a separate window"))
  1604. (define-key map
  1605. [menu-bar immediate image-dired-dired-toggle-marked-thumbs]
  1606. '(menu-item "Toggle Image Thumbnails in This Buffer" image-dired-dired-toggle-marked-thumbs
  1607. :help "Add or remove image thumbnails in front of marked file names"))
  1608. (define-key map [menu-bar immediate hide-details]
  1609. '(menu-item "Hide Details" dired-hide-details-mode
  1610. :help "Hide details in buffer"
  1611. :button (:toggle . dired-hide-details-mode)))
  1612. (define-key map [menu-bar immediate revert-buffer]
  1613. '(menu-item "Refresh" revert-buffer
  1614. :help "Update contents of shown directories"))
  1615. (define-key map [menu-bar immediate dashes]
  1616. '("--"))
  1617. (define-key map [menu-bar immediate isearch-filenames-regexp]
  1618. '(menu-item "Isearch Regexp in File Names..." dired-isearch-filenames-regexp
  1619. :help "Incrementally search for regexp in file names only"))
  1620. (define-key map [menu-bar immediate isearch-filenames]
  1621. '(menu-item "Isearch in File Names..." dired-isearch-filenames
  1622. :help "Incrementally search for string in file names only."))
  1623. (define-key map [menu-bar immediate compare-directories]
  1624. '(menu-item "Compare Directories..." dired-compare-directories
  1625. :help "Mark files with different attributes in two Dired buffers"))
  1626. (define-key map [menu-bar immediate backup-diff]
  1627. '(menu-item "Compare with Backup" dired-backup-diff
  1628. :help "Diff file at cursor with its latest backup"))
  1629. (define-key map [menu-bar immediate diff]
  1630. '(menu-item "Diff..." dired-diff
  1631. :help "Compare file at cursor with another file"))
  1632. (define-key map [menu-bar immediate view]
  1633. '(menu-item "View This File" dired-view-file
  1634. :help "Examine file at cursor in read-only mode"))
  1635. (define-key map [menu-bar immediate display]
  1636. '(menu-item "Display in Other Window" dired-display-file
  1637. :help "Display file at cursor in other window"))
  1638. (define-key map [menu-bar immediate find-file-other-window]
  1639. '(menu-item "Find in Other Window" dired-find-file-other-window
  1640. :help "Edit file at cursor in other window"))
  1641. (define-key map [menu-bar immediate find-file]
  1642. '(menu-item "Find This File" dired-find-file
  1643. :help "Edit file at cursor"))
  1644. (define-key map [menu-bar immediate create-directory]
  1645. '(menu-item "Create Directory..." dired-create-directory
  1646. :help "Create a directory"))
  1647. (define-key map [menu-bar immediate wdired-mode]
  1648. '(menu-item "Edit File Names" wdired-change-to-wdired-mode
  1649. :help "Put a Dired buffer in a mode in which filenames are editable"
  1650. :keys "C-x C-q"
  1651. :filter (lambda (x) (if (eq major-mode 'dired-mode) x))))
  1652. (define-key map [menu-bar regexp]
  1653. (cons "Regexp" (make-sparse-keymap "Regexp")))
  1654. (define-key map
  1655. [menu-bar regexp image-dired-mark-tagged-files]
  1656. '(menu-item "Mark From Image Tag..." image-dired-mark-tagged-files
  1657. :help "Mark files whose image tags matches regexp"))
  1658. (define-key map [menu-bar regexp dashes-1]
  1659. '("--"))
  1660. (define-key map [menu-bar regexp downcase]
  1661. '(menu-item "Downcase" dired-downcase
  1662. ;; When running on plain MS-DOS, there's only one
  1663. ;; letter-case for file names.
  1664. :enable (or (not (fboundp 'msdos-long-file-names))
  1665. (msdos-long-file-names))
  1666. :help "Rename marked files to lower-case name"))
  1667. (define-key map [menu-bar regexp upcase]
  1668. '(menu-item "Upcase" dired-upcase
  1669. :enable (or (not (fboundp 'msdos-long-file-names))
  1670. (msdos-long-file-names))
  1671. :help "Rename marked files to upper-case name"))
  1672. (define-key map [menu-bar regexp hardlink]
  1673. '(menu-item "Hardlink..." dired-do-hardlink-regexp
  1674. :help "Make hard links for files matching regexp"))
  1675. (define-key map [menu-bar regexp symlink]
  1676. '(menu-item "Symlink..." dired-do-symlink-regexp
  1677. :visible (fboundp 'make-symbolic-link)
  1678. :help "Make symbolic links for files matching regexp"))
  1679. (define-key map [menu-bar regexp rename]
  1680. '(menu-item "Rename..." dired-do-rename-regexp
  1681. :help "Rename marked files matching regexp"))
  1682. (define-key map [menu-bar regexp copy]
  1683. '(menu-item "Copy..." dired-do-copy-regexp
  1684. :help "Copy marked files matching regexp"))
  1685. (define-key map [menu-bar regexp flag]
  1686. '(menu-item "Flag..." dired-flag-files-regexp
  1687. :help "Flag files matching regexp for deletion"))
  1688. (define-key map [menu-bar regexp mark]
  1689. '(menu-item "Mark..." dired-mark-files-regexp
  1690. :help "Mark files matching regexp for future operations"))
  1691. (define-key map [menu-bar regexp mark-cont]
  1692. '(menu-item "Mark Containing..." dired-mark-files-containing-regexp
  1693. :help "Mark files whose contents matches regexp"))
  1694. (define-key map [menu-bar mark]
  1695. (cons "Mark" (make-sparse-keymap "Mark")))
  1696. (define-key map [menu-bar mark prev]
  1697. '(menu-item "Previous Marked" dired-prev-marked-file
  1698. :help "Move to previous marked file"))
  1699. (define-key map [menu-bar mark next]
  1700. '(menu-item "Next Marked" dired-next-marked-file
  1701. :help "Move to next marked file"))
  1702. (define-key map [menu-bar mark marks]
  1703. '(menu-item "Change Marks..." dired-change-marks
  1704. :help "Replace marker with another character"))
  1705. (define-key map [menu-bar mark unmark-all]
  1706. '(menu-item "Unmark All" dired-unmark-all-marks))
  1707. (define-key map [menu-bar mark symlinks]
  1708. '(menu-item "Mark Symlinks" dired-mark-symlinks
  1709. :visible (fboundp 'make-symbolic-link)
  1710. :help "Mark all symbolic links"))
  1711. (define-key map [menu-bar mark directories]
  1712. '(menu-item "Mark Directories" dired-mark-directories
  1713. :help "Mark all directories except `.' and `..'"))
  1714. (define-key map [menu-bar mark directory]
  1715. '(menu-item "Mark Old Backups" dired-clean-directory
  1716. :help "Flag old numbered backups for deletion"))
  1717. (define-key map [menu-bar mark executables]
  1718. '(menu-item "Mark Executables" dired-mark-executables
  1719. :help "Mark all executable files"))
  1720. (define-key map [menu-bar mark garbage-files]
  1721. '(menu-item "Flag Garbage Files" dired-flag-garbage-files
  1722. :help "Flag unneeded files for deletion"))
  1723. (define-key map [menu-bar mark backup-files]
  1724. '(menu-item "Flag Backup Files" dired-flag-backup-files
  1725. :help "Flag all backup files for deletion"))
  1726. (define-key map [menu-bar mark auto-save-files]
  1727. '(menu-item "Flag Auto-save Files" dired-flag-auto-save-files
  1728. :help "Flag auto-save files for deletion"))
  1729. (define-key map [menu-bar mark deletion]
  1730. '(menu-item "Flag" dired-flag-file-deletion
  1731. :help "Flag current line's file for deletion"))
  1732. (define-key map [menu-bar mark unmark]
  1733. '(menu-item "Unmark" dired-unmark
  1734. :help "Unmark or unflag current line's file"))
  1735. (define-key map [menu-bar mark mark]
  1736. '(menu-item "Mark" dired-mark
  1737. :help "Mark current line's file for future operations"))
  1738. (define-key map [menu-bar mark toggle-marks]
  1739. '(menu-item "Toggle Marks" dired-toggle-marks
  1740. :help "Mark unmarked files, unmark marked ones"))
  1741. (define-key map [menu-bar operate]
  1742. (cons "Operate" (make-sparse-keymap "Operate")))
  1743. (define-key map
  1744. [menu-bar operate image-dired-delete-tag]
  1745. '(menu-item "Delete Image Tag..." image-dired-delete-tag
  1746. :help "Delete image tag from current or marked files"))
  1747. (define-key map
  1748. [menu-bar operate image-dired-tag-files]
  1749. '(menu-item "Add Image Tags..." image-dired-tag-files
  1750. :help "Add image tags to current or marked files"))
  1751. (define-key map
  1752. [menu-bar operate image-dired-dired-comment-files]
  1753. '(menu-item "Add Image Comment..." image-dired-dired-comment-files
  1754. :help "Add image comment to current or marked files"))
  1755. (define-key map
  1756. [menu-bar operate image-dired-display-thumbs]
  1757. '(menu-item "Display Image Thumbnails" image-dired-display-thumbs
  1758. :help "Display image thumbnails for current or marked image files"))
  1759. (define-key map [menu-bar operate dashes-4]
  1760. '("--"))
  1761. (define-key map
  1762. [menu-bar operate epa-dired-do-decrypt]
  1763. '(menu-item "Decrypt..." epa-dired-do-decrypt
  1764. :help "Decrypt current or marked files"))
  1765. (define-key map
  1766. [menu-bar operate epa-dired-do-verify]
  1767. '(menu-item "Verify" epa-dired-do-verify
  1768. :help "Verify digital signature of current or marked files"))
  1769. (define-key map
  1770. [menu-bar operate epa-dired-do-sign]
  1771. '(menu-item "Sign..." epa-dired-do-sign
  1772. :help "Create digital signature of current or marked files"))
  1773. (define-key map
  1774. [menu-bar operate epa-dired-do-encrypt]
  1775. '(menu-item "Encrypt..." epa-dired-do-encrypt
  1776. :help "Encrypt current or marked files"))
  1777. (define-key map [menu-bar operate dashes-3]
  1778. '("--"))
  1779. (define-key map [menu-bar operate query-replace]
  1780. '(menu-item "Query Replace in Files..." dired-do-find-regexp-and-replace
  1781. :help "Replace regexp matches in marked files"))
  1782. (define-key map [menu-bar operate search]
  1783. '(menu-item "Search Files..." dired-do-find-regexp
  1784. :help "Search marked files for matches of regexp"))
  1785. (define-key map [menu-bar operate isearch-regexp]
  1786. '(menu-item "Isearch Regexp Files..." dired-do-isearch-regexp
  1787. :help "Incrementally search marked files for regexp"))
  1788. (define-key map [menu-bar operate isearch]
  1789. '(menu-item "Isearch Files..." dired-do-isearch
  1790. :help "Incrementally search marked files for string"))
  1791. (define-key map [menu-bar operate chown]
  1792. '(menu-item "Change Owner..." dired-do-chown
  1793. :visible (not (memq system-type '(ms-dos windows-nt)))
  1794. :help "Change the owner of marked files"))
  1795. (define-key map [menu-bar operate chgrp]
  1796. '(menu-item "Change Group..." dired-do-chgrp
  1797. :visible (not (memq system-type '(ms-dos windows-nt)))
  1798. :help "Change the group of marked files"))
  1799. (define-key map [menu-bar operate chmod]
  1800. '(menu-item "Change Mode..." dired-do-chmod
  1801. :help "Change mode (attributes) of marked files"))
  1802. (define-key map [menu-bar operate touch]
  1803. '(menu-item "Change Timestamp..." dired-do-touch
  1804. :help "Change timestamp of marked files"))
  1805. (define-key map [menu-bar operate load]
  1806. '(menu-item "Load" dired-do-load
  1807. :help "Load marked Emacs Lisp files"))
  1808. (define-key map [menu-bar operate compile]
  1809. '(menu-item "Byte-compile" dired-do-byte-compile
  1810. :help "Byte-compile marked Emacs Lisp files"))
  1811. (define-key map [menu-bar operate compress]
  1812. '(menu-item "Compress" dired-do-compress
  1813. :help "Compress/uncompress marked files"))
  1814. (define-key map [menu-bar operate print]
  1815. '(menu-item "Print..." dired-do-print
  1816. :help "Ask for print command and print marked files"))
  1817. (define-key map [menu-bar operate hardlink]
  1818. '(menu-item "Hardlink to..." dired-do-hardlink
  1819. :help "Make hard links for current or marked files"))
  1820. (define-key map [menu-bar operate symlink]
  1821. '(menu-item "Symlink to..." dired-do-symlink
  1822. :visible (fboundp 'make-symbolic-link)
  1823. :help "Make symbolic links for current or marked files"))
  1824. (define-key map [menu-bar operate async-command]
  1825. '(menu-item "Asynchronous Shell Command..." dired-do-async-shell-command
  1826. :help "Run a shell command asynchronously on current or marked files"))
  1827. (define-key map [menu-bar operate command]
  1828. '(menu-item "Shell Command..." dired-do-shell-command
  1829. :help "Run a shell command on current or marked files"))
  1830. (define-key map [menu-bar operate delete]
  1831. '(menu-item "Delete" dired-do-delete
  1832. :help "Delete current file or all marked files"))
  1833. (define-key map [menu-bar operate rename]
  1834. '(menu-item "Rename to..." dired-do-rename
  1835. :help "Rename current file or move marked files"))
  1836. (define-key map [menu-bar operate copy]
  1837. '(menu-item "Copy to..." dired-do-copy
  1838. :help "Copy current file or all marked files"))
  1839. map)
  1840. "Local keymap for Dired mode buffers.")
  1841. ;; Dired mode is suitable only for specially formatted data.
  1842. (put 'dired-mode 'mode-class 'special)
  1843. ;; Autoload cookie needed by desktop.el
  1844. ;;;###autoload
  1845. (defun dired-mode (&optional dirname switches)
  1846. "\
  1847. Mode for \"editing\" directory listings.
  1848. In Dired, you are \"editing\" a list of the files in a directory and
  1849. (optionally) its subdirectories, in the format of `ls -lR'.
  1850. Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
  1851. \"Editing\" means that you can run shell commands on files, visit,
  1852. compress, load or byte-compile them, change their file attributes
  1853. and insert subdirectories into the same buffer. You can \"mark\"
  1854. files for later commands or \"flag\" them for deletion, either file
  1855. by file or all files matching certain criteria.
  1856. You can move using the usual cursor motion commands.\\<dired-mode-map>
  1857. The buffer is read-only. Digits are prefix arguments.
  1858. Type \\[dired-flag-file-deletion] to flag a file `D' for deletion.
  1859. Type \\[dired-mark] to Mark a file or subdirectory for later commands.
  1860. Most commands operate on the marked files and use the current file
  1861. if no files are marked. Use a numeric prefix argument to operate on
  1862. the next ARG (or previous -ARG if ARG<0) files, or just `1'
  1863. to operate on the current file only. Prefix arguments override marks.
  1864. Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
  1865. to see why something went wrong.
  1866. Type \\[dired-unmark] to Unmark a file or all files of an inserted subdirectory.
  1867. Type \\[dired-unmark-backward] to back up one line and unmark or unflag.
  1868. Type \\[dired-do-flagged-delete] to delete (eXpunge) the files flagged `D'.
  1869. Type \\[dired-find-file] to Find the current line's file
  1870. (or dired it in another buffer, if it is a directory).
  1871. Type \\[dired-find-file-other-window] to find file or Dired directory in Other window.
  1872. Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
  1873. Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
  1874. Type \\[dired-do-copy] to Copy files.
  1875. Type \\[dired-sort-toggle-or-edit] to toggle Sorting by name/date or change the `ls' switches.
  1876. Type \\[revert-buffer] to read all currently expanded directories aGain.
  1877. This retains all marks and hides subdirs again that were hidden before.
  1878. Use `SPC' and `DEL' to move down and up by lines.
  1879. If Dired ever gets confused, you can either type \\[revert-buffer] \
  1880. to read the
  1881. directories again, type \\[dired-do-redisplay] \
  1882. to relist the file at point or the marked files or a
  1883. subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
  1884. again for the directory tree.
  1885. Customization variables (rename this buffer and type \\[describe-variable] on each line
  1886. for more info):
  1887. `dired-listing-switches'
  1888. `dired-trivial-filenames'
  1889. `dired-marker-char'
  1890. `dired-del-marker'
  1891. `dired-keep-marker-rename'
  1892. `dired-keep-marker-copy'
  1893. `dired-keep-marker-hardlink'
  1894. `dired-keep-marker-symlink'
  1895. Hooks (use \\[describe-variable] to see their documentation):
  1896. `dired-before-readin-hook'
  1897. `dired-after-readin-hook'
  1898. `dired-mode-hook'
  1899. `dired-load-hook'
  1900. Keybindings:
  1901. \\{dired-mode-map}"
  1902. ;; Not to be called interactively (e.g. dired-directory will be set
  1903. ;; to default-directory, which is wrong with wildcards).
  1904. (kill-all-local-variables)
  1905. (use-local-map dired-mode-map)
  1906. (dired-advertise) ; default-directory is already set
  1907. (setq major-mode 'dired-mode
  1908. mode-name "Dired"
  1909. ;; case-fold-search nil
  1910. buffer-read-only t
  1911. selective-display t ; for subdirectory hiding
  1912. mode-line-buffer-identification
  1913. (propertized-buffer-identification "%17b"))
  1914. ;; Ignore dired-hide-details-* value of invisible text property by default.
  1915. (when (eq buffer-invisibility-spec t)
  1916. (setq buffer-invisibility-spec (list t)))
  1917. (setq-local revert-buffer-function #'dired-revert)
  1918. (setq-local buffer-stale-function #'dired-buffer-stale-p)
  1919. (setq-local page-delimiter "\n\n")
  1920. (setq-local dired-directory (or dirname default-directory))
  1921. ;; list-buffers uses this to display the dir being edited in this buffer.
  1922. (setq list-buffers-directory
  1923. (expand-file-name (if (listp dired-directory)
  1924. (car dired-directory)
  1925. dired-directory)))
  1926. (setq-local dired-actual-switches (or switches dired-listing-switches))
  1927. (setq-local font-lock-defaults
  1928. '(dired-font-lock-keywords t nil nil beginning-of-line))
  1929. (setq-local desktop-save-buffer 'dired-desktop-buffer-misc-data)
  1930. (setq dired-switches-alist nil)
  1931. (hack-dir-local-variables-non-file-buffer) ; before sorting
  1932. (dired-sort-other dired-actual-switches t)
  1933. (when (featurep 'dnd)
  1934. (setq-local dnd-protocol-alist
  1935. (append dired-dnd-protocol-alist dnd-protocol-alist)))
  1936. (add-hook 'file-name-at-point-functions 'dired-file-name-at-point nil t)
  1937. (add-hook 'isearch-mode-hook 'dired-isearch-filenames-setup nil t)
  1938. (run-mode-hooks 'dired-mode-hook))
  1939. ;; Idiosyncratic dired commands that don't deal with marks.
  1940. (defun dired-summary ()
  1941. "Summarize basic Dired commands and show recent Dired errors."
  1942. (interactive)
  1943. (dired-why)
  1944. ;>> this should check the key-bindings and use substitute-command-keys if non-standard
  1945. (message
  1946. "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
  1947. (defun dired-undo ()
  1948. "Undo in a Dired buffer.
  1949. This doesn't recover lost files, it just undoes changes in the buffer itself.
  1950. You can use it to recover marks, killed lines or subdirs."
  1951. (interactive)
  1952. (let ((inhibit-read-only t))
  1953. (undo))
  1954. (dired-build-subdir-alist)
  1955. (message "Change in Dired buffer undone.
  1956. Actual changes in files cannot be undone by Emacs."))
  1957. (defun dired-toggle-read-only ()
  1958. "Edit Dired buffer with Wdired, or make it read-only.
  1959. If the current buffer can be edited with Wdired, (i.e. the major
  1960. mode is `dired-mode'), call `wdired-change-to-wdired-mode'.
  1961. Otherwise, toggle `read-only-mode'."
  1962. (interactive)
  1963. (if (derived-mode-p 'dired-mode)
  1964. (wdired-change-to-wdired-mode)
  1965. (read-only-mode 'toggle)))
  1966. (defun dired-next-line (arg)
  1967. "Move down lines then position at filename.
  1968. Optional prefix ARG says how many lines to move; default is one line."
  1969. (interactive "^p")
  1970. (let ((line-move-visual)
  1971. (goal-column))
  1972. (line-move arg t))
  1973. ;; We never want to move point into an invisible line.
  1974. (while (and (invisible-p (point))
  1975. (not (if (and arg (< arg 0)) (bobp) (eobp))))
  1976. (forward-char (if (and arg (< arg 0)) -1 1)))
  1977. (dired-move-to-filename))
  1978. (defun dired-previous-line (arg)
  1979. "Move up lines then position at filename.
  1980. Optional prefix ARG says how many lines to move; default is one line."
  1981. (interactive "^p")
  1982. (dired-next-line (- (or arg 1))))
  1983. (defun dired-next-dirline (arg &optional opoint)
  1984. "Goto ARGth next directory file line."
  1985. (interactive "p")
  1986. (or opoint (setq opoint (point)))
  1987. (if (if (> arg 0)
  1988. (re-search-forward dired-re-dir nil t arg)
  1989. (beginning-of-line)
  1990. (re-search-backward dired-re-dir nil t (- arg)))
  1991. (dired-move-to-filename) ; user may type `i' or `f'
  1992. (goto-char opoint)
  1993. (error "No more subdirectories")))
  1994. (defun dired-prev-dirline (arg)
  1995. "Goto ARGth previous directory file line."
  1996. (interactive "p")
  1997. (dired-next-dirline (- arg)))
  1998. (defun dired-up-directory (&optional other-window)
  1999. "Run Dired on parent directory of current directory.
  2000. Find the parent directory either in this buffer or another buffer.
  2001. Creates a buffer if necessary.
  2002. If OTHER-WINDOW (the optional prefix arg), display the parent
  2003. directory in another window."
  2004. (interactive "P")
  2005. (let* ((dir (dired-current-directory))
  2006. (up (file-name-directory (directory-file-name dir))))
  2007. (or (dired-goto-file (directory-file-name dir))
  2008. ;; Only try dired-goto-subdir if buffer has more than one dir.
  2009. (and (cdr dired-subdir-alist)
  2010. (dired-goto-subdir up))
  2011. (progn
  2012. (if other-window
  2013. (dired-other-window up)
  2014. (dired up))
  2015. (dired-goto-file dir)))))
  2016. (defun dired-get-file-for-visit ()
  2017. "Get the current line's file name, with an error if file does not exist."
  2018. (interactive)
  2019. ;; We pass t for second arg so that we don't get error for `.' and `..'.
  2020. (let ((raw (dired-get-filename nil t))
  2021. file-name)
  2022. (if (null raw)
  2023. (error "No file on this line"))
  2024. (setq file-name (file-name-sans-versions raw t))
  2025. (if (file-exists-p file-name)
  2026. file-name
  2027. (if (file-symlink-p file-name)
  2028. (error "File is a symlink to a nonexistent target")
  2029. (error "File no longer exists; type `g' to update Dired buffer")))))
  2030. ;; Force C-m keybinding rather than `f' or `e' in the mode doc:
  2031. (define-obsolete-function-alias 'dired-advertised-find-file 'dired-find-file "23.2")
  2032. (defun dired-find-file ()
  2033. "In Dired, visit the file or directory named on this line."
  2034. (interactive)
  2035. ;; Bind `find-file-run-dired' so that the command works on directories
  2036. ;; too, independent of the user's setting.
  2037. (let ((find-file-run-dired t)
  2038. ;; This binding prevents problems with preserving point in
  2039. ;; windows displaying Dired buffers, because reverting a Dired
  2040. ;; buffer empties it, which changes the places where the
  2041. ;; markers used by switch-to-buffer-preserve-window-point
  2042. ;; point.
  2043. (switch-to-buffer-preserve-window-point
  2044. (if dired-auto-revert-buffer
  2045. nil
  2046. switch-to-buffer-preserve-window-point)))
  2047. (find-file (dired-get-file-for-visit))))
  2048. (defun dired-find-alternate-file ()
  2049. "In Dired, visit this file or directory instead of the Dired buffer."
  2050. (interactive)
  2051. (set-buffer-modified-p nil)
  2052. (find-alternate-file (dired-get-file-for-visit)))
  2053. ;; Don't override the setting from .emacs.
  2054. ;;;###autoload (put 'dired-find-alternate-file 'disabled t)
  2055. (defun dired-mouse-find-file-other-window (event)
  2056. "In Dired, visit the file or directory name you click on."
  2057. (interactive "e")
  2058. (let (window pos file)
  2059. (save-excursion
  2060. (setq window (posn-window (event-end event))
  2061. pos (posn-point (event-end event)))
  2062. (if (not (windowp window))
  2063. (error "No file chosen"))
  2064. (set-buffer (window-buffer window))
  2065. (goto-char pos)
  2066. (setq file (dired-get-file-for-visit)))
  2067. (if (file-directory-p file)
  2068. (or (and (cdr dired-subdir-alist)
  2069. (dired-goto-subdir file))
  2070. (progn
  2071. (select-window window)
  2072. (dired-other-window file)))
  2073. (select-window window)
  2074. (find-file-other-window (file-name-sans-versions file t)))))
  2075. (defun dired-view-file ()
  2076. "In Dired, examine a file in view mode, returning to Dired when done.
  2077. When file is a directory, show it in this buffer if it is inserted.
  2078. Otherwise, display it in another buffer."
  2079. (interactive)
  2080. (let ((file (dired-get-file-for-visit)))
  2081. (if (file-directory-p file)
  2082. (or (and (cdr dired-subdir-alist)
  2083. (dired-goto-subdir file))
  2084. (dired file))
  2085. (view-file file))))
  2086. (defun dired-find-file-other-window ()
  2087. "In Dired, visit this file or directory in another window."
  2088. (interactive)
  2089. (find-file-other-window (dired-get-file-for-visit)))
  2090. (defun dired-display-file ()
  2091. "In Dired, display this file or directory in another window."
  2092. (interactive)
  2093. (display-buffer (find-file-noselect (dired-get-file-for-visit))
  2094. t))
  2095. ;;; Functions for extracting and manipulating file names in Dired buffers.
  2096. (defun dired-get-filename (&optional localp no-error-if-not-filep)
  2097. "In Dired, return name of file mentioned on this line.
  2098. Value returned normally includes the directory name.
  2099. Optional arg LOCALP with value `no-dir' means don't include directory
  2100. name in result. A value of `verbatim' means to return the name exactly as
  2101. it occurs in the buffer, and a value of t means construct name relative to
  2102. `default-directory', which still may contain slashes if in a subdirectory.
  2103. Optional arg NO-ERROR-IF-NOT-FILEP means treat `.' and `..' as
  2104. regular filenames and return nil if no filename on this line.
  2105. Otherwise, an error occurs in these cases."
  2106. (let (case-fold-search file p1 p2 already-absolute)
  2107. (save-excursion
  2108. (if (setq p1 (dired-move-to-filename (not no-error-if-not-filep)))
  2109. (setq p2 (dired-move-to-end-of-filename no-error-if-not-filep))))
  2110. ;; nil if no file on this line, but no-error-if-not-filep is t:
  2111. (if (setq file (and p1 p2 (buffer-substring p1 p2)))
  2112. (progn
  2113. ;; Get rid of the mouse-face property that file names have.
  2114. (set-text-properties 0 (length file) nil file)
  2115. ;; Unquote names quoted by ls or by dired-insert-directory.
  2116. ;; This code was written using `read' to unquote, because
  2117. ;; it's faster than substituting \007 (4 chars) -> ^G (1
  2118. ;; char) etc. in a lisp loop. Unfortunately, this decision
  2119. ;; has necessitated hacks such as dealing with filenames
  2120. ;; with quotation marks in their names.
  2121. (while (string-match "\\(?:[^\\]\\|\\`\\)\\(\"\\)" file)
  2122. (setq file (replace-match "\\\"" nil t file 1)))
  2123. ;; Unescape any spaces escaped by ls -b (bug#10469).
  2124. ;; Other -b quotes, eg \t, \n, work transparently.
  2125. (if (dired-switches-escape-p dired-actual-switches)
  2126. (let ((start 0)
  2127. (rep "")
  2128. (shift -1))
  2129. (if (eq localp 'verbatim)
  2130. (setq rep "\\\\"
  2131. shift +1))
  2132. (while (string-match "\\(\\\\\\) " file start)
  2133. (setq file (replace-match rep nil t file 1)
  2134. start (+ shift (match-end 0))))))
  2135. (when (eq system-type 'windows-nt)
  2136. (save-match-data
  2137. (let ((start 0))
  2138. (while (string-match "\\\\" file start)
  2139. (aset file (match-beginning 0) ?/)
  2140. (setq start (match-end 0))))))
  2141. ;; Hence we don't need to worry about converting `\\' back to `\'.
  2142. (setq file (read (concat "\"" file "\"")))
  2143. ;; The above `read' will return a unibyte string if FILE
  2144. ;; contains eight-bit-control/graphic characters.
  2145. (if (and enable-multibyte-characters
  2146. (not (multibyte-string-p file)))
  2147. (setq file (string-to-multibyte file)))))
  2148. (and file (files--name-absolute-system-p file)
  2149. (setq already-absolute t))
  2150. (cond
  2151. ((null file)
  2152. nil)
  2153. ((eq localp 'verbatim)
  2154. file)
  2155. ((and (not no-error-if-not-filep)
  2156. (member file '("." "..")))
  2157. (error "Cannot operate on `.' or `..'"))
  2158. ((and (eq localp 'no-dir) already-absolute)
  2159. (file-name-nondirectory file))
  2160. (already-absolute
  2161. (let ((handler (find-file-name-handler file nil)))
  2162. ;; check for safe-magic property so that we won't
  2163. ;; put /: for names that don't really need them.
  2164. ;; For instance, .gz files when auto-compression-mode is on.
  2165. (if (and handler (not (get handler 'safe-magic)))
  2166. (concat "/:" file)
  2167. file)))
  2168. ((eq localp 'no-dir)
  2169. file)
  2170. ((equal (dired-current-directory) "/")
  2171. (setq file (concat (dired-current-directory localp) file))
  2172. (let ((handler (find-file-name-handler file nil)))
  2173. ;; check for safe-magic property so that we won't
  2174. ;; put /: for names that don't really need them.
  2175. ;; For instance, .gz files when auto-compression-mode is on.
  2176. (if (and handler (not (get handler 'safe-magic)))
  2177. (concat "/:" file)
  2178. file)))
  2179. (t
  2180. (concat (dired-current-directory localp) file)))))
  2181. (defun dired-string-replace-match (regexp string newtext
  2182. &optional literal global)
  2183. "Replace first match of REGEXP in STRING with NEWTEXT.
  2184. If it does not match, nil is returned instead of the new string.
  2185. Optional arg LITERAL means to take NEWTEXT literally.
  2186. Optional arg GLOBAL means to replace all matches."
  2187. (if global
  2188. (let ((start 0) ret)
  2189. (while (string-match regexp string start)
  2190. (let ((from-end (- (length string) (match-end 0))))
  2191. (setq ret (setq string (replace-match newtext t literal string)))
  2192. (setq start (- (length string) from-end))))
  2193. ret)
  2194. (if (not (string-match regexp string 0))
  2195. nil
  2196. (replace-match newtext t literal string))))
  2197. (defun dired-make-absolute (file &optional dir)
  2198. ;;"Convert FILE (a file name relative to DIR) to an absolute file name."
  2199. ;; We can't always use expand-file-name as this would get rid of `.'
  2200. ;; or expand in / instead default-directory if DIR=="".
  2201. ;; This should be good enough for ange-ftp.
  2202. ;; It should be reasonably fast, though, as it is called in
  2203. ;; dired-get-filename.
  2204. (concat (or dir default-directory) file))
  2205. (defun dired-make-relative (file &optional dir)
  2206. "Convert FILE (an absolute file name) to a name relative to DIR.
  2207. If DIR is omitted or nil, it defaults to `default-directory'.
  2208. If FILE is not in the directory tree of DIR, return FILE
  2209. unchanged."
  2210. (or dir (setq dir default-directory))
  2211. ;; This case comes into play if default-directory is set to
  2212. ;; use ~.
  2213. (if (and (> (length dir) 0) (= (aref dir 0) ?~))
  2214. (setq dir (expand-file-name dir)))
  2215. (if (string-match (concat "^" (regexp-quote dir)) file)
  2216. (substring file (match-end 0))
  2217. file))
  2218. (define-minor-mode dired-hide-details-mode
  2219. "Toggle visibility of detailed information in current Dired buffer.
  2220. When this minor mode is enabled, details such as file ownership and
  2221. permissions are hidden from view.
  2222. See options: `dired-hide-details-hide-symlink-targets' and
  2223. `dired-hide-details-hide-information-lines'."
  2224. :group 'dired
  2225. (unless (derived-mode-p 'dired-mode)
  2226. (error "Not a Dired buffer"))
  2227. (dired-hide-details-update-invisibility-spec)
  2228. (if dired-hide-details-mode
  2229. (add-hook 'wdired-mode-hook
  2230. 'dired-hide-details-update-invisibility-spec
  2231. nil
  2232. t)
  2233. (remove-hook 'wdired-mode-hook
  2234. 'dired-hide-details-update-invisibility-spec
  2235. t)))
  2236. (defun dired-hide-details-update-invisibility-spec ()
  2237. (funcall (if dired-hide-details-mode
  2238. 'add-to-invisibility-spec
  2239. 'remove-from-invisibility-spec)
  2240. 'dired-hide-details-detail)
  2241. (funcall (if (and dired-hide-details-mode
  2242. dired-hide-details-hide-information-lines)
  2243. 'add-to-invisibility-spec
  2244. 'remove-from-invisibility-spec)
  2245. 'dired-hide-details-information)
  2246. (funcall (if (and dired-hide-details-mode
  2247. dired-hide-details-hide-symlink-targets
  2248. (not (derived-mode-p 'wdired-mode)))
  2249. 'add-to-invisibility-spec
  2250. 'remove-from-invisibility-spec)
  2251. 'dired-hide-details-link))
  2252. ;;; Functions for finding the file name in a dired buffer line.
  2253. (defvar dired-permission-flags-regexp
  2254. "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
  2255. "Regular expression to match the permission flags in `ls -l'.")
  2256. ;; Move to first char of filename on this line.
  2257. ;; Returns position (point) or nil if no filename on this line."
  2258. (defun dired-move-to-filename (&optional raise-error eol)
  2259. "Move to the beginning of the filename on the current line.
  2260. Return the position of the beginning of the filename, or nil if none found."
  2261. ;; This is the UNIX version.
  2262. (or eol (setq eol (line-end-position)))
  2263. (beginning-of-line)
  2264. ;; First try assuming `ls --dired' was used.
  2265. (let ((change (next-single-property-change (point) 'dired-filename nil eol)))
  2266. (cond
  2267. ((and change (< change eol))
  2268. (goto-char change))
  2269. ((re-search-forward directory-listing-before-filename-regexp eol t)
  2270. (goto-char (match-end 0)))
  2271. ((re-search-forward dired-permission-flags-regexp eol t)
  2272. ;; Ha! There *is* a file. Our regexp-from-hell just failed to find it.
  2273. (if raise-error
  2274. (error "Unrecognized line! Check directory-listing-before-filename-regexp"))
  2275. (beginning-of-line)
  2276. nil)
  2277. (raise-error
  2278. (error "No file on this line")))))
  2279. (defun dired-move-to-end-of-filename (&optional no-error)
  2280. ;; Assumes point is at beginning of filename,
  2281. ;; thus the rwx bit re-search-backward below will succeed in *this*
  2282. ;; line if at all. So, it should be called only after
  2283. ;; (dired-move-to-filename t).
  2284. ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
  2285. ;; This is the UNIX version.
  2286. (if (get-text-property (point) 'dired-filename)
  2287. (goto-char (next-single-property-change (point) 'dired-filename))
  2288. (let (opoint file-type executable symlink hidden used-F eol)
  2289. (setq used-F (dired-check-switches dired-actual-switches "F" "classify")
  2290. opoint (point)
  2291. eol (line-end-position)
  2292. hidden (and selective-display
  2293. (save-excursion (search-forward "\r" eol t))))
  2294. (if hidden
  2295. nil
  2296. (save-excursion ;; Find out what kind of file this is:
  2297. ;; Restrict perm bits to be non-blank,
  2298. ;; otherwise this matches one char to early (looking backward):
  2299. ;; "l---------" (some systems make symlinks that way)
  2300. ;; "----------" (plain file with zero perms)
  2301. (if (re-search-backward
  2302. dired-permission-flags-regexp nil t)
  2303. (setq file-type (char-after (match-beginning 1))
  2304. symlink (eq file-type ?l)
  2305. ;; Only with -F we need to know whether it's an executable
  2306. executable (and
  2307. used-F
  2308. (string-match
  2309. "[xst]" ;; execute bit set anywhere?
  2310. (concat
  2311. (match-string 2)
  2312. (match-string 3)
  2313. (match-string 4)))))
  2314. (or no-error (error "No file on this line"))))
  2315. ;; Move point to end of name:
  2316. (if symlink
  2317. (if (search-forward " -> " eol t)
  2318. (progn
  2319. (forward-char -4)
  2320. (and used-F
  2321. dired-ls-F-marks-symlinks
  2322. (eq (preceding-char) ?@) ;; did ls really mark the link?
  2323. (forward-char -1))))
  2324. (goto-char eol) ;; else not a symbolic link
  2325. ;; ls -lF marks dirs, sockets, fifos and executables with exactly
  2326. ;; one trailing character. (Executable bits on symlinks ain't mean
  2327. ;; a thing, even to ls, but we know it's not a symlink.)
  2328. (and used-F
  2329. (or (memq file-type '(?d ?s ?p))
  2330. executable)
  2331. (forward-char -1))))
  2332. (or no-error
  2333. (not (eq opoint (point)))
  2334. (error "%s" (if hidden
  2335. (substitute-command-keys
  2336. "File line is hidden, type \\[dired-hide-subdir] to unhide")
  2337. "No file on this line")))
  2338. (if (eq opoint (point))
  2339. nil
  2340. (point)))))
  2341. ;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
  2342. (defun dired-copy-filename-as-kill (&optional arg)
  2343. "Copy names of marked (or next ARG) files into the kill ring.
  2344. The names are separated by a space.
  2345. With a zero prefix arg, use the absolute file name of each marked file.
  2346. With \\[universal-argument], use the file name relative to the Dired buffer's
  2347. `default-directory'. (This still may contain slashes if in a subdirectory.)
  2348. If on a subdir headerline, use absolute subdirname instead;
  2349. prefix arg and marked files are ignored in this case.
  2350. You can then feed the file name(s) to other commands with \\[yank]."
  2351. (interactive "P")
  2352. (let ((string
  2353. (or (dired-get-subdir)
  2354. (mapconcat #'identity
  2355. (if arg
  2356. (cond ((zerop (prefix-numeric-value arg))
  2357. (dired-get-marked-files))
  2358. ((consp arg)
  2359. (dired-get-marked-files t))
  2360. (t
  2361. (dired-get-marked-files
  2362. 'no-dir (prefix-numeric-value arg))))
  2363. (dired-get-marked-files 'no-dir))
  2364. " "))))
  2365. (unless (string= string "")
  2366. (if (eq last-command 'kill-region)
  2367. (kill-append string nil)
  2368. (kill-new string))
  2369. (message "%s" string))))
  2370. ;; Keeping Dired buffers in sync with the filesystem and with each other
  2371. (defun dired-buffers-for-dir (dir &optional file)
  2372. ;; Return a list of buffers for DIR (top level or in-situ subdir).
  2373. ;; If FILE is non-nil, include only those whose wildcard pattern (if any)
  2374. ;; matches FILE.
  2375. ;; The list is in reverse order of buffer creation, most recent last.
  2376. ;; As a side effect, killed dired buffers for DIR are removed from
  2377. ;; dired-buffers.
  2378. (setq dir (file-name-as-directory dir))
  2379. (let (result buf)
  2380. (dolist (elt dired-buffers)
  2381. (setq buf (cdr elt))
  2382. (cond
  2383. ((null (buffer-name buf))
  2384. ;; Buffer is killed - clean up:
  2385. (setq dired-buffers (delq elt dired-buffers)))
  2386. ((dired-in-this-tree dir (car elt))
  2387. (with-current-buffer buf
  2388. (and (assoc dir dired-subdir-alist)
  2389. (or (null file)
  2390. (if (stringp dired-directory)
  2391. (let ((wildcards (file-name-nondirectory
  2392. dired-directory)))
  2393. (or (zerop (length wildcards))
  2394. (string-match-p (dired-glob-regexp wildcards)
  2395. file)))
  2396. (member (expand-file-name file dir)
  2397. (cdr dired-directory))))
  2398. (setq result (cons buf result)))))))
  2399. result))
  2400. (defun dired-glob-regexp (pattern)
  2401. "Convert glob-pattern PATTERN to a regular expression."
  2402. (let ((matched-in-pattern 0) ;; How many chars of PATTERN we've handled.
  2403. regexp)
  2404. (while (string-match "[[?*]" pattern matched-in-pattern)
  2405. (let ((op-end (match-end 0))
  2406. (next-op (aref pattern (match-beginning 0))))
  2407. (setq regexp (concat regexp
  2408. (regexp-quote
  2409. (substring pattern matched-in-pattern
  2410. (match-beginning 0)))))
  2411. (cond ((= next-op ??)
  2412. (setq regexp (concat regexp "."))
  2413. (setq matched-in-pattern op-end))
  2414. ((= next-op ?\[)
  2415. ;; Fails to handle ^ yet ????
  2416. (let* ((set-start (match-beginning 0))
  2417. (set-cont
  2418. (if (= (aref pattern (1+ set-start)) ?^)
  2419. (+ 3 set-start)
  2420. (+ 2 set-start)))
  2421. (set-end (string-match-p "]" pattern set-cont))
  2422. (set (substring pattern set-start (1+ set-end))))
  2423. (setq regexp (concat regexp set))
  2424. (setq matched-in-pattern (1+ set-end))))
  2425. ((= next-op ?*)
  2426. (setq regexp (concat regexp ".*"))
  2427. (setq matched-in-pattern op-end)))))
  2428. (concat "\\`"
  2429. regexp
  2430. (regexp-quote
  2431. (substring pattern matched-in-pattern))
  2432. "\\'")))
  2433. (defun dired-advertise ()
  2434. ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
  2435. ;; With wildcards we actually advertise too much.
  2436. (let ((expanded-default (expand-file-name default-directory)))
  2437. (if (memq (current-buffer) (dired-buffers-for-dir expanded-default))
  2438. t ; we have already advertised ourselves
  2439. (setq dired-buffers
  2440. (cons (cons expanded-default (current-buffer))
  2441. dired-buffers)))))
  2442. (defun dired-unadvertise (dir)
  2443. ;; Remove DIR from the buffer alist in variable dired-buffers.
  2444. ;; This has the effect of removing any buffer whose main directory is DIR.
  2445. ;; It does not affect buffers in which DIR is a subdir.
  2446. ;; Removing is also done as a side-effect in dired-buffer-for-dir.
  2447. (setq dired-buffers
  2448. (delq (assoc (expand-file-name dir) dired-buffers) dired-buffers)))
  2449. ;; Tree Dired
  2450. ;;; utility functions
  2451. (defun dired-in-this-tree (file dir)
  2452. ;;"Is FILE part of the directory tree starting at DIR?"
  2453. (let (case-fold-search)
  2454. (string-match-p (concat "^" (regexp-quote dir)) file)))
  2455. (defun dired-normalize-subdir (dir)
  2456. ;; Prepend default-directory to DIR if relative file name.
  2457. ;; dired-get-filename must be able to make a valid file name from a
  2458. ;; file and its directory DIR.
  2459. (file-name-as-directory
  2460. (if (file-name-absolute-p dir)
  2461. dir
  2462. (expand-file-name dir default-directory))))
  2463. (defun dired-get-subdir ()
  2464. ;;"Return the subdir name on this line, or nil if not on a headerline."
  2465. ;; Look up in the alist whether this is a headerline.
  2466. (save-excursion
  2467. (let ((cur-dir (dired-current-directory)))
  2468. (beginning-of-line) ; alist stores b-o-l positions
  2469. (and (zerop (- (point)
  2470. (dired-get-subdir-min (assoc cur-dir
  2471. dired-subdir-alist))))
  2472. cur-dir))))
  2473. ;; can't use macro, must be redefinable for other alist format in dired-nstd.
  2474. (defalias 'dired-get-subdir-min 'cdr)
  2475. (defun dired-get-subdir-max (elt)
  2476. (save-excursion
  2477. (goto-char (dired-get-subdir-min elt))
  2478. (dired-subdir-max)))
  2479. (defun dired-clear-alist ()
  2480. (while dired-subdir-alist
  2481. (set-marker (dired-get-subdir-min (car dired-subdir-alist)) nil)
  2482. (setq dired-subdir-alist (cdr dired-subdir-alist))))
  2483. (defun dired-subdir-index (dir)
  2484. ;; Return an index into alist for use with nth
  2485. ;; for the sake of subdir moving commands.
  2486. (let (found (index 0) (alist dired-subdir-alist))
  2487. (while alist
  2488. (if (string= dir (car (car alist)))
  2489. (setq alist nil found t)
  2490. (setq alist (cdr alist) index (1+ index))))
  2491. (if found index nil)))
  2492. (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip)
  2493. "Go to next subdirectory, regardless of level."
  2494. ;; Use 0 arg to go to this directory's header line.
  2495. ;; NO-SKIP prevents moving to end of header line, returning whatever
  2496. ;; position was found in dired-subdir-alist.
  2497. (interactive "p")
  2498. (let ((this-dir (dired-current-directory))
  2499. pos index)
  2500. ;; nth with negative arg does not return nil but the first element
  2501. (setq index (- (dired-subdir-index this-dir) arg))
  2502. (setq pos (if (>= index 0)
  2503. (dired-get-subdir-min (nth index dired-subdir-alist))))
  2504. (if pos
  2505. (progn
  2506. (goto-char pos)
  2507. (or no-skip (skip-chars-forward "^\n\r"))
  2508. (point))
  2509. (if no-error-if-not-found
  2510. nil ; return nil if not found
  2511. (error "%s directory" (if (> arg 0) "Last" "First"))))))
  2512. (defun dired-build-subdir-alist (&optional switches)
  2513. "Build `dired-subdir-alist' by parsing the buffer.
  2514. Returns the new value of the alist.
  2515. If optional arg SWITCHES is non-nil, use its value
  2516. instead of `dired-actual-switches'."
  2517. (interactive)
  2518. (dired-clear-alist)
  2519. (save-excursion
  2520. (let* ((count 0)
  2521. (inhibit-read-only t)
  2522. (buffer-undo-list t)
  2523. (switches (or switches dired-actual-switches))
  2524. new-dir-name
  2525. (R-ftp-base-dir-regex
  2526. ;; Used to expand subdirectory names correctly in recursive
  2527. ;; ange-ftp listings.
  2528. (and (dired-switches-recursive-p switches)
  2529. (string-match "\\`/.*:\\(/.*\\)" default-directory)
  2530. (concat "\\`" (match-string 1 default-directory)))))
  2531. (goto-char (point-min))
  2532. (setq dired-subdir-alist nil)
  2533. (while (re-search-forward dired-subdir-regexp nil t)
  2534. ;; Avoid taking a file name ending in a colon
  2535. ;; as a subdir name.
  2536. (unless (save-excursion
  2537. (goto-char (match-beginning 0))
  2538. (beginning-of-line)
  2539. (forward-char 2)
  2540. (looking-at-p dired-re-perms))
  2541. (save-excursion
  2542. (goto-char (match-beginning 1))
  2543. (setq new-dir-name
  2544. (buffer-substring-no-properties (point) (match-end 1))
  2545. new-dir-name
  2546. (save-match-data
  2547. (if (and R-ftp-base-dir-regex
  2548. (not (string= new-dir-name default-directory))
  2549. (string-match R-ftp-base-dir-regex new-dir-name))
  2550. (concat default-directory
  2551. (substring new-dir-name (match-end 0)))
  2552. (expand-file-name new-dir-name))))
  2553. (delete-region (point) (match-end 1))
  2554. (insert new-dir-name))
  2555. (setq count (1+ count))
  2556. ;; Undo any escaping of newlines and \ by dired-insert-directory.
  2557. ;; Convert "n" preceded by odd number of \ to newline, and \\ to \.
  2558. (when (and (dired-switches-escape-p switches)
  2559. (string-match-p "\\\\" new-dir-name))
  2560. (let (temp res)
  2561. (mapc (lambda (char)
  2562. (cond ((equal char ?\\)
  2563. (if temp
  2564. (setq res (concat res "\\")
  2565. temp nil)
  2566. (setq temp "\\")))
  2567. ((and temp (equal char ?n))
  2568. (setq res (concat res "\n")
  2569. temp nil))
  2570. (t
  2571. (setq res (concat res temp (char-to-string char))
  2572. temp nil))))
  2573. new-dir-name)
  2574. (setq new-dir-name res)))
  2575. (dired-alist-add-1 new-dir-name
  2576. ;; Place a sub directory boundary between lines.
  2577. (save-excursion
  2578. (goto-char (match-beginning 0))
  2579. (beginning-of-line)
  2580. (point-marker)))))
  2581. (if (and (> count 1) (called-interactively-p 'interactive))
  2582. (message "Buffer includes %d directories" count)))
  2583. ;; We don't need to sort it because it is in buffer order per
  2584. ;; constructionem. Return new alist:
  2585. dired-subdir-alist))
  2586. (defun dired-alist-add-1 (dir new-marker)
  2587. ;; Add new DIR at NEW-MARKER. Don't sort.
  2588. (setq dired-subdir-alist
  2589. (cons (cons (dired-normalize-subdir dir) new-marker)
  2590. dired-subdir-alist)))
  2591. (defun dired-goto-next-nontrivial-file ()
  2592. ;; Position point on first nontrivial file after point.
  2593. (dired-goto-next-file);; so there is a file to compare with
  2594. (if (stringp dired-trivial-filenames)
  2595. (while (and (not (eobp))
  2596. (string-match-p dired-trivial-filenames
  2597. (file-name-nondirectory
  2598. (or (dired-get-filename nil t) ""))))
  2599. (forward-line 1)
  2600. (dired-move-to-filename))))
  2601. (defun dired-goto-next-file ()
  2602. (let ((max (1- (dired-subdir-max))))
  2603. (while (and (not (dired-move-to-filename)) (< (point) max))
  2604. (forward-line 1))))
  2605. (defun dired-goto-file (file)
  2606. "Go to line describing file FILE in this Dired buffer."
  2607. ;; Return value of point on success, else nil.
  2608. ;; FILE must be an absolute file name.
  2609. ;; Loses if FILE contains control chars like "\007" for which ls
  2610. ;; either inserts "?" or "\\007" into the buffer, so we won't find
  2611. ;; it in the buffer.
  2612. (interactive
  2613. (prog1 ; let push-mark display its message
  2614. (list (expand-file-name
  2615. (read-file-name "Goto file: "
  2616. (dired-current-directory))))
  2617. (push-mark)))
  2618. (unless (file-name-absolute-p file)
  2619. (error "File name `%s' is not absolute" file))
  2620. (setq file (directory-file-name file)) ; does no harm if not a directory
  2621. (let* ((case-fold-search nil)
  2622. (dir (file-name-directory file))
  2623. (found (or
  2624. ;; First, look for a listing under the absolute name.
  2625. (save-excursion
  2626. (goto-char (point-min))
  2627. (dired-goto-file-1 file file (point-max)))
  2628. ;; Next, look for it as a relative name with leading
  2629. ;; subdirectories. (This happens in Dired buffers
  2630. ;; created by find-dired, for example.)
  2631. (save-excursion
  2632. (goto-char (point-min))
  2633. (dired-goto-file-1 (file-relative-name file
  2634. default-directory)
  2635. file (point-max)))
  2636. ;; Otherwise, look for it as a relative name, a base
  2637. ;; name only. The hair is to get the result of
  2638. ;; `dired-goto-subdir' without calling it if we don't
  2639. ;; have any subdirs.
  2640. (save-excursion
  2641. (when (if (string= dir (expand-file-name default-directory))
  2642. (goto-char (point-min))
  2643. (and (cdr dired-subdir-alist)
  2644. (dired-goto-subdir dir)))
  2645. (dired-goto-file-1 (file-name-nondirectory file)
  2646. file
  2647. (dired-subdir-max)))))))
  2648. ;; Return buffer position, if found.
  2649. (if found
  2650. (goto-char found))))
  2651. (defun dired-goto-file-1 (file full-name limit)
  2652. "Advance to the Dired listing labeled by FILE; return its position.
  2653. Return nil if the listing is not found. If FILE contains
  2654. characters that would not appear in a Dired buffer, search using
  2655. the quoted forms of those characters.
  2656. FULL-NAME specifies the actual file name the listing must have,
  2657. as returned by `dired-get-filename'. LIMIT is the search limit."
  2658. (let (str)
  2659. (setq str (replace-regexp-in-string "\^m" "\\^m" file nil t))
  2660. (setq str (replace-regexp-in-string "\\\\" "\\\\" str nil t))
  2661. (and (dired-switches-escape-p dired-actual-switches)
  2662. (string-match-p "[ \t\n]" str)
  2663. ;; FIXME: to fix this for embedded control characters etc, we
  2664. ;; should escape everything that `ls -b' does.
  2665. (setq str (replace-regexp-in-string " " "\\ " str nil t)
  2666. str (replace-regexp-in-string "\t" "\\t" str nil t)
  2667. str (replace-regexp-in-string "\n" "\\n" str nil t)))
  2668. (let ((found nil)
  2669. ;; filenames are preceded by SPC, this makes the search faster
  2670. ;; (e.g. for the filename "-").
  2671. (search-string (concat " " str)))
  2672. (while (and (not found)
  2673. (search-forward search-string limit 'move))
  2674. ;; Check that we are in the right place. Match could have
  2675. ;; BASE just as initial substring or in permission bits etc.
  2676. (if (equal full-name (dired-get-filename nil t))
  2677. (setq found (dired-move-to-filename))
  2678. (forward-line 1)))
  2679. found)))
  2680. (defvar dired-find-subdir)
  2681. ;; FIXME document whatever dired-x is doing.
  2682. (defun dired-initial-position (dirname)
  2683. "Where point should go in a new listing of DIRNAME.
  2684. Point is assumed to be at the beginning of new subdir line.
  2685. It runs the hook `dired-initial-position-hook'."
  2686. (end-of-line)
  2687. (and (featurep 'dired-x) dired-find-subdir
  2688. (dired-goto-subdir dirname))
  2689. (if dired-trivial-filenames (dired-goto-next-nontrivial-file))
  2690. (run-hooks 'dired-initial-position-hook))
  2691. ;; These are hooks which make tree dired work.
  2692. ;; They are in this file because other parts of dired need to call them.
  2693. ;; But they don't call the rest of tree dired unless there are subdirs loaded.
  2694. ;; This function is called for each retrieved filename.
  2695. ;; It could stand to be faster, though it's mostly function call
  2696. ;; overhead. Avoiding the function call seems to save about 10% in
  2697. ;; dired-get-filename. Make it a defsubst?
  2698. (defun dired-current-directory (&optional localp)
  2699. "Return the name of the subdirectory to which this line belongs.
  2700. This returns a string with trailing slash, like `default-directory'.
  2701. Optional argument means return a file name relative to `default-directory',
  2702. in which case the value could be an empty string if `default-directory'
  2703. is the directory where the file on this line resides."
  2704. (let ((here (point))
  2705. (alist (or dired-subdir-alist
  2706. ;; probably because called in a non-dired buffer
  2707. (error "No subdir-alist in %s" (current-buffer))))
  2708. elt dir)
  2709. (while alist
  2710. (setq elt (car alist)
  2711. dir (car elt)
  2712. ;; use `<=' (not `<') as subdir line is part of subdir
  2713. alist (if (<= (dired-get-subdir-min elt) here)
  2714. nil ; found
  2715. (cdr alist))))
  2716. (if localp
  2717. (dired-make-relative dir default-directory)
  2718. dir)))
  2719. ;; Subdirs start at the beginning of their header lines and end just
  2720. ;; before the beginning of the next header line (or end of buffer).
  2721. (defun dired-subdir-max ()
  2722. (save-excursion
  2723. (if (or (null (cdr dired-subdir-alist)) (not (dired-next-subdir 1 t t)))
  2724. (point-max)
  2725. (point))))
  2726. ;; Deleting files
  2727. (defcustom dired-recursive-deletes 'top
  2728. "Whether Dired deletes directories recursively.
  2729. If nil, Dired will not delete non-empty directories.
  2730. `always' means to delete non-empty directories recursively,
  2731. without asking. This is dangerous!
  2732. `top' means to ask for each top-level directory specified by the
  2733. Dired deletion command, and delete its subdirectories without
  2734. asking.
  2735. Any other value means to ask for each directory."
  2736. :type '(choice :tag "Delete non-empty directories"
  2737. (const :tag "Yes" always)
  2738. (const :tag "No--only delete empty directories" nil)
  2739. (const :tag "Confirm for each directory" t)
  2740. (const :tag "Confirm for each top directory only" top))
  2741. :group 'dired)
  2742. ;; Match anything but `.' and `..'.
  2743. (defvar dired-re-no-dot "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")
  2744. (defconst dired-delete-help
  2745. "Type:
  2746. `yes' to delete recursively the current directory,
  2747. `no' to skip to next,
  2748. `all' to delete all remaining directories with no more questions,
  2749. `quit' to exit,
  2750. `help' to show this help message.")
  2751. (defun dired--yes-no-all-quit-help (prompt &optional help-msg)
  2752. "Ask a question with valid answers: yes, no, all, quit, help.
  2753. PROMPT must end with '? ', for instance, 'Delete it? '.
  2754. If optional arg HELP-MSG is non-nil, then is a message to show when
  2755. the user answers 'help'. Otherwise, default to `dired-delete-help'."
  2756. (let ((valid-answers (list "yes" "no" "all" "quit"))
  2757. (answer "")
  2758. (input-fn (lambda ()
  2759. (read-string
  2760. (format "%s [yes, no, all, quit, help] " prompt)))))
  2761. (setq answer (funcall input-fn))
  2762. (when (string= answer "help")
  2763. (with-help-window "*Help*"
  2764. (with-current-buffer "*Help*"
  2765. (insert (or help-msg dired-delete-help)))))
  2766. (while (not (member answer valid-answers))
  2767. (unless (string= answer "help")
  2768. (beep)
  2769. (message "Please answer `yes' or `no' or `all' or `quit'")
  2770. (sleep-for 2))
  2771. (setq answer (funcall input-fn)))
  2772. answer))
  2773. ;; Delete file, possibly delete a directory and all its files.
  2774. ;; This function is useful outside of dired. One could change its name
  2775. ;; to e.g. recursive-delete-file and put it somewhere else.
  2776. (defun dired-delete-file (file &optional recursive trash) "\
  2777. Delete FILE or directory (possibly recursively if optional RECURSIVE is true.)
  2778. RECURSIVE determines what to do with a non-empty directory. The effect of
  2779. its possible values is:
  2780. nil -- do not delete.
  2781. `always' -- delete recursively without asking.
  2782. `top' -- ask for each directory at top level.
  2783. Anything else -- ask for each sub-directory.
  2784. TRASH non-nil means to trash the file instead of deleting, provided
  2785. `delete-by-moving-to-trash' (which see) is non-nil."
  2786. ;; This test is equivalent to
  2787. ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
  2788. ;; but more efficient
  2789. (if (not (eq t (car (file-attributes file))))
  2790. (delete-file file trash)
  2791. (let* ((empty-dir-p (null (directory-files file t dired-re-no-dot))))
  2792. (if (and recursive (not empty-dir-p))
  2793. (unless (eq recursive 'always)
  2794. (let ((prompt
  2795. (format "Recursively %s %s? "
  2796. (if (and trash delete-by-moving-to-trash)
  2797. "trash"
  2798. "delete")
  2799. (dired-make-relative file))))
  2800. (pcase (dired--yes-no-all-quit-help prompt) ; Prompt user.
  2801. ('"all" (setq recursive 'always dired-recursive-deletes recursive))
  2802. ('"yes" (if (eq recursive 'top) (setq recursive 'always)))
  2803. ('"no" (setq recursive nil))
  2804. ('"quit" (keyboard-quit)))))
  2805. (setq recursive nil)) ; Empty dir or recursive is nil.
  2806. (delete-directory file recursive trash))))
  2807. (defun dired-do-flagged-delete (&optional nomessage)
  2808. "In Dired, delete the files flagged for deletion.
  2809. If NOMESSAGE is non-nil, we don't display any message
  2810. if there are no flagged files.
  2811. `dired-recursive-deletes' controls whether deletion of
  2812. non-empty directories is allowed."
  2813. (interactive)
  2814. (let* ((dired-marker-char dired-del-marker)
  2815. (regexp (dired-marker-regexp))
  2816. case-fold-search)
  2817. (if (save-excursion (goto-char (point-min))
  2818. (re-search-forward regexp nil t))
  2819. (dired-internal-do-deletions
  2820. (nreverse
  2821. ;; this can't move point since ARG is nil
  2822. (dired-map-over-marks (cons (dired-get-filename) (point))
  2823. nil))
  2824. nil t)
  2825. (or nomessage
  2826. (message "(No deletions requested)")))))
  2827. (defun dired-do-delete (&optional arg)
  2828. "Delete all marked (or next ARG) files.
  2829. `dired-recursive-deletes' controls whether deletion of
  2830. non-empty directories is allowed."
  2831. ;; This is more consistent with the file marking feature than
  2832. ;; dired-do-flagged-delete.
  2833. (interactive "P")
  2834. (dired-internal-do-deletions
  2835. (nreverse
  2836. ;; this may move point if ARG is an integer
  2837. (dired-map-over-marks (cons (dired-get-filename) (point))
  2838. arg))
  2839. arg t))
  2840. (defvar dired-deletion-confirmer 'yes-or-no-p) ; or y-or-n-p?
  2841. (defun dired-internal-do-deletions (l arg &optional trash)
  2842. ;; L is an alist of files to delete, with their buffer positions.
  2843. ;; ARG is the prefix arg.
  2844. ;; Filenames are absolute.
  2845. ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
  2846. ;; That way as changes are made in the buffer they do not shift the
  2847. ;; lines still to be changed, so the (point) values in L stay valid.
  2848. ;; Also, for subdirs in natural order, a subdir's files are deleted
  2849. ;; before the subdir itself - the other way around would not work.
  2850. (let* ((files (mapcar #'car l))
  2851. (count (length l))
  2852. (succ 0)
  2853. ;; Bind `dired-recursive-deletes' so that we can change it
  2854. ;; locally according with the user answer within `dired-delete-file'.
  2855. (dired-recursive-deletes dired-recursive-deletes)
  2856. (trashing (and trash delete-by-moving-to-trash)))
  2857. ;; canonicalize file list for pop up
  2858. (setq files (nreverse (mapcar #'dired-make-relative files)))
  2859. (if (dired-mark-pop-up
  2860. " *Deletions*" 'delete files dired-deletion-confirmer
  2861. (format "%s %s "
  2862. (if trashing "Trash" "Delete")
  2863. (dired-mark-prompt arg files)))
  2864. (save-excursion
  2865. (catch '--delete-cancel
  2866. (let ((progress-reporter
  2867. (make-progress-reporter
  2868. (if trashing "Trashing..." "Deleting...")
  2869. succ count))
  2870. failures) ;; files better be in reverse order for this loop!
  2871. (while l
  2872. (goto-char (cdr (car l)))
  2873. (let ((inhibit-read-only t))
  2874. (condition-case err
  2875. (let ((fn (car (car l))))
  2876. (dired-delete-file fn dired-recursive-deletes trash)
  2877. ;; if we get here, removing worked
  2878. (setq succ (1+ succ))
  2879. (progress-reporter-update progress-reporter succ)
  2880. (dired-fun-in-all-buffers
  2881. (file-name-directory fn) (file-name-nondirectory fn)
  2882. #'dired-delete-entry fn))
  2883. (quit (throw '--delete-cancel (message "OK, canceled")))
  2884. (error ;; catch errors from failed deletions
  2885. (dired-log "%s\n" err)
  2886. (setq failures (cons (car (car l)) failures)))))
  2887. (setq l (cdr l)))
  2888. (if (not failures)
  2889. (progress-reporter-done progress-reporter)
  2890. (dired-log-summary
  2891. (format "%d of %d deletion%s failed"
  2892. (length failures) count
  2893. (dired-plural-s count))
  2894. failures)))))
  2895. (message "(No deletions performed)")))
  2896. (dired-move-to-filename))
  2897. (defun dired-fun-in-all-buffers (directory file fun &rest args)
  2898. ;; In all buffers dired'ing DIRECTORY, run FUN with ARGS.
  2899. ;; If the buffer has a wildcard pattern, check that it matches FILE.
  2900. ;; (FILE does not include a directory component.)
  2901. ;; FILE may be nil, in which case ignore it.
  2902. ;; Return list of buffers where FUN succeeded (i.e., returned non-nil).
  2903. (let (success-list)
  2904. (dolist (buf (dired-buffers-for-dir (expand-file-name directory)
  2905. file))
  2906. (with-current-buffer buf
  2907. (if (apply fun args)
  2908. (setq success-list (cons (buffer-name buf) success-list)))))
  2909. success-list))
  2910. ;; Delete the entry for FILE from
  2911. (defun dired-delete-entry (file)
  2912. (save-excursion
  2913. (and (dired-goto-file file)
  2914. (let ((inhibit-read-only t))
  2915. (delete-region (progn (beginning-of-line) (point))
  2916. (save-excursion (forward-line 1) (point))))))
  2917. (dired-clean-up-after-deletion file))
  2918. (defvar dired-clean-up-buffers-too)
  2919. (defun dired-clean-up-after-deletion (fn)
  2920. "Clean up after a deleted file or directory FN.
  2921. Removes any expanded subdirectory of deleted directory.
  2922. If `dired-x' is loaded and `dired-clean-up-buffers-too' is non-nil,
  2923. also offers to kill buffers visiting deleted files and directories."
  2924. (save-excursion (and (cdr dired-subdir-alist)
  2925. (dired-goto-subdir fn)
  2926. (dired-kill-subdir)))
  2927. ;; Offer to kill buffer of deleted file FN.
  2928. (when (and (featurep 'dired-x) dired-clean-up-buffers-too)
  2929. (let ((buf (get-file-buffer fn)))
  2930. (and buf
  2931. (funcall #'y-or-n-p
  2932. (format "Kill buffer of %s, too? "
  2933. (file-name-nondirectory fn)))
  2934. (kill-buffer buf)))
  2935. (let ((buf-list (dired-buffers-for-dir (expand-file-name fn))))
  2936. (and buf-list
  2937. (y-or-n-p (format "Kill Dired buffer%s of %s, too? "
  2938. (dired-plural-s (length buf-list))
  2939. (file-name-nondirectory fn)))
  2940. (dolist (buf buf-list)
  2941. (kill-buffer buf))))))
  2942. ;; Confirmation
  2943. (defun dired-marker-regexp ()
  2944. (concat "^" (regexp-quote (char-to-string dired-marker-char))))
  2945. (defun dired-plural-s (count)
  2946. (if (= 1 count) "" "s"))
  2947. (defun dired-mark-prompt (arg files)
  2948. "Return a string suitable for use in a Dired prompt.
  2949. ARG is normally the prefix argument for the calling command.
  2950. FILES should be a list of file names.
  2951. The return value has a form like \"foo.txt\", \"[next 3 files]\",
  2952. or \"* [3 files]\"."
  2953. ;; distinguish-one-marked can cause the first element to be just t.
  2954. (if (eq (car files) t) (setq files (cdr files)))
  2955. (let ((count (length files)))
  2956. (if (= count 1)
  2957. (car files)
  2958. ;; more than 1 file:
  2959. (if (integerp arg)
  2960. ;; abs(arg) = count
  2961. ;; Perhaps this is nicer, but it also takes more screen space:
  2962. ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
  2963. ;; count)
  2964. (format "[next %d files]" arg)
  2965. (format "%c [%d files]" dired-marker-char count)))))
  2966. (defun dired-pop-to-buffer (buf)
  2967. "Pop up buffer BUF in a way suitable for Dired."
  2968. (declare (obsolete dired-mark-pop-up "24.3"))
  2969. (let ((split-window-preferred-function
  2970. (lambda (window)
  2971. (or (and (let ((split-height-threshold 0))
  2972. (window-splittable-p (selected-window)))
  2973. ;; Try to split the selected window vertically if
  2974. ;; that's possible. (Bug#1806)
  2975. (split-window-below))
  2976. ;; Otherwise, try to split WINDOW sensibly.
  2977. (split-window-sensibly window))))
  2978. pop-up-frames)
  2979. (pop-to-buffer (get-buffer-create buf)))
  2980. ;; See Bug#12281.
  2981. (set-window-start nil (point-min))
  2982. ;; If dired-shrink-to-fit is t, make its window fit its contents.
  2983. (when dired-shrink-to-fit
  2984. ;; Try to not delete window when we want to display less than
  2985. ;; `window-min-height' lines.
  2986. (fit-window-to-buffer (get-buffer-window buf) nil 1 nil nil t)))
  2987. (defcustom dired-no-confirm nil
  2988. "A list of symbols for commands Dired should not confirm, or t.
  2989. Command symbols are `byte-compile', `chgrp', `chmod', `chown', `compress',
  2990. `copy', `delete', `hardlink', `load', `move', `print', `shell', `symlink',
  2991. `touch' and `uncompress'.
  2992. If t, confirmation is never needed."
  2993. :group 'dired
  2994. :type '(choice (const :tag "Confirmation never needed" t)
  2995. (set (const byte-compile) (const chgrp)
  2996. (const chmod) (const chown) (const compress)
  2997. (const copy) (const delete) (const hardlink)
  2998. (const load) (const move) (const print)
  2999. (const shell) (const symlink) (const touch)
  3000. (const uncompress))))
  3001. (defun dired-mark-pop-up (buffer-or-name op-symbol files function &rest args)
  3002. "Return FUNCTION's result on ARGS after showing which files are marked.
  3003. Displays the file names in a window showing a buffer named
  3004. BUFFER-OR-NAME; the default name being \" *Marked Files*\". The
  3005. window is not shown if there is just one file, `dired-no-confirm'
  3006. is t, or OP-SYMBOL is a member of the list in `dired-no-confirm'.
  3007. By default, Dired shrinks the display buffer to fit the marked files.
  3008. To disable this, use the Customization interface to add a new rule
  3009. to `display-buffer-alist' where condition regexp is \"^ \\*Marked Files\\*$\",
  3010. action argument symbol is `window-height' and its value is nil.
  3011. FILES is the list of marked files. It can also be (t FILENAME)
  3012. in the case of one marked file, to distinguish that from using
  3013. just the current file.
  3014. FUNCTION should not manipulate files, just read input (an
  3015. argument or confirmation)."
  3016. (if (or (eq dired-no-confirm t)
  3017. (memq op-symbol dired-no-confirm)
  3018. ;; If FILES defaulted to the current line's file.
  3019. (= (length files) 1))
  3020. (apply function args)
  3021. (let ((buffer (get-buffer-create (or buffer-or-name " *Marked Files*")))
  3022. ;; Mark *Marked Files* window as softly-dedicated, to prevent
  3023. ;; other buffers e.g. *Completions* from reusing it (bug#17554).
  3024. (display-buffer-mark-dedicated 'soft))
  3025. (with-displayed-buffer-window
  3026. buffer
  3027. (cons 'display-buffer-below-selected
  3028. '((window-height . fit-window-to-buffer)
  3029. (preserve-size . (nil . t))))
  3030. #'(lambda (window _value)
  3031. (with-selected-window window
  3032. (unwind-protect
  3033. (apply function args)
  3034. (when (window-live-p window)
  3035. (quit-restore-window window 'kill)))))
  3036. ;; Handle (t FILE) just like (FILE), here. That value is
  3037. ;; used (only in some cases), to mean just one file that was
  3038. ;; marked, rather than the current line file.
  3039. (with-current-buffer buffer
  3040. (dired-format-columns-of-files
  3041. (if (eq (car files) t) (cdr files) files))
  3042. (remove-text-properties (point-min) (point-max)
  3043. '(mouse-face nil help-echo nil)))))))
  3044. (defun dired-format-columns-of-files (files)
  3045. (let ((beg (point)))
  3046. (completion--insert-strings files)
  3047. (put-text-property beg (point) 'mouse-face nil)))
  3048. ;; Commands to mark or flag file(s) at or near current line.
  3049. (defun dired-repeat-over-lines (arg function)
  3050. ;; This version skips non-file lines.
  3051. (let ((pos (make-marker)))
  3052. (beginning-of-line)
  3053. (while (and (> arg 0) (not (eobp)))
  3054. (setq arg (1- arg))
  3055. (beginning-of-line)
  3056. (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
  3057. (save-excursion
  3058. (forward-line 1)
  3059. (move-marker pos (1+ (point))))
  3060. (save-excursion (funcall function))
  3061. ;; Advance to the next line--actually, to the line that *was* next.
  3062. ;; (If FUNCTION inserted some new lines in between, skip them.)
  3063. (goto-char pos))
  3064. (while (and (< arg 0) (not (bobp)))
  3065. (setq arg (1+ arg))
  3066. (forward-line -1)
  3067. (while (and (not (bobp)) (dired-between-files)) (forward-line -1))
  3068. (beginning-of-line)
  3069. (save-excursion (funcall function)))
  3070. (move-marker pos nil)
  3071. (dired-move-to-filename)))
  3072. (defun dired-between-files ()
  3073. ;; This used to be a regexp match of the `total ...' line output by
  3074. ;; ls, which is slightly faster, but that is not very robust; notably,
  3075. ;; it fails for non-english locales.
  3076. (save-excursion (not (dired-move-to-filename))))
  3077. (defun dired-next-marked-file (arg &optional wrap opoint)
  3078. "Move to the next marked file.
  3079. If WRAP is non-nil, wrap around to the beginning of the buffer if
  3080. we reach the end."
  3081. (interactive "p\np")
  3082. (or opoint (setq opoint (point)));; return to where interactively started
  3083. (if (if (> arg 0)
  3084. (re-search-forward dired-re-mark nil t arg)
  3085. (beginning-of-line)
  3086. (re-search-backward dired-re-mark nil t (- arg)))
  3087. (dired-move-to-filename)
  3088. (if (null wrap)
  3089. (progn
  3090. (goto-char opoint)
  3091. (error "No next marked file"))
  3092. (message "(Wraparound for next marked file)")
  3093. (goto-char (if (> arg 0) (point-min) (point-max)))
  3094. (dired-next-marked-file arg nil opoint))))
  3095. (defun dired-prev-marked-file (arg &optional wrap)
  3096. "Move to the previous marked file.
  3097. If WRAP is non-nil, wrap around to the end of the buffer if we
  3098. reach the beginning of the buffer."
  3099. (interactive "p\np")
  3100. (dired-next-marked-file (- arg) wrap))
  3101. (defun dired-file-marker (file)
  3102. ;; Return FILE's marker, or nil if unmarked.
  3103. (save-excursion
  3104. (and (dired-goto-file file)
  3105. (progn
  3106. (beginning-of-line)
  3107. (if (not (equal ?\040 (following-char)))
  3108. (following-char))))))
  3109. (defun dired-mark-files-in-region (start end)
  3110. (let ((inhibit-read-only t))
  3111. (if (> start end)
  3112. (error "start > end"))
  3113. (goto-char start) ; assumed at beginning of line
  3114. (while (< (point) end)
  3115. ;; Skip subdir line and following garbage like the `total' line:
  3116. (while (and (< (point) end) (dired-between-files))
  3117. (forward-line 1))
  3118. (if (and (not (looking-at-p dired-re-dot))
  3119. (dired-get-filename nil t))
  3120. (progn
  3121. (delete-char 1)
  3122. (insert dired-marker-char)))
  3123. (forward-line 1))))
  3124. (defun dired-mark (arg &optional interactive)
  3125. "Mark the file at point in the Dired buffer.
  3126. If the region is active, mark all files in the region.
  3127. Otherwise, with a prefix arg, mark files on the next ARG lines.
  3128. If on a subdir headerline, mark all its files except `.' and `..'.
  3129. Use \\[dired-unmark-all-files] to remove all marks
  3130. and \\[dired-unmark] on a subdir to remove the marks in
  3131. this subdir."
  3132. (interactive (list current-prefix-arg t))
  3133. (cond
  3134. ;; Mark files in the active region.
  3135. ((and interactive (use-region-p))
  3136. (save-excursion
  3137. (let ((beg (region-beginning))
  3138. (end (region-end)))
  3139. (dired-mark-files-in-region
  3140. (progn (goto-char beg) (line-beginning-position))
  3141. (progn (goto-char end) (line-beginning-position))))))
  3142. ;; Mark subdir files from the subdir headerline.
  3143. ((dired-get-subdir)
  3144. (save-excursion (dired-mark-subdir-files)))
  3145. ;; Mark the current (or next ARG) files.
  3146. (t
  3147. (let ((inhibit-read-only t))
  3148. (dired-repeat-over-lines
  3149. (prefix-numeric-value arg)
  3150. (lambda () (delete-char 1) (insert dired-marker-char)))))))
  3151. (defun dired-unmark (arg &optional interactive)
  3152. "Unmark the file at point in the Dired buffer.
  3153. If the region is active, unmark all files in the region.
  3154. Otherwise, with a prefix arg, unmark files on the next ARG lines.
  3155. If looking at a subdir, unmark all its files except `.' and `..'.
  3156. If the region is active in Transient Mark mode, unmark all files
  3157. in the active region."
  3158. (interactive (list current-prefix-arg t))
  3159. (let ((dired-marker-char ?\040))
  3160. (dired-mark arg interactive)))
  3161. (defun dired-flag-file-deletion (arg &optional interactive)
  3162. "In Dired, flag the current line's file for deletion.
  3163. If the region is active, flag all files in the region.
  3164. Otherwise, with a prefix arg, flag files on the next ARG lines.
  3165. If on a subdir headerline, flag all its files except `.' and `..'.
  3166. If the region is active in Transient Mark mode, flag all files
  3167. in the active region."
  3168. (interactive (list current-prefix-arg t))
  3169. (let ((dired-marker-char dired-del-marker))
  3170. (dired-mark arg interactive)))
  3171. (defun dired-unmark-backward (arg)
  3172. "In Dired, move up lines and remove marks or deletion flags there.
  3173. Optional prefix ARG says how many lines to unmark/unflag; default
  3174. is one line.
  3175. If the region is active in Transient Mark mode, unmark all files
  3176. in the active region."
  3177. (interactive "p")
  3178. (dired-unmark (- arg) t))
  3179. (defun dired-toggle-marks ()
  3180. "Toggle marks: marked files become unmarked, and vice versa.
  3181. Files marked with other flags (such as `D') are not affected.
  3182. `.' and `..' are never toggled.
  3183. As always, hidden subdirs are not affected."
  3184. (interactive)
  3185. (save-excursion
  3186. (goto-char (point-min))
  3187. (let ((inhibit-read-only t))
  3188. (while (not (eobp))
  3189. (or (dired-between-files)
  3190. (looking-at-p dired-re-dot)
  3191. ;; use subst instead of insdel because it does not move
  3192. ;; the gap and thus should be faster and because
  3193. ;; other characters are left alone automatically
  3194. (apply 'subst-char-in-region
  3195. (point) (1+ (point))
  3196. (if (eq ?\040 (following-char)) ; SPC
  3197. (list ?\040 dired-marker-char)
  3198. (list dired-marker-char ?\040))))
  3199. (forward-line 1)))))
  3200. ;;; Commands to mark or flag files based on their characteristics or names.
  3201. (defvar dired-regexp-history nil
  3202. "History list of regular expressions used in Dired commands.")
  3203. (defun dired-read-regexp (prompt &optional default history)
  3204. "Read a regexp using `read-regexp'."
  3205. (declare (obsolete read-regexp "24.5"))
  3206. (read-regexp prompt default (or history 'dired-regexp-history)))
  3207. (defun dired-mark-files-regexp (regexp &optional marker-char)
  3208. "Mark all files matching REGEXP for use in later commands.
  3209. A prefix argument means to unmark them instead.
  3210. `.' and `..' are never marked.
  3211. REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
  3212. object files--just `.o' will mark more than you might think."
  3213. (interactive
  3214. (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
  3215. " files (regexp): ")
  3216. ;; Add more suggestions into the default list
  3217. (cons nil (list (dired-get-filename t t)
  3218. (and (dired-get-filename nil t)
  3219. (concat (regexp-quote
  3220. (file-name-extension
  3221. (dired-get-filename nil t) t))
  3222. "\\'"))))
  3223. 'dired-regexp-history)
  3224. (if current-prefix-arg ?\040)))
  3225. (let ((dired-marker-char (or marker-char dired-marker-char)))
  3226. (dired-mark-if
  3227. (and (not (looking-at-p dired-re-dot))
  3228. (not (eolp)) ; empty line
  3229. (let ((fn (dired-get-filename t t)))
  3230. (and fn (string-match-p regexp fn))))
  3231. "matching file")))
  3232. (defun dired-mark-files-containing-regexp (regexp &optional marker-char)
  3233. "Mark all files with contents containing REGEXP for use in later commands.
  3234. A prefix argument means to unmark them instead.
  3235. `.' and `..' are never marked.
  3236. Note that if a file is visited in an Emacs buffer, and
  3237. `dired-always-read-filesystem' is nil, this command will
  3238. look in the buffer without revisiting the file, so the results might
  3239. be inconsistent with the file on disk if its contents has changed
  3240. since it was last visited."
  3241. (interactive
  3242. (list (read-regexp (concat (if current-prefix-arg "Unmark" "Mark")
  3243. " files containing (regexp): ")
  3244. nil 'dired-regexp-history)
  3245. (if current-prefix-arg ?\040)))
  3246. (let ((dired-marker-char (or marker-char dired-marker-char)))
  3247. (dired-mark-if
  3248. (and (not (looking-at-p dired-re-dot))
  3249. (not (eolp)) ; empty line
  3250. (let ((fn (dired-get-filename nil t)))
  3251. (when (and fn (file-readable-p fn)
  3252. (not (file-directory-p fn)))
  3253. (let ((prebuf (get-file-buffer fn)))
  3254. (message "Checking %s" fn)
  3255. ;; For now we do it inside emacs
  3256. ;; Grep might be better if there are a lot of files
  3257. (if (and prebuf (not dired-always-read-filesystem))
  3258. (with-current-buffer prebuf
  3259. (save-excursion
  3260. (goto-char (point-min))
  3261. (re-search-forward regexp nil t)))
  3262. (with-temp-buffer
  3263. (insert-file-contents fn)
  3264. (goto-char (point-min))
  3265. (re-search-forward regexp nil t))))
  3266. )))
  3267. "matching file")))
  3268. (defun dired-flag-files-regexp (regexp)
  3269. "In Dired, flag all files containing the specified REGEXP for deletion.
  3270. The match is against the non-directory part of the filename. Use `^'
  3271. and `$' to anchor matches. Exclude subdirs by hiding them.
  3272. `.' and `..' are never flagged."
  3273. (interactive (list (read-regexp "Flag for deletion (regexp): "
  3274. nil 'dired-regexp-history)))
  3275. (dired-mark-files-regexp regexp dired-del-marker))
  3276. (defun dired-mark-symlinks (unflag-p)
  3277. "Mark all symbolic links.
  3278. With prefix argument, unmark or unflag all those files."
  3279. (interactive "P")
  3280. (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  3281. (dired-mark-if (looking-at-p dired-re-sym) "symbolic link")))
  3282. (defun dired-mark-directories (unflag-p)
  3283. "Mark all directory file lines except `.' and `..'.
  3284. With prefix argument, unmark or unflag all those files."
  3285. (interactive "P")
  3286. (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  3287. (dired-mark-if (and (looking-at-p dired-re-dir)
  3288. (not (looking-at-p dired-re-dot)))
  3289. "directory file")))
  3290. (defun dired-mark-executables (unflag-p)
  3291. "Mark all executable files.
  3292. With prefix argument, unmark or unflag all those files."
  3293. (interactive "P")
  3294. (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char)))
  3295. (dired-mark-if (looking-at-p dired-re-exe) "executable file")))
  3296. ;; dired-x.el has a dired-mark-sexp interactive command: mark
  3297. ;; files for which PREDICATE returns non-nil.
  3298. (defun dired-flag-auto-save-files (&optional unflag-p)
  3299. "Flag for deletion files whose names suggest they are auto save files.
  3300. A prefix argument says to unmark or unflag those files instead."
  3301. (interactive "P")
  3302. (let ((dired-marker-char (if unflag-p ?\040 dired-del-marker)))
  3303. (dired-mark-if
  3304. ;; It is less than general to check for # here,
  3305. ;; but it's the only way this runs fast enough.
  3306. (and (save-excursion (end-of-line)
  3307. (or
  3308. (eq (preceding-char) ?#)
  3309. ;; Handle executables in case of -F option.
  3310. ;; We need not worry about the other kinds
  3311. ;; of markings that -F makes, since they won't
  3312. ;; appear on real auto-save files.
  3313. (if (eq (preceding-char) ?*)
  3314. (progn
  3315. (forward-char -1)
  3316. (eq (preceding-char) ?#)))))
  3317. (not (looking-at-p dired-re-dir))
  3318. (let ((fn (dired-get-filename t t)))
  3319. (if fn (auto-save-file-name-p
  3320. (file-name-nondirectory fn)))))
  3321. "auto save file")))
  3322. (defcustom dired-garbage-files-regexp
  3323. ;; `log' here is dubious, since it's typically used for useful log
  3324. ;; files, not just TeX stuff. -- fx
  3325. (concat (regexp-opt
  3326. '(".log" ".toc" ".dvi" ".bak" ".orig" ".rej" ".aux"))
  3327. "\\'")
  3328. "Regular expression to match \"garbage\" files for `dired-flag-garbage-files'."
  3329. :type 'regexp
  3330. :group 'dired)
  3331. (defun dired-flag-garbage-files ()
  3332. "Flag for deletion all files that match `dired-garbage-files-regexp'."
  3333. (interactive)
  3334. (dired-flag-files-regexp dired-garbage-files-regexp))
  3335. (defun dired-flag-backup-files (&optional unflag-p)
  3336. "Flag all backup files (names ending with `~') for deletion.
  3337. With prefix argument, unmark or unflag these files."
  3338. (interactive "P")
  3339. (let ((dired-marker-char (if unflag-p ?\s dired-del-marker)))
  3340. (dired-mark-if
  3341. ;; Don't call backup-file-name-p unless the last character looks like
  3342. ;; it might be the end of a backup file name. This isn't very general,
  3343. ;; but it's the only way this runs fast enough.
  3344. (and (save-excursion (end-of-line)
  3345. ;; Handle executables in case of -F option.
  3346. ;; We need not worry about the other kinds
  3347. ;; of markings that -F makes, since they won't
  3348. ;; appear on real backup files.
  3349. (if (eq (preceding-char) ?*)
  3350. (forward-char -1))
  3351. (eq (preceding-char) ?~))
  3352. (not (looking-at-p dired-re-dir))
  3353. (let ((fn (dired-get-filename t t)))
  3354. (if fn (backup-file-name-p fn))))
  3355. "backup file")))
  3356. (defun dired-change-marks (&optional old new)
  3357. "Change all OLD marks to NEW marks.
  3358. OLD and NEW are both characters used to mark files."
  3359. (interactive
  3360. (let* ((cursor-in-echo-area t)
  3361. (old (progn (message "Change (old mark): ") (read-char)))
  3362. (new (progn (message "Change %c marks to (new mark): " old)
  3363. (read-char))))
  3364. (list old new)))
  3365. (if (or (eq old ?\r) (eq new ?\r))
  3366. (ding)
  3367. (let ((string (format "\n%c" old))
  3368. (inhibit-read-only t))
  3369. (save-excursion
  3370. (goto-char (point-min))
  3371. (while (search-forward string nil t)
  3372. (if (if (= old ?\s)
  3373. (save-match-data
  3374. (dired-get-filename 'no-dir t))
  3375. t)
  3376. (subst-char-in-region (match-beginning 0)
  3377. (match-end 0) old new)))))))
  3378. (defun dired-unmark-all-marks ()
  3379. "Remove all marks from all files in the Dired buffer."
  3380. (interactive)
  3381. (dired-unmark-all-files ?\r))
  3382. ;; Bound in dired-unmark-all-files
  3383. (defvar dired-unmark-all-files-query)
  3384. (defun dired-unmark-all-files (mark &optional arg)
  3385. "Remove a specific mark (or any mark) from every file.
  3386. After this command, type the mark character to remove,
  3387. or type RET to remove all marks.
  3388. With prefix arg, query for each marked file.
  3389. Type \\[help-command] at that time for help."
  3390. (interactive "cRemove marks (RET means all): \nP")
  3391. (save-excursion
  3392. (let* ((count 0)
  3393. (inhibit-read-only t) case-fold-search
  3394. dired-unmark-all-files-query
  3395. (string (format "\n%c" mark))
  3396. (help-form "\
  3397. Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
  3398. `!' to unmark all remaining files with no more questions."))
  3399. (goto-char (point-min))
  3400. (while (if (eq mark ?\r)
  3401. (re-search-forward dired-re-mark nil t)
  3402. (search-forward string nil t))
  3403. (if (or (not arg)
  3404. (let ((file (dired-get-filename t t)))
  3405. (and file
  3406. (dired-query 'dired-unmark-all-files-query
  3407. "Unmark file `%s'? "
  3408. file))))
  3409. (progn (subst-char-in-region (1- (point)) (point)
  3410. (preceding-char) ?\s)
  3411. (setq count (1+ count)))))
  3412. (message (if (= count 1) "1 mark removed"
  3413. "%d marks removed")
  3414. count))))
  3415. ;; Logging failures operating on files, and showing the results.
  3416. (defvar dired-log-buffer "*Dired log*")
  3417. (defun dired-why ()
  3418. "Pop up a buffer with error log output from Dired.
  3419. A group of errors from a single command ends with a formfeed.
  3420. Thus, use \\[backward-page] to find the beginning of a group of errors."
  3421. (interactive)
  3422. (if (get-buffer dired-log-buffer)
  3423. (let ((owindow (selected-window))
  3424. (window (display-buffer (get-buffer dired-log-buffer))))
  3425. (unwind-protect
  3426. (progn
  3427. (select-window window)
  3428. (goto-char (point-max))
  3429. (forward-line -1)
  3430. (backward-page 1)
  3431. (recenter 0))
  3432. (select-window owindow)))))
  3433. (defun dired-log (log &rest args)
  3434. ;; Log a message or the contents of a buffer.
  3435. ;; If LOG is a string and there are more args, it is formatted with
  3436. ;; those ARGS. Usually the LOG string ends with a \n.
  3437. ;; End each bunch of errors with (dired-log t):
  3438. ;; this inserts the current time and buffer at the start of the page,
  3439. ;; and \f (formfeed) at the end.
  3440. (let ((obuf (current-buffer)))
  3441. (with-current-buffer (get-buffer-create dired-log-buffer)
  3442. (goto-char (point-max))
  3443. (let ((inhibit-read-only t))
  3444. (cond ((stringp log)
  3445. (insert (if args
  3446. (apply #'format-message log args)
  3447. log)))
  3448. ((bufferp log)
  3449. (insert-buffer-substring log))
  3450. ((eq t log)
  3451. (backward-page 1)
  3452. (unless (bolp)
  3453. (insert "\n"))
  3454. (insert (current-time-string)
  3455. (format-message "\tBuffer `%s'\n" (buffer-name obuf)))
  3456. (goto-char (point-max))
  3457. (insert "\f\n")))))))
  3458. (defun dired-log-summary (string failures)
  3459. "State a summary of a command's failures, in echo area and log buffer.
  3460. STRING is an overall summary of the failures.
  3461. FAILURES is a list of file names that we failed to operate on,
  3462. or nil if file names are not applicable."
  3463. (if (= (length failures) 1)
  3464. (message "%s"
  3465. (with-current-buffer dired-log-buffer
  3466. (goto-char (point-max))
  3467. (backward-page 1)
  3468. (if (eolp) (forward-line 1))
  3469. (buffer-substring (point) (point-max))))
  3470. (message (if failures "%s--type ? for details (%s)"
  3471. "%s--type ? for details")
  3472. string failures))
  3473. ;; Log a summary describing a bunch of errors.
  3474. (dired-log (concat "\n" string "\n"))
  3475. (dired-log t))
  3476. ;;; Sorting
  3477. ;; Most ls can only sort by name or by date (with -t), nothing else.
  3478. ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
  3479. ;; So anything that does not contain these is sort "by name".
  3480. (defvar dired-ls-sorting-switches "SXU"
  3481. "String of `ls' switches (single letters) except \"t\" that influence sorting.
  3482. This indicates to Dired which option switches to watch out for because they
  3483. will change the sorting order behavior of `ls'.
  3484. To change the default sorting order (e.g. add a `-v' option), see the
  3485. variable `dired-listing-switches'. To temporarily override the listing
  3486. format, use `\\[universal-argument] \\[dired]'.")
  3487. (defvar dired-sort-by-date-regexp
  3488. (concat "\\(\\`\\| \\)-[^- ]*t"
  3489. ;; `dired-ls-sorting-switches' after -t overrides -t.
  3490. "[^ " dired-ls-sorting-switches "]*"
  3491. "\\(\\(\\`\\| +\\)\\(--[^ ]+\\|-[^- t"
  3492. dired-ls-sorting-switches "]+\\)\\)* *$")
  3493. "Regexp recognized by Dired to set `by date' mode.")
  3494. (defvar dired-sort-by-name-regexp
  3495. (concat "\\`\\(\\(\\`\\| +\\)\\(--[^ ]+\\|"
  3496. "-[^- t" dired-ls-sorting-switches "]+\\)\\)* *$")
  3497. "Regexp recognized by Dired to set `by name' mode.")
  3498. (defvar dired-sort-inhibit nil
  3499. "Non-nil means the Dired sort command is disabled.
  3500. The idea is to set this buffer-locally in special Dired buffers.")
  3501. (defun dired-sort-set-mode-line ()
  3502. ;; Set mode line display according to dired-actual-switches.
  3503. ;; Mode line display of "by name" or "by date" guarantees the user a
  3504. ;; match with the corresponding regexps. Non-matching switches are
  3505. ;; shown literally.
  3506. (when (eq major-mode 'dired-mode)
  3507. (setq mode-name
  3508. (let (case-fold-search)
  3509. (cond ((string-match-p
  3510. dired-sort-by-name-regexp dired-actual-switches)
  3511. "Dired by name")
  3512. ((string-match-p
  3513. dired-sort-by-date-regexp dired-actual-switches)
  3514. "Dired by date")
  3515. (t
  3516. (concat "Dired " dired-actual-switches)))))
  3517. (force-mode-line-update)))
  3518. (define-obsolete-function-alias 'dired-sort-set-modeline
  3519. 'dired-sort-set-mode-line "24.3")
  3520. (defun dired-sort-toggle-or-edit (&optional arg)
  3521. "Toggle sorting by date, and refresh the Dired buffer.
  3522. With a prefix argument, edit the current listing switches instead."
  3523. (interactive "P")
  3524. (when dired-sort-inhibit
  3525. (error "Cannot sort this Dired buffer"))
  3526. (if arg
  3527. (dired-sort-other
  3528. (read-string "ls switches (must contain -l): " dired-actual-switches))
  3529. (dired-sort-toggle)))
  3530. (defun dired-sort-toggle ()
  3531. ;; Toggle between sort by date/name. Reverts the buffer.
  3532. (let ((sorting-by-date (string-match-p dired-sort-by-date-regexp
  3533. dired-actual-switches))
  3534. ;; Regexp for finding (possibly embedded) -t switches.
  3535. (switch-regexp "\\(\\`\\| \\)-\\([a-su-zA-Z]*\\)\\(t\\)\\([^ ]*\\)")
  3536. case-fold-search)
  3537. ;; Remove the -t switch.
  3538. (while (string-match switch-regexp dired-actual-switches)
  3539. (if (and (equal (match-string 2 dired-actual-switches) "")
  3540. (equal (match-string 4 dired-actual-switches) ""))
  3541. ;; Remove a stand-alone -t switch.
  3542. (setq dired-actual-switches
  3543. (replace-match "" t t dired-actual-switches))
  3544. ;; Remove a switch of the form -XtY for some X and Y.
  3545. (setq dired-actual-switches
  3546. (replace-match "" t t dired-actual-switches 3))))
  3547. ;; Now, if we weren't sorting by date before, add the -t switch.
  3548. ;; Some simple-minded ls implementations (eg ftp servers) only
  3549. ;; allow a single option string, so try not to add " -t" if possible.
  3550. (unless sorting-by-date
  3551. (setq dired-actual-switches
  3552. (concat dired-actual-switches
  3553. (if (string-match-p "\\`-[[:alnum:]]+\\'"
  3554. dired-actual-switches)
  3555. "t"
  3556. " -t")))))
  3557. (dired-sort-set-mode-line)
  3558. (revert-buffer))
  3559. ;; Some user code loads dired especially for this.
  3560. ;; Don't do that--use replace-regexp-in-string instead.
  3561. (defun dired-replace-in-string (regexp newtext string)
  3562. ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
  3563. ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
  3564. (let ((result "") (start 0) mb me)
  3565. (while (string-match regexp string start)
  3566. (setq mb (match-beginning 0)
  3567. me (match-end 0)
  3568. result (concat result (substring string start mb) newtext)
  3569. start me))
  3570. (concat result (substring string start))))
  3571. (defun dired-sort-other (switches &optional no-revert)
  3572. "Specify new `ls' SWITCHES for current Dired buffer.
  3573. Values matching `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp'
  3574. set the minor mode accordingly, others appear literally in the mode line.
  3575. With optional second arg NO-REVERT, don't refresh the listing afterwards."
  3576. (dired-sort-R-check switches)
  3577. (setq dired-actual-switches switches)
  3578. (dired-sort-set-mode-line)
  3579. (or no-revert (revert-buffer)))
  3580. (defvar-local dired-subdir-alist-pre-R nil
  3581. "Value of `dired-subdir-alist' before -R switch added.")
  3582. (defun dired-sort-R-check (switches)
  3583. "Additional processing of -R in ls option string SWITCHES.
  3584. Saves `dired-subdir-alist' when R is set and restores saved value
  3585. minus any directories explicitly deleted when R is cleared.
  3586. To be called first in body of `dired-sort-other', etc."
  3587. (cond
  3588. ((and (dired-switches-recursive-p switches)
  3589. (not (dired-switches-recursive-p dired-actual-switches)))
  3590. ;; Adding -R to ls switches -- save `dired-subdir-alist':
  3591. (setq dired-subdir-alist-pre-R dired-subdir-alist))
  3592. ((and (dired-switches-recursive-p dired-actual-switches)
  3593. (not (dired-switches-recursive-p switches)))
  3594. ;; Deleting -R from ls switches -- revert to pre-R subdirs
  3595. ;; that are still present:
  3596. (setq dired-subdir-alist
  3597. (if dired-subdir-alist-pre-R
  3598. (let (subdirs)
  3599. (while dired-subdir-alist-pre-R
  3600. (if (assoc (caar dired-subdir-alist-pre-R)
  3601. dired-subdir-alist)
  3602. ;; subdir still present...
  3603. (setq subdirs
  3604. (cons (car dired-subdir-alist-pre-R)
  3605. subdirs)))
  3606. (setq dired-subdir-alist-pre-R
  3607. (cdr dired-subdir-alist-pre-R)))
  3608. (reverse subdirs))
  3609. ;; No pre-R subdir alist, so revert to main directory
  3610. ;; listing:
  3611. (list (car (reverse dired-subdir-alist))))))))
  3612. ;;;; Drag and drop support
  3613. (defcustom dired-recursive-copies 'top
  3614. "Whether Dired copies directories recursively.
  3615. If nil, never copy recursively.
  3616. `always' means to copy recursively without asking.
  3617. `top' means to ask for each directory at top level.
  3618. Any other value means to ask for each directory."
  3619. :type '(choice :tag "Copy directories"
  3620. (const :tag "No recursive copies" nil)
  3621. (const :tag "Ask for each directory" t)
  3622. (const :tag "Ask for each top directory only" top)
  3623. (const :tag "Copy directories without asking" always))
  3624. :group 'dired)
  3625. (defun dired-dnd-popup-notice ()
  3626. (message-box
  3627. "Dired recursive copies are currently disabled.\nSee the variable `dired-recursive-copies'."))
  3628. (declare-function x-popup-menu "menu.c" (position menu))
  3629. (defun dired-dnd-do-ask-action (uri)
  3630. ;; No need to get actions and descriptions from the source,
  3631. ;; we only have three actions anyway.
  3632. (let ((action (x-popup-menu
  3633. t
  3634. (list "What action?"
  3635. (cons ""
  3636. '(("Copy here" . copy)
  3637. ("Move here" . move)
  3638. ("Link here" . link)
  3639. "--"
  3640. ("Cancel" . nil)))))))
  3641. (if action
  3642. (dired-dnd-handle-local-file uri action)
  3643. nil)))
  3644. (declare-function dired-relist-entry "dired-aux" (file))
  3645. (declare-function make-symbolic-link "fileio.c")
  3646. ;; Only used when (featurep 'dnd).
  3647. (declare-function dnd-get-local-file-name "dnd" (uri &optional must-exist))
  3648. (declare-function dnd-get-local-file-uri "dnd" (uri))
  3649. (defvar dired-overwrite-confirmed) ;Defined in dired-aux.
  3650. (defun dired-dnd-handle-local-file (uri action)
  3651. "Copy, move or link a file to the Dired directory.
  3652. URI is the file to handle, ACTION is one of copy, move, link or ask.
  3653. Ask means pop up a menu for the user to select one of copy, move or link."
  3654. (require 'dired-aux)
  3655. (let* ((from (dnd-get-local-file-name uri t))
  3656. (to (when from
  3657. (concat (dired-current-directory)
  3658. (file-name-nondirectory from)))))
  3659. (when from
  3660. (cond ((eq action 'ask)
  3661. (dired-dnd-do-ask-action uri))
  3662. ;; If copying a directory and dired-recursive-copies is
  3663. ;; nil, dired-copy-file fails. Pop up a notice.
  3664. ((and (memq action '(copy private))
  3665. (file-directory-p from)
  3666. (not dired-recursive-copies))
  3667. (dired-dnd-popup-notice))
  3668. ((memq action '(copy private move link))
  3669. (let ((overwrite (and (file-exists-p to)
  3670. (y-or-n-p
  3671. (format-message
  3672. "Overwrite existing file `%s'? " to))))
  3673. ;; Binding dired-overwrite-confirmed to nil makes
  3674. ;; dired-handle-overwrite a no-op. We instead use
  3675. ;; y-or-n-p, which pops a graphical menu.
  3676. dired-overwrite-confirmed backup-file)
  3677. (when (and overwrite
  3678. ;; d-b-o is defined in dired-aux.
  3679. (boundp 'dired-backup-overwrite)
  3680. dired-backup-overwrite
  3681. (setq backup-file
  3682. (car (find-backup-file-name to)))
  3683. (or (eq dired-backup-overwrite 'always)
  3684. (y-or-n-p
  3685. (format-message
  3686. "Make backup for existing file `%s'? " to))))
  3687. (rename-file to backup-file 0)
  3688. (dired-relist-entry backup-file))
  3689. (cond ((memq action '(copy private))
  3690. (dired-copy-file from to overwrite))
  3691. ((eq action 'move)
  3692. (dired-rename-file from to overwrite))
  3693. ((eq action 'link)
  3694. (make-symbolic-link from to overwrite)))
  3695. (dired-relist-entry to)
  3696. action))))))
  3697. (defun dired-dnd-handle-file (uri action)
  3698. "Copy, move or link a file to the Dired directory if it is a local file.
  3699. URI is the file to handle. If the hostname in the URI isn't local, do nothing.
  3700. ACTION is one of copy, move, link or ask.
  3701. Ask means pop up a menu for the user to select one of copy, move or link."
  3702. (let ((local-file (dnd-get-local-file-uri uri)))
  3703. (if local-file (dired-dnd-handle-local-file local-file action)
  3704. nil)))
  3705. ;;;; Desktop support
  3706. (eval-when-compile (require 'desktop))
  3707. (declare-function desktop-file-name "desktop" (filename dirname))
  3708. (defun dired-desktop-buffer-misc-data (dirname)
  3709. "Auxiliary information to be saved in desktop file."
  3710. (cons
  3711. ;; Value of `dired-directory'.
  3712. (if (consp dired-directory)
  3713. ;; Directory name followed by list of files.
  3714. (cons (desktop-file-name (car dired-directory) dirname)
  3715. (cdr dired-directory))
  3716. ;; Directory name, optionally with shell wildcard.
  3717. (desktop-file-name dired-directory dirname))
  3718. ;; Subdirectories in `dired-subdir-alist'.
  3719. (cdr
  3720. (nreverse
  3721. (mapcar
  3722. (lambda (f) (desktop-file-name (car f) dirname))
  3723. dired-subdir-alist)))))
  3724. (defun dired-restore-desktop-buffer (_file-name
  3725. _buffer-name
  3726. misc-data)
  3727. "Restore a Dired buffer specified in a desktop file."
  3728. ;; First element of `misc-data' is the value of `dired-directory'.
  3729. ;; This value is a directory name, optionally with shell wildcard or
  3730. ;; a directory name followed by list of files.
  3731. (let* ((dired-dir (car misc-data))
  3732. (dir (if (consp dired-dir) (car dired-dir) dired-dir)))
  3733. (if (file-directory-p (file-name-directory dir))
  3734. (with-demoted-errors "Desktop: Problem restoring directory: %S"
  3735. (dired dired-dir)
  3736. ;; The following elements of `misc-data' are the keys
  3737. ;; from `dired-subdir-alist'.
  3738. (mapc 'dired-maybe-insert-subdir (cdr misc-data))
  3739. (current-buffer))
  3740. (message "Desktop: Directory %s no longer exists." dir)
  3741. (when desktop-missing-file-warning (sit-for 1))
  3742. nil)))
  3743. (add-to-list 'desktop-buffer-mode-handlers
  3744. '(dired-mode . dired-restore-desktop-buffer))
  3745. (provide 'dired)
  3746. (run-hooks 'dired-load-hook) ; for your customizations
  3747. ;;; dired.el ends here