guix-ui-license.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; guix-ui-license.el --- Interface for displaying licenses -*- lexical-binding: t -*-
  2. ;; Copyright © 2016–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 'list'/'info' interface for displaying licenses of
  18. ;; Guix packages.
  19. ;;; Code:
  20. (require 'bui)
  21. (require 'guix nil t)
  22. (require 'guix-repl)
  23. (require 'guix-guile)
  24. (require 'guix-license)
  25. (require 'guix-utils)
  26. (guix-define-groups license)
  27. (bui-define-entry-type guix-license
  28. :message-function 'guix-license-message
  29. :titles '((url . "URL")))
  30. (defun guix-license-get-entries (search-type &rest args)
  31. "Receive 'license' entries.
  32. SEARCH-TYPE may be one of the following symbols: `all', `id', `name'."
  33. (guix-eval-read
  34. (apply #'guix-make-guile-expression
  35. 'license-sexps search-type args)))
  36. (defun guix-license-get-display (search-type &rest args)
  37. "Search for licenses and show results."
  38. (apply #'bui-list-get-display-entries
  39. 'guix-license search-type args))
  40. (defun guix-license-message (entries _search-type &rest args)
  41. "Display a message after showing license ENTRIES."
  42. ;; Some objects in (guix licenses) module are procedures (e.g.,
  43. ;; 'non-copyleft' or 'x11-style'). Such licenses cannot be "described".
  44. (when (null entries)
  45. (if (cdr args)
  46. (message "Unknown licenses.")
  47. (message "Unknown license."))))
  48. ;;; License 'info'
  49. (bui-define-interface guix-license info
  50. :mode-name "License-Info"
  51. :buffer-name "*Guix License Info*"
  52. :get-entries-function 'guix-license-get-entries
  53. :format '((name nil (simple bui-info-heading))
  54. nil
  55. guix-license-insert-packages-button
  56. (url nil (simple bui-url))
  57. guix-license-insert-comment
  58. nil
  59. guix-license-insert-file))
  60. (declare-function guix-packages-by-license "guix-ui-package" t)
  61. (defun guix-license-insert-packages-button (entry)
  62. "Insert button to display packages by license ENTRY."
  63. (let ((license (bui-entry-value entry 'name)))
  64. (bui-insert-action-button
  65. "Packages"
  66. (lambda (btn)
  67. (guix-packages-by-license (button-get btn 'license)))
  68. (format "Display packages with license '%s'" license)
  69. 'license license))
  70. (bui-newline))
  71. (defun guix-license-insert-comment (entry)
  72. "Insert 'comment' of a license ENTRY."
  73. (let ((comment (bui-entry-value entry 'comment)))
  74. (if (and comment
  75. (string-match-p "^http" comment))
  76. (bui-info-insert-value-simple comment 'bui-url)
  77. (bui-info-insert-title-simple (bui-current-param-title 'comment))
  78. (bui-info-insert-value-indent comment)))
  79. (bui-newline))
  80. (defun guix-license-insert-file (entry)
  81. "Insert button to open license definition."
  82. (let ((license (bui-entry-value entry 'name)))
  83. (bui-insert-button
  84. (guix-license-file) 'bui-file
  85. 'help-echo (format "Open definition of license '%s'" license)
  86. 'action (lambda (btn)
  87. (guix-find-license-definition (button-get btn 'license)))
  88. 'license license))
  89. (bui-newline))
  90. ;;; License 'list'
  91. (bui-define-interface guix-license list
  92. :mode-name "License-List"
  93. :buffer-name "*Guix Licenses*"
  94. :get-entries-function 'guix-license-get-entries
  95. :describe-function 'guix-license-list-describe
  96. :format '((name nil 40 t)
  97. (url bui-list-get-url 50 t))
  98. :titles '((name . "License"))
  99. :hint 'guix-license-list-hint
  100. :sort-key '(name))
  101. (let ((map guix-license-list-mode-map))
  102. (define-key map (kbd "e") 'guix-license-list-edit)
  103. (define-key map (kbd "P") 'guix-license-list-show-packages))
  104. (defvar guix-license-list-default-hint
  105. '(("\\[guix-license-list-show-packages]") " show packages;\n"
  106. ("\\[guix-license-list-edit]") " edit (go to) the license definition;\n"))
  107. (defun guix-license-list-hint ()
  108. (bui-format-hints
  109. guix-license-list-default-hint
  110. (bui-list-hint)
  111. bui-common-hint))
  112. (defun guix-license-list-describe (&rest ids)
  113. "Describe licenses with IDS (list of identifiers)."
  114. (bui-display-entries
  115. (bui-entries-by-ids (bui-current-entries) ids)
  116. 'guix-license 'info (cons 'id ids)))
  117. (defun guix-license-list-show-packages ()
  118. "Display packages with the license at point."
  119. (interactive)
  120. (guix-packages-by-license (bui-list-current-id)))
  121. (defun guix-license-list-edit (&optional directory)
  122. "Go to the location of the current license definition.
  123. See `guix-license-file' for the meaning of DIRECTORY."
  124. (interactive (list (guix-read-directory)))
  125. (guix-find-license-definition (bui-list-current-id) directory))
  126. ;;; Interactive commands
  127. (defun guix-licenses-show ()
  128. "Display licenses of the Guix packages.
  129. Unlike `guix-licenses', this command always recreates
  130. `guix-license-list-buffer-name' buffer."
  131. (interactive)
  132. (guix-license-get-display 'all))
  133. ;;;###autoload
  134. (defun guix-licenses ()
  135. "Display licenses of the Guix packages.
  136. Switch to the `guix-license-list-buffer-name' buffer if it
  137. already exists."
  138. (interactive)
  139. (guix-switch-to-buffer-or-funcall
  140. guix-license-list-buffer-name #'guix-licenses-show 'message))
  141. (provide 'guix-ui-license)
  142. ;;; guix-ui-license.el ends here