annotate lisp/hyperbole/kotl/kfill.el @ 39:06f275776fba

Added tag r19-15b102 for changeset 1a767b41a199
author cvs
date Mon, 13 Aug 2007 08:54:02 +0200
parents c53a95d3c46d
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;!emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; FILE: kfill.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; SUMMARY: Fill and justify koutline cells (adapted from Kyle Jones' filladapt).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; USAGE: GNU Emacs Lisp Library
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; KEYWORDS: outlines, wp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; AUTHOR: Bob Weiner
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; ORIG-DATE: 23-Jan-94
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 24
diff changeset
10 ;; LAST-MOD: 6-Mar-97 at 01:18:10 by Bob Weiner
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 24
diff changeset
11
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;; Public variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 (defvar kfill:function-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 (if (featurep 'filladapt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 (progn (load "fill") ;; Save basic fill-paragraph function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 (load "simple"))) ;; Save basic do-auto-fill function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 (list (cons 'fill-paragraph (symbol-function 'fill-paragraph))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 (cons 'do-auto-fill (symbol-function 'do-auto-fill))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 "Table containing the old function definitions that kfill overrides.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 (defvar kfill:prefix-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 '(
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; Lists with hanging indents, e.g.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; 1. xxxxx or 1) xxxxx etc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; xxxxx xxx
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; Be sure pattern does not match to: (last word in parens starts
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; newline)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (" *(?\\([0-9][0-9a-z.]*\\|[a-z][0-9a-z.]\\)) +" . kfill:hanging-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (" *\\([0-9]+[a-z.]+[0-9a-z.]*\\|[0-9]+\\|[a-z]\\)\\([.>] +\\| +\\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 . kfill:hanging-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; Included text in news or mail replies
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ("[ \t]*\\(>+ *\\)+" . kfill:normal-included-text)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; Included text generated by SUPERCITE. We can't hope to match all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; the possible variations, your mileage may vary.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ("[ \t]*[A-Za-z0-9][^'`\"< \t\n]*>[ \t]*" . kfill:supercite-included-text)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; Lisp comments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ("[ \t]*\\(;+[ \t]*\\)+" . kfill:lisp-comment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;; UNIX shell comments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ("[ \t]*\\(#+[ \t]*\\)+" . kfill:sh-comment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;; Postscript comments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ("[ \t]*\\(%+[ \t]*\\)+" . kfill:postscript-comment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;; C++ comments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ("[ \t]*//[/ \t]*" . kfill:c++-comment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 ("[?!~*+ -]+ " . kfill:hanging-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; This keeps normal paragraphs from interacting unpleasantly with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; the types given above.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ("[^ \t/#%?!~*+-]" . kfill:normal)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 "Value is an alist of the form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ((REGXP . FUNCTION) ...)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 When fill-paragraph or do-auto-fill is called, the REGEXP of each alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 element is compared with the beginning of the current line. If a match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 is found the corresponding FUNCTION is called. FUNCTION is called with
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 one argument, which is non-nil when invoked on the behalf of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 fill-paragraph, nil for do-auto-fill. It is the job of FUNCTION to set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 the values of the paragraph-* variables (or set a clipping region, if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 paragraph-start and paragraph-separate cannot be made discerning enough)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 so that fill-paragraph and do-auto-fill work correctly in various
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 contexts.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;;; Public functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (defun do-auto-fill ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (if (null fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (let ((paragraph-ignore-fill-prefix nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;; Need this or Emacs 19 ignores fill-prefix when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;; inside a comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (comment-multi-line t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (kfill:adapt nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (kfill:funcall 'do-auto-fill))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (kfill:funcall 'do-auto-fill))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (defun fill-paragraph (arg &optional skip-prefix-remove)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 "Fill paragraph at or after point. Prefix ARG means justify as well."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (interactive "*P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;; Emacs 19 expects a specific symbol here.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (if (and arg (not (symbolp arg))) (setq arg 'full))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (or skip-prefix-remove (kfill:remove-paragraph-prefix))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (catch 'done
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (if (null fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (let ((paragraph-ignore-fill-prefix nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 ;; Need this or Emacs 19 ignores fill-prefix when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ;; inside a comment.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (comment-multi-line t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (paragraph-start paragraph-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (paragraph-separate paragraph-separate)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (if (kfill:adapt t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (throw 'done (kfill:funcall 'fill-paragraph arg)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 ;; Kfill:adapt failed or fill-prefix is set, so do a basic
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;; paragraph fill as adapted from par-align.el.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (kfill:fill-paragraph arg skip-prefix-remove))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ;;;
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 24
diff changeset
107 ;;; Redefine this function so that it sets `fill-prefix-prev' also.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (defun set-fill-prefix (&optional turn-off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 "Set the fill-prefix to the current line up to point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 Also sets fill-prefix-prev to previous value of fill-prefix.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 Filling expects lines to start with the fill prefix and reinserts the fill
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 prefix in each resulting line."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (setq fill-prefix-prev fill-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 fill-prefix (if turn-off
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (buffer-substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (save-excursion (beginning-of-line) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (if (equal fill-prefix-prev "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (setq fill-prefix-prev nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (if (equal fill-prefix "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (setq fill-prefix nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (if fill-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (message "fill-prefix: \"%s\"" fill-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (message "fill-prefix cancelled")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ;;; Private functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (defun kfill:adapt (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (let ((table kfill:prefix-table)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 case-fold-search
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 success )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (while table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (if (not (looking-at (car (car table))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (setq table (cdr table))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (funcall (cdr (car table)) paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (setq success t table nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 success ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (defun kfill:c++-comment (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (setq fill-prefix (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (setq paragraph-separate "^[^ \t/]")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (defun kfill:fill-paragraph (justify-flag &optional leave-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 ;; Backward to para begin
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (re-search-backward (concat "\\`\\|" paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (let ((region-start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (forward-line -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (let ((from (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (forward-paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ;; Forward to real paragraph end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (re-search-forward (concat "\\'\\|" paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (or (= (point) (point-max)) (beginning-of-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (or leave-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (kfill:replace-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (or fill-prefix fill-prefix-prev)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 "" nil region-start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (fill-region-as-paragraph from (point) justify-flag)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (defun kfill:funcall (function &rest args)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (apply (cdr (assq function kfill:function-table)) args))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (defun kfill:hanging-list (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (let (prefix match beg end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (setq prefix (make-string (- (match-end 0) (match-beginning 0)) ?\ ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (if (string-match "^ +$" match)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (while (and (not (bobp)) (looking-at prefix))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (forward-line -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (cond ((kfill:hanging-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (setq beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (t (setq beg (progn (forward-line 1) (point))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (setq beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (forward-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (while (and (looking-at prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (not (equal (char-after (match-end 0)) ?\ )))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (forward-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (setq end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (narrow-to-region beg end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (setq fill-prefix prefix)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (defun kfill:hanging-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 "Return non-nil iff point is in front of a hanging list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (eval kfill:hanging-expression))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (defun kfill:lisp-comment (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (setq fill-prefix (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (setq paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (concat "^" fill-prefix " *;\\|^"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (kfill:negate-string fill-prefix)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (defun kfill:negate-string (string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (let ((len (length string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (i 0) string-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (setq string-list (cons "\\(" nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (while (< i len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (setq string-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (cons (if (= i (1- len)) "" "\\|")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (cons "]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (cons (substring string i (1+ i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (cons "[^"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (cons (regexp-quote (substring string 0 i))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 string-list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 i (1+ i)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (setq string-list (cons "\\)" string-list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (apply 'concat (nreverse string-list))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (defun kfill:normal (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (setq paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 (concat paragraph-separate "\\|^[ \t/#%?!~*+-]"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (defun kfill:normal-included-text (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 (setq fill-prefix (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (setq paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 (concat "^" fill-prefix " *>\\|^"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (kfill:negate-string fill-prefix)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (defun kfill:postscript-comment (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (setq fill-prefix (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (setq paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 (concat "^" fill-prefix " *%\\|^"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (kfill:negate-string fill-prefix)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (defun kfill:remove-paragraph-prefix (&optional indent-str)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 "Remove fill prefix from current paragraph."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 ;; Backward to para begin
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (re-search-backward (concat "\\`\\|" paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (let ((region-start (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (forward-line -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (forward-paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 ;; Forward to real paragraph end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (re-search-forward (concat "\\'\\|" paragraph-separate))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (or (= (point) (point-max)) (beginning-of-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (kfill:replace-string (or fill-prefix fill-prefix-prev)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (if (eq major-mode 'kotl-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (or indent-str
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (make-string (kcell-view:indent) ? ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 nil region-start (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (defun kfill:replace-string (fill-str-prev fill-str &optional suffix start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 "Replace whitespace separated FILL-STR-PREV with FILL-STR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 Optional SUFFIX non-nil means replace at ends of lines, default is beginnings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 Optional arguments START and END specify the replace region, default is the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 current region."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (if fill-str-prev
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (progn (if start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (let ((s (min start end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (setq end (max start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 start s))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (setq start (region-beginning)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 end (region-end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (if (not fill-str) (setq fill-str ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (narrow-to-region start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (let ((prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (if suffix nil "^")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 "[ \t]*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (regexp-quote
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 ;; Get non-whitespace separated fill-str-prev
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (substring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 fill-str-prev
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (or (string-match "[^ \t]" fill-str-prev) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (if (string-match
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 "[ \t]*\\(.*[^ \t]\\)[ \t]*$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 fill-str-prev)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (match-end 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 "[ \t]*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (if suffix "$"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (while (re-search-forward prefix nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (replace-match fill-str nil t))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (defun kfill:sh-comment (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (setq fill-prefix (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (setq paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (concat "^" fill-prefix " *#\\|^"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (kfill:negate-string fill-prefix)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (defun kfill:supercite-included-text (paragraph)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (setq fill-prefix (buffer-substring (match-beginning 0) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (if paragraph
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (setq paragraph-separate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (concat "^" (kfill:negate-string fill-prefix)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 ;;; Private variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (defconst kfill:hanging-expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (cons 'or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (delq nil (mapcar (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (lambda (pattern-type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (if (eq (cdr pattern-type) 'kfill:hanging-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (list 'looking-at (car pattern-type)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 kfill:prefix-table)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 "Conditional expression used to test for hanging indented lists.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (defvar fill-prefix-prev nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 "Prior string inserted at front of new line during filling, or nil for none.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 Setting this variable automatically makes it local to the current buffer.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (make-variable-buffer-local 'fill-prefix-prev)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (provide 'kfill)