From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.help Subject: RE: Writing a function for a indented copy of a region Date: Wed, 17 Dec 2008 10:49:01 -0800 Message-ID: <002901c96078$20dbd670$c2b22382@us.oracle.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1229539800 15705 80.91.229.12 (17 Dec 2008 18:50:00 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Dec 2008 18:50:00 +0000 (UTC) To: "'Decebal'" , Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 17 19:51:03 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 1LD1Tw-0006lO-KZ for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Dec 2008 19:50:52 +0100 Original-Received: from localhost ([127.0.0.1]:37496 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LD1Si-0007cS-BS for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Dec 2008 13:49:36 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LD1SO-0007c3-7F for help-gnu-emacs@gnu.org; Wed, 17 Dec 2008 13:49:16 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LD1SL-0007br-NU for help-gnu-emacs@gnu.org; Wed, 17 Dec 2008 13:49:15 -0500 Original-Received: from [199.232.76.173] (port=54600 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LD1SL-0007bo-E0 for help-gnu-emacs@gnu.org; Wed, 17 Dec 2008 13:49:13 -0500 Original-Received: from rcsinet13.oracle.com ([148.87.113.125]:51501 helo=rgminet13.oracle.com) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LD1SK-0004Ue-Tc for help-gnu-emacs@gnu.org; Wed, 17 Dec 2008 13:49:13 -0500 Original-Received: from acsinet13.oracle.com (acsinet13.oracle.com [141.146.126.235]) by rgminet13.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id mBHInZjG016232 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 17 Dec 2008 18:49:36 GMT Original-Received: from acsmt702.oracle.com (acsmt702.oracle.com [141.146.40.80]) by acsinet13.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id mBHInSTx027695; Wed, 17 Dec 2008 18:49:29 GMT Original-Received: from dradamslap1 (/130.35.178.194) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 17 Dec 2008 18:49:00 +0000 X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 In-Reply-To: Thread-Index: Aclgb1y4o74AMtHZTT+umcKOxAhDPAAADvEw X-Source-IP: acsmt702.oracle.com [141.146.40.80] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A090201.4949499E.00DC:SCFSTAT928724,ss=1,fgs=0 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 1) 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:60740 Archived-At: > > > 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.