From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kin Cho Newsgroups: gmane.emacs.help Subject: Re: Suggestions? Better filetype sniffing -- XHTML vs. HTML Date: 24 Feb 2004 08:47:18 -0800 Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <7i8yisfmqx.fsf@neoscale.com> References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1077642121 11008 80.91.224.253 (24 Feb 2004 17:02:01 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 24 Feb 2004 17:02:01 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Feb 24 18:01:48 2004 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AvfwJ-0002dT-00 for ; Tue, 24 Feb 2004 18:01:47 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1AvfvV-0001L9-75 for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Feb 2004 12:00:57 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!12.24.46.85!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 91 Original-NNTP-Posting-Host: 12.24.46.85 Original-X-Trace: news.uni-berlin.de 1077641239 52653319 I 12.24.46.85 ([151019]) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 Original-Xref: shelby.stanford.edu gnu.emacs.help:121196 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:17148 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:17148 (add-hook 'find-file-hooks 'my-find-file-hooks t) (defun my-find-file-hooks () (when (save-excursion (search-forward-regexp "\?xml\\|XHTML" 80 t)) ;; do whatever you need to do )) -kin "D. D. Brierton" writes: > I'd like to be able to have emacs autodetect whether a file is an HTML > file or an XHTML file. Standardly, file extension is not enough for this > as HTML and XHTML files tend to have the same file extensions. I have to > edit a lot of files created by other people, and they are often hopelessly > invalid, so I have no hope of perfectly differentiating XHTML from HTML. > However, there are some good clues to go on: > > If a file ends in one of the following: > > \.inc$ > \.php[34]?$ > \.[sjp]?html?$ > > Then (in my case) it is *either* HTML *or* XHTML. > > If a file with one of the above extensions has very near the beginning one > or both of: > > > then it is XHTML. Otherwise it is probably just HTML. > > I know how to (add-to-list 'auto-mode-alist ... the file extensions, but I > don't know how to also check the first few lines of the file. Can anyone > offer any suggestions? > > Further details: > > I use psgml, and I define two derived modes: > > (define-derived-mode xml-html-mode xml-mode "XHTML" > "This version of html mode is just a wrapper around xml mode." > (make-local-variable 'sgml-declaration) > (make-local-variable 'sgml-default-doctype-name) > (setq > sgml-default-doctype-name "html" > sgml-declaration "/usr/share/sgml/xml.dcl" > sgml-always-quote-attributes t > sgml-indent-step 2 > sgml-indent-data t > sgml-minimize-attributes nil > sgml-omittag nil > sgml-shorttag nil > ) > ) > > (define-derived-mode sgml-html-mode sgml-mode "HTML" > "This version of html mode is just a wrapper around sgml mode." > (make-local-variable 'sgml-declaration) > (make-local-variable 'sgml-default-doctype-name) > (setq > sgml-default-doctype-name "html" > sgml-declaration "~/lib/DTD/html401/HTML4.decl" > sgml-always-quote-attributes t > sgml-indent-step 2 > sgml-indent-data t > sgml-minimize-attributes nil > sgml-omittag nil > sgml-shorttag nil > ) > ) > > I also have the following: > > ; What files to invoke the new html-mode for? > (add-to-list 'auto-mode-alist '("\\.inc\\'" . sgml-html-mode)) > (add-to-list 'auto-mode-alist '("\\.php[34]?\\'" . sgml-html-mode)) > (add-to-list 'auto-mode-alist '("\\.[sj]?html?\\'" . sgml-html-mode)) > > So that basically I end up in sgml-html-mode when I open an (X)HTML file, > and then if it is an XHTML file I have to manually M-x xml-html-mode. > > TIA, Darren > > -- > ====================================================================== > D. D. Brierton darren@dzr-web.com www.dzr-web.com > Trying is the first step towards failure (Homer Simpson) > ======================================================================