Re: FVWM: Using output of external programs.

From: Daniel M. <xt_at_nm.ru>
Date: Wed, 13 Oct 2004 00:04:22 +0200

> It's hard to say what goes wrong without knowing the script. Can
> you please post it to the list?
>
> Ciao
>
> Dominik ^_^ ^_^

The script is included below. It is only a first draft version,
and using anything complex will probably break it.

Usage:

The script receives one argument, which should be a path to
a directory where menu configuration files reside.

The script (recursively) does the following:

Every directory becomes a menu, with the same label as a
directory. for example, ~/.fvwm/menu will become a manu with
'menu' as a label (the menu itself will be named ~/.fvwm/menu).
Subirectories in a given directory will become submenus, and
files in a directory will contain configuration for menu items.
For example, '+ "Firefox browser" Exec exec mozilla-firefox'
expression, which can be part of a AddToMenu command, will appear
in some file in a directory without the leading '+'. Such an
expressions may be put into one or more files in a directory (in
case of multiple files, their contents will be simply concatanated
- files would be read in alphabetical order of their names).

Single letter on a line tells how to sort items/submenus in a
menu. Default is an alphabetical descending order of labels,
submenus first (which is the only working option right now). Actually,
sort order may be set for every section of a menu. Secion is all
the items from the beginning of a menu/separator till separator/end
of a menu. By default, all submenus are put into the first section.
If user wants to put it in other section, he has to insert double-quoted
name (label, actually) of that submenu into the desired section.

One of the rules, is that every line should contain a command,
in addition to menu item's label. For example, it won't work to put
'""' all alone on a line to create a separator - it should be '"" Nop',
otherwise the line would be disregarded.


And here is a script:

---------------------------------------------------
#!/usr/bin/perl

use strict;

my $dir = $ARGV[0];
my $menus_config;

process_dir($dir);
print $menus_config;


sub process_dir {
        my ($dir, _at_dir_contents, @menu_contents, %submenus_list) = shift;

        opendir(DIR, $dir) or warn "Counldn't read directory $dir: $!. Skipping...\n"
                              and return;
         _at_dir_contents = sort(readdir(DIR));

        foreach (_at_dir_contents) {
                next if (/^(\.|\.\.)$/);
                $submenus_list{$_} = 1 and next if (-d "$dir/$_");
                if (-f "$dir/$_") {
                        open(FILE, "$dir/$_") or next;
                        push _at_menu_contents, <FILE>;
                        close(FILE);
                }
        }



        make_menu($dir, \%submenus_list, \_at_menu_contents);

        foreach (keys %submenus_list) {
                process_dir("$dir/$_");
        }
}
        

sub make_menu {
        my ($dir, $submenus_list_ref, $menu_contents_ref) = _at__;

        my ($section, _at_sections) = [];
        foreach (_at_$menu_contents_ref) {
                s/^\s*//;
                s/\s*$//;
                next if ($_ eq "");
                
                push(_at_$section, ("CONTROL", lc($_))) and next if (length == 1);

                /^([^"].*?)\s{1,}(.*)/ or /^"(.*?)"\s*(.*)/;
                # separator or tear up menu
                if ($1 eq "") {
                        push(_at_$section, ("", $2));
                        push(_at_sections, $section);
                        $section = [];
                        next;
                }
                
                # means that it submenu is supposed to be inserted here
                if ($2 eq "" and exists $$submenus_list_ref{$1}) {
                        delete $$submenus_list_ref{$1};
                } elsif ($2 eq "") {
                        next;
                }
                
                push(_at_$section, ($1, $2));
        }
        push(_at_sections, $section);

        # if some of the submenu names were not in one of the sections,
        # insert them all into the first section, and sort according to
        # that section's rules.
        foreach (keys %$submenus_list_ref) {
                push(_at_{$sections[0]}, ($_, ""));
        }
                
        $menus_config .= "AddToMenu \"$dir\"\n";
        foreach (_at_sections) {
                sort_section($dir, $_);
                foreach (_at_$_) {
                        $menus_config .= $_;
                }
        }

}
                        
sub sort_section {
        my $dir = shift;
        my $section_ref = shift;
        my %entries = _at_$section_ref;

        my $sort_type = $entries{CONTROL};
        delete $entries{CONTROL};
        
        my (_at_submenus, @items, @processed_section);
        # Alphabetic sort, submenus at the beginning
        if ($sort_type eq "") {
                foreach (sort keys %entries) {
                        push(_at_submenus, "+ \"$_\" PopUp \"$dir/$_\"\n") and next
                                if ($entries{$_} eq "");
                                
                        push(_at_items, "+ \"$_\" $entries{$_}\n");
                }
                
                _at_processed_section = (@submenus, @items);
                
        } elsif ($sort_type eq "n") { # no sorting
        
                for (my $n = 0; $n < $#$section_ref; $n += 2) {
                        push(_at_processed_section, "+ \"$section_ref->[$n]\" PopUp ",
                             "\"$dir/$section_ref->[$n]\"\n") and next
                             if ( $section_ref->[$n+1] eq "");
                                
                        push(_at_processed_section, "+ \"$section_ref->[$n]\" ",
                             "$section_ref->[$n+1]\n");
                }
        }
        
        _at_$section_ref = @processed_section;
}
---------------------------------------------------

I have a great doubt, though, that somebody is going to
get to that last line :).


Daniel.
--
Visit the official FVWM web page at <URL: http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm" in the body of a
message to majordomo_at_fvwm.org.
To report problems, send mail to fvwm-owner_at_fvwm.org.
Received on Tue Oct 12 2004 - 16:59:43 BST

This archive was generated by hypermail 2.3.0 : Mon Aug 29 2016 - 19:37:57 BST