1

Here is a terse snippet with which you can recreate the issue, I hope:

#+begin_src sh
for x in $(for y in $(cmdA '"'); do cmdB $y; done); do cmdC $x; done
#+end_src

Here if I try to operate with respect to parenthetical operations (e.g. delete surrounding), or if I simply try to fold/close-all, I get forward-sexp: Scan error: "Unbalanced parentheses", nnnnn, mmmmm. If I remove '"' this goes away, but this is valid sh, so I'm unclear why this is.

My version information is:

GNU Emacs 28.1 (build 1, x86_64-apple-darwin21.4.0, NS appkit-2113.40 Version 12.3.1 (Build 21E258)) of 2022-04-21
Doom core     v3.0.0-dev       HEAD -> master f51a2cdd3 2022-04-18 18:14:16 +0200
Doom modules  v22.04.0-dev     HEAD -> master f51a2cdd3 2022-04-18 18:14:16 +0200

If you have any ideas or if this is expected behavior, let me know.

2
  • Unable to reproduce on vanilla Emacs 27.1, Org 9.5.2, with Paredit enabled, in either the Org buffer or in sh-mode (following org-edit-src-code) on Manjaro. Commented Apr 25, 2022 at 13:53
  • 1
    I can: C-M-f on the first open paren gives No next sexp and similarly for others (that's in the Org mode buffer, not in the sh-mode buffer where the syntax rules are appropriate). Paredit mode does not seem to make a difference, except that when enabling it in the Org mode buffer, I get an error Unmatched bracket or quote. The problem is the single " in the argument of cmdA: sh-mode knows how to make sense of that but Org mode does not. IIRC, the advice from the mailing list is: "you'll have to live with it". Versions: GNU Emacs 29.0.50, Org mode version 9.5.2+
    – NickD
    Commented Apr 25, 2022 at 15:57

1 Answer 1

2

The problem is that the buffer's major mode is Org mode, not sh-mode. The syntax rules for Org mode do not know about constructs like '"' and the unmatched " causes havoc. It would be possible perhaps to switch major mode depending on the context[1], but that has been deemed much too complicated a way to deal with a relatively minor problem: my recollection is that it has been brought up on the mailing list and the advice has been: "You'll have to live with it".

You can do C-c ' on the source block and get a buffer in sh-mode for this block, where delimiter scanning works as you would expect.

[1] There is a package called polymode but I'm unsure whether it provides context-dependent major modes in the same buffer or whether it allows multiple major modes to coexist somehow. I have the impression that it is somewhat fragile, but I have never used it, so treat this as hearsay plus recollections from my very imperfect memory.

1
  • Okay. I understand. I wasn't clear that the org syntax blocks weren't able, by default, to use the syntax rules for the matching mode. But maybe that should have been obvious to me. Thank you for clarifying.
    – theherk
    Commented Apr 25, 2022 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.