0060. Vim syntax highlighting for ptsxpy

Getting started, ptsxpy, Python language, trueSpace, programming, syntax highlighting for text editor, etc.
Note: it's provisional and topics will be added and revised at random till some sort of documentations are prepared.
Post Reply
User avatar
3DfromNULL
Site Admin
Posts: 43
Joined: 2018-Jul-13 9:35

0060. Vim syntax highlighting for ptsxpy

Post by 3DfromNULL » 2021-Apr-21 9:28

This topic focuses on a text editor Vim. Just for your information if you use other editors.

Vim highlighting for ptsxpy scripts

The author mainly uses a text editor "Vim" to edit script files. It already has syntax-highlighting for Python, and I added ptsxpy specific highlighting setting to it.

Image

In example above, I am using "morning" color scheme (by specifying a line "colorscheme morning" to gvimrc file) and editing a script file with file extension ".py". According to the file extension and color scheme setting, Vim highlights words; comment is blue, string literal is pink, print is ocean green, reserved keyword is bold and dark red, etc.

However, Vim never knows about ptsxpy syntax and API functiopns. There are about 850 thru 1300 API functions (varies per tS version) defined in tsxAPIxx.doc and header files. I added a file in order to highlighte ptsxpy predefined classes, "p.xxxxxx", "ptsxgp.xxxxxx", etc. The code below is of the file "syn1.vim".

Code: Select all

sy match meth1 "\<p\>\.[a-zA-Z0-9_]*"
sy match meth2 "ptsxgp\.[a-zA-Z0-9_]*"
sy match pointer1 "\.p\>"

sy keyword	class1	Vec3f Vec2f Vec3fArray Axes3f Txmx3f BBox3f WidgetReg CollisionReport Quaternion ViewParam WDG_CALLBACKS Color SurfaceProps ProTexGranite ProTexMarble ProTexWood TextureProps BumpProps Txmx2f Rect FaceVx FaceVxArray Face FaceArray Hole UV Edge EdgeArray LatticeVector LatticeFloor MatRectProps DerivCurves SubDivEdge SubDivEdgeArray SubDivFace SubDivFaceArray PhysEnvir FloatColor RenderToFileData RenderData Image TextureFromLightingData String CharArray UcharArray Float FloatArray Double DoubleArray Long LongArray Ulong Short Ushort IntArray

sy keyword	class2	Vec3f_p Vec2f_p Vec3fArray_p Axes3f_p Txmx3f_p BBox3f_p WidgetReg_p CollisionReport_p Quaternion_p WDG_CALLBACKS_p Color_p SurfaceProps_p ProTexGranite_p ProTexMarble_p ProTexWood_p TextureProps_p BumpProps_p Txmx2f_p Rect_p FaceVx_p FaceVxArray_p Face_p FaceArray_p Hole_p UV_p Edge_p EdgeArray_p LatticeVector_p LatticeFloor_p MatRectProps_p DerivCurves_p SubDivEdge_p SubDivEdgeArray_p SubDivFace_p SubDivFaceArray_p PhysEnvir_p FloatColor_p RenderToFileData_p RenderData_p Image_p TextureFromLightingData_p String_p CharArray_p UcharArray_p Float_p FloatArray_p Double_p DoubleArray_p Long_p LongArray_p Ulong_p Short_p Ushort_p IntArray_p

sy match comma1 ","

hi meth1    guifg=#d25400 
hi meth2    guifg=#666633
hi class1   guifg=#006388 gui=bold
hi class2   guifg=#810083 gui=bold
hi pointer1 guibg=#e6c6ff
hi comma1   guibg=#cccc99 gui=bold
The file is written in the form that Vim knows. The leading "sy" determines syntax and the leading "hi" determines highlighting (foreground/background color, bold). The sy and hi are linked by name that I named;
  • methods of ptsxpy. It begins at "p." and ends at "(". Orange letters.
  • methods of ptsxgp. It begins at "ptsxgp." and ends at "(". Dark Orange letters.
  • ptsxpy predefined class. It has specific name. Sky Blue letters
  • inherited class for pointer. Its name is base class (i.e. parent's) name + "_p". Purple letters.
  • pointer member of ptsxpy predefined class instances. It is ".p". Light Mauve background.
I added highlighting for comma "," using the opportunity because the "foot" of comma is too small in the font I use for editing.

Usage:
  • Make a text file in any folder you like, and rename it to "syn1.vim". It's convenient to put it near your ptsxpy script files (e.g. parent folder of scripts). It's not recommended to put it in the Vim installed directory because you might forget that and do Vim update.
  • Copy & paste the above code (with sy and hi) to it.
  • Locate to the folder where your Vim was installed, and find a file "gvimrc".
  • Open gvimrc using your text editor. (Note: It's not vimrc but gvimrc. The g means "graphical" Vim.)
  • (option) Find a line of "colorscheme" and change its value to your favorite one if needed. (You can get a list from Vim menu - Edit - Color Scheme.)
  • Add a line to the last of vimrc as below. It lets Vim to read the syn1.vim when user open a file with file extension ".py". (Change c:/aaaa/bbbb into your path where your syn1.vim is.)

    Code: Select all

    autocmd BufReadPost *.py  :source c:/aaaa/bbbb/syn1.vim
    
Try your favorite colors by changing the lines of "hi". The format is #rrggbb where rr is red component, gg is green, bb is blue, and each value is 00 (min) thru ff (max) in hexa decimal.

Please note that this topic might not incorporate the latest information (especially not all class names might be listed in the code above). Change the code for highlighting if needed.

Vim highlighting for tsxAPI document

The author has 4 plain text files; apidoc43.txt, apidoc51.txt, apidoc60.txt, and apidoc66.txt. I copied all text from the tsxAPIxx.doc for each trueSpace version to corresponding file of them, and gave another line in my gvimrc in order to let Vim highlight tsxAPI fuctions;

Code: Select all

autocmd BufReadPost apidoc*.txt :source c:/cccc/dddd/syn2.vim
The code in the "syn2.vim" is;

Code: Select all

sy match tsx   "tsx[a-zA-Z0-9_]* *("
sy match syn   "SYNTAX"
hi tsx   gui=bold guifg=#ff6600
hi syn   gui=bold guifg=#00cc00
The code highlights words that starts at "tsx" and ends at parenthesis "(", and the word "SYNTAX";

Image

That is very convenient because I can search words in a moment unlike the original document.

For your information; Most parts of ptsxpy C++ source code (i.e. functions to bridge Python script and C++ API) and SQL for Syntax search page are generated from the four apidocX.txt using Cygwin + bash script + awk scripts + sed scripts.

Post Reply