unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* rectangular region kill saves leading whitespaces
@ 2018-10-30  5:07 Garreau, Alexandre
  2018-10-30  5:16 ` `move-end-of-line' in `rectangular region': `goto-longest-line' Garreau, Alexandre
  2018-10-30 11:06 ` rectangular region kill saves leading whitespaces Kaushal Modi
  0 siblings, 2 replies; 10+ messages in thread
From: Garreau, Alexandre @ 2018-10-30  5:07 UTC (permalink / raw)
  To: emacs-devel

Try copying that (without “> ” prefixes) with rectangular selection:
> a
> aaa
> aaaaaaaaaaaaaaaaaaaaaaa
> aaa
> a

It copies created leading whitespaces at each line shorter than the
longest one.

It is annoying to me: is it intended to be as it is, is it a bug, or a
non-existing-yet feature?



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

* `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30  5:07 rectangular region kill saves leading whitespaces Garreau, Alexandre
@ 2018-10-30  5:16 ` Garreau, Alexandre
  2018-10-30  7:37   ` Eli Zaretskii
                     ` (2 more replies)
  2018-10-30 11:06 ` rectangular region kill saves leading whitespaces Kaushal Modi
  1 sibling, 3 replies; 10+ messages in thread
From: Garreau, Alexandre @ 2018-10-30  5:16 UTC (permalink / raw)
  To: emacs-devel

On 2018-10-30 at 06:07, Garreau, Alexandre wrote:
> Try copying that (without “> ” prefixes) with rectangular selection:
>> a
>> aaa
>> aaaaaaaaaaaaaaaaaaaaaaa
>> aaa
>> a

(This is a followup because initially I wanted to talk about this in my
other mail, and subject are near.)

Something I wished existed out of the box with rectangular-selection, is
C-e (`move-end-of-line') going not at the end of the current line, but
at “max pos” horizontally, that is, on the current line, n chars away
where n is the number of chars of the longest line in (I guess,
non-rectangular) region.  As C-e is often used, I wished its first call
went at the end-of-line, and only a second times it will go at a such
position.

