Browse Source

`org-fill-paragraph' leaves buffer unmodified when doing nothing

* lisp/org.el (org-fill-paragraph): Leave buffer unmodified when
  nothing was filled.
* lisp/org-compat.el: Add forward compatibility with `buffer-hash'
  function.
Nicolas Goaziou 6 years ago
parent
commit
0dd2985509
2 changed files with 25 additions and 14 deletions
  1. 4 0
      lisp/org-compat.el
  2. 21 14
      lisp/org.el

+ 4 - 0
lisp/org-compat.el

@@ -81,6 +81,10 @@
     (defalias 'org-line-number-display-width 'line-number-display-width)
     (defalias 'org-line-number-display-width 'line-number-display-width)
   (defun org-line-number-display-width (&rest _) 0))
   (defun org-line-number-display-width (&rest _) 0))
 
 
+(if (fboundp 'buffer-hash)
+    (defalias 'org-buffer-hash 'buffer-hash)
+  (defun org-buffer-hash () (md5 (current-buffer))))
+
 
 
 ;;; Emacs < 25.1 compatibility
 ;;; Emacs < 25.1 compatibility
 
 

+ 21 - 14
lisp/org.el

@@ -22154,20 +22154,27 @@ filling the current element."
   (interactive (progn
   (interactive (progn
 		 (barf-if-buffer-read-only)
 		 (barf-if-buffer-read-only)
 		 (list (when current-prefix-arg 'full) t)))
 		 (list (when current-prefix-arg 'full) t)))
-  (cond
-   ((and region transient-mark-mode mark-active
-	 (not (eq (region-beginning) (region-end))))
-    (let ((origin (point-marker))
-	  (start (region-beginning)))
-      (unwind-protect
-	  (progn
-	    (goto-char (region-end))
-	    (while (> (point) start)
-	      (org-backward-paragraph)
-	      (org-fill-element justify)))
-	(goto-char origin)
-	(set-marker origin nil))))
-   (t (org-fill-element justify))))
+  (let ((hash (and (not (buffer-modified-p))
+		   (org-buffer-hash))))
+    (cond
+     ((and region transient-mark-mode mark-active
+	   (not (eq (region-beginning) (region-end))))
+      (let ((origin (point-marker))
+	    (start (region-beginning)))
+	(unwind-protect
+	    (progn
+	      (goto-char (region-end))
+	      (while (> (point) start)
+		(org-backward-paragraph)
+		(org-fill-element justify)))
+	  (goto-char origin)
+	  (set-marker origin nil))))
+     (t (org-fill-element justify)))
+    ;; 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 (org-buffer-hash)))
+      (set-buffer-modified-p nil))))
 
 
 (defun org-auto-fill-function ()
 (defun org-auto-fill-function ()
   "Auto-fill function."
   "Auto-fill function."