[ESS-bugs] ess-mode 5.3.8; after-change-functions; RET at end of file

Martin Maechler maechler at stat.math.ethz.ch
Fri Nov 28 10:47:09 CET 2008


>>>>> "MT" == Markus Triska <markus.triska at gmx.at>
>>>>>     on Fri, 28 Nov 2008 06:05:27 +0100 writes:

    MT> Hi Martin,
    MT> Martin Maechler <maechler at stat.math.ethz.ch> writes:

    >> Hmm, thanks for the report.
    >> Unfortunately I (and others) have repeatedly found that the
    >> text <-> code   mode switching for editing Rnw files, has
    >> sometimes been behaving "illogically" / "stubornly" / ....
    >> 
    >> I have never started trying to find out how to fix it.
    >> If you (or anyone in ESS-core {which is listening to ESS-bugs})
    >> have time  or know of someone else (smart in Emacs & Emacs lisp)
    >> who'd have, we'd be very glad for helping hands...

    MT> It seems that only a few calls of `noweb-update-chunk-vector' are
    MT> missing. Adding such a (wrapped) call to `after-change-functions' fixes
    MT> the problem I reported, and probably other similar cases you found:

    MT> (defun my-update-chunk-vector (&rest any) ; ignore arguments
    MT> (noweb-update-chunk-vector))
    MT> (add-hook 'after-change-functions 'my-update-chunk-vector nil t)

Ahh... now (after studying noweb-mode.el a bit more),
I see.

Note (you probably did) that the usual  after-change-functions
only contains 'noweb-after-change-function
which only calls  noweb-update-chunk-vector when it finds noweb
chunks in the current region :

(defun noweb-after-change-function (begin end length)
  "Function to run after every change in a noweb buffer.
If the changed region contains a chunk start (^@ or ^<<), it will
update the chunk vector"
  (save-excursion
    (goto-char begin)
    (if (re-search-forward "^\\(@[^@]\\)\\|\\(<<\\)" end t)
      (noweb-update-chunk-vector))))

So, it seems that noweb-after-change-function could be
customized to "more quickly" call
      (noweb-update-chunk-vector)
directly.
We could have it call it "unconditionally" in the case some
customizable variable is set,
or try to make it really smarter and call
noweb-update-chunk-vector in ``exactly those cases where
needed'' (;-)

    MT> For my use cases, I did not notice any slowdown; 

[how long *.Rnw files where you using?]

    MT> however, if you find that too slow, I think it could be
    MT> improved by adding code to `before-change-functions'
    MT> that checks whether a chunk boundary is affected by that
    MT> change, and only if yes, schedules a call of
    MT> `noweb-update-chunk-vector' to be called in
    MT> `after-change-functions'.

Yes, a smart idea.  
That would be a valuable addition to noweb-mode.el


    MT> For the concrete case I reported, I also found a much simpler fix, which
    MT> I think also improves the general use case: I changed `noweb-electric-<'
    MT> to also insert a matching "@ " in addition to "<<>>=", which has so far
    MT> always been what I have inserted manually afterwards anyways:

    MT> diff --git a/lisp/noweb-mode.el b/lisp/noweb-mode.el
    MT> index 7458e3f..2e4e2a1 100644
    MT> --- a/lisp/noweb-mode.el
    MT> +++ b/lisp/noweb-mode.el
    MT> @@ -1171,7 +1171,7 @@ and and update the chunk vector."
    MT> (progn
    MT> (insert "<<")
    MT> (save-excursion
    MT> -            (insert ">>=")
    MT> +            (insert ">>=\n@ ")
    MT> (if (not (looking-at "\\s *$"))
    MT> (newline)))
    MT> (noweb-update-chunk-vector))

Very good, in any case.
I now remember I've once wondered in the past why I didn't see
'@' inserted. 
So the above change is a bug fix in my view, that I'll commit to 
5.3.10 as well.


    MT> When I then undo the change made by "<", I'm back to LaTeX mode.

Good.
Thanks a lot, Markus!

Martin



More information about the ESS-bugs mailing list