Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

keep a region highlighted in transient mark mode

29 views
Skip to first unread message

Peter Chen

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
A developer here asked me how to keep a region highlighted in
transient mark mode after he executes a function on the region. For
one thing, he would like to mark a region, and indent the region by
inserting tabs similar to the functionality in VC5 (he didn't like the
default mode indentation).

Being the local emacs advocate, I wasn't about to let Microsoft Visual
Studio outdo emacs. So I wrote a short emacs lisp function to insert
tab line by line in the region. The only problem is that once the
function executes, the mark is deactivated, and the region is no
longer highlighted.

I am hoping someone could shed some light on the subject. TIA.

BTW, I checked dejanews for old articles on this, but didn't come up
with anything conclusive.

PeteChen <pete...@remus.rutgers.edu>

Lars R. Clausen

unread,
Feb 9, 1999, 3:00:00 AM2/9/99
to
On 9 Feb 1999, Peter Chen verbalised:

> A developer here asked me how to keep a region highlighted in
> transient mark mode after he executes a function on the region. For
> one thing, he would like to mark a region, and indent the region by
> inserting tabs similar to the functionality in VC5 (he didn't like the
> default mode indentation).

Now I don't know how VC5 does it, but can't you customize the C(++)
indenter to be like that?

> Being the local emacs advocate, I wasn't about to let Microsoft Visual
> Studio outdo emacs. So I wrote a short emacs lisp function to insert
> tab line by line in the region. The only problem is that once the
> function executes, the mark is deactivated, and the region is no
> longer highlighted.

You can either stop using transient-mark-mode (which I personally found to
be the Devils gift to Emacs users), or add at the end of your function
(setq mark-active t). (At least, that's what I glean from the elisp manual
is the thing to do.)

-Lars

--
Lars R. Clausen (lrcl...@cs.uiuc.edu)
A *real* smart bomb would call in sick, perhaps move to another country,
changing its name in the process, open a beach bar maybe and live out its
days in safe anonymity. -- Barry O'Neill in rhod

Peter Chen

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
Lars R. Clausen <lrcl...@cs.uiuc.edu.STOPSPAM> writes:

> On 9 Feb 1999, Peter Chen verbalised:
>
> You can either stop using transient-mark-mode (which I personally found to
> be the Devils gift to Emacs users),

I agree. I personally don't use transient-mark-mode at all. Then
again, it's not my place to tell another developer how he should use
emacs.

> or add at the end of your function
> (setq mark-active t). (At least, that's what I glean from the elisp manual
> is the thing to do.)

I have tried this to no avail. Here is the actual elisp code based
on indent.el. I will first qualify that I am no elisp expert. I just
know enough to muck around.

Pete
----
(defun indent-region-by-tab (start end)
""
(interactive "r\n")
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char start)
(or (bolp) (forward-line 1))
(while (< (point) end)
(or (and (bolp) (eolp))
(insert-tab))
(forward-line 1))
(move-marker end nil))
(setq mark-active t)
)

(defun unindent-region-by-tab (start end)
""
(interactive "r\n")
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char start)
(or (bolp) (forward-line 1))
(while (< (point) end)
(or (and (bolp) (eolp))
(if (eq (following-char) ?\t)
(delete-char 1))
)
(forward-line 1))
(move-marker end nil)))

Kai.Gro...@cs.uni-dortmund.de

unread,
Feb 10, 1999, 3:00:00 AM2/10/99
to
Peter Chen <pete...@remus.rutgers.edu> writes:

> A developer here asked me how to keep a region highlighted in
> transient mark mode after he executes a function on the region. For
> one thing, he would like to mark a region, and indent the region by
> inserting tabs similar to the functionality in VC5 (he didn't like the
> default mode indentation).

Try C-u 8 C-x C-i -- this indents the region eight spaces to the
right, and this might insert tab characters, depending on tab-width
and indent-tabs-mode.

Your explanation was a bit vague -- what exactly is it your colleague
wants to do?

You can type C-x C-x to exchange point and mark, as a side effect
rehighlighting the region. Obviously, C-x C-x C-x C-x is also useful.

kai
--
I like _ b_ o_ t_ h kinds of music.

Peter Chen

unread,
Feb 11, 1999, 3:00:00 AM2/11/99
to
Kai.Gro...@CS.Uni-Dortmund.DE writes:

> Your explanation was a bit vague -- what exactly is it your colleague
> wants to do?

In VC5, one can highlight a region then press <tab> to indent the
entire region, and <shift><tab> to unindent. He wants the same
functionality in emacs.

> You can type C-x C-x to exchange point and mark, as a side effect
> rehighlighting the region. Obviously, C-x C-x C-x C-x is also useful.

That's true. I have advised him the same. But that's only a workaround
after all.

Pete

Steve Gonedes

unread,
Feb 12, 1999, 3:00:00 AM2/12/99
to

