Avoid messing up with buffers

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
This commit is contained in:
Carlo Sciolla 2019-05-03 17:15:30 +02:00
parent 2b84a2df52
commit 3154cebb00
No known key found for this signature in database
GPG Key ID: BA5D71E6F3C580C1
1 changed files with 19 additions and 17 deletions

View File

@ -262,33 +262,35 @@ to choose where to display it:
- 4 (when prefixing the command with C-u) -> new window - 4 (when prefixing the command with C-u) -> new window
- 16 (when prefixing the command with C-u C-u) -> new frame. - 16 (when prefixing the command with C-u C-u) -> new frame.
- else -> new buffer" - else -> new buffer"
(let ((b (get-buffer plantuml-preview-buffer)))
(when b
(kill-buffer b)))
(let* ((imagep (and (display-images-p) (let* ((imagep (and (display-images-p)
(plantuml-is-image-output-p))) (plantuml-is-image-output-p)))
(process-connection-type nil) (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-read (and imagep 'binary))
(coding-system-for-write (and imagep 'binary))) (coding-system-for-write (and imagep 'binary))
(temp-buffer (make-temp-name "__plantuml__")))
(let ((ps (plantuml-start-process buf))) (let ((ps (plantuml-start-process temp-buffer)))
(process-send-string ps string) (process-send-string ps string)
(process-send-eof ps) (process-send-eof ps)
(set-process-sentinel ps (set-process-sentinel ps
(lambda (_ps event) (lambda (_ps event)
(unless (equal event "finished\n") (unless (equal event "finished\n")
(error "PLANTUML Preview failed: %s" event)) (error "PLANTUML Preview failed: %s" event))
(cond (with-current-buffer preview-buf
((= prefix 16) (let ((inhibit-read-only t))
(switch-to-buffer-other-frame plantuml-preview-buffer)) (fundamental-mode) ; prevent weird loops
((= prefix 4) (erase-buffer)
(switch-to-buffer-other-window plantuml-preview-buffer)) (replace-buffer-contents temp-buffer))
(t (switch-to-buffer plantuml-preview-buffer))) (when imagep
(when imagep (image-mode)))
(image-mode) (save-excursion
(set-buffer-multibyte t))))))) (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) (defun plantuml-preview-buffer (prefix)
"Preview diagram from the PlantUML sources in the current buffer. "Preview diagram from the PlantUML sources in the current buffer.