Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hs-hide-all gives unbalanced parentheses error when it shouldn't in some Rnw buffers #332

Open
rdiaz02 opened this issue Jan 29, 2024 · 0 comments

Comments

@rdiaz02
Copy link

rdiaz02 commented Jan 29, 2024

Running hs-hide-all (from hs-minor-mode) can lead to Scan error: "Unbalanced parentheses" when it shouldn't; once that happens, other things fail, like syntax highlighting.
It seems that hs-hide-all continues scanning beyond the boundaries of the code chunk.

This is an exampe.

  1. Do emacs -Q and evaluate
(package-initialize)
(add-hook 'prog-mode-hook (lambda () (hs-minor-mode 1) (hs-hide-all)))
  1. Suppose this Rnw file:
\documentclass{article}

\usepackage{color}
\newcommand{\blu}[1]{\textcolor{blue}{#1}}

\begin{document}

<<>>=
f1 <- function(x) {
  return(2 * x)
}
@ 

\end{document}

If we find-file the file, we get

Polymode error (pm--mode-setup ’ess-r-mode): Scan error: "Unbalanced parentheses", 65, 169

That error disappears if:

  1. The Rnw file does not have the line \newcommand{\blu}[1]{\textcolor{blue}{#1}} or that line is commented out. The culprit seems to be the # .
  2. You load the file, but the hook does not include the hs-hide-all. In this second case, the file loads without error, but you can trigger the error by M-x hs-hide-all in the R chunk.

For now, a workaround I am using is to modify the hook, so that if the file ends on Rnw (and, since I am at it, in Rmd) hs-minor-mode is not enabled (or hs-hide-all is not called). (For anybody encountering this problem, this is what I do, and seems to work but, caveat emptor, this is from a complete elisp newby:

(defun prog-mode-hook-no-hide-all-in-polym ()
  "hs-minor-mode and hs-hide-all, but in Rnw and Rmd
do not hs-hide-all, because it can break polymode.
I am not sure this only disables hs-hide-all : I think hs-minor-mode is 
not enabled in ESS chunks. Well, no big deal for now."
  (hs-minor-mode 1)
  (unless (and (buffer-file-name)
               (or (string-match-p "\\.Rnw\\'" (buffer-file-name))
                   (string-match-p "\\.Rmd\\'" (buffer-file-name))))
    (hs-hide-all)))


(add-hook 'prog-mode-hook 'prog-mode-hook-no-hide-all-in-polym)
@rdiaz02 rdiaz02 changed the title hs-hide-all produces unbalanced parentheses when it shouldn't in at least Rnw buffers hs-hide-all gives unbalanced parentheses error when it shouldn't in some Rnw buffers Jan 29, 2024
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

No branches or pull requests

1 participant