From 2f05d5164867700d7826ac9c99d00c0358cc1bbf Mon Sep 17 00:00:00 2001 From: Jonas Bernoulli Date: Sun, 21 Apr 2024 20:30:24 +0200 Subject: [PATCH] forge-tablist.el: Create new library from existing code --- default.mk | 1 + lisp/Makefile | 5 ++- lisp/forge-repos.el | 5 ++- lisp/forge-tablist.el | 85 +++++++++++++++++++++++++++++++++++++++++++ lisp/forge-topics.el | 64 +------------------------------- 5 files changed, 95 insertions(+), 65 deletions(-) create mode 100644 lisp/forge-tablist.el diff --git a/default.mk b/default.mk index 8b9042eb..5b9a2958 100644 --- a/default.mk +++ b/default.mk @@ -19,6 +19,7 @@ ELS += $(PKG)-gogs.el ELS += $(PKG)-bitbucket.el ELS += $(PKG)-semi.el ELS += $(PKG)-commands.el +ELS += $(PKG)-tablist.el ELS += $(PKG)-topics.el ELS += $(PKG)-repos.el ELCS = $(ELS:.el=.elc) diff --git a/lisp/Makefile b/lisp/Makefile index 5c04f42b..9c189bab 100644 --- a/lisp/Makefile +++ b/lisp/Makefile @@ -22,8 +22,9 @@ $(PKG)-gogs.elc: $(PKG).elc $(PKG)-bitbucket.elc: $(PKG).elc $(PKG)-semi.elc: $(PKG).elc $(PKG)-commands.elc: $(PKG).elc -$(PKG)-topics.elc: $(PKG).elc -$(PKG)-repos.elc: $(PKG)-topics.elc +$(PKG)-list.elc: $(PKG).elc +$(PKG)-topics.elc: $(PKG)-tablist.elc +$(PKG)-repos.elc: $(PKG)-tablist.elc %.elc: %.el @printf "Compiling $<\n" diff --git a/lisp/forge-repos.el b/lisp/forge-repos.el index 1bd4a9a7..9f21709f 100644 --- a/lisp/forge-repos.el +++ b/lisp/forge-repos.el @@ -22,7 +22,10 @@ ;;; Code: -(require 'forge-topics) +(require 'hl-line) + +(require 'forge-repo) +(require 'forge-tablist) ;;; Options diff --git a/lisp/forge-tablist.el b/lisp/forge-tablist.el new file mode 100644 index 00000000..31e2561e --- /dev/null +++ b/lisp/forge-tablist.el @@ -0,0 +1,85 @@ +;;; forge-tablist.el --- Tabulated-list interface -*- lexical-binding:t -*- + +;; Copyright (C) 2018-2024 Jonas Bernoulli + +;; Author: Jonas Bernoulli +;; Maintainer: Jonas Bernoulli + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;; This file 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 file 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 file. If not, see . + +;;; Code: + +(require 'tabulated-list) + +(require 'forge) + +(defvar-local forge--buffer-list-type nil) +(defvar-local forge--buffer-list-filter nil) +(defvar-local forge--buffer-list-global nil) + +(defconst forge--tablist-columns-type + '(repeat + (list :tag "Column" + (string :tag "Header Label") + (choice :tag "Value source" + function + (symbol :tag "Object slot")) + (integer :tag "Column Width") + (choice :tag "Sort predicate" + (const :tag "Don't sort" nil) + (const :tag "Default" t) + function) + (plist :tag "Properties" + :key-type (choice :tag "Property" + (const :right-align) + (const :pad-right) + symbol) + :value-type (sexp :tag "Value"))))) + +(defvar-local forge--tabulated-list-columns nil) +(put 'forge--tabulated-list-columns 'permanent-local t) + +(defvar-local forge--tabulated-list-query nil) +(put 'forge--tabulated-list-query 'permanent-local t) + +(defun forge--tablist-refresh () + (setq tabulated-list-format + (vconcat (mapcar (pcase-lambda (`(,name ,_get ,width ,sort ,props)) + `(,name ,width ,sort . ,props)) + forge--tabulated-list-columns))) + (tabulated-list-init-header) + (setq tabulated-list-entries + (mapcar + (lambda (obj) + (list (oref obj id) + (vconcat + (mapcar (pcase-lambda (`(,_name ,get ,_width ,_sort ,_props)) + (let ((val (cond + ((functionp get) + (funcall get obj)) + ((eq (car-safe get) 'repository) + (eieio-oref (forge-get-repository obj) + (cadr get))) + ((eieio-oref obj get))))) + (cond ((stringp val) val) + ((null val) "") + ((format "%s" val))))) + forge--tabulated-list-columns)))) + (funcall forge--tabulated-list-query)))) + +;;; _ +(provide 'forge-tablist) +;;; forge-tablist.el ends here diff --git a/lisp/forge-topics.el b/lisp/forge-topics.el index d5abbdda..a99eb2f9 100644 --- a/lisp/forge-topics.el +++ b/lisp/forge-topics.el @@ -23,10 +23,9 @@ ;;; Code: (require 'hl-line) -(require 'tabulated-list) -(require 'forge) (require 'forge-topic) +(require 'forge-tablist) (defvar x-stretch-cursor) @@ -39,25 +38,6 @@ :type 'hook :options '(hl-line-mode)) -(defconst forge--tablist-columns-type - '(repeat - (list :tag "Column" - (string :tag "Header Label") - (choice :tag "Value source" - function - (symbol :tag "Object slot")) - (integer :tag "Column Width") - (choice :tag "Sort predicate" - (const :tag "Don't sort" nil) - (const :tag "Default" t) - function) - (plist :tag "Properties" - :key-type (choice :tag "Property" - (const :right-align) - (const :pad-right) - symbol) - :value-type (sexp :tag "Value"))))) - (defcustom forge-topic-list-columns '(("#" forge--format-topic-slug 5 nil nil) ("Title" forge--format-topic-title+labels 35 nil nil)) @@ -150,47 +130,7 @@ label, cannot be changed independently of the color used in the forges web interface." :group 'forge-faces) -;;; Variables - -(defvar-local forge--tabulated-list-columns nil) -(put 'forge--tabulated-list-columns 'permanent-local t) - -(defvar-local forge--tabulated-list-query nil) -(put 'forge--tabulated-list-query 'permanent-local t) - -(defvar-local forge--buffer-list-type nil) -(defvar-local forge--buffer-list-filter nil) -(defvar-local forge--buffer-list-global nil) - -;;; Modes -;;;; Common - -(defun forge--tablist-refresh () - (setq tabulated-list-format - (vconcat (mapcar (pcase-lambda (`(,name ,_get ,width ,sort ,props)) - `(,name ,width ,sort . ,props)) - forge--tabulated-list-columns))) - (tabulated-list-init-header) - (setq tabulated-list-entries - (mapcar - (lambda (obj) - (list (oref obj id) - (vconcat - (mapcar (pcase-lambda (`(,_name ,get ,_width ,_sort ,_props)) - (let ((val (cond - ((functionp get) - (funcall get obj)) - ((eq (car-safe get) 'repository) - (eieio-oref (forge-get-repository obj) - (cadr get))) - ((eieio-oref obj get))))) - (cond ((stringp val) val) - ((null val) "") - ((format "%s" val))))) - forge--tabulated-list-columns)))) - (funcall forge--tabulated-list-query)))) - -;;;; Topics +;;; Mode (defvar-keymap forge-topic-list-mode-map :doc "Local keymap for Forge-Topic-List mode buffers."