Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Get selected filename
- -- 2018.10.16
- --
- -- Based on "on_menu_open_selected_file1_activate()" (Ctrl+Shift+O) and "document_open_file_full()"
- local function path_is_absolute(p)
- if (geany.dirsep == '\\') then
- if (string.match(string.sub(p, 1, 3), '[A-Za-z]:[\\\/]') ~= nil) then
- return true;
- end
- else
- if (string.sub(p, 1, 1) == geany.dirsep) then return true end;
- end
- return false;
- end
- local function file_is_exists(p)
- local f = geany.stat(p);
- if (f ~= nil) then
- if (f.type == 'r') then return true end;
- end
- return false;
- end
- local function tidy_path(fp)
- local c1, c2, ii, fd, td
- -- Remove relative junk
- if (geany.dirsep == '\\') then
- fp = string.gsub(fp, '/', geany.dirsep);
- end
- -- Replace "/./" and "//"
- fp = string.gsub(fp, geany.dirsep .. '%.' .. geany.dirsep, geany.dirsep);
- fp = string.gsub(fp, geany.dirsep .. geany.dirsep, geany.dirsep);
- -- Replace "/../"
- while true do
- c1 = string.find(fp, geany.dirsep .. '..' .. geany.dirsep, 1, true);
- if (c1 == nil) then break end;
- if (c1 <= 4) then return nil end; -- Bad path
- fd = string.sub(fp, 1, c1 - 1);
- ii = 1;
- while true do
- -- Search for last "/" before found "/../"
- c2 = string.find(fd, geany.dirsep, ii, true);
- if (c2 == nil) then
- if (ii == 1) then
- return nil; -- Bad path
- else
- break;
- end
- end
- ii = c2 + 1;
- end
- fd = string.sub(fd, 1, ii - 2);
- td = string.sub(fp, c1 + 4, -1);
- fp = string.gsub(fd .. geany.dirsep .. td, geany.dirsep .. geany.dirsep, geany.dirsep);
- end
- return fp;
- end
- local wc = geany.wordchars
- local sel = geany.selection()
- local ai, fn, pn, tm
- if (sel == nil) then return end;
- if (sel == '') then
- if (geany.dirsep == '\\') then
- geany.wordchars = wc .. './-\\:'; --Win
- elseif (geany.dirsep == '/') then
- geany.wordchars = wc .. './-'; --Unix
- else
- geany.message('Where are you?');
- return;
- end
- sel = geany.word();
- if (string.len(sel) == 0) then return end;
- end
- if path_is_absolute(sel) then
- fn = sel;
- else
- -- Relative filename, add the path of the current file
- tm = geany.filename();
- if (tm ~= nil) then
- pn = geany.dirname(tm);
- else
- pn = geany.wkdir();
- end
- fn = pn .. geany.dirsep .. sel;
- if not file_is_exists(fn) then
- -- Try the project's base path
- ai = geany.appinfo();
- if (ai.project ~= nil) then
- if path_is_absolute(ai.project.base) then
- pn = ai.project.base;
- else
- -- Build path out of project file name's dir and base path
- tm = geany.dirname(ai.project.file);
- if (ai.project.base == './') or (ai.project.base == '.\\') then
- pn = tm;
- else
- pn = tm .. geany.dirsep .. ai.project.base;
- end
- end
- fn = pn .. geany.dirsep .. sel;
- if (geany.dirsep == '/') then
- if not file_is_exists(fn) then fn = '/usr/local/include/' .. sel end;
- if not file_is_exists(fn) then fn = '/usr/include/' .. sel end;
- end
- end
- end
- end
- fn = tidy_path(fn);
- if (fn ~= nil) and (file_is_exists(fn)) then
- geany.message('File:\n"' .. fn .. '"');
- return;
- end
- geany.message('File not found!\n"' .. fn .. '"');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement