annotate lisp/hyperbole/kotl/kfill.el @ 0:376386a54a3c r19-14

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