|
0 |
;;; page-break-lines.el --- Display ^L page breaks as tidy horizontal lines -*- lexical-binding: t -*-
|
|
1 |
|
|
2 |
;; Copyright (C) 2012-2015 Steve Purcell
|
|
3 |
|
|
4 |
;; Author: Steve Purcell <steve@sanityinc.com>
|
|
5 |
;; URL: https://github.com/purcell/page-break-lines
|
|
6 |
;; Package-Version: 0
|
|
7 |
;; Package-Requires: ((emacs "24.4"))
|
|
8 |
;; Keywords: convenience, faces
|
|
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 |
;; This library provides a global mode which displays form feed
|
|
26 |
;; characters as horizontal rules.
|
|
27 |
|
|
28 |
;; Install from Melpa or Marmalade, or add to `load-path' and use
|
|
29 |
;; (require 'page-break-lines).
|
|
30 |
|
|
31 |
;; Use `page-break-lines-mode' to enable the mode in specific buffers,
|
|
32 |
;; or customize `page-break-lines-modes' and enable the mode globally with
|
|
33 |
;; `global-page-break-lines-mode'.
|
|
34 |
|
|
35 |
;; Issues and limitations:
|
|
36 |
|
|
37 |
;; If `page-break-lines-char' is displayed at a different width to
|
|
38 |
;; regular characters, the rule may be either too short or too long:
|
|
39 |
;; rules may then wrap if `truncate-lines' is nil. On some systems,
|
|
40 |
;; Emacs may erroneously choose a different font for the page break
|
|
41 |
;; symbol, which choice can be overridden using code such as:
|
|
42 |
|
|
43 |
;; (set-fontset-font "fontset-default"
|
|
44 |
;; (cons page-break-lines-char page-break-lines-char)
|
|
45 |
;; (face-attribute 'default :family))
|
|
46 |
|
|
47 |
;; Use `describe-char' on a page break char to determine whether this
|
|
48 |
;; is the case.
|
|
49 |
|
|
50 |
;; Additionally, the use of `text-scale-increase' or
|
|
51 |
;; `text-scale-decrease' will cause the rule width to be incorrect,
|
|
52 |
;; because the reported window width (in characters) will continue to
|
|
53 |
;; be the width in the frame's default font, not the scaled font used to
|
|
54 |
;; display the rule.
|
|
55 |
|
|
56 |
;; Adapted from code http://www.emacswiki.org/emacs/PageBreaks
|
|
57 |
|
|
58 |
;;; Code:
|
|
59 |
|
|
60 |
(defgroup page-break-lines nil
|
|
61 |
"Display ugly ^L page breaks as tidy horizontal lines."
|
|
62 |
:prefix "page-break-lines-"
|
|
63 |
:group 'faces)
|
|
64 |
|
|
65 |
(defcustom page-break-lines-char ?β
|
|
66 |
"Character used to render page break lines."
|
|
67 |
:type 'character
|
|
68 |
:group 'page-break-lines)
|
|
69 |
|
|
70 |
(defcustom page-break-lines-lighter " PgLn"
|
|
71 |
"Mode-line indicator for `page-break-lines-mode'."
|
|
72 |
:type '(choice (const :tag "No lighter" "") string)
|
|
73 |
:group 'page-break-lines)
|
|
74 |
|
|
75 |
(defcustom page-break-lines-max-width nil
|
|
76 |
"If non-nil, maximum width (in characters) of page break indicator.
|
|
77 |
If nil, indicator will span the width of the frame."
|
|
78 |
:type '(choice integer (const :tag "Full width" nil))
|
|
79 |
:group 'page-break-lines)
|
|
80 |
|
|
81 |
(defcustom page-break-lines-modes
|
|
82 |
'(emacs-lisp-mode lisp-mode scheme-mode compilation-mode outline-mode help-mode)
|
|
83 |
"Modes in which to enable `page-break-lines-mode'."
|
|
84 |
:type '(repeat symbol)
|
|
85 |
:group 'page-break-lines)
|
|
86 |
|
|
87 |
(defface page-break-lines
|
|
88 |
'((t :inherit font-lock-comment-face :bold nil :italic nil))
|
|
89 |
"Face used to colorize page break lines.
|
|
90 |
If using :bold or :italic, please ensure `page-break-lines-char'
|
|
91 |
is available in that variant of your font, otherwise it may be
|
|
92 |
displayed as a junk character."
|
|
93 |
:group 'page-break-lines)
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
;;;###autoload
|
|
98 |
(define-minor-mode page-break-lines-mode
|
|
99 |
"Toggle Page Break Lines mode.
|
|
100 |
|
|
101 |
In Page Break mode, page breaks (^L characters) are displayed as a
|
|
102 |
horizontal line of `page-break-lines-char' characters."
|
|
103 |
:lighter page-break-lines-lighter
|
|
104 |
:group 'page-break-lines
|
|
105 |
(page-break-lines--update-display-tables))
|
|
106 |
|
|
107 |
(dolist (hook '(window-configuration-change-hook
|
|
108 |
window-size-change-functions
|
|
109 |
after-setting-font-hook
|
|
110 |
display-line-numbers-mode-hook))
|
|
111 |
(add-hook hook 'page-break-lines--update-display-tables))
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
(defun page-break-lines--update-display-table (window)
|
|
116 |
"Modify a display-table that displays page-breaks prettily.
|
|
117 |
If the buffer inside WINDOW has `page-break-lines-mode' enabled,
|
|
118 |
its display table will be modified as necessary."
|
|
119 |
(with-current-buffer (window-buffer window)
|
|
120 |
(with-selected-window window
|
|
121 |
(if page-break-lines-mode
|
|
122 |
(progn
|
|
123 |
(unless buffer-display-table
|
|
124 |
(setq buffer-display-table (make-display-table)))
|
|
125 |
(let ((default-height (face-attribute 'default :height nil 'default)))
|
|
126 |
(set-face-attribute 'page-break-lines nil :height default-height)
|
|
127 |
(let* ((cwidth (char-width page-break-lines-char))
|
|
128 |
(wwidth-pix (- (window-width nil t)
|
|
129 |
(if (and (bound-and-true-p display-line-numbers)
|
|
130 |
(fboundp 'line-number-display-width))
|
|
131 |
(line-number-display-width t)
|
|
132 |
0)))
|
|
133 |
(width (- (/ wwidth-pix (frame-char-width) cwidth)
|
|
134 |
(if (display-graphic-p) 0 1)))
|
|
135 |
(width (if page-break-lines-max-width
|
|
136 |
(min width page-break-lines-max-width)
|
|
137 |
width))
|
|
138 |
(glyph (make-glyph-code page-break-lines-char 'page-break-lines))
|
|
139 |
(new-display-entry (vconcat (make-list width glyph))))
|
|
140 |
(unless (equal new-display-entry (elt buffer-display-table ?\^L))
|
|
141 |
(aset buffer-display-table ?\^L new-display-entry)))))
|
|
142 |
(when (and (apply 'derived-mode-p page-break-lines-modes)
|
|
143 |
buffer-display-table)
|
|
144 |
(aset buffer-display-table ?\^L nil))))))
|
|
145 |
|
|
146 |
(defun page-break-lines--update-display-tables (&optional frame)
|
|
147 |
"Function called for updating display table in windows of FRAME."
|
|
148 |
(unless (minibufferp)
|
|
149 |
(mapc 'page-break-lines--update-display-table (window-list frame 'no-minibuffer))))
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
;;;###autoload
|
|
154 |
(defun page-break-lines-mode-maybe ()
|
|
155 |
"Enable `page-break-lines-mode' in the current buffer if desired.
|
|
156 |
When `major-mode' is listed in `page-break-lines-modes', then
|
|
157 |
`page-break-lines-mode' will be enabled."
|
|
158 |
(if (and (not (minibufferp))
|
|
159 |
(apply 'derived-mode-p page-break-lines-modes))
|
|
160 |
(page-break-lines-mode 1)))
|
|
161 |
|
|
162 |
;;;###autoload
|
|
163 |
(define-global-minor-mode global-page-break-lines-mode
|
|
164 |
page-break-lines-mode page-break-lines-mode-maybe
|
|
165 |
:require 'page-break-lines
|
|
166 |
:group 'page-break-lines)
|
|
167 |
|
|
168 |
|
|
169 |
(provide 'page-break-lines)
|
|
170 |
|
|
171 |
;; Local Variables:
|
|
172 |
;; coding: utf-8
|
|
173 |
;; checkdoc-minor-mode: t
|
|
174 |
;; End:
|
|
175 |
|
|
176 |
;;; page-break-lines.el ends here
|