unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Keep-only-column
@ 2007-02-08 19:15 weber
  2007-02-08 19:23 ` Keep-only-column weber
  0 siblings, 1 reply; 11+ messages in thread
From: weber @ 2007-02-08 19:15 UTC (permalink / raw)
  To: help-gnu-emacs

hello again!
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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-08 19:15 Keep-only-column weber
@ 2007-02-08 19:23 ` weber
  2007-02-08 19:43   ` Keep-only-column Joost Kremers
  2007-02-08 21:55   ` Keep-only-column Marc Tfardy
  0 siblings, 2 replies; 11+ messages in thread
From: weber @ 2007-02-08 19:23 UTC (permalink / raw)
  To: help-gnu-emacs

On 8 fev, 16:15, "weber" <hug...@gmail.com> wrote:
> hello again!
> 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

Thanks again
HS

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-08 19:23 ` Keep-only-column weber
@ 2007-02-08 19:43   ` Joost Kremers
  2007-02-08 19:46     ` Keep-only-column weber
  2007-02-08 21:55   ` Keep-only-column Marc Tfardy
  1 sibling, 1 reply; 11+ messages in thread
From: Joost Kremers @ 2007-02-08 19:43 UTC (permalink / raw)
  To: help-gnu-emacs

weber wrote:
> 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

if the columns are (more or less) nicely lined up, you can use
kill-rectangle to kill a rectangle defined by point and mark, which is
bound to `C-x r k'. you'll have to apply it twice. do `C-h a rectangle RET'
to see some more useful command that can be applied to rectangles.

-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-08 19:43   ` Keep-only-column Joost Kremers
@ 2007-02-08 19:46     ` weber
  0 siblings, 0 replies; 11+ messages in thread
From: weber @ 2007-02-08 19:46 UTC (permalink / raw)
  To: help-gnu-emacs

On 8 fev, 16:43, Joost Kremers <joostkrem...@yahoo.com> wrote:
> weber wrote:
> > 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
>
> if the columns are (more or less) nicely lined up, you can use
> kill-rectangle to kill a rectangle defined by point and mark, which is
> bound to `C-x r k'. you'll have to apply it twice. do `C-h a rectangle RET'
> to see some more useful command that can be applied to rectangles.
>
> --
> Joost Kremers                                      joostkrem...@yahoo.com
> Selbst in die Unterwelt dringt durch Spalten Licht
> EN:SiS(9)

Yes, that is one simple way which I use a lot, but this keep-column
command would be better for using with the keyboard :)
Cheers
weber

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-08 19:23 ` Keep-only-column weber
  2007-02-08 19:43   ` Keep-only-column Joost Kremers
@ 2007-02-08 21:55   ` Marc Tfardy
  2007-02-09 10:47     ` Keep-only-column weber
  2007-02-09 10:58     ` Keep-only-column Marc Tfardy
  1 sibling, 2 replies; 11+ messages in thread
From: Marc Tfardy @ 2007-02-08 21:55 UTC (permalink / raw)
  To: help-gnu-emacs

weber wrote:
 > On 8 fev, 16:15, "weber" <hug...@gmail.com> wrote:
 >> hello again!
 >> 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))))))

HTH

regards

Marc

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-08 21:55   ` Keep-only-column Marc Tfardy
@ 2007-02-09 10:47     ` weber
  2007-02-09 10:58     ` Keep-only-column Marc Tfardy
  1 sibling, 0 replies; 11+ messages in thread
From: weber @ 2007-02-09 10:47 UTC (permalink / raw)
  To: help-gnu-emacs

On 8 fev, 18:55, Marc Tfardy <m...@web.de> wrote:
> weber wrote:
>
>  > On 8 fev, 16:15, "weber" <hug...@gmail.com> wrote:
>  >> hello again!
>  >> 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))))))
>
> HTH
>
> regards
>
> Marc

