In 2013, there was not a proper syntax highlighting for SPICE anywhere I looked, so I created one to fit my needs, for any text editors that have GtkSourceView↗ at the core of their highlighting. Now, 2019, the only other I see is on github, and looks decent. I had sent the one I made to the GtkSourceView team, maybe they would incorporate this in their release, but so far it's currently pending↗.
The reason I did it was because I wasn't very pleased with the way LTspice highlights the netlists. It does a crude job by only making the text blue for any lines beginning with a SPICE directive (dot), green (or dark green) for any line that is a comment (starts with *, ;, or #), red for any continued line (+ at the beginning), and black for the rest. It's not that it's not working, it's that it oversimplifies everything. Looking for variables is still a hunt for characters, finding a .model
is the same as finding a .tran
.
The way I did it emphasizes the elements by making them bold but only at the beginning of the line, variables are clearly marked with red, easily seen in any block of text, no matter how complicated the formula, functions too, subcircuits and models are now easily spotted by the bold red name, concatenated lines no longer look out of place but similar to .param
& co, while retaining the easy spotting of variables, and some SPICE directives differ than others by being italic, or not. All this without abusing colours, there are only black, red, blue, and green, LTspice's own colours, but they help now, not drown your retina. At least, these files help me, I hope they will serve others, as well.
The colour theme, though, is separately of the syntax file, but optional. If you want to feel closer to what you see in LTspice, use it, or not, it's your eyes. It's nothing much, really, just the Kate colour theme, modified and adapted.
Custom syntax highlighting | LTspice syntax highlighting |
---|---|
* /path/to/some/netlist.net -- first line comment
..params a1=2 c3 = f( a1 ) ; ..param ..params ,param are accepted, same for .func ,func _1#bla( x, y, z) { x+ y - 1/z } ; weird names that work ,param number 1 ; variable without equal, accepted, but only once per row, or first in a row Adevices {evaluated_net_name} 2 3 4 5 y x 0 AND td=1n ; all A-devices highlighted, BUF, OTA, VARISTOR, etc A12) 1 0 0 0 0 0 x 0 SCHMITT td=1n ref=0 ; improper naming Bsource {if( a, {a*b}, 2)} {a <=b } V=f(time) tripdt=1 tripdv = 1 ; weird spacing Capacitor#@^% 1 {if(a,1,2)} zzz 1.2 ic={_1#bla(1,2 3) } temp=-270.16 ; weird spacing&naming, but accepted Diode 1 {a-{b}} D m=1 n=2 ; curly braces within curly braces .model D D(Is=1f Cjo=1p) Esource 2 {a*1} table {v(a)} (1,1 2,2) ; old style E_1 2 {a*1} b 3 table (1,1 1) ; new style #E11 { a } { b } tbl{ 0, 0, 1, 1} ; # also allowed as commented line Find the source V1 {-theta} G_1 2 {a * 1} freq {v(b)} (1,1 1) (2,2,2) ; old style G@543 1 {1 * 2 } poly(2) {v(1),v(2)} 1 2 3 4 5 ; new style G_x 0 1 2 3 Laplace (s) ; Laplace, new style G_y 2 3 {s} {laplace} laplace = s^2+1 ; Laplace, old style H1 1 0 V1 1 I1 1 0 22222 J1 3 1 2 PJF K13 L1 L2 0.9 L2 1 2 {g(1,2*3,5)==1} Rpar=1 ; boolean is no assignment, lval not highlighted M1 3 1 2 2 PMOS .machine tripdt=1u ; state-machine differently highlighted than commands .state a0 0 .state a1 1 .rule a0 a1 v(x)>0.25 .rule a1 a0 v(x)<0.75 .output (y) state .endmachine .mach ; also accepted .output (z) 1 .endmach N7 1 2 3 4 dunnoifitexists ; for the future O1 1 0 2 0 LTRA Q1 3 1 2 0 NPN R1 1 0 R=time ; behavioural R2 0 1 {11*a} temp=0 ; normal S1 2 0 0 1 SW T1 1 0 2 0 Td=50n Z0=50 Uuu bo gus net URC W1 1 2 V1 CSW off V1 1 0 1 XU1 dut+ dut- bias resistance capacitance capometer current=1m freq=3Meg C=.5µ Q=.25 Ytline 1 2 3 4 Ymod ; see ltwiki.org Zmesfet a b c NMF .param a=1 b = a c = f(1) and=1 inv=1 ;allowed, space doesn't interfere in highlighting + x = 2 y= x *3 z =f( x** y ) ;continued line comment +dflop =1 _1= pi value=f(1) ; continued line without space +f(x,y)=2 ; not allowed, not highlighted .func f(x)=x**2 ; no longer allowed like this, function not highlighted .func world_domination(x,y,z) {table(x,y,1,z,2)} ; only the function handle is highlighted .lib path/to/some.lib .tran 0 1 0 1m steady startup nodiscard uic .op .four 1k 25 v(1) .ferret .global 1 999 .ic v(2)=2 .inc capometer.sub .loadbias ./load.bias .savebias ./save.bias .meas xxx param x .net v(1) v(2) .nodeset v(4)=1 .opt gshunt=1p ; all .opts reltol=1m ; three .option cshunt=1p numdgt=16 ; cases are detected .save v(2) .step param b 1 10 2 # name stands out, no need to go hunting .subckt @1xxx s[1] _1 2 a x1 x2 params: a=2 ; "params:" highlighted r1 1 2 {a} r2 2 0 2k ,ends @1xxx .subckt bla a b ,parma works=pi ; this also works Impossible current source {works} .ends bla .temp 10 20 .wave .\snd.wav 16 48k v(1) v(2) .backanno .end |
* /path/to/some/netlist.net -- first line comment
..params a1=2 c3 = f( a1 ) ; ..param ..params ,param are accepted, same for .func ,func _1#bla( x, y, z) { x+ y - 1/z } ; weird names that work ,param number 1 ; variable without equal, accepted, but only once per row, or first in a row Adevices {evaluated_net_name} 2 3 4 5 y x 0 AND td=1n ; all A-devices highlighted, BUF, OTA, VARISTOR, etc A12) 1 0 0 0 0 0 x 0 SCHMITT td=1n ref=0 ; improper naming Bsource {if( a, {a*b}, 2)} {a <=b } V=f(time) tripdt=1 tripdv = 1 ; weird spacing Capacitor#@^% 1 {if(a,1,2)} zzz 1.2 ic={_1#bla(1,2 3) } temp=-270.16 ; weird spacing&naming, but accepted Diode 1 {a-{b}} D m=1 n=2 ; curly braces within curly braces .model D D(Is=1f Cjo=1p) Esource 2 {a*1} table {v(a)} (1,1 2,2) ; old style E_1 2 {a*1} b 3 table (1,1 1) ; new style #E11 { a } { b } tbl{ 0, 0, 1, 1} ; # also allowed as commented line Find the source V1 {-theta} G_1 2 {a * 1} freq {v(b)} (1,1 1) (2,2,2) ; old style G@543 1 {1 * 2 } poly(2) {v(1),v(2)} 1 2 3 4 5 ; new style G_x 0 1 2 3 Laplace (s) ; Laplace, new style G_y 2 3 {s} {laplace} laplace = s^2+1 ; Laplace, old style H1 1 0 V1 1 I1 1 0 22222 J1 3 1 2 PJF K13 L1 L2 0.9 L2 1 2 {g(1,2*3,5)==1} Rpar=1 ; boolean is no assignment, lval not highlighted M1 3 1 2 2 PMOS .machine tripdt=1u ; state-machine differently highlighted than commands .state a0 0 .state a1 1 .rule a0 a1 v(x)>0.25 .rule a1 a0 v(x)<0.75 .output (y) state .endmachine .mach ; also accepted .output (z) 1 .endmach N7 1 2 3 4 dunnoifitexists ; for the future O1 1 0 2 0 LTRA Q1 3 1 2 0 NPN R1 1 0 R=time ; behavioural R2 0 1 {11*a} temp=0 ; normal S1 2 0 0 1 SW T1 1 0 2 0 Td=50n Z0=50 Uuu bo gus net URC W1 1 2 V1 CSW off V1 1 0 1 XU1 dut+ dut- bias resistance capacitance capometer current=1m freq=3Meg C=.5µ Q=.25 Ytline 1 2 3 4 Ymod ; see ltwiki.org Zmesfet a b c NMF .param a=1 b = a c = f(1) and=1 inv=1 ;allowed, space doesn't interfere in highlighting + x = 2 y= x *3 z =f( x** y ) ;continued line comment +dflop =1 _1= pi value=f(1) ; continued line without space +f(x,y)=2 ; not allowed, not highlighted .func f(x)=x**2 ; no longer allowed like this, function not highlighted .func world_domination(x,y,z) {table(x,y,1,z,2)} ; only the function handle is highlighted .lib path/to/some.lib .tran 0 1 0 1m steady startup nodiscard uic .op .four 1k 25 v(1) .ferret .global 1 999 .ic v(2)=2 .inc capometer.sub .loadbias ./load.bias .savebias ./save.bias .meas xxx param x .net v(1) v(2) .nodeset v(4)=1 .opt gshunt=1p ; all .opts reltol=1m ; three .option cshunt=1p numdgt=16 ; cases are detected .save v(2) .step param b 1 10 2 # name stands out, no need to go hunting .subckt @1xxx s[1] _1 2 a x1 x2 params: a=2 ; "params:" highlighted r1 1 2 {a} r2 2 0 2k ,ends @1xxx .subckt bla a b ,parma works=pi ; this also works Impossible current source {works} .ends bla .temp 10 20 .wave .\snd.wav 16 48k v(1) v(2) .backanno .end |
And this is the actual , netlist, I hope I didn't miss too many. Don't bother correcting possible mistakes, not only they're intended, but it's also a peacock-show.
It's not entirely perfect, as you an see above the Laplace is sometimes Laplace, poly should have been poly, and variables with no equal, for now, it's a problem↗.
The archive contains the language definition, spice.lang, and the colour theme, spice.xml, and they should be copied, or symlinked, or what have you, into (X represents the version):
After installing, the files with the extensions *.net, *.cir, *.sp, *.lib, *.sub and *.mod should be automatically recognized and the SPICE highlight mode will be used. If they aren't, selecting the SPICE highlighting will work. The colour theme, if desired, can be selected in the editor's preferences.