home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / latexinfo-structure.el < prev    next >
Encoding:
Text File  |  1992-06-16  |  10.4 KB  |  362 lines

  1. (defconst latexinfo-structure-id
  2.       "$Id: latexinfo-structure.el,v 1.4 1992/01/23 19:03:10 rjc Exp $")
  3.  
  4.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5.  ;;                                                                  ;;
  6.  ;;                                                                  ;;
  7.  ;;         Code to add structure to a latexinfo document.           ;;
  8.  ;;                                                                  ;;
  9.  ;;                 Comments to Richard Caley.                       ;;
  10.  ;;                     (rjc@cstr.ed.ac.uk)                          ;;
  11.  ;;                                                                  ;;
  12.  ;;                                                                  ;;
  13.  ;; Some code to do the hard work of making the \node entries and    ;;
  14.  ;; menus for latexinfo automagically. It understands comments       ;;
  15.  ;; placed below sectioning commands to be the text to be inserted   ;;
  16.  ;; into the menu. Thus:                                             ;;
  17.  ;;                                                                  ;;
  18.  ;;     \subsection{Tweaking Dongles}                                ;;
  19.  ;;         \c How to tweak dongles for fun and profit.              ;;
  20.  ;;                                                                  ;;
  21.  ;; Would produce a menu entry like:                                 ;;
  22.  ;;                                                                  ;;
  23.  ;;  * Tweaking Dongles:: How to tweak dongles for fun and profit.   ;;
  24.  ;;                                                                  ;;
  25.  ;; This code ignores the first \node command in a file since it     ;;
  26.  ;; does not know how you want it connected to other info files.     ;;
  27.  ;; You will have to create that one yourself.                       ;;
  28.  ;;                                                                  ;;
  29.  ;; Obviously this doesn't do all of the work for you, for           ;;
  30.  ;; instance it doesn't do anything about cross references.          ;;
  31.  ;; However it is a good way of getting an initial latexinfo file    ;;
  32.  ;; from which you can go on to do things properly. My strategy is   ;;
  33.  ;; to do a rough draft almost as if writing a normal LaTeX          ;;
  34.  ;; document, but using short section headings suitable for node     ;;
  35.  ;; names. Then create the basic structure with latexinfo-structure  ;;
  36.  ;; and work on from there.                                          ;;
  37.  ;;                                                                  ;;
  38.  ;; I think it is fairly stable, but since it has only had to eat    ;;
  39.  ;; my LaTeX habbits there may be things it doesn't understand.      ;;
  40.  ;;                                                                  ;;
  41.  ;; Many thanks to Hannes Faestermann for finding and reporting      ;;
  42.  ;; some of the sillier bugs.                                        ;;
  43.  ;;                                                                  ;;
  44.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  45.  
  46. ;; LCD Archive Entry:
  47. ;; latexinfo-structure|Richard Caley|R.Caley@ed.ac.uk|
  48. ;; Make latexinfo node and menu entries automatically|
  49. ;; 92-01-23|1.4|~/misc/latexinfo-structure.el.Z|
  50.  
  51.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52.  ;; 
  53.  ;; $Log: latexinfo-structure.el,v $
  54.  ;; Revision 1.4  1992/01/23  19:03:10  rjc
  55.  ;; Added the LCD header.
  56.  ;;
  57.  ;; Revision 1.3  1991/08/05  04:46:17  rjc
  58.  ;; Test for LaTeX commands in latexinfo-detex-string so that
  59.  ;;     the full code is not called for trivial cases
  60.  ;; Put another layer of code in to delete commas from node names.
  61.  ;; Fixed the regular expression matching section titles.
  62.  ;; Added some comments to say what it is doing.
  63.  ;;
  64.  ;; 25th July 1991
  65.  ;; Added latexinfo-detex-string to change subjects and commentry into
  66.  ;;    a form suitable for node names.
  67.  ;; Changed that initial depth to 0 rather than 1, it was confused by
  68.  ;;    \chapters.
  69.  ;; Replaced (line-number) with a call to count-lines.
  70.  ;; 24th July 1991
  71.  ;; Changed looking-at-string to loking-at since the former is not a 
  72.  ;;    standard function.
  73.  ;; Had forgotten to rename the recursive call to build-section-subtree.
  74.  ;;
  75.  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  76.  
  77. (require 'latexinfo)
  78.  
  79. (defvar latexinfo-section-commands
  80.     '("chapter"
  81.       "section"
  82.       "subsection"
  83.       "subsubsection"))
  84.  
  85. (defun latexinfo-looking-at-level ()
  86.        "Returns the level number of the section command we are looking at, 
  87. if any"
  88.  
  89.        (save-excursion
  90.     (if (not (equal (char-after (point)) ?\\ ))
  91.         nil
  92.         (let ((l latexinfo-section-commands)
  93.           (i 1)
  94.           (c nil)
  95.           )
  96.          (forward-char 1)
  97.         
  98.          (while (and (not c) l)
  99.             (if (looking-at (car l))
  100.                 (setq c i)
  101.                 )
  102.             (setq i (1+ i))
  103.             (setq l (cdr l))
  104.             )
  105.          c
  106.          )
  107.         )
  108.     )
  109.        )
  110.  
  111. (defun latexinfo-extract-name ()
  112.  
  113.        "extract the name from the section command here."
  114.  
  115.        (save-excursion
  116.     (if (re-search-forward "{\\([^\n]*\\)}[^\n}]*$" nil t)
  117.         (buffer-substring (match-beginning 1) (match-end 1))
  118.         "none"
  119.         )
  120.     )
  121.        )
  122.  
  123. (defun latexinfo-extract-commentry ()
  124.  
  125.        "Extract any comment following the current line."
  126.  
  127.        (save-excursion
  128.     (let ((c "") s)
  129.          (forward-line 1)
  130.          (beginning-of-line)
  131.          (while (eolp)
  132.             (forward-line 1)
  133.             )
  134.     
  135.          (while (looking-at "[ \t]*\\\\c[ \t]")
  136.             (re-search-forward "[ \t]*\\\\c[ \t]+" nil t)
  137.             (setq c (concat c " " (buffer-substring (point) 
  138.                             (save-excursion
  139.                              (end-of-line)
  140.                              (point)))))
  141.             (forward-line 1)
  142.             (beginning-of-line)
  143.             )
  144.          c
  145.          )
  146.     )
  147.        )
  148.  
  149. (defun latexinfo-detex-string (string)
  150.  
  151.        "remove latex commands from string"
  152.  
  153.        (if (string-match "[\\{}]" string)
  154.        (let ((b (get-buffer-create " latexinfo-tmp-buffer"))
  155.          latexinfo-command-start
  156.          latexinfo-command-end
  157.          latexinfo-command-name
  158.          latexinfo-vindex
  159.          latexinfo-findex
  160.          latexinfo-cindex
  161.          latexinfo-pindex
  162.          latexinfo-tindex
  163.          latexinfo-kindex
  164.          latexinfo-stack
  165.          latexinfo-format-filename
  166.          latexinfo-example-start
  167.          latexinfo-last-node
  168.          latexinfo-node-names
  169.          (fill-column fill-column)
  170.          (input-buffer (current-buffer))
  171.          (input-directory default-directory)
  172.          filename-beginning
  173.          filename-ending)
  174.         (save-excursion
  175.          (set-buffer b)
  176.          (insert string)
  177.          (goto-char (point-min))
  178.          (latexinfo-format-scan-noverbatim)
  179.          (setq string (buffer-substring (point-min) (point-max)))
  180.          (kill-buffer b)
  181.          )
  182.         string
  183.         )
  184.        string
  185.        )
  186.        )
  187.     
  188.  
  189. (defun latexinfo-heading-to-node (heading)
  190.        "Massage a section heading into a node name"
  191.  
  192.        (let ((raw (latexinfo-detex-string heading))
  193.          b)
  194.         (if (string-match "," raw)
  195.         (progn
  196.          (setq b (get-buffer-create " latexinfo-tmp-buffer"))
  197.          (save-excursion
  198.           (set-buffer b)
  199.           (erase-buffer)
  200.           (insert raw)
  201.           (subst-char-in-region (point-min) (point-max) 
  202.                     ?, ?;
  203.                     t)
  204.           (buffer-substring (point-min) (point-max))
  205.           )
  206.          )
  207.         raw
  208.         )
  209.         )
  210.        )
  211.            
  212.                
  213. (defun latexinfo-build-section-subtree (depth)
  214.        "Build a tree representing the structure of the
  215. document as given by the sectioning commands."
  216.  
  217.        (let ((subtree nil) 
  218.          (finished nil)
  219.          it)
  220.  
  221.         (while (and (not finished) (not (eobp)))
  222.             (if (setq it (latexinfo-looking-at-level))
  223.             (if (<= it depth)
  224.                 (setq finished t)
  225.                 (setq subtree 
  226.                   (cons
  227.                    (list (set-marker (make-marker) (point))
  228.                      (1+ (count-lines (point-min) (point)))
  229.                      (latexinfo-heading-to-node (latexinfo-extract-name))
  230.                      (latexinfo-detex-string (latexinfo-extract-commentry))
  231.                      (progn
  232.                       (forward-line 1)
  233.                       (latexinfo-build-section-subtree it)
  234.                       )
  235.                      )
  236.                    subtree))
  237.                 )
  238.             (forward-line 1)
  239.             )
  240.             )
  241.         (reverse subtree)
  242.         )
  243.        )
  244.  
  245. (defun latexinfo-build-section-tree ()
  246.        "Build a tree describing the section structure of BUFFER."
  247.  
  248.        (save-excursion
  249.     (beginning-of-buffer)
  250.     (latexinfo-build-section-subtree 0)
  251.     )
  252.        )
  253.  
  254. (defun latexinfo-insert-structure (nodes parent)
  255.        "Insert the \\node commands into the buffer."
  256.  
  257.        (save-excursion
  258.     (let (nd
  259.           (nds nodes)
  260.           (n nil)
  261.           (p "\t")
  262.           (children nil)
  263.           m
  264.           )
  265.     (while nds
  266.            (setq nd (car nds))
  267.            (setq n (if (cdr nds)
  268.                (car (cdr nds))
  269.                nil))
  270.            (goto-char (car nd))
  271.            (open-line 2)
  272.            (insert "\\node "
  273.                (nth 2 nd) "," 
  274.                (if n (nth 2 n) "\t") ","
  275.                p ","
  276.                parent)
  277.            (setq children (cons (cons (nth 2 nd) (nth 3 nd)) children))
  278.            (latexinfo-insert-structure (nth 4 nd) (nth 2 nd))
  279.            (setq p (nth 2 nd))
  280.            (setq nds (cdr nds))
  281.            )
  282.     (if children
  283.         (progn
  284.          (setq children (reverse children))
  285.          (goto-char (car (car nodes)))
  286.          (forward-line -2)
  287.          (open-line 2)
  288.          (insert "\\begin{menu}\n")
  289.          (while children
  290.             (insert "* " (car (car children)) 
  291.                 "::") 
  292.             (insert-char ? (- 20 (length (car (car children)))))
  293.             (insert (cdr (car children)) "\n")
  294.             (setq children (cdr children))
  295.             )
  296.          (insert "\\end{menu}")
  297.          )
  298.         )
  299.     )
  300.     )
  301.        )
  302.  
  303. (defun latexinfo-delete-structure (buffer)
  304.        "Remove node and meny commands from a buffer."
  305.  
  306.        (interactive "bDelete structure from which buffer? ")
  307.  
  308.        (save-excursion
  309.     (set-buffer buffer)
  310.     (beginning-of-buffer)
  311.     (re-search-forward "\\\\node\\|\\\\begin{menu}\\|\\\\end{menu}" nil t)
  312.     (beginning-of-line)
  313.     (if (looking-at "\\\\node")
  314.         (progn
  315.          (forward-line 1)
  316.         (re-search-forward "\\\\node\\|\\\\begin{menu}\\|\\\\end{menu}" nil t)
  317.         )
  318.         )
  319.     (beginning-of-line)
  320.     (while (re-search-forward "\\\\node\\|\\\\begin{menu}\\|\\\\end{menu}" nil t)
  321.            (beginning-of-line)
  322.            (if (looking-at "\\\\begin{menu}")
  323.            (while (not (looking-at "\\\\end{menu}"))
  324.               (kill-line 1)
  325.               )
  326.            )
  327.            (kill-line 1)
  328.            (delete-blank-lines)
  329.            )
  330.     )
  331.        )
  332.        
  333.  
  334.         
  335.  
  336. (defun latexinfo-structure (buffer)
  337.        "Insert a basic structure into the document based
  338. on the sectioning commands."
  339.  
  340.        (interactive "bAdd structure to which buffer? ")
  341.  
  342.        (save-excursion
  343.     (set-buffer buffer)
  344.     (if (or  (re-search-forward "\\\\begin{menu}\\|\\\\end{menu}" nil t)
  345.          (and 
  346.           (search-forward "\\node" nil t)
  347.           (search-forward "\\node" nil t)))
  348.         (error "There is already structure present")
  349.         )
  350.     (let ( tree )
  351.          (message "building tree...")
  352.          (setq tree (latexinfo-build-section-tree))
  353.          (message "inserting structure...")
  354.          (latexinfo-insert-structure tree "Top")
  355.          (message "done")
  356.          )
  357.     )
  358.        )
  359.  
  360.  
  361.  
  362.