Advertisement
Guest User

GeanyLua: get selected filename

a guest
Oct 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. -- Get selected filename
  2. -- 2018.10.16
  3. --
  4. -- Based on "on_menu_open_selected_file1_activate()" (Ctrl+Shift+O) and "document_open_file_full()"
  5.  
  6. local function path_is_absolute(p)
  7.   if (geany.dirsep == '\\') then
  8.     if (string.match(string.sub(p, 1, 3), '[A-Za-z]:[\\\/]') ~= nil) then
  9.       return true;
  10.     end
  11.   else
  12.     if (string.sub(p, 1, 1) == geany.dirsep) then return true end;
  13.   end
  14.   return false;
  15. end
  16.  
  17. local function file_is_exists(p)
  18.   local f = geany.stat(p);
  19.   if (f ~= nil) then
  20.     if (f.type == 'r') then return true end;
  21.   end
  22.   return false;
  23. end
  24.  
  25. local function  tidy_path(fp)
  26.   local c1, c2, ii, fd, td
  27.   -- Remove relative junk
  28.   if (geany.dirsep == '\\') then
  29.     fp = string.gsub(fp, '/', geany.dirsep);
  30.   end
  31.   -- Replace "/./" and "//"
  32.   fp = string.gsub(fp, geany.dirsep .. '%.' .. geany.dirsep, geany.dirsep);
  33.   fp = string.gsub(fp, geany.dirsep .. geany.dirsep, geany.dirsep);
  34.   -- Replace "/../"
  35.   while true do
  36.     c1 = string.find(fp, geany.dirsep .. '..' .. geany.dirsep, 1, true);
  37.     if (c1 == nil) then break end;
  38.     if (c1 <= 4) then return nil end; -- Bad path
  39.     fd = string.sub(fp, 1, c1 - 1);
  40.     ii = 1;
  41.     while true do
  42.       -- Search for last "/" before found "/../"
  43.       c2 = string.find(fd, geany.dirsep, ii, true);
  44.       if (c2 == nil) then
  45.         if (ii == 1) then
  46.           return nil; -- Bad path
  47.         else
  48.           break;
  49.         end
  50.       end
  51.       ii = c2 + 1;
  52.     end
  53.     fd = string.sub(fd, 1, ii - 2);
  54.     td = string.sub(fp, c1 + 4, -1);
  55.     fp = string.gsub(fd .. geany.dirsep .. td, geany.dirsep .. geany.dirsep, geany.dirsep);
  56.   end
  57.   return fp;
  58. end
  59.  
  60. local wc = geany.wordchars
  61. local sel = geany.selection()
  62. local ai, fn, pn, tm
  63.  
  64. if (sel == nil) then return end;
  65. if (sel == '') then
  66.   if (geany.dirsep == '\\') then
  67.     geany.wordchars = wc .. './-\\:'; --Win
  68.   elseif (geany.dirsep == '/') then
  69.     geany.wordchars = wc .. './-'; --Unix
  70.   else
  71.     geany.message('Where are you?');
  72.     return;
  73.   end
  74.   sel = geany.word();
  75.   if (string.len(sel) == 0) then return end;
  76. end
  77. if path_is_absolute(sel) then
  78.   fn = sel;
  79. else
  80.   -- Relative filename, add the path of the current file
  81.   tm = geany.filename();
  82.   if (tm ~= nil) then
  83.     pn = geany.dirname(tm);
  84.   else
  85.     pn = geany.wkdir();
  86.   end
  87.   fn = pn .. geany.dirsep .. sel;
  88.   if not file_is_exists(fn) then
  89.     -- Try the project's base path
  90.     ai = geany.appinfo();
  91.     if (ai.project ~= nil) then
  92.       if path_is_absolute(ai.project.base) then
  93.         pn = ai.project.base;
  94.       else
  95.         -- Build path out of project file name's dir and base path
  96.         tm = geany.dirname(ai.project.file);
  97.         if (ai.project.base == './') or (ai.project.base == '.\\') then
  98.           pn = tm;
  99.         else
  100.           pn = tm .. geany.dirsep .. ai.project.base;
  101.         end
  102.       end
  103.       fn = pn .. geany.dirsep .. sel;
  104.       if (geany.dirsep == '/') then
  105.         if not file_is_exists(fn) then fn = '/usr/local/include/' .. sel end;
  106.         if not file_is_exists(fn) then fn = '/usr/include/' .. sel end;
  107.       end
  108.     end
  109.   end
  110. end
  111. fn = tidy_path(fn);
  112. if (fn ~= nil) and (file_is_exists(fn)) then
  113.   geany.message('File:\n"' .. fn .. '"');
  114.   return;
  115. end
  116. geany.message('File not found!\n"' .. fn .. '"');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement