From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "weber" Newsgroups: gmane.emacs.help Subject: Re: Keep-only-column Date: 9 Feb 2007 03:10:33 -0800 Organization: http://groups.google.com Message-ID: <1171019432.995872.293180@v45g2000cwv.googlegroups.com> References: <1170962128.311804.182850@v45g2000cwv.googlegroups.com> <1170962615.632938.25870@j27g2000cwj.googlegroups.com> <531k72F1qnjq5U3@mid.individual.net> <5332esF1q7gjvU1@mid.individual.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: sea.gmane.org 1171021303 4506 80.91.229.12 (9 Feb 2007 11:41:43 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 9 Feb 2007 11:41:43 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 09 12:41:37 2007 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 1HFU8H-0001iI-BX for geh-help-gnu-emacs@m.gmane.org; Fri, 09 Feb 2007 12:41:37 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HFU8G-0000b1-DU for geh-help-gnu-emacs@m.gmane.org; Fri, 09 Feb 2007 06:41:36 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!v45g2000cwv.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 114 Original-NNTP-Posting-Host: 200.213.54.31 Original-X-Trace: posting.google.com 1171019438 15332 127.0.0.1 (9 Feb 2007 11:10:38 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 9 Feb 2007 11:10:38 +0000 (UTC) In-Reply-To: <5332esF1q7gjvU1@mid.individual.net> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1,gzip(gfe),gzip(gfe) X-HTTP-Via: 1.1 fw.datacom-telematica.com.br:3128 (squid/2.5.STABLE6) Complaints-To: groups-abuse@google.com Injection-Info: v45g2000cwv.googlegroups.com; posting-host=200.213.54.31; posting-account=pG57fA0AAADtQ-4h1MyvjXjZpSNZC0zj Original-Xref: shelby.stanford.edu gnu.emacs.help:145400 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:41005 Archived-At: On 9 fev, 07:58, Marc Tfardy wrote: > Marc Tfardy schrieb: > > > > > weber wrote: > > >> Quite frequently I paste stuff which is space-separated. > > >> I would really like a function that would let me select a region and > > >> keep only a column, deleting all the rest. > > >> Anyone knows how I could implement something like this? > > >> I can do it with regexps, but then I have to come up with a new one > > >> every time... > > >> TIA > > >> HS > > > > I think the format of "stuff" can't be understood from above :) Here > > > it is: > > > > variable BLABLA = 438438 > > > variable LABLAC = 312 > > > variable DAUSH = 43538 > > > > Apply to that region: keep-only-column 2 makes it: > > > BLABLA > > > LABLAC > > > DAUSHD > > > Here my draft. Probalby inefficient, but it does the job. > > > (defun trim-rectangle (start end) > > "Trims region to rectangle." > > (interactive "r") > > (save-excursion > > (let ((offset 0) > > (width 0) > > (lines (count-lines start end)) > > (i 0)) > > (goto-char start) > > (beginning-of-line) > > (setq offset (- start (point))) > > (goto-char end) > > (beginning-of-line) > > (forward-char offset) > > (setq width (- end (point))) > > (goto-char start) > > (while (< i lines) > > (beginning-of-line) > > (delete-region (point) (+ (point) offset)) > > (forward-char width) > > (delete-region (point) (line-end-position)) > > (end-of-line) > > (forward-char) > > (setq i (+ i 1)))))) > > And now slightly tuned version with extra feature - deleting > trailing whitespaces in the region: > > (defun trim-rectangle (start end &optional delete-whitespace) > "Trims region to rectangle. With \\[universal-argument] deletes > trailing whitespaces." > (interactive "*r\nP") > (save-match-data > (save-excursion > (let ((offset 0) > (width 0) > (lines (count-lines start end)) > (curren-point 0) > (i 0)) > (goto-char start) > (setq offset (current-column)) > (goto-char end) > (setq width (- (current-column) offset)) > (goto-char start) > (while (< i lines) > (beginning-of-line) > (setq current-point (point)) > (delete-region current-point (+ current-point offset)) > (forward-char width) > (delete-region (point) (line-end-position)) > (if delete-whitespace > (progn > (beginning-of-line) > (if (re-search-forward "[ \t]*$" (line-end-position) t) > (delete-region(match-beginning 0) (match-end 0))) > (end-of-line))) > (forward-char) > (setq i (+ i 1))))))) > > regards > > Marc Hello. Here's a version to apply the function to a region. If there is a easier way plese tell me! (defun keep-column-on-region (beg end &optional arg) "Apply keep-column over region." (interactive "r\np") (save-excursion (if mark-active (let ((beg (region-beginning)) (end (copy-marker (region-end)))) (goto-char beg) (while (< (point) end) (beginning-of-line) (keep-column arg) (next-line 1)))))) Also, is there a way to make the ARG 0 (instead of 1) when it's not supplied ? Thanks weber