概要
以下の記事で GNU GLOBAL (以下GLOBAL) で Pygments をパーサーとして使えることを知りました。
- [GNU GLOBALへのPygmentsパーサー取り込みでソースコード読みが信じられないくらいに捗るはず]
(http://qiita.com/5t111111/items/c14ac68f762ce71a7760)
この記事では Homebrew でインストール方法を紹介しています。私は普段 MacPorts (以下ports) を使っているのですが、インストールしようと思ったらちょっと戸惑ったので整理しておきます。
ちなみに GLOBAL の最新バージョンは 6.3.3 で Python3 の Pygments に対応したようです。
- [GLOBAL-6.3.3 released.]
(http://lists.gnu.org/archive/html/info-global/2014-11/msg00000.html)
Exuberant Ctags のインストール
パーサーに Exuberant Ctags を使いたい場合は GLOBAL をインストールする前にインストールしておくと良いでしょう。ややこしいのですが、Mac OS X (Xcode?) に付属している ctags コマンドは Exuberant Ctags ではないようです。ctags の派生版については wikipedia:Ctags を参照してください。
ports にある ctags が Exuberant Ctags のようです。
$ port info ctags
ctags @5.8_1 (devel)
Variants: universal
Description: Exuberant Ctags generates ...
まずは ctags をインストールします。
$ sudo port install ctags
Pygments のインストール
ports でも pip でもどちらでも良いと思いますが、ports でインストールするならこうかな。
$ sudo port install py27-pygments # or py34-pygments
GLOBAL のインストール
普通にインストールしようとすると、バイナリパッケージをインストールするようです。バイナリパッケージでは ctags や pygments のプラグインが有効になっていないため、ソースからビルドしてインストールするために -s
オプションを付けます。
$ sudo port -s install global
$ global --version
global (GNU GLOBAL) 6.3.3
Copyright (c) 2014 Tama Communications Corporation
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
以下にプラグインのバインディングが置かれます。libtool をインストールしていても la
は作られないようです。
$ ls /opt/local/lib/gtags/
exuberant-ctags.a exuberant-ctags.so
pygments-parser.a pygments-parser.so
user-custom.a user-custom.so
設定
ここでは ports に含まれている gtags.conf をコピーしてきて編集します。
$ cp -p /opt/local/share/gtags/gtags.conf ~/.globalrc
デフォルトでは、la
を使う設定となっているため、so
を使うように変更します。
$ sed -i -e "s/exuberant-ctags\.la/exuberant-ctags.so/g" ~/.globalrc
$ sed -i -e "s/pygments-parser\.la/pygments-parser.so/g" ~/.globalrc
これで準備は整いました。
これらの設定ファイルの場所や後述するタグパーサーを環境変数で指定することもできるようです。
$ export GTAGSCONF="/opt/local/share/gtags/gtags.conf"
$ export GTAGSLABEL="pygments" # or ctags
pygments パーサーを使ってタグを生成する
hyper という Python 製の http2 クライアントツールで試してみましょう。
$ git clone git@github.com:Lukasa/hyper.git
$ cd hyper
$ gtags -v --gtagslabel=pygments --debug
...
[1] extracting tags of conftest.py
File './conftest.py' is handled as follows:
suffix: |.py|
language: |Python|
parser: |parser|
library: |/opt/local/lib/gtags/pygments-parser.so|
...
pygments-parser を使ってタグファイルを生成しているのが分かります。
もし以下のようなエラーが出たら、
File './conftest.py' is handled as follows:
suffix: |.py|
language: |Python|
parser: |parser|
library: |/opt/local/lib/gtags/pygments-parser.so|
Traceback (most recent call last):
File "/opt/local/share/gtags/script/pygments_parser.py", line 27, in <module>
import pygments.lexers
ImportError: No module named pygments.lexers
gtags: unexpected EOF.
pygments を使うスクリプトのシェバングをその環境の Python を使うように変更すると良いです。Pygments をどこにインストールするか次第で変わります。
$ vi /opt/local/share/gtags/script/pygments_parser.py
--- pygments_parser.py.orig 2014-12-02 08:39:05.000000000 +0900
+++ pygments_parser.py 2015-01-26 18:37:30.000000000 +0900
@@ -1,4 +1,4 @@
-#!/opt/local/bin/python
+#!/usr/bin/env python
#
# Copyright (c) 2014
# Yoshitaro Makise
タグを参照する
global コマンドを使って参照してみます。
$ global -x -t ignore_missing
ignore_missing hyper/compat.py 24
ignore_missing hyper/http20/tls.py 10
$ global -x -r ignore_missing
ignore_missing 42 hyper/http20/tls.py with ignore_missing():
ignore_missing 58 hyper/http20/tls.py with ignore_missing():
exuberant-ctags パーサーを使う
同様に ctags を使うように指定してタグファイルを生成します。ctags で生成したときはシンボルに対するリファレンスの方がうまく動きませんでした。
$ gtags -v --gtagslabel=exuberant-ctags --debug
$ global -x -t ignore_missing
ignore_missing hyper/compat.py 24
ignore_missing hyper/http20/tls.py 10
$ global -x -r ignore_missing