guix-ui.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ;;; guix-ui.el --- Common code for Guix package management interface -*- lexical-binding: t -*-
  2. ;; Copyright © 2014–2017 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides some general code for 'list'/'info' interfaces for
  18. ;; packages and generations.
  19. ;;; Code:
  20. (require 'cl-lib)
  21. (require 'bui)
  22. (require 'guix nil t)
  23. (require 'guix-repl)
  24. (require 'guix-guile)
  25. (require 'guix-utils)
  26. (require 'guix-ui-messages)
  27. (require 'guix-profiles)
  28. (defvar guix-ui-map
  29. (let ((map (make-sparse-keymap)))
  30. (define-key map (kbd "M") 'guix-apply-manifest)
  31. (define-key map (kbd "C-c C-z") 'guix-switch-to-repl)
  32. map)
  33. "Parent keymap for Guix package/generation buffers.")
  34. (bui-define-current-args-accessors
  35. guix-ui-current profile search-type search-values)
  36. (defun guix-ui-read-package-profile ()
  37. "Return `guix-current-profile' or prompt for it.
  38. This function is intended for using in `interactive' forms."
  39. (if current-prefix-arg
  40. (guix-read-package-profile)
  41. guix-current-profile))
  42. (defun guix-ui-read-generation-profile ()
  43. "Return `guix-current-profile' or prompt for it.
  44. This function is intended for using in `interactive' forms."
  45. (if current-prefix-arg
  46. (guix-read-generation-profile)
  47. guix-current-profile))
  48. (defun guix-ui-get-entries (profile entry-type search-type search-values
  49. &optional params)
  50. "Receive ENTRY-TYPE entries for PROFILE.
  51. Call an appropriate scheme procedure and return a list of entries.
  52. ENTRY-TYPE should be one of the following symbols: `package' or
  53. `output'.
  54. SEARCH-TYPE may be one of the following symbols: `id', `name',
  55. `regexp', `all-available', `newest-available', `installed',
  56. `obsolete', `license', `location', `from-file', `from-os-file'.
  57. PARAMS is a list of parameters for receiving. If nil, get data
  58. with all available parameters."
  59. (guix-eval-read
  60. (guix-make-guile-expression
  61. 'package/output-sexps
  62. profile entry-type search-type search-values params)))
  63. (defun guix-ui-list-describe (&rest ids)
  64. "Describe 'ui' entries with IDS (list of identifiers)."
  65. (bui-get-display-entries
  66. (bui-current-entry-type) 'info
  67. (cl-list* (guix-ui-current-profile) 'id ids)
  68. 'add))
  69. ;;; Buffers
  70. (defcustom guix-ui-buffer-name-function
  71. #'guix-ui-buffer-name-full
  72. "Function used to define a name of a Guix buffer.
  73. The function is called with 2 arguments: BASE-NAME and PROFILE."
  74. :type '(choice (function-item guix-ui-buffer-name-full)
  75. (function-item guix-ui-buffer-name-short)
  76. (function-item guix-ui-buffer-name-simple)
  77. (function :tag "Other function"))
  78. :group 'guix)
  79. (defun guix-ui-buffer-name-simple (base-name &rest _)
  80. "Return BASE-NAME."
  81. base-name)
  82. (defun guix-ui-buffer-name-short (base-name profile)
  83. "Return buffer name by appending BASE-NAME and PROFILE's base file name."
  84. (guix-compose-buffer-name base-name
  85. (file-name-base (directory-file-name profile))))
  86. (defun guix-ui-buffer-name-full (base-name profile)
  87. "Return buffer name by appending BASE-NAME and PROFILE's full name."
  88. (guix-compose-buffer-name base-name profile))
  89. (defun guix-ui-buffer-name (base-name profile)
  90. "Return Guix buffer name based on BASE-NAME and profile.
  91. See `guix-ui-buffer-name-function' for details."
  92. (funcall guix-ui-buffer-name-function
  93. base-name profile))
  94. ;;; Interface definers
  95. (defmacro guix-ui-define-entry-type (entry-type &rest args)
  96. "Define general code for ENTRY-TYPE.
  97. Remaining arguments (ARGS) should have a form [KEYWORD VALUE] ...
  98. They are passed to `bui-define-entry-type' macro.
  99. This macro also defines:
  100. - `guix-TYPE-message' - a wrapper around `guix-result-message'."
  101. (declare (indent 1))
  102. (let* ((entry-type-str (symbol-name entry-type))
  103. (full-entry-type (guix-make-symbol entry-type))
  104. (prefix (concat "guix-" entry-type-str))
  105. (message-fun (intern (concat prefix "-message"))))
  106. `(progn
  107. (defun ,message-fun (entries profile search-type
  108. &rest search-values)
  109. ,(format "\
  110. Display a message after showing '%s' entries.
  111. This is a wrapper for `guix-result-message'."
  112. entry-type-str)
  113. (guix-result-message profile entries ',entry-type
  114. search-type search-values))
  115. (guix-define-groups ,entry-type)
  116. (bui-define-entry-type ,full-entry-type
  117. :message-function ',message-fun
  118. ,@args))))
  119. (defmacro guix-ui-define-interface (entry-type buffer-type &rest args)
  120. "Define BUFFER-TYPE interface for displaying ENTRY-TYPE entries.
  121. Remaining arguments (ARGS) should have a form [KEYWORD VALUE] ...
  122. In the following description TYPE means ENTRY-TYPE-BUFFER-TYPE.
  123. Required keywords:
  124. - `:buffer-name' - base part of a buffer name. It is used in a
  125. generated `guix-TYPE-buffer-name' function; see
  126. `guix-ui-buffer-name' for details.
  127. Optional keywords:
  128. - `:required' - default value of the generated
  129. `guix-TYPE-required-params' variable.
  130. The rest keyword arguments are passed to `bui-define-interface'
  131. macro.
  132. Along with the mentioned definitions, this macro also defines:
  133. - `guix-TYPE-mode-map' - keymap based on `guix-ui-map' and
  134. `bui-BUFFER-TYPE-mode-map'."
  135. (declare (indent 2))
  136. (let* ((entry-type-str (symbol-name entry-type))
  137. (buffer-type-str (symbol-name buffer-type))
  138. (prefix (concat "guix-" entry-type-str "-"
  139. buffer-type-str))
  140. (mode-str (concat prefix "-mode"))
  141. (mode-map (intern (concat mode-str "-map")))
  142. (parent-map (intern (format "bui-%s-mode-map"
  143. buffer-type-str)))
  144. (required-var (intern (concat prefix "-required-params")))
  145. (buffer-name-fun (intern (concat prefix "-buffer-name"))))
  146. (bui-plist-let args
  147. ((buffer-name-val :buffer-name)
  148. (required-val :required ''(id)))
  149. `(progn
  150. (defvar ,mode-map
  151. (let ((map (make-sparse-keymap)))
  152. (set-keymap-parent
  153. map (make-composed-keymap ,parent-map guix-ui-map))
  154. map)
  155. ,(format "Keymap for `%s' buffers." mode-str))
  156. (defvar ,required-var ,required-val
  157. ,(format "\
  158. List of the required '%s' parameters.
  159. These parameters are received from the Scheme side
  160. along with the displayed parameters.
  161. Do not remove `id' from this list as it is required for
  162. identifying an entry."
  163. entry-type-str))
  164. (defun ,buffer-name-fun (profile &rest _)
  165. ,(format "\
  166. Return a name of '%s' buffer for displaying '%s' entries.
  167. See `guix-ui-buffer-name' for details."
  168. buffer-type-str entry-type-str)
  169. (guix-ui-buffer-name ,buffer-name-val profile))
  170. (bui-define-interface ,(guix-make-symbol entry-type) ,buffer-type
  171. :buffer-name ',buffer-name-fun
  172. ,@%foreign-args)))))
  173. (defvar guix-ui-font-lock-keywords
  174. (eval-when-compile
  175. `((,(rx "(" (group (or "guix-ui-define-entry-type"
  176. "guix-ui-define-interface"))
  177. symbol-end)
  178. . 1))))
  179. (font-lock-add-keywords 'emacs-lisp-mode guix-ui-font-lock-keywords)
  180. (provide 'guix-ui)
  181. ;;; guix-ui.el ends here