vim/neovim fold: Avoid cascaded fold when fold all - neovim

When reviewing some code, I always enjoy the folding feature of vim/nvim. I set foldmethod=intend, and that works pretty well except the following case:
When it comes to "close all folds", which is triggered by zM, there will be meaningless cascaded folds under the lines which have a high indentation while the surrounding indentation is low. For example,
def a_very_long_function(arg1
arg2
arg3
):
pass
Then when I press zM, I will need to press za many times to unfold the args. I thought foldexp might solve this problem, but I have no idea how to write a suitable function. Cause I use nvim, I also tried the pretty-fold plugin but it doesn't seem to have a concern of my issue. I also notice the intendexp option, but I don't know if it's the best practice. Is there anything else could solve the problem or there's a existing feature which could avoid this?
What I want is When I unfold after close all folds, there isn't any meaningless cascaded folds which needs to be unfold many times.

Related

VS Code: Autofill (in Scala / Metals) forces the cursor to jump *after* the parentheses

I don't think this is just Scala, or the Metals plugin. I remember this happening with Python, too, but my current situation is with Scala.
I really like using autofill and suggestions; it's my favorite part of any IDE, including VS Code.
VS Code autofill works great, it's just really frustrating where it puts the cursor after autofilling. And I feel like I must be doing something wrong, since I couldn't find any search of others having this problem.
Example:
Below I am writing println, just as a dummy, but it happens with any autofill. I type enough so that I can see the autocomplete options appear.
Then I hit to autocomplete on the highlighted option, and it fills with the cursor after the closing parenthesis:
But, of course, when I autofill, I want the cursor to be inside the parentheses, not after them.
What can I do to get autocomplete to be more friendly in VS Code?
Argh!! I eventually figured out what was happening, and why it seemed so inconsistent:
If I choose an autofilled function that requires no arguments, then it will autofill and place the cursor at the end of the parentheses. If I choose one that does require arguments, then it will go inside the parentheses.
That makes sense, except that there are several functions which can be used without arguments, but rarely are, and VS Code will always put the no-argument case as the top choice. So, if you see the top one and are "happy" with it and choose it, the cursor will jump to the end.
As you can see above, there is a no argument println() which is the top of the autofill list. Not ideal, but at least now I understand what was going on. Not sure of a fast way to fix it, since again, this is something that I will be typing a bunch. Probably just avoiding autofill will be fastest. :(

Stuck at "Retracting buffer" after type class resolution diverges

When using typeclasses, Coq sometimes diverges. When this happens under Proof General, Emacs also hangs.
The best way I know to recover is to interrupt emacs (C-g) and restart Coq (C-c C-x). But, instead of just killing the Coq process, this puts me in a mode where Emacs is again hung, displaying "Retracting buffer..." for a quite long time (sometimes so long that I give up and just start a whole new emacs!).
Why?
Is there a better way? (And: Is there an easy first step for viewing what is diverging in the first place when this happens?)
If emacs is hung on anything other than garbage collection, you can generally interrupt it with C-g. If C-c C-x / C-c C-c don't work, you can kill the coqtop process from process manager / from the command line with pkill coqtop.
This happens because coqtop has some loops where it does not check for interrupt, or does not check often enough, and so it does not catch the interrupt from PG in a timely manner.
Additionally, as mentioned in other answers, Set Typeclasses Debug will give you a very verbose log of typeclass resolution in the *response* and *coq* buffers.
I don't know if this actually does anything, but sometimes I find that Proof General interrupts Coq if I press C-c C-c a few times.
As for your second question, you can add Set Typeclasses Debug. to your script before invoking the problematic line. Then Coq will print a trace of its instance search in the *response* buffer in Emacs. If your script is looping, make sure you hit C-c C-c right after triggering the loop, as the trace can get very long quite fast
For the last bit, I reminded myself of
Set Typeclasses Debug.
Strange: I'm not actually sure any more whether emacs is stuck at the end or whether it's a display bug: it seems that if I switch to another screen and then switch back I find the coq exited message that I expect, right away.

Delete extra brackets in Counterclockwise strict paredit mode

I'm sure the answer to this question is obvious, but I've read the Counterclockwise documentation and searched on the internet and haven't been able to find it. I frequently end up accidentally putting extra brackets, parens, etc while editing Clojure files in Counterclockwise and have not been able to figure out how to delete them without dropping out of strict paredit mode. For example suppose I have the following function:
(defn (add-x-y-z)
[x y z]
(+ x y z))
I've accidentally put parens around the function name, and as far as I can tell there is no way to delete them short of deleting the function name, deleting the extra parens, and typing the function name back in again, or temporarily dropping out of strict paredit mode. Is there any other option for this? For whatever reason, I run into this problem quite frequently.
You need 'Raise Selection' I believe, which on Mac is Option-R
FYI, you can look at all the key bindings in the preferences window, but as ever can be difficult to find in eclipse. See attached screenshot and notice that I've sorted on 'When' = 'Editing Clojure Source'

How do I get "rainbow parentheses" in emacs?

I would like rainbow parens for editing Clojure in Emacs and since VI does this I assume that in Emacs it should be something like M-x butterfly or something :)
This is an old question now, but I recently wrote RainbowDelimiters mode for this. It's a 'rainbow parens'-type mode which colors all parens, brackets and braces, made with Clojure programming in mind.
It highlights the whole buffer, not just the parens surrounding point.
The most important thing is that it's FAST - all the other rainbow paren modes I've tried slow down editing (especially scrolling) quite a lot. I put significant effort into profiling and optimizing it so doesn't have any noticeable impact on scrolling/editing speed.
You can find info about it at the EmacsWiki page, and the mode itself is at rainbow-delimiters.el.
I'm using highlight-parentheses-mode from the script mquander mentioned. It doesn't provide much of a rainbow effect out of the box, but it is customisable:
(setq hl-paren-colors
'(;"#8f8f8f" ; this comes from Zenburn
; and I guess I'll try to make the far-outer parens look like this
"orange1" "yellow1" "greenyellow" "green1"
"springgreen1" "cyan1" "slateblue1" "magenta1" "purple"))
I believe I've lifted the actual colours from Vimclojure. Note that Vimclojure highlights all parentheses in the file, whereas with highlight-parentheses-mode only the parens which actually contain the point will be highlighted (and only a limited number of levels). I happen to find this behaviour useful, but it is perhaps a bit lacking in the prettiness area in comparison with the Vimclojure way.
I now notice I've never gotten 'round to fixing those outer paren colours actually... Maybe I will now that you've reminded me about it.
Here's a mode for that which I have used in the past briefly. Here's another one which I haven't tried.

