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() 比附加至字串更有效率;請參閱 [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匿名 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
我用 Lua 5.2 測試過。
我是 Lua 新手,因此請見諒(或更正)任何風格上的錯誤。
EllenSpertus?