Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do EMACS Lisp programmers read text files for non-editing purposes?

What do EMACS Lisp programmers do, when they want to write something roughly the equivalent of...

for line in open("foo.txt", "r", encoding="utf-8").readlines():
    ...(split on ws and call a fn, or whatever)...

..?

When I look in the EMACS lisp help, I see functions about opening files into text editing buffers -- not exactly what I was intending. I suppose I could write functions to visit the lines of the file, but if I did that, I wouldn't want the user to see it, and besides, it doesn't seem very efficient from a text-processing standpoint.

like image 262
LionKimbro Avatar asked May 29 '15 17:05

LionKimbro


1 Answers

I think a more direct translation of the original Python code is as follows:

(with-temp-buffer
  (insert-file-contents "foo.txt")
  (while (search-forward-regexp "\\(.*\\)\n?" nil t)
    ; do something with this line in (match-string 1)
    ))

I think with-temp-buffer/insert-file-contents is generally preferable to with-current-buffer/find-file-noselect, because the former guarantees that you're working with a fresh copy of the entire file contents. With the latter construction, if you happen to already have a buffer visiting the target file, then that buffer is returned by find-file-noselect, so if that buffer has been narrowed, you'll only see that part of the file when you process it.

Keep in mind that it may very well be more convenient not to process the file line-by-line. For example, this is an expression that returns a list of all sequences of consecutive digits in the file:

(with-temp-buffer
  (insert-file-contents "foo.txt")
  (loop while (search-forward-regexp "[0-9]+" nil t)
        collect (match-string 0)))

(require 'cl) first to bring in the loop macro.

like image 86
Sean Avatar answered Oct 24 '22 10:10

Sean