From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Decebal Newsgroups: gmane.emacs.help Subject: Re: Writing a function for a indented copy of a region Date: Wed, 17 Dec 2008 15:04:39 -0800 (PST) Organization: http://groups.google.com Message-ID: <147a4500-328a-4600-ad73-f67481a01139@r36g2000prf.googlegroups.com> References: <1229543337.962004@arno.fh-trier.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1229557322 11979 80.91.229.12 (17 Dec 2008 23:42:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Dec 2008 23:42:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 18 00:43:06 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 1LD62f-000513-Iw for geh-help-gnu-emacs@m.gmane.org; Thu, 18 Dec 2008 00:43:01 +0100 Original-Received: from localhost ([127.0.0.1]:33931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LD61T-0000cU-DT for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Dec 2008 18:41:47 -0500 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!r36g2000prf.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 56 Original-NNTP-Posting-Host: 84.53.123.169 Original-X-Trace: posting.google.com 1229555080 20826 127.0.0.1 (17 Dec 2008 23:04:40 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Wed, 17 Dec 2008 23:04:40 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: r36g2000prf.googlegroups.com; posting-host=84.53.123.169; posting-account=K-cdeAoAAAD_0d505kUtHXJaT5LFIu-3 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; nl; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4,gzip(gfe),gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:165428 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:60759 Archived-At: On 17 dec, 20:47, Andreas Politz wrote: > 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) > > =A0 "Put indented region in the kill-ring" > > =A0 (interactive "p") > > =A0 (setq indent (cond ((eq indent 0) 1) > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0((eq indent 1) 4) > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(t indent) > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0) > > =A0 ) > > =A0 (indent-rigidly (region-beginning) (region-end) indent) > > =A0 (copy-region-as-kill (region-beginning) (region-end)) > > =A0 (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 his= tory. > > (defun kill-save-indent-region (indent start end) > =A0 =A0(interactive "p\nr") > =A0 =A0(kill-new (replace-regexp-in-string > =A0 =A0 =A0 =A0 =A0 =A0 =A0"^" =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0;or "^\\s-*" > =A0 =A0 =A0 =A0 =A0 =A0 =A0(format (format "%%%ds" (case indent > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0(0 1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0(1 4) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0(t indent))) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0"") > =A0 =A0 =A0 =A0 =A0 =A0 =A0(buffer-substring start end))) > =A0 =A0(deactivate-mark)) > > -ap I have just started with programming in elisp. ;-} So I still need to learn a lot. I'll try to understand your code.