files.texi 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2017 Free Software
  4. @c Foundation, Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Files
  7. @chapter Files
  8. This chapter describes the Emacs Lisp functions and variables to
  9. find, create, view, save, and otherwise work with files and
  10. directories. A few other file-related functions are described in
  11. @ref{Buffers}, and those related to backups and auto-saving are
  12. described in @ref{Backups and Auto-Saving}.
  13. Many of the file functions take one or more arguments that are file
  14. names. A file name is a string. Most of these functions expand file
  15. name arguments using the function @code{expand-file-name}, so that
  16. @file{~} is handled correctly, as are relative file names (including
  17. @file{../}). @xref{File Name Expansion}.
  18. In addition, certain @dfn{magic} file names are handled specially.
  19. For example, when a remote file name is specified, Emacs accesses the
  20. file over the network via an appropriate protocol. @xref{Remote
  21. Files,, Remote Files, emacs, The GNU Emacs Manual}. This handling is
  22. done at a very low level, so you may assume that all the functions
  23. described in this chapter accept magic file names as file name
  24. arguments, except where noted. @xref{Magic File Names}, for details.
  25. When file I/O functions signal Lisp errors, they usually use the
  26. condition @code{file-error} (@pxref{Handling Errors}). The error
  27. message is in most cases obtained from the operating system, according
  28. to locale @code{system-messages-locale}, and decoded using coding system
  29. @code{locale-coding-system} (@pxref{Locales}).
  30. @menu
  31. * Visiting Files:: Reading files into Emacs buffers for editing.
  32. * Saving Buffers:: Writing changed buffers back into files.
  33. * Reading from Files:: Reading files into buffers without visiting.
  34. * Writing to Files:: Writing new files from parts of buffers.
  35. * File Locks:: Locking and unlocking files, to prevent
  36. simultaneous editing by two people.
  37. * Information about Files:: Testing existence, accessibility, size of files.
  38. * Changing Files:: Renaming files, changing permissions, etc.
  39. * Files and Storage:: Surviving power and media failures
  40. * File Names:: Decomposing and expanding file names.
  41. * Contents of Directories:: Getting a list of the files in a directory.
  42. * Create/Delete Dirs:: Creating and Deleting Directories.
  43. * Magic File Names:: Special handling for certain file names.
  44. * Format Conversion:: Conversion to and from various file formats.
  45. @end menu
  46. @node Visiting Files
  47. @section Visiting Files
  48. @cindex finding files
  49. @cindex visiting files
  50. Visiting a file means reading a file into a buffer. Once this is
  51. done, we say that the buffer is @dfn{visiting} that file, and call the
  52. file @dfn{the visited file} of the buffer.
  53. A file and a buffer are two different things. A file is information
  54. recorded permanently in the computer (unless you delete it). A
  55. buffer, on the other hand, is information inside of Emacs that will
  56. vanish at the end of the editing session (or when you kill the
  57. buffer). When a buffer is visiting a file, it contains information
  58. copied from the file. The copy in the buffer is what you modify with
  59. editing commands. Changes to the buffer do not change the file; to
  60. make the changes permanent, you must @dfn{save} the buffer, which
  61. means copying the altered buffer contents back into the file.
  62. Despite the distinction between files and buffers, people often
  63. refer to a file when they mean a buffer and vice-versa. Indeed, we
  64. say, ``I am editing a file'', rather than, ``I am editing a buffer
  65. that I will soon save as a file of the same name''. Humans do not
  66. usually need to make the distinction explicit. When dealing with a
  67. computer program, however, it is good to keep the distinction in mind.
  68. @menu
  69. * Visiting Functions:: The usual interface functions for visiting.
  70. * Subroutines of Visiting:: Lower-level subroutines that they use.
  71. @end menu
  72. @node Visiting Functions
  73. @subsection Functions for Visiting Files
  74. @cindex visiting files, functions for
  75. @cindex how to visit files
  76. This section describes the functions normally used to visit files.
  77. For historical reasons, these functions have names starting with
  78. @samp{find-} rather than @samp{visit-}. @xref{Buffer File Name}, for
  79. functions and variables that access the visited file name of a buffer or
  80. that find an existing buffer by its visited file name.
  81. In a Lisp program, if you want to look at the contents of a file but
  82. not alter it, the fastest way is to use @code{insert-file-contents} in a
  83. temporary buffer. Visiting the file is not necessary and takes longer.
  84. @xref{Reading from Files}.
  85. @deffn Command find-file filename &optional wildcards
  86. This command selects a buffer visiting the file @var{filename},
  87. using an existing buffer if there is one, and otherwise creating a
  88. new buffer and reading the file into it. It also returns that buffer.
  89. Aside from some technical details, the body of the @code{find-file}
  90. function is basically equivalent to:
  91. @smallexample
  92. (switch-to-buffer (find-file-noselect filename nil nil wildcards))
  93. @end smallexample
  94. @noindent
  95. (See @code{switch-to-buffer} in @ref{Switching Buffers}.)
  96. If @var{wildcards} is non-@code{nil}, which is always true in an
  97. interactive call, then @code{find-file} expands wildcard characters in
  98. @var{filename} and visits all the matching files.
  99. When @code{find-file} is called interactively, it prompts for
  100. @var{filename} in the minibuffer.
  101. @end deffn
  102. @deffn Command find-file-literally filename
  103. This command visits @var{filename}, like @code{find-file} does, but it
  104. does not perform any format conversions (@pxref{Format Conversion}),
  105. character code conversions (@pxref{Coding Systems}), or end-of-line
  106. conversions (@pxref{Coding System Basics, End of line conversion}).
  107. The buffer visiting the file is made unibyte, and its major mode is
  108. Fundamental mode, regardless of the file name. File local variable
  109. specifications in the file (@pxref{File Local Variables}) are
  110. ignored, and automatic decompression and adding a newline at the end
  111. of the file due to @code{require-final-newline} (@pxref{Saving
  112. Buffers, require-final-newline}) are also disabled.
  113. Note that if Emacs already has a buffer visiting the same file
  114. non-literally, it will not visit the same file literally, but instead
  115. just switch to the existing buffer. If you want to be sure of
  116. accessing a file's contents literally, you should create a temporary
  117. buffer and then read the file contents into it using
  118. @code{insert-file-contents-literally} (@pxref{Reading from Files}).
  119. @end deffn
  120. @defun find-file-noselect filename &optional nowarn rawfile wildcards
  121. This function is the guts of all the file-visiting functions. It
  122. returns a buffer visiting the file @var{filename}. You may make the
  123. buffer current or display it in a window if you wish, but this
  124. function does not do so.
  125. The function returns an existing buffer if there is one; otherwise it
  126. creates a new buffer and reads the file into it. When
  127. @code{find-file-noselect} uses an existing buffer, it first verifies
  128. that the file has not changed since it was last visited or saved in
  129. that buffer. If the file has changed, this function asks the user
  130. whether to reread the changed file. If the user says @samp{yes}, any
  131. edits previously made in the buffer are lost.
  132. Reading the file involves decoding the file's contents (@pxref{Coding
  133. Systems}), including end-of-line conversion, and format conversion
  134. (@pxref{Format Conversion}). If @var{wildcards} is non-@code{nil},
  135. then @code{find-file-noselect} expands wildcard characters in
  136. @var{filename} and visits all the matching files.
  137. This function displays warning or advisory messages in various peculiar
  138. cases, unless the optional argument @var{nowarn} is non-@code{nil}. For
  139. example, if it needs to create a buffer, and there is no file named
  140. @var{filename}, it displays the message @samp{(New file)} in the echo
  141. area, and leaves the buffer empty.
  142. The @code{find-file-noselect} function normally calls
  143. @code{after-find-file} after reading the file (@pxref{Subroutines of
  144. Visiting}). That function sets the buffer major mode, parses local
  145. variables, warns the user if there exists an auto-save file more recent
  146. than the file just visited, and finishes by running the functions in
  147. @code{find-file-hook}.
  148. If the optional argument @var{rawfile} is non-@code{nil}, then
  149. @code{after-find-file} is not called, and the
  150. @code{find-file-not-found-functions} are not run in case of failure.
  151. What's more, a non-@code{nil} @var{rawfile} value suppresses coding
  152. system conversion and format conversion.
  153. The @code{find-file-noselect} function usually returns the buffer that
  154. is visiting the file @var{filename}. But, if wildcards are actually
  155. used and expanded, it returns a list of buffers that are visiting the
  156. various files.
  157. @example
  158. @group
  159. (find-file-noselect "/etc/fstab")
  160. @result{} #<buffer fstab>
  161. @end group
  162. @end example
  163. @end defun
  164. @deffn Command find-file-other-window filename &optional wildcards
  165. This command selects a buffer visiting the file @var{filename}, but
  166. does so in a window other than the selected window. It may use
  167. another existing window or split a window; see @ref{Switching
  168. Buffers}.
  169. When this command is called interactively, it prompts for
  170. @var{filename}.
  171. @end deffn
  172. @deffn Command find-file-read-only filename &optional wildcards
  173. This command selects a buffer visiting the file @var{filename}, like
  174. @code{find-file}, but it marks the buffer as read-only. @xref{Read Only
  175. Buffers}, for related functions and variables.
  176. When this command is called interactively, it prompts for
  177. @var{filename}.
  178. @end deffn
  179. @defopt find-file-wildcards
  180. If this variable is non-@code{nil}, then the various @code{find-file}
  181. commands check for wildcard characters and visit all the files that
  182. match them (when invoked interactively or when their @var{wildcards}
  183. argument is non-@code{nil}). If this option is @code{nil}, then
  184. the @code{find-file} commands ignore their @var{wildcards} argument
  185. and never treat wildcard characters specially.
  186. @end defopt
  187. @defopt find-file-hook
  188. The value of this variable is a list of functions to be called after a
  189. file is visited. The file's local-variables specification (if any) will
  190. have been processed before the hooks are run. The buffer visiting the
  191. file is current when the hook functions are run.
  192. This variable is a normal hook. @xref{Hooks}.
  193. @end defopt
  194. @defvar find-file-not-found-functions
  195. The value of this variable is a list of functions to be called when
  196. @code{find-file} or @code{find-file-noselect} is passed a nonexistent
  197. file name. @code{find-file-noselect} calls these functions as soon as
  198. it detects a nonexistent file. It calls them in the order of the list,
  199. until one of them returns non-@code{nil}. @code{buffer-file-name} is
  200. already set up.
  201. This is not a normal hook because the values of the functions are
  202. used, and in many cases only some of the functions are called.
  203. @end defvar
  204. @defvar find-file-literally
  205. This buffer-local variable, if set to a non-@code{nil} value, makes
  206. @code{save-buffer} behave as if the buffer were visiting its file
  207. literally, i.e., without conversions of any kind. The command
  208. @code{find-file-literally} sets this variable's local value, but other
  209. equivalent functions and commands can do that as well, e.g., to avoid
  210. automatic addition of a newline at the end of the file. This variable
  211. is permanent local, so it is unaffected by changes of major modes.
  212. @end defvar
  213. @node Subroutines of Visiting
  214. @subsection Subroutines of Visiting
  215. The @code{find-file-noselect} function uses two important subroutines
  216. which are sometimes useful in user Lisp code: @code{create-file-buffer}
  217. and @code{after-find-file}. This section explains how to use them.
  218. @c FIXME This does not describe the default behavior, because
  219. @c uniquify is enabled by default and advises this function.
  220. @c This is confusing. uniquify should be folded into the function proper.
  221. @defun create-file-buffer filename
  222. This function creates a suitably named buffer for visiting
  223. @var{filename}, and returns it. It uses @var{filename} (sans directory)
  224. as the name if that name is free; otherwise, it appends a string such as
  225. @samp{<2>} to get an unused name. See also @ref{Creating Buffers}.
  226. Note that the @file{uniquify} library affects the result of this
  227. function. @xref{Uniquify,,, emacs, The GNU Emacs Manual}.
  228. @strong{Please note:} @code{create-file-buffer} does @emph{not}
  229. associate the new buffer with a file and does not select the buffer.
  230. It also does not use the default major mode.
  231. @example
  232. @group
  233. (create-file-buffer "foo")
  234. @result{} #<buffer foo>
  235. @end group
  236. @group
  237. (create-file-buffer "foo")
  238. @result{} #<buffer foo<2>>
  239. @end group
  240. @group
  241. (create-file-buffer "foo")
  242. @result{} #<buffer foo<3>>
  243. @end group
  244. @end example
  245. This function is used by @code{find-file-noselect}.
  246. It uses @code{generate-new-buffer} (@pxref{Creating Buffers}).
  247. @end defun
  248. @defun after-find-file &optional error warn noauto after-find-file-from-revert-buffer nomodes
  249. This function sets the buffer major mode, and parses local variables
  250. (@pxref{Auto Major Mode}). It is called by @code{find-file-noselect}
  251. and by the default revert function (@pxref{Reverting}).
  252. @cindex new file message
  253. @cindex file open error
  254. If reading the file got an error because the file does not exist, but
  255. its directory does exist, the caller should pass a non-@code{nil} value
  256. for @var{error}. In that case, @code{after-find-file} issues a warning:
  257. @samp{(New file)}. For more serious errors, the caller should usually not
  258. call @code{after-find-file}.
  259. If @var{warn} is non-@code{nil}, then this function issues a warning
  260. if an auto-save file exists and is more recent than the visited file.
  261. If @var{noauto} is non-@code{nil}, that says not to enable or disable
  262. Auto-Save mode. The mode remains enabled if it was enabled before.
  263. If @var{after-find-file-from-revert-buffer} is non-@code{nil}, that
  264. means this call was from @code{revert-buffer}. This has no direct
  265. effect, but some mode functions and hook functions check the value
  266. of this variable.
  267. If @var{nomodes} is non-@code{nil}, that means don't alter the buffer's
  268. major mode, don't process local variables specifications in the file,
  269. and don't run @code{find-file-hook}. This feature is used by
  270. @code{revert-buffer} in some cases.
  271. The last thing @code{after-find-file} does is call all the functions
  272. in the list @code{find-file-hook}.
  273. @end defun
  274. @node Saving Buffers
  275. @section Saving Buffers
  276. @cindex saving buffers
  277. When you edit a file in Emacs, you are actually working on a buffer
  278. that is visiting that file---that is, the contents of the file are
  279. copied into the buffer and the copy is what you edit. Changes to the
  280. buffer do not change the file until you @dfn{save} the buffer, which
  281. means copying the contents of the buffer into the file.
  282. @deffn Command save-buffer &optional backup-option
  283. This function saves the contents of the current buffer in its visited
  284. file if the buffer has been modified since it was last visited or saved.
  285. Otherwise it does nothing.
  286. @code{save-buffer} is responsible for making backup files. Normally,
  287. @var{backup-option} is @code{nil}, and @code{save-buffer} makes a backup
  288. file only if this is the first save since visiting the file. Other
  289. values for @var{backup-option} request the making of backup files in
  290. other circumstances:
  291. @itemize @bullet
  292. @item
  293. With an argument of 4 or 64, reflecting 1 or 3 @kbd{C-u}'s, the
  294. @code{save-buffer} function marks this version of the file to be
  295. backed up when the buffer is next saved.
  296. @item
  297. With an argument of 16 or 64, reflecting 2 or 3 @kbd{C-u}'s, the
  298. @code{save-buffer} function unconditionally backs up the previous
  299. version of the file before saving it.
  300. @item
  301. With an argument of 0, unconditionally do @emph{not} make any backup file.
  302. @end itemize
  303. @end deffn
  304. @deffn Command save-some-buffers &optional save-silently-p pred
  305. @anchor{Definition of save-some-buffers}
  306. This command saves some modified file-visiting buffers. Normally it
  307. asks the user about each buffer. But if @var{save-silently-p} is
  308. non-@code{nil}, it saves all the file-visiting buffers without querying
  309. the user.
  310. @vindex save-some-buffers-default-predicate
  311. The optional @var{pred} argument provides a predicate that controls
  312. which buffers to ask about (or to save silently if
  313. @var{save-silently-p} is non-@code{nil}). If @var{pred} is
  314. @code{nil}, that means to use the value of
  315. @code{save-some-buffers-default-predicate} instead of @var{pred}. If
  316. the result is @code{nil}, it means ask only about file-visiting
  317. buffers. If it is @code{t}, that means also offer to save certain
  318. other non-file buffers---those that have a non-@code{nil} buffer-local
  319. value of @code{buffer-offer-save} (@pxref{Killing Buffers}). A user
  320. who says @samp{yes} to saving a non-file buffer is asked to specify
  321. the file name to use. The @code{save-buffers-kill-emacs} function
  322. passes the value @code{t} for @var{pred}.
  323. If the predicate is neither @code{t} nor @code{nil}, then it should be
  324. a function of no arguments. It will be called in each buffer to decide
  325. whether to offer to save that buffer. If it returns a non-@code{nil}
  326. value in a certain buffer, that means do offer to save that buffer.
  327. @end deffn
  328. @deffn Command write-file filename &optional confirm
  329. @anchor{Definition of write-file}
  330. This function writes the current buffer into file @var{filename}, makes
  331. the buffer visit that file, and marks it not modified. Then it renames
  332. the buffer based on @var{filename}, appending a string like @samp{<2>}
  333. if necessary to make a unique buffer name. It does most of this work by
  334. calling @code{set-visited-file-name} (@pxref{Buffer File Name}) and
  335. @code{save-buffer}.
  336. If @var{confirm} is non-@code{nil}, that means to ask for confirmation
  337. before overwriting an existing file. Interactively, confirmation is
  338. required, unless the user supplies a prefix argument.
  339. @c FIXME: This disagrees with the doc string, which talks about
  340. @c directory names, not directories. See Bug#27986.
  341. If @var{filename} is an existing directory, or a symbolic link to one,
  342. @code{write-file} uses the name of the visited file, in directory
  343. @var{filename}. If the buffer is not visiting a file, it uses the
  344. buffer name instead.
  345. @end deffn
  346. Saving a buffer runs several hooks. It also performs format
  347. conversion (@pxref{Format Conversion}). Note that these hooks,
  348. described below, are only run by @code{save-buffer}, they are not run
  349. by other primitives and functions that write buffer text to files, and
  350. in particular auto-saving (@pxref{Auto-Saving}) doesn't run these
  351. hooks.
  352. @defvar write-file-functions
  353. The value of this variable is a list of functions to be called before
  354. writing out a buffer to its visited file. If one of them returns
  355. non-@code{nil}, the file is considered already written and the rest of
  356. the functions are not called, nor is the usual code for writing the file
  357. executed.
  358. If a function in @code{write-file-functions} returns non-@code{nil}, it
  359. is responsible for making a backup file (if that is appropriate).
  360. To do so, execute the following code:
  361. @example
  362. (or buffer-backed-up (backup-buffer))
  363. @end example
  364. You might wish to save the file modes value returned by
  365. @code{backup-buffer} and use that (if non-@code{nil}) to set the mode
  366. bits of the file that you write. This is what @code{save-buffer}
  367. normally does. @xref{Making Backups,, Making Backup Files}.
  368. The hook functions in @code{write-file-functions} are also responsible
  369. for encoding the data (if desired): they must choose a suitable coding
  370. system and end-of-line conversion (@pxref{Lisp and Coding Systems}),
  371. perform the encoding (@pxref{Explicit Encoding}), and set
  372. @code{last-coding-system-used} to the coding system that was used
  373. (@pxref{Encoding and I/O}).
  374. If you set this hook locally in a buffer, it is assumed to be
  375. associated with the file or the way the contents of the buffer were
  376. obtained. Thus the variable is marked as a permanent local, so that
  377. changing the major mode does not alter a buffer-local value. On the
  378. other hand, calling @code{set-visited-file-name} will reset it.
  379. If this is not what you want, you might like to use
  380. @code{write-contents-functions} instead.
  381. Even though this is not a normal hook, you can use @code{add-hook} and
  382. @code{remove-hook} to manipulate the list. @xref{Hooks}.
  383. @end defvar
  384. @c Emacs 19 feature
  385. @defvar write-contents-functions
  386. This works just like @code{write-file-functions}, but it is intended
  387. for hooks that pertain to the buffer's contents, not to the particular
  388. visited file or its location. Such hooks are usually set up by major
  389. modes, as buffer-local bindings for this variable. This variable
  390. automatically becomes buffer-local whenever it is set; switching to a
  391. new major mode always resets this variable, but calling
  392. @code{set-visited-file-name} does not.
  393. If any of the functions in this hook returns non-@code{nil}, the file
  394. is considered already written and the rest are not called and neither
  395. are the functions in @code{write-file-functions}.
  396. @end defvar
  397. @defopt before-save-hook
  398. This normal hook runs before a buffer is saved in its visited file,
  399. regardless of whether that is done normally or by one of the hooks
  400. described above. For instance, the @file{copyright.el} program uses
  401. this hook to make sure the file you are saving has the current year in
  402. its copyright notice.
  403. @end defopt
  404. @c Emacs 19 feature
  405. @defopt after-save-hook
  406. This normal hook runs after a buffer has been saved in its visited file.
  407. One use of this hook is in Fast Lock mode; it uses this hook to save the
  408. highlighting information in a cache file.
  409. @end defopt
  410. @defopt file-precious-flag
  411. If this variable is non-@code{nil}, then @code{save-buffer} protects
  412. against I/O errors while saving by writing the new file to a temporary
  413. name instead of the name it is supposed to have, and then renaming it to
  414. the intended name after it is clear there are no errors. This procedure
  415. prevents problems such as a lack of disk space from resulting in an
  416. invalid file.
  417. As a side effect, backups are necessarily made by copying. @xref{Rename
  418. or Copy}. Yet, at the same time, saving a precious file always breaks
  419. all hard links between the file you save and other file names.
  420. Some modes give this variable a non-@code{nil} buffer-local value
  421. in particular buffers.
  422. @end defopt
  423. @defopt require-final-newline
  424. This variable determines whether files may be written out that do
  425. @emph{not} end with a newline. If the value of the variable is
  426. @code{t}, then @code{save-buffer} silently adds a newline at the end
  427. of the buffer whenever it does not already end in one. If the value
  428. is @code{visit}, Emacs adds a missing newline just after it visits the
  429. file. If the value is @code{visit-save}, Emacs adds a missing newline
  430. both on visiting and on saving. For any other non-@code{nil} value,
  431. @code{save-buffer} asks the user whether to add a newline each time
  432. the case arises.
  433. If the value of the variable is @code{nil}, then @code{save-buffer}
  434. doesn't add newlines at all. @code{nil} is the default value, but a few
  435. major modes set it to @code{t} in particular buffers.
  436. @end defopt
  437. See also the function @code{set-visited-file-name} (@pxref{Buffer File
  438. Name}).
  439. @node Reading from Files
  440. @section Reading from Files
  441. @cindex reading from files
  442. To copy the contents of a file into a buffer, use the function
  443. @code{insert-file-contents}. (Don't use the command
  444. @code{insert-file} in a Lisp program, as that sets the mark.)
  445. @defun insert-file-contents filename &optional visit beg end replace
  446. This function inserts the contents of file @var{filename} into the
  447. current buffer after point. It returns a list of the absolute file name
  448. and the length of the data inserted. An error is signaled if
  449. @var{filename} is not the name of a file that can be read.
  450. This function checks the file contents against the defined file
  451. formats, and converts the file contents if appropriate and also calls
  452. the functions in the list @code{after-insert-file-functions}.
  453. @xref{Format Conversion}. Normally, one of the functions in the
  454. @code{after-insert-file-functions} list determines the coding system
  455. (@pxref{Coding Systems}) used for decoding the file's contents,
  456. including end-of-line conversion. However, if the file contains null
  457. bytes, it is by default visited without any code conversions.
  458. @xref{Lisp and Coding Systems, inhibit-null-byte-detection}.
  459. If @var{visit} is non-@code{nil}, this function additionally marks the
  460. buffer as unmodified and sets up various fields in the buffer so that it
  461. is visiting the file @var{filename}: these include the buffer's visited
  462. file name and its last save file modtime. This feature is used by
  463. @code{find-file-noselect} and you probably should not use it yourself.
  464. If @var{beg} and @var{end} are non-@code{nil}, they should be numbers
  465. that are byte offsets specifying the portion of the file to insert.
  466. In this case, @var{visit} must be @code{nil}. For example,
  467. @example
  468. (insert-file-contents filename nil 0 500)
  469. @end example
  470. @noindent
  471. inserts the first 500 characters of a file.
  472. If the argument @var{replace} is non-@code{nil}, it means to replace the
  473. contents of the buffer (actually, just the accessible portion) with the
  474. contents of the file. This is better than simply deleting the buffer
  475. contents and inserting the whole file, because (1) it preserves some
  476. marker positions and (2) it puts less data in the undo list.
  477. It is possible to read a special file (such as a FIFO or an I/O device)
  478. with @code{insert-file-contents}, as long as @var{replace} and
  479. @var{visit} are @code{nil}.
  480. @end defun
  481. @defun insert-file-contents-literally filename &optional visit beg end replace
  482. This function works like @code{insert-file-contents} except that it
  483. does not run @code{find-file-hook}, and does not do format decoding,
  484. character code conversion, automatic uncompression, and so on.
  485. @end defun
  486. If you want to pass a file name to another process so that another
  487. program can read the file, use the function @code{file-local-copy}; see
  488. @ref{Magic File Names}.
  489. @node Writing to Files
  490. @section Writing to Files
  491. @cindex writing to files
  492. You can write the contents of a buffer, or part of a buffer, directly
  493. to a file on disk using the @code{append-to-file} and
  494. @code{write-region} functions. Don't use these functions to write to
  495. files that are being visited; that could cause confusion in the
  496. mechanisms for visiting.
  497. @deffn Command append-to-file start end filename
  498. This function appends the contents of the region delimited by
  499. @var{start} and @var{end} in the current buffer to the end of file
  500. @var{filename}. If that file does not exist, it is created. This
  501. function returns @code{nil}.
  502. An error is signaled if @var{filename} specifies a nonwritable file,
  503. or a nonexistent file in a directory where files cannot be created.
  504. When called from Lisp, this function is completely equivalent to:
  505. @example
  506. (write-region start end filename t)
  507. @end example
  508. @end deffn
  509. @deffn Command write-region start end filename &optional append visit lockname mustbenew
  510. This function writes the region delimited by @var{start} and @var{end}
  511. in the current buffer into the file specified by @var{filename}.
  512. If @var{start} is @code{nil}, then the command writes the entire buffer
  513. contents (@emph{not} just the accessible portion) to the file and
  514. ignores @var{end}.
  515. @c Emacs 19 feature
  516. If @var{start} is a string, then @code{write-region} writes or appends
  517. that string, rather than text from the buffer. @var{end} is ignored in
  518. this case.
  519. If @var{append} is non-@code{nil}, then the specified text is appended
  520. to the existing file contents (if any). If @var{append} is a
  521. number, @code{write-region} seeks to that byte offset from the start
  522. of the file and writes the data from there.
  523. If @var{mustbenew} is non-@code{nil}, then @code{write-region} asks
  524. for confirmation if @var{filename} names an existing file. If
  525. @var{mustbenew} is the symbol @code{excl}, then @code{write-region}
  526. does not ask for confirmation, but instead it signals an error
  527. @code{file-already-exists} if the file already exists. Although
  528. @code{write-region} normally follows a symbolic link and creates the
  529. pointed-to file if the symbolic link is dangling, it does not follow
  530. symbolic links if @var{mustbenew} is @code{excl}.
  531. The test for an existing file, when @var{mustbenew} is @code{excl}, uses
  532. a special system feature. At least for files on a local disk, there is
  533. no chance that some other program could create a file of the same name
  534. before Emacs does, without Emacs's noticing.
  535. If @var{visit} is @code{t}, then Emacs establishes an association
  536. between the buffer and the file: the buffer is then visiting that file.
  537. It also sets the last file modification time for the current buffer to
  538. @var{filename}'s modtime, and marks the buffer as not modified. This
  539. feature is used by @code{save-buffer}, but you probably should not use
  540. it yourself.
  541. @c Emacs 19 feature
  542. If @var{visit} is a string, it specifies the file name to visit. This
  543. way, you can write the data to one file (@var{filename}) while recording
  544. the buffer as visiting another file (@var{visit}). The argument
  545. @var{visit} is used in the echo area message and also for file locking;
  546. @var{visit} is stored in @code{buffer-file-name}. This feature is used
  547. to implement @code{file-precious-flag}; don't use it yourself unless you
  548. really know what you're doing.
  549. The optional argument @var{lockname}, if non-@code{nil}, specifies the
  550. file name to use for purposes of locking and unlocking, overriding
  551. @var{filename} and @var{visit} for that purpose.
  552. The function @code{write-region} converts the data which it writes to
  553. the appropriate file formats specified by @code{buffer-file-format}
  554. and also calls the functions in the list
  555. @code{write-region-annotate-functions}.
  556. @xref{Format Conversion}.
  557. Normally, @code{write-region} displays the message @samp{Wrote
  558. @var{filename}} in the echo area. This message is inhibited if
  559. @var{visit} is neither @code{t} nor @code{nil} nor a string, or if
  560. Emacs is operating in batch mode (@pxref{Batch Mode}). This
  561. feature is useful for programs that use files for internal purposes,
  562. files that the user does not need to know about.
  563. @end deffn
  564. @defvar write-region-inhibit-fsync
  565. If this variable's value is @code{nil}, @code{write-region} uses the
  566. @code{fsync} system call after writing a file. Although this slows
  567. Emacs down, it lessens the risk of data loss after power failure. If
  568. the value is @code{t}, Emacs does not use @code{fsync}. The default
  569. value is @code{nil} when Emacs is interactive, and @code{t} when Emacs
  570. runs in batch mode. @xref{Files and Storage}.
  571. @end defvar
  572. @defmac with-temp-file file body@dots{}
  573. @anchor{Definition of with-temp-file}
  574. The @code{with-temp-file} macro evaluates the @var{body} forms with a
  575. temporary buffer as the current buffer; then, at the end, it writes the
  576. buffer contents into file @var{file}. It kills the temporary buffer
  577. when finished, restoring the buffer that was current before the
  578. @code{with-temp-file} form. Then it returns the value of the last form
  579. in @var{body}.
  580. The current buffer is restored even in case of an abnormal exit via
  581. @code{throw} or error (@pxref{Nonlocal Exits}).
  582. See also @code{with-temp-buffer} in @ref{Definition of
  583. with-temp-buffer,, The Current Buffer}.
  584. @end defmac
  585. @node File Locks
  586. @section File Locks
  587. @cindex file locks
  588. @cindex lock file
  589. When two users edit the same file at the same time, they are likely
  590. to interfere with each other. Emacs tries to prevent this situation
  591. from arising by recording a @dfn{file lock} when a file is being
  592. modified.
  593. Emacs can then detect the first attempt to modify a buffer visiting a
  594. file that is locked by another Emacs job, and ask the user what to do.
  595. The file lock is really a file, a symbolic link with a special name,
  596. stored in the same directory as the file you are editing. (On file
  597. systems that do not support symbolic links, a regular file is used.)
  598. When you access files using NFS, there may be a small probability that
  599. you and another user will both lock the same file simultaneously.
  600. If this happens, it is possible for the two users to make changes
  601. simultaneously, but Emacs will still warn the user who saves second.
  602. Also, the detection of modification of a buffer visiting a file changed
  603. on disk catches some cases of simultaneous editing; see
  604. @ref{Modification Time}.
  605. @defun file-locked-p filename
  606. This function returns @code{nil} if the file @var{filename} is not
  607. locked. It returns @code{t} if it is locked by this Emacs process, and
  608. it returns the name of the user who has locked it if it is locked by
  609. some other job.
  610. @example
  611. @group
  612. (file-locked-p "foo")
  613. @result{} nil
  614. @end group
  615. @end example
  616. @end defun
  617. @defun lock-buffer &optional filename
  618. This function locks the file @var{filename}, if the current buffer is
  619. modified. The argument @var{filename} defaults to the current buffer's
  620. visited file. Nothing is done if the current buffer is not visiting a
  621. file, or is not modified, or if the option @code{create-lockfiles} is
  622. @code{nil}.
  623. @end defun
  624. @defun unlock-buffer
  625. This function unlocks the file being visited in the current buffer,
  626. if the buffer is modified. If the buffer is not modified, then
  627. the file should not be locked, so this function does nothing. It also
  628. does nothing if the current buffer is not visiting a file, or is not locked.
  629. @end defun
  630. @defopt create-lockfiles
  631. If this variable is @code{nil}, Emacs does not lock files.
  632. @end defopt
  633. @defun ask-user-about-lock file other-user
  634. This function is called when the user tries to modify @var{file}, but it
  635. is locked by another user named @var{other-user}. The default
  636. definition of this function asks the user to say what to do. The value
  637. this function returns determines what Emacs does next:
  638. @itemize @bullet
  639. @item
  640. A value of @code{t} says to grab the lock on the file. Then
  641. this user may edit the file and @var{other-user} loses the lock.
  642. @item
  643. A value of @code{nil} says to ignore the lock and let this
  644. user edit the file anyway.
  645. @item
  646. @kindex file-locked
  647. This function may instead signal a @code{file-locked} error, in which
  648. case the change that the user was about to make does not take place.
  649. The error message for this error looks like this:
  650. @example
  651. @error{} File is locked: @var{file} @var{other-user}
  652. @end example
  653. @noindent
  654. where @code{file} is the name of the file and @var{other-user} is the
  655. name of the user who has locked the file.
  656. @end itemize
  657. If you wish, you can replace the @code{ask-user-about-lock} function
  658. with your own version that makes the decision in another way.
  659. @end defun
  660. @node Information about Files
  661. @section Information about Files
  662. @cindex file, information about
  663. This section describes the functions for retrieving various types of
  664. information about files (or directories or symbolic links), such as
  665. whether a file is readable or writable, and its size. These functions
  666. all take arguments which are file names. Except where noted, these
  667. arguments need to specify existing files, or an error is signaled.
  668. @cindex file names, trailing whitespace
  669. @cindex trailing blanks in file names
  670. Be careful with file names that end in spaces. On some filesystems
  671. (notably, MS-Windows), trailing whitespace characters in file names
  672. are silently and automatically ignored.
  673. @menu
  674. * Testing Accessibility:: Is a given file readable? Writable?
  675. * Kinds of Files:: Is it a directory? A symbolic link?
  676. * Truenames:: Eliminating symbolic links from a file name.
  677. * File Attributes:: File sizes, modification times, etc.
  678. * Extended Attributes:: Extended file attributes for access control.
  679. * Locating Files:: How to find a file in standard places.
  680. @end menu
  681. @node Testing Accessibility
  682. @subsection Testing Accessibility
  683. @cindex accessibility of a file
  684. @cindex file accessibility
  685. These functions test for permission to access a file for reading,
  686. writing, or execution. Unless explicitly stated otherwise, they
  687. follow symbolic links. @xref{Kinds of Files}.
  688. On some operating systems, more complex sets of access permissions
  689. can be specified, via mechanisms such as Access Control Lists (ACLs).
  690. @xref{Extended Attributes}, for how to query and set those
  691. permissions.
  692. @defun file-exists-p filename
  693. This function returns @code{t} if a file named @var{filename} appears
  694. to exist. This does not mean you can necessarily read the file, only
  695. that you can find out its attributes. (On Unix and GNU/Linux, this is
  696. true if the file exists and you have execute permission on the
  697. containing directories, regardless of the permissions of the file
  698. itself.)
  699. If the file does not exist, or if access control policies prevent you
  700. from finding its attributes, this function returns @code{nil}.
  701. Directories are files, so @code{file-exists-p} returns @code{t} when
  702. given a directory name. However, because @code{file-exists-p} follows
  703. symbolic links, it returns @code{t} for a symbolic link
  704. name only if the target file exists.
  705. @end defun
  706. @defun file-readable-p filename
  707. This function returns @code{t} if a file named @var{filename} exists
  708. and you can read it. It returns @code{nil} otherwise.
  709. @end defun
  710. @defun file-executable-p filename
  711. This function returns @code{t} if a file named @var{filename} exists and
  712. you can execute it. It returns @code{nil} otherwise. On Unix and
  713. GNU/Linux, if the file is a directory, execute permission means you can
  714. check the existence and attributes of files inside the directory, and
  715. open those files if their modes permit.
  716. @end defun
  717. @defun file-writable-p filename
  718. This function returns @code{t} if the file @var{filename} can be written
  719. or created by you, and @code{nil} otherwise. A file is writable if the
  720. file exists and you can write it. It is creatable if it does not exist,
  721. but the specified directory does exist and you can write in that
  722. directory.
  723. In the example below, @file{foo} is not writable because the parent
  724. directory does not exist, even though the user could create such a
  725. directory.
  726. @example
  727. @group
  728. (file-writable-p "~/no-such-dir/foo")
  729. @result{} nil
  730. @end group
  731. @end example
  732. @end defun
  733. @defun file-accessible-directory-p dirname
  734. This function returns @code{t} if you have permission to open existing
  735. files in the directory whose name as a file is @var{dirname};
  736. otherwise (or if there is no such directory), it returns @code{nil}.
  737. The value of @var{dirname} may be either a directory name (such as
  738. @file{/foo/}) or the file name of a file which is a directory
  739. (such as @file{/foo}, without the final slash).
  740. For example, from the following we deduce that any attempt to read a
  741. file in @file{/foo/} will give an error:
  742. @example
  743. (file-accessible-directory-p "/foo")
  744. @result{} nil
  745. @end example
  746. @end defun
  747. @defun access-file filename string
  748. This function opens file @var{filename} for reading, then closes it and
  749. returns @code{nil}. However, if the open fails, it signals an error
  750. using @var{string} as the error message text.
  751. @end defun
  752. @defun file-ownership-preserved-p filename &optional group
  753. This function returns @code{t} if deleting the file @var{filename} and
  754. then creating it anew would keep the file's owner unchanged. It also
  755. returns @code{t} for nonexistent files.
  756. If the optional argument @var{group} is non-@code{nil}, this function
  757. also checks that the file's group would be unchanged.
  758. This function does not follow symbolic links.
  759. @end defun
  760. @defun file-modes filename
  761. @cindex mode bits
  762. @cindex file permissions
  763. @cindex permissions, file
  764. @cindex file modes
  765. This function returns the @dfn{mode bits} of @var{filename}---an
  766. integer summarizing its read, write, and execution permissions.
  767. This function follows symbolic links. If the file does not exist, the
  768. return value is @code{nil}.
  769. @xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils}
  770. Manual}, for a description of mode bits. For example, if the
  771. low-order bit is 1, the file is executable by all users; if the
  772. second-lowest-order bit is 1, the file is writable by all users; etc.
  773. The highest possible value is 4095 (7777 octal), meaning that everyone
  774. has read, write, and execute permission, the @acronym{SUID} bit is set
  775. for both others and group, and the sticky bit is set.
  776. @xref{Changing Files}, for the @code{set-file-modes} function, which
  777. can be used to set these permissions.
  778. @example
  779. @group
  780. (file-modes "~/junk/diffs")
  781. @result{} 492 ; @r{Decimal integer.}
  782. @end group
  783. @group
  784. (format "%o" 492)
  785. @result{} "754" ; @r{Convert to octal.}
  786. @end group
  787. @group
  788. (set-file-modes "~/junk/diffs" #o666)
  789. @result{} nil
  790. @end group
  791. @group
  792. $ ls -l diffs
  793. -rw-rw-rw- 1 lewis lewis 3063 Oct 30 16:00 diffs
  794. @end group
  795. @end example
  796. @cindex MS-DOS and file modes
  797. @cindex file modes and MS-DOS
  798. @strong{MS-DOS note:} On MS-DOS, there is no such thing as an
  799. executable file mode bit. So @code{file-modes} considers a file
  800. executable if its name ends in one of the standard executable
  801. extensions, such as @file{.com}, @file{.bat}, @file{.exe}, and some
  802. others. Files that begin with the Unix-standard @samp{#!} signature,
  803. such as shell and Perl scripts, are also considered executable.
  804. Directories are also reported as executable, for compatibility with
  805. Unix. These conventions are also followed by @code{file-attributes}
  806. (@pxref{File Attributes}).
  807. @end defun
  808. @node Kinds of Files
  809. @subsection Distinguishing Kinds of Files
  810. @cindex file classification
  811. @cindex classification of file types
  812. @cindex symbolic links
  813. This section describes how to distinguish various kinds of files, such
  814. as directories, symbolic links, and ordinary files.
  815. Symbolic links are ordinarily followed wherever they appear. For
  816. example, to interpret the file name @file{a/b/c}, any of @file{a},
  817. @file{a/b}, and @file{a/b/c} can be symbolic links that are followed,
  818. possibly recursively if the link targets are themselves symbolic
  819. links. However, a few functions do not follow symbolic links at the
  820. end of a file name (@file{a/b/c} in this example). Such a function
  821. is said to @dfn{not follow symbolic links}.
  822. @defun file-symlink-p filename
  823. @cindex symbolic links
  824. If the file @var{filename} is a symbolic link, this function does not
  825. follow it and instead returns its link target
  826. as a string. (The link target string is not necessarily the full
  827. absolute file name of the target; determining the full file name that
  828. the link points to is nontrivial, see below.)
  829. If the file @var{filename} is not a symbolic link, or does not exist,
  830. @code{file-symlink-p} returns @code{nil}.
  831. Here are a few examples of using this function:
  832. @example
  833. @group
  834. (file-symlink-p "not-a-symlink")
  835. @result{} nil
  836. @end group
  837. @group
  838. (file-symlink-p "sym-link")
  839. @result{} "not-a-symlink"
  840. @end group
  841. @group
  842. (file-symlink-p "sym-link2")
  843. @result{} "sym-link"
  844. @end group
  845. @group
  846. (file-symlink-p "/bin")
  847. @result{} "/pub/bin"
  848. @end group
  849. @end example
  850. Note that in the third example, the function returned @file{sym-link},
  851. but did not proceed to resolve it, although that file is itself a
  852. symbolic link. That is because this function does not follow symbolic
  853. links---the process of following the symbolic links does not apply to
  854. the last component of the file name.
  855. The string that this function returns is what is recorded in the
  856. symbolic link; it may or may not include any leading directories.
  857. This function does @emph{not} expand the link target to produce a
  858. fully-qualified file name, and in particular does not use the leading
  859. directories, if any, of the @var{filename} argument if the link target
  860. is not an absolute file name. Here's an example:
  861. @example
  862. @group
  863. (file-symlink-p "/foo/bar/baz")
  864. @result{} "some-file"
  865. @end group
  866. @end example
  867. @noindent
  868. Here, although @file{/foo/bar/baz} was given as a fully-qualified file
  869. name, the result is not, and in fact does not have any leading
  870. directories at all. And since @file{some-file} might itself be a
  871. symbolic link, you cannot simply prepend leading directories to it,
  872. nor even naively use @code{expand-file-name} (@pxref{File Name
  873. Expansion}) to produce its absolute file name.
  874. For this reason, this function is seldom useful if you need to
  875. determine more than just the fact that a file is or isn't a symbolic
  876. link. If you actually need the file name of the link target, use
  877. @code{file-chase-links} or @code{file-truename}, described in
  878. @ref{Truenames}.
  879. @end defun
  880. @defun file-directory-p filename
  881. This function returns @code{t} if @var{filename} is the name of an
  882. existing directory, @code{nil} otherwise.
  883. This function follows symbolic links.
  884. @example
  885. @group
  886. (file-directory-p "~rms")
  887. @result{} t
  888. @end group
  889. @group
  890. (file-directory-p "~rms/lewis/files.texi")
  891. @result{} nil
  892. @end group
  893. @group
  894. (file-directory-p "~rms/lewis/no-such-file")
  895. @result{} nil
  896. @end group
  897. @group
  898. (file-directory-p "$HOME")
  899. @result{} nil
  900. @end group
  901. @group
  902. (file-directory-p
  903. (substitute-in-file-name "$HOME"))
  904. @result{} t
  905. @end group
  906. @end example
  907. @end defun
  908. @defun file-regular-p filename
  909. This function returns @code{t} if the file @var{filename} exists and is
  910. a regular file (not a directory, named pipe, terminal, or
  911. other I/O device).
  912. This function follows symbolic links.
  913. @end defun
  914. @node Truenames
  915. @subsection Truenames
  916. @cindex truename (of file)
  917. The @dfn{truename} of a file is the name that you get by following
  918. symbolic links at all levels until none remain, then simplifying away
  919. @samp{.}@: and @samp{..}@: appearing as name components. This results
  920. in a sort of canonical name for the file. A file does not always have a
  921. unique truename; the number of distinct truenames a file has is equal to
  922. the number of hard links to the file. However, truenames are useful
  923. because they eliminate symbolic links as a cause of name variation.
  924. @defun file-truename filename
  925. This function returns the truename of the file @var{filename}. If the
  926. argument is not an absolute file name, this function first expands it
  927. against @code{default-directory}.
  928. This function does not expand environment variables. Only
  929. @code{substitute-in-file-name} does that. @xref{Definition of
  930. substitute-in-file-name}.
  931. If you may need to follow symbolic links preceding @samp{..}@:
  932. appearing as a name component, call @code{file-truename} without prior
  933. direct or indirect calls to @code{expand-file-name}. Otherwise, the
  934. file name component immediately preceding @samp{..} will be
  935. simplified away before @code{file-truename} is called. To
  936. eliminate the need for a call to @code{expand-file-name},
  937. @code{file-truename} handles @samp{~} in the same way that
  938. @code{expand-file-name} does. @xref{File Name Expansion,, Functions
  939. that Expand Filenames}.
  940. @end defun
  941. @defun file-chase-links filename &optional limit
  942. This function follows symbolic links, starting with @var{filename},
  943. until it finds a file name which is not the name of a symbolic link.
  944. Then it returns that file name. This function does @emph{not} follow
  945. symbolic links at the level of parent directories.
  946. If you specify a number for @var{limit}, then after chasing through
  947. that many links, the function just returns what it has even if that is
  948. still a symbolic link.
  949. @end defun
  950. To illustrate the difference between @code{file-chase-links} and
  951. @code{file-truename}, suppose that @file{/usr/foo} is a symbolic link to
  952. the directory @file{/home/foo}, and @file{/home/foo/hello} is an
  953. ordinary file (or at least, not a symbolic link) or nonexistent. Then
  954. we would have:
  955. @example
  956. (file-chase-links "/usr/foo/hello")
  957. ;; @r{This does not follow the links in the parent directories.}
  958. @result{} "/usr/foo/hello"
  959. (file-truename "/usr/foo/hello")
  960. ;; @r{Assuming that @file{/home} is not a symbolic link.}
  961. @result{} "/home/foo/hello"
  962. @end example
  963. @defun file-equal-p file1 file2
  964. This function returns @code{t} if the files @var{file1} and
  965. @var{file2} name the same file. This is similar to comparing their
  966. truenames, except that remote file names are also handled in an
  967. appropriate manner. If @var{file1} or @var{file2} does not exist, the
  968. return value is unspecified.
  969. @end defun
  970. @defun file-name-case-insensitive-p filename
  971. Sometimes file names or their parts need to be compared as strings, in
  972. which case it's important to know whether the underlying filesystem is
  973. case-insensitive. This function returns @code{t} if file
  974. @var{filename} is on a case-insensitive filesystem. It always returns
  975. @code{t} on MS-DOS and MS-Windows. On Cygwin and Mac OS X,
  976. filesystems may or may not be case-insensitive, and the function tries
  977. to determine case-sensitivity by a runtime test. If the test is
  978. inconclusive, the function returns @code{t} on Cygwin and @code{nil}
  979. on Mac OS X.
  980. Currently this function always returns @code{nil} on platforms other
  981. than MS-DOS, MS-Windows, Cygwin, and Mac OS X. It does not detect
  982. case-insensitivity of mounted filesystems, such as Samba shares or
  983. NFS-mounted Windows volumes. On remote hosts, it assumes @code{t} for
  984. the @samp{smb} method. For all other connection methods, runtime
  985. tests are performed.
  986. @end defun
  987. @defun file-in-directory-p file dir
  988. This function returns @code{t} if @var{file} is a file in directory
  989. @var{dir}, or in a subdirectory of @var{dir}. It also returns
  990. @code{t} if @var{file} and @var{dir} are the same directory. It
  991. compares the truenames of the two directories. If @var{dir} does not
  992. name an existing directory, the return value is @code{nil}.
  993. @end defun
  994. @defun vc-responsible-backend file
  995. This function determines the responsible VC backend of the given
  996. @var{file}. For example, if @file{emacs.c} is a file tracked by Git,
  997. @w{@code{(vc-responsible-backend "emacs.c")}} returns @samp{Git}.
  998. Note that if @var{file} is a symbolic link,
  999. @code{vc-responsible-backend} will not resolve it---the backend of the
  1000. symbolic link file itself is reported. To get the backend VC of the
  1001. file to which @var{file} refers, wrap @var{file} with a symbolic link
  1002. resolving function such as @code{file-chase-links}:
  1003. @smallexample
  1004. (vc-responsible-backend (file-chase-links "emacs.c"))
  1005. @end smallexample
  1006. @end defun
  1007. @node File Attributes
  1008. @subsection File Attributes
  1009. @cindex file attributes
  1010. This section describes the functions for getting detailed
  1011. information about a file, including the owner and group numbers, the
  1012. number of names, the inode number, the size, and the times of access
  1013. and modification.
  1014. @defun file-newer-than-file-p filename1 filename2
  1015. @cindex file age
  1016. @cindex file modification time
  1017. This function returns @code{t} if the file @var{filename1} is
  1018. newer than file @var{filename2}. If @var{filename1} does not
  1019. exist, it returns @code{nil}. If @var{filename1} does exist, but
  1020. @var{filename2} does not, it returns @code{t}.
  1021. In the following example, assume that the file @file{aug-19} was written
  1022. on the 19th, @file{aug-20} was written on the 20th, and the file
  1023. @file{no-file} doesn't exist at all.
  1024. @example
  1025. @group
  1026. (file-newer-than-file-p "aug-19" "aug-20")
  1027. @result{} nil
  1028. @end group
  1029. @group
  1030. (file-newer-than-file-p "aug-20" "aug-19")
  1031. @result{} t
  1032. @end group
  1033. @group
  1034. (file-newer-than-file-p "aug-19" "no-file")
  1035. @result{} t
  1036. @end group
  1037. @group
  1038. (file-newer-than-file-p "no-file" "aug-19")
  1039. @result{} nil
  1040. @end group
  1041. @end example
  1042. @end defun
  1043. @defun file-attributes filename &optional id-format
  1044. @anchor{Definition of file-attributes}
  1045. This function returns a list of attributes of file @var{filename}. If
  1046. the specified file's attributes cannot be accessed, it returns @code{nil}.
  1047. This function does not follow symbolic links.
  1048. The optional parameter @var{id-format} specifies the preferred format
  1049. of attributes @acronym{UID} and @acronym{GID} (see below)---the
  1050. valid values are @code{'string} and @code{'integer}. The latter is
  1051. the default, but we plan to change that, so you should specify a
  1052. non-@code{nil} value for @var{id-format} if you use the returned
  1053. @acronym{UID} or @acronym{GID}.
  1054. On GNU platforms when operating on a local file, this function is
  1055. atomic: if the filesystem is simultaneously being changed by some
  1056. other process, this function returns the file's attributes either
  1057. before or after the change. Otherwise this function is not atomic,
  1058. and might return @code{nil} if it detects the race condition, or might
  1059. return a hodgepodge of the previous and current file attributes.
  1060. Accessor functions are provided to access the elements in this list.
  1061. The accessors are mentioned along with the descriptions of the
  1062. elements below.
  1063. The elements of the list, in order, are:
  1064. @enumerate 0
  1065. @item
  1066. @code{t} for a directory, a string for a symbolic link (the name
  1067. linked to), or @code{nil} for a text file
  1068. (@code{file-attribute-type}).
  1069. @c Wordy so as to prevent an overfull hbox. --rjc 15mar92
  1070. @item
  1071. The number of names the file has (@code{file-attribute-link-number}).
  1072. Alternate names, also known as hard links, can be created by using the
  1073. @code{add-name-to-file} function (@pxref{Changing Files}).
  1074. @item
  1075. The file's @acronym{UID}, normally as a string
  1076. (@code{file-attribute-user-id}). However, if it does not correspond
  1077. to a named user, the value is a number.
  1078. @item
  1079. The file's @acronym{GID}, likewise (@code{file-attribute-group-id}).
  1080. @item
  1081. The time of last access, as a list of four integers
  1082. @code{(@var{sec-high} @var{sec-low} @var{microsec} @var{picosec})}
  1083. (@code{file-attribute-access-time}). (This is similar to the value of
  1084. @code{current-time}; see @ref{Time of Day}.) Note that on some
  1085. FAT-based filesystems, only the date of last access is recorded, so
  1086. this time will always hold the midnight of the day of last access.
  1087. @cindex modification time of file
  1088. @item
  1089. The time of last modification as a list of four integers (as above)
  1090. (@code{file-attribute-modification-time}). This is the last time when
  1091. the file's contents were modified.
  1092. @item
  1093. The time of last status change as a list of four integers (as above)
  1094. (@code{file-attribute-status-change-time}). This is the time of the
  1095. last change to the file's access mode bits, its owner and group, and
  1096. other information recorded in the filesystem for the file, beyond the
  1097. file's contents.
  1098. @item
  1099. The size of the file in bytes (@code{file-attribute-size}). This is
  1100. floating point if the size is too large to fit in a Lisp integer.
  1101. @item
  1102. The file's modes, as a string of ten letters or dashes, as in
  1103. @samp{ls -l} (@code{file-attribute-modes}).
  1104. @item
  1105. An unspecified value, present for backward compatibility.
  1106. @item
  1107. The file's inode number (@code{file-attribute-inode-number}). If
  1108. possible, this is an integer. If the inode number is too large to be
  1109. represented as an integer in Emacs Lisp but dividing it by
  1110. @math{2^{16}} yields a representable integer, then the value has the
  1111. form @code{(@var{high} . @var{low})}, where @var{low} holds the low 16
  1112. bits. If the inode number is too wide for even that, the value is of
  1113. the form @code{(@var{high} @var{middle} . @var{low})}, where
  1114. @code{high} holds the high bits, @var{middle} the middle 24 bits, and
  1115. @var{low} the low 16 bits.
  1116. @item
  1117. The filesystem number of the device that the file is on
  1118. @code{file-attribute-device-number}). Depending on the magnitude of
  1119. the value, this can be either an integer or a cons cell, in the same
  1120. manner as the inode number. This element and the file's inode number
  1121. together give enough information to distinguish any two files on the
  1122. system---no two files can have the same values for both of these
  1123. numbers.
  1124. @end enumerate
  1125. For example, here are the file attributes for @file{files.texi}:
  1126. @example
  1127. @group
  1128. (file-attributes "files.texi" 'string)
  1129. @result{} (nil 1 "lh" "users"
  1130. (20614 64019 50040 152000)
  1131. (20000 23 0 0)
  1132. (20614 64555 902289 872000)
  1133. 122295 "-rw-rw-rw-"
  1134. t (5888 2 . 43978)
  1135. (15479 . 46724))
  1136. @end group
  1137. @end example
  1138. @noindent
  1139. and here is how the result is interpreted:
  1140. @table @code
  1141. @item nil
  1142. is neither a directory nor a symbolic link.
  1143. @item 1
  1144. has only one name (the name @file{files.texi} in the current default
  1145. directory).
  1146. @item "lh"
  1147. is owned by the user with name @samp{lh}.
  1148. @item "users"
  1149. is in the group with name @samp{users}.
  1150. @item (20614 64019 50040 152000)
  1151. was last accessed on October 23, 2012, at 20:12:03.050040152 UTC.
  1152. @item (20000 23 0 0)
  1153. was last modified on July 15, 2001, at 08:53:43 UTC.
  1154. @item (20614 64555 902289 872000)
  1155. last had its status changed on October 23, 2012, at 20:20:59.902289872 UTC.
  1156. @item 122295
  1157. is 122295 bytes long. (It may not contain 122295 characters, though,
  1158. if some of the bytes belong to multibyte sequences, and also if the
  1159. end-of-line format is CR-LF.)
  1160. @item "-rw-rw-rw-"
  1161. has a mode of read and write access for the owner, group, and world.
  1162. @item t
  1163. is merely a placeholder; it carries no information.
  1164. @item (5888 2 . 43978)
  1165. has an inode number of 6473924464520138.
  1166. @item (15479 . 46724)
  1167. is on the file-system device whose number is 1014478468.
  1168. @end table
  1169. @end defun
  1170. @defun file-nlinks filename
  1171. This function returns the number of names (i.e., hard links) that
  1172. file @var{filename} has. If the file does not exist, this function
  1173. returns @code{nil}. Note that symbolic links have no effect on this
  1174. function, because they are not considered to be names of the files
  1175. they link to. This function does not follow symbolic links.
  1176. @example
  1177. @group
  1178. $ ls -l foo*
  1179. -rw-rw-rw- 2 rms rms 4 Aug 19 01:27 foo
  1180. -rw-rw-rw- 2 rms rms 4 Aug 19 01:27 foo1
  1181. @end group
  1182. @group
  1183. (file-nlinks "foo")
  1184. @result{} 2
  1185. @end group
  1186. @group
  1187. (file-nlinks "doesnt-exist")
  1188. @result{} nil
  1189. @end group
  1190. @end example
  1191. @end defun
  1192. @node Extended Attributes
  1193. @subsection Extended File Attributes
  1194. @cindex extended file attributes
  1195. On some operating systems, each file can be associated with arbitrary
  1196. @dfn{extended file attributes}. At present, Emacs supports querying
  1197. and setting two specific sets of extended file attributes: Access
  1198. Control Lists (ACLs) and SELinux contexts. These extended file
  1199. attributes are used, on some systems, to impose more sophisticated
  1200. file access controls than the basic Unix-style permissions
  1201. discussed in the previous sections.
  1202. @cindex access control list
  1203. @cindex ACL entries
  1204. @cindex SELinux context
  1205. A detailed explanation of ACLs and SELinux is beyond the scope of
  1206. this manual. For our purposes, each file can be associated with an
  1207. @dfn{ACL}, which specifies its properties under an ACL-based file
  1208. control system, and/or an @dfn{SELinux context}, which specifies its
  1209. properties under the SELinux system.
  1210. @defun file-acl filename
  1211. This function returns the ACL for the file @var{filename}. The exact
  1212. Lisp representation of the ACL is unspecified (and may change in
  1213. future Emacs versions), but it is the same as what @code{set-file-acl}
  1214. takes for its @var{acl} argument (@pxref{Changing Files}).
  1215. The underlying ACL implementation is platform-specific; on GNU/Linux
  1216. and BSD, Emacs uses the POSIX ACL interface, while on MS-Windows Emacs
  1217. emulates the POSIX ACL interface with native file security APIs.
  1218. If Emacs was not compiled with ACL support, or the file does not exist
  1219. or is inaccessible, or Emacs was unable to determine the ACL entries
  1220. for any other reason, then the return value is @code{nil}.
  1221. @end defun
  1222. @defun file-selinux-context filename
  1223. This function returns the SELinux context of the file @var{filename},
  1224. as a list of the form @code{(@var{user} @var{role} @var{type}
  1225. @var{range})}. The list elements are the context's user, role, type,
  1226. and range respectively, as Lisp strings; see the SELinux documentation
  1227. for details about what these actually mean. The return value has the
  1228. same form as what @code{set-file-selinux-context} takes for its
  1229. @var{context} argument (@pxref{Changing Files}).
  1230. If Emacs was not compiled with SELinux support, or the file does not
  1231. exist or is inaccessible, or if the system does not support SELinux,
  1232. then the return value is @code{(nil nil nil nil)}.
  1233. @end defun
  1234. @defun file-extended-attributes filename
  1235. This function returns an alist of the Emacs-recognized extended
  1236. attributes of file @var{filename}. Currently, it serves as a
  1237. convenient way to retrieve both the ACL and SELinux context; you can
  1238. then call the function @code{set-file-extended-attributes}, with the
  1239. returned alist as its second argument, to apply the same file access
  1240. attributes to another file (@pxref{Changing Files}).
  1241. One of the elements is @code{(acl . @var{acl})}, where @var{acl} has
  1242. the same form returned by @code{file-acl}.
  1243. Another element is @code{(selinux-context . @var{context})}, where
  1244. @var{context} is the SELinux context, in the same form returned by
  1245. @code{file-selinux-context}.
  1246. @end defun
  1247. @node Locating Files
  1248. @subsection Locating Files in Standard Places
  1249. @cindex locate file in path
  1250. @cindex find file in path
  1251. This section explains how to search for a file in a list of
  1252. directories (a @dfn{path}), or for an executable file in the standard
  1253. list of executable file directories.
  1254. To search for a user-specific configuration file, @xref{Standard
  1255. File Names}, for the @code{locate-user-emacs-file} function.
  1256. @defun locate-file filename path &optional suffixes predicate
  1257. This function searches for a file whose name is @var{filename} in a
  1258. list of directories given by @var{path}, trying the suffixes in
  1259. @var{suffixes}. If it finds such a file, it returns the file's
  1260. absolute file name (@pxref{Relative File Names}); otherwise it returns
  1261. @code{nil}.
  1262. The optional argument @var{suffixes} gives the list of file-name
  1263. suffixes to append to @var{filename} when searching.
  1264. @code{locate-file} tries each possible directory with each of these
  1265. suffixes. If @var{suffixes} is @code{nil}, or @code{("")}, then there
  1266. are no suffixes, and @var{filename} is used only as-is. Typical
  1267. values of @var{suffixes} are @code{exec-suffixes} (@pxref{Subprocess
  1268. Creation}), @code{load-suffixes}, @code{load-file-rep-suffixes} and
  1269. the return value of the function @code{get-load-suffixes} (@pxref{Load
  1270. Suffixes}).
  1271. Typical values for @var{path} are @code{exec-path} (@pxref{Subprocess
  1272. Creation}) when looking for executable programs, or @code{load-path}
  1273. (@pxref{Library Search}) when looking for Lisp files. If
  1274. @var{filename} is absolute, @var{path} has no effect, but the suffixes
  1275. in @var{suffixes} are still tried.
  1276. The optional argument @var{predicate}, if non-@code{nil}, specifies a
  1277. predicate function for testing whether a candidate file is suitable.
  1278. The predicate is passed the candidate file name as its single
  1279. argument. If @var{predicate} is @code{nil} or omitted,
  1280. @code{locate-file} uses @code{file-readable-p} as the predicate.
  1281. @xref{Kinds of Files}, for other useful predicates, e.g.,
  1282. @code{file-executable-p} and @code{file-directory-p}.
  1283. For compatibility, @var{predicate} can also be one of the symbols
  1284. @code{executable}, @code{readable}, @code{writable}, @code{exists}, or
  1285. a list of one or more of these symbols.
  1286. @end defun
  1287. @defun executable-find program
  1288. This function searches for the executable file of the named
  1289. @var{program} and returns the absolute file name of the executable,
  1290. including its file-name extensions, if any. It returns @code{nil} if
  1291. the file is not found. The functions searches in all the directories
  1292. in @code{exec-path}, and tries all the file-name extensions in
  1293. @code{exec-suffixes} (@pxref{Subprocess Creation}).
  1294. @end defun
  1295. @node Changing Files
  1296. @section Changing File Names and Attributes
  1297. @c @cindex renaming files Duplicates rename-file
  1298. @cindex copying files
  1299. @cindex deleting files
  1300. @cindex linking files
  1301. @cindex setting modes of files
  1302. The functions in this section rename, copy, delete, link, and set
  1303. the modes (permissions) of files. Typically, they signal a
  1304. @code{file-error} error if they fail to perform their function,
  1305. reporting the system-dependent error message that describes the reason
  1306. for the failure. If they fail because a file is missing, they signal
  1307. a @code{file-missing} error instead.
  1308. For performance, the operating system may cache or alias changes
  1309. made by these functions instead of writing them immediately to
  1310. secondary storage. @xref{Files and Storage}.
  1311. @c FIXME: This paragraph is purposely silent on what happens if
  1312. @c @var{newname} is not a directory name but happens to name a
  1313. @c directory. See Bug#27986 for discussion on how to clear this up.
  1314. In the functions that have an argument @var{newname}, if this
  1315. argument is a directory name it is treated as if the nondirectory part
  1316. of the source name were appended. Typically, a directory name is one
  1317. that ends in @samp{/} (@pxref{Directory Names}). For example, if the
  1318. old name is @file{a/b/c}, the @var{newname} @file{d/e/f/} is treated
  1319. as if it were @file{d/e/f/c}.
  1320. In the functions that have an argument @var{newname}, if a file by the
  1321. name of @var{newname} already exists, the actions taken depend on the
  1322. value of the argument @var{ok-if-already-exists}:
  1323. @itemize @bullet
  1324. @item
  1325. Signal a @code{file-already-exists} error if
  1326. @var{ok-if-already-exists} is @code{nil}.
  1327. @item
  1328. Request confirmation if @var{ok-if-already-exists} is a number.
  1329. @item
  1330. Replace the old file without confirmation if @var{ok-if-already-exists}
  1331. is any other value.
  1332. @end itemize
  1333. @deffn Command add-name-to-file oldname newname &optional ok-if-already-exists
  1334. @cindex file with multiple names
  1335. @cindex file hard link
  1336. This function gives the file named @var{oldname} the additional name
  1337. @var{newname}. This means that @var{newname} becomes a new hard
  1338. link to @var{oldname}.
  1339. If @var{newname} is a symbolic link, its directory entry is replaced,
  1340. not the directory entry it points to. If @var{oldname} is a symbolic
  1341. link, this function might or might not follow the link; it does not
  1342. follow the link on GNU platforms. If @var{oldname} is a directory,
  1343. this function typically fails, although for the superuser on a few
  1344. old-fashioned non-GNU platforms it can succeed and create a filesystem
  1345. that is not tree-structured.
  1346. In the first part of the following example, we list two files,
  1347. @file{foo} and @file{foo3}.
  1348. @example
  1349. @group
  1350. $ ls -li fo*
  1351. 81908 -rw-rw-rw- 1 rms rms 29 Aug 18 20:32 foo
  1352. 84302 -rw-rw-rw- 1 rms rms 24 Aug 18 20:31 foo3
  1353. @end group
  1354. @end example
  1355. Now we create a hard link, by calling @code{add-name-to-file}, then list
  1356. the files again. This shows two names for one file, @file{foo} and
  1357. @file{foo2}.
  1358. @example
  1359. @group
  1360. (add-name-to-file "foo" "foo2")
  1361. @result{} nil
  1362. @end group
  1363. @group
  1364. $ ls -li fo*
  1365. 81908 -rw-rw-rw- 2 rms rms 29 Aug 18 20:32 foo
  1366. 81908 -rw-rw-rw- 2 rms rms 29 Aug 18 20:32 foo2
  1367. 84302 -rw-rw-rw- 1 rms rms 24 Aug 18 20:31 foo3
  1368. @end group
  1369. @end example
  1370. Finally, we evaluate the following:
  1371. @example
  1372. (add-name-to-file "foo" "foo3" t)
  1373. @end example
  1374. @noindent
  1375. and list the files again. Now there are three names
  1376. for one file: @file{foo}, @file{foo2}, and @file{foo3}. The old
  1377. contents of @file{foo3} are lost.
  1378. @example
  1379. @group
  1380. (add-name-to-file "foo1" "foo3")
  1381. @result{} nil
  1382. @end group
  1383. @group
  1384. $ ls -li fo*
  1385. 81908 -rw-rw-rw- 3 rms rms 29 Aug 18 20:32 foo
  1386. 81908 -rw-rw-rw- 3 rms rms 29 Aug 18 20:32 foo2
  1387. 81908 -rw-rw-rw- 3 rms rms 29 Aug 18 20:32 foo3
  1388. @end group
  1389. @end example
  1390. This function is meaningless on operating systems where multiple names
  1391. for one file are not allowed. Some systems implement multiple names
  1392. by copying the file instead.
  1393. See also @code{file-nlinks} in @ref{File Attributes}.
  1394. @end deffn
  1395. @deffn Command rename-file filename newname &optional ok-if-already-exists
  1396. This command renames the file @var{filename} as @var{newname}.
  1397. If @var{filename} has additional names aside from @var{filename}, it
  1398. continues to have those names. In fact, adding the name @var{newname}
  1399. with @code{add-name-to-file} and then deleting @var{filename} has the
  1400. same effect as renaming, aside from momentary intermediate states and
  1401. treatment of errors, directories and symbolic links.
  1402. This command does not follow symbolic links. If @var{filename} is a
  1403. symbolic link, this command renames the symbolic link, not the file it
  1404. points to. If @var{newname} is a symbolic link, its directory entry
  1405. is replaced, not the directory entry it points to.
  1406. This command does nothing if @var{filename} and @var{newname} are the
  1407. same directory entry, i.e., if they refer to the same parent directory
  1408. and give the same name within that directory. Otherwise, if
  1409. @var{filename} and @var{newname} name the same file, this command does
  1410. nothing on POSIX-conforming systems, and removes @var{filename} on
  1411. some non-POSIX systems.
  1412. If @var{newname} exists, then it must be an empty directory if
  1413. @var{oldname} is a directory and a non-directory otherwise.
  1414. @end deffn
  1415. @deffn Command copy-file oldname newname &optional ok-if-already-exists time preserve-uid-gid preserve-extended-attributes
  1416. This command copies the file @var{oldname} to @var{newname}. An
  1417. error is signaled if @var{oldname} is not a regular file. If @var{newname}
  1418. names a directory, it copies @var{oldname} into that directory,
  1419. preserving its final name component.
  1420. @c FIXME: See Bug#27986 for how the previous sentence might change.
  1421. This function follows symbolic links, except that it does not follow a
  1422. dangling symbolic link to create @var{newname}.
  1423. If @var{time} is non-@code{nil}, then this function gives the new file
  1424. the same last-modified time that the old one has. (This works on only
  1425. some operating systems.) If setting the time gets an error,
  1426. @code{copy-file} signals a @code{file-date-error} error. In an
  1427. interactive call, a prefix argument specifies a non-@code{nil} value
  1428. for @var{time}.
  1429. If argument @var{preserve-uid-gid} is @code{nil}, we let the operating
  1430. system decide the user and group ownership of the new file (this is
  1431. usually set to the user running Emacs). If @var{preserve-uid-gid} is
  1432. non-@code{nil}, we attempt to copy the user and group ownership of the
  1433. file. This works only on some operating systems, and only if you have
  1434. the correct permissions to do so.
  1435. If the optional argument @var{preserve-permissions} is non-@code{nil},
  1436. this function copies the file modes (or ``permissions'') of
  1437. @var{oldname} to @var{newname}, as well as the Access Control List and
  1438. SELinux context (if any). @xref{Information about Files}.
  1439. Otherwise, the file modes of @var{newname} are left unchanged if it is
  1440. an existing file, and set to those of @var{oldname}, masked by the
  1441. default file permissions (see @code{set-default-file-modes} below), if
  1442. @var{newname} is to be newly created. The Access Control List or
  1443. SELinux context are not copied over in either case.
  1444. @end deffn
  1445. @deffn Command make-symbolic-link target newname &optional ok-if-already-exists
  1446. @pindex ln
  1447. @kindex file-already-exists
  1448. This command makes a symbolic link to @var{target}, named
  1449. @var{newname}. This is like the shell command @samp{ln -s
  1450. @var{target} @var{newname}}. The @var{target} argument
  1451. is treated only as a string; it need not name an existing file.
  1452. If @var{ok-if-already-exists} is an integer, indicating interactive
  1453. use, then leading @samp{~} is expanded and leading @samp{/:} is
  1454. stripped in the @var{target} string.
  1455. If @var{target} is a relative file name, the resulting symbolic link
  1456. is interpreted relative to the directory containing the symbolic link.
  1457. @xref{Relative File Names}.
  1458. This function is not available on systems that don't support symbolic
  1459. links.
  1460. @end deffn
  1461. @cindex trash
  1462. @vindex delete-by-moving-to-trash
  1463. @deffn Command delete-file filename &optional trash
  1464. @pindex rm
  1465. This command deletes the file @var{filename}. If the file has
  1466. multiple names, it continues to exist under the other names. If
  1467. @var{filename} is a symbolic link, @code{delete-file} deletes only the
  1468. symbolic link and not its target.
  1469. A suitable kind of @code{file-error} error is signaled if the file
  1470. does not exist, or is not deletable. (On Unix and GNU/Linux, a file
  1471. is deletable if its directory is writable.)
  1472. If the optional argument @var{trash} is non-@code{nil} and the
  1473. variable @code{delete-by-moving-to-trash} is non-@code{nil}, this
  1474. command moves the file into the system Trash instead of deleting it.
  1475. @xref{Misc File Ops,,Miscellaneous File Operations, emacs, The GNU
  1476. Emacs Manual}. When called interactively, @var{trash} is @code{t} if
  1477. no prefix argument is given, and @code{nil} otherwise.
  1478. See also @code{delete-directory} in @ref{Create/Delete Dirs}.
  1479. @end deffn
  1480. @cindex file permissions, setting
  1481. @cindex permissions, file
  1482. @cindex file modes, setting
  1483. @deffn Command set-file-modes filename mode
  1484. This function sets the @dfn{file mode} (or @dfn{permissions}) of
  1485. @var{filename} to @var{mode}. This function follows symbolic links.
  1486. If called non-interactively, @var{mode} must be an integer. Only the
  1487. lowest 12 bits of the integer are used; on most systems, only the
  1488. lowest 9 bits are meaningful. You can use the Lisp construct for
  1489. octal numbers to enter @var{mode}. For example,
  1490. @example
  1491. (set-file-modes #o644)
  1492. @end example
  1493. @noindent
  1494. specifies that the file should be readable and writable for its owner,
  1495. readable for group members, and readable for all other users.
  1496. @xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils}
  1497. Manual}, for a description of mode bit specifications.
  1498. Interactively, @var{mode} is read from the minibuffer using
  1499. @code{read-file-modes} (see below), which lets the user type in either
  1500. an integer or a string representing the permissions symbolically.
  1501. @xref{File Attributes}, for the function @code{file-modes}, which
  1502. returns the permissions of a file.
  1503. @end deffn
  1504. @defun set-default-file-modes mode
  1505. @cindex umask
  1506. This function sets the default permissions for new files created by
  1507. Emacs and its subprocesses. Every file created with Emacs initially
  1508. has these permissions, or a subset of them (@code{write-region} will
  1509. not grant execute permissions even if the default file permissions
  1510. allow execution). On Unix and GNU/Linux, the default permissions are
  1511. given by the bitwise complement of the @samp{umask} value.
  1512. The argument @var{mode} should be an integer which specifies the
  1513. permissions, similar to @code{set-file-modes} above. Only the lowest
  1514. 9 bits are meaningful.
  1515. The default file permissions have no effect when you save a modified
  1516. version of an existing file; saving a file preserves its existing
  1517. permissions.
  1518. @end defun
  1519. @defmac with-file-modes mode body@dots{}
  1520. This macro evaluates the @var{body} forms with the default
  1521. permissions for new files temporarily set to @var{modes} (whose value
  1522. is as for @code{set-file-modes} above). When finished, it restores
  1523. the original default file permissions, and returns the value of the
  1524. last form in @var{body}.
  1525. This is useful for creating private files, for example.
  1526. @end defmac
  1527. @defun default-file-modes
  1528. This function returns the default file permissions, as an integer.
  1529. @end defun
  1530. @defun read-file-modes &optional prompt base-file
  1531. This function reads a set of file mode bits from the minibuffer. The
  1532. first optional argument @var{prompt} specifies a non-default prompt.
  1533. Second second optional argument @var{base-file} is the name of a file
  1534. on whose permissions to base the mode bits that this function returns,
  1535. if what the user types specifies mode bits relative to permissions of
  1536. an existing file.
  1537. If user input represents an octal number, this function returns that
  1538. number. If it is a complete symbolic specification of mode bits, as
  1539. in @code{"u=rwx"}, the function converts it to the equivalent numeric
  1540. value using @code{file-modes-symbolic-to-number} and returns the
  1541. result. If the specification is relative, as in @code{"o+g"}, then
  1542. the permissions on which the specification is based are taken from the
  1543. mode bits of @var{base-file}. If @var{base-file} is omitted or
  1544. @code{nil}, the function uses @code{0} as the base mode bits. The
  1545. complete and relative specifications can be combined, as in
  1546. @code{"u+r,g+rx,o+r,g-w"}. @xref{File permissions,,, coreutils, The
  1547. @sc{gnu} @code{Coreutils} Manual}, for a description of file mode
  1548. specifications.
  1549. @end defun
  1550. @defun file-modes-symbolic-to-number modes &optional base-modes
  1551. This function converts a symbolic file mode specification in
  1552. @var{modes} into the equivalent integer. If the symbolic
  1553. specification is based on an existing file, that file's mode bits are
  1554. taken from the optional argument @var{base-modes}; if that argument is
  1555. omitted or @code{nil}, it defaults to 0, i.e., no access rights at
  1556. all.
  1557. @end defun
  1558. @defun set-file-times filename &optional time
  1559. This function sets the access and modification times of @var{filename}
  1560. to @var{time}. The return value is @code{t} if the times are successfully
  1561. set, otherwise it is @code{nil}. @var{time} defaults to the current
  1562. time and must be in the format returned by @code{current-time}
  1563. (@pxref{Time of Day}).
  1564. @end defun
  1565. @defun set-file-extended-attributes filename attribute-alist
  1566. This function sets the Emacs-recognized extended file attributes for
  1567. @code{filename}. The second argument @var{attribute-alist} should be
  1568. an alist of the same form returned by @code{file-extended-attributes}.
  1569. The return value is @code{t} if the attributes are successfully set,
  1570. otherwise it is @code{nil}.
  1571. @xref{Extended Attributes}.
  1572. @end defun
  1573. @defun set-file-selinux-context filename context
  1574. This function sets the SELinux security context for @var{filename} to
  1575. @var{context}. The @var{context} argument should be a list
  1576. @code{(@var{user} @var{role} @var{type} @var{range})}, where each
  1577. element is a string. @xref{Extended Attributes}.
  1578. The function returns @code{t} if it succeeds in setting the SELinux
  1579. context of @var{filename}. It returns @code{nil} if the context was
  1580. not set (e.g., if SELinux is disabled, or if Emacs was compiled
  1581. without SELinux support).
  1582. @end defun
  1583. @defun set-file-acl filename acl
  1584. This function sets the Access Control List for @var{filename} to
  1585. @var{acl}. The @var{acl} argument should have the same form returned
  1586. by the function @code{file-acl}. @xref{Extended Attributes}.
  1587. The function returns @code{t} if it successfully sets the ACL of
  1588. @var{filename}, @code{nil} otherwise.
  1589. @end defun
  1590. @node Files and Storage
  1591. @section Files and Secondary Storage
  1592. @cindex secondary storage
  1593. After Emacs changes a file, there are two reasons the changes might
  1594. not survive later failures of power or media, both having to do with
  1595. efficiency. First, the operating system might alias written data with
  1596. data already stored elsewhere on secondary storage until one file or
  1597. the other is later modified; this will lose both files if the only
  1598. copy on secondary storage is lost due to media failure. Second, the
  1599. operating system might not write data to secondary storage
  1600. immediately, which will lose the data if power is lost.
  1601. @findex write-region
  1602. Although both sorts of failures can largely be avoided by a suitably
  1603. configured file system, such systems are typically more expensive or
  1604. less efficient. In more-typical systems, to survive media failure you
  1605. can copy the file to a different device, and to survive a power
  1606. failure you can use the @code{write-region} function with the
  1607. @code{write-region-inhibit-fsync} variable set to @code{nil}.
  1608. @xref{Writing to Files}.
  1609. @node File Names
  1610. @section File Names
  1611. @cindex file names
  1612. Files are generally referred to by their names, in Emacs as elsewhere.
  1613. File names in Emacs are represented as strings. The functions that
  1614. operate on a file all expect a file name argument.
  1615. In addition to operating on files themselves, Emacs Lisp programs
  1616. often need to operate on file names; i.e., to take them apart and to use
  1617. part of a name to construct related file names. This section describes
  1618. how to manipulate file names.
  1619. The functions in this section do not actually access files, so they
  1620. can operate on file names that do not refer to an existing file or
  1621. directory.
  1622. @findex cygwin-convert-file-name-from-windows
  1623. @findex cygwin-convert-file-name-to-windows
  1624. @cindex MS-Windows file-name syntax
  1625. @cindex converting file names from/to MS-Windows syntax
  1626. On MS-DOS and MS-Windows, these functions (like the function that
  1627. actually operate on files) accept MS-DOS or MS-Windows file-name syntax,
  1628. where backslashes separate the components, as well as Unix syntax; but
  1629. they always return Unix syntax. This enables Lisp programs to specify
  1630. file names in Unix syntax and work properly on all systems without
  1631. change.@footnote{In MS-Windows versions of Emacs compiled for the Cygwin
  1632. environment, you can use the functions
  1633. @code{cygwin-convert-file-name-to-windows} and
  1634. @code{cygwin-convert-file-name-from-windows} to convert between the
  1635. two file-name syntaxes.}
  1636. @menu
  1637. * File Name Components:: The directory part of a file name, and the rest.
  1638. * Relative File Names:: Some file names are relative to a current directory.
  1639. * Directory Names:: A directory's name as a directory
  1640. is different from its name as a file.
  1641. * File Name Expansion:: Converting relative file names to absolute ones.
  1642. * Unique File Names:: Generating names for temporary files.
  1643. * File Name Completion:: Finding the completions for a given file name.
  1644. * Standard File Names:: If your package uses a fixed file name,
  1645. how to handle various operating systems simply.
  1646. @end menu
  1647. @node File Name Components
  1648. @subsection File Name Components
  1649. @cindex directory part (of file name)
  1650. @cindex nondirectory part (of file name)
  1651. @cindex version number (in file name)
  1652. The operating system groups files into directories. To specify a
  1653. file, you must specify the directory and the file's name within that
  1654. directory. Therefore, Emacs considers a file name as having two main
  1655. parts: the @dfn{directory name} part, and the @dfn{nondirectory} part
  1656. (or @dfn{file name within the directory}). Either part may be empty.
  1657. Concatenating these two parts reproduces the original file name.
  1658. On most systems, the directory part is everything up to and including
  1659. the last slash (backslash is also allowed in input on MS-DOS or
  1660. MS-Windows); the nondirectory part is the rest.
  1661. For some purposes, the nondirectory part is further subdivided into
  1662. the name proper and the @dfn{version number}. On most systems, only
  1663. backup files have version numbers in their names.
  1664. @defun file-name-directory filename
  1665. This function returns the directory part of @var{filename}, as a
  1666. directory name (@pxref{Directory Names}), or @code{nil} if
  1667. @var{filename} does not include a directory part.
  1668. On GNU and Unix systems, a string returned by this function always
  1669. ends in a slash. On MS-DOS it can also end in a colon.
  1670. @example
  1671. @group
  1672. (file-name-directory "lewis/foo") ; @r{Unix example}
  1673. @result{} "lewis/"
  1674. @end group
  1675. @group
  1676. (file-name-directory "foo") ; @r{Unix example}
  1677. @result{} nil
  1678. @end group
  1679. @end example
  1680. @end defun
  1681. @defun file-name-nondirectory filename
  1682. This function returns the nondirectory part of @var{filename}.
  1683. @example
  1684. @group
  1685. (file-name-nondirectory "lewis/foo")
  1686. @result{} "foo"
  1687. @end group
  1688. @group
  1689. (file-name-nondirectory "foo")
  1690. @result{} "foo"
  1691. @end group
  1692. @group
  1693. (file-name-nondirectory "lewis/")
  1694. @result{} ""
  1695. @end group
  1696. @end example
  1697. @end defun
  1698. @defun file-name-sans-versions filename &optional keep-backup-version
  1699. This function returns @var{filename} with any file version numbers,
  1700. backup version numbers, or trailing tildes discarded.
  1701. If @var{keep-backup-version} is non-@code{nil}, then true file version
  1702. numbers understood as such by the file system are discarded from the
  1703. return value, but backup version numbers are kept.
  1704. @example
  1705. @group
  1706. (file-name-sans-versions "~rms/foo.~1~")
  1707. @result{} "~rms/foo"
  1708. @end group
  1709. @group
  1710. (file-name-sans-versions "~rms/foo~")
  1711. @result{} "~rms/foo"
  1712. @end group
  1713. @group
  1714. (file-name-sans-versions "~rms/foo")
  1715. @result{} "~rms/foo"
  1716. @end group
  1717. @end example
  1718. @end defun
  1719. @defun file-name-extension filename &optional period
  1720. This function returns @var{filename}'s final extension, if any,
  1721. after applying @code{file-name-sans-versions} to remove any
  1722. version/backup part. The extension, in a file name, is the part that
  1723. follows the last @samp{.} in the last name component (minus any
  1724. version/backup part).
  1725. This function returns @code{nil} for extensionless file names such as
  1726. @file{foo}. It returns @code{""} for null extensions, as in
  1727. @file{foo.}. If the last component of a file name begins with a
  1728. @samp{.}, that @samp{.} doesn't count as the beginning of an
  1729. extension. Thus, @file{.emacs}'s extension is @code{nil}, not
  1730. @samp{.emacs}.
  1731. If @var{period} is non-@code{nil}, then the returned value includes
  1732. the period that delimits the extension, and if @var{filename} has no
  1733. extension, the value is @code{""}.
  1734. @end defun
  1735. @defun file-name-sans-extension filename
  1736. This function returns @var{filename} minus its extension, if any. The
  1737. version/backup part, if present, is only removed if the file has an
  1738. extension. For example,
  1739. @example
  1740. (file-name-sans-extension "foo.lose.c")
  1741. @result{} "foo.lose"
  1742. (file-name-sans-extension "big.hack/foo")
  1743. @result{} "big.hack/foo"
  1744. (file-name-sans-extension "/my/home/.emacs")
  1745. @result{} "/my/home/.emacs"
  1746. (file-name-sans-extension "/my/home/.emacs.el")
  1747. @result{} "/my/home/.emacs"
  1748. (file-name-sans-extension "~/foo.el.~3~")
  1749. @result{} "~/foo"
  1750. (file-name-sans-extension "~/foo.~3~")
  1751. @result{} "~/foo.~3~"
  1752. @end example
  1753. Note that the @samp{.~3~} in the two last examples is the backup part,
  1754. not an extension.
  1755. @end defun
  1756. @defun file-name-base &optional filename
  1757. This function is the composition of @code{file-name-sans-extension}
  1758. and @code{file-name-nondirectory}. For example,
  1759. @example
  1760. (file-name-base "/my/home/foo.c")
  1761. @result{} "foo"
  1762. @end example
  1763. The @var{filename} argument defaults to @code{buffer-file-name}.
  1764. @end defun
  1765. @node Relative File Names
  1766. @subsection Absolute and Relative File Names
  1767. @cindex absolute file name
  1768. @cindex relative file name
  1769. All the directories in the file system form a tree starting at the
  1770. root directory. A file name can specify all the directory names
  1771. starting from the root of the tree; then it is called an
  1772. @dfn{absolute} file name. Or it can specify the position of the file
  1773. in the tree relative to a default directory; then it is called a
  1774. @dfn{relative} file name. On Unix and GNU/Linux, an absolute file
  1775. name starts with a @samp{/} or a @samp{~}
  1776. (@pxref{abbreviate-file-name}), and a relative one does not. On
  1777. MS-DOS and MS-Windows, an absolute file name starts with a slash or a
  1778. backslash, or with a drive specification @samp{@var{x}:/}, where
  1779. @var{x} is the @dfn{drive letter}.
  1780. @defun file-name-absolute-p filename
  1781. This function returns @code{t} if file @var{filename} is an absolute
  1782. file name or begins with @samp{~}, @code{nil} otherwise.
  1783. @example
  1784. @group
  1785. (file-name-absolute-p "~rms/foo")
  1786. @result{} t
  1787. @end group
  1788. @group
  1789. (file-name-absolute-p "rms/foo")
  1790. @result{} nil
  1791. @end group
  1792. @group
  1793. (file-name-absolute-p "/user/rms/foo")
  1794. @result{} t
  1795. @end group
  1796. @end example
  1797. @end defun
  1798. Given a possibly relative file name, you can expand any
  1799. leading @samp{~} and convert the result to an
  1800. absolute name using @code{expand-file-name} (@pxref{File Name
  1801. Expansion}). This function converts absolute file names to relative
  1802. names:
  1803. @defun file-relative-name filename &optional directory
  1804. This function tries to return a relative name that is equivalent to
  1805. @var{filename}, assuming the result will be interpreted relative to
  1806. @var{directory} (an absolute directory name or directory file name).
  1807. If @var{directory} is omitted or @code{nil}, it defaults to the
  1808. current buffer's default directory.
  1809. On some operating systems, an absolute file name begins with a device
  1810. name. On such systems, @var{filename} has no relative equivalent based
  1811. on @var{directory} if they start with two different device names. In
  1812. this case, @code{file-relative-name} returns @var{filename} in absolute
  1813. form.
  1814. @example
  1815. (file-relative-name "/foo/bar" "/foo/")
  1816. @result{} "bar"
  1817. (file-relative-name "/foo/bar" "/hack/")
  1818. @result{} "../foo/bar"
  1819. @end example
  1820. @end defun
  1821. @node Directory Names
  1822. @subsection Directory Names
  1823. @cindex directory name
  1824. @cindex directory file name
  1825. @cindex file name of directory
  1826. A @dfn{directory name} is the name of a directory. A directory is
  1827. actually a kind of file, so it has a file name (called the
  1828. @dfn{directory file name}, which is related to the directory name but
  1829. not identical to it. (This is not quite the same as the usual Unix
  1830. terminology.) These two different names for the same entity are
  1831. related by a syntactic transformation. On GNU and Unix systems, this
  1832. is simple: a directory name ends in a slash, whereas the directory
  1833. file name lacks that slash. On MS-DOS the relationship is more
  1834. complicated.
  1835. The difference between directory name and directory file name is
  1836. subtle but crucial. When an Emacs variable or function argument is
  1837. described as being a directory name, a directory file name is not
  1838. acceptable. When @code{file-name-directory} returns a string, that is
  1839. always a directory name.
  1840. The following two functions convert between directory names and
  1841. directory file names. They do nothing special with environment
  1842. variable substitutions such as @samp{$HOME}, and the constructs
  1843. @samp{~}, @samp{.} and @samp{..}.
  1844. @defun file-name-as-directory filename
  1845. This function returns a string representing @var{filename} in a form
  1846. that the operating system will interpret as the name of a directory (a
  1847. directory name). On most systems, this means appending a slash to the
  1848. string (if it does not already end in one).
  1849. @example
  1850. @group
  1851. (file-name-as-directory "~rms/lewis")
  1852. @result{} "~rms/lewis/"
  1853. @end group
  1854. @end example
  1855. @end defun
  1856. @defun directory-name-p filename
  1857. This function returns non-@code{nil} if @var{filename} ends with a
  1858. directory separator character. This is the forward slash @samp{/} on
  1859. Unix and GNU systems; MS-Windows and MS-DOS recognize both the forward
  1860. slash and the backslash @samp{\} as directory separators.
  1861. @end defun
  1862. @defun directory-file-name dirname
  1863. This function returns a string representing @var{dirname} in a form
  1864. that the operating system will interpret as the name of a file (a
  1865. directory file name). On most systems, this means removing the final
  1866. slash (or backslash) from the string.
  1867. @example
  1868. @group
  1869. (directory-file-name "~lewis/")
  1870. @result{} "~lewis"
  1871. @end group
  1872. @end example
  1873. @end defun
  1874. Given a directory name, you can combine it with a relative file name
  1875. using @code{concat}:
  1876. @example
  1877. (concat @var{dirname} @var{relfile})
  1878. @end example
  1879. @noindent
  1880. Be sure to verify that the file name is relative before doing that.
  1881. If you use an absolute file name, the results could be syntactically
  1882. invalid or refer to the wrong file.
  1883. If you want to use a directory file name in making such a
  1884. combination, you must first convert it to a directory name using
  1885. @code{file-name-as-directory}:
  1886. @example
  1887. (concat (file-name-as-directory @var{dirfile}) @var{relfile})
  1888. @end example
  1889. @noindent
  1890. Don't try concatenating a slash by hand, as in
  1891. @example
  1892. ;;; @r{Wrong!}
  1893. (concat @var{dirfile} "/" @var{relfile})
  1894. @end example
  1895. @noindent
  1896. because this is not portable. Always use
  1897. @code{file-name-as-directory}.
  1898. To avoid the issues mentioned above, or if the @var{dirname} value
  1899. might be nil (for example, from an element of @code{load-path}), use:
  1900. @example
  1901. (expand-file-name @var{relfile} @var{dirname})
  1902. @end example
  1903. However, @code{expand-file-name} expands leading @samp{~} in
  1904. @var{relfile}, which may not be what you want. @xref{File Name
  1905. Expansion}.
  1906. To convert a directory name to its abbreviation, use this
  1907. function:
  1908. @cindex file name abbreviations
  1909. @cindex abbreviated file names
  1910. @vindex directory-abbrev-alist
  1911. @defun abbreviate-file-name filename
  1912. @anchor{abbreviate-file-name}
  1913. This function returns an abbreviated form of @var{filename}. It
  1914. applies the abbreviations specified in @code{directory-abbrev-alist}
  1915. (@pxref{File Aliases,,File Aliases, emacs, The GNU Emacs Manual}),
  1916. then substitutes @samp{~} for the user's home directory if the
  1917. argument names a file in the home directory or one of its
  1918. subdirectories. If the home directory is a root directory, it is not
  1919. replaced with @samp{~}, because this does not make the result shorter
  1920. on many systems.
  1921. You can use this function for directory names and for file names,
  1922. because it recognizes abbreviations even as part of the name.
  1923. @end defun
  1924. @node File Name Expansion
  1925. @subsection Functions that Expand Filenames
  1926. @cindex expansion of file names
  1927. @dfn{Expanding} a file name means converting a relative file name to
  1928. an absolute one. Since this is done relative to a default directory,
  1929. you must specify the default directory name as well as the file name
  1930. to be expanded. It also involves expanding abbreviations like
  1931. @file{~/}
  1932. @ifnottex
  1933. (@pxref{abbreviate-file-name}),
  1934. @end ifnottex
  1935. and eliminating redundancies like @file{./} and @file{@var{name}/../}.
  1936. @defun expand-file-name filename &optional directory
  1937. This function converts @var{filename} to an absolute file name. If
  1938. @var{directory} is supplied, it is the default directory to start with
  1939. if @var{filename} is relative and does not start with @samp{~}.
  1940. (The value of @var{directory} should
  1941. itself be an absolute directory name or directory file name; it may
  1942. start with @samp{~}.) Otherwise, the current buffer's value of
  1943. @code{default-directory} is used. For example:
  1944. @example
  1945. @group
  1946. (expand-file-name "foo")
  1947. @result{} "/xcssun/users/rms/lewis/foo"
  1948. @end group
  1949. @group
  1950. (expand-file-name "../foo")
  1951. @result{} "/xcssun/users/rms/foo"
  1952. @end group
  1953. @group
  1954. (expand-file-name "foo" "/usr/spool/")
  1955. @result{} "/usr/spool/foo"
  1956. @end group
  1957. @end example
  1958. If the part of @var{filename} before the first slash is
  1959. @samp{~}, it expands to the value of the @env{HOME} environment
  1960. variable (usually your home directory). If the part before the first
  1961. slash is @samp{~@var{user}} and if @var{user} is a valid login name,
  1962. it expands to @var{user}'s home directory.
  1963. If you do not want this expansion for a relative @var{filename} that
  1964. might begin with a literal @samp{~}, you can use @code{(concat
  1965. (file-name-as-directory directory) filename)} instead of
  1966. @code{(expand-file-name filename directory)}.
  1967. Filenames containing @samp{.} or @samp{..} are simplified to their
  1968. canonical form:
  1969. @example
  1970. @group
  1971. (expand-file-name "bar/../foo")
  1972. @result{} "/xcssun/users/rms/lewis/foo"
  1973. @end group
  1974. @end example
  1975. In some cases, a leading @samp{..} component can remain in the output:
  1976. @example
  1977. @group
  1978. (expand-file-name "../home" "/")
  1979. @result{} "/../home"
  1980. @end group
  1981. @end example
  1982. @noindent
  1983. This is for the sake of filesystems that have the concept of a
  1984. superroot above the root directory @file{/}. On other filesystems,
  1985. @file{/../} is interpreted exactly the same as @file{/}.
  1986. Note that @code{expand-file-name} does @emph{not} expand environment
  1987. variables; only @code{substitute-in-file-name} does that:
  1988. @example
  1989. @group
  1990. (expand-file-name "$HOME/foo")
  1991. @result{} "/xcssun/users/rms/lewis/$HOME/foo"
  1992. @end group
  1993. @end example
  1994. Note also that @code{expand-file-name} does not follow symbolic links
  1995. at any level. This results in a difference between the way
  1996. @code{file-truename} and @code{expand-file-name} treat @samp{..}.
  1997. Assuming that @samp{/tmp/bar} is a symbolic link to the directory
  1998. @samp{/tmp/foo/bar} we get:
  1999. @example
  2000. @group
  2001. (file-truename "/tmp/bar/../myfile")
  2002. @result{} "/tmp/foo/myfile"
  2003. @end group
  2004. @group
  2005. (expand-file-name "/tmp/bar/../myfile")
  2006. @result{} "/tmp/myfile"
  2007. @end group
  2008. @end example
  2009. If you may need to follow symbolic links preceding @samp{..}, you
  2010. should make sure to call @code{file-truename} without prior direct or
  2011. indirect calls to @code{expand-file-name}. @xref{Truenames}.
  2012. @end defun
  2013. @defvar default-directory
  2014. The value of this buffer-local variable is the default directory for the
  2015. current buffer. It should be an absolute directory name; it may start
  2016. with @samp{~}. This variable is buffer-local in every buffer.
  2017. @code{expand-file-name} uses the default directory when its second
  2018. argument is @code{nil}.
  2019. The value is always a string ending with a slash.
  2020. @example
  2021. @group
  2022. default-directory
  2023. @result{} "/user/lewis/manual/"
  2024. @end group
  2025. @end example
  2026. @end defvar
  2027. @defun substitute-in-file-name filename
  2028. @anchor{Definition of substitute-in-file-name}
  2029. This function replaces environment variable references in
  2030. @var{filename} with the environment variable values. Following
  2031. standard Unix shell syntax, @samp{$} is the prefix to substitute an
  2032. environment variable value. If the input contains @samp{$$}, that is
  2033. converted to @samp{$}; this gives the user a way to quote a
  2034. @samp{$}.
  2035. The environment variable name is the series of alphanumeric characters
  2036. (including underscores) that follow the @samp{$}. If the character following
  2037. the @samp{$} is a @samp{@{}, then the variable name is everything up to the
  2038. matching @samp{@}}.
  2039. Calling @code{substitute-in-file-name} on output produced by
  2040. @code{substitute-in-file-name} tends to give incorrect results. For
  2041. instance, use of @samp{$$} to quote a single @samp{$} won't work
  2042. properly, and @samp{$} in an environment variable's value could lead
  2043. to repeated substitution. Therefore, programs that call this function
  2044. and put the output where it will be passed to this function need to
  2045. double all @samp{$} characters to prevent subsequent incorrect
  2046. results.
  2047. @c Wordy to avoid overfull hbox. --rjc 15mar92
  2048. Here we assume that the environment variable @env{HOME}, which holds
  2049. the user's home directory name, has value @samp{/xcssun/users/rms}.
  2050. @example
  2051. @group
  2052. (substitute-in-file-name "$HOME/foo")
  2053. @result{} "/xcssun/users/rms/foo"
  2054. @end group
  2055. @end example
  2056. After substitution, if a @samp{~} or a @samp{/} appears immediately
  2057. after another @samp{/}, the function discards everything before it (up
  2058. through the immediately preceding @samp{/}).
  2059. @example
  2060. @group
  2061. (substitute-in-file-name "bar/~/foo")
  2062. @result{} "~/foo"
  2063. @end group
  2064. @group
  2065. (substitute-in-file-name "/usr/local/$HOME/foo")
  2066. @result{} "/xcssun/users/rms/foo"
  2067. ;; @r{@file{/usr/local/} has been discarded.}
  2068. @end group
  2069. @end example
  2070. @end defun
  2071. Sometimes, it is not desired to expand file names. In such cases,
  2072. the file name can be quoted to suppress the expansion, and to handle
  2073. the file name literally. Quoting happens by prefixing the file name
  2074. with @samp{/:}.
  2075. @defmac file-name-quote name
  2076. This macro adds the quotation prefix @samp{/:} to the file @var{name}.
  2077. For a local file @var{name}, it prefixes @var{name} with @samp{/:}.
  2078. If @var{name} is a remote file name, the local part of @var{name} is
  2079. quoted. If @var{name} is already a quoted file name, @var{name} is
  2080. returned unchanged.
  2081. @example
  2082. @group
  2083. (substitute-in-file-name (file-name-quote "bar/~/foo"))
  2084. @result{} "/:bar/~/foo"
  2085. @end group
  2086. @group
  2087. (substitute-in-file-name (file-name-quote "/ssh:host:bar/~/foo"))
  2088. @result{} "/ssh:host:/:bar/~/foo"
  2089. @end group
  2090. @end example
  2091. The macro cannot be used to suppress file name handlers from magic
  2092. file names (@pxref{Magic File Names}).
  2093. @end defmac
  2094. @defmac file-name-unquote name
  2095. This macro removes the quotation prefix @samp{/:} from the file
  2096. @var{name}, if any. If @var{name} is a remote file name, the local
  2097. part of @var{name} is unquoted.
  2098. @end defmac
  2099. @defmac file-name-quoted-p name
  2100. This macro returns non-@code{nil}, when @var{name} is quoted with the
  2101. prefix @samp{/:}. If @var{name} is a remote file name, the local part
  2102. of @var{name} is checked.
  2103. @end defmac
  2104. @node Unique File Names
  2105. @subsection Generating Unique File Names
  2106. @cindex unique file names
  2107. @cindex temporary files
  2108. Some programs need to write temporary files. Here is the usual way to
  2109. construct a name for such a file:
  2110. @example
  2111. (make-temp-file @var{name-of-application})
  2112. @end example
  2113. @noindent
  2114. The job of @code{make-temp-file} is to prevent two different users or
  2115. two different jobs from trying to use the exact same file name.
  2116. @defun make-temp-file prefix &optional dir-flag suffix text
  2117. This function creates a temporary file and returns its name. Emacs
  2118. creates the temporary file's name by adding to @var{prefix} some
  2119. random characters that are different in each Emacs job. The result is
  2120. guaranteed to be a newly created file, containing @var{text} if that's
  2121. given as a string and empty otherwise. On MS-DOS, this function
  2122. can truncate the @var{string} prefix to fit into the 8+3 file-name
  2123. limits. If @var{prefix} is a relative file name, it is expanded
  2124. against @code{temporary-file-directory}.
  2125. @example
  2126. @group
  2127. (make-temp-file "foo")
  2128. @result{} "/tmp/foo232J6v"
  2129. @end group
  2130. @end example
  2131. When @code{make-temp-file} returns, the file has been created and is
  2132. empty. At that point, you should write the intended contents into the
  2133. file.
  2134. If @var{dir-flag} is non-@code{nil}, @code{make-temp-file} creates an
  2135. empty directory instead of an empty file. It returns the file name,
  2136. not the directory name, of that directory. @xref{Directory Names}.
  2137. If @var{suffix} is non-@code{nil}, @code{make-temp-file} adds it at
  2138. the end of the file name.
  2139. If @var{text} is a string, @code{make-temp-file} inserts it in the file.
  2140. To prevent conflicts among different libraries running in the same
  2141. Emacs, each Lisp program that uses @code{make-temp-file} should have its
  2142. own @var{prefix}. The number added to the end of @var{prefix}
  2143. distinguishes between the same application running in different Emacs
  2144. jobs. Additional added characters permit a large number of distinct
  2145. names even in one Emacs job.
  2146. @end defun
  2147. The default directory for temporary files is controlled by the
  2148. variable @code{temporary-file-directory}. This variable gives the user
  2149. a uniform way to specify the directory for all temporary files. Some
  2150. programs use @code{small-temporary-file-directory} instead, if that is
  2151. non-@code{nil}. To use it, you should expand the prefix against
  2152. the proper directory before calling @code{make-temp-file}.
  2153. @defopt temporary-file-directory
  2154. @cindex @env{TMPDIR} environment variable
  2155. @cindex @env{TMP} environment variable
  2156. @cindex @env{TEMP} environment variable
  2157. This variable specifies the directory name for creating temporary files.
  2158. Its value should be a directory name (@pxref{Directory Names}), but it
  2159. is good for Lisp programs to cope if the value is a directory's file
  2160. name instead. Using the value as the second argument to
  2161. @code{expand-file-name} is a good way to achieve that.
  2162. The default value is determined in a reasonable way for your operating
  2163. system; it is based on the @env{TMPDIR}, @env{TMP} and @env{TEMP}
  2164. environment variables, with a fall-back to a system-dependent name if
  2165. none of these variables is defined.
  2166. Even if you do not use @code{make-temp-file} to create the temporary
  2167. file, you should still use this variable to decide which directory to
  2168. put the file in. However, if you expect the file to be small, you
  2169. should use @code{small-temporary-file-directory} first if that is
  2170. non-@code{nil}.
  2171. @end defopt
  2172. @defopt small-temporary-file-directory
  2173. This variable specifies the directory name for
  2174. creating certain temporary files, which are likely to be small.
  2175. If you want to write a temporary file which is likely to be small, you
  2176. should compute the directory like this:
  2177. @example
  2178. (make-temp-file
  2179. (expand-file-name @var{prefix}
  2180. (or small-temporary-file-directory
  2181. temporary-file-directory)))
  2182. @end example
  2183. @end defopt
  2184. @defun make-temp-name base-name
  2185. This function generates a string that might be a unique file
  2186. name. The name starts with @var{base-name}, and has several random
  2187. characters appended to it, which are different in each Emacs job. It
  2188. is like @code{make-temp-file} except that (i) it just constructs a
  2189. name and does not create a file, (ii) @var{base-name} should be an
  2190. absolute file name that is not magic, and (iii) if the returned file
  2191. name is magic, it might name an existing file. @xref{Magic File
  2192. Names}.
  2193. @strong{Warning:} In most cases, you should not use this function; use
  2194. @code{make-temp-file} instead! This function is susceptible to a race
  2195. condition, between the @code{make-temp-name} call and the creation of
  2196. the file, which in some cases may cause a security hole.
  2197. @end defun
  2198. Sometimes, it is necessary to create a temporary file on a remote host
  2199. or a mounted directory. The following two functions support this.
  2200. @defun make-nearby-temp-file prefix &optional dir-flag suffix
  2201. This function is similar to @code{make-temp-file}, but it creates a
  2202. temporary file as close as possible to @code{default-directory}. If
  2203. @var{prefix} is a relative file name, and @code{default-directory} is
  2204. a remote file name or located on a mounted file systems, the temporary
  2205. file is created in the directory returned by the function
  2206. @code{temporary-file-directory}. Otherwise, the function
  2207. @code{make-temp-file} is used. @var{prefix}, @var{dir-flag} and
  2208. @var{suffix} have the same meaning as in @code{make-temp-file}.
  2209. @example
  2210. @group
  2211. (let ((default-directory "/ssh:remotehost:"))
  2212. (make-nearby-temp-file "foo"))
  2213. @result{} "/ssh:remotehost:/tmp/foo232J6v"
  2214. @end group
  2215. @end example
  2216. @end defun
  2217. @defun temporary-file-directory
  2218. The directory for writing temporary files via
  2219. @code{make-nearby-temp-file}. In case of a remote
  2220. @code{default-directory}, this is a directory for temporary files on
  2221. that remote host. If such a directory does not exist, or
  2222. @code{default-directory} ought to be located on a mounted file system
  2223. (see @code{mounted-file-systems}), the function returns
  2224. @code{default-directory}. For a non-remote and non-mounted
  2225. @code{default-directory}, the value of the variable
  2226. @code{temporary-file-directory} is returned.
  2227. @end defun
  2228. In order to extract the local part of the path name from a temporary
  2229. file, @code{file-local-name} could be used.
  2230. @node File Name Completion
  2231. @subsection File Name Completion
  2232. @cindex file name completion subroutines
  2233. @cindex completion, file name
  2234. This section describes low-level subroutines for completing a file
  2235. name. For higher level functions, see @ref{Reading File Names}.
  2236. @defun file-name-all-completions partial-filename directory
  2237. This function returns a list of all possible completions for a file
  2238. whose name starts with @var{partial-filename} in directory
  2239. @var{directory}. The order of the completions is the order of the files
  2240. in the directory, which is unpredictable and conveys no useful
  2241. information.
  2242. The argument @var{partial-filename} must be a file name containing no
  2243. directory part and no slash (or backslash on some systems). The current
  2244. buffer's default directory is prepended to @var{directory}, if
  2245. @var{directory} is not absolute.
  2246. In the following example, suppose that @file{~rms/lewis} is the current
  2247. default directory, and has five files whose names begin with @samp{f}:
  2248. @file{foo}, @file{file~}, @file{file.c}, @file{file.c.~1~}, and
  2249. @file{file.c.~2~}.
  2250. @example
  2251. @group
  2252. (file-name-all-completions "f" "")
  2253. @result{} ("foo" "file~" "file.c.~2~"
  2254. "file.c.~1~" "file.c")
  2255. @end group
  2256. @group
  2257. (file-name-all-completions "fo" "")
  2258. @result{} ("foo")
  2259. @end group
  2260. @end example
  2261. @end defun
  2262. @defun file-name-completion filename directory &optional predicate
  2263. This function completes the file name @var{filename} in directory
  2264. @var{directory}. It returns the longest prefix common to all file names
  2265. in directory @var{directory} that start with @var{filename}. If
  2266. @var{predicate} is non-@code{nil} then it ignores possible completions
  2267. that don't satisfy @var{predicate}, after calling that function
  2268. with one argument, the expanded absolute file name.
  2269. If only one match exists and @var{filename} matches it exactly, the
  2270. function returns @code{t}. The function returns @code{nil} if directory
  2271. @var{directory} contains no name starting with @var{filename}.
  2272. In the following example, suppose that the current default directory
  2273. has five files whose names begin with @samp{f}: @file{foo},
  2274. @file{file~}, @file{file.c}, @file{file.c.~1~}, and
  2275. @file{file.c.~2~}.
  2276. @example
  2277. @group
  2278. (file-name-completion "fi" "")
  2279. @result{} "file"
  2280. @end group
  2281. @group
  2282. (file-name-completion "file.c.~1" "")
  2283. @result{} "file.c.~1~"
  2284. @end group
  2285. @group
  2286. (file-name-completion "file.c.~1~" "")
  2287. @result{} t
  2288. @end group
  2289. @group
  2290. (file-name-completion "file.c.~3" "")
  2291. @result{} nil
  2292. @end group
  2293. @end example
  2294. @end defun
  2295. @defopt completion-ignored-extensions
  2296. @code{file-name-completion} usually ignores file names that end in any
  2297. string in this list. It does not ignore them when all the possible
  2298. completions end in one of these suffixes. This variable has no effect
  2299. on @code{file-name-all-completions}.
  2300. A typical value might look like this:
  2301. @example
  2302. @group
  2303. completion-ignored-extensions
  2304. @result{} (".o" ".elc" "~" ".dvi")
  2305. @end group
  2306. @end example
  2307. If an element of @code{completion-ignored-extensions} ends in a slash
  2308. @samp{/}, it signals a directory. The elements which do @emph{not} end
  2309. in a slash will never match a directory; thus, the above value will not
  2310. filter out a directory named @file{foo.elc}.
  2311. @end defopt
  2312. @node Standard File Names
  2313. @subsection Standard File Names
  2314. Sometimes, an Emacs Lisp program needs to specify a standard file
  2315. name for a particular use---typically, to hold configuration data
  2316. specified by the current user. Usually, such files should be located
  2317. in the directory specified by @code{user-emacs-directory}, which is
  2318. @file{~/.emacs.d} by default (@pxref{Init File}). For example, abbrev
  2319. definitions are stored by default in @file{~/.emacs.d/abbrev_defs}.
  2320. The easiest way to specify such a file name is to use the function
  2321. @code{locate-user-emacs-file}.
  2322. @defun locate-user-emacs-file base-name &optional old-name
  2323. This function returns an absolute file name for an Emacs-specific
  2324. configuration or data file. The argument @file{base-name} should be a
  2325. relative file name. The return value is the absolute name of a file
  2326. in the directory specified by @code{user-emacs-directory}; if that
  2327. directory does not exist, this function creates it.
  2328. If the optional argument @var{old-name} is non-@code{nil}, it
  2329. specifies a file in the user's home directory,
  2330. @file{~/@var{old-name}}. If such a file exists, the return value is
  2331. the absolute name of that file, instead of the file specified by
  2332. @var{base-name}. This argument is intended to be used by Emacs
  2333. packages to provide backward compatibility. For instance, prior to
  2334. the introduction of @code{user-emacs-directory}, the abbrev file was
  2335. located in @file{~/.abbrev_defs}. Here is the definition of
  2336. @code{abbrev-file-name}:
  2337. @example
  2338. (defcustom abbrev-file-name
  2339. (locate-user-emacs-file "abbrev_defs" ".abbrev_defs")
  2340. "Default name of file from which to read abbrevs."
  2341. @dots{}
  2342. :type 'file)
  2343. @end example
  2344. @end defun
  2345. A lower-level function for standardizing file names, which
  2346. @code{locate-user-emacs-file} uses as a subroutine, is
  2347. @code{convert-standard-filename}.
  2348. @defun convert-standard-filename filename
  2349. This function returns a file name based on @var{filename}, which fits
  2350. the conventions of the current operating system.
  2351. On GNU and Unix systems, this simply returns @var{filename}. On other
  2352. operating systems, it may enforce system-specific file name
  2353. conventions; for example, on MS-DOS this function performs a variety
  2354. of changes to enforce MS-DOS file name limitations, including
  2355. converting any leading @samp{.} to @samp{_} and truncating to three
  2356. characters after the @samp{.}.
  2357. The recommended way to use this function is to specify a name which
  2358. fits the conventions of GNU and Unix systems, and pass it to
  2359. @code{convert-standard-filename}.
  2360. @end defun
  2361. @node Contents of Directories
  2362. @section Contents of Directories
  2363. @cindex directory-oriented functions
  2364. @cindex file names in directory
  2365. A directory is a kind of file that contains other files entered under
  2366. various names. Directories are a feature of the file system.
  2367. Emacs can list the names of the files in a directory as a Lisp list,
  2368. or display the names in a buffer using the @code{ls} shell command. In
  2369. the latter case, it can optionally display information about each file,
  2370. depending on the options passed to the @code{ls} command.
  2371. @defun directory-files directory &optional full-name match-regexp nosort
  2372. This function returns a list of the names of the files in the directory
  2373. @var{directory}. By default, the list is in alphabetical order.
  2374. If @var{full-name} is non-@code{nil}, the function returns the files'
  2375. absolute file names. Otherwise, it returns the names relative to
  2376. the specified directory.
  2377. If @var{match-regexp} is non-@code{nil}, this function returns only
  2378. those file names that contain a match for that regular expression---the
  2379. other file names are excluded from the list. On case-insensitive
  2380. filesystems, the regular expression matching is case-insensitive.
  2381. @c Emacs 19 feature
  2382. If @var{nosort} is non-@code{nil}, @code{directory-files} does not sort
  2383. the list, so you get the file names in no particular order. Use this if
  2384. you want the utmost possible speed and don't care what order the files
  2385. are processed in. If the order of processing is visible to the user,
  2386. then the user will probably be happier if you do sort the names.
  2387. @example
  2388. @group
  2389. (directory-files "~lewis")
  2390. @result{} ("#foo#" "#foo.el#" "." ".."
  2391. "dired-mods.el" "files.texi"
  2392. "files.texi.~1~")
  2393. @end group
  2394. @end example
  2395. An error is signaled if @var{directory} is not the name of a directory
  2396. that can be read.
  2397. @end defun
  2398. @defun directory-files-recursively directory regexp &optional include-directories
  2399. Return all files under @var{directory} whose names match @var{regexp}.
  2400. This function searches the specified @var{directory} and its
  2401. sub-directories, recursively, for files whose basenames (i.e., without
  2402. the leading directories) match the specified @var{regexp}, and returns
  2403. a list of the absolute file names of the matching files
  2404. (@pxref{Relative File Names, absolute file names}). The file names
  2405. are returned in depth-first order, meaning that files in some
  2406. sub-directory are returned before the files in its parent directory.
  2407. In addition, matching files found in each subdirectory are sorted
  2408. alphabetically by their basenames. By default, directories whose
  2409. names match @var{regexp} are omitted from the list, but if the
  2410. optional argument @var{include-directories} is non-@code{nil}, they
  2411. are included.
  2412. @end defun
  2413. @defun directory-files-and-attributes directory &optional full-name match-regexp nosort id-format
  2414. This is similar to @code{directory-files} in deciding which files
  2415. to report on and how to report their names. However, instead
  2416. of returning a list of file names, it returns for each file a
  2417. list @code{(@var{filename} . @var{attributes})}, where @var{attributes}
  2418. is what @code{file-attributes} would return for that file.
  2419. The optional argument @var{id-format} has the same meaning as the
  2420. corresponding argument to @code{file-attributes} (@pxref{Definition
  2421. of file-attributes}).
  2422. @end defun
  2423. @defun file-expand-wildcards pattern &optional full
  2424. This function expands the wildcard pattern @var{pattern}, returning
  2425. a list of file names that match it.
  2426. If @var{pattern} is written as an absolute file name,
  2427. the values are absolute also.
  2428. If @var{pattern} is written as a relative file name, it is interpreted
  2429. relative to the current default directory. The file names returned are
  2430. normally also relative to the current default directory. However, if
  2431. @var{full} is non-@code{nil}, they are absolute.
  2432. @end defun
  2433. @defun insert-directory file switches &optional wildcard full-directory-p
  2434. This function inserts (in the current buffer) a directory listing for
  2435. directory @var{file}, formatted with @code{ls} according to
  2436. @var{switches}. It leaves point after the inserted text.
  2437. @var{switches} may be a string of options, or a list of strings
  2438. representing individual options.
  2439. The argument @var{file} may be either a directory name or a file
  2440. specification including wildcard characters. If @var{wildcard} is
  2441. non-@code{nil}, that means treat @var{file} as a file specification with
  2442. wildcards.
  2443. If @var{full-directory-p} is non-@code{nil}, that means the directory
  2444. listing is expected to show the full contents of a directory. You
  2445. should specify @code{t} when @var{file} is a directory and switches do
  2446. not contain @samp{-d}. (The @samp{-d} option to @code{ls} says to
  2447. describe a directory itself as a file, rather than showing its
  2448. contents.)
  2449. On most systems, this function works by running a directory listing
  2450. program whose name is in the variable @code{insert-directory-program}.
  2451. If @var{wildcard} is non-@code{nil}, it also runs the shell specified by
  2452. @code{shell-file-name}, to expand the wildcards.
  2453. MS-DOS and MS-Windows systems usually lack the standard Unix program
  2454. @code{ls}, so this function emulates the standard Unix program @code{ls}
  2455. with Lisp code.
  2456. As a technical detail, when @var{switches} contains the long
  2457. @samp{--dired} option, @code{insert-directory} treats it specially,
  2458. for the sake of dired. However, the normally equivalent short
  2459. @samp{-D} option is just passed on to @code{insert-directory-program},
  2460. as any other option.
  2461. @end defun
  2462. @defvar insert-directory-program
  2463. This variable's value is the program to run to generate a directory listing
  2464. for the function @code{insert-directory}. It is ignored on systems
  2465. which generate the listing with Lisp code.
  2466. @end defvar
  2467. @node Create/Delete Dirs
  2468. @section Creating, Copying and Deleting Directories
  2469. @cindex creating, copying and deleting directories
  2470. @c Emacs 19 features
  2471. Most Emacs Lisp file-manipulation functions get errors when used on
  2472. files that are directories. For example, you cannot delete a directory
  2473. with @code{delete-file}. These special functions exist to create and
  2474. delete directories.
  2475. @findex mkdir
  2476. @deffn Command make-directory dirname &optional parents
  2477. This command creates a directory named @var{dirname}. If
  2478. @var{parents} is non-@code{nil}, as is always the case in an
  2479. interactive call, that means to create the parent directories first,
  2480. if they don't already exist.
  2481. @code{mkdir} is an alias for this.
  2482. @end deffn
  2483. @deffn Command copy-directory dirname newname &optional keep-time parents copy-contents
  2484. This command copies the directory named @var{dirname} to
  2485. @var{newname}. If @var{newname} names an existing directory,
  2486. @var{dirname} will be copied to a subdirectory there.
  2487. It always sets the file modes of the copied files to match the
  2488. corresponding original file.
  2489. The third argument @var{keep-time} non-@code{nil} means to preserve the
  2490. modification time of the copied files. A prefix arg makes
  2491. @var{keep-time} non-@code{nil}.
  2492. The fourth argument @var{parents} says whether to
  2493. create parent directories if they don't exist. Interactively,
  2494. this happens by default.
  2495. The fifth argument @var{copy-contents}, if non-@code{nil}, means to
  2496. copy the contents of @var{dirname} directly into @var{newname} if the
  2497. latter is an existing directory, instead of copying @var{dirname} into
  2498. it as a subdirectory.
  2499. @end deffn
  2500. @cindex trash
  2501. @vindex delete-by-moving-to-trash
  2502. @deffn Command delete-directory dirname &optional recursive trash
  2503. This command deletes the directory named @var{dirname}. The function
  2504. @code{delete-file} does not work for files that are directories; you
  2505. must use @code{delete-directory} for them. If @var{recursive} is
  2506. @code{nil}, and the directory contains any files,
  2507. @code{delete-directory} signals an error.
  2508. If recursive is non-@code{nil}, there is no error merely because the
  2509. directory or its files are deleted by some other process before
  2510. @code{delete-directory} gets to them.
  2511. @code{delete-directory} only follows symbolic links at the level of
  2512. parent directories.
  2513. If the optional argument @var{trash} is non-@code{nil} and the
  2514. variable @code{delete-by-moving-to-trash} is non-@code{nil}, this
  2515. command moves the file into the system Trash instead of deleting it.
  2516. @xref{Misc File Ops,,Miscellaneous File Operations, emacs, The GNU
  2517. Emacs Manual}. When called interactively, @var{trash} is @code{t} if
  2518. no prefix argument is given, and @code{nil} otherwise.
  2519. @end deffn
  2520. @node Magic File Names
  2521. @section Making Certain File Names ``Magic''
  2522. @cindex magic file names
  2523. You can implement special handling for certain file names. This is
  2524. called making those names @dfn{magic}. The principal use for this
  2525. feature is in implementing access to remote files (@pxref{Remote Files,,
  2526. Remote Files, emacs, The GNU Emacs Manual}).
  2527. To define a kind of magic file name, you must supply a regular
  2528. expression to define the class of names (all those that match the
  2529. regular expression), plus a handler that implements all the primitive
  2530. Emacs file operations for file names that match.
  2531. @cindex file handler
  2532. @vindex file-name-handler-alist
  2533. The variable @code{file-name-handler-alist} holds a list of handlers,
  2534. together with regular expressions that determine when to apply each
  2535. handler. Each element has this form:
  2536. @example
  2537. (@var{regexp} . @var{handler})
  2538. @end example
  2539. @noindent
  2540. All the Emacs primitives for file access and file name transformation
  2541. check the given file name against @code{file-name-handler-alist}. If
  2542. the file name matches @var{regexp}, the primitives handle that file by
  2543. calling @var{handler}.
  2544. The first argument given to @var{handler} is the name of the
  2545. primitive, as a symbol; the remaining arguments are the arguments that
  2546. were passed to that primitive. (The first of these arguments is most
  2547. often the file name itself.) For example, if you do this:
  2548. @example
  2549. (file-exists-p @var{filename})
  2550. @end example
  2551. @noindent
  2552. and @var{filename} has handler @var{handler}, then @var{handler} is
  2553. called like this:
  2554. @example
  2555. (funcall @var{handler} 'file-exists-p @var{filename})
  2556. @end example
  2557. When a function takes two or more arguments that must be file names,
  2558. it checks each of those names for a handler. For example, if you do
  2559. this:
  2560. @example
  2561. (expand-file-name @var{filename} @var{dirname})
  2562. @end example
  2563. @noindent
  2564. then it checks for a handler for @var{filename} and then for a handler
  2565. for @var{dirname}. In either case, the @var{handler} is called like
  2566. this:
  2567. @example
  2568. (funcall @var{handler} 'expand-file-name @var{filename} @var{dirname})
  2569. @end example
  2570. @noindent
  2571. The @var{handler} then needs to figure out whether to handle
  2572. @var{filename} or @var{dirname}.
  2573. If the specified file name matches more than one handler, the one
  2574. whose match starts last in the file name gets precedence. This rule
  2575. is chosen so that handlers for jobs such as uncompression are handled
  2576. first, before handlers for jobs such as remote file access.
  2577. Here are the operations that a magic file name handler gets to handle:
  2578. @ifnottex
  2579. @noindent
  2580. @code{access-file}, @code{add-name-to-file},
  2581. @code{byte-compiler-base-file-name},@*
  2582. @code{copy-directory}, @code{copy-file},
  2583. @code{delete-directory}, @code{delete-file},
  2584. @code{diff-latest-backup-file},
  2585. @code{directory-file-name},
  2586. @code{directory-files},
  2587. @code{directory-files-and-attributes},
  2588. @code{dired-compress-file}, @code{dired-uncache},@*
  2589. @code{expand-file-name},
  2590. @code{file-accessible-directory-p},
  2591. @code{file-acl},
  2592. @code{file-attributes},
  2593. @code{file-directory-p},
  2594. @code{file-equal-p},
  2595. @code{file-executable-p}, @code{file-exists-p},
  2596. @code{file-in-directory-p},
  2597. @code{file-local-copy},
  2598. @code{file-modes}, @code{file-name-all-completions},
  2599. @code{file-name-as-directory},
  2600. @code{file-name-case-insensitive-p},
  2601. @code{file-name-completion},
  2602. @code{file-name-directory},
  2603. @code{file-name-nondirectory},
  2604. @code{file-name-sans-versions}, @code{file-newer-than-file-p},
  2605. @code{file-notify-add-watch}, @code{file-notify-rm-watch},
  2606. @code{file-notify-valid-p},
  2607. @code{file-ownership-preserved-p},
  2608. @code{file-readable-p}, @code{file-regular-p},
  2609. @code{file-remote-p}, @code{file-selinux-context},
  2610. @code{file-symlink-p}, @code{file-truename}, @code{file-writable-p},
  2611. @code{find-backup-file-name},@*
  2612. @code{get-file-buffer},
  2613. @code{insert-directory},
  2614. @code{insert-file-contents},@*
  2615. @code{load},
  2616. @code{make-auto-save-file-name},
  2617. @code{make-directory},
  2618. @code{make-directory-internal},
  2619. @code{make-nearby-temp-file},
  2620. @code{make-symbolic-link},@*
  2621. @code{process-file},
  2622. @code{rename-file}, @code{set-file-acl}, @code{set-file-modes},
  2623. @code{set-file-selinux-context}, @code{set-file-times},
  2624. @code{set-visited-file-modtime}, @code{shell-command},
  2625. @code{start-file-process},
  2626. @code{substitute-in-file-name},@*
  2627. @code{temporary-file-directory},
  2628. @code{unhandled-file-name-directory},
  2629. @code{vc-registered},
  2630. @code{verify-visited-file-modtime},@*
  2631. @code{write-region}.
  2632. @end ifnottex
  2633. @iftex
  2634. @noindent
  2635. @flushleft
  2636. @code{access-file}, @code{add-name-to-file},
  2637. @code{byte-com@discretionary{}{}{}piler-base-file-name},
  2638. @code{copy-directory}, @code{copy-file},
  2639. @code{delete-directory}, @code{delete-file},
  2640. @code{diff-latest-backup-file},
  2641. @code{directory-file-name},
  2642. @code{directory-files},
  2643. @code{directory-files-and-at@discretionary{}{}{}tributes},
  2644. @code{dired-compress-file}, @code{dired-uncache},
  2645. @code{expand-file-name},
  2646. @code{file-accessible-direc@discretionary{}{}{}tory-p},
  2647. @code{file-acl},
  2648. @code{file-attributes},
  2649. @code{file-direc@discretionary{}{}{}tory-p},
  2650. @code{file-equal-p},
  2651. @code{file-executable-p}, @code{file-exists-p},
  2652. @code{file-in-directory-p},
  2653. @code{file-local-copy},
  2654. @code{file-modes}, @code{file-name-all-completions},
  2655. @code{file-name-as-directory},
  2656. @code{file-name-case-insensitive-p},
  2657. @code{file-name-completion},
  2658. @code{file-name-directory},
  2659. @code{file-name-nondirec@discretionary{}{}{}tory},
  2660. @code{file-name-sans-versions}, @code{file-newer-than-file-p},
  2661. @code{file-notify-add-watch}, @code{file-notify-rm-watch},
  2662. @code{file-notify-valid-p},
  2663. @code{file-ownership-pre@discretionary{}{}{}served-p},
  2664. @code{file-readable-p}, @code{file-regular-p},
  2665. @code{file-remote-p}, @code{file-selinux-context},
  2666. @code{file-symlink-p}, @code{file-truename}, @code{file-writable-p},
  2667. @code{find-backup-file-name},
  2668. @code{get-file-buffer},
  2669. @code{insert-directory},
  2670. @code{insert-file-contents},
  2671. @code{load},
  2672. @code{make-auto-save-file-name},
  2673. @code{make-direc@discretionary{}{}{}tory},
  2674. @code{make-direc@discretionary{}{}{}tory-internal},
  2675. @code{make-symbolic-link},
  2676. @code{process-file},
  2677. @code{rename-file}, @code{set-file-acl}, @code{set-file-modes},
  2678. @code{set-file-selinux-context}, @code{set-file-times},
  2679. @code{set-visited-file-modtime}, @code{shell-command},
  2680. @code{start-file-process},
  2681. @code{substitute-in-file-name},
  2682. @code{unhandled-file-name-directory},
  2683. @code{vc-regis@discretionary{}{}{}tered},
  2684. @code{verify-visited-file-modtime},
  2685. @code{write-region}.
  2686. @end flushleft
  2687. @end iftex
  2688. Handlers for @code{insert-file-contents} typically need to clear the
  2689. buffer's modified flag, with @code{(set-buffer-modified-p nil)}, if the
  2690. @var{visit} argument is non-@code{nil}. This also has the effect of
  2691. unlocking the buffer if it is locked.
  2692. The handler function must handle all of the above operations, and
  2693. possibly others to be added in the future. It need not implement all
  2694. these operations itself---when it has nothing special to do for a
  2695. certain operation, it can reinvoke the primitive, to handle the
  2696. operation in the usual way. It should always reinvoke the primitive
  2697. for an operation it does not recognize. Here's one way to do this:
  2698. @smallexample
  2699. (defun my-file-handler (operation &rest args)
  2700. ;; @r{First check for the specific operations}
  2701. ;; @r{that we have special handling for.}
  2702. (cond ((eq operation 'insert-file-contents) @dots{})
  2703. ((eq operation 'write-region) @dots{})
  2704. @dots{}
  2705. ;; @r{Handle any operation we don't know about.}
  2706. (t (let ((inhibit-file-name-handlers
  2707. (cons 'my-file-handler
  2708. (and (eq inhibit-file-name-operation operation)
  2709. inhibit-file-name-handlers)))
  2710. (inhibit-file-name-operation operation))
  2711. (apply operation args)))))
  2712. @end smallexample
  2713. When a handler function decides to call the ordinary Emacs primitive for
  2714. the operation at hand, it needs to prevent the primitive from calling
  2715. the same handler once again, thus leading to an infinite recursion. The
  2716. example above shows how to do this, with the variables
  2717. @code{inhibit-file-name-handlers} and
  2718. @code{inhibit-file-name-operation}. Be careful to use them exactly as
  2719. shown above; the details are crucial for proper behavior in the case of
  2720. multiple handlers, and for operations that have two file names that may
  2721. each have handlers.
  2722. @kindex safe-magic (@r{property})
  2723. Handlers that don't really do anything special for actual access to the
  2724. file---such as the ones that implement completion of host names for
  2725. remote file names---should have a non-@code{nil} @code{safe-magic}
  2726. property. For instance, Emacs normally protects directory names
  2727. it finds in @code{PATH} from becoming magic, if they look like magic
  2728. file names, by prefixing them with @samp{/:}. But if the handler that
  2729. would be used for them has a non-@code{nil} @code{safe-magic}
  2730. property, the @samp{/:} is not added.
  2731. @kindex operations (@r{property})
  2732. A file name handler can have an @code{operations} property to
  2733. declare which operations it handles in a nontrivial way. If this
  2734. property has a non-@code{nil} value, it should be a list of
  2735. operations; then only those operations will call the handler. This
  2736. avoids inefficiency, but its main purpose is for autoloaded handler
  2737. functions, so that they won't be loaded except when they have real
  2738. work to do.
  2739. Simply deferring all operations to the usual primitives does not
  2740. work. For instance, if the file name handler applies to
  2741. @code{file-exists-p}, then it must handle @code{load} itself, because
  2742. the usual @code{load} code won't work properly in that case. However,
  2743. if the handler uses the @code{operations} property to say it doesn't
  2744. handle @code{file-exists-p}, then it need not handle @code{load}
  2745. nontrivially.
  2746. @defvar inhibit-file-name-handlers
  2747. This variable holds a list of handlers whose use is presently inhibited
  2748. for a certain operation.
  2749. @end defvar
  2750. @defvar inhibit-file-name-operation
  2751. The operation for which certain handlers are presently inhibited.
  2752. @end defvar
  2753. @defun find-file-name-handler file operation
  2754. This function returns the handler function for file name @var{file},
  2755. or @code{nil} if there is none. The argument @var{operation} should
  2756. be the operation to be performed on the file---the value you will pass
  2757. to the handler as its first argument when you call it. If
  2758. @var{operation} equals @code{inhibit-file-name-operation}, or if it is
  2759. not found in the @code{operations} property of the handler, this
  2760. function returns @code{nil}.
  2761. @end defun
  2762. @defun file-local-copy filename
  2763. This function copies file @var{filename} to an ordinary non-magic file
  2764. on the local machine, if it isn't on the local machine already. Magic
  2765. file names should handle the @code{file-local-copy} operation if they
  2766. refer to files on other machines. A magic file name that is used for
  2767. other purposes than remote file access should not handle
  2768. @code{file-local-copy}; then this function will treat the file as
  2769. local.
  2770. If @var{filename} is local, whether magic or not, this function does
  2771. nothing and returns @code{nil}. Otherwise it returns the file name
  2772. of the local copy file.
  2773. @end defun
  2774. @defun file-remote-p filename &optional identification connected
  2775. This function tests whether @var{filename} is a remote file. If
  2776. @var{filename} is local (not remote), the return value is @code{nil}.
  2777. If @var{filename} is indeed remote, the return value is a string that
  2778. identifies the remote system.
  2779. This identifier string can include a host name and a user name, as
  2780. well as characters designating the method used to access the remote
  2781. system. For example, the remote identifier string for the filename
  2782. @code{/sudo::/some/file} is @code{/sudo:root@@localhost:}.
  2783. If @code{file-remote-p} returns the same identifier for two different
  2784. filenames, that means they are stored on the same file system and can
  2785. be accessed locally with respect to each other. This means, for
  2786. example, that it is possible to start a remote process accessing both
  2787. files at the same time. Implementers of file handlers need to ensure
  2788. this principle is valid.
  2789. @var{identification} specifies which part of the identifier shall be
  2790. returned as string. @var{identification} can be the symbol
  2791. @code{method}, @code{user} or @code{host}; any other value is handled
  2792. like @code{nil} and means to return the complete identifier string.
  2793. In the example above, the remote @code{user} identifier string would
  2794. be @code{root}.
  2795. If @var{connected} is non-@code{nil}, this function returns @code{nil}
  2796. even if @var{filename} is remote, if Emacs has no network connection
  2797. to its host. This is useful when you want to avoid the delay of
  2798. making connections when they don't exist.
  2799. @end defun
  2800. @defun unhandled-file-name-directory filename
  2801. This function returns the name of a directory that is not magic. For
  2802. a non-magic @var{filename} it returns the corresponding directory name
  2803. (@pxref{Directory Names}). For a magic @var{filename}, it invokes the
  2804. file name handler, which therefore decides what value to return. If
  2805. @var{filename} is not accessible from a local process, then the file
  2806. name handler should indicate that by returning @code{nil}.
  2807. This is useful for running a subprocess; every subprocess must have a
  2808. non-magic directory to serve as its current directory, and this function
  2809. is a good way to come up with one.
  2810. @end defun
  2811. @defun file-local-name filename
  2812. This function returns the local part of file @var{filename}. For a
  2813. remote @var{filename}, it returns a file name which could be used
  2814. directly as argument of a remote process. If @var{filename} is local,
  2815. this function returns the file name.
  2816. @end defun
  2817. @defopt remote-file-name-inhibit-cache
  2818. The attributes of remote files can be cached for better performance. If
  2819. they are changed outside of Emacs's control, the cached values become
  2820. invalid, and must be reread.
  2821. When this variable is set to @code{nil}, cached values are never
  2822. expired. Use this setting with caution, only if you are sure nothing
  2823. other than Emacs ever changes the remote files. If it is set to
  2824. @code{t}, cached values are never used. This is the safest value, but
  2825. could result in performance degradation.
  2826. A compromise is to set it to a positive number. This means that
  2827. cached values are used for that amount of seconds since they were
  2828. cached. If a remote file is checked regularly, it might be a good
  2829. idea to let-bind this variable to a value less than the time period
  2830. between consecutive checks. For example:
  2831. @example
  2832. (defun display-time-file-nonempty-p (file)
  2833. (let ((remote-file-name-inhibit-cache
  2834. (- display-time-interval 5)))
  2835. (and (file-exists-p file)
  2836. (< 0 (nth 7 (file-attributes
  2837. (file-chase-links file)))))))
  2838. @end example
  2839. @end defopt
  2840. @node Format Conversion
  2841. @section File Format Conversion
  2842. @cindex file format conversion
  2843. @cindex encoding file formats
  2844. @cindex decoding file formats
  2845. @cindex text properties in files
  2846. @cindex saving text properties
  2847. Emacs performs several steps to convert the data in a buffer (text,
  2848. text properties, and possibly other information) to and from a
  2849. representation suitable for storing into a file. This section describes
  2850. the fundamental functions that perform this @dfn{format conversion},
  2851. namely @code{insert-file-contents} for reading a file into a buffer,
  2852. and @code{write-region} for writing a buffer into a file.
  2853. @menu
  2854. * Overview: Format Conversion Overview. @code{insert-file-contents} and @code{write-region}.
  2855. * Round-Trip: Format Conversion Round-Trip. Using @code{format-alist}.
  2856. * Piecemeal: Format Conversion Piecemeal. Specifying non-paired conversion.
  2857. @end menu
  2858. @node Format Conversion Overview
  2859. @subsection Overview
  2860. @noindent
  2861. The function @code{insert-file-contents}:
  2862. @itemize
  2863. @item initially, inserts bytes from the file into the buffer;
  2864. @item decodes bytes to characters as appropriate;
  2865. @item processes formats as defined by entries in @code{format-alist}; and
  2866. @item calls functions in @code{after-insert-file-functions}.
  2867. @end itemize
  2868. @noindent
  2869. The function @code{write-region}:
  2870. @itemize
  2871. @item initially, calls functions in @code{write-region-annotate-functions};
  2872. @item processes formats as defined by entries in @code{format-alist};
  2873. @item encodes characters to bytes as appropriate; and
  2874. @item modifies the file with the bytes.
  2875. @end itemize
  2876. This shows the symmetry of the lowest-level operations; reading and
  2877. writing handle things in opposite order. The rest of this section
  2878. describes the two facilities surrounding the three variables named
  2879. above, as well as some related functions. @ref{Coding Systems}, for
  2880. details on character encoding and decoding.
  2881. @node Format Conversion Round-Trip
  2882. @subsection Round-Trip Specification
  2883. The most general of the two facilities is controlled by the variable
  2884. @code{format-alist}, a list of @dfn{file format} specifications, which
  2885. describe textual representations used in files for the data in an Emacs
  2886. buffer. The descriptions for reading and writing are paired, which is
  2887. why we call this ``round-trip'' specification
  2888. (@pxref{Format Conversion Piecemeal}, for non-paired specification).
  2889. @defvar format-alist
  2890. This list contains one format definition for each defined file format.
  2891. Each format definition is a list of this form:
  2892. @example
  2893. (@var{name} @var{doc-string} @var{regexp} @var{from-fn} @var{to-fn} @var{modify} @var{mode-fn} @var{preserve})
  2894. @end example
  2895. @end defvar
  2896. @cindex format definition
  2897. @noindent
  2898. Here is what the elements in a format definition mean:
  2899. @table @var
  2900. @item name
  2901. The name of this format.
  2902. @item doc-string
  2903. A documentation string for the format.
  2904. @item regexp
  2905. A regular expression which is used to recognize files represented in
  2906. this format. If @code{nil}, the format is never applied automatically.
  2907. @item from-fn
  2908. A shell command or function to decode data in this format (to convert
  2909. file data into the usual Emacs data representation).
  2910. A shell command is represented as a string; Emacs runs the command as a
  2911. filter to perform the conversion.
  2912. If @var{from-fn} is a function, it is called with two arguments, @var{begin}
  2913. and @var{end}, which specify the part of the buffer it should convert.
  2914. It should convert the text by editing it in place. Since this can
  2915. change the length of the text, @var{from-fn} should return the modified
  2916. end position.
  2917. One responsibility of @var{from-fn} is to make sure that the beginning
  2918. of the file no longer matches @var{regexp}. Otherwise it is likely to
  2919. get called again. Also, @var{from-fn} must not involve buffers or
  2920. files other than the one being decoded, otherwise the internal buffer
  2921. used for formatting might be overwritten.
  2922. @item to-fn
  2923. A shell command or function to encode data in this format---that is, to
  2924. convert the usual Emacs data representation into this format.
  2925. If @var{to-fn} is a string, it is a shell command; Emacs runs the
  2926. command as a filter to perform the conversion.
  2927. If @var{to-fn} is a function, it is called with three arguments:
  2928. @var{begin} and @var{end}, which specify the part of the buffer it
  2929. should convert, and @var{buffer}, which specifies which buffer. There
  2930. are two ways it can do the conversion:
  2931. @itemize @bullet
  2932. @item
  2933. By editing the buffer in place. In this case, @var{to-fn} should
  2934. return the end-position of the range of text, as modified.
  2935. @item
  2936. By returning a list of annotations. This is a list of elements of the
  2937. form @code{(@var{position} . @var{string})}, where @var{position} is an
  2938. integer specifying the relative position in the text to be written, and
  2939. @var{string} is the annotation to add there. The list must be sorted in
  2940. order of position when @var{to-fn} returns it.
  2941. When @code{write-region} actually writes the text from the buffer to the
  2942. file, it intermixes the specified annotations at the corresponding
  2943. positions. All this takes place without modifying the buffer.
  2944. @end itemize
  2945. @var{to-fn} must not involve buffers or files other than the one being
  2946. encoded, otherwise the internal buffer used for formatting might be
  2947. overwritten.
  2948. @item modify
  2949. A flag, @code{t} if the encoding function modifies the buffer, and
  2950. @code{nil} if it works by returning a list of annotations.
  2951. @item mode-fn
  2952. A minor-mode function to call after visiting a file converted from this
  2953. format. The function is called with one argument, the integer 1;
  2954. that tells a minor-mode function to enable the mode.
  2955. @item preserve
  2956. A flag, @code{t} if @code{format-write-file} should not remove this format
  2957. from @code{buffer-file-format}.
  2958. @end table
  2959. The function @code{insert-file-contents} automatically recognizes file
  2960. formats when it reads the specified file. It checks the text of the
  2961. beginning of the file against the regular expressions of the format
  2962. definitions, and if it finds a match, it calls the decoding function for
  2963. that format. Then it checks all the known formats over again.
  2964. It keeps checking them until none of them is applicable.
  2965. Visiting a file, with @code{find-file-noselect} or the commands that use
  2966. it, performs conversion likewise (because it calls
  2967. @code{insert-file-contents}); it also calls the mode function for each
  2968. format that it decodes. It stores a list of the format names in the
  2969. buffer-local variable @code{buffer-file-format}.
  2970. @defvar buffer-file-format
  2971. This variable states the format of the visited file. More precisely,
  2972. this is a list of the file format names that were decoded in the course
  2973. of visiting the current buffer's file. It is always buffer-local in all
  2974. buffers.
  2975. @end defvar
  2976. When @code{write-region} writes data into a file, it first calls the
  2977. encoding functions for the formats listed in @code{buffer-file-format},
  2978. in the order of appearance in the list.
  2979. @deffn Command format-write-file file format &optional confirm
  2980. This command writes the current buffer contents into the file @var{file}
  2981. in a format based on @var{format}, which is a list of format names. It
  2982. constructs the actual format starting from @var{format}, then appending
  2983. any elements from the value of @code{buffer-file-format} with a
  2984. non-@code{nil} @var{preserve} flag (see above), if they are not already
  2985. present in @var{format}. It then updates @code{buffer-file-format} with
  2986. this format, making it the default for future saves. Except for the
  2987. @var{format} argument, this command is similar to @code{write-file}. In
  2988. particular, @var{confirm} has the same meaning and interactive treatment
  2989. as the corresponding argument to @code{write-file}. @xref{Definition of
  2990. write-file}.
  2991. @end deffn
  2992. @deffn Command format-find-file file format
  2993. This command finds the file @var{file}, converting it according to
  2994. format @var{format}. It also makes @var{format} the default if the
  2995. buffer is saved later.
  2996. The argument @var{format} is a list of format names. If @var{format} is
  2997. @code{nil}, no conversion takes place. Interactively, typing just
  2998. @key{RET} for @var{format} specifies @code{nil}.
  2999. @end deffn
  3000. @deffn Command format-insert-file file format &optional beg end
  3001. This command inserts the contents of file @var{file}, converting it
  3002. according to format @var{format}. If @var{beg} and @var{end} are
  3003. non-@code{nil}, they specify which part of the file to read, as in
  3004. @code{insert-file-contents} (@pxref{Reading from Files}).
  3005. The return value is like what @code{insert-file-contents} returns: a
  3006. list of the absolute file name and the length of the data inserted
  3007. (after conversion).
  3008. The argument @var{format} is a list of format names. If @var{format} is
  3009. @code{nil}, no conversion takes place. Interactively, typing just
  3010. @key{RET} for @var{format} specifies @code{nil}.
  3011. @end deffn
  3012. @defvar buffer-auto-save-file-format
  3013. This variable specifies the format to use for auto-saving. Its value is
  3014. a list of format names, just like the value of
  3015. @code{buffer-file-format}; however, it is used instead of
  3016. @code{buffer-file-format} for writing auto-save files. If the value
  3017. is @code{t}, the default, auto-saving uses the same format as a
  3018. regular save in the same buffer. This variable is always buffer-local
  3019. in all buffers.
  3020. @end defvar
  3021. @node Format Conversion Piecemeal
  3022. @subsection Piecemeal Specification
  3023. In contrast to the round-trip specification described in the previous
  3024. subsection (@pxref{Format Conversion Round-Trip}), you can use the variables
  3025. @code{after-insert-file-functions} and @code{write-region-annotate-functions}
  3026. to separately control the respective reading and writing conversions.
  3027. Conversion starts with one representation and produces another
  3028. representation. When there is only one conversion to do, there is no
  3029. conflict about what to start with. However, when there are multiple
  3030. conversions involved, conflict may arise when two conversions need to
  3031. start with the same data.
  3032. This situation is best understood in the context of converting text
  3033. properties during @code{write-region}. For example, the character at
  3034. position 42 in a buffer is @samp{X} with a text property @code{foo}. If
  3035. the conversion for @code{foo} is done by inserting into the buffer, say,
  3036. @samp{FOO:}, then that changes the character at position 42 from
  3037. @samp{X} to @samp{F}. The next conversion will start with the wrong
  3038. data straight away.
  3039. To avoid conflict, cooperative conversions do not modify the buffer,
  3040. but instead specify @dfn{annotations}, a list of elements of the form
  3041. @code{(@var{position} . @var{string})}, sorted in order of increasing
  3042. @var{position}.
  3043. If there is more than one conversion, @code{write-region} merges their
  3044. annotations destructively into one sorted list. Later, when the text
  3045. from the buffer is actually written to the file, it intermixes the
  3046. specified annotations at the corresponding positions. All this takes
  3047. place without modifying the buffer.
  3048. @c ??? What about "overriding" conversions like those allowed
  3049. @c ??? for 'write-region-annotate-functions', below? --ttn
  3050. In contrast, when reading, the annotations intermixed with the text
  3051. are handled immediately. @code{insert-file-contents} sets point to
  3052. the beginning of some text to be converted, then calls the conversion
  3053. functions with the length of that text. These functions should always
  3054. return with point at the beginning of the inserted text. This
  3055. approach makes sense for reading because annotations removed by the
  3056. first converter can't be mistakenly processed by a later converter.
  3057. Each conversion function should scan for the annotations it
  3058. recognizes, remove the annotation, modify the buffer text (to set a
  3059. text property, for example), and return the updated length of the
  3060. text, as it stands after those changes. The value returned by one
  3061. function becomes the argument to the next function.
  3062. @defvar write-region-annotate-functions
  3063. A list of functions for @code{write-region} to call. Each function in
  3064. the list is called with two arguments: the start and end of the region
  3065. to be written. These functions should not alter the contents of the
  3066. buffer. Instead, they should return annotations.
  3067. As a special case, a function may return with a different buffer
  3068. current. Emacs takes this to mean that the current buffer contains
  3069. altered text to be output. It therefore changes the @var{start} and
  3070. @var{end} arguments of the @code{write-region} call, giving them the
  3071. values of @code{point-min} and @code{point-max} in the new buffer,
  3072. respectively. It also discards all previous annotations, because they
  3073. should have been dealt with by this function.
  3074. @end defvar
  3075. @defvar write-region-post-annotation-function
  3076. The value of this variable, if non-@code{nil}, should be a function.
  3077. This function is called, with no arguments, after @code{write-region}
  3078. has completed.
  3079. If any function in @code{write-region-annotate-functions} returns with
  3080. a different buffer current, Emacs calls
  3081. @code{write-region-post-annotation-function} more than once. Emacs
  3082. calls it with the last buffer that was current, and again with the
  3083. buffer before that, and so on back to the original buffer.
  3084. Thus, a function in @code{write-region-annotate-functions} can create
  3085. a buffer, give this variable the local value of @code{kill-buffer} in
  3086. that buffer, set up the buffer with altered text, and make the buffer
  3087. current. The buffer will be killed after @code{write-region} is done.
  3088. @end defvar
  3089. @defvar after-insert-file-functions
  3090. Each function in this list is called by @code{insert-file-contents}
  3091. with one argument, the number of characters inserted, and with point
  3092. at the beginning of the inserted text. Each function should leave
  3093. point unchanged, and return the new character count describing the
  3094. inserted text as modified by the function.
  3095. @c ??? The docstring mentions a handler from 'file-name-handler-alist'
  3096. @c "intercepting" 'insert-file-contents'. Hmmm. --ttn
  3097. @end defvar
  3098. We invite users to write Lisp programs to store and retrieve text
  3099. properties in files, using these hooks, and thus to experiment with
  3100. various data formats and find good ones. Eventually we hope users
  3101. will produce good, general extensions we can install in Emacs.
  3102. We suggest not trying to handle arbitrary Lisp objects as text property
  3103. names or values---because a program that general is probably difficult
  3104. to write, and slow. Instead, choose a set of possible data types that
  3105. are reasonably flexible, and not too hard to encode.