-
Notifications
You must be signed in to change notification settings - Fork 3
/
magic-buffer.el
1446 lines (1318 loc) · 58.1 KB
/
magic-buffer.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
;;; magic-buffer.el --- -*- lexical-binding: t -*-
;;; Version: 0.1
;;; Author: sabof
;;; URL: https://github.com/sabof/magic-buffer
;;; Commentary:
;; The project is hosted at https://github.com/sabof/magic-buffer
;; The latest version, and all the relevant information can be found there.
;;
;; Some sections have comments such as this:
;;
;; (info "(elisp) Pixel Specification")
;;
;; If you place the cursor in the end, and press C-x C-e, it will take you to
;; the related info page.
;;; License:
;; This file is NOT part of GNU Emacs.
;;
;; 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 2, 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 ; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Code:
(require 'cl-lib)
(require 'color)
(require 'info) ; For title faces
;;; * Library ------------------------------------------------------------------
;; Functions potentially useful in other contexts
(defun mb-in-range (number from to)
"Test whether a number is in FROM \(inclusive\) TO \(exclusive\) range."
(and (<= from number) (< number to)))
(defun mb-table-asciify-char (char)
"Convert UTF8 table characters to their ASCII equivalents.
If a character is not a table character, it will be left unchanged."
;; All table characters belong to the range 9472 inclusive - 9600 exclusive,
;; The comment contains the first character of each range
(cond ( (mb-in-range char 9472 9474) ?-) ; ─
( (mb-in-range char 9474 9476) ?|) ; │
( (mb-in-range char 9476 9478) ?-) ; ┄
( (mb-in-range char 9478 9480) ?|) ; ┆
( (mb-in-range char 9480 9482) ?-) ; ┈
( (mb-in-range char 9482 9484) ?|) ; ┊
( (mb-in-range char 9484 9500) ?-) ; ┌
( (mb-in-range char 9500 9508) ?|) ; ├
( (mb-in-range char 9508 9516) ?|) ; ┤
( (mb-in-range char 9516 9524) ?-) ; ┬
( (mb-in-range char 9524 9532) ?-) ; ┴
( (mb-in-range char 9532 9548) ?+) ; ┼
( (mb-in-range char 9548 9550) ?-) ; ╌
( (mb-in-range char 9550 9552) ?|) ; ╎
( (member char '(?═ ?╒ ?╔ ?╕ ?╗ ?╘ ?╚ ?╛ ?╝)) ?=)
( (member char '(?║ ?╓ ?╖ ?╙ ?╜)) ?-)
( (member char '(?╞ ?╠ ?╡ ?╣ ?╤ ?╦ ?╧ ?╩ ?╪ ?╬)) ?=)
( (member char '(?╟ ?╢ ?╥ ?╨ ?╫)) ?-)
( (member char '(?╭ ?╯ ?╱)) ?/)
( (member char '(?╮ ?╰ ?╲)) (string-to-char "\\"))
( (= char ?╳) ?X)
( (mb-in-range char 9588 9600)
(if (cl-evenp char) ?- ?|))
( t char)))
(defun mb-table-asciify-string (string)
"Convert an UTF8 table to an ASCII table.
Accepts two numeric arguments, and will replace charactres in the
the corresponding region of the buffer."
(cl-loop for char across string
collect (or (mb-table-asciify-char char)
char)
into char-list
finally return (apply 'string char-list)))
(defun mb-table-asciify-region (from to)
(save-excursion
(goto-char from)
(insert-and-inherit
(mb-table-asciify-string
(delete-and-extract-region from to)))))
(defun mb-table-insert (string)
"Insert a table with UTF8 table borders, replacing them with ASCII
fallbacks, if needed."
(let ((start-pos (point))
end-pos)
(condition-case error
(progn
(cl-loop for char across string
do
(cl-assert (char-displayable-p char)))
(insert string)
(setq end-pos (point))
(goto-char start-pos)
(let (( regions (cl-loop while (< (point) end-pos)
collecting (car (mb-posn-at-point
(line-end-position)))
until (cl-plusp (forward-line)))))
(cl-assert (cl-every (apply-partially '= (car regions))
regions)))
(goto-char end-pos))
(error (mb-table-asciify-region
start-pos end-pos)
))))
(defun mb-colorize-string-chars (string)
(cl-loop for i from 0 to (1- (length string))
do (put-text-property
i (1+ i) 'face (append `(:background ,(mb-theme-color-source))
(get-text-property i 'face string))
string))
string)
(defun mb-random-hex-color ()
(apply 'format "#%02X%02X%02X"
(mapcar 'random (make-list 3 255))))
(defun mb-kick-cursor (old new)
(cond ( (and (< old new) (not (= (point-max) (point))))
(forward-char 1))
( (and (not (< old new)) (not (= (point-min) (point))))
(forward-char -1))))
(defmacro mb-with-adjusted-enviroment (&rest body)
(declare (indent defun))
`(save-excursion
(cond ( (get 'mb-with-adjusted-enviroment 'active)
,@body)
( (eq (current-buffer) (window-buffer))
(let (( initial-start (window-start)))
(prog1 (progn ,@body)
(set-window-start nil initial-start))))
( t (unwind-protect
(save-window-excursion
(set-window-buffer nil (current-buffer))
(put 'mb-with-adjusted-enviroment 'active t)
,@body)
(put 'mb-with-adjusted-enviroment 'active nil))))))
(defun mb-posn-at-point (&optional pos)
(unless pos
(setq pos (point)))
(mb-with-adjusted-enviroment
(goto-char pos)
(or (posn-x-y (posn-at-point pos))
(progn
(goto-char (line-beginning-position))
(set-window-start nil (point))
(goto-char pos)
(posn-x-y (posn-at-point pos))))))
(defun mb-region-pixel-width (from to)
"Find a region's pixel "
(mb-with-adjusted-enviroment
(let (( from (car (mb-posn-at-point from)))
( to (car (mb-posn-at-point to))))
(- to from))))
(defun mb-window-inside-pixel-width (&optional window)
(setq window (window-normalize-window window))
(let (( window-pixel-edges (window-inside-pixel-edges)))
(- (nth 2 window-pixel-edges) (nth 0 window-pixel-edges))))
(defun mb-window-inside-pixel-height (&optional window)
(setq window (window-normalize-window window))
(let (( window-pixel-edges (window-inside-pixel-edges)))
(- (nth 3 window-pixel-edges) (nth 1 window-pixel-edges))))
(defun mb-put-halign-overlay (from to &rest properties)
(setq properties
(append (list 'window (selected-window)
'mb-hcenterer (selected-window))
properties))
(let ((ov (make-overlay from to)))
(while properties
(overlay-put ov (pop properties) (pop properties)))
ov))
(defun mb-align-variable-width (&optional right)
(mb-with-adjusted-enviroment
;; Add protection against negative aligmnets
(beginning-of-visual-line)
(let* (( beginning-of-visual-line
(point))
( end-of-visual-line
(save-excursion
(end-of-visual-line)
;; (when (and right (/= (line-end-position) (point)))
;; (skip-chars-backward " " beginning-of-visual-line))
;; (sit-for 0.5)
(point)))
( split-line (/= (line-end-position) end-of-visual-line))
( split-at-limit
(and split-line
(/= (cdr (mb-posn-at-point end-of-visual-line))
(cdr (mb-posn-at-point (point))))))
( pixel-width
(if split-at-limit
(- (mb-window-inside-pixel-width)
(car (mb-posn-at-point)))
(mb-region-pixel-width
(point)
end-of-visual-line)))
( align-spec (if right
;; Full-width lines will break, when word-wrap is
;; enabled. That's why I leave some space in the
;; end.
;; http://debbugs.gnu.org/cgi/bugreport.cgi?2749
`(space :align-to
(- right (,pixel-width)
,(if word-wrap 2.0 0)
))
`(space :align-to
(- center (,(/ pixel-width 2))
;; Didn't seem to be necessary so far.
;; ,(if word-wrap 1.0 0)
)))))
;; (message "split: %s at limit: %s" split-line split-at-limit)
;; (sit-for 0.5)
(if (= (point) (line-beginning-position))
(if (looking-at "[\t ]+")
(progn
(mb-put-halign-overlay
(match-beginning 0)
(match-end 0)
'display align-spec))
(mb-put-halign-overlay
(line-beginning-position)
(line-end-position)
'before-string
(propertize " " 'display align-spec)))
(if (looking-at "[\t ]+") ; in the middle of a logical line
;; In some cases, when a line is almost equal to the window's
;; width, and it ends with an align-to spec, it will belong to the
;; next line, while being centered to the previous, resulting in
;; that character's disappearance. Or something like that. Might
;; try to reproduce it later.
;; This is mostly relevant for cases when truncate-lines is
;; non-nil, and word-wrap is nil -- a broken line with word-wrap
;; usually begins with a visible character.
(if right
(mb-put-halign-overlay
(match-beginning 0)
(match-end 0)
'display
`(space :width (,(- (mb-window-inside-pixel-width)
pixel-width))))
(mb-put-halign-overlay
(match-beginning 0)
(match-end 0)
'display
`(space :width (,(/ (- (mb-window-inside-pixel-width)
pixel-width)
2)))))
(mb-put-halign-overlay
beginning-of-visual-line
end-of-visual-line
'before-string
(concat "\n" (propertize " " 'display align-spec)))))
;; (sit-for 0.5)
;; Frame width 65
;; (error "test")
pixel-width
)))
(defvar mb-buffer-auto-align-markers
'((right/center (marker) (marker)))
)
(defun mb-align-regions-horizontally-do (&optional all-windows)
(cl-dolist (win (if all-windows
(get-buffer-window-list)
(list (selected-window))))
;; (with-selected-window win
;; (cl-loop for (type start end) in mb-buffer-auto-align-markers
;; do
;; ))
))
(defun mb-auto-align-region-horizontally ()
(add-hook 'window-configuration-change-hook 'ignore)
(add-hook 'post-command-hook 'ignore))
(defun mb-delete-subsequence (from to list)
(let (( after-to (nthcdr to list)))
(if (zerop from)
after-to
(progn
(setcdr (nthcdr (1- from) list) after-to)
list))))
(defun mb-plist-remove-key (key plist)
(let ((pos (cl-position key plist)))
(if pos
(mb-delete-subsequence
pos (+ 2 pos) plist)
plist)))
(defun mb-show-in-two-columns (outer-margins inner-border rows)
(cl-dolist (row rows)
(insert (propertize " " 'display `(space :align-to ,outer-margins))
(car row)
(apply 'propertize " "
'display `(space :align-to (- center (,inner-border . 0.5)))
(mb-plist-remove-key
'display (text-properties-at
0 (car row))))
(propertize " "
'display `(space :width ,inner-border)
)
(nth 1 row)
(apply 'propertize " "
'display `(space :align-to (- right ,outer-margins))
(mb-plist-remove-key
'display (text-properties-at
0 (nth 1 row))))
(apply 'propertize "\n" (car (last row))))))
(defun mb-content-height ()
(let (added-newline)
(mb-with-adjusted-enviroment
(with-silent-modifications
(set-window-start nil (point-min))
(goto-char (point-max))
(unless (or (equal (char-before) ?\n )
(= (point-min) (point-max)))
(insert "\n")
(setq added-newline t))
(prog1 (cdr (posn-x-y (posn-at-point)))
(when added-newline
(delete-char -1)))
))))
(defvar mb-centerv nil)
(defun mb-virtualize-overlay (ov)
(prog1 (append (list (overlay-start ov) (overlay-end ov))
(overlay-properties ov))
(delete-overlay ov)))
(let (( colors
(sort (mapcar (lambda (face)
(face-attribute face :foreground))
'(font-lock-string-face
font-lock-keyword-face
font-lock-comment-face
font-lock-variable-name-face
font-lock-function-name-face))
(lambda (a b)
(zerop (random 2)))))
expendable-colors)
(defun mb-theme-color-source ()
(unless expendable-colors
(setq expendable-colors colors))
(pop expendable-colors)))
(defun mb-realize-overlay (ov-spec)
(cl-destructuring-bind
(start end &rest props)
ov-spec
(let ((ov (make-overlay start end)))
(while props (overlay-put ov (pop props) (pop props)))
ov)))
(cl-defun mb-insert-bordered-text (text &key (border-width 3)
(horizontal-margins 20)
(padding 5)
(border-color "DarkRed")
(right-align-spec 'right))
(let* (( segment-left-margin
(lambda (&optional additonal-specs)
(propertize " "
'display `(space :align-to (,horizontal-margins)
,@additonal-specs))))
( segment-filler
(lambda (&optional additonal-specs)
(propertize " "
'display `(space :align-to
(- ,right-align-spec
(,border-width)
(,horizontal-margins))
,@additonal-specs))))
( segment-lr-border
(lambda (&optional additonal-specs)
(propertize " " 'display `(space :width (,border-width)
,@additonal-specs)
'face `(:background ,border-color))))
( segment-lr-padding
(lambda ()
(propertize " " 'display `(space :width (,padding)))))
( segment-tb-border
(lambda ()
(propertize
(concat
(funcall segment-left-margin `(:height (,border-width)))
(propertize " "
'display `(space :align-to
(- ,right-align-spec
(,horizontal-margins))
:height (,border-width))
'face `(:background ,border-color))
(propertize "\n" 'line-height t))
'point-entered 'mb-kick-cursor)))
( segment-tb-padding
(lambda ()
(propertize
(concat
(funcall segment-left-margin `(:height (,padding)))
(funcall segment-lr-border `(:height (,padding)))
(funcall segment-filler `(:height (,padding)))
(funcall segment-lr-border `(:height (,padding)))
(propertize "\n" 'line-height t))
'point-entered 'mb-kick-cursor)))
( segment-line
(lambda (text)
(concat (propertize
(concat
(funcall segment-left-margin)
(funcall segment-lr-border)
(funcall segment-lr-padding))
'point-entered 'mb-kick-cursor)
(propertize text
'wrap-prefix
(concat (funcall segment-left-margin)
(funcall segment-lr-border)
(funcall segment-lr-padding)))
(propertize
(concat
(funcall segment-filler)
(funcall segment-lr-border)
"\n")
'point-entered 'mb-kick-cursor)))))
(insert (funcall segment-tb-border)
(funcall segment-tb-padding))
(mapc (lambda (line) (insert (funcall segment-line line)))
(split-string text "\n"))
(insert (funcall segment-tb-padding)
(funcall segment-tb-border))
))
(cl-defun mb-insert-shadowed-text
(text &key (horizontal-margins 20)
(padding 5)
(shadow-distance 5)
(background-color "DarkRed")
(right-align-spec 'right))
(let* (;; Legacy stuff
(border-width 5)
( stipple
;; Everything can be converted to bits, so as far as I can tell,
;; everything will work.
'(2 2 "a
a"))
( segment-left-margin
(lambda (&optional additonal-specs)
(propertize " "
'display `(space :align-to (,horizontal-margins)
,@additonal-specs))))
( segment-filler
(lambda (&optional additonal-specs)
(propertize " "
'display `(space :align-to
(- ,right-align-spec
(,border-width)
(,horizontal-margins))
,@additonal-specs)
'face `(:background ,background-color))))
( segment-r-shadow
(lambda (&optional additonal-specs)
(propertize " " 'display `(space :width (,border-width)
,@additonal-specs)
'face `(:foreground "Black" :stipple ,stipple))))
( segment-lr-padding
(lambda ()
(propertize " " 'display `(space :width (,padding))
'face `(:background ,background-color))))
( segment-b-shadow
(lambda ()
(propertize
(concat
(funcall segment-left-margin `(:height (,border-width)))
(propertize " "
'display `(space :align-to
(- ,right-align-spec
(,horizontal-margins))
:height (,shadow-distance)
:width (,shadow-distance)))
(propertize " "
'display `(space :align-to
(- ,right-align-spec
(,horizontal-margins))
:height (,shadow-distance))
'face `(:foreground "Black" :stipple ,stipple))
(propertize "\n" 'line-height t))
;; 'point-entered 'mb-kick-cursor
)))
( segment-tb-padding
(lambda ()
(propertize
(concat
(funcall segment-left-margin `(:height (,padding)))
(funcall segment-filler `(:height (,padding))))
;; 'point-entered 'mb-kick-cursor
)))
( segment-line
(lambda (text)
(concat (propertize
(concat
(funcall segment-left-margin)
(funcall segment-lr-padding))
;; 'point-entered 'mb-kick-cursor
)
(propertize text
'wrap-prefix
(concat (funcall segment-left-margin)
(funcall segment-r-shadow)
(funcall segment-lr-padding))
'face `(:background ,background-color))
(propertize
(concat
(funcall segment-filler)
(funcall segment-r-shadow)
"\n")
;; 'point-entered 'mb-kick-cursor
)))))
(insert ;; (funcall segment-b-shadow)
(funcall segment-tb-padding)
(propertize "\n" 'line-height t))
(mapc (lambda (line) (insert (funcall segment-line line)))
(split-string text "\n"))
(insert (funcall segment-tb-padding)
(funcall segment-r-shadow `(:height (,padding)))
(propertize "\n" 'line-height t)
(funcall segment-b-shadow)
)
))
(cl-defun mb--recenter-buffer-vertically (&optional dont-recenter)
(let* (content-height
( window-height (mb-window-inside-pixel-height))
( inhibit-read-only t)
( old-overlays
(cl-remove-if-not
(lambda (ov)
(and (overlay-get ov 'mb-vcenterer)
(overlay-get ov 'mb-hcenterer)
(eq (overlay-get ov 'window)
(selected-window))))
(overlays-at (point-min))))
( old-before-string
(and (car old-overlays)
(overlay-get (car old-overlays) 'mb-hcenterer)
(overlay-get (car old-overlays) 'before-string)))
( old-horizontal-centering
(or (and old-before-string
(string-match " " old-before-string)
(get-text-property (match-beginning 0) 'display
old-before-string))
""))
new-before-string
new-ov)
(mapc 'delete-overlay old-overlays)
(unless (setq content-height (mb-content-height))
(cl-return-from mb--recenter-buffer-vertically nil))
(setq new-before-string
(concat (when content-height
(propertize "\n"
'line-height
(/ (- window-height
content-height) 2)))
old-horizontal-centering))
(when (not (string= "" new-before-string))
(setq new-ov (make-overlay
(point-min)
(min (point-max)
(1+ (point-min)))))
(overlay-put new-ov 'mb-centerer t)
;; (overlay-put new-ov 'read-only t)
(overlay-put new-ov 'window (selected-window))
(overlay-put new-ov 'before-string new-before-string))
(unless dont-recenter
(set-window-start nil (point-min)))))
(cl-defun mb-center-buffer-vertically (&optional (buffer (current-buffer)))
"Inserts a newline character in the beginning of the buffer,
displayed in a way that will make the buffer appear vertically
centered. Not meant to be used in \"writing\" buffers, where
undo history is important."
(with-current-buffer buffer
(add-hook 'window-configuration-change-hook
'mb--recenter-buffer-vertically
nil t)
(add-hook 'post-command-hook
'mb--recenter-buffer-vertically
nil t)
(add-hook 'window-scroll-functions
(lambda (&rest ignore)
(mb--recenter-buffer-vertically t))
nil t)
;; (add-hook 'window-scroll-functions 'mb--recenter-buffer nil t)
))
(defun mb-clear-window-align-overlays ()
(remove-overlays (window-start) (window-end nil t)
'mb-hcenterer-window (selected-window)))
(defun mb-realign-hook ()
(mb-clear-window-align-overlays)
(goto-char (window-start))
(let ((prop))
(while (< (point) (window-end nil t))
(setq prop (get-text-property mb-hcenterer (point))))))
(define-minor-mode mb-alignment-mode
"Doc" nil nil nil
(if mb-alignment-mode
(progn
(add-hook))))
;;; * Helpers ------------------------------------------------------------------
;; Utilities that make this presentation possible
(defvar mb-sections nil)
(setq mb-sections nil)
(defvar mb-counter 1)
(setq mb-counter 1)
(defvar mb--exclusive-section nil
"Only show the section with a particular number.
Created to ease development.")
(defvar mb-expamle-image
(or (and load-file-name
(file-exists-p
(concat
(file-name-directory load-file-name)
"lady-with-an-ermine.jpg"))
(concat
(file-name-directory load-file-name)
"lady-with-an-ermine.jpg"))
(and buffer-file-name
(file-exists-p
(concat
(file-name-directory buffer-file-name)
"lady-with-an-ermine.jpg"))
(concat
(file-name-directory buffer-file-name)
"lady-with-an-ermine.jpg"))
(let (( file-name
(concat temporary-file-directory
"lady-with-an-ermine.jpg")))
(url-copy-file
"https://raw.github.com/sabof/magic-buffer/master/lady-with-an-ermine.jpg"
file-name t)
file-name)))
(defmacro mb-section (name &rest body)
(declare (indent defun))
`(let* (( cons (car (push (list (prog1 mb-counter
(cl-incf mb-counter)))
mb-sections))))
(setcdr cons (list ,name
,(if (stringp (car body))
(pop body)
nil)
(lambda ()
,@body)))))
(defmacro mb-subsection (name &rest body)
(declare (indent defun))
`(progn
(insert "\n" (propertize ,name 'face 'info-title-4) "\n")
,@body))
(defun mb-insert-filled (string)
(let ((beginning (point)))
(insert string)
(fill-region beginning (point))))
(defun mb-comment (string)
(mb-insert-filled (propertize string 'face '(:inherit (variable-pitch font-lock-comment-face))))
(insert "\n"))
(defmacro mb-insert-info-links (&rest links)
`(progn
(delete-char -1)
,@(mapcar (lambda (link)
`(progn
(insert-text-button ,(cadr link) 'action
(lambda (e) (info ,(cadr link))))
(insert (propertize " | " 'face 'bold))))
links)
(delete-char -3)
(insert "\n")))
;;; * Sections ------------------------------------------------------------------
(mb-section "Horizontal line"
"The point-entered property prevents the point from staying on that location,
since that would change the color of the line."
(insert (propertize
"\n"
'display `(space :align-to (- right (1)))
'face '(:underline t)
'point-entered 'mb-kick-cursor
))
(insert "\n"))
;; -----------------------------------------------------------------------------
(mb-section "Stipples / 2 columns / Line cursor"
"Uses stipples that come with your unix distribution. They have \
some re-drawing issues after scrolling."
(let ((ori-point (point))
grid-strings stipple-names
( grey-stipple
'(2 2 "a
a")))
(cl-dolist (dir x-bitmap-file-path)
(setq stipple-names
(nconc
stipple-names
(cl-remove-if (lambda (file) (member file '(".." ".")))
(directory-files dir)))))
(setq stipple-names
(sort stipple-names
(lambda (&rest ignore)
(zerop (random 2)))))
(setq stipple-names
(or (last stipple-names 12)
(make-list 12 grey-stipple)))
(while stipple-names
(let* (( current-batch
(list (pop stipple-names)
(pop stipple-names))))
(push (nconc (mapcar
(lambda (name)
(cond ( (not name)
" ")
( (stringp name)
(propertize name 'face
'(:weight
bold
:inherit variable-pitch)))
( t "default")))
current-batch)
(list (list 'line-height 2.0)))
grid-strings)
(push (nconc (mapcar (lambda (stipple)
(if stipple
(propertize
" " 'face
`(:inherit
font-lock-comment-face
:stipple ,stipple))
" "))
current-batch)
(list (list 'line-height 2.0)))
grid-strings)))
(setq tmp (length grid-strings))
(setq grid-strings (nreverse grid-strings))
(mb-show-in-two-columns '(20) 2 grid-strings)
(backward-char)
(setq-local face-remapping-alist
`((hl-line (:background
,(apply 'format "#%02X%02X%02X"
(mapcar (apply-partially '* 255)
(color-complement
(face-attribute 'cursor :background))))
:inverse-video t))))
(add-text-properties ori-point (point)
(list 'point-entered (lambda (&rest ignore)
(setq cursor-type nil)
(hl-line-mode))
'point-left (lambda (&rest ignore)
(setq cursor-type t)
(hl-line-mode -1))))))
;; -----------------------------------------------------------------------------
(mb-section "Differentiate displays"
(mb-insert-info-links
(info "(elisp) Defining Faces")
(info "(elisp) Display Feature Testing"))
(insert "\n")
(defface mb-diff-terminal
`(( ((type graphic))
(:background ,(mb-theme-color-source)
:foreground ,(face-attribute 'default :background)
:weight bold))
( ((class color)
(min-colors 88))
(:background ,(mb-theme-color-source)
:foreground ,(face-attribute 'default :background)
:weight bold))
( ((class color)
(min-colors 88))
(:background ,(mb-theme-color-source)
:foreground ,(face-attribute 'default :background)
:weight bold))
( t (:background ,(mb-theme-color-source)
:foreground ,(face-attribute 'default :background)
:weight bold)
))
"A face with different background, depending on type of display")
(mb-insert-filled
(propertize " This text will have a different background, depending on \
the type of display (Graphical, tty, \"full color\" tty). "
'face 'mb-diff-terminal))
(insert "\n"))
;; -----------------------------------------------------------------------------
(mb-section "Differentiate windows"
(mb-insert-info-links
(info "(elisp) Overlay Properties"))
(insert "\n")
(let (( text " This text will have a different background color in each \
window it is displayed ")
( window-list (list 'window-list))
( point-a (point))
point-b)
(insert text)
(setq point-b (point))
(add-hook 'window-configuration-change-hook
(lambda (&rest ignore)
(cl-dolist (win (get-buffer-window-list nil nil t))
(unless (assoc win (cdr window-list))
(let ((ov (make-overlay point-a point-b)))
(setcdr window-list (cl-acons win ov (cdr window-list)))
(overlay-put ov 'window win)
(overlay-put ov 'face
`(:background
,(mb-theme-color-source)
:foreground ,(face-attribute 'default :background)
:weight bold)))
)))
nil t)))
;; -----------------------------------------------------------------------------
(mb-section "Aligning fixed width text"
(mb-insert-info-links
(info "(elisp) Pixel Specification"))
(mb-comment "The alignment will persist on window resizing, unless the window is narrower
than the text.")
(let* (( text-lines (split-string
"Reperiuntur quaeritur horrent nisi, summum solido animo
Consequi quapropter e sed dolor ita fore
Censes profecto legendos neque quid, omne laudantium putanda beatus philosophi
Fieri quam ad nos et ut alios voluptatibus, statuam
Cernantur individua ista dicam tua igitur philosophia amicitia numeranda arbitratu"
"\n")))
(mb-subsection "Center"
(insert "\n")
(cl-dolist (text text-lines)
(let ((spec `(space :align-to (- center ,(/ (length text) 2)))))
(insert (propertize text 'line-prefix
(propertize " " 'display spec))
"\n"))))
(mb-subsection "Right"
(insert "\n")
(cl-dolist (text text-lines)
(let ((spec `(space :align-to (- right ,(length text) (1)))))
(insert (propertize text 'line-prefix
(propertize " " 'display spec))
"\n")))))
(mb-subsection "Display on both sides of the window"
(insert "\n")
(let* (( text-left "LEFT --")
( text-right "-- RIGHT")
( spec `(space :align-to (- right ,(length text-right) (1)))))
(insert text-left)
(insert (propertize " " 'display spec))
(insert text-right)
)))
;; -----------------------------------------------------------------------------
(mb-section "Aligning variable width text"
(mb-insert-info-links
(info "(elisp) Pixel Specification"))
(mb-comment "Will break, should the size of frame's text
change. If there are line breaks, the lines won't align after a
window resize. *WIP*")
(let* (( paragraphs "Omnino enim quidem concederetur, sapiens qua. Concessum \
rerum non cum dicta iudiciorumque assecutus cum, nutu feci magnum
Suscipiet tum, ista et dicenda cum
Faciendum sine ut litterae sentit autem neque
Partitio partes ea operosam te, doloris etiam
De ad, quam, nam laboriosam hoc triarius, qua multo graecis aut")
start end)
(mb-subsection "Center"
(insert "\n")
(setq start (point))
(cl-loop for text in (split-string paragraphs "\n")
for height = 2.4 then (- height 0.4)
for face-spec = `(:inherit variable-pitch :height ,height)
do (insert (propertize text 'face face-spec) "\n"))
(setq end (point))
(let (virtual-overlays)
;; (vertical-motion) seems to misbehave when the buffer is burried
;; (mb-with-adjusted-enviroment) ensures that the buffer is displayed. It
;; also reduces multiple (save-window-excurson)s to one.
(mb-with-adjusted-enviroment
(goto-char start)
(cl-loop while (< (point) end)
do
(mb-align-variable-width)
(unless (cl-plusp (vertical-motion 1))
(return)))))
(goto-char (point-max)))
(mb-subsection "Right"
(insert "\n")
(save-excursion
(cl-loop for text in (split-string paragraphs "\n")
for height = 1.0 then (+ height 0.4)
for face-spec = `(:inherit variable-pitch :height ,height)
do (insert (propertize text 'face face-spec) "\n"))
(setq end (point)))
(mb-with-adjusted-enviroment
(cl-loop while (< (point) end)
do
(mb-align-variable-width 'right)
(unless (cl-plusp (vertical-motion 1))
(return)))))
))
;; -----------------------------------------------------------------------------
(mb-section "Re-align after variable-width font lines"
"Similar to what `fill-column-indicator' does. A similar effect
can be achieved by setting `tab-width' to a large number, and
splitting columns with tabs, but this will affect tabs in the
whole buffer. The red line will move further to the right,
should the preceeding text be long."
(let* (( sentances (split-string
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Donec hendrerit tempor tellus.
Donec pretium posuere tellus.
Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Nulla posuere.
Donec vitae dolor.
Nullam tristique diam non turpis.
Cras placerat accumsan nulla.
Nullam rutrum.
Nam vestibulum accumsan nisl."
"\n"))
( spec `(space :align-to 80)))
(cl-dolist (sentance sentances)
(insert (propertize sentance 'face `(:inherit
variable-pitch
:height ,(+ 1.0 (/ (random 10) 10.0))))
(propertize " " 'display spec)
(propertize " " 'face '(:background "DarkRed")
'display '(space :width (3)))
" More text"
"\n"))))
;; -----------------------------------------------------------------------------
;; (mb-section "Center horizontally and vertically"
;; (insert-button "Show in new buffer"
;; 'action (lambda (e)