| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
(defgroup fill nil |
|---|
| 34 |
"Indenting and filling text." |
|---|
| 35 |
:link '(custom-manual "(emacs)Filling") |
|---|
| 36 |
:group 'editing) |
|---|
| 37 |
|
|---|
| 38 |
(defcustom fill-individual-varying-indent nil |
|---|
| 39 |
"*Controls criterion for a new paragraph in `fill-individual-paragraphs'. |
|---|
| 40 |
Non-nil means changing indent doesn't end a paragraph. |
|---|
| 41 |
That mode can handle paragraphs with extra indentation on the first line, |
|---|
| 42 |
but it requires separator lines between paragraphs. |
|---|
| 43 |
A value of nil means that any change in indentation starts a new paragraph." |
|---|
| 44 |
:type 'boolean |
|---|
| 45 |
:group 'fill) |
|---|
| 46 |
|
|---|
| 47 |
(defcustom colon-double-space nil |
|---|
| 48 |
"*Non-nil means put two spaces after a colon when filling." |
|---|
| 49 |
:type 'boolean |
|---|
| 50 |
:group 'fill) |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
(defvar fill-paragraph-function nil |
|---|
| 54 |
"Mode-specific function to fill a paragraph, or nil if there is none. |
|---|
| 55 |
If the function returns nil, then `fill-paragraph' does its normal work. |
|---|
| 56 |
A value of t means explicitly \"do nothing special\".") |
|---|
| 57 |
|
|---|
| 58 |
(defvar fill-paragraph-handle-comment t |
|---|
| 59 |
"Non-nil means paragraph filling will try to pay attention to comments.") |
|---|
| 60 |
|
|---|
| 61 |
(defcustom enable-kinsoku t |
|---|
| 62 |
"*Non-nil means enable \"kinsoku\" processing on filling paragraphs. |
|---|
| 63 |
Kinsoku processing is designed to prevent certain characters from being |
|---|
| 64 |
placed at the beginning or end of a line by filling. |
|---|
| 65 |
See the documentation of `kinsoku' for more information." |
|---|
| 66 |
:type 'boolean |
|---|
| 67 |
:group 'fill) |
|---|
| 68 |
|
|---|
| 69 |
(defun set-fill-prefix () |
|---|
| 70 |
"Set the fill prefix to the current line up to point. |
|---|
| 71 |
Filling expects lines to start with the fill prefix and |
|---|
| 72 |
reinserts the fill prefix in each resulting line." |
|---|
| 73 |
(interactive) |
|---|
| 74 |
(let ((left-margin-pos (save-excursion (move-to-left-margin) (point)))) |
|---|
| 75 |
(if (> (point) left-margin-pos) |
|---|
| 76 |
(progn |
|---|
| 77 |
(setq fill-prefix (buffer-substring left-margin-pos (point))) |
|---|
| 78 |
(if (equal fill-prefix "") |
|---|
| 79 |
(setq fill-prefix nil))) |
|---|
| 80 |
(setq fill-prefix nil))) |
|---|
| 81 |
(if fill-prefix |
|---|
| 82 |
(message "fill-prefix: \"%s\"" fill-prefix) |
|---|
| 83 |
(message "fill-prefix cancelled"))) |
|---|
| 84 |
|
|---|
| 85 |
(defcustom adaptive-fill-mode t |
|---|
| 86 |
"*Non-nil means determine a paragraph's fill prefix from its text." |
|---|
| 87 |
:type 'boolean |
|---|
| 88 |
:group 'fill) |
|---|
| 89 |
|
|---|
| 90 |
(defcustom adaptive-fill-regexp |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
"[ \t]*\\([-!|#%;>*,A7$,1s"s#sC$,2"F(B]+[ \t]*\\)*" |
|---|
| 95 |
"*Regexp to match text at start of line that constitutes indentation. |
|---|
| 96 |
If Adaptive Fill mode is enabled, a prefix matching this pattern |
|---|
| 97 |
on the first and second lines of a paragraph is used as the |
|---|
| 98 |
standard indentation for the whole paragraph. |
|---|
| 99 |
|
|---|
| 100 |
If the paragraph has just one line, the indentation is taken from that |
|---|
| 101 |
line, but in that case `adaptive-fill-first-line-regexp' also plays |
|---|
| 102 |
a role." |
|---|
| 103 |
:type 'regexp |
|---|
| 104 |
:group 'fill) |
|---|
| 105 |
|
|---|
| 106 |
(defcustom adaptive-fill-first-line-regexp "\\`[ \t]*\\'" |
|---|
| 107 |
"*Regexp specifying whether to set fill prefix from a one-line paragraph. |
|---|
| 108 |
When a paragraph has just one line, then after `adaptive-fill-regexp' |
|---|
| 109 |
finds the prefix at the beginning of the line, if it doesn't |
|---|
| 110 |
match this regexp, it is replaced with whitespace. |
|---|
| 111 |
|
|---|
| 112 |
By default, this regexp matches sequences of just spaces and tabs. |
|---|
| 113 |
|
|---|
| 114 |
However, we never use a prefix from a one-line paragraph |
|---|
| 115 |
if it would act as a paragraph-starter on the second line." |
|---|
| 116 |
:type 'regexp |
|---|
| 117 |
:group 'fill) |
|---|
| 118 |
|
|---|
| 119 |
(defcustom adaptive-fill-function nil |
|---|
| 120 |
"Function to call to choose a fill prefix for a paragraph, or nil. |
|---|
| 121 |
A nil value means the function has not determined the fill prefix." |
|---|
| 122 |
:type '(choice (const nil) function) |
|---|
| 123 |
:group 'fill) |
|---|
| 124 |
|
|---|
| 125 |
(defvar fill-indent-according-to-mode nil |
|---|
| 126 |
"Whether or not filling should try to use the major mode's indentation.") |
|---|
| 127 |
|
|---|
| 128 |
(defun current-fill-column () |
|---|
| 129 |
"Return the fill-column to use for this line. |
|---|
| 130 |
The fill-column to use for a buffer is stored in the variable `fill-column', |
|---|
| 131 |
but can be locally modified by the `right-margin' text property, which is |
|---|
| 132 |
subtracted from `fill-column'. |
|---|
| 133 |
|
|---|
| 134 |
The fill column to use for a line is the first column at which the column |
|---|
| 135 |
number equals or exceeds the local fill-column - right-margin difference." |
|---|
| 136 |
(save-excursion |
|---|
| 137 |
(if fill-column |
|---|
| 138 |
(let* ((here (progn (beginning-of-line) (point))) |
|---|
| 139 |
(here-col 0) |
|---|
| 140 |
(eol (progn (end-of-line) (point))) |
|---|
| 141 |
margin fill-col change col) |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
(while (and (setq margin (get-text-property here 'right-margin) |
|---|
| 145 |
fill-col (- fill-column (or margin 0)) |
|---|
| 146 |
change (text-property-not-all |
|---|
| 147 |
here eol 'right-margin margin)) |
|---|
| 148 |
(progn (goto-char (1- change)) |
|---|
| 149 |
(setq col (current-column)) |
|---|
| 150 |
(< col fill-col))) |
|---|
| 151 |
(setq here change |
|---|
| 152 |
here-col col)) |
|---|
| 153 |
(max here-col fill-col))))) |
|---|
| 154 |
|
|---|
| 155 |
(defun canonically-space-region (beg end) |
|---|
| 156 |
"Remove extra spaces between words in region. |
|---|
| 157 |
Leave one space between words, two at end of sentences or after colons |
|---|
| 158 |
\(depending on values of `sentence-end-double-space', `colon-double-space', |
|---|
| 159 |
and `sentence-end-without-period'). |
|---|
| 160 |
Remove indentation from each line." |
|---|
| 161 |
(interactive "*r") |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
(unless (markerp end) (setq end (copy-marker end t))) |
|---|
| 167 |
(let ((end-spc-re (concat "\\(" (sentence-end) "\\) *\\| +"))) |
|---|
| 168 |
(save-excursion |
|---|
| 169 |
(goto-char beg) |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
(subst-char-in-region beg end ?\t ?\s) |
|---|
| 175 |
(while (and (< (point) end) |
|---|
| 176 |
(re-search-forward end-spc-re end t)) |
|---|
| 177 |
(delete-region |
|---|
| 178 |
(cond |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
((and (match-end 1) (> (match-end 0) (match-end 1))) (match-end 1)) |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
((match-end 1) |
|---|
| 187 |
(min (match-end 0) |
|---|
| 188 |
(+ (if sentence-end-double-space 2 1) |
|---|
| 189 |
(save-excursion (goto-char (match-end 0)) |
|---|
| 190 |
(skip-chars-backward " ") |
|---|
| 191 |
(point))))) |
|---|
| 192 |
(t |
|---|
| 193 |
(+ (match-beginning 0) |
|---|
| 194 |
|
|---|
| 195 |
(save-excursion |
|---|
| 196 |
(skip-chars-backward " ]})\"'") |
|---|
| 197 |
(cond ((and sentence-end-double-space |
|---|
| 198 |
(or (memq (preceding-char) '(?. ?? ?!)) |
|---|
| 199 |
(and sentence-end-without-period |
|---|
| 200 |
(= (char-syntax (preceding-char)) ?w)))) 2) |
|---|
| 201 |
((and colon-double-space |
|---|
| 202 |
(= (preceding-char) ?:)) 2) |
|---|
| 203 |
((char-equal (preceding-char) ?\n) 0) |
|---|
| 204 |
(t 1)))))) |
|---|
| 205 |
(match-end 0)))))) |
|---|
| 206 |
|
|---|
| 207 |
(defun fill-common-string-prefix (s1 s2) |
|---|
| 208 |
"Return the longest common prefix of strings S1 and S2, or nil if none." |
|---|
| 209 |
(let ((cmp (compare-strings s1 nil nil s2 nil nil))) |
|---|
| 210 |
(if (eq cmp t) |
|---|
| 211 |
s1 |
|---|
| 212 |
(setq cmp (1- (abs cmp))) |
|---|
| 213 |
(unless (zerop cmp) |
|---|
| 214 |
(substring s1 0 cmp))))) |
|---|
| 215 |
|
|---|
| 216 |
(defun fill-match-adaptive-prefix () |
|---|
| 217 |
(let ((str (or |
|---|
| 218 |
(and adaptive-fill-function (funcall adaptive-fill-function)) |
|---|
| 219 |
(and adaptive-fill-regexp (looking-at adaptive-fill-regexp) |
|---|
| 220 |
(match-string-no-properties 0))))) |
|---|
| 221 |
(if (>= (+ (current-left-margin) (length str)) (current-fill-column)) |
|---|
| 222 |
|
|---|
| 223 |
nil |
|---|
| 224 |
str))) |
|---|
| 225 |
|
|---|
| 226 |
(defun fill-context-prefix (from to &optional first-line-regexp) |
|---|
| 227 |
"Compute a fill prefix from the text between FROM and TO. |
|---|
| 228 |
This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function' |
|---|
| 229 |
and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role; |
|---|
| 230 |
we reject a prefix based on a one-line paragraph if that prefix would |
|---|
| 231 |
act as a paragraph-separator." |
|---|
| 232 |
(or first-line-regexp |
|---|
| 233 |
(setq first-line-regexp adaptive-fill-first-line-regexp)) |
|---|
| 234 |
(save-excursion |
|---|
| 235 |
(goto-char from) |
|---|
| 236 |
(if (eolp) (forward-line 1)) |
|---|
| 237 |
|
|---|
| 238 |
(move-to-left-margin) |
|---|
| 239 |
(let (first-line-prefix |
|---|
| 240 |
|
|---|
| 241 |
second-line-prefix) |
|---|
| 242 |
(setq first-line-prefix |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
(fill-match-adaptive-prefix)) |
|---|
| 249 |
(forward-line 1) |
|---|
| 250 |
(if (< (point) to) |
|---|
| 251 |
(progn |
|---|
| 252 |
(move-to-left-margin) |
|---|
| 253 |
(setq second-line-prefix |
|---|
| 254 |
(cond ((looking-at paragraph-start) nil) |
|---|
| 255 |
(t (fill-match-adaptive-prefix)))) |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
(when second-line-prefix |
|---|
| 259 |
(unless first-line-prefix (setq first-line-prefix "")) |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
(let ((tmp second-line-prefix) |
|---|
| 265 |
(re "\\`")) |
|---|
| 266 |
(while (string-match "\\`[ \t]*\\([^ \t]+\\)" tmp) |
|---|
| 267 |
(setq re (concat re ".*" (regexp-quote (match-string 1 tmp)))) |
|---|
| 268 |
(setq tmp (substring tmp (match-end 0)))) |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
(if (string-match re first-line-prefix) |
|---|
| 272 |
second-line-prefix |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
(fill-common-string-prefix first-line-prefix |
|---|
| 277 |
second-line-prefix))))) |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
(if first-line-prefix |
|---|
| 282 |
(let ((result |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
(if (or (and first-line-regexp |
|---|
| 287 |
(string-match first-line-regexp |
|---|
| 288 |
first-line-prefix)) |
|---|
| 289 |
(and comment-start-skip |
|---|
| 290 |
(string-match comment-start-skip |
|---|
| 291 |
first-line-prefix))) |
|---|
| 292 |
first-line-prefix |
|---|
| 293 |
(make-string (string-width first-line-prefix) ?\s)))) |
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
(if (not (eq 0 (string-match paragraph-start |
|---|
| 297 |
(concat result "a")))) |
|---|
| 298 |
result))))))) |
|---|
| 299 |
|
|---|
| 300 |
(defun fill-single-word-nobreak-p () |
|---|
| 301 |
"Don't break a line after the first or before the last word of a sentence." |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
(or (looking-at (concat "[ \t]*\\sw+" "\\(?:" (sentence-end) "\\)[ \t]*$")) |
|---|
| 305 |
(save-excursion |
|---|
| 306 |
(skip-chars-backward " \t") |
|---|
| 307 |
(and (/= (skip-syntax-backward "w") 0) |
|---|
| 308 |
(/= (skip-chars-backward " \t") 0) |
|---|
| 309 |
(/= (skip-chars-backward ".?!:") 0) |
|---|
| 310 |
(looking-at (sentence-end)))))) |
|---|
| 311 |
|
|---|
| 312 |
(defun fill-french-nobreak-p () |
|---|
| 313 |
"Return nil if French style allows breaking the line at point. |
|---|
| 314 |
This is used in `fill-nobreak-predicate' to prevent breaking lines just |
|---|
| 315 |
after an opening paren or just before a closing paren or a punctuation |
|---|
| 316 |
mark such as `?' or `:'. It is common in French writing to put a space |
|---|
| 317 |
at such places, which would normally allow breaking the line at those |
|---|
| 318 |
places." |
|---|
| 319 |
(or (looking-at "[ \t]*[])},A;,b;(B?!;:-]") |
|---|
| 320 |
(save-excursion |
|---|
| 321 |
(skip-chars-backward " \t") |
|---|
| 322 |
(unless (bolp) |
|---|
| 323 |
(backward-char 1) |
|---|
| 324 |
(or (looking-at "[([{,A+,b+(B]") |
|---|
| 325 |
|
|---|
| 326 |
(and (memq (preceding-char) '(?\t ?\s)) |
|---|
| 327 |
(eq (char-syntax (following-char)) ?w))))))) |
|---|
| 328 |
|
|---|
| 329 |
(defcustom fill-nobreak-predicate nil |
|---|
| 330 |
"List of predicates for recognizing places not to break a line. |
|---|
| 331 |
The predicates are called with no arguments, with point at the place to |
|---|
| 332 |
be tested. If it returns t, fill commands do not break the line there." |
|---|
| 333 |
:group 'fill |
|---|
| 334 |
:type 'hook |
|---|
| 335 |
:options '(fill-french-nobreak-p fill-single-word-nobreak-p)) |
|---|
| 336 |
|
|---|
| 337 |
(defcustom fill-nobreak-invisible nil |
|---|
| 338 |
"Non-nil means that fill commands do not break lines in invisible text." |
|---|
| 339 |
:type 'boolean |
|---|
| 340 |
:group 'fill) |
|---|
| 341 |
|
|---|
| 342 |
(defun fill-nobreak-p () |
|---|
| 343 |
"Return nil if breaking the line at point is allowed. |
|---|
| 344 |
Can be customized with the variables `fill-nobreak-predicate' |
|---|
| 345 |
and `fill-nobreak-invisible'." |
|---|
| 346 |
(or |
|---|
| 347 |
(and fill-nobreak-invisible (line-move-invisible-p (point))) |
|---|
| 348 |
(unless (bolp) |
|---|
| 349 |
(or |
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
(and sentence-end-double-space |
|---|
| 357 |
(save-excursion |
|---|
| 358 |
(skip-chars-backward " ") |
|---|
| 359 |
(and (eq (preceding-char) ?.) |
|---|
| 360 |
(looking-at " \\([^ ]\\|$\\)")))) |
|---|
| 361 |
|
|---|
| 362 |
(save-excursion |
|---|
| 363 |
(skip-chars-backward " ") |
|---|
| 364 |
(and (eq (preceding-char) ?.) |
|---|
| 365 |
(not (progn (forward-char -1) (looking-at (sentence-end)))))) |
|---|
| 366 |
|
|---|
| 367 |
(unless use-hard-newlines |
|---|
| 368 |
(save-excursion |
|---|
| 369 |
(skip-chars-forward " \t") |
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
(and (not (eolp)) |
|---|
| 374 |
(looking-at paragraph-start)))) |
|---|
| 375 |
(run-hook-with-args-until-success 'fill-nobreak-predicate))))) |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
(dolist (pair '((katakana-jisx0201 . kinsoku) |
|---|
| 380 |
(chinese-gb2312 . kinsoku) |
|---|
| 381 |
(japanese-jisx0208 . kinsoku) |
|---|
| 382 |
(japanese-jisx0212 . kinsoku) |
|---|
| 383 |
(chinese-big5-1 . kinsoku) |
|---|
| 384 |
(chinese-big5-2 . kinsoku))) |
|---|
| 385 |
(put-charset-property (car pair) 'fill-find-break-point-function (cdr pair))) |
|---|
| 386 |
|
|---|
| 387 |
(defun fill-find-break-point (limit) |
|---|
| 388 |
"Move point to a proper line breaking position of the current line. |
|---|
| 389 |
Don't move back past the buffer position LIMIT. |
|---|
| 390 |
|
|---|
| 391 |
This function is called when we are going to break the current line |
|---|
| 392 |
after or before a non-ASCII character. If the charset of the |
|---|
| 393 |
character has the property `fill-find-break-point-function', this |
|---|
| 394 |
function calls the property value as a function with one arg LIMIT. |
|---|
| 395 |
If the charset has no such property, do nothing." |
|---|
| 396 |
(let* ((ch (following-char)) |
|---|
| 397 |
(charset (char-charset ch)) |
|---|
| 398 |
func) |
|---|
| 399 |
(if (eq charset 'ascii) |
|---|
| 400 |
(setq ch (preceding-char) |
|---|
| 401 |
charset (char-charset ch))) |
|---|
| 402 |
(if (charsetp charset) |
|---|
| 403 |
(setq func |
|---|
| 404 |
(get-charset-property charset 'fill-find-break-point-function))) |
|---|
| 405 |
(if (and func (fboundp func)) |
|---|
| 406 |
(funcall func limit)))) |
|---|
| 407 |
|
|---|
| 408 |
(defun fill-delete-prefix (from to prefix) |
|---|
| 409 |
"Delete the fill prefix from every line except the first. |
|---|
| 410 |
The first line may not even have a fill prefix. |
|---|
| 411 |
Point is moved to just past the fill prefix on the first line." |
|---|
| 412 |
(let ((fpre (if (and prefix (not (string-match "\\`[ \t]*\\'" prefix))) |
|---|
| 413 |
(concat "[ \t]*\\(" |
|---|
| 414 |
(replace-regexp-in-string |
|---|
| 415 |
"[ \t]+" "[ \t]*" |
|---|
| 416 |
(regexp-quote prefix)) |
|---|
| 417 |
"\\)?[ \t]*") |
|---|
| 418 |
"[ \t]*"))) |
|---|
| 419 |
(goto-char from) |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
(forward-line 1) |
|---|
| 425 |
(while (< (point) to) |
|---|
| 426 |
(if (looking-at fpre) |
|---|
| 427 |
(delete-region (point) (match-end 0))) |
|---|
| 428 |
(forward-line 1)) |
|---|
| 429 |
(goto-char from) |
|---|
| 430 |
(if (looking-at fpre) |
|---|
| 431 |
(goto-char (match-end 0))) |
|---|
| 432 |
(point))) |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
(add-to-list 'text-property-default-nonsticky '(fill-space . t)) |
|---|
| 439 |
|
|---|
| 440 |
(defun fill-delete-newlines (from to justify nosqueeze squeeze-after) |
|---|
| 441 |
(goto-char from) |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
(let ((eol-double-space-re |
|---|
| 445 |
(cond |
|---|
| 446 |
((not colon-double-space) (concat (sentence-end) "$")) |
|---|
| 447 |
|
|---|
| 448 |
((string-match "\\[[^][]*\\(\\.\\)[^][]*\\]" (sentence-end)) |
|---|
| 449 |
(concat (replace-match ".:" nil nil (sentence-end) 1) "$")) |
|---|
| 450 |
|
|---|
| 451 |
(t "[.?!:][])}\"']*$"))) |
|---|
| 452 |
(sentence-end-without-space-list |
|---|
| 453 |
(string-to-list sentence-end-without-space))) |
|---|
| 454 |
(while (re-search-forward eol-double-space-re to t) |
|---|
| 455 |
(or (>= (point) to) (memq (char-before) '(?\t ?\s)) |
|---|
| 456 |
(memq (char-after (match-beginning 0)) |
|---|
| 457 |
sentence-end-without-space-list) |
|---|
| 458 |
(insert-and-inherit ?\s)))) |
|---|
| 459 |
|
|---|
| 460 |
(goto-char from) |
|---|
| 461 |
(if enable-multibyte-characters |
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
(while (search-forward "\n" to t) |
|---|
| 472 |
(if (get-text-property (match-beginning 0) 'fill-space) |
|---|
| 473 |
(replace-match (get-text-property (match-beginning 0) 'fill-space)) |
|---|
| 474 |
(let ((prev (char-before (match-beginning 0))) |
|---|
| 475 |
(next (following-char))) |
|---|
| 476 |
(if (and (or (aref (char-category-set next) ?|) |
|---|
| 477 |
(aref (char-category-set prev) ?|)) |
|---|
| 478 |
(or (get-charset-property (char-charset prev) |
|---|
| 479 |
'nospace-between-words) |
|---|
| 480 |
(get-text-property (1- (match-beginning 0)) |
|---|
| 481 |
'nospace-between-words))) |
|---|
| 482 |
(delete-char -1)))))) |
|---|
| 483 |
|
|---|
| 484 |
(goto-char from) |
|---|
| 485 |
(skip-chars-forward " \t") |
|---|
| 486 |
|
|---|
| 487 |
(subst-char-in-region from to ?\n ?\s) |
|---|
| 488 |
(if (and nosqueeze (not (eq justify 'full))) |
|---|
| 489 |
nil |
|---|
| 490 |
(canonically-space-region (or squeeze-after (point)) to) |
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
(goto-char to) (delete-char (- (skip-chars-backward " \t")))) |
|---|
| 494 |
(goto-char from)) |
|---|
| 495 |
|
|---|
| 496 |
(defun fill-move-to-break-point (linebeg) |
|---|
| 497 |
"Move to the position where the line should be broken. |
|---|
| 498 |
The break position will be always after LINEBEG and generally before point." |
|---|
| 499 |
|
|---|
| 500 |
(if (> linebeg (point)) (goto-char linebeg)) |
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
(while |
|---|
| 508 |
(when (re-search-backward "[ \t]\\|\\c|.\\|.\\c|" linebeg 0) |
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
(forward-char 1) |
|---|
| 515 |
(when (fill-nobreak-p) (skip-chars-backward " \t" linebeg)))) |
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
(skip-chars-backward " \t") |
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
(if (>= linebeg (point)) |
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 |
(let ((to (line-end-position)) |
|---|
| 529 |
(first t)) |
|---|
| 530 |
(goto-char linebeg) |
|---|
| 531 |
(while (and (< (point) to) (or first (fill-nobreak-p))) |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
(skip-chars-forward " \t") |
|---|
| 535 |
(if (looking-at "\\c|") |
|---|
| 536 |
(forward-char 1) |
|---|
| 537 |
(let ((pos (save-excursion |
|---|
| 538 |
(skip-chars-forward "^ \n\t") |
|---|
| 539 |
(point)))) |
|---|
| 540 |
(if (re-search-forward "\\c|" pos t) |
|---|
| 541 |
(forward-char -1) |
|---|
| 542 |
(goto-char pos)))) |
|---|
| 543 |
(setq first nil))) |
|---|
| 544 |
|
|---|
| 545 |
(if enable-multibyte-characters |
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
(if (not (and (eq (charset-after (1- (point))) 'ascii) |
|---|
| 551 |
(eq (charset-after (point)) 'ascii))) |
|---|
| 552 |
|
|---|
| 553 |
(fill-find-break-point linebeg))))) |
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
(defun fill-text-properties-at (pos) |
|---|
| 557 |
(let ((l (text-properties-at pos)) |
|---|
| 558 |
prop-list) |
|---|
| 559 |
(while l |
|---|
| 560 |
(unless (eq (car l) 'composition) |
|---|
| 561 |
(setq prop-list |
|---|
| 562 |
(cons (car l) (cons (cadr l) prop-list)))) |
|---|
| 563 |
(setq l (cddr l))) |
|---|
| 564 |
prop-list)) |
|---|
| 565 |
|
|---|
| 566 |
(defun fill-newline () |
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
(skip-chars-backward " \t") |
|---|
| 570 |
(insert ?\n) |
|---|
| 571 |
|
|---|
| 572 |
(set-text-properties (1- (point)) (point) |
|---|
| 573 |
(fill-text-properties-at (point))) |
|---|
| 574 |
(and (looking-at "\\( [ \t]*\\)\\(\\c|\\)?") |
|---|
| 575 |
(or (aref (char-category-set (or (char-before (1- (point))) ?\000)) ?|) |
|---|
| 576 |
(match-end 2)) |
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
(put-text-property (1- (point)) (point) 'fill-space (match-string 1))) |
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 |
(if fill-nobreak-invisible |
|---|
| 584 |
(remove-text-properties (1- (point)) (point) |
|---|
| 585 |
'(invisible t))) |
|---|
| 586 |
(if (or fill-prefix |
|---|
| 587 |
(not fill-indent-according-to-mode)) |
|---|
| 588 |
(fill-indent-to-left-margin) |
|---|
| 589 |
(indent-according-to-mode)) |
|---|
| 590 |
|
|---|
| 591 |
(and fill-prefix (not (equal fill-prefix "")) |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
(insert-before-markers-and-inherit fill-prefix))) |
|---|
| 595 |
|
|---|
| 596 |
(defun fill-indent-to-left-margin () |
|---|
| 597 |
"Indent current line to the column given by `current-left-margin'." |
|---|
| 598 |
(let ((beg (point))) |
|---|
| 599 |
(indent-line-to (current-left-margin)) |
|---|
| 600 |
(put-text-property beg (point) 'face 'default))) |
|---|
| 601 |
|
|---|
| 602 |
(defun fill-region-as-paragraph (from to &optional justify |
|---|
| 603 |
nosqueeze squeeze-after) |
|---|
| 604 |
"Fill the region as one paragraph. |
|---|
| 605 |
It removes any paragraph breaks in the region and extra newlines at the end, |
|---|
| 606 |
indents and fills lines between the margins given by the |
|---|
| 607 |
`current-left-margin' and `current-fill-column' functions. |
|---|
| 608 |
\(In most cases, the variable `fill-column' controls the width.) |
|---|
| 609 |
It leaves point at the beginning of the line following the paragraph. |
|---|
| 610 |
|
|---|
| 611 |
Normally performs justification according to the `current-justification' |
|---|
| 612 |
function, but with a prefix arg, does full justification instead. |
|---|
| 613 |
|
|---|
| 614 |
From a program, optional third arg JUSTIFY can specify any type of |
|---|
| 615 |
justification. Fourth arg NOSQUEEZE non-nil means not to make spaces |
|---|
| 616 |
between words canonical before filling. Fifth arg SQUEEZE-AFTER, if non-nil, |
|---|
| 617 |
means don't canonicalize spaces before that position. |
|---|
| 618 |
|
|---|
| 619 |
Return the `fill-prefix' used for filling. |
|---|
| 620 |
|
|---|
| 621 |
If `sentence-end-double-space' is non-nil, then period followed by one |
|---|
| 622 |
space does not end a sentence, so don't break a line there." |
|---|
| 623 |
(interactive (progn |
|---|
| 624 |
(barf-if-buffer-read-only) |
|---|
| 625 |
(list (region-beginning) (region-end) |
|---|
| 626 |
(if current-prefix-arg 'full)))) |
|---|
| 627 |
(unless (memq justify '(t nil none full center left right)) |
|---|
| 628 |
(setq justify 'full)) |
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
(goto-char (min from to)) |
|---|
| 632 |
(setq to (max from to)) |
|---|
| 633 |
|
|---|
| 634 |
(skip-chars-forward " \t\n") |
|---|
| 635 |
|
|---|
| 636 |
(let ((from-plus-indent (point)) |
|---|
| 637 |
(oneleft nil)) |
|---|
| 638 |
|
|---|
| 639 |
(beginning-of-line) |
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
(setq from (max (point) from)) |
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
(goto-char to) |
|---|
| 650 |
(while (and (> (point) from) (eq ?\n (char-after (1- (point))))) |
|---|
| 651 |
(if (and oneleft |
|---|
| 652 |
(not (and use-hard-newlines |
|---|
| 653 |
(get-text-property (1- (point)) 'hard)))) |
|---|
| 654 |
(delete-backward-char 1) |
|---|
| 655 |
(backward-char 1) |
|---|
| 656 |
(setq oneleft t))) |
|---|
| 657 |
(setq to (copy-marker (point) t)) |
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
(goto-char from-plus-indent)) |
|---|
| 663 |
|
|---|
| 664 |
(if (not (> to (point))) |
|---|
| 665 |
nil |
|---|
| 666 |
|
|---|
| 667 |
(or justify (setq justify (current-justification))) |
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
(let ((fill-prefix fill-prefix)) |
|---|
| 671 |
|
|---|
| 672 |
(when (and adaptive-fill-mode |
|---|
| 673 |
(or (null fill-prefix) (string= fill-prefix ""))) |
|---|
| 674 |
(setq fill-prefix (fill-context-prefix from to)) |
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
(when (and fill-prefix fill-indent-according-to-mode |
|---|
| 678 |
(string-match "\\`[ \t]*\\'" fill-prefix)) |
|---|
| 679 |
(setq fill-prefix nil))) |
|---|
| 680 |
|
|---|
| 681 |
(goto-char from) |
|---|
| 682 |
(beginning-of-line) |
|---|
| 683 |
|
|---|
| 684 |
(if (not justify) |
|---|
| 685 |
(progn |
|---|
| 686 |
(goto-char from) |
|---|
| 687 |
(while (< (point) to) |
|---|
| 688 |
(if (and (not (eolp)) |
|---|
| 689 |
(< (current-indentation) (current-left-margin))) |
|---|
| 690 |
(fill-indent-to-left-margin)) |
|---|
| 691 |
(forward-line 1))) |
|---|
| 692 |
|
|---|
| 693 |
(if use-hard-newlines |
|---|
| 694 |
(remove-list-of-text-properties from to '(hard))) |
|---|
| 695 |
|
|---|
| 696 |
(if (or (memq justify '(right center)) |
|---|
| 697 |
(< (current-indentation) (current-left-margin))) |
|---|
| 698 |
(fill-indent-to-left-margin)) |
|---|
| 699 |
|
|---|
| 700 |
(fill-delete-prefix from to fill-prefix) |
|---|
| 701 |
(setq from (point)) |
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
(fill-delete-newlines from to justify nosqueeze squeeze-after) |
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
(goto-char from) |
|---|
| 710 |
(let (linebeg) |
|---|
| 711 |
(while (< (point) to) |
|---|
| 712 |
(setq linebeg (point)) |
|---|
| 713 |
(move-to-column (current-fill-column)) |
|---|
| 714 |
(if (when (< (point) to) |
|---|
| 715 |
|
|---|
| 716 |
(forward-char 1) |
|---|
| 717 |
(fill-move-to-break-point linebeg) |
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
(skip-chars-forward " \t") |
|---|
| 721 |
(< (point) to)) |
|---|
| 722 |
|
|---|
| 723 |
(progn |
|---|
| 724 |
(fill-newline) |
|---|
| 725 |
(when justify |
|---|
| 726 |
|
|---|
| 727 |
(save-excursion |
|---|
| 728 |
(forward-line -1) |
|---|
| 729 |
(justify-current-line justify nil t)))) |
|---|
| 730 |
|
|---|
| 731 |
(goto-char to) |
|---|
| 732 |
|
|---|
| 733 |
(if justify (justify-current-line justify t t)))))) |
|---|
| 734 |
|
|---|
| 735 |
(goto-char to) |
|---|
| 736 |
(unless (eobp) (forward-char 1)) |
|---|
| 737 |
|
|---|
| 738 |
fill-prefix))) |
|---|
| 739 |
|
|---|
| 740 |
(defsubst skip-line-prefix (prefix) |
|---|
| 741 |
"If point is inside the string PREFIX at the beginning of line, move past it." |
|---|
| 742 |
(when (and prefix |
|---|
| 743 |
(< (- (point) (line-beginning-position)) (length prefix)) |
|---|
| 744 |
(save-excursion |
|---|
| 745 |
(beginning-of-line) |
|---|
| 746 |
(looking-at (regexp-quote prefix)))) |
|---|
| 747 |
(goto-char (match-end 0)))) |
|---|
| 748 |
|
|---|
| 749 |
(defun fill-minibuffer-function (arg) |
|---|
| 750 |
"Fill a paragraph in the minibuffer, ignoring the prompt." |
|---|
| 751 |
(save-restriction |
|---|
| 752 |
(narrow-to-region (minibuffer-prompt-end) (point-max)) |
|---|
| 753 |
(fill-paragraph arg))) |
|---|
| 754 |
|
|---|
| 755 |
(defun fill-paragraph (arg) |
|---|
| 756 |
"Fill paragraph at or after point. |
|---|
| 757 |
|
|---|
| 758 |
If ARG is non-nil (interactively, with prefix argument), justify as well. |
|---|
| 759 |
If `sentence-end-double-space' is non-nil, then period followed by one |
|---|
| 760 |
|
|---|