Go to the first, previous, next, last section, table of contents.

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 (you can use the find-tag command to find the source of a function):

(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 definition 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. However, the buffer is not selected. Emacs does not switch its attention (or yours if you are using find-file-noselect) to the named 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. (See section Switching 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.

Go to the first, previous, next, last section, table of contents.