Vim: Wrap a plugin mapping with my own expression - plugins

I have a plugin which has the following mapping:
:i <CR> <CR><Plug>DiscretionaryEnd
I'd like to also create my own expression for <CR> which falls through to this original mapping. However, when I do something like this:
:inoremap <expr> <CR> test ? '<CR><Plug>DiscretionaryEnd' : '<Esc>'
CR prints \n<Plug>DiscretionaryEnd rather than just \n followed by a delegation to the plugin's DiscretionaryEnd function.
:h <Plug> says it's used for internal mappings, so I'm probably going about this wrong. But I figure there's a way to wrap a plugin's behavior without diving into the internals of it. How should I do this?

Though it's generally recommended to use :noremap, because it makes the mapping immune to remapping and recursion, this is an exception to the rule.
In an expression mapping, the mapping applies to the expression itself, not the returned stream of characters.
:imap <expr> <CR> test ? '<CR><Plug>DiscretionaryEnd' : '<Esc>'

Related

Regex works in "Find" but not "Find in Files" - Why? How to fix?

From a previous question I have this nice regular expression:
\[.*\]\((?!http)(?!.+\.md).+\)
It matches all internal Markdown links that do not have a .md file extension. You can see some matches of it here: https://regex101.com/r/0uW1cl/6
This regex works just fine in an opened file via Edit -> Find (or Ctrl + F) when I enable "Use Regular Expression" in the find input field.
But it does not work in the Edit -> Find in Files functionality:
As you can see on the screenshot it gives me this nice error message:
Error parsing regex near `]\((?!http' at character offset 9: Unrecognized flag: '!'. (Allowed flags: i, m, s, U, u, x.)
Blockquote
As far as I know those use the same code behind the scenes to execute regular expression searches.
Why does it work in "Find", but not "Find in Files"?
Is there a way to fix this or a workaround?
(For now I am using Notepad++ which seems to handle this regex fine, but of course I would prefer to keep working in VS Code)
As far as I know those use the same code behind the scenes to execute regular expression searches.
Yeah, I shouldn't assume things like that:
Rust Regex in the Find/Replace in Files Sidebar
JavaScript Regex in the Find/Replace in File Widget
via https://stackoverflow.com/a/42184299/252627
And Rust Regex can not handle negative lookaheads as it doesn't support lookaround at all:
Its syntax is similar to Perl-style regular expressions, but lacks a few features like look around and backreferences. In exchange, all searches execute in linear time with respect to the size of the regular expression and search text.
https://doc.rust-lang.org/regex/regex/index.html

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)

Default regular expression for etags

I'd like to create additional regular expressions for use with etags, but I'd like them to be based upon what's already there (in particular, I'd like to add [ \t]* to the current set of regular expressions that etags uses). What are the default set of regular expressions that etags uses for .lisp files?
etags doesn't generally use regular expressions to implement the languages that are built-in. Instead, it has custom parser code for each built-in language.
That said, it seems for Lisp to look for just a few forms, that could be handled by regular expressions:
It looks for "(" in the first column
It skips defvar
It ignores a "cl-" or "CL-" appearing after the "("
Then, "def" and "DEF" match
It also checks for "(package::def" as a lead-in
I found this by reading lib-src/etags.c in the Emacs source tree. Search for "Lisp_functions".

Can the substitute-and-expand model of Scheme macros be improved?

As far as I know, the pattern&template-based Scheme macro system works by first pattern matching a macro invocation, obtaining a substitution in case of success, applying the resulted substitution to the corresponding template to build up a (maybe) partially-expanded expression, and then continuously expanding the resulted expression. If what I describe is true (please correct me otherwise), then it seems to me this building-up and expanding-again model is not efficient. Why does the expansion need to be done like this? Is it possible to finish the expansion by a run down the template once and for all?
Keep in mind that macros can be handled define time for procedures and compile time for a whole program.
Also, a macro expansion might turn into another (or similar) macro form that needs expanding. Eg. you can make a macro that ends up as a cond expression which of course is a macro for nested if expressions in most Schemes.
Have you seen Alexpander? It evaluates a program (in one expression) and returns an equal program without macros.
The semantics of the macro system are specified in the way you describe. However, implementations are free to implement that specification any way they want; in particular, they could "inline" macro expansions ahead of time to speed the process of macro expansion.
I'm not aware of any Scheme implementations that do what you describe, and I would guess it's because macro expansion is not usually a big bottleneck in compilation.

Comments for Function in Emacs

I'm looking for a way to generate and insert header comment blocks above my functions in Emacs (in any mode), with the default contents of the comment automatically based on the function's signature (i.e. the correct number of #param place-holders).
Doxymacs is a nice candidate. But I prefer another way works without the necessary libs. Can anyone recommend some others ways for adding smart comments for functions in Emacs? Thanks.
Edit:
Now I found this: http://nschum.de/src/emacs/doc-mode/, but it seems that it does not work well after I require it into my .emacs and add hook for js-mode. Doesn't it support js functions ?
I don't know of any general-purpose approach.
Csharp-mode has a defun that is bound to / , which tries to generate comments appropriate for C#. The way it works: Every time you type a slash, it looks to see if it is the third slash in a row. (In C#, three slashes are used to denote comments that produce documentation). If it is the third slash, then it looks at the surrounding text and inserts a comment skeleton or fragment that is appropriate.
It is not generalized in any way to support javascript or other language syntaxes. But you might be able to build what you want, if you start with that.
here's the excerpt:
http://pastebin.com/ATCustgi
I've used doxymacs in the past and I've found it useful
http://doxymacs.sourceforge.net/