From 3154cebb004da61e64ece4735803fa21485775d1 Mon Sep 17 00:00:00 2001 From: Carlo Sciolla Date: Fri, 3 May 2019 17:15:30 +0200 Subject: [PATCH] Avoid messing up with buffers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is yet another attempt at fixing #66, using a different approach than #93 By using `display-buffer` the preview buffer should stay wherever the user put it last without jumping around and without stealing focus from the source buffer. As an additional bonus, a temporary buffer is used to enable some sort of double buffering and avoid the flicker of seeing the preview buffer disappear --- plantuml-mode.el | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/plantuml-mode.el b/plantuml-mode.el index 2cf2cf6..c81a204 100644 --- a/plantuml-mode.el +++ b/plantuml-mode.el @@ -262,33 +262,35 @@ to choose where to display it: - 4 (when prefixing the command with C-u) -> new window - 16 (when prefixing the command with C-u C-u) -> new frame. - else -> new buffer" - (let ((b (get-buffer plantuml-preview-buffer))) - (when b - (kill-buffer b))) - (let* ((imagep (and (display-images-p) (plantuml-is-image-output-p))) (process-connection-type nil) - (buf (get-buffer-create plantuml-preview-buffer)) + (preview-buf (get-buffer-create plantuml-preview-buffer)) (coding-system-for-read (and imagep 'binary)) - (coding-system-for-write (and imagep 'binary))) - - (let ((ps (plantuml-start-process buf))) + (coding-system-for-write (and imagep 'binary)) + (temp-buffer (make-temp-name "__plantuml__"))) + (let ((ps (plantuml-start-process temp-buffer))) (process-send-string ps string) (process-send-eof ps) (set-process-sentinel ps (lambda (_ps event) (unless (equal event "finished\n") (error "PLANTUML Preview failed: %s" event)) - (cond - ((= prefix 16) - (switch-to-buffer-other-frame plantuml-preview-buffer)) - ((= prefix 4) - (switch-to-buffer-other-window plantuml-preview-buffer)) - (t (switch-to-buffer plantuml-preview-buffer))) - (when imagep - (image-mode) - (set-buffer-multibyte t))))))) + (with-current-buffer preview-buf + (let ((inhibit-read-only t)) + (fundamental-mode) ; prevent weird loops + (erase-buffer) + (replace-buffer-contents temp-buffer)) + (when imagep + (image-mode))) + (save-excursion + (cond + ((= prefix 16) + (switch-to-buffer-other-frame plantuml-preview-buffer)) + ((= prefix 4) + (switch-to-buffer-other-window plantuml-preview-buffer)) + (t (display-buffer plantuml-preview-buffer)))) + (kill-buffer temp-buffer)))))) (defun plantuml-preview-buffer (prefix) "Preview diagram from the PlantUML sources in the current buffer.