Fuco1 / smartparens

Minor mode for Emacs that deals with parens pairs and tries to be smart about it.
GNU General Public License v3.0
1.81k stars 193 forks source link

spaces in parentheses #624

Open Pitometsu opened 8 years ago

Pitometsu commented 8 years ago

Feature request


To solve this question in right way, it should be implemented as part of smartparens (most adequate parentheses-drive mode).

Propose next behaviour:

when smartparens-space-in-parens
action result
example (example-string\|)
<space> => ( example-string \|)
<backspace> => (example-string\|)
Pitometsu commented 6 years ago

Any updates here? :scream_cat:

Pitometsu commented 6 years ago

There's already implemented similar logic in https://github.com/Fuco1/smartparens/blob/6ce2f7af6d28391cc9ce7325bf1797b8b71a7910/smartparens-latex.el#L56

Fuco1 commented 6 years ago

I don't know, there seems to be a lot of edge cases. For example in lisp you can type (foo which would result into ( foo |)... but maybe I just want to write (foo bar) (no leading space). Having to hit backspace would be annoying.

I think this would have to be implemented on a case-by-case basis as per major-mode, and even better maybe as a set of functions that users can use in their own config (so they wouldn't need to reimplement all the logic).

However it is quite simple to do so. If you specify this as a post-handler it will do what you want ("| " "SPC").

Here's my config for java-style docstrings

(sp-local-pair 'php-mode "/**" "*/" :post-handlers '(("| " "SPC")
                                                     (my-php-handle-docstring "RET")))

The "string syntax" for specifying these hooks is described here: https://github.com/Fuco1/smartparens/wiki/Permissions#insertion-specification

If this is universal enough for C++ we can set it up for C++, but I wouldn't do it as a global thing.

Pitometsu commented 6 years ago

You right here, agree: spaces would be convenient only just after the first paren. Updated behaviour proposal here:

action result
( => (\|)
<space> => (_\|_)
<space> => (__\|__)
<backspace> => (_\|_)
word => (_word\|_)
<space> => (_word_\|_)
<backspace> => (_word\|_)
<backspace> x4 => (_\|_)
<backspace> => (\|)
Pitometsu commented 6 years ago

For sure, there's should be some flag to enable/disable such behaviour (not sure about per-mode).

Pitometsu commented 6 years ago

It should work even for already exist pairs with content:

action result
(\|example) => (\|example)
<space> => (_\|example_)
<space> => (__\|example__)
<backspace> x2 => (\|example)
edam commented 5 years ago

I implemented a load of my own code to achieve this. Although I'd quite happily delete my hard work to have it supported by smartparens!

The logic I ended up with was:

It took some experimentation to get this logic right, but I can report that it works well.

Fuco1 commented 5 years ago

@edam Do you think your code is in state that could be contributed to smartparens? I would be happy to add a feature like this.

Obviosuly we would need to make it a bit configurable for people to at least have a way to disable it, so there would be some extra work required.

edam commented 4 years ago

@Fuco1 it is currently implemented in terms of a loadable extension, but I would be happy to rework it in to a branch. I could do with cleaning up a few things anyway.

Fuco1 commented 4 years ago

@edam Maybe we can ship it as an additional config that can be required or enabled by some flag. Feel free to put the code somewhere and we can have a look!

Fuco1 commented 4 months ago

@edam is the code available somewhere, in your config or as a package / repo?