home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / mutt / package / textmode.mut < prev    next >
Text File  |  1995-01-14  |  9KB  |  334 lines

  1.   ;; textmode.mut : An electric text mode.
  2.   ;; C Durland    Public Domain
  3.  
  4.   ;; You will notice a lack of anything to do with sentences, outlining and
  5.   ;;   other useful text mode things.
  6.  
  7.   ;; Routines provided:
  8.   ;;   adjust-block (bound to M-J):  Format all lines in block.  Uses
  9.   ;;     (word-wrap) or fill-column as the right margin.
  10.   ;;   center-line (bound to M-S):  Center the text in the line that the
  11.   ;;     cursor is on.  With C-U, center n lines.  Uses (word-wrap) or
  12.   ;;     (screen-width) as right margin.
  13.   ;;   center-region (not bound):  Center all lines in a block.
  14.   ;;   underline-line (not bound):  Underline a line of text.  All nonspace
  15.   ;;     characters are underlined with dashes.
  16.  
  17.   ;;   forward-paragraph (bound to M-e):  Attempts to move to the end of a
  18.   ;;     paragraph.
  19.   ;;   backward-paragraph (bound to M-a):  Attempts to move to the begining
  20.   ;;     of a paragraph.
  21.   ;;   mark-paragraph (bound to M-h):  Attempts to make the region include
  22.   ;;     the paragraph the cursor is in.  Put mark at beginning of this
  23.   ;;     paragraph, point at end.  If between paragraphs, mark the next
  24.   ;;     one.
  25.   ;;   cut-paragraph: cut to end of paragraph.
  26.   ;;   backward-cut-paragraph: cut back to start of paragraph.
  27.  
  28.  
  29. (const
  30.   FILL-COLUMN  72    ; Column to word wrap at (0 means no wrapping)
  31.   TAB-SIZE    0    ; Tab size (0 means use the TAB character)
  32. )
  33.  
  34. (include me.mh)
  35.  
  36. (small-int fill-column text-mode-keymap)
  37.  
  38. (defun
  39.   MAIN
  40.   {
  41.     (bind-key (text-mode-keymap (create-keymap))
  42.     "newline-and-indent"    "C-M"
  43.     "adjust-block"        "M-J"
  44.     "center-line"        "M-S"
  45.  
  46.     "backward-paragraph"    "M-a"
  47.     "forward-paragraph"    "M-e"
  48.     "mark-paragraph"    "M-h")
  49.   }
  50.   text-mode
  51.   {
  52.     (clear-modes)
  53.  
  54.     (install-keymap text-mode-keymap LOCAL-KEYMAP)
  55.  
  56.     (major-mode "Text")
  57.  
  58.     (tab-stops TAB-SIZE)
  59.     (word-wrap FILL-COLUMN)
  60.     (text-fill-column FILL-COLUMN)
  61.  
  62.     (if (pgm-exists "text-mode-hook") (floc "text-mode-hook"()))
  63.   }
  64.   text-fill-column    ;; set or get the fill-column
  65.   {
  66.     (if (!= 0 (nargs)) (fill-column (arg 0)))
  67.     fill-column
  68.   }
  69. )
  70.  
  71. (include wspace.mut)
  72.  
  73. (defun
  74.   center-line  ; Center line the cursor is on.  With arg, centers n lines.
  75.   {
  76.     (int n indent wrap-col)
  77.  
  78.     (if (== (wrap-col (word-wrap)) 0) (wrap-col (screen-width)))
  79.     (beginning-of-line)
  80.     (for (n (arg-prefix)) (< 0 n) (-= n 1)
  81.     {
  82.       (delete-whitespace)
  83.       (end-of-line)(indent (/ (+ 1 (- wrap-col (current-column))) 2))
  84.       (beginning-of-line)(to-col indent)
  85.       (forward-line 1)        ; move to the next line
  86.     })
  87.   }
  88.   center-region        ; Center all the lines in a region.
  89.   {
  90.     (byte type)(small-int left-edge width height)(int size)    ;; RegionInfo
  91.  
  92.     (region-stats (loc type) THE-DOT THE-MARK TRUE)
  93.  
  94.     (arg-prefix height)(center-line)
  95.   }
  96. )
  97.  
  98. (defun
  99.   underline-line    ;; underline (with dashes) all text on a line
  100.   {
  101.     (int col)
  102.  
  103.     (end-of-line)(open-line)(beginning-of-line)
  104.     (while (not (looking-at "$"))
  105.     {
  106.       (if (not (is-space))    ;; underline this character
  107.       {
  108.     (col (current-column))
  109.     (forward-line 1)(end-of-line)
  110.     (to-col col)(insert-text "-")
  111.     (forward-line -1)(current-column col)
  112.       })
  113.       (next-character)
  114.     })
  115.   }
  116. )
  117.  
  118. (include block.mut)    ;; for (delete-region-as-block)
  119.  
  120.     ;; Format a block of lines - those lines between the dot and mark
  121.     ;;   inclusive.
  122.     ;; Formats the block ragged right.  With argument, justifies.
  123.     ;; See adjust.mut for details.
  124. (defun
  125. ;  adjust-block
  126. ;  {
  127. ;    (byte type)(small-int left-edge width height)(int size)    ;; RegionInfo
  128. ;
  129. ;    (region-stats (loc type) THE-DOT THE-MARK TRUE)
  130. ;
  131. ;    (msg "Formatting ...")
  132. ;    (adjust-lines height
  133. ;      (if (== 0 (word-wrap)) fill-column (word-wrap))
  134. ;      (arg-flag))
  135. ;    (msg "Formatted.")
  136. ;  }
  137.     ;; Another version of adjust-block.  I did this one because
  138.     ;;   adjust-lines is pretty slow and undo makes it really slow.  So
  139.     ;;   I do the formating in a buffer with no undo and take the hit
  140.     ;;   of moving the text from buffer to buffer.
  141.   adjust-block
  142.   {
  143.     (int text-buffer temp-buffer bag-id wrap-col tab-size)
  144.  
  145.     (wrap-col (if (== 0 (word-wrap)) fill-column (word-wrap)))
  146.  
  147.     (text-buffer (current-buffer))
  148.     (delete-region-as-block)
  149.  
  150.     (tab-size (tab-stops))
  151.     (current-buffer (temp-buffer (create-buffer "")))
  152.     (tab-stops tab-size)
  153.     (insert-bag CUT-BUFFER)
  154.     (beginning-of-buffer)
  155.  
  156.     (msg "Formatting ...")
  157.     (adjust-lines 10000 wrap-col (arg-flag))        ;; !!!ick
  158.  
  159.     (beginning-of-buffer)(set-mark)(end-of-buffer)
  160.     (append-to-bag (bag-id (create-bag)) APPEND-REGION)
  161.  
  162.     (msg "Formatted.")
  163.  
  164.     (current-buffer text-buffer)
  165.     (insert-bag bag-id)
  166.  
  167.     ; clean up
  168.     (free-buffer temp-buffer)(free-bag bag-id)
  169.   }
  170. )
  171.  
  172. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  173. ;;;;;;;; Paragraphs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  174. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  175.  
  176. (const
  177.   between-paragraphs        0
  178.   at-start-of-paragraph        1
  179.   on-first-line-of-paragraph    2
  180.   in-paragraph            3
  181.   on-last-line-of-paragraph    4
  182.   at-end-of-paragraph        5
  183. )
  184.  
  185. ;; Move forward to end of paragraph.  With arg, do it arg times.
  186. ;; If at end of paragraph, move to the end of the next one.
  187.  
  188. (defun
  189.   forward-paragraph
  190.   {
  191.     (int col)
  192.  
  193.     (switch (where-in-paragraph)
  194.       between-paragraphs
  195.       {
  196.       (label above-paragraph)
  197.     (skip-forward-blank-lines)
  198.     (forward-line 1)        ;; skip over first line of paragraph
  199.     (if (looking-at '\ *$') { (forward-line -1)(end-of-line)(done) })
  200.       }
  201.       at-start-of-paragraph        ;; skip over first line of paragraph
  202.         (forward-line 1)
  203.       on-first-line-of-paragraph    ;; skip over first line of paragraph
  204.         (forward-line 1)
  205.       on-last-line-of-paragraph   { (end-of-line)(done) }
  206.       at-end-of-paragraph      { (forward-line 1)(goto above-paragraph) }
  207.     )
  208.     (col (get-indent))
  209.     (while TRUE
  210.     {
  211.       (if (not (forward-line 1)) (break))    ;; at end of buffer
  212.       (if (looking-at '\ *$') { (forward-line -1)(break) })
  213.       (skip-whitespace)
  214.       (if (!= col (current-column)) { (forward-line -1)(break) })
  215.     })
  216.     (end-of-line)
  217.   }
  218. )
  219.  
  220. ;; Move backward to start of paragraph.  With arg, do it arg times.
  221. ;; If at start of paragraph, move to start of the one above.
  222.  
  223. (defun
  224.   backward-paragraph
  225.   {
  226.     (int col)
  227.  
  228.     (switch (where-in-paragraph)
  229.       between-paragraphs      (skip-backward-blank-lines)
  230.       at-start-of-paragraph
  231.         { (forward-line -1)(skip-backward-blank-lines) }
  232.       on-first-line-of-paragraph  { (beginning-of-line) (done) }
  233.     )
  234.     (col (get-indent))
  235.     (while TRUE
  236.     {
  237.       (if (not (forward-line -1)) (break))    ;; top of buffer
  238.       (if (looking-at '\ *$') { (forward-line 1)(break) })
  239.       (skip-whitespace)
  240.       (if (!= col (current-column)) (break) )
  241.     })
  242.     (beginning-of-line)
  243.   }
  244. )
  245.  
  246. (defun
  247.     ;; Put mark at beginning of this paragraph, point at end.
  248.     ;; If between paragraphs, mark the next one.
  249.   mark-paragraph
  250.   {
  251.     (forward-paragraph)
  252.     (set-mark)
  253.     (backward-paragraph)
  254.     (swap-marks)
  255.     (msg "Paragraph marked")
  256.   }
  257.   cut-paragraph        ;; Cut to end of paragraph.
  258.   {
  259.     (set-mark)(forward-paragraph)(cut-region)
  260.   }
  261.   backward-cut-paragraph    ;; Cut back to start of paragraph.
  262.   {
  263.     (set-mark)(backward-paragraph)(cut-region)
  264.   }
  265. )
  266.  
  267.  
  268. (defun
  269.     ;;   Try to figure out where the point is in a paragraph by looking at
  270.     ;; the lines above and below the point.
  271.   where-in-paragraph HIDDEN
  272.   {
  273.     (int above below point col)
  274.  
  275.     (col (current-column))
  276.     (beginning-of-line)
  277.     (if (looking-at '\ *$') { between-paragraphs (done) })
  278.     ;; looking at text
  279.     (point (get-indent))
  280.     ;; look at the line above
  281.     (if (not (forward-line -1))        ;; at beginning of buffer
  282.     {
  283.     (label on-first-line-of-paragraph)
  284.       (if (== col 1) at-start-of-paragraph on-first-line-of-paragraph)
  285.       (done)
  286.     })
  287.     (if (looking-at '\ *$')
  288.       { (forward-line 1) (goto on-first-line-of-paragraph) })
  289.     (above (get-indent))
  290.         ;; look at the following line
  291.     (if (forward-line 2)
  292.     {
  293.       (if (looking-at '\ *$')
  294.       {
  295.     (forward-line -1)
  296.       (label on-last-line-of-paragraph)
  297.     (end-of-line)
  298.     (if (== col (current-column))
  299.        at-end-of-paragraph on-last-line-of-paragraph)
  300.     (done)
  301.       }
  302.       {
  303.     (below (get-indent))
  304.     (forward-line -1)    ;; move point back to where it started
  305.     (if (== above below)
  306.     {
  307.       (if (!= above point) (goto on-first-line-of-paragraph))
  308.     }
  309.     {    ;; above != below
  310.       (if (== above point) (goto on-last-line-of-paragraph))
  311.       (if (!= above point) (goto on-first-line-of-paragraph))
  312.     })
  313.       })
  314.       in-paragraph
  315.     }
  316.     (goto on-last-line-of-paragraph))        ;; else at end of buffer
  317.   }
  318. )
  319.  
  320. (defun
  321.   skip-forward-blank-lines HIDDEN
  322.   {
  323.     (beginning-of-line)
  324.     (while (and (looking-at '\ *$') (forward-line 1)) ())
  325.   }
  326.   skip-backward-blank-lines HIDDEN
  327.   {
  328.     (beginning-of-line)
  329.     (while (and (looking-at '\ *$') (forward-line -1)) ())
  330.   }
  331.   get-indent HIDDEN
  332.     { (beginning-of-line) (skip-whitespace) (current-column) }
  333. )
  334.