* How to exclude a major mode from a hook
@ 2015-02-11 9:22 Cecil Westerhof
2015-02-11 9:50 ` Fabrice Niessen
2015-02-11 10:15 ` Nicolas Richard
0 siblings, 2 replies; 5+ messages in thread
From: Cecil Westerhof @ 2015-02-11 9:22 UTC (permalink / raw)
To: help-gnu-emacs
I had the following in my .emacs:
(add-hook 'before-save-hook 'delete-trailing-whitespace)
But I also use Gnus and a signature starts with '-- ' and now the
space is deleted and it is not a signature anymore.
So I rewrote it to:
(add-hook 'before-save-hook (lambda () (when (not (string= major-mode "message-mode"))
'delete-trailing-whitespace)))
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to exclude a major mode from a hook
2015-02-11 9:22 How to exclude a major mode from a hook Cecil Westerhof
@ 2015-02-11 9:50 ` Fabrice Niessen
2015-02-11 12:31 ` Cecil Westerhof
2015-02-11 10:15 ` Nicolas Richard
1 sibling, 1 reply; 5+ messages in thread
From: Fabrice Niessen @ 2015-02-11 9:50 UTC (permalink / raw)
To: help-gnu-emacs-mXXj517/zsQ
Cecil Westerhof wrote:
> I had the following in my .emacs:
> (add-hook 'before-save-hook 'delete-trailing-whitespace)
>
> But I also use Gnus and a signature starts with '-- ' and now the
> space is deleted and it is not a signature anymore.
>
> So I rewrote it to:
> (add-hook 'before-save-hook (lambda () (when (not (string= major-mode "message-mode"))
> 'delete-trailing-whitespace)))
I do have:
--8<---------------cut here---------------start------------->8---
;; Nuke all trailing whitespaces in the buffer.
(add-hook 'before-save-hook
(lambda () ; Except for ...
(unless (or (eq major-mode 'message-mode)
; ... where "-- " is the signature
; separator (for when using emacsclient
; to compose emails and doing C-x #).
(eq major-mode 'diff-mode))
; ... where the patch file can't be
; changed!
(delete-trailing-whitespace))))
--8<---------------cut here---------------end--------------->8---
Best regards,
Fabrice
--
Fabrice Niessen
Leuven, Belgium
http://www.pirilampo.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to exclude a major mode from a hook
2015-02-11 9:50 ` Fabrice Niessen
@ 2015-02-11 12:31 ` Cecil Westerhof
0 siblings, 0 replies; 5+ messages in thread
From: Cecil Westerhof @ 2015-02-11 12:31 UTC (permalink / raw)
To: help-gnu-emacs
Op Wednesday 11 Feb 2015 10:50 CET schreef Fabrice Niessen:
> Cecil Westerhof wrote:
>> I had the following in my .emacs:
>> (add-hook 'before-save-hook 'delete-trailing-whitespace)
>>
>> But I also use Gnus and a signature starts with '-- ' and now the
>> space is deleted and it is not a signature anymore.
>>
>> So I rewrote it to: (add-hook 'before-save-hook (lambda () (when
>> (not (string= major-mode "message-mode"))
>> 'delete-trailing-whitespace)))
>
> I do have:
>
> ;; Nuke all trailing whitespaces in the buffer.
> (add-hook 'before-save-hook
> (lambda () ; Except for ...
> (unless (or (eq major-mode 'message-mode)
> ; ... where "-- " is the signature
> ; separator (for when using emacsclient
> ; to compose emails and doing C-x #).
> (eq major-mode 'diff-mode))
> ; ... where the patch file can't be
> ; changed!
> (delete-trailing-whitespace))))
At the moment I do not use diff-mode, but it does not hurt to add it.
;-)
Also your solution is a little more clear. (unless instead of not)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to exclude a major mode from a hook
2015-02-11 9:22 How to exclude a major mode from a hook Cecil Westerhof
2015-02-11 9:50 ` Fabrice Niessen
@ 2015-02-11 10:15 ` Nicolas Richard
2015-02-12 2:39 ` Robert Thorpe
1 sibling, 1 reply; 5+ messages in thread
From: Nicolas Richard @ 2015-02-11 10:15 UTC (permalink / raw)
To: Cecil Westerhof; +Cc: help-gnu-emacs
Hi,
Cecil Westerhof <Cecil@decebal.nl> writes:
> I had the following in my .emacs:
> (add-hook 'before-save-hook 'delete-trailing-whitespace)
IMO this is not the right thing to do, because one day you'll want to
edit a file in which trailing white spaces are important and you'll
wonder why it's all screwed up.
I would suggest to use a function that checks for trailing whitespaces
(with exceptions, such as the signature delimiter in message-mode) and
(interactievly) prompts if there is something to fix. I don't know if
such a function exists.
> So I rewrote it to:
> (add-hook 'before-save-hook (lambda () (when (not (string= major-mode "message-mode"))
> 'delete-trailing-whitespace)))
The condition
(string= major-mode "message-mode")
can be written as
(eq major-mode 'message-mode)
and would be better written as
(derived-mode-p 'message-mode)
--
Nicolas
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to exclude a major mode from a hook
2015-02-11 10:15 ` Nicolas Richard
@ 2015-02-12 2:39 ` Robert Thorpe
0 siblings, 0 replies; 5+ messages in thread
From: Robert Thorpe @ 2015-02-12 2:39 UTC (permalink / raw)
To: Nicolas Richard; +Cc: Cecil, help-gnu-emacs
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> Hi,
>
> Cecil Westerhof <Cecil@decebal.nl> writes:
>> I had the following in my .emacs:
>> (add-hook 'before-save-hook 'delete-trailing-whitespace)
>
> IMO this is not the right thing to do, because one day you'll want to
> edit a file in which trailing white spaces are important and you'll
> wonder why it's all screwed up.
I agree. It's much better to use an inclusive method for things like
this. Find the programming languages and text modes that you care about
and only hook those. It's not very hard, and when you think about it
the list is probably less than 20 items. Doing it this way also means
you can (eventually) put the add-hook in a section of your init file
devoted to that a group of related language modes which makes it easier
to understand.
BR,
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-02-12 2:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-11 9:22 How to exclude a major mode from a hook Cecil Westerhof
2015-02-11 9:50 ` Fabrice Niessen
2015-02-11 12:31 ` Cecil Westerhof
2015-02-11 10:15 ` Nicolas Richard
2015-02-12 2:39 ` Robert Thorpe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).