]> code.delx.au - gnu-emacs-elpa/blob - compact-docstrings.el
Initial import
[gnu-emacs-elpa] / compact-docstrings.el
1 ;;; compact-docstrings.el --- Shrink blank lines in docstrings and doc comments
2
3 ;; Copyright (C) 2016 Clément Pit-Claudel
4
5 ;; Author: Clément Pit-Claudel <clement.pitclaudel@live.com>
6 ;; URL: https://github.com/cpitclaudel/compact-docstrings
7 ;; Package-Version: 0.1
8 ;; Keywords: convenience, faces, lisp, maint, c
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Shrink blank lines in docstrings and doc comments
26 ;;
27 ;; Enable locally with `compact-docstrings-mode':
28 ;; (add-hook 'some-mode-hook #'compact-docstrings-mode)
29 ;;
30 ;; Enable globally (in all programming modes) with
31 ;; (add-hook 'after-init-hook #'global-compact-docstrings--mode)
32
33 ;;; Code:
34
35 (defgroup compact-docstrings nil
36 "Shrink empty lines in docstrings and doc comments."
37 :group 'faces)
38
39 (defface compact-docstrings-face
40 '((t :height 0.5))
41 "Face applied to blank lines in docstrings."
42 :group 'compact-docstrings)
43
44 (defcustom compact-docstrings-only-doc-blocks t
45 "When nil, also shrink blank lines in regular strings and comments."
46 :group 'compact-docstrings
47 :type 'boolean)
48
49 (defun compact-docstrings--matcher (bound)
50 "Find blank line in docstring, looking in point .. BOUND."
51 (let ((found nil))
52 (while (and (not found) (re-search-forward "^\\s-*\n" bound t))
53 (let ((syntax (syntax-ppss)))
54 (when (and (or (nth 3 syntax) ;; In string
55 (nth 4 syntax)) ;; In comment
56 (or (not compact-docstrings-only-doc-blocks)
57 (let ((face (get-text-property (point) 'face)))
58 (or (eq face 'font-lock-doc-face)
59 (and (listp face) (memq 'font-lock-doc-face face))))))
60 (setq found t))))
61 found))
62
63 (defconst compact-docstrings--keywords
64 '((compact-docstrings--matcher 0 'compact-docstrings-face prepend)) 'append)
65
66 ;;;###autoload
67 (define-minor-mode compact-docstrings-mode
68 "Shrink empty lines in docstrings and doc comments."
69 :lighter " compact"
70 (if compact-docstrings-mode
71 (font-lock-add-keywords nil compact-docstrings--keywords 'append)
72 (font-lock-remove-keywords nil compact-docstrings--keywords))
73 (if (fboundp #'font-lock-flush)
74 (font-lock-flush)
75 (with-no-warnings (font-lock-fontify-buffer))))
76
77 (defun compact-docstrings--mode-on ()
78 "Turn on `compact-docstrings-mode', if appropriate."
79 (when (derived-mode-p major-mode #'prog-mode)
80 (compact-docstrings-mode)))
81
82 ;;;###autoload
83 (defalias 'shrink-docstrings #'compact-docstrings--mode-on)
84
85 ;;;###autoload
86 (define-globalized-minor-mode global-compact-docstrings-mode compact-docstrings-mode
87 compact-docstrings--mode-on
88 :init-value nil)
89
90 (provide 'compact-docstrings)
91 ;;; compact-docstrings.el ends here