From 13fc1b1cbed79dc69c487cc752810032c44e503c Mon Sep 17 00:00:00 2001 From: David Vazquez Date: Sat, 26 Sep 2015 12:39:16 +0200 Subject: [PATCH 1/3] Customizable output type Define a variable `puml-output-type' that the user can customize per buffer. It does also define a `puml-set-output-type' command to set the output type for the current buffer interactively. --- puml-mode.el | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/puml-mode.el b/puml-mode.el index c54a78b..d4f30eb 100644 --- a/puml-mode.el +++ b/puml-mode.el @@ -123,20 +123,43 @@ (defconst puml-preview-buffer "*PUML Preview*") -(defun puml-output-type () - "Detects the best output type to use for generated diagrams." - (cond ((image-type-available-p 'svg) 'svg) - ((image-type-available-p 'png) 'png) - ('utxt))) +(defvar puml-output-type + (cond ((image-type-available-p 'svg) "svg") + ((image-type-available-p 'png) "png") + (t "utxt")) + "Specify the desired output type to use for generated diagrams.") + +(defun puml-read-output-type () + "Read from the minibuffer a output type." + (let* ((completion-ignore-case t) + (available-types + (append + (and (image-type-available-p 'svg) '("svg")) + (and (image-type-available-p 'png) '("png")) + '("utxt")))) + (completing-read (format "Output type [%s]: " puml-output-type) + available-types + nil + t + nil + nil + puml-output-type))) + +(defun puml-set-output-type (type) + "Set the desired output type for the current buffer. If the +major mode of the current buffer mode is not puml-mode, set the +default output type for new buffers." + (interactive (list (puml-read-output-type))) + (setq puml-output-type type)) + (defun puml-is-image-output-p () "Return true if the diagram output format is an image, false if it's text based." - (not (eq 'utxt (puml-output-type)))) + (not (equal "utxt" puml-output-type))) (defun puml-output-type-opt () "Create the flag to pass to PlantUML to produce the selected output format." - (let ((type (puml-output-type))) - (concat "-t" (symbol-name type)))) + (concat "-t" puml-output-type)) (defun puml-preview-sentinel (ps event) "For the PlantUML process (as PS) reacts on the termination event (as EVENT)." @@ -231,6 +254,7 @@ Shortcuts Command Name \\[puml-complete-symbol] `puml-complete-symbol'" + (make-local-variable 'puml-output-type) (setq font-lock-defaults '((puml-font-lock-keywords) nil t))) (provide 'puml-mode) From e886aff9c56d7d89144ce04ac766a21870afb675 Mon Sep 17 00:00:00 2001 From: David Vazquez Date: Sat, 26 Sep 2015 12:42:34 +0200 Subject: [PATCH 2/3] Use utxt as default output type if can't display images for example, if it is in terminal node (-nw) --- puml-mode.el | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/puml-mode.el b/puml-mode.el index d4f30eb..9cc2421 100644 --- a/puml-mode.el +++ b/puml-mode.el @@ -124,9 +124,11 @@ (defconst puml-preview-buffer "*PUML Preview*") (defvar puml-output-type - (cond ((image-type-available-p 'svg) "svg") - ((image-type-available-p 'png) "png") - (t "utxt")) + (if (not (display-images-p)) + "utxt" + (cond ((image-type-available-p 'svg) "svg") + ((image-type-available-p 'png) "png") + (t "utxt"))) "Specify the desired output type to use for generated diagrams.") (defun puml-read-output-type () From 7ff5e2bb3dcdfcadfbeb8b4619c95c5beb30c393 Mon Sep 17 00:00:00 2001 From: David Vazquez Date: Sat, 26 Sep 2015 13:05:00 +0200 Subject: [PATCH 3/3] Fix the output type UTXT Only set the coding for the output buffer of the process to binary if the output is an image. --- puml-mode.el | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/puml-mode.el b/puml-mode.el index 9cc2421..3a6a7fc 100644 --- a/puml-mode.el +++ b/puml-mode.el @@ -1,4 +1,4 @@ -;;; puml-mode.el --- Major mode for PlantUML +;;; puml-mode.el --- Major mode for PlantUML -*- lexical-binding: t; -*- ;; Filename: puml-mode.el ;; Description: Major mode for PlantUML diagrams sources @@ -163,32 +163,33 @@ default output type for new buffers." "Create the flag to pass to PlantUML to produce the selected output format." (concat "-t" puml-output-type)) -(defun puml-preview-sentinel (ps event) - "For the PlantUML process (as PS) reacts on the termination event (as EVENT)." - (if (equal event "finished\n") - (progn - (switch-to-buffer puml-preview-buffer) - (when (and (display-images-p) - (puml-is-image-output-p)) - (image-mode))) - (warn "PUML Preview failed: %s" event))) - (defun puml-preview () "Preview diagram." (interactive) (let ((b (get-buffer puml-preview-buffer))) (when b (kill-buffer b))) - (let ((process-connection-type nil) - (buf (get-buffer-create puml-preview-buffer)) - (coding-system-for-read 'binary) - (coding-system-for-write 'binary)) + + (let* ((imagep (and (display-images-p) + (puml-is-image-output-p))) + (process-connection-type nil) + (buf (get-buffer-create puml-preview-buffer)) + (coding-system-for-read (and imagep 'binary)) + (coding-system-for-write (and imagep 'binary))) + (let ((ps (start-process "PUML" buf "java" "-jar" (shell-quote-argument puml-plantuml-jar-path) (puml-output-type-opt) "-p"))) (process-send-region ps (point-min) (point-max)) (process-send-eof ps) - (set-process-sentinel ps 'puml-preview-sentinel)))) + (set-process-sentinel ps + (lambda (ps event) + (unless (equal event "finished\n") + (error "PUML Preview failed: %s" event)) + (switch-to-buffer puml-preview-buffer) + (when imagep + (image-mode) + (set-buffer-multibyte t))))))) (unless puml-plantuml-kwdList (puml-init)