syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.65k stars 4.89k forks source link

Smart parentheses in ipython-notebook #5491

Closed aldanor closed 4 years ago

aldanor commented 8 years ago

Description

Smart parentheses are not behaving well in ipython-notebook (proper modes not enabled?).

Reproduction guide

foo()
bar()

Then, delete the closing parenthesis of foo() and type it again. You then end up with something like this:

foo()
bar)()
foo('bar)')

Observed behaviour: smartparens-mode and smartparens-global-mode are enabled in Python buffers but not enabled in ipython-notebook layer. This leads to some weird behaviour when trying to close parentheses, sometimes extra parens are inserted or it won't let you close them at all. In any case, the error message is: "Search failed. This means there is an unmatched expression somewhere or we are at the beginning/end of file.", which seems to be an :unmatched-expression error thrown in sp-get-paired-expression.

Enabling smartparens-mode and smartparens-global-mode seems to fix it, but I'm not sure how it should be fixed properly.

System Info

((auto-completion :variables auto-completion-enable-sort-by-usage t)
 (c-c   :variables c-c  -default-mode-for-headers 'c  -mode c-c  -enable-clang-support t)
 csv deft emacs-lisp eyebrowse
 (git :variables git-magit-status-fullscreen t magit-save-repository-buffers 'dontask magit-revision-show-gravatars nil)
 (github :variables gh-profile-default-profile "aldanor")
 html javascript ipython-notebook markdown org
 (python :variables python-fill-column 99 python-test-runner 'pytest)
 rust search-engine
 (shell :variables shell-default-shell 'eshell shell-enable-smart-eshell t)
 shell-scripts spacemacs-helm syntax-checking themes-megapack version-control yaml aldanor dash osx)
aldanor commented 8 years ago

Looks like adding

(add-hook 'ein:notebook-multilang-mode-hook 'smartparens-mode)

to post-init-ein should be sufficient to address the first problem.

The second problem (closing parenthesis on one line erroneously duplicates it and closes it on the next line) seems to also be present in python-mode which is quite frustrating.

TheBB commented 8 years ago

@StreakyCobra Does this have something to do with your trick?

StreakyCobra commented 8 years ago

Unless dotspacemacs-smart-closing-parenthesis is set to t (what shouldn't be the case by default), it shouldn't have any effect on Spacemacs, buuut…

I can indeed reproduce the Python bug when it's activated, it's related to the use of sp-navigate-close-if-unbalanced t in the function, turning it off makes this bug disappear. So I would say it's a bug between smartparens and python as I don't notice this behaviour in other modes, like in sh-mode for instance.

@aldanor I suppose you also have dotspacemacs-smart-closing-parenthesis set to t, it's what is causing your bug, try turning it off.


Keeping sp-navigate-close-if-unbalanced set to t is useful for this kind of case:

Starting with the following text (not in the last line of the file, this is a limitation of smartparens, no idea why…):

([auie

And pressing two times a closing parenthesis ())):

aldanor commented 8 years ago

@StreakyCobra Indeed turning dotspacemacs-smart-closing-parenthesis off fixes the second problem. I was actually using it for another feature: in the following example

(auie

navigate the cursor between au and ie. and press ):

StreakyCobra commented 8 years ago

Yes I added this feature recently because it was really useful for dealing with closing parenthesis (bracket, quote, etc.) in a lot of ways. The implementation is more like a hack on top of smartparens, but it's an elegant hack (from my point of view at least, but I'm probably biased :wink:)

I am able to reproduce this with a minimum configuration:

So it seems to be a smartparens bug (if it is a bug at all, maybe it's the expected behaviour?).

I don't have a lot of time these days, so I encourage you to report it upstream to smartparens :smiley: You can give them the reproduction steps and eventually point them to this link if you do it.

aldanor commented 8 years ago

@StreakyCobra Yea, looks like it happens in all prog modes, not just Python (e.g. fire a scratch buffer with prog-mode and the error is the same).

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!