home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / mutt / textmode.mut < prev    next >
Text File  |  1988-10-11  |  6KB  |  233 lines

  1.   ;; textmode.mut : An electric text mode.
  2.   ;;    C Durland
  3.  
  4.   ;; You will notice a lack of anything to do with sentences, outlining and
  5.   ;;   other useful text mode things.
  6.  
  7. (const
  8.   fill-column  72    ; Column to word wrap at (0 means no wrapping)
  9.   tab-size    0    ; Tab size (0 means use the TAB character)
  10. )
  11.  
  12. (defun
  13.   text-mode
  14.   {
  15.     (word-wrap fill-column)(tab-stops tab-size)
  16.  
  17.     (bind-local-key "newline-and-indent" "C-M")
  18.     (bind-local-key "adjust-block"     "M-J")
  19.     (bind-local-key "center-line"     "M-S")
  20.  
  21.     (bind-local-key "backward-paragraph" "M-a")
  22.     (bind-local-key "forward-paragraph"  "M-e")
  23.     (bind-local-key "mark-paragraph"     "M-h")
  24.   }
  25. )
  26.  
  27. (include me.h)
  28. (include wspace.mut)
  29.  
  30. (defun
  31.   center-line  ; Center line the cursor is on.  With arg, centers n lines.
  32.   {
  33.     (int n width wrap-col)
  34.  
  35.     (beginning-of-line)
  36.     (if (== (wrap-col (word-wrap)) 0) (wrap-col (screen-width)))
  37.     (for (n (arg-prefix)) (< 0 n) (-= n 1)
  38.     {
  39.       (kill-whitespace)
  40.       (end-of-line)(width (current-column))(beginning-of-line)
  41.       (to-col (/ (+ 1 (- wrap-col width)) 2))
  42.       (forward-line 1)        ; move to the next line
  43.     })
  44.   }
  45.   center-region        ; Center all the lines in a region.
  46.   {
  47.     (byte type)(int left-edge width height)(INT size)
  48.  
  49.     (region-stats (loc type))
  50.  
  51.     (if (== type MARK-ABOVE-DOT)(exchange-dot-and-mark))
  52.     (arg-prefix height)(center-line)
  53.   }
  54.     ;; Format a block of lines - those lines between the dot and mark
  55.     ;;   inclusive.
  56.     ;; Formats the block ragged right.  With argument, justifies.
  57.     ;; See adjust.mut for details.
  58.   adjust-block
  59.   {
  60.     (byte type)(int left-edge width height)(INT size)
  61.  
  62.     (region-stats (loc type))
  63.  
  64.     (if (== type MARK-ABOVE-DOT)(exchange-dot-and-mark))
  65.     (msg "Formatting ...")
  66.     (adjust-lines height
  67.       (if (== 0 (word-wrap)) fill-column (word-wrap))
  68.       (arg-flag))
  69.     (msg "Formatted.")
  70.   }
  71. )
  72.  
  73. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74. ;;;;;;;; Paragraphs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  76.  
  77. (const
  78.   between-paragraphs        0
  79.   at-start-of-paragraph        1
  80.   on-first-line-of-paragraph    2
  81.   in-paragraph            3
  82.   on-last-line-of-paragraph    4
  83.   at-end-of-paragraph        5
  84. )
  85.  
  86. ;; Move forward to end of paragraph.  With arg, do it arg times.
  87. ;; If at end of paragraph, move to the end of the next one.
  88.  
  89. (defun
  90.   forward-paragraph
  91.   {
  92.     (int col)
  93.  
  94.     (switch (where-in-paragraph)
  95.       between-paragraphs
  96.       {
  97.       (label above-paragraph)
  98.     (skip-forward-blank-lines)
  99.     (forward-line 1)        ;; skip over first line of paragraph
  100.     (if (looking-at '\ *$') { (forward-line -1)(end-of-line)(done) })
  101.       }
  102.       at-start-of-paragraph        ;; skip over first line of paragraph
  103.         (forward-line 1)
  104.       on-first-line-of-paragraph    ;; skip over first line of paragraph
  105.         (forward-line 1)
  106.       on-last-line-of-paragraph   { (end-of-line)(done) }
  107.       at-end-of-paragraph      { (forward-line 1)(goto above-paragraph) }
  108.     )
  109.     (col (get-indent))
  110.     (while TRUE
  111.     {
  112.       (if (not (forward-line 1)) (break))    ;; at end of buffer
  113.       (if (looking-at '\ *$') { (forward-line -1)(break) })
  114.       (skip-whitespace)
  115.       (if (!= col (current-column)) { (forward-line -1)(break) })
  116.     })
  117.     (end-of-line)
  118.   }
  119. )
  120.  
  121. ;; Move backward to start of paragraph.  With arg, do it arg times.
  122. ;; If at start of paragraph, move to start of the one above.
  123.  
  124. (defun
  125.   backward-paragraph
  126.   {
  127.     (int col)
  128.  
  129.     (switch (where-in-paragraph)
  130.       between-paragraphs      (skip-backward-blank-lines)
  131.       at-start-of-paragraph
  132.         { (forward-line -1)(skip-backward-blank-lines) }
  133.       on-first-line-of-paragraph  { (beginning-of-line) (done) }
  134.     )
  135.     (col (get-indent))
  136.     (while TRUE
  137.     {
  138.       (if (not (forward-line -1)) (break))    ;; top of buffer
  139.       (if (looking-at '\ *$') { (forward-line 1)(break) })
  140.       (skip-whitespace)
  141.       (if (!= col (current-column)) (break) )
  142.     })
  143.     (beginning-of-line)
  144.   }
  145. )
  146.  
  147. (defun
  148.     ;; Put mark at beginning of this paragraph, point at end.
  149.     ;; If between paragraphs, mark the next one.
  150.   mark-paragraph
  151.   {
  152.     (forward-paragraph)
  153.     (set-mark)
  154.     (backward-paragraph)
  155.     (exchange-dot-and-mark)
  156.     (msg "Paragraph marked")
  157.   }
  158.   kill-paragraph        ;; Kill to end of paragraph.
  159.   {
  160.     (set-mark)(forward-paragraph)(kill-region)
  161.   }
  162.   backward-kill-paragraph    ;; Kill back to start of paragraph.
  163.   {
  164.     (set-mark)(backward-paragraph)(kill-region)
  165.   }
  166. )
  167.  
  168.  
  169. (defun
  170.   where-in-paragraph HIDDEN
  171.   {
  172.     (int above below dastart col)
  173.  
  174.     (col (current-column))
  175.     (beginning-of-line)
  176.     (if (looking-at '\ *$') { between-paragraphs (done) })
  177.     ;; looking at text
  178.     (dastart (get-indent))
  179.     ;; look at the line above
  180.     (if (not (forward-line -1))
  181.     {
  182.     (label on-first-line-of-paragraph)
  183.       (if (== col 1) at-start-of-paragraph on-first-line-of-paragraph)
  184.       (done)
  185.     })
  186.     (if (looking-at '\ *$')
  187.       { (forward-line 1) (goto on-first-line-of-paragraph) })
  188.     (above (get-indent))
  189.         ;; look at the following line
  190.     (if (forward-line 2)
  191.     {
  192.       (if (looking-at '\ *$')
  193.     {
  194.       (forward-line -1)
  195.     (label on-last-line-of-paragraph)
  196.       (end-of-line)
  197.       (if (== col (current-column))
  198.          at-end-of-paragraph on-last-line-of-paragraph)
  199.       (done)
  200.     }
  201.     {
  202.       (below (get-indent))
  203.       (forward-line -1)    ;; move point back to where it started
  204.       (if (== above below)
  205.         {
  206.           (if (!= above dastart) (goto on-first-line-of-paragraph))
  207.         }
  208.         {    ;; above != below
  209.           (if (== above dastart) (goto on-last-line-of-paragraph))
  210.           (if (!= above dastart) (goto on-first-line-of-paragraph))
  211.         })
  212.     })
  213.     })
  214.     in-paragraph
  215.   }
  216. )
  217.  
  218. (defun
  219.   skip-forward-blank-lines HIDDEN
  220.   {
  221.     (beginning-of-line)
  222.     (while (and (looking-at '\ *$') (forward-line 1)) ())
  223.   }
  224.   skip-backward-blank-lines HIDDEN
  225.   {
  226.     (beginning-of-line)
  227.     (while (and (looking-at '\ *$') (forward-line -1)) ())
  228.   }
  229.   get-indent HIDDEN
  230.     { (beginning-of-line) (skip-whitespace) (current-column) }
  231.  
  232. )
  233.