skuro / plantuml-mode

A major mode for editing PlantUML sources in Emacs
GNU General Public License v3.0
511 stars 96 forks source link

Vertical split preview #134

Closed matthjes closed 3 years ago

matthjes commented 3 years ago

Hi,

I'm using Doom Emacs and whenever I'm opening the preview window it is shown with a horizontal split. Is it possible to configure the preview to open in a vertical split (side-by-side)? I've tried C-u C-c C-c to no avail, it always opens in a horizontal split.

mcraveiro commented 3 years ago

I have a somewhat similar problem, in that I want to have a the preview always displaying on an existing dedicated frame. I thought one way to achieve this would be to avoid the deletion and recreation of the preview buffer, but sadly I could not get it to work:

(defun plantuml-preview-string (prefix string)
  "Preview diagram from PlantUML sources (as STRING), using prefix (as PREFIX)
to choose where to display it."
  (let ((b (get-buffer plantuml-preview-buffer)))
    (when b
      (with-current-buffer b
        (let ((inhibit-read-only t))
          (erase-buffer)))))

  (let* ((imagep (and (display-images-p)
                      (plantuml-is-image-output-p)))
         (buf (get-buffer-create plantuml-preview-buffer))
         (coding-system-for-read (and imagep 'binary))
         (coding-system-for-write (and imagep 'binary)))
    (plantuml-exec-mode-preview-string prefix (plantuml-get-exec-mode) string buf)))

I end up with a blank buffer the second time round. If this worked, we could at least manually determine the screen arrangement and then keep it that way for the duration of the session... I'll keep hacking when I get more time, but any suggestions are welcome.

mcraveiro commented 3 years ago

Actually, I just bumped into something which could almost solve my problems, but unfortunately does not: previewing on another frame. This is great in that it does make the PlantUML preview go into a different frame, but the trouble is it recreates the frame every time you redo the preview. If only there was a way to keep the frame it would solve my problem.

mcraveiro commented 3 years ago

OK wow, this was rather painful but I finally got a setup that does what I want it to do. So first I applied the patch you see above [1]. This does the right thing in that it does not kill the preview buffer but instead erases its contents. However, for some reason (which may or may not be a bug in image-mode), once you do that the buffer will not display the SVG image. The contents of the buffer look perfectly fine, but it appears to render as a blank buffer. I think its just that you are not in the right position in the buffer, so I changed this method:

(defun plantuml-update-preview-buffer (prefix buf)
  "Show the preview in the preview buffer BUF.
Window is selected according to PREFIX:
- 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 ((imagep (and (display-images-p)
                     (plantuml-is-image-output-p))))
    (cond
     ((= prefix 16) (switch-to-buffer-other-frame buf))
     ((= prefix 4)  (switch-to-buffer-other-window buf))
     (t             (display-buffer buf)))
    (when imagep
      (with-current-buffer buf
        (image-mode)
        (goto-char (point-min))
        (set-buffer-multibyte t)))))

Note the (goto-char (point-min)) after calling image-mode. In addition, I also disabled the automatic image resizing on my config:

(setq image-auto-resize nil)

With all of this I now have an updating diagram. To scroll, I am using the horizontal keys [2]. I'm sure there is a "proper" way of solving this problem, but at least it solves my immediate needs.

HTH.

[1] https://github.com/skuro/plantuml-mode/issues/134#issuecomment-752638766 [2] https://www.gnu.org/software/emacs/manual/html_node/emacs/Horizontal-Scrolling.html

mcraveiro commented 3 years ago

Turns out I'm not the first person trying to solve this problem. Looking through the network graph, found the following solution by @mrtimdog, which may be better:

(defun plantuml-preview-string (prefix string)
  "Preview diagram from PlantUML sources (as STRING), using prefix (as PREFIX)
to choose where to display it."
  (let* ((imagep (and (display-images-p)
                      (plantuml-is-image-output-p)))
         (buf (get-buffer-create plantuml-preview-buffer))
         (coding-system-for-read (and imagep 'binary))
         (coding-system-for-write (and imagep 'binary)))
    (with-current-buffer buf
      (image-toggle-display-text)
      (erase-buffer))
    (plantuml-exec-mode-preview-string prefix (plantuml-get-exec-mode) string buf)))

I tested it locally and it seems to work. @skuro if I raise a PR for this change, would you accept it?

Thanks very much for your time, and thanks for a great package.

[1] https://github.com/mrtimdog/plantuml-mode/commit/ce4e3feaa8bf5a3c0771c6004a4c60d0d55a0aab

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.