files.texi 133 KB

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