Skip to content

Instantly share code, notes, and snippets.

@ixth
Created May 28, 2011 15:45
Show Gist options
  • Save ixth/996964 to your computer and use it in GitHub Desktop.
Save ixth/996964 to your computer and use it in GitHub Desktop.
Path autocompletion script for SciTE
-- Path autocompletion script for SciTE
-- Opens autocomplete dialogue for selected path string
SCI_SUBSTITUTE_PATH = 1987
function hasbit(x, p)
return x % (p + p) >= p
end
function TrimPath(p)
if p:sub(1, 1) == '/' then p = p:sub(2) end
if p:sub(-1) == '/' then p = p:sub(1, -2) end
return p
end
function FileType(path)
local stat = io.popen('stat --printf=%f ' .. path)
if stat ~= nil then
local mode = stat:read('*a')
stat:close()
return tonumber(mode, 16)
else
return false
end
end
function sp_FindFile(name, pathspec)
local result = nil
for i,prefix in next,pathspec do
local path = prefix .. '/' .. name
local filetype = FileType(path)
if filetype ~= nil then
result = {path = path, type = filetype}
end
end
return result
end
function sp_ListFiles(file)
local path = ''
local flags = 0
if hasbit(file.type, 0x4000) then
path = file.path
flags = SCI_SUBSTITUTE_PATH
elseif hasbit(file.type, 0x8000) then
path = file.path:match('.*/')
flags = SCI_SUBSTITUTE_PATH + 1
end
local ls = io.popen('ls -1 ' .. path)
local list = ls:read('*a')
ls:close()
editor:UserListShow(flags, list:gsub('[\r\n]*$', ''))
end
function sp_GetPathInPosition()
local line, pos = editor:GetCurLine()
local s_s, m, s_e
s_s, m, s_e = line:match('url()(%b())()')
if m:match('([\'"]).*%1') then
s_s = s_s + 2
s_e = s_e - 2
else
s_s = s_s + 1
s_e = s_e - 1
end
local offset = editor:PositionFromLine(-1) - 1
return offset + s_s, offset + s_e
end
function SubstitutePath()
local sel = editor:GetSelText()
if sel == '' then
local s_s, s_e = sp_GetPathInPosition()
editor:SetSel(s_s, s_e)
sel = editor:GetSelText()
end
local file = sp_FindFile(sel, {'', '.', '~', props['SciteUserHome']})
sel = TrimPath(sel)
if file ~= nil then
sp_ListFiles(file)
end
end
local sp_OnUserListSelection_p = OnUserListSelection
function OnUserListSelection(id, val)
sp_OnUserListSelection(id, val)
if sp_OnUserListSelection_p ~= nil then
sp_OnUserListSelection_p(id, val)
end
end
function sp_OnUserListSelection(id, val)
if id == SCI_SUBSTITUTE_PATH or id == SCI_SUBSTITUTE_PATH + 1 then
local sel = editor:GetSelText()
if id == SCI_SUBSTITUTE_PATH + 1 then
sel = sel:match('.*/')
end
if sel:sub(-1) == '/' then
sel = sel:sub(1, -2)
end
editor:ReplaceSel(sel .. '/' .. val)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment