Rev 4fb8255 completes #311 
<https://github.com/leo-editor/leo-editor/issues/311>: Simplify how code 
uses k.getArg. The new code creates two new methods, k.get1arg and 
k.getNextArg.  Rev 179c672 creates proper docstrings for these methods.

The rest of this post describes the new, simpler, way of getting user 
arguments from the command line.  Feel free to ignore...

k.get1Arg handles the next character the user types when accumulating a 
user argument from the minibuffer.  k.get1Arg handles details such as tab 
completion, backspacing, Ctrl-G etc. 

Commands should use k.get1Arg to get the first minibuffer argument and 
k.getNextArg to get all other arguments.

k.get1Arg is a state machine. It has to be because it's almost always 
waiting for the user to type the next character. Previously, the commands 
that needed user arguments inited and updated states explicitly.  But this 
has never been necessary!  The following examples could have been used from 
day one.  Its embarrassing that I didn't see this earlier.

The following examples will work in any class having a 'c' ivar bound to a 
commander.
    
Example 1: get one argument from the user:
    
    @cmd('my-command')
    def myCommand(self, event):
        k = self.c.k
        k.setLabelBlue('prompt: ')
        k.get1Arg(event, handler=self.myCommand1)
            
    def myCommand1(self, event):
        k = self.c.k
        # ----> k.arg contains the argument.
        # Finish the command.
        ...
        # Reset the minibuffer.
        k.clearState()
        k.resetLabel()
        k.showStateAndMode()
        
Example 2: get two arguments from the user:
    
    @cmd('my-command')
    def myCommand(self, event):
        k = self.c.k
        k.setLabelBlue('first prompt: ')
        k.get1Arg(event, handler=self.myCommand1)
            
    def myCommand1(self, event):
        k = self.c.k
        self.arg1 = k.arg
        k.setLabelBlue('second prompt: ')
        k.getNextArg(handler=self.myCommand2)
        
    def myCommand2(self, event):
        k = self.c.k
        # -----> k.arg contains second argument.
        # Finish the command, using self.arg1 and k.arg.
        ...
        # Reset the minibuffer.
        k.clearState()
        k.resetLabel()
        k.showStateAndMode()

*Summary*

The handler keyword argument to k.get1Arg and k.getNextArg specifies the 
next state in the state machine.

k.get1Arg contains many optional keyword arguments.  See Leo's code base 
for examples of using these.

I'll be adding an entry to the scripting miscellany section of Leo's docs 
based on this post.

All comments and questions welcome.

Edward

P.S. I tested all changes by hand in addition to running unit tests.  I saw 
several commands that could be improved, including especially shell-command.

EKR

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to