File: doc2texi.el

package info (click to toggle)
gnuplot5 5.0.3%2Bdfsg1-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 12,488 kB
  • sloc: ansic: 77,884; cpp: 6,939; makefile: 1,967; sh: 1,343; lisp: 655; perl: 302; awk: 235; pascal: 194; tcl: 88; python: 46
file content (906 lines) | stat: -rw-r--r-- 30,859 bytes parent folder | download
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
;;;; doc2texi.el -- generate a texinfo file from the gnuplot doc file

;; Copyright (C) 1999 Bruce Ravel

;; Author:     Bruce Ravel <ravel@phys.washington.edu>
;; Maintainer: Bruce Ravel <ravel@phys.washington.edu>
;; Created:    March 23 1999
;; Updated:    May 28 1999
;; Version:    0.2
;; Keywords:   gnuplot, document, info

;; This file is not part of GNU Emacs.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This lisp script 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.
;;
;; Permission is granted to distribute copies of this lisp script
;; provided the copyright notice and this permission are preserved in
;; all copies.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; send bug reports to the author (ravel@phys.washington.edu)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Commentary:
;;
;; I suppose the most immediate question to ask is "Why do this in
;; emacs lisp???"  While it is true that the gnuplot.doc file lends
;; itself to processing by a 1 pass filter, there are some aspects of
;; texinfo files that are a bit tricky and would require 2 or perhaps
;; three passes.  Specifically, getting all of the cross references
;; made correctly is a lot of work.  Fortunately, texinfo-mode has
;; functions for building menus and updating nodes.  This saves a lot
;; of sweat and is the principle reason why I decided to write this in
;; emacs lisp.
;;
;; Everything else in gnuplot is written in C for the sake of
;; portability.  Emacs lisp, of course, requires that you have emacs.
;; For many gnuplot users, that is not a good assumption.  However,
;; the likelihood of needing info files in the absence of emacs is
;; very, very slim.  I think it is safe to say that someone who needs
;; info files has emacs installed and thus will be able to use this
;; program.
;;
;; Since this is emacs, I am not treating the gnuplot.doc file as a
;; text stream.  It seems much more efficient in this context to treat
;; it as a buffer.  All of the work is done by the function
;; `d2t-doc-to-texi'.  Each of the conversion chores is handled by an
;; individual function.  Each of thse functions is very similar in
;; structure.  They start at the top of the buffer, search forward for
;; a line matching the text element being converted, perform the
;; replacement in place, and move on until the end of the buffer.
;; These text manipulations are actually quite speedy.  The slow part
;; of the process is using the texinfo-mode function to update the
;; nodes and menus.  However, using these slow functions has one
;; advantage -- the texinfo-mode functions for doing menus and nodes
;; are certain to do the job correctly.  Although rather slow, this
;; approach doesn't totally suck compared to the time cost of
;; compiling and running a C program.  And the output from this is
;; much more useful than the output from the doc2info program.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Use:
;;
;; Customize the variables at the start of the executable code.  Then
;; I intend that this be used from the command line or from a Makefile
;; like so:
;;
;;      emacs -batch -l doc2texi.el -f d2t-doc-to-texi
;;
;; or
;;
;;      emacs -batch -l doc2texi.el -f d2t-doc-to-texi-verbosely
;;
;; This will start emacs in batch mode, load this file, run the
;; converter, then quit.  This takes about 30 seconds my 133 MHz
;; Pentium.  It also sends a large number of mesages to stderr, so you
;; may want to redirect stderr to /dev/null or to a file.
;;
;; Then you can do
;;
;;      makeinfo gnuplot5.info
;;
;; You may want to use the --no-split option.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; History:
;;
;;  0.1  Mar 23 1999 <BR> Initial version
;;  0.2  May 28 1999 <BR>
;;  0.3  Jun  2 1999 <BR> Added terminal information, fixed uref problem.
;;  0.4  Feb 23 2012 <jjo> Added generation of gnuplot-eldoc strings file
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Acknowledgements:
;;
;; Lars Hecking asked me to look into the doc -> info problem.  Silly
;; me, I said "ok."  This is what I came up with.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; To do:
;;
;;  -- internal cross-references: these are not perfect due to
;;     inconsistencies in the use of `` and case inconsistencies in
;;     the text.  The latter can be fixed.  Also I need a way to not
;;     make the @ref if point is currently in that region.
;;  -- catch errors gracefully, particularly when looking for files.
;;  -- are guesses about OS specific terminal information correct?
;;  -- turn the lists in the "What's New" and "xlabel" sections into
;;     proper lists.  "^\\([0-9]\\)+\." finds the list items.  If
;;     (match-string 1) is "1" then insert "@enumerate\n@item\n", else
;;     insert "@item\n".  also use (replace-match "").  need to find
;;     the end somehow.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Code:

;;; You may need to customize these variables:
(defvar d2t-doc-file-name "gnuplot.doc"
  "Name of the gnuplot.doc file.")
(defvar d2t-terminal-directory (expand-file-name "../term/")
  "Location of .trm files in gnuplot source tree.")


;;; You should not need to touch anything below here ;;;;;;;;;;;;;;;;;;;;;

(require 'cl)
(eval-and-compile			; need split-string to do xrefs
  (if (fboundp 'split-string)
      ()
    (defun split-string (string &optional pattern)
      "Return a list of substrings of STRING which are separated by PATTERN.
If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
      (or pattern
	  (setq pattern "[ \f\t\n\r\v]+"))
      (let (parts (start 0))
	(while (string-match pattern string start)
	  (setq parts (cons (substring string start (match-beginning 0)) parts)
		start (match-end 0)))
	(nreverse (cons (substring string start) parts)))) ))
(eval-and-compile
  (and (featurep 'xemacs)
    ;; XEmacs 21.4 doesn't have replace-regexp-in-string
    (not (fboundp 'replace-regexp-in-string))
    (defun replace-regexp-in-string (regexp rep string)
       (replace-in-string string regexp rep))))

(defconst d2t-work-buffer-name "*doc2texi*"
  "Name of scratch buffer where the doc file will be converted into a
  texi file.")
(defconst d2t-scratch-buffer-name "*doc2texi-output*")
(defconst d2t-terminal-buffer-name "*doc2texi-terminal*")
(defvar d2t-verbose nil)
(defconst d2t-texi-filename "gnuplot.texi")

(defconst d2t-texi-header
  "\\input texinfo   @c -*-texinfo-*-

@c %**start of header
@setfilename gnuplot5.info
@settitle Gnuplot: An Interactive Plotting Program
@setchapternewpage odd
@c %**end of header

@c define the command and options indeces
@defindex cm
@defindex op
@defindex tm

@dircategory Math
@direntry
* GNUPLOT5: (gnuplot5).           An Interactive Plotting Program
@end direntry

@ifnottex
@node Top, gnuplot, (dir), (dir)
@top Master Menu
@end ifnottex

@example
                       GNUPLOT

            An Interactive Plotting Program
             Thomas Williams & Colin Kelley
                Version 5.0 organized by
       Ethan A Merritt with help from many others

   Copyright (C) 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
           Copyright (C) 2004 - 2012  various authors

       Mailing list for comments: gnuplot-info@@lists.sourceforge.net
     Mailing list for bug reports: gnuplot-bugs@@lists.sourceforge.net

         This manual was originally prepared by Dick Crawford
                   Version 5.0 - March 2014


Major contributors (alphabetic order):
@end example

"
  "Texinfo header.")

(defconst d2t-main-menu
  "@menu
@end menu"
  "Main menu.")

(defconst d2t-texi-footer
  "@node Concept_Index, Command_Index, Bugs, Top
@unnumbered Concept Index
@printindex cp

@node Command_Index, Option_Index, Concept_Index, Top
@unnumbered Command Index
@printindex cm

@node Options_Index, Function_Index, Command_Index, Top
@unnumbered Options Index
@printindex op

@node Function_Index, Terminal_Index, Options_Index, Top
@unnumbered Function Index
@printindex fn

@node Terminal_Index, , Options_Index, Top
@unnumbered Terminal Index
@printindex tm

@c @shortcontents
@contents
@bye
"
  "Texinfo file terminator.")

(defvar d2t-level-1-alist nil
  "Alist of level 1 tags and markers.")
(defvar d2t-level-2-alist nil
  "Alist of level 2 tags and markers.")
(defvar d2t-level-3-alist nil
  "Alist of level 3 tags and markers.")
(defvar d2t-level-4-alist nil
  "Alist of level 4 tags and markers.")
(defvar d2t-level-5-alist nil
  "Alist of level 5 tags and markers.")
(defvar d2t-commands-alist nil
  "Alist of commands and markers.")
(defvar d2t-set-show-alist nil
  "Alist of options and markers.")
(defvar d2t-functions-alist nil
  "Alist of functions and markers.")
(defvar d2t-terminals-alist nil
  "Alist of terminal types and markers.")
(defvar d2t-node-list nil
  "List of nodes.")

(defvar d2t-terminal-list ())
(setq d2t-terminal-list
      '("ai"
	"aquaterm"
	"be"
	"cairo"
	"canvas"
	"cgm"
	"context"
	"corel"
	"debug"
	"djsvga"
	"dumb"
	"dxf"
	"dxy"
	"eepic"
	"emf"
	"emxvga"
	"epslatex"
	"epson"
	"excl"
	"fg"
	"fig"
	"gd"
	"ggi"
	"gif"
	"gpic"
	"gpr"
	"grass"
	"hp26"
	"hp2648"
	"hp500c"
	"hpgl"
	"hpljii"
	"hppj"
	"imagen"
	"jpeg"
	"kyo"
	"latex"
	"linux"
	"lua"
	"mac"
	"metafont"
	"metapost"
	"mgr"
	"mif"
	"next"
	"openstep"
	"pbm"
	"pc"
	"pdf"
	"png"
	"post"
	"pslatex"
	"pstricks"
	"qms"
	"regis"
	"sun"
	"svg"
	"t410x"
	"table"
	"tek"
	"texdraw"
	"tgif"
	"tikz"
	"tkcanvas"
	"tpic"
	"vgagl"
	"vws"
	"win"
	"wxt"
	"x11"
	"xlib"))

(defvar d2t-gnuplot-license
  ";;;; This file is generated automatically from the Gnuplot
;;;; documentation by `doc2texi.el', part of the Gnuplot distribution.
;;;; It is not intended to be edited manually.  See docs/gnuplot.doc
;;;; in the gnuplot source tree (available from
;;;; gnuplot.sourcefourge.net) for the original.

