… | |
… | |
73 | } else { |
73 | } else { |
74 | return load "$HOME/sunday.png"; |
74 | return load "$HOME/sunday.png"; |
75 | } |
75 | } |
76 | } |
76 | } |
77 | |
77 | |
78 | This expression is evaluated once per hour. It will set F<sunday.png> as |
78 | This inner expression is evaluated once per hour (and whenever the |
|
|
79 | temrinal window is resized). It sets F<sunday.png> as background on |
79 | background on Sundays, and F<weekday.png> on all other days. |
80 | Sundays, and F<weekday.png> on all other days. |
80 | |
81 | |
81 | Fortunately, we expect that most expressions will be much simpler, with |
82 | Fortunately, we expect that most expressions will be much simpler, with |
82 | little Perl knowledge needed. |
83 | little Perl knowledge needed. |
83 | |
84 | |
84 | Basically, you always start with a function that "generates" an image |
85 | Basically, you always start with a function that "generates" an image |
… | |
… | |
117 | horizontal and vertical dimensions. For example, this halves the image |
118 | horizontal and vertical dimensions. For example, this halves the image |
118 | width and doubles the image height: |
119 | width and doubles the image height: |
119 | |
120 | |
120 | scale 0.5, 2, load "$HOME/mypic.png" |
121 | scale 0.5, 2, load "$HOME/mypic.png" |
121 | |
122 | |
122 | IF you try out these expressions, you might suffer from sluggishness, |
123 | IF you try out these expressions, you might suffer from some sluggishness, |
123 | because each time the terminal is resized, it again loads the PNG image |
124 | because each time the terminal is resized, it loads the PNG image agin |
124 | and scales it. Scaling is usually fast, but loading the image can be quite |
125 | and scales it. Scaling is usually fast (and unavoidable), but loading the |
125 | time consuming. This is where C<keep> comes in handy: |
126 | image can be quite time consuming. This is where C<keep> comes in handy: |
126 | |
127 | |
127 | scale 0.5, 2, keep { load "$HOME/mypic.png" } |
128 | scale 0.5, 2, keep { load "$HOME/mypic.png" } |
128 | |
129 | |
129 | The C<keep> operator executes all the statements inside the braces only |
130 | The C<keep> operator executes all the statements inside the braces only |
130 | once, or when it thinks the outcome might change. In other cases it |
131 | once, or when it thinks the outcome might change. In other cases it |
… | |
… | |
172 | ... }>. The C<keep> operator takes a block of multiple statements enclosed |
173 | ... }>. The C<keep> operator takes a block of multiple statements enclosed |
173 | by C<{}> and keeps the return value in memory. |
174 | by C<{}> and keeps the return value in memory. |
174 | |
175 | |
175 | An expression can be "sensitive" to various external events, such as |
176 | An expression can be "sensitive" to various external events, such as |
176 | scaling or moving the window, root background changes and timers. Simply |
177 | scaling or moving the window, root background changes and timers. Simply |
177 | using an expression (such as C<scale> without parameters) that depend on |
178 | using an expression (such as C<scale> without parameters) that depends on |
178 | certain changing values (called "variables"), or using those variables |
179 | certain changing values (called "variables"), or using those variables |
179 | directly, will make an expression sensitive to these events - for example, |
180 | directly, will make an expression sensitive to these events - for example, |
180 | using C<scale> or C<TW> will make the expression sensitive to the terminal |
181 | using C<scale> or C<TW> will make the expression sensitive to the terminal |
181 | size, and thus to resizing events. |
182 | size, and thus to resizing events. |
182 | |
183 | |
183 | When such an event happens, C<keep> will automatically trigger a |
184 | When such an event happens, C<keep> will automatically trigger a |
184 | reevaluation of the whole expression with the new value of the expression. |
185 | reevaluation of the whole expression with the new value of the expression. |
185 | |
186 | |
186 | C<keep> is most useful for expensive operations, such as C<blur>: |
187 | C<keep> is most useful for expensive operations, such as C<blur>: |
187 | |
188 | |
188 | rootalign once { blur 20, root } |
189 | rootalign keep { blur 20, root } |
189 | |
190 | |
190 | This makes a blurred copy of the root background once, and on subsequent |
191 | This makes a blurred copy of the root background once, and on subsequent |
191 | calls, just root-aligns it. Since C<blur> is usually quite slow and |
192 | calls, just root-aligns it. Since C<blur> is usually quite slow and |
192 | C<rootalign> is quite fast, this trades extra memory (for the cached |
193 | C<rootalign> is quite fast, this trades extra memory (for the cached |
193 | blurred pixmap) with speed (blur only needs to be redone when root |
194 | blurred pixmap) with speed (blur only needs to be redone when root |
… | |
… | |
845 | Anything that didn't fit any of the other categories, even after applying |
846 | Anything that didn't fit any of the other categories, even after applying |
846 | force and closing our eyes. |
847 | force and closing our eyes. |
847 | |
848 | |
848 | =over 4 |
849 | =over 4 |
849 | |
850 | |
850 | =item once { ... } |
851 | =item keep { ... } |
851 | |
852 | |
852 | This function takes a code block as argument, that is, one or more |
853 | This operator takes a code block as argument, that is, one or more |
853 | statements enclosed by braces. |
854 | statements enclosed by braces. |
854 | |
855 | |
855 | The trick is that this code block is only evaluated once - future calls |
856 | The trick is that this code block is only evaluated when the outcome |
856 | will simply return the original image (yes, it should only be used with |
857 | changes - on other calls the C<keep> simply returns the image it computed |
857 | images). |
858 | previously (yes, it should only be used with images). Or in other words, |
|
|
859 | C<keep> I<caches> the result of the code block so it doesn't need to be |
|
|
860 | computed again. |
858 | |
861 | |
859 | This can be extremely useful to avoid redoing the same slow operations |
862 | This can be extremely useful to avoid redoing slow operations - for |
860 | again and again- for example, if your background expression takes the root |
863 | example, if your background expression takes the root background, blurs it |
861 | background, blurs it and then root-aligns it it would have to blur the |
864 | and then root-aligns it it would have to blur the root background on every |
862 | root background on every window move or resize. |
865 | window move or resize. |
|
|
866 | |
|
|
867 | Another example is C<load>, which can be quite slow. |
863 | |
868 | |
864 | In fact, urxvt itself encloses the whole expression in some kind of |
869 | In fact, urxvt itself encloses the whole expression in some kind of |
865 | C<once> block so it only is reevaluated as required. |
870 | C<keep> block so it only is reevaluated as required. |
866 | |
871 | |
867 | Putting the blur into a C<once> block will make sure the blur is only done |
872 | Putting the blur into a C<keep> block will make sure the blur is only done |
868 | once: |
873 | once, while the C<rootalign> is still done each time the window moves. |
869 | |
874 | |
870 | rootlign once { blur 10, root } |
875 | rootlign keep { blur 10, root } |
871 | |
876 | |
872 | This leaves the question of how to force reevaluation of the block, |
877 | This leaves the question of how to force reevaluation of the block, |
873 | in case the root background changes: If expression inside the block |
878 | in case the root background changes: If expression inside the block |
874 | is sensitive to some event (root background changes, window geometry |
879 | is sensitive to some event (root background changes, window geometry |
875 | changes), then it will be reevaluated automatically as needed. |
880 | changes), then it will be reevaluated automatically as needed. |
876 | |
881 | |
877 | =item once_again |
|
|
878 | |
|
|
879 | Resets all C<once> block as if they had never been called, i.e. on the |
|
|
880 | next call they will be reevaluated again. |
|
|
881 | |
|
|
882 | =cut |
882 | =cut |
883 | |
883 | |
884 | sub once(&) { |
884 | sub keep(&) { |
885 | my $id = $_[0]+0; |
885 | my $id = $_[0]+0; |
886 | |
886 | |
887 | local $frame = $self->{frame_cache}{$id} ||= [$frame]; |
887 | local $frame = $self->{frame_cache}{$id} ||= [$frame]; |
888 | |
888 | |
889 | unless ($frame->[FR_CACHE]) { |
889 | unless ($frame->[FR_CACHE]) { |
… | |
… | |
907 | wantarray |
907 | wantarray |
908 | ? @{ $frame->[FR_CACHE] } |
908 | ? @{ $frame->[FR_CACHE] } |
909 | : $frame->[FR_CACHE][0] |
909 | : $frame->[FR_CACHE][0] |
910 | } |
910 | } |
911 | |
911 | |
912 | sub once_again() { |
912 | # sub keep_clear() { |
913 | delete $self->{frame_cache}; |
913 | # delete $self->{frame_cache}; |
914 | } |
914 | # } |
915 | |
915 | |
916 | =back |
916 | =back |
917 | |
917 | |
918 | =cut |
918 | =cut |
919 | |
919 | |