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. --- config.vala | 9 +++++++++ configreader.vala | 38 ++++++++++++++++++++++++++++++++++++++ po/de.po | 40 ++++++++++++++++++++++++++++------------ po/fi.po | 40 ++++++++++++++++++++++++++++------------ po/weltschmerz.pot | 42 +++++++++++++++++++++++++++++------------- terminal.ui | 5 +++++ terminal.vala | 40 ++++++++++++++++++++++++++++++++++++++++ weltschmerz.1 | 20 +++++++++++++++++++- 8 files changed, 196 insertions(+), 38 deletions(-) diff --git a/config.vala b/config.vala index 0234aac..c0a084f 100644 --- a/config.vala +++ b/config.vala @@ -1,3 +1,8 @@ +struct OpenWithProgram { + string name; + string[] command; +} + class Config { public bool autohide_mouse; @@ -18,6 +23,8 @@ class Config { public Gdk.RGBA[] palette = new Gdk.RGBA[16]; + public OpenWithProgram[] open_with_programs; + struct PaletteEntry { string name; string normal; @@ -65,6 +72,8 @@ class Config { palette[i + 8] = reader.read_colour("colours", "bright." + entry.name, entry.bright); } + open_with_programs = reader.read_open_with({}); + reader.log_unknown_keys(); } diff --git a/configreader.vala b/configreader.vala index 46f6b1f..8336637 100644 --- a/configreader.vala +++ b/configreader.vala @@ -208,4 +208,42 @@ class ConfigReader { return SYSTEM; } } + + public OpenWithProgram[] read_open_with(OpenWithProgram[] default) { + if (keyfile == null) + return default; + + OpenWithProgram[] programs = {}; + + string[] keys; + try { + keys = keyfile.get_keys("open-with"); + } catch (GLib.KeyFileError e) { + return default; + } + + foreach (var name in keys) { + var command_string = read_string("open-with", name, null); + if (command_string == null) + continue; + + string[] command; + try { + Shell.parse_argv(command_string, out command); + } catch (ShellError e) { + append_warning(_("ignoring open-with.%s due to malformed command: %s").printf(name, e.message)); + continue; + } + + if (!("%" in command)) { + append_warning(_("ignoring command in open-with.%s: missing '%%'").printf(name)); + continue; + } + + OpenWithProgram program = {name, command}; + programs += program; + } + + return programs; + } } diff --git a/po/de.po b/po/de.po index c62f019..220ca39 100644 --- a/po/de.po +++ b/po/de.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: weltschmerz\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-07-20 20:19+0300\n" +"POT-Creation-Date: 2021-07-24 19:10+0300\n" "PO-Revision-Date: 2021-07-20 20:22+0300\n" "Last-Translator: Juhani Krekelä \n" "Language-Team: Wolfgang Müller\n" @@ -44,6 +44,16 @@ msgstr "ungültige Cursor-Form '%s' in %s.%s" msgid "invalid cursor blink setting '%s' in %s.%s" msgstr "ungültige Cursor-Blinkeinstellung '%s' in %s.%s" +#: configreader.vala:234 +#, c-format +msgid "ignoring open-with.%s due to malformed command: %s" +msgstr "" + +#: configreader.vala:239 +#, c-format +msgid "ignoring command in open-with.%s: missing '%%'" +msgstr "" + #: terminal.ui:16 msgid "_Copy URI" msgstr "URI _kopieren" @@ -52,47 +62,53 @@ msgstr "URI _kopieren" msgid "Copy the URI to the clipboard" msgstr "Kopiert die URI in die Zwischenablage" -#: terminal.ui:53 +#: terminal.ui:58 msgid "Copy as _HTML" msgstr "Als _HTML kopieren" -#: terminal.ui:56 +#: terminal.ui:61 msgid "Copy the selection as HTML" msgstr "Kopiert die Auswahl als HTML" -#: terminal.ui:80 +#: terminal.ui:85 msgid "Open _terminal" msgstr "_Terminal öffnen" -#: terminal.ui:83 +#: terminal.ui:88 msgid "Open new terminal window in the current directory" msgstr "Öffnet ein neues Terminalfenster im aktuellen Verzeichnis" -#: terminal.ui:93 +#: terminal.ui:98 msgid "_Open directory" msgstr "_Verzeichnis öffnen" -#: terminal.ui:96 +#: terminal.ui:101 msgid "Open current directory in the file manager" msgstr "Öffnet das aktuelle Verzeichnis im Dateimanager" -#: terminal.ui:171 +#: terminal.ui:176 msgid "Search..." msgstr "Suchen..." -#: terminal.ui:190 +#: terminal.ui:195 msgid "Go to next result" msgstr "Gehe zum nächsten Suchergebnis" -#: terminal.ui:206 +#: terminal.ui:211 msgid "Go to previous result" msgstr "Gehe zum vorherigen Suchergebnis" -#: terminal.vala:139 +#. TRANSLATORS: %s is the name of a "open with …" program set by the user in the configuration +#: terminal.vala:148 +#, c-format +msgid "Open with %s" +msgstr "" + +#: terminal.vala:157 msgid "Configuration loaded with warnings:" msgstr "Konfiguration wurde mit Warnungen geladen:" -#: terminal.vala:142 +#: terminal.vala:160 msgid "Configuration loaded successfully." msgstr "Konfiguration wurde erfolgreich geladen." diff --git a/po/fi.po b/po/fi.po index 0372708..e816719 100644 --- a/po/fi.po +++ b/po/fi.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: weltschmerz\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-07-20 20:19+0300\n" +"POT-Creation-Date: 2021-07-24 19:10+0300\n" "PO-Revision-Date: 2021-07-20 20:22+0300\n" "Last-Translator: Juhani Krekelä \n" "Language-Team: nortti\n" @@ -44,6 +44,16 @@ msgstr "tuntematon osoittimen muoto '%s' asetuksessa %s.%s" msgid "invalid cursor blink setting '%s' in %s.%s" msgstr "tuntematon osoitiimen välkkymisasetus '%s' asetuksessa %s.%s" +#: configreader.vala:234 +#, c-format +msgid "ignoring open-with.%s due to malformed command: %s" +msgstr "" + +#: configreader.vala:239 +#, c-format +msgid "ignoring command in open-with.%s: missing '%%'" +msgstr "" + #: terminal.ui:16 msgid "_Copy URI" msgstr "_Kopioi URI" @@ -52,47 +62,53 @@ msgstr "_Kopioi URI" msgid "Copy the URI to the clipboard" msgstr "Kopioi URI leikepöydälle" -#: terminal.ui:53 +#: terminal.ui:58 msgid "Copy as _HTML" msgstr "Kopioi _HTML:nä" -#: terminal.ui:56 +#: terminal.ui:61 msgid "Copy the selection as HTML" msgstr "Kopioi valinta HTML:nä" -#: terminal.ui:80 +#: terminal.ui:85 msgid "Open _terminal" msgstr "Avaa _pääte" -#: terminal.ui:83 +#: terminal.ui:88 msgid "Open new terminal window in the current directory" msgstr "Avaa pääteikkuna tämänhetkisessä hakemistossa" -#: terminal.ui:93 +#: terminal.ui:98 msgid "_Open directory" msgstr "_Avaa hakemisto" -#: terminal.ui:96 +#: terminal.ui:101 msgid "Open current directory in the file manager" msgstr "Avaa tämänhetkinen hakemisto tiedostoselaimessa" -#: terminal.ui:171 +#: terminal.ui:176 msgid "Search..." msgstr "Etsi..." -#: terminal.ui:190 +#: terminal.ui:195 msgid "Go to next result" msgstr "Seuraava osuma" -#: terminal.ui:206 +#: terminal.ui:211 msgid "Go to previous result" msgstr "Edellinen osuma" -#: terminal.vala:139 +#. TRANSLATORS: %s is the name of a "open with …" program set by the user in the configuration +#: terminal.vala:148 +#, c-format +msgid "Open with %s" +msgstr "" + +#: terminal.vala:157 msgid "Configuration loaded with warnings:" msgstr "Asetukset ladattu varoituksin:" -#: terminal.vala:142 +#: terminal.vala:160 msgid "Configuration loaded successfully." msgstr "Asetukset ladattu onnistuneesti." diff --git a/po/weltschmerz.pot b/po/weltschmerz.pot index fd35ae0..f1d1b1d 100644 --- a/po/weltschmerz.pot +++ b/po/weltschmerz.pot @@ -8,13 +8,13 @@ msgid "" msgstr "" "Project-Id-Version: weltschmerz\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-07-20 20:19+0300\n" +"POT-Creation-Date: 2021-07-24 19:10+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "Language: \n" "MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=CHARSET\n" +"Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" @@ -44,6 +44,16 @@ msgstr "" msgid "invalid cursor blink setting '%s' in %s.%s" msgstr "" +#: configreader.vala:234 +#, c-format +msgid "ignoring open-with.%s due to malformed command: %s" +msgstr "" + +#: configreader.vala:239 +#, c-format +msgid "ignoring command in open-with.%s: missing '%%'" +msgstr "" + #: terminal.ui:16 msgid "_Copy URI" msgstr "" @@ -52,47 +62,53 @@ msgstr "" msgid "Copy the URI to the clipboard" msgstr "" -#: terminal.ui:53 +#: terminal.ui:58 msgid "Copy as _HTML" msgstr "" -#: terminal.ui:56 +#: terminal.ui:61 msgid "Copy the selection as HTML" msgstr "" -#: terminal.ui:80 +#: terminal.ui:85 msgid "Open _terminal" msgstr "" -#: terminal.ui:83 +#: terminal.ui:88 msgid "Open new terminal window in the current directory" msgstr "" -#: terminal.ui:93 +#: terminal.ui:98 msgid "_Open directory" msgstr "" -#: terminal.ui:96 +#: terminal.ui:101 msgid "Open current directory in the file manager" msgstr "" -#: terminal.ui:171 +#: terminal.ui:176 msgid "Search..." msgstr "" -#: terminal.ui:190 +#: terminal.ui:195 msgid "Go to next result" msgstr "" -#: terminal.ui:206 +#: terminal.ui:211 msgid "Go to previous result" msgstr "" -#: terminal.vala:139 +#. TRANSLATORS: %s is the name of a "open with …" program set by the user in the configuration +#: terminal.vala:148 +#, c-format +msgid "Open with %s" +msgstr "" + +#: terminal.vala:157 msgid "Configuration loaded with warnings:" msgstr "" -#: terminal.vala:142 +#: terminal.vala:160 msgid "Configuration loaded successfully." msgstr "" diff --git a/terminal.ui b/terminal.ui index 57e4ad9..6d5d206 100644 --- a/terminal.ui +++ b/terminal.ui @@ -24,6 +24,11 @@ + + + False + + True 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)); } diff --git a/weltschmerz.1 b/weltschmerz.1 index 77a67df..6b64184 100644 --- a/weltschmerz.1 +++ b/weltschmerz.1 @@ -1,4 +1,4 @@ -.Dd July 12, 2021 +.Dd July 24, 2021 .Dt WELTSCHMERZ 1 .Os .Sh NAME @@ -88,6 +88,9 @@ font = Iosevka Light 16 [colours] foreground = #000000 background = #ffffff + +[open-with] +mpv = mpv % .Ed .Pp Refer to the GLib documentation for detailed information on this format. @@ -254,6 +257,21 @@ selection.background/reverse video bold/inherit colour .TE .Ed +.Pp +The options in the +.Em open-with +section define programs that can be invoked from the URI context menu. +The option name determines the name of the program as displayed in the +menu, and the value the command to be executed. +.Pp +.Nm +handles quotes in the command following POSIX +.Xr sh 1 +rules, +but does not do wildcard or variable expansion. +Any +.Dq % +is replaced with the URI when executing the command. .Sh FILES .Bl -tag -width Ds .It Em $XDG_CONFIG_HOME/weltschmerz/config -- cgit v1.2.3-2-gb3c3