LoginSignup
1
1

More than 5 years have passed since last update.

Common Lispの手軽なSymbolの調べ方

Posted at

HyperSpecとは?

HyperSpecはCommon Lisp(以下CL)のシンボルをmanのように調べられる。
つまり CL の関数やマクロ、ifなどについて調べたくなった時は HyperSpec が頼りになる。
それを利用する上で、おそらく一番簡単な方法はここからシンボルを検索することだ。

w3m 'http://clhs.lisp.se/Front/X_AllSym.htm'

何の準備もいらない上にシンボルの一覧性はある。
しかし、perldocやpydocのように doc_command Symbol と利用したくなる。
平たく言えばめんどくさい。

試しにHyperSpecを利用する現実的なコード

補完もできないやっつけ仕事だか、書き出すとこんな感じになる。
引数無しで一覧の所に飛び、1つのシンボル名を与えると該当urlに飛ぶ。

#!/usr/bin/env python3
import urllib.request
import re
import subprocess
import sys

Symbol_ref = 'http://clhs.lisp.se/Front/X_AllSym.htm'
if len(sys.argv) == 1:
    to_Symbol_list = 'w3m ' + Symbol_ref
    subprocess.call(to_Symbol_list, shell=True)
elif len(sys.argv) == 2:
    with urllib.request.urlopen(Symbol_ref) as res:
        html = res.read()
        is_Symbol = 'Body/.+?#' + sys.argv[1] + '"'
        url_suffix = re.sub('#.*', '',
                            "".join(re.findall(is_Symbol, html.decode())))
        if url_suffix:
            do_w3m = 'w3m http://clhs.lisp.se/' + url_suffix
            subprocess.call(do_w3m, shell=True)
        else:
            print("The symbol was not found.")
else:
    print("Please input 0 or 1 Symbol of Lisp.")

ソースをダウンロードして実用化したコード

これで使い勝手は分かるが、通信環境次第で遅さや利用できない事が気になる。
雰囲気をつかむお試しなら十分ながらperldocやpydocのような速度、
ネット環境に依存しない堅実性をもとめるとやはりダウンロードが必須になる。
幸い HyperSpec-7-0.tar.gz のような形でそのhtmlを丸ごとダウンロードできるので ~/lib/ に置くとする。

#!/usr/bin/env python3
import urllib.request
import re
import os
import subprocess
import sys

# USAGE: this-script.py or this-script.py [Symbol]
ref_dir = os.environ["HOME"] + "/lib/HyperSpec/"
ref_top_dir = ref_dir + "/Front/X_AllSym.htm"
if len(sys.argv) == 1:
    to_Symbol_list = "w3m " + ref_top_dir
    subprocess.call(to_Symbol_list, shell=True)
elif len(sys.argv) == 2:
    top_as_html = "file://" + ref_top_dir
    with urllib.request.urlopen(top_as_html) as res:
        html = res.read()
        is_Symbol = 'Body/.+?#' + sys.argv[1] + '"'
        url_suffix = re.sub("#.*", "",
                            "".join(re.findall(is_Symbol, html.decode())))
        if url_suffix:
            do_w3m = "w3m " + ref_dir + url_suffix
            subprocess.call(do_w3m, shell=True)
        else:
            print("The symbol was not found.")
else:
    print("Please input 0 or 1 Symbol of Lisp.")

補完も相変わらず効かない上にaspellのような正しくないシンボル名からタイポ復元を示唆することもできない。
荒いところはそのままだが、これで純粋に速度等々を改善できた。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1