]> git.eshelyaron.com Git - dict.git/commitdiff
ADDED: sweep Top-level Menu for listing Prolog top-levels
authorEshel Yaron <me@eshelyaron.com>
Mon, 3 Oct 2022 15:55:36 +0000 (18:55 +0300)
committerEshel Yaron <me@eshelyaron.com>
Mon, 3 Oct 2022 20:56:03 +0000 (23:56 +0300)
* sweep.pl
- sweep_top_level_threads/2: new predicate for querying
currently active Prolog top-level threads

* sweeprolog.el
- sweeprolog-top-level-menu--entries: new function, exposing
sweep_top_level_threads/2 to Elisp.
- sweeprolog-top-level-menu-mode: new major mode derived from
tabulated-list-mode
- sweeprolog-top-level-menu-go-to: new command, switches to the
top-level buffer of the Top-level Menu entry at point
- sweeprolog-top-level-menu-kill: new command, kills the top-level
buffer of the Top-level Menu entry at point
- sweeprolog-top-level-menu-new: new command, creates a new top-level
from the Top-level Menu
- sweeprolog-list-top-levels: new command, opens the Top-level Menu

sweep.pl
sweeprolog.el

index 763bdd003b3f9bcb6a1e72194c1d2811e54c6535..b119f9c5ae9fa807a13b6d7ff82801c0910f2b18 100644 (file)
--- a/sweep.pl
+++ b/sweep.pl
@@ -57,6 +57,7 @@
             sweep_imenu_index/2,
             sweep_module_path/2,
             sweep_top_level_server/2,
+            sweep_top_level_threads/2,
             sweep_accept_top_level_client/2,
             write_sweep_module_location/0
           ]).
@@ -99,6 +100,15 @@ prolog:xref_open_source(Source, Stream) :-
 prolog:xref_close_source(Source, Stream) :-
     sweep_open(Source, Stream).
 
+sweep_top_level_threads(_, Ts) :-
+    findall([Id, Buffer, Status, Stack, CPUTime],
+            (   sweep_top_level_thread_buffer(Id, Buffer),
+                thread_property(Id, status(Status0)),
+                term_string(Status0, Status),
+                thread_statistics(Id, stack, Stack),
+                thread_statistics(Id, cputime, CPUTime)
+            ),
+            Ts).
 
 sweep_current_prolog_flags(Sub, Flags) :-
     findall([Flag|Value],
index 993fd097cc0a3c0e9ef4ce5504d577c2368a44e5..f9308c8630e72fe85368b9e6d6a6c903c6b0e67c 100644 (file)
@@ -2480,6 +2480,85 @@ variable at point, if any."
                str
                "\n\n*/\n\n"))
 
+(defun sweeprolog-top-level-menu--entries ()
+  (sweeprolog-open-query "user"
+                         "sweep"
+                         "sweep_top_level_threads"
+                         nil)
+  (let ((sol (sweeprolog-next-solution)))
+    (sweeprolog-close-query)
+    (when (sweeprolog-true-p sol)
+      (mapcar (lambda (th)
+                (let ((id (nth 0 th))
+                      (bn (nth 1 th))
+                      (st (nth 2 th))
+                      (sz (number-to-string (nth 3 th)))
+                      (ct (number-to-string (nth 4 th))))
+                  (list id (vector bn st sz ct))))
+              (cdr sol)))))
+
+(defun sweeprolog-top-level-menu--refresh ()
+  (tabulated-list-init-header)
+  (setq tabulated-list-entries (sweeprolog-top-level-menu--entries)))
+
+(defun sweeprolog-top-level-menu-new (name)
+  "Create and switch to a new Prolog top-level buffer named NAME."
+  (interactive (list
+                (read-string
+                 "New top-level buffer name: "
+                 nil nil
+                 (generate-new-buffer-name "*sweeprolog-top-level*")))
+               sweeprolog-top-level-menu-mode)
+  (sweeprolog-top-level name))
+
+(defun sweeprolog-top-level-menu-kill ()
+  "Kill the buffer of to the `sweep' Top-level Menu entry at point."
+  (interactive "" sweeprolog-top-level-menu-mode)
+  (if-let ((vec (tabulated-list-get-entry)))
+      (let ((bn (seq-elt vec 0)))
+        (kill-buffer bn))
+    (user-error "No top-level menu entry here")))
+
+(defun sweeprolog-top-level-menu-go-to ()
+  "Go to the buffer of to the `sweep' Top-level Menu entry at point."
+  (interactive "" sweeprolog-top-level-menu-mode)
+  (if-let ((vec (tabulated-list-get-entry)))
+      (let* ((bn (seq-elt vec 0))
+             (buf (get-buffer bn)))
+        (if (buffer-live-p buf)
+            (pop-to-buffer buf sweeprolog-top-level-display-action)
+          (message "Buffer %s is no longer availabe." bn)))
+    (user-error "No top-level menu entry here")))
+
+(defvar-keymap sweeprolog-top-level-menu-mode-map
+  :doc "Local keymap for `sweeprolog-top-level-menu-mode' buffers."
+  "RET" #'sweeprolog-top-level-menu-go-to
+  "k"   #'sweeprolog-top-level-menu-kill
+  "t"   #'sweeprolog-top-level-menu-new)
+
+(define-derived-mode sweeprolog-top-level-menu-mode
+  tabulated-list-mode "sweep Top-level Menu"
+  "Major mode for browsing a list of active `sweep' top-levels."
+  (setq tabulated-list-format [("Buffer"      32 t)
+                               ("Status"      32 t)
+                               ("Stacks Size" 20 t)
+                               ("CPU Time"    20 t)])
+  (setq tabulated-list-padding 2)
+  (setq tabulated-list-sort-key (cons "Buffer" nil))
+  (add-hook 'tabulated-list-revert-hook
+            #'sweeprolog-top-level-menu--refresh nil t)
+  (tabulated-list-init-header))
+
+(defun sweeprolog-list-top-levels ()
+  "Display a list of Prolog top-level threads."
+  (interactive)
+  (let ((buf (get-buffer-create "*sweep Top-levels*")))
+    (with-current-buffer buf
+      (sweeprolog-top-level-menu-mode)
+      (sweeprolog-top-level-menu--refresh)
+      (tabulated-list-print))
+    (pop-to-buffer-same-window buf)))
+
 (provide 'sweeprolog)
 
 ;;; sweeprolog.el ends here