Here's mine, but I guess we were thinking different things...
Now I must apply it to a region

(defun keep-column (&optional arg)
  "Considering space-separated columns, keep only column ARG."
  (interactive "p")
  (save-excursion
    (let ((cnt-col 0))
      (while (not (eolp))
        (if (looking-at "[ \t\r\n]")
            (progn
              (incf cnt-col)
              (delete-char 1)
              (while (and (not (eolp)) (looking-at "[ \t\n\r]"))
                (delete-char 1)))
            (progn
              (if (= cnt-col arg)
                  (forward-char)
                  (delete-char 1))))))))

Cheers
weber

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-08 21:55   ` Keep-only-column Marc Tfardy
  2007-02-09 10:47     ` Keep-only-column weber
@ 2007-02-09 10:58     ` Marc Tfardy
  2007-02-09 11:10       ` Keep-only-column weber
  1 sibling, 1 reply; 11+ messages in thread
From: Marc Tfardy @ 2007-02-09 10:58 UTC (permalink / raw)
  To: help-gnu-emacs

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-09 10:58     ` Keep-only-column Marc Tfardy
@ 2007-02-09 11:10       ` weber
  2007-02-09 12:35         ` Keep-only-column weber
  2007-02-09 12:49         ` Keep-only-column Marc Tfardy
  0 siblings, 2 replies; 11+ messages in thread
From: weber @ 2007-02-09 11:10 UTC (permalink / raw)
  To: help-gnu-emacs

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-09 11:10       ` Keep-only-column weber
@ 2007-02-09 12:35         ` weber
  2007-02-09 12:49         ` Keep-only-column Marc Tfardy
  1 sibling, 0 replies; 11+ messages in thread
From: weber @ 2007-02-09 12:35 UTC (permalink / raw)
  To: help-gnu-emacs

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-09 11:10       ` Keep-only-column weber
  2007-02-09 12:35         ` Keep-only-column weber
@ 2007-02-09 12:49         ` Marc Tfardy
  2007-02-09 13:00           ` Keep-only-column weber
  1 sibling, 1 reply; 11+ messages in thread
From: Marc Tfardy @ 2007-02-09 12:49 UTC (permalink / raw)
  To: help-gnu-emacs

weber schrieb:

> 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))))))

You use interactive "r" which put automatically boundary of region in
beg and end, but you still compute beg (and end) self in you code.
Why?

regards

Marc

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Keep-only-column
  2007-02-09 12:49         ` Keep-only-column Marc Tfardy
@ 2007-02-09 13:00           ` weber
  0 siblings, 0 replies; 11+ messages in thread
From: weber @ 2007-02-09 13:00 UTC (permalink / raw)
  To: help-gnu-emacs

On 9 fev, 09:49, Marc Tfardy <m-t-o___CUT__IT...@web.de> wrote:
> weber schrieb:
>
> > 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))))))
>
> You use interactive "r" which put automatically boundary of region in
> beg and end, but you still compute beg (and end) self in you code.
> Why?
>
> regards
>
> Marc

True.... but it didn't work otherwise...
I think the problem is that sometimes a region may be not marked
("selected") ?
I'm accepting improvements :)
Cheers
weber

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2007-02-09 13:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-02-08 19:15 Keep-only-column weber
2007-02-08 19:23 ` Keep-only-column weber
2007-02-08 19:43   ` Keep-only-column Joost Kremers
2007-02-08 19:46     ` Keep-only-column weber
2007-02-08 21:55   ` Keep-only-column Marc Tfardy
2007-02-09 10:47     ` Keep-only-column weber
2007-02-09 10:58     ` Keep-only-column Marc Tfardy
2007-02-09 11:10       ` Keep-only-column weber
2007-02-09 12:35         ` Keep-only-column weber
2007-02-09 12:49         ` Keep-only-column Marc Tfardy
2007-02-09 13:00           ` Keep-only-column weber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).