How do you type lisp efficiently, with so many parentheses? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I try to keep my fingers on home row as much as possible.
Typing all the parentheses makes me move away from there a fair bit.
I use Emacs; the parentheses themselves are no issue, I'm comfortable with them. And I don't like modes that type them for me automatically.
I've thought about remapping the square brackets to parentheses and vice versa. Is this a good idea? What does everyone else do?
I would personally recommend the lethal combo of Emacs, SLIME & paredit.el
Paredit allows you to pseudo-semantically edit the LISP code at sexp level, and that makes the parentheses disappear almost completely. You can move around sexps as if they are simple words and even mutate them with just a couple of key-strokes. There is also a minor mode called Redshank which works in conjunction with Paredit and that provides more power.
Consider simple these examples (| is the position of the cursor):
(foo bar (baz| quux) zot) + C-( => (foo (bar baz| quux) zot)
(foo (bar |baz) quux zot) + C-) => (foo (bar |baz quux) zot)
(foo (bar baz |quux) zot) + C-{ => (foo bar (baz |quux) zot)
(foo (bar |baz quux) zot) + C-} => (foo (bar |baz) quux zot)
I have done some serious Common Lisp programming in my time, and paredit has proved to be invaluable to me. I can't even think of writing non-trivial LISP code without my toolbox. And after all that's the way it should be; you should never have to count or match your parentheses... once you master Paredit, the parentheses will just disappear from in front of your eyes.
Remapping the [] to () will help, but you will stop caring much after you start using Paredit.
Feel free to use the LISP specific portions from my dot-emacs.
With many non-US keyboard layouts, typing square brackets or braces is even more cumbersome than typing parentheses, anyway, which makes programming in most languages very strainful, so consider yourself lucky. ;)
As for me, I use a programmer-friendly non-standard keyboard layout that lets me type parentheses via [Super]-j and [Super]-k.
I have foot pedals. LeftFoot = open paren, RightFoot = close paren.
Well, I don't, but I don't use Lisp. It doesn't seem like a bad idea, though.
Could you imagine a variation on Lisp that used indentation instead of parens? (taking a page from the Python spec)
I take my fingers off the home keys....
I tried remapping in Emacs, but it creates new problems: say you're editing in a terminal window through ssh and you paste a snippet into the window; then parens and brackets get swapped in your pasting, not just your typing. If you try this, remap at a lower level in your system, like xmodmap.
(Of course, the obvious other problem is using other computers without your remapping. That was a nuisance too, though bearable.)
"... so many parenethesis"
The first thing I did was bind the '(' key to the sequence '('+')'+right(), so my parenthesis auto balance, leaving half as many left to type when writing new code.
You also want a convenient way to navigate out one paren -- bind C-] to the sequence search(')')+right(). Authoring becomes shorter now, as you don't need to take hands off the home position -- just type C-] every time you complete an S-expr.
Next thing I did was bind a key to a subroutine that pushes an existing item onto the current list ... so if // is the cursor position, then this command will transform:
(if (< //) (+ x 1)
(x)
(y))
to
(if (< (+ x 1) //)
(x)
(y))
Effectively pushing one item from the right into the current list -- very useful for editing existing code. The sequence '(', '<', C-S-], Space, '2' adds "compare less than 2" to an existing expression. Combined with C-], this lets you build new expressions very quickly from existing ones.
#jamesnvc,
I didn't think about binding () to [] keys... I'll have to try that, thx!
I remapped [] to () with xmodmap and like it. It was a bit weird getting used to writing code in languages that use [], but like any change, you get used to it. Having unshifted parens in Lisp is nicer than not having unshifted brackets in other languages, so it works out.
Anyway, here is the necessary xmodmap incantation for my US keyboard:
!! swap () and []
keycode 18 = 9 bracketleft
keycode 19 = 0 bracketright
keycode 34 = parenleft braceleft
keycode 35 = parenright braceright
I have to take my fingers off the home row to reach all the other shift-number operators, so I never thought about it much.
And once you type a left-parens, electric-parens give you the right.
If you use the parentheses more than the square brackets, by all means, remap away. I don't see how it could pose any more problems than, say, a lefty swapping her mouse buttons.
When I'm writing code, I generally spend much more time thinking and reading my code, than I do typing it. I've tried a couple of times in the past to switch to the Dvorak keyboard layout, but I lack obvious motivation because I can type much faster than I can think. Programming language syntax is a similar issue - as long as I can type code without leaving the keyboard (ie. using the mouse would be bad), I'm happy.
DrScheme has the keystrokes for parens and square braces flipped by default. It also has a feature where it magically guesses which one of the two you meant, so you rarely reach for shift-9.
Quack has a similar feature to that of DrScheme.
DivaScheme (my editor), is something completely different. It edits at the sexp level, so that the parens are no longer in the way.
Mostly, I just type them, but occasionally, I use M-( and M-) (especially when I am adding a LET binding "late in the stage"), to enclose the relevant nnumber of expressions.
I also changed my (dvorak) keyboard layout (via xmodmap) to switch the brackets ("[]") and parens, in conjunction with paredit-mode (which does indeed take some getting used to).
I use paredit and pair-mode packages but, for fast parenthesis typing I use electric-dot-and-dash to replace a double period in a () on a 5 ms delay (if I type slowly I get two dots then). It's a wonderful package (I hacked a bit for my personal preference; as I type with Dvorak keyboard, I replaced the dash key by a slash (// is not so common in lisp)).
To avoid the mess in parens, I add a package named 'highlight-parentheses to my tool set, and for maximum efficiency on sexp grabbings or text navigation in general, I also use vimpulse (as I am a Vim addict).
Rebind capslock to "(" and have the editor autoinsert ")" for you.
(This also helps for other languages with a lot of brackets, for instance HTML...)
I use Vim with vim-sexp and vim-sexp-mappings-for-regular-people then have mapped <leader>u to put me in normal mode inside of a new (.
map <leader>u i(