<?xml version="1.0" encoding="utf-8"?>
<!-- generator="Kukkaisvoima version 9" -->
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<channel>
<atom:link href="http://www.nickcoleman.org/blog/index.cgi/feed" rel="self" />
<title>Nick Coleman: software</title>
<link>http://www.nickcoleman.org/blog/index.cgi</link>
<description>Nick Coleman blog</description>
<pubDate>Sat, 25 Feb 2012 09:00:00 -0700</pubDate>
<lastBuildDate>Sat, 25 Feb 2012 09:00:00 -0700</lastBuildDate>
<generator>http://23.fi/kukkaisvoima/</generator>
<language>en</language>
<item>
<title>How To Set Up A Light-Weight On-line Thesaurus For Vim Pt.II
</title>
<link>http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus-syntax%21201202250900%21blogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix</link>
<comments>http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus-syntax%21201202250900%21blogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix#comments</comments>
<pubDate>Sat, 25 Feb 2012 09:00:00 -0700</pubDate>
<dc:creator>Nick</dc:creator>
<category>blogging</category>
<category>internet</category>
<category>programming</category>
<category>software</category>
<category>unix</category>
<guid isPermaLink="true">http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus-syntax%21201202250900%21blogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix/</guid>
<description><![CDATA[ 
 [...]]]></description>
