Vim 的 syntax coloring 功能強大,讓人愛不釋手;而 doxygen 的出現,讓 C、C++ 甚至 Perl,也都能有像 Javadoc 那樣的 in-code documentation solution。如果能夠兩者結合,如許多 Java IDE 在 editor 裡對 Javadoc 的支援那樣[1],連 comment 裡的文件內容,都能夠把 syntax coloring 弄得漂漂亮亮的,那就更棒了。

剛剛在逛 doxygen 網站,被我發現這個 script:《DoxyGen Syntax : DoxyGen Highlighting on top of c/c++/java》。簡單說,就是能夠在原本 Vim 對 C/C++/Java/... 的 syntax coloring 的基礎上,再增加對註解裡 doxygen 文件內容的 syntax coloring。於是趕緊抓下來試試。

網頁上的安裝方法比較複雜,我試了幾次沒有成功。所以就研究了一下 Vim 的 online document,發現 *mysyntaxfile-add* 這一節這麼說道:

If you are mostly satisfied with an existing syntax file, but would like to add a few items or change the highlighting, follow these steps:

  1. Create your user directory from 'runtimepath', see above.
  2. Create a directory in there called "after/syntax".  For Unix:
    mkdir ~/.vim/after
    mkdir ~/.vim/after/syntax
  3. Write a Vim script that contains the commands you want to use.  For example, to change the colors for the C syntax:
    highlight cComment ctermfg=Green guifg=Green
  4. Write that file in the "after/syntax" directory.  Use the name of the syntax, with ".vim" added.  For our C syntax:
    :w ~/.vim/after/syntax/c.vim

That's it.  The next time you edit a C file the Comment color will be different.  You don't even have to restart Vim.

也就是說,放在 ~/.vim/after/syntax 目錄下的檔案,會自動附加到對應檔名的 vim syntax coloring 檔案。如 ~/.vim/after/syntax/c.vim 就會自動附加到 /usr/locall/share/vim/syntax/c.vim 裡。因此,整件事就很簡單啦:為每一個想要增加 doxygen syntax coloring 的檔案類型,複製一份 doxygen syntax 檔以檔案類型為檔名,放在該目錄下即可,正常來說,連一行 ~/.vimrc 的設定都不必加。我的作法如下:

  1. 建立 ~/.vim/after/syntax 目錄;
  2. 網頁上下載 doxygen.zip (v1.15)
  3. 解開後得到 doxygen.vimdoxygen.txt 兩個檔案,前者是 syntax 檔,後者是說明檔。都放到 ~/.vim/after/syntax 目錄下;
  4. 對於每一個想要使用 doxygen syntax coloring 功能的檔案類型,做一個 soft link 如下:
    cd ~/.vim/after/syntax;
    ln -s doxygen.vim c.vim;
    ln -s doxygen.vim cpp.vim;
    ln -s doxygen.vim java.vim;
  5. 如果是在 Windows 下,那就在第四步時改用 copy。

有圖有真相:

Screenshot for doxygen syntax coloring in vim

另外,以下是看到的幾套,doxygen 整合其他 IDE 的 solutions:

  • Eclox - Eclox is a simple doxygen frontend plug-in for eclipse.
  • Doxbar - DoxBar is an add-in for using doxygen from within Developer Studio (VC6).
  • DoxyComment for Visual Studio 2005 - DoxyComment is an add-in for Visual Studio 2005, which can help you insert comment blocks in C/C++ code. Comment blocks are generated based on the current context of the cursor. The add-in integrates nicely into Visual Studio, so you can bind your favourite short-cut keys to its commands.

  1. 例如 JBuilder。