1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-26 23:31:55 -08:00

Have `M-q' not mark buffers are changed when they haven't

* lisp/textmodes/fill.el (fill-paragraph): Use `buffer-hash'
to avoid marking buffers as changed when they haven't.
This commit is contained in:
Lars Magne Ingebrigtsen 2016-03-28 19:08:33 +02:00
parent 15357f6d1f
commit 742bf8243c

View file

@ -804,65 +804,75 @@ region, instead of just filling the current paragraph."
(interactive (progn (interactive (progn
(barf-if-buffer-read-only) (barf-if-buffer-read-only)
(list (if current-prefix-arg 'full) t))) (list (if current-prefix-arg 'full) t)))
(or (let ((hash (and (not (buffer-modified-p))
;; 1. Fill the region if it is active when called interactively. (buffer-hash))))
(and region transient-mark-mode mark-active (prog1
(not (eq (region-beginning) (region-end))) (or
(or (fill-region (region-beginning) (region-end) justify) t)) ;; 1. Fill the region if it is active when called interactively.
;; 2. Try fill-paragraph-function. (and region transient-mark-mode mark-active
(and (not (eq fill-paragraph-function t)) (not (eq (region-beginning) (region-end)))
(or fill-paragraph-function (or (fill-region (region-beginning) (region-end) justify) t))
(and (minibufferp (current-buffer)) ;; 2. Try fill-paragraph-function.
(= 1 (point-min)))) (and (not (eq fill-paragraph-function t))
(let ((function (or fill-paragraph-function (or fill-paragraph-function
;; In the minibuffer, don't count the width (and (minibufferp (current-buffer))
;; of the prompt. (= 1 (point-min))))
'fill-minibuffer-function)) (let ((function (or fill-paragraph-function
;; If fill-paragraph-function is set, it probably takes care ;; In the minibuffer, don't count
;; of comments and stuff. If not, it will have to set ;; the width of the prompt.
;; fill-paragraph-handle-comment back to t explicitly or 'fill-minibuffer-function))
;; return nil. ;; If fill-paragraph-function is set, it probably
(fill-paragraph-handle-comment nil) ;; takes care of comments and stuff. If not, it
(fill-paragraph-function t)) ;; will have to set fill-paragraph-handle-comment
(funcall function justify))) ;; back to t explicitly or return nil.
;; 3. Try our syntax-aware filling code. (fill-paragraph-handle-comment nil)
(and fill-paragraph-handle-comment (fill-paragraph-function t))
;; Our code only handles \n-terminated comments right now. (funcall function justify)))
comment-start (equal comment-end "") ;; 3. Try our syntax-aware filling code.
(let ((fill-paragraph-handle-comment nil)) (and fill-paragraph-handle-comment
(fill-comment-paragraph justify))) ;; Our code only handles \n-terminated comments right now.
;; 4. If it all fails, default to the good ol' text paragraph filling. comment-start (equal comment-end "")
(let ((before (point)) (let ((fill-paragraph-handle-comment nil))
(paragraph-start paragraph-start) (fill-comment-paragraph justify)))
;; Fill prefix used for filling the paragraph. ;; 4. If it all fails, default to the good ol' text paragraph filling.
fill-pfx) (let ((before (point))
;; Try to prevent code sections and comment sections from being (paragraph-start paragraph-start)
;; filled together. ;; Fill prefix used for filling the paragraph.
(when (and fill-paragraph-handle-comment comment-start-skip) fill-pfx)
(setq paragraph-start ;; Try to prevent code sections and comment sections from being
(concat paragraph-start "\\|[ \t]*\\(?:" ;; filled together.
comment-start-skip "\\)"))) (when (and fill-paragraph-handle-comment comment-start-skip)
(save-excursion (setq paragraph-start
;; To make sure the return value of forward-paragraph is meaningful, (concat paragraph-start "\\|[ \t]*\\(?:"
;; we have to start from the beginning of line, otherwise skipping comment-start-skip "\\)")))
;; past the last few chars of a paragraph-separator would count as (save-excursion
;; a paragraph (and not skipping any chars at EOB would not count ;; To make sure the return value of forward-paragraph is
;; as a paragraph even if it is). ;; meaningful, we have to start from the beginning of
(move-to-left-margin) ;; line, otherwise skipping past the last few chars of a
(if (not (zerop (fill-forward-paragraph 1))) ;; paragraph-separator would count as a paragraph (and
;; There's no paragraph at or after point: give up. ;; not skipping any chars at EOB would not count as a
(setq fill-pfx "") ;; paragraph even if it is).
(let ((end (point)) (move-to-left-margin)
(beg (progn (fill-forward-paragraph -1) (point)))) (if (not (zerop (fill-forward-paragraph 1)))
(goto-char before) ;; There's no paragraph at or after point: give up.
(setq fill-pfx (setq fill-pfx "")
(if use-hard-newlines (let ((end (point))
;; Can't use fill-region-as-paragraph, since this (beg (progn (fill-forward-paragraph -1) (point))))
;; paragraph may still contain hard newlines. See (goto-char before)
;; fill-region. (setq fill-pfx
(fill-region beg end justify) (if use-hard-newlines
(fill-region-as-paragraph beg end justify)))))) ;; Can't use fill-region-as-paragraph, since this
fill-pfx))) ;; paragraph may still contain hard newlines. See
;; fill-region.
(fill-region beg end justify)
(fill-region-as-paragraph beg end justify))))))
fill-pfx))
;; If we didn't change anything in the buffer (and the buffer
;; was previously unmodified), then flip the modification status
;; back to "unchanged".
(when (and hash
(equal hash (buffer-hash)))
(set-buffer-modified-p nil)))))
(declare-function comment-search-forward "newcomment" (limit &optional noerror)) (declare-function comment-search-forward "newcomment" (limit &optional noerror))
(declare-function comment-string-strip "newcomment" (str beforep afterp)) (declare-function comment-string-strip "newcomment" (str beforep afterp))