syntax highlighting for Assembler - vim

I need to add support for assembler language I'm working with (it is not x86, 68K, or 8051 which are well supported by vim). I looked at the existing syntax files, and here are my questions
1) When does it really make sense to use syn keyword and syn match? My understanding is that the latter supports regex and gives more flexibility. On the other way, looking at /usr/share/vim/vim70/syntax/asmh8300.vim - they define opcodes in both keyword and match, what benefit does it really give?
2) Instructions in my Asm have a common format:
INSTR OP1, OP2 ..; i.e. space delimits the instruction name from operands.
I think for this I'm ok with only defining all Asm commands in 'keyword' since space symbol is by default in 'iskeyword'. Am I right ?
3) The Asm also supports C-style structures, enums and comments. Can I just borrow its syntax definition from c.vim or it won't work and requires some tweaking?

If you have a limited set of identifiers, and if they all consist solely of keyword characters, then :syn keyword is the best choice. You're right in that :syn match provides a superset of functionality. Essentially,
:syn keyword myGroup foobar
is equivalent to
:syn match myGroup "\<foobar\>"
Beware of old versions
The syntax/asmh8300.vim syntax you've referenced is from 2002, it may not be the best example of how to write a syntax file. (For example, it omits the \<...\> around its matches, what looks like a bug to me. And it still has compatibility stuff for Vim 5 / 6 that's not needed any more.)
Also, do you actually use Vim 7.0?! Vim 7.0 is from 2007 and very outdated. It should be possible to install the latest version 7.3; if you can't find a proper package for your distribution (for Windows, check the binaries from the Cream project, it's also not very difficult to compile (e.g. from the Mercurial sources) on Linux.
Borrow other syntax elements
If other syntaxes are embedded in your syntax, and clearly delimited (e.g. like JavaScript inside HTML), you can :syn include it. But if there are just similar constructs, it's best to copy-and-paste them into your syntax (and adapt at least the group names). You need to be careful to catch all contained syntax groups, too; together with syntax clusters, the hierarchy can be quite complex!
More tips
When writing a syntax, you often need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

Related

Vim generate text expansion from shortcut

