Automatically move to the beginning/end of line

73 views
Skip to first unread message

Brian Theado

unread,
Sep 26, 2019, 8:07:05 AM9/26/19
to leo-editor
When moving down a text editor line-by-line, I rely on the common behavior of automatically moving to the end of the line when trying to scroll down past the last line. Similar for when scrolling up past the first line. Leo doesn't have this behavior, so I wrote the below script. Thought I'd share in case anyone else finds it useful.

@settings->@shortcuts:
previous-or-beginning-of-line ! text = Up
next-or-end-of-line ! text = Down
previous-or-beginning-of-line-extend-selection ! text = Shift-Up
next-or-end-of-line-extend-selection ! text = Shift-Down

@script
def lineScrollHelper(c, prefix1, prefix2, suffix):
    w = c.frame.body.wrapper
    ins = w.getInsertPoint()
    c.inCommand = False
    c.executeMinibufferCommand(prefix1 + 'line' + suffix)
    ins2 = w.getInsertPoint()
    # If the cursor didn't change, then go to beginning/end of line
    if ins == ins2:
        c.executeMinibufferCommand(prefix2 + 'of-line' + suffix)

@g.command('next-or-end-of-line')
def nextOrEndOfLine(event):
    lineScrollHelper(event['c'], 'next-', 'end-', '')

@g.command('previous-or-beginning-of-line')
def previousOrBeginningOfLine(event):
    lineScrollHelper(event['c'], 'previous-', 'beginning-', '')

@g.command('next-or-end-of-line-extend-selection')
def nextOrEndOfLineExtendSelection(event):
    lineScrollHelper(event['c'], 'next-', 'end-', '-extend-selection')

@g.command('previous-or-beginning-of-line-extend-selection')
def previousOrBeginningOfLineExtendSelection(event):
    lineScrollHelper(event['c'], 'previous-', 'beginning-', '-extend-selection')

gar

unread,
Sep 26, 2019, 9:16:11 AM9/26/19
to leo-e...@googlegroups.com
Thanks for that, Brian! Exactly what is required!

By the way, recently I discovered another oddness.
When you select several lines at the moment and try to ident/deident them by a single 'tab' or 'ctrl->` - the line above/below the cursor is also affected.
This is not exactly what user expects.
Can't you give a clue how can this be fixed? Since you have already dived into the deepness of text editing commands.

чт, 26 сент. 2019 г. в 15:07, Brian Theado <brian....@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/CAO5X8CyVRMJ%2BO0tU%3D-7MAm6qhsELhj3kV4N8X33YOh2EL%3DuhEg%40mail.gmail.com.

Edward K. Ream

unread,
Sep 26, 2019, 9:41:15 AM9/26/19
to leo-editor
On Thu, Sep 26, 2019 at 8:16 AM gar <gar...@gmail.com> wrote:
Thanks for that, Brian! Exactly what is required!

By the way, recently I discovered another oddness.
When you select several lines at the moment and try to ident/deident them by a single 'tab' or 'ctrl->` - the line above/below the cursor is also affected.

I haven't seen this.  I suspect this happens only when the lines are continued.

Edward

gar

unread,
Sep 26, 2019, 9:44:04 AM9/26/19
to leo-e...@googlegroups.com
1. move cursor to the beginning of the line
2. press shift and up twice
3. press tab
4. enjoy with how first not selected line is idented 

чт, 26 сент. 2019 г. в 16:41, Edward K. Ream <edre...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.

Edward K. Ream

unread,
Sep 26, 2019, 9:46:23 AM9/26/19
to leo-editor
On Thu, Sep 26, 2019 at 7:07 AM Brian Theado <brian....@gmail.com> wrote:
When moving down a text editor line-by-line, I rely on the common behavior of automatically moving to the end of the line when trying to scroll down past the last line. Similar for when scrolling up past the first line. Leo doesn't have this behavior, so I wrote the below script. Thought I'd share in case anyone else finds it useful.

Excellent work! Rather than relying on @script (or a plugin), I'll add these commands later today to Leo's core, and add do-nothing entries for the new commands to leoSettings.leo.  Users will then be able to bind them as they please in myLeoSettings.leo.

Edward

Edward K. Ream

unread,
Sep 26, 2019, 9:51:02 AM9/26/19
to leo-editor
On Thursday, September 26, 2019 at 8:46:23 AM UTC-5, Edward K. Ream wrote:

> I'll add these commands later today to Leo's core, and add do-nothing entries for the new commands to leoSettings.leo.

See #1353.

Edward

Edward K. Ream

unread,
Sep 26, 2019, 12:10:33 PM9/26/19
to leo-editor
On Thursday, September 26, 2019 at 8:51:02 AM UTC-5, Edward K. Ream wrote:

> I'll add these commands later today to Leo's core, and add do-nothing entries for the new commands to leoSettings.leo.
> See #1353.

Completed at 2d736a in devel.

Edward

Brian Theado

unread,
Sep 26, 2019, 12:31:35 PM9/26/19
to leo-editor
Yes, I've always noticed this behavior from Leo and rather than look into fixing it, I've trained myself when using leo to select one fewer lines than needed before I hit the tab key to indent. Maybe it isn't all that hard to fix it, but maybe some people don't see this behavior as "broken". I think it is because the cursor is on the line (even though nothing on that line is highlighted for selection). The indentation code probably operates on all the lines with selection as well as the line with the cursor.

Brian Theado

unread,
Sep 26, 2019, 12:34:48 PM9/26/19
to leo-editor
Thanks a lot, Edward! I'll give it a try later.

I discovered when using my code on a mac laptop that I had to bind to Keypad-Up and Keypad-Down. Apparently the laptop keyboard on my mac doesn't have any keys mapping to just plain Up and Down.

--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.

jkn

unread,
Sep 26, 2019, 4:23:44 PM9/26/19
to leo-editor


On Thursday, September 26, 2019 at 2:46:23 PM UTC+1, Edward K. Ream wrote:
I confess I had always assumed that these constituted some of the plethora of already-available settings! ;-o. I have never missed them much myself, so hadn't gone looking... but nice to know that they will now be there.

    J^n

gar

unread,
Sep 27, 2019, 8:27:12 AM9/27/19
to leo-e...@googlegroups.com
Actually leo's indentation method is not common for text editors. I would rather say it is very uncommon.
In conjunction with leo's way to make 'ctrl-left' and 'ctrl-right' it makes impression that your memory and habbits are broken :-)

чт, 26 сент. 2019 г. в 19:31, Brian Theado <brian....@gmail.com>:
Reply all
Reply to author
Forward
0 new messages