[hackers] [sites] Add wmii/p9p code snippets. || Kris Maglione

From: <hg_AT_suckless.org>
Date: Sun, 16 Aug 2009 10:54:06 +0000 (UTC)

changeset: 96:aa2c7d3e5138
user: Kris Maglione <jg_AT_suckless.org>
date: Tue May 19 14:12:49 2009 -0400
files: wmii.suckless.org/code_snippets/plan_9_port/index.md wmii.suckless.org/code_snippets/plan_9_port/irssi_notifier.md wmii.suckless.org/code_snippets/plan_9_port/mail_notifier.md
description:
Add wmii/p9p code snippets.

diff -r 5885431f168e -r aa2c7d3e5138 wmii.suckless.org/code_snippets/plan_9_port/index.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/wmii.suckless.org/code_snippets/plan_9_port/index.md Tue May 19 14:12:49 2009 -0400
@@ -0,0 +1,119 @@
+Code snippets for wmii-snap
+===========================
+
+The [9P filesystem](http://en.wikipedia.org/wiki/9P) backend of wmii allows for
+rich scripting possibilites, adding new features and more convenience to your
+wmii. Because of the simple nature of the 9P protocol, scripting is possible in
+any language, using either native 9P libraries in that language or external
+tools like the supplied wmiir/[ixpc](http://libs.suckless.org/).
+
+Feel free to add your own scripts to this page!
+
+The snippets that are in `rc` syntax should be added to your `rc.wmii.local`
+file.
+
+See this sample `rc.wmii.local`: [(3.6)](http://sqweek.dnsdojo.org/wmii/rc.wmii.local-3.6) [(newer snapshots)](http://sqweek.dnsdojo.org/wmii/rc.wmii.local-20080520)
+
+Cycle Views
+-----------
+ fn next_tag {
+ awk -v curtag'='`{wmiir read /tag/sel/ctl | sed 1q} '
+ NR==1 {first = $0}
+ $0==curtag { if(getline) print $0; else print first; exit }'
+ }
+ fn Key-$MODKEY-n {
+ wmiir xwrite /ctl view `{ read_tags | next_tag}
+ }
+ fn Key-$MODKEY-b {
+ wmiir xwrite /ctl view `{ read_tags | tail -r | next_tag}
+ }
+
+Tag selected client and jump to new view
+----------------------------------------
+If tagged with multiple tags, it will jump to the last view of the set.
+
+ fn Key-MODKEY-Control-t {
+ newtag=`{wi_tags | wi_menu}
+ wmiir xwrite /client/sel/tags $newtag
+ newtag=`{echo $newtag | sed 's/.*\+//; s/-.*//'}
+ wmiir xwrite /ctl view $newtag
+ }
+
+Switch to named view by number
+-----------------------------
+
+This allows to switch to a view with its position number, regardless of the view's name.
+
+ fn keynum {
+ echo `{echo $1 | sed 's/.*-//'}
+ }
+ fn tagnum {
+ echo `{read_tags | sed -n $1^p}
+ }
+
+ for(i in `{seq 0 9}) {
+ fn Key-$MODKEY-$i {
+ wmiir xwrite /ctl view `{tagnum `{keynum $1}}
+ }
+ fn Key-Shift-$MODKEY-$i {
+ wmiir xwrite /client/sel/tags `{tagnum `{keynum $1}}
+ }
+ }
+
+Tag addition and subtraction using tag+ and tag-
+------------------------------------------------
+
+If you tend to rely on wimenu's completion for retagging, it can be annoying that + or - prefix
+renders it useless. This snippet allows you to append the + or -, rather than prepending
+it. Prepending still works as before.
+
+ fn Key-$MODKEY-Shift-t {
+ tag=`{wi_tags | wimenu | sed 's/(.*)([+\-])$/\2\1/'}
+ wmiir xwrite /client/`{wmiir read /client/sel/ctl | sed 1q}^/tags $tag
+ }
+
+Retag all clients in a view
+---------------------------
+ fn clients_on {
+ wmiir read /tag/$1/index | awk '!/^#/ { print $2 }'
+ }
+
+ fn Key-$MODKEY-r {
+ target=`{wi_tags | wimenu}
+ for(c in `{clients_on sel}){
+ wmiir xwrite /client/$c/tags -sel+$target
+ }
+ wmiir xwrite /ctl view $target
+ }
+
+Cycle trough tags starting with a specific character
+----------------------------------------------------
+
+(No tested, but might work)
+Add a event in wmiirc that calls the following code.
+If you have 3 tags named plan9, planner and plant: by calling the script you will cycle between the 3.
+(Note: it does not work with capitals)
+
+ count=1
+ fn TagCycle {
+ inputtag=$1
+ currenttag=`{wmiir read /tag/sel/ctl | awk '{ print substr($2, 1, 1); exit }'}
+ if(~ $currenttag $inputtag) {
+ count=`{expr $count + 1}
+ maxcount=`{wi_tags | grep '^'$inputtag | wc -l}
+ if (test $count -gt $maxcount)
+ count=1
+ }
+ if not
+ count=1
+ gototag=`{wi_tags | grep '^'$inputtag | sed -n $count^p}
+ wmiir xwrite /ctl view $gototag
+ }
+
+To easily add the entire alphabet in the bound buttons you can use this code.
+Add this to your rc.wmii BEFORE the other keys (you still want your other old keys to work right ?):
+
+ # Tag cycle
+ key $MODKEY-A-^(a b c d e f g h i j k l m n o p q r s t u v w x y z) || fn $key {
+ TagCycle $1}
+
diff -r 5885431f168e -r aa2c7d3e5138 wmii.suckless.org/code_snippets/plan_9_port/irssi_notifier.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/wmii.suckless.org/code_snippets/plan_9_port/irssi_notifier.md Tue May 19 14:12:49 2009 -0400
@@ -0,0 +1,103 @@
+Irssi Message Notifier
+======================
+
+This script notifies you, by setting the Urgent hint on your irssi window, of messages with your nick, or in channels you care about. It can optionally display the messages on the bar. You'll need to set the title of the window running irssi to 'irssi' for this to work. I use `label`(1) from plan9port.
+
+This portion goes in your rc.wmii.local, or can be modified to run standalone:
+
+ showmessage=false # true, false
+
+ fn isirssi { ~ `{wmiir read /client/$"*/label} irssi }
+ fn clients { wmiir ls /client | sed 's,/,,; /^sel$/d' }
+
+ fn Event-IRCMessage {
+ if(! isirssi sel) {
+ for(c in `{clients})
+ if(isirssi $c)
+ wmiir xwrite /client/$c/ctl Urgent
+ if($showmessage) {
+ shift 2
+ echo $WMII_NORMCOLORS $* | wmiir create /rbar/0status
+ }
+ }
+ }
+
+ fn Event-ClientFocus {
+ if(isirssi $1)
+ wmiir xwrite /client/$1/ctl NotUrgent
+ }
+
+This portion is a perl script for irssi. It should go in `~/.irssi/scripts`, and can be loaded with `/script load notify`, or autoloaded by symlinking it into `~/.irssi/scripts/autoload`. The irssi `notify_channels` setting takes a list of channels to always notify you of messages to, or '*' to notify you for all channels.
+
+ # ---------------------------------------------------------------------------
+ # "THE BEER-WARE LICENSE" (Revision 42):
+ # <fbsdaemon_AT_gmail.com> wrote this file. As long as you retain this notice you
+ # can do whatever you want with this stuff. If we meet some day, and you think
+ # this stuff is worth it, you can buy me a beer in return. Kris Maglione
+ # ---------------------------------------------------------------------------
+ # <phk_AT_FreeBSD.ORG> wrote this license. As long as you retain this notice you
+ # can do whatever you want with this stuff. If we meet some day, and you think
+ # this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
+ # ---------------------------------------------------------------------------
+
+ use strict;
+
+ our $VERSION = '20070524';
+ our %IRSSI = (
+ authors => 'Kris Maglione',
+ contact => 'bsdaemon_AT_comcast.net',
+ name => 'notify',
+ description => 'Writes certain messages to the wmii event FIFO',
+ license => 'BEER-WARE',
+ url => '',
+ changed => $VERSION,
+ modules => '',
+ );
+
+ use Irssi;
+ use Fcntl;
+
+ sub should_notify ($);
+
+ sub message_handler {
+ my ($server, $mesg, $nick, $address, $target) = @_;
+ my $mynick = $server->{nick};
+
+ if (not defined $target
+ or should_notify($target)
+ or $mesg =~ /\b$mynick\b/) {
+ $mesg =~ s/\n/\\n/g;
+ $target = ($target ? " in $target" : "");
+
+ open my $fifo, '| wmiir write /event' or return;
+ print $fifo "IRCMessage from $nick$target: $mesg\n";
+ close $fifo;
+ }
+ }
+
+ sub should_notify ($) {
+ my $target = shift;
+ my @notifies = split /,\s*/, Irssi::settings_get_str('notify_channels');
+
+ if (grep { $_ eq "*" || $_ eq $target } @notifies) {
+ return 1;
+ }
+
+ 0;
+ }
+
+ Irssi::signal_add('message private', 'message_handler');
+ Irssi::signal_add('message public', 'message_handler');
+ Irssi::settings_add_str('misc', 'notify_channels', '');
+
+There is another way to set the label:
+
+Use the perl script in at this website: `http://tldp.org/HOWTO/Xterm-Title-7.html#ss7.2`
+And put it into `~/.irssi/scripts/title.pl` for example.
+
+Content of ` ~/.irssi/scripts/autorun/title.pl`:
+
+ system('~/.irssi/scripts/title.pl irssi');
+
+This will nicely set the title for irssi.
+
diff -r 5885431f168e -r aa2c7d3e5138 wmii.suckless.org/code_snippets/plan_9_port/mail_notifier.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/wmii.suckless.org/code_snippets/plan_9_port/mail_notifier.md Tue May 19 14:12:49 2009 -0400
@@ -0,0 +1,44 @@
+Mail Notifier
+=============
+
+This script will notify you of mail by setting the urgent hint
+on windows for mail folders in which you have new mail. It only
+works for Maildirs, and assumes that you have a window on the
+`mail` tag named for each folder you wish to check. The name
+`inbox` is special, and is treated as the root folder. When
+there are files in the `new` folder for a given window, its
+urgent hint is set. Otherwise, it is unset.
+
+ #!/usr/local/plan9/bin/rc
+ . 9.rc
+
+ # Configuration
+ delay=5
+ maildir=$home/Maildir
+ # End Configuration
+
+ echo Start mail | wmiir write /event
+
+ { wmiir read /event &
+ while(echo Tick)
+ sleep $delay
+ } | while(*=`{read})
+ switch($1) {
+ case Start
+ if(~ $2 mail)
+ exit
+ case Tick
+ wmiir read /tag/mail/index |
+ while(l = `{read}) {
+ b = `{echo $l | awk -F: '{print $3}'}
+ if(~ $b inbox)
+ b = ''
+ if(! ~ $#b 0 && test -d $maildir/.$b/new) {
+ knot = ''
+ if(~ `{ls -l $maildir/.$b/new | wc -l} 0)
+ knot = Not
+ wmiir xwrite /client/$l(2)^/ctl $knot^Urgent
+ }
+ }
+ }
+
Received on Sun Aug 16 2009 - 10:54:06 UTC

This archive was generated by hypermail 2.2.0 : Sun Aug 16 2009 - 14:19:38 UTC