guix-prettify.el 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ;;; guix-prettify.el --- Prettify Guix store file names
  2. ;; Copyright © 2014, 2015, 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 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. (rx-to-string `(and "/" (or "store" "log"
  73. (and "nar" (zero-or-one "/gzip")))
  74. "/" (group (regexp ,guix-hash-regexp)))
  75. t)
  76. "Regexp matching file names for prettifying.
  77. Disable `guix-prettify-mode' before modifying this variable and
  78. make sure to modify `guix-prettify-regexp-group' if needed.
  79. Example of a \"deeper\" prettifying:
  80. (setq guix-prettify-regexp \"store/[[:alnum:]]\\\\\\={32\\\\}\"
  81. guix-prettify-regexp-group 0)
  82. This will transform
  83. '/gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1' into
  84. '/gnu/…-foo-0.1'"
  85. :type 'regexp
  86. :group 'guix-prettify)
  87. (defcustom guix-prettify-regexp-group 1
  88. "Regexp group in `guix-prettify-regexp' for prettifying."
  89. :type 'integer
  90. :group 'guix-prettify)
  91. (defvar guix-prettify-special-modes
  92. '(ibuffer-mode)
  93. "List of special modes that support font-locking.
  94. By default, \\[global-guix-prettify-mode] enables prettifying in
  95. all buffers except the ones where `font-lock-defaults' is
  96. nil (see Info node `(elisp) Font Lock Basics'), because it may
  97. break the existing highlighting.
  98. Modes from this list and all derived modes are exceptions
  99. \(`global-guix-prettify-mode' enables prettifying there).")
  100. (defun guix-prettify-compose ()
  101. "Compose matching region in the current buffer."
  102. (let ((beg (match-beginning guix-prettify-regexp-group))
  103. (end (match-end guix-prettify-regexp-group)))
  104. (compose-region beg end guix-prettify-char 'decompose-region))
  105. ;; Return nil because we're not adding any face property.
  106. nil)
  107. (defun guix-prettify-decompose-buffer ()
  108. "Remove file names compositions from the current buffer."
  109. (with-silent-modifications
  110. (let ((inhibit-read-only t))
  111. (if guix-prettify-decompose-force
  112. (remove-text-properties (point-min)
  113. (point-max)
  114. '(composition nil))
  115. (guix-while-search guix-prettify-regexp
  116. (remove-text-properties
  117. (match-beginning guix-prettify-regexp-group)
  118. (match-end guix-prettify-regexp-group)
  119. '(composition nil)))))))
  120. ;;;###autoload
  121. (define-minor-mode guix-prettify-mode
  122. "Toggle Guix Prettify mode.
  123. With a prefix argument ARG, enable Guix Prettify mode if ARG is
  124. positive, and disable it otherwise. If called from Lisp, enable
  125. the mode if ARG is omitted or nil.
  126. When Guix Prettify mode is enabled, hash parts of the Guix store
  127. file names (see `guix-prettify-regexp') are displayed as
  128. `guix-prettify-char' character, i.e.:
  129. /gnu/store/…-foo-0.1 instead of:
  130. /gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1
  131. This mode can be enabled programmatically using hooks:
  132. (add-hook 'shell-mode-hook 'guix-prettify-mode)
  133. It is possible to enable the mode in any buffer, however not any
  134. buffer's highlighting may survive after adding new elements to
  135. `font-lock-keywords' (see `guix-prettify-special-modes' for
  136. details).
  137. Also you can use `global-guix-prettify-mode' to enable Guix
  138. Prettify mode for all modes that support font-locking."
  139. :init-value nil
  140. :lighter " …"
  141. (let ((keywords `((,guix-prettify-regexp
  142. (,guix-prettify-regexp-group
  143. (guix-prettify-compose))))))
  144. (if guix-prettify-mode
  145. ;; Turn on.
  146. (font-lock-add-keywords nil keywords)
  147. ;; Turn off.
  148. (font-lock-remove-keywords nil keywords)
  149. (guix-prettify-decompose-buffer))
  150. (guix-font-lock-flush)))
  151. (defun guix-prettify-supported-p ()
  152. "Return non-nil, if the mode can be harmlessly enabled in current buffer."
  153. (or font-lock-defaults
  154. (apply #'derived-mode-p guix-prettify-special-modes)))
  155. (defun guix-prettify-turn-on ()
  156. "Enable `guix-prettify-mode' in the current buffer if needed.
  157. See `guix-prettify-special-modes' for details."
  158. (and (not guix-prettify-mode)
  159. (guix-prettify-supported-p)
  160. (guix-prettify-mode)))
  161. ;;;###autoload
  162. (define-globalized-minor-mode global-guix-prettify-mode
  163. guix-prettify-mode guix-prettify-turn-on)
  164. ;;;###autoload
  165. (defalias 'guix-prettify-global-mode 'global-guix-prettify-mode)
  166. (provide 'guix-prettify)
  167. ;;; guix-prettify.el ends here