all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Doing things only in a particular mode
@ 2015-08-24 14:49 Colin Yates
  0 siblings, 0 replies; 14+ messages in thread
From: Colin Yates @ 2015-08-24 14:49 UTC (permalink / raw
  To: help-gnu-emacs

(newbie warning).

So I understand about (add-hook...) but I can't find the hook I
want. Basically, I have visual-line-mode turned on globally, but I want
to disable it when I view the headers in mu4e.

The buffer is called *mu4e-headers* and I can see the major mode is
mu4e-headers but the following code has no effect:

(add-hook 'mu4e-headers-hook
  (lambda ()
    (visual-line-mode 0)))

I am not sure how 'hooks' are created - I searched through the source
code for my4e-headers-hook but couldn't find it.

Assuming this is the right approach, how can I say 'when the major mode
is X then do this'. What is the idiomatic Emacs way?

Thanks!

Sent with my mu4e



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
       [not found] <mailman.0.1440427806.19573.help-gnu-emacs@gnu.org>
@ 2015-08-24 15:18 ` Dan Espen
  2015-08-24 16:28   ` Colin Yates
  2015-08-24 17:18 ` Joost Kremers
  1 sibling, 1 reply; 14+ messages in thread
From: Dan Espen @ 2015-08-24 15:18 UTC (permalink / raw
  To: help-gnu-emacs

Colin Yates <colin.yates@gmail.com> writes:

> (newbie warning).
>
> So I understand about (add-hook...) but I can't find the hook I
> want. Basically, I have visual-line-mode turned on globally, but I want
> to disable it when I view the headers in mu4e.
>
> The buffer is called *mu4e-headers* and I can see the major mode is
> mu4e-headers but the following code has no effect:
>
> (add-hook 'mu4e-headers-hook
>   (lambda ()
>     (visual-line-mode 0)))
>
> I am not sure how 'hooks' are created - I searched through the source
> code for my4e-headers-hook but couldn't find it.
>
> Assuming this is the right approach, how can I say 'when the major mode
> is X then do this'. What is the idiomatic Emacs way?

Sorry, I know nothing about mu4e, but the docs seem pretty good.
Here's where they describe the compose mode hook:

http://www.djcbsoftware.nl/code/mu/mu4e/Compose-hooks.html#Compose-hooks

Usually I do ^h m (help mode), and the help text mentions the hooks.


-- 
Dan Espen


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-24 15:18 ` Doing things only in a particular mode Dan Espen
@ 2015-08-24 16:28   ` Colin Yates
  2015-08-24 17:32     ` John Mastro
  2015-08-25  1:30     ` Emanuel Berg
  0 siblings, 2 replies; 14+ messages in thread
From: Colin Yates @ 2015-08-24 16:28 UTC (permalink / raw
  To: Dan Espen; +Cc: help-gnu-emacs

Thanks Dan. I did look at those sources, specifiying looking for
something like =mu4e-headers-hook= but couldn't find one.

Do you have any idea about the more generic emacs question?
Specifically, how do I achieve the following:

(when (= MAJOR_MODE "mu4e-headers")
  (do this)
  (and do that))

Thanks.

Dan Espen writes:

> Colin Yates <colin.yates@gmail.com> writes:
>
>> (newbie warning).
>>
>> So I understand about (add-hook...) but I can't find the hook I
>> want. Basically, I have visual-line-mode turned on globally, but I want
>> to disable it when I view the headers in mu4e.
>>
>> The buffer is called *mu4e-headers* and I can see the major mode is
>> mu4e-headers but the following code has no effect:
>>
>> (add-hook 'mu4e-headers-hook
>>   (lambda ()
>>     (visual-line-mode 0)))
>>
>> I am not sure how 'hooks' are created - I searched through the source
>> code for my4e-headers-hook but couldn't find it.
>>
>> Assuming this is the right approach, how can I say 'when the major mode
>> is X then do this'. What is the idiomatic Emacs way?
>
> Sorry, I know nothing about mu4e, but the docs seem pretty good.
> Here's where they describe the compose mode hook:
>
> http://www.djcbsoftware.nl/code/mu/mu4e/Compose-hooks.html#Compose-hooks
>
> Usually I do ^h m (help mode), and the help text mentions the hooks.

-- 
Sent with my mu4e



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
       [not found] <mailman.0.1440427806.19573.help-gnu-emacs@gnu.org>
  2015-08-24 15:18 ` Doing things only in a particular mode Dan Espen
@ 2015-08-24 17:18 ` Joost Kremers
  2015-08-24 17:30   ` Colin Yates
  2015-08-24 19:47   ` Jorge A. Alfaro-Murillo
  1 sibling, 2 replies; 14+ messages in thread
From: Joost Kremers @ 2015-08-24 17:18 UTC (permalink / raw
  To: help-gnu-emacs

Colin Yates wrote:
> (newbie warning).
>
> So I understand about (add-hook...) but I can't find the hook I
> want. Basically, I have visual-line-mode turned on globally, but I want
> to disable it when I view the headers in mu4e.
>
> The buffer is called *mu4e-headers* and I can see the major mode is
> mu4e-headers but the following code has no effect:
>
> (add-hook 'mu4e-headers-hook
>   (lambda ()
>     (visual-line-mode 0)))

Well, the hook is actually called `mu4e-headers-mode-hook`, so if you
use that, it should work.

To check if a variable exists, or find one if you have some idea what it
might be called, you can use `C-h v`, type the name and hit RET. TAB
completion works, so typing e.g., `C-h v mu4e-headers-hook TAB` would
have found the right variable for you.

BTW, the general advice is to not use lambdas in hook variables, just
function names. You might not really care, but if you want to be
pedantically correct about things, you could write:

(defun my-mu4e-headers-function ()  ; use whatever name you see fit
  (visual-line-mode -1))
(add-hook 'my-mu4e-headers-function)

Note that I use `-1` as the argument to `visual-line-mode`. IIRC an
argument of 0 would actually activate the mode.

> I am not sure how 'hooks' are created - I searched through the source
> code for my4e-headers-hook but couldn't find it.

They are created automatically when you create a major or minor mode
with `define-derived-mode` or `define-minor-mode`, so that's why you
couldn't find it by grepping the source.

> Assuming this is the right approach, how can I say 'when the major mode
> is X then do this'. What is the idiomatic Emacs way?

Well, one can argue about the meaning of "idiomatic", but here's how one
could do it:

(when (eq major-mode 'mu4e-headers-mode)
  (do this)
  (and that))

Note, however, that in a mode major hook, there's no need to use this,
because if the mode were anything else, the hook wouldn't be run. It
could be useful in an Elisp program or in a (function called in a) minor
mode hook, though.

HTH


-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-24 17:18 ` Joost Kremers
@ 2015-08-24 17:30   ` Colin Yates
  2015-08-24 19:47   ` Jorge A. Alfaro-Murillo
  1 sibling, 0 replies; 14+ messages in thread
From: Colin Yates @ 2015-08-24 17:30 UTC (permalink / raw
  To: Joost Kremers; +Cc: help-gnu-emacs

Many thanks, this was exactly the type of info I was looking for. The
name of the hook was valuable but advice on how to find the name was
ever more appreciated.

Thanks Joost!

Joost Kremers writes:

> Colin Yates wrote:
>> (newbie warning).
>>
>> So I understand about (add-hook...) but I can't find the hook I
>> want. Basically, I have visual-line-mode turned on globally, but I want
>> to disable it when I view the headers in mu4e.
>>
>> The buffer is called *mu4e-headers* and I can see the major mode is
>> mu4e-headers but the following code has no effect:
>>
>> (add-hook 'mu4e-headers-hook
>>   (lambda ()
>>     (visual-line-mode 0)))
>
> Well, the hook is actually called `mu4e-headers-mode-hook`, so if you
> use that, it should work.
>
> To check if a variable exists, or find one if you have some idea what it
> might be called, you can use `C-h v`, type the name and hit RET. TAB
> completion works, so typing e.g., `C-h v mu4e-headers-hook TAB` would
> have found the right variable for you.
>
> BTW, the general advice is to not use lambdas in hook variables, just
> function names. You might not really care, but if you want to be
> pedantically correct about things, you could write:
>
> (defun my-mu4e-headers-function ()  ; use whatever name you see fit
>   (visual-line-mode -1))
> (add-hook 'my-mu4e-headers-function)
>
> Note that I use `-1` as the argument to `visual-line-mode`. IIRC an
> argument of 0 would actually activate the mode.
>
>> I am not sure how 'hooks' are created - I searched through the source
>> code for my4e-headers-hook but couldn't find it.
>
> They are created automatically when you create a major or minor mode
> with `define-derived-mode` or `define-minor-mode`, so that's why you
> couldn't find it by grepping the source.
>
>> Assuming this is the right approach, how can I say 'when the major mode
>> is X then do this'. What is the idiomatic Emacs way?
>
> Well, one can argue about the meaning of "idiomatic", but here's how one
> could do it:
>
> (when (eq major-mode 'mu4e-headers-mode)
>   (do this)
>   (and that))
>
> Note, however, that in a mode major hook, there's no need to use this,
> because if the mode were anything else, the hook wouldn't be run. It
> could be useful in an Elisp program or in a (function called in a) minor
> mode hook, though.
>
> HTH

-- 
Sent with my mu4e



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-24 16:28   ` Colin Yates
@ 2015-08-24 17:32     ` John Mastro
  2015-08-25  1:30     ` Emanuel Berg
  1 sibling, 0 replies; 14+ messages in thread
From: John Mastro @ 2015-08-24 17:32 UTC (permalink / raw
  To: help-gnu-emacs@gnu.org; +Cc: Colin Yates

> Do you have any idea about the more generic emacs question?
> Specifically, how do I achieve the following:
>
> (when (= MAJOR_MODE "mu4e-headers")
>   (do this)
>   (and do that))

There will usually be a `foo-mode-hook' for any mode `foo', so you would
create a function to perform you configuration and add it to that hook.

There's also an `after-change-major-mode-hook', where you can add
functions that you want to be run after the major mode changes. However,
in practice I've never needed to use it.

As an aside, the value of `major-mode' is a symbol, so `=' won't work
for the equality comparison (unlike in Clojure, `=' isn't very
polymorphic; it can only compare numbers or markers). Either `eq'
(equivalent to Clojure's `identical?') or `equal' (the closest to
Clojure's `=', though not equivalent) would work.

-- 
john



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-24 17:18 ` Joost Kremers
  2015-08-24 17:30   ` Colin Yates
@ 2015-08-24 19:47   ` Jorge A. Alfaro-Murillo
  2015-08-26 13:54     ` Stefan Monnier
  1 sibling, 1 reply; 14+ messages in thread
From: Jorge A. Alfaro-Murillo @ 2015-08-24 19:47 UTC (permalink / raw
  To: help-gnu-emacs

Joost Kremers writes:
 
> Note that I use `-1` as the argument to `visual-line-mode`. IIRC 
> an argument of 0 would actually activate the mode.

That is wrong, for any mode, say "foo", calling the function 
foo-mode with a prefix argument ARG, should enable the mode if ARG 
is positive or nil, and disable it otherwise. 0 is not positive.

-- 
Jorge.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-24 16:28   ` Colin Yates
  2015-08-24 17:32     ` John Mastro
@ 2015-08-25  1:30     ` Emanuel Berg
  2015-08-25  1:55       ` Emanuel Berg
  2015-08-27  5:17       ` Marcin Borkowski
  1 sibling, 2 replies; 14+ messages in thread
From: Emanuel Berg @ 2015-08-25  1:30 UTC (permalink / raw
  To: help-gnu-emacs

Colin Yates <colin.yates@gmail.com> writes:

> Do you have any idea about the more generic emacs
> question? Specifically, how do I achieve the
> following:
>
> (when (= MAJOR_MODE "mu4e-headers") (do this) (and do
> that))

Pseudo-code that looks like real code but isn't -
run!!!

;; syntax for `if' with single-form branches

(if (eq major-mode 'mu4e-headers-mode) (do-something)
  (do-something-else) )

;; ditto multiple-form branches

(if (eq major-mode 'mu4e-headers-mode)
  (progn
    (do-something-step-1)
    (do-something-step-2) )
  (progn
    (else-do-this-step-1)
    ;; ...
    (else-do-this-step-n) ))

;; syntax for when

(when condition
   (do-this)
   (and-this)
   (including-this) )

;; group major modes

(unless (member major-mode '(makefile-gmake-mode
                             makefile-mode) ) ; exceptions
    (untabify (point-min) (point-max)))

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-25  1:30     ` Emanuel Berg
@ 2015-08-25  1:55       ` Emanuel Berg
  2015-08-27  5:17       ` Marcin Borkowski
  1 sibling, 0 replies; 14+ messages in thread
From: Emanuel Berg @ 2015-08-25  1:55 UTC (permalink / raw
  To: help-gnu-emacs

Emanuel Berg <embe8573@student.uu.se> writes:

> ;; syntax for `if' with single-form branches
>
> (if (eq major-mode 'mu4e-headers-mode) (do-something)
>   (do-something-else) )
>
> ;; ditto multiple-form branches
>
> (if (eq major-mode 'mu4e-headers-mode)
>   (progn
>     (do-something-step-1)
>     (do-something-step-2) )
>   (progn
>     (else-do-this-step-1)
>     ;; ...
>     (else-do-this-step-n) ))

Maybe "syntax" is stretching it for the last example.
Let's say it is one way of doing it, a way that is
common and not bad. But actually you can put whatever
you want there that makes sense.

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-24 19:47   ` Jorge A. Alfaro-Murillo
@ 2015-08-26 13:54     ` Stefan Monnier
  2015-08-26 19:35       ` Jorge A. Alfaro-Murillo
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2015-08-26 13:54 UTC (permalink / raw
  To: help-gnu-emacs

>> Note that I use `-1` as the argument to `visual-line-mode`.

I do the same and recommend others do that as well.

>> IIRC an argument of 0 would actually activate the mode.
> That is wrong,

Indeed, it's wrong.  But whether 0 is negative or positive or both or
neither is a long discussion, so by using -1 you completely side step
the issue.


        Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-26 13:54     ` Stefan Monnier
@ 2015-08-26 19:35       ` Jorge A. Alfaro-Murillo
  2015-08-26 20:09         ` Jorge A. Alfaro-Murillo
  2015-08-27 13:53         ` Stefan Monnier
  0 siblings, 2 replies; 14+ messages in thread
From: Jorge A. Alfaro-Murillo @ 2015-08-26 19:35 UTC (permalink / raw
  To: help-gnu-emacs

Stefan Monnier writes:

>>> Note that I use `-1` as the argument to `visual-line-mode`. 
> 
> I do the same and recommend others do that as well.

I like better:

(foo-mode 1) ; on

(foo-mode 0) ; off

It looks like a on/off switch.

>>> IIRC an argument of 0 would actually activate the mode. 
>> That is wrong, 
> 
> Indeed, it's wrong.  But whether 0 is negative or positive or 
> both or neither is a long discussion, so by using -1 you 
> completely side step the issue.

A positive number is a number bigger than zero. Zero is not bigger 
than zero, so it is not a positive number. It is also not a 
negative number, since it is not smaller than zero.

-- 
Jorge.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-26 19:35       ` Jorge A. Alfaro-Murillo
@ 2015-08-26 20:09         ` Jorge A. Alfaro-Murillo
  2015-08-27 13:53         ` Stefan Monnier
  1 sibling, 0 replies; 14+ messages in thread
From: Jorge A. Alfaro-Murillo @ 2015-08-26 20:09 UTC (permalink / raw
  To: help-gnu-emacs

Jorge A. Alfaro-Murillo writes:

> A positive number is a number bigger than zero. Zero is not 
> bigger than zero, so it is not a positive number. It is also not 
> a negative number, since it is not smaller than zero.

Although in French 0 is "positif" which is the equivalent of 
"non-negative" in English.

-- 
Jorge.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-25  1:30     ` Emanuel Berg
  2015-08-25  1:55       ` Emanuel Berg
@ 2015-08-27  5:17       ` Marcin Borkowski
  1 sibling, 0 replies; 14+ messages in thread
From: Marcin Borkowski @ 2015-08-27  5:17 UTC (permalink / raw
  To: help-gnu-emacs


On 2015-08-25, at 03:30, Emanuel Berg <embe8573@student.uu.se> wrote:

> ;; ditto multiple-form branches
>
> (if (eq major-mode 'mu4e-headers-mode)
>   (progn
>     (do-something-step-1)
>     (do-something-step-2) )
>   (progn
>     (else-do-this-step-1)
>     ;; ...
>     (else-do-this-step-n) ))

I guess you got the indentation wrong.  Also, you don't need progn for
else-branch.  Also, if you need progn for then-branch, I'd prefer cond
to if.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Doing things only in a particular mode
  2015-08-26 19:35       ` Jorge A. Alfaro-Murillo
  2015-08-26 20:09         ` Jorge A. Alfaro-Murillo
@ 2015-08-27 13:53         ` Stefan Monnier
  1 sibling, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2015-08-27 13:53 UTC (permalink / raw
  To: help-gnu-emacs

> A positive number is a number bigger than zero.

That's one definition,


        Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2015-08-27 13:53 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.0.1440427806.19573.help-gnu-emacs@gnu.org>
2015-08-24 15:18 ` Doing things only in a particular mode Dan Espen
2015-08-24 16:28   ` Colin Yates
2015-08-24 17:32     ` John Mastro
2015-08-25  1:30     ` Emanuel Berg
2015-08-25  1:55       ` Emanuel Berg
2015-08-27  5:17       ` Marcin Borkowski
2015-08-24 17:18 ` Joost Kremers
2015-08-24 17:30   ` Colin Yates
2015-08-24 19:47   ` Jorge A. Alfaro-Murillo
2015-08-26 13:54     ` Stefan Monnier
2015-08-26 19:35       ` Jorge A. Alfaro-Murillo
2015-08-26 20:09         ` Jorge A. Alfaro-Murillo
2015-08-27 13:53         ` Stefan Monnier
2015-08-24 14:49 Colin Yates

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.