* python.el: improve sexp-based navigation or make it optional?
@ 2020-05-16 13:16 immerrr again
2020-05-16 14:05 ` Stefan Kangas
` (3 more replies)
0 siblings, 4 replies; 11+ messages in thread
From: immerrr again @ 2020-05-16 13:16 UTC (permalink / raw)
To: emacs-devel
Hi everyone,
I'm a frequent user of python.el, and I have a qualm about its "smart"
sexp navigation. As a long-time Emacs user, I have come to certain
expectations about how Emacs behaves, and I cannot wrap my hand around
some of the idiosyncrasies there.
Below you can find a couple of examples. Where forward-sexp from 1
takes point to 2, and backward-sexp from 2 takes point back to 1.
A.
<1>x = 'foo'<2>
B.
<1>class Test:
X = 'foo'
Y = 'bar'<2>
C.
<1>def calc_foo(...):
...<X lines of code>
return foo<2>
It seems logical to be able to skip an entire statement, or an entire
defun/defclass, but what if I want to go select just the last literal
in a statement? The problem here is that there is no way to
unambiguously tell python-mode what symbol/literal/block do I want to
skip. To clarify, let's pretend Python had parentheses around its
syntactical blocks:
<1>(<2>def<2'> my_func(x):
<3>(if (x == 'foo'):
<4>(Y = <5>'bar'<5'>)<4'>)<3'>)<1'>
In this case it's easy to predict that forward/backward-sexp should
establish the following pairs: 1 with 1', 2 with 2', 3 with 3' and so
on. But without the parentheses, there is no way to distinguish
between 1 and 2, or between 1', 3', 4', and 5'. Python.el essentially
tries to make a guess, and curiously, this explicitly goes against one
of the principles listed in the Zen of Python,
> In the face of ambiguity, refuse the temptation to guess.
Some kind of a visual-sexp mode for this type of navigation would
reduce the uncertainty. A mode that would augment the code with some
kind of an overlay, showing parentheses around each sexp composed of
more than 1 symbol/literal, and provide visual guidance as to where
will point end up after a sexp-based navigation command. However, IIRC
overlays don't scale well, and it might require implementing yet
another parser or hacking all the structural information to the
pre-existing one, which is a quite tedious task.
In absence of such mode, I wonder if it would be a welcome addition to
introduce a configuration parameter that at least disables the smart
navigation, making sexp skip one symbol or explicitly
parenthesized/bracketed expression at a time.
What do you think?
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: python.el: improve sexp-based navigation or make it optional?
2020-05-16 13:16 python.el: improve sexp-based navigation or make it optional? immerrr again
@ 2020-05-16 14:05 ` Stefan Kangas
2020-05-16 19:11 ` Andreas Röhler
` (2 subsequent siblings)
3 siblings, 0 replies; 11+ messages in thread
From: Stefan Kangas @ 2020-05-16 14:05 UTC (permalink / raw)
To: immerrr again, emacs-devel
Hi,
immerrr again <immerrr@gmail.com> writes:
> I wonder if it would be a welcome addition to
> introduce a configuration parameter that at least disables the smart
> navigation, making sexp skip one symbol or explicitly
> parenthesized/bracketed expression at a time.
I don't see any reason not to welcome such a contribution.
I happen to think the sexp-navigation in python-mode does a decent job
(read: it never bothered me), but why not give an option to users who
find it too unpredictable.
Best regards,
Stefan Kangas
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: python.el: improve sexp-based navigation or make it optional?
2020-05-16 13:16 python.el: improve sexp-based navigation or make it optional? immerrr again
2020-05-16 14:05 ` Stefan Kangas
@ 2020-05-16 19:11 ` Andreas Röhler
2020-05-16 20:46 ` immerrr again
2020-05-17 14:12 ` bug#41361: Fwd: " Andreas Röhler
2020-05-17 3:04 ` Stefan Monnier
2020-05-20 1:06 ` Stefan Kangas
3 siblings, 2 replies; 11+ messages in thread
From: Andreas Röhler @ 2020-05-16 19:11 UTC (permalink / raw)
To: emacs-devel; +Cc: immerrr again, Stefan Kangas
On 16.05.20 15:16, immerrr again wrote:
>
> In absence of such mode, I wonder if it would be a welcome addition to
> introduce a configuration parameter that at least disables the smart
> navigation, making sexp skip one symbol or explicitly
> parenthesized/bracketed expression at a time.
Maybe that does it?: (from python.el)
If you prefer `cc-mode'-like
;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
;; enough, You can do that using the `python-mode-hook':
;; (add-hook 'python-mode-hook
;; (lambda () (setq forward-sexp-function nil)))
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: python.el: improve sexp-based navigation or make it optional?
2020-05-16 19:11 ` Andreas Röhler
@ 2020-05-16 20:46 ` immerrr again
2020-05-17 14:12 ` bug#41361: Fwd: " Andreas Röhler
1 sibling, 0 replies; 11+ messages in thread
From: immerrr again @ 2020-05-16 20:46 UTC (permalink / raw)
To: Andreas Röhler; +Cc: Stefan Kangas, emacs-devel
On Sat, May 16, 2020 at 9:04 PM Andreas Röhler
<andreas.roehler@online.de> wrote:
>
>
> On 16.05.20 15:16, immerrr again wrote:
> >
> > In absence of such mode, I wonder if it would be a welcome addition to
> > introduce a configuration parameter that at least disables the smart
> > navigation, making sexp skip one symbol or explicitly
> > parenthesized/bracketed expression at a time.
>
> Maybe that does it?: (from python.el)
>
> If you prefer `cc-mode'-like
> ;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
> ;; enough, You can do that using the `python-mode-hook':
>
> ;; (add-hook 'python-mode-hook
> ;; (lambda () (setq forward-sexp-function nil)))
>
Yep, it works, thanks! I have forgotten about that variable.
I wonder if it would be hard to implement the sexp visualisation mode
that would show "virtual parentheses".
Overlays perhaps would be a tad slow, if there was a parenthesis pair
shown for every sexp out there. Maybe I could limit it only to the
sexps that immediately surround point...
Properties would be much better performance wise, but looks like
"display" characters are added as a block, and one cannot enter a
sequence of virtual parentheses with point. I could try adding spaces
or whatnot, but then I'd have to be extra careful that they are
cleaned up before saving or copying buffer contents.
Any recommendations on what could be the starting point here?
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#41361: Fwd: Re: python.el: improve sexp-based navigation or make it optional?
2020-05-16 19:11 ` Andreas Röhler
2020-05-16 20:46 ` immerrr again
@ 2020-05-17 14:12 ` Andreas Röhler
2020-05-20 1:06 ` Stefan Kangas
2021-07-31 16:44 ` Lars Ingebrigtsen
1 sibling, 2 replies; 11+ messages in thread
From: Andreas Röhler @ 2020-05-17 14:12 UTC (permalink / raw)
To: 41361
[-- Attachment #1: Type: text/plain, Size: 1510 bytes --]
Hi,
maybe make forward-sexp-function customizable. Currently that var is set
inside the derived mode:
(set (make-local-variable 'forward-sexp-function)
'python-nav-forward-sexp)
and comment section in python.el head contains:
If you prefer `cc-mode'-like
;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
;; enough, You can do that using the `python-mode-hook':
;; (add-hook 'python-mode-hook
;; (lambda () (setq forward-sexp-function nil)))
Thread is here:
https://lists.gnu.org/archive/html/emacs-devel/2020-05/msg02196.html
Thanks,
Andreas
-------- Forwarded Message --------
Subject: Re: python.el: improve sexp-based navigation or make it optional?
Date: Sat, 16 May 2020 21:11:51 +0200
From: Andreas Röhler <andreas.roehler@online.de>
To: emacs-devel@gnu.org
CC: immerrr again <immerrr@gmail.com>, Stefan Kangas
<stefankangas@gmail.com>
On 16.05.20 15:16, immerrr again wrote:
> In absence of such mode, I wonder if it would be a welcome addition to
> introduce a configuration parameter that at least disables the smart
> navigation, making sexp skip one symbol or explicitly
> parenthesized/bracketed expression at a time.
Maybe that does it?: (from python.el)
If you prefer `cc-mode'-like
;; `forward-sexp' movement, setting `forward-sexp-function' to nil is
;; enough, You can do that using the `python-mode-hook':
;; (add-hook 'python-mode-hook
;; (lambda () (setq forward-sexp-function nil)))
[-- Attachment #2: Type: text/html, Size: 3798 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#41361: Fwd: Re: python.el: improve sexp-based navigation or make it optional?
2020-05-17 14:12 ` bug#41361: Fwd: " Andreas Röhler
@ 2020-05-20 1:06 ` Stefan Kangas
2020-05-21 13:42 ` João Távora
2021-07-31 16:44 ` Lars Ingebrigtsen
1 sibling, 1 reply; 11+ messages in thread
From: Stefan Kangas @ 2020-05-20 1:06 UTC (permalink / raw)
To: Andreas Röhler; +Cc: 41361
Andreas Röhler <andreas.roehler@online.de> writes:
> maybe make forward-sexp-function customizable. Currently that var is
> set inside the derived mode:
I agree that this should be customizable.
Best regards,
Stefan Kangas
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#41361: Fwd: Re: python.el: improve sexp-based navigation or make it optional?
2020-05-20 1:06 ` Stefan Kangas
@ 2020-05-21 13:42 ` João Távora
2021-03-20 2:51 ` Stefan Kangas
0 siblings, 1 reply; 11+ messages in thread
From: João Távora @ 2020-05-21 13:42 UTC (permalink / raw)
To: Stefan Kangas; +Cc: 41361, Andreas Röhler, immerrr
Stefan Kangas <stefan@marxist.se> writes:
> Andreas Röhler <andreas.roehler@online.de> writes:
>
>> maybe make forward-sexp-function customizable. Currently that var is
>> set inside the derived mode:
>
> I agree that this should be customizable.
I too am a fan of sexp navigation (C-M-f, C-M-b, etc.) If this
navigation in indeed superior, we could consider making it the default
for python-mode? Something similar thing was done recently for
nxml-mode, by changing nxml-sexp-element-flag from nil to t. I liked
this change very much.
João
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#41361: Fwd: Re: python.el: improve sexp-based navigation or make it optional?
2020-05-21 13:42 ` João Távora
@ 2021-03-20 2:51 ` Stefan Kangas
0 siblings, 0 replies; 11+ messages in thread
From: Stefan Kangas @ 2021-03-20 2:51 UTC (permalink / raw)
To: João Távora; +Cc: 41361, immerrr, Andreas Röhler
João Távora <joaotavora@gmail.com> writes:
> Stefan Kangas <stefan@marxist.se> writes:
>
>> Andreas Röhler <andreas.roehler@online.de> writes:
>>
>>> maybe make forward-sexp-function customizable. Currently that var is
>>> set inside the derived mode:
>>
>> I agree that this should be customizable.
>
> I too am a fan of sexp navigation (C-M-f, C-M-b, etc.) If this
> navigation in indeed superior, we could consider making it the default
> for python-mode? Something similar thing was done recently for
> nxml-mode, by changing nxml-sexp-element-flag from nil to t. I liked
> this change very much.
I have been back to coding a bunch of Python lately, and having
experimented with this I think making it the default behavior is indeed
a good idea.
In the linked emacs-devel thread, I said that the old behavior was fine,
which I guess is true. I have now come to the conclusion that it is
also worse than the alternative. :-)
^ permalink raw reply [flat|nested] 11+ messages in thread
* bug#41361: Fwd: Re: python.el: improve sexp-based navigation or make it optional?
2020-05-17 14:12 ` bug#41361: Fwd: " Andreas Röhler
2020-05-20 1:06 ` Stefan Kangas
@ 2021-07-31 16:44 ` Lars Ingebrigtsen
1 sibling, 0 replies; 11+ messages in thread
From: Lars Ingebrigtsen @ 2021-07-31 16:44 UTC (permalink / raw)
To: Andreas Röhler; +Cc: 41361
Andreas Röhler <andreas.roehler@online.de> writes:
> maybe make forward-sexp-function customizable.
Makes sense to me, so I've now added that to Emacs 28. It was also
suggested to change the default to nil, but that seems too annoying, so
I didn't do that.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: python.el: improve sexp-based navigation or make it optional?
2020-05-16 13:16 python.el: improve sexp-based navigation or make it optional? immerrr again
2020-05-16 14:05 ` Stefan Kangas
2020-05-16 19:11 ` Andreas Röhler
@ 2020-05-17 3:04 ` Stefan Monnier
2020-05-20 1:06 ` Stefan Kangas
3 siblings, 0 replies; 11+ messages in thread
From: Stefan Monnier @ 2020-05-17 3:04 UTC (permalink / raw)
To: immerrr again; +Cc: emacs-devel
> <1>class Test:
> X = 'foo'
> Y = 'bar'<2>
SMIE navigation faces similar problems and solves them differently: from
(1) is would jump to (2) but from (2) it would jump back to just before
'bar' instead of jumping back to (1).
IOW it jumps over "the nearest/smallest" sub-expression.
Stefan
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: python.el: improve sexp-based navigation or make it optional?
2020-05-16 13:16 python.el: improve sexp-based navigation or make it optional? immerrr again
` (2 preceding siblings ...)
2020-05-17 3:04 ` Stefan Monnier
@ 2020-05-20 1:06 ` Stefan Kangas
3 siblings, 0 replies; 11+ messages in thread
From: Stefan Kangas @ 2020-05-20 1:06 UTC (permalink / raw)
To: immerrr again, emacs-devel
immerrr again <immerrr@gmail.com> writes:
> In absence of such mode, I wonder if it would be a welcome addition to
> introduce a configuration parameter that at least disables the smart
> navigation, making sexp skip one symbol or explicitly
> parenthesized/bracketed expression at a time.
>
> What do you think?
Andreas Röhler filed this bug:
Fwd: Re: python.el: improve sexp-based navigation or make it optional?
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=41361
It would be most welcome if you worked on it.
Best regards,
Stefan Kangas
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2021-07-31 16:44 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-05-16 13:16 python.el: improve sexp-based navigation or make it optional? immerrr again
2020-05-16 14:05 ` Stefan Kangas
2020-05-16 19:11 ` Andreas Röhler
2020-05-16 20:46 ` immerrr again
2020-05-17 14:12 ` bug#41361: Fwd: " Andreas Röhler
2020-05-20 1:06 ` Stefan Kangas
2020-05-21 13:42 ` João Távora
2021-03-20 2:51 ` Stefan Kangas
2021-07-31 16:44 ` Lars Ingebrigtsen
2020-05-17 3:04 ` Stefan Monnier
2020-05-20 1:06 ` Stefan Kangas
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.