How to make Merlin (OCaml) ignore some lines? - emacs

I use Merlin with Emacs to edit OCaml code. It normally works perfectly fine, but I wound the following problem:
I'm need to use a package, built by someone else, that adds to OCaml some keywords not native to the language. Since I use the package to compile the code, compilation works great. On the other hand Merlin goes crazy and thinks that the new keywords are an error. Luckily the new keywords only appear at the beginning of a line, so my code looks something like this:
let square x = x * x;;
let rec fact x =
if x <= 1 then 1 else x * fact (x - 1);;
FOO "This syntax is not standard Ocaml" square fact;;
Where FOO is the new keyword. Merlin will complain and say Unbound constructor FOO. So the question is, can I make Marlin ignore that line? OR can you think of a hack to wrap the syntax in something Merlin won't complain about?

Merlin doesn't and will not (afaik) support arbitrary syntax extensions, but they do have parsing hacks for most commonly used camlp4 extensions like pa_lwt, pa_macro, etc. Also newest merlin versions will skip unknown lines and recover parsing, so that 'go-to-definition' and type throwback work on other parts of the file not modified by syntax extensions.

An alternative, although quite different, but that offers a subset of the functionalities of Merlin (completion and documentation from libraries, navigating around files), is ocp-index.
Ocp-index doesn't interpret or type your current file (it only scans it for opens and the like), so it won't be troubled by arbitrary extensions.

Related

What does ${plugin::command} mean in NSIS?

I'm trying to figure out how to modify an XML file with NSIS. So I'm trying to learn how to use the XML plugin. The examples on the forum page often use the format ${plugin::command} like:
${xml::LoadFile}
The documentation gives no indication that you need the dollar sign and curly braces. As I understand it, just plugin::command will do. So I've been trying to figure out what that syntax means.
The documentation says a $ is for variables and the {} are for code blocks, but I can't find anything about what it means when they're used together. My Internet searches have revealed that it's used for something called template literals in JavaScript. But what does it mean in NSIS?
EDIT: I should mention that the NSIS documentation does show examples of this syntax, especially in the Predefines section, but it still doesn't explain what the syntax means in general.
EDIT: Okay, now I see that the syntax is for the compiler to replace things using !define and !macro. But... what about this specific case? Is it valid to use colons in such a symbol? Why are some people writing ${xml::LoadFile}and some people just writing xml::LoadFile?
It's a !define. There is a header file for this plugin that defines it. The plugin probably needs to do more than one thing, so they wrapped a few lines together with a define that inserts a macro. Either that or it has some default parameters for the plugin call. Either way, it's trying to save you some typing with this syntax.

Folding of Scala imports in Vim

I'm trying to have my Vim setup automatically fold import statements when I open a Scala file. This is similar to what Intellij does when you open a file. I currently have the following lines in my .vimrc file from the wiki.
set foldmethod=syntax
set foldenable
syn region foldImports start=/(^\s*^import\) .\+/ end=/^\s*$/ transparent fold keepend
However when I open a .scala file it doesn't fold the imports but the body of objects. I am also using the vim-scala plugin. Thanks for the help!
You were pretty close to getting this to work. There are a few funky factors at play that we should consider.
setting foldmethod to syntax (btw this is not documented on learn Vimscript the Hardway..so :help foldmethod was key to figure this out)
SYNTAX fold-syntax
A fold is defined by syntax items that have the "fold" argument.
|:syn-fold|
The fold level is defined by nesting folds. The nesting of folds is
limited with 'foldnestmax'.
Be careful to specify proper syntax syncing. If this is not done
right, folds may differ from the displayed highlighting. This is
especially relevant when using patterns that match more than one line.
In case of doubt, try using brute-force syncing:
:syn sync fromstart
The main thing to note is the sync fromstart this is a useful helper if you have regex that would match throughout the file and only want to catch the header. In your case you should be able to ignore this but just something to be aware of.
top down regex scanning
Since the import block is fairly predictable we can simplify the start and end to look something like this:
syn region foldImports start="import" end=/import.*\n^$/ fold keepend
Since the region is just looking for some string to start the matching on we can just use "import"(or /import/) and then for the end value we want to use a little bit more carefully crafted statement. The key is that we want have the end be the last line of the import with a blank line following it (/import.*\n^$/)
Hopefully that does the trick for you (I do not work with scala so you may have to adjust the regex a bit as needed)

