Source

ecb / ecb-compilation.el

Diff from to

File ecb-compilation.el

               ;;else it isn't a complication buffer
               nil)))))))
 
+;; Klaus Berndl <klaus.berndl@sdm.de>: The following mechanism is necessary to
+;; avoid eating up whole CPU for updating the menu-entries for the
+;; compilation-buffers. Especially if you have opened a lot of buffers this
+;; can slow down Emacs/ECB dramatically. Now we add an idle-times
+;; check-function `ecb-compilation-buffer-list-changed-p' which checks if the
+;; buffer-list has changed. If yes, then the variable
+;; `ecb-compilation-update-menu-p' is set to t. Only if this variable if not
+;; nil the menu-bar-update-hook `ecb-compilation-update-menu' updates the
+;; ECB-menu.
+
+(defvar ecb-compilation-update-menu-p nil)
+(defvar ecb-compilation-buffer-list-cache nil)
+(defvar ecb-compilation-update-idle-time 0.25)
+
+(defun ecb-compilation-buffer-list-init ()
+  "Initialize the compilation buffer list cache."
+  (setq ecb-compilation-update-menu-p nil)
+  (setq ecb-compilation-buffer-list-cache nil)
+  (ecb-compilation-buffer-list-changed-p))
+
+(defun ecb-compilation-buffer-list-changed-p ()
+  "Check if current active buffer list has changed - i.e. if a new buffer has
+been created or a buffer has been deleted. If yes then
+`ecb-compilation-update-menu-p' is set to not nil and the cache is updated."
+  (let ((new-buffer-list (buffer-list)))
+    (when (not (equal new-buffer-list
+                      ecb-compilation-buffer-list-cache))
+      (setq ecb-compilation-buffer-list-cache new-buffer-list)
+      ;; Nowhere else this variable will be set to t.
+      (setq ecb-compilation-update-menu-p t))))
+
 (defun ecb-compilation-update-menu()
   "Create an install a menu that allows the user to navigate buffers that are
-valid ECB compilation buffers.  See `ecb-compilation-buffer-p' for more
-information about compilation buffers."
+valid ECB compilation buffers. This is only done if
+`ecb-compilation-update-menu-p' is not nil; see
+`ecb-compilation-buffer-list-changed-p'. For more information about
+compilation buffers see `ecb-compilation-buffer-p'."
 
-  (let((submenu nil)
-       (buffers (ecb-compilation-get-buffers)))
-
-    (dolist(buffer buffers)
-      (setq submenu
-            (append submenu
-                    (list (vector (car buffer)
-                                  `(funcall (if (ecb-compile-window-live-p)
-                                                'switch-to-buffer
-                                              'switch-to-buffer-other-window)
-                                            ,(car buffer))
-                                    :active t)))))
-
-    ;;TODO: Klaus Berndl <klaus.berndl@sdm.de>: Seems not to work with Emacs 20.X
-    (easy-menu-change (list ecb-menu-name)
-                      "Compilation Buffers"
-                      submenu
-                      "Navigate")))
+  (when ecb-compilation-update-menu-p
+    (let ((submenu nil)
+          (buffers (ecb-compilation-get-buffers)))
+      (condition-case nil
+          (progn
+            (setq ecb-compilation-update-menu-p nil)
+            (dolist(buffer buffers)
+              (setq submenu
+                    (append submenu
+                            (list (vector (car buffer)
+                                          `(funcall (if (ecb-compile-window-live-p)
+                                                        'switch-to-buffer
+                                                      'switch-to-buffer-other-window)
+                                                  ,(car buffer))
+                                          :active t)))))
+            
+            ;;TODO: Klaus Berndl <klaus.berndl@sdm.de>: Seems not to work with
+            ;;Emacs 20.X
+            (easy-menu-change (list ecb-menu-name)
+                              "Compilation Buffers"
+                              submenu
+                              "Navigate")
+            t)
+        (error nil)))))
+      
 
 
 (silentcomp-provide 'ecb-compilation)