;; This file is covered by the Gnuplot licensing terms:

;; Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
;;
;; Permission to use, copy, and distribute this software and its
;; documentation for any purpose with or without fee is hereby granted,
;; provided that the above copyright notice appear in all copies and
;; that both that copyright notice and this permission notice appear
;; in supporting documentation.
;;
;; Permission to modify the software is granted, but not the right to
;; distribute the complete modified source code.  Modifications are to
;; be distributed as patches to the released version.  Permission to
;; distribute binaries produced by compiling modified sources is granted,
;; provided you
;;   1. distribute the corresponding source modifications from the
;;    released version in the form of a patch file along with the binaries,
;;   2. add special version identification to distinguish your version
;;    in addition to the base release version number,
;;   3. provide your name and address as the primary contact for the
;;    support of your modified version, and
;;   4. retain our contact information in regard to use of the base
;;    software.
;; Permission to distribute the released version of the source code along
;; with corresponding source modifications in the form of a patch file is
;; granted with same provisions 2 through 4 for binary distributions.
;;
;; This software is provided \"as is\" without express or implied warranty
;; to the extent permitted by applicable law."

  "Gnuplot license for insertion into automatically generated gnuplot-eldoc.el file.")


(defun d2t-doc-to-texi-verbosely ()
  "Run `d2t-doc-to-texi' noisily"
  (interactive)
  (setq d2t-verbose t)
  (d2t-doc-to-texi))

