]> git.sur5r.net Git - i3/i3/blobdiff - i3-dmenu-desktop
Update copyright notices and get rid of ranges
[i3/i3] / i3-dmenu-desktop
index e0a35b8dbb87c19d4d2f795ee80c3611241c139c..a0549ead7dbb81c37991cc748f782ef2eace0f33 100755 (executable)
@@ -45,7 +45,7 @@ my $result = GetOptions(
     'dmenu=s' => \$dmenu_cmd,
     'entry-type=s' => \@entry_types,
     'version' => sub {
-        say "dmenu-desktop 1.4 © 2012 Michael Stapelberg";
+        say "dmenu-desktop 1.5 © 2012 Michael Stapelberg";
         exit 0;
     },
     'help' => sub {
@@ -55,8 +55,12 @@ 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', 'command', 'filename');
-@entry_types = grep { $_ ~~ @valid_types } @entry_types;
+my $valid_types = {
+    name => 1,
+    command => 1,
+    filename => 1,
+};
+@entry_types = grep { exists($valid_types->{$_}) } @entry_types;
 @entry_types = ('name', 'command') unless @entry_types;
 
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
@@ -175,6 +179,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' ||
@@ -256,7 +261,7 @@ for my $app (keys %apps) {
         }
     }
 
-    if ('name' ~~ @entry_types) {
+    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
@@ -272,22 +277,22 @@ for my $app (keys %apps) {
         $choices{$name} = $app;
     }
 
-    if ('command' ~~ @entry_types) {
+    if ((scalar grep { $_ eq 'command' } @entry_types) > 0) {
         my ($command) = split(' ', $apps{$app}->{Exec});
 
         # Don’t add “geany” if “Geany” is already present.
         my @keys = map { lc } keys %choices;
-        next if lc(basename($command)) ~~ @keys;
+        next if (scalar grep { $_ eq lc(basename($command)) } @keys) > 0;
 
         $choices{basename($command)} = $app;
     }
 
-    if ('filename' ~~ @entry_types) {
+    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 lc($filename) ~~ @keys;
+        next if (scalar grep { $_ eq lc($filename) } @keys) > 0;
 
         $choices{$filename} = $app;
     }
@@ -301,7 +306,7 @@ 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.
@@ -346,7 +351,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} = '';
     }
 }
 
@@ -369,6 +380,7 @@ sub quote {
 
 $choice = quote($choice);
 $location = quote($location);
+$name = quote($name);
 
 # Remove deprecated field codes, as the spec dictates.
 $exec =~ s/%[dDnNvm]//g;
@@ -397,6 +409,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}) {
@@ -501,7 +517,7 @@ command), and "libreoffice-writer" (type = filename).
 
 =head1 VERSION
 
-Version 1.4
+Version 1.5
 
 =head1 AUTHOR