The attached Leo outline contains a script to highlight the line in the 
body that has the cursor.  You can activate it by selecting the top-most 
node, then pressing CTRL-b.  After this, the cursor line should become 
highlighted anywhere in the outline.  Other outlines will not be affected.

If you want to try it out in another outline, copy the script, paste it 
into the other outline, and run it with CTRL-b.

Activating the highlighting this way is a bit strange, but lets you try it 
out without needing to modify Leo itself or install a different version.  
Eventually, the code would be included and activated when the body pane is 
constructed.

I'd like to know how you like the effect, and whether you think the 
highlight color works well enough.  

Note that the highlight color will be different for different Leo themes 
(and no-theme color schemes).  The script tries to calculate reasonable 
colors.  If the background is light, the highlight will be the same hue but 
darker.  If the background is dark, the highlight will be the same hue but 
lighter.  If the background is very light or very dark, different rules may 
be applied.

I have tried this out on every theme in Leo's theme directory.  The colors 
seem to work acceptably to me.  If you have your own theme that isn't one 
of the standard ones, I'm especially interested in how that works out.  
And, of course, post here if you notice any bugs or anomalies.

-- 
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/67e19356-791c-4b93-80d9-8d95f9f9c394n%40googlegroups.com.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1"; >
<leo_header file_format="2"/>
<vnodes>
<v t="tom.20210827170535.1"><vh>Highlight Current Line</vh>
<v t="tom.20210827170535.2"><vh>highlightCurrentLine</vh>
<v t="tom.20210827170535.3"><vh>parse_css</vh></v>
<v t="tom.20210827170535.4"><vh>assign_bg</vh></v>
<v t="tom.20210827170535.5"><vh>calc_hl</vh></v>
</v>
</v>
</vnodes>
<tnodes>
<t tx="tom.20210827170535.1">@language python
from leo.core.leoQt import QtGui
QColor = QtGui.QColor
FullWidthSelection = 0x06000 # works for both Qt5 and Qt6

w = c.frame.body.wrapper
editor = w.widget

@others

editor.cursorPositionChanged.connect(highlightCurrentLine)</t>
<t tx="tom.20210827170535.2">@language python
def highlightCurrentLine():
    """Highlight cursor line.
    
    Based in part on code from
    https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
    """

    ssm = c.styleSheetManager
    sheet = ssm.expand_css_constants(c.active_stylesheet)

    fg, bg = parse_css(sheet, 'QTextEdit')
    bg_color = QColor(bg) if bg else assign_bg(fg)
    hl_color = calc_hl(bg_color)

    selection = editor.ExtraSelection()
    selection.format.setBackground(hl_color)
    selection.format.setProperty(FullWidthSelection, True)
    selection.cursor = editor.textCursor()
    selection.cursor.clearSelection()

    editor.setExtraSelections([selection])

</t>
<t tx="tom.20210827170535.3">@language python
def parse_css(css_string, clas=''):
    """Extract colors from a css stylesheet string. 
    
    This is an extremely simple-minded function. It assumes
    that no quotation marks are being used, and that the
    first block in braces with the name clas is the controlling
    css for our widget.
    
    Returns a tuple of strings (foregound, background).
    """
    # Get first block with name matching "clas'
    block = css_string.split(clas, 1)
    block = block[1].split('{', 1)
    block = block[1].split('}', 1)

    # Split into styles separated by ";"
    styles = block[0].split(';')

    # Split into fields separated by ":"
    fields = [style.split(':') for style in styles if style.strip()]

    # Only get fields whose names are "color" and "background"
    color = bg = ''
    for style, val in fields:
        style = style.strip()
        if style == 'color':
            color = val.strip()
        elif style == 'background':
            bg = val.strip()
    return color, bg

</t>
<t tx="tom.20210827170535.4">@language python
def assign_bg(fg):
    """If fg or bg colors are missing, assign
    reasonable values.  Can happen with incorrectly
    constructed themes, or no-theme color schemes.
    
    RETURNS
    a QColor object for the background color
    """
    if not fg:
        fg = 'black' # QTextEdit default
        bg = 'white' # QTextEdit default
    if fg == 'black':
        bg = 'white' # QTextEdit default
    else:
        fg_color = QColor(fg)
        h, s, v, a = fg_color.getHsv()
        if v &lt; 128: # dark foreground
            bg = 'white'
        else:
            bg = 'black'
    return QColor(bg)
</t>
<t tx="tom.20210827170535.5">@language python
def calc_hl(bg_color):
    """Return the line highlight color.
    
    ARGUMENT
    bg_color -- a QColor object for the background color
    
    RETURNS
    a QColor object for the highlight color
    """
    h, s, v, a = bg_color.getHsv()

    if v &lt; 24:
        v = 50
        bg_color.setHsv(h, s, v, a)
    elif v &gt; 240:
        v = 220
        bg_color.setHsv(h, s, v, a)
    elif v &lt; 128:
        bg_color = bg_color.lighter(130)
    else:
        bg_color = bg_color.darker(130)

    return bg_color
</t>
</tnodes>
</leo_file>

Reply via email to