(defun d2t-doc-to-texi ()
  "This is the doc to texi converter function.
It calls a bunch of other functions, each of which handles one
particular conversion chore."
  (interactive)
  (setq d2t-level-1-alist   nil ;; initialize variables
	d2t-level-2-alist   nil
	d2t-level-3-alist   nil
	d2t-level-4-alist   nil
	d2t-level-5-alist   nil
	d2t-commands-alist  nil
	d2t-set-show-alist  nil
	d2t-functions-alist nil
	d2t-terminals-alist nil
	d2t-node-list       nil)
  ;; open the doc file and get some data about its contents
  (d2t-prepare-workspace)
  (message "Inserting help for terminals ...")
  (d2t-get-terminals)
  (message "Analyzing doc file ...")
  (d2t-get-levels)
  (d2t-get-commands)
  (d2t-get-set-show)
  (d2t-get-functions)
  ;; convert the buffer from doc to texi, one element at a time
  (message "Converting to texinfo ...")
  (d2t-braces-atsigns)   ;; this must be the first conversion function
  (d2t-comments)         ;; delete comments
  (d2t-sectioning)       ;; chapters, sections, etc
  (d2t-get-eldoc-strings)
  (d2t-indexing)         ;; index markup
  (d2t-tables)           ;; fix up tables
  (d2t-handle-html)      ;; fix up html markup
  (d2t-first-column)     ;; left justify normal text
  (d2t-enclose-examples) ;; turn indented text into @examples
  (message "Menus, nodes, xrefs ...")
  (d2t-make-refs)
  ;(d2t-make-menus)
  ;(d2t-set-nodes)
  (save-excursion        ;; fix a few more things explicitly
    (goto-char (point-min))
    (insert d2t-texi-header)
    (search-forward "@node")
    ;; (beginning-of-line)
    ;; (insert "\n\n" d2t-main-menu "\n\n")
    (search-forward "@node Bugs")	; `texinfo-all-menus-update' seems
    (beginning-of-line)			; to miss this one.  how odd.
    (insert "@menu\n* Bugs::\t\t\t\n@end menu\n\n")
    (goto-char (point-max))
    (insert d2t-texi-footer))
  (load-library "texinfo") ;; now do the hard stuff with texinfo-mode
  (texinfo-mode)
  (let ((message-log-max 0)
	(standard-output (get-buffer-create d2t-scratch-buffer-name)))
    (message "Making texinfo nodes ...\n")
    (texinfo-every-node-update)
    (message "Making texinfo menus ...\n")
    (texinfo-all-menus-update))
  (write-file d2t-texi-filename) )  ; save it and done!

(defun d2t-prepare-workspace ()
  "Create a scratch buffer and populate it with gnuplot.doc."
  (and d2t-verbose (message "  Doing d2t-prepare-workspace ..."))
  (if (get-buffer d2t-work-buffer-name)
      (kill-buffer d2t-work-buffer-name))
  (if (get-buffer d2t-texi-filename)
      (kill-buffer d2t-texi-filename))
  (if (get-buffer d2t-terminal-buffer-name)
      (kill-buffer d2t-terminal-buffer-name))
  (get-buffer-create d2t-terminal-buffer-name)
  (set-buffer (get-buffer-create d2t-work-buffer-name))
  (insert-file-contents d2t-doc-file-name)
  (goto-char (point-min)))


(defvar d2t-additional-terminals
  '(("linux" "linux")
    ("canvas" "canvas")
    ("lua" "lua" "tikz")
    ("mac" "mac" "openstep")
    ("beos" "be")
    ("dos" "emxvga" "djsvga" "fg" "pc")
    ("windows" "win")
    ("next" "next")
    ("os2" "pm" "emxvga")
    ("sun" "sun")
    ("vms" "vws"))

  "Additional terminals to add to the converted gnuplot5.info file.

The CARs of the items are strings to match against
`system-configuration', and the CDRs are the elements to add to
the end of `d2t-get-terminals'.")

(defun d2t-get-terminals ()
  "Insert all appropriate terminal help."
  (let ((case-fold-search t))
    (dolist (rec d2t-additional-terminals)
      (let ((system (car rec))
	    (terms (cdr rec)))
	(when (string-match system system-configuration)
	  (dolist (term terms)
	    (add-to-list 'd2t-terminal-list term)))))

    (unless (member* system-configuration '("pc" "windows")
		     :test 'string-match)
      (dolist (term '("x11" "tgif" "gpic" "regis" "t410x" "tex" "xlib"))
	(add-to-list 'd2t-terminal-list term)))

  (setq d2t-terminal-list (sort d2t-terminal-list 'string<))
  (let ((list d2t-terminal-list) file node marker)
    (save-excursion
      (when (re-search-forward "^<3" (point-max) t)
	(beginning-of-line)
	(insert "@c ")
	(forward-line 1)
	(dolist (elem list)
          (and d2t-verbose (message "    %s ..." elem))
          (setq file (concat d2t-terminal-directory elem ".trm"))
          (when (file-exists-p file)
            (with-current-buffer d2t-terminal-buffer-name
              (erase-buffer)
              (insert-file-contents file)
              ;; find the terminal help
              (when (search-forward "START_HELP" (point-max) "to_end")
                (forward-line 1)
                (delete-region (point-min) (point-marker))
                (search-forward "END_HELP" (point-max) "to_end")
                (beginning-of-line)
                (delete-region (point-marker) (point-max))
                ;; tidy up the terminal help content
                (goto-char (point-min))
                (while (re-search-forward "\",[ \t]*$" nil t)
                  (replace-match "" nil nil))
                (goto-char (point-min))
                (while (re-search-forward "^\"" nil t)
                  (replace-match "" nil nil))
                (goto-char (point-min))
                (while (re-search-forward "\\\\\"" nil t)
                  (replace-match "\"" nil nil))
                (goto-char (point-min))
                (while (re-search-forward "^1[ \t]+\\(.+\\)$" nil t)
                  (setq node   (match-string 1)
                        marker (point-marker))
                  (replace-match (concat "3  " node) nil nil))
                (goto-char (point-min))
                (while (re-search-forward "^2" nil t)
                  (replace-match "4 " nil nil))
                (goto-char (point-min))
                ;; set up terminals index
                (while (re-search-forward "^\?\\([^ ]+\\)$" nil t)
                  (let ((word (match-string 1)))
                    (unless (string-match "_\\|command-line-options" word)
                      (setq d2t-terminals-alist
                            (append d2t-terminals-alist
                                    (list (cons word (point-marker))))))))
                
                ;; and cram it into the doc buffer
                (with-current-buffer d2t-work-buffer-name
                  (insert-buffer-substring d2t-terminal-buffer-name)))))))))))
	  

;;; functions for obtaining lists of nodes in the document

(defun d2t-get-levels ()
  "Find positions of all nodes in the doc."
  (and d2t-verbose (message "  Doing d2t-get-levels ..."))
  (let ((list '("1" "2" "3" "4" "5")) str)
    (while list
      (setq str (concat "d2t-level-" (car list) "-alist"))
      (and d2t-verbose (message "    %s ..." str))
      (save-excursion
	(while (not (eobp))
	  (when (re-search-forward (concat "^" (car list) " \\(.+\\)$")
				   (point-max) "to_end")
	    (beginning-of-line)
	    (set (intern str)
		 (append (eval (intern str))
			 (list (cons (match-string 1) (point-marker)))))
	    (forward-line 1))))
      (setq list (cdr list)))))


(defun d2t-get-commands ()
  "Find all commands in the doc."
  (and d2t-verbose (message "  Doing d2t-get-commands ..."))
  (save-excursion
    (let ((alist d2t-level-1-alist) start end)
      (while alist
	(if (string= (caar alist) "Commands")
	    (setq start (cdar  alist) ;; location of "1 Commands"
		  end   (cdadr alist) ;; location of next level 1 heading
		  alist nil)
	  (setq alist (cdr alist))))
      ;;(message "%S %S" start end)
      (goto-char start)
      (while (< (point) end)
	(when (re-search-forward "^2 \\(.+\\)$" (point-max) "to_end")
	  (beginning-of-line)
	  (unless (> (point) end)
	    (setq d2t-commands-alist
		  (append d2t-commands-alist
			  (list (cons (match-string 1) (point-marker))))))
	  (forward-line 1))) )))

(defun d2t-get-set-show ()
  "Find all set-show options in the doc."
  (and d2t-verbose (message "  Doing d2t-get-set-show ..."))
  (save-excursion
    (let ((alist d2t-commands-alist) start end)
      (while alist
	(if (string= (caar alist) "set-show")
	    (setq start (cdar  alist) ;; location of "1 set-show"
		  end   (cdadr alist) ;; location of next level 2 heading
		  alist nil)
	  (setq alist (cdr alist))))
      ;;(message "%S %S" start end)
      (goto-char start)
      (while (< (point) end)
	(when (re-search-forward "^3 \\(.+\\)$" (point-max) "to_end")
	  (beginning-of-line)
	  (unless (> (point) end)
	    (setq d2t-set-show-alist
		  (append d2t-set-show-alist
			  (list (cons (match-string 1) (point-marker))))))
	  (forward-line 1))) )))


(defun d2t-get-functions ()
  "Find all functions in the doc."
  (and d2t-verbose (message "  Doing d2t-get-functions ..."))
  (let (begin end)
    (save-excursion			; determine bounds of functions
      (when (re-search-forward "^3 Functions" (point-max) "to_end")
	  (beginning-of-line)
	  (setq begin (point-marker))
	  (forward-line 1)
	  (when (re-search-forward "^3 " (point-max) "to_end")
	    (beginning-of-line)
	    (setq end (point-marker))))
      (goto-char begin)
      (while (< (point) end)
	(when (re-search-forward "^4 \\(.+\\)$" (point-max) "to_end")
	  (beginning-of-line)
	  (unless (> (point) end)
	    (setq d2t-functions-alist
		  (append d2t-functions-alist
			  (list (cons (match-string 1) (point-marker))))))
	  (forward-line 1))) )))

;; buffer manipulation functions

;; this can probably be made faster using a let-scoped alist rather
;; than the big cons block
(defun d2t-sectioning ()
  "Find all lines starting with a number.
These are chapters, sections, etc.  Delete these lines and insert the
appropriate sectioning and @node commands."
  (and d2t-verbose (message "  Doing d2t-sectioning ..."))
  (save-excursion
    (while (not (eobp))
      (re-search-forward "^\\([1-9]\\) +\\(.+\\)$" (point-max) "to_end")
      (unless (eobp)
	(let* ((number (match-string 1))
	       (word (match-string 2))
	       (node (substitute ?_ ?  word :test 'char-equal))
	       (eol  (save-excursion (end-of-line) (point-marker))))
	  ;; some node names appear twice.  make them unique.
	  (while (member* node d2t-node-list :test 'string=)
	    (setq node (concat node "_")))
	  (setq d2t-node-list (append d2t-node-list (list node)))
	  (beginning-of-line)
	  (delete-region (point-marker) eol)
	  (if (string-match "[1-4]" number) (insert "\n@node " node "\n"))
	  (cond ((string= number "1")
		 (insert "@chapter " word "\n"))
		((string= number "2")
		 (insert "@section " word "\n"))
		((string= number "3")
		 (insert "@subsection " word "\n"))
		((string= number "4")
		 (insert "@subsubsection " word "\n"))
		(t
		 (insert "\n\n@noindent --- " (upcase word) " ---\n")) ) )))))

(defun d2t-get-eldoc-strings ()
  "Find all syntax descriptions in the doc for ElDoc alist.

Dumps the resulting data into gnuplot-eldoc.el"
  (and d2t-verbose (message "  Doing d2t-get-eldoc-strings ..."))
  (save-excursion
    (goto-char (point-min))
    (let ((alist '()))
      (while (re-search-forward "^\\s-Syntax:" nil t)
	;; Find the section name
	(let ((section
	       (save-excursion
		 (re-search-backward "@node *\\([^ ].*\\)$")
		 (match-string 1))))

	  ;; Skip blank lines
	  (forward-line)
	  (while (looking-at "^\\s-*$") (forward-line))

	  ;; Grab everything at this level of indentation
	  (let* ((lines '())
		 (indent
		  (save-excursion
		    (skip-syntax-forward "-")))
		 (indent-regexp
		  (buffer-substring (point) (+ (point) indent))))
	    (while (looking-at indent-regexp)
	      (let ((line
		     (replace-regexp-in-string
		      "@\\([@{}]\\)" "\\1"
		      (buffer-substring (+ (point-at-bol) indent) (point-at-eol)))))
		(push line lines))
	      (forward-line))

	    ;; Add to alist
	    (if (= (length lines) 1)
		(push `(,section ,(car lines)) alist)
	      (setq lines (nreverse lines))
	      (push `(,section ,(concat (car lines) " [more ...]")
			       ,(mapconcat 'identity lines "\n")) alist)))))
      ;; Save in "gnuplot-eldoc.el"
      (with-temp-file "gnuplot-eldoc.el"
	(let ((print-level nil)
	      (print-length nil))
	  (insert
	   (format ";;;; Automatically generated by doc2texi.el on %s\n\n"
		   (format-time-string "%a, %d %B %Y")))
	  (insert d2t-gnuplot-license)
	  (insert
	   (format
            "\n\n%S\n%S"
            '(eval-when-compile (defvar gnuplot-eldoc-hash nil))       
            `(setq gnuplot-eldoc-hash
                   (let ((tbl (make-hash-table :test 'equal))
                         (alist ',alist))
                     (while alist
                       (puthash (caar alist) (cdar alist) tbl)
                       (setq alist (cdr alist)))
                     tbl)))))))))


