From a9706ec27edc0aef03f72daba28647aca1c42070 Mon Sep 17 00:00:00 2001 From: Juhani Krekelä Date: Sat, 24 Jul 2021 19:15:13 +0300 Subject: Add ability to specify additional URI handlers This commit adds a new section, open-with, to the configuration file. The options specified there will be added as "Open with …" menu items in the URI context menu. This is to make it easier to perform other actions on the URI than opening it in the system default browser; a user might for example add another browser, media player, or a script that preprocesses the URI before opening it. --- terminal.vala | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'terminal.vala') diff --git a/terminal.vala b/terminal.vala index 0013abd..c392d7f 100644 --- a/terminal.vala +++ b/terminal.vala @@ -32,11 +32,14 @@ class Terminal : Gtk.Overlay { [GtkChild] unowned Gtk.Revealer search_revealer; [GtkChild] unowned Gtk.ScrolledWindow scrolled_window; [GtkChild] unowned Gtk.SearchEntry search_entry; + [GtkChild] unowned Gtk.SeparatorMenuItem open_with_separator; [GtkChild] unowned Vte.Terminal vte; Gtk.Clipboard clipboard; Gtk.Clipboard primary; Gtk.Settings settings; + Gtk.MenuItem[] open_with_items = {}; + bool has_search; bool overlay_scrolling_env_override; string url_match; @@ -135,6 +138,21 @@ class Terminal : Gtk.Overlay { vte.set_color_highlight_foreground(conf.selection_foreground); vte.set_color_highlight(conf.selection_background); + foreach (var item in open_with_items) { + uri_context_menu.remove(item); + } + open_with_items.resize(0); + + foreach(var program in conf.open_with_programs) { + // TRANSLATORS: %s is the name of a "open with …" program set by the user in the configuration + var item = new Gtk.MenuItem.with_label(_("Open with %s").printf(program.name)); + item.activate.connect(() => { uri_open_with(program.command); }); + item.set_visible(true); + open_with_items += item; + uri_context_menu.add(item); + } + open_with_separator.set_visible(conf.open_with_programs.length > 0); + if (conf.get_warnings().length > 0) { string header = "%s\n".printf(_("Configuration loaded with warnings:")); infobar_show(header + string.joinv("\n", conf.get_warnings()), Gtk.MessageType.WARNING); @@ -316,6 +334,28 @@ class Terminal : Gtk.Overlay { } } + void uri_open_with(string[] command) { + string uri; + if (url_match != null) { + uri = url_match; + } else if (hyperlink_match != null) { + uri = hyperlink_match; + } else { + return; + } + + string[] argv = {}; + foreach (var arg in command) { + if (arg == "%") { + argv += uri; + } else { + argv += arg; + } + } + + spawn_process(null, argv); + } + void adjust_font_scale(double scale) { vte.set_font_scale(scale.clamp(FONT_SCALE_MIN, FONT_SCALE_MAX)); } -- cgit v1.2.3-2-gb3c3