-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphscroll.el
1426 lines (1203 loc) · 52.1 KB
/
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; phscroll.el --- Partial horizontal scroll -*- 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:
;;; Usage:
;;; 1. (require 'phscroll)
;;; 2. Select region you want to hscroll.
;;; 3. M-x phscroll-region
;;; Code:
(require 'cl-lib)
(require 'subr-x)
;; TODO:
;; - [X] truncate-lines によって動作を変える。truncate-linesのときは何もしない。切り替えを検出できるか
;; - [X] remove-overlaysのところ
;; - phscroll-update-area-displayが遅い
;; - [X] 既存の水平スクロール操作に対応?(scroll-left, scroll-right)
;; - [X] 複数ウィンドウの挙動、特に左右に分割した場合で左右のサイズが異なる場合はどうしようもない。最小幅を使うしか? 何もしない方が良い? →area毎に幅を持って変化をチェックする。
;;;; Customization
(defgroup phscroll nil
"Creates horizontally scrolling areas in a buffer."
:prefix "phscroll-"
:group 'convenience)
(defcustom phscroll-use-fringe t
"Specifies how to indicate the possibility of scrolling left or right.
Use fringe when non-nil.
If it is nil, it is indicated by the < and > characters."
:type 'boolean
:group 'phscroll)
(defvar phscroll-margin-right-additional 0) ;;for stability
(defun phscroll-margin-right ()
(+ phscroll-margin-right-additional
(if phscroll-use-fringe 1 2)))
(defcustom phscroll-scroll-left-right-move-point t
"If non-nil, move point with left/right scroll commands."
:type 'boolean
:group 'phscroll)
(defcustom phscroll-scroll-left-right-reverse-direction nil
"If non-nil, reverse direction of left/right scroll command."
:type 'boolean
:group 'phscroll)
(defcustom phscroll-calculate-in-pixels nil
"Experimental."
:type 'boolean
:group 'phscroll)
;;;; Basic Commands
(defvar-local phscroll-truncate-lines nil) ;; to detect truncate-lines change
(define-minor-mode phscroll-mode
"Partial horizontal scroll mode."
:global nil
(cond
(phscroll-mode
(setq-local phscroll-truncate-lines truncate-lines)
(add-hook 'post-command-hook #'phscroll-on-post-command nil t)
(add-hook 'window-scroll-functions #'phscroll-on-window-scroll nil t)
;;(add-hook 'window-size-change-functions #'phscroll-on-window-size-changed nil t)
(add-hook 'pre-redisplay-functions #'phscroll-on-pre-redisplay nil t)
)
(t
(remove-hook 'post-command-hook #'phscroll-on-post-command t)
(remove-hook 'window-scroll-functions #'phscroll-on-window-scroll t)
;;(remove-hook 'window-size-change-functions #'phscroll-on-window-size-changed t)
(remove-hook 'pre-redisplay-functions #'phscroll-on-pre-redisplay t)
)))
(defun phscroll-region (beg end)
"Make a partial horizontal scrolling area.
The area to be created has a range from the line containing BEG
to the line containing END (however, if END is the beginning of
the line, that line is not included).
Any existing overlapping areas are destroyed."
(interactive "r")
(setq beg (phscroll-line-begin beg))
(setq end (if (save-excursion (goto-char end) (bolp))
end
(phscroll-line-end end)))
(when (< beg end)
;; turn on phscroll-mode
(unless phscroll-mode
(phscroll-mode))
;; create overlay and area object
(let* ((overlap-areas (phscroll-enum-area beg end))
(area (car overlap-areas)))
(if area
;; reuse area
(progn
;; reuse area
(phscroll-area-move area beg end)
(phscroll-update-area-display area)
;; destroy other overlap areas
(cl-loop for ooa in (cdr overlap-areas)
do (phscroll-area-destroy ooa)))
;; create new area
(setq area (phscroll-area-create beg end))
(phscroll-update-area-display area t))
area)))
(defun phscroll-delete-at (&optional pos)
"Delete the area at POS."
(interactive "d")
(phscroll-area-destroy (phscroll-get-area-at pos)))
(defun phscroll-delete-all ()
"Destroy all areas in the current buffer."
(interactive)
(cl-loop for area in (phscroll-enum-area)
do (phscroll-area-destroy area)))
(defun phscroll-update-at (&optional pos)
"Redisplay the area at POS."
(interactive "d")
(phscroll-update-area-display (phscroll-get-area-at pos) t))
(defun phscroll-merge-region (beg end &optional no-update)
"Merge all areas that overlap the region BEG..END.
If NO-UPDATE is non-nil, this function will not call
`phscroll-update-area-display'."
(interactive "r")
(let* ((areas (sort (phscroll-enum-area beg end)
(lambda (a1 a2)
(< (phscroll-area-begin a1) (phscroll-area-begin a2)))))
(result (car areas)))
(setq areas (cdr areas))
(while areas
(phscroll-area-merge result (car areas))
(setq areas (cdr areas)))
(when (and result (not no-update))
(phscroll-update-area-display result))
result))
(defun phscroll-cover-region (beg end)
"Cover the region BEG..END with a single phscroll area."
(interactive "r")
(if-let ((area (phscroll-merge-region beg end t)));;no-update=t
(let ((area-beg (phscroll-area-begin area))
(area-end (phscroll-area-end area)))
(when (or (< beg area-beg) (< area-end end))
(phscroll-area-move area
(min beg area-beg)
(max end area-end)))
(phscroll-update-area-display area)
area)
(phscroll-region beg end)))
(defun phscroll-remove-region (beg end)
"Remove phscroll areas from the region BEG..END.
Areas that overlap the region will be deleted, moved, or splitted."
(interactive "r")
(dolist (area (phscroll-enum-area beg end))
(let ((area-beg (phscroll-area-begin area))
(area-end (phscroll-area-end area)))
(cond
;; keep area
((<= area-end beg) nil)
((<= end area-beg) nil)
;; delete area
((and (<= beg area-beg) (<= area-end end))
(phscroll-area-destroy area))
;; split area
((and (< area-beg beg) (< end area-end))
(phscroll-area-split area end)
(phscroll-area-move area area-beg beg)
(phscroll-update-area-display area);;unnecessary
)
;; move area
((< area-beg beg)
(phscroll-area-move area area-beg beg)
(phscroll-update-area-display area);;unnecessary
)
((< end area-end)
(phscroll-area-move area end area-end)
(phscroll-update-area-display area);;unnecessary
)))))
;;;; Area object
(defun phscroll-area-create (beg end &optional evaporate
init-scroll-column
init-updated-ranges
init-window-width)
"Create a new area object covering BEG to END.
EVAPORATE is the same as the overlay property of the same name.
INIT-SCROLL-COLUMN, INIT-UPDATED-RANGES, INIT-WINDOW-WIDTH are
for special cases."
;;(message "create %s %s" beg end)
(let* ((ov (make-overlay beg end))
(area (list
'phscroll ;;0
(or init-scroll-column 0) ;; 1:scroll-column
ov ;; 2:overlay
(append (list (cons -1 -1))
init-updated-ranges);; 3:updated-ranges
(or init-window-width -1) ;; 4:window-width when last update-area-display
)))
(overlay-put ov 'phscroll t)
(overlay-put ov 'phscroll-area area)
(overlay-put ov 'modification-hooks (list #'phscroll-on-modified))
(when evaporate
(overlay-put ov 'evaporate t))
(phscroll-area-set-keymap area)
area))
(defun phscroll-area-destroy (area)
"Destroy AREA, an area object."
(when area
(let ((beg (phscroll-area-begin area))
(end (phscroll-area-end area))
(ov (phscroll-area-overlay area)))
(remove-overlays beg end 'phscroll-left t)
(remove-overlays beg end 'phscroll-right t)
(delete-overlay ov))))
(defun phscroll-area-move (area beg end &optional update)
"Set the endpoints of AREA to BEG and END.
Call `phscroll-update-area-display' if UPDATE is non-nil.
Return AREA."
(when (and area (<= beg end))
(let* ((ov (phscroll-area-overlay area))
(old-beg (overlay-start ov))
(old-end (overlay-end ov)))
;; If area range changed
(when (or (/= old-beg beg)
(/= old-end end))
;; Remove ranges outside the new area from updated ranges list
;; and remove left and right overlays
(if (or (<= old-end beg) (<= end old-beg))
;; If there is no overlap between old and new, simply remove old
(progn
(phscroll-area-clear-updated-ranges area)
(remove-overlays old-beg old-end 'phscroll-left t)
(remove-overlays old-beg old-end 'phscroll-right t))
;; If overlap, remove protruding part
(when (< old-beg beg)
(phscroll-area-remove-updated-range old-beg beg area)
(remove-overlays old-beg beg 'phscroll-left t)
(remove-overlays old-beg beg 'phscroll-right t))
(when (< end old-end)
(phscroll-area-remove-updated-range end old-end area)
(remove-overlays end old-end 'phscroll-left t)
(remove-overlays end old-end 'phscroll-right t)))
;; Shift relative positions in updated ranges list
(when (/= old-beg beg)
(phscroll-area-shift-updated-ranges-after old-beg (- old-beg beg)
area))
;; Move overlay
(move-overlay ov beg end)
;; Update new area range
(when update
(phscroll-update-area-display area)))))
area)
(defun phscroll-area-merge (to-area from-area &optional update)
"Merge FROM-AREA to TO-AREA.
Call `phscroll-update-area-display' if UPDATE is non-nil.
Return TO-AREA."
(when (and to-area from-area)
(let* ((to-ov (phscroll-area-overlay to-area))
(to-beg (overlay-start to-ov))
(to-end (overlay-end to-ov))
(from-ov (phscroll-area-overlay from-area))
(from-beg (overlay-start from-ov))
(from-end (overlay-end from-ov))
(overlap-beg (max to-beg from-beg))
(overlap-end (min to-end from-end))
(new-beg (min to-beg from-beg))
(new-end (max to-end from-end)))
(if (/= (phscroll-area-get-window-width to-area)
(phscroll-area-get-window-width from-area))
(progn
(phscroll-area-move to-area new-beg new-end)
(move-overlay from-ov new-end new-end)
(delete-overlay from-ov))
;; same window width
;; Remove updated-ranges in overlapping area from to-area and from-area.
;; If overlap, they may be broken.
(when (< overlap-beg overlap-end)
(phscroll-area-remove-updated-range overlap-beg overlap-end to-area)
(phscroll-area-remove-updated-range overlap-beg overlap-end from-area))
;; Shift relative positions in updated-ranges
(phscroll-area-shift-updated-ranges-after to-beg (- to-beg new-beg) to-area)
(phscroll-area-shift-updated-ranges-after from-beg (- from-beg new-beg) from-area)
;; Concat updated-ranges list
(phscroll-area-updated-ranges-set
to-area
(let* ((lower-ranges (phscroll-area-updated-ranges-get
(if (< to-beg from-beg) to-area from-area)))
(upper-ranges (phscroll-area-updated-ranges-get
(if (< to-beg from-beg) from-area to-area)))
(last-lower (car (last lower-ranges)))
(first-upper (car upper-ranges)))
;; Merge last of lower ranges and first of upper ranges
;;..(last-beg . last-end=first-beg)(last-end=first-beg . first-end)..
;;..(last-beg . first-end)..
(when (and last-lower
first-upper
(= (cdr last-lower) (car first-upper)))
(setcdr last-lower (cdr first-upper))
(setq upper-ranges (cdr upper-ranges)))
;; Concat list
(nconc lower-ranges upper-ranges)))
;; Move overlay
(move-overlay to-ov new-beg new-end)
(move-overlay from-ov new-end new-end)
;; Delete from-ov
(delete-overlay from-ov)))
(when update
(phscroll-update-area-display to-area)))
to-area)
(defun phscroll-area-split (area pos)
"Split AREA at POS.
AREA will be the first half of the divided area.
Return a new area that is the second half of the divided area."
(when area
(let* ((area-ov (phscroll-area-overlay area))
(area-beg (overlay-start area-ov))
(area-end (overlay-end area-ov)))
(when (and (< area-beg pos) (< pos area-end))
(let ((prev-r (phscroll-area-updated-ranges-head area))
(rel-pos (- pos area-beg))
new-updated-ranges)
;; Find first range r.end > pos
(while (and (cdr prev-r) (<= (cdadr prev-r) rel-pos))
(setq prev-r (cdr prev-r)))
;; If r.beg < pos, split r
(if (< (caadr prev-r) rel-pos)
(progn
(setq new-updated-ranges
(cons
(cons rel-pos (cdadr prev-r))
(cddr prev-r)))
(setf (cdadr prev-r) rel-pos)
(setf (cddr prev-r) nil))
;; pos <= r.beg and r.end
(setq new-updated-ranges (cdr prev-r))
(setcdr prev-r nil))
;; Move overlay first half
(move-overlay area-ov area-beg pos)
;; Create a area second half
(let ((new-area (phscroll-area-create
pos area-end
(overlay-get area-ov 'evaporate)
(phscroll-get-scroll-column area)
new-updated-ranges
(phscroll-area-get-window-width area))))
;; Shift updated ranges
(phscroll-area-shift-updated-ranges-after pos (- area-beg pos) new-area)
new-area))))))
;; Area Scroll Position
(defconst phscroll-interactive-scroll-commands
'(phscroll-set-scroll-column
phscroll-scroll-left
phscroll-scroll-right
phscroll-recenter
phscroll-recenter-left-right
phscroll-mwheel-scroll-left
phscroll-mwheel-scroll-right))
(defun phscroll-get-scroll-column (&optional area)
(nth 1 (or area (phscroll-get-current-area))))
(defun phscroll-set-scroll-column (pos &optional area)
(interactive "nColumn: ")
(when (null area)
(setq area (phscroll-get-current-area)))
(when (< pos 0)
(setq pos 0))
(when (and area (not (= (phscroll-get-scroll-column area) pos)))
(setcar (nthcdr 1 area) pos)
(phscroll-update-area-display area t)))
(defun phscroll-add-scroll-column (delta &optional area)
(when (null area)
(setq area (phscroll-get-current-area)))
(when area
(phscroll-set-scroll-column
(+ (phscroll-get-scroll-column area) delta)
area)))
(defun phscroll-scroll-left (&optional arg area)
(interactive "P")
(phscroll-scroll-left-right-internal
(if arg (prefix-numeric-value arg) (phscroll-scroll-left-right-unit))
area))
(defun phscroll-scroll-right (&optional arg area)
(interactive "P")
(phscroll-scroll-left-right-internal
(- (if arg (prefix-numeric-value arg) (phscroll-scroll-left-right-unit)))
area))
(defun phscroll-scroll-left-right-unit ()
(max 1
(- (phscroll-window-width-at (point) nil)
(phscroll-margin-right)
4)))
(defun phscroll-scroll-left-right-internal (delta area)
(when phscroll-scroll-left-right-reverse-direction
(setq delta (- delta)))
(when (null area)
(setq area (phscroll-get-current-area)))
(when area
(when (and phscroll-scroll-left-right-move-point
(<= (phscroll-area-begin area) (point))
(< (point) (phscroll-area-end area)))
(cond
((> delta 0)
(goto-char
(+ (phscroll-line-begin)
(phscroll-string-length
(car (phscroll-substring-over-width
(phscroll-current-line-string)
(+ (phscroll-column (point))
delta)))))))
((< delta 0)
(goto-char
(+ (phscroll-line-begin)
(phscroll-string-length
(car (phscroll-substring-over-width
(phscroll-current-line-string)
(max 0 (+ (phscroll-column (point)) delta)))))))))))
(phscroll-add-scroll-column delta area))
(defun phscroll-column (pos)
(phscroll-string-width
(phscroll-buffer-substring
(phscroll-line-begin pos) pos)))
(defun phscroll-show-point (pos)
(let ((area (phscroll-get-area-at pos)))
(when area
(let ((scroll-column (phscroll-get-scroll-column area))
(pos-column (phscroll-column pos))
(window-width (phscroll-window-width-at pos nil)))
(cond
((< pos-column scroll-column)
(phscroll-set-scroll-column pos-column area))
((> pos-column (+ scroll-column window-width))
(phscroll-set-scroll-column (- pos-column window-width) area)))))))
(defun phscroll-scroll-point (pos)
(let ((area (phscroll-get-area-at pos)))
(when area
(let ((scroll-column (phscroll-get-scroll-column area))
(pos-column (phscroll-column pos))
(window-width (phscroll-window-width-at pos nil))
(step (if (= hscroll-step 0)
(/ (1+ (phscroll-window-width-at pos nil)) 2)
hscroll-step)))
(cond
((< pos-column (+ scroll-column hscroll-margin))
(phscroll-set-scroll-column (max 0 (- pos-column hscroll-margin step)) area))
((> pos-column (+ scroll-column (- window-width hscroll-margin)))
(phscroll-set-scroll-column (+ (- pos-column window-width) hscroll-margin step) area)))))))
(defun phscroll-recenter (&optional arg)
(interactive "P")
(let* ((pos (point))
(pos-column (phscroll-column pos))
(area (phscroll-get-area-at pos)))
(when area
(phscroll-set-scroll-column
(- pos-column
(if arg
(let ((n (prefix-numeric-value arg)))
(if (>= n 0) n (+ (phscroll-window-width-at pos nil) n)))
(/ (phscroll-window-width-at pos nil) 2)))
area))))
(defvar phscroll-recenter-last-op nil)
(defcustom phscroll-recenter-positions '(center left right)
"Cycling order for `phscroll-recenter-left-right'.
Like a recenter-top-bottom."
:type '(repeat (choice
(const :tag "Left" left)
(const :tag "Center" center)
(const :tag "Right" right)
(integer :tag "Column number")
(float :tag "Percentage")))
:group 'phscroll)
(defun phscroll-recenter-left-right (&optional arg)
;; The foloweing code was copied and modified from
;; recenter-top-bottom in window.el
(interactive "P")
(cond
(arg (phscroll-recenter arg))
(t
(setq phscroll-recenter-last-op
(car (or
(if (eq this-command last-command)
(cdr (member phscroll-recenter-last-op
phscroll-recenter-positions)))
phscroll-recenter-positions)))
(let* ((win-width (phscroll-window-width-at (point) nil))
(this-scroll-margin
(min (max 0 hscroll-margin)
(truncate (/ win-width 4.0)))))
(cond ((eq phscroll-recenter-last-op 'center)
(phscroll-recenter))
((eq phscroll-recenter-last-op 'left)
(phscroll-recenter this-scroll-margin))
((eq phscroll-recenter-last-op 'right)
(phscroll-recenter (- -1 this-scroll-margin)))
((integerp phscroll-recenter-last-op)
(phscroll-recenter phscroll-recenter-last-op))
((floatp phscroll-recenter-last-op)
(phscroll-recenter (round (* phscroll-recenter-last-op win-width)))))))))
(defun phscroll-recenter-top-bottom (&optional _arg)
(interactive "P")
(call-interactively #'recenter-top-bottom)
(phscroll-recenter))
;; Area Overlay
(defun phscroll-area-overlay (&optional area)
(nth 2 (or area (phscroll-get-current-area))))
;; Area Range
(defun phscroll-area-begin (&optional area)
(overlay-start (phscroll-area-overlay (or area (phscroll-get-current-area)))))
(defun phscroll-area-end (&optional area)
(overlay-end (phscroll-area-overlay (or area (phscroll-get-current-area)))))
;; Area Finding
(defun phscroll-area-from-overlay (ov)
(overlay-get ov 'phscroll-area))
(defun phscroll-get-area-at (pos)
(let (area
(overlays (overlays-at pos)))
(while (and overlays
(null (setq area (phscroll-area-from-overlay (car overlays)))))
(setq overlays (cdr overlays)))
area))
(defun phscroll-get-current-area ()
(phscroll-get-area-at (point)))
(defun phscroll-enum-area (&optional beg end)
(interactive "r")
(let* ((overlays (overlays-in (or beg (point-min)) (or end (point-max)))))
(cl-loop for ov in overlays
if (overlay-get ov 'phscroll-area)
collect (overlay-get ov 'phscroll-area))))
(defun phscroll-update-all-area ()
(save-restriction
(widen)
(cl-loop for area in (phscroll-enum-area)
do (phscroll-update-area-display area t))))
(defun phscroll-invalidate-all-area ()
(save-restriction
(widen)
(cl-loop for area in (phscroll-enum-area)
do (phscroll-area-clear-updated-ranges area))))
(defun phscroll-invalidate-region (beg end)
(dolist (area (phscroll-enum-area beg end))
(phscroll-area-remove-updated-range beg end area)))
(defun phscroll-areas-in-window (&optional window)
(phscroll-enum-area
(min (phscroll-window-start window))
(max (phscroll-window-end window))))
(defun phscroll-update-areas-in-window (&optional redraw window)
(cl-loop for area in (phscroll-areas-in-window window)
do (phscroll-update-area-display area redraw window)))
;;;; Updated Range Management
;;
;; updated-ranges:
;; ((-1 . -1) (beg0 . end0) (beg1 . end1) ... (begN . endN))
(defun phscroll-area-updated-ranges-head (area)
(nth 3 area))
(defun phscroll-area-updated-ranges-get (area)
(cdr (phscroll-area-updated-ranges-head area)))
(defun phscroll-area-updated-ranges-set (area ranges)
(when area
(setcdr (nth 3 area) ranges))
area)
(defun phscroll-area-clear-updated-ranges (area)
(phscroll-area-updated-ranges-set area nil))
(defun phscroll-area-add-updated-range (beg end area)
;; 1. check and normalize range (beg, end), if empty then exit
(when (and (< beg end) area)
(let ((area-begin (phscroll-area-begin area))
(area-end (phscroll-area-end area)))
(when (< beg area-begin) (setq beg area-begin))
(when (> end area-end) (setq end area-end))
(when (< beg end)
;; convert to relative position
(setq beg (- beg area-begin))
(setq end (- end area-begin))
;;(message "add-updated-range %s %s to area %s" beg end area)
;; 2. find first range r1.end [>=] beg
(let ((prev-r1 (phscroll-area-updated-ranges-head area)))
(while (and (cdr prev-r1) (< (cdadr prev-r1) beg))
(setq prev-r1 (cdr prev-r1)))
;; 3. if not found, append to last
(if (null (cdr prev-r1))
(setcdr prev-r1 (cons (cons beg end) nil))
;; 4. if r1.beg [>] end, insert before r1
(if (< end (caadr prev-r1))
(setcdr prev-r1 (cons (cons beg end) (cdr prev-r1)))
;; else,
(let ((prev-r2 prev-r1))
;; 5. find last range r2.begin [<=] end
(while (and (cddr prev-r2) (<= (caaddr prev-r2) end))
(setq prev-r2 (cdr prev-r2)))
;; 6. replace r1~r2 to (union r1~r2, beg~end)
(setcdr prev-r1
(cons
(cons
(min (caadr prev-r1) beg)
(max (cdadr prev-r2) end))
(cddr prev-r2))))))))))
area)
(defun phscroll-area-remove-updated-range (beg end area)
;; 1. check and normalize range (beg, end), if empty then exit
(when (and (< beg end) area)
(let ((area-begin (phscroll-area-begin area))
(area-end (phscroll-area-end area)))
(when (< beg area-begin) (setq beg area-begin))
(when (> end area-end) (setq end area-end))
(when (< beg end)
;; convert to relative position
(setq beg (- beg area-begin))
(setq end (- end area-begin))
;; 2. find first range r1.end [>] beg
(let ((prev-r1 (phscroll-area-updated-ranges-head area)))
(while (and (cdr prev-r1) (<= (cdadr prev-r1) beg))
(setq prev-r1 (cdr prev-r1)))
;; 3. if not found, do nothing
(if (null (cdr prev-r1))
nil
;; 4. if r1.beg [>=] end, do nothing
(if (<= end (caadr prev-r1))
nil
;; else,
(let ((prev-r2 prev-r1))
;; 5. find last range r2.begin [<] end
(while (and (cddr prev-r2) (< (caaddr prev-r2) end))
(setq prev-r2 (cdr prev-r2)))
;; 6. subtract beg~end from each r1~r2
(let ((prev-r prev-r1)
(end-prev-r (cdr prev-r2)))
(while (not (eq prev-r end-prev-r))
(cond
;; if beg <= r.beg and r.end <= end, remove r from list
((and (<= beg (caadr prev-r)) (<= (cdadr prev-r) end))
(if (eq (cdr prev-r) end-prev-r) ;;remove last range
(setq end-prev-r prev-r)) ;;set prev-r to end
(setcdr prev-r (cddr prev-r))
;; keep prev-r
)
;; set r to (end . r.end)
((and (<= beg (caadr prev-r)) (< end (cdadr prev-r)))
(setcar (cadr prev-r) end)
(setq prev-r (cdr prev-r)))
;; set r to (r.beg . beg)
((and (< (caadr prev-r) beg) (<= (cdadr prev-r) end))
(setcdr (cadr prev-r) beg)
(setq prev-r (cdr prev-r)))
;; divide r to (r.beg . beg) [reuse](end . r.end)
((and (< (caadr prev-r) beg) (< end (cdadr prev-r)))
(setcdr prev-r
(cons (cons (caadr prev-r) beg)
(cdr prev-r)))
(setcar (caddr prev-r)
end)
(setq prev-r (cddr prev-r)))))))))))))
area)
;; (setq test-area (phscroll-area-create 10 22))
;; (phscroll-area-add-updated-range 12 14 test-area)
;; (phscroll-area-add-updated-range 16 18 test-area)
;; (phscroll-area-add-updated-range 20 22 test-area)
;; (phscroll-area-add-updated-range 13 16 test-area)
;; (phscroll-area-remove-updated-range 13 21 test-area)
;; (phscroll-area-remove-updated-range 10 22 test-area)
(defun phscroll-area-needs-update-range (beg end area)
(when (and (< beg end) area)
(let ((area-begin (phscroll-area-begin area))
(area-end (phscroll-area-end area)))
(when (< beg area-begin) (setq beg area-begin))
(when (> end area-end) (setq end area-end))
(when (< beg end)
;; convert to relative position
(setq beg (- beg area-begin))
(setq end (- end area-begin))
(not
(cl-find-if
(lambda (range) (and (<= (car range) beg) (<= end (cdr range))))
(phscroll-area-updated-ranges-head area)))))))
;;(phscroll-area-needs-update-range 21 23 test-area)
(defun phscroll-area-shift-updated-ranges-after (pos delta area)
(when (and area (/= delta 0))
(let ((area-begin (phscroll-area-begin area))
(r (cdr (phscroll-area-updated-ranges-head area))))
;; convert to relative position
(setq pos (- pos area-begin))
(while (and r (< (cdar r) pos))
(setq r (cdr r)))
(when r
(when (<= pos (caar r)) (setcar (car r) (+ (caar r) delta)))
(when (<= pos (cdar r)) (setcdr (car r) (+ (cdar r) delta)))
(setq r (cdr r))
(while r
(setcar (car r) (+ (caar r) delta))
(setcdr (car r) (+ (cdar r) delta))
(setq r (cdr r))))))
area)
;;(phscroll-area-shift-updated-ranges-after 51 10 test-area)
;; Area outer width
;; area毎にレイアウト時のウィンドウ幅を記録している。
;; 複数のウィンドウが異なる幅を持つ場合にarea毎に別々の幅で表示できると便利なので。
;; 更新時に以前のウィンドウ幅と変わっている場合は、全ての範囲を無効化して再描画する。
;; 別々のウィンドウに同じareaが表示される場合はレイアウトが乱れる場合もあるがそれは諦める。
(defun phscroll-area-get-window-width (area)
(nth 4 area))
(defun phscroll-area-set-window-width (area width)
(setcar (nthcdr 4 area) width))
(defun phscroll-area-window-width-changed-p (area window)
(let ((new-width (phscroll-window-width window))) ;;;@todo find minimum width of (phscroll-window-width-at line-pos window) ?
(when (not (= new-width (phscroll-area-get-window-width area)))
(phscroll-area-set-window-width area new-width)
t)))
;;;; Event Handlers
;; (defun phscroll-on-window-size-changed (&optional _frame)
;; ;;(message "window-size-changed width beg=%s end=%s width=%s" (window-start) (window-end) (window-width))
;; ;;(phscroll-update-all-area) ;;Too slow
;; ;;(phscroll-update-areas-in-window) ;;Do not use. window-start and window-end are not updated
;; ;; (phscroll-invalidate-all-area) ;;Use phscroll-area-window-width-changed-p
;; )
(defun phscroll-on-post-command ()
;(message "on post command window-end=%s" (window-end))
;;(phscroll-show-point (point))
(unless (cl-find this-command phscroll-interactive-scroll-commands)
(phscroll-scroll-point (point)))
;;(phscroll-update-area-display (phscroll-get-area-at (point)))
(phscroll-update-areas-in-window nil nil)
)
(defun phscroll-on-window-scroll (window _new-display-start-pos)
(phscroll-update-areas-in-window nil window))
(defun phscroll-on-pre-redisplay (&optional window)
;;(message "redisplay window=%s start=%s end=%s width=%s" window (window-start window) (window-end window) (window-width window))
(phscroll-check-truncate-lines)
(phscroll-update-areas-in-window nil window))
(defvar-local phscroll-update-area-display-on-modified t)
(defun phscroll-on-modified (ov after beg end &optional before-length)
;;(message "modified %s %s %s %s" after beg end before-length)
(when after
(let* ((after-length (- end beg))
(delta-length (- after-length before-length))
(area (phscroll-area-from-overlay ov)))
(when area
(if (= (phscroll-area-begin area) (phscroll-area-end area))
;; destroy empty area
(phscroll-area-destroy area)
;; update modified range
(cond
((> delta-length 0)
(phscroll-area-shift-updated-ranges-after beg delta-length area)
(phscroll-area-remove-updated-range (phscroll-line-begin beg) (phscroll-line-end end) area))
((< delta-length 0)
(phscroll-area-remove-updated-range (phscroll-line-begin beg) (phscroll-line-end (+ beg before-length)) area)
(phscroll-area-shift-updated-ranges-after end delta-length area))
(t
(phscroll-area-remove-updated-range (phscroll-line-begin beg) (phscroll-line-end end) area)))
;;;@todo do pre-redisplay only?
(when phscroll-update-area-display-on-modified
(phscroll-update-area-display area)))))))
(defun phscroll-check-truncate-lines ()
(when (not (equal truncate-lines phscroll-truncate-lines))
(setq phscroll-truncate-lines truncate-lines)
(when phscroll-truncate-lines
;; remove left, right overlays
(cl-loop for area in (phscroll-enum-area)
do (let ((beg (phscroll-area-begin area))
(end (phscroll-area-end area)))
(remove-overlays beg end 'phscroll-left t)
(remove-overlays beg end 'phscroll-right t))))
;; remove update-ranges and redraw
(phscroll-update-all-area)))
;; Mouse Wheel
(defcustom phscroll-mwheel-scroll-amount-horizontal nil
"Amount to scroll phscroll areas horizontally."
:type '(choice (const :tag "Use `mouse-wheel-scroll-amount-horizontal'" nil)
(integer 4))
:group 'phscroll)
(defun phscroll-mwheel-scroll-left (event)
(interactive "e")
(phscroll-mwheel-scroll-left-right-internal event 1))
(defun phscroll-mwheel-scroll-right (event)
(interactive "e")
(phscroll-mwheel-scroll-left-right-internal event -1))
(defun phscroll-mwheel-scroll-left-right-internal (event dir)
(interactive "e")
(when-let ((point (posn-point (event-start event)))
(window (posn-window (event-start event))))
(with-current-buffer (window-buffer window)
(when-let ((area (phscroll-get-area-at point)))
(phscroll-scroll-right
(* dir
(or phscroll-mwheel-scroll-amount-horizontal
(and (boundp 'mouse-wheel-scroll-amount-horizontal)
mouse-wheel-scroll-amount-horizontal)
4))
area)))))
;; Keymap
(defvar phscroll-keymap
(let ((map (make-sparse-keymap)))
(define-key map "\C-x<" 'phscroll-scroll-left)
(define-key map "\C-x>" 'phscroll-scroll-right)
(define-key map (kbd "C-S-l") 'phscroll-recenter-left-right)
(define-key map (kbd "C-l") 'phscroll-recenter-top-bottom)
;; Shift + Mouse Wheel
(when (boundp 'mouse-wheel-down-event)
(define-key map (vector (list 'shift mouse-wheel-down-event))
'phscroll-mwheel-scroll-left))
(when (boundp 'mouse-wheel-up-event)
(define-key map (vector (list 'shift mouse-wheel-up-event))
'phscroll-mwheel-scroll-right))
map))
(defun phscroll-area-set-keymap (area)
(when area
(overlay-put (phscroll-area-overlay area) 'keymap phscroll-keymap)))
;;;; Window Utilities
(defvar phscroll-use-window-end-update nil) ;;;@todo
(defun phscroll-window-start (window)
;;@todo pre-redisplay-functions 内では正しい値を返さない?
(window-start window))
(defun phscroll-window-end (window)
;;@todo pre-redisplay-functions 内では正しい値を返さない。徐々に増えていく場合がある。
(max
(or (window-end window phscroll-use-window-end-update) 0)
;; window-startからwindow行数だけ進んだ場所。
;; 不可視の行がある場合は正しくないが、再描画が終わるまで待つよりは良い場合がある。不可視の行を判定すればたどり着けるかもしれないが、テキストプロパティやオーバーレイを取得しながらだとおそらくかなり遅い。
(save-excursion
(goto-char (window-start window))
(forward-line (window-body-height window))
(point))))
(defun phscroll-window-width-at (pos window)
(-
(phscroll-window-width window)
;; Count wrap-prefix width.
;; org-indent uses this.
(length (get-text-property pos 'wrap-prefix))
;; Count before-string width at the beginning of the line.
;; org-table-overlay-coordinates uses this.
(let ((bol (phscroll-line-begin pos)))
(cl-loop for ov in (overlays-at bol)
sum (length (and (equal (overlay-start ov) bol)
(overlay-get ov 'before-string)))))))
(defun phscroll-window-width (&optional window)
(max
0
(-
(window-body-width window)
(phscroll-margin-right)
;; line numbers