forked from emacs-mirror/emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexinfmt.el
4313 lines (3777 loc) · 157 KB
/
texinfmt.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
;;; texinfmt.el --- format Texinfo files into Info files
;; Copyright (C) 1985-1986, 1988, 1990-1998, 2000-2020 Free Software
;; Foundation, Inc.
;; Maintainer: [email protected]
;; Keywords: maint, tex, docs
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
;;; Emacs lisp functions to convert Texinfo files to Info files.
(defvar texinfmt-version "2.42 of 7 Jul 2006")
(defun texinfmt-version (&optional here)
"Show the version of texinfmt.el in the minibuffer.
If optional argument HERE is non-nil, insert info at point."
(interactive "P")
(let ((version-string
(format-message "Version of `texinfmt.el': %s" texinfmt-version)))
(if here
(insert version-string)
(if (called-interactively-p 'interactive)
(message "%s" version-string)
version-string))))
;;; Variable definitions
(require 'texinfo) ; So `texinfo-footnote-style' is defined.
(require 'texnfo-upd) ; So `texinfo-section-types-regexp' is defined.
(defvar texinfo-vindex)
(defvar texinfo-findex)
(defvar texinfo-cindex)
(defvar texinfo-pindex)
(defvar texinfo-tindex)
(defvar texinfo-kindex)
(defvar texinfo-last-node)
(defvar texinfo-node-names)
(defvar texinfo-enclosure-list)
(defvar texinfo-alias-list)
(defvar texinfo-fold-nodename-case nil)
(defvar texinfo-command-start)
(defvar texinfo-command-end)
(defvar texinfo-command-name)
(defvar texinfo-defun-type)
(defvar texinfo-last-node-pos)
(defvar texinfo-stack)
(defvar texinfo-short-index-cmds-alist)
(defvar texinfo-short-index-format-cmds-alist)
(defvar texinfo-format-filename)
(defvar texinfo-footnote-number)
(defvar texinfo-raisesections-alist
'((@chapter . @chapter) ; Cannot go higher
(@unnumbered . @unnumbered)
(@centerchap . @unnumbered)
(@majorheading . @majorheading)
(@chapheading . @chapheading)
(@appendix . @appendix)
(@section . @chapter)
(@unnumberedsec . @unnumbered)
(@heading . @chapheading)
(@appendixsec . @appendix)
(@subsection . @section)
(@unnumberedsubsec . @unnumberedsec)
(@subheading . @heading)
(@appendixsubsec . @appendixsec)
(@subsubsection . @subsection)
(@unnumberedsubsubsec . @unnumberedsubsec)
(@subsubheading . @subheading)
(@appendixsubsubsec . @appendixsubsec))
"An alist of next higher levels for chapters, sections, etc...
For example, section to chapter, subsection to section.
Used by `texinfo-raise-lower-sections'.
The keys specify types of section; the values correspond to the next
higher types.")
(defvar texinfo-lowersections-alist
'((@chapter . @section)
(@unnumbered . @unnumberedsec)
(@centerchap . @unnumberedsec)
(@majorheading . @heading)
(@chapheading . @heading)
(@appendix . @appendixsec)
(@section . @subsection)
(@unnumberedsec . @unnumberedsubsec)
(@heading . @subheading)
(@appendixsec . @appendixsubsec)
(@subsection . @subsubsection)
(@unnumberedsubsec . @unnumberedsubsubsec)
(@subheading . @subsubheading)
(@appendixsubsec . @appendixsubsubsec)
(@subsubsection . @subsubsection) ; Cannot go lower.
(@unnumberedsubsubsec . @unnumberedsubsubsec)
(@subsubheading . @subsubheading)
(@appendixsubsubsec . @appendixsubsubsec))
"An alist of next lower levels for chapters, sections, etc...
For example, chapter to section, section to subsection.
Used by `texinfo-raise-lower-sections'.
The keys specify types of section; the values correspond to the next
lower types.")
;;; Syntax table
(defvar texinfo-format-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\" " " st)
(modify-syntax-entry ?\\ " " st)
(modify-syntax-entry ?@ "\\" st)
(modify-syntax-entry ?\^q "\\" st)
(modify-syntax-entry ?\[ "." st)
(modify-syntax-entry ?\] "." st)
(modify-syntax-entry ?\( "." st)
(modify-syntax-entry ?\) "." st)
(modify-syntax-entry ?{ "(}" st)
(modify-syntax-entry ?} "){" st)
(modify-syntax-entry ?\' "." st)
st))
;;; Top level buffer and region formatting functions
;;;###autoload
(defun texinfo-format-buffer (&optional nosplit)
"Process the current buffer as texinfo code, into an Info file.
The Info file output is generated in a buffer visiting the Info file
name specified in the @setfilename command.
Non-nil argument (prefix, if interactive) means don't make tag table
and don't split the file if large. You can use `Info-tagify' and
`Info-split' to do these manually."
(interactive "P")
(let ((lastmessage "Formatting Info file...")
(coding-system-for-write buffer-file-coding-system))
(message lastmessage)
(widen)
(texinfo-format-buffer-1)
(Info-tagify)
(if nosplit
nil
(if (> (buffer-size) (+ 50000 Info-split-threshold))
(progn
(message (setq lastmessage "Splitting Info file..."))
(Info-split))))
(message (concat lastmessage
(if (called-interactively-p 'interactive)
"done. Now save it." "done.")))))
(defvar texinfo-region-buffer-name "*Info Region*"
"Name of the temporary buffer used by \\[texinfo-format-region].")
(defvar texinfo-pre-format-hook nil
"Hook called before the conversion of the Texinfo file to Info format.
The functions on this hook are called with argument BUFFER, the buffer
containing the Texinfo file.")
;; These come from tex-mode.el.
(defvar tex-start-of-header)
(defvar tex-end-of-header)
;;;###autoload
(defun texinfo-format-region (region-beginning region-end)
"Convert the current region of the Texinfo file to Info format.
This lets you see what that part of the file will look like in Info.
The command is bound to \\[texinfo-format-region]. The text that is
converted to Info is stored in a temporary buffer."
(interactive "r")
(message "Converting region to Info format...")
(let (texinfo-command-start
texinfo-command-end
texinfo-command-name
texinfo-vindex
texinfo-findex
texinfo-cindex
texinfo-pindex
texinfo-tindex
texinfo-kindex
texinfo-stack
(texinfo-format-filename "")
texinfo-example-start
texinfo-last-node-pos
texinfo-last-node
texinfo-node-names
(texinfo-footnote-number 0)
last-input-buffer
(fill-column-for-info fill-column)
(input-buffer (current-buffer))
(input-directory default-directory)
(header-text "")
(header-beginning 1)
(header-end 1))
;;; Copy lines between beginning and end of header lines,
;;; if any, or else copy the `@setfilename' line, if any.
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(let ((search-end (line-beginning-position 101)))
(if (or
;; Either copy header text.
(and
(prog1
(search-forward tex-start-of-header search-end t)
(forward-line 1)
;; Mark beginning of header.
(setq header-beginning (point)))
(prog1
(search-forward tex-end-of-header nil t)
(beginning-of-line)
;; Mark end of header
(setq header-end (point))))
;; Or copy @filename line.
(prog2
(goto-char (point-min))
(search-forward "@setfilename" search-end t)
(beginning-of-line)
(setq header-beginning (point))
(forward-line 1)
(setq header-end (point))))
;; Copy header
(setq header-text
(buffer-substring-no-properties
(min header-beginning region-beginning)
header-end))))))
;;; Find a buffer to use.
(switch-to-buffer (get-buffer-create texinfo-region-buffer-name))
(setq buffer-read-only t)
(let ((inhibit-read-only t))
(erase-buffer)
;; Insert the header into the buffer.
(insert header-text)
;; Insert the region into the buffer.
(insert-buffer-substring
input-buffer
(max region-beginning header-end)
region-end)
(run-hook-with-args 'texinfo-pre-format-hook input-buffer)
;; Make sure region ends in a newline.
(or (= (preceding-char) ?\n)
(insert "\n"))
(goto-char (point-min))
(texinfo-mode)
(message "Converting region to Info format...")
(setq fill-column fill-column-for-info)
;; Install a syntax table useful for scanning command operands.
(set-syntax-table texinfo-format-syntax-table)
;; Insert @include files so `texinfo-raise-lower-sections' can
;; work on them without losing track of multiple
;; @raise/@lowersections commands.
(while (re-search-forward "^@include" nil t)
(setq texinfo-command-end (point))
(let ((filename (concat input-directory
(texinfo-parse-line-arg))))
(re-search-backward "^@include")
(delete-region (point) (line-beginning-position 2))
(message "Reading included file: %s" filename)
(save-excursion
(save-restriction
(narrow-to-region
(point)
(+ (point) (car (cdr (insert-file-contents filename)))))
(goto-char (point-min))
;; Remove `@setfilename' line from included file, if any,
;; so @setfilename command not duplicated.
(if (re-search-forward "^@setfilename" (line-end-position 100) t)
(delete-region (line-beginning-position 1)
(line-beginning-position 2)))))))
;; Raise or lower level of each section, if necessary.
(goto-char (point-min))
(texinfo-raise-lower-sections)
;; Append @refill to appropriate paragraphs for filling.
(goto-char (point-min))
(texinfo-append-refill)
;; If the region includes the effective end of the data,
;; discard everything after that.
(goto-char (point-max))
(if (re-search-backward "^@bye" nil t)
(delete-region (point) (point-max)))
;; Make sure buffer ends in a newline.
(or (= (preceding-char) ?\n)
(insert "\n"))
;; Don't use a previous value of texinfo-enclosure-list.
(setq texinfo-enclosure-list nil)
(setq texinfo-alias-list nil)
(goto-char (point-min))
(if (looking-at "\\\\input[ \t]+texinfo")
(delete-region (point) (line-beginning-position 2)))
;; Insert Info region title text.
(goto-char (point-min))
(if (search-forward "@setfilename" (line-beginning-position 101) t)
(progn
(setq texinfo-command-end (point))
(beginning-of-line)
(setq texinfo-command-start (point))
(let ((arg (texinfo-parse-arg-discard)))
(insert " "
texinfo-region-buffer-name
(format-message " buffer for: `"))
(insert (file-name-nondirectory (expand-file-name arg)))
(insert (format-message "', -*-Text-*-\n"))))
;; Else no `@setfilename' line
(insert " "
texinfo-region-buffer-name
" buffer -*-Text-*-\n"))
(insert (format-message "produced by `texinfo-format-region'\n")
"from a region in: "
(if (buffer-file-name input-buffer)
(format-message "`%s'"
(file-name-sans-versions
(file-name-nondirectory
(buffer-file-name input-buffer))))
(format-message "buffer `%s'" (buffer-name input-buffer)))
(format-message "\nusing `texinfmt.el' version ")
texinfmt-version
".\n\n")
;; Now convert for real.
(goto-char (point-min))
(texinfo-format-scan)
(goto-char (point-min))
(Info-tagify input-buffer)
(goto-char (point-min))
(message "Done."))))
;;;###autoload
(defun texi2info (&optional nosplit)
"Convert the current buffer (written in Texinfo code) into an Info file.
The Info file output is generated in a buffer visiting the Info file
names specified in the @setfilename command.
This function automatically updates all node pointers and menus, and
creates a master menu. This work is done on a temporary buffer that
is automatically removed when the Info file is created. The original
Texinfo source buffer is not changed.
Non-nil argument (prefix, if interactive) means don't split the file
if large. You can use `Info-split' to do this manually."
(interactive "P")
(let ((temp-buffer (concat "*--" (buffer-name) "--temporary-buffer*" )))
(message "First updating nodes and menus, then creating Info file.")
;; (sit-for 2)
(copy-to-buffer temp-buffer (point-min) (point-max))
(switch-to-buffer temp-buffer)
(texinfo-master-menu t)
(message "Now creating Info file.")
(sit-for 2)
(texinfo-format-buffer nosplit)
(save-buffer)
(kill-buffer temp-buffer)))
;;; Primary internal formatting function for the whole buffer.
(defun texinfo-format-buffer-1 ()
(let (texinfo-format-filename
texinfo-example-start
texinfo-command-start
texinfo-command-end
texinfo-command-name
texinfo-last-node
texinfo-last-node-pos
texinfo-vindex
texinfo-findex
texinfo-cindex
texinfo-pindex
texinfo-tindex
texinfo-kindex
texinfo-stack
texinfo-node-names
(texinfo-footnote-number 0)
last-input-buffer
outfile
(fill-column-for-info fill-column)
(input-buffer (current-buffer))
(input-directory default-directory))
(setq texinfo-enclosure-list nil)
(setq texinfo-alias-list nil)
(save-excursion
(goto-char (point-min))
(or (search-forward "@setfilename" nil t)
(error "Texinfo file needs an `@setfilename FILENAME' line"))
(setq texinfo-command-end (point))
(setq outfile (texinfo-parse-line-arg)))
(find-file outfile)
(texinfo-mode)
(erase-buffer)
(buffer-disable-undo)
(message "Formatting Info file: %s" outfile)
(setq texinfo-format-filename
(file-name-nondirectory (expand-file-name outfile)))
(setq fill-column fill-column-for-info)
(set-syntax-table texinfo-format-syntax-table)
(insert-buffer-substring input-buffer)
(run-hook-with-args 'texinfo-pre-format-hook input-buffer)
(message "Converting %s to Info format..." (buffer-name input-buffer))
;; Insert @include files so `texinfo-raise-lower-sections' can
;; work on them without losing track of multiple
;; @raise/@lowersections commands.
(goto-char (point-min))
(while (re-search-forward "^@include" nil t)
(setq texinfo-command-end (point))
(let ((filename (concat input-directory
(texinfo-parse-line-arg))))
(re-search-backward "^@include")
(delete-region (point) (line-beginning-position 2))
(message "Reading included file: %s" filename)
(save-excursion
(save-restriction
(narrow-to-region
(point)
(+ (point) (car (cdr (insert-file-contents filename)))))
(goto-char (point-min))
;; Remove `@setfilename' line from included file, if any,
;; so @setfilename command not duplicated.
(if (re-search-forward "^@setfilename" (line-end-position 100) t)
(delete-region (line-beginning-position 1)
(line-beginning-position 2)))))))
;; Raise or lower level of each section, if necessary.
(goto-char (point-min))
(texinfo-raise-lower-sections)
;; Append @refill to appropriate paragraphs
(goto-char (point-min))
(texinfo-append-refill)
(goto-char (point-min))
(search-forward "@setfilename")
(beginning-of-line)
(delete-region (point-min) (point))
;; Remove @bye at end of file, if it is there.
(goto-char (point-max))
(if (search-backward "@bye" nil t)
(delete-region (point) (point-max)))
;; Make sure buffer ends in a newline.
(or (= (preceding-char) ?\n)
(insert "\n"))
;; Scan the whole buffer, converting to Info format.
(texinfo-format-scan)
(goto-char (point-min))
;; Insert info about how this file was made.
(insert "Info file: "
texinfo-format-filename ", -*-Text-*-\n"
(format-message "produced by `texinfo-format-buffer'\n")
;; Date string removed so that regression testing is easier.
;; "on "
;; (insert (format-time-string "%e %b %Y")) " "
"from file"
(if (buffer-file-name input-buffer)
(format-message " `%s'"
(file-name-sans-versions
(file-name-nondirectory
(buffer-file-name input-buffer))))
(format-message "buffer `%s'" (buffer-name input-buffer)))
(format-message "\nusing `texinfmt.el' version ")
texinfmt-version
".\n\n")
;; Return data for indices.
(list outfile
texinfo-vindex texinfo-findex texinfo-cindex
texinfo-pindex texinfo-tindex texinfo-kindex)))
;;; Perform non-@-command file conversions: quotes and hyphens
(defun texinfo-format-convert (min max)
;; Convert left and right quotes to typewriter font quotes.
(goto-char min)
(while (search-forward "``" max t)
(replace-match "\""))
(goto-char min)
(while (search-forward "''" max t)
(replace-match "\""))
;; Convert three hyphens in a row to two.
(goto-char min)
(while (re-search-forward "\\( \\|\\w\\)\\(---\\)\\( \\|\\w\\)" max t)
(delete-region (1+ (match-beginning 2)) (+ 2 (match-beginning 2)))))
;;; Handle paragraph filling
;; Keep as concatenated lists for ease of maintenance
(defvar texinfo-no-refill-regexp
(concat
"^@"
"\\("
;; add "itemize\\|" (from experiment of 2001 Nov 28)
;; because of a problem with @end itemize@refill
;; I don't know if this causes other problems.
;; I suspect itemized lists don't get filled properly and a
;; more precise fix is required. Bob
;; commented out on 2005 Feb 28 by Bob
;; "itemize\\|"
"direntry\\|"
"lisp\\|"
"smalllisp\\|"
"example\\|"
"smallexample\\|"
"display\\|"
"smalldisplay\\|"
"format\\|"
"smallformat\\|"
"flushleft\\|"
"flushright\\|"
"menu\\|"
"multitable\\|"
"titlepage\\|"
"iftex\\|"
"ifhtml\\|"
"tex\\|"
"html"
"\\)")
"Regexp specifying environments in which paragraphs are not filled.")
(defvar texinfo-accent-commands
(concat
"@[\"',=^`~]\\|"
"@OE{\\|"
"@oe{\\|"
"@AA{\\|"
"@aa{\\|"
"@AE{\\|"
"@ae{\\|"
"@ss{\\|"
"@questiondown{\\|"
"@exclamdown{\\|"
"@L{\\|"
"@l{\\|"
"@O{\\|"
"@o{\\|"
"@dotaccent{\\|"
"@ubaraccent{\\|"
"@d{\\|"
"@H{\\|"
"@ringaccent{\\|"
"@tieaccent{\\|"
"@u{\\|"
"@v{\\|"
"@dotless{"
))
(defvar texinfo-part-of-para-regexp
(concat
"^@"
"\\("
"b{\\|"
"bullet{\\|"
"cite{\\|"
"code{\\|"
"email{\\|"
"emph{\\|"
"equiv{\\|"
"error{\\|"
"expansion{\\|"
"file{\\|"
"i{\\|"
"inforef{\\|"
"kbd{\\|"
"key{\\|"
"lisp{\\|"
"minus{\\|"
"point{\\|"
"print{\\|"
"pxref{\\|"
"r{\\|"
"ref{\\|"
"result{\\|"
"samp{\\|"
"sc{\\|"
"t{\\|"
"TeX{\\|"
"today{\\|"
"url{\\|"
"var{\\|"
"w{\\|"
"xref{\\|"
"@-\\|" ; @- is a discretionary hyphen (not an accent) (a noop).
texinfo-accent-commands
"\\)"
)
"Regexp specifying @-commands found within paragraphs.")
(defun texinfo-append-refill ()
"Append @refill at end of each paragraph that should be filled.
Do not append @refill to paragraphs within @example and similar environments.
Do not append @refill to paragraphs containing @w{TEXT} or @*."
;; It is necessary to append @refill before other processing because
;; the other processing removes information that tells Texinfo
;; whether the text should or should not be filled.
(while (< (point) (point-max))
(let ((refill-blank-lines "^[ \t\n]*$")
(case-fold-search nil)) ; Don't confuse @TeX and @tex....
(beginning-of-line)
;; 1. Skip over blank lines;
;; skip over lines beginning with @-commands,
;; but do not skip over lines
;; that are no-refill environments such as @example or
;; that begin with within-paragraph @-commands such as @code.
(while (and (looking-at (concat "^@\\|^\\\\\\|" refill-blank-lines))
(not (looking-at
(concat
"\\("
texinfo-no-refill-regexp
"\\|"
texinfo-part-of-para-regexp
"\\)")))
(< (point) (point-max)))
(forward-line 1))
;; 2. Skip over @example and similar no-refill environments.
(if (looking-at texinfo-no-refill-regexp)
(let ((environment (match-string-no-properties 1)))
(progn (re-search-forward (concat "^@end " environment) nil t)
(forward-line 1)))
;; Else
;; 3. Do not refill a paragraph containing @w or @*, or ending
;; with @<newline> followed by a newline.
(if (or (>= (point) (point-max))
(re-search-forward
"@w{\\|@\\*\\|@\n\n"
(save-excursion (forward-paragraph)
(line-beginning-position 2))
t))
;; Go to end of paragraph and do nothing.
(forward-paragraph)
;; 4. Else go to end of paragraph and insert @refill
(forward-paragraph)
(forward-line -1)
(let ((line-beg (point)))
(end-of-line)
(delete-region
(point)
(save-excursion (skip-chars-backward " \t") (point)))
(forward-char 1)
(unless (re-search-backward "@c[ \t\n]\\|@comment[ \t\n]" line-beg t)
(forward-char -1))
(unless (re-search-backward "@refill\\|^[ \t]*@" line-beg t)
(insert "@refill")))
(forward-line 1))))))
;;; Handle `@raisesections' and `@lowersections' commands
;; These commands change the hierarchical level of chapter structuring
;; commands.
;;
;; @raisesections changes @subsection to @section,
;; @section to @chapter,
;; etc.
;;
;; @lowersections changes @chapter to @section
;; @subsection to @subsubsection,
;; etc.
;;
;; An @raisesections/@lowersections command changes only those
;; structuring commands that follow the @raisesections/@lowersections
;; command.
;;
;; Repeated @raisesections/@lowersections continue to raise or lower
;; the heading level.
;;
;; An @lowersections command cancels an @raisesections command, and
;; vice versa.
;;
;; You cannot raise or lower "beyond" chapters or subsubsections, but
;; trying to do so does not elicit an error---you just get more
;; headings that mean the same thing as you keep raising or lowering
;; (for example, after a single @raisesections, both @chapter and
;; @section produce chapter headings).
(defun texinfo-raise-lower-sections ()
"Raise or lower the hierarchical level of chapters, sections, etc.
This function acts according to `@raisesections' and `@lowersections'
commands in the Texinfo file.
For example, an `@lowersections' command is useful if you wish to
include what is written as an outer or standalone Texinfo file in
another Texinfo file as an inner, included file. The `@lowersections'
command changes chapters to sections, sections to subsections and so
on.
@raisesections changes @subsection to @section,
@section to @chapter,
@heading to @chapheading,
etc.
@lowersections changes @chapter to @section,
@subsection to @subsubsection,
@heading to @subheading,
etc.
An `@raisesections' or `@lowersections' command changes only those
structuring commands that follow the `@raisesections' or
`@lowersections' command.
An `@lowersections' command cancels an `@raisesections' command, and
vice versa.
Repeated use of the commands continue to raise or lower the hierarchical
level a step at a time.
An attempt to raise above `chapters' reproduces chapter commands; an
attempt to lower below subsubsections reproduces subsubsection
commands."
;; `texinfo-section-types-regexp' is defined in `texnfo-upd.el';
;; it is a regexp matching chapter, section, other headings
;; (but not the top node).
(let (type (level 0))
(while
(re-search-forward
(concat
"\\(\\(^@\\(raise\\|lower\\)sections\\)\\|\\("
texinfo-section-types-regexp
"\\)\\)")
nil t)
(beginning-of-line)
(save-excursion (setq type (read (current-buffer))))
(cond
;; 1. Increment level
((eq type '@raisesections)
(setq level (1+ level))
(delete-region
(point) (line-beginning-position 2)))
;; 2. Decrement level
((eq type '@lowersections)
(setq level (1- level))
(delete-region
(point) (line-beginning-position 2)))
;; Now handle structuring commands
((cond
;; 3. Raise level when positive
((> level 0)
(let ((count level)
(new-level type))
(while (> count 0)
(setq new-level
(cdr (assq new-level texinfo-raisesections-alist)))
(setq count (1- count)))
(kill-word 1)
(insert (symbol-name new-level))))
;; 4. Do nothing except move point when level is zero
((= level 0) (forward-line 1))
;; 5. Lower level when positive
((< level 0)
(let ((count level)
(new-level type))
(while (< count 0)
(setq new-level
(cdr (assq new-level texinfo-lowersections-alist)))
(setq count (1+ count)))
(kill-word 1)
(insert (symbol-name new-level))))))))))
;;; Perform those texinfo-to-info conversions that apply to the whole input
;;; uniformly.
(defun texinfo-format-scan ()
(texinfo-format-convert (point-min) (point-max))
;; Search for @copying, which has to be first since the
;; @insertcopying command then inserts the text elsewhere.
(goto-char (point-min))
(when (search-forward "@copying" nil t)
(texinfo-copying))
(while (search-forward "@insertcopying" nil t)
(delete-region (match-beginning 0) (match-end 0))
(texinfo-insertcopying))
;; Scan for other @-commands.
(goto-char (point-min))
(while (search-forward "@" nil t)
;;
;; These are the single-character accent commands: @^ @` @' @" @= @~
;; In Info, they are simply quoted and the @ deleted.
;; Other single-character commands:
;; @* forces a line break,
;; @- is a discretionary hyphenation point; does nothing in Info.
;; @<space>, @<tab>, @<newline> each produce a single space,
;; unless followed by a newline.
;;
;; Old version 2.34 expression: (looking-at "[@{}^'` *\"?!]")
(if (looking-at "[@{}^'`\"=~ \t\n*?!-]")
;; @*, causes a line break.
(cond
;; @*, a line break
((= (following-char) ?*)
;; remove command
(delete-region (1- (point)) (1+ (point)))
;; insert return if not at end of line;
;; else line is already broken.
(if (not (= (following-char) ?\n))
(insert ?\n)))
;; @-, deleted
((= (following-char) ?-)
(delete-region (1- (point)) (1+ (point))))
;; @<space>, @<tab>, @<newline>: produce a single space,
;; unless followed by a newline.
((= (following-char) ? )
(delete-region (1- (point)) (1+ (point)))
;; insert single space if not at end of line;
;; else line is already broken.
(if (not (= (following-char) ?\n))
(insert ? )))
((= (following-char) ?\t)
(delete-region (1- (point)) (1+ (point)))
;; insert single space if not at end of line;
;; else line is already broken.
(if (not (= (following-char) ?\n))
(insert ? )))
;; following char is a carriage return
((= (following-char) ?\n)
;; remove command
(delete-region (1- (point)) (1+ (point)))
;; insert single space if not at end of line;
;; else line is already broken.
(if (not (= (following-char) ?\n))
(insert ? )))
;; Otherwise: the other characters are simply quoted. Delete the @.
(t
(delete-char -1)
;; Be compatible with makeinfo: if @' and its ilk are
;; followed by a @ without a brace, barf.
(if (looking-at "[\"'^`~=]")
(progn
(if (= (char-after (1+ (point))) ?@)
(error "Use braces to give a command as an argument to @%c"
(following-char)))
(forward-char 1)
;; @' etc. can optionally accept their argument in
;; braces (makeinfo supports that).
(when (looking-at "{")
(let ((start (point)))
(forward-list 1)
(delete-char -1)
(goto-char start)
(delete-char 1))))
(forward-char 1))))
;; @ is followed by a command-word; find the end of the word.
(setq texinfo-command-start (1- (point)))
(if (= (char-syntax (following-char)) ?w)
(forward-word-strictly 1)
(forward-char 1))
(setq texinfo-command-end (point))
;; Detect the case of two @-commands in a row;
;; process just the first one.
(goto-char (1+ texinfo-command-start))
(skip-chars-forward "^@" texinfo-command-end)
(setq texinfo-command-end (point))
;; Handle let aliasing
(setq texinfo-command-name
(let (trial
(cmdname
(buffer-substring-no-properties
(1+ texinfo-command-start) texinfo-command-end)))
(while (setq trial (assoc cmdname texinfo-alias-list))
(setq cmdname (cdr trial)))
(intern cmdname)))
;; Call the handler for this command.
(let ((enclosure-type
(assoc
(symbol-name texinfo-command-name)
texinfo-enclosure-list)))
(if enclosure-type
(progn
(insert
(car (car (cdr enclosure-type)))
(texinfo-parse-arg-discard)
(car (cdr (car (cdr enclosure-type)))))
(goto-char texinfo-command-start))
(let ((cmd (get texinfo-command-name 'texinfo-format)))
(if cmd (funcall cmd) (texinfo-unsupported)))))))
(cond (texinfo-stack
(goto-char (nth 2 (car texinfo-stack)))
(error "Unterminated @%s" (car (car texinfo-stack)))))
;; Remove excess whitespace
(let ((whitespace-silent t))
(whitespace-cleanup)))
(defvar texinfo-copying-text ""
"Text of the copyright notice and copying permissions.")
(defun texinfo-copying ()
"Copy the copyright notice and copying permissions from the Texinfo file,
as indicated by the @copying ... @end copying command;
insert the text with the @insertcopying command."
(let ((beg (progn (beginning-of-line) (point)))
(end (progn (re-search-forward "^@end copying[ \t]*\n") (point))))
(setq texinfo-copying-text
(buffer-substring-no-properties
(save-excursion (goto-char beg) (line-beginning-position 2))
(save-excursion (goto-char end) (line-beginning-position 0))))
(delete-region beg end)))
(defun texinfo-insertcopying ()
"Insert the copyright notice and copying permissions from the Texinfo file,
which are indicated by the @copying ... @end copying command."
(insert (concat "\n" texinfo-copying-text)))
(put 'begin 'texinfo-format 'texinfo-format-begin)
(defun texinfo-format-begin ()
(texinfo-format-begin-end 'texinfo-format))
(put 'end 'texinfo-format 'texinfo-format-end)
(defun texinfo-format-end ()
(texinfo-format-begin-end 'texinfo-end))
(defun texinfo-format-begin-end (prop)
(setq texinfo-command-name (intern (texinfo-parse-line-arg)))
(let ((cmd (get texinfo-command-name prop)))
(if cmd (funcall cmd)
(texinfo-unsupported))))
;;; Parsing functions
(defun texinfo-parse-line-arg ()
"Return argument of @-command as string.
Argument is separated from command either by a space or by a brace.
If a space, return rest of line, with beginning and ending white
space removed. If a brace, return string between braces.
Leave point after argument."
(goto-char texinfo-command-end)
(let ((start (point)))
(cond ((looking-at " ")
(skip-chars-forward " ")
(setq start (point))
(end-of-line)
(skip-chars-backward " ")
(delete-region (point) (progn (end-of-line) (point)))
(setq texinfo-command-end (1+ (point))))
((looking-at "{")
(setq start (1+ (point)))
(forward-list 1)
(setq texinfo-command-end (point))
(forward-char -1))
(t
(error "Invalid texinfo command arg format")))
(prog1 (buffer-substring-no-properties start (point))
(if (eolp) (forward-char 1)))))
(defun texinfo-parse-expanded-arg ()
(goto-char texinfo-command-end)
(let ((start (point))
marker)
(cond ((looking-at " ")
(skip-chars-forward " ")
(setq start (point))
(end-of-line)
(setq texinfo-command-end (1+ (point))))
((looking-at "{")
(setq start (1+ (point)))
(forward-list 1)