bui.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; bui.el --- Buffer interface library -*- lexical-binding: t -*-
  2. ;; Copyright © 2014-2018 Alex Kost <alezost@gmail.com>
  3. ;; Author: Alex Kost <alezost@gmail.com>
  4. ;; Version: 1.2.1
  5. ;; URL: https://github.com/alezost/bui.el
  6. ;; Keywords: tools
  7. ;; Package-Requires: ((emacs "24.3") (dash "2.11.0"))
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; BUI (Buffer User Interface) is a library for making 'list' (similar
  22. ;; to "M-x list-packages") and 'info' (similar to customization buffers)
  23. ;; interfaces to display various data (packages, buffers, functions,
  24. ;; etc.).
  25. ;;
  26. ;; It is not an end-user package, it is a library that is intended to be
  27. ;; used by other packages.
  28. ;;
  29. ;; Basically, at first you define 'list'/'info' interface using
  30. ;; `bui-define-interface' macro, and then you can make user commands
  31. ;; that will display entries using `bui-get-display-entries' and similar
  32. ;; functions.
  33. ;;
  34. ;; See README at <https://github.com/alezost/bui.el> for more details.
  35. ;;; Code:
  36. (require 'cl-lib)
  37. ;; Require all features, so a package maker can require only `bui'.
  38. (require 'bui-button)
  39. (require 'bui-core)
  40. (require 'bui-entry)
  41. (require 'bui-info)
  42. (require 'bui-list)
  43. (require 'bui-utils)
  44. (defmacro bui-define-entry-type (entry-type &rest args)
  45. "Define variables for ENTRY-TYPE.
  46. ARGS can be the same arguments as for `bui-define-interface'.
  47. The difference is: arguments for `bui-define-interface' define
  48. specific variables for different buffer types, while this macro
  49. defines general variables used for any buffer type."
  50. (declare (indent 1))
  51. (bui-plist-let args
  52. ((reduced? :reduced?))
  53. `(progn
  54. ,@(bui-map-symbol-specifications
  55. (lambda (key suffix generate)
  56. (let ((val (plist-get %foreign-args key)))
  57. (when (or val (bui-symbol-generate? generate reduced?))
  58. (bui-inherit-defvar-clause
  59. (bui-entry-symbol entry-type suffix)
  60. (bui-make-symbol 'bui suffix)
  61. :value val
  62. :group entry-type))))
  63. bui-entry-symbol-specifications)
  64. ,@(bui-map-symbol-specifications
  65. (lambda (key suffix _generate)
  66. (let ((val (plist-get %foreign-args key)))
  67. (when val
  68. (bui-inherit-defvar-clause
  69. (bui-entry-symbol entry-type suffix)
  70. (bui-make-symbol 'bui suffix)
  71. :value val
  72. :group entry-type))))
  73. bui-symbol-specifications))))
  74. (defmacro bui-define-interface (entry-type buffer-type &rest args)
  75. "Define BUFFER-TYPE interface for displaying ENTRY-TYPE entries.
  76. Remaining arguments ARGS should have a form [KEYWORD VALUE] ...
  77. They are used to generate variables specific for the defined
  78. interface. For more details and the available keywords, see
  79. `bui-symbol-specifications', `bui-entry-symbol-specifications'
  80. and `bui-BUFFER-TYPE-symbol-specifications'.
  81. `:get-entries-function' is the only required keyword (if the
  82. interface is reduced, all keywords become optional).
  83. To denote that the interface is reduced, a special `:reduced?'
  84. keyword may be specified. If it is non-nil, generate only
  85. customization group, faces group and specified variables. If it
  86. is nil, along with the mentioned groups and variables,
  87. `ENTRY-TYPE-BUFFER-TYPE-mode' will be generated."
  88. (declare (indent 2))
  89. (cl-flet ((name (&rest symbols)
  90. (apply #'bui-symbol entry-type buffer-type symbols))
  91. (bui-name (&rest symbols)
  92. (apply #'bui-make-symbol 'bui symbols)))
  93. (let ((group (name))
  94. (faces-group (name 'faces))
  95. (mode (name 'mode))
  96. (mode-map (name 'mode-map))
  97. (bui-buffer-type (bui-name buffer-type))
  98. (symbol-fun (bui-name buffer-type 'symbol))
  99. (symbol-specs (bui-name buffer-type 'symbol-specifications))
  100. (parent-mode (bui-name buffer-type 'mode)))
  101. (bui-plist-let args
  102. ((mode-name :mode-name (capitalize (symbol-name group)))
  103. (reduced? :reduced?))
  104. `(progn
  105. (defgroup ,group nil
  106. ,(format "Displaying '%S' entries in '%S' buffer."
  107. entry-type buffer-type)
  108. :group ',entry-type
  109. :group ',bui-buffer-type)
  110. (defgroup ,faces-group nil
  111. ,(format "Faces for displaying '%S' entries in '%S' buffer."
  112. entry-type buffer-type)
  113. :group ',group
  114. :group ',(bui-entry-symbol entry-type 'faces)
  115. :group ',(bui-name buffer-type 'faces))
  116. ,@(bui-map-symbol-specifications
  117. (lambda (key suffix generate)
  118. (let ((val (plist-get %foreign-args key)))
  119. (when (or val (bui-symbol-generate? generate reduced?))
  120. (bui-inherit-defvar-clause
  121. (name suffix)
  122. (bui-name suffix)
  123. :value val
  124. :group group))))
  125. bui-symbol-specifications)
  126. ,@(bui-map-symbol-specifications
  127. (lambda (key suffix _generate)
  128. (let ((val (plist-get %foreign-args key)))
  129. (when val
  130. (bui-inherit-defvar-clause
  131. (name suffix)
  132. (bui-name suffix)
  133. :value val
  134. :group group))))
  135. bui-entry-symbol-specifications)
  136. ,@(bui-map-symbol-specifications
  137. (lambda (key suffix generate)
  138. (let ((val (plist-get args key)))
  139. (when (or val (bui-symbol-generate? generate reduced?))
  140. (bui-inherit-defvar-clause
  141. (funcall symbol-fun entry-type suffix)
  142. (bui-name buffer-type suffix)
  143. :value val
  144. :group group))))
  145. (symbol-value symbol-specs))
  146. ,(unless reduced?
  147. `(define-derived-mode ,mode ,parent-mode
  148. '(,mode-name (bui-active-filter-predicates
  149. bui-filter-mode-line-string))
  150. ,(format "\
  151. Major mode for displaying '%S' entries in '%S' buffer.
  152. \\{%S}"
  153. entry-type buffer-type mode-map)
  154. (bui-mode-initialize ',entry-type ',buffer-type)))
  155. (bui-register-interface ',entry-type ',buffer-type))))))
  156. (provide 'bui)
  157. ;;; bui.el ends here