vim-clojure-static, vim-clojure-highlight syntax updates

111 views
Skip to first unread message

guns

unread,
Apr 4, 2014, 12:21:11 AM4/4/14
to vimcl...@googlegroups.com
Hello everyone,

On browsing the source of TimL¹, I noticed that Tim Pope's syntax file
dynamically generated its core syntax matches from buffer variables in
order to quickly highlight local vars after evaluation.

This is such an awesome and obvious way of highlighting keywords that
I've changed vim-clojure-static to do the same. The README² has the
details, but here is a quick summary:

g:clojure_syntax_keywords
b:clojure_syntax_keywords

A dictionary of syntax-group to a list of symbol names. Symbols
listed here will be added to a syntax group when the syntax for
the file is reloaded.

The global version can contain symbols you always want
highlighted:

e.g.
let g:clojure_syntax_keywords = {
\ 'clojureMacro': ['defproject']
\ }

b:clojure_syntax_without_core_keywords

This is a boolean flag that causes the syntax script to skip
highlighting vars from clojure.core. Special forms and constants
remain highlighted.

The primary motivation behind these variables is to give plugins more
control over the syntax highlighting of a buffer. In particular:

a. Highlighting ClojureScript and other Clojure derivatives can now
be done (mostly) without forking the syntax script.

b. Namespaces that use :refer-clojure to create DSLs that clash
with clojure.core can be accurately highlighted. core.matrix,
core.logic, and David Greenberg's piplin come to mind.

The latter point is now a feature of vim-clojure-highlight³, so if
you have ever felt bothered by extraneous highlighting of vars you
specifically excluded, give it a try!

Unfortunately, there is no easy way to hook into Fireplace's eval
to change the highlighting after eval like in TimL. There are two
solutions:

1. Send a patch to fireplace.vim that adds a custom EvalPost event
that we can hook into.

2. Define your own mappings with :ClojureHighlightReferences. For
instance, if you use `cpr` to re-evaluate a buffer:

augroup my_clojure_autocmds
autocmd!
autocmd FileType clojure
\ nnoremap <silent><buffer> cpr :Require \| ClojureHighlightReferences<CR>
augroup END

Thanks to everyone who's contributed to vim-clojure-static¹, and thanks
to Tim Pope for providing the inspiration for these changes.

Happy hacking,
guns

¹ https://github.com/tpope/timl
² https://github.com/guns/vim-clojure-static#syntax-options
³ https://github.com/guns/vim-clojure-highlight
⁴ There have been a steady stream of improvements over the past year:
https://github.com/guns/vim-clojure-static/issues?state=closed

Oskar Kvist

unread,
May 3, 2014, 8:17:31 AM5/3/14
to vimcl...@googlegroups.com
Hi guns!

Sounds awesome! However, vim-clojure-highlight doesn't seem to work at all for me. I should just install it, right? The readme doesn't mention anything else.

I got the 3 plugins (fireplace, static, highlight), started a repl, and opened up my files in vim. No extra color from vim-clojure-highlight. What am I doing wrong?

Oskar Kvist

unread,
May 3, 2014, 9:50:42 AM5/3/14
to vimcl...@googlegroups.com
Hm... I went to make lunch, without closing vim. And when I came back, the next time I opened a file, it worked. :p

Oskar Kvist

unread,
May 3, 2014, 10:31:38 AM5/3/14
to vimcl...@googlegroups.com
Oh, turns out I didn't read correctly: "after evaluation".

Btw, is there a way to make everything that start with a given prefix belong to a specific syntax group? For example I want all my `defwhatever` do be clojureDefines and not clojureMacro. :P

Oskar Kvist

unread,
May 3, 2014, 11:54:48 AM5/3/14
to vimcl...@googlegroups.com
Hi again! :p

Just some thoughts:

I like to highlight private vars too (but you said you didn't[1]) so I changed to (ns-interns ns) instead of (ns-publics ns) in ns-syntax-command. 

Also, for now, I added the line
`(.startsWith (str (:name m)) "def") "clojureDefine"`
in the cond of var-type, to satisfy my desire to always make everything that starts with def a clojureDefine. But one could use .matches of java.lang.String and use some kind of variables there to make it more customizable.

And yes, it would be nice to have an EvalPost event. :P

With my own custom changes, I'm happy with my highlighting for now. :)

Good work! Thanks!




guns

unread,
May 7, 2014, 4:07:42 PM5/7/14
to Oskar Kvist, vimcl...@googlegroups.com
On Sat 3 May 2014 at 08:54:48AM -0700, Oskar Kvist wrote:

> Also, for now, I added the line `(.startsWith (str (:name m)) "def")
> "clojureDefine"` in the cond of var-type, to satisfy my desire to
> always make everything that starts with def a clojureDefine.

My colorscheme doesn't actually differentiate between clojureDefine and
clojureMacro, so I suppose that's why this never occured to me!

I would worry about false positives however. For instance, in
vim-clojure-static ftplugin:

" There will be false positives, but this is better than missing the
" whole set of user-defined def* definitions.
setlocal define=\\v[(/]def(ault)@!\\S*

(ault) is excluded because of clojure.core/default-data-readers.
Implementing a whitelist with a single regular expression is never much
fun.

guns
Reply all
Reply to author
Forward
0 new messages