On Mon, Oct 31, 2011 at 12:33:00AM +0000, Harry portobello wrote:
> hullo,
> 
> 2011/10/30 Thomas Adam <tho...@fvwm.org>:
> > On Sun, Oct 30, 2011 at 08:09:52PM +0000, Harry portobello wrote:
> >> How do i do this with FvwmEvent? Thanks..
> >
> > Something like:
> >
> >    DestroyFunc CountWindow
> >    AddToFunc   CountWindow
> >    + I SetEnv WindowCount 0
> >    + I All ($[w.class], !Transient) PipeRead \
> >        `echo SetEnv WindowCount $$$$(($$$$WindowCount + 1))`
> >    + I PipeRead `[ $WindowCount = 1 ] && echo \
> >        "WindowStyle TitleFormat %n" || echo "Nop"`
> >    + I UnsetEnv WindowCount
> >
> >    DestroyModuleConfig FE-tweakttformat
> >    *FE-tweakttformat: add_window CountWindow
> >
> >    AddToFunc StartFunction I Module FvwmEvent FE-tweakttformat
> >
> > Change to suit your needs.  I have not tested this myself.
> 
> this works but is complex. can you tell me how it works? would be nice
> still for such things to be part of %t for titleformat

It works like this; although I should point out that what I am about to
detail to you has little to do with FVWM and more generally about basic
shell constructs.

        DestroyFunc CountWindow
        AddToFunc   CountWindow

Destroys and recreates a function named CountWindow.

    + I SetEnv WindowCount 0

Sets up an environment variable called WindowCount which we initially set to
zero [1].

    + I All ($[w.class], !Transient) PipeRead \
        `echo SetEnv WindowCount $$$$(($$$$WindowCount + 1))`

This does all of the work for us.  Note that here, $[w.class] expands to the
window's class, which we're using as a reference point to count all windows
of this type.  There's two things going on here which you should be aware
of:

* We don't pass in the window name.  This is because when CountWindow is
  called via FvwmEvent, it's already running in the window context of the
  window which had the action.  Since we're using add_window, each window
  that is mapped is referenced without us needing to track it manually.

* We're using the window ring to go through all windows, looking for ones
  match the class name, as well as those which are not transient.  It is not
  enough in this context to initially use the window's WM_NAME or resource,
  as they can change, and you wanted an accurate count, so...

Then we call PipeRead to put us in a shell -- perform some expression with
it, and then tell FVWM to do something.  This will be called once per match
from the All command, and all we're doing here is incrementing the value
stored in $[WindowCount] each time.

    + I PipeRead `[ $WindowCount = 1 ] && echo \
        "WindowStyle TitleFormat %n" || echo "Nop"`

Having collected all of that, we then check to see how many instances of
that window exist.  If there's only one instance of such a window we've just
mapped, then we set its TitleFormat to just %n; otherwise we ask FVWM to do
nothing (Nop).  By using a Nop, we ensure that for all other cases, the
TitleFormat used via usual Style conditions are met instead.  This means
any window with a count here > 1, will be counted by %t in the usual way.

    + I UnsetEnv WindowCount

Since we're now done with what we need, clear out the WindowCount
environment variable.

    DestroyModuleConfig FE-tweakttformat
    *FE-tweakttformat: add_window CountWindow

This destroys and creates a new module alias called FE-tweakttformat, which
we'll be asking FvwmEvent to look after.  In this instance, we're only
interested in newly-created windows, hence we've defined the add_window
event, which will call out CountWindow function whenever new windows are
mapped.

    AddToFunc StartFunction I Module FvwmEvent FE-tweakttformat

Finally, we'll ask FVWM to start FvwmEvent with this module alias when it
starts up at init, and again should we restart FVWM.

HTH,

-- Thomas Adam

[1]  Using environment variables in this way is useful; the persistency of
this value is cleared at the end of the function -- a very *good* use of
SetEnv here.   But people forget to clear out the environment variable
afterwards, cluttering up the environment with stale information.

Reply via email to