unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Andreas Politz <politza@fh-trier.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Writing a function for a indented copy of a region
Date: Wed, 17 Dec 2008 20:47:51 +0100	[thread overview]
Message-ID: <1229543337.962004@arno.fh-trier.de> (raw)
In-Reply-To: <fd067370-3034-4b7a-a17e-dda55358bc18@e1g2000pra.googlegroups.com>

Decebal wrote:
> On 17 dec, 14:31, Decebal <CLDWester...@gmail.com> wrote:
>> 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?
> 
> I found a way. In my .emacs I put:
> (defun my-indented-yank(indent)
>   "Put indented region in the kill-ring"
>   (interactive "p")
>   (setq indent (cond ((eq indent 0) 1)
>                      ((eq indent 1) 4)
>                      (t indent)
>                )
>   )
>   (indent-rigidly (region-beginning) (region-end) indent)
>   (copy-region-as-kill (region-beginning) (region-end))
>   (undo)
> )
> 
> It looks like this satifies my demands. ;-}
> In this way the default indent is 4. If I need an indent of one I can
> use 'C-u 0'.
> 
> The only thing is that the 'GNU Emacs Lisp Reference Manual' says that
> I should not use 'copy-region-as-kill'. I should use 'kill-new' or
> 'kill-append'. But those do not work with a region. What am I missing?

Why not work with a string, instead of messing with your buffers undo history.

(defun kill-save-indent-region (indent start end)
   (interactive "p\nr")
   (kill-new (replace-regexp-in-string
	     "^"			;or "^\\s-*"
	     (format (format "%%%ds" (case indent
				       (0 1)
				       (1 4)
				       (t indent)))
		     "")
	     (buffer-substring start end)))
   (deactivate-mark))

-ap


  reply	other threads:[~2008-12-17 19:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-17 13:31 Writing a function for a indented copy of a region Decebal
2008-12-17 15:59 ` Drew Adams
2008-12-17 16:26 ` Matthias
     [not found] ` <mailman.2956.1229529567.26697.help-gnu-emacs@gnu.org>
2008-12-17 17:09   ` Decebal
2008-12-17 18:49     ` Drew Adams
2008-12-17 19:18 ` Decebal
2008-12-17 19:47   ` Andreas Politz [this message]
2008-12-17 23:04     ` Decebal
2008-12-17 23:37     ` Decebal
2008-12-18  0:27       ` Andreas Politz
2008-12-18  0:49         ` Decebal
2008-12-17 22:58   ` Decebal
2008-12-18 13:11 ` Decebal
2008-12-18 15:05   ` Andreas Politz
2008-12-18 15:48     ` Decebal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1229543337.962004@arno.fh-trier.de \
    --to=politza@fh-trier.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).