help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Keep-only-column


From: weber
Subject: Re: Keep-only-column
Date: 9 Feb 2007 04:35:36 -0800
User-agent: G2/1.0

On 9 fev, 08:10, "weber" <hug...@gmail.com> wrote:
> On 9 fev, 07:58, Marc Tfardy <m-t-o___CUT__IT...@web.de> 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

ARG = 0 is solved by using (interactive "P"). Tks



reply via email to

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