-
Notifications
You must be signed in to change notification settings - Fork 12
/
rebox2.el
2884 lines (2575 loc) · 109 KB
/
rebox2.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
;;; rebox2.el --- Handling of comment boxes in various styles.
;; Filename: rebox2.el
;; Description:
;; Author: François Pinard
;; Le Wang
;; Maintainer: Le Wang (lewang.emacs!!!gmayo.com remove exclamations, correct host, hint: google mail)
;; Copyright © 2011 Le Wang
;; Copyright © 1991,92,93,94,95,96,97,98,00 Progiciels Bourbeau-Pinard inc.
;; François Pinard <[email protected]>, April 1991.
;; Created: Mon Jan 10 22:22:32 2011 (+0800)
;; Version: 0.7
;; Last-Updated: Tue Nov 13 20:57:37 2012 (+0800)
;; By: Le Wang
;; Update #: 478
;; URL: https://github.com/lewang/rebox2
;; Keywords:
;; Compatibility: GNU Emacs 23.2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Hi, I'm a box. My style is 525 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Features first:
;;
;; ** minor-mode features
;;
;; - auto-fill boxes (install filladapt for optimal filling)
;; - motion (beginning-of-line, end-of-line) within box
;; - S-return rebox-newline
;; - kill/yank (within box) only text, not box borders
;; - move box by using space, backspace / center with M-c
;; - point has to be to the left of the border
;;
;; ** interesting variables for customization
;;
;; - `rebox-style-loop'
;; - `rebox-min-fill-column'
;; - `rebox-allowance'
;;; Installation:
;;
;; 1. Add rebox2.el to a directory in your load-path.
;;
;; 2. Basic install - add to your ".emacs":
;;
;; (setq rebox-style-loop '(24 16))
;; (require 'rebox2)
;; (global-set-key [(meta q)] 'rebox-dwim)
;; (global-set-key [(shift meta q)] 'rebox-cycle)
;;
;; Note that if you do not need to specify the HUNDREDS digit of the style,
;; rebox will figure it out based on the major-moe.
;;
;; 3. Full install - use `rebox-mode' in major-mode hooks:
;;
;; ;; setup rebox for emacs-lisp
;; (add-hook 'emacs-lisp-mode-hook (lambda ()
;; (set (make-local-variable 'rebox-style-loop) '(25 17 21))
;; (set (make-local-variable 'rebox-min-fill-column) 40)
;; (rebox-mode 1)))
;;
;; Default `rebox-style-loop' should work for most programming modes, however,
;; you may want to set the style you prefer.
;;
;; Here is an customization example that
;;
;; - sets comments to use "/* ... */" style in c++-mode
;; - adds Doxygen box style for C++
;; (defun my-c++-setup ()
;; (setq comment-start "/* "
;; comment-end " */")
;; (unless (memq 46 rebox-style-loop)
;; (make-local-variable 'rebox-style-loop)
;; (nconc rebox-style-loop '(46))))
;; (add-hook 'c++-mode-hook #'my-c++-setup)
;;
;;; Ideas removed from François Pinard's original version
;;
;; * Building styles on top of each other.
;;
;;; Future improvement ideas:
;;
;; * remove reliance on dynamic binding using `destructuring-bind' or a hash
;; * allow mixed borders "-=-=-=-=-=-"
;; * optimize functions that modify the box contents so they don't unbuild and
;; rebuild boxes all the time.
;; * style selection can use some kind of menu completion where all styles are
;; presented and the user navigates
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;;
;; 0.6
;;
;; * add `rebox-allowance' customization to allow title in top/bottom border
;; or not.
;; * restructured internal datastructure to be hash-table.
;;
;; 0.5
;;
;; * major breaking change
;;
;; * entry-functions completely rethought and simplified
;;
;; removed:
;; * `rebox-region'
;; * `rebox-comment'
;; * `rebox-dwim-fill'
;; * `rebox-dwim-no-fill'
;; added:
;; * `rebox-dwim'
;; * `rebox-fill'
;; * `rebox-cycle'
;;
;; * boxing styling system rethought and simplified
;;
;; removed:
;; * `rebox-default-style'
;; * `rebox-no-box-comment-style'
;; added:
;; * `rebox-style-loop'
;;
;; * new system description
;;
;; I coded the previous entry system when I was new to Emacs lisp years and
;; years ago. The new system is much more intuitive.
;;
;; Call `rebox-dwim' to refill current box or region and cycle with refill.
;;
;; Call `rebox-cycle' to cycle style without refilling.
;;
;; Instead of describing the boxed style and comment style as different
;; variables, you describe the loop you want rebox to follow with
;; `rebox-style-loop'. If rebox detects that the current box style is not
;; in the loop, it uses the first style.
;;
;; `rebox-style-loop' is not made to be always buffer-local, and you can
;; customize it using the custom facilities. Optionally, you can make a
;; buffer-local version of it in a major-mode hook yourself.
;;
;; 0.4
;;
;; * add `rebox-min-fill-column' option fo minimum box size
;;
;; before that
;;
;; * better error handling
;; * fixed a few boxing and unboxing corner cases where boxes were malformed
;; * changed how spaces are handled, rebox was very aggressive in removing
;; white space in every direction, and even when it was keeping spaces, it
;; would delete them and reinsert, which killed any markers.
;;
;; now spaces are precious and never aggressively deleted. Unboxing
;; followed by boxing is idempotent.
;; * instead of parsing a current-prefix-arg in convoluted ways `rebox-engine'
;; and most other functions now take key parameters
;; * increased the lengh of the box text in the box definition so that longer
;; merged top and bottom borders can be specified.
;; * auto-filling
;; * minor-mode
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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, 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., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
;;,----
;;| François Pinard's original commentary (with non-relevant stuff removed)
;;`----
;; For comments held within boxes, it is painful to fill paragraphs, while
;; stretching or shrinking the surrounding box "by hand", as needed. This
;; piece of GNU Emacs LISP code eases my life on this. I find only fair,
;; while giving all sources for a package using such boxed comments, to also
;; give the means I use for nicely modifying comments. So here they are!
;; The function `rebox-comment' automatically discovers the extent of the
;; boxed comments near the cursor, possibly refills the text, then adjusts the
;; comment box style. When this command is executed, the cursor should be
;; within a comment, or else it should be between two comments, in which case
;; the command applies to the next comment. The function `rebox-region' does
;; the same, except that it takes the current region as a boxed comment. Both
;; commands obey numeric prefixes to add or remove a box, force a particular
;; box style, or to prevent refilling of text. Without such prefixes, the
;; commands may deduce the current comment box style from the comment itself
;; so the style is preserved. An unboxed comment is merely one of box styles.
;; A style is identified by three non-zero digits. The _convention_ about
;; style numbering is such the the hundreds digit roughly represents the
;; programming language, the tens digit roughly represents a box quality (or
;; weight) and the units digit roughly a box type (or figure). Language,
;; quality and types are collectively referred to as style attributes.
;;;; Convention:
;; A programming language is associated with comment delimiters. Values are
;; 100 for none or unknown, 200 for `/*' and `*/' as in plain C, 300 for `//'
;; as in C++, 400 for `#' as in most scripting languages, 500 for `;' as in
;; LISP or assembler and 600 for `%' as in TeX or PostScript.
;; Box quality differs according to language. For unknown languages (100) or
;; for the C language (200), values are 10 for simple, 20 for rounded, and 30
;; or 40 for starred. Simple quality boxes (10) use comment delimiters to
;; left and right of each comment line, and also for the top or bottom line
;; when applicable. Rounded quality boxes (20) try to suggest rounded corners
;; in boxes. Starred quality boxes (40) mostly use a left margin of asterisks
;; or X'es, and use them also in box surroundings. For all others languages,
;; box quality indicates the thickness in characters of the left and right
;; sides of the box: values are 10, 20, 30 or 40 for 1, 2, 3 or 4 characters
;; wide. With C++, quality 10 is not useful, you should force 20 instead.
;; Box type values are 1 for fully opened boxes for which boxing is done
;; only for the left and right but not for top or bottom, 2 for half
;; single lined boxes for which boxing is done on all sides except top,
;; 3 for fully single lined boxes for which boxing is done on all sides,
;; 4 for half double lined boxes which is like type 2 but more bold,
;; or 5 for fully double lined boxes which is like type 3 but more bold.
;; The special style 221 is for C comments between a single opening `/*' and a
;; single closing `*/'. The special style 111 deletes a box.
;;;; History:
;; I first observed rounded corners, as in style 223 boxes, in code from
;; Warren Tucker, a previous maintainer of the `shar' package. Besides very
;; special files, I was carefully avoiding to use such boxes for real work,
;; as I found them much too hard to maintain. My friend Paul Provost was
;; working at Taarna, a computer graphics place, which had boxes as part of
;; their coding standards. He asked that we try something to get out of his
;; misery, and this how `rebox.el' was originally written. I did not plan to
;; use it for myself, but Paul was so enthusiastic that I timidly started to
;; use boxes in my things, very little at first, but more and more as time
;; passed, yet not fully sure it was a good move. Later, many friends
;; spontaneously started to use this tool for real, some being very serious
;; workers. This finally convinced me that boxes are acceptable, after all.
(require 'newcomment)
(eval-when-compile
(require 'filladapt nil t)
(require 'cl))
;; functions passed to rebox-engine inspect these variables
(eval-when-compile
(defvar previous-nn)
(defvar previous-ne)
(defvar previous-sw)
(defvar previous-ss)
(defvar previous-se)
(defvar previous-regexp3)
(defvar previous-margin)
(defvar previous-ee)
(defvar previous-nw)
(defvar border-margin)
(defvar orig-m)
(defvar orig-col)
(defvar max-n)
(defvar marked-point)
(defvar curr-ww)
(defvar previous-regexp1)
(defvar curr-regexp1)
(defvar orig-func)
)
;; Box templates. First number is style, second is recognition weight.
(defconst rebox-templates
;; Generic programming language templates. Adding 300 replaces
;; `?' by `/', for C++ style comments. Adding 400 replaces `?' by
;; `#', for scripting languages. Adding 500 replaces `?' by ';',
;; for LISP and assembler. Adding 600 replaces `?' by `%', for
;; TeX and PostScript.
'((10 114
"?box123456")
(11 115
"? box123456")
(12 215
"? box123456 ?"
"? --------- ?")
(13 315
"? --------- ?"
"? box123456 ?"
"? --------- ?")
(14 415
"? box123456 ?"
"?????????????")
(15 515
"?????????????"
"? box123456 ?"
"?????????????")
(20 124
"??box123456")
(21 125
"?? box123456")
(22 225
"?? box123456 ??"
"?? --------- ??")
(23 325
"?? --------- ??"
"?? box123456 ??"
"?? --------- ??")
(24 425
"?? box123456 ??"
"???????????????")
(25 525
"???????????????"
"?? box123456 ??"
"???????????????")
(30 134
"???box123456")
(31 135
"??? box123456")
(32 235
"??? box123456 ???"
"??? --------- ???")
(33 335
"??? --------- ???"
"??? box123456 ???"
"??? --------- ???")
(34 435
"??? box123456 ???"
"?????????????????")
(35 535
"?????????????????"
"??? box123456 ???"
"?????????????????")
(40 144
"????box123456")
(41 145
"???? box123456")
(42 245
"???? box123456 ????"
"???? --------- ????")
(43 345
"???? --------- ????"
"???? box123456 ????"
"???? --------- ????")
(44 445
"???? box123456 ????"
"???????????????????")
(45 545
"???????????????????"
"???? box123456 ????"
"???????????????????")
(50 154
"?????box123456")
(51 155
"????? box123456")
(60 164
"??????box123456")
(61 165
"?????? box123456")
;;,----
;;| boxquote style for comments
;;`----
(16 126
"?,----"
"?| box123456"
"?`----")
(17 226
"?,----------"
"?| box123456"
"?`----------")
(26 126
"??,----"
"??| box123456"
"??`----")
(27 226
"??,----------"
"??| box123456"
"??`----------")
;;; Text mode (non programming) templates.
(111 113
"box123456")
(112 213
"| box123456 |"
"+-----------+")
(113 313
"+-----------+"
"| box123456 |"
"+-----------+")
(114 413
"| box123456 |"
"*===========*")
(115 513
"*===========*"
"| box123456 |"
"*===========*")
(116 114
"---------"
"box123456"
"---------")
(121 243
"| box123456 |")
(122 223
"| box123456 |"
"`-----------'")
(123 323
".-----------."
"| box123456 |"
"`-----------'")
(124 423
"| box123456 |"
"\\===========/")
(125 523
"/===========\\"
"| box123456 |"
"\\===========/")
;; boxquote style
(126 126
",----"
"| box123456"
"`----")
(127 226
",----------"
"| box123456"
"`----------")
(136 126
",----"
"| box123456"
"`----")
(137 226
",----------"
"| box123456"
"`----------")
(141 143
"| box123456 ")
(142 243
"* box123456 *"
"*************")
(143 343
"*************"
"* box123456 *"
"*************")
(144 443
"X box123456 X"
"XXXXXXXXXXXXX")
(145 543
"XXXXXXXXXXXXX"
"X box123456 X"
"XXXXXXXXXXXXX")
;; C language templates.
(211 118
"/* box123456 */")
(212 218
"/* box123456 */"
"/* --------- */")
(213 318
"/* --------- */"
"/* box123456 */"
"/* --------- */")
(214 418
"/* box123456 */"
"/* ========= */")
(215 518
"/* ========= */"
"/* box123456 */"
"/* ========= */")
(221 128
"/*"
" box123456"
" */")
(222 228
"/* ."
" | box123456 |"
" `----------*/")
(223 328
"/*----------."
"| box123456 |"
"`----------*/")
(224 428
"/* \\"
"| box123456 |"
"\\==========*/")
(225 528
"/*==========\\"
"| box123456 |"
"\\==========*/")
(231 138
"/*"
" | box123456"
" */")
(232 238
"/* "
" | box123456 | "
" *-----------*/")
(233 338
"/*-----------* "
" | box123456 | "
" *-----------*/")
(234 438
"/* box123456 */"
"/*-----------*/")
(235 538
"/*-----------*/"
"/* box123456 */"
"/*-----------*/")
(241 148
"/*"
" * box123456"
" */")
(242 248
"/* * "
" * box123456 * "
" *************/")
(243 348
"/************* "
" * box123456 * "
" *************/")
(244 448
"/* box123456 */"
"/*************/")
(245 548
"/*************/"
"/* box123456 */"
"/*************/")
;; doxygen style
(246 248
"/************//**"
" * box123456 "
" ****************/")
))
(defvar rebox-cache nil)
(make-variable-buffer-local 'rebox-cache)
(defvar rebox-comment-vars-normalized nil)
(make-variable-buffer-local 'rebox-comment-vars-normalized)
(put 'rebox-cache 'permanent-local t)
(defvar rebox-save-env-vars
'(comment-auto-fill-only-comments
auto-fill-function
normal-auto-fill-function)
"list of variables overwritten by `rebox-mode' to be saved.")
(defgroup rebox nil
"rebox."
:group 'convenience)
(defface rebox-style-face
'((t (:underline t)))
"chevron markers around the selected style"
:group 'rebox)
(defface rebox-style-chevron-face
'((t (:inherit font-lock-warning-face)))
"chevron markers around the selected style"
:group 'rebox)
(defface rebox-disabled-style-face
'((t (:inherit font-lock-comment-face :strike-through t)))
"disabled style face"
:group 'rebox)
(defcustom rebox-style-loop '(21 25 27)
"list of styles for cycling by `rebox-cycle'
* In the future there may be a better way to try out box styles,
but for now, you will have to look at the header of the source
file to find the styles you like.
* Two or three digit styles are permissible in this list. If
using three digit style, be sure to make a buffer-local version
of this variable. (see docs)
* It may be tempting to include the no-box style -- 111 in this
list, but if you do, cycling through styles without an
active-region will break because rebox can't figure out a
region to act on.
It's preferrable to have a boxing style that's undo-able by your
major-mode's comment handling, like 11 or 21. That way, when
you need to remove comment marks, you can cycle to such a style
and use `uncomment-region'.
"
:type '(repeat number)
:group 'rebox)
(defcustom rebox-min-fill-column nil
"The minimum fill-column to use when filling boxes.
Boxes that start at column0 will be at least this many columns wide.
nil means boxes resize according to text."
:group 'rebox)
(defcustom rebox-pad-sentence-end nil
"When this is t, and refilling, if end-of-line is also the end of a sentence, then pad it with
`sentence-end-double-space' number of spaces."
:group 'rebox)
(defcustom rebox-allowances '(top-title bottom-title)
"If `top-title' is in this list, then a title may
be present in the top border.
If `bottom-title' is present then ditto for bottom.
* Both would look like:
*={ hi }======*
| a b c asdf |
*====={ bye }=*
* For a partial-width bottom border, a bottom title is never allowed.
* The top and bottom titles are preserved while traversing the
style-loop. However if you settle on a style without borders,
they will be discarded.
"
:type '(repeat symbol)
:group 'rebox)
(defcustom rebox-keep-blank-lines t
"Non-nil gives rebox permission to truncate blank lines at
beginning of box, end, and more than three consecutive blank
lines in the body of box."
:type 'boolean
:group 'rebox)
(defcustom rebox-mode-line-string " rebox"
""
:type 'string
:group 'rebox)
(defcustom rebox-newline-indent-command 'comment-indent-new-line
"function called by `rebox-indent-new-line' no box is found."
:type 'command
:group 'rebox)
(make-variable-buffer-local 'rebox-newline-indent-command)
(defcustom rebox-backspace-command 'backward-delete-char-untabify
"function called by `rebox-backpace' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-space-command 'self-insert-command
"function called by `rebox-space' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-kill-line-command 'kill-line
"function called by `rebox-kill-line' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-kill-ring-save-command 'kill-ring-save
"function called by `rebox-kill-ring-save' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-yank-command 'yank
"function called by `rebox-yank' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-end-of-line-command 'move-end-of-line
"function called by `rebox-end-of-line' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-beginning-of-line-command 'move-beginning-of-line
"function called by `rebox-beginning-of-line' when no box is found."
:type 'command
:group 'rebox)
(defcustom rebox-yank-pop-command 'yank-pop
"function called by `rebox-yank-pop' when no box is found."
:type 'command
:group 'rebox)
;;; We can potentially just lookup the key-binding for "C-j", but that would
;;; be wrong for emacs-lisp-mode, so it's not always consistent.
(defcustom rebox-newline-indent-function-alist
'((c-mode . c-indent-new-comment-line)
(c++-mode . c-indent-new-comment-line)
(org-mode . org-return-indent))
"list of (major-mode . function) for making a newline.
The function should make and indent a new comment-line for the
mode. `comment-indent-newline' is the default.
"
:type '(alist :key-type 'symbol
:value-type 'symbol)
:group 'rebox)
(defcustom rebox-hybrid-major-modes
'(org-mode)
"Text based major modes that also have `comment-start' defined.
In these modes, auto-filling should be on for all text. And
boxing should recognize paragraphs as well as comment blocks.
"
:type 'list
:group 'rebox)
(defmacro rebox-cache ()
'(if (and (boundp 'rebox-cache)
(symbol-value 'rebox-cache))
rebox-cache
(setq rebox-cache (make-hash-table :test 'eq :size 10))))
(defsubst rebox-is-regular-comment (style)
(and (memq (% style 10) '(0 1))
(> style 300)))
;;;###autoload
(define-minor-mode rebox-mode
"Toggle rebox mode for managing text and comment boxes.
1. Auto-filling is enabled, and comment boxes are auto-filled.
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.
You don't need to enable the minor mode to use rebox2, see rebox2
header.
"
:init-value nil
:lighter rebox-mode-line-string
:version "0.6"
:keymap '(([(shift return)] . rebox-indent-new-line)
([(meta q)] . rebox-dwim)
([(meta Q)] . rebox-cycle)
([(control a)] . rebox-beginning-of-line)
([(control e)] . rebox-end-of-line)
([(control k)] . rebox-kill-line)
([(meta w)] . rebox-kill-ring-save)
([(control y)] . rebox-yank)
([(meta y)] . rebox-yank-pop)
([(meta c)] . rebox-center)
(" " . rebox-space)
("" . rebox-backspace))
:group 'rebox
(if rebox-mode
(progn
(rebox-save-env)
(set (make-local-variable 'comment-auto-fill-only-comments)
(if (and (stringp comment-start)
(not (zerop (length comment-start)))
(not (memq major-mode rebox-hybrid-major-modes)))
t
nil))
(make-local-variable 'normal-auto-fill-function)
(setq normal-auto-fill-function 'rebox-do-auto-fill)
(auto-fill-mode 1)
;; we call non-autoloaded functions from newcomment, so this is needed
(when (fboundp 'yas/minor-mode)
(add-hook 'yas/before-expand-snippet-hook 'turn-off-rebox nil t)
(add-hook 'yas/after-exit-snippet-hook 'turn-on-rebox nil t))
(when (fboundp 'iedit-mode)
(add-hook 'iedit-mode-hook 'turn-off-rebox nil t)
(add-hook 'iedit-mode-end-hook 'turn-on-rebox nil t)))
(rebox-restore-env)
(when (called-interactively-p 'any)
(remove-hook 'yas/before-expand-snippet-hook 'turn-off-rebox t)
(remove-hook 'yas/after-exit-snippet-hook 'turn-on-rebox t)
(remove-hook 'iedit-mode-hook 'turn-off-rebox t)
(remove-hook 'iedit-mode-end-hook 'turn-on-rebox t))))
(defun turn-on-rebox ()
(rebox-mode 1))
(defun turn-off-rebox ()
(rebox-mode 0))
(put 'rebox-error
'error-conditions
'(error rebox-error))
(put 'rebox-error
'error-message
"rebox error")
(put 'rebox-comment-not-found-error
'error-conditions
'(error rebox-error rebox-comment-not-found-error))
(put 'rebox-comment-not-found-error
'error-message
"Comment not found")
(put 'rebox-mid-line-comment-found
'error-conditions
'(error rebox-error rebox-mid-line-comment-found))
(put 'rebox-mid-line-comment-found
'error-message
"Comment started mid-line.")
(put 'rebox-invalid-style-error
'error-conditions
'(error rebox-error rebox-invalid-style-error))
(put 'rebox-invalid-style-error
'error-message
"Invalid style.")
;; we don't use syntax table for whitespace definition here because we don't
;; trust major-modes to define them properly.
(defconst rebox-blank-line-regexp "^[ \t]*$")
;; Template numbering dependent code.
(defvar rebox-language-character-alist
'((3 . "/") (4 . "#") (5 . ";") (6 . "%"))
"Alist relating language to comment character, for generic languages.")
;;; Regexp to match the comment start, given a LANGUAGE value as index.
(defvar rebox-regexp-start
["^[ \t]*\\(/\\*\\|//+\\|#+\\|;+\\|%+\\)"
"^" ; 1
"^[ \t]*/\\*" ; 2
"^[ \t]*//+" ; 3
"^[ \t]*#+" ; 4
"^[ \t]*\;+" ; 5
"^[ \t]*%+" ; 6
])
;; Template ingestion.
;;; Information about registered templates.
(defvar rebox-style-hash nil
"contails parsed style hash")
;;; Register all box templates.
(defun rebox-register-all-templates ()
(setq rebox-style-hash (make-hash-table :test 'eq :size 200))
(dolist (template rebox-templates)
(rebox-register-template (first template)
(second template)
(nthcdr 2 template))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; On merge-se: ;
; ;
; In the original version, if line3 was shorter than line2, then it's ;
; considered a merge of SE. I think this is because of the "*/" C style ;
; end-comment. However, the boxes would never look right if that was the ;
; case, and I don't see any box style that look good when used this way. ;
; ;
; Styles like the boxquote style need the shorter third line to be ;
; considered considered SW. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Register a single box template.
(defun rebox-register-template (style weight lines)
"Digest and register a single template.
The template is numbered STYLE, and is described by one to three LINES.
If STYLE is below 100, it is generic for a few programming languages and
within lines, `?' is meant to represent the language comment character.
STYLE should be used only once through all `rebox-register-template' calls.
One of the lines should contain the substring `box' to represent the comment
to be boxed, and if three lines are given, `box' should appear in the middle
one. Lines containing only spaces are implied as necessary before and after
the the `box' line, so we have three lines.
Normally, all three template lines should be of the same length. If the first
line is shorter, it represents a start comment string to be bundled within the
first line of the comment text. If the third line is shorter, it represents
an end comment string to be bundled at the end of the comment text, and
refilled with it."
(cond ((< style 100)
(let ((pairs rebox-language-character-alist)
language character)
(while pairs
(setq language (caar pairs)
character (cdar pairs)
pairs (cdr pairs))
(rebox-register-template
(+ (* 100 language) style)
weight
(mapcar (lambda (line)
(while (string-match "\?" line)