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: Andreas Politz
Subject: Re: Writing a function for a indented copy of a region
Date: Wed, 17 Dec 2008 20:47:51 +0100
User-agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018)

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 via email to

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