Scite 标题大小写 |
|
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
请注意,在这种情况下使用 table.concat() 比追加到字符串更有效;请参阅 [Programming in Lua 11.6 字符串缓冲区]。
-- 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)
因此它变成了
function titlecase(sel)
现在似乎工作得更好。
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
将以上内容插入您的扩展文件,并将以下内容放入 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+Z匿名史蒂夫。
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
我在 Lua 5.2 上测试了它。
我是 Lua 新手,所以请原谅(或者,更好的是,纠正)任何风格错误。
EllenSpertus?