[SOLVED] FVWM Perl Preprocessing

I wonder if with some Perl Preprocessing it would be possible to simplify fvwm configuration files. For example, I have defined the following key-bindings using the numeric keypad:

[code]# KEYPAD: Switch pages via keypad (no modifier)
Key KP_Home A N GotoDesk 0 0
Key KP_Up A N GotoDesk 0 1
Key KP_Prior A N GotoDesk 0 2
Key KP_Left A N GotoDesk 0 3
Key KP_Begin A N GotoDesk 0 4
Key KP_Right A N GotoDesk 0 5
Key KP_End A N GotoDesk 0 6
Key KP_Down A N GotoDesk 0 7
Key KP_Next A N GotoDesk 0 8

alt: Move a window to a different page

Key KP_Home A M MoveToDesk 0 0
Key KP_Up A M MoveToDesk 0 1
Key KP_Prior A M MoveToDesk 0 2
Key KP_Left A M MoveToDesk 0 3
Key KP_Begin A M MoveToDesk 0 4
Key KP_Right A M MoveToDesk 0 5
Key KP_End A M MoveToDesk 0 6
Key KP_Down A M MoveToDesk 0 7
Key KP_Next A M MoveToDesk 0 8

ctrl: Show menus

Key KP_Home A C Menu Utils
Key KP_Up A C Menu Gent
Key KP_Prior A C Menu Oficina
Key KP_Left A C Menu Internet
Key KP_Begin A C Menu Sistema
Key KP_Right A C Menu Oci
Key KP_End A C Menu Xarxa
Key KP_Down A C Menu
Key KP_Next A C Menu Global[/code]

And some more, meaning that my key-binding definition is about 200 lines long. For example in awesome, using lua, all this lines can be greatly reduced (see this link). I was wondering if this can be achieved with some Perl Preprocessing or some kind of perl module. I have found some examples like this one that uses fvwm preprocessing, but only on short expressions. I have been unsuccessful at something like this:

[code]%{

{my @keys = qw /KP_Home KP_Up KP_Prior KP_Left KP_Begin KP_Right KP_End KP_Down KP_Next/;
my $i = 0;
foreach (@keys) {
{ “Key $_ A N GotoDesk 0 $i\n”; }
++$i;
}

}%[/code]

Thanks for your attention!

Fvwm rocks!!!

Opening fvwm with this instruction…

eval 'fvwm -c "Module FvwmPerl --preprocess /home/mimosinnet/.fvwm/config"'

… and having the Perl code in the .fvwm/config file and not into one of the files read by config, you can define the keypad keys with instructions like this:

[code]%{
my @keypad = qw (KP_Home KP_Up KP_Prior KP_Left KP_Begin KP_Right KP_End KP_Down KP_Next );
my @menus = qw (Utils Gent Oficina Internet Sistema Oci Chrome Xarxa Global);

my $code = “”;
for my $i ( 0 … 8 ) {
# KEYPAD: Switch pages via keypad (no modifier)
$code = $code . “Key $keypad[$i] A N GotoDesk 0 $i \n”;

# alt: Move a window to a different page
$code = $code . "Key $keypad[$i] A M MoveToDesk 0 $i \n";

# ctrl: Show menus
$code = $code . "Key $keypad[$i] A C Menu $menus[$i] \n";

# Shift: Go to a different desk and show menu
$code = $code . "Key $keypad[$i] A S  F-GotoDeskMenu 0 $i $menus[$i] \n";

# WindowKey: Move a window and follow it
$code = $code . "Key $keypad[$i] W 4  F-MoveAndGotoDesk 0 $i \n";

}
$code
}%[/code]

Thanks to Thomas for pointing me to the right direction!

Ah, no. I gave you a much more succinct version than what you’re using here, and I’d like to point out that I want nothing to do with the version you’re using; it’s horrid.

– Thomas Adam

I am sorry if I have implied this is the solution you have given me as I was referring to the previous post. Although I do not have much idea of programming, I can see that your approach is clearly more succinct and idiomatic. Nevertheless, I had difficulties in extending your example as I had some errors when using the two arrays (keys and menus), and the previous code seemed to work.

Having the previous code being qualified as horrid has given the extra motivation, and I think this is how the code should look like:

[code]%{
my @keypad = qw(KP_Home KP_Up KP_Prior KP_Left KP_Begin KP_Right KP_End KP_Down KP_Next);
my @menus = qw(Utils Gent Oficina Internet Sistema Oci Chrome Xarxa Global);
my $i;

KEYPAD: Move window to desk with the keypad (no modifier)

$i = 0; my $code1 = join (“\n”, map { "Key $_ A N GotoDesk 0 " . $i++} @keypad);

alt: Move a window to a different desk

$i = 0; my $code2 = join (“\n”, map { "Key $_ W M MoveToDesk 0 " . $i++} @keypad);

ctrl: Show menus related with the desk

$i = 0; my $code3 = join (“\n”, map { “Key $_ A C Menu $menus[$i++]”} @keypad);

Shift: Go to a different desk and show menu related with the desk

$i = 0; my $code4 = join (“\n”, map { “Key $_ A S F-GotoDeskMenu 0 $i $menus[$i++]”} @keypad);

WindowKey: Move a window to a different desk and follow it

$i = 0; my $code5 = join (“\n”, map { "Key $_ W 4 F-MoveAndGotoDesk 0 " . $i++} @keypad);

my $code = “$code1 \n$code2 \n$code3 \n$code4 \n$code5”;

$code;
}%[/code]

Cheers!

Edit: I have benchmarked both pieces of code (with the Benchmark) module to see if the horridness of the code was something more related with performance than aesthetics:

[code]Benchmark: timing 100000 iterations of awesome, horrid…
awesome: -1 wallclock secs ( 0.68 usr + 0.00 sys = 0.68 CPU) @ 147058.82/s (n=100000)
horrid: 9 wallclock secs ( 8.55 usr + 0.00 sys = 8.55 CPU) @ 11695.91/s (n=100000)

        Rate                   horrid awesome

horrid 11891/s – -92%
awesome 140845/s 1085% --[/code]

The moral of the story is that horrid code takes much longer!

Edit: Made small corrections to the code.