Peter Chen <pete...@remus.rutgers.edu> writes:

< A developer here asked me how to keep a region highlighted in
< transient mark mode after he executes a function on the region. For
< one thing, he would like to mark a region, and indent the region by
< inserting tabs similar to the functionality in VC5 (he didn't like the
< default mode indentation).
<
< Being the local emacs advocate, I wasn't about to let Microsoft Visual
< Studio outdo emacs. So I wrote a short emacs lisp function to insert
< tab line by line in the region. The only problem is that once the
< function executes, the mark is deactivated, and the region is no
< longer highlighted.
<
< I am hoping someone could shed some light on the subject. TIA.
<
< BTW, I checked dejanews for old articles on this, but didn't come up
< with anything conclusive.

Would indent-region and transient-mark-mode work?

What do you mean by `the function executes'? Does VC studio have a C++
interpreter or are you refering to the infamous debugger? What does
the debugger/environment do that emacs doesn't do? Just curious as I
haven't had the opportunity to see VCStudio in action and am always
curious about innovation, productivity gains, and other suches.


Miles Bader

unread,
Feb 12, 1999, 3:00:00 AM2/12/99
to
Lars R. Clausen <lrcl...@cs.uiuc.edu.STOPSPAM> writes:
> On 9 Feb 1999, Peter Chen verbalised:
> > The only problem is that once the
> > function executes, the mark is deactivated, and the region is no
> > longer highlighted.
>
> You can either stop using transient-mark-mode (which I personally found to
> be the Devils gift to Emacs users), or add at the end of your function
> (setq mark-active t). (At least, that's what I glean from the elisp manual
> is the thing to do.)

The correct thing to do is to put:

(setq deactivate-mark nil)

at the end of your function (it's initially nil, but functions that
modify the buffer will set it to t).

-Miles
--
`Cars give people wonderful freedom and increase their opportunities. But
they also destroy the environment, to an extent so drastic that they kill
all social life' (from _A Pattern Language_)

Kai.Gro...@cs.uni-dortmund.de

unread,
Feb 12, 1999, 3:00:00 AM2/12/99
to
Peter Chen <pete...@remus.rutgers.edu> writes:

> Kai.Gro...@CS.Uni-Dortmund.DE writes:
>
> > You can type C-x C-x to exchange point and mark, as a side effect
> > rehighlighting the region. Obviously, C-x C-x C-x C-x is also useful.
>
> That's true. I have advised him the same. But that's only a workaround
> after all.

The following works, but I'm not sure why:

,-----
| (defun kai-indent-rigidly (start end arg)
| (interactive "r\np")
| (let ((mark-active mark-active)
| (deactivate-mark deactivate-mark))
| (indent-rigidly start end arg)))
`-----

Works just like indent-rigidly except for the mark behavior. Only
tested with transient-mark-mode being on.

You could also add some simple functions for S-<right> and S-<left>:

(defun s-right (b e) (interactive "r") (kai-indent-rigidly b e 1))
(defun s-left (b e) (interactive "r") (kai-indent-rigidly b e -1))

Peter Chen

unread,
Feb 17, 1999, 3:00:00 AM2/17/99
to
Miles Bader <mi...@ccs.mt.nec.co.jp> writes:

> The correct thing to do is to put:
>
> (setq deactivate-mark nil)
>
> at the end of your function (it's initially nil, but functions that
> modify the buffer will set it to t).
>
> -Miles

Thank you very much. This is exactly what I needed. I hacked up the
following code to emulate <tab> and <shift><tab> from VC. My
developer is happy. :-)

Pete
----
(define-key global-map "\t" 'mytab)
(define-key global-map [S-tab] 'myshifttab)

(defun mytab ()
""
(interactive "")
(if mark-active
(indent-region-by-tab (region-beginning) (region-end))
(indent-for-tab-command)))

(defun myshifttab ()
""
(interactive "")
(if mark-active
(unindent-region-by-tab (region-beginning) (region-end))
(unindent-line)))

(defun unindent-line ()
""
(interactive "")
(if (not (bolp))
(if (eq (char-before) ?\t)
(delete-char -1))
(signal 'quit nil))
)

(defun indent-region-by-tab (start end)
""
(interactive "r\n")
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char start)
(or (bolp) (forward-line 1))
(while (< (point) end)
(or (and (bolp) (eolp))
(insert-tab))
(forward-line 1))
(move-marker end nil)

(setq deactivate-mark nil)
)
)

(defun unindent-region-by-tab (start end)
""
(interactive "r\n")
(save-excursion
(goto-char end)
(setq end (point-marker))
(goto-char start)
(or (bolp) (forward-line 1))
(while (< (point) end)
(or (and (bolp) (eolp))
(if (eq (following-char) ?\t)
(delete-char 1))
)
(forward-line 1))
(move-marker end nil))

(setq deactivate-mark nil)
)

0 new messages