-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorg-phscroll.el
352 lines (304 loc) · 14 KB
/
org-phscroll.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
;;; org-phscroll.el --- Apply horizontal scroll to org-mode table -*- lexical-binding: t; -*-
;; Copyright (C) 2020 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Enable wide table horizontal scrolling even if org-startup-truncated is nil.
;;; Usage:
;;; (load "org-phscroll.el")
;;; Code:
(require 'org)
(require 'phscroll)
(defvar org-phscroll-buffer-name-regexp-to-disable
'(;; see: org-src-font-lock-fontify-block in org-src.el
"\\` *\\*org-src-fontification:.*\\*\\'"
;; see: org-html-fontify-code in ox-html.el
"\\` *\\*temp\\*-[0-9]+\\'"))
(defvar-local org-phscroll-disabled nil)
(defvar-local org-phscroll-font-lock-keywords (list '(org-phscroll--fontify)))
;;;###autoload
(define-minor-mode org-phscroll-mode
"Apply phscroll to org-table."
:type 'boolean
:init-value nil
:group 'org-phscroll
(if org-phscroll-mode
(progn
;; Do not anything in some buffers.
(setq-local org-phscroll-disabled
(not
(null
(seq-some
(lambda (regexp) (string-match-p regexp (buffer-name)))
org-phscroll-buffer-name-regexp-to-disable))))
;; (when org-phscroll-disabled
;; (message "Disable org-phscroll-mode in %s" (buffer-name)))
(unless org-phscroll-disabled
;; Do not update on modification-hooks. Update on font-lock.
(setq-local phscroll-update-area-display-on-modified nil)
(phscroll-mode 1)
(font-lock-add-keywords nil org-phscroll-font-lock-keywords t)
(font-lock-flush)))
(unless org-phscroll-disabled
(font-lock-remove-keywords nil org-phscroll-font-lock-keywords)
(setq-local phscroll-update-area-display-on-modified t)
(phscroll-delete-all) ;;@todo Keep manually added areas (Identify areas created by org-phscroll, and delete(remove-region) only areas created by org-phscroll when fontify)
(phscroll-mode -1))))
(defun org-phscroll--fontify (limit)
(when org-phscroll-mode
(save-match-data
(save-excursion
(save-restriction
(widen)
;; Invalidate areas that overlaps range to fontify.
;; Because the text width may have changed due to the
;; fontify of the previous keyword (for example, the
;; org-link value of the invisible property).
(phscroll-invalidate-region (point) limit)
;; Create/Delete/Move phscroll areas.
(goto-char (phscroll-line-begin))
(let* ((start (point))
;; ("|" or "+-[+-]") ... not whitespace
(table-re "^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)")
;; [^|+]
;; +([^-] or $)
;; +-([^+-] or $)
;; +-[+-] whitespace...$
;; | whitespace...$
;; $
(not-table-re "^[ \t]*\\([^|+ \t]\\|\\+\\([^-]\\|$\\|-\\([^-+]\\|$\\|[-+]\\s-*$\\)\\)\\||\\s-*$\\|$\\)")
(phscroll-fontify-range (cons start limit))
not-table-beg)
;;(message "org-phscroll--fontify start=%s limit=%s line-end=%s" start limit (phscroll-line-end));;debug
(while (progn
(setq not-table-beg (point))
(and (< (point) limit)
(re-search-forward table-re limit t)))
(let* ((not-table-end (phscroll-line-begin))
(table-beg not-table-end)
(table-end (if (re-search-forward not-table-re limit t)
(goto-char (phscroll-line-begin (match-beginning 0))) ;; beginning of not table
(goto-char limit)))) ;; end of fontify
;; not table
(if (< not-table-beg not-table-end)
(phscroll-remove-region not-table-beg not-table-end))
;; table?
;; exclude comment, blocks
(if (or (text-property-any table-beg table-end 'face 'org-table)
(eq (car-safe (get-text-property table-beg 'face)) 'org-table)) ;; for org-modern (org-table (:overline) (:height 0.1))
;; table
(progn
;; include previous line if previous line is in area
(if (and (= start table-beg)
(phscroll-enum-area (phscroll-line-begin (1- start))
(phscroll-line-end (1- start))))
(setq table-beg (phscroll-line-begin (1- start))))
;; include next line if next line is in area
(if (phscroll-enum-area (phscroll-line-begin table-end)
(phscroll-line-end table-end))
(setq table-end (min (point-max)
(1+ (phscroll-line-end table-end)))))
;; cover single area
(phscroll-cover-region table-beg table-end))
;; not table
(phscroll-remove-region table-beg table-end))))
;; not table
(if (< not-table-beg limit)
(phscroll-remove-region not-table-beg limit))))))
nil))
;; (defun org-phscroll--font-lock-set-keywords ()
;; ;; See: org-set-font-lock-defaults
;; (nconc
;; org-font-lock-extra-keywords
;; (list
;; '(org-phscroll--fontify))))
;;;; Support for table column shrink/expand
(defun org-phscroll-invalidate-table (pos)
(when-let ((area (phscroll-get-area-at pos)))
(phscroll-area-clear-updated-ranges area)
;; @todo necessary?
;; (font-lock-unfontify-region
;; (phscroll-area-begin area)
;; (phscroll-area-end area))
))
(defun org-phscroll--table-shrink-columns (_columns beg _end &rest _)
(org-phscroll-invalidate-table beg))
(defun org-phscroll--table-expand (&optional beg _end &rest _)
(when (and (null beg)
(org-at-table-p))
(setq beg (org-table-begin)))
(when beg
(org-phscroll-invalidate-table beg)))
;;;; Support for org-table-overlay-coordinates
(defun org-phscroll--table-toggle-coordinate-overlays (&rest _)
(org-phscroll-invalidate-table (point)))
;;;; Support for org-table-header-line-mode
(defun org-phscroll--after-table-header-line-mode (&rest _)
;; Cancel post-command-hook.
;; Use phscroll's post-command-hook only.
(remove-hook 'post-command-hook #'org-table-header-set-header t))
(defun org-phscroll--table-row-get-visible-string (old-func &optional beg)
(unless beg
(setq beg (point)))
(let ((area (phscroll-get-area-at beg)))
(if (null area)
;; Call original
(funcall old-func beg)
;; In phscroll area
(save-excursion
;;@todo Is it possible to unify with around phscroll-char-width-next?
;;@todo Support more properties
(let ((pos (phscroll-line-begin beg))
(eol (phscroll-line-end beg))
visible-strs)
(while (< pos eol)
(let (next-pos pvalue)
;; Overlay
(let ((overlays (overlays-at pos t)))
(while (and overlays (null next-pos))
(let ((ov (car overlays)))
(unless (or (overlay-get ov 'phscroll)
(overlay-get ov 'phscroll-ignore))
(cond
;; Overlay's display
((setq pvalue (overlay-get ov 'display))
(when (stringp pvalue)
(push pvalue visible-strs))
(setq next-pos (overlay-end ov)))
;; Overlay's invisible
((and (setq pvalue (overlay-get ov 'invisible))
(invisible-p pvalue))
(setq next-pos (overlay-end ov))))))
(setq overlays (cdr overlays))))
;; Text property
(unless next-pos
(cond
;; Text's display
((setq pvalue (get-text-property pos 'display))
(cond
((stringp pvalue)
(push pvalue visible-strs))
((and (consp pvalue)
(eq (car pvalue) 'space)
(eq (cadr pvalue) :relative-width)
(integerp (caddr pvalue)))
(push (make-string (caddr pvalue) (char-after pos)) visible-strs)))
(setq next-pos
(next-single-property-change pos 'display nil eol)))
;; Text's invisible
((and (setq pvalue (get-text-property pos 'invisible))
(invisible-p pvalue))
(setq next-pos
(next-single-property-change pos 'invisible nil eol)))
;; Character
(t
(push (char-to-string (char-after pos)) visible-strs)
(setq next-pos (1+ pos)))))
(setq pos next-pos)))
(let* ((win-width (phscroll-window-width-at beg nil))
(scroll-column (phscroll-get-scroll-column area))
(visible-str (apply #'concat (nreverse visible-strs)))
(result (truncate-string-to-width
visible-str
(+ scroll-column win-width)
scroll-column ?\s)))
;;(message "result=%s" result)
result))))))
(defun org-phscroll--around-post-command-in-header-line-mode (old-func &rest args)
(if (and (boundp 'org-table-header-line-mode) ;; Org 9.4 or later
org-table-header-line-mode)
(progn
;; Delete overlay before phscroll process
(when (overlayp org-table-header-overlay)
(delete-overlay org-table-header-overlay)
(setq org-table-header-overlay nil))
;; Call phscroll process
(prog1 (apply old-func args)
;; Update header line
(ignore-errors
(org-table-header-set-header))
(when org-table-header-overlay
(overlay-put org-table-header-overlay 'phscroll-ignore t)
(overlay-put org-table-header-overlay 'priority 20)
(let ((win-start (overlay-start org-table-header-overlay)))
(move-overlay org-table-header-overlay
(phscroll-line-begin win-start)
(phscroll-line-end win-start))))))
;; Not in header line mode
(apply old-func args)))
;;;; Support for org-indent
(defun org-phscroll-invalidate-indent (beg end)
;;(message "invalidate %s %s" beg end)
(dolist (area (phscroll-enum-area beg end))
(phscroll-area-remove-updated-range beg end area)
;;(phscroll-area-clear-updated-ranges area)
))
(defun org-phscroll--indent-add-properties (beg end &optional _delay)
(org-phscroll-invalidate-indent beg end))
;;;; Hook global functions
;;;###autoload
(defun org-phscroll-activate ()
(interactive)
(add-hook 'org-mode-hook
#'org-phscroll-mode)
;; (add-hook 'org-font-lock-set-keywords-hook
;; #'org-phscroll--font-lock-set-keywords)
;; for table shrink/expand
(advice-add #'org-table--shrink-columns
:after #'org-phscroll--table-shrink-columns)
(advice-add #'org-table-expand
:after #'org-phscroll--table-expand)
;; for org-table-overlay-coordinates
(advice-add #'org-table-toggle-coordinate-overlays
:after #'org-phscroll--table-toggle-coordinate-overlays)
;; for org-table-header-line-mode
(advice-add #'org-table-header-line-mode
:after #'org-phscroll--after-table-header-line-mode)
(advice-add #'org-table-row-get-visible-string
:around #'org-phscroll--table-row-get-visible-string)
(advice-add #'phscroll-on-post-command
:around #'org-phscroll--around-post-command-in-header-line-mode)
;; for indent
(with-no-warnings ;;Some one not needs to (require 'org-indent)
(advice-add #'org-indent-add-properties
:after #'org-phscroll--indent-add-properties)))
;;;###autoload
(defun org-phscroll-deactivate ()
(interactive)
(remove-hook 'org-mode-hook
#'org-phscroll-mode)
;; (remove-hook 'org-font-lock-set-keywords-hook
;; #'org-phscroll--font-lock-set-keywords)
;; for table shrink/expand
(advice-remove #'org-table--shrink-columns
#'org-phscroll--table-shrink-columns)
(advice-remove #'org-table-expand
#'org-phscroll--table-expand)
;; for org-table-overlay-coordinates
(advice-remove #'org-table-toggle-coordinate-overlays
#'org-phscroll--table-toggle-coordinate-overlays)
;; for org-table-header-line-mode
(advice-remove #'org-table-header-line-mode
#'org-phscroll--after-table-header-line-mode)
(advice-remove #'org-table-row-get-visible-string
#'org-phscroll--table-row-get-visible-string)
(advice-remove #'phscroll-on-post-command
#'org-phscroll--around-post-command-in-header-line-mode)
;; for indent
(with-no-warnings ;;Some one not needs to (require 'org-indent)
(advice-remove #'org-indent-add-properties
#'org-phscroll--indent-add-properties)))
(with-eval-after-load "org"
(org-phscroll-activate))
(provide 'org-phscroll)
;;; org-phscroll.el ends here