]> git.sur5r.net Git - i3/i3/blobdiff - i3-dmenu-desktop
Merge branch 'release-4.16.1'
[i3/i3] / i3-dmenu-desktop
index 20c7fbeaaec296aafe71becc8b60a40a506bf6d8..d57e9a38c1d2a7fdae5d587fd11dad201022775f 100755 (executable)
@@ -6,7 +6,7 @@
 # No dependencies except for perl ≥ v5.10
 
 use strict;
-use warnings;
+use warnings qw(FATAL utf8);
 use Data::Dumper;
 use IPC::Open2;
 use POSIX qw(locale_h);
@@ -17,25 +17,39 @@ use Getopt::Long;
 use Pod::Usage;
 use v5.10;
 use utf8;
-use open ':encoding(utf8)';
+use open ':encoding(UTF-8)';
 
 binmode STDOUT, ':utf8';
 binmode STDERR, ':utf8';
 
 # reads in a whole file
 sub slurp {
-    open(my $fh, '<', shift) or die "$!";
+    my ($filename) = @_;
+    my $fh;
+    if (!open($fh, '<', $filename)) {
+        warn "Could not open $filename: $!";
+        return undef;
+    }
     local $/;
-    <$fh>;
+    my $result;
+    eval {
+        $result = <$fh>;
+    };
+    if ($@) {
+        warn "Could not read $filename: $@";
+        return undef;
+    } else {
+        return $result;
+    }
 }
 
