views:

674

answers:

4

I'm writing a config file and I need to define if the process expects a windows format file or a unix format file. I've got a copy of the expected file - is there a way I can check if it uses \n or \r\n without exiting emacs?

A: 

Open the file in emacs using find-file-literally. If lines have ^M symbols at the end, it expects a windows format text file.

Chris
+2  A: 
Jouni K. Seppänen
This assumes you're not running on Windows. There, the modeline would be \ for \r\n or (Unix) for \n.
cjm
Surely the OP wants a programmatic solution and not a visual inspection of the modeline?
Chris Conway
Nope! wanted a visual one, although the elsip is nice to have.
kanja
A: 

The following Elisp function will return nil if no "\r\n" terminators appear in a file (otherwise it returns the point of the first occurrence). You can put it in your .emacs and call it with M-x check-eol.

(defun check-eol (FILE)
  (interactive "fFile: ")
  (set-buffer (generate-new-buffer "*check-eol*"))
  (insert-file-contents-literally FILE)
  (let ((point (search-forward "\r\n")))
    (kill-buffer nil)
    point))
Chris Conway
But modern versions of Emacs convert CRLF to LF when loading (and back when saving), so you won't have any \r characters in your buffer. You need to check the buffer's coding system.
cjm
Won't this work in combination with find-file-literally?
Chris Conway
Yes, it will, but you didn't mention using find-file-literally first.
cjm
A: 

If you go in hexl-mode (M-x hexl-mode), you shoul see the line termination bytes.

stephanea