My first attempt to change the code

81 views
Skip to first unread message

gar

unread,
Jul 23, 2019, 1:20:51 PM7/23/19
to leo-editor
Stumbled with uncommon and uncomfortable editor behavior I decided to improve it a little.
I'd like to implement the following functions:

- highlight word under cursor in the current edit
- fast find word under cursor (similar to C-S-f but w/o but for the current edit only and w/o need the word to be selected, like */# in vim)
- jump to empty line above/below the current cursor position ({/} in vim)
- highlight open/closing bracket (and jump to it as % does in vim)
- delete line from here up to the begin/end (D in vim)
- join lines (J in vim)

This is the absolute minimum the editor must provide to make me like it :-)

Can somebody please tell me 

- which branch to take as a basis
- what part of code to investigate (I tried to find something relevant and failed, still dont understand where all this editor commands reside)

Not sure that my attempt succeed but cannot suffer from this missing features anymore :-)

vitalije

unread,
Jul 23, 2019, 5:19:39 PM7/23/19
to leo-editor
This is what I would done:
Open leoSettings.leo, there is a menu command in the Settings menu.
Look at the node:
@settings-->Keyboard shortcuts-->@keys EKR bindings-->@shortcuts Cursor Moves

There you can see commands for moving cursor in the body pane and their default shortcuts.
To select word you can see there is a command named `extend-to-word` which by default is bound to Ctrl-w.
Then you can open LeoPyRef.leo (and make your own copy of that file with the name LeoPy.leo). Then you can use quick search or find to search for the string `extend-to-word` and you would get the node at the:
Code-->Command classes-->@file ../commands/editCommands.py-->class EditCommandsClass-->ec: move cursor-->ec.extend-to-word


There is the implementation of the command, so you can learn how it is done and perhaps find what you need to change to get the effect you want.

But you should not change that function. Instead you should copy the code to a new node and change it there. You can then make a script button out of this node but first you should change the headline to something like this:
my-new-word-search-command @key=Ctrl-Shift-f
Of course you should give it a meaningful name and choose shortcut to your liking.  When you make script button by clicking in the button in toolbar with the label `script-button`, the code inside this node and its subtree will be bound to the shortcut and every time you press this key combination the code will be executed.

As I understand you wish to start search for the word containing cursor and only in the current body. The script to do this is as follows:

c.editCommands.extendToWord(None)

c.frame.nav.children()[4].setCurrentIndex(4)

c.executeMinibufferCommand('find-quick-selected')



To construct this script I had to look in leoPlugins.leo in the quicksearch.py plugin, to find that c.frame.nav is the widget representing 'Nav' pane. To find that combo box is the child at index 4, I used small script:
for ch in c.frame.nav.children():
    g
.es(ch.objectName())
Looking in the combo box you can find that the item at index 4 is 'Node' for searching only inside current node.

Once you are satisfied with the command, you can change its headline to be '@command <name of command> @key=<shortcut>' , then copy and paste it in your myLeoSettings.leo under the `@settings` node. Every time you start Leo after this, it will have this command bound to your shortcut just as if it were a command implemented in the Leo core.

HTH Vitalije


vitalije

unread,
Jul 23, 2019, 5:29:40 PM7/23/19
to leo-editor
For other commands the process is similar. You investigate similar commands in Leo, copy them to your workbook.leo or other outline, change them to your needs, and turn them in the commands in your myLeoSettings.leo. 

If you wish them to become part of the Leo core, you can turn them in the methods or functions decorated by `@g.command('name-of-command')`, just like the many other commands you can find inside Leo. Put them somewhere where similar commands are located and make it a new branch based on devel branch. Later Edward can merge them into devel, or if you are sure your changes would be accepted you can perhaps directly change the devel branch and `git push` them.

Vitalije

Matt Wilkie

unread,
Jul 24, 2019, 12:19:54 AM7/24/19
to leo-editor
This is gold. Thank you.

gar

unread,
Jul 24, 2019, 12:51:08 AM7/24/19
to leo-e...@googlegroups.com
Vitalije, thanks for such an exhaustive manual!

2019-07-24 7:19 GMT+03:00, Matt Wilkie <map...@gmail.com>:
> This is gold. Thank you.
>
> --
> 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/d452a58c-abe7-4f3f-93d4-cd780f6e22e6%40googlegroups.com.
>

Edward K. Ream

unread,
Jul 24, 2019, 7:55:19 AM7/24/19
to leo-editor
On Tue, Jul 23, 2019 at 12:20 PM gar <gar...@gmail.com> wrote:

Can somebody please tell me 

- which branch to take as a basis

Use a branch based on devel.
- what part of code to investigate (I tried to find something relevant and failed, still dont understand where all this editor commands reside)

As Vitalije says, start with the extend-to-word command, that is, ec.extendToWord and related code.

Vitalije's suggestions are spot on. In addition unitTest.leo contains @test extend-to-word, which you could use as the basis for a unit test for your new code.  But you might be better off making your own unit tests.

Edward

Edward K. Ream

unread,
Jul 24, 2019, 7:56:20 AM7/24/19
to leo-editor
On Wed, Jul 24, 2019 at 6:55 AM Edward K. Ream <edre...@gmail.com> wrote:

As Vitalije says, start with the extend-to-word command, that is, ec.extendToWord and related code.

I'll be happy to answer any questions you might have about the code.

Edward

gar

unread,
Jul 24, 2019, 9:45:15 AM7/24/19
to leo-e...@googlegroups.com

Edward, I can see that when I type closing brace - matching open one is highlighted for a moment.
Can you please tell me where to find corresponding piece of code?
I'd like to program some logic which must work when user types.

Edward K. Ream

unread,
Jul 24, 2019, 11:28:55 AM7/24/19
to leo-editor
On Wed, Jul 24, 2019 at 8:45 AM gar <gar...@gmail.com> wrote:

Edward, I can see that when I type closing brace - matching open one is highlighted for a moment.
Can you please tell me where to find corresponding piece of code?

leoSettings contains @bool flash-matching-brackets

Searching for flash-matching-brackets finds:

self.flashMatchingBrackets = cf.getBool(
    'flash-matching-brackets')

in class EditCommandsClass.  Searching for flashMatchingBrackets finds ec.doPlainChar, which then calls flashMatchingBracketsHelper which calls flashCharacter.

Edward
Reply all
Reply to author
Forward
0 new messages