diff options
Diffstat (limited to 'Master/texmf-dist/scripts/context/lua/scite-ctx.lua')
-rw-r--r-- | Master/texmf-dist/scripts/context/lua/scite-ctx.lua | 342 |
1 files changed, 130 insertions, 212 deletions
diff --git a/Master/texmf-dist/scripts/context/lua/scite-ctx.lua b/Master/texmf-dist/scripts/context/lua/scite-ctx.lua index 5836bcbfb71..1b832928909 100644 --- a/Master/texmf-dist/scripts/context/lua/scite-ctx.lua +++ b/Master/texmf-dist/scripts/context/lua/scite-ctx.lua @@ -1,7 +1,7 @@ --- version : 1.0.0 - 07/2005 +-- version : 1.0.0 - 07/2005 (2008: lua 5.1) -- author : Hans Hagen - PRAGMA ADE - www.pragma-ade.com -- copyright : public domain or whatever suits --- remark : part of the context distribution +-- remark : part of the context distribution, my first lua code -- todo: name space for local functions @@ -75,15 +75,12 @@ function traceln(str) io.flush() end -table.len = table.getn -table.join = table.concat - function table.found(tab, str) local l, r, p - if string.len(str) == 0 then + if #str == 0 then return false else - l, r = 1, table.len(tab) + l, r = 1, #tab while l <= r do p = math.floor((l+r)/2) if str < tab[p] then @@ -98,43 +95,44 @@ function table.found(tab, str) end end -function string.grab(str, delimiter) +function string:grab(delimiter) local list = {} - for snippet in string.gfind(str,delimiter) do - table.insert(list, snippet) + for snippet in self:gmatch(delimiter) do + list[#list+1] = snippet end return list end -function string.join(list, delimiter) - local size, str = table.len(list), '' - if size > 0 then - str = list[1] - for i = 2, size, 1 do - str = str .. delimiter .. list[i] - end - end - return str +function string:is_empty() + return not self:find("%S") end -function string.spacy(str) - if string.find(str,"^%s*$") then - return true - else - return false - end +function string:expand() + return (self:gsub("ENV%((%w+)%)", os.envvar)) end -function string.alphacmp(a,b,i) -- slow but ok - if i and i > 0 then - return string.lower(string.gsub(string.sub(a,i),'0',' ')) < string.lower(string.gsub(string.sub(b,i),'0',' ')) - else - return string.lower(a) < string.lower(b) - end +function string:strip() + return (self:gsub("^%s*(.-)%s*$", "%1")) end -function table.alphasort(list,i) - table.sort(list, function(a,b) return string.alphacmp(a,b,i) end) +do + + local lower, gsub, sub = string.lower, string.gsub, string.sub + + function table.alphasort(list,i) + if i and i > 0 then + local function alphacmp(a,b) + return lower(gsub(sub(a,i),'0',' ')) < lower(gsub(sub(b,i),'0',' ')) + end + table.sort(list,alphacmp) + else + local function alphacmp(a,b) + return a:lower() < b:lower() + end + table.sort(list,alphacmp) + end + end + end function io.exists(filename) @@ -148,29 +146,18 @@ function io.exists(filename) end function os.envvar(str) - if os.getenv(str) ~= '' then - return os.getenv(str) - elseif os.getenv(string.upper(str)) ~= '' then - return os.getenv(string.upper(str)) - elseif os.getenv(string.lower(str)) ~= '' then - return os.getenv(string.lower(str)) - else - return '' + local s = os.getenv(str) + if s ~= '' then + return s + end + s = os.getenv(str:upper()) + if s ~= '' then + return s + end + s = os.getenv(str:lower()) + if s ~= '' then + return s end -end - -function string.expand(str) - return string.gsub(str, "ENV%((%w+)%)", os.envvar) -end - -function string.strip(str) - return string.gsub(string.gsub(str,"^%s+",''),"%s+$",'') -end - -function string.replace(original,pattern,replacement) - local str = string.gsub(original,pattern,replacement) --- print(str) -- indirect, since else str + nofsubs - return str -- indirect, since else str + nofsubs end -- support functions, maybe editor namespace @@ -230,9 +217,9 @@ function getfiletype() return 'tex' elseif editor.Lexer == SCLEX_XML then return 'xml' - elseif string.find(firstline,"^%%") then + elseif firstline:find("^%%") then return 'tex' - elseif string.find(firstline,"^<%?xml") then + elseif firstline:find("^<%?xml") then return 'xml' else return 'unknown' @@ -246,7 +233,7 @@ function get_dir_list(mask) if props['PLAT_GTK'] and props['PLAT_GTK'] ~= "" then f = io.popen('ls -1 ' .. mask) else - mask = string.gsub(mask, '/','\\') + mask = mask:gsub('/','\\') local tmpfile = 'scite-ctx.tmp' local cmd = 'dir /b "' .. mask .. '" > ' .. tmpfile os.execute(cmd) @@ -257,7 +244,7 @@ function get_dir_list(mask) return files end for line in f:lines() do - table.insert(files, line) + files[#files+1] = line end f:close() return files @@ -265,32 +252,29 @@ end -- banner -print("loading scite-ctx.lua definition file") -print("") -print("- see scite-ctx.properties for configuring info") -print("") -print("- ctx.spellcheck.wordpath set to " .. props['ctx.spellcheck.wordpath']) -if string.find(string.lower(props['ctx.spellcheck.wordpath']), "ctxspellpath") then - if os.getenv('ctxspellpath') then - print("- ctxspellpath set to " .. os.getenv('CTXSPELLPATH')) - else - print("- 'ctxspellpath is not set") +do + + print("loading scite-ctx.lua definition file\n") + print("- see scite-ctx.properties for configuring info\n") + print("- ctx.spellcheck.wordpath set to " .. props['ctx.spellcheck.wordpath']) + if (props['ctx.spellcheck.wordpath']:lower()):find("ctxspellpath") then + if os.getenv('ctxspellpath') then + print("- ctxspellpath set to " .. os.getenv('CTXSPELLPATH')) + else + print("- 'ctxspellpath is not set") + end + print("- ctx.spellcheck.wordpath expands to " .. string.expand(props['ctx.spellcheck.wordpath'])) end - print("- ctx.spellcheck.wordpath expands to " .. string.expand(props['ctx.spellcheck.wordpath'])) -end -print("") -print("- ctx.wraptext.length is set to " .. props['ctx.wraptext.length']) -if props['ctx.helpinfo'] ~= '' then - print("- key bindings:") - print("") - print(string.replace(string.strip(props['ctx.helpinfo']),"%s*\|%s*","\n")) -- indirect, since else str + nofsubs -end -print("") -print("- recognized first lines:") -print("") -print("xml <?xml version='1.0' language='nl'") -print("tex % language=nl") + print("\n- ctx.wraptext.length is set to " .. props['ctx.wraptext.length']) + if props['ctx.helpinfo'] ~= '' then + print("\n- key bindings:\n") + print(((string.strip(props['ctx.helpinfo'])):gsub("%s*\|%s*","\n"))) + end + print("\n- recognized first lines:\n") + print("xml <?xml version='1.0' language='nl'") + print("tex % language=nl") +end -- text functions @@ -307,15 +291,15 @@ function wrap_text() if length == '' then length = 80 else length = tonumber(length) end - local startposition = editor.SelectionStart - local endposition = editor.SelectionEnd + local startposition = editor.SelectionStart + local endposition = editor.SelectionEnd if startposition == endposition then return end editor:LineEndExtend() - startposition = editor.SelectionStart - endposition = editor.SelectionEnd + startposition = editor.SelectionStart + endposition = editor.SelectionEnd -- local startline = line_of_position(startposition) -- local endline = line_of_position(endposition) @@ -327,46 +311,47 @@ function wrap_text() local startline = props['SelectionStartLine'] local endline = props['SelectionEndLine'] local startcolumn = props['SelectionStartColumn'] - 1 - local endcolumn = props['SelectionEndColumn'] - 1 + local endcolumn = props['SelectionEndColumn'] - 1 - local indentation = string.rep(' ', startcolumn) --- local selection = string.gsub(editor:GetSelText(),"[\n\r][\n\r]+", ' ' .. magicstring .. ' ') - local selection = string.gsub(editor:GetSelText(),"[\n\r][\n\r]", "\n") - local selection = string.gsub(selection,"\n\n+", ' ' .. magicstring .. ' ') - local replacement = '' + local replacement = { } local templine = '' + local indentation = string.rep(' ',startcolumn) + local selection = editor:GetSelText() - selection = string.gsub(selection,"^%s", '') + selection = selection:gsub("[\n\r][\n\r]","\n") + selection = selection:gsub("\n\n+",' ' .. magicstring .. ' ') + selection = selection:gsub("^%s",'') - for snippet in string.gfind(selection, "%S+") do + for snippet in selection:gmatch("%S+") do if snippet == magicstring then - replacement = replacement .. templine .. "\n\n" + replacement[#replacement+1] = templine + replacement[#replacement+1] = "" templine = '' - elseif string.len(templine) + string.len(snippet) > length then - replacement = replacement .. templine .. "\n" + elseif #templine + #snippet > length then + replacement[#replacement+1] = templine templine = indentation .. snippet - elseif string.len(templine) == 0 then + elseif #templine == 0 then templine = indentation .. snippet else templine = templine .. ' ' .. snippet end end - replacement = replacement .. templine - replacement = string.gsub(replacement, "^%s+", '') + replacement[#replacement+1] = templine + replacement[1] = replacement[1]:gsub("^%s+",'') if endcolumn == 0 then - replacement = replacement .. "\n" + replacement[#replacement+1] = "" end - editor:ReplaceSel(replacement) + editor:ReplaceSel(table.concat(replacement,"\n")) end function unwrap_text() - local startposition = editor.SelectionStart - local endposition = editor.SelectionEnd + local startposition = editor.SelectionStart + local endposition = editor.SelectionEnd if startposition == endposition then return end @@ -380,7 +365,7 @@ function unwrap_text() local selection = string.gsub(editor:GetSelText(),"[\n\r][\n\r]+", ' ' .. magicstring .. ' ') local replacement = '' - for snippet in string.gfind(selection, "%S+") do + for snippet in selection:gmatch("%S+") do if snippet == magicstring then replacement = replacement .. "\n" else @@ -447,25 +432,25 @@ function document_text() for i = editor:LineFromPosition(startposition), editor:LineFromPosition(endposition) do local str = editor:GetLine(i) if filetype == 'xml' then - if string.find(str,"^<%!%-%- .* %-%->%s*$") then - replacement = replacement .. string.gsub(str,"^<%!%-%- (.*) %-%->(%s*)$", "%1\n") - elseif not string.spacy(str) then - replacement = replacement .. '<!-- ' .. string.gsub(str,"(%s*)$", '') .. " -->\n" + if str:find("^<%!%-%- .* %-%->%s*$") then + replacement = replacement .. str:gsub("^<%!%-%- (.*) %-%->(%s*)$","%1\n") + elseif not str:is_empty() then + replacement = replacement .. '<!-- ' .. str:gsub("(%s*)$",'') .. " -->\n" else replacement = replacement .. str end else - if string.find(str,"^%%D%s+$") then + if str:find("^%%D%s+$") then replacement = replacement .. "\n" - elseif string.find(str,"^%%D ") then - replacement = replacement .. string.gsub(str,"^%%D ", '') + elseif str:find("^%%D ") then + replacement = replacement .. str:gsub("^%%D ",'') else replacement = replacement .. '%D ' .. str end end end - editor:ReplaceSel(string.gsub(replacement, "[\n\r]$", '')) + editor:ReplaceSel(replacement:gsub("[\n\r]$",'')) end @@ -482,10 +467,10 @@ function quote_text() end local replacement = editor:GetSelText() - replacement = string.gsub(replacement, "\`\`(.-)\'\'", leftquotation .. "%1" .. rightquotation) - replacement = string.gsub(replacement, "\"(.-)\"", leftquotation .. "%1" .. rightquotation) - replacement = string.gsub(replacement, "\`(.-)\'", leftquote .. "%1" .. rightquote ) - replacement = string.gsub(replacement, "\'(.-)\'", leftquote .. "%1" .. rightquote ) + replacement = replacement.gsub("\`\`(.-)\'\'", leftquotation .. "%1" .. rightquotation) + replacement = replacement.gsub("\"(.-)\"", leftquotation .. "%1" .. rightquotation) + replacement = replacement.gsub("\`(.-)\'", leftquote .. "%1" .. rightquote ) + replacement = replacement.gsub("\'(.-)\'", leftquote .. "%1" .. rightquote ) editor:ReplaceSel(replacement) end @@ -558,9 +543,9 @@ function check_text() if props["ctx.spellcheck.language"] == 'auto' then if filetype == 'tex' then -- % version =1.0 language=uk - firstline = string.gsub(firstline, "^%%%s*", '') - firstline = string.gsub(firstline, "%s*$", '') - for key, val in string.gfind(firstline,"(%w+)=(%w+)") do + firstline = firstline:gsub("^%%%s*",'') + firstline = firstline:gsub("%s*$",'') + for key, val in firstline:gmatch("(%w+)=(%w+)") do if key == "language" then language = val traceln("auto document language " .. "'" .. language .. "' (tex)") @@ -569,9 +554,9 @@ function check_text() skipfirst = true elseif filetype == 'xml' then -- <?xml version='1.0' language='uk' ?> - firstline = string.gsub(firstline, "^%<%?xml%s*", '') - firstline = string.gsub(firstline, "%s*%?%>%s*$", '') - for key, val in string.gfind(firstline,"(%w+)=[\"\'](.-)[\"\']") do + firstline = firstline:gsub("^%<%?xml%s*", '') + firstline = firstline:gsub("%s*%?%>%s*$", '') + for key, val in firstline:gmatch("(%w+)=[\"\'](.-)[\"\']") do if key == "language" then language = val traceln("auto document language " .. "'" .. language .. "' (xml)") @@ -588,16 +573,16 @@ function check_text() if fname ~= '' and fname ~= wordfile then wordfile, worddone, wordlist = fname, 0, {} - for filename in string.gfind(wordfile,"[^%,]+") do + for filename in wordfile:gmatch("[^%,]+") do if wordpath ~= '' then filename = string.expand(wordpath) .. '/' .. filename end if io.exists(filename) then traceln("loading " .. filename) for line in io.lines(filename) do - if not string.find(line,"^[\%\#\-]") then - str = string.gsub(line,"%s*$", '') - rawset(wordlist,str,true) -- table.insert(wordlist,str) + if not line:find("^[\%\#\-]") then + str = line:gsub("%s*$", '') + rawset(wordlist,str,true) worddone = worddone + 1 end end @@ -619,7 +604,7 @@ function check_text() end local i, j, lastpos, startpos, endpos, snippet, len, first = 0, 0, -1, 0, 0, '', 0, 0 local ok, skip, ch = false, false, '' - if skipfirst then first = string.len(firstline) end + if skipfirst then first = #firstline end for k = first, editor.TextLength do ch = editor:textrange(k,k+1) if wordgood ~= '' and ch == wordgood then @@ -627,7 +612,7 @@ function check_text() elseif ch == wordskip then skip = true end - if string.find(ch,"%w") and not string.find(ch,"%d") then + if ch:find("%w") and not ch:find("%d") then if not skip then if ok then endpos = k @@ -642,7 +627,7 @@ function check_text() if len >= wordsize then snippet = editor:textrange(startpos,endpos+1) i = i + 1 - if wordlist[snippet] or wordlist[string.lower(snippet)] then -- table.found(wordlist,snippet) + if wordlist[snippet] or wordlist[snippet:lower()] then -- table.found(wordlist,snippet) j = j + 1 else editor:StartStyling(startpos,INDICS_MASK) @@ -673,15 +658,15 @@ function UserListShow(menutrigger, menulist) local menuentries = {} local list = string.grab(menulist,"[^%|]+") menuactions = {} - for i=1, table.len(list) do + for i=1, #list do if list[i] ~= '' then - for key, val in string.gfind(list[i],"%s*(.+)=(.+)%s*") do - table.insert(menuentries,key) - rawset(menuactions,key,val) + for key, val in list[i]:gmatch("%s*(.+)=(.+)%s*") do + menuentries[#menuentries+1] = key + menuactions[key] = val end end end - local menustring = table.join(menuentries,'|') + local menustring = table.concat(menuentries,'|') if menustring == "" then traceln("There are no templates defined for this file type.") else @@ -708,7 +693,7 @@ function show_menu(menulist) end function process_menu(action) - if not string.find(action,"%(%)$") then + if not action:find("%(%)$") then assert(loadstring(action .. "()"))() else assert(loadstring(action))() @@ -721,73 +706,6 @@ menufunctions[12] = process_menu local templatetrigger = 13 --- local ctx_template_paths = { "./ctx-templates", "../ctx-templates", "../../ctx-templates" } --- local ctx_auto_templates = false --- local ctx_template_list = "" --- local ctx_dir_list = { } --- local ctx_dir_name = "./ctx-templates" - --- local ctx_path_list = {} --- local ctx_path_done = {} - --- function ctx_list_loaded() --- return ctx_dir_list and table.getn(ctx_dir_list) > 0 --- end - --- function insert_template(templatelist) --- if props["ctx.template.scan"] == "yes" then --- local current = props["FileDir"] .. "+" .. props["FileExt"] -- no name --- local rescan = props["ctx.template.rescan"] == "yes" --- local suffix = props["ctx.template.suffix."..props["FileExt"]] -- alas, no suffix expansion here --- if rescan then --- print("re-scanning enabled") --- end --- if current ~= ctx_file_path then --- rescan = true --- ctx_file_path = current --- ctx_file_done = false --- ctx_template_list = "" --- end --- if not ctx_file_done or rescan then --- local pattern = "*.*" --- for i, pathname in ipairs(ctx_template_paths) do --- print("scanning " .. pathname .. " for " .. pattern) --- ctx_dir_name = pathname --- ctx_dir_list = get_dir_list(pathname .. "/" .. pattern) --- if ctx_list_loaded() then --- break --- end --- end --- ctx_file_done = true --- end --- if ctx_list_loaded() then --- ctx_template_list = "" --- local pattern = "%." .. suffix .. "$" --- for j, filename in ipairs(ctx_dir_list) do --- if string.find(filename,pattern) then --- local menuname = string.gsub(filename,"%..-$","") --- if ctx_template_list ~= "" then --- ctx_template_list = ctx_template_list .. "|" --- end --- ctx_template_list = ctx_template_list .. menuname .. "=" .. ctx_dir_name .. "/" .. filename --- end --- end --- else --- print("no template files found") --- end --- if ctx_template_list == "" then --- ctx_auto_templates = false --- print("no file related templates found") --- else --- ctx_auto_templates = true --- templatelist = ctx_template_list --- end --- end --- if templatelist ~= "" then --- UserListShow(templatetrigger, templatelist) --- end --- end - local ctx_template_paths = { "./ctx-templates", "../ctx-templates", "../../ctx-templates" } local ctx_auto_templates = false local ctx_template_list = "" @@ -797,7 +715,7 @@ local ctx_path_done = {} local ctx_path_name = {} function ctx_list_loaded(path) - return ctx_path_list[path] and table.getn(ctx_path_list[path]) > 0 + return ctx_path_list[path] and #ctx_path_list[path] > 0 end function insert_template(templatelist) @@ -813,7 +731,7 @@ function insert_template(templatelist) if not ctx_path_done[path] or rescan then local pattern = "*.*" for i, pathname in ipairs(ctx_template_paths) do - print("scanning " .. string.gsub(path,"\\","/") .. "/" .. pathname) + print("scanning " .. path:gsub("\\","/") .. "/" .. pathname) ctx_path_name[path] = pathname ctx_path_list[path] = get_dir_list(pathname .. "/" .. pattern) if ctx_list_loaded(path) then @@ -822,7 +740,7 @@ function insert_template(templatelist) end end if ctx_list_loaded(path) then - print(table.getn(ctx_path_list[path]) .. " template files found") + print(#ctx_path_list[path] .. " template files found") else print("no template files found") end @@ -832,9 +750,9 @@ function insert_template(templatelist) local pattern = "%." .. suffix .. "$" local n = 0 for j, filename in ipairs(ctx_path_list[path]) do - if string.find(filename,pattern) then + if filename:find(pattern) then n = n + 1 - local menuname = string.gsub(filename,"%..-$","") + local menuname = filename:gsub("%..-$","") if ctx_template_list ~= "" then ctx_template_list = ctx_template_list .. "|" end @@ -897,9 +815,9 @@ function process_template_one(action) end end if text then - text = string.replace(text,"\\n","\n") - local pos = string.find(text,"%?") - text = string.replace(text,"%?","") + text = text:gsub("\\n","\n") + local pos = text:find("%?") + text = text:gsub("%?","") editor:insert(editor.CurrentPos,text) if pos then editor.CurrentPos = editor.CurrentPos + pos - 1 |