help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to set outline-level in the first line?


From: Stefan Monnier
Subject: Re: How to set outline-level in the first line?
Date: Fri, 11 May 2007 01:08:47 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (gnu/linux)

> some days ago I sent the message below to the list.  Until now, I did not
> receive any answer.
> I assume, this means, it is not possible to set outline-level as local
> variable in the first line (except by eval:).
> Could one of you experts confirm this opinion? I would be happy about some
> yes/no answer.

It's definitely possible.  But since it's dangerous (in case the file comes
from someone you don't trust), Emacs should try to make it a bit harder.

>> The outline-level should be:
>> (setq outline-level (defun outline-level ()
>> "adjust outline-level to R-comments"  
>> (interactive)
>> (cond ((looking-at "#\\{5\\} ") 1)
>> ((looking-at "#### ") 2)
>> ((looking-at "### ") 3)
>> ((looking-at "## ") 4)  
>> (t 1000))))

This can't be right.  `defun' is a command which is used for its
side-effect, not its return value.  Use:

(setq outline-level (lambda ()
                      "Adjust outline-level to R-comments."
                      (interactive)
                      (cond ((looking-at "#\\{5\\} ") 1)
                            ((looking-at "#### ") 2)
                            ((looking-at "### ") 3)
                            ((looking-at "## ") 4)
                            (t 1000))))

instead, or

(defun my-R-outline-level ()
  "Adjust outline-level to R-comments."
  (interactive)
  (cond ((looking-at "#\\{5\\} ") 1)
        ((looking-at "#### ") 2)
        ((looking-at "### ") 3)
        ((looking-at "## ") 4)
        (t 1000))))

(setq outline-level 'my-R-outline-level)


>> I tried to do this in the first line by something like
>> outline-level: (defun outline-level () (interactive) (cond ((looking-at
>> "##### ") 1)((looking-at "#### ") 2)((looking-at "### ") 3)((looking-at "##
>> ") 4)  (t 1000)))
>> but I did not find the right way.

The expression after the : should be a *value*, not an expression: it will
not be evaluated but just directly assigned to the variable.
I.e.

       foo-bar: (+ 3 4)

will not set `foo-bar' to 7 but to the 3-element list containing the
symbol + and the integers 3 and 4.  Luckily (lambda () ...) *is* a value, so

 outline-level: (lambda () (interactive) (cond ((looking-at
 "##### ") 1)((looking-at "#### ") 2)((looking-at "### ") 3)((looking-at "##
 ") 4)  (t 1000)))

might work (provided you add the necessary -*- around, of course.

Another option might be to change your R mode with

   (add-hook 'R-mode-hook 'my-R-outline-level)


-- Stefan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]