Doom 的呐喊

Crying in the Day of Doom

Code Hilighting in ConTeXt

今天需要排一个带 Scheme 代码的文档,里面还有点数学公式,所以决定用 ConTeXt。正好前几天 Steamedfish 同学跟我说过 ConTeXt 可以读取 Vim 的语法加亮,决定看一看。

Google 了一下之后发现 ConTeXt 是使用一个叫做 t-vim 的 module 来干这件事的,我使用的 ConTeXt 发行是 ConTeXt Minimal,于是进入 ConTeXt 的安装目录,更新

./first-setup.sh --extras=t-vim

然后随便排了一个代码。ConTeXt 报错

VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Mar  8 2010 22:56:09)
Garbage after option argument: "-u NONE -e -C -n -c "set tabstop=8" -c
"syntax on" -c "set syntax=scheme" -c "let contextstartline=1" -c "let
contextstopline=0" -c "source 2context.vim" -c "wqa"
"009-vimsyntax.tmp" "

哭了… 这不是要整死我这个 Vim 白痴么… 经过无穷多小时的 google,我终于认识到“Garbage after option argument”错误表示 Vim 的参数有错… 看来要改 t-vim 的代码了。于是打开 t-vim.tex,一看又哭了… 一个 macro 都不认识,这不是要整死我这个 TeX 白痴么… 经过 3000+ ms 的搜索,我终于认识到问题出在 t-vim.tex 的第 344 行:

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
   {\executesystemcommand
      {mtxrun --verbose --noquote bin:vim
          "-u NONE  % No need to read unnessary configurations
           -e       % run in ex mode
           -C       % Set compatibile
           -n       % No swap
%          -V10log  % For debugging only, will go away later.
           -c \shellescapedquote set tabstop=\@@vstab \shellescapedquote\space    
           -c \shellescapedquote syntax on\shellescapedquote\space 
           -c \shellescapedquote set syntax=\@@vssyntax\shellescapedquote\space
           -c \shellescapedquote let contextstartline=\@@vsstart\shellescapedquote\space
           -c \shellescapedquote let contextstopline=\@@vsstop\shellescapedquote  \space
           -c \shellescapedquote source kpse:2context.vim\shellescapedquote\space
           -c \shellescapedquote wqa\shellescapedquote\space    
            \shellescapedquote#1\shellescapedquote\space "}}

Macro \shellescapedquote 后面的 \space 会在文件名(#1)的后面加上一个空格。而 mtxrun 会把这个空格当作参数的一部分??!!!于是 vim 就会收到一个很诡异的参数而报错。所以我首先想到的解决方法就是把最后那个 \space 去掉。然后发现编译时最后的那个引号(\shellescapedquote)居然就没了,还是出错… 于是我怒了,毅然决定把 mtxrun 换成无敌的 Bash。

\def\runvimsyntax#1
   {\executesystemcommand
      {bash -c "vim -u NONE -e -C -n -c \shellescapedquote set tabstop=\@@vstab \shellescapedquote\space -c \shellescapedquote syntax on\shellescapedquote\space -c \shellescapedquote set syntax=\@@vssyntax\shellescapedquote\space -c \shellescapedquote let contextstartline=\@@vsstart\shellescapedquote\space -c \shellescapedquote let contextstopline=\@@vsstop\shellescapedquote  \space -c \shellescapedquote source /home/corsair/software/context/tex/texmf-context/tex/context/third/vim/2context.vim\shellescapedquote\space -c \shellescapedquote wqa\shellescapedquote\space \shellescapedquote#1\shellescapedquote\space "}}

宇宙终于又开始正常运行了。

2 Responses to “Code Hilighting in ConTeXt”

  1. SteamedFish says:

    最近 Arch 升级了 zlib 之后 luatex 挂掉了。你的还能用么?

  2. Corsair says:

    我的 ConTeXt minimal 升级之后一次之后就可以用了。而且升级之后再降级还是可以用,不知道为什么…

Leave a Reply