(defun d2t-indexing ()
  "Find all lines starting with a question mark.
These are index references.  Delete these lines and insert the
appropriate indexing commands.  Only index one word ? entries,
comment out the multi-word ? entries."
  (and d2t-verbose (message "  Doing d2t-indexing ..."))
  (save-excursion
    (while (not (eobp))
      (re-search-forward "^\\(\\\?\\([^ \n]+\\)\\|=\\([^\n]+\\)\\) *$" 
			 (point-max) "to_end")
      (unless (eobp)
	(let ((word (or (match-string 2) (match-string 3)))
	      (eol  (save-excursion (end-of-line) (point-marker))))
	  (beginning-of-line)
	  (delete-region (point-marker) eol)
	  (insert "@cindex " word "\n")
	  (cond ((assoc word d2t-commands-alist)
		 (insert "@cmindex " word "\n\n"))
		((assoc word d2t-set-show-alist)
		 (insert "@opindex " word "\n\n"))
		((assoc word d2t-terminals-alist)
		 (insert "@tmindex " word "\n\n"))
		((assoc word d2t-functions-alist)
		 (insert "@findex " word "\n\n"))) )))
    (goto-char (point-min))
    (while (not (eobp))
      (re-search-forward "^\\\?" (point-max) "to_end")
      (unless (eobp)
	(if (looking-at "functions? \\(tm_\\w+\\)")
	    (progn
	      (beginning-of-line)
	      (insert "@findex " (match-string 1) "\n@c "))
	  (beginning-of-line)
	  (insert "@c ")))) ))

