Scite Open Filename

lua-users home
wiki

Ctrl+Shift+O replacement for SciTE

SciTEOpenFilename is a replacement for the OpenFilename function in SciTE (Ctrl+Shift+O), which is used to quickly open files specified in the current document. The major advantage of this Lua extension version of OpenFilename is the opening of library or system header files of certain languages. SciTEOpenFilename can be downloaded from Files:wiki_insecure/editors/SciTE/SciTEOpenFilename.lua.

Instead of using the open.suffix and openpath properties, SciTEOpenFilename has custom handlers for opening the libraries and system header files of the following languages: C, C++, Perl and Python. Since it is a Lua extension, SciTEOpenFilename can be easily extended to handle other languages. Customizing search locations is a simple matter of editing and adding entries to the table of paths to be searched.

SciTEOpenFilename also checks the prefix to determine whether an appropriate keyword is present. This disables the more promiscuous behaviour of the default Ctrl+Shift+O function (to remove this feature, simply delete the relevant tests.) For property files, the script checks for import; for C/C++, the script checks for #include; for Perl, the script checks for use, require, do or no; for Python, the script checks for import and from.

SciTEOpenFilename is not an exact work-alike version of OpenFilename. It does not support Ctags (see SciteTags if you want Ctags, it is a more complete solution), it does not support the open.suffix and openpath properties, and it does not support the opening of http:, ftp: etc. kind of resources. But it is in Lua, and you can extend it any way you want without needing to recompile binaries.

If anyone extends SciTEOpenFilename, please update the appropriate stuff.

2007-03-29: fixed bug in lookupExt() comparing extensions

--khman--

Update 2010 by Hank

This alternative opens lists of multiple highlighted files. It also scrolls the editor to line numbers separated by colons, such as lists of compiler error output. Just highlight groups of files to open and press Ctrl+Shift+O.

I use it to highlight all the dofile("") entries in startup.lua and open them. It also works on lists of #include <> directives. It does its best to determine files from non-files. If it can't find something, it uses the 'locate' command to try and find them.

-- Compatibility: Lua 5.3
function try_open(txt)
    local x
    local words = split(txt,"\n")
    for k, word in pairs(words) do
        local line=tonumber(word:gsub(".-:([^:]*).*","%1"),10)
        local col =tonumber(word:gsub(".-:.-:([^:]+).*", "%1"),10)
        if col then col = col - 1 else col = 0 end
        if line then line = line- 1 end
        local subd=word:gsub(":.*","")
        subd = subd:gsub(".*[[(<\"' ](.*)[])>\"' ].*", "%1")
        if(io.open(subd)) then
            io.close()
            scite.Open(subd)
        elseif(io.open("/usr/include/"..subd)) then
            io.close()
            scite.Open("/usr/include/"..subd)
        else
            print("other possible candidates")
            local f = io.popen("locate -l 5 "..
            "/`basename "..subd.."`")
            local l = f:read("*a") -- get locate output
            print(l) -- print possible search candidates
            file = split(l, "\n")[1] -- open first one
            if file then scite.Open(file) end
            f:close()
            return
        end
        if line then
            if not col then col = 0 end
            marker_define(1,1,'black','blue')
            editor:MarkerAdd(line, 1)
            editor:GotoLine(line)            
            editor.SelectionStart   = editor.CurrentPos + col
            editor.SelectionEnd   = editor.LineEndPosition[line]
        end
    end
end

local colours = {red = "#FF0000", blue = '#0000FF', green = '#00FF00',pink ="#FFAAAA" ,
                black = '#000000', lightblue = '#AAAAFF',lightgreen = '#AAFFAA', yellow = '#FFFF00' }

function color_parse(str)
    if string.sub(str,1,1) ~= '#' then
        str = colours[str]
    end
    return tonumber(string.sub(str,6,7)..string.sub(str,4,5)..string.sub(str,2,4),16)
end

function marker_define(idx,typ,fore,back)
    editor:MarkerDefine(idx,typ)
    if fore then editor.MarkerFore[idx]=color_parse(fore) end
    if back then editor.MarkerFore[idx]=color_parse(back) end
end

-- Compatibility: Lua-5.1
function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
      table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

And in .SciTEUser.properties:

command.name.10.*=Open Filename
command.10.*=quick_open $(CurrentSelection)
command.shortcut.10.*=Ctrl+Shift+O
command.subsystem.10.*=3

RecentChanges · preferences
edit · history
Last edited March 25, 2019 2:15 am GMT (diff)