well ... yes, I have previously just totally **set** the body text (ie. I am creating a totally new node), and as you say this 'sticks'. For this new command, I want to add to any body text which is already there.
But I am puzzled about what seems to be a mixture in this example: - you get the insertion point and then use w.insert() to write your text to ... the wrapper for the body?? - but then you do p.v.b = w.getAllText() to ... set the body text to ... that which you have just set for the body wrapper? I am missing something in the paradigm here... J^n On Sunday, July 28, 2024 at 2:52:47 PM UTC+1 tbp1...@gmail.com wrote: > I think I forgot that the recommended way to do unto/redo has been changed > since I wrote that script. Also, you can directly set p.b or p.v.b and > those changes will stick. Here's one I found in Leo's code base that does > that and uses the newer undo system: > > def insertBodyTime(self: Self, event: LeoKeyEvent = None) -> None: > """Insert a time/date stamp at the cursor.""" > c, p, u = self, self.p, self.undoer > w = c.frame.body.wrapper > undoType = 'Insert Body Time' > if g.app.batchMode: > c.notValidInBatchMode(undoType) > return > bunch = u.beforeChangeBody(p) > w.deleteTextSelection() > s = self.getTime(body=True) > i = w.getInsertPoint() > w.insert(i, s) > p.v.b = w.getAllText() > u.afterChangeBody(p, undoType, bunch) > > On Sunday, July 28, 2024 at 9:17:45 AM UTC-4 jkn wrote: > >> Thanks. Hmm, looks like I was wrong about onBodyChanged being outdated - >> there seem to be hardly any uses in Leo's source, which perhaps misled me. >> >> I removed my undo code from my example... ;-) >> >> Regards >> J^n >> >> >> On Sunday, July 28, 2024 at 1:58:07 PM UTC+1 tbp1...@gmail.com wrote: >> >>> Here's one I use and it works fine. You should set up for undoing after >>> an insert, and this script does that too: >>> >>> """Insert a node with id and creation timestamp strings""" >>> >>> c.doCommandByName('insert-node') >>> undoType = 'xx-insert-node' >>> p.moveToNext() >>> w = c.frame.body.wrapper >>> >>> oldSel = w.getSelectionRange() >>> w.deleteTextSelection() >>> w.setInsertPoint(0) >>> >>> id_label = f':id: {p.gnx}\n' >>> time_string = ':created: ' + c.getTime(body=True) + '\n' >>> i = w.getInsertPoint() >>> w.insert(i, f'{id_label}{time_string}') >>> >>> c.frame.body.onBodyChanged(undoType, oldSel=oldSel) >>> c.bodyWantsFocusNow() >>> >>> On Sunday, July 28, 2024 at 8:40:32 AM UTC-4 jkn wrote: >>> >>>> I'm being a bit dim here, but: >>>> >>>> I am writing a simple command to append text to the body of a node. The >>>> only commands I have written before set the entire body text to some value. >>>> >>>> What do I have to call after using w.insert() etc. to make these >>>> changes persistent.? >>>> >>>> c.redraw() does not seem to do the trick. >>>> >>>> The (outdated?) scripting miscellany: >>>> https://leo-editor.github.io/leo-editor/scripting-miscellany.html >>>> >>>> suggests that c.frame.body.onBodyChanged should be called; but it looks >>>> like this is not longer the case. >>>> >>>> A trivial example (that I do not seem to be able to find) would be >>>> helpful - thanks. >>>> >>>> w = c.frame.body.wrapper >>>> for line in range(10): >>>> w.appendText("%d\n" % line) >>>> >>>> # now what? >>>> >>>> J^n >>>> >>>> -- 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/1b6b27ed-1532-473e-8ee9-d98676693decn%40googlegroups.com.