(defun d2t-comments ()
  "Delete comments and lines beginning with # or %.
# and % lines are used in converting tables into various formats.
Also ignore F lines denoting figures."
  (and d2t-verbose (message "  Doing d2t-comments ..."))
  (save-excursion
    (while (not (eobp))
      (re-search-forward "^[C#%F]" (point-max) "to_end")
      (unless (eobp)
	(let ((eol  (save-excursion (end-of-line)
				    (forward-char 1)
				    (point-marker))))
	  (beginning-of-line)
	  (delete-region (point-marker) eol) )))))

(defun d2t-first-column ()
  "Justify normal text to the 0th column.
This must be run before `d2t-enclose-examples'.
This is rather slow since there are almost 9000 lines of text."
  (and d2t-verbose (message "  Doing d2t-first-column ..."))
  (save-excursion
    (while (not (eobp))
      (and (char-equal (char-after (point)) ? )
	   (delete-char 1))
      (forward-line))))

(defun d2t-braces-atsigns ()
  "Prepend @ to @, {, or } everywhere in the doc.
This MUST be the first conversion function called in
`d2t-doc-to-texi'."
  (and d2t-verbose (message "  Doing d2t-braces-atsigns ..."))
  (save-excursion
    (while (not (eobp))
      (re-search-forward "[@{}]" (point-max) "to_end")
      (unless (eobp)
	(backward-char 1)
	(insert "@")
	(forward-char 1)))))

