From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas Politz Newsgroups: gmane.emacs.help Subject: Re: Writing a function for a indented copy of a region Date: Wed, 17 Dec 2008 20:47:51 +0100 Organization: FH-Trier Message-ID: <1229543337.962004@arno.fh-trier.de> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1229546478 7606 80.91.229.12 (17 Dec 2008 20:41:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Dec 2008 20:41:18 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 17 21:42:23 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1LD3DU-0002ZK-TF for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Dec 2008 21:42:01 +0100 Original-Received: from localhost ([127.0.0.1]:38311 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LD3CI-0005jk-S9 for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Dec 2008 15:40:46 -0500 Original-Path: news.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news-2.dfn.de!news-stu1.dfn.de!news.belwue.de!news.uni-kl.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 47 Original-NNTP-Posting-Host: 143-93-54-11.arno.fh-trier.de Original-X-Trace: news.uni-kl.de 1229543387 15359 143.93.54.11 (17 Dec 2008 19:49:47 GMT) Original-X-Complaints-To: usenet@news.uni-kl.de Original-NNTP-Posting-Date: Wed, 17 Dec 2008 19:49:47 +0000 (UTC) User-Agent: Mozilla-Thunderbird 2.0.0.17 (X11/20081018) In-Reply-To: Cache-Post-Path: arno.fh-trier.de!unknown@dslb-088-069-055-224.pools.arcor-ip.net X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) Original-Xref: news.stanford.edu gnu.emacs.help:165414 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:60746 Archived-At: Decebal wrote: > On 17 dec, 14:31, Decebal 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