From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Can window-state-change-hook be considered "buffer ranking" hook? Date: Sun, 29 Dec 2024 19:51:37 +0300 Message-ID: <23c1eb2819c0c31c3658de054d0a37a6.support1@rcdrun.com> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35096"; mail-complaints-to="usenet@ciao.gmane.io" To: Help GNU Emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 29 17:53:47 2024 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tRwXy-00091Y-JF for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 29 Dec 2024 17:53:46 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tRwWt-0008EL-Pz; Sun, 29 Dec 2024 11:52:39 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tRwWX-0008CT-4Z for help-gnu-emacs@gnu.org; Sun, 29 Dec 2024 11:52:18 -0500 Original-Received: from stw1.rcdrun.com ([217.170.207.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tRwWV-0003IW-5f for help-gnu-emacs@gnu.org; Sun, 29 Dec 2024 11:52:16 -0500 Original-Received: from localhost ([::ffff:41.75.177.38]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 000000000007DC1D.0000000067717E1C.00136EB1; Sun, 29 Dec 2024 09:51:39 -0700 Received-SPF: pass client-ip=217.170.207.13; envelope-from=support1@rcdrun.com; helo=stw1.rcdrun.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.227, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:149054 Archived-At: 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 ;; 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 . ;;; 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