From: Jean Louis <bugs@gnu.support>
To: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Can window-state-change-hook be considered "buffer ranking" hook?
Date: Sun, 29 Dec 2024 19:51:37 +0300 [thread overview]
Message-ID: <23c1eb2819c0c31c3658de054d0a37a6.support1@rcdrun.com> (raw)
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
next reply other threads:[~2024-12-29 16:51 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-29 16:51 Jean Louis [this message]
2024-12-29 19:42 ` Can window-state-change-hook be considered "buffer ranking" hook? Stefan Monnier via Users list for the GNU Emacs text editor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=23c1eb2819c0c31c3658de054d0a37a6.support1@rcdrun.com \
--to=bugs@gnu.support \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).