User:Marshall Lochbaum/Dynamic Syntax Highlighting

From J Wiki
Jump to navigation Jump to search

This is a project to make vim highlight J names correctly in a file, under Linux. The code defines a vim command :Jsyn to run the current script in a J console, find all the current names, and add them to vim's syntax file for J. The current version works only for names which are defined in the path of the base locale after running the script. It does not work on local definitions within the script.

This script requires a .vim file defining J's syntax highlighting. I use the one in Guides/Syntax Coloring.

The main portion of this script is a J script which runs from the console, loads its console argument, and changes j.vim according to the names defined after that is run. jconsole and j.vim are hard-coded and need to be changed.

#! /home/marshall/j701/bin/jconsole

load 2{::ARGV

cocurrent 'pvim'

file =: '/home/marshall/.vim/syntax/j.vim'
lines =: <;._2 (1!:1) <file

insert =: 1 :(':';'(m&{. , x , m&}.) y')
getnames =: ([:". 'nl_',>@[,'_ ',":@])"0 _(<@)(;@:)

comments =: (<;._2) 0 :0
" Automatically generated names.
" End automatically generated names.
)

NB. If there is currently a generated names
NB. section, remove it.
3 :0 ''
while. *./comments e. lines do.
  ind =: lines i. comments
  NB. end before start
  if. >:/ ind do. return. end.
  lines =: lines (({.~{.) , (}.~>:@{:)) ind
  NB. remove empty lines after the block
  i=.{.ind
  lines =: (#~ (i#1) , ([:-.@:(*./\) a:&=)@:(i&}.)) lines
end.
)

types =. ;: 'noun adverb conjunction verb'
vimdecl =. (25 {. 'syn keyword j',toupper@{.,}.)&.> types
names =. <@getnames"_ 0&(i.4) (,copath) <'base'
add =. vimdecl ,&.> ;:^:_1&.> names
add =. a:,({.comments),add,({:comments)
n =. {:@I. ('syn match '&([-:#@[{.]))@> lines
file (1!:2<)~ ;,&LF&.> add (>:n) insert lines

2!:55 ''

Then edit your .vimrc (or vim plugin for J) to include the following line, replacing /home/marshall/.vim/editjsyntax with the appropriate path:

command Jsyn :exec " :!/home/marshall/.vim/editjsyntax %:p" | :e

Contributed by Marshall Lochbaum.