home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / elisp / elisp-17 < prev    next >
Encoding:
GNU Info File  |  1993-05-31  |  45.0 KB  |  1,079 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.  
  4.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  5. Emacs Version 19.
  6.  
  7.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  8. Cambridge, MA 02139 USA
  9.  
  10.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  11.  
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.  
  16.    Permission is granted to copy and distribute modified versions of
  17. this manual under the conditions for verbatim copying, provided that
  18. the entire resulting derived work is distributed under the terms of a
  19. permission notice identical to this one.
  20.  
  21.    Permission is granted to copy and distribute translations of this
  22. manual into another language, under the above conditions for modified
  23. versions, except that this permission notice may be stated in a
  24. translation approved by the Foundation.
  25.  
  26. 
  27. File: elisp,  Node: Accessing Documentation,  Next: Keys in Documentation,  Prev: Documentation Basics,  Up: Documentation
  28.  
  29. Access to Documentation Strings
  30. ===============================
  31.  
  32.  - Function: documentation-property SYMBOL PROPERTY &optional VERBATIM
  33.      This function returns the documentation string that is recorded
  34.      SYMBOL's property list under property PROPERTY.  This uses the
  35.      function `get', but does more than that: it also retrieves the
  36.      string from the file `emacs/etc/DOC-VERSION' if necessary, and
  37.      runs `substitute-command-keys' to substitute the actual (current)
  38.      key bindings.
  39.  
  40.      If VERBATIM is non-`nil', that inhibits running
  41.      `substitute-command-keys'.  (The VERBATIM argument exists only as
  42.      of Emacs 19.)
  43.  
  44.           (documentation-property 'command-line-processed
  45.              'variable-documentation)
  46.                => "t once command line has been processed"
  47.  
  48.           (symbol-plist 'command-line-processed)
  49.                => (variable-documentation 188902)
  50.  
  51.  - Function: documentation FUNCTION &optional VERBATIM
  52.      This function returns the documentation string of FUNCTION.  If
  53.      the documentation string is stored in the `emacs/etc/DOC-VERSION'
  54.      file, this function will access it there.
  55.  
  56.      In addition, `documentation' runs `substitute-command-keys' on the
  57.      resulting string, so the value contains the actual (current) key
  58.      bindings.  (This is not done if VERBATIM is non-`nil'; the
  59.      VERBATIM argument exists only as of Emacs 19.)
  60.  
  61.      The function `documentation' signals a `void-function' error
  62.      unless FUNCTION has a function definition.  However, FUNCTION does
  63.      not need to have a documentation string.  If there is no
  64.      documentation string, `documentation' returns `nil'.
  65.  
  66.    Here is an example of using the two functions, `documentation' and
  67. `documentation-property', to display the documentation strings for
  68. several symbols in a `*Help*' buffer.
  69.  
  70.      (defun describe-symbols (pattern)
  71.        "Describe the Emacs Lisp symbols matching PATTERN.
  72.      All symbols that have PATTERN in their name are described
  73.      in the `*Help*' buffer."
  74.        (interactive "sDescribe symbols matching: ")
  75.        (let ((describe-func
  76.               (function
  77.                (lambda (s)
  78.  
  79.      ;; Print description of symbol.
  80.                  (if (fboundp s)             ; It is a function.
  81.                      (princ
  82.                       (format "%s\t%s\n%s\n\n" s
  83.                         (if (commandp s)
  84.                             (let ((keys (where-is-internal s)))
  85.                               (if keys
  86.                                   (concat
  87.                                    "Keys: "
  88.                                    (mapconcat 'key-description
  89.                                               keys " "))
  90.                                 "Keys: none"))
  91.                           "Function")
  92.  
  93.      (or (documentation s)
  94.                             "not documented"))))
  95.      
  96.                  (if (boundp s)              ; It is a variable.
  97.  
  98.      (princ
  99.                       (format "%s\t%s\n%s\n\n" s
  100.                         (if (user-variable-p s)
  101.                             "Option " "Variable")
  102.  
  103.      (or (documentation-property
  104.                               s 'variable-documentation)
  105.                             "not documented")))))))
  106.              sym-list)
  107.  
  108.      ;; Build a list of symbols that match pattern.
  109.          (mapatoms (function
  110.                     (lambda (sym)
  111.                       (if (string-match pattern (symbol-name sym))
  112.                           (setq sym-list (cons sym sym-list))))))
  113.  
  114.      ;; Display the data.
  115.          (with-output-to-temp-buffer "*Help*"
  116.            (mapcar describe-func (sort sym-list 'string<))
  117.            (print-help-return-message))))
  118.  
  119.    The `describe-symbols' function works like `apropos', but provides
  120. more information.
  121.  
  122.      (describe-symbols "goal")
  123.      
  124.      ---------- Buffer: *Help* ----------
  125.      goal-column     Option
  126.      *Semipermanent goal column for vertical motion, as set by C-x C-n, or nil.
  127.  
  128.      set-goal-column Command: C-x C-n
  129.      Set the current horizontal position as a goal for C-n and C-p.
  130.  
  131.      Those commands will move to this position in the line moved to
  132.      rather than trying to keep the same horizontal position.
  133.      With a non-nil argument, clears out the goal column
  134.      so that C-n and C-p resume vertical motion.
  135.      The goal column is stored in the variable `goal-column'.
  136.  
  137.      temporary-goal-column   Variable
  138.      Current goal column for vertical motion.
  139.      It is the column where point was
  140.      at the start of current run of vertical motion commands.
  141.      When the `track-eol' feature is doing its job, the value is 9999.
  142.      ---------- Buffer: *Help* ----------
  143.  
  144.  - Function: Snarf-documentation FILENAME
  145.      This function is used only during Emacs initialization, just before
  146.      the runnable Emacs is dumped.  It finds the file offsets of the
  147.      documentation strings stored in the file FILENAME, and records
  148.      them in the in-core function definitions and variable property
  149.      lists in place of the actual strings.  *Note Building Emacs::.
  150.  
  151.      Emacs finds the file FILENAME in the `emacs/etc' directory.  When
  152.      the dumped Emacs is later executed, the same file is found in the
  153.      directory `data-directory'.  Usually FILENAME is `"DOC-VERSION"'.
  154.  
  155.  - Variable: data-directory
  156.      This variable holds the name of the directory in which Emacs finds
  157.      certain data files that come with Emacs or are built as part of
  158.      building Emacs.  (In older Emacs versions, this directory was the
  159.      same as `exec-directory'.)
  160.  
  161. 
  162. File: elisp,  Node: Keys in Documentation,  Next: Describing Characters,  Prev: Accessing Documentation,  Up: Documentation
  163.  
  164. Substituting Key Bindings in Documentation
  165. ==========================================
  166.  
  167.    This function makes it possible for you to write a documentation
  168. string that enables a user to display information about the current,
  169. actual key bindings.  if you call `documentation' with non-`nil'
  170. VERBATIM, you might later call this function to do the substitution
  171. that you prevented `documentation' from doing.
  172.  
  173.  - Function: substitute-command-keys STRING
  174.      This function returns STRING with certain special substrings
  175.      replaced by the actual (current) key bindings.  This permits the
  176.      documentation to be displayed with accurate information about key
  177.      bindings.  (The key bindings may be changed by the user between
  178.      the time Emacs is built and the time that the documentation is
  179.      asked for.)
  180.  
  181.      This table lists the forms of the special substrings and what they
  182.      are replaced with:
  183.  
  184.     `\[COMMAND]'
  185.           is replaced either by a keystroke sequence that will invoke
  186.           COMMAND, or by `M-x COMMAND' if COMMAND is not bound to any
  187.           key sequence.
  188.  
  189.     `\{MAPVAR}'
  190.           is replaced by a summary of the value of MAPVAR, taken as a
  191.           keymap.  (The summary is made by `describe-bindings'.)
  192.  
  193.     `\<MAPVAR>'
  194.           makes this call to `substitute-command-keys' use the value of
  195.           MAPVAR as the keymap for future `\[COMMAND]' substrings.
  196.           This special string does not produce any replacement text
  197.           itself; it only affects the replacements done later.
  198.  
  199.      *Please note:* each `\' must be doubled when written in a string
  200.      in Emacs Lisp.
  201.  
  202.      Here are examples of the special substrings:
  203.  
  204.           (substitute-command-keys
  205.              "To abort recursive edit, type: \\[abort-recursive-edit]")
  206.           
  207.           => "To abort recursive edit, type: C-]"
  208.  
  209.           (substitute-command-keys
  210.              "The keys that are defined for the minibuffer here are:
  211.             \\{minibuffer-local-must-match-map}")
  212.           
  213.           => "The keys that are defined for the minibuffer here are:
  214.           
  215.           ?               minibuffer-completion-help
  216.           SPC             minibuffer-complete-word
  217.           TAB             minibuffer-complete
  218.           LFD             minibuffer-complete-and-exit
  219.           RET             minibuffer-complete-and-exit
  220.           C-g             abort-recursive-edit
  221.           "
  222.           (substitute-command-keys
  223.              "To abort a recursive edit from the minibuffer, type\
  224.           \\<minibuffer-local-must-match-map>\\[abort-recursive-edit].")
  225.           => "To abort a recursive edit from the minibuffer, type C-g."
  226.  
  227. 
  228. File: elisp,  Node: Describing Characters,  Next: Help Functions,  Prev: Keys in Documentation,  Up: Documentation
  229.  
  230. Describing Characters for Help Messages
  231. =======================================
  232.  
  233.    These functions convert events, key sequences or characters to
  234. textual descriptions.  These descriptions are useful for including
  235. arbitrary text characters or key sequences in messages, because they
  236. convert non-printing characters to sequences of printing characters.
  237. The description of a printing character is the character itself.
  238.  
  239.  - Function: key-description SEQUENCE
  240.      This function returns a string containing the Emacs standard
  241.      notation for the input events in SEQUENCE.  The argument SEQUENCE
  242.      may be a string, vector or list.  *Note Input Events::, for more
  243.      information about valid events.  See also the examples for
  244.      `single-key-description', below.
  245.  
  246.  - Function: single-key-description EVENT
  247.      This function returns a string describing EVENT in the standard
  248.      Emacs notation for keyboard input.  A normal printing character is
  249.      represented by itself, but a control character turns into a string
  250.      starting with `C-', a meta character turns into a string starting
  251.      with `M-', and space, linefeed, etc. are transformed to `SPC',
  252.      `LFD', etc.  A function key is represented by its name.  An event
  253.      which is a list is represented by the name of the symbol in the CAR
  254.      of the list.
  255.  
  256.           (single-key-description ?\C-x)
  257.                => "C-x"
  258.  
  259.           (key-description "\C-x \M-y \n \t \r \f123")
  260.                => "C-x SPC M-y SPC LFD SPC TAB SPC RET SPC C-l 1 2 3"
  261.  
  262.           (single-key-description 'C-mouse-1)
  263.                => "C-mouse-1"
  264.  
  265.  - Function: text-char-description CHARACTER
  266.      This function returns a string describing CHARACTER in the
  267.      standard Emacs notation for characters that appear in text--like
  268.      `single-key-description', except that control characters are
  269.      represented with a leading caret (which is how control characters
  270.      in Emacs buffers are usually displayed).
  271.  
  272.           (text-char-description ?\C-c)
  273.                => "^C"
  274.  
  275.           (text-char-description ?\M-m)
  276.                => "M-m"
  277.  
  278.           (text-char-description ?\C-\M-m)
  279.                => "M-^M"
  280.  
  281. 
  282. File: elisp,  Node: Help Functions,  Prev: Describing Characters,  Up: Documentation
  283.  
  284. Help Functions
  285. ==============
  286.  
  287.    Emacs provides a variety of on-line help functions, all accessible to
  288. the user as subcommands of the prefix `C-h'.  For more information
  289. about them, see *Note Help: (emacs)Help.  Here we describe some
  290. program-level interfaces to the same information.
  291.  
  292.  - Command: apropos REGEXP &optional DO-ALL PREDICATE
  293.      This function finds all symbols whose names contain a match for the
  294.      regular expression REGEXP, and returns a list of them.  It also
  295.      displays the symbols in a buffer named `*Help*', each with a
  296.      one-line description.
  297.  
  298.      If DO-ALL is non-`nil', then `apropos' also shows key bindings for
  299.      the functions that are found.
  300.  
  301.      If PREDICATE is non-`nil', it should be a function to be called on
  302.      each symbol that has matched REGEXP.  Only symbols for which
  303.      PREDICATE returns a non-`nil' value are listed or displayed.
  304.  
  305.      In the first of the following examples, `apropos' finds all the
  306.      symbols with names containing `exec'.  In the second example, it
  307.      finds and returns only those symbols that are also commands.  (We
  308.      don't show the output that results in the `*Help*' buffer.)
  309.  
  310.           (apropos "exec")
  311.                => (Buffer-menu-execute command-execute exec-directory
  312.               exec-path execute-extended-command execute-kbd-macro
  313.               executing-kbd-macro executing-macro)
  314.  
  315.           (apropos "exec" nil 'commandp)
  316.                => (Buffer-menu-execute execute-extended-command)
  317.  
  318.      The command `C-h a' (`command-apropos') calls `apropos', but
  319.      specifies a PREDICATE to restrict the output to symbols that are
  320.      commands.  The call to `apropos' looks like this:
  321.  
  322.           (apropos string t 'commandp)
  323.  
  324.  - Command: super-apropos REGEXP &optional DO-ALL
  325.      This function differs from `apropos' in that it searches
  326.      documentation strings as well as symbol names for matches for
  327.      REGEXP.  By default, it searches only the documentation strings,
  328.      and only those of functions and variables that are included in
  329.      Emacs when it is dumped.  If DO-ALL is non-`nil', it scans the
  330.      names and documentation strings of all functions and variables.
  331.  
  332.  - Command: help-command
  333.      This command is not a function, but rather a symbol which is
  334.      equivalent to the keymap called `help-map'.  It is defined in
  335.      `help.el' as follows:
  336.  
  337.           (define-key global-map "\C-h" 'help-command)
  338.           (fset 'help-command help-map)
  339.  
  340.  - Variable: help-map
  341.      The value of this variable is a local keymap for characters
  342.      following the Help key, `C-h'.
  343.  
  344.  - Function: print-help-return-message &optional FUNCTION
  345.      This function builds a string which is a message explaining how to
  346.      restore the previous state of the windows after a help command.
  347.      After building the message, it applies FUNCTION to it if FUNCTION
  348.      is non-`nil'.  Otherwise it calls `message' to display it in the
  349.      echo area.
  350.  
  351.      This function expects to be called inside a
  352.      `with-output-to-temp-buffer' special form, and expects
  353.      `standard-output' to have the value bound by that special form.
  354.      For an example of its use, see the example in the section
  355.      describing the `documentation' function (*note Accessing
  356.      Documentation::.).
  357.  
  358.      The constructed message will have one of the forms shown below.
  359.  
  360.           ---------- Echo Area ----------
  361.           Type C-x 1 to remove help window.
  362.           ---------- Echo Area ----------
  363.  
  364.           ---------- Echo Area ----------
  365.           Type C-x 4 b RET to restore old contents of help window.
  366.           ---------- Echo Area ----------
  367.  
  368.  - Variable: help-char
  369.      The value of this variable is the character that Emacs recognizes
  370.      as meaning Help.  When Emacs reads this character (which is
  371.      usually 8, the value of `C-h'), Emacs evaluates `(eval
  372.      help-form)', and displays the result if it is a string.  If
  373.      `help-form''s value is `nil', this character is read normally.
  374.  
  375.  - Variable: help-form
  376.      The value of this variable is a form to execute when the character
  377.      `help-char' is read.  If the form returns a string, that string is
  378.      displayed.  If `help-form' is `nil', then the help character is
  379.      not recognized.
  380.  
  381.      Entry to the minibuffer binds this variable to the value of
  382.      `minibuffer-help-form'.
  383.  
  384.    The following two functions are found in the library `helper'.  They
  385. are for modes that want to provide help without relinquishing control,
  386. such as the "electric" modes.  You must load that library with
  387. `(require 'helper)' in order to use them.  Their names begin with
  388. `Helper' to distinguish them from the ordinary help functions.
  389.  
  390.  - Command: Helper-describe-bindings
  391.      This command pops up a window displaying a help buffer containing a
  392.      listing of all of the key bindings from both the local and global
  393.      keymaps.  It works by calling `describe-bindings'.
  394.  
  395.  - Command: Helper-help
  396.      This command provides help for the current mode.  It prompts the
  397.      user in the minibuffer with the message `Help (Type ? for further
  398.      options)', and then provides assistance in finding out what the key
  399.      bindings are, and what the mode is intended for.  It returns `nil'.
  400.  
  401.      This can be customized by changing the map `Helper-help-map'.
  402.  
  403. 
  404. File: elisp,  Node: Files,  Next: Backups and Auto-Saving,  Prev: Documentation,  Up: Top
  405.  
  406. Files
  407. *****
  408.  
  409.    In Emacs, you can find, create, view, save, and otherwise work with
  410. files and file directories.  This chapter describes most of the
  411. file-related functions of Emacs Lisp, but a few others are described in
  412. *Note Buffers::, and those related to backups and auto-saving are
  413. described in *Note Backups and Auto-Saving::.
  414.  
  415. * Menu:
  416.  
  417. * Visiting Files::           Reading files into Emacs buffers for editing.
  418. * Saving Buffers::           Writing changed buffers back into files.
  419. * Reading from Files::       Reading files into buffers without visiting.
  420. * Writing to Files::         Writing new files from parts of buffers.
  421. * File Locks::               Locking and unlocking files, to prevent
  422.                                simultaneous editing by two people.
  423. * Information about Files::  Testing existence, accessibility, size of files.
  424. * Contents of Directories::  Getting a list of the files in a directory.
  425. * Create/Delete Dirs::         Creating and Deleting Directories.
  426. * Changing File Attributes:: Renaming files, changing protection, etc.
  427. * File Names::               Decomposing and expanding file names.
  428. * Magic File Names::         Defining "magic" special handling
  429.                    for certain file names.
  430.  
  431. 
  432. File: elisp,  Node: Visiting Files,  Next: Saving Buffers,  Up: Files
  433.  
  434. Visiting Files
  435. ==============
  436.  
  437.    Visiting a file means reading a file into a buffer.  Once this is
  438. done, we say that the buffer is "visiting" that file, and call the file
  439. "the visited file" of the buffer.
  440.  
  441.    A file and a buffer are two different things.  A file is information
  442. recorded permanently in the computer (unless you delete it).  A buffer,
  443. on the other hand, is information inside of Emacs that will vanish at
  444. the end of the editing session (or when you kill the buffer).  Usually,
  445. a buffer contains information that you have copied from a file; then we
  446. say the buffer is visiting that file.  The copy in the buffer is what
  447. you modify with editing commands.  Such changes to the buffer do not
  448. change the file; therefore, to make the changes permanent, you must
  449. "save" the buffer, which means copying the altered buffer contents back
  450. into the file.
  451.  
  452.    In spite of the distinction between files and buffers, people often
  453. refer to a file when they mean a buffer and vice-versa.  Indeed, we say,
  454. "I am editing a file," rather than, "I am editing a buffer which I will
  455. soon save as a file of the same name."  Humans do not usually need to
  456. make the distinction explicit.  When dealing with a computer program,
  457. however, it is good to keep the distinction in mind.
  458.  
  459. * Menu:
  460.  
  461. * Visiting Functions::         The usual interface functions for visiting.
  462. * Subroutines of Visiting::    Lower-level subroutines that they use.
  463.  
  464. 
  465. File: elisp,  Node: Visiting Functions,  Next: Subroutines of Visiting,  Up: Visiting Files
  466.  
  467. Functions for Visiting Files
  468. ----------------------------
  469.  
  470.    This section describes the functions normally used to visit files.
  471. For historical reasons, these functions have names starting with
  472. `find-' rather than `visit-'.  *Note Buffer File Name::, for functions
  473. and variables that access the visited file name of a buffer or that
  474. find an existing buffer by its visited file name.
  475.  
  476.  - Command: find-file FILENAME
  477.      This function reads the file FILENAME into a buffer and displays
  478.      that buffer in the selected window so that the user can edit it.
  479.  
  480.      The body of the `find-file' function is very simple and looks like
  481.      this:
  482.  
  483.           (switch-to-buffer (find-file-noselect filename))
  484.  
  485.      (See `switch-to-buffer' in *Note Displaying Buffers::.)
  486.  
  487.      When `find-file' is called interactively, it prompts for FILENAME
  488.      in the minibuffer.
  489.  
  490.  - Function: find-file-noselect FILENAME
  491.      This function is the guts of all the file-visiting functions.  It
  492.      reads a file into a buffer and returns the buffer.  You may then
  493.      make the buffer current or display it in a window if you wish, but
  494.      this function does not do so.
  495.  
  496.      If no buffer is currently visiting FILENAME, then one is created
  497.      and the file is visited.  If FILENAME does not exist, the buffer
  498.      is left empty, and `find-file-noselect' displays the message `New
  499.      file' in the echo area.
  500.  
  501.      If a buffer is already visiting FILENAME, then the
  502.      `find-file-noselect' function uses that buffer rather than creating
  503.      a new one.  However, it does verify that the file has not changed
  504.      since it was last visited or saved in that buffer.  If the file
  505.      has changed, then this function asks the user whether to reread
  506.      the changed file.  If the user says `yes', any changes previously
  507.      made in the buffer are lost.
  508.  
  509.      The `find-file-noselect' function calls `after-find-file' after
  510.      the file is read in (*note Subroutines of Visiting::.).  The
  511.      `after-find-file' function sets the buffer major mode, parses local
  512.      variables, warns the user if there exists an auto-save file more
  513.      recent than the file just visited, and finishes by running the
  514.      functions in `find-file-hooks'.
  515.  
  516.      The `find-file-noselect' function returns the buffer that is
  517.      visiting the file FILENAME.
  518.  
  519.           (find-file-noselect "/etc/fstab")
  520.                => #<buffer fstab>
  521.  
  522.  - Command: find-alternate-file FILENAME
  523.      This function reads the file FILENAME into a buffer and selects
  524.      it, killing the buffer current at the time the command is run.  It
  525.      is useful if you have visited the wrong file by mistake, so that
  526.      you can get rid of the buffer that you did not want to create, at
  527.      the same time as you visit the file you intended.
  528.  
  529.      When this function is called interactively, it prompts for
  530.      FILENAME.
  531.  
  532.  - Command: find-file-other-window FILENAME
  533.      This function visits the file FILENAME and displays its buffer in
  534.      a window other than the selected window.  It may use another
  535.      existing window or split a window; see *Note Displaying Buffers::.
  536.  
  537.      When this function is called interactively, it prompts for
  538.      FILENAME.
  539.  
  540.  - Command: find-file-read-only FILENAME
  541.      This function visits the file named FILENAME and selects its
  542.      buffer, just like `find-file', but it marks the buffer as
  543.      read-only.  *Note Read Only Buffers::, for related functions and
  544.      variables.
  545.  
  546.      When this function is called interactively, it prompts for
  547.      FILENAME.
  548.  
  549.  - Command: view-file FILENAME
  550.      This function views FILENAME in View mode, returning to the
  551.      previous buffer when done.  View mode is a mode that allows you to
  552.      skim rapidly through the file but does not let you modify it.
  553.  
  554.      After loading the file, `view-file' runs the normal hook
  555.      `view-hook' using `run-hooks'.  *Note Hooks::.
  556.  
  557.      When this function is called interactively, it prompts for
  558.      FILENAME.
  559.  
  560.  - Variable: find-file-hooks
  561.      The value of this variable is a list of functions to be called
  562.      after a file is visited.  The file's local-variables specification
  563.      (if any) will have been processed before the hooks are run.  The
  564.      buffer visiting the file is current when the hook functions are
  565.      run.
  566.  
  567.      This variable could be a normal hook, but we think that renaming it
  568.      would not be advisable.
  569.  
  570.  - Variable: find-file-not-found-hooks
  571.      The value of this variable is a list of functions to be called when
  572.      `find-file' or `find-file-noselect' is passed a nonexistent
  573.      FILENAME.  These functions are called as soon as the error is
  574.      detected.  `buffer-file-name' is already set up.  The functions are
  575.      called in the order given, until one of them returns non-`nil'.
  576.  
  577.      This is not a normal hook because the values of the functions are
  578.      used and they may not all be run.
  579.  
  580. 
  581. File: elisp,  Node: Subroutines of Visiting,  Prev: Visiting Functions,  Up: Visiting Files
  582.  
  583. Subroutines of Visiting
  584. -----------------------
  585.  
  586.    The `find-file-noselect' function uses the `create-file-buffer' and
  587. `after-find-file' functions as subroutines.  Sometimes it is useful to
  588. call them directly.
  589.  
  590.  - Function: create-file-buffer FILENAME
  591.      This function creates a suitably named buffer for visiting
  592.      FILENAME, and returns it.  The string FILENAME (sans directory) is
  593.      used unchanged if that name is free; otherwise, a string such as
  594.      `<2>' is appended to get an unused name.  See also *Note Creating
  595.      Buffers::.
  596.  
  597.      *Please note:* `create-file-buffer' does *not* associate the new
  598.      buffer with a file and does not make it the current buffer.
  599.  
  600.           (create-file-buffer "foo")
  601.                => #<buffer foo>
  602.           (create-file-buffer "foo")
  603.                => #<buffer foo<2>>
  604.           (create-file-buffer "foo")
  605.                => #<buffer foo<3>>
  606.  
  607.      This function is used by `find-file-noselect'.  It uses
  608.      `generate-new-buffer' (*note Creating Buffers::.).
  609.  
  610.  - Function: after-find-file &optional ERROR WARN
  611.      This function is called by `find-file-noselect' and by the default
  612.      revert function (*note Reverting::.).  It sets the buffer major
  613.      mode, and parses local variables (*note Auto Major Mode::.).
  614.  
  615.      If there was an error in opening the file, the calling function
  616.      should pass ERROR a non-`nil' value.  In that case,
  617.      `after-find-file' issues a warning: `(New File)'.  Note that, for
  618.      serious errors, you would not even call `after-find-file'.  Only
  619.      "file not found" errors get here with a non-`nil' ERROR.
  620.  
  621.      If WARN is non-`nil', then this function issues a warning if an
  622.      auto-save file exists and is more recent than the visited file.
  623.  
  624.      The last thing `after-find-file' does is call all the functions in
  625.      `find-file-hooks'.
  626.  
  627. 
  628. File: elisp,  Node: Saving Buffers,  Next: Reading from Files,  Prev: Visiting Files,  Up: Files
  629.  
  630. Saving Buffers
  631. ==============
  632.  
  633.    When you edit a file in Emacs, you are actually working on a buffer
  634. that is visiting that file--that is, the contents of the file are
  635. copied into the buffer and the copy is what you edit.  Changes to the
  636. buffer do not change the file until you "save" the buffer, which means
  637. copying the contents of the buffer into the file.
  638.  
  639.  - Command: save-buffer &optional BACKUP-OPTION
  640.      This function saves the contents of the current buffer in its
  641.      visited file if the buffer has been modified since it was last
  642.      visited or saved.  Otherwise it does nothing.
  643.  
  644.      `save-buffer' is responsible for making backup files.  Normally,
  645.      BACKUP-OPTION is `nil', and `save-buffer' makes a backup file only
  646.      if this is the first save or if the buffer was previously
  647.      modified.  Other values for BACKUP-OPTION request the making of
  648.      backup files in other circumstances:
  649.  
  650.         * With an argument of 4 or 64, reflecting 1 or 3 `C-u''s, the
  651.           `save-buffer' function marks this version of the file to be
  652.           backed up when the buffer is next saved.
  653.  
  654.         * With an argument of 16 or 64, reflecting 2 or 3 `C-u''s, the
  655.           `save-buffer' function unconditionally backs up the previous
  656.           version of the file before saving it.
  657.  
  658.  - Command: save-some-buffers &optional SAVE-SILENTLY-P EXITING
  659.      This command saves some modified file-visiting buffers.  Normally
  660.      it asks the user about each buffer.  But if SAVE-SILENTLY-P is
  661.      non-`nil', it saves all the file-visiting buffers without querying
  662.      the user.
  663.  
  664.      The optional EXITING argument, if non-`nil', requests this
  665.      function to offer also to save certain other buffers that are not
  666.      visiting files.  These are buffers that have a non-`nil' local
  667.      value of `buffer-offer-save'.  (A user who says yes to saving one
  668.      of these is asked to specify a file name to use.)  The
  669.      `save-buffers-kill-emacs' function passes a non-`nil' value for
  670.      this argument.
  671.  
  672.  - Variable: buffer-offer-save
  673.      When this variable is non-`nil' in a buffer, Emacs offers to save
  674.      the buffer on exit even if the buffer is not visiting a file.  The
  675.      variable is automatically local in all buffers.  Normally, Mail
  676.      mode (used for editing outgoing mail) sets this to `t'.
  677.  
  678.  - Command: write-file FILENAME
  679.      This function writes the current buffer into file FILENAME, makes
  680.      the buffer visit that file, and marks it not modified.  The buffer
  681.      is renamed to correspond to FILENAME unless that name is already
  682.      in use.
  683.  
  684.  - Variable: write-file-hooks
  685.      The value of this variable is a list of functions to be called
  686.      before writing out a buffer to its visited file.  If one of them
  687.      returns non-`nil', the file is considered already written and the
  688.      rest of the functions are not called, nor is the usual code for
  689.      writing the file executed.
  690.  
  691.      If a function in `write-file-hooks' returns non-`nil', it is
  692.      responsible for making a backup file (if that is appropriate).  To
  693.      do so, execute the following code:
  694.  
  695.           (or buffer-backed-up (backup-buffer))
  696.  
  697.      You might wish to save the file modes value returned by
  698.      `backup-buffer' and use that to set the mode bits of the file that
  699.      you write.  This is what `basic-save-buffer' does when it writes a
  700.      file in the usual way.
  701.  
  702.      Here is an example showing how to add an element to
  703.      `write-file-hooks' but avoid adding it twice:
  704.  
  705.           (or (memq 'my-write-file-hook write-file-hooks)
  706.               (setq write-file-hooks
  707.                     (cons
  708.                     'my-write-file-hook write-file-hooks)))
  709.  
  710.  - Variable: local-write-file-hooks
  711.      This works just like `write-file-hooks', but it is intended to be
  712.      made local to particular buffers.  It's not a good idea to make
  713.      `write-file-hooks' local to a buffer--use this variable instead.
  714.  
  715.      The variable is marked as a permanent local, so that changing the
  716.      major mode does not alter a buffer-local value.  This is
  717.      convenient for packages that read "file" contents in special ways,
  718.      and set up hooks to save the data in a corresponding way.
  719.  
  720.  - Variable: write-contents-hooks
  721.      This works just like `write-file-hooks', but it is intended to be
  722.      used for hooks that pertain to the contents of the file, as
  723.      opposed to hooks that pertain to where the file came from.
  724.  
  725.  - Variable: after-save-hook
  726.      This normal hook runs after a buffer has been saved in its visited
  727.      file.
  728.  
  729.  - Variable: file-precious-flag
  730.      If this variable is non-`nil', then `save-buffer' protects against
  731.      I/O errors while saving by writing the new file to a temporary
  732.      name instead of the name it is supposed to have, and then renaming
  733.      it to the intended name after it is clear there are no errors.
  734.      This procedure prevents problems such as a lack of disk space from
  735.      resulting in an invalid file.
  736.  
  737.      (This feature worked differently in older Emacs versions.)
  738.  
  739.      Some modes set this non-`nil' locally in particular buffers.
  740.  
  741.  - User Option: require-final-newline
  742.      This variable determines whether files may be written out that do
  743.      *not* end with a newline.  If the value of the variable is `t',
  744.      then Emacs silently puts a newline at the end of the file whenever
  745.      the buffer being saved does not already end in one.  If the value
  746.      of the variable is non-`nil', but not `t', then Emacs asks the
  747.      user whether to add a newline each time the case arises.
  748.  
  749.      If the value of the variable is `nil', then Emacs doesn't add
  750.      newlines at all.  `nil' is the default value, but a few major modes
  751.      set it to `t' in particular buffers.
  752.  
  753. 
  754. File: elisp,  Node: Reading from Files,  Next: Writing to Files,  Prev: Saving Buffers,  Up: Files
  755.  
  756. Reading from Files
  757. ==================
  758.  
  759.    You can copy a file from the disk and insert it into a buffer using
  760. the `insert-file-contents' function.  Don't use the user-level command
  761. `insert-file' in a Lisp program, as that sets the mark.
  762.  
  763.  - Function: insert-file-contents FILENAME &optional VISIT
  764.      This function inserts the contents of file FILENAME into the
  765.      current buffer after point. It returns a list of the absolute file
  766.      name and the length of the data inserted.  An error is signaled if
  767.      FILENAME is not the name of a file that can be read.
  768.  
  769.      If VISIT is non-`nil', it also marks the buffer as unmodified and
  770.      sets up various fields in the buffer so that it is visiting the
  771.      file FILENAME: these include the buffer's visited file name and
  772.      its last save file modtime.  This feature is used by
  773.      `find-file-noselect' and you should probably not use it yourself.
  774.  
  775.    If you want to pass a file name to another process so that another
  776. program can read the file, see the function `file-local-copy' in *Note
  777. Magic File Names::.
  778.  
  779. 
  780. File: elisp,  Node: Writing to Files,  Next: File Locks,  Prev: Reading from Files,  Up: Files
  781.  
  782. Writing to Files
  783. ================
  784.  
  785.    You can write the contents of a buffer, or part of a buffer, directly
  786. to a file on disk using the `append-to-file' and `write-region'
  787. functions.  Don't use these functions to write to files that are being
  788. visited; that could cause confusion in the mechanisms for visiting.
  789.  
  790.  - Command: append-to-file START END FILENAME
  791.      This function appends the contents of the region delimited by
  792.      START and END in the current buffer to the end of file FILENAME.
  793.      If that file does not exist, it is created.  This function returns
  794.      `nil'.
  795.  
  796.      An error is signaled if FILENAME specifies a nonwritable file, or
  797.      a nonexistent file in a directory where files cannot be created.
  798.  
  799.  - Command: write-region START END FILENAME &optional APPEND VISIT
  800.      This function writes the region (of the current buffer) delimited
  801.      by START and END into the file specified by FILENAME.
  802.  
  803.      If START is a string, then `write-region' writes or appends that
  804.      string, rather than text from the buffer.
  805.  
  806.      If APPEND is non-`nil', then the region is appended to the
  807.      existing file contents (if any).
  808.  
  809.      If VISIT is `t', then Emacs establishes an association between the
  810.      buffer and the file: the buffer is then visiting that file.  It
  811.      also sets the last file modification time for the current buffer to
  812.      FILENAME's modtime, and marks the buffer as not modified.  This
  813.      feature is used by `write-file' and you should probably not use it
  814.      yourself.
  815.  
  816.      If VISIT is a string, it specifies the file name to visit.  This
  817.      way, you can write the data to one file (FILENAME) while recording
  818.      the buffer as visiting another file (VISIT).  The argument VISIT
  819.      is used in the echo area message and also for file locking; VISIT
  820.      is stored in `buffer-file-name'.  This feature is used to
  821.      implement `file-precious-flag'; don't use it yourself unless you
  822.      really know what you're doing.
  823.  
  824.      Normally, `write-region' displays a message `Wrote file FILENAME'
  825.      in the echo area.  If VISIT is neither `t' nor `nil' nor a string,
  826.      then this message is inhibited.  This feature is useful for
  827.      programs that use files for internal purposes, files which the
  828.      user does not need to know about.
  829.  
  830. 
  831. File: elisp,  Node: File Locks,  Next: Information about Files,  Prev: Writing to Files,  Up: Files
  832.  
  833. File Locks
  834. ==========
  835.  
  836.    When two users edit the same file at the same time, they are likely
  837. to interfere with each other.  Emacs tries to prevent this situation
  838. from arising by recording a "file lock" when a file is being modified.
  839. Emacs can then detect the first attempt to modify a buffer visiting a
  840. file that is locked by another Emacs job, and ask the user what to do.
  841.  
  842.    File locks do not work properly when multiple machines can share
  843. file systems, such as with NFS.  Perhaps a better file locking system
  844. will be implemented in the future.  When file locks do not work, it is
  845. possible for two users to make changes simultaneously, but Emacs can
  846. still warn the user who saves second.  Also, the detection of
  847. modification of a buffer visiting a file changed on disk catches some
  848. cases of simultaneous editing; see *Note Modification Time::.
  849.  
  850.  - Function: file-locked-p FILENAME
  851.      This function returns `nil' if the file FILENAME is not locked by
  852.      this Emacs process.  It returns `t' if it is locked by this Emacs,
  853.      and it returns the name of the user who has locked it if it is
  854.      locked by someone else.
  855.  
  856.           (file-locked-p "foo")
  857.                => nil
  858.  
  859.  - Function: lock-buffer &optional FILENAME
  860.      This function locks the file FILENAME, if the current buffer is
  861.      modified.  The argument FILENAME defaults to the current buffer's
  862.      visited file.  Nothing is done if the current buffer is not
  863.      visiting a file, or is not modified.
  864.  
  865.  - Function: unlock-buffer
  866.      This function unlocks the file being visited in the current buffer,
  867.      if the buffer is modified.  If the buffer is not modified, then
  868.      the file should not be locked, so this function does nothing.  It
  869.      also does nothing if the current buffer is not visiting a file.
  870.  
  871.  - Function: ask-user-about-lock FILE OTHER-USER
  872.      This function is called when the user tries to modify FILE, but it
  873.      is locked by another user name OTHER-USER.  The value it returns
  874.      tells Emacs what to do next:
  875.  
  876.         * A value of `t' tells Emacs to grab the lock on the file.  Then
  877.           this user may edit the file and OTHER-USER loses the lock.
  878.  
  879.         * A value of `nil' tells Emacs to ignore the lock and let this
  880.           user edit the file anyway.
  881.  
  882.         * This function may instead signal a `file-locked' error, in
  883.           which case the change to the buffer which the user was about
  884.           to make does not take place.
  885.  
  886.           The error message for this error looks like this:
  887.  
  888.                error--> File is locked: FILE OTHER-USER
  889.  
  890.           where `file' is the name of the file and OTHER-USER is the
  891.           name of the user who has locked the file.
  892.  
  893.      The default definition of this function asks the user to choose
  894.      what to do.  If you wish, you can replace the `ask-user-about-lock'
  895.      function with your own version that decides in another way.  The
  896.      code for its usual definition is in `userlock.el'.
  897.  
  898. 
  899. File: elisp,  Node: Information about Files,  Next: Contents of Directories,  Prev: File Locks,  Up: Files
  900.  
  901. Information about Files
  902. =======================
  903.  
  904.    The functions described in this section are similar in as much as
  905. they all operate on strings which are interpreted as file names.  All
  906. have names that begin with the word `file'.  These functions all return
  907. information about actual files or directories, so their arguments must
  908. all exist as actual files or directories unless otherwise noted.
  909.  
  910.    Most of the file-oriented functions take a single argument,
  911. FILENAME, which must be a string.  The file name is expanded using
  912. `expand-file-name', so `~' is handled correctly, as are relative file
  913. names (including `../').  Environment variable substitutions, such as
  914. `$HOME', are not recognized by these functions.  *Note File Name
  915. Expansion::.
  916.  
  917. * Menu:
  918.  
  919. * Testing Accessibility::   Is a given file readable?  Writable?
  920. * Kinds of Files::          Is it a directory?  A symbolic link?
  921. * Truenames::            Eliminating symbolic links from a file name.
  922. * File Attributes::         How large is it?  Any other names?  Etc.
  923.  
  924. 
  925. File: elisp,  Node: Testing Accessibility,  Next: Kinds of Files,  Up: Information about Files
  926.  
  927. Testing Accessibility
  928. ---------------------
  929.  
  930.    These functions test for permission to access a file in specific
  931. ways.
  932.  
  933.  - Function: file-exists-p FILENAME
  934.      This function returns `t' if a file named FILENAME appears to
  935.      exist.  This does not mean you can necessarily read the file, only
  936.      that you can find out its attributes.  (On Unix, this is true if
  937.      the file exists and you have execute permission on the containing
  938.      directories, regardless of the protection of the file itself.)
  939.  
  940.      If the file does not exist, or if fascist access control policies
  941.      prevent you from finding the attributes of the file, this function
  942.      returns `nil'.
  943.  
  944.  - Function: file-readable-p FILENAME
  945.      This function returns `t' if a file named FILENAME exists and you
  946.      can read it.  It returns `nil' otherwise.
  947.  
  948.           (file-readable-p "files.texi")
  949.                => t
  950.           (file-exists-p "/usr/spool/mqueue")
  951.                => t
  952.           (file-readable-p "/usr/spool/mqueue")
  953.                => nil
  954.  
  955.  - Function: file-executable-p FILENAME
  956.      This function returns `t' if a file named FILENAME exists and you
  957.      can execute it.  It returns `nil' otherwise.  If the file is a
  958.      directory, execute permission means you can access files inside
  959.      the directory.
  960.  
  961.  - Function: file-writable-p FILENAME
  962.      This function returns `t' if FILENAME can be written or created by
  963.      you.  It is writable if the file exists and you can write it.  It
  964.      is creatable if the file does not exist, but the specified
  965.      directory does exist and you can write in that directory.
  966.      `file-writable-p' returns `nil' otherwise.
  967.  
  968.      In the third example below, `foo' is not writable because the
  969.      parent directory does not exist, even though the user could create
  970.      it.
  971.  
  972.           (file-writable-p "~rms/foo")
  973.                => t
  974.           (file-writable-p "/foo")
  975.                => nil
  976.           (file-writable-p "~rms/no-such-dir/foo")
  977.                => nil
  978.  
  979.  - Function: file-accessible-directory-p DIRNAME
  980.      This function returns `t' if you have permission to open existing
  981.      files in directory DIRNAME; otherwise (and if there is no such
  982.      directory), it returns `nil'.  The value of DIRNAME may be either
  983.      a directory name or the file name of a directory.
  984.  
  985.      Example: after the following,
  986.  
  987.           (file-accessible-directory-p "/foo")
  988.                => nil
  989.  
  990.      we can deduce that any attempt to read a file in `/foo/' will give
  991.      an error.
  992.  
  993.  - Function: file-newer-than-file-p FILENAME1 FILENAME2
  994.      This functions returns `t' if the file FILENAME1 is newer than
  995.      file FILENAME2.  If FILENAME1 does not exist, it returns `nil'.
  996.      If FILENAME2 does not exist, it returns `t'.
  997.  
  998.      You can use `file-attributes' to get a file's last modification
  999.      time as a list of two numbers.  *Note File Attributes::.
  1000.  
  1001.      In the following example, assume that the file `aug-19' was
  1002.      written on the 19th, and `aug-20' was written on the 20th.  The
  1003.      file `no-file' doesn't exist at all.
  1004.  
  1005.           (file-newer-than-file-p "aug-19" "aug-20")
  1006.                => nil
  1007.           (file-newer-than-file-p "aug-20" "aug-19")
  1008.                => t
  1009.           (file-newer-than-file-p "aug-19" "no-file")
  1010.                => t
  1011.           (file-newer-than-file-p "no-file" "aug-19")
  1012.                => nil
  1013.  
  1014. 
  1015. File: elisp,  Node: Kinds of Files,  Next: Truenames,  Prev: Testing Accessibility,  Up: Information about Files
  1016.  
  1017. Distinguishing Kinds of Files
  1018. -----------------------------
  1019.  
  1020.    This section describes how to distinguish directories and symbolic
  1021. links from ordinary files.
  1022.  
  1023.  - Function: file-symlink-p FILENAME
  1024.      If FILENAME is a symbolic link, the `file-symlink-p' function
  1025.      returns the file name to which it is linked.  This may be the name
  1026.      of a text file, a directory, or even another symbolic link, or of
  1027.      no file at all.
  1028.  
  1029.      If FILENAME is not a symbolic link (or there is no such file),
  1030.      `file-symlink-p' returns `nil'.
  1031.  
  1032.           (file-symlink-p "foo")
  1033.                => nil
  1034.           (file-symlink-p "sym-link")
  1035.                => "foo"
  1036.           (file-symlink-p "sym-link2")
  1037.                => "sym-link"
  1038.           (file-symlink-p "/bin")
  1039.                => "/pub/bin"
  1040.  
  1041.  
  1042.  - Function: file-directory-p FILENAME
  1043.      This function returns `t' if FILENAME is the name of an existing
  1044.      directory, `nil' otherwise.
  1045.  
  1046.           (file-directory-p "~rms")
  1047.                => t
  1048.           (file-directory-p "~rms/lewis/files.texi")
  1049.                => nil
  1050.           (file-directory-p "~rms/lewis/no-such-file")
  1051.                => nil
  1052.           (file-directory-p "$HOME")
  1053.                => nil
  1054.           (file-directory-p
  1055.            (substitute-in-file-name "$HOME"))
  1056.                => t
  1057.  
  1058. 
  1059. File: elisp,  Node: Truenames,  Next: File Attributes,  Prev: Kinds of Files,  Up: Information about Files
  1060.  
  1061. Truenames
  1062. ---------
  1063.  
  1064.    The "truename" of a file is the name that you get by following
  1065. symbolic links until none remain, then expanding to get rid of `.' and
  1066. `..' as components.  Strictly speaking, a file need not have a unique
  1067. truename; the number of distinct truenames a file has is equal to the
  1068. number of hard links to the file.  However, truenames are useful
  1069. because they eliminate symbolic links as a cause of name variation.
  1070.  
  1071.  - Function: file-truename FILENAME
  1072.      The function `file-truename' returns the true name of the file
  1073.      FILENAME.  This is the name that you get by following symbolic
  1074.      links until none remain.  The argument must be an absolute file
  1075.      name.
  1076.  
  1077.    *Note Buffer File Name::, for related information.
  1078.  
  1079.