Advertisement
Guest User

GeanyLua: get selected filename: geany.selfile()

a guest
Oct 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. /*
  2.  * Return the selected filename in the active Geany document (like as
  3.  *   "Open selected file" (Ctrl+Shift+O) but without opening).
  4.  * Based on "on_menu_open_selected_file1_activate" (geany/src/callbacks.c)
  5.  */
  6. static gint glspi_selfile(lua_State* L)
  7. {
  8.     DOC_REQUIRED
  9.     gchar *sel = NULL;
  10.     gchar *filename = NULL;
  11.     const gchar *wc;
  12.  
  13. #ifdef G_OS_WIN32
  14.     wc = GEANY_WORDCHARS "./-" "\\";
  15. #else
  16.     wc = GEANY_WORDCHARS "./-";
  17. #endif
  18.  
  19.     if (sci_has_selection(doc->editor->sci))
  20.     {
  21.         if (sci_get_selected_text_length(doc->editor->sci) < GEANY_MAX_WORD_LENGTH)
  22.             sel = sci_get_selection_contents(doc->editor->sci);
  23.     }
  24.     else
  25.         sel = editor_get_word_at_pos(doc->editor, sci_get_current_position(doc->editor->sci), wc);
  26.  
  27.     if (sel == NULL)
  28.         return 0;
  29.  
  30.     SETPTR(sel, utils_get_locale_from_utf8(sel));
  31.     if (g_path_is_absolute(sel))
  32.         filename = g_strdup(sel);
  33.     else
  34.     {   /* relative filename, add the path of the current file */
  35.         gchar *path;
  36.         if (doc->file_name != NULL)
  37.             path = g_path_get_dirname(doc->file_name); /* returns "." if no path */
  38.         else
  39.             path = g_get_current_dir();
  40.         SETPTR(path, utils_get_locale_from_utf8(path));
  41.  
  42.         filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);
  43.  
  44.         if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
  45.                 geany->app->project != NULL && !EMPTY(geany->app->project->base_path))
  46.         {
  47.             /* try the project's base path */
  48.             if (g_path_is_absolute(geany->app->project->base_path))
  49.                 SETPTR(path, utils_get_locale_from_utf8(geany->app->project->base_path));
  50.             else
  51.             {
  52.                 /* build base_path out of project file name's dir and base_path */
  53.                 gchar *tmp;
  54.                 tmp = g_path_get_dirname(geany->app->project->file_name);
  55.                 if (utils_str_equal(geany->app->project->base_path, "./"))
  56.                     SETPTR(path, utils_get_locale_from_utf8(tmp));
  57.                 else
  58.                     SETPTR(path, utils_get_locale_from_utf8(g_build_filename(tmp, geany->app->project->base_path, NULL)));
  59.                 g_free(tmp);
  60.             }
  61.             SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL));
  62.         }
  63.         g_free(path);
  64. #ifdef G_OS_UNIX
  65.         if (! g_file_test(filename, G_FILE_TEST_EXISTS))
  66.             SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));
  67.  
  68.         if (! g_file_test(filename, G_FILE_TEST_EXISTS))
  69.             SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
  70. #endif
  71.     }
  72.  
  73.     if (g_file_test(filename, G_FILE_TEST_EXISTS))
  74.     {
  75.         /* remove relative junk */
  76.         utils_tidy_path(filename);
  77.  
  78.         lua_pushstring(L, (const gchar *) filename);
  79.         g_free(filename);
  80.         g_free(sel);
  81.         return 1;
  82.     }
  83.     else
  84.     {
  85.         g_free(filename);
  86.         g_free(sel);
  87.         return 0;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement