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

Markus Triska markus.triska at gmx.at
Fri Nov 28 19:12:01 CET 2008


Hi Martin,

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

> That would be a valuable addition to noweb-mode.el

The following patch seems to work - feel free to use any parts:

diff --git a/lisp/noweb-mode.el b/lisp/noweb-mode.el
index 2e4e2a1..7e034c5 100644
--- a/lisp/noweb-mode.el
+++ b/lisp/noweb-mode.el
@@ -426,6 +426,7 @@ Misc:
     (mapcar 'noweb-make-variable-permanent-local
             '(noweb-mode
               after-change-functions
+              before-change-functions
               noweb-narrowing
               noweb-chunk-vector
               post-command-hook
@@ -446,10 +447,12 @@ Misc:
           (noweb-font-lock-mode 1)))
     (add-hook 'post-command-hook 'noweb-post-command-function)
 
-    (if (or (<= emacs-major-version 20)
-	    (featurep 'xemacs)) ;; Xemacs or very old GNU Emacs
-	(make-local-hook 'after-change-functions))
+    (when (or (<= emacs-major-version 20)
+              (featurep 'xemacs)) ;; Xemacs or very old GNU Emacs
+      (make-local-hook 'after-change-functions)
+      (make-local-hook 'before-change-functions))
     (add-hook 'after-change-functions 'noweb-after-change-function nil t)
+    (add-hook 'before-change-functions 'noweb-before-change-function nil t)
 
     (add-hook 'noweb-select-doc-mode-hook 'noweb-auto-fill-doc-mode)
     (add-hook 'noweb-select-code-mode-hook 'noweb-auto-fill-code-mode)
@@ -465,8 +468,11 @@ Misc:
     (remove-hook 'post-command-hook 'noweb-post-command-function)
 
     (if (fboundp 'remove-local-hook)
-	(remove-local-hook 'after-change-functions 'noweb-after-change-function)
-      (remove-hook 'after-change-functions 'noweb-after-change-function t))
+        (progn
+          (remove-local-hook 'after-change-functions 'noweb-after-change-function)
+          (remove-local-hook 'before-change-functions 'noweb-before-change-function))
+      (remove-hook 'after-change-functions 'noweb-after-change-function t)
+      (remove-hook 'before-change-functions 'noweb-before-change-function t))
 
     (remove-hook 'noweb-select-doc-mode-hook 'noweb-auto-fill-doc-mode)
     (remove-hook 'noweb-select-code-mode-hook 'noweb-auto-fill-code-mode)
@@ -497,14 +503,28 @@ by major mode changes."
   "The hook being run after each command in noweb mode."
   (noweb-select-mode))
 
+(defvar noweb-chunk-boundary-changed nil
+  "Whether the current change affects a chunk boundary.")
+
+(defvar noweb-chunk-boundary-regexp "^\\(@[^@]\\)\\|\\(<<\\)")
+
+(defun noweb-before-change-function (begin end)
+  "Record changes to chunk boundaries."
+  (save-excursion
+    (goto-char begin)
+    (setq noweb-chunk-boundary-changed
+          (re-search-forward noweb-chunk-boundary-regexp end t))))
+
 (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"
+If the changed region contains a chunk boundary, it will update
+the chunk vector"
   (save-excursion
     (goto-char begin)
-    (if (re-search-forward "^\\(@[^@]\\)\\|\\(<<\\)" end t)
-      (noweb-update-chunk-vector))))
+    (when (or noweb-chunk-boundary-changed
+              (re-search-forward noweb-chunk-boundary-regexp end t))
+      (noweb-update-chunk-vector)
+      (setq noweb-chunk-boundary-changed nil))))
 
 
 ;;; Chunks

All the best,
Markus



More information about the ESS-bugs mailing list