2009-06-05

Delphi Pascal/Object Pascal editing in Vim

Here are a few Vim plugins for Pascal editing.

Compiler
fpc.vim : "Free Pascal Compiler" compiler plugin
It can already be bundled in your Vim. If it is not, you can download it. After opening a .pas file, type
:compiler fpc
Compiler plugins are not loaded automatically in Vim. You'll have to load them explicitly.

Syntax
Syntax file for Borlands Delphi/Kylix
Vim 7 is already bundled with Pascal syntax plugin. But Object Pascal is more than that. To colorize the keywords of Object Pascal, you can use this plugin which is derived from the original Pascal syntax plugin.

Note that although this plugin is a syntax file, if you look in the script, you will see that it also contains color codes. That is why you will not see the color scheme you selected if you have this plugin. To overcome this issue, you can delete the following lines from the delphi.vim :


261 hi pascalObject          ctermfg=white         guifg=white         gui=italic
262 hi pascalFunction        ctermfg=LightRed      guifg=Orange        gui=NONE
263 hi pascalStatement       ctermfg=Green         guifg=Green         gui=NONE
264 hi pascalNumber          ctermfg=LightBlue     guifg=LightBlue     gui=NONE
265 hi pascalByte            ctermfg=LightRed      guifg=LightRed      gui=bold
266 hi pascalString          ctermfg=LightBlue     guifg=LightBlue     gui=NONE
267 hi pascalMatrixDelimiter ctermfg=lightred      guifg=lightred      gui=NONE
268 hi pascalConditional     ctermfg=Green         guifg=Green         gui=NONE
269 hi pascalConstant        ctermfg=white         guifg=white         gui=NONE
270 hi pascalModifier        ctermfg=Green         guifg=Green         gui=NONE
271 hi pascalType            ctermfg=white         guifg=white         gui=NONE
272 hi pascalStructure       ctermfg=Green         guifg=Green         gui=NONE
273 hi pascalRepeat          ctermfg=Green         guifg=Green         gui=NONE
274 hi pascalOperator        ctermfg=Green         guifg=Green         gui=NONE
275 hi pascalAssignment      ctermfg=Magenta       guifg=Magenta       gui=NONE
276 hi pascalComment         ctermfg=LightBlue     guifg=#00A0A0       gui=italic
277 hi pascalLabel           ctermfg=Green         guifg=Green         gui=NONE
278 hi pascalDelimiter       ctermfg=Yellow        guifg=Yellow        gui=NONE
279 hi pascalComparator      ctermfg=Yellow        guifg=Yellow        gui=NONE

I should remind you that the colors are not bad though. Delete them if you want to use the color scheme selected; otherwise leave it as it is, it just works.
If you are curious, here is the screenshot of the colors provided by the syntax plugin:


Also the author of this plugin provides some code to be put on your .vimrc and it is quite useful.
This is the code to be put on your .vimrc:
 1 " Pascal / Delphi
 2 if (1==1) "change to 1==0 to use original syntax
 3   au BufNewFile,BufRead *.pas,*.PAS set ft=delphi
 4 else
 5   au BufNewFile,BufRead *.pas,*.PAS set ft=pascal
 6 endif
 7 " Delphi project file
 8 au BufNewFile,BufRead *.dpr,*.DPR set ft=delphi
 9 " Delphi form file
10 au BufNewFile,BufRead *.dfm,*.DFM set ft=delphi
11 au BufNewFile,BufRead *.xfm,*.XFM set ft=delphi
12 " Delphi package file
13 au BufNewFile,BufRead *.dpk,*.DPK set ft=delphi
14 " Delphi .DOF file = INI file for MSDOS
15 au BufNewFile,BufRead *.dof,*.DOF set ft=dosini
16 au BufNewFile,BufRead *.kof,*.KOF set ft=dosini
17 au BufNewFile,BufRead *.dsk,*.DSK set ft=dosini
18 au BufNewFile,BufRead *.desk,*.DESK set ft=dosini
19 au BufNewFile,BufRead *.dti,*.DTI set ft=dosini
20 " Delphi .BPG = Makefile
21 au BufNewFile,BufRead *.bpg,*.BPG set ft=make|setlocal makeprg=make\ -f\ %

Color Schemes
Delphi color scheme

Turbo color scheme

Borland color scheme

2 comments:

  1. Here is a workaround, if you cannot figure how to load the plugin:
    1) Open vim.
    2) Edit a pascal source file in vim
    3) Type in ":scriptnames". -> A list of loaded plugins and their path appears.
    4) Highlight the path of the package most likely to be for pascal syntax and copy the path.
    5) sudo mv the original file somewhere else.
    6) Download the above file, and set a symbolic link from that file to the path
    and file name of the file you just moved away.
    7) Restart vim and edit a pascal source file. You should have nice syntax highlighting for Pascal sources now.

    ReplyDelete
  2. ":scriptnames" would list the files sourced order as I remember, so it will be useful.

    Also, "echo &ft" would be useful, to get the current file type.

    Thanks for your comment.

    ReplyDelete