Syntax highlighting (how to do like emacs ?)

24 views
Skip to first unread message

Soo

unread,
Nov 4, 2009, 8:02:40 PM11/4/09
to vim_use
Hi all,
(I'm french, so excuse me for my english level :)
In emacs, when you write a syntax highlighting script you can do
something like this :
("[a-z]+[a-z0-9_]*" 0 (some-function) nil)
Indeed, when a syntax pattern matched (here "[a-z]+[a-z0-9_]*") you
can call a function (here "some-function") to handle this syntax item.
But how to do the same in Vim ?
something like :
highlight match some-group "[a-z]\+[a-z0-9_]*" call some-function
I search about it since very very long time, and i have no idea how to
do it

Thanks

Ben Fritz

unread,
Nov 5, 2009, 11:42:58 AM11/5/09
to vim_use
Vim's syntax highlighting functionality does not support calling
functions upon a match.

You might be able to write a plugin that uses CursorHold or even
CursorMoved, and potentially InsertLeave as well, that would run a :g
command to call the desired function. However, such a method would
scale even worse than normal syntax highlighting. What do you desire
this feature for? I.e., what task are you trying to accomplish with
this feature? There might be a better way than calling a function in a
syntax script.

Soo

unread,
Nov 5, 2009, 12:06:35 PM11/5/09
to vim_use
I have to write a syntax highligthing script for a new language. And
there is a great difficulty :
You have to recognize identifiers with this regex : "[a-z][a-z0-9_]*"
the difficulty resides in the fact that :
- You have two kind of identifiers : global and local
- All global identifiers have to be colored
- All local identifiers, and their occurences have NOT to be colored
- All global identifiers recognized within a function body have to be
colored *

There is an example written in this new language called Lisaac (http://
www.lisaac.org).
Please take care of comment to understand my problem :

Section Header

+ name := FILE_SYSTEM;

- copyright := "2003-2005 Jérome Boutet, 2003-2007 Benoit
Sonntag";

- comment := "File System manager for Unix.";

- external := `#include <unistd.h>`;

// In the above code "name", "copyright", "comment" and "external"
have to be colored

Section Inherit

// "parent_directory" has to be colored

+ parent_directory:DIRECTORY <-
(

// "cwd" and "result" have NOT to be colored because they are local
declaration
+ cwd:NATIVE_ARRAY(CHARACTER);
+ result:DIRECTORY;

// In the above code all occurences of "cwd" and "result" have NOT to
be colored because they have a local declaration
DIRECTORY.string_tmp.clear;
cwd := DIRECTORY.string_tmp.to_external;
`getcwd(@cwd,255)`;
DIRECTORY.string_tmp.from_external cwd;

result ?= DIRECTORY_UNIX.physical_get_entry
(DIRECTORY.string_tmp);
DIRECTORY.alias.put result to (result.path);
? {result != NULL};

// "parent_directory" have to be colored be cause it has a global
declaration in the above code
parent_directory := result // Parent is a data now!
);

So you see that all theses identifiers matches the regex but they have
or have not to be colored depending
on their declaration context
The use of a function like emacs permit us to handle each identifiers
differently depending on its context.
So i have no idea to achieve this feature !

Charles Campbell

unread,
Nov 5, 2009, 12:42:14 PM11/5/09
to vim...@googlegroups.com
Soo wrote:
> [snip]

> So you see that all theses identifiers matches the regex but they have
> or have not to be colored depending
> on their declaration context
> The use of a function like emacs permit us to handle each identifiers
> differently depending on its context.
> So i have no idea to achieve this feature !
>
There are some plugins that use ctags to build special syntax
highlighting rules.
As an example of one such, see Al Budden's script:
http://www.vim.org/scripts/script.php?script_id=2646 .

Although you can't call a function based on a match, what you can do is
preprocess the file to extract the
global variable names and manipulate that list into syntax keywords.

Regards,
Chip Campbell

Farzad Sehat

unread,
Nov 5, 2009, 12:50:51 PM11/5/09
to vim...@googlegroups.com
2009/11/5 Charles Campbell <Charles.E...@nasa.gov>
The problem is that when using ctags and other a priori code analyses you cannot
color syntax item which are being written by the user at realtime.
Suppose the user open a new file and declare some new global identifier and then he uses
this identifier in the rest of the code. We cannot recognize it just by using ctags, isn't it ?

Regards
Farzad Sehat

Charles Campbell

unread,
Nov 5, 2009, 12:56:54 PM11/5/09
to vim...@googlegroups.com
Farzad Sehat wrote:
>
> The problem is that when using ctags and other a priori code analyses
> you cannot
> color syntax item which are being written by the user at realtime.
> Suppose the user open a new file and declare some new global
> identifier and then he uses
> this identifier in the rest of the code. We cannot recognize it just
> by using ctags, isn't it ?
Well, actually one can do so. Using autocmd events (CursorHold,
CursorMoved, CursorMovedI),
have vim modify the syntax recognition rules (perhaps using ctags,
perhaps something else).

Regards,
Chip Campbell

Reply all
Reply to author
Forward
0 new messages