eev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[eev] infoindex.rb


From: rubikitch
Subject: [eev] infoindex.rb
Date: Mon, 20 Jun 2005 00:00:55 +0900 (JST)

I created a command index of ratpoison and screen.

# (eev "infoindex.rb -o /tmp/ratpoison.e ratpoison.info")
# (eev "infoindex.rb -o /tmp/screen.e screen.info*")

# (find-fline "/tmp/ratpoison.e")
# (find-fline "/tmp/screen.e")


The infoindex.rb script is here.

#!/usr/bin/env ruby
require 'optparse'
require 'tempfile'

class << IO
  def redirect(stdout)
    begin
      stdout_sv = STDOUT.dup
      STDOUT.reopen(stdout)
      yield
    ensure
      STDOUT.flush
      STDOUT.reopen(stdout_sv)
    end
  end
end

def system_to_string(*args)
  ret = nil
  Tempfile.open("s2s") do |f|
    IO.redirect(f) {
      system *args
    }
    ret = f.open.read
  end
  ret
end

module EmacsLisp
  def elisp(lisp)
    system_to_string("gnudoit", lisp).chomp
  end

  # imported from el4r
  def dump_string(string)
    dumped = string.dup
    # \ -> \\
    dumped.gsub! %r"\\" do '\\\\' end
    # " -> \"
    dumped.gsub! %r'"' do '\\"' end
    # (zero byte) -> \0
    dumped.gsub! %r'\0' do "\\\0" end
    %Q'"#{dumped}"'
  end

end

class ParseInfo
  def initialize(io, regexp)
    @io = io
    @regexp = regexp
    @results = []
  end

  class Result < Struct.new(:info, :node, :line)
    include EmacsLisp
    def to_s
      arg = dump_string("(#{info})#{node}")
      line = dump_string(self.line)
      %Q[# (find-node #{arg} #{line})]
    end
  end

  def parse
    @io.read.split(/\n/).each do |line|
      case line
      when /^File: (.+)\.info,  Node: (.+?),/
        @info = $1
        @node = $2
      when @regexp
        @results << Result.new(@info, @node, line)
      else
      end
    end
        
  end

  def output(output=$>)
    @results.each do |res|
      output.puts res
    end
  end
end
  

if __FILE__==$0
  outfile = nil
  ARGV.options {|o|
    o.on("-o OUTFILE") {|v| outfile = v}
    o.parse!
  }

  obj = ParseInfo.new(ARGF, /^ - Command: (.+)$/)
  obj.parse

  if outfile
    File.chmod 0644, outfile if File.exist? outfile
    open(outfile, "w"){|io|
      obj.output io
      io.chmod 0444
    }
  else
    obj.output $>
  end
end


--
rubikitch




reply via email to

[Prev in Thread] Current Thread [Next in Thread]