[comp.emacs] text-mode-hook

Damon.Lipparelli@CS.CMU.EDU (04/09/90)

Hello.  I'm having trouble with the text-mode-hook feature.  I'd like to
set things so that when I enter text mode, I'm automatically put into
auto-fill mode as well.  I tried setting the variable text-mode-hook to
be auto-fill-mode (ie, "(setq text-mode-hook 'auto-fill-mode)"), but I
get the error "Wrong number of arguments" and then a lambda expression. 
I assume that this means that auto-fill-mode is expecting arguments (as
documented it can take one), but I can just type auto-fill-mode to the
mode-line and it toggles auto-fill.  What am I missing?

Thanks in advance,
Damon Lipparelli
(lipp@cs.cmu.edu)

hopkins@gmuvax2.gmu.edu (Matt hopkins) (04/09/90)

In article <sa7sr8K00hMN1MQG0R@cs.cmu.edu> Damon.Lipparelli@CS.CMU.EDU writes:
>Hello.  I'm having trouble with the text-mode-hook feature.  I'd like to
>set things so that when I enter text mode, I'm automatically put into
>auto-fill mode as well.  I tried setting the variable text-mode-hook to
>be auto-fill-mode (ie, "(setq text-mode-hook 'auto-fill-mode)"), but I
>Damon Lipparelli
>(lipp@cs.cmu.edu)