-my $entry_type = 'both';
+my @entry_types;
 my $dmenu_cmd = 'dmenu -i';
 my $result = GetOptions(
     'dmenu=s' => \$dmenu_cmd,
-    'entry-type=s' => \$entry_type,
+    'entry-type=s' => \@entry_types,
     'version' => sub {
-        say "dmenu-desktop 1.2 © 2012 Michael Stapelberg";
+        say "dmenu-desktop 1.5 © 2012 Michael Stapelberg";
         exit 0;
     },
     'help' => sub {
@@ -44,6 +58,15 @@ my $result = GetOptions(
 
 die "Could not parse command line options" unless $result;
 
+# Filter entry types and set default type(s) if none selected
+my $valid_types = {
+    name => 1,
+    command => 1,
+    filename => 1,
+};
+@entry_types = grep { exists($valid_types->{$_}) } @entry_types;
+@entry_types = ('name', 'command') unless @entry_types;
+
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
 # ┃ Convert LC_MESSAGES into an ordered list of suffixes to search for in the ┃
 # ┃ .desktop files (e.g. “Name[de_DE@euro]” for LC_MESSAGES=de_DE.UTF-8@euro  ┃
@@ -52,7 +75,7 @@ die "Could not parse command line options" unless $result;
 # For details on how the transformation of LC_MESSAGES to a list of keys that
 # should be looked up works, refer to “Localized values for keys” of the
 # “Desktop Entry Specification”:
-# http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s04.html
+# https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s04.html
 my $lc_messages = setlocale(LC_MESSAGES);
 
 # Ignore the encoding (e.g. .UTF-8)
@@ -80,7 +103,7 @@ push @suffixes, $lc_messages;
 # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 
 my %desktops;
-# See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
+# See https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
 my $xdg_data_home = $ENV{XDG_DATA_HOME};
 $xdg_data_home = $ENV{HOME} . '/.local/share' if
     !defined($xdg_data_home) ||
@@ -135,7 +158,9 @@ for my $file (values %desktops) {
     # Extract all “Name” and “Exec” keys from the [Desktop Entry] group
     # and store them in $apps{$base}.
     my %names;
-    my @lines = split("\n", slurp($file));
+    my $content = slurp($file);
+    next unless defined($content);
+    my @lines = split("\n", $content);
     for my $line (@lines) {
         my $first = substr($line, 0, 1);
         next if $line eq '' || $first eq '#';
@@ -158,6 +183,7 @@ for my $file (values %desktops) {
             $names{$key} = $value;
         } elsif ($key eq 'Exec' ||
                  $key eq 'TryExec' ||
+                 $key eq 'Path' ||
                  $key eq 'Type') {
             $apps{$base}->{$key} = $value;
         } elsif ($key eq 'NoDisplay' ||
@@ -166,7 +192,7 @@ for my $file (values %desktops) {
                  $key eq 'Terminal') {
             # Values of type boolean must either be string true or false,
             # see “Possible value types”:
-            # http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html
+            # https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s03.html
             $apps{$base}->{$key} = ($value eq 'true');
         }
     }
@@ -209,8 +235,15 @@ for my $app (keys %apps) {
     next if (!exists($apps{$app}->{Type}) ||
              $apps{$app}->{Type} ne 'Application');
 
+    # Skip broken files (Type=application, but no Exec key).
+    if (!exists($apps{$app}->{Exec}) ||
+        $apps{$app}->{Exec} eq '') {
+        warn 'File ' . $apps{$app}->{_Location} . ' is broken: it contains Type=Application, but no Exec key/value pair.';
+        next;
+    }
+
     # Don’t offer apps which have NoDisplay == true or Hidden == true.
-    # See http://wiki.xfce.org/howto/customize-menu#hide_menu_entries
+    # See https://wiki.xfce.org/howto/customize-menu#hide_menu_entries
     # for the difference between NoDisplay and Hidden.
     next if (exists($apps{$app}->{NoDisplay}) && $apps{$app}->{NoDisplay}) ||
             (exists($apps{$app}->{Hidden}) && $apps{$app}->{Hidden});
@@ -232,7 +265,7 @@ for my $app (keys %apps) {
         }
     }
 
-    if ($entry_type eq 'name' || $entry_type eq 'both') {
+    if ((scalar grep { $_ eq 'name' } @entry_types) > 0) {
         if (exists($choices{$name})) {
             # There are two .desktop files which contain the same “Name” value.
             # I’m not sure if that is allowed to happen, but we disambiguate the
@@ -246,11 +279,52 @@ for my $app (keys %apps) {
         }
 
         $choices{$name} = $app;
+        next;
+    }
+
+    if ((scalar grep { $_ eq 'command' } @entry_types) > 0) {
+        my $command = $apps{$app}->{Exec};
+
+        # Handle escape sequences (should be done for all string values, but does
+        # matter here).
+        my %escapes = (
+            '\\s' => ' ',
+            '\\n' => '\n',
+            '\\t' => '\t',
+            '\\r' => '\r',
+            '\\\\' => '\\',
+        );
+        $command =~ s/(\\[sntr\\])/$escapes{$1}/go;
+
+        # Extract executable
+        if ($command =~ m/^\s*([^\s\"]+)(?:\s|$)/) {
+            # No quotes
+            $command = $1;
+        } elsif ($command =~ m/^\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"(?:\s|$)/) {
+            # Quoted, remove quotes and fix escaped characters
+            $command = $1;
+            $command =~ s/\\([\"\`\$\\])/$1/g;
+        } else {
+            # Invalid quotes, fallback to whitespace
+            ($command) = split(' ', $command);
+        }
+
+        # Don’t add “geany” if “Geany” is already present.
+        my @keys = map { lc } keys %choices;
+        if (!(scalar grep { $_ eq lc(basename($command)) } @keys) > 0) {
+            $choices{basename($command)} = $app;
+        }
+        next;
     }
 
-    if ($entry_type eq 'command' || $entry_type eq 'both') {
-        my ($command) = split(' ', $apps{$app}->{Exec});
-        $choices{basename($command)} = $app;
+    if ((scalar grep { $_ eq 'filename' } @entry_types) > 0) {
+        my $filename = basename($app, '.desktop');
+
+        # Don’t add “geany” if “Geany” is already present.
+        my @keys = map { lc } keys %choices;
+        next if (scalar grep { $_ eq lc($filename) } @keys) > 0;
+
+        $choices{$filename} = $app;
     }
 }
 
@@ -262,12 +336,19 @@ for my $app (keys %apps) {
 #   };
 
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
-# ┃ Run dmenu to ask the user for her choice                                  ┃
+# ┃ Run dmenu to ask the user for their choice                                ┃
 # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 
 # open2 will just make dmenu’s STDERR go to our own STDERR.
 my ($dmenu_out, $dmenu_in);
-my $pid = open2($dmenu_out, $dmenu_in, $dmenu_cmd);
+my $pid = eval {
+    open2($dmenu_out, $dmenu_in, $dmenu_cmd);
+} or do {
+    print STDERR "$@";
+    say STDERR "Running dmenu failed. Is dmenu installed at all? Try running dmenu -v";
+    exit 1;
+};
+
 binmode $dmenu_in, ':utf8';
 binmode $dmenu_out, ':utf8';
 
@@ -282,6 +363,8 @@ my $status = ($? >> 8);
 exit $status unless $status == 0;
 
 my $choice = <$dmenu_out>;
+# dmenu ≥ 4.4 adds a newline after the choice
+chomp($choice);
 my $app;
 # Exact match: the user chose “Avidemux (GTK+)”
 if (exists($choices{$choice})) {
@@ -298,7 +381,13 @@ if (exists($choices{$choice})) {
         last;
     }
     if (!defined($app)) {
-        die "Invalid input: “$choice” does not match any application.";
+        warn "Invalid input: “$choice” does not match any application. Trying to execute nevertheless.";
+        $app->{Name} = '';
+        $app->{Exec} = $choice;
+        # We assume that the app is old and does not support startup
+        # notifications because it doesn’t ship a desktop file.
+        $app->{StartupNotify} = 0;
+        $app->{_Location} = '';
     }
 }
 
@@ -311,7 +400,7 @@ my $exec = $app->{Exec};
 my $location = $app->{_Location};
 
 # Quote as described by “The Exec key”:
-# http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
+# https://standards.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
 sub quote {
     my ($str) = @_;
     $str =~ s/("|`|\$|\\)/\\$1/g;
@@ -321,6 +410,7 @@ sub quote {
 
 $choice = quote($choice);
 $location = quote($location);
+$name = quote($name);
 
 # Remove deprecated field codes, as the spec dictates.
 $exec =~ s/%[dDnNvm]//g;
@@ -341,6 +431,7 @@ $exec =~ s/%c/$name/g;
 # XXX: Icons are not implemented. Is the complexity (looking up the path if
 # only a name is given) actually worth it?
 #$exec =~ s/%i/--icon $icon/g;
+$exec =~ s/%i//g;
 
 # location of .desktop file
 $exec =~ s/%k/$location/g;
@@ -348,6 +439,10 @@ $exec =~ s/%k/$location/g;
 # Literal % characters are represented as %%.
 $exec =~ s/%%/%/g;
 
+if (exists($app->{Path}) && $app->{Path} ne '') {
+    $exec = 'cd ' . quote($app->{Path}) . ' && ' . $exec;
+}
+
 my $nosn = '';
 my $cmd;
 if (exists($app->{Terminal}) && $app->{Terminal}) {
@@ -392,7 +487,7 @@ system('i3-msg', $cmd) == 0 or die "Could not launch i3-msg: $?";
 
 =head1 SYNOPSIS
 
-    i3-dmenu-desktop [--dmenu='dmenu -i'] [--entry-type=both]
+    i3-dmenu-desktop [--dmenu='dmenu -i'] [--entry-type=name]
 
 =head1 DESCRIPTION
 
@@ -441,17 +536,18 @@ version of dmenu.
 
 =item B<--entry-type=type>
 
-Display the (localized) "Name" (type = name) or the command (type = command) or
-both (type = both) in dmenu.
+Display the (localized) "Name" (type = name), the command (type = command) or
+the (*.desktop) filename (type = filename) in dmenu. This option can be
+specified multiple times.
 
 Examples are "GNU Image Manipulation Program" (type = name), "gimp" (type =
-command) and both (type = both).
+command), and "libreoffice-writer" (type = filename).
 
 =back
 
 =head1 VERSION
 
-Version 1.2
+Version 1.5
 
 =head1 AUTHOR