I'm using modern fortran for doing parallel programming. I'm using vim and it's been really annoying me that the fortran.vim syntax files don't seem to handle compiler directives like !$omp or !dir$. These just get rendered as comments in vim so they don't stand out. In c/c++ these compiler directives are done using #pragma's so everything stands out like it were preprocessor code rather than comment code. So I want similar treatment in my fortran syntax.
Here's an example of a multiline directive that I want to colour:
!$omp parallel do reduction(+: sum0) reduction(+: sum1) &
private( nn, S1, S2, Y1, Y2, rvec0, rvec1, iThreadNum)
What I have so far is a new fortran.vim file located in $HOME/.vim/after/syntax.
I've got it to recognise the '!$omp' at the start of a line and to colour that line and also to colour the multilines properly. My syntax file contains this:
syn region fortranDirective start=/!$omp.*/ end=/[^\&]$/
hi def link fortranDirective PreProc
My problem is that it now can't handle the simple case of just a single line. I.e:
!$omp parallel do blah blah
call foobar <-- this is coloured the same as the line above
I need some kind of regex rule in my syntax file to be able to correctly match both single line and continued line. Can anybody help please?
As far as I can tell, the problem is that your start regex is too greedy.
This should work:
syn region fortranDirective start=/!$omp.\{-}/ end=/[^\&]$/
Related
When I generate an .ml file with ocamllex it has a bunch of lines like:
# 21 "lib/myproj/example.ml"
These look like comments, except AFAIK comments in OCaml are like (* this is a comment *)
VS Code doesn't seem to treat them as comments either.
What syntax is this? What do they do?
These are "line number directives":
https://ocaml.org/manual/lex.html#sss:lex-linedir
Preprocessors that generate OCaml source code can insert line number directives in their output so that error messages produced by the compiler contain line numbers and file names referring to the source file before preprocessing, instead of after preprocessing.
Vim supports spell-check only in comments section already, however, if I have a type name or something not a regular word, it will consider it as a typo. For instance, in the following example, std::endl will be highlighted as typo.
// Don't use std::endl, it will flush unnecessarily
I wish we could use `` to escape them like following.
// Don't use `std::endl`, it will flush unnecessarily
Is there any tips or solution for this besides adding everything into dictionary?
I really don't want to disable spell-check due to this, so any help is greatly appreciated.
Thank you!
You can use this syntax rule to create a new group matching a `...` block and disable spelling inside those blocks:
syntax region cCommentNoSpell start=+`+ end=+`+
\ contained containedin=cComment,cCommentL transparent
\ contains=#NoSpell
To load this for cpp and c files, add this line (by itself) to a file ~/.vim/after/syntax/c.vim, so it is loaded after the system syntax files for C++ and C. (The cpp syntax rule includes all syntax for c so you'll get it on cpp too.)
The syntax rule uses ` as both start and ending delimiter.
It uses contained and containedin to only match inside comments. The cComment rule matches traditional multi-line /* ... */ comments and cCommentL matches single-line // ... comments. (Both are defined in the syntax file for C and C++ shipped with Vim.)
The transparent attribute instructs it not to use this syntax rule as a highlighting group, so it keeps the normal highlighting for comments in the parts matched by this rule.
Finally, contains=#NoSpell is what disables spelling on the regions that match this rule. See :help spell-syntax for more details on how spelling works together with syntax highlighting.
There was a very useful answer on how to highlight openmp directives in Fortran code (Vim syntax highlighting for multiline fortran openmp directives).
So lines like
!$omp parallel
are no longer highlighted as comments.
It would be great also to make vim not to treat as comments
lines starting with "!$", i.e. in constructs like
! Make it compile both with and without OMP
nThreads = 1
!$ nThreads = omp_get_num_threads()
I want to have !$ highlighted as fortrandirective,
and the rest of the last line highlighted normally.
You could use syn match for this:
:syn match fortranDirective "\v!\$\s"
This matches !$ with a trailing whitespace (to distinguish it from !$omp).
Is there a way to highlight built in Python functions in vim only when they are preceded by 1 more whitespaces? Furthermore, is there a modular way to do this? That is, I don't want to edit every single syn keyword pythonBuiltinFunc abs chr ... line, I just want to be able to say something like syn keyword pythonBuiltinFunc onlymatchafter="\s+"?
EDIT:
Here's an example, since the two people who answered my question didn't seem to understand what I was asking which is my fault for not being more clear.
When I write the following Python code
import numpy as np
x = np.abs(np.random.randn(10, 10))
The word abs gets highlighted simply because vim is essentially just matching anything that has the word abs in it that is not inside of a string. How can I get vim to highlight the Python builtins WITHOUT highlighting them when they are preceded by a dot?
The matched text of :syn keyword can only be comprised of keyword characters; though that set can be configured (:setlocal iskeyword=...), it would be foolish to include whitespace in there.
You have two options: Either re-write all keywords with :syn match (which can include whitespace), or make all keywords contained and define a :syn region that only starts after whitespace.
Both are rather huge interventions that basically mean you're (re-)writing your own Python syntax. You haven't told us why you'd want that... I'd say it's a bad idea.
Is the reason you want to do this because you want to remind yourself of using a consistent style in Python?
If so, I would like to recommend you adding the syntastic plugin. In combination with a tool such as flake8 it automatically detects and highlights any style errors you wish (by default PEP8 style violations), together with general syntax errors. I use this plugin a lot and highly recommend it.
I'm writing a tutorial/book with Vim, and I'd like to turn off the syntax highlighting for a block of text (the normal text of the book) and reactivate it for the code examples.
I have Googled to no end, but I cannot find a simple solution. Am I missing something? Or is this not achievable in Vim?
I have written the SyntaxRange plugin for that (my main use case is highlighting patches inside emails as with the "diff" syntax). With it, you can :[range]SyntaxIgnore or :[range]SyntaxInclude {filetype} certain sections of a buffer, or, when the sections start and end with certain markers, define dynamic sections that adapt when the number of lines change.
You can create a syntax file for your book.
For example, you can create a script: ~/.vim/syntax/tutor.vim
"
" tutor.vim -- syntax file for my book/tutor
"
syn include #PY $VIMRUNTIME/syntax/python.vim
syn region pyBlock start="{{{" end="}}}" contains=#PY
This is a sample file:
# File: intro.txt
# Date: 2012-08-19
blah, blah ...
So, I will show you some code:
{{{
def hello():
print "world"
}}}
# vim: set syn=tutor :
Thinking tangentially...
How about using something like restructured text or markdown within Vim and render on github. This gives you version management for free. You can have code-blocks.