From 6d8659a1ff85a10d15cf0411131080706fb019f1 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Mon, 3 Oct 2022 18:55:36 +0300 Subject: [PATCH] ADDED: sweep Top-level Menu for listing Prolog top-levels * 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 | 10 +++++++ sweeprolog.el | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/sweep.pl b/sweep.pl index 763bdd0..b119f9c 100644 --- 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], diff --git a/sweeprolog.el b/sweeprolog.el index 993fd09..f9308c8 100644 --- a/sweeprolog.el +++ b/sweeprolog.el @@ -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 -- 2.39.5