[Zope-dev] Re: ZMI / JavaScript brainstorm
Luca Manini
manini@flashnet.it
Thu, 18 Apr 2002 20:39:07 +0200
>>>>> "Chris" == Chris Withers <chrisw@nipltd.com> writes:
> Well, is there any way I can easily tell emacs:
> - If a file starts with <html> or <!DOCTYPE, then go into
> html-mode
> - If a file starts with #, then go into python mode?
Asking "is it possibile in Emacs..." ?
You are joking!
I did my last lisp programming 10 years ago, but thanks to the
power of (e)lisp and the wonderful documentation of Emacs I came
up with the following stuff, that you can put in your init file
(.emacs)
;;; hook to change a buffer mode depending on the first buffer's chars.
(defun foo () "foo fun"
(interactive "") ; so you can do M-x foo to test it
(save-excursion ; come back to current point after execution
(goto-char 0) ; go to buffer start
(cond ((looking-at "##") ; are we matching pattern ?
(python-mode)) ; set mode!
((looking-at "<html>")
(html-mode)))))
(remove-hook 'find-file-hooks 'foo) ; remove old hook (if any)
(add-hook 'find-file-hooks 'foo t) ; install this one (at the end)
It works like this:
1) whenever Emacs loads a file, it calls (at the end) the
functions listed (registered) in find-file-hooks,
2) we register (add-hook) our function (foo) at the end (last
param) of that list,
3) foo is defined with defun ..., it has no params (()), its doc
string is "foo fun", and can be called interactively (not
really needed),
4) foo is very simple, "cond" is the lisp version of a "case",
looking-at returns true if text after point matches regular the
given regexp adn in that case, "cond" evaluate the expression
that follows.
That's all. It is really an hack, but it works for simple
cases. If you need something "a little" more elaborate let me
know. For a "real solution" aks the same question on some
emacs newsgroups.
Regards, Luca.