paren-face.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; paren-face.el --- a face for parentheses in lisp modes -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2013-2016 Jonas Bernoulli
  3. ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
  4. ;; Homepage: https://github.com/tarsius/paren-face
  5. ;; This file is not part of GNU Emacs.
  6. ;; This file is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 3, or (at your option)
  9. ;; any later version.
  10. ;; This file is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; For a full copy of the GNU General Public License
  15. ;; see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This library defines a face named `parenthesis' used just for
  18. ;; parentheses. The intended purpose of this face is to make
  19. ;; parentheses less visible in Lisp code by dimming them.
  20. ;; We lispers probably don't need to be constantly made aware of the
  21. ;; existence of the parentheses. Dimming them might be even more
  22. ;; useful for people new to lisp who have not yet learned to
  23. ;; subconsciously blend out the parentheses.
  24. ;; To use the `parenthesis' face, turn on `global-paren-face-mode'.
  25. ;; The option `paren-face-modes' controls in what buffers the minor
  26. ;; mode `paren-face-mode' is turned on.
  27. ;; The parenthesis at or before point, as well as the parenthesis at
  28. ;; the other end of the s-expression should actually stand out, but
  29. ;; that is beyond the scope of the mode defined here. Instead use one
  30. ;; of the modes dedicated to that, e.g. the builtin `show-paren-mode'.
  31. ;; While this face is intended to be used with Lisp modes, it also
  32. ;; works with other major modes, just add the mode to value of
  33. ;; `paren-face-modes'.
  34. ;; By default only parentheses are dimmed, customize option
  35. ;; `paren-face-regexp' if you also want to dim brackets or braces.
  36. ;; If you want to use a differnt regexp in different major-modes,
  37. ;; then use a the mode hook to set the buffer local value.
  38. ;;; History:
  39. ;; Dave Pearson's `parenface.el' implements the same basic idea.
  40. ;; Unfortunately that library doesn't use the appropriate Emacs
  41. ;; interfaces correctly, so I wrote this as a replacement.
  42. ;;; Code:
  43. (defgroup paren-face nil
  44. "Face for parentheses in lisp modes."
  45. :group 'font-lock-extra-types
  46. :group 'faces)
  47. (defface parenthesis '((t (:inherit shadow)))
  48. "Face for parentheses in lisp modes.
  49. This face is only used if `paren-face-mode' is turned on.
  50. See `global-paren-face-mode' for an easy way to do so."
  51. :group 'paren-face)
  52. (defcustom paren-face-modes
  53. '(lisp-mode
  54. emacs-lisp-mode lisp-interaction-mode ielm-mode
  55. scheme-mode inferior-scheme-mode
  56. clojure-mode cider-repl-mode nrepl-mode
  57. arc-mode inferior-arc-mode)
  58. "Major modes in which `paren-face-mode' should be turned on.
  59. When `global-paren-face-mode' is turned on, the buffer-local mode
  60. is turned on in all buffers whose major mode is or derives from
  61. one of the modes listed here."
  62. :type '(repeat symbol)
  63. :group 'paren-face)
  64. (defcustom paren-face-regexp "[()]"
  65. "Regular expression to match parentheses."
  66. :type 'regexp
  67. :group 'paren-face)
  68. (defcustom paren-face-mode-lighter ""
  69. "String to display in the mode line when `paren-face-mode' is active."
  70. :type 'string
  71. :group 'paren-face)
  72. ;;;###autoload
  73. (define-minor-mode paren-face-mode
  74. "Use a dedicated face just for parentheses."
  75. :lighter paren-face-mode-lighter
  76. (let ((keywords `((,paren-face-regexp 0 'parenthesis))))
  77. (if paren-face-mode
  78. (font-lock-add-keywords nil keywords)
  79. (font-lock-remove-keywords nil keywords)))
  80. (when font-lock-mode
  81. (if (and (fboundp 'font-lock-flush)
  82. (fboundp 'font-lock-ensure))
  83. (save-restriction
  84. (widen)
  85. (font-lock-flush)
  86. (font-lock-ensure))
  87. (with-no-warnings
  88. (font-lock-fontify-buffer)))))
  89. ;;;###autoload
  90. (define-globalized-minor-mode global-paren-face-mode
  91. paren-face-mode turn-on-paren-face-mode-if-desired
  92. :group 'paren-face)
  93. (defun turn-on-paren-face-mode-if-desired ()
  94. (when (apply 'derived-mode-p paren-face-modes)
  95. (paren-face-mode 1)))
  96. (provide 'paren-face)
  97. ;; Local Variables:
  98. ;; indent-tabs-mode: nil
  99. ;; End:
  100. ;;; paren-face.el ends here