* Can window-state-change-hook be considered "buffer ranking" hook?
@ 2024-12-29 16:51 Jean Louis
2024-12-29 19:42 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 2+ messages in thread
From: Jean Louis @ 2024-12-29 16:51 UTC (permalink / raw)
To: Help GNU Emacs
I have made this program to rank my buffers by visiting or switching
to them. Each time I switch to buffer, the buffer rank increases and
menu is generated with those buffers mostly visited, but also saved,
etc.
I am using `window-state-change-hook' to "detect" that buffer is used.
I have not found other hooks to be used, so I am asking here if this
approach may be wrong, maybe I should use some other "signal" or
indication that buffer should be ranked.
I like to rank buffers, though I still do not know if this program
will give me what I need, and I need to adapt it. Basically, I am
switching to some buffers for monitoring purposes, and exactly those
where I switched too often, disappeared from main buffer menu, that is
why I am thinking having separate ranking buffer menu.
Each buffer used, saved, to which attention is brought, that shall get
increased rank, and by rank those 10-20 should be displayed in menu.
Question is if I should use maybe some other indication or hook, to
rank the buffer?
;;; rcd-buffer-rank.el --- RCD Buffer Rank -*- lexical-binding: t; -*-
;; Copyright (C) 2024 by Jean Louis
;; Author: Jean Louis <bugs@gnu.support>
;; Version: 0.1
;; Package-Requires: (rcd-utilities)
;; Keywords: convenience extensions
;; URL:
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Change Log:
;;; Code:
(defvar-local rcd-buffer-rank 0
"Buffer rank variable.")
(put 'rcd-buffer-rank 'permanent-local t)
(setq-default rcd-buffer-rank 0)
(defvar rcd-buffer-rank-hash (make-hash-table)
"Buffer list with ranks.")
(defvar rcd-buffer-rank-exclude '("*Minibuf"))
(defun rcd-buffer-rank-increase ()
"Increase the rank of the buffer."
(let* ((buffer (current-buffer))
(buffer-name (buffer-name buffer)))
(unless (string-match-p (rx "*Minibuf") buffer-name)
(message "CURRENT:%s" buffer)
(setq rcd-buffer-rank (1+ rcd-buffer-rank))
(puthash buffer rcd-buffer-rank rcd-buffer-rank-hash)
(message "Increased rank to %s" rcd-buffer-rank)
(rcd-buffer-rank-menu-refresh))))
(defun rcd-buffer-rank-switch-to-buffer (name)
"Switch to buffer, function used in menu."
(cond ((buffer-live-p (get-buffer name))
(switch-to-buffer name))
(t (rcd-buffer-rank-menu-refresh))))
(defvar rcd-buffer-rank-maximum 10)
(defun rcd-buffer-rank-menu ()
"Generate menu."
(let* ((keys (hash-table-keys rcd-buffer-rank-hash))
(menu (list "Buffer by rank"))
(keys (sort keys (lambda (a b)
(< (gethash a rcd-buffer-rank-hash)
(gethash b rcd-buffer-rank-hash)))))
(keys (reverse keys))
(max 0))
(while (and keys (<= max rcd-buffer-rank-maximum))
(let* ((key (pop keys))
(name (buffer-name key)))
(setq max (1+ max))
(cond ((not (buffer-live-p key))
(remhash key rcd-buffer-rank-hash))
((and (buffer-live-p key) (not (string-match-p (rx "*Minibuf") name)))
(setq menu (append menu `([,name (lambda () (interactive) (switch-to-buffer ,name)) t]))))
(t (message "Unexpected")))))
menu))
(defun rcd-buffer-rank-menu-refresh ()
"Refresh menu."
(easy-menu-define buffer-rank-menu global-map "Buffers by rank"
(rcd-buffer-rank-menu)))
(defun rcd-buffer-rank-start ()
"Start buffer ranking system."
(interactive)
(add-hook 'window-state-change-hook 'rcd-buffer-rank-increase)
;; on kill, update menu
(message "Started `rcd-buffer-rank'."))
(defun rcd-buffer-rank-stop ()
"Stop the buffer ranking system."
(interactive)
(remove-hook 'window-state-change-hook 'rcd-buffer-rank-increase)
(message "Stopped `rcd-buffer-rank'."))
(provide 'rcd-buffer-rank)
;;; rcd-buffer-rank.el ends here
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Can window-state-change-hook be considered "buffer ranking" hook?
2024-12-29 16:51 Can window-state-change-hook be considered "buffer ranking" hook? Jean Louis
@ 2024-12-29 19:42 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-12-29 19:42 UTC (permalink / raw)
To: help-gnu-emacs
> I have not found other hooks to be used, so I am asking here if this
> approach may be wrong, maybe I should use some other "signal" or
> indication that buffer should be ranked.
I suspect you're fine, but you could also use `buffer-list-update-hook`.
Stefan
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-12-29 19:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-29 16:51 Can window-state-change-hook be considered "buffer ranking" hook? Jean Louis
2024-12-29 19:42 ` Stefan Monnier via Users list for the GNU Emacs text editor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).