The codebase I'm working on requires standard comments around any modifications that we make:
// -----------ABCD---------------
and then
// ----------\ABCD---------------
What's the best way to map a short combination of keys like -abcd and \abcd to generate these longer strings?
The simplest, built-in :help abbreviations:
inoreab abcd // -----------ABCD---------------
inoreab abcD // ----------\ABCD---------------
Why did I choose different triggers? There are some rules (documented as "three types of abbreviations" under the above help link) for the allowed keys; the easiest is that it's all keyword characters.
If you insist on those exact triggers, you'd have to fiddle with the 'iskeyword' option (which can affect syntax highlighting and motions!), or switch to :inoremap. The downside of that is that you won't see the typed characters until any ambiguity has been resolved (try it; you'll see what I mean).
ABCD is just a dummy; I need dynamic text here
If multiple abbreviations won't do, you'll need snippets.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki and this comparison by Mark Weber.
A snippet would look like this (snipMate syntax):
snippet abcd
// -----------${1:ABCD}---------------
${2:content}
// ----------\$1---------------
${3}

vim highlighting: remove syntax keyword

One of the vim plugins I use does:
syn keyword rustTodo contained TODO FIXME XXX NB NOTE
Resulting in highlighting NB in comments, which I don't like. Is there a way to either redefine the keywords or remove one from them? Looking at :help syn-keyword makes me think that this is not possible.
If that is the only definition for rustTodo (these are cummulative), you can remove and then redefine it:
syn clear rustTodo
syn keyword rustTodo contained TODO FIXME XXX NOTE
Unfortunately, the granularity for removal of syntax items is limited to whole syntax groups (here : rustTodo); you cannot pick individual keywords unless they also have separate groups (which would result in much more linking of highlight groups and therefore inefficient).
To make this permanent, put it into ~/.vim/after/syntax/rust.vim
If you think that the majority of users doesn't like NB here, please suggest to the author to drop it. Adding it back as a personal customization is easier and more maintainable than removing it...
I had a similar question while importing syntax files from within other syntax files, and came up with a dirty hack.
Instead of loading your syntax file with
syn include #othersyntax syntax/othersyntax.vim
you can use
syn include #othersyntax !sed 's/<NB>//' syntax/othersyntax.vim
Eww. This brings serious platform dependency and security issues, but at least relieves you of the burden to keep the replacement keyword list in sync with upstream.

Vim recipe for a minor syntax highlighting extension

I want to leave my system's Python syntax highlighting mostly intact, but I have a specific pattern I'd like to highlight for an idiom I use a lot. How can I add additional highlighting instructions on top of the existing highlighting done by vim?
(Apologies if this has already been asked. All the vim syntax highlighting questions I found seemed to involve writing a new syntax highlighting from scratch.)
Put your additional :syntax commands into ~/.vim/after/syntax/python.vim, and they will be automatically executed after the original syntax script.
It's easy to highlight stuff that so far isn't parsed at all.
For elements already parsed / hightlighted, you need to find out by which syntax group (e.g. pythonFunction), and add a containedin=pythonFunction clause to your :syntax commands. Without that, the original matching will obscure yours. To find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
Introducing highlighting across (larger) elements that have multiple existing syntax groups is difficult, as your match will obscure the original ones, and that may break the entire parsing. You need to carefully examine the existing nested element structure, and try to fit in yours, again via contains= and containedin= clauses. Depending on the actual situation, that can be difficult.
For the actual syntax definitions, see the help starting at :h :syn-keyword. Basically, there are simple keyword definitions, regular expression matches, and regions defined by start and end patterns.

removing spell syntax highlighting in vim

This question is a sequel question of this one. I have the following script that removes capitalized words from vim spell check.
syn match myExCapitalWords +\<\w*[A-Z]\K*\>+ contains=#NoSpell
But it works only if I do syn clear first. But then all other highlighting (e.g. markdown) gets lost. I went through syn list to see what might be causing the conflict, but now I am out of clue.
It looks like you're extending arbitrary syntaxes with your myExCapitalWords group. Whether / in which syntax items that works depends on the underlying syntax. Unfortunately, it is not possible to extend arbitrary syntaxes in a blanket way. That's why you're seeing problems that can only be solved via :syn clear (which gets rid of the underlying syntax).
A syntax contains multiple groups, some of which are usually contained= in others. If you introduce a new syntax, it will only apply to where no other syntax group already matches. You can force your group into others via containedin=TOP or even containedin=ALL, but that overlay may prevent other original groups from matching, and causes strange effects because their own contains= or nextgroup= now don't apply.
So, unfortunately, there's no general solution for this. If you're only interested in a few syntaxes, you can tweak your one-liner to make it cooperate with the underlying syntax (e.g. try containedin={syntaxName}Comment{s}), but there's no generally applicable solution.

Vim improperly highlights "very magic" and "very nomagic" regular expressions

I'm editing a .vim syntax file in vim itself, and I'm using "very magic" and "very nomagic" regexes, simply because I think they make more sense. Unfortunately, vim does not in any way highlight these correctly. For example, this regex, using very nomagic:
/\V[/
should match an open bracket. Unfortunately, vim thinks this is an unterminated collection, and so it floods the file with purple highlighting until it finds the next ].
Is there any way to get vim to properly highlight these regular expression modes?
You'd have to override some of the default syntax/vim.vim syntax definitions (in ~/.vim/after/syntax/vim.vim), because the syntax doesn't consider this case, and assumes the default 'magic' regular expression syntax.
I guess the author of this syntax, Chip Campbell, didn't want to further complicate the (already quite large) syntax, and avoid the increased testing effort. It's certainly a good idea to ask him about including support for the \V / \M atoms, or whether he at least would accept a patch to include such an enhancement.

Resources