Well, the way I do it is to (setq text-mode-hook 'turn-on-auto-fill)
And then a M-X text-mode so emacs will use it.

Hope this works.

Matt
-- 
 "Do you hate people?"  "No ... but I seem to feel      Matt Hopkins  
 better when they're not around."  - Barfly             George Mason University
 "Six double five three two one." - A Clockwork Orange  Fairfax, Virginia
			   hopkins@gmuvax2.gmu.edu

jr@bbn.com (John Robinson) (04/10/90)

In article <sa7sr8K00hMN1MQG0R@cs.cmu.edu>, Damon.Lipparelli@CS writes:
>Hello.  I'm having trouble with the text-mode-hook feature.  I'd like to
>set things so that when I enter text mode, I'm automatically put into
>auto-fill mode as well.  I tried setting the variable text-mode-hook to
>be auto-fill-mode (ie, "(setq text-mode-hook 'auto-fill-mode)"), but I
>get the error "Wrong number of arguments" and then a lambda expression. 
>I assume that this means that auto-fill-mode is expecting arguments (as
>documented it can take one), but I can just type auto-fill-mode to the
>mode-line and it toggles auto-fill.  What am I missing?

Functions called interactively (i.e. with M-x, or by being bound to a
key you type) can take their arguments in a number of ways, and may
default them.  All this is explained in the documentation for the
"function" called interactive.  When they're called from a program,
however, the lisp interpreter has to match arguments and formals.  The
sepecification for the function auto-fill-mode is:

  (interactive "P")

which means "pick up the prefix argument, if any, in raw form".  When
there is no prefix argument, the argument to auto-fill-mode defaults
to nil.  One could plausibly argue that the argument to auto-fill-mode
ought to be &optional, but it ain't.  Fortunately, emacs provides you
with [in simple.el]:

  (defun turn-on-auto-fill ()
    "Unconditionally turn on Auto Fill mode."
    (auto-fill-mode 1))

which serves just fine for your text-mode-hook (and whatever else).
--
/jr, nee John Robinson     Life did not take over the globe by combat,
jr@bbn.com or bbn!jr          but by networking -- Lynn Margulis

pd@ixi.uucp (Paul Davey) (04/10/90)

In article <sa7sr8K00hMN1MQG0R@cs.cmu.edu> Damon.Lipparelli@CS.CMU.EDU writes:

   Hello.  I'm having trouble with the text-mode-hook feature.  I'd like to
   set things so that when I enter text mode, I'm automatically put into
   auto-fill mode as well.  I tried setting the variable text-mode-hook to
   be auto-fill-mode (ie, "(setq text-mode-hook 'auto-fill-mode)"), but I
   get the error "Wrong number of arguments" and then a lambda expression. 
   I assume that this means that auto-fill-mode is expecting arguments (as
   documented it can take one), but I can just type auto-fill-mode to the
   mode-line and it toggles auto-fill.  What am I missing?

   Thanks in advance,
   Damon Lipparelli
   (lipp@cs.cmu.edu)


I had some trouble working this out when I first started customising
emacs. One way is to use

(setq text-mode-hook 'turn-on-auto-fill)

but when I wanted abbrev mode in text as well I changed it to

(setq text-mode-hook '(lambda () (abbrev-mode 1) (auto-fill-mode 1)))

--

 Regards,			paul@ixi.uucp           IXI Limited          
	 Paul Davey		...!uunet!ixi!paul      62-74 Burleigh Street
				+44 224 462 132 (fax)   Cambridge  U.K.      
 "These are interesting times"	+44 223 462 131 (vox)   CB1  1OJ             

-- 

 Regards,			paul@ixi.uucp           IXI Limited          
	 Paul Davey		...!uunet!ixi!paul      62-74 Burleigh Street
				+44 224 462 132 (fax)   Cambridge  U.K.      

jr@bbn.com (John Robinson) (04/11/90)

In article <PD.90Apr10155710@rutland.ixi.uucp>, pd@ixi (Paul Davey) writes:
>but when I wanted abbrev mode in text as well I changed it to
>
> (setq text-mode-hook '(lambda () (abbrev-mode 1) (auto-fill-mode 1)))

A hook can be a list of functions.  Thus the above could be rewritten:

  (setq text-mode-hook '((lambda () (abbrev-mode 1)) (turn-on-auto-fill-mode)))

Pretty pointless in this case, but this lets you add to an existing
hook list if that's what you'd like to do:

  (setq text-mode-hook (cons text-mode-hook '(turn-on-auto-fill)))
...
  (setq text-mode-hook (cons text-mode-hook '((lambda () (abbrev-mode 1)))))

See the definition of run-hooks.
--
/jr, nee John Robinson     Life did not take over the globe by combat,
jr@bbn.com or bbn!jr          but by networking -- Lynn Margulis

weiner@novavax.UUCP (Bob &) (04/12/90)

pd@ixi.uucp (Paul Davey) writes:
> 
> In article <sa7sr8K00hMN1MQG0R@cs.cmu.edu> Damon.Lipparelli@CS.CMU.EDU writes:
>    Hello.  I'm having trouble with the text-mode-hook feature.  I'd like to
>    set things so that when I enter text mode, I'm automatically put into
>    auto-fill mode as well.  I tried setting the variable text-mode-hook to
>    be auto-fill-mode (ie, "(setq text-mode-hook 'auto-fill-mode)"), but I
>    get the error "Wrong number of arguments" and then a lambda expression. 
> 
> I had some trouble working this out when I first started customising
> emacs. One way is to use
> 
> (setq text-mode-hook 'turn-on-auto-fill)
> 
> but when I wanted abbrev mode in text as well I changed it to
> 
> (setq text-mode-hook '(lambda () (abbrev-mode 1) (auto-fill-mode 1)))
> 

A better solution to the above problem is one we have used on a heavily
site customized version of GNU Emacs for two years.  Emacs init files
seldom have any knowledge of whether another init file has already set a
hook variable to some value and they typically don't want to test for
this either.  Overwriting such a variable is a bad idea since one
typically wants to add functionality to the hook, not replace it.  The
best tactic is then to simply append to the value of a hook variable
when you want to change its behavior in an init file.

Here is the needed Elisp:

;; Bob Weiner, Motorola Inc., 4/14/88
;;
;; Function used anytime a hook variable value needs to be initially set
;; or further customized.
;;
(defun append-to-var (var-symbol-name list-to-add)
  "If VAR-SYMBOL-NAME is bound, append LIST-TO-ADD (a list) to value of
variable (a list) given by var-symbol-name.  If unbound, variable is set
to list-to-add.  Often used to append to 'hook' variables."
  (set-variable var-symbol-name
		(append (and (boundp var-symbol-name)
			     (symbol-value var-symbol-name))
			list-to-add)))

;;
;; Examples
;;

;; Default to Auto-fill when in text mode
(append-to-var 'text-mode-hook '(turn-on-auto-fill))

;; Add personal behavior to rmail hook
(append-to-var 'rmail-mode-hook '(rmail-personal-init))
(defun rmail-personal-init ()
  "Function to run when rmail-mode is invoked."
  <body>)
--
Bob Weiner, Motorola, Inc.,   Usenet:   ...!gatech!uflorida!novavax!weiner
			      Internet: novavax!weiner@uunet.uu.net
(407) 364-2087

jr@bbn.com (John Robinson) (04/12/90)

In article <54725@bbn.COM>, jr@bbn (John Robinson) writes:
>A hook can be a list of functions. ...
>
>  (setq text-mode-hook '((lambda () (abbrev-mode 1)) (turn-on-auto-fill-mode)))

Well, that had two problems [assuming 18,55, if it matters]:

1.  turn-on-auto-fill-mode doesn't exist; it is really
turn-on-auto-fill.

2.  The members of the list are symbols, not lists of symbols [a list
is okay only when it is a lambda-expression].

So the correct form should be (and I *did* try this one):

  (setq text-mode-hook '((lambda () (abbrev-mode 1)) turn-on-auto-fill))

>  (setq text-mode-hook (cons text-mode-hook '(turn-on-auto-fill)))
>...
>  (setq text-mode-hook (cons text-mode-hook '((lambda () (abbrev-mode 1)))))

These fail if text-mode-hook is undefined.  See Bob Weiner's separate
followup for a nicer solution.
--
/jr, nee John Robinson     Life did not take over the globe by combat,
jr@bbn.com or bbn!jr          but by networking -- Lynn Margulis