On Sun, Sep 27, 2020 at 12:45 AM Félix <[email protected]> wrote:

I'm trying to integrate 'selection state' by overriding the 'wrapper'
> object ( in c.frame.body ) which would be useful for commands such as
> 'execute selected script, extract node, etc...
>

[snip]

I dont wanna force "inBridge" to be false, it's gonna break everything else
> I presume!
>

You should be able to override any commander method from inside the
LeoBridgeIntegController (BIC) class. This is tricky. Here is tested code:

class Test:
    """
    An example patching a method in Leo's QTextMixin class.
    The Test class represents the LeoBridgeIntegController class.
    """

    def __init__(self, c):
        self.commander = c
        self.old_getSelectedText = c.frame.body.wrapper.getSelectedText
        g.funcToMethod(
            self.patched_getSelectedText,
            c.frame.body.wrapper,
            name='getSelectedText',
        )
        g.trace('Patched!')

    def patched_getSelectedText(self):
        """
        Call the original getSelectedText with g.app.inBridge = False.
        """
        old_in_bridge = g.app.inBridge
        try:
            g.app.inBridge = False
            func = self.old_getSelectedText
            g.trace('Calling', func)
            return func()
        except Exception:
            g.app.inBridge = old_in_bridge
            g.es_exception()
            return ''

Test(c)

To test:

- Run the script above.
- Create a node containing leading spaces. Select all the body text of that
node.
- Run the tabify command.

The output will be something like:

__init__ Patched!
patched_getSelectedText Calling <bound method QTextMixin.getSelectedText of
 <QTextEditWrapper: 2633857821064 body>>
getSelectedText ===== QTextMixin  <QTextEditWrapper: 2633857821064 body>

Notes:

- The patch should be applied to all commanders. Test by opening a new
outline.
- This is just a start. If you are going to wrap lots of methods it might
be useful to use __getattr__ in a generic wrapper.
  Having said that, I never like to use __getattr__ :-)

HTH.  I'm happy to help you on this project, so feel free to ask more
questions.

Edward

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAMF8tS0buhjqJF4d%2B8yU%3DKk9pdsyd%3DJXZEvdbRTmYsX3CUcJ0g%40mail.gmail.com.

Reply via email to