Is there a plugin for the terminal which allow to detect and open files?

I'm looking for a terminal plugin/extension that would allow me to detect files path in terminal output and open them in a text editor by clicking on them. The perfect solution would open the file at the given line.

/home/dell/pcl/2d/include/pcl/2d/impl/keypoint.hpp:233: warning: no uniquely matching class member found for void pcl::keypoint::imageElementMultiply(ImageType &output, ImageType &input1, ImageType &input2)

In the example above I would be able to click on /home/dell/pcl/2d/include/pcl/2d/impl/keypoint.hpp:233 and I would open the file with Gedit (or whatever) at line 233.


Solution 1:

I though, I may need such option myself. AFAIK, gnome-terminal does not support plug-ins.

I'm using Ubuntu 14.04, gnome-terminal 3.6.2. I didn't test for enough time, expect bugs.

  1. Download source & build dependencies

    sudo apt-get build-dep gnome-terminal
    apt-get source gnome-terminal
    
  2. Add support for paths

    src/terminal-screen.h, add FLAVOR_DEFAULT_TO_FILE enum type for file paths

    typedef enum {
      FLAVOR_AS_IS,
      FLAVOR_DEFAULT_TO_HTTP,
      FLAVOR_VOIP_CALL,
      FLAVOR_EMAIL,
      FLAVOR_LP,
      FLAVOR_DEFAULT_TO_FILE
    } TerminalURLFlavour;
    

    src/terminal-screen.c, add path regex (it's not perfect, may need some tuning)

    static const TerminalRegexPattern url_regex_patterns[] = {
      { SCHEME "//(?:" USERPASS "\\@)?" HOST PORT URLPATH, FLAVOR_AS_IS, G_REGEX_CASELESS },
      { "(?:www|ftp)" HOSTCHARS_CLASS "*\\." HOST PORT URLPATH , FLAVOR_DEFAULT_TO_HTTP, G_REGEX_CASELESS  },
      { "(?:callto:|h323:|sip:)" USERCHARS_CLASS "[" USERCHARS ".]*(?:" PORT "/[a-z0-9]+)?\\@" HOST, FLAVOR_VOIP_CALL, G_REGEX_CASELESS  },
      { "(?:mailto:)?" USERCHARS_CLASS "[" USERCHARS ".]*\\@" HOSTCHARS_CLASS "+\\." HOST, FLAVOR_EMAIL, G_REGEX_CASELESS  },
      { "(?:news:|man:|info:)[[:alnum:]\\Q^_{|}~!\"#$%&'()*+,./;:=?`\\E]+", FLAVOR_AS_IS, G_REGEX_CASELESS  },
      { "(?:lp: #)[[:digit:]]+", FLAVOR_LP, G_REGEX_CASELESS  },
      { "((~/)|(\\.\\./)|(\\./)|(/))+[^\\n\\t\\r\\v\\0 !$`&*()+:]+[^\\n\\t\\r\\v\\0 !$`&*()+:?.,;\"'\\]\\[<>#{}(]", FLAVOR_DEFAULT_TO_FILE, G_REGEX_CASELESS },
    };
    

    src/terminal-util.c, compose correct URL by adding file:// prefix

      switch (flavor)
        {
        case FLAVOR_DEFAULT_TO_HTTP:
          uri = g_strdup_printf ("http://%s", orig_url);
          break;
        case FLAVOR_EMAIL:
          if (g_ascii_strncasecmp ("mailto:", orig_url, 7) != 0)
        uri = g_strdup_printf ("mailto:%s", orig_url);
          else
        uri = g_strdup (orig_url);
          break;
        case FLAVOR_VOIP_CALL:
        case FLAVOR_AS_IS:
          uri = g_strdup (orig_url);
          break;
        case FLAVOR_LP:
          uri = terminal_util_get_lp_url (orig_url);
          break;
        case FLAVOR_DEFAULT_TO_FILE:
          uri = g_strdup_printf ("file://%s", orig_url);
          break;    
        default:
          uri = NULL;
          g_assert_not_reached ();
        }
    

    src/terminal-window.c, resolve ~ and relative paths (./ & ../)

    static void
    popup_open_url_callback (GtkAction *action,
                             TerminalWindow *window)
    {
      TerminalWindowPrivate *priv = window->priv;
      TerminalScreenPopupInfo *info = priv->popup_info;
    
      if (info == NULL)
        return;
    
      if (info->flavour==FLAVOR_DEFAULT_TO_FILE){
        if (info->string[0]=='~') {
          char* current_dir_full=terminal_util_resolve_relative_path (g_get_home_dir(), &(info->string)[2]);
          terminal_util_open_url (GTK_WIDGET (window), current_dir_full, info->flavour,
                                gtk_get_current_event_time ());
        } else {
          char* current_dir=terminal_screen_get_current_dir_with_fallback (info->screen);
          char* current_dir_full=terminal_util_resolve_relative_path (current_dir, info->string);
          terminal_util_open_url (GTK_WIDGET (window), current_dir_full, info->flavour,
                                gtk_get_current_event_time ());
        }
      }
      else {
        terminal_util_open_url (GTK_WIDGET (window), info->string, info->flavour,
                              gtk_get_current_event_time ());
      }
    }
    

    ...

      show_link = info->string != NULL && (info->flavour == FLAVOR_AS_IS ||
                                           info->flavour == FLAVOR_DEFAULT_TO_HTTP ||
                                           info->flavour == FLAVOR_LP ||
                                           info->flavour == FLAVOR_DEFAULT_TO_FILE );
    

    ...

    static gboolean
    screen_match_clicked_cb (TerminalScreen *screen,
                             const char *match,
                             int flavour,
                             guint state,
                             TerminalWindow *window)
    {
      TerminalWindowPrivate *priv = window->priv;
    
      if (screen != priv->active_screen)
        return FALSE;
    
      gtk_widget_grab_focus (GTK_WIDGET (screen));
    
      if (flavour==FLAVOR_DEFAULT_TO_FILE){
        if (match[0]=='~') {
          char* current_dir_full=terminal_util_resolve_relative_path (g_get_home_dir(), &(match)[2]);
          terminal_util_open_url (GTK_WIDGET (window), current_dir_full, flavour,
                                gtk_get_current_event_time ());
        } else {
          char* current_dir=terminal_screen_get_current_dir_with_fallback (screen);
          char* current_dir_full=terminal_util_resolve_relative_path (current_dir, match);
          terminal_util_open_url (GTK_WIDGET (window), current_dir_full, flavour,
                                gtk_get_current_event_time ());
        }
      }
      else {
        terminal_util_open_url (GTK_WIDGET (window), match, flavour,
                                gtk_get_current_event_time ());
      }
    
      return TRUE;
    }
    
  3. Build & install

    cd gnome-terminal-3.6.2/
    ./configure
    make
    make install
    

Solution 2:

Only links can be opened from a terminal (right-click -> Open Link), basically you'd need to prefix your file path with the full URI:

/home/dell/pcl/2d/include/pcl/2d/impl/keypoint.hpp

becoming (just add the file:// prefix):

file:///home/dell/pcl/2d/include/pcl/2d/impl/keypoint.hpp

enter image description here

Note: This command will give you the absolute path URI to a file:

echo file://$(readlink -f <your file>)

Personally gedit /home/dell/pcl/2d/include/pcl/2d/impl/keypoint.hpp is probably the best option.