From 95bb255f823e1ce41acca8d574552d113e35643a Mon Sep 17 00:00:00 2001 From: Bjarte Johansen Date: Mon, 18 Oct 2021 13:20:00 +0200 Subject: [PATCH] Use replace-buffer-contents to update formatting blacken.el (blacken-buffer): Use replace-buffer-contents to update formatting after running black. This is potentially slower than using copy-to-buffer, but it keeps the position of point etc (if it can). blacken.el (blacken-replace-buffer-contents-max-secs): Add new variable to control how long to spend calculating replac-buffer-contents. --- blacken.el | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blacken.el b/blacken.el index 818caf5..b1360b1 100644 --- a/blacken.el +++ b/blacken.el @@ -80,6 +80,11 @@ If `fill', the `fill-column' variable value is used." :type 'boolean :safe 'booleanp) +(defcustom blacken-replace-buffer-contents-max-secs nil + "Max number of seconds for replacing buffer contents. See +`replace-buffer-contents' for more information." + :type 'float) + (defun blacken-call-bin (input-buffer output-buffer error-buffer) "Call process black. @@ -137,8 +142,6 @@ Return black process the exit code." Show black output, if black exit abnormally and DISPLAY is t." (interactive (list t)) (let* ((original-buffer (current-buffer)) - (original-point (point)) - (original-window-pos (window-start)) (tmpbuf (get-buffer-create "*blacken*")) (errbuf (get-buffer-create "*blacken-error*"))) ;; This buffer can be left after previous black invocation. It @@ -150,10 +153,7 @@ Show black output, if black exit abnormally and DISPLAY is t." (if (not (zerop (blacken-call-bin original-buffer tmpbuf errbuf))) (error "Black failed, see %s buffer for details" (buffer-name errbuf)) (unless (eq (compare-buffer-substrings tmpbuf nil nil original-buffer nil nil) 0) - (with-current-buffer tmpbuf - (copy-to-buffer original-buffer (point-min) (point-max))) - (goto-char original-point) - (set-window-start (selected-window) original-window-pos)) + (replace-buffer-contents tmpbuf blacken-replace-buffer-contents-max-secs)) (mapc 'kill-buffer (list tmpbuf errbuf))) (error (message "%s" (error-message-string err)) (when display