emacsからgoogle code searchを引く

プログラミング中に関数・メソッド・変数等の具体的な使い方がわからなくなったら、とりあえずgoogle code searchを引くとよい。そこで、emacsの中で現在のメジャーモードに合わせた言語でgoogle code searchを引くEmacs Lispをでっちあげた。要emacs-w3m

M-x gcode でクエリを入力すると、別ウィンドウに検索結果が出てくるぞ。

(setq google-code-search-languages
      '(;; 機械生成のため、すごいてきとー 要修正
        (actionscript-mode . "actionscript")
        (ada-mode . "ada")
        (applescript-mode . "applescript")
        (asp-mode . "asp")
        (assembly-mode . "assembly")
        (autoconf-mode . "autoconf")
        (automake-mode . "automake")
        (awk-mode . "awk")
        (basic-mode . "basic")
        (bat-mode . "bat")
        (c-mode . "c")
        (c++-mode . "c++")
        (csharp-mode . "c#")
        (cobol-mode . "cobol")
        (coldfusion-mode . "coldfusion")
        (configure-mode . "configure")
        (css-mode . "css")
        (d-mode . "d")
        (eiffel-mode . "eiffel")
        (erlang-mode . "erlang")
        (fortran-mode . "fortran")
        (haskell-mode . "haskell")
        (inform-mode . "inform")
        (java-mode . "java")
        (javascript-mode . "javascript")
        (jsp-mode . "jsp")
        (lex-mode . "lex")
        (limbo-mode . "limbo")
        (lisp-mode . "lisp")
        (emacs-lisp-mode . "lisp")
        (lua-mode . "lua")
        (m4-mode . "m4")
        (makefile-mode . "makefile")
        (maple-mode . "maple")
        (mathematica-mode . "mathematica")
        (matlab-mode . "matlab")
        (messagecatalog-mode . "messagecatalog")
        (modula2-mode . "modula2")
        (modula3-mode . "modula3")
        (objectivec-mode . "objectivec")
        (ocaml-mode . "ocaml")
        (pascal-mode . "pascal")
        (perl-mode . "perl")
        (php-mode . "php")
        (pod-mode . "pod")
        (prolog-mode . "prolog")
        (python-mode . "python")
        (r-mode . "r")
        (rebol-mode . "rebol")
        (ruby-mode . "ruby")
        (sas-mode . "sas")
        (scheme-mode . "scheme")
        (scilab-mode . "scilab")
        (shell-mode . "shell")
        (sgml-mode . "sgml")
        (smalltalk-mode . "smalltalk")
        (sql-mode . "sql")
        (sml-mode . "sml")
        (svg-mode . "svg")
        (tcl-mode . "tcl")
        (tex-mode . "tex")
        (texinfo-mode . "texinfo")
        (troff-mode . "troff")
        (verilog-mode . "verilog")
        (vhdl-mode . "vhdl")
        (vim-mode . "vim")
        (xslt-mode . "xslt")
        (xul-mode . "xul")
        (yacc-mode . "yacc")
        (aladdin-mode . "aladdin")
        (artistic-mode . "artistic")
        (apache-mode . "apache")
        (apple-mode . "apple")
        (bsd-mode . "bsd")
        (cpl-mode . "cpl")
        (gpl-mode . "gpl")
        (lgpl-mode . "lgpl")
        (disclaimer-mode . "disclaimer")
        (ibm-mode . "ibm")
        (lucent-mode . "lucent")
        (mit-mode . "mit")
        (mozilla-mode . "mozilla")
        (nasa-mode . "nasa")
        (python-mode . "python")
        (qpl-mode . "qpl")
        (sleepycat-mode . "sleepycat")
        (zope-mode . "zope")))

(defun google-code-search (query &optional lang)
  (interactive
   (let ((lang (gcs-read-language)))
     (list (read-string (format "Google code search (lang:%s): " lang)) lang)))
  (let ((w3m-pop-up-windows t))
    (if (one-window-p) (split-window))
    (other-window 1)
    (w3m
     (format "http://www.google.com/codesearch?q=%s+lang:%s&hl=ja&num=20" query lang)
     t)))
(defun gcs-read-language ()
  (let ((cell (assq major-mode google-code-search-languages)))
    (if cell (cdr cell)
      (read-string "Language: "))))

(defalias 'gcode 'google-code-search)


ちなみに、google-code-search-languagesは以下のRubyスクリプトで生成した。

require 'open-uri'
langs = URI("http://www.google.com/codesearch/advanced_code_search").read.scan(/option value=(\S+)/).flatten[1..-1]
puts langs.map {|x| "  (#{x}-mode . #{x.dump})\n" }
# >>   (actionscript-mode . "actionscript")
# >>   (ada-mode . "ada")
# >>   (applescript-mode . "applescript")
# >>   (asp-mode . "asp")
# >>   (assembly-mode . "assembly")
# >>   (autoconf-mode . "autoconf")
# >>   (automake-mode . "automake")
# >>   (awk-mode . "awk")
# >>   (basic-mode . "basic")
# 略