It would be good to have a miscellany of little script examples like this easily available, to give people assistance with learning Leo scripting. I know a few have been published as Github gists in the past, and there are some clues in the Leoeditor.com documentation, but a few dozen snippets on one place, with some annotation, would be very useful, I think. I might even have a couple of my own to contribute...
J^n On Monday, January 2, 2023 at 5:14:47 AM UTC [email protected] wrote: > Leo has commands to capitalize, upcase, and lowercase the body text or > selections of the body text. I could not find a command to convert to > title case (first letter of every word capitalized). So here is a script > to do so. > > The script does not take into account any tricky edge cases - it just uses > the Python string.title() method. The command is undoable. > > """Convert selection or body to title case.""" > w = c.frame.body.wrapper > p = c.p > s = p.b > u = c.undoer > > start, end = w.getSelectionRange() > use_entire = start == end # no selection, convert entire body > > undoType = 'title-case-body-selection' > undoData = u.beforeChangeNodeContents(p) > > if use_entire: > p.b = s.title() > else: > sel = s[start:end] > head, tail = s[:start], s[end:] > p.b = head + sel.title() + tail > > c.setChanged() > p.setDirty() > u.afterChangeNodeContents(p, undoType, undoData) > c.redraw() > -- 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/54de3190-80e1-48b9-87df-0ecd39fd4f38n%40googlegroups.com.
