Scite Title Case |
|
function titlecase(str) --os.setlocale('pt_BR','ctype') -- set a locale as needed buf ={} local sel = editor:GetSelText() for word in string.gfind(sel, "%S+") do local first = string.sub(word,1,1) table.insert(buf,string.upper(first) .. string.lower(string.sub(word,2))) end editor:ReplaceSel(table.concat(buf," ")) end
function titlecase(str) result='' for word in string.gfind(str, "%S+") do local first = string.sub(word,1,1) result = (result .. string.upper(first) .. string.lower(string.sub(word,2)) .. ' ') end return result end
Note that using table.concat() in this case is more efficient than appending to a string; see [Programming in Lua 11.6 String Buffers].
-- normalize case of words in 'str' to Title Case function titlecase(str) local buf = {} for word in string.gfind(str, "%S+") do local first, rest = string.sub(word, 1, 1), string.sub(word, 2) table.insert(buf, string.upper(first) .. string.lower(rest)) end return table.concat(buf, " ") end -- For use in SciTE function scite_titlecase() local sel = editor:GetSelText() editor:ReplaceSel(titlecase(sel)) end
function titlecase(str)
so that it becomes
function titlecase(sel)
Seems to work better now.
local function tchelper(first, rest) return first:upper()..rest:lower() end function scite_titlecase() local sel = editor:GetSelText() sel = sel:gsub("(%a)([%w_']*)", tchelper) editor:ReplaceSel((sel)) end
Insert the above in your extensions file, and put the following into SciTEUser.properties:
command.name.6.*=Title Case command.6.*=scite_titlecase command.subsystem.6.*=3 command.mode.6.*=savebefore:no command.shortcut.6.*=Ctrl+Alt+ZAnonymous Steve.
function titlecase(str) local buf = {} local inWord = false for i = 1, #str do local c = string.sub(str, i, i) if inWord then table.insert(buf, string.lower(c)) if string.find(c, '%s') then inWord = false end else table.insert(buf, string.upper(c)) inWord = true end end return table.concat(buf) end
I tested it with Lua 5.2.
I'm new to Lua, so forgive (or, better yet, correct) any style mistakes.
EllenSpertus?