home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34exe.zip / mutt / contrib / docform.mut < prev    next >
Text File  |  1995-01-14  |  7KB  |  260 lines

  1. ;;
  2. ;; File:
  3. ;;      docform.mut  -  document formatting
  4. ;;
  5. ;; Description:
  6. ;;      Support for formatting of documents.
  7. ;;
  8. ;;      The function 'doc-repaginate' is able to repaginate a document
  9. ;;      (using ^L's at the beginning of an empty line line). The
  10. ;;      repagination function is modelled after MS-WORD version 4.00.
  11. ;;
  12. ;;      Although useful as a standalone package, this module is
  13. ;;      modelled as an extension to 'docmode'.
  14. ;;
  15. ;; Known bugs:
  16. ;;     - Repagination ussumes ^L's are at the beginning of an empty line
  17. ;;       (so be careful when entering a ^L manually, that is, without the
  18. ;;       help of the repagination functions).
  19. ;;
  20. ;; Historie:
  21. ;;     940126 M.J. van der Velden
  22. ;;            - Creation
  23. ;;            Public Domain (Version 1.0)
  24. ;;
  25.  
  26. (include "me.mh")
  27.  
  28. ;; Constants that define defaults for the global variables below.
  29. (const
  30.     DOC-DEF-LINES-ON-PAGE 60
  31.     DOC-NEWPAGE           "^L"
  32.     DOC-NEWLINE           "^J"
  33. )
  34. ;; End Of Constants to define default behavior.
  35.  
  36. ;; Global variables
  37. (int
  38.     docLinesOnPage   ;; Number of lines allowed on a page.
  39. )
  40. ;; End Of Globale Variables
  41.  
  42. ;; Interactivally change document options.
  43. (defun
  44.     doc-set-lines-on-page
  45.     {
  46.         (string answer)
  47.  
  48.         (answer (ask (concat "Maximum number of lines on page [" docLinesOnPage "]: ")))
  49.         (if (!= answer "") {
  50.             (docLinesOnPage (convert-to NUMBER answer))
  51.             (word-wrap docLinesOnPage)
  52.         })
  53.     }
  54. )
  55.  
  56. ;; Non-interactivally change document options.
  57. (defun
  58.     ;; set or get docLinesOnPage
  59.     doc-lines-on-page
  60.     {
  61.         (if (!= 0 (nargs)) {
  62.             (docLinesOnPage (arg 0))
  63.         })
  64.         docLinesOnPage
  65.     }
  66. )
  67.  
  68. ;; Repagination functions
  69. (defun
  70.     doc-remove-pagebreaks
  71.     {
  72.         (msg "Removing pagebreaks... ")
  73.  
  74.         (save-point
  75.         {{
  76.             (beginning-of-buffer)
  77.             (while (not (EoB)) {
  78.                 (if (search-forward DOC-NEWPAGE) {
  79.                     (beginning-of-line)
  80.                     (cut-line)
  81.                 }{
  82.                     (break)
  83.                 })
  84.             })
  85.         }})
  86.  
  87.         (msg "Pagebreaks removed!")
  88.     }
  89.  
  90.     doc-keep-pagebreak (int pageNo linesOnPage) HIDDEN
  91.     {
  92.         (int  key)
  93.         (bool ready)
  94.  
  95.         (msg linesOnPage " line" (if (== linesOnPage 1) "" "s") " on page " pageNo
  96.              ". Press Y to keep pagebreak, N to remove or Q to quit... "
  97.         )
  98.         (update)
  99.  
  100.         (ready FALSE)
  101.         (while (not ready) {
  102.             (key (get-key))
  103.             (switch key
  104.                 0x0079 { ;; y
  105.                     (ready TRUE)
  106.                 }
  107.                 0x0059  { ;; Y
  108.                     (ready TRUE)
  109.                 }
  110.                 0x006E { ;; n
  111.                     (cut-line)
  112.                     (ready TRUE)
  113.                 }
  114.                 0x004E { ;; N
  115.                     (cut-line)
  116.                     (ready TRUE)
  117.                 }
  118.                 0x0071 { ;; q
  119.                     (msg "Repagination aborted!")
  120.                     (halt)
  121.                     (ready TRUE)
  122.                 }
  123.                 0x0051 { ;; Q
  124.                     (msg "Repagination aborted!")
  125.                     (halt)
  126.                     (ready TRUE)
  127.                 }
  128.                 0x0147 { ;; ^G
  129.                     (msg "Repagination aborted!")
  130.                     (halt)
  131.                     (ready TRUE)
  132.                 }
  133.                 default {
  134.                     (ready FALSE)
  135.                 }
  136.             )
  137.         })
  138.  
  139.         (or (== key 0x0079) (== key 0x0059))   ;; y or Y
  140.     }
  141.  
  142.     doc-confirm-pagebreak (int pageNo linesOnPage maxLinesOnPage) HIDDEN
  143.     {
  144.         (int  key)
  145.         (bool ready)
  146.         (int  lineNo)
  147.  
  148.         (lineNo linesOnPage)
  149.         (ready FALSE)
  150.         (while (not ready) {
  151.             (msg lineNo " lines on page " pageNo
  152.                  ". Press Y to confirm, Up/Down to move or Q to quit... "
  153.             )
  154.             (update)
  155.  
  156.             (key (get-key))
  157.             (switch key
  158.                 0x0079 { ;; y
  159.                     (insert-text DOC-NEWPAGE DOC-NEWLINE)
  160.                     (ready TRUE)
  161.                 }
  162.                 0x0059  { ;; Y
  163.                     (insert-text DOC-NEWPAGE DOC-NEWLINE)
  164.                     (ready TRUE)
  165.                 }
  166.                 0x0843 { ;; Up
  167.                     (if (and (> lineNo 1) (forward-line -1)) {
  168.                         (-= lineNo 1)
  169.                         (update)
  170.                     })
  171.                     (ready FALSE)
  172.                 }
  173.                 0x0150 { ;; ^P
  174.                     (if (and (> lineNo 1) (forward-line -1)) {
  175.                         (-= lineNo 1)
  176.                         (update)
  177.                     })
  178.                     (ready FALSE)
  179.                 }
  180.                 0x0844 { ;; Down
  181.                     (if (forward-line 1) {
  182.                         (+= lineNo 1)
  183.                         (update)
  184.                     })
  185.                     (ready FALSE)
  186.                 }
  187.                 0x014E { ;; ^N
  188.                     (if (forward-line 1) {
  189.                         (+= lineNo 1)
  190.                         (update)
  191.                     })
  192.                     (ready FALSE)
  193.                 }
  194.                 0x0071 { ;; q
  195.                     (msg "Repagination aborted!")
  196.                     (halt)
  197.                     (ready TRUE)
  198.                 }
  199.                 0x0051 { ;; Q
  200.                     (msg "Repagination aborted!")
  201.                     (halt)
  202.                     (ready TRUE)
  203.                 }
  204.                 0x0147 { ;; ^G
  205.                     (msg "Repagination aborted!")
  206.                     (halt)
  207.                     (ready TRUE)
  208.                 }
  209.                 default {
  210.                     (ready FALSE)
  211.                 }
  212.             )
  213.         })
  214.  
  215.         (or (== key 121) (== key 89))   ;; y or Y
  216.     }
  217.  
  218.     doc-repaginate
  219.     {
  220.         (int pageHeight)
  221.         (int lineNo)
  222.         (int linesOnPage)
  223.         (int maxLinesOnPage)
  224.         (int pageNo)
  225.  
  226.         (beginning-of-buffer)
  227.         (lineNo 1)
  228.         (linesOnPage 0)
  229.         (maxLinesOnPage (doc-lines-on-page))
  230.         (pageNo 1)
  231.         (while (not (EoB)) {
  232.             (+= lineNo 1)
  233.             (if (looking-at DOC-NEWPAGE) {
  234.                 (if (doc-keep-pagebreak pageNo linesOnPage) {
  235.                     (+= pageNo 1)
  236.                     (linesOnPage 0)
  237.                 })
  238.             })
  239.             (if (== linesOnPage maxLinesOnPage) {
  240.                 (if (doc-confirm-pagebreak pageNo linesOnPage 3) {
  241.                     (+= pageNo 1)
  242.                     (linesOnPage 0)
  243.                 })
  244.             })
  245.             (forward-line 1)
  246.             (+= linesOnPage 1)
  247.         })
  248.  
  249.         (msg "Repagination ready, " pageNo " pages counted!")
  250.     }
  251. )
  252.  
  253. ;; Initialization
  254. (defun
  255.     MAIN
  256.     {
  257.         (docLinesOnPage DOC-DEF-LINES-ON-PAGE)
  258.     }
  259. )
  260.