Klimi's new dotfiles with stow.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

202 linhas
6.9 KiB

há 5 anos
  1. ;;; nix-prettify.el -- Prettify Nix store file names -*- lexical-binding: t -*-
  2. ;; Copyright © 2014, 2015 Alex Kost <alezost@gmail.com>
  3. ;; Modified by Matthew Bauer for use in nix-mode
  4. ;; Author: Alex Kost
  5. ;; Maintainer: Matthew Bauer <mjbauer95@gmail.com>
  6. ;; Homepage: https://github.com/NixOS/nix-mode
  7. ;; Version: 1.1
  8. ;; Keywords: nix
  9. ;; This file is NOT part of GNU Emacs.
  10. ;;; Commentary:
  11. ;; This package provides minor-mode for prettifying Nix store file
  12. ;; names — i.e., after enabling `nix-prettify-mode',
  13. ;; '/gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1' names will be
  14. ;; replaced with '/gnu/store/…-foo-0.1' in the current buffer. There is
  15. ;; also `global-nix-prettify-mode' for global prettifying.
  16. ;; To install, add the following to your Emacs init file:
  17. ;;
  18. ;; (add-to-list 'load-path "/path/to/dir-with-nix-prettify")
  19. ;; (autoload 'nix-prettify-mode "nix-prettify" nil t)
  20. ;; (autoload 'global-nix-prettify-mode "nix-prettify" nil t)
  21. ;; If you want to enable/disable composition after "M-x font-lock-mode",
  22. ;; use the following setting:
  23. ;;
  24. ;; (setq font-lock-extra-managed-props
  25. ;; (cons 'composition font-lock-extra-managed-props))
  26. ;; Credits:
  27. ;;
  28. ;; Thanks to Ludovic Courtès for the idea of this package.
  29. ;;
  30. ;; Thanks to the authors of `prettify-symbols-mode' (part of Emacs 24.4)
  31. ;; and "pretty-symbols.el" <http://github.com/drothlis/pretty-symbols>
  32. ;; for the code. It helped to write this package.
  33. ;;; Code:
  34. (defgroup nix-prettify nil
  35. "Prettify Nix store file names."
  36. :prefix "nix-prettify-"
  37. :group 'nix
  38. :group 'font-lock
  39. :group 'convenience)
  40. (defcustom nix-prettify-char ?…
  41. "Character used for prettifying."
  42. :type 'character
  43. :group 'nix-prettify)
  44. (defcustom nix-prettify-decompose-force nil
  45. "If non-nil, remove any composition.
  46. By default, after disabling `nix-prettify-mode',
  47. compositions (prettifying names with `nix-prettify-char') are
  48. removed only from strings matching `nix-prettify-regexp', so
  49. that compositions created by other modes are left untouched.
  50. Set this variable to non-nil, if you want to remove any
  51. composition unconditionally (like variable `prettify-symbols-mode' does).
  52. Most likely it will do no harm and will make the process of
  53. disabling `nix-prettify-mode' a little faster."
  54. :type 'boolean
  55. :group 'nix-prettify)
  56. (defcustom nix-prettify-regexp
  57. ;; The following file names / URLs should be abbreviated:
  58. ;; /gnu/store/…-foo-0.1
  59. ;; /nix/store/…-foo-0.1
  60. ;; http://hydra.gnu.org/nar/…-foo-0.1
  61. ;; http://hydra.gnu.org/log/…-foo-0.1
  62. (rx "/" (or "store" "nar" "log") "/"
  63. ;; Hash-parts do not include "e", "o", "u" and "t". See base32Chars
  64. ;; at <https://github.com/NixOS/nix/blob/master/src/libutil/hash.cc>
  65. (group (= 32 (any "0-9" "a-d" "f-n" "p-s" "v-z"))))
  66. "Regexp matching file names for prettifying.
  67. Disable `nix-prettify-mode' before modifying this variable and
  68. make sure to modify `nix-prettify-regexp-group' if needed.
  69. Example of a \"deeper\" prettifying:
  70. (setq nix-prettify-regexp \"store/[[:alnum:]]\\\\\\={32\\\\}\"
  71. nix-prettify-regexp-group 0)
  72. This will transform
  73. '/gnu/store/72f54nfp6g1hz873w8z3gfcah0h4nl9p-foo-0.1' into
  74. '/gnu/-foo-0.1'"
  75. :type 'regexp
  76. :group 'nix-prettify)
  77. (defcustom nix-prettify-regexp-group 1
  78. "Regexp group in `nix-prettify-regexp' for prettifying."
  79. :type 'integer
  80. :group 'nix-prettify)
  81. (defvar nix-prettify-special-modes
  82. '(nix-info-mode ibuffer-mode)
  83. "List of special modes that support font-locking.
  84. By default, \\[global-nix-prettify-mode] enables prettifying in
  85. all buffers except the ones where `font-lock-defaults' is
  86. nil (see Info node `(elisp) Font Lock Basics'), because it may
  87. break the existing highlighting.
  88. Modes from this list and all derived modes are exceptions
  89. \(`global-nix-prettify-mode' enables prettifying there).")
  90. (defvar nix-prettify-flush-function
  91. (cond ((fboundp 'font-lock-flush) #'font-lock-flush)
  92. ((fboundp 'jit-lock-refontify) #'jit-lock-refontify))
  93. "Function used to refontify buffer.
  94. This function is called without arguments after
  95. enabling/disabling `nix-prettify-mode'. If nil, do nothing.")
  96. (defun nix-prettify-compose ()
  97. "Compose matching region in the current buffer."
  98. (let ((beg (match-beginning nix-prettify-regexp-group))
  99. (end (match-end nix-prettify-regexp-group)))
  100. (compose-region beg end nix-prettify-char 'decompose-region))
  101. ;; Return nil because we're not adding any face property.
  102. nil)
  103. (defun nix-prettify-decompose-buffer ()
  104. "Remove file names compositions from the current buffer."
  105. (with-silent-modifications
  106. (let ((inhibit-read-only t))
  107. (if nix-prettify-decompose-force
  108. (remove-text-properties (point-min)
  109. (point-max)
  110. '(composition nil))
  111. (nix-while-search nix-prettify-regexp
  112. (remove-text-properties
  113. (match-beginning nix-prettify-regexp-group)
  114. (match-end nix-prettify-regexp-group)
  115. '(composition nil)))))))
  116. ;;;###autoload
  117. (define-minor-mode nix-prettify-mode
  118. "Toggle Nix Prettify mode.
  119. With a prefix argument ARG, enable Nix Prettify mode if ARG is
  120. positive, and disable it otherwise. If called from Lisp, enable
  121. the mode if ARG is omitted or nil.
  122. When Nix Prettify mode is enabled, hash-parts of the Nix store
  123. file names (see `nix-prettify-regexp') are prettified,
  124. i.e. displayed as `nix-prettify-char' character. This mode can
  125. be enabled programmatically using hooks:
  126. (add-hook 'shell-mode-hook 'nix-prettify-mode)
  127. It is possible to enable the mode in any buffer, however not any
  128. buffer's highlighting may survive after adding new elements to
  129. `font-lock-keywords' (see `nix-prettify-special-modes' for
  130. details).
  131. Also you can use `global-nix-prettify-mode' to enable Nix
  132. Prettify mode for all modes that support font-locking."
  133. :init-value nil
  134. :lighter ""
  135. (let ((keywords `((,nix-prettify-regexp
  136. (,nix-prettify-regexp-group
  137. (nix-prettify-compose))))))
  138. (if nix-prettify-mode
  139. ;; Turn on.
  140. (font-lock-add-keywords nil keywords)
  141. ;; Turn off.
  142. (font-lock-remove-keywords nil keywords)
  143. (nix-prettify-decompose-buffer))
  144. (and nix-prettify-flush-function
  145. (funcall nix-prettify-flush-function))))
  146. (defun nix-prettify-supported-p ()
  147. "Return non-nil, if the mode can be harmlessly enabled in current buffer."
  148. (or font-lock-defaults
  149. (apply #'derived-mode-p nix-prettify-special-modes)))
  150. (defun nix-prettify-turn-on ()
  151. "Enable `nix-prettify-mode' in the current buffer if needed.
  152. See `nix-prettify-special-modes' for details."
  153. (and (not nix-prettify-mode)
  154. (nix-prettify-supported-p)
  155. (nix-prettify-mode)))
  156. ;;;###autoload
  157. (define-globalized-minor-mode nix-prettify-global-mode
  158. nix-prettify-mode nix-prettify-turn-on)
  159. ;;;###autoload
  160. (define-obsolete-function-alias 'global-nix-prettify-mode 'nix-prettify-global-mode)
  161. (provide 'nix-prettify-mode)
  162. ;;; nix-prettify-mode.el ends here