(defun d2t-tables ()
  "Remove @start table and @end table tags.
These will be made into @example's by `d2t-enclose-examples'.
Thus, the plain text formatting already in the doc is used."
  (and d2t-verbose (message "  Doing d2t-tables ..."))
  (save-excursion
    (while (not (eobp))
      (re-search-forward "^ *@+start table" (point-max) "to_end")
      (unless (eobp)
	(let ((eol  (save-excursion (end-of-line) (point-marker))))
	  (beginning-of-line)
	  (delete-region (point-marker) eol))))
	  ;;(insert "@example")
    (goto-char (point-min))
    (while (not (eobp))
      (re-search-forward "^ *@+end table" (point-max) "to_end")
      (unless (eobp)
	(let ((eol  (save-excursion (end-of-line) (point-marker))))
	  (beginning-of-line)
	  (delete-region (point-marker) eol))))))
	  ;;(insert "@end example")


(defun d2t-enclose-examples ()
  "Turn indented text in the doc into @examples.
This must be run after `d2t-first-column'."
  (and d2t-verbose (message "  Doing d2t-enclose-examples ..."))
  (save-excursion
    (while (not (eobp))
      (re-search-forward "^ +[^ \n]" (point-max) "to_end")
      (unless (eobp)
	(beginning-of-line)
	(insert "@example\n")
	(forward-line 1)
	(re-search-forward "^[^ ]" (point-max) "to_end")
	(beginning-of-line)
	(insert "@end example\n\n") ))))

(defun d2t-handle-html ()
  "Deal with all of the html markup in the doc."
  (and d2t-verbose (message "  Doing d2t-handle-html ..."))
  (save-excursion
    (while (not (eobp))
      (let ((rx (concat "^" (regexp-quote "^")
			" *\\(<?\\)\\([^ \n]+\\)" )))
	(re-search-forward rx (point-max) "to_end")
	(unless (eobp)
	  (let ((bracket (match-string 1))
		(tag (match-string 2))
		(eol  (save-excursion (end-of-line) (point-marker))))
	    (beginning-of-line)
	    (cond
	     ;; comment out images
	     ((and (string= bracket "<") (string= tag "img"))
	      (insert "@c "))
	     ;; tyepset anchors
	     ((and (string= bracket "<") (string-match "^/?a" tag))
	      ;(insert "@c fix me!!  ")
	      (beginning-of-line)
	      (if (looking-at (concat "\\^\\s-*<a\\s-+href=" ; opening tag
				      "\"\\([^\"]+\\)\">\\s-*\n*\\s-*" ; url
				      "\\([^<]+\\)" ; text
				      "[ \t]*\\^?</a>" ; closing tag
				      ))
		  (replace-match (concat "@uref{"
					 (match-string 1)
					 ","
					 (remove* ?^ (match-string 2)
						  :test 'char-equal)
					 "}"))
		  (insert "@c ")))
	     ;; translate <ul> </ul> to @itemize environment
	     ((and (string= bracket "<") (string-match "^ul" tag))
	      (delete-region (point) eol)
	      (insert "\n@itemize @bullet"))
	     ((and (string= bracket "<") (string-match "/ul" tag))
	      (delete-region (point) eol)
	      (insert "@end itemize\n"))
	     ;; list items
	     ((and (string= bracket "<") (string-match "^li" tag))
	      (delete-char 5)
	      (delete-horizontal-space)
	      (insert "@item\n"))
	     ;; fix up a few miscellaneous things
	     (t ;;(looking-at ".*Terminal Types")
	      (insert "@c ")) )
	    (forward-line))) ))))


(defvar d2t-dont-make-ref
  "^fit f\(x\)\\|gnuplot\\|help\\s-+plotting")
(defun d2t-make-refs ()
  "Make cross-references in the text."
  (and d2t-verbose (message "  Doing d2t-make-refs ..."))
  (let ((big-alist (append d2t-level-1-alist
			   d2t-level-2-alist
			   d2t-level-3-alist
			   d2t-level-4-alist)))
    (save-excursion
      (while (re-search-forward "\\(`\\([^`]+\\)`\\)" nil t)
	(let* ((b (match-beginning 1))
	       (e (match-end 1))
	       (list (save-match-data (split-string (match-string 2))))
	       (last (car (reverse list)))
	       (text (concat "@ref{" last "}")))
	  ;; (message "%s %s" (match-string 1) last)
	  (when (and (equal t (try-completion last big-alist))
		     (not (string= last "gnuplot")))
	    (delete-region b e)
	    (insert text)))))))

;;; doc2texi.el ends here