polymode / poly-org

Polymode for org-mode
57 stars 12 forks source link

Scope is not narrowed consistently for other modes (causing problems for paredit, in particular) #22

Closed slyrus closed 4 years ago

slyrus commented 4 years ago

The following simple file:

* Table test
|--------+---------|
| Foo    | Bar     |
|--------+---------|
| Mouse  | Moose   |
| Donkey | Giraffe |
|--------+---------|

* Lisp test

#+begin_src lisp :session

  (format nil "Hello world!")

#+end_src

results in the following error when I try to open it with poly-org mode:

Polymode error (pm--mode-setup ’lisp-mode): Unmatched bracket or quote

If I delete one of the table lines (reducing the table to a header line and a single line of table contents, everything works fine.

slyrus commented 4 years ago

The problem seems to be that in paredit.el define-minor-mode paredit-mode calls check-parens:

(define-minor-mode paredit-mode
  "Minor mode for pseudo-structurally editing Lisp code.
\\<paredit-mode-map>"
  :lighter " Paredit"
  ;; If we're enabling paredit-mode, the prefix to this code that
  ;; DEFINE-MINOR-MODE inserts will have already set PAREDIT-MODE to
  ;; true.  If this is the case, then first check the parentheses, and
  ;; if there are any imbalanced ones we must inhibit the activation of
  ;; paredit mode.  We skip the check, though, if the user supplied a
  ;; prefix argument interactively.
  (if (and paredit-mode
           (not current-prefix-arg))
      (if (not (fboundp 'check-parens))
          (paredit-warn "`check-parens' is not defined; %s"
                        "be careful of malformed S-expressions.")
          (condition-case condition
              (check-parens)
            (error (setq paredit-mode nil)
                   (signal (car condition) (cdr condition)))))))

And check-parens (from emacs/lisp/emacs-lisp/lisp.el) throws an error. There's a comment in there that says:

  "Check for unbalanced parentheses in the current buffer.
More accurately, check the narrowed part of the buffer for unbalanced
expressions (\"sexps\") in general. ...

Should poly-org mode be narrowing the buffer somehow before turning on paredit mode?

slyrus commented 4 years ago

Adding the following to my .emacs (with the latest paredit) fixes (or works around, at least) the problem for me:

(defvar paredit-override-check-parens-function
  (lambda (condition) (declare ignore condition) t)
  "Function to tell whether unbalanced text should inhibit Paredit Mode.")
akater commented 4 years ago

Since this has nothing to do with multi-line tables, or tables in general, I suggest you rename this issue, along the lines of “Scope is not narrowed consistently for other modes”.

slyrus commented 4 years ago

Thanks for the suggestion!

vspinu commented 4 years ago

I think the right solution would be to have a list of functions that should be protected (aka executed with narrowing). There is a related discussion here.

presnell commented 4 years ago

I'm going to put this here because I suspect that it comes down to the same issue; if I am wrong, please feel free to promote it.

The specific problem that I have encountered is that some of the table/spreadsheet functionality of org-mode is broken by poly-org.

As an example, taken from url, try saving the two tables below into an org file, say, currencies.org. Then open the file in emacs with poly-org loaded and enter C-cC-c on the tblfm line in the second table. This will cause the last two columns to be filled with #ERROR (or perhaps some other incorrect result, depending on org version). Turning on show-paren mode also shows incorrect matches for the parentheses in the tblfm line, which is why I suspect that the problem is related to the parent issue.

#+tblname: rates
| currency        | abbreviation | euros |
|-----------------+--------------+-------|
| euro            | eur          |     1 |
| Norwegian krone | nok          |  0.14 |
| Swedish krona   | sek          |  0.12 |
| US dollar       | usd          |  0.77 |

|  date | expense          |  sum | currency | rate | euros |
|-------+------------------+------+----------+------+-------|
|  1.3. | flights          |  324 | eur      |      |       |
|  4.6. | books and maps   |  243 | usd      |      |       |
| 30.7. | rental car       | 8300 | sek      |      |       |
|  2.7. | hotel            | 1150 | sek      |      |       |
|  2.7. | lunch            |  190 | sek      |      |       |
|  3.7. | fishing licenses | 1400 | nok      |      |       |
|  3.7. | gasoline         |  340 |          |      |       |
#+tblfm: $5='(org-lookup-first $4 '(remote(rates,@2$2..@>$2)) '(remote(rates,@2$3..@>$3)))::$6=$5*$3
vspinu commented 4 years ago

I can see the issue without poly-org as well. The examples in that tutorial just don't seem to work. You can always check by switching with M-x org-mode.

vspinu commented 4 years ago

Closing in favor of the more generic polymode/polymode#255