1FvwmPerl(1) Fvwm Modules FvwmPerl(1)
2
3
4
6 FvwmPerl - the fvwm perl manipulator and preprocessor
7
9 FvwmPerl should be spawned by fvwm(1) for normal functionality.
10
11 To run this module, place this command somewhere in the configuration:
12
13 Module FvwmPerl [params]
14
15 or:
16
17 ModuleSynchronize FvwmPerl [params]
18
19 if you want to immediately start to send commands to FvwmPerl.
20
22 This module is intended to extend fvwm commands with the perl scripting
23 power. It enables to embed perl expressions in the fvwm config files
24 and construct fvwm commands.
25
27 If you want to invoke the unique and persistent instanse of FvwmPerl,
28 it is suggested to do this from the StartFunction. Calling it from the
29 top is also possible, but involves some issues not discussed here.
30
31 AddToFunc StartFunction I Module FvwmPerl
32
33 There are several command line switches:
34
35 FvwmPerl [ --eval line ] [ --load file ] [ --preprocess [ --quote char
36 ] [ --winid wid ] [ --cmd ] [ --nosend ] [ --noremove ] [ line | file ]
37 ] [ --export [names] ] [ --stay ] [ --nolock ] [ alias ]
38
39 Long switches may be abbreviated to short one-letter switches.
40
41 -e|--eval line - evaluate the given perl code
42
43 -l|--load file - evaluate perl code in the given file
44
45 -p|--preprocess [ file ] - preprocess the given fvwm config file
46
47 The following 5 options are only valid together with --preprocess
48 option.
49
50 -c|--cmd line - an fvwm command to be preprocessed instead of file
51
52 -q|--quote char - change the default '%' quote
53
54 -w|--winid wid - set explicit window context (should begin with digit,
55 may be in oct or hex form; this window id overwrites implicit window
56 context if any)
57
58 --nosend - do not send the preprocessed file to fvwm for Reading, the
59 default is send. Useful for preprocessing non fvwm config files.
60
61 --noremove - do not remove the preprocessed file after sending it to
62 fvwm for Reading, the default is remove. Useful for debugging.
63
64 -x|--export [names] - define fvwm shortcut functions (by default, two
65 functions named "Eval" and "."). This option implies --stay.
66
67 -s|--stay - continues an execution after --eval, --load or --preprocess
68 are processed. By default, the module is not persistent in this case,
69 i.e. --nostay is assumed.
70
71 --nolock - when one of the 3 action options is given, this option
72 causes unlocking fvwm immediately. By default the requested action is
73 executed synchronously; this only makes difference when invoked like:
74
75 ModuleSynchronous FvwmPerl --preprocess someconfig.ppp
76
77 If --nolock is added here, ModuleSynchronous returns immediately. Note
78 that Module returns immediately regardless of this option.
79
81 Aliases allow one to have several module invocations and work
82 separately with all invocations, here is an example:
83
84 ModuleSynchronous FvwmPerl FvwmPerl-JustTest
85 SendToModule FvwmPerl-JustTest eval $a = 2 + 2; $b = $a
86 SendToModule FvwmPerl-JustTest eval cmd("Echo 2 + 2 = $b")
87 KillModule FvwmPerl FvwmPerl-JustTest
88
90 One of the effective proprocessing solutions is to pass the whole fvwm
91 configuration with embedded perl code to "FvwmPerl --preprocess". An
92 alternative approach is to write a perl script that produces fvwm
93 commands and sends them for execution, this script may be loaded using
94 "FvwmPerl --load". There are however intermediate solutions that
95 preprocess only separate configuration lines (or alternatively, execute
96 separate perl commands that produce fvwm commands).
97
98 The following code snippet adds ability of arithmetics and string
99 scripting to certain lines that need this. To use this, you want to
100 start FvwmPerl as your first command so that other commands may be
101 asked to be preprosessed.
102
103 ModuleSynchronize FvwmPerl
104
105 AddToFunc .
106 + I SendToModule FvwmPerl preprocess -c -- $*
107
108 . Exec exec xterm -name xterm-%{++$i}% # use unique name
109
110 . GotoDesk 0 %{ $[desk.n] + 1 }% # go to next desk
111
112 . Exec exec %{ -x "/usr/bin/X11/aterm" ? "aterm" : "xterm" }% -sb
113
114 # center a window
115 Next (MyWindow) . Move \
116 %{($WIDTH - $[w.width]) / 2}%p %{($HEIGHT - $[w.height]) / 2}%p
117
118 . Exec exec xmessage %{2 + 2}% # simple calculator
119
120 . %{main::show_message(2 + 2, "Yet another Calculator"); ""}%
121
123 There are several actions that FvwmPerl may perform:
124
125 eval perl-code
126 Evaluate a line of perl code.
127
128 A special function cmd("command") may be used in perl code to send
129 commands back to fvwm.
130
131 If perl code contains an error, it is printed to the standard error
132 stream with the [FvwmPerl][eval]: header prepended.
133
134 load file-name
135 Load a file of perl code. If the file is not fully qualified, it is
136 searched in the user directory $FVWM_USERDIR (usually ~/.fvwm) and
137 the system wide data directory $FVWM_DATADIR.
138
139 A special function cmd("command") may be used in perl code to send
140 commands back to fvwm.
141
142 If perl code contains an error, it is printed to the standard error
143 stream with the [FvwmPerl][load]: header prepended.
144
145 preprocess [-q|--quote char] [-c|--cmd] [line | file]
146 Preprocess fvwm config file or (if --cmd is given) line. This file
147 contains lines that are not touched (usually fvwm commands) and
148 specially preformatted perl code that is processed and replaced.
149 Text enclosed in %{ ... }% delimiters, that may start anywhere on
150 the line and end anywhere on the same or another line, is perl
151 code.
152
153 The quote parameter changes perl code delimiters. If a single char
154 is given, like '@', the delimiters are @{ ... }@. If the given
155 quote is 2 chars, like <>, the quotes are <{ ... }>
156
157 The perl code is substituted for the result of its evaluation. I.e.
158 %{$a = "c"; ++$a}% is replaced with "d".
159
160 The evaluation is unlike eval and load is done under the package
161 PreprocessNamespace and without use strict, so you are free to use
162 any variable names without fear of conflicts. Just don't use
163 uninitialized variables to mean undef or empty list (they may be in
164 fact initialized by the previous preprocess action), and do a
165 clean-up if needed. The variables and function in the main package
166 are still available, like ::cmd() or ::skip(), but it is just not a
167 good idea to access them while preprocessing.
168
169 There is a special function include(file) that loads a file,
170 preprocesses it and returns the preprocessed result. Avoid
171 recursion.
172
173 If any embedded perl code contains an error, it is printed to the
174 standard error stream and prepended with the
175 [FvwmPerl][preprocess]: header. The result of substitution is empty
176 in this case.
177
178 The following variables may be used in the perl code:
179
180 $USER, $DISPLAY, $WIDTH, $HEIGHT, $FVWM_VERSION, $FVWM_MODULEDIR,
181 $FVWM_DATADIR, $FVWM_USERDIR
182
183 The following line based directives are recognized when
184 preprocessing. They are processed after the perl code (if any) is
185 substituted.
186
187 %Repeat count
188 Causes the following lines to be repeated count times.
189
190 %ModuleConfig module-name [ destroy ]
191 Causes the following lines to be interpreted as the given
192 module configuration. If "destroy" is specified the previous
193 module configuration is destroyed first.
194
195 %Prefix prefix
196 Prefixes the following lines with the quoted prefix.
197
198 %End any-optional-comment
199 Ends any of the directives described above, may be nested.
200
201 Examples:
202
203 %Prefix "AddToFunc SwitchToWindow I"
204 Iconify off
205 WindowShade off
206 Raise
207 WarpToWindow 50 50
208 %End
209
210 %ModuleConfig FvwmPager destroy
211 Colorset 0
212 Font lucidasans-10
213 DeskTopScale 28
214 MiniIcons
215 %End ModuleConfig FvwmPager
216
217 %Prefix "All (MyWindowToAnimate) ResizeMove "
218 100 100 %{($WIDTH - 100) / 2}% %{($HEIGHT - 100) / 2}%
219 %Repeat %{$count}%
220 br w+2c w+2c w-1c w-1c
221 %End
222 %Repeat %{$count}%
223 br w-2c w-2c w+1c w+1c
224 %End
225 %End Prefix
226
227 Additional preprocess parameters --nosend and --noremove may be
228 given too. See their description at the top.
229
230 export [func-names]
231 Send to fvwm the definition of shortcut functions that help to
232 activate different actions of the module (i.e. eval, load and
233 preprocess).
234
235 Function names (func-names) may be separated by commas or/and
236 whitespace. By default, two functions "Eval" and "." are assumed.
237
238 The actual action defined in a function is guessed from the
239 function name if possible, where function name "." is reserved for
240 preprocess action.
241
242 For example, any of these two fvwm commands
243
244 SendToModule MyPerl export PerlEval,PP
245 FvwmPerl --export PerlEval,PP MyPerl
246
247 define the following two shortcut functions:
248
249 DestroyFunc PerlEval
250 AddToFunc I SendToModule MyPerl eval $*
251 DestroyFunc PP
252 AddToFunc I SendToModule MyPerl preprocess -c -- $*
253
254 These 4 actions may be requested in one of 3 ways: 1) in the command
255 line when FvwmPerl is invoked (in this case FvwmPerl is short-lived
256 unless --stay or --export is also given), 2) by sending the
257 corresponding message in fvwm config using SendToModule, 3) by calling
258 the corresponding perl function in perl code.
259
261 There are several functions that perl code may call:
262
263 cmd($fvwm_command)
264 In case of eval or load - send back to fvwm a string $fvwm_command.
265 In case of preprocess - append a string $fvwm_command to the output
266 of the embedded perl code.
267
268 do_eval($perl_code)
269 This function is equivalent to the eval functionality on the string
270 $perl_code, described above.
271
272 load($filename)
273 This function is equivalent to the load functionality on the file
274 $filename, described above.
275
276 preprocess(@params, ["-c $command"] [$filename])
277 This function is equivalent to the preprocess functionality with
278 the given parameters and the file $filename described above.
279
280 export($func_names, [$do_unexport])
281 This function is equivalent to the export functionality with the
282 given $func_names, described above. May also unexport the function
283 names if the second parameter is true.
284
285 Function names should be separated by commas or/and whitespace. If
286 $func_names is empty then functions "Eval" and "." are assumed.
287
288 stop()
289 Terminates the module.
290
291 skip()
292 Skips the rest of the event callback code, i.e. the module returns
293 to listen to new module events.
294
295 unlock()
296 Unsynchronizes the event callback from fvwm. This may be useful to
297 prevent deadlocks, i.e. usually fvwm kills the non-responding
298 module if the event callback is not finished in ModuleTimeout
299 seconds. This prevents it.
300
301 This example causes FvwmPerl to suspend its execution for one
302 minute:
303
304 SendModule FvwmPerl eval unlock(); sleep(60);
305
306 However, verify that there is no way a new message is sent by fvwm
307 while the module is busy, and fvwm stays locked on this new message
308 for too long. See also the detach solution if you need long lasting
309 operations.
310
311 detach()
312 Forks and detaches the rest of the event callback code from the
313 main process. This may be useful to prevent killing the module if
314 its event callback should take a long time to complete and it may
315 be done in the detached child. The detached child may still send
316 commands to fvwm (don't rely on this), but not receive the events
317 of course, it exits immediately after the callback execution is
318 finished.
319
320 If you use detach(), better only send commands to fvwm in one
321 process (the main one or the detached one), doing otherwise may
322 often cause conflicts.
323
324 show_message($msg, $title[, $use_stderr_too=1])
325 Shows a dialog window with the given message, using whichever X
326 tool is found in the system.
327
328 See FVWM::Module::Toolkit::show_message for more information.
329
331 There are several global variables in the main namespace that may be
332 used in the perl code:
333
334 $a, $b, ... $h
335 @a, @b, ... @h
336 %a, %b, ... %h
337
338 They all are initialized to the empty value and may be used to store a
339 state between different calls to FvwmPerl actions (eval and load).
340
341 If you need more readable variable names, either write "no strict
342 'vars';" at the start of every perl code or use a hash for this, like:
343
344 $h{id} = $h{first_name} . " " . $h{second_name}
345
346 or use a package name, like:
347
348 @MyMenu::terminals = qw( xterm rxvt );
349 $MyMenu::item_num = @MyMenu::terminals;
350
351 There may be a configuration option to turn strictness on and off.
352
354 FvwmPerl may receive messages using the fvwm command SendToModule. The
355 names, meanings and parameters of the messages are the same as the
356 corresponding actions, described above.
357
358 Additionally, a message stop causes a module to quit.
359
360 A message unexport [func-names] undoes the effect of export, described
361 in the ACTIONS section.
362
363 A message dump dumps the contents of the changed variables (not yet).
364
366 A simple test:
367
368 SendToModule FvwmPerl eval $h{dir} = $ENV{HOME}
369 SendToModule FvwmPerl eval load($h{dir} . "/test.fpl")
370 SendToModule FvwmPerl load $[HOME]/test.fpl
371 SendToModule FvwmPerl preprocess config.ppp
372 SendToModule FvwmPerl export Eval,PerlEval,PerlLoad,PerlPP
373 SendToModule FvwmPerl unexport PerlEval,PerlLoad,PerlPP
374 SendToModule FvwmPerl stop
375
376 The following example handles root backgrounds in fvwmrc. All these
377 commands may be added to StartFunction.
378
379 Module FvwmPerl --export PerlEval
380
381 # find all background pixmaps for a later use
382 PerlEval $a = $ENV{HOME} . "/bg"; \
383 opendir DIR, $a; @b = grep { /xpm$/ } readdir(DIR); closedir DIR
384
385 # build a menu of background pixmaps
386 AddToMenu MyBackgrounds "My Backgrounds" Title
387 PerlEval foreach $b (@b) \
388 { cmd("AddToMenu MyBackgrounds '$b' Exec fvwm-root $a/$b") }
389
390 # choose a random background to load on start-up
391 PerlEval cmd("AddToFunc \
392 InitFunction + I Exec exec fvwm-root $a/" . $b[int(random(@b))])
393
395 SendToModule just like any other fvwm commands expands several dollar
396 prefixed variables. This may clash with the dollars perl uses. You may
397 avoid this by prefixing SendToModule with a leading dash. The following
398 2 lines in each pair are equivalent:
399
400 SendToModule FvwmPerl eval $$d = "$[DISPLAY]"
401 -SendToModule FvwmPerl eval $d = "$ENV{DISPLAY}"
402
403 SendToModule FvwmPerl eval \
404 cmd("Echo desk=$d, display=$$d")
405 SendToModule FvwmPerl preprocess -c \
406 Echo desk=%("$d")%, display=%{$$d}%
407
408 Another solution to avoid escaping of special symbols like dollars and
409 backslashes is to create a perl file in ~/.fvwm and then load it:
410
411 SendToModule FvwmPerl load build-menus.fpl
412
413 If you need to preprocess one command starting with a dash, you should
414 precede it using "--".
415
416 # this prints the current desk, i.e. "0"
417 SendToModule FvwmPerl preprocess -c Echo "$%{$a = "c"; ++$a}%"
418 # this prints "$d"
419 SendToModule FvwmPerl preprocess -c -- -Echo "$%{"d"}%"
420 # this prints "$d" (SendToModule expands $$ to $)
421 SendToModule FvwmPerl preprocess -c -- -Echo "$$%{"d"}%"
422 # this prints "$$d"
423 -SendToModule FvwmPerl preprocess -c -- -Echo "$$%{"d"}%"
424
425 Again, it is suggested to put your command(s) into file and preprocess
426 the file instead.
427
429 FvwmPerl being written in perl and dealing with perl, follows the
430 famous perl motto: "There's more than one way to do it", so the choice
431 is yours.
432
433 Here are more pairs of equivalent lines:
434
435 Module FvwmPerl --load "my.fpl" --stay
436 Module FvwmPerl -e 'load("my.fpl")' -s
437
438 SendToModule FvwmPerl preprocess --quote '@' my.ppp
439 SendToModule FvwmPerl eval preprocess({quote => '@'}, "my.ppp");
440
441 Warning, you may affect the way FvwmPerl works by evaluating
442 appropriate perl code, this is considered a feature not a bug. But
443 please don't do this, write your own fvwm module in perl instead.
444
446 The fvwm(1) man page describes all available commands.
447
448 Basically, in your perl code you may use any function or class method
449 from the perl library installed with fvwm, see the man pages of perl
450 packages General::FileSystem, General::Parse and FVWM::Module.
451
453 Mikhael Goikhman <migo@homemail.com>.
454
455
456
4572.5.28 (from cvs) 2009-03-22 FvwmPerl(1)