Advertisement
Guest User

GeanyLua: get selected filename as module

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