Vim syntax highlight function name and call in lisp-like language - vim

I'm trying to add syntax highlighting for function names in a small lisp-like language.
Functions are created using
(define my-function-name (n) (...))
and called by (my-function-name my-arg)
How can I highlight every instance of my-function-name as a special color.
It is safe to assume that the first term of an expression is always either a function call or a keyword.

Related

Vim: Substitute only in syntax-selected text areas

The exact problem: I have a source in C++ and I need to replace a symbol name to some other name. However, I need that this replace the symbol only, not accidentally the same looking word in comments or text in "".
The source information what particular language section it is, is enough defined in the syntax highlighting rules. I know they can fail sometimes, but let's state this isn't a problem. I need some way to walk through all found occurrences of the phrase, then check in which section it is found, and if it's text or comment, this phrase should be skipped. Otherwise the replacement should be done either immediately, or by asking first, depending on well known c flag.
What I imagine would be at least theoretically possible is:
Having a kinda "callback" when doing substitution (called for each phrase found, and requesting the answer whether to substitute or not), or extract the list of positions where the phrase has been found, then iterate through all of them
Extract the name of the current "hi-linked" syntax highlighting rule, which is used to color the text at given position
Is it at all possible within the current features of vim?
Yes, with a :help sub-replace-expression, you can evaluate arbitrary expressions in the replacement part of :substitute. Vim's synID() and synstack() functions allow you to get the current syntax element.
Luc Hermitte has an implementation that omits replacement inside strings, here. You can easily adapt this to your use case.
With the help of my ingo-library plugin, you can define a short predicate function, e.g. matching comments and constants (strings, numbers, etc.):
function! CommentOrConstant()
return ingo#syntaxitem#IsOnSyntax(getpos('.'), '^\%(Comment\|Constant\)$')
endfunction
My PatternsOnText plugin now provides a :SubstituteIf command that works like :substitute, but also takes a predicate expression. With that, it's very easy to do a replacement anywhere except in comments or constants:
:%SubstituteIf/pattern/replacement/g !CommentOrConstant()

How to color function call in Vim syntax highlighting