I thought Drew Adams’ `goto-longest-line', strangely not added in
misc.el, 9 years ago, might do:

;---------------8<-----------------------
(defun goto-longest-line (beg end)
  "Go to the first of the longest lines in the region or buffer.
If the region is active, it is checked.
If not, the buffer (or its restriction) is checked.

Returns a list of three elements:

 (LINE LINE-LENGTH OTHER-LINES LINES-CHECKED)

LINE is the first of the longest lines measured.
LINE-LENGTH is the length of LINE.
OTHER-LINES is a list of other lines checked that are as long as LINE.
LINES-CHECKED is the number of lines measured.

Interactively, a message displays this information.

If there is only one line in the active region, then the region is
deactivated after this command, and the message mentions only LINE and
LINE-LENGTH.  

If this command is repeated, it checks for the longest line after the
cursor.  That is *not* necessarily the longest line other than the
current line.  That longest line could be before or after the current
line:
> To search only from the current line forward, not throughout the
> buffer, you can use `C-SPC' to set the mark, then use this
> \(repeatedly)."
>   (interactive
>    (if (or (not mark-active) (null (mark)))
>        (list (point-min) (point-max))
>      (if (< (point) (mark))
>          (list (point) (mark))
>        (list (mark) (point)))))
>   (when (and (not mark-active) (= beg end))
>     (error "The buffer is empty"))
>   (when (and mark-active (> (point) (mark))) (exchange-point-and-mark))
>   (when (< end beg) (setq end (prog1 beg (setq beg end))))
>   (when (eq this-command last-command)
>     (forward-line 1) (setq beg (point)))
>   (goto-char beg)
>   (when (eobp) (error "End of buffer"))
>   (cond ((<= end (save-excursion
>                    (goto-char beg) (forward-line 1) (point)))
>          (beginning-of-line)
>          (when (require 'hl-line nil t)
>            (let ((hl-line-mode t)) (hl-line-highlight))
>            (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
>          (let ((lineno (line-number-at-pos))
>                (chars (save-excursion (end-of-line) (current-column))))
>            (message "Only line %d: %d chars" lineno chars)
>            (let ((visible-bell t)) (ding))
>            (setq mark-active nil)
>            (list lineno chars nil 1)))
>         (t
>          (let* ((start-line (line-number-at-pos))
>                 (max-width 0)
>                 (line start-line)
>                 long-lines col)
>            (when (eobp) (error "End of buffer"))
>            (while (and (not (eobp)) (< (point) end))
>              (end-of-line)
>              (setq col (current-column))
>              (when (>= col max-width)
>                (if (= col max-width)
>                    (setq long-lines (cons line long-lines))
>                  (setq long-lines (list line)))
>                (setq max-width col))
>              (forward-line 1)
>              (setq line (1+ line)))
>            (setq long-lines (nreverse long-lines))
>            (let ((lines long-lines))
>              (while (and lines (> start-line (car lines))) (pop lines))
>              (goto-char (point-min))
>              (when (car lines) (forward-line (1- (car lines)))))
>            (when (require 'hl-line nil t)
>              (let ((hl-line-mode t)) (hl-line-highlight))
>              (add-hook 'pre-command-hook #'hl-line-unhighlight nil t))
>            (when (interactive-p)
>              (let ((others (cdr long-lines)))
>                (message
>                 "Line %d: %d chars%s (%d lines measured)"
>                 (car long-lines) max-width
>                 (concat
>                  (and others
>                       (format ", Others: {%s}"
>                               (mapconcat
>                                (lambda (line) (format "%d" line))
>                                (cdr long-lines) ", "))))
>                 (- line start-line))))
>            (list (car long-lines) max-width (cdr long-lines)
>                  (- line start-line))))))



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

* Re: `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30  5:16 ` `move-end-of-line' in `rectangular region': `goto-longest-line' Garreau, Alexandre
@ 2018-10-30  7:37   ` Eli Zaretskii
  2018-10-30 10:24     ` Garreau, Alexandre
  2018-10-30 11:40   ` Kaushal Modi
  2018-10-30 13:20   ` Stefan Monnier
  2 siblings, 1 reply; 10+ messages in thread
From: Eli Zaretskii @ 2018-10-30  7:37 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: emacs-devel

> From: "Garreau\, Alexandre" <galex-713@galex-713.eu>
> Date: Tue, 30 Oct 2018 06:16:11 +0100
> 
> On 2018-10-30 at 06:07, Garreau, Alexandre wrote:
> > Try copying that (without “> ” prefixes) with rectangular selection:
> >> a
> >> aaa
> >> aaaaaaaaaaaaaaaaaaaaaaa
> >> aaa
> >> a
> 
> (This is a followup because initially I wanted to talk about this in my
> other mail, and subject are near.)
> 
> Something I wished existed out of the box with rectangular-selection, is
> C-e (`move-end-of-line') going not at the end of the current line, but
> at “max pos” horizontally, that is, on the current line, n chars away
> where n is the number of chars of the longest line in (I guess,
> non-rectangular) region.  As C-e is often used, I wished its first call
> went at the end-of-line, and only a second times it will go at a such
> position.

cua-rect can do that.



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

* Re: `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30  7:37   ` Eli Zaretskii
@ 2018-10-30 10:24     ` Garreau, Alexandre
  2018-10-30 10:49       ` Eli Zaretskii
  0 siblings, 1 reply; 10+ messages in thread
From: Garreau, Alexandre @ 2018-10-30 10:24 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Le 30/10/2018 à 09h37, Eli Zaretskii a écrit :
>> From: "Garreau\, Alexandre" <galex-713@galex-713.eu>
>> Date: Tue, 30 Oct 2018 06:16:11 +0100
>> 
>> On 2018-10-30 at 06:07, Garreau, Alexandre wrote:
>> > Try copying that (without “> ” prefixes) with rectangular selection:
>> >> a
>> >> aaa
>> >> aaaaaaaaaaaaaaaaaaaaaaa
>> >> aaa
>> >> a
>> 
>> (This is a followup because initially I wanted to talk about this in my
>> other mail, and subject are near.)
>> 
>> Something I wished existed out of the box with rectangular-selection, is
>> C-e (`move-end-of-line') going not at the end of the current line, but
>> at “max pos” horizontally, that is, on the current line, n chars away
>> where n is the number of chars of the longest line in (I guess,
>> non-rectangular) region.  As C-e is often used, I wished its first call
>> went at the end-of-line, and only a second times it will go at a such
>> position.
>
> cua-rect can do that.

How?  And what about without cua-mode (btw cua-rect isn’t autoloading,
requires loading cua-rect/mode first)?



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

* Re: `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30 10:24     ` Garreau, Alexandre
@ 2018-10-30 10:49       ` Eli Zaretskii
  2018-10-30 11:20         ` Garreau, Alexandre
  0 siblings, 1 reply; 10+ messages in thread
From: Eli Zaretskii @ 2018-10-30 10:49 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: emacs-devel

> From: "Garreau\, Alexandre" <galex-713@galex-713.eu>
> Cc: emacs-devel@gnu.org
> Date: Tue, 30 Oct 2018 11:24:11 +0100
> 
> > cua-rect can do that.
> 
> How?

Activate cua-rectangle-mark-mode via M-x, and then move cursor, in
particular beyond the last character of a line.  You will see what it
does right away.

> And what about without cua-mode

You don't need CUA mode to activate cua-rectangle-mark-mode.

> (btw cua-rect isn’t autoloading, requires loading cua-rect/mode
> first)?

cua-rectangle-mark-mode is autoloaded here.  Did you try that?



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

* Re: rectangular region kill saves leading whitespaces
  2018-10-30  5:07 rectangular region kill saves leading whitespaces Garreau, Alexandre
  2018-10-30  5:16 ` `move-end-of-line' in `rectangular region': `goto-longest-line' Garreau, Alexandre
@ 2018-10-30 11:06 ` Kaushal Modi
  2018-10-30 11:28   ` Garreau, Alexandre
  1 sibling, 1 reply; 10+ messages in thread
From: Kaushal Modi @ 2018-10-30 11:06 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 753 bytes --]

> It copies created leading whitespaces at each line shorter than the
longest one.

I am assuming that you mean the trailing (towards the end of line) space.

> It is annoying to me: is it intended to be as it is, is it a bug, or a
non-existing-yet feature?

I see that copying of whitespace as a feature, as I have needed that many
times. It's really helpful when you are pasting a rectangle block of text
in between some other text.

Also, as the name says, it copies a "reactangle" of text. Without the
whitespace, the block won't be a rectangle.

Look into delete-trailing-whitespace[0] function, which you can even add to
the after-save-hook.

--
Kaushal Modi

[0]:
https://www.gnu.org/software/emacs/manual/html_node/emacs/Useless-Whitespace.html

[-- Attachment #2: Type: text/html, Size: 1247 bytes --]

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

* Re: `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30 10:49       ` Eli Zaretskii
@ 2018-10-30 11:20         ` Garreau, Alexandre
  0 siblings, 0 replies; 10+ messages in thread
From: Garreau, Alexandre @ 2018-10-30 11:20 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Le 30/10/2018 à 12h49, Eli Zaretskii a:
>> (btw cua-rect isn’t autoloading, requires loading cua-rect/mode
>> first)?
>
> cua-rectangle-mark-mode is autoloaded here.  Did you try that?

No, thanks

>> And what about without cua-mode
>   
> You don't need CUA mode to activate cua-rectangle-mark-mode.
    
Okay, CUA-* looks pretty fancy and complex though: different region
highlight color, new keybindings…  Why can’t that be done with normal,
simple rectangle-mode?

Also I’m not sure but it seems to behaves strangely with undo history
(sometimes only some lines are reverted instead of all, etc.).

>> From: "Garreau\, Alexandre" <galex-713@galex-713.eu>
>> Cc: emacs-devel@gnu.org
>> Date: Tue, 30 Oct 2018 11:24:11 +0100
>>> cua-rect can do that.
>> 
>> How?
>
> Activate cua-rectangle-mark-mode via M-x, and then move cursor, in
> particular beyond the last character of a line.  You will see what it
> does right away.

No it doesn’t: it copy invented blank characters as well (and there’s no
way to go at the same column as the last of the longest line of the
region).  However at some point, using some combinations of actions or
of the specified new (8!?) proposed temp keybindings, I succeeded in
having a line ends different for each line of the rectangle (at least it
was hightlighted to suggest so).



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

* Re: rectangular region kill saves leading whitespaces
  2018-10-30 11:06 ` rectangular region kill saves leading whitespaces Kaushal Modi
@ 2018-10-30 11:28   ` Garreau, Alexandre
  0 siblings, 0 replies; 10+ messages in thread
From: Garreau, Alexandre @ 2018-10-30 11:28 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Emacs developers

Le 30/10/2018 à 07h06, Kaushal Modi a écrit :
>> It copies created leading whitespaces at each line shorter than the
>> longest one.
>
> I am assuming that you mean the trailing (towards the end of line) space.

Yes! sorry.

>> It is annoying to me: is it intended to be as it is, is it a bug, or a
>> non-existing-yet feature?
>
> I see that copying of whitespace as a feature, as I have needed that many
> times. It's really helpful when you are pasting a rectangle block of text
> in between some other text.

I don’t understand clearly and concretely what do you mean as an
example.

> Also, as the name says, it copies a "reactangle" of text. Without the
> whitespace, the block won't be a rectangle.

I understand: that’s why I wasn’t sure.  Initially when discovering
rectangular selection I believed this did must be a feature.

> Look into delete-trailing-whitespace[0] function, which you can even add to
> the after-save-hook.

Okay so that’s the intended workaround…

However I find it unclean: most of times it should do, but if there
really were trailing whitespaces initially, it will either add some to
them, or remove them all.  Isn’t there a way to properly save the end of
lines?

That might be because you use rectangular selection so that to simply
filter out lines ends and beginning before and past a certain line
column (I often do that to get rid of some indentation, or “> ” line
prefixes, as it is faster than normal (non-rectangular) copy/pasting,
then rectangularely kill the ends/beginnings).



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

* Re: `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30  5:16 ` `move-end-of-line' in `rectangular region': `goto-longest-line' Garreau, Alexandre
  2018-10-30  7:37   ` Eli Zaretskii
@ 2018-10-30 11:40   ` Kaushal Modi
  2018-10-30 13:20   ` Stefan Monnier
  2 siblings, 0 replies; 10+ messages in thread
From: Kaushal Modi @ 2018-10-30 11:40 UTC (permalink / raw)
  To: Garreau, Alexandre; +Cc: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]

On Tue, Oct 30, 2018, 1:16 AM Garreau, Alexandre <galex-713@galex-713.eu>
wrote:

> On 2018-10-30 at 06:07, Garreau, Alexandre wrote:
> > Try copying that (without “> ” prefixes) with rectangular selection:
> >> a
> >> aaa
> >> aaaaaaaaaaaaaaaaaaaaaaa
> >> aaa
> >> a
>
> (This is a followup because initially I wanted to talk about this in my
> other mail, and subject are near.)
>
> Something I wished existed out of the box with rectangular-selection, is
> C-e (`move-end-of-line') going not at the end of the current line, but
> at “max pos” horizontally, that is, on the current line, n chars away
> where n is the number of chars of the longest line in (I guess,
> non-rectangular) region.  As C-e is often used, I wished its first call
> went at the end-of-line, and only a second times it will go at a such
> position.
>

I had ended up writing something in elisp that takes care of that
transparently. Search for ";; Rectangle" in
https://github.com/kaushalmodi/.emacs.d/blob/master/setup-files/setup-editing.el
.

That code "just works" for few years (I think) now.

[-- Attachment #2: Type: text/html, Size: 1659 bytes --]

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

* Re: `move-end-of-line' in `rectangular region': `goto-longest-line'
  2018-10-30  5:16 ` `move-end-of-line' in `rectangular region': `goto-longest-line' Garreau, Alexandre
  2018-10-30  7:37   ` Eli Zaretskii
  2018-10-30 11:40   ` Kaushal Modi
@ 2018-10-30 13:20   ` Stefan Monnier
  2 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2018-10-30 13:20 UTC (permalink / raw)
  To: emacs-devel

> Something I wished existed out of the box with rectangular-selection, is
> C-e (`move-end-of-line') going not at the end of the current line, but
> at “max pos” horizontally, that is, on the current line, n chars away
> where n is the number of chars of the longest line in (I guess,
> non-rectangular) region.  As C-e is often used, I wished its first call

Patch welcome,


        Stefan




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

end of thread, other threads:[~2018-10-30 13:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-30  5:07 rectangular region kill saves leading whitespaces Garreau, Alexandre
2018-10-30  5:16 ` `move-end-of-line' in `rectangular region': `goto-longest-line' Garreau, Alexandre
2018-10-30  7:37   ` Eli Zaretskii
2018-10-30 10:24     ` Garreau, Alexandre
2018-10-30 10:49       ` Eli Zaretskii
2018-10-30 11:20         ` Garreau, Alexandre
2018-10-30 11:40   ` Kaushal Modi
2018-10-30 13:20   ` Stefan Monnier
2018-10-30 11:06 ` rectangular region kill saves leading whitespaces Kaushal Modi
2018-10-30 11:28   ` Garreau, Alexandre

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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