Leo Command To Convert To Title Case

29 views
Skip to first unread message

Thomas Passin

unread,
Feb 3, 2024, 3:50:15 PMFeb 3
to leo-editor
Converting to Title Case means to make the first letter of every word in a string capitalized, and all the other letters made lowercase.  It can be handy if you copy an all-capital string from somewhere else but you don't like all-caps and want to convert it to title case. I couldn't find a built-in Leo command to do this.  

Maybe it's there somewhere, but here's a little script to do the job.  It uses the title() method of strings. You can put it into myLeoSettings.leo as a command, button, or menu item.

"""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()

Edward K. Ream

unread,
Feb 4, 2024, 3:30:15 AMFeb 4
to leo-e...@googlegroups.com
On Sat, Feb 3, 2024 at 2:50 PM Thomas Passin <tbp1...@gmail.com> wrote:
Converting to Title Case means to make the first letter of every word in a string capitalized, and all the other letters made lowercase.  It can be handy if you copy an all-capital string from somewhere else but you don't like all-caps and want to convert it to title case. I couldn't find a built-in Leo command to do this.  

Maybe it's there somewhere, but here's a little script to do the job. 

Thanks for this. A cff shows that Leo never calls string.title.

Edward

HaveF HaveF

unread,
Feb 4, 2024, 8:01:23 PMFeb 4
to leo-editor
Thanks, Thomas, I like these little scripts :-)
It can study the api bit by bit, like, I didn't use undoer before

Reply all
Reply to author
Forward
0 new messages