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 @@
C-h v magic-mode-alist
says this:
magic-mode-alist
is a variable defined infiles.el
. Its value isnil
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, ifREGEXP
matches the text at the beginning of the buffer, or callingMATCH-FUNCTION
returns non-nil
,normal-mode
will callFUNCTION
rather than allowingauto-mode-alist
to decide the buffer's major mode.If
FUNCTION
isnil
, then it is not called. (That is a way of saying "allowauto-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 infiles.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 thanauto-mode-alist
. Each element looks like(REGEXP . FUNCTION)
or(MATCH-FUNCTION . FUNCTION)
. After visiting a file, ifREGEXP
matches the text at the beginning of the buffer, or callingMATCH-FUNCTION
returns non-nil
,normal-mode
will callFUNCTION
, provided thatmagic-mode-alist
andauto-mode-alist
have not specified a mode for this file.If
FUNCTION
isnil
, 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.