Nearly all Leo themes use a monospaced typeface for the body font.  This is 
what you want for programming, but for documentation and other non-code 
writing, many people would prefer a non-monospaced font.  Some might event 
wish for a serif style typeface.

The attached script installs a global script that changes the font for 
nodes that use an @language directive of rest, md, asciidoc, text, or plain.  
By default the alternate font is either Verdana or Sans-serif, but any font 
name installed on the system can be used.  If the body does not have an 
@language 
directive, the command looks up the tree to find what language is in effect.

The change comes into effect the next time a node receives focus after the 
command is run.  So after you run it, you will not see any effect on the 
currently focused node until you navigate away and return to it.  The 
command is global in scope, so it will affect all outlines.

If you want to use a different font or font size, just edit the values in 
the script.  If you have a text node but you temporarily want to see it in 
the theme's font instead, temporarily add an @language python (or some 
other programming language name) at or near the top of the node. Navigate 
away then back to the node.

This font-changing behavior will remain in effect until Leo is restarted.

Create a new node in, say, workbook.leo and paste the contents of the 
attached file into it.  Run it with the usual <CTRL-b>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/cc310b38-a2a9-4a72-8671-3338ae5e929cn%40googlegroups.com.
@language python
"""Change the body font of a node.

Called when the selected node changes.  If the language directive 
in effect is one of the specified ones, change the font family to
one of the specified fonts.  Also change the font if the body
contains lines near the start that begin with ":id:" or ":ref:".
Otherwise change it back to the normal font."""

NOSTYLE = 'LeoQTextBrowser {}'
STYLE = ('LeoQTextBrowser {font-family: Verdana, "Sans-serif";'
                           'font-size: 10pt;}')
LANGS = ('@language rest', '@language md', '@language asciidoc',
         '@language text', '@language plain')
LANGTAGS = ('@rest', '@md', '@rst', '@adoc')
TAGS = (':id:', ':ref:')

def qualifies(b, h):
    """See if we have a qualifying language or tag.

    RETURNS    
    -- True if headline h starts with one of the LANGTAGS.
    -- True if body b has a line starting with a qualifying tag or
        language directive.
    -- -1 if body b has a line starting with "@language" but
        the language is not a qualifying one.
    -- False otherwise.
    """
    if h.startswith(LANGTAGS):
        return True
    lines = b[:300].splitlines()
    lines = [l for l in lines if l.strip()]
    for line in lines:
        if line.startswith('@language'):
            if line.startswith(LANGS):
                return True
            else:
                return -1  # Tri-state logic; not a mistake
        else:
            if line.startswith(TAGS):
                return True
    return False

def set_alt_body_font(tag, keys):
    """Set body font if this node is one of the qualifying node kinds.

    Looks up the tree until a qualifying node is found. Then sets the 
    CSS style based on the qualified language or tag.
    
    Expected to be called by a hook procedure.
    """
    c = keys['c']
    css = NOSTYLE
    # Check if language or special tags qualify
    for p in c.p.self_and_parents():
        found = qualifies(p.b, p.h)
        if found is True:
            css = STYLE
            break
        elif found is not False:  # Tri-state logic!
            break

    editor = c.frame.body.wrapper.widget
    editor.setStyleSheet(css)

ud = g.user_dict
ud['alt_body_font_proc'] = set_alt_body_font

g.registerHandler('select3', ud['alt_body_font_proc'])


Reply via email to