[BACK]Return to doc2texi.el CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gnuplot / docs

Annotation of OpenXM_contrib/gnuplot/docs/doc2texi.el, Revision 1.1.1.1

1.1       maekawa     1: ;;;; doc2texi.el -- generate a texinfo file from the gnuplot doc file
                      2:
                      3: ;; Copyright (C) 1999 Bruce Ravel
                      4:
                      5: ;; Author:     Bruce Ravel <ravel@phys.washington.edu>
                      6: ;; Maintainer: Bruce Ravel <ravel@phys.washington.edu>
                      7: ;; Created:    March 23 1999
                      8: ;; Updated:    May 28 1999
                      9: ;; Version:    0.2
                     10: ;; Keywords:   gnuplot, document, info
                     11:
                     12: ;; This file is not part of GNU Emacs.
                     13:
                     14: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     15: ;; This lisp script is distributed in the hope that it will be useful,
                     16: ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
                     18: ;;
                     19: ;; Permission is granted to distribute copies of this lisp script
                     20: ;; provided the copyright notice and this permission are preserved in
                     21: ;; all copies.
                     22: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     23: ;; send bug reports to the author (ravel@phys.washington.edu)
                     24: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     25: ;;; Commentary:
                     26: ;;
                     27: ;; I suppose the most immediate question to ask is "Why do this in
                     28: ;; emacs lisp???"  While it is true that the gnuplot.doc file lends
                     29: ;; itself to processing by a 1 pass filter, there are some aspects of
                     30: ;; texinfo files that are a bit tricky and would require 2 or perhaps
                     31: ;; three passes.  Specifically, getting all of the cross references
                     32: ;; made correctly is a lot of work.  Fortunately, texinfo-mode has
                     33: ;; functions for building menus and updating nodes.  This saves a lot
                     34: ;; of sweat and is the principle reason why I decided to write this is
                     35: ;; emacs lisp.
                     36: ;;
                     37: ;; Everything else in gnuplot is written in C for the sake of
                     38: ;; portability.  Emacs lisp, of course, requires that you have emacs.
                     39: ;; For many gnuplot users, that is not a good assumption.  However,
                     40: ;; the likelihood of needing info files in the absence of emacs is
                     41: ;; very, very slim.  I think it is safe to say that someone who needs
                     42: ;; info files has emacs installed and thus will be able to use this
                     43: ;; program.
                     44: ;;
                     45: ;; Since this is emacs, I am not treating the gnuplot.doc file as a
                     46: ;; text stream.  It seems much more efficient in this context to treat
                     47: ;; it as a buffer.  All of the work is done by the function
                     48: ;; `d2t-doc-to-texi'.  Each of the conversion chores is handled by an
                     49: ;; individual function.  Each of thse functions is very similar in
                     50: ;; structure.  They start at the top of the buffer, search forward for
                     51: ;; a line matching the text element being converted, perform the
                     52: ;; replacement in place, and move on until the end of the buffer.
                     53: ;; These text manipulations are actually quite speedy.  The slow part
                     54: ;; of the process is using the texinfo-mode function to update the
                     55: ;; nodes and menus.  However, using these slow functions has one
                     56: ;; advantage -- the texinfo-mode functions for doing menus and nodes
                     57: ;; are certain to do the job correctly.  Although rather slow, this
                     58: ;; approach doesn't totally suck compared to the time cost of
                     59: ;; compiling and running a C program.  And the output from this is
                     60: ;; much more useful than the output from the doc2info program.
                     61: ;;
                     62: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     63: ;;; Use:
                     64: ;;
                     65: ;; Customize the variables at the start of the executable code.  Then
                     66: ;; I intend that this be used from the command line or from a Makefile
                     67: ;; like so:
                     68: ;;
                     69: ;;      emacs -batch -l doc2texi.el -f d2t-doc-to-texi
                     70: ;;
                     71: ;; or
                     72: ;;
                     73: ;;      emacs -batch -l doc2texi.el -f d2t-doc-to-texi-verbosely
                     74: ;;
                     75: ;; This will start emacs in batch mode, load this file, run the
                     76: ;; converter, then quit.  This takes about 30 seconds my 133 MHz
                     77: ;; Pentium.  It also sends a large number of mesages to stderr, so you
                     78: ;; may want to redirect stderr to /dev/null or to a file.
                     79: ;;
                     80: ;; Then you can do
                     81: ;;
                     82: ;;      makeinfo gnuplot.info
                     83: ;;
                     84: ;; You may want to use the --no-split option.
                     85: ;;
                     86: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     87: ;;; History:
                     88: ;;
                     89: ;;  0.1  Mar 23 1999 <BR> Initial version
                     90: ;;  0.2  May 28 1999 <BR>
                     91: ;;  0.3  Jun  2 1999 <BR> Added terminal information, fixed uref problem.
                     92: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     93: ;;; Acknowledgements:
                     94: ;;
                     95: ;; Lars Hecking asked me to look into the doc -> info problem.  Silly
                     96: ;; me, I said "ok."  This is what I came up with.
                     97: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                     98: ;;; To do:
                     99: ;;
                    100: ;;  -- internal cross-references: these are not perfect due to
                    101: ;;     inconsistencies in the use of `` and case inconsistencies in
                    102: ;;     the text.  The latter can be fixed.  Also I need a way to not
                    103: ;;     make the @ref if point is currently in that region.
                    104: ;;  -- catch errors gracefully, particularly when looking for files.
                    105: ;;  -- are guesses about OS specific terminal information correct?
                    106: ;;  -- turn the lists in the "What's New" and "xlabel" sections into
                    107: ;;     proper lists.  "^\\([0-9]\\)+\." finds the list items.  If
                    108: ;;     (match-string 1) is "1" then insert "@enumerate\n@item\n", else
                    109: ;;     insert "@item\n".  also use (replace-match "").  need to find
                    110: ;;     the end somehow.
                    111: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                    112: ;;; Code:
                    113:
                    114: ;;; You may need to customize these variables:
                    115: (defvar d2t-doc-file-name "gnuplot.doc"
                    116:   "Name of the gnuplot.doc file.")
                    117: (defvar d2t-terminal-directory (expand-file-name "../term/")
                    118:   "Location of .trm files in gnuplot source tree.")
                    119:
                    120:
                    121: ;;; You should not need to touch anything below here ;;;;;;;;;;;;;;;;;;;;;
                    122:
                    123: (require 'cl)
                    124: (eval-and-compile                      ; need split-string to do xrefs
                    125:   (if (fboundp 'split-string)
                    126:       ()
                    127:     (defun split-string (string &optional pattern)
                    128:       "Return a list of substrings of STRING which are separated by PATTERN.
                    129: If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
                    130:       (or pattern
                    131:          (setq pattern "[ \f\t\n\r\v]+"))
                    132:       (let (parts (start 0))
                    133:        (while (string-match pattern string start)
                    134:          (setq parts (cons (substring string start (match-beginning 0)) parts)
                    135:                start (match-end 0)))
                    136:        (nreverse (cons (substring string start) parts)))) ))
                    137:
                    138: (defconst d2t-work-buffer-name "*doc2texi*"
                    139:   "Name of scratch buffer where the doc file will be converted into a
                    140:   texi file.")
                    141: (defconst d2t-scratch-buffer-name "*doc2texi-output*")
                    142: (defconst d2t-terminal-buffer-name "*doc2texi-terminal*")
                    143: (defvar d2t-verbose nil)
                    144: (defconst d2t-texi-filename "gnuplot.texi")
                    145:
                    146: (defconst d2t-texi-header
                    147:   "\\input texinfo   @c -*-texinfo-*-
                    148:
                    149: @c %**start of header
                    150: @setfilename gnuplot.info
                    151: @settitle Gnuplot: An Interactive Plotting Program
                    152: @setchapternewpage odd
                    153: @c %**end of header
                    154:
                    155: @c define the command and options indeces
                    156: @defindex cm
                    157: @defindex op
                    158: @defindex tm
                    159:
                    160: @direntry
                    161: * GNUPLOT: (gnuplot).             An Interactive Plotting Program
                    162: @end direntry
                    163:
                    164: @ifnottex
                    165: @node Top, gnuplot, (dir), (dir)
                    166: @top Master Menu
                    167: @end ifnottex
                    168:
                    169: @example
                    170:                        GNUPLOT
                    171:
                    172:             An Interactive Plotting Program
                    173:              Thomas Williams & Colin Kelley
                    174:           Version 3.7 organized by: David Denholm
                    175:
                    176:  Copyright (C) 1986 - 1993, 1998   Thomas Williams, Colin Kelley
                    177:
                    178:        Mailing list for comments: info-gnuplot@@dartmouth.edu
                    179:      Mailing list for bug reports: bug-gnuplot@@dartmouth.edu
                    180:
                    181:          This manual was prepared by Dick Crawford
                    182:                    3 December 1998
                    183:
                    184:
                    185: Major contributors (alphabetic order):
                    186: @end example
                    187:
                    188: "
                    189:   "Texinfo header.")
                    190:
                    191: (defconst d2t-main-menu
                    192:   "@menu
                    193: @end menu"
                    194:   "Main menu.")
                    195:
                    196: (defconst d2t-texi-footer
                    197:   "@node Concept_Index, Command_Index, Bugs, Top
                    198: @unnumbered Concept Index
                    199: @printindex cp
                    200:
                    201: @node Command_Index, Option_Index, Concept_Index, Top
                    202: @unnumbered Command Index
                    203: @printindex cm
                    204:
                    205: @node Options_Index, Function_Index, Command_Index, Top
                    206: @unnumbered Options Index
                    207: @printindex op
                    208:
                    209: @node Function_Index, Terminal_Index, Options_Index, Top
                    210: @unnumbered Function Index
                    211: @printindex fn
                    212:
                    213: @node Terminal_Index, , Options_Index, Top
                    214: @unnumbered Terminal Index
                    215: @printindex tm
                    216:
                    217: @c @shortcontents
                    218: @contents
                    219: @bye
                    220: "
                    221:   "Texinfo file terminator.")
                    222:
                    223: (defvar d2t-level-1-alist nil
                    224:   "Alist of level 1 tags and markers.")
                    225: (defvar d2t-level-2-alist nil
                    226:   "Alist of level 2 tags and markers.")
                    227: (defvar d2t-level-3-alist nil
                    228:   "Alist of level 3 tags and markers.")
                    229: (defvar d2t-level-4-alist nil
                    230:   "Alist of level 4 tags and markers.")
                    231: (defvar d2t-level-5-alist nil
                    232:   "Alist of level 5 tags and markers.")
                    233: (defvar d2t-commands-alist nil
                    234:   "Alist of commands and markers.")
                    235: (defvar d2t-set-show-alist nil
                    236:   "Alist of options and markers.")
                    237: (defvar d2t-functions-alist nil
                    238:   "Alist of functions and markers.")
                    239: (defvar d2t-terminals-alist nil
                    240:   "Alist of terminal types and markers.")
                    241: (defvar d2t-node-list nil
                    242:   "List of nodes.")
                    243:
                    244: (defvar d2t-terminal-list ())
                    245: (setq d2t-terminal-list
                    246:       '("ai"
                    247:        "cgm"
                    248:        "corel"
                    249:        "dumb"
                    250:        "dxf"
                    251:        "eepic"
                    252:        "epson"
                    253:        "fig"
                    254:        "gif"
                    255:        "hp26"
                    256:        "hp2648"
                    257:        "hp500c"
                    258:        "hpgl"
                    259:        "hpljii"
                    260:        "hppj"
                    261:        "imagen"
                    262:        "latex"
                    263:        "metafont"
                    264:        "mif"
                    265:        "pbm"
                    266:        "png"
                    267:        "post"
                    268:        "pslatex"
                    269:        "pstricks"
                    270:        "qms"
                    271:        "table"
                    272:        "tek"
                    273:        "texdraw"
                    274:        "tkcanvas"
                    275:        "tpic"))
                    276:
                    277: (defun d2t-doc-to-texi-verbosely ()
                    278:   "Run `d2t-doc-to-texi' noisily"
                    279:   (interactive)
                    280:   (setq d2t-verbose t)
                    281:   (d2t-doc-to-texi))
                    282:
                    283: (defun d2t-doc-to-texi ()
                    284:   "This is the doc to texi converter function.
                    285: It calls a bunch of other functions, each of which handles one
                    286: particular conversion chore."
                    287:   (interactive)
                    288:   (setq d2t-level-1-alist   nil ;; initialize variables
                    289:        d2t-level-2-alist   nil
                    290:        d2t-level-3-alist   nil
                    291:        d2t-level-4-alist   nil
                    292:        d2t-level-5-alist   nil
                    293:        d2t-commands-alist  nil
                    294:        d2t-set-show-alist  nil
                    295:        d2t-functions-alist nil
                    296:        d2t-terminals-alist nil
                    297:        d2t-node-list       nil)
                    298:   ;; open the doc file and get some data about its contents
                    299:   (d2t-prepare-workspace)
                    300:   (message "Inserting help for terminals ...")
                    301:   (d2t-get-terminals)
                    302:   (message "Analyzing doc file ...")
                    303:   (d2t-get-levels)
                    304:   (d2t-get-commands)
                    305:   (d2t-get-set-show)
                    306:   (d2t-get-functions)
                    307:   ;; convert the buffer from doc to texi, one element at a time
                    308:   (message "Converting to texinfo ...")
                    309:   (d2t-braces-atsigns)   ;; this must be the first conversion function
                    310:   (d2t-comments)         ;; delete comments
                    311:   (d2t-sectioning)       ;; chapters, sections, etc
                    312:   (d2t-indexing)         ;; index markup
                    313:   (d2t-tables)           ;; fix up tables
                    314:   (d2t-handle-html)      ;; fix up html markup
                    315:   (d2t-first-column)     ;; left justify normal text
                    316:   (d2t-enclose-examples) ;; turn indented text into @examples
                    317:   (message "Menus, nodes, xrefs ...")
                    318:   (d2t-make-refs)
                    319:   ;(d2t-make-menus)
                    320:   ;(d2t-set-nodes)
                    321:   (save-excursion        ;; fix a few more things explicitly
                    322:     (goto-char (point-min))
                    323:     (insert d2t-texi-header)
                    324:     (search-forward "@node")
                    325:     ;; (beginning-of-line)
                    326:     ;; (insert "\n\n" d2t-main-menu "\n\n")
                    327:     (search-forward "@node Old_bugs")  ; `texinfo-all-menus-update' seems
                    328:     (beginning-of-line)                        ; to miss this one.  how odd.
                    329:     (insert "@menu\n* Old_bugs::\t\t\t\n@end menu\n\n")
                    330:     (goto-char (point-max))
                    331:     (insert d2t-texi-footer))
                    332:   (load-library "texinfo") ;; now do the hard stuff with texinfo-mode
                    333:   (texinfo-mode)
                    334:   (let ((message-log-max 0)
                    335:        (standard-output (get-buffer-create d2t-scratch-buffer-name)))
                    336:     (message "Making texinfo nodes ...\n")
                    337:     (texinfo-every-node-update)
                    338:     (message "Making texinfo menus ...\n")
                    339:     (texinfo-all-menus-update))
                    340:   (write-file d2t-texi-filename) )  ; save it and done!
                    341:
                    342: (defun d2t-prepare-workspace ()
                    343:   "Create a scratch buffer and populate it with gnuplot.doc."
                    344:   (and d2t-verbose (message "  Doing d2t-prepare-workspace ..."))
                    345:   (if (get-buffer d2t-work-buffer-name)
                    346:       (kill-buffer d2t-work-buffer-name))
                    347:   (if (get-buffer d2t-texi-filename)
                    348:       (kill-buffer d2t-texi-filename))
                    349:   (if (get-buffer d2t-terminal-buffer-name)
                    350:       (kill-buffer d2t-terminal-buffer-name))
                    351:   (get-buffer-create d2t-terminal-buffer-name)
                    352:   (set-buffer (get-buffer-create d2t-work-buffer-name))
                    353:   (insert-file-contents d2t-doc-file-name)
                    354:   (goto-char (point-min)))
                    355:
                    356:
                    357: (defun d2t-get-terminals ()
                    358:   "Insert all appropriate terminal help."
                    359:   (let ((case-fold-search t))
                    360:     (if (string-match "linux" system-configuration)
                    361:        (setq d2t-terminal-list (append d2t-terminal-list
                    362:                                        '("linux"))))
                    363:     (if (string-match "amiga" system-configuration)
                    364:        (setq d2t-terminal-list (append d2t-terminal-list
                    365:                                        '("amiga"))))
                    366:     (if (string-match "atari" system-configuration)
                    367:        (setq d2t-terminal-list (append d2t-terminal-list
                    368:                                        '("atarivdi" "multitos" "atariaes"))))
                    369:     (if (string-match "mac" system-configuration)
                    370:        (setq d2t-terminal-list (append d2t-terminal-list
                    371:                                        '("mac"))))
                    372:     (if (string-match "beos" system-configuration)
                    373:        (setq d2t-terminal-list (append d2t-terminal-list
                    374:                                        '("be"))))
                    375:     (if (string-match "dos" system-configuration)
                    376:        (setq d2t-terminal-list (append d2t-terminal-list
                    377:                                        '("emxvga" "djsvga" "fg" "pc"))))
                    378:     (if (string-match "windows" (format "%s" system-type))
                    379:        (setq d2t-terminal-list (append d2t-terminal-list
                    380:                                        '("win"))))
                    381:     (if (string-match "next" system-configuration)
                    382:        (setq d2t-terminal-list (append d2t-terminal-list
                    383:                                        '("next"))))
                    384:     (if (string-match "os2" system-configuration)
                    385:        (setq d2t-terminal-list (append d2t-terminal-list
                    386:                                        '("pm" "emxvga"))))
                    387:     (if (string-match "irix" system-configuration)
                    388:        (setq d2t-terminal-list (append d2t-terminal-list
                    389:                                        '("iris4d"))))
                    390:     (if (string-match "sco" system-configuration)
                    391:        (setq d2t-terminal-list (append d2t-terminal-list
                    392:                                        '("cgi"))))
                    393:     (if (string-match "sun" system-configuration)
                    394:        (setq d2t-terminal-list (append d2t-terminal-list
                    395:                                        '("sun"))))
                    396:     (if (string-match "vms" system-configuration)
                    397:        (setq d2t-terminal-list (append d2t-terminal-list
                    398:                                        '("vws"))))
                    399:     (unless (member* system-configuration '("dos" "windows" "atari" "amiga")
                    400:                     :test 'string-match)
                    401:       (setq d2t-terminal-list
                    402:            (append d2t-terminal-list
                    403:                    '("x11" "tgif" "gpic" "regis" "t410x" "tex" "xlib")))) )
                    404:   (setq d2t-terminal-list (sort d2t-terminal-list 'string<))
                    405:   (let ((list d2t-terminal-list) file node marker)
                    406:     (save-excursion
                    407:       (when (re-search-forward "^<4" (point-max) t)
                    408:        (beginning-of-line)
                    409:        (insert "@c ")
                    410:        (forward-line 1)
                    411:        (while list
                    412:          (and d2t-verbose (message "    %s ..." (car list)))
                    413:          (setq file (concat d2t-terminal-directory (car list) ".trm"))
                    414:          (when (file-exists-p file)
                    415:            (set-buffer d2t-terminal-buffer-name)
                    416:            (erase-buffer)
                    417:            (insert-file-contents file)
                    418:            ;; find the terminal help
                    419:            (when (search-forward "START_HELP" (point-max) "to_end")
                    420:              (forward-line 1)
                    421:              (delete-region (point-min) (point-marker))
                    422:              (search-forward "END_HELP" (point-max) "to_end")
                    423:              (beginning-of-line)
                    424:              (delete-region (point-marker) (point-max))
                    425:              ;; tidy up the terminal help content
                    426:              (goto-char (point-min))
                    427:              (while (re-search-forward "\",[ \t]*$" nil t)
                    428:                (replace-match "" nil nil))
                    429:              (goto-char (point-min))
                    430:              (while (re-search-forward "^\"" nil t)
                    431:                (replace-match "" nil nil))
                    432:              (goto-char (point-min))
                    433:              (while (re-search-forward "\\\\\"" nil t)
                    434:                (replace-match "\"" nil nil))
                    435:              (goto-char (point-min))
                    436:              (while (re-search-forward "^1[ \t]+\\(.+\\)$" nil t)
                    437:                (setq node   (match-string 1)
                    438:                      marker (point-marker))
                    439:                (replace-match (concat "4  " node) nil nil))
                    440:              (goto-char (point-min))
                    441:              (while (re-search-forward "^2" nil t)
                    442:                (replace-match "5 " nil nil))
                    443:              (goto-char (point-min))
                    444:              ;; set up terminals index
                    445:              (while (re-search-forward "^\?\\([^ ]+\\)$" nil t)
                    446:                (let ((word (match-string 1)))
                    447:                  (unless (string-match "_\\|command-line-options" word)
                    448:                    (setq d2t-terminals-alist
                    449:                          (append d2t-terminals-alist
                    450:                                  (list (cons word (point-marker))))))))
                    451:              ;; and cram it into the doc buffer
                    452:              (set-buffer d2t-work-buffer-name)
                    453:              (insert-buffer-substring d2t-terminal-buffer-name)
                    454:              ))
                    455:          (setq list (cdr list))
                    456:          )))))
                    457:
                    458: ;;; functions for obtaining lists of nodes in the document
                    459:
                    460: (defun d2t-get-levels ()
                    461:   "Find positions of all nodes in the doc."
                    462:   (and d2t-verbose (message "  Doing d2t-get-levels ..."))
                    463:   (let ((list '("1" "2" "3" "4" "5")) str)
                    464:     (while list
                    465:       (setq str (concat "d2t-level-" (car list) "-alist"))
                    466:       (and d2t-verbose (message "    %s ..." str))
                    467:       (save-excursion
                    468:        (while (not (eobp))
                    469:          (when (re-search-forward (concat "^" (car list) " \\(.+\\)$")
                    470:                                   (point-max) "to_end")
                    471:            (beginning-of-line)
                    472:            (set (intern str)
                    473:                 (append (eval (intern str))
                    474:                         (list (cons (match-string 1) (point-marker)))))
                    475:            (forward-line 1))))
                    476:       (setq list (cdr list)))))
                    477:
                    478:
                    479: (defun d2t-get-commands ()
                    480:   "Find all commands in the doc."
                    481:   (and d2t-verbose (message "  Doing d2t-get-commands ..."))
                    482:   (save-excursion
                    483:     (let ((alist d2t-level-1-alist) start end)
                    484:       (while alist
                    485:        (if (string= (caar alist) "Commands")
                    486:            (setq start (cdar  alist) ;; location of "1 Commands"
                    487:                  end   (cdadr alist) ;; location of next level 1 heading
                    488:                  alist nil)
                    489:          (setq alist (cdr alist))))
                    490:       ;;(message "%S %S" start end)
                    491:       (goto-char start)
                    492:       (while (< (point) end)
                    493:        (when (re-search-forward "^2 \\(.+\\)$" (point-max) "to_end")
                    494:          (beginning-of-line)
                    495:          (unless (> (point) end)
                    496:            (setq d2t-commands-alist
                    497:                  (append d2t-commands-alist
                    498:                          (list (cons (match-string 1) (point-marker))))))
                    499:          (forward-line 1))) )))
                    500:
                    501: (defun d2t-get-set-show ()
                    502:   "Find all set-show options in the doc."
                    503:   (and d2t-verbose (message "  Doing d2t-get-set-show ..."))
                    504:   (save-excursion
                    505:     (let ((alist d2t-commands-alist) start end)
                    506:       (while alist
                    507:        (if (string= (caar alist) "set-show")
                    508:            (setq start (cdar  alist) ;; location of "1 set-show"
                    509:                  end   (cdadr alist) ;; location of next level 2 heading
                    510:                  alist nil)
                    511:          (setq alist (cdr alist))))
                    512:       ;;(message "%S %S" start end)
                    513:       (goto-char start)
                    514:       (while (< (point) end)
                    515:        (when (re-search-forward "^3 \\(.+\\)$" (point-max) "to_end")
                    516:          (beginning-of-line)
                    517:          (unless (> (point) end)
                    518:            (setq d2t-set-show-alist
                    519:                  (append d2t-set-show-alist
                    520:                          (list (cons (match-string 1) (point-marker))))))
                    521:          (forward-line 1))) )))
                    522:
                    523:
                    524: (defun d2t-get-functions ()
                    525:   "Find all functions in the doc."
                    526:   (and d2t-verbose (message "  Doing d2t-get-functions ..."))
                    527:   (let (begin end)
                    528:     (save-excursion                    ; determine bounds of functions
                    529:       (when (re-search-forward "^3 Functions" (point-max) "to_end")
                    530:          (beginning-of-line)
                    531:          (setq begin (point-marker))
                    532:          (forward-line 1)
                    533:          (when (re-search-forward "^3 " (point-max) "to_end")
                    534:            (beginning-of-line)
                    535:            (setq end (point-marker))))
                    536:       (goto-char begin)
                    537:       (while (< (point) end)
                    538:        (when (re-search-forward "^4 \\(.+\\)$" (point-max) "to_end")
                    539:          (beginning-of-line)
                    540:          (unless (> (point) end)
                    541:            (setq d2t-functions-alist
                    542:                  (append d2t-functions-alist
                    543:                          (list (cons (match-string 1) (point-marker))))))
                    544:          (forward-line 1))) )))
                    545:
                    546: ;; buffer manipulation functions
                    547:
                    548: ;; this can probably be made faster using a let-scoped alist rather
                    549: ;; than the big cons block
                    550: (defun d2t-sectioning ()
                    551:   "Find all lines starting with a number.
                    552: These are chapters, sections, etc.  Delete these lines and insert the
                    553: appropriate sectioning and @node commands."
                    554:   (and d2t-verbose (message "  Doing d2t-sectioning ..."))
                    555:   (save-excursion
                    556:     (while (not (eobp))
                    557:       (re-search-forward "^\\([1-9]\\) +\\(.+\\)$" (point-max) "to_end")
                    558:       (unless (eobp)
                    559:        (let* ((number (match-string 1))
                    560:               (word (match-string 2))
                    561:               (node (substitute ?_ ?  word :test 'char-equal))
                    562:               (eol  (save-excursion (end-of-line) (point-marker))))
                    563:          ;; some node names appear twice.  make the second one unique.
                    564:          ;; this will fail to work if a third pops up in the future!
                    565:          (if (member* node d2t-node-list :test 'string=)
                    566:              (setq node (concat node "_")))
                    567:          (setq d2t-node-list (append d2t-node-list (list node)))
                    568:          (beginning-of-line)
                    569:          (delete-region (point-marker) eol)
                    570:          (if (string-match "[1-4]" number) (insert "\n@node " node "\n"))
                    571:          (cond ((string= number "1")
                    572:                 (insert "@chapter " word "\n"))
                    573:                ((string= number "2")
                    574:                 (insert "@section " word "\n"))
                    575:                ((string= number "3")
                    576:                 (insert "@subsection " word "\n"))
                    577:                ((string= number "4")
                    578:                 (insert "@subsubsection " word "\n"))
                    579:                (t
                    580:                 (insert "\n\n@noindent --- " (upcase word) " ---\n")) ) )))))
                    581:
                    582: (defun d2t-indexing ()
                    583:   "Find all lines starting with a question mark.
                    584: These are index references.  Delete these lines and insert the
                    585: appropriate indexing commands.  Only index one word ? entries,
                    586: comment out the multi-word ? entries."
                    587:   (and d2t-verbose (message "  Doing d2t-indexing ..."))
                    588:   (save-excursion
                    589:     (while (not (eobp))
                    590:       (re-search-forward "^\\\?\\([^ \n]+\\) *$" (point-max) "to_end")
                    591:       (unless (eobp)
                    592:        (let ((word (match-string 1))
                    593:              (eol  (save-excursion (end-of-line) (point-marker))))
                    594:          (beginning-of-line)
                    595:          (delete-region (point-marker) eol)
                    596:          (insert "@cindex " word "\n")
                    597:          (cond ((assoc word d2t-commands-alist)
                    598:                 (insert "@cmindex " word "\n\n"))
                    599:                ((assoc word d2t-set-show-alist)
                    600:                 (insert "@opindex " word "\n\n"))
                    601:                ((assoc word d2t-terminals-alist)
                    602:                 (insert "@tmindex " word "\n\n"))
                    603:                ((assoc word d2t-functions-alist)
                    604:                 (insert "@findex " word "\n\n"))) )))
                    605:     (goto-char (point-min))
                    606:     (while (not (eobp))
                    607:       (re-search-forward "^\\\?" (point-max) "to_end")
                    608:       (unless (eobp)
                    609:        (if (looking-at "functions? \\(tm_\\w+\\)")
                    610:            (progn
                    611:              (beginning-of-line)
                    612:              (insert "@findex " (match-string 1) "\n@c "))
                    613:          (beginning-of-line)
                    614:          (insert "@c ")))) ))
                    615:
                    616: (defun d2t-comments ()
                    617:   "Delete comments and lines beginning with # or %.
                    618: # and % lines are used in converting tables into various formats."
                    619:   (and d2t-verbose (message "  Doing d2t-comments ..."))
                    620:   (save-excursion
                    621:     (while (not (eobp))
                    622:       (re-search-forward "^[C#%]" (point-max) "to_end")
                    623:       (unless (eobp)
                    624:        (let ((eol  (save-excursion (end-of-line)
                    625:                                    (forward-char 1)
                    626:                                    (point-marker))))
                    627:          (beginning-of-line)
                    628:          (delete-region (point-marker) eol) )))))
                    629:
                    630: (defun d2t-first-column ()
                    631:   "Justify normal text to the 0th column.
                    632: This must be run before `d2t-enclose-examples'.
                    633: This is rather slow since there are almost 9000 lines of text."
                    634:   (and d2t-verbose (message "  Doing d2t-first-column ..."))
                    635:   (save-excursion
                    636:     (while (not (eobp))
                    637:       (and (char-equal (char-after (point)) ? )
                    638:           (delete-char 1))
                    639:       (forward-line))))
                    640:
                    641: (defun d2t-braces-atsigns ()
                    642:   "Prepend @ to @, {, or } everywhere in the doc.
                    643: This MUST be the first conversion function called in
                    644: `d2t-doc-to-texi'."
                    645:   (and d2t-verbose (message "  Doing d2t-braces-atsigns ..."))
                    646:   (save-excursion
                    647:     (while (not (eobp))
                    648:       (re-search-forward "[@{}]" (point-max) "to_end")
                    649:       (unless (eobp)
                    650:        (backward-char 1)
                    651:        (insert "@")
                    652:        (forward-char 1)))))
                    653:
                    654: (defun d2t-tables ()
                    655:   "Remove @start table and @end table tags.
                    656: These will be made into @example's by `d2t-enclose-examples'.
                    657: Thus, the plain text formatting already in the doc is used."
                    658:   (and d2t-verbose (message "  Doing d2t-tables ..."))
                    659:   (save-excursion
                    660:     (while (not (eobp))
                    661:       (re-search-forward "^ *@+start table" (point-max) "to_end")
                    662:       (unless (eobp)
                    663:        (let ((eol  (save-excursion (end-of-line) (point-marker))))
                    664:          (beginning-of-line)
                    665:          (delete-region (point-marker) eol))))
                    666:          ;;(insert "@example")
                    667:     (goto-char (point-min))
                    668:     (while (not (eobp))
                    669:       (re-search-forward "^ *@+end table" (point-max) "to_end")
                    670:       (unless (eobp)
                    671:        (let ((eol  (save-excursion (end-of-line) (point-marker))))
                    672:          (beginning-of-line)
                    673:          (delete-region (point-marker) eol))))))
                    674:          ;;(insert "@end example")
                    675:
                    676:
                    677: (defun d2t-enclose-examples ()
                    678:   "Turn indented text in the doc into @examples.
                    679: This must be run after `d2t-first-column'."
                    680:   (and d2t-verbose (message "  Doing d2t-enclose-examples ..."))
                    681:   (save-excursion
                    682:     (while (not (eobp))
                    683:       (re-search-forward "^ +[^ \n]" (point-max) "to_end")
                    684:       (unless (eobp)
                    685:        (beginning-of-line)
                    686:        (insert "@example\n")
                    687:        (forward-line 1)
                    688:        (re-search-forward "^[^ ]" (point-max) "to_end")
                    689:        (beginning-of-line)
                    690:        (insert "@end example\n\n") ))))
                    691:
                    692: (defun d2t-handle-html ()
                    693:   "Deal with all of the html markup in the doc."
                    694:   (and d2t-verbose (message "  Doing d2t-handle-html ..."))
                    695:   (save-excursion
                    696:     (while (not (eobp))
                    697:       (let ((rx (concat "^" (regexp-quote "^")
                    698:                        " *\\(<?\\)\\([^ \n]+\\)" )))
                    699:        (re-search-forward rx (point-max) "to_end")
                    700:        (unless (eobp)
                    701:          (let ((bracket (match-string 1))
                    702:                (tag (match-string 2))
                    703:                (eol  (save-excursion (end-of-line) (point-marker))))
                    704:            (beginning-of-line)
                    705:            (cond
                    706:             ;; comment out images
                    707:             ((and (string= bracket "<") (string= tag "img"))
                    708:              (insert "@c "))
                    709:             ;; tyepset anchors
                    710:             ((and (string= bracket "<") (string-match "^/?a" tag))
                    711:              ;(insert "@c fix me!!  ")
                    712:              (beginning-of-line)
                    713:              (if (looking-at (concat "\\^\\s-*<a\\s-+href=" ; opening tag
                    714:                                      "\"\\([^\"]+\\)\">\\s-*" ; url
                    715:                                      "\\([^<]+\\)" ; text
                    716:                                      "[ \t]*\\^?</a>" ; closing tag
                    717:                                      ))
                    718:                  (replace-match (concat "@uref{"
                    719:                                         (match-string 1)
                    720:                                         ","
                    721:                                         (remove* ?^ (match-string 2)
                    722:                                                  :test 'char-equal)
                    723:                                         "}"))))
                    724:             ;; translate <ul> </ul> to @itemize environment
                    725:             ((and (string= bracket "<") (string-match "^ul" tag))
                    726:              (delete-region (point) eol)
                    727:              (insert "\n@itemize @bullet"))
                    728:             ((and (string= bracket "<") (string-match "/ul" tag))
                    729:              (delete-region (point) eol)
                    730:              (insert "@end itemize\n"))
                    731:             ;; list items
                    732:             ((and (string= bracket "<") (string-match "^li" tag))
                    733:              (delete-char 5)
                    734:              (delete-horizontal-space)
                    735:              (insert "@item\n"))
                    736:             ;; fix up a few miscellaneous things
                    737:             (t ;;(looking-at ".*Terminal Types")
                    738:              (insert "@c ")) )
                    739:            (forward-line))) ))))
                    740:
                    741:
                    742: (defvar d2t-dont-make-ref
                    743:   "^fit f\(x\)\\|gnuplot\\|help\\s-+plotting")
                    744: (defun d2t-make-refs ()
                    745:   "Make cross-references in the text."
                    746:   (and d2t-verbose (message "  Doing d2t-make-refs ..."))
                    747:   (let ((big-alist (append d2t-level-1-alist
                    748:                           d2t-level-2-alist
                    749:                           d2t-level-3-alist
                    750:                           d2t-level-4-alist)))
                    751:     (save-excursion
                    752:       (while (not (eobp))
                    753:        (re-search-forward "\\(`\\([^`]+\\)`\\)" (point-max) "to_end")
                    754:        (unless (eobp)
                    755:          (let* ((b (match-beginning 1))
                    756:                 (e (match-end 1))
                    757:                 (list (split-string (match-string 2)))
                    758:                 (last (car (reverse list)))
                    759:                 (text (concat "@ref{" last "}")))
                    760:            ;;(message "%s %s" (match-string 1) last)
                    761:            (when (and (equal t (try-completion last big-alist))
                    762:                       (not (string= last "gnuplot")))
                    763:              (delete-region b e)
                    764:              (insert text))))
                    765:        ))))
                    766:
                    767: ;;; doc2texi.el ends here

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>