From 3752dd653906f2d93e56eff9ff23dec05bd6c615 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Fri, 7 Jun 2024 19:57:07 +0300 Subject: [PATCH] * lisp/buff-menu.el (Buffer-menu-group-sort-by): New defcustom. (list-buffers--refresh): Use Buffer-menu-group-sort-by instead of the hard-coded function (bug#70150). (Buffer-menu-group-sort-alphabetically): New function as an option for 'Buffer-menu-group-sort-by'. (list-buffers-noselect): Remove setting outline-minor-mode-use-buttons to 'in-margins' that it not required for this feature to work correctly. * lisp/emacs-lisp/tabulated-list.el (tabulated-list-groups-sort): Add optional argument 'level'. (cherry picked from commit 9cd182dae8822e810b73b2c1c1c41955096bb2de) --- lisp/buff-menu.el | 24 ++++++++++++++++++------ lisp/emacs-lisp/tabulated-list.el | 7 ++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lisp/buff-menu.el b/lisp/buff-menu.el index c35fa42934c..fa706a4e1a7 100644 --- a/lisp/buff-menu.el +++ b/lisp/buff-menu.el @@ -119,6 +119,19 @@ If this is nil, buffers are not divided into groups." :group 'Buffer-menu :version "30.1") +(defcustom Buffer-menu-group-sort-by nil + "If non-nil, function to sort buffer-menu groups by name. +Each function is called with two arguments: an alist of groups +where an alist key is a group name and also the level as a number, +and should return the same alist where groups are sorted. +If this is nil, group names are unsorted." + :type '(choice (const :tag "No group sorting" nil) + (const :tag "Sort groups alphabetically" + Buffer-menu-group-sort-alphabetically) + (function :tag "Custom function")) + :group 'Buffer-menu + :version "30.1") + (defvar-local Buffer-menu-files-only nil "Non-nil if the current Buffer Menu lists only file buffers. This is set by the prefix argument to `buffer-menu' and related @@ -759,8 +772,7 @@ See more at `Buffer-menu-filter-predicate'." (tabulated-list-print) (when tabulated-list-groups (setq-local outline-minor-mode-cycle t - outline-minor-mode-highlight t - outline-minor-mode-use-buttons 'in-margins) + outline-minor-mode-highlight t) (outline-minor-mode 1))) buffer)) @@ -845,10 +857,7 @@ See more at `Buffer-menu-filter-predicate'." ,(lambda (entry) (list (mapcar (lambda (f) (funcall f entry)) Buffer-menu-group-by))) - :sort-function - ,(lambda (groups) - ;; Sort groups by name - (sort groups :key #'car :in-place t)))))) + :sort-function ,Buffer-menu-group-sort-by)))) (tabulated-list-init-header)) (defun tabulated-list-entry-size-> (entry1 entry2) @@ -881,4 +890,7 @@ See more at `Buffer-menu-filter-predicate'." (project-root project) default-directory))) +(defun Buffer-menu-group-sort-alphabetically (groups _level) + (sort groups :in-place t :key #'car)) + ;;; buff-menu.el ends here diff --git a/lisp/emacs-lisp/tabulated-list.el b/lisp/emacs-lisp/tabulated-list.el index a0a58bf8b42..30397137efb 100644 --- a/lisp/emacs-lisp/tabulated-list.el +++ b/lisp/emacs-lisp/tabulated-list.el @@ -929,14 +929,15 @@ where every string is an outline heading at increasing level of deepness." (cl-pushnew entry (gethash path hash)))) (trie-get tree nil)))) -(defun tabulated-list-groups-sort (tree sort-function) +(defun tabulated-list-groups-sort (tree sort-function &optional level) "Sort TREE using the sort function SORT-FUN." + (unless level (setq level 1)) (mapcar (lambda (elt) (if (vectorp (cdr elt)) elt (cons (car elt) (tabulated-list-groups-sort - (cdr elt) sort-function)))) - (funcall sort-function tree))) + (cdr elt) sort-function (1+ level))))) + (funcall sort-function tree level))) (defun tabulated-list-groups-flatten (tree) "Flatten multi-level TREE to single level." -- 2.39.5