+ (goto-char from)
+ (let ((column makefile-backslash-column)
+ (endmark (make-marker)))
+ (move-marker endmark to)
+ ;; Compute the smallest column number past the ends of all the lines.
+ (if makefile-backslash-align
+ (progn
+ (if (not delete-flag)
+ (while (< (point) to)
+ (end-of-line)
+ (if (= (preceding-char) ?\\)
+ (progn (forward-char -1)
+ (skip-chars-backward " \t")))
+ (setq column (max column (1+ (current-column))))
+ (forward-line 1)))
+ ;; Adjust upward to a tab column, if that doesn't push
+ ;; past the margin.
+ (if (> (% column tab-width) 0)
+ (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
+ tab-width)))
+ (if (< adjusted (window-width))
+ (setq column adjusted))))))
+ ;; Don't modify blank lines at start of region.
+ (goto-char from)
+ (while (and (< (point) endmark) (eolp))
+ (forward-line 1))
+ ;; Add or remove backslashes on all the lines.
+ (while (and (< (point) endmark)
+ ;; Don't backslashify the last line
+ ;; if the region ends right at the start of the next line.
+ (save-excursion
+ (forward-line 1)
+ (< (point) endmark)))
+ (if (not delete-flag)
+ (makefile-append-backslash column)
+ (makefile-delete-backslash))
+ (forward-line 1))
+ (move-marker endmark nil))))
+
+(defun makefile-append-backslash (column)
+ (end-of-line)
+ ;; Note that "\\\\" is needed to get one backslash.
+ (if (= (preceding-char) ?\\)
+ (progn (forward-char -1)
+ (delete-horizontal-space)
+ (indent-to column (if makefile-backslash-align nil 1)))
+ (indent-to column (if makefile-backslash-align nil 1))
+ (insert "\\")))
+
+(defun makefile-delete-backslash ()
+ (end-of-line)
+ (or (bolp)
+ (progn
+ (forward-char -1)
+ (if (looking-at "\\\\")
+ (delete-region (1+ (point))
+ (progn (skip-chars-backward " \t") (point)))))))
+
+\f
+
+;; Filling
+
+(defun makefile-fill-paragraph (arg)
+ ;; Fill comments, backslashed lines, and variable definitions
+ ;; specially.
+ (save-excursion
+ (beginning-of-line)
+ (cond
+ ((looking-at "^#+ ")
+ ;; Found a comment. Set the fill prefix, and find the paragraph
+ ;; boundaries by searching for lines that look like comment-only
+ ;; lines.
+ (let ((fill-prefix (match-string-no-properties 0))
+ (fill-paragraph-function nil))
+ (save-excursion
+ (save-restriction
+ (narrow-to-region
+ ;; Search backwards.
+ (save-excursion
+ (while (and (zerop (forward-line -1))
+ (looking-at "^#")))
+ ;; We may have gone too far. Go forward again.
+ (or (looking-at "^#")
+ (forward-line 1))
+ (point))
+ ;; Search forwards.
+ (save-excursion
+ (while (looking-at "^#")
+ (forward-line))
+ (point)))
+ (fill-paragraph nil)
+ t))))
+
+ ;; Must look for backslashed-region before looking for variable
+ ;; assignment.
+ ((or (eq (char-before (line-end-position 1)) ?\\)
+ (eq (char-before (line-end-position 0)) ?\\))
+ ;; A backslash region. Find beginning and end, remove
+ ;; backslashes, fill, and then reapply backslahes.
+ (end-of-line)
+ (let ((beginning
+ (save-excursion
+ (end-of-line 0)
+ (while (= (preceding-char) ?\\)
+ (end-of-line 0))
+ (forward-char)
+ (point)))
+ (end
+ (save-excursion
+ (while (= (preceding-char) ?\\)
+ (end-of-line 2))
+ (point))))
+ (save-restriction
+ (narrow-to-region beginning end)
+ (makefile-backslash-region (point-min) (point-max) t)
+ (let ((fill-paragraph-function nil))
+ (fill-paragraph nil))
+ (makefile-backslash-region (point-min) (point-max) nil)
+ (goto-char (point-max))
+ (if (< (skip-chars-backward "\n") 0)
+ (delete-region (point) (point-max))))))
+
+ ((looking-at makefile-macroassign-regex)
+ ;; Have a macro assign. Fill just this line, and then backslash
+ ;; resulting region.