<content:encoded><![CDATA[
<p>
<div class="summary">Vim has support for a built-in thesaurus. However, it consumes
memory and its auto-complete selection has issues. In Part I, I showed how to set up an
on-line thesaurus.   Here is how to build syntax rules that will colour the
output.</div>
<p>
<h2>Summary</h2>
<p>
This is the second post of two about a light weight way to implement a thesaurus.  In
<a
href="http://www.nickcoleman.org/axs/ax.pl?http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus%21201202170802%21general%2Cblogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix">
Part I</a>, I described how to set up a script that provides access to an on-line thesaurus.
In this Part, I describe how to write a set of simple syntax rules to provide colour and
highlighting for the output.
<p>
<a name="screenshot"></a>
Here is a screenshot of the finished syntax rules (using dummy data):
<div class="image">
<img src="http://www.nickcoleman.org/blog/images/thes-syntax.jpg" title="Syntax highlighting example" alt="syntax
highlighting screenshot" >
</div>
<p>
If you don't like these colours, you can change the rules to use whatever colours you prefer.
<p>
<hr>
<h2>How Vim does it</h2>
<p>
Vim does syntax highlighting in two parts:
<ul><li>A set of rules that define a (group of) word, and</li>
<li>A colour description for each rule.</li></ul>
<p>
<h4>Set of rules</h4>
The set of rules is in a file named for the filetype in either
<code>$VIMRUNTIME/syntax</code> or your <code>~/.vim/syntax</code>.  Each rule defines a
name for that rule and a list or a regular expression to match words that should be
captured by that rule.
<p>
An example is 
<div class="code">
syntax keyword     cStatement	goto break return continue asm
syntax keyword     cLabel	case default
syntax match	   cTodo        /\/* *TODO/
syntax region	   cBlock       start="{" end="}" 
</div>
The first and second lines are the simplest case of <strong>keyword</strong>, which is a
list of words.  The third line is the next simplest case of a regex <strong>pattern
match</strong>, in this case matching "TODO" within a C-style comment leading with "/*".
The fourth line is like a broader <strong>match</strong> in that it specifies a
<strong>region</strong> defined by a start pattern and an end pattern.
<p>
Notice each rule has a name that is a unique identifier: <strong>cStatement</strong>,
<strong>cLabel</strong>, <strong>cTodo</strong> and <strong>cBlock</strong>.
<p>
In fact, it can be much more complicated than that.  Rules can be contained within rules
(for example, nested <code>if-then</code>s), rules can apply only if another rule has
triggered (a '(' as part of a condition, but not as part of a comment), rules can apply
to entire regions, and so on.
<p>
However, for our simple purpose which is to provide slight colour hints to a plain text
list, we can ignore all that, to which we say, "Thank goodness."  Check out sh.vim or
even simple old c.vim  to see why.
<p>
<h4>Colour description</h4>
The colour description is usually in the <code>colorscheme</code> you are using.  For
each rule name, it provides a colour scheme to use, along the lines of 
<p>
<div class="code">
hi Comment term=bold ctermfg=8 guifg=#7C7C7C
</div>
<p>
which translates as: for rule name "Comment", for a simple terminal set to bold, for a colour
terminal set the foreground to colour number 8, for the GUI version of Vim set the
foreground to colour number #7C7C7C.
<p>
You can think of the process as going like this: when Vim displays a file, each
token or word is checked against the set of rules and assigned a rule name, and is then
colourised according to the highlighting for that rule name.
<p>
You can see your highlighting description with <code>:hi Comment</code>, which shows the
current highlighting for rule "Comment" for the filetype in the current buffer.  To see all
highlighting, use <code>:hi</code> by itself.
<p>
From this you can see that the rules and the highlighting scheme are tightly coupled.
Each rule has a name, and that name should be in the colorscheme<sup><small><a href="#fn1" id="back1">1</a></small></sup> .  Vim controls the
tight coupling by providing a set of standard names which, if we were writing rules for
a new programming language, we should use.  Those names are things like "Comment",
"Keyword", "Statement", and so on.
<p>
However, we are creating our own set of rules for this specific instance of text, so we
can do what we like.  Which we shall.
<p>
<h2>Syntax rules</h2>
<p>
<h4>The data</h4>
Here is the (made up) sample text we will be working with:
<p>
<div class="code">
   Main Entry:     which pronunciation [hwich, wich] [IMG] Show IPA/<h>wItS,
   Definition:     what
   Synonyms:       and that, that, whatever, whichever
		   in order that, in that, so, so that
   Notes:          in current usage, that refers to persons or things and
                   which is used chiefly for things. The standard rule says
                   that one uses that only to introduce a restrictive or
   Antonyms:       none
<p>
   Main Entry:     this
   Definition:     the one
   Synonyms:       that, the aforementioned one, the one in question, the
                   thing indicated, this one, this person
<p>
</div>
<p>
Typically, you get a set of entries comprising Main Entry, Definition, Synonyms, Notes
and Antonyms.  Not every entry is present, often Antonyms and Notes are absent.
<p>
As well, if the word is common, you often get repeating sets of entries for very similar
words, as in the example where the entry for <b>this</b> is also shown.  (This isn't
real data, by the way; <b>this</b> doesn't appear with <b>which</b> normally.)
<p>
<h4>Set up some rules</h4>
How to define some rules?  Some things come to mind immediately.
<ul><li>Entry names distinguished (highlighted differently) from entry contents, i.e.
"Main Entry:" should be different from "which".</li>
<li>The main entry word should be bolded or similar to make it stand out.</li>
<li>The main word melds too easily into "pronunciation..", which makes it hard to pick out.
Diminish everything to the right of the main word.</li>
<li>Each entry should be distinguished so you can scan them easily.  For example, "Main
Entry" should stand out, "Definition" less so, "Synonyms" more so.</li></ul>
<p>
Here is an example rule set that roughly implements the first, second and fourth items.
<p>
<div class="pygmentize"><pre><span class="c">&quot; Include the colon as part of the word</span><br><span class="k">setlocal</span> <span class="nb">iskeyword</span><span class="p">+=</span>:<br><br><span class="c">&quot; Rules</span><br><span class="c">&quot; a keyword</span><br><span class="nb">syntax</span> keyword  thesSynonyms Synonyms:<br><span class="c">&quot; this entry name has a space, so needs a regex </span><br><span class="nb">syntax</span> <span class="k">match</span>  thesMainEntry <span class="sr">/Main Entry: */</span> <br><span class="c">&quot; this entry should include the line to the end</span><br><span class="nb">syntax</span> region thesDefinition <span class="k">start</span><span class="p">=</span><span class="sr">/Definition: /</span>  <span class="k">end</span><span class="p">=</span><span class="sr">/$/</span><br><br><span class="c">&quot; Highlighting</span><br><span class="c">&quot; link the highlighting to the defined </span><br><span class="c">&quot; name &quot;Keyword&quot; in the colorscheme</span><br><span class="nb">hi</span> link thesMainEntry Keyword<br><span class="nb">hi</span> link thesSynonyms  Statement<br><span class="nb">hi</span> link thesDefinition  Todo<br><span class="c">&quot; specify the colours directly</span><br><span class="nb">hi</span>  thesMainEntry <span class="nb">term</span><span class="p">=</span><span class="nb">bold</span> <br> \ctermfg<span class="p">=</span>White cterm<span class="p">=</span><span class="nb">bold</span> guifg<span class="p">=</span><span class="m">6</span> gui<span class="p">=</span><span class="nb">bold</span><br><span class="c">&quot; or, keep the current colour, but bold it</span><br><span class="nb">hi</span>  thesMainEntry <span class="nb">term</span><span class="p">=</span><span class="nb">bold</span> cterm<span class="p">=</span><span class="nb">bold</span> gui<span class="p">=</span><span class="nb">bold</span><br></pre></div>
<p>
which gives this:
<div class="image">
<img src="http://www.nickcoleman.org/blog/images/thes-syntax-example.jpg" title="Syntax example rule set"
alt="syntax example" >
</div>
<p>
You can see that the keyword "Synonyms:" has matched a rule set, and the highlighting
for the rule "Statement" in my colorscheme (likely different to your colorscheme) has
been applied.
<p>
The regex pattern for "Main Entry: " has matched a rule set and the highlighting for the
rule set has been applied.
<p>
Similarly for "Definition", the rule set has matched to the end of the line, and the
highlighting for the rule "Todo" in my colorscheme  has been applied.
<p>
<h2>The real deal</h2>
<p>
<h4>Rules</h4>
Enough examples. Here is the actual syntax file to produce the highlighting in the <a
href="#screenshot">screenshot at the top of the page.</a>
<p>
<div class="pygmentize"><pre><span class="c">&quot; Vi syntax file</span><br><span class="c">&quot; Language: text dump from online thesaurus</span><br><span class="c">&quot; Maintainer: Nick Coleman</span><br><span class="c">&quot; Last Change:  2012 Feb 18</span><br><span class="c">&quot; Remark: for the online thesaurus script by Nick Coleman</span><br>  <br><span class="k">if</span> exists<span class="p">(</span><span class="s2">&quot;b:current_syntax&quot;</span><span class="p">)</span><br>    <span class="k">finish</span><br><span class="k">endif</span><br><br><span class="c">&quot; Setup</span><br><span class="c">&quot; syntax clear    &quot; only useful for testing</span><br><span class="nb">syntax</span> case <span class="k">match</span><br><span class="k">setlocal</span> <span class="nb">iskeyword</span><span class="p">+=</span>:<br><br><span class="c">&quot; Entry name rules</span><br><span class="nb">syntax</span> <span class="k">match</span> thesMainEntry <span class="sr">/Main Entry: */</span> contained<br><span class="nb">syntax</span> region  thesDefinition <span class="k">start</span><span class="p">=</span><span class="sr">/Definition: /</span>  <span class="k">end</span><span class="p">=</span><span class="sr">/$/</span><br><span class="nb">syntax</span> keyword thesNotes Notes: contained<br><span class="nb">syntax</span> keyword thesSynonyms Synonyms:<br><span class="nb">syntax</span> region  thesAntonyms <span class="k">start</span><span class="p">=</span><span class="sr">/Antonyms:/</span>  <span class="k">end</span><span class="p">=</span><span class="sr">/$/</span> <br><br><span class="c">&quot; give the pronunciation region a special name</span><br><span class="nb">syntax</span> region thesPronunciation <span class="k">start</span><span class="p">=</span><span class="sr">/pronunciation \[/</span> <span class="k">end</span><span class="p">=</span><span class="sr">/$/</span> contained<br><br><span class="c">&quot; Entry contents rules</span><br><span class="nb">syntax</span> region thesMainWord <span class="k">start</span><span class="p">=</span><span class="sr">/Main Entry:/</span>  <span class="k">end</span><span class="p">=</span><span class="sr">/$/</span> contains<span class="p">=</span>CONTAINED keepend<br><span class="nb">syntax</span> region thesNotesEntry <span class="k">start</span><span class="p">=</span><span class="sr">/Notes:/</span>  <span class="k">end</span><span class="p">=</span><span class="sr">/^ *$/</span> contains<span class="p">=</span>thesNotes<span class="p">,</span>thesAntonyms keepend <br><br><span class="c">&quot; Highlighting</span><br><br><span class="nb">hi</span> link thesMainEntry     Keyword<br><span class="nb">hi</span>  thesMainWord      <span class="nb">term</span><span class="p">=</span><span class="nb">bold</span> cterm<span class="p">=</span><span class="nb">bold</span> gui<span class="p">=</span><span class="nb">bold</span><br><span class="nb">hi</span> link thesDefinition      String<br><span class="nb">hi</span> link thesNotes     Number<br><span class="nb">hi</span> link thesNotesEntry      Number<br><span class="nb">hi</span> link thesSynonyms      Statement<br><span class="nb">hi</span> link thesAntonyms      Todo<br><span class="nb">hi</span> link thesPronunciation   Comment<br><br><span class="k">let</span> <span class="k">b</span>:current_syntax <span class="p">=</span> <span class="c">&quot;thesaurus&quot;</span><br></pre></div>
<p>
Some rules have "contains".  This allows a rule within a rule, the classic example being
Todo appearing within a comment where you want a different colour to make Todo stand
out.  "keepend" is part of that, it stops both rules at the first end pattern match rather than
the final end match.  <code>:h usr_44</code> section 44.5 for more.
<p>
<h4>Setup</h4>
<p>
Recall from Part I that the script sets the thesaurus' buffer to filetype thesaurus.
Put the above syntax file in <code>$HOME/.vim/syntax/thesaurus.vim</code> and the buffer will pick up the
syntax rules automatically. 
<p>
Windows users can put it in <code>C:\Program Files\Vim\vimfiles\syntax\thesaurus.vim</code> or the
equivalent if using Vista or Windows 7.  If you don't have administrator privileges,
find where Vim thinks $HOME is by (within Vim) trying <code>:echo $HOME</code> or <code>:version</code> and
putting it in $HOME\vimfiles\syntax\thesaurus.vim.
<p>
<h2>Trying it out</h2>
<p>
You probably want to use your own highlighting.  A tip: to easily see the
effect of your changes reload the highlighting for the data buffer with <code>:setlocal filetype=thesaurus</code>.
<p>
To see the colours that your colorscheme uses for a particular rule use <code>:hi
&lt;rule&gt;</code> as in <code>:hi Statement</code>.  To see all colours, use hi by itself <code>:hi</code>.
<p>
<div class="footnote">
<a href="#back1" id="fn1">[1]</a> I said the rule should be in the colorscheme.  In
fact, it is not an error if there is no colour highlighting for that rule, it simply
gets ignored. And you can put highlighting descriptions anywhere, such as your
<code>.vimrc</code>. For example, I quite like the colorscheme I use, except for the Search colours
which I override with a separate description in my <code>.vimrc</code> like this:
<pre><code>
hi Search ctermfg=white ctermbg=darkblue
</code></pre> .<a href="#back1"> &uarr; </a>
</div>
]]></content:encoded>
<wfw:commentRss>http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus-syntax%21201202250900%21blogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix/feed/</wfw:commentRss>
</item>
<item>
<title>How To Set Up A Light-Weight On-line Thesaurus For Vim
</title>
<link>http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus%21201202170802%21general%2Cblogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix</link>
<comments>http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus%21201202170802%21general%2Cblogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix#comments</comments>
<pubDate>Fri, 17 Feb 2012 08:02:00 -0700</pubDate>
<dc:creator>Nick</dc:creator>
<category>general</category>
<category>blogging</category>
<category>internet</category>
<category>programming</category>
<category>software</category>
<category>unix</category>
<guid isPermaLink="true">http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus%21201202170802%21general%2Cblogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix/</guid>
<description><![CDATA[ 
 [...]]]></description>
<content:encoded><![CDATA[
<p>
<div class="summary">Vim has support for a built-in thesaurus. However, it consumes a
lot of memory, which you may not want for a feature you do not use much, and its
auto-complete selection has issues.  Here is how to set up an on-line thesaurus query that is light weight.</div>
<p>
<h2>Summary</h2>
<p>
This is the first post of two (<a
href="http://www.nickcoleman.org/axs/ax.pl?http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus-syntax%21201202250900%21blogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix">second
here</a>) about a light weight way to implement a thesaurus.  It is
great for what I need, which is the occasional use of a thesaurus for writing text such
as this article.  Once it is set up, you can forget about it and just use <b>K</b>
whenever you want to look up a word.
<p>
A nice bonus or synergy of using an online source is that the website also returns a definition for the word, so
it functions as a simple dictionary as well.
<p>
The second post (<a
href="http://www.nickcoleman.org/axs/ax.pl?http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus-syntax%21201202250900%21blogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix">here</a>) will deal with how to use Vim's built in syntax rule sets to provide
highlighting and nice colours.
<p>
There are two or three simple steps:
<ol><li>a vim script that passes the cursor word to an external shell script.</li>
<li>a shell script that looks up the word using an online thesaurus, then parses
the output to remove unnecessary cruft.</li>
<li>an optional syntax file to provide highlighting and colours.</li></ol>
<p>
Here is what it looks like with a quick-n-dirty syntax rule set:
<div class="image">
<img src="http://www.nickcoleman.org/blog/images/thesaurus.jpg" title="Vim thesaurus"
alt="vim with thesaurus window" >
<p>Sample output for "quick"
</div>
<p>
<hr>
<p>
I use Vim for programming and it is great for that.  I use it when I need to edit files on remote
servers that I have ssh'ed in.  I also use Vim for writing text because it is a great
text editor.  I am writing this in Vim, for example. 
<p>
Often, one of the things you want when writing text is a thesaurus, which is the topic
for today.
<p>
<h2>Why on-line</h2>
Vim comes with support for a thesaurus, but I've never really liked it, for two reasons.  
<p>
You download a thesaurus (the Moby one is common), point Vim to it and you are good to
go. However, a thesaurus can consume a lot of memory.  The Moby file is 24 MB, although to
be fair Vim doesn't use anything like that.  If you are using Vim remotely, you may not
want to use <em>any</em> extra memory. For example, I have a remote VPS that has only
128 MB of total memory, so every megabyte is critical.  This is especially true if you
use the thesaurus only rarely; the trade-off is not worth it.
<p>
The bigger issue for me is that Vim does not handle a long list of alternative words very well.
It is fine with a short list where you use it just like auto-complete, but it seems to
get confused with a long list.  Unfortunately, Moby often serves up a long list, and you
frequently find you have inadvertently changed your perfectly good word to something
completely different.  You then have to Undo and try again.
<p>
In fact, I got so frustrated with the Vim/Moby combination I ended up unsetting the
thesaurus feature in Vim after just a couple of months.
<p>
The good news is that there is an alternative, and that is to use one of the many
on-line thesauruses. 
<p>
I have set things up so that a script calls a command-line browser to query for a word
and dump the output from the online website, then parses it to remove extra cruft such
as sidebars, headers and footers, and white space, and puts it into a scratch buffer in
Vim itself for me to browse through and perhaps copy a word from.
<p>
<h2>Vim setup</h2>
First, set up Vim to call a script, which I will describe further down, and display the
results in a scratch buffer.  (I shamelessly pulled the concept straight from the ReadMan
script, which displays a Unix man page for the keyword over the cursor.) To use it, simply
press <b>K</b> with the cursor under the word that you want to see alternatives.  You
can do all the normal things in the buffer like search, jump to a line, copy a word
(<b>cw</b>), and so on. To close it, just hit the <b>q</b> key.  
<p>
Copy and paste the following into your <code>.vimrc</code> or wherever you prefer.  I
have it in a common.vim file in my ftplugin directory, where my various filetype scripts
can source it if they want.
<p>
<div class="pygmentize"><pre><span class="k">fun</span><span class="p">!</span> ReadThesaurus<span class="p">()</span><br><span class="c">   &quot; Assign current word under cursor to a script variable</span><br>   <span class="k">let</span> s:thes_word <span class="p">=</span> expand<span class="p">(</span><span class="s1">&#39;&lt;cword&gt;&#39;</span><span class="p">)</span><br><span class="c">   &quot; Open a new window, keep the alternate so this doesn&#39;t clobber it. </span><br>   <span class="k">keepalt split </span>thes_<br><span class="c">   &quot; Show cursor word in status line</span><br>   <span class="k">exe</span> &quot;<span class="k">setlocal </span><span class="nb">statusline</span><span class="p">=</span>&quot; . <span class="s1">s:thes_word</span><br><span class="c">   &quot; Set buffer options for scratch buffer</span><br>   <span class="k">setlocal</span> <span class="nb">noswapfile</span> <span class="nb">nobuflisted</span> <span class="nb">nowrap</span> <span class="nb">nospell</span> <br>     \<span class="nb">buftype</span><span class="p">=</span>nofile <span class="nb">bufhidden</span><span class="p">=</span>hide <br><span class="c">   &quot; Delete existing content</span><br>   <span class="m">1</span><span class="p">,</span>$<span class="k">d</span><br><span class="c">   &quot; Run the thesaurus script</span><br>   <span class="k">exe </span><span class="s2">&quot;:0r !/home/nickcoleman/bin/thesaurus &quot;</span> . s:thes_word <br><span class="c">   &quot; Goto first line</span><br>   <span class="m">1</span><br><span class="c">   &quot; Set file type to &#39;thesaurus&#39;</span><br>   <span class="k">set</span> <span class="k">filetype</span><span class="p">=</span><span class="nb">thesaurus</span><br><span class="c">   &quot; Map q to quit without confirm</span><br>   <span class="k">nmap </span><span class="p">&lt;</span><span class="nb">buffer</span><span class="p">&gt;</span> <span class="k">q</span> :<span class="k">q</span><span class="p">&lt;</span>CR<span class="p">&gt;</span><br><span class="k">endfun</span><br><span class="c">&quot; Map the K key to the ReadThesaurus function</span><br><span class="nb">noremap</span> <span class="p">&lt;</span><span class="nb">buffer</span><span class="p">&gt;</span> K :<span class="k">call</span> ReadThesaurus<span class="p">()&lt;</span>CR<span class="p">&gt;&lt;</span>CR<span class="p">&gt;</span><br></pre></div>
<p>
The script is fully commented for you to follow what is going on.
<p>
Notice I specified the location of the shell script that Vim is calling to be
<code>$HOME/bin/thesaurus</code>.  Change this to wherever you are going to put the
shell script.
<p>
I call the scratch buffer "thes_" so that I have a name for it which means I can
easily re-use the buffer again.  Otherwise Vim would create a new buffer every time and
go through buffer numbers like crazy.  In the unlikely event you have an actual file
called "thes_", change the script to use some other wacky name.  I could have used the
Vim function <code>tempname()</code> to generate a unique buffer name, but "thes_" is
meaningful and easy to remember if you want to unhide the buffer later.
<p>
I set the status line to show the word I am looking up.  Sometimes the website will
return a different word, a near synonym, so the status line reminds me exactly which
word I am looking up. An example is looking up the word "the" which returns the
definition and synonyms for "histrionical".  No, I don't know why.
<p>
I include a line to set the filetype to "thesaurus". Its purpose is to allow me to set
up anything special for that filetype later on.  A possible use would be to create a
syntax file to do special highlighting or colouring.  That will be the topic for the
next post.
<p>
The mapping for K has a second <code>&lt;CR&gt;</code>.  It eats up the "Press ENTER or type
command to continue" prompt.  There are other ways to do this, but they can mess up the
display or have the side effect of applying globally instead of just this buffer.
<p>
By the way, in case it isn't obvious, the reason I split the functionality in to
two&mdash;part- Vim and part-shell scripts&mdash;instead of doing it all in Vim is
because Vim's scripting language, always an awkward beast at the best of times, would
make it too hard.  Best to use the good parts of each and combine them.
<p>
<h2>Thesaurus script</h2> Up to now, everything has been operating system agnostic.  The
script below is written for unix-like operating systems, including OS X, because it uses
some unix utilities like links and sed.  (The others it uses such as basename and
readlink aren't absolutely necessary, just good style.) I have written a <a
href="#windows">paragraph or two below </a> on how to get those tools for Windows.
<p>
Originally I wrote a quick-n-dirty one-liner, but I decided the script might be useful
outside Vim as well, so I tidied it up to be a useful script that you can call from the
command line.
<p>
This is the shell script that Vim calls.  Put it in the location you specified in the
Vim script above.  It is straightforward, apart from the <code>sed</code> call, and there
are enough comments so you can see what is happening.
<p>
<div class="pygmentize"><pre><span class="c">#!/bin/sh</span><br><br><span class="c"># This searchs an online thesaurus, cuts as much cruft out as possible,</span><br><span class="c"># and displays the definitions.</span><br><br><span class="c">#URL=&#39;http://www.merriam-webster.com/thesaurus/&#39;</span><br><span class="nv">URL</span><span class="o">=</span><span class="s1">&#39;http://thesaurus.com/browse/&#39;</span><br><br><span class="c"># Display help, and quit.</span><br><span class="k">function </span>usage <span class="o">{</span><br>    cat <span class="s">&lt;&lt; EOF_HELP</span><br><span class="s">    Usage: $(basename $0) [-r][-h] word</span><br><span class="s">    Display the parsed results of an online thesaurus search for &lt;word&gt;</span><br><br><span class="s">    -r  Raw results; no filtering.</span><br><span class="s">    -h  Display this help.</span><br><br><span class="s">EOF_HELP</span><br>    <span class="nb">exit</span> <br><span class="o">}</span><br><br><span class="c"># Check for a parameter.  Get the output from the website</span><br><span class="k">function </span>get_thes <span class="o">{</span><br>    <span class="o">[</span>  -z <span class="s2">&quot;$1&quot;</span> <span class="o">]</span> <span class="o">&amp;&amp;</span> usage<br>    <span class="k">$(</span>readlink -e <span class="k">$(</span>which links<span class="k">))</span> -dump <span class="s2">&quot;${URL}$1&quot;</span> <br><span class="o">}</span><br><br><span class="c"># Check for a -h parameter or absence of parameter.</span><br><span class="o">[</span> <span class="s2">&quot;$1&quot;</span> <span class="o">=</span> <span class="s2">&quot;-h&quot;</span> -o -z <span class="s2">&quot;$1&quot;</span> <span class="o">]</span> <span class="o">&amp;&amp;</span> usage<br><br><span class="c"># If -r just get the raw output, otherwise pass it through sed.</span><br><span class="k">if</span> <span class="o">[</span> <span class="s2">&quot;$1&quot;</span> <span class="o">=</span> <span class="s2">&quot;-r&quot;</span> <span class="o">]</span> ; <span class="k">then</span><br><span class="k">    </span><span class="nb">shift</span><br><span class="nb">    </span>get_thes <span class="nv">$1</span><br><span class="k">else</span><br>    <span class="c"># Sed is doing many things: </span><br>    <span class="c"># Print out &quot;no results found for &quot; and quit. </span><br>    <span class="c"># Print out only the lines I&#39;m interested in, which are:</span><br>    <span class="c">#   No results found for</span><br>    <span class="c">#   Main Entry:</span><br>    <span class="c">#   Definition:</span><br>    <span class="c">#   and all from Synonyms: to Antonyms:</span><br>    <span class="c"># Put a new line after Antonyms.</span><br>    get_thes <span class="nv">$1</span> | sed -n -e <span class="s1">&#39;/No results found for/ {&#39;</span> <span class="se">\</span><br>      -e <span class="s1">&#39; s|^[ \t]*\(No results .*\)|\1|p ; q} &#39;</span><span class="se">\</span><br>      -e <span class="s1">&#39;{/Main Entry:/p &#39;</span> <span class="se">\</span><br>      -e <span class="s1">&#39;/Definition:/p &#39;</span> <span class="se">\</span><br>      -e <span class="s1">&#39;/Synonyms:/,/\(Antonyms:\)\|\(^$\)/p} &#39;</span> <span class="se">\</span><br>      -e <span class="s1">&#39;/Antonyms:/ a \ &#39;</span><br><span class="k">fi</span>    <br></pre></div>
<p>
I use <b>thesaurus.com</b> because I found it has a wider coverage than
<b>merriam-webster.com</b>.  If you want to use a different website, you will need to
write your own sed script to parse it.
<p>
I separated the sed actions into discrete chunks above so you can get a clearer picture
of what sed is doing, but they appear as one line in the actual script.  That line is
below, for you to cut-n-paste.
<p>
<div class="pygmentize"><pre>    get_thes <span class="nv">$1</span> | sed -n -e <span class="s1">&#39;/No results found for/ { s|^[ \t]*\(No results .*\)|\1|p ; q} ;{/Main Entry:/p ; /Definition:/p ; /Synonyms:/,/\(Antonyms:\)\|\(^$\)/p} ; /Antonyms:/ a \  &#39;</span>
</pre></div>
<p>
<h2>Install</h2>
<p>
I prefer that this script is only available in buffers where I am writing text.  That
way, I can keep the <b>K</b> mapping for unix man pages in buffers where I am writing code.
<p>
The way to do that is to put the Vim script in its own file that is called only by
text-like filetypes.  Scripts that are unique to filetypes are put in <code>~/.vim/ftplugin</code> or
<code>~/.vim/after/ftplugin</code>.  I created files named text.vim, xml.vim and blog.vim that are in
the ftplugin directory.  (XML is treated as text because most of my XML is text data.)
I put the thesaurus script in a file called common.vim and I <code>source
common.vim</code> from within all the above ftplugin scripts.  
<p>
If ftplugin (and perhaps autocmd) are a mystery, see <code>:h ftplugin</code> and <code>:h autocmd</code>.
<p>
With that, I am done.  To use, move the cursor in Normal mode underneath the word of interest
and press <b>K</b>.
<p>
<h3>Bugs</h3>
<p>
I noticed once that the script loses permissions to a temporary file that Vim uses
internally.  It seems to only happen if a remote ssh session in screen is unexpectedly
terminated, and not always then.  Closing and re-opening Vim (yes, a pain) will fix it.
<p>
<a name="windows"></a>
<p>
<h2>Additional Info for Windows Users</h2>
<p>
<h4>Summary</h4>
In summary, Windows users have a little extra to do, but not much.
<ol><li>Install <code>sed</code> and <code>links</code>. It takes only a few seconds
each to download and install.  The default install is fine.</li>
<li>Create a batch file to get and parse the thesaurus data. The batch file will be an
abbreviated form of the shell script above, tailored for Windows.</li>
<li>Point vim to the location of the batch file.</li></ol>
<p>
<h4>Install Links &amp; Sed</h4>
Windows users will need to install the <code>links</code> and <code>sed</code>
commands and perhaps a shell if you don't want to use a DOS batch file (which is below).
I did a bit of a search and found a  <a
href="http://www.nickcoleman.org/axs/ax.pl?http://links.twibright.com/download/binaries/win32/">Windows
build of <code>links</code> here</a>, and a good set of  <a
href="http://www.nickcoleman.org/axs/ax.pl?http://gnuwin32.sourceforge.net/packages.html">unix
tools for Windows here</a> (direct link for sed is <a
href="http://www.nickcoleman.org/axs/ax.pl?http://gnuwin32.sourceforge.net/packages/sed.htm">
here</a>), both of which are fine.
<p>
<h4>Create DOS Batch File</h4>
You don't need the full-blown shell script above.  The simple DOS batch file below will do. It
runs links and then pipes the output to sed.  You might need to change the paths to the
folder(s) where you installed links and sed if you did not use the defaults when you
installed them.
<p>
<div class="pygmentize"><pre>@"c:\program files\links\links.exe" -dump http://thesaurus.com/browse/%1 | "c:\program files\gnuwin32\bin\sed.exe" -n -e <span class="s1">&quot;/No results found for/ { s|^[ \t]*\(No results .*\)|\1|p ; q} ;{/Main Entry:/p ; /Definition:/p ; /Synonyms:/,/\(Antonyms:\)\|\(^$\)/p} ; /Antonyms:/ a \  &quot;</span><br></pre></div>
<p>
There are a couple of differences to the unix script.  Sed needs double quotes
surrounding its commands instead of single quotes.  The paths to the executables need
double quotes because of the spaces in the path.  I put a '@' in front of everything
to prevent DOS from echoing back the entire command. Finally, I put <code>%1</code>
instead of <code>$1</code> for the DOS way to pass in the parameter.
<p>
It is probably worth opening a <code>cmd.exe</code> window and testing the batch file.  Assuming you
called the batch file "thesaurus.bat", run <code>thesaurus.bat loose</code> and you
should get a listing back after a few seconds with all the synonyms of "loose".
<p>
<h4>Point Vim</h4>
Now that you have a batch file that works, put it in a folder somewhere. The vim script
needs one small change to point to that location.  
<p>
In Windows, $HOME expands to "C:\Documents and Settings\{user}".<a href="#fn1"
id="back1"><sup>1</sup></a>  However, it doesn't
expand in a script if it is contained within quotes.  So the vim script should be
changed from 
<p>
<div class="code">
" Run the thesaurus script
   :exe ":0r !$HOME/bin/thesaurus " . s:thes_word 
</div>
<p>
to 
<p>
<div class="code">
" Run the thesaurus script
   :exe ':0r !"' . $HOME . '\thesaurus.bat" ' . s:thes_word
</div>
assuming again that your DOS batch file is called "thesaurus.bat".  This puts
$HOME outside the quotes and Vim will expand it to "C:\Documents and
Settings\{user}\thesaurus.bat"<a href="#fn1"
id="back1"><sup>1</sup></a>.  
<p>
If you put the batch file in a sub folder, add that folder name in front of
<code>'\thesaurus.bat'</code> like this: <code>'\my_folder\thesaurus.bat'</code>.
<p>
If it does not work, because you tested the batch file itself beforehand, the problem is
almost certainly in how you specified the location of your batch file in the vim script.
<p>
<hr>
<p>
<div class="footnote">
<a href="#back1" id="fn1">[1] for Windows XP.  Vista expands to "C:\Users\{user}".  Windows
7 the same as Vista? -- I don't know, I don't have it. </a> <a href="#back1">&#8593;</a>
</div>
<p>
]]></content:encoded>
<wfw:commentRss>http://www.nickcoleman.org/blog/index.cgi?post=vim-thesaurus%21201202170802%21general%2Cblogging%2Cinternet%2Cprogramming%2Csoftware%2Cunix/feed/</wfw:commentRss>
</item>
<item>
<title>Tiling Window Manager
</title>
<link>http://www.nickcoleman.org/blog/index.cgi?post=tilingwindowmanager%21201005041032%21software</link>
<comments>http://www.nickcoleman.org/blog/index.cgi?post=tilingwindowmanager%21201005041032%21software#comments</comments>
<pubDate>Tue, 04 May 2010 10:32:00 -0700</pubDate>
<dc:creator>Nick</dc:creator>
<category>software</category>
<guid isPermaLink="true">http://www.nickcoleman.org/blog/index.cgi?post=tilingwindowmanager%21201005041032%21software/</guid>
<description><![CDATA[ 
 [...]]]></description>
<content:encoded><![CDATA[
<p>
I would hazard a guess that almost all unix-like system users are using
a desktop of some sort or another.  KDE is very popular, along with
Gnome, Fluxbox and so on. Some people like all the bells and whistles
such as menus, fancy graphics, feedback via sounds, and beautiful
backgrounds.  Others prefer a utilitarian environment and don't want
anything that gets in the way or slows the system down.
<p>
I'm in the latter group, probably because my hardware is invariably a
few years old with not the latest and greatest graphics card.  It's not such a big deal
nowadays, but fancy graphics used to slow things down too much, and now
I'm used to the leaner look.  I'm a touch-typist too, so I like to keep my hands on
the keyboard.  It really slows me down and interrupts my chain of
thought if I have to lift my hands to go and move the mouse.  I'm not a
point-and-click person and I invariably use keyboard shortcuts even when
I'm using a fancy desktop.
<p>
I wonder how many people know that you don't need a desktop at all.  Before you
throw up your hands in horror at early memories of a single xterm in
X11, 
<p>
<div class="image"><img src="http://www.nickcoleman.org/blog/images/x11.png" alt="x11 no wm"
/><p>Xterm in X11 without a window manager</p></div>
let me reassure you we are talking about a managed environment
where you can do as much as easily as you can using a desktop. 
<p>
The environment we're talking about is a <b>tiling window manager</b>.
In this environment, there is no desktop.  Instead, the screen displays
one or more <i>frames</i> and each frame has only one application
running in it.  A single frame is the same as an application using the
full screen, or you may have two frames split either vertically or
horizontally on the screen, or three or more.  A common setup for
programming might be one large frame on the left, and two or three vertical
frames on the right with one showing, say,  a <i>man
</i> page and the other the progress of a compile.  Here is an example
with 4 frames:
<div class="image"><img src="http://www.nickcoleman.org/blog/images/tilingwm.jpg" alt="tiling
wm"><p>Tiling Window Manager with four frames</p> </div>
<p>
Typically you can resize or move a frame around with simple keystrokes.
In the example above, you might move frame (or window) 4 up by one frame, so now your
display might show the right hand column as window two, window four then
window three below.  
<p>
Why bother with this when your desktop can do the same thing?  Two
reasons: you do everything without your hands leaving the keyboard; and
you have a "fixed" layout that doesn't change no matter what you do with
your applications. For example, if you open a new application in a
desktop it typically might open in the centre of the screen and take up
about two-thirds of available space even if it covers up other
applications.  That can be annoying.  In a tiling window manager, it
opens up within the frame and takes up the frame's available space.  You know
exactly where and how the application will appear.
<p>
If you are a touch-typist, you will find it an absolute boon to be able
to move across to a spare frame, start up an app, and move back to where
you were, all done by using just a few strokes of the keyboard.
<p>
If you're a programmer, you will find the layout that you like and tend
to use it all the time because it makes you more productive.  As a
result, almost all tiling window managers allow you to give a name to a
particular layout and save it so it can be re-used via a concept called
grouping.  This ability is
very similar to the desktop's concept of a virtual desktop where you might have a
shell on Desktop One, a browser and email client on Desktop Two, and so
on.   In a tiling window manager you might have a group called "shells"
with a layout like the image shown above, a group called "browser" with
just a single frame with the browser in it, and so on.  It is usually
just a single keystroke like Alt-1, Alt-2, etc to switch between groups.
<p>
Another benefit of a tiling window manager is the speed.  It is
blistering fast to switch groups.  That is to say, it is blistering fast
to switch between your display of several xterms and your display with a browser
in it.  I imagine that is because you have removed a layer of
abstraction by not dealing with a desktop, but directly
with the window manager.
<p>
For me, there are very few drawbacks.  In fact, I can't think of any
drawbacks.  The only thing missing is a status bar like at the bottom of
a Desktop window that has a clock, weather, disk space and so on.  As we
shall see in a coming post, it is very easy to create such a display
using tools that work in a similar way to <a
href="http://www.nickcoleman.org/axs/ax.pl?http://conky.sourceforge.net">conky</a> and <a
href="http://www.nickcoleman.org/axs/ax.pl?http://dzen.geekmode.org/">dzen2</a>.  Stay tuned for my 700 line
shell script that provides any system information you might want and is
efficient and easy on resources (the secret is that very little of the
700 lines is actually executing at any one time).
<p>
If you want to give a tiling window manager a go, check the <a
href="http://www.nickcoleman.org/axs/ax.pl?http://en.wikipedia.org/wiki/Tiling_window_manager">Wikipedia
article</a> that has more details and a pretty comprehensive list of
available window managers.  I use <a
href="http://www.nickcoleman.org/axs/ax.pl?http://aerosuidae.net/musca/Musca_Window_Manager">Musca</a>
because it is very configurable and it lets me pass commands to it from
a script, a very useful feature as we shall see in the forthcoming
article.
]]></content:encoded>
<wfw:commentRss>http://www.nickcoleman.org/blog/index.cgi?post=tilingwindowmanager%21201005041032%21software/feed/</wfw:commentRss>
</item>
<item>
<title>PHP Loathing
</title>
<link>http://www.nickcoleman.org/blog/index.cgi?post=phphate%21201002120843%21software%2Cprogramming</link>
<comments>http://www.nickcoleman.org/blog/index.cgi?post=phphate%21201002120843%21software%2Cprogramming#comments</comments>
<pubDate>Fri, 12 Feb 2010 08:43:00 -0700</pubDate>
<dc:creator>Nick</dc:creator>
<category>software</category>
<category>programming</category>
<guid isPermaLink="true">http://www.nickcoleman.org/blog/index.cgi?post=phphate%21201002120843%21software%2Cprogramming/</guid>
<description><![CDATA[ 
 [...]]]></description>
<content:encoded><![CDATA[
<p>
I've recently had to use PHP to do some work on this site.  After using
Python for a while and not having used PHP for a few years, I'd
forgotten why I hate programming in it.
<p>
The documentation is appalling. I've been trying for <b>two</b>
days<sup><small><a href="#fn1" id="back1">1</a></small></sup> to
figure out how to do a very simple (you would think) thing: display the
local time.  The docs beat around the bush and tell you how to get time
difference from GMT, how to format the date, etc, etc. All common
vanilla stuff.  But how do I simply <i>show the freakin' time</i>?
<p>
I've tried <code>echo localtime()</code>.  Nope.  Ok, how about
<code>echo localtime(time());</code>.  Nope, it's an array, for god's
sake.  
<p>
How about this helpful suggestion for the PHP developers.  <code>echo
localtime()</code>, called without parameters, should do just that, using
locale timezone defaults, not return an array that presumably I then
have format myself.
<p>
At least Python has meaningful API calls that you can half-guess
yourself.
<p>
The situation is not helped because the official documentation can be
edited by <i>users</i>.  Can you believe it?  The <b>official</b> API
documentation can be edited by your non-official user.  What other
language allows that?  Answer: none that I use, for very good reasons.
<p>
API documentation serves as the official reference.  You don't want any
johhny-come-lately to be able to add their well-meaning but stupid
comments into the mix.  God knows API docs can be confusing enough
without a user being led up the garden path by some naive self-taught
PHP programmer who wouldn't know rigourous methods if they leapt up and
bit him on the bum.
<p>
So you are trying to find out how to print the time.  You start to read
the API docs.  <i>One thousand lines</i> later, you have read a couple of
hundred lines of docs and about five times as many lines again of
threaded comments that go off on tangents or become meaningless
is-too-no-it-isn't dribble.
<p>
Adding to the frustration is that, in their eternal wisdom, the doc
site maintainers have decided that syntax colouring is a good thing.
Normally it is, I use it all the time in vim.  Except that I get to
choose the colours in vim.  I don't get to choose on their web page
where their colours are forced on me.  Too bad if I have colour vision
deficiencies.  Too bad if I have trouble reading orange text on a gray
background (yep, that's their default).  Too bad if I hate their mono
font.  I can't change it.  Yet, for some reason, I can choose to display
the page in Bulgarian.  If I want.  
<p>
That sort of symbolises my problem with the whole language.  It's a bizarre
mix of strange choices.  Personally, I would have designed
the web page so that colour theming for the viewer was a bit of a higher
priority than seeing the page in Bulgarian.  Call me odd, but there you
are.  
<p>
Alright then, yes, I am being a bit facetious.  I'm sure that French and German speakers
warrant some docs in their language, but English docs that one can't read
because of typesetting choices also deserve better.
<p>
Hate, hate, hate PHP.  
<p>
Ok, rant mode off now.
<p>
<div id="footnote">
<a href="#back1" id="fn1">[1]</a> I'm not stupid.  I got some sort of
half-baked solution within five minutes.  The rest of the time was spent
looking for a better solution because I refused to believe that the one
I'd found was the correct way to do it.  It was though. I had the
unreasonable belief that a server-side scripting language would have an
easy way to do one of the most common things on the web: display the
local time.<a href="#back1">&#8593;</a>
</div>
]]></content:encoded>
<wfw:commentRss>http://www.nickcoleman.org/blog/index.cgi?post=phphate%21201002120843%21software%2Cprogramming/feed/</wfw:commentRss>
</item>
<item>
<title>Easy Charts and Graphs -- Use Google's Visualization API
</title>
<link>http://www.nickcoleman.org/blog/index.cgi?post=chartsandgraphsbygoogle%21201002040825%21software%2Cpython%2Cinternet</link>
<comments>http://www.nickcoleman.org/blog/index.cgi?post=chartsandgraphsbygoogle%21201002040825%21software%2Cpython%2Cinternet#comments</comments>
<pubDate>Thu, 04 Feb 2010 08:25:00 -0700</pubDate>
<dc:creator>Nick</dc:creator>
<category>software</category>
<category>python</category>
<category>internet</category>
<guid isPermaLink="true">http://www.nickcoleman.org/blog/index.cgi?post=chartsandgraphsbygoogle%21201002040825%21software%2Cpython%2Cinternet/</guid>
<description><![CDATA[ 
 [...]]]></description>
<content:encoded><![CDATA[
<p>
Have you sometimes wanted to make a quick graph or a pie chart to
display on your website?  Decided it was too hard for the amount of
effort and given up?  There is a very simple solution that is remarkably
easy.  You can use Google's Visualization API to create the graphs for you with just a
few lines of code.
<p>
Here is a screen shot of a very simple pie chart.  Note the extra
features such as a slice that pops out to identify it, a pop up with
more information, and sorted columns in the table.  These features come with it,
you don't need to do anything.
<div class="image"><img src="http://www.nickcoleman.org/blog/images/googlegraph.png" alt="google graph
example" /></div>
<p>
The image shows a pie chart and a table.  You can show either or both
plus several other types of graphs. I think it looks professional and neat.
<p>
The graphs are generated using javascript and ajax, and the browser will go and
load the Google javascript (which I guess could cause a small delay,
though on my site it is less than a second).  <a href="http://www.nickcoleman.org/blog/static/google_vis.html">Here </a> is the actual
page from which the screen shot was taken.  You will notice it is quite
quick in practical terms. Try clicking on one of the wedges, one of the
coloured index icons and a column header.
<p>
Here is the code that produced this display:
<p>
<pre><code>
<p>
&lt;html&gt;
  &lt;head&gt;
    &lt;!--Load the AJAX API--&gt;
    &lt;script type="text/javascript" src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
    
      // Load the Visualization API and the piechart package.
      google.load('visualization', '1', {'packages':['piechart', 'table']});
      
      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);
      
      // Callback that creates and populates a data table, 
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {
<p>
      // Create our data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Task');
        data.addColumn('number', 'Hours per Day');
        data.addRows([
          ['Work', 11],
          ['Eat', 2],
          ['Commute', 2],
          ['Watch TV', 2],
          ['Sleep', 7]
        ]);
<p>
        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 400, height: 240, is3D: true, title: 'My Daily Activities'});
	var table = new google.visualization.Table(document.getElementById('table_div'));
	table.draw(data );
      }
    &lt;/script&gt;
  &lt;/head&gt;
<p>
  &lt;body&gt;
    &lt;!--Div that will hold the pie chart--&gt;
    &lt;div id="chart_div"&gt;&lt;/div&gt;
    &lt;div id="table_div"&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
<p>
</code></pre>
<p>
It might look a fair bit more than the few lines of code I mentioned
above, but take out the comments and notice that 5 lines are the data
itself.  Twelve lines of code generate everything.  Most of it is template stuff and you provide only a few
parameters yourself like table type, column headings and so on.  You can see
that all of it goes in the &lt;head&gt; section, with just a couple of
&lt;div&gt; tags in the body.
<h4>Parameters</h4>
The parameters you set to control the display are the chart type, the
column and row names, the data, the chart's title, width and height, and
the &lt;div&gt; section to display the chart in the body of your page.
<p>
The chart <b>type</b> is set when you load the vizualization api itself.
You specify an array of packages; here I've chosen pie chart and table.
<p>
The <b>column names</b> are set when you add a column of data.  Then
you add the <b>rows</b> of data and there are several ways to do this.  The
simplest is to place it in an array.  Just note that this means
your data will be viewable in the source.  For simple charts this may
not matter, and you can get the data externally in which case it
won't show up. 
<p>
The <b>title</b>, <b>width</b> and <b>height</b> are set when you draw
the chart.
<p>
Finally, the chart is displayed in a <b>&lt;div&gt;</b> section that you specified
when you created the chart.
<p>
The point is that it is extremely simple and you don't need to know any
javascript at all.
<h4>More</h4>
Once you've made up one graph, you've pretty much made any others you
might want later.  You only need to copy the html file and edit the
headings, etc.  Cut and paste the data and you are done.  Simple.
<p>
The API can draw a range of different chart types such as pie, horizontal bar,
vertical columns, lines, scatter plots, and even images like piles of money.
You can also get the API to show minimum and maximums, averages, aggregators
(similar to SQL aggregators), and get the data from an external source
like a Google spreadsheet or a javascript file instead of hard
coding it into the source code of the chart's page. If you are a bit more advanced and know python,
you can use a python module to talk directly to the API and
generate everything within python.
<p>
Google's visualization documentation starts <a
href="http://www.nickcoleman.org/axs/ax.pl?http://code.google.com/apis/visualization/">here</a>, and the
docs for graphs are <a
href="http://www.nickcoleman.org/axs/ax.pl?http://code.google.com/apis/visualization/documentation/using_overview.html#preparedata">here</a>.
While you are there, check out the <a href="http://www.nickcoleman.org/axs/ax.pl?http://code.google.com/apis/ajax/playground/?type=visualization">Interactive Samples</a> section to see how the different
graph types can be used.
<p>
<h4>Sweet Spot</h4>
Finally, I offer this as a sort of after-dinner mint, one extra
visualization from Google.  The API provides a <b>tag cloud</b> gadget that you
can easily plug in to your blog or cms.  I personally am not a huge fan
of tag clouds.  To my eyes they look like a good way of obfuscating data
in a mass of competing visuals.  However, if you want one and can't be
bothered/don't know how to implement one yourself, check out a couple of
offerings of <a
href="http://www.nickcoleman.org/axs/ax.pl?http://code.google.com/apis/visualization/documentation/gadgetgallery.html">
Google's</a>.  They are straighforward to implement and look the goods.
<p>
]]></content:encoded>
<wfw:commentRss>http://www.nickcoleman.org/blog/index.cgi?post=chartsandgraphsbygoogle%21201002040825%21software%2Cpython%2Cinternet/feed/</wfw:commentRss>
</item>
<item>
<title>Kukkaisvoima Blogging Software
</title>
<link>http://www.nickcoleman.org/blog/index.cgi?post=kukkaisvoima%21200912201044%21python%2Csoftware%2Cblogging</link>
<comments>http://www.nickcoleman.org/blog/index.cgi?post=kukkaisvoima%21200912201044%21python%2Csoftware%2Cblogging#comments</comments>
<pubDate>Sun, 20 Dec 2009 10:44:00 -0700</pubDate>
<dc:creator>Nick</dc:creator>
<category>python</category>
<category>software</category>
<category>blogging</category>
<guid isPermaLink="true">http://www.nickcoleman.org/blog/index.cgi?post=kukkaisvoima%21200912201044%21python%2Csoftware%2Cblogging/</guid>
<description><![CDATA[ 
 [...]]]></description>
<content:encoded><![CDATA[
<p>
I wanted a simple light-weight personal blogging tool that lets me write in flat files.
Like many <a
href="http://www.nickcoleman.org/axs/ax.pl?http://utcc.utoronto.ca/~cks/space/blog/web/BrowsersMakeBadEditorsII">others</a>,
I very much dislike editing text in a browser, especially anything more than a
few lines.  Browsers seem so overloaded with junk and javascript nowadays
that their response time has crept up to several tenths of a second, very frustrating for a
touch-typist who needs response times of around a tenth to catch
errors <small><sup><a href="#fn1" id="back1">1</a></sup></small>.  So, this tool needed to work with flat
files that I could edit in vim and easily transfer up to the blog host.
<p>
As well, it needed to be light-weight.  I can't see my blog ever running to more
than a few hundred entries per year max.  Tools like Wordpress et al are too heavy
for me, in the sense that their processing slows things down by comparison.
<p>
Python would be good, I prefer it to PHP.  I really like it, it is a great language.
<p>
Also, I wanted some simple things like categories and comments, and search would
be nice too.
<p>
Originally, I had tried pyblosxom, but I found the lack of documentation
frustrating.  I put a few days into it and got the core working and a couple
of plugins.  Eventually though,  I gave up after two days of unsuccessfully
trying to get the comments plugin to work.  Plus, I couldn't get the logging to
work so I couldn't debug the problem.  I decided that almost a week's worth of
time was enough.  
<p>
(To be "sort of" fair to pyblosxom, they <i>are</i> in the middle of a version
upgrade, which means older plugins are broken and docs are behind the pace.
That doesn't solve my immediate need though, so pyblosxom is out for me. And,
really, I don't get the whole "I'd rather be programming" thing.  I write docs
as I go, they form the application's specifications<sup><a href="#fn2"
name="back2">2</a></sup>.)
<p>
All of which motivated me to look for something else, which led me
to <a
href="http://www.nickcoleman.org/axs/ax.pl?http://23.fi/kukkaisvoima/">Kukkaisvoima</a>, for which I'm grateful.
I downloaded and installed Kukkaisvoima  on my local machine.  It is
very useful for my needs: a light-weight simple blogging tool for flat files,
written in Python, with comments and categories.  The archive and search are a nice bonus.
<p>
<h4>Kukkaisvoima in a nutshell</h4>
Kukkaisvoima uses simple text files for posts.  The first line is the headline and everything
else is text.  HTML is allowed, which is useful for links and images.  It
requires you to use a &lt;p&gt; tag to start a new paragraph.
<p>
The text file's name is used to generate the post's date and categories.  For
example, the file name for this entry (now changed, so it won't work as a link) is
"kukkaisvoima:2009-12-20:python,software,blogging.txt", which kukkaisvoima tokenises
into three parts separated by a ':'; the first is (presumably) not used
internally, the second is used to generate the post's date, and the third is a
comma-separated list of categories for this post.  Simple and effective.
<p>
Kukkaisvoima automatically shows in the sidebar a list of categories, month archives, and a
search box.
<p>
Each post can have comments or they can be disabled globally, but not for
individual posts.  There is very limited checking on comments: required
username and email, a simple question-and-answer check for a live person (no
captcha or akismet), and html tags are stripped from the comment's text.
IP addresses are not logged.  There is no database engine, so SQL injection
is not a problem.
<p>
<h4>My Modifications</h4>
Kukkaisvoima has no docs apart from a simple README, so am I about to complain as I
did for pyblosxom?  No.  It's ok because the program logic is very simple to
follow.  It's all clear and concise, self-documenting really.  Meaningful
function names help a lot, don't they :).  I had no trouble to find my way
around the code, unlike pyblosxom which was confusing to me.   
<p>
I modified it a little to suit my needs, which is not a criticism. We all have
our own idea of what we need and how important they are.
<p>
The main things I wanted to implement were:  recording the IP address of
people making comments; removing the need for &lt;p&gt; tags to signify a new
paragraph when writing a blog entry; and removing the need for a date token
in the blog text's filename.  Fairly minor stuff, except I really wanted to
record the IP address, it was a priority for me.
<p>
The IP address turned out to be quite easy once I realised that, like PHP,
Python has it in the external environment.  I added a hidden attribute to the
comment form that recorded os.environ['REMOTE_ADDR'], and added attributes
for it to the appropriate classes and functions.  In the comments display
function, I print it out with the least-significant octet displayed as .xxx,
for privacy.  (It's still recorded in full, just not displayed.)
<p>
The tags thing was easy too.  I added a line to the display function that
checked for a newline at the start of the line, and replaced the line with "&lt;p&gt;\n".  Now I
simply put a blank line in the text, and the html renderer adds a &lt;p&gt; tag.
(I really don't like writing HTML when I'm in the middle of writing text. It
interupts the flow.)
<p>
My next change will be to the file name tokenising, to remove the need for the
date in the file name.  For my needs, I'm happy to use os.stat.CTIME to
generate the post's date.  
<p>
I also made some minor format changes: the date display uses a strtime()
format; date appears directly under the headline rather than with the
comments; IP address for each comment; and some spacing and colour changes in
the CSS.
<p>
In the CSS, I did change many px attributes to either % or em.  I have a
wide-screen laptop and fixed-size px meant the columns were quite narrow
compared to the screen's real estate.  Again, thanks to the clear relationship
between divs in the html and the css, it was very easy to find which classes
and ids needed to be changed.  
<p>
All in all, it has been a great learning experience.  I have brushed up on my
Python and my CSS understanding.  The author deserves kudos for making the
codebase clear and logical with easily understood relationships.
<p>
Oh, it's Finnish for "flower power" according to google translate.
<p>
<hr>
<div id="footnote">
<a href="#back1" id="fn1">[1]</a>  I change <a href="http://www.nickcoleman.org/axs/ax.pl?http://www.gnu.org/software/screen">screen's</a>
keystroke timeout to less than 100 msecs, otherwise I'm constantly tripping up in vim.
<p>
<a href="#back2" name="fn2">[2]</a>  You won't be surprised to hear I like
Eiffel and the self-documenting design-by-contract philosophy. <a href="#back1">&#8593;</a>
</div>
]]></content:encoded>
<wfw:commentRss>http://www.nickcoleman.org/blog/index.cgi?post=kukkaisvoima%21200912201044%21python%2Csoftware%2Cblogging/feed/</wfw:commentRss>
</item>
</channel>
</rss>

