Skip to content

Conversation

akater
Copy link
Contributor

@akater akater commented Jul 22, 2021

This is a draft. We aim to

  • narrow the scope of some external functions to the current chunk
  • allow some “inner” major modes to initialize without errors

As written, this mitigates #255 w.r.t. paredit for me. We add global advices on loading polymode. How about we provide a custom variable to specify which functions to advice?

I did not research the smartparens part as I don't use it. I'll dig deeper if the overall idea gets approved.

@akater akater changed the title Aggressively narrow some functions Aggressively narrow some external functions Jul 22, 2021
Copy link
Collaborator

@vspinu vspinu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

All compatibility stuff should go into polymode-compat.el as much a possible.

polymode-core.el Outdated
;; Also, smartparens does sp--update-local-pairs
;; which might bring smartparens into broken state
;; if brackets are unbalanced.
(advice-add 'sp-wrap--can-wrap-p :around #'always)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this should be happening when the mode is installed. It's a global advice, so it could be done just fine like other advices in polymode-compat.el or not?

I would actually very much prefer it to be a well documented stand alone advice and not a programatically installed one somewhere in the core code. This way people could easier find out who is advising and why.

@akater akater force-pushed the aggressive-narrow branch from 5b22656 to 702f082 Compare July 26, 2021 14:08
@akater
Copy link
Contributor Author

akater commented Jul 27, 2021

I see that you alerady have this mechanism. This begs several questions. In order of decreasing importance,

  1. Suppose we
(pm-around-advice 'paredit-mode #'pm-execute-narrowed-to-span)

and initialize an Org buffer with lisp blocks, and with paredit-mode being loaded in lisp-mode-hook.

In what case then will paredit-mode get enabled? Do paren checks need to get passed in each standalone inner lisp-mode span? (Such ambiguity was the reason why I preferred to enable paredit-mode unconditionally.)

This better be in the documentation; I couldn't find the answer. In fact, the whole polymode-compat facility better be mentioned there.

  1. Why is *span* not defined buffer-local?

  2. Why does its name violate Elisp conventions? Why not pm--span or pm--narrowing-span?

  3. When I evaluate *span* in an Org block in poly-org, i still see nil value which makes it unclear how does it work exactly. Could something be written in documentation to highlight that? Docstring for pm-execute-narrowed-to-span says

*span* in pm-map-over-spans has precedence over span at point.

which does not help.

  1. Why wasn't paredit-mode advised aready in the last couple of years? (Rhethorical question but since polymode-compat was not mentioned in Protect smartparens, paredit, check-parens (others?) to operate within current chunk #255, I presumed there's nothing even to start with and I ended up wasting more time than needed.)

@vspinu
Copy link
Collaborator

vspinu commented Sep 7, 2021

Sorry for late response. I have missed your reply somehow.

(pm-around-advice 'paredit-mode #'pm-execute-narrowed-to-span)

I don't use paredit but I think this is not what you want. As the name suggests pm-execute-narrowed-to-span should be added as an advise to the actual functions which need to operate within a span.

Paredit's situation might be a bit more complex if it has to parse the entire buffer and does assume homogenuous syntax. If it simply relies on parse-ppss then it should work out of the box if it's activated globally.

Why is span not defined buffer-local?
Why does its name violate Elisp conventions? Why not pm--span or pm--narrowing-span?

Because it's a dynamically bound variable available only within the mapping functions. It does't make sense to bind it globally or locally in the buffer. I have clarified the docs of those compact functions. *span* is bound in the pm-map-over-span.

Why wasn't paredit-mode advised aready in the last couple of years?

No one reported a simple and clear reproducible issue. I am not a user of paredit mode so I cannot see the issue by myself either.

@akater akater force-pushed the aggressive-narrow branch from 4ac4fbd to 6490c92 Compare April 15, 2024 04:07
@akater akater force-pushed the aggressive-narrow branch from 6490c92 to 5bac79b Compare May 25, 2025 10:34
@akater akater force-pushed the aggressive-narrow branch from 5bac79b to 0887ce4 Compare May 25, 2025 10:49
Copy link
Collaborator

@vspinu vspinu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am looking at this again after some years, and it's still not making a lot of sense to me.

I think a much smaller and more readable incision could be done here without these extra constructs. To summarize the comments, I think all what's needed is to:

  1. Advice whatever functions need to be narrowed with pm-execute-narrowed-to-span
  2. Advice worker functions to do what's needed conditionally on if it's in a polymode buffer (ex. paredit-override-check-parens-function)
  3. (most likely unnecessary) Advice mode functions to behave differently in the polymode buffers (conditional on polymode-mode being t)

@@ -28,6 +28,7 @@
;;; Code:

(require 'polymode-core)
(require 'polymode-compat)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

polymode-compat is very dirty, it should not be included in the core.

@@ -155,7 +156,7 @@ Ran by the polymode mode function."
(pm--move-vars polymode-move-these-vars-from-base-buffer base))
(condition-case-unless-debug err
;; !! run-mode-hooks and hack-local-variables run here
(funcall mode)
(pm--funcall-mode-with-convenience mode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just can be done by advising the mode function. I don't see the need for such a hack in the polymode proper.

;; This is merely a guess, for example's sake.
;; Also, smartparens does sp--update-local-pairs
;; which might bring smartparens into broken state
;; if brackets are unbalanced.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand all these changes. These are essentially internals of smartparens and paredit. Also the comments do explicitly say it's either "wrong" or "unsure about". So it's clearly not "production" ready.

(narrow-to-region (nth 1 pm--localizing-span)
(nth 2 pm--localizing-span))
(apply f args))
(apply f args)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If narrowing to current span is all what is needed, then one could just do:

(pm-around-advice 'some-function #'pm-execute-narrowed-to-span)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Yes, almost certainly, significant rewrites are needed. I submitted what seemed to be working for me just because it's a better start than an issue without any patch. Hopefully, I'll rewrite it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants