* What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
@ 2021-04-03 3:42 Marcin Borkowski
2021-04-03 4:11 ` [External] : " Drew Adams
0 siblings, 1 reply; 9+ messages in thread
From: Marcin Borkowski @ 2021-04-03 3:42 UTC (permalink / raw)
To: Help Gnu Emacs mailing list
Well, I understand what each of them does, but whay was the variable
introduced and why would I want to say `(setq deactivate-mark t)' in
a command instead of just calling `(deactivate-mark)'? I can't think of
any possible reasons. Any ideas?
TIA,
--
Marcin Borkowski
http://mbork.pl
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-03 3:42 What is the difference between (deactivate-mark) and (setq deactivate-mark t)? Marcin Borkowski
@ 2021-04-03 4:11 ` Drew Adams
2021-04-03 4:13 ` Drew Adams
2021-04-07 5:14 ` Marcin Borkowski
0 siblings, 2 replies; 9+ messages in thread
From: Drew Adams @ 2021-04-03 4:11 UTC (permalink / raw)
To: Marcin Borkowski, Help Gnu Emacs mailing list
> Well, I understand what each of them does, but whay was the variable
> introduced and why would I want to say `(setq deactivate-mark t)' in
> a command instead of just calling `(deactivate-mark)'? I can't think of
> any possible reasons. Any ideas?
I may be repeating what you say you already
understand, but...
Function `deactivate-mark' deactivates the mark
(duh), and it does so right away.
After a command finishes and returns, the command
loop normally automatically deactivates the mark.
IOW, for the next command the mark is inactive.
But if you set variable `deactivate-mark' to `nil'
in your command then the command loop won't
deactivate it when your command is done.
As the manual says:
To write Lisp code that modifies the buffer
without causing deactivation of the mark at
the end of the command, bind 'deactivate-mark'
to 'nil' around the code that does the modification.
For example:
(let (deactivate-mark)
(insert " "))
Setting the variable to nil says do NOT deactivate
the mark. See (elisp) `The Mark'.
https://www.gnu.org/software/emacs/manual/html_node/elisp/The-Mark.html
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-03 4:11 ` [External] : " Drew Adams
@ 2021-04-03 4:13 ` Drew Adams
2021-04-03 4:24 ` Drew Adams
2021-04-07 5:14 ` Marcin Borkowski
1 sibling, 1 reply; 9+ messages in thread
From: Drew Adams @ 2021-04-03 4:13 UTC (permalink / raw)
To: Drew Adams, Marcin Borkowski, Help Gnu Emacs mailing list
> I may be repeating what you say you already
> understand, but...
>
> Function `deactivate-mark' deactivates the mark
> (duh), and it does so right away.
>
> After a command finishes and returns, the command
> loop normally automatically deactivates the mark.
> IOW, for the next command the mark is inactive.
>
> But if you set variable `deactivate-mark' to `nil'
> in your command then the command loop won't
> deactivate it when your command is done.
>
> As the manual says:
>
> To write Lisp code that modifies the buffer
> without causing deactivation of the mark at
> the end of the command, bind 'deactivate-mark'
> to 'nil' around the code that does the modification.
> For example:
>
> (let (deactivate-mark)
> (insert " "))
>
> Setting the variable to nil says do NOT deactivate
> the mark. See (elisp) `The Mark'.
>
> https://urldefense.com/v3/__https://www.gnu.org/software/emacs/manual/html_no
> de/elisp/The-Mark.html__;!!GqivPVa7Brio!J-
> d7d78WD6nya8fCTTFpzUVHQ663J53lu6PUPuwSANdbP-lnuZeC2k3gEwHg1-Di$
I meant to add that you can do this in one command
when, for example, you want to keep the region active
for the next command. E.g. when one command sets up
the region for other possible commands to use.
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-03 4:13 ` Drew Adams
@ 2021-04-03 4:24 ` Drew Adams
0 siblings, 0 replies; 9+ messages in thread
From: Drew Adams @ 2021-04-03 4:24 UTC (permalink / raw)
To: Marcin Borkowski, Help Gnu Emacs mailing list
Here are some examples from my code:
1. `mark-buffer-after-point'. The purpose of the command
is precisely to select some text as the region.
(defun mark-buffer-after-point (reversep)
"Select the part of the buffer after point.
With a prefix argument, select the part before point."
(interactive "P")
(push-mark (if reversep (point-min) (point-max)) nil t)
(setq deactivate-mark nil))
2. `isearchp-set-region-around-search-target'. Again,
the purpose is to select some text (the search hit)
and leave it selected.
(defun isearchp-set-region-around-search-target ()
"Set the region around the last search or query-replace target."
(interactive)
(case last-command
((isearch-forward isearch-backward
isearch-forward-regexp isearch-backward-regexp)
(push-mark isearch-other-end t 'activate))
(t (push-mark (match-beginning 0) t 'activate)))
(setq deactivate-mark nil))
3. `Info-change-visited-status'. Do stuff, but first
record whether the region was active at the outset.
When done, if it was active then re-activate it; if
it wasn't then deactivate it.
(Info-change-visited-status START END &optional ARG)
Change whether the nodes in the region have been visited.
If the region is not active then act on only the node at point.
No prefix arg means toggle the visited status of each node.
A non-negative prefix arg means consider the nodes visited.
A negative prefix arg means consider the nodes not visited.
The command does stuff, but it ends with this, where
variable `active' is non-nil if the region was active
at the outset.
(if (not active)
(deactivate-mark)
(activate-mark)
(setq deactivate-mark nil))
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-03 4:11 ` [External] : " Drew Adams
2021-04-03 4:13 ` Drew Adams
@ 2021-04-07 5:14 ` Marcin Borkowski
2021-04-07 7:25 ` tomas
2021-04-07 14:54 ` Drew Adams
1 sibling, 2 replies; 9+ messages in thread
From: Marcin Borkowski @ 2021-04-07 5:14 UTC (permalink / raw)
To: Drew Adams; +Cc: Help Gnu Emacs mailing list
On 2021-04-03, at 06:11, Drew Adams <drew.adams@oracle.com> wrote:
>> Well, I understand what each of them does, but whay was the variable
>> introduced and why would I want to say `(setq deactivate-mark t)' in
>> a command instead of just calling `(deactivate-mark)'? I can't think of
>> any possible reasons. Any ideas?
>
> I may be repeating what you say you already
> understand, but...
>
> Function `deactivate-mark' deactivates the mark
> (duh), and it does so right away.
>
> After a command finishes and returns, the command
> loop normally automatically deactivates the mark.
> IOW, for the next command the mark is inactive.
Well, I don't think that's the case. Take this command:
(defun petrichor ()
(interactive)
(message "Smell of the dust after the rain"))
activate the region and say M-x petrichor - the region is still active.
If I want to change this, I can either say `(deactivate-mark)' or `(setq
deactivate-mark t)'. My question was, why I would want to choose the
latter?
I think I now know the reason - while experimenting with this and
studying the docs, I probably found out. Can anyone correct me if I'm
wrong? It seems that its main use is to actually _prevent_ the code
from deactivating the region _if_ it would do it otherwise, by means of
_modifying_ the buffer. Since every primitive that changes the buffer
(like `insert') sets it to `t', you can say `(setq deactivate-mark nil)`
at the end of a buffer-modifying command and the region will remain
active if it was before running it. (The manual suggests temporarily
binding it with `let' which has a similar effect, as you mentioned.)
Does that make sense?
--
Marcin Borkowski
http://mbork.pl
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-07 5:14 ` Marcin Borkowski
@ 2021-04-07 7:25 ` tomas
2021-04-07 14:58 ` Drew Adams
2021-04-07 14:54 ` Drew Adams
1 sibling, 1 reply; 9+ messages in thread
From: tomas @ 2021-04-07 7:25 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 743 bytes --]
On Wed, Apr 07, 2021 at 07:14:20AM +0200, Marcin Borkowski wrote:
>
> On 2021-04-03, at 06:11, Drew Adams <drew.adams@oracle.com> wrote:
> > After a command finishes and returns, the command
> > loop normally automatically deactivates the mark.
> > IOW, for the next command the mark is inactive.
[...]
> I think I now know the reason - while experimenting with this and
> studying the docs, I probably found out [...]
> It seems that its main use is to actually _prevent_ the code
> from deactivating the region _if_ it would do it otherwise, by means of
> _modifying_ the buffer.
In a nutshell: deactivating (i.e. setting to nil) `deactivate-mark'
deactivates the mark-deactivating code. Phew ;-)
Cheers
- t
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-07 7:25 ` tomas
@ 2021-04-07 14:58 ` Drew Adams
2021-04-07 15:30 ` tomas
0 siblings, 1 reply; 9+ messages in thread
From: Drew Adams @ 2021-04-07 14:58 UTC (permalink / raw)
To: tomas@tuxteam.de, help-gnu-emacs@gnu.org
> In a nutshell: deactivating (i.e. setting to nil) `deactivate-mark'
> deactivates the mark-deactivating code. Phew ;-)
Yes. An alternative name for the variable
could have been `don't-deactivate-mark',
with the nil/non-nil values swapped.
The name `deactivate-mark', especially with
the same for the function, which kinda has
an opposite effect, maybe led to Marcin's
confusion.
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [External] : What is the difference between (deactivate-mark) and (setq deactivate-mark t)?
2021-04-07 5:14 ` Marcin Borkowski
2021-04-07 7:25 ` tomas
@ 2021-04-07 14:54 ` Drew Adams
1 sibling, 0 replies; 9+ messages in thread
From: Drew Adams @ 2021-04-07 14:54 UTC (permalink / raw)
To: Marcin Borkowski; +Cc: Help Gnu Emacs mailing list
> > Function `deactivate-mark' deactivates the mark
> > (duh), and it does so right away.
> >
> > After a command finishes and returns, the command
> > loop normally automatically deactivates the mark.
> > IOW, for the next command the mark is inactive.
>
> Well, I don't think that's the case...
>
> I think I now know the reason - while experimenting with this and
> studying the docs, I probably found out. Can anyone correct me if I'm
> wrong? It seems that its main use is to actually _prevent_ the code
> from deactivating the region _if_ it would do it otherwise, by means of
> _modifying_ the buffer.
Yes. I should have said just what the doc says:
If an editor command sets this variable non-'nil',
then the editor command loop deactivates the mark
after the command returns (if Transient Mark mode
is enabled).
What I said gave the impression that that is always
the case. The point is that it is often the case,
or sometimes the case. And as you say, the real
point is that your command can't know whether it
will be the case. Setting the variable to nil makes
sure it won't be deactivated by the command loop.
The doc, which I pointed to, is very clear about all
of this, I think.
> Since every primitive that changes the buffer
> (like `insert') sets it to `t', you can say `(setq deactivate-mark nil)`
> at the end of a buffer-modifying command and the region will remain
> active if it was before running it.
Yes, I believe I said that. And not just if it was
active before running your command. Your command
can intentionally activate the region and use the
variable (non-nil) to tell the command loop not to
deactivate it. I gave examples of that.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2021-04-07 15:30 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-03 3:42 What is the difference between (deactivate-mark) and (setq deactivate-mark t)? Marcin Borkowski
2021-04-03 4:11 ` [External] : " Drew Adams
2021-04-03 4:13 ` Drew Adams
2021-04-03 4:24 ` Drew Adams
2021-04-07 5:14 ` Marcin Borkowski
2021-04-07 7:25 ` tomas
2021-04-07 14:58 ` Drew Adams
2021-04-07 15:30 ` tomas
2021-04-07 14:54 ` Drew Adams
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).