-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrebox.el
852 lines (725 loc) · 28.9 KB
/
rebox.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
;;; Handling of comment boxes.
;;; Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
;;; François Pinard <pinard@iro.umontreal.ca>, April 1991.
;;; I first observed rounded corners, as in style 223 boxes, in code from
;;; Warren Tucker <wht@n4hgf.mt-park.ga.us>, a previous shar maintainer.
;;; Refilling paragraphs inside comments, stretching or shrinking the
;;; surrounding box as needed, is a pain to do "by hand". This GNU Emacs
;;; LISP code eases my life on this and I find it fair, giving all sources
;;; for a package, to also give the means for nicely modifying comments.
;;; The function rebox-comment discovers the extent of the boxed comments
;;; near the cursor, possibly refills the text, then adjusts the comment
;;; box style. The function rebox-region does the same, except that it
;;; takes the current region as a boxed comment. Numeric prefixes are
;;; used to add or remove a box, change its style (language, quality or
;;; type), or to prevent refilling of its text. A minus sign alone as
;;; prefix asks for interactive style selection.
;;; For most Emacs language editing modes, refilling does not make sense
;;; outside comments, so you may redefine the M-q command and link it to
;;; this file. For example, I use this in my .emacs file:
;;; (setq c-mode-hook
;;; '(lambda ()
;;; (define-key c-mode-map "\M-q" 'rebox-comment)))
;;; (autoload 'rebox-comment "rebox" nil t)
;;; (autoload 'rebox-region "rebox" nil t)
;;; The cursor should be within a comment before any of these commands,
;;; or else it should be between two comments, in which case the command
;;; applies to the next comment. When the command is given without prefix,
;;; the current comment box style is recognized from the comment itself
;;; as far as possible, and preserved. A prefix may be used to force
;;; a particular box style. A style is made up of three attributes: a
;;; language (the hundreds digit), a quality (the tens digit) and a type
;;; (the units digit). A zero or negative flag value changes the default
;;; box style to its absolute value. Zero digits in default style,
;;; when not overriden in flag, asks for recognition of corresponding
;;; attributes from the current box. `C-u' avoids refilling the text,
;;; using the default box style. `C-u -' defines the style interactively.
;;; Box 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 or 30 for
;;; rounded, and 40 for starred. For all others, 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. C++
;;; quality 10 is always promoted to 20. Roughly said, 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 or 30) 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. Experiment a little to
;;; see what happens.
;;; 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 or 231 is worth a note, because it is fairly
;;; common: the whole C comment stays between a single opening `/*'
;;; and a single closing `*/'. The special style 111 deletes a box.
;;; The initial default style is 023 so, unless overriden, comments are
;;; put in single lined boxes, C comments are of rounded quality.
(defvar rebox-default-style 0 "*Preferred style for box comments.")
;;; Help strings for prompting or error messages.
(defconst REBOX_HELP_FOR_LANGUAGE
"Box language is 100-none, 200-/*, 300-//, 400-#, 500-;, 600-%%")
(defconst REBOX_LANGUAGE_NONE 100)
(defconst REBOX_LANGUAGE_C 200)
(defconst REBOX_LANGUAGE_C++ 300)
(defconst REBOX_LANGUAGE_AWK 400)
(defconst REBOX_LANGUAGE_LISP 500)
(defconst REBOX_LANGUAGE_TEX 600)
(defun rebox-help-string-for-language (language)
(cond ((= language 0) "default language")
((= language REBOX_LANGUAGE_NONE) "no language")
((= language REBOX_LANGUAGE_C) "plain C")
((= language REBOX_LANGUAGE_C++) "C++")
((= language REBOX_LANGUAGE_AWK) "sh/Perl/make")
((= language REBOX_LANGUAGE_LISP) "LISP/assembler")
((= language REBOX_LANGUAGE_TEX) "TeX/PostScript")
(t "<Unknown Language>")))
(defconst REBOX_HELP_FOR_QUALITY
"Box quality/width is 10-simple, 20-rounded, 30-rounded or 40-starred")
(defconst REBOX_QUALITY_SIMPLE_ONE 10)
(defconst REBOX_QUALITY_ROUNDED_TWO 20)
(defconst REBOX_QUALITY_ROUNDED_THREE 30)
(defconst REBOX_QUALITY_STARRED_FOUR 40)
(defun rebox-help-string-for-quality (quality)
(cond ((= quality 0) "default quality")
((= quality REBOX_QUALITY_SIMPLE_ONE) "square or 1-wide")
((= quality REBOX_QUALITY_ROUNDED_TWO) "rounded or 2-wide")
((= quality REBOX_QUALITY_ROUNDED_THREE) "rounded or 3-wide")
((= quality REBOX_QUALITY_STARRED_FOUR) "starred or 4-wide")
(t "<Unknown Quality>")))
(defconst REBOX_HELP_FOR_TYPE
"Box type is 1-open, 2-half-single, 3-single, 4-half-double or 5-double")
(defconst REBOX_TYPE_OPEN 1)
(defconst REBOX_TYPE_HALF_SINGLE 2)
(defconst REBOX_TYPE_SINGLE 3)
(defconst REBOX_TYPE_HALF_DOUBLE 4)
(defconst REBOX_TYPE_DOUBLE 5)
(defun rebox-help-string-for-type (type)
(cond ((= type 0) "default type")
((= type REBOX_TYPE_OPEN) "opened box")
((= type REBOX_TYPE_HALF_SINGLE) "half normal")
((= type REBOX_TYPE_SINGLE) "full normal")
((= type REBOX_TYPE_HALF_DOUBLE) "half bold")
((= type REBOX_TYPE_DOUBLE) "full bold")
(t "<Unknown Type>")))
(defconst REBOX_MAX_LANGUAGE 6)
(defconst REBOX_MAX_QUALITY 4)
(defconst REBOX_MAX_TYPE 5)
;;; Request the style interactively, using the minibuffer.
(defun rebox-ask-for-style ()
(let (key language quality type)
(while (not language)
(message REBOX_HELP_FOR_LANGUAGE)
(setq key (read-char))
(if (and (>= key ?0) (<= key (+ ?0 REBOX_MAX_LANGUAGE)))
(setq language (- key ?0))))
(while (not quality)
(message REBOX_HELP_FOR_QUALITY)
(setq key (read-char))
(if (and (>= key ?0) (<= key (+ ?0 REBOX_MAX_QUALITY)))
(setq quality (- key ?0))))
(while (not type)
(message REBOX_HELP_FOR_TYPE)
(setq key (read-char))
(if (and (>= key ?0) (<= key (+ ?0 REBOX_MAX_TYPE)))
(setq type (- key ?0))))
(+ (* 100 language) (* 10 quality) type)))
;;; Write some TEXT followed by an edited STYLE value into the minibuffer.
(defun rebox-show-style (text style)
(message
(concat text (format " (%03d)" style)
": " (rebox-help-string-for-language (* (/ style 100) 100))
", " (rebox-help-string-for-quality (* (% (/ style 10) 10) 10))
", " (rebox-help-string-for-type (% style 10)))))
;;; Validate FLAG and usually return t if not interrupted by errors.
;;; But if FLAG is zero or negative, then change default box style and
;;; return nil.
(defun rebox-validate-flag (flag)
;; Validate flag.
(if (numberp flag)
(let ((value (if (< flag 0) (- flag) flag)))
(if (> (/ value 100) REBOX_MAX_LANGUAGE)
(error REBOX_HELP_FOR_LANGUAGE))
(if (> (% (/ value 10) 10) REBOX_MAX_QUALITY)
(error REBOX_HELP_FOR_QUALITY))
(if (> (% value 10) REBOX_MAX_TYPE)
(error REBOX_HELP_FOR_TYPE))))
;; Change default box style if requested.
(if (and (numberp flag) (<= flag 0))
(progn
(setq flag (- flag))
(if (not (zerop (/ flag 100)))
(setq rebox-default-style
(+ (* (/ flag 100) 100)
(% rebox-default-style 100))))
(if (not (zerop (% (/ flag 10) 10)))
(setq rebox-default-style
(+ (* (/ rebox-default-style 100) 100)
(* (% (/ flag 10) 10) 10)
(% rebox-default-style 10))))
(if (not (zerop (% flag 10)))
(setq rebox-default-style
(+ (* (/ rebox-default-style 10) 10)
(% flag 10))))
(rebox-show-style "Default style" rebox-default-style)
nil)
t))
;;; Return the minimum value of the left margin of all lines, or -1 if
;;; all lines are empty.
(defun rebox-left-margin ()
(let ((margin -1))
(goto-char (point-min))
(while (not (eobp))
(skip-chars-forward " \t")
(if (not (looking-at "\n"))
(setq margin
(if (< margin 0)
(current-column)
(min margin (current-column)))))
(forward-line 1))
margin))
;;; Return the maximum value of the right margin of all lines. Any
;;; sentence ending a line has a space guaranteed before the margin.
(defun rebox-right-margin ()
(let ((margin 0) period)
(goto-char (point-min))
(while (not (eobp))
(end-of-line)
(if (bobp)
(setq period 0)
(backward-char 1)
(setq period (if (looking-at "[.?!]") 1 0))
(forward-char 1))
(setq margin (max margin (+ (current-column) period)))
(forward-char 1))
margin))
;;; Return a regexp to match the start or end of a comment for some
;;; LANGUAGE, leaving the comment marks themselves available in \1.
;; FIXME: Recognize style 1** boxes.
(defun rebox-regexp-start (language)
(cond ((= language 0) "^[ \t]*\\(/\\*\\|//+\\|#+\\|;+\\|%+\\)")
((= language REBOX_LANGUAGE_NONE) "^\\(\\)")
((= language REBOX_LANGUAGE_C) "^[ \t]*\\(/\\*\\)")
((= language REBOX_LANGUAGE_C++) "^[ \t]*\\(//+\\)")
((= language REBOX_LANGUAGE_AWK) "^[ \t]*\\(#+\\)")
((= language REBOX_LANGUAGE_LISP) "^[ \t]*\\(;+\\)")
((= language REBOX_LANGUAGE_TEX) "^[ \t]*\\(%+\\)")))
(defun rebox-regexp-end (language)
(cond ((= language 0) "\\(\\*/\\|//+\\|#+\\|;+\\|%+\\)[ \t]*$")
((= language REBOX_LANGUAGE_NONE) "\\(\\)$")
((= language REBOX_LANGUAGE_C) "\\(\\*/\\)[ \t]*$")
((= language REBOX_LANGUAGE_C++) "\\(//+\\)[ \t]*$")
((= language REBOX_LANGUAGE_AWK) "\\(#+\\)[ \t]*$")
((= language REBOX_LANGUAGE_LISP) "\\(;+\\)[ \t]*$")
((= language REBOX_LANGUAGE_TEX) "\\(%+\\)[ \t]*$")))
;;; By looking at the text starting at the cursor position, guess the
;;; language in use, and return it.
(defun rebox-guess-language ()
(let ((language REBOX_LANGUAGE_NONE)
(value (* 100 REBOX_MAX_LANGUAGE)))
(while (not (zerop value))
(if (looking-at (rebox-regexp-start value))
(progn
(setq language value)
(setq value 0))
(setq value (- value 100))))
language))
;;; Find the limits of the block of comments following or enclosing
;;; the cursor, or return an error if the cursor is not within such a
;;; block of comments. Extend it as far as possible in both
;;; directions, then narrow the buffer around it.
(defun rebox-find-and-narrow ()
(save-excursion
(let (start end temp language)
;; Find the start of the current or immediately following comment.
(beginning-of-line)
(skip-chars-forward " \t\n")
(beginning-of-line)
(if (not (looking-at (rebox-regexp-start 0)))
(progn
(setq temp (point))
(if (re-search-forward "\\*/" nil t)
(progn
(re-search-backward "/\\*")
(if (> (point) temp)
(error "outside any comment block"))
(setq temp (point))
(beginning-of-line)
(skip-chars-forward " \t")
(if (not (= (point) temp))
(error "text before start of comment"))
(beginning-of-line))
(error "outside any comment block"))))
(setq start (point))
(setq language (rebox-guess-language))
;; - find the end of this comment
(if (= language REBOX_LANGUAGE_C)
(progn
(search-forward "*/")
(if (not (looking-at "[ \t]*$"))
(error "text after end of comment"))))
(end-of-line)
(if (eobp)
(insert "\n")
(forward-char 1))
(setq end (point))
;; - try to extend the comment block backwards
(goto-char start)
(while (and (not (bobp))
(if (= language REBOX_LANGUAGE_C)
(progn
(skip-chars-backward " \t\n")
(if (and (looking-at "[ \t]*\n[ \t]*/\\*")
(> (point) 2))
(progn
(backward-char 2)
(if (looking-at "\\*/")
(progn
(re-search-backward "/\\*")
(setq temp (point))
(beginning-of-line)
(skip-chars-forward " \t")
(if (= (point) temp)
(progn (beginning-of-line) t)))))))
(previous-line 1)
(looking-at (rebox-regexp-start language))))
(setq start (point)))
;; - try to extend the comment block forward
(goto-char end)
(while (looking-at (rebox-regexp-start language))
(if (= language REBOX_LANGUAGE_C)
(progn
(re-search-forward "[ \t]*/\\*")
(re-search-forward "\\*/")
(if (looking-at "[ \t]*$")
(progn
(beginning-of-line)
(forward-line 1)
(setq end (point)))))
(forward-line 1)
(setq end (point))))
;; - narrow to the whole block of comments
(narrow-to-region start end))))
;;; After refilling it if REFILL is not nil, while respecting a left
;;; MARGIN, put the narrowed buffer back into a boxed LANGUAGE comment
;;; box of a given QUALITY and TYPE.
(defun rebox-reconstruct (refill margin language quality type)
(rebox-show-style "Style" (+ language quality type))
(let (right-margin nw nn ne ww ee sw ss se x xx)
;; - decide the elements of the box being produced
(cond ((= language REBOX_LANGUAGE_NONE)
;; - planify a comment for no language in particular
(cond ((= quality REBOX_QUALITY_SIMPLE_ONE)
;; - planify a simple box
(cond ((= type REBOX_TYPE_OPEN)
(setq nw "") (setq sw "")
(setq ww "") (setq ee ""))
((= type REBOX_TYPE_HALF_SINGLE)
(setq nw "")
(setq ww "| ") (setq ee " |")
(setq sw "+-") (setq ss ?-) (setq se "-+"))
((= type REBOX_TYPE_SINGLE)
(setq nw "+-") (setq nn ?-) (setq ne "-+")
(setq ww "| ") (setq ee " |")
(setq sw "+-") (setq ss ?-) (setq se "-+"))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq nw "")
(setq ww "| ") (setq ee " |")
(setq sw "*=") (setq ss ?=) (setq se "=*"))
((= type REBOX_TYPE_DOUBLE)
(setq nw "*=") (setq nn ?=) (setq ne "=*")
(setq ww "| ") (setq ee " |")
(setq sw "*=") (setq ss ?=) (setq se "=*"))))
((or (= quality REBOX_QUALITY_ROUNDED_TWO)
(= quality REBOX_QUALITY_ROUNDED_THREE))
;; - planify a rounded box
(cond ((= type REBOX_TYPE_OPEN)
(setq nw "") (setq sw "")
(setq ww "| ") (setq ee " |"))
((= type REBOX_TYPE_HALF_SINGLE)
(setq nw "")
(setq ww "| ") (setq ee " |")
(setq sw "`-") (setq ss ?-) (setq se "-'"))
((= type REBOX_TYPE_SINGLE)
(setq nw ".-") (setq nn ?-) (setq ne "-.")
(setq ww "| ") (setq ee " |")
(setq sw "`-") (setq ss ?-) (setq se "-'"))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq nw "")
(setq ww "| " ) (setq ee " |" )
(setq sw "\\=") (setq ss ?=) (setq se "=/" ))
((= type REBOX_TYPE_DOUBLE)
(setq nw "/=" ) (setq nn ?=) (setq ne "=\\")
(setq ww "| " ) (setq ee " |" )
(setq sw "\\=") (setq ss ?=) (setq se "=/" ))))
((= quality REBOX_QUALITY_STARRED_FOUR)
;; - planify a starred box
(cond ((= type REBOX_TYPE_OPEN)
(setq nw "") (setq sw "")
(setq ww "| ") (setq ee ""))
((= type REBOX_TYPE_HALF_SINGLE)
(setq nw "")
(setq ww "* ") (setq ee " *")
(setq sw "**") (setq ss ?*) (setq se "**"))
((= type REBOX_TYPE_SINGLE)
(setq nw "**") (setq nn ?*) (setq ne "**")
(setq ww "* ") (setq ee " *")
(setq sw "**") (setq ss ?*) (setq se "**"))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq nw "")
(setq ww "X ") (setq ee " X")
(setq sw "XX") (setq ss ?X) (setq se "XX"))
((= type REBOX_TYPE_DOUBLE)
(setq nw "XX") (setq nn ?X) (setq ne "XX")
(setq ww "X ") (setq ee " X")
(setq sw "XX") (setq ss ?X) (setq se "XX"))))))
((= language REBOX_LANGUAGE_C)
;; - planify a comment for C
(cond ((= quality REBOX_QUALITY_SIMPLE_ONE)
;; - planify a simple C comment
(cond ((= type REBOX_TYPE_OPEN)
(setq nw "") (setq sw "")
(setq ww "/* ") (setq ee " */"))
((= type REBOX_TYPE_HALF_SINGLE)
(setq nw "")
(setq ww "/* ") (setq ee " */")
(setq sw "/* ") (setq ss ?-) (setq se " */"))
((= type REBOX_TYPE_SINGLE)
(setq nw "/* ") (setq nn ?-) (setq ne " */")
(setq ww "/* ") (setq ee " */")
(setq sw "/* ") (setq ss ?-) (setq se " */"))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq nw "")
(setq ww "/* ") (setq ee " */")
(setq sw "/* ") (setq ss ?=) (setq se " */"))
((= type REBOX_TYPE_DOUBLE)
(setq nw "/* ") (setq nn ?=) (setq ne " */")
(setq ww "/* ") (setq ee " */")
(setq sw "/* ") (setq ss ?=) (setq se " */"))))
((or (= quality REBOX_QUALITY_ROUNDED_TWO)
(= quality REBOX_QUALITY_ROUNDED_THREE))
;; - planify a rounded C comment
(cond ((= type REBOX_TYPE_OPEN)
;; ``open rounded'' is a special case
(setq nw "") (setq sw "")
(setq ww " ") (setq ee ""))
((= type REBOX_TYPE_HALF_SINGLE)
(setq nw "/*") (setq nn ? ) (setq ne " .")
(setq ww "| ") (setq ee " |")
(setq sw "`-") (setq ss ?-) (setq se "*/"))
((= type REBOX_TYPE_SINGLE)
(setq nw "/*") (setq nn ?-) (setq ne "-.")
(setq ww "| ") (setq ee " |")
(setq sw "`-") (setq ss ?-) (setq se "*/"))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq nw "/*" ) (setq nn ? ) (setq ne " \\")
(setq ww "| " ) (setq ee " |" )
(setq sw "\\=") (setq ss ?=) (setq se "*/" ))
((= type REBOX_TYPE_DOUBLE)
(setq nw "/*" ) (setq nn ?=) (setq ne "=\\")
(setq ww "| " ) (setq ee " |" )
(setq sw "\\=") (setq ss ?=) (setq se "*/" ))))
((= quality REBOX_QUALITY_STARRED_FOUR)
;; - planify a starred C comment
(cond ((= type REBOX_TYPE_OPEN)
(setq nw "/* ") (setq nn ? ) (setq ne "")
(setq ww " * ") (setq ee "")
(setq sw " */") (setq ss ? ) (setq se ""))
((= type REBOX_TYPE_HALF_SINGLE)
(setq nw "/* ") (setq nn ? ) (setq ne " *")
(setq ww " * ") (setq ee " *")
(setq sw " **") (setq ss ?*) (setq se "**/"))
((= type REBOX_TYPE_SINGLE)
(setq nw "/**") (setq nn ?*) (setq ne "**")
(setq ww " * ") (setq ee " *")
(setq sw " **") (setq ss ?*) (setq se "**/"))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq nw "/* " ) (setq nn ? ) (setq ne " *\\")
(setq ww "|* " ) (setq ee " *|" )
(setq sw "\\**") (setq ss ?*) (setq se "**/" ))
((= type REBOX_TYPE_DOUBLE)
(setq nw "/**" ) (setq nn ?*) (setq ne "**\\")
(setq ww "|* " ) (setq ee " *|" )
(setq sw "\\**") (setq ss ?*) (setq se "**/" ))))))
(t
;; - planify a comment for all other things
(if (and (= language REBOX_LANGUAGE_C++)
(= quality REBOX_QUALITY_SIMPLE_ONE))
(setq quality REBOX_QUALITY_ROUNDED_TWO))
(setq x (cond ((= language REBOX_LANGUAGE_C++) ?/)
((= language REBOX_LANGUAGE_AWK) ?#)
((= language REBOX_LANGUAGE_LISP) ?\;)
((= language REBOX_LANGUAGE_TEX) ?%)))
(setq xx (make-string (/ quality 10) x))
(setq ww (concat xx " "))
(cond ((= type REBOX_TYPE_OPEN)
(setq nw "") (setq sw "") (setq ee ""))
((= type REBOX_TYPE_HALF_SINGLE)
(setq ee (concat " " xx))
(setq nw "")
(setq sw ww) (setq ss ?-) (setq se ee))
((= type REBOX_TYPE_SINGLE)
(setq ee (concat " " xx))
(setq nw ww) (setq nn ?-) (setq ne ee)
(setq sw ww) (setq ss ?-) (setq se ee))
((= type REBOX_TYPE_HALF_DOUBLE)
(setq ee (concat " " xx))
(setq xx (make-string (1+ (/ quality 10)) x))
(setq nw "")
(setq sw xx) (setq ss x) (setq se xx))
((= type REBOX_TYPE_DOUBLE)
(setq ee (concat " " xx))
(setq xx (make-string (1+ (/ quality 10)) x))
(setq nw xx) (setq nn x) (setq ne xx)
(setq sw xx) (setq ss x) (setq se xx)))))
;; - possibly refill, and adjust margins to account for left inserts
(if (not (and flag (listp flag)))
(let ((fill-prefix (make-string margin ? ))
(fill-column (- fill-column (+ (length ww) (length ee)))))
(fill-region (point-min) (point-max))))
(setq right-margin (+ (rebox-right-margin) (length ww)))
;; - construct the box comment, from top to bottom
(goto-char (point-min))
(if (and (= language REBOX_LANGUAGE_C)
(or (= quality REBOX_QUALITY_ROUNDED_TWO)
(= quality REBOX_QUALITY_ROUNDED_THREE))
(= type REBOX_TYPE_OPEN))
(progn
;; - construct an 33 style comment
(skip-chars-forward " " (+ (point) margin))
(insert (make-string (- margin (current-column)) ? )
"/* ")
(end-of-line)
(forward-char 1)
(while (not (eobp))
(skip-chars-forward " " (+ (point) margin))
(insert (make-string (- margin (current-column)) ? )
ww)
(beginning-of-line)
(forward-line 1))
(backward-char 1)
(insert " */"))
;; - construct all other comment styles
;; construct one top line
(if (not (zerop (length nw)))
(progn
(indent-to margin)
(insert nw)
(if (or (not (eq nn ? )) (not (zerop (length ne))))
(insert (make-string (- right-margin (current-column)) nn)
ne))
(insert "\n")))
;; construct one middle line
(while (not (eobp))
(skip-chars-forward " " (+ (point) margin))
(insert (make-string (- margin (current-column)) ? )
ww)
(end-of-line)
(if (not (zerop (length ee)))
(progn
(indent-to right-margin)
(insert ee)))
(beginning-of-line)
(forward-line 1))
;; construct one bottom line
(if (not (zerop (length sw)))
(progn
(indent-to margin)
(insert sw)
(if (or (not (eq ss ? )) (not (zerop (length se))))
(insert (make-string (- right-margin (current-column)) ss)
se "\n")))))))
;;; Add, delete or adjust a comment box in the narrowed buffer.
;;; Various FLAG values are explained at beginning of this file.
(defun rebox-engine (flag)
(let ((undo-list buffer-undo-list)
(marked-point (point-marker))
(language (progn (goto-char (point-min)) (rebox-guess-language)))
(quality 0)
(type 0))
(untabify (point-min) (point-max))
;; Remove all the comment marks, and move all the text rigidly to the
;; left for insuring that the left margin stays at the same place.
;; At the same time, try recognizing the box style, saving its quality
;; in QUALITY and its type in TYPE. (LANGUAGE is already guessed.)
(let ((indent-tabs-mode nil)
(previous-margin (rebox-left-margin))
actual-margin)
;; FIXME: Cleanup style 1** boxes.
;; FIXME: Recognize really all cases of type and quality.
;; - remove all comment marks
(if (= language REBOX_LANGUAGE_NONE)
nil
(goto-char (point-min))
(while (re-search-forward (rebox-regexp-start language) nil t)
(goto-char (match-beginning 1))
(delete-region (point) (match-end 1))
(insert (make-string (- (match-end 1) (point)) ? )))
(goto-char (point-min))
(while (re-search-forward (rebox-regexp-end language) nil t)
(replace-match "" t t)))
(if (= language REBOX_LANGUAGE_C)
(progn
(goto-char (point-min))
(while (re-search-forward "\\*/ */\\*" nil t)
(replace-match " " t t))
(goto-char (point-min))
(while (re-search-forward "^\\( *\\)|\\*\\(.*\\)\\*| *$" nil t)
(setq quality REBOX_QUALITY_STARRED_FOUR)
(setq type REBOX_TYPE_DOUBLE)
(replace-match "\\1 \\2" t))
(goto-char (point-min))
(while (re-search-forward "^\\( *\\)\\*\\(.*\\)\\* *$" nil t)
(setq quality REBOX_QUALITY_STARRED_FOUR)
(setq type REBOX_TYPE_SINGLE)
(replace-match "\\1 \\2" t))
(goto-char (point-min))
(while (re-search-forward "^\\( *\\)|\\(.*\\)| *$" nil t)
(setq quality REBOX_QUALITY_ROUNDED_TWO)
(replace-match "\\1 \\2" t))
(goto-char (point-min))
(if (zerop quality)
(while (re-search-forward "^\\( +\\)\\* " nil t)
(setq quality REBOX_QUALITY_STARRED_FOUR)
(setq type REBOX_TYPE_OPEN)
(replace-match "\\1 " t)))))
;; - remove the first dashed or starred line
(goto-char (point-min))
(if (looking-at "^ *\\(--+\\|\\*\\*+\\)[.\+\\]? *\n")
(progn
(setq type REBOX_TYPE_SINGLE)
(replace-match "" t t))
(if (looking-at "^ *\\(==\\|XX+\\|##+\\|;;+\\)[.\+\\]? *\n")
(progn
(setq type REBOX_TYPE_DOUBLE)
(replace-match "" t t))))
;; - remove the last dashed or starred line
(goto-char (point-max))
(previous-line 1)
(if (looking-at "^ *[`\+\\]?*--+ *\n")
(progn
(if (= type REBOX_TYPE_OPEN)
(setq type REBOX_TYPE_HALF_SINGLE))
(replace-match "" t t))
(if (looking-at "^ *[`\+\\]?*\\(==+\\|##+\\|;;+\\) *\n")
(progn
(if (= type REBOX_TYPE_OPEN)
(setq type REBOX_TYPE_HALF_DOUBLE))
(replace-match "" t t))
(if (looking-at "^ *\\*\\*+[.\+\\]? *\n")
(progn
(setq quality REBOX_QUALITY_STARRED_FOUR)
(setq type REBOX_TYPE_HALF_SINGLE)
(replace-match "" t t))
(if (looking-at "^ *XX+[.\+\\]? *\n")
(progn
(setq quality REBOX_QUALITY_STARRED_FOUR)
(setq type REBOX_TYPE_HALF_DOUBLE)
(replace-match "" t t))))))
;; - remove all spurious whitespace
(goto-char (point-min))
(while (re-search-forward " +$" nil t)
(replace-match "" t t))
(goto-char (point-min))
(if (looking-at "\n+")
(replace-match "" t t))
(goto-char (point-max))
(skip-chars-backward "\n")
(if (looking-at "\n\n+")
(replace-match "\n" t t))
(goto-char (point-min))
(while (re-search-forward "\n\n\n+" nil t)
(replace-match "\n\n" t t))
;; - move the text left is adequate
(setq actual-margin (rebox-left-margin))
(if (not (= previous-margin actual-margin))
(indent-rigidly (point-min) (point-max)
(- previous-margin actual-margin))))
;; Override box style according to FLAG or chosen default style.
;; Else, use either recognized style elements or built-in defaults.
(cond ((and (numberp flag) (not (zerop (/ flag 100))))
(setq language (* (/ flag 100) 100)))
((not (zerop (/ rebox-default-style 100)))
(setq language (* (/ rebox-default-style 100) 100))))
(cond ((and (numberp flag) (not (zerop (% (/ flag 10) 10))))
(setq quality (* (% (/ flag 10) 10) 10)))
((not (zerop (% (/ rebox-default-style 10) 10)))
(setq quality (* (% (/ rebox-default-style 10) 10) 10)))
((zerop quality)
(setq quality REBOX_QUALITY_ROUNDED_TWO)))
(cond ((and (numberp flag) (not (zerop (% flag 10))))
(setq type (% flag 10)))
((not (zerop (% rebox-default-style 10)))
(setq type (% rebox-default-style 10)))
((zerop type)
(setq type 1)))
;; Possibly refill, then reconstruct the comment box.
(let ((indent-tabs-mode nil))
(rebox-reconstruct (not (and flag (listp flag)))
(rebox-left-margin)
language quality type))
;; Retabify to the left only (adapted from tabify.el).
(if indent-tabs-mode
(progn
(goto-char (point-min))
(while (re-search-forward "^[ \t][ \t]+" nil t)
(let ((column (current-column)))
(delete-region (match-beginning 0) (point))
(indent-to column)))))
;; Restore the point position.
(goto-char (marker-position marked-point))
;; Remove all intermediate boundaries from the undo list.
(if (not (eq buffer-undo-list undo-list))
(let ((cursor buffer-undo-list))
(while (not (eq (cdr cursor) undo-list))
(if (car (cdr cursor))
(setq cursor (cdr cursor))
(rplacd cursor (cdr (cdr cursor)))))))))
;;; Set or reset the Taarna team's own way for a C style. You do not
;;; really want to know about this.
(defvar c-mode-taarna-style nil "*Non-nil for Taarna team C-style.")
(defun taarna-mode ()
(interactive)
(if c-mode-taarna-style
(progn
(setq c-mode-taarna-style nil)
(setq c-indent-level 2)
(setq c-continued-statement-offset 2)
(setq c-brace-offset 0)
(setq c-argdecl-indent 5)
(setq c-label-offset -2)
(setq c-tab-always-indent t)
(setq rebox-default-style REBOX_QUALITY_ROUNDED_TWO)
(message "C mode: GNU style"))
(setq c-mode-taarna-style t)
(setq c-indent-level 4)
(setq c-continued-statement-offset 4)
(setq c-brace-offset -4)
(setq c-argdecl-indent 4)
(setq c-label-offset -4)
(setq c-tab-always-indent t)
(setq rebox-default-style
(+ REBOX_QUALITY_SIMPLE_ONE REBOX_TYPE_HALF_SINGLE))
(message "C mode: Taarna style")))
;;; Rebox the current region.
(defun rebox-region (flag)
(interactive "P")
(if (eq flag '-) (setq flag (rebox-ask-for-style)))
(if (rebox-validate-flag flag)
(save-restriction
(narrow-to-region (region-beginning) (region-end))
(rebox-engine flag))))
;;; Rebox the surrounding comment.
(defun rebox-comment (flag)
(interactive "P")
(if (eq flag '-) (setq flag (rebox-ask-for-style)))
(if (rebox-validate-flag flag)
(save-restriction
(rebox-find-and-narrow)
(rebox-engine flag))))