Defining a Line Terminator in Emacs

I am writing a configuration file and I need to determine if the process is expecting a Windows file or a unix file. I have a copy of the expected file - is there a way to check if it uses \ n or \ r \ n without leaving emacs?

+4
source share
4 answers

If he says (DOS) about modeling when opening a file on Unix, the line ending is a Windows style. If it says (Unix) when you open the file on Windows, the line ending is a Unix style.

In the Emacs 22.2 manual (Node: Mode line):

If the spooled file uses carriage return with return, the colon changes either the backslash ('\') or '(DOS)', depending on the system. If the file uses only carriage return, the colon pointer changes to either a slash ('/') or '(Mac)'. On some systems, Emacs displays "(Unix)" instead of a colon for files that use the new line as a line separator.

Here is a function that - I think ndash; shows how to check from elisp what Emacs defines as a type of line endings. If it looks overly complex, perhaps it is.

(defun describe-eol () (interactive) (let ((eol-type (coding-system-eol-type buffer-file-coding-system))) (when (vectorp eol-type) (setq eol-type (coding-system-eol-type (aref eol-type 0)))) (message "Line endings are of type: %s" (case eol-type (0 "Unix") (1 "DOS") (2 "Mac") (t "Unknown"))))) 
+7
source

If you go into hex mode (Mx hexl-mode), you will see that line termination bytes are displayed.

+2
source

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

0
source

The following Elisp function will return nil if there are no terminators "\r\n" in the file (otherwise it returns the point of the first occurrence). You can put it in your .emacs and call it with Mx 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)) 
0
source

All Articles