home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 5 Edit
/
05-Edit.zip
/
me34exe.zip
/
mutt
/
package
/
textmode.mut
< prev
next >
Wrap
Text File
|
1995-01-14
|
9KB
|
334 lines
;; textmode.mut : An electric text mode.
;; C Durland Public Domain
;; You will notice a lack of anything to do with sentences, outlining and
;; other useful text mode things.
;; Routines provided:
;; adjust-block (bound to M-J): Format all lines in block. Uses
;; (word-wrap) or fill-column as the right margin.
;; center-line (bound to M-S): Center the text in the line that the
;; cursor is on. With C-U, center n lines. Uses (word-wrap) or
;; (screen-width) as right margin.
;; center-region (not bound): Center all lines in a block.
;; underline-line (not bound): Underline a line of text. All nonspace
;; characters are underlined with dashes.
;; forward-paragraph (bound to M-e): Attempts to move to the end of a
;; paragraph.
;; backward-paragraph (bound to M-a): Attempts to move to the begining
;; of a paragraph.
;; mark-paragraph (bound to M-h): Attempts to make the region include
;; the paragraph the cursor is in. Put mark at beginning of this
;; paragraph, point at end. If between paragraphs, mark the next
;; one.
;; cut-paragraph: cut to end of paragraph.
;; backward-cut-paragraph: cut back to start of paragraph.
(const
FILL-COLUMN 72 ; Column to word wrap at (0 means no wrapping)
TAB-SIZE 0 ; Tab size (0 means use the TAB character)
)
(include me.mh)
(small-int fill-column text-mode-keymap)
(defun
MAIN
{
(bind-key (text-mode-keymap (create-keymap))
"newline-and-indent" "C-M"
"adjust-block" "M-J"
"center-line" "M-S"
"backward-paragraph" "M-a"
"forward-paragraph" "M-e"
"mark-paragraph" "M-h")
}
text-mode
{
(clear-modes)
(install-keymap text-mode-keymap LOCAL-KEYMAP)
(major-mode "Text")
(tab-stops TAB-SIZE)
(word-wrap FILL-COLUMN)
(text-fill-column FILL-COLUMN)
(if (pgm-exists "text-mode-hook") (floc "text-mode-hook"()))
}
text-fill-column ;; set or get the fill-column
{
(if (!= 0 (nargs)) (fill-column (arg 0)))
fill-column
}
)
(include wspace.mut)
(defun
center-line ; Center line the cursor is on. With arg, centers n lines.
{
(int n indent wrap-col)
(if (== (wrap-col (word-wrap)) 0) (wrap-col (screen-width)))
(beginning-of-line)
(for (n (arg-prefix)) (< 0 n) (-= n 1)
{
(delete-whitespace)
(end-of-line)(indent (/ (+ 1 (- wrap-col (current-column))) 2))
(beginning-of-line)(to-col indent)
(forward-line 1) ; move to the next line
})
}
center-region ; Center all the lines in a region.
{
(byte type)(small-int left-edge width height)(int size) ;; RegionInfo
(region-stats (loc type) THE-DOT THE-MARK TRUE)
(arg-prefix height)(center-line)
}
)
(defun
underline-line ;; underline (with dashes) all text on a line
{
(int col)
(end-of-line)(open-line)(beginning-of-line)
(while (not (looking-at "$"))
{
(if (not (is-space)) ;; underline this character
{
(col (current-column))
(forward-line 1)(end-of-line)
(to-col col)(insert-text "-")
(forward-line -1)(current-column col)
})
(next-character)
})
}
)
(include block.mut) ;; for (delete-region-as-block)
;; Format a block of lines - those lines between the dot and mark
;; inclusive.
;; Formats the block ragged right. With argument, justifies.
;; See adjust.mut for details.
(defun
; adjust-block
; {
; (byte type)(small-int left-edge width height)(int size) ;; RegionInfo
;
; (region-stats (loc type) THE-DOT THE-MARK TRUE)
;
; (msg "Formatting ...")
; (adjust-lines height
; (if (== 0 (word-wrap)) fill-column (word-wrap))
; (arg-flag))
; (msg "Formatted.")
; }
;; Another version of adjust-block. I did this one because
;; adjust-lines is pretty slow and undo makes it really slow. So
;; I do the formating in a buffer with no undo and take the hit
;; of moving the text from buffer to buffer.
adjust-block
{
(int text-buffer temp-buffer bag-id wrap-col tab-size)
(wrap-col (if (== 0 (word-wrap)) fill-column (word-wrap)))
(text-buffer (current-buffer))
(delete-region-as-block)
(tab-size (tab-stops))
(current-buffer (temp-buffer (create-buffer "")))
(tab-stops tab-size)
(insert-bag CUT-BUFFER)
(beginning-of-buffer)
(msg "Formatting ...")
(adjust-lines 10000 wrap-col (arg-flag)) ;; !!!ick
(beginning-of-buffer)(set-mark)(end-of-buffer)
(append-to-bag (bag-id (create-bag)) APPEND-REGION)
(msg "Formatted.")
(current-buffer text-buffer)
(insert-bag bag-id)
; clean up
(free-buffer temp-buffer)(free-bag bag-id)
}
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; Paragraphs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(const
between-paragraphs 0
at-start-of-paragraph 1
on-first-line-of-paragraph 2
in-paragraph 3
on-last-line-of-paragraph 4
at-end-of-paragraph 5
)
;; Move forward to end of paragraph. With arg, do it arg times.
;; If at end of paragraph, move to the end of the next one.
(defun
forward-paragraph
{
(int col)
(switch (where-in-paragraph)
between-paragraphs
{
(label above-paragraph)
(skip-forward-blank-lines)
(forward-line 1) ;; skip over first line of paragraph
(if (looking-at '\ *$') { (forward-line -1)(end-of-line)(done) })
}
at-start-of-paragraph ;; skip over first line of paragraph
(forward-line 1)
on-first-line-of-paragraph ;; skip over first line of paragraph
(forward-line 1)
on-last-line-of-paragraph { (end-of-line)(done) }
at-end-of-paragraph { (forward-line 1)(goto above-paragraph) }
)
(col (get-indent))
(while TRUE
{
(if (not (forward-line 1)) (break)) ;; at end of buffer
(if (looking-at '\ *$') { (forward-line -1)(break) })
(skip-whitespace)
(if (!= col (current-column)) { (forward-line -1)(break) })
})
(end-of-line)
}
)
;; Move backward to start of paragraph. With arg, do it arg times.
;; If at start of paragraph, move to start of the one above.
(defun
backward-paragraph
{
(int col)
(switch (where-in-paragraph)
between-paragraphs (skip-backward-blank-lines)
at-start-of-paragraph
{ (forward-line -1)(skip-backward-blank-lines) }
on-first-line-of-paragraph { (beginning-of-line) (done) }
)
(col (get-indent))
(while TRUE
{
(if (not (forward-line -1)) (break)) ;; top of buffer
(if (looking-at '\ *$') { (forward-line 1)(break) })
(skip-whitespace)
(if (!= col (current-column)) (break) )
})
(beginning-of-line)
}
)
(defun
;; Put mark at beginning of this paragraph, point at end.
;; If between paragraphs, mark the next one.
mark-paragraph
{
(forward-paragraph)
(set-mark)
(backward-paragraph)
(swap-marks)
(msg "Paragraph marked")
}
cut-paragraph ;; Cut to end of paragraph.
{
(set-mark)(forward-paragraph)(cut-region)
}
backward-cut-paragraph ;; Cut back to start of paragraph.
{
(set-mark)(backward-paragraph)(cut-region)
}
)
(defun
;; Try to figure out where the point is in a paragraph by looking at
;; the lines above and below the point.
where-in-paragraph HIDDEN
{
(int above below point col)
(col (current-column))
(beginning-of-line)
(if (looking-at '\ *$') { between-paragraphs (done) })
;; looking at text
(point (get-indent))
;; look at the line above
(if (not (forward-line -1)) ;; at beginning of buffer
{
(label on-first-line-of-paragraph)
(if (== col 1) at-start-of-paragraph on-first-line-of-paragraph)
(done)
})
(if (looking-at '\ *$')
{ (forward-line 1) (goto on-first-line-of-paragraph) })
(above (get-indent))
;; look at the following line
(if (forward-line 2)
{
(if (looking-at '\ *$')
{
(forward-line -1)
(label on-last-line-of-paragraph)
(end-of-line)
(if (== col (current-column))
at-end-of-paragraph on-last-line-of-paragraph)
(done)
}
{
(below (get-indent))
(forward-line -1) ;; move point back to where it started
(if (== above below)
{
(if (!= above point) (goto on-first-line-of-paragraph))
}
{ ;; above != below
(if (== above point) (goto on-last-line-of-paragraph))
(if (!= above point) (goto on-first-line-of-paragraph))
})
})
in-paragraph
}
(goto on-last-line-of-paragraph)) ;; else at end of buffer
}
)
(defun
skip-forward-blank-lines HIDDEN
{
(beginning-of-line)
(while (and (looking-at '\ *$') (forward-line 1)) ())
}
skip-backward-blank-lines HIDDEN
{
(beginning-of-line)
(while (and (looking-at '\ *$') (forward-line -1)) ())
}
get-indent HIDDEN
{ (beginning-of-line) (skip-whitespace) (current-column) }
)