eintr: Find a File

 
 14.5 Find a File
 ================
 
 To find a file in Emacs, you use the ‘C-x C-f’ (‘find-file’) command.
 This command is almost, but not quite right for the lengths problem.
 
    Let’s look at the source for ‘find-file’:
 
      (defun find-file (filename)
        "Edit file FILENAME.
      Switch to a buffer visiting file FILENAME,
      creating one if none already exists."
        (interactive "FFind file: ")
        (switch-to-buffer (find-file-noselect filename)))
 
 (The most recent version of the ‘find-file’ function definition permits
 you to specify optional wildcards to visit multiple files; that makes
 the definition more complex and we will not discuss it here, since it is
 not relevant.  You can see its source using either ‘M-.’ (‘find-tag’) or
 ‘C-h f’ (‘describe-function’).)
 
    The definition I am showing possesses short but complete
 documentation and an interactive specification that prompts you for a
 file name when you use the command interactively.  The body of the
 definition contains two functions, ‘find-file-noselect’ and
 ‘switch-to-buffer’.
 
    According to its documentation as shown by ‘C-h f’ (the
 ‘describe-function’ command), the ‘find-file-noselect’ function reads
 the named file into a buffer and returns the buffer.  (Its most recent
 version includes an optional WILDCARDS argument, too, as well as another
 to read a file literally and an other you suppress warning messages.
 These optional arguments are irrelevant.)
 
    However, the ‘find-file-noselect’ function does not select the buffer
 in which it puts the file.  Emacs does not switch its attention (or
 yours if you are using ‘find-file-noselect’) to the selected buffer.
 That is what ‘switch-to-buffer’ does: it switches the buffer to which
 Emacs attention is directed; and it switches the buffer displayed in the
 window to the new buffer.  We have discussed buffer switching elsewhere.
 (SeeSwitching Buffers.)
 
    In this histogram project, we do not need to display each file on the
 screen as the program determines the length of each definition within
 it.  Instead of employing ‘switch-to-buffer’, we can work with
 ‘set-buffer’, which redirects the attention of the computer program to a
 different buffer but does not redisplay it on the screen.  So instead of
 calling on ‘find-file’ to do the job, we must write our own expression.
 
    The task is easy: use ‘find-file-noselect’ and ‘set-buffer’.