help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Writing a function for a indented copy of a region


From: Drew Adams
Subject: RE: Writing a function for a indented copy of a region
Date: Wed, 17 Dec 2008 10:49:01 -0800

> > > A lot of times I need to copy a part of a (log) file for 
> > > an e-mail. I like to indent this (default with four spaces).
> > > At this moment I do this by hand. But I would like to do
> > > this with a function.
> > > What I would like this function to do is take the part that is
> > > selected, indent this with (default) four spaces, put the indented
> > > region in the kill-ring and undo the indent. Has anyone a pointer
> > > about how to code this?
> >
> > `C-x TAB' is `indent-rigidly'
> > `M-w' copies the region to the `kill-ring'
> 
> But the first statement unselects the region,

It _deactivates_ the region (the text is still selected). (I assume you are
using transient-mark-mode, otherwise, there is no notion of active region.)

You can re-activate the region at any time using `C-x C-x' (repeat it if you
care which end point is).

>From Lisp code, it often doesn't matter if the region is active -
`kill-ring-save' (`C-w') doesn't care, for instance.

You can test whether it is active by examining the value of (buffer-local)
variable `mark-active'.

I assume that you want to expand the region to include the whole lines at point
and mark. Does this do what you want?

(defun foo (cols beg end)
  (interactive "p\nr")
  (indent-rigidly
   (save-excursion (goto-char beg) (forward-line 0) (point))
   (save-excursion (goto-char end) (forward-line 1) (point))
   cols)
  (copy-region-as-kill
   (save-excursion (goto-char beg) (forward-line 0) (point))
   (save-excursion (goto-char end) (forward-line 1) (point))))

Most of the complication here is just to pick up the whole lines where point and
mark are. And you need to do that twice, because point moves when the region is
indented.

`kill-ring-save' would be OK instead of `copy-region-as-kill', but the latter is
a bit simpler.

Unless I misunderstand what you want, that should do it. And it doesn't matter
whether you use transient-mark-mode or whether the region is active.

If you want the _un_indented region copied, then reverse the order of
`indent-rigidly' and `copy-region-as-kill'. And if you don't care about whole
lines, and just want to copy the original region, then things are simpler:

(defun foo (cols beg end)
  (interactive "p\nr")
  (copy-region-as-kill beg end)
  (indent-rigidly beg end cols))

Anyway, you get the idea. Fiddle, depending on what you really want.

> that is why I want to
> write an elisp function to call after I selected the region. Is it
> possible to see if there is a region selected in an elisp function?
> What I was thinking about was to put the start and the end of the
> region in registers and use those registers in the function to mark
> the region again after I indented to put the indented text into the
> kill-ring. After that I should do an undo. And then the file is not
> changed, but I have the indented region in the kill-ring.

Unless I misunderstand what you want, you don't need to do any of that.

And as Matthias mentioned, it's often a good idea to just create a macro to do
what you want. That too should work in this case. That's why I originally
suggested calling the commands interactively.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]