comparison lisp/textmodes/fill.el @ 49599:5ade352e8d1c

Trailing whitespace deleted.
author Juanma Barranquero <lekktu@gmail.com>
date Tue, 04 Feb 2003 13:30:45 +0000
parents 7a843709dcda
children 329ac267f1fc d7ddb3e565de
comparison
equal deleted inserted replaced
49598:0d8b17d428b5 49599:5ade352e8d1c
195 (if (eq cmp t) 195 (if (eq cmp t)
196 s1 196 s1
197 (setq cmp (1- (abs cmp))) 197 (setq cmp (1- (abs cmp)))
198 (unless (zerop cmp) 198 (unless (zerop cmp)
199 (substring s1 0 cmp))))) 199 (substring s1 0 cmp)))))
200 200
201 (defun fill-context-prefix (from to &optional first-line-regexp) 201 (defun fill-context-prefix (from to &optional first-line-regexp)
202 "Compute a fill prefix from the text between FROM and TO. 202 "Compute a fill prefix from the text between FROM and TO.
203 This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function' 203 This uses the variables `adaptive-fill-regexp' and `adaptive-fill-function'
204 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role; 204 and `adaptive-fill-first-line-regexp'. `paragraph-start' also plays a role;
205 we reject a prefix based on a one-line paragraph if that prefix would 205 we reject a prefix based on a one-line paragraph if that prefix would
590 (let ((from-plus-indent (point)) 590 (let ((from-plus-indent (point))
591 (oneleft nil)) 591 (oneleft nil))
592 592
593 (beginning-of-line) 593 (beginning-of-line)
594 (setq from (point)) 594 (setq from (point))
595 595
596 ;; Delete all but one soft newline at end of region. 596 ;; Delete all but one soft newline at end of region.
597 ;; And leave TO before that one. 597 ;; And leave TO before that one.
598 (goto-char to) 598 (goto-char to)
599 (while (and (> (point) from) (eq ?\n (char-after (1- (point))))) 599 (while (and (> (point) from) (eq ?\n (char-after (1- (point)))))
600 (if (and oneleft 600 (if (and oneleft
647 (< (current-indentation) (current-left-margin))) 647 (< (current-indentation) (current-left-margin)))
648 (fill-indent-to-left-margin)) 648 (fill-indent-to-left-margin))
649 ;; Delete the fill-prefix from every line. 649 ;; Delete the fill-prefix from every line.
650 (fill-delete-prefix from to fill-prefix) 650 (fill-delete-prefix from to fill-prefix)
651 (setq from (point)) 651 (setq from (point))
652 652
653 ;; FROM, and point, are now before the text to fill, 653 ;; FROM, and point, are now before the text to fill,
654 ;; but after any fill prefix on the first line. 654 ;; but after any fill prefix on the first line.
655 655
656 (fill-delete-newlines from to justify nosqueeze squeeze-after) 656 (fill-delete-newlines from to justify nosqueeze squeeze-after)
657 657
776 (beginning-of-line) 776 (beginning-of-line)
777 (when (setq comstart (comment-search-forward (line-end-position) t)) 777 (when (setq comstart (comment-search-forward (line-end-position) t))
778 (setq comin (point)) 778 (setq comin (point))
779 (goto-char comstart) (skip-chars-backward " \t") 779 (goto-char comstart) (skip-chars-backward " \t")
780 (setq has-code-and-comment (not (bolp))))) 780 (setq has-code-and-comment (not (bolp)))))
781 781
782 (if (not comstart) 782 (if (not comstart)
783 ;; Return nil, so the normal filling will take place. 783 ;; Return nil, so the normal filling will take place.
784 nil 784 nil
785 785
786 ;; Narrow to include only the comment, and then fill the region. 786 ;; Narrow to include only the comment, and then fill the region.
940 (defun current-justification () 940 (defun current-justification ()
941 "How should we justify this line? 941 "How should we justify this line?
942 This returns the value of the text-property `justification', 942 This returns the value of the text-property `justification',
943 or the variable `default-justification' if there is no text-property. 943 or the variable `default-justification' if there is no text-property.
944 However, it returns nil rather than `none' to mean \"don't justify\"." 944 However, it returns nil rather than `none' to mean \"don't justify\"."
945 (let ((j (or (get-text-property 945 (let ((j (or (get-text-property
946 ;; Make sure we're looking at paragraph body. 946 ;; Make sure we're looking at paragraph body.
947 (save-excursion (skip-chars-forward " \t") 947 (save-excursion (skip-chars-forward " \t")
948 (if (and (eobp) (not (bobp))) 948 (if (and (eobp) (not (bobp)))
949 (1- (point)) (point))) 949 (1- (point)) (point)))
950 'justification) 950 'justification)
951 default-justification))) 951 default-justification)))
952 (if (eq 'none j) 952 (if (eq 'none j)
980 t)) 980 t))
981 (save-excursion 981 (save-excursion
982 (save-restriction 982 (save-restriction
983 (if whole-par 983 (if whole-par
984 (let ((paragraph-start (if use-hard-newlines "." paragraph-start)) 984 (let ((paragraph-start (if use-hard-newlines "." paragraph-start))
985 (paragraph-ignore-fill-prefix (if use-hard-newlines t 985 (paragraph-ignore-fill-prefix (if use-hard-newlines t
986 paragraph-ignore-fill-prefix))) 986 paragraph-ignore-fill-prefix)))
987 (goto-char begin) 987 (goto-char begin)
988 (while (and (bolp) (not (eobp))) (forward-char 1)) 988 (while (and (bolp) (not (eobp))) (forward-char 1))
989 (backward-paragraph) 989 (backward-paragraph)
990 (setq begin (point)) 990 (setq begin (point))
1037 (if mark-active (region-end) (point)))) 1037 (if mark-active (region-end) (point))))
1038 (set-justification b e 'center t)) 1038 (set-justification b e 'center t))
1039 1039
1040 ;; A line has up to six parts: 1040 ;; A line has up to six parts:
1041 ;; 1041 ;;
1042 ;; >>> hello. 1042 ;; >>> hello.
1043 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline] 1043 ;; [Indent-1][FP][ Indent-2 ][text][trailing whitespace][newline]
1044 ;; 1044 ;;
1045 ;; "Indent-1" is the left-margin indentation; normally it ends at column 1045 ;; "Indent-1" is the left-margin indentation; normally it ends at column
1046 ;; given by the `current-left-margin' function. 1046 ;; given by the `current-left-margin' function.
1047 ;; "FP" is the fill-prefix. It can be any string, including whitespace. 1047 ;; "FP" is the fill-prefix. It can be any string, including whitespace.
1049 ;; `center' or `right'. In `left' and `full' justification regions, any 1049 ;; `center' or `right'. In `left' and `full' justification regions, any
1050 ;; whitespace there is part of the line's text, and should not be changed. 1050 ;; whitespace there is part of the line's text, and should not be changed.
1051 ;; Trailing whitespace is not counted as part of the line length when 1051 ;; Trailing whitespace is not counted as part of the line length when
1052 ;; center- or right-justifying. 1052 ;; center- or right-justifying.
1053 ;; 1053 ;;
1054 ;; All parts of the line are optional, although the final newline can 1054 ;; All parts of the line are optional, although the final newline can
1055 ;; only be missing on the last line of the buffer. 1055 ;; only be missing on the last line of the buffer.
1056 1056
1057 (defun justify-current-line (&optional how eop nosqueeze) 1057 (defun justify-current-line (&optional how eop nosqueeze)
1058 "Do some kind of justification on this line. 1058 "Do some kind of justification on this line.
1059 Normally does full justification: adds spaces to the line to make it end at 1059 Normally does full justification: adds spaces to the line to make it end at
1060 the column given by `current-fill-column'. 1060 the column given by `current-fill-column'.
1061 Optional first argument HOW specifies alternate type of justification: 1061 Optional first argument HOW specifies alternate type of justification:
1062 it can be `left', `right', `full', `center', or `none'. 1062 it can be `left', `right', `full', `center', or `none'.
1063 If HOW is t, will justify however the `current-justification' function says to. 1063 If HOW is t, will justify however the `current-justification' function says to.
1064 If HOW is nil or missing, full justification is done by default. 1064 If HOW is nil or missing, full justification is done by default.
1065 Second arg EOP non-nil means that this is the last line of the paragraph, so 1065 Second arg EOP non-nil means that this is the last line of the paragraph, so
1066 it will not be stretched by full justification. 1066 it will not be stretched by full justification.
1067 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged, 1067 Third arg NOSQUEEZE non-nil means to leave interior whitespace unchanged,
1086 ; added between each words 1086 ; added between each words
1087 (curr-fracspace 0) ; current fractional space amount 1087 (curr-fracspace 0) ; current fractional space amount
1088 count) 1088 count)
1089 (end-of-line) 1089 (end-of-line)
1090 ;; Check if this is the last line of the paragraph. 1090 ;; Check if this is the last line of the paragraph.
1091 (if (and use-hard-newlines (null eop) 1091 (if (and use-hard-newlines (null eop)
1092 (get-text-property (point) 'hard)) 1092 (get-text-property (point) 'hard))
1093 (setq eop t)) 1093 (setq eop t))
1094 (skip-chars-backward " \t") 1094 (skip-chars-backward " \t")
1095 ;; Quick exit if it appears to be properly justified already 1095 ;; Quick exit if it appears to be properly justified already
1096 ;; or there is no text. 1096 ;; or there is no text.
1100 nil 1100 nil
1101 (setq end (point)) 1101 (setq end (point))
1102 (beginning-of-line) 1102 (beginning-of-line)
1103 (skip-chars-forward " \t") 1103 (skip-chars-forward " \t")
1104 ;; Skip over fill-prefix. 1104 ;; Skip over fill-prefix.
1105 (if (and fill-prefix 1105 (if (and fill-prefix
1106 (not (string-equal fill-prefix "")) 1106 (not (string-equal fill-prefix ""))
1107 (equal fill-prefix 1107 (equal fill-prefix
1108 (buffer-substring 1108 (buffer-substring
1109 (point) (min (point-max) (+ (length fill-prefix) 1109 (point) (min (point-max) (+ (length fill-prefix)
1110 (point)))))) 1110 (point))))))
1111 (forward-char (length fill-prefix)) 1111 (forward-char (length fill-prefix))
1112 (if (and adaptive-fill-mode 1112 (if (and adaptive-fill-mode
1113 (looking-at adaptive-fill-regexp)) 1113 (looking-at adaptive-fill-regexp))
1114 (goto-char (match-end 0)))) 1114 (goto-char (match-end 0))))
1115 (setq fp-end (point)) 1115 (setq fp-end (point))
1116 (skip-chars-forward " \t") 1116 (skip-chars-forward " \t")
1117 ;; This is beginning of the line's text. 1117 ;; This is beginning of the line's text.
1119 (setq beg (point)) 1119 (setq beg (point))
1120 (goto-char end) 1120 (goto-char end)
1121 (setq endcol (current-column)) 1121 (setq endcol (current-column))
1122 1122
1123 ;; HOW can't be null or left--we would have exited already 1123 ;; HOW can't be null or left--we would have exited already
1124 (cond ((eq 'right how) 1124 (cond ((eq 'right how)
1125 (setq ncols (- fc endcol)) 1125 (setq ncols (- fc endcol))
1126 (if (< ncols 0) 1126 (if (< ncols 0)
1127 ;; Need to remove some indentation 1127 ;; Need to remove some indentation
1128 (delete-region 1128 (delete-region
1129 (progn (goto-char fp-end) 1129 (progn (goto-char fp-end)
1130 (if (< (current-column) (+ indent ncols)) 1130 (if (< (current-column) (+ indent ncols))
1131 (move-to-column (+ indent ncols) t)) 1131 (move-to-column (+ indent ncols) t))
1132 (point)) 1132 (point))
1133 (progn (move-to-column indent) (point))) 1133 (progn (move-to-column indent) (point)))
1134 ;; Need to add some 1134 ;; Need to add some
1135 (goto-char beg) 1135 (goto-char beg)
1136 (indent-to (+ indent ncols)) 1136 (indent-to (+ indent ncols))
1137 ;; If point was at beginning of text, keep it there. 1137 ;; If point was at beginning of text, keep it there.
1138 (if (= beg pos) 1138 (if (= beg pos)
1139 (move-marker pos (point))))) 1139 (move-marker pos (point)))))
1140 1140
1141 ((eq 'center how) 1141 ((eq 'center how)
1142 ;; Figure out how much indentation is needed 1142 ;; Figure out how much indentation is needed
1143 (setq ncols (+ (current-left-margin) 1143 (setq ncols (+ (current-left-margin)
1202 (canonically-space-region (point) (line-end-position))) 1202 (canonically-space-region (point) (line-end-position)))
1203 ((memq justify '(center right)) 1203 ((memq justify '(center right))
1204 (save-excursion 1204 (save-excursion
1205 (move-to-left-margin nil t) 1205 (move-to-left-margin nil t)
1206 ;; Position ourselves after any fill-prefix. 1206 ;; Position ourselves after any fill-prefix.
1207 (if (and fill-prefix 1207 (if (and fill-prefix
1208 (not (string-equal fill-prefix "")) 1208 (not (string-equal fill-prefix ""))
1209 (equal fill-prefix 1209 (equal fill-prefix
1210 (buffer-substring 1210 (buffer-substring
1211 (point) (min (point-max) (+ (length fill-prefix) 1211 (point) (min (point-max) (+ (length fill-prefix)
1212 (point)))))) 1212 (point))))))
1213 (forward-char (length fill-prefix))) 1213 (forward-char (length fill-prefix)))
1214 (delete-region (point) (progn (skip-chars-forward " \t") 1214 (delete-region (point) (progn (skip-chars-forward " \t")
1215 (point)))))))) 1215 (point))))))))
1216 1216
1217 (defun unjustify-region (&optional begin end) 1217 (defun unjustify-region (&optional begin end)
1218 "Remove justification whitespace from region. 1218 "Remove justification whitespace from region.
1219 For centered or right-justified regions, this function removes any indentation 1219 For centered or right-justified regions, this function removes any indentation
1220 past the left margin from each line. For full-justified lines, it removes 1220 past the left margin from each line. For full-justified lines, it removes
1221 extra spaces between words. It does nothing in other justification modes. 1221 extra spaces between words. It does nothing in other justification modes.
1222 Arguments BEGIN and END are optional; default is the whole buffer." 1222 Arguments BEGIN and END are optional; default is the whole buffer."
1223 (save-excursion 1223 (save-excursion
1224 (save-restriction 1224 (save-restriction
1225 (if end (narrow-to-region (point-min) end)) 1225 (if end (narrow-to-region (point-min) end))
1251 (let ((fill-individual-varying-indent t)) 1251 (let ((fill-individual-varying-indent t))
1252 (fill-individual-paragraphs min max justifyp citation-regexp))) 1252 (fill-individual-paragraphs min max justifyp citation-regexp)))
1253 1253
1254 (defun fill-individual-paragraphs (min max &optional justify citation-regexp) 1254 (defun fill-individual-paragraphs (min max &optional justify citation-regexp)
1255 "Fill paragraphs of uniform indentation within the region. 1255 "Fill paragraphs of uniform indentation within the region.
1256 This command divides the region into \"paragraphs\", 1256 This command divides the region into \"paragraphs\",
1257 treating every change in indentation level or prefix as a paragraph boundary, 1257 treating every change in indentation level or prefix as a paragraph boundary,
1258 then fills each paragraph using its indentation level as the fill prefix. 1258 then fills each paragraph using its indentation level as the fill prefix.
1259 1259
1260 There is one special case where a change in indentation does not start 1260 There is one special case where a change in indentation does not start
1261 a new paragraph. This is for text of this form: 1261 a new paragraph. This is for text of this form:
1323 ;; Now stop the loop if end of paragraph. 1323 ;; Now stop the loop if end of paragraph.
1324 (and (not (eobp)) 1324 (and (not (eobp))
1325 (if fill-individual-varying-indent 1325 (if fill-individual-varying-indent
1326 ;; If this line is a separator line, with or 1326 ;; If this line is a separator line, with or
1327 ;; without prefix, end the paragraph. 1327 ;; without prefix, end the paragraph.
1328 (and 1328 (and
1329 (not (looking-at paragraph-separate)) 1329 (not (looking-at paragraph-separate))
1330 (save-excursion 1330 (save-excursion
1331 (not (and (looking-at fill-prefix-regexp) 1331 (not (and (looking-at fill-prefix-regexp)
1332 (progn (forward-char 1332 (progn (forward-char
1333 (length fill-prefix)) 1333 (length fill-prefix))
1337 ;; than the fill prefix wants, end the paragraph. 1337 ;; than the fill prefix wants, end the paragraph.
1338 (and (looking-at fill-prefix-regexp) 1338 (and (looking-at fill-prefix-regexp)
1339 ;; If fill prefix is shorter than a new 1339 ;; If fill prefix is shorter than a new
1340 ;; fill prefix computed here, end paragraph. 1340 ;; fill prefix computed here, end paragraph.
1341 (let ((this-line-fill-prefix 1341 (let ((this-line-fill-prefix
1342 (fill-individual-paragraphs-prefix 1342 (fill-individual-paragraphs-prefix
1343 citation-regexp))) 1343 citation-regexp)))
1344 (>= (length fill-prefix) 1344 (>= (length fill-prefix)
1345 (length this-line-fill-prefix))) 1345 (length this-line-fill-prefix)))
1346 (save-excursion 1346 (save-excursion
1347 (not (progn (forward-char 1347 (not (progn (forward-char
1348 (length fill-prefix)) 1348 (length fill-prefix))
1349 (or (looking-at "[ \t]") 1349 (or (looking-at "[ \t]")