PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/elisp/guix-prettify.el

https://gitlab.com/emacs-guix/emacs-guix
Emacs Lisp | 212 lines | 117 code | 42 blank | 53 comment | 11 complexity | b2c2041f01e7d0ca3ec013fd1bab9695 MD5 | raw file
  1. ;;; guix-prettify.el --- Prettify Guix store file names
  2. ;; Copyright © 2014, 2015, 2017, 2020 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 package provides minor-mode for prettifying Guix store file
  18. ;; names i.e., after enabling `guix-prettify-mode',
  19. ;; '/gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1' names will be
  20. ;; replaced with '/gnu/store/…-foo-0.1' in the current buffer. There is
  21. ;; also `global-guix-prettify-mode' for global prettifying.
  22. ;; To install, add the following to your emacs init file:
  23. ;;
  24. ;; (add-to-list 'load-path "/path/to/dir-with-guix-prettify")
  25. ;; (autoload 'guix-prettify-mode "guix-prettify" nil t)
  26. ;; (autoload 'global-guix-prettify-mode "guix-prettify" nil t)
  27. ;; If you want to enable/disable composition after "M-x font-lock-mode",
  28. ;; use the following setting:
  29. ;;
  30. ;; (setq font-lock-extra-managed-props
  31. ;; (cons 'composition font-lock-extra-managed-props))
  32. ;; Credits:
  33. ;;
  34. ;; Thanks to Ludovic Courtès for the idea of this package.
  35. ;;
  36. ;; Thanks to the authors of `prettify-symbols-mode' (part of Emacs 24.4)
  37. ;; and "pretty-symbols.el" <http://github.com/drothlis/pretty-symbols>
  38. ;; for the code. It helped to write this package.
  39. ;;; Code:
  40. (require 'guix nil t)
  41. (require 'guix-auto-mode) ; for `guix-hash-regexp'
  42. (require 'guix-utils)
  43. (defgroup guix-prettify nil
  44. "Prettify Guix store file names."
  45. :prefix "guix-prettify-"
  46. :group 'guix
  47. :group 'font-lock
  48. :group 'convenience)
  49. (defcustom guix-prettify-char ?
  50. "Character used for prettifying."
  51. :type 'character
  52. :group 'guix-prettify)
  53. (defcustom guix-prettify-decompose-force nil
  54. "If non-nil, remove any composition.
  55. By default, after disabling `guix-prettify-mode',
  56. compositions (prettifying names with `guix-prettify-char') are
  57. removed only from strings matching `guix-prettify-regexp', so
  58. that compositions created by other modes are left untouched.
  59. Set this variable to non-nil, if you want to remove any
  60. composition unconditionally (like `prettify-symbols-mode' does).
  61. Most likely it will do no harm and will make the process of
  62. disabling `guix-prettify-mode' a little faster."
  63. :type 'boolean
  64. :group 'guix-prettify)
  65. (defcustom guix-prettify-regexp
  66. ;; The following file names / URLs should be abbreviated:
  67. ;; /gnu/store/aiywpm2w299pk1ps96a8d8qwnwkzfr2g-foo-0.1
  68. ;; /nix/store/inb6pfvfm2vqpn9wlyrivj3iyx7k2pv6-foo-0.1
  69. ;; http://hydra.gnu.org/nar/hrr424q661d9wdpkr48gyk5a9w8nrlcr-foo-0.1
  70. ;; http://hydra.gnu.org/log/fjbx25bap58k3mywzpmc8w9fjdydxqv8-foo-0.1
  71. ;; https://bayfront.guixsd.org/nar/gzip/m4ccn9nzlsbvlj36w45555pq98spy007-foo-0.1
  72. ;; https://ci.guix.gnu.org/nar/lzip/6njvvg1541ny17x5mpv1gar7yzd19g6a-zile-on-guile-2.4.14-0.fd09781
  73. (rx-to-string `(and "/" (or "store" "log"
  74. (and "nar" (zero-or-one
  75. (or "/lzip" "/gzip"))))
  76. "/" (group (regexp ,guix-hash-regexp)))
  77. t)
  78. "Regexp matching file names for prettifying.
  79. Disable `guix-prettify-mode' before modifying this variable and
  80. make sure to modify `guix-prettify-regexp-group' if needed.
  81. Example of a \"deeper\" prettifying:
  82. (setq guix-prettify-regexp \"store/[[:alnum:]]\\\\\\={32\\\\}\"
  83. guix-prettify-regexp-group 0)
  84. This will transform
  85. '/gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1' into
  86. '/gnu/…-foo-0.1'"
  87. :type 'regexp
  88. :group 'guix-prettify)
  89. (defcustom guix-prettify-regexp-group 1
  90. "Regexp group in `guix-prettify-regexp' for prettifying."
  91. :type 'integer
  92. :group 'guix-prettify)
  93. (defvar guix-prettify-special-modes
  94. '(ibuffer-mode
  95. guix-store-item-list-mode)
  96. "List of special modes that support font-locking.
  97. By default, \\[global-guix-prettify-mode] enables prettifying in
  98. all buffers except the ones where `font-lock-defaults' is
  99. nil (see Info node `(elisp) Font Lock Basics'), because it may
  100. break the existing highlighting.
  101. Modes from this list and all derived modes are exceptions
  102. \(`global-guix-prettify-mode' enables prettifying there).")
  103. (defun guix-prettify-compose ()
  104. "Compose matching region in the current buffer."
  105. (let ((beg (match-beginning guix-prettify-regexp-group))
  106. (end (match-end guix-prettify-regexp-group)))
  107. (compose-region beg end guix-prettify-char 'decompose-region))
  108. ;; Return nil because we're not adding any face property.
  109. nil)
  110. (defun guix-prettify-decompose-buffer ()
  111. "Remove file names compositions from the current buffer."
  112. (with-silent-modifications
  113. (let ((inhibit-read-only t))
  114. (if guix-prettify-decompose-force
  115. (remove-text-properties (point-min)
  116. (point-max)
  117. '(composition nil))
  118. (guix-while-search guix-prettify-regexp
  119. (remove-text-properties
  120. (match-beginning guix-prettify-regexp-group)
  121. (match-end guix-prettify-regexp-group)
  122. '(composition nil)))))))
  123. ;;;###autoload
  124. (define-minor-mode guix-prettify-mode
  125. "Toggle Guix Prettify mode.
  126. With a prefix argument ARG, enable Guix Prettify mode if ARG is
  127. positive, and disable it otherwise. If called from Lisp, enable
  128. the mode if ARG is omitted or nil.
  129. When Guix Prettify mode is enabled, hash parts of the Guix store
  130. file names (see `guix-prettify-regexp') are displayed as
  131. `guix-prettify-char' character, i.e.:
  132. /gnu/store/…-foo-0.1 instead of:
  133. /gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1
  134. This mode can be enabled programmatically using hooks:
  135. (add-hook 'shell-mode-hook 'guix-prettify-mode)
  136. It is possible to enable the mode in any buffer, however not any
  137. buffer's highlighting may survive after adding new elements to
  138. `font-lock-keywords' (see `guix-prettify-special-modes' for
  139. details).
  140. Also you can use `global-guix-prettify-mode' to enable Guix
  141. Prettify mode for all modes that support font-locking."
  142. :init-value nil
  143. :lighter " …"
  144. (let ((keywords `((,guix-prettify-regexp
  145. (,guix-prettify-regexp-group
  146. (guix-prettify-compose))))))
  147. (if guix-prettify-mode
  148. ;; Turn on.
  149. (font-lock-add-keywords nil keywords)
  150. ;; Turn off.
  151. (font-lock-remove-keywords nil keywords)
  152. (guix-prettify-decompose-buffer))
  153. (guix-font-lock-flush)))
  154. (defun guix-prettify-supported-p ()
  155. "Return non-nil, if the mode can be harmlessly enabled in current buffer."
  156. (or font-lock-defaults
  157. (apply #'derived-mode-p guix-prettify-special-modes)))
  158. (defun guix-prettify-turn-on ()
  159. "Enable `guix-prettify-mode' in the current buffer if needed.
  160. See `guix-prettify-special-modes' for details."
  161. (and (not guix-prettify-mode)
  162. (guix-prettify-supported-p)
  163. (guix-prettify-mode)))
  164. ;;;###autoload
  165. (define-globalized-minor-mode global-guix-prettify-mode
  166. guix-prettify-mode guix-prettify-turn-on)
  167. ;;;###autoload
  168. (defalias 'guix-prettify-global-mode 'global-guix-prettify-mode)
  169. (provide 'guix-prettify)
  170. ;;; guix-prettify.el ends here