In a function show passed arguments in a different color [duplicate]

In Emacs, is it possible to mark all variables of different data types with different colors? e.g. if I have the following variables in C/C++ my program
int i,j;
float g,h;
char a,b;
Then throughout the source code i and j would be marked as red, g and h as green, a and b as blue.
I am not sure how useful this will be in future, but I feel it would help me while reading code,
and be a good alternative to the Hungarian notation(not that I use this notation :D).
No. Emacs has no idea about the type of a specific expression; doing this would be tantamount to writing a significant part of a C compiler in ELisp.
However, there is a light at the end of the tunnel.
E.g., if you edit OCaml code using tuareg-mode, you can ask Emacs about the type of any expression because the ocaml compiler provides that information; thus you should be able to ask it to highlight variables by type. This is the path to follow.
Alas, gcc does not provide that information; however, its extensiongccxml does.
Also, other C compilers, e.g., clang, provide that information out of the box, and there is a new file semantic-clang.el which relies on those features (although for completion only, not for syntax highlighting).
So, nothing out of the box for you here, but if you are willing to use clang instead of gcc and contribute to the CEDET development, you might get what you want.
No, it's not possible to selectively assign a given color to a given variable in emacs (or just for one given program).
However, if it's just syntax highlighting you are looking for, of course, emacs will highlight most languages, and you can even create syntax highlighting for languages emacs would not know about.
Ex. Smali: https://github.com/strazzere/Emacs-Smali

semantic in emacs does not find fopen

When I type 'f' in a C source file, my emacs with semantic installed and working
will show 25 completion matches, for functions like fclose, fread, and so on (only stdio.h
included)... but not fopen! Why?
Plus, same problem with opendir and readdir, with sys/types.h and dirent.h included.
P.S. semantic has all my sympathy, as upon my personal reading glibc header files showed
as a horrible mess of #ifnded and alikes.
'fopen' is turning up fine for me. Are you by any chance working on OS X? I remember that there were some preprocessor macros there which confused Semantic. Otherwise, your best chance for help is posting to the CEDET-devel mailing list, stating the CEDET version you're using and posting the exact definition of the 'fopen' function which apparently cannot be parsed.

Automatically replace certain text string as they are typed in emacs

There's a small nuance that's been bugging me for a while, namely that I frequently type #inclued instead of #include. If it wasn't obvious, I program a lot of C and C++. That typo has wrecked countless builds and consumed time that would have been better spent drinking coffee or surfing stackoverflow. Surely emacs can be helpful and rectify my mistakes as I type (in cc-mode only, of course). But how?
Googling and searching stackoverflow didn't provide any answers.
You could use this in abbrev-mode: After you entered #inclued, do C-x a i g include RET, and from then on, every time you type #inclued, it will be changed to #include automatically. If you want that abbrev to be local to a mode, use C-x a i l instead of C-x a i g. Also, you can edit your abbrevs with M-x edit-abbrevs.
A nicier and more global solution than abbrevs (because you can't predict all the typos you'll make) is to use flymake (which comes with emacs distribution).
http://flymake.sourceforge.net/
Flymake checks your source code behind the scene while you're still typing your code into the buffer. It highlights what's wrong with your code (that is : what gcc tells is wrong).
Running gcc in the background does not use more CPU than your antivirus bloatware. Moreover, if you have 2 or more cores, gcc can take advantage of parallelization. It only checks the syntax, not compiling anything.
if you're interested in something a bit different, you could write all your little piece into snippets, using the YAsnippet package, then you could type something like #in, hit TAB, and it will expand into... whatever you want.
Sounds like flymake is exactly what your after. It runs a compiler in the background, and will hightlight errors, as you type.
I was going to suggest that this could be a slightly odd application for flyspell, but danlei's answer looks better.