Modified headline query

17 views
Skip to first unread message

lewis

unread,
May 24, 2024, 7:37:42 PMMay 24
to leo-editor
I wrote a script to add a datestamp to the top node of a file. It modifies the headline and the headline icon changes to show it has changed.
However when I close the leo file there is no prompt to save the file and the update is lost.

I'm obviously missing some important detail and look forward to a helpful explanation of why the file is not recognised as changed.
Here is the script:

@language python

from datetime import datetime

now = datetime.now()

TIME_STAMP = (f"{now.day}/{now.month}/{now.year} {now.hour}:{now.minute}:{now.second}")

def datestamp_node():
    # POSITION
    c.selectPosition(c.rootPosition())  # select root node position and move to it
        # MOVES TO FIRST NODE
    p = c.p
   
    # NODE headline
    p.h = "datestamp Leo" + " " + TIME_STAMP  # timestamp "datestamp Leo" node
    c.setHeadString(p, p.h)  # write the new headline.
    g.es_print(f"  ==== new headline is: ", p.h)
    c.redraw(p)
   
datestamp_node()


Thomas Passin

unread,
May 25, 2024, 12:48:01 AMMay 25
to leo-editor
I think you need to mark the node as dirty - p.setDirty() - and also c.setChanged().  I'm not sure why the first doesn't also set the second, but it doesn't.

You might also want to save undo data.  Here's an example where I write text into the body and make it undoable.  The outline asks if I want to save before closing.  

p, u, = c.p, c.undoer
bunch = u.beforeChangeBody(p)

title = p.h
title += '\n' + '-'*(len(title) + 1)
time_string = ':created: ' + c.getTime(body=True) + '\n'

zettel_str = f'{title}\n\n:id: {p.gnx}\n{time_string}\n'

w = c.frame.body.wrapper
w.setInsertPoint(0)

p.b = zettel_str + p.b
u.afterChangeBody(p, 'zettel-convert-card', bunch)


For undoing a headline change, you could use u.beforeChangeHeadline() / u.afterChangeHeadline() instead. I haven't tried this to change a headline but I presume the node and outline would also show as changed.
Reply all
Reply to author
Forward
0 new messages