1645 |
(mapc |
(mapc |
1646 |
(lambda (elt) |
(lambda (elt) |
1647 |
(cons (purecopy (car elt)) (cdr elt))) |
(cons (purecopy (car elt)) (cdr elt))) |
1648 |
'(("\\.te?xt\\'" . text-mode) |
'(;; do this first, so that .html.pl is Polish html, not Perl |
1649 |
|
("\\.s?html?\\(\\.[a-zA-Z_]+\\)?\\'" . html-mode) |
1650 |
|
("\\.te?xt\\'" . text-mode) |
1651 |
("\\.[tT]e[xX]\\'" . tex-mode) |
("\\.[tT]e[xX]\\'" . tex-mode) |
1652 |
("\\.ins\\'" . tex-mode) ;Installation files for TeX packages. |
("\\.ins\\'" . tex-mode) ;Installation files for TeX packages. |
1653 |
("\\.ltx\\'" . latex-mode) |
("\\.ltx\\'" . latex-mode) |
1663 |
("\\.ad[abs]\\'" . ada-mode) |
("\\.ad[abs]\\'" . ada-mode) |
1664 |
("\\.ad[bs].dg\\'" . ada-mode) |
("\\.ad[bs].dg\\'" . ada-mode) |
1665 |
("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode) |
("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode) |
|
("\\.s?html?\\'" . html-mode) |
|
1666 |
("\\.mk\\'" . makefile-mode) |
("\\.mk\\'" . makefile-mode) |
1667 |
("\\([Mm]\\|GNUm\\)akep*file\\'" . makefile-mode) |
("\\([Mm]\\|GNUm\\)akep*file\\'" . makefile-mode) |
1668 |
("\\.am\\'" . makefile-mode) ;For Automake. |
("\\.am\\'" . makefile-mode) ;For Automake. |
1690 |
("\\.bib\\'" . bibtex-mode) |
("\\.bib\\'" . bibtex-mode) |
1691 |
("\\.sql\\'" . sql-mode) |
("\\.sql\\'" . sql-mode) |
1692 |
("\\.m[4c]\\'" . m4-mode) |
("\\.m[4c]\\'" . m4-mode) |
1693 |
("\\.m[fp]\\'" . metapost-mode) |
("\\.mf\\'" . metafont-mode) |
1694 |
|
("\\.mp\\'" . metapost-mode) |
1695 |
("\\.vhdl?\\'" . vhdl-mode) |
("\\.vhdl?\\'" . vhdl-mode) |
1696 |
("\\.article\\'" . text-mode) |
("\\.article\\'" . text-mode) |
1697 |
("\\.letter\\'" . text-mode) |
("\\.letter\\'" . text-mode) |
1843 |
the file with one of the modes in this list, that mode will be |
the file with one of the modes in this list, that mode will be |
1844 |
used. Else `xml-mode' or `sgml-mode' is used.") |
used. Else `xml-mode' or `sgml-mode' is used.") |
1845 |
|
|
1846 |
(defun set-auto-mode (&optional just-from-file-name) |
(defun set-auto-mode (&optional keep-mode-if-same) |
1847 |
"Select major mode appropriate for current buffer. |
"Select major mode appropriate for current buffer. |
1848 |
This checks for a -*- mode tag in the buffer's text, checks the |
This checks for a -*- mode tag in the buffer's text, checks the |
1849 |
interpreter that runs this file against `interpreter-mode-alist', |
interpreter that runs this file against `interpreter-mode-alist', |
1857 |
If `enable-local-variables' is nil, this function does not check for a |
If `enable-local-variables' is nil, this function does not check for a |
1858 |
-*- mode tag. |
-*- mode tag. |
1859 |
|
|
1860 |
If the optional argument JUST-FROM-FILE-NAME is non-nil, |
If the optional argument KEEP-MODE-IF-SAME is non-nil, |
1861 |
then we do not set anything but the major mode, |
then we do not set anything but the major mode, |
1862 |
and we don't even do that unless it would come from the file name." |
and we don't even do that unless it would come from the file name." |
1863 |
;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- |
;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- |
1864 |
(let (end done mode modes xml) |
(let (end done mode modes xml) |
1865 |
(unless just-from-file-name |
;; Find a -*- mode tag |
1866 |
;; Find a -*- mode tag |
(save-excursion |
1867 |
(save-excursion |
(goto-char (point-min)) |
1868 |
(goto-char (point-min)) |
(skip-chars-forward " \t\n") |
1869 |
(skip-chars-forward " \t\n") |
;; While we're at this point, check xml for later. |
1870 |
;; While we're at this point, check xml for later. |
(setq xml (looking-at "<\\?xml \\|<!DOCTYPE")) |
1871 |
(setq xml (looking-at "<\\?xml \\|<!DOCTYPE")) |
(and enable-local-variables |
1872 |
(and enable-local-variables |
(setq end (set-auto-mode-1)) |
1873 |
(setq end (set-auto-mode-1)) |
(if (save-excursion (search-forward ":" end t)) |
1874 |
(if (save-excursion (search-forward ":" end t)) |
;; Find all specifications for the `mode:' variable |
1875 |
;; Find all specifications for the `mode:' variable |
;; and execute them left to right. |
1876 |
;; and execute them left to right. |
(while (let ((case-fold-search t)) |
1877 |
(while (let ((case-fold-search t)) |
(or (and (looking-at "mode:") |
1878 |
(or (and (looking-at "mode:") |
(goto-char (match-end 0))) |
1879 |
(goto-char (match-end 0))) |
(re-search-forward "[ \t;]mode:" end t))) |
1880 |
(re-search-forward "[ \t;]mode:" end t))) |
(skip-chars-forward " \t") |
1881 |
(skip-chars-forward " \t") |
(let ((beg (point))) |
1882 |
(let ((beg (point))) |
(if (search-forward ";" end t) |
1883 |
(if (search-forward ";" end t) |
(forward-char -1) |
1884 |
(forward-char -1) |
(goto-char end)) |
1885 |
(goto-char end)) |
(skip-chars-backward " \t") |
1886 |
(skip-chars-backward " \t") |
(push (intern (concat (downcase (buffer-substring beg (point))) "-mode")) |
1887 |
(push (intern (concat (downcase (buffer-substring beg (point))) "-mode")) |
modes))) |
1888 |
modes))) |
;; Simple -*-MODE-*- case. |
1889 |
;; Simple -*-MODE-*- case. |
(push (intern (concat (downcase (buffer-substring (point) end)) |
1890 |
(push (intern (concat (downcase (buffer-substring (point) end)) |
"-mode")) |
1891 |
"-mode")) |
modes)))) |
1892 |
modes)))) |
;; If we found modes to use, invoke them now, outside the save-excursion. |
1893 |
;; If we found modes to use, invoke them now, outside the save-excursion. |
(if modes |
1894 |
(if modes |
(dolist (mode (nreverse modes)) |
1895 |
(dolist (mode (nreverse modes)) |
(if (not (functionp mode)) |
1896 |
(if (not (functionp mode)) |
(message "Ignoring unknown mode `%s'" mode) |
1897 |
(message "Ignoring unknown mode `%s'" mode) |
(setq done t) |
1898 |
(setq done t) |
(unless (if keep-mode-if-same (eq mode major-mode)) |
1899 |
(funcall mode))) |
(funcall mode)))) |
1900 |
;; If we didn't, look for an interpreter specified in the first line. |
;; If we didn't, look for an interpreter specified in the first line. |
1901 |
;; As a special case, allow for things like "#!/bin/env perl", which |
;; As a special case, allow for things like "#!/bin/env perl", which |
1902 |
;; finds the interpreter anywhere in $PATH. |
;; finds the interpreter anywhere in $PATH. |
1903 |
(setq mode (save-excursion |
(setq mode (save-excursion |
1904 |
(goto-char (point-min)) |
(goto-char (point-min)) |
1905 |
(if (looking-at auto-mode-interpreter-regexp) |
(if (looking-at auto-mode-interpreter-regexp) |
1906 |
(match-string 2) |
(match-string 2) |
1907 |
"")) |
"")) |
1908 |
;; Map interpreter name to a mode, signalling we're done at the |
;; Map interpreter name to a mode, signalling we're done at the |
1909 |
;; same time. |
;; same time. |
1910 |
done (assoc (file-name-nondirectory mode) |
done (assoc (file-name-nondirectory mode) |
1911 |
interpreter-mode-alist)) |
interpreter-mode-alist)) |
1912 |
;; If we found an interpreter mode to use, invoke it now. |
;; If we found an interpreter mode to use, invoke it now. |
1913 |
(if done (funcall (cdr done))))) |
(and done |
1914 |
|
(not (if keep-mode-if-same (eq mode major-mode))) |
1915 |
|
(funcall (cdr done)))) |
1916 |
(if (and (not done) buffer-file-name) |
(if (and (not done) buffer-file-name) |
1917 |
(let ((name buffer-file-name)) |
(let ((name buffer-file-name)) |
1918 |
;; Remove backup-suffixes from file name. |
;; Remove backup-suffixes from file name. |
1923 |
(memq system-type '(vax-vms windows-nt cygwin)))) |
(memq system-type '(vax-vms windows-nt cygwin)))) |
1924 |
(if (and (setq mode (assoc-default name auto-mode-alist |
(if (and (setq mode (assoc-default name auto-mode-alist |
1925 |
'string-match)) |
'string-match)) |
|
(setq done t) |
|
1926 |
(consp mode) |
(consp mode) |
1927 |
(cadr mode)) |
(cadr mode)) |
1928 |
(setq mode (car mode) |
(setq mode (car mode) |
1929 |
name (substring name 0 (match-beginning 0))) |
name (substring name 0 (match-beginning 0))) |
1930 |
(setq name))) |
(setq name))) |
1931 |
(if mode |
(when mode |
1932 |
;; When JUST-FROM-FILE-NAME is set, we are working on behalf |
(if xml (or (memq mode xml-based-modes) |
1933 |
;; of set-visited-file-name. In that case, if the major mode |
(setq mode 'sgml-mode))) ; alias to xml-mode for `eq' |
1934 |
;; specified is the same one we already have, don't actually |
;; When KEEP-MODE-IF-SAME is set, we are working on behalf of |
1935 |
;; reset it. We don't want to lose minor modes such as Font |
;; set-visited-file-name. In that case, if the major mode |
1936 |
;; Lock. |
;; specified is the same one we already have, don't actually |
1937 |
(unless (and just-from-file-name (eq mode major-mode)) |
;; reset it. We don't want to lose minor modes such as Font |
1938 |
(if (if xml (memq mode xml-based-modes) t) |
;; Lock. |
1939 |
(funcall mode) |
(unless (if keep-mode-if-same (eq mode major-mode)) |
1940 |
(xml-mode))))))) |
(funcall mode)) |
1941 |
|
(setq done t))))) |
1942 |
(and (not done) |
(and (not done) |
1943 |
xml |
xml |
1944 |
|
(not (if keep-mode-if-same (eq 'sgml-mode major-mode))) |
1945 |
(xml-mode)))) |
(xml-mode)))) |
1946 |
|
|
1947 |
|
|