1

How do I make Emacs automatically turn on diff-mode for any file with diff-looking content, eg.

--- a.txt
+++ b.txt
@@ -1234,56 +1234,56 @@

1 Answer 1

3

C-h v magic-mode-alist says this:

magic-mode-alist is a variable defined in files.el. Its value is nil

This variable can be risky when used as a file-local variable.

Documentation:

Alist of buffer beginnings vs. corresponding major mode functions. Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION). After visiting a file, if REGEXP matches the text at the beginning of the buffer, or calling MATCH-FUNCTION returns non-nil, normal-mode will call FUNCTION rather than allowing auto-mode-alist to decide the buffer's major mode.

If FUNCTION is nil, then it is not called. (That is a way of saying "allow auto-mode-alist to decide for these files.")

So either use a REGEXP that distinguishes the beginning of the buffer text or use MATCH-FUNCTION that does so.

See also C-h v magic-fallback-mode-alist:

magic-fallback-mode-alist is a variable defined in files.el. Its value is shown below.

This variable can be risky when used as a file-local variable.

Documentation:

Like magic-mode-alist but has lower priority than auto-mode-alist. Each element looks like (REGEXP . FUNCTION) or (MATCH-FUNCTION . FUNCTION). After visiting a file, if REGEXP matches the text at the beginning of the buffer, or calling MATCH-FUNCTION returns non-nil, normal-mode will call FUNCTION, provided that magic-mode-alist and auto-mode-alist have not specified a mode for this file.

If FUNCTION is nil, then it is not called.

Value:

  ((image-type-auto-detected-p . image-mode)
     ("\\(PK00\\)?[P]K" . archive-mode)
     ("\\(?:<\\?xml[    
       \n]+[^>]*>\\)?[

       \n]*<\\(?:!--\\(?:[^-]\\|-[^-]\\)*-->[

       \n]*<\\)*\\(?:!DOCTYPE[    
       \n]+[^>]*>[  
       \n]*<[

       \n]*\\(?:!--\\(?:[^-]\\|-[^-]\\)*-->[

       \n]*<\\)*\\)?[Hh][Tt][Mm][Ll]" . mhtml-mode)
     ("<!DOCTYPE[

       \n]+[Hh][Tt][Mm][Ll]" . mhtml-mode)
     ("<\\?xml " . xml-mode)
     ("[

       \n]*<\\(?:!--\\(?:[^-]\\|-[^-]\\)*-->[     
       \n]*<\\)*!DOCTYPE " . sgml-mode)
     ("%!PS" . ps-mode)
     ("# xmcd " . conf-unix-mode))

See also the Emacs manual, node Choosing Modes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.