I'm new to Vim and curious how to highlight a function call after the function has been defined. As an example, in the SublimeText version, totalForArray is green when it is defined, as well as when it is called on line 12. This is what my Vim looks like imgur.com/q2WMQ4d, and I'm wondering how to make totalForArray highlighted when it's called.
An improvement on Vitor's regex matching.
This will highlight nested function calls while respecting highlighting for keywords like while, if, for, etc... and also allows for whitespace between function name and parenthesis
e.g. myFunction (int argc) { ... }
syn match dFunction "\zs\(\k\w*\)*\s*\ze("
hi link dFunction Function
Vim's syntax parsing usually only colors the function definition, because that one is easy to locate with a regular expression. For function calls, it would have to maintain a list of detected functions.
There are plugins that extend syntax highlighting with such a list, usually taken from the tags database. For example, the easytags.vim plugin performs automatic tags updates and can highlight those via the :HighlightTags command.
As a simpler alternative to what was proposed by #Ingo, you can also define a syntax to match any keyword that is directly followed by a parentheses:
syn match jsFunction "\<\k\+\ze("
hi link jsFunction Function
Searching in github I was also able to find the vim-javascript plugin, which appears to have various extensions to the default Javascript syntax included in Vim. In particular, it contains the following syntax definition:
syntax match jsFuncCall /\k\+\%(\s*(\)\#=/
This will implement the same syntax highlight I described before, but by using this plugin you might also benefit from other improvements that are included in it.

Emacs 24.1 & Haskell syntax highlighting of Prelude and commonly used functions

Another question on Emacs 24.1 and Haskell. I've noticed that it does indenting for me and it does very basic highlighting for me (types are in green, for example). But out-of-box Emacs 24.1 doesn't highlight commonly used functions like foldr, map, etc. Is there an ability for Emacs and haskell-mode to highlight commonly used functions?
Fundamentally, standard library functions are just that--functions. In fact, depending on your imports, any of them could be user-supplied rather than from the standard prelude! This actually happens often--for example, if you want to use Control.Category you usually hide id and replace it with a polymorphic version.
So in short, there is no real reason to highlight standard functions. So I really doubt this functionality is present in the standard Haskell mode.
That said, this is Emacs. You can easily add anything you want. If you have a list of all the function names you want to highlight, it should not be difficult to add that to the Haskell mode.
You can add your new functions to the haskell-mode highlighting with code something like this in your .emacs file:
(font-lock-add-keywords 'haskell-mode
'(("\\<\\(map\\|foldr\\|foldl\\)\\>" 1
'(:foreground "#3366FF") t)))
The weird looking string is an Emacs-style regular expression. \< and \> are like \b and \(, \| and \) are for alternation inside a group. Since there are no regex literals, every \ has to be escaped inside the string. The regex would be more readable as \<\(map\|foldr\|foldl\)\>. You can easily add other function names by adding new cases to the expression.
The (:foreground "#3366FF") just sets the color of the text to a rather fetching shade of blue.

Make vim close syntactic construction for code block automatically

Is there plug-in that makes vim close syntactic constructions that wraps code blocks automatically for any language based on data in indent and syntax files?
For example I editing lua file, when I write
function myfunction()
and press enter it automatically make an closing "end"
function myfunction()
<cursor position>
end
Or maybe anyone can show an example how to write your own plug-in for specific language with long keywords for code blocks (like lua, erlang or pascal) ?
Endwise seems to be what you're looking for. It already has lua support, and it looks like it would be pretty easy to extend, if you are comfortable with VimL.
I would use a snippet plugin like UltiSnips (my favorite) or Snipmate. They essentially expand some small word or piece of text into larger pieces of code and allow you to edit only the parts that are unique for each structure. Both UltiSnips and Snippmate include snippets for many languages and let you create your own snippets.
For example UltiSnips inclueds a function snippet for Lua. I just have to type out fun and then press tab and it gets expanded to
function new_function(args)
end
and new_fuctnion gets selected so I can edit the function name. Then once I press Ctrl-j (in my vimrc I rebind this to Tab) it selects the next part of the snippet which is args in this case allowing me to enter the function's arguments. A final Ctrl-j puts the cursor in the body of the function.

continuously execute an emacs lisp function

Is there a way to trigger the execution of an emacs lisp function other than M-x myfun? I would like to have the function re-called every time the buffer is changed.
Background: I have a table of numbers with some mistakes. The table has column totals and other features which can be used to identify the mistakes. My elisp function highlights suspicious columns of numbers. What I would like is for the highlighting to disappear as soon as the numbers are corrected, without repeated calling of the highlight-errors function.
The analogous feature in Excel is called, I believe, "conditional formatting"
The concept you're looking for in your first paragraphs is hooks. A hook variable is a list of functions that are executed when a certain event happens. Most hook variables have a name ending in -hook. The hook after-change-functions is executed each time you type something or otherwise change the buffer. Hooks are discussed in the Emacs Lisp manual under the heading "Hooks".
However, given what you're trying to do, it would be easier to use Emacs's highlighting mechanism. The solution may be as simple as adding a regexp in the right place.
Most files containing structured text (especially programming languages) are highlighted with the font locking mechanism. This is documented in both the Emacs and Emacs Lisp manuals under "Font Lock"; see in particular the function font-lock-add-keywords, for which the Emacs manual gives an example that is pretty much what you're after. There is also some information on the Emacs wiki.
ADDED:
Font lock can go beyond regexps; unfortunately the documentation is limited to the terse explanation in the docstring of font-lock-keywords. There are a few simple examples in cperl-mode.el (though they're somewhat buried in the mass). The wiki also references ctypes.el which uses this feature. Here is an example that highlights wrong integer additions.
(defun maybe-warn-about-addition ()
(let ((x (string-to-int (match-string 1)))
(y (string-to-int (match-string 2)))
(z (string-to-int (match-string 3))))
(if (/= (+ x y) z)
font-lock-warning-face)))
(font-lock-add-keywords
nil
'(("\\s-\\([0-9]+\\)\\s-*\\+\\s-*\\([0-9]+\\)\\s-*=\\s-*\\([0-9]+\\)\\s-"
(3 (maybe-warn-about-addition) t))))
Even the regexp can be replaced by arbitrary code that looks for the bounds of what you want to highlight (a function name as MATCHER, using the vocabulary from the docstring). There is an advanced example of font lock keywords in the standard C mode (cc-fonts.el).
Add your function to the variable after-change-functions.

Resources