huh!?

Jul 7 ’09

vim geekery (the end of the blogging hiatus)

In my never ending quest for the perfect editor, I have returned to vim for a while. On the 43folders wiki, I came across a neat little trick to do a proto-wiki. By adding the following line

:map gf :e <cfile><CR>

to the .vimrc file, you can type gf to open a file named with the word under the cursor (or create it if it does not exist).

I toyed around with this, and was slightly bothered by the fact that there was no visual indication as to whether a file exists or not. I then came up with a little hack to resolve the situation. First, I changed the shortcut to create a file with the extension hw (for helgewiki).

:nmap hw :lchdir %:p:h<CR>:e <cword>.hw<CR>

Then I created a small ruby script that collects all the file names in the wiki directory and creates a syntax definition file where each filename is defined as a keyword (the file extension is ignored). This script is automatically invoked when a file is saved. I also set files with the extension hw to use the helgewiki syntax definition.

class HelgeWiki
  WIKI_PATH = "D:\\txt\\protowiki"
  SYNTAX_PATH = ENV['HOMEDRIVE']+ENV['HOMEPATH']+"\\vimfiles\\syntax\\"
  SYNTAX_FILE = "helgewiki.vim"
  SYNTAX_PREFIX = "syntax keyword identifier"
  WIKI_FILE_SUFFIX = "hw"
  def parse
    @wiki_names = []
    File.delete(SYNTAX_PATH + SYNTAX_FILE) if File.exists?(SYNTAX_PATH + SYNTAX_FILE) 
    file_mask = /(\w+)\.#{WIKI_FILE_SUFFIX}/
    contains = Dir.new(WIKI_PATH).entries
    contains.each do |file|
      if file =~ file_mask 
        @wiki_names.push($1)
      end
    end
    sugar = SYNTAX_PREFIX + " " + @wiki_names.join(" ")
    File.open(SYNTAX_PATH + SYNTAX_FILE, 'w') {|f| f.write(sugar) }
  end
end
h = HelgeWiki.new
h.parse

It is kludgy, and I am not sure it scales well, but for now, I have a cheap, functional, barebones plain text wiki with no special syntax needed for links.