Added run mode EXECUTABLE (#102)

This commit is contained in:
Felix Weilbach 2019-08-19 14:10:35 +02:00 committed by Carlo Sciolla
parent 1e5f8beedd
commit 9cba6835fd
3 changed files with 58 additions and 11 deletions

View File

@ -97,6 +97,7 @@ As of `v1.3.0` support is added for switching execution mode. The following two
- `jar` (default) to run PlantUML as a local JAR file. This is the traditional system used by `plantuml-mode` - `jar` (default) to run PlantUML as a local JAR file. This is the traditional system used by `plantuml-mode`
- `server` (experimental) to let an instance of [`plantuml-server`](https://github.com/plantuml/plantuml-server) render the preview - `server` (experimental) to let an instance of [`plantuml-server`](https://github.com/plantuml/plantuml-server) render the preview
- `executable` to run PlantUML as a local executable file. This is useful if your package manager provides a executable for PlantUML.
You can customize `plantuml-default-exec-mode` or run `plantuml-set-exec-mode` from a `plantuml-mode` buffer to switch modes. You can customize `plantuml-default-exec-mode` or run `plantuml-set-exec-mode` from a `plantuml-mode` buffer to switch modes.

View File

@ -84,6 +84,12 @@
:type 'string :type 'string
:group 'plantuml) :group 'plantuml)
(defcustom plantuml-executable-path
"plantuml"
"The location of the PlantUML executable."
:type 'string
:group 'plantuml)
(defvar plantuml-mode-hook nil "Standard hook for plantuml-mode.") (defvar plantuml-mode-hook nil "Standard hook for plantuml-mode.")
(defconst plantuml-mode-version "1.3.1" "The plantuml-mode version string.") (defconst plantuml-mode-version "1.3.1" "The plantuml-mode version string.")
@ -118,12 +124,17 @@
:type 'string :type 'string
:group 'plantuml) :group 'plantuml)
(defcustom plantuml-executable-args (list "-headless")
"The parameters passed to plantuml executable when executing PlantUML."
:type '(repeat string)
:group 'plantuml)
(defcustom plantuml-default-exec-mode 'server (defcustom plantuml-default-exec-mode 'server
"Default execution mode for PlantUML. Valid values are: "Default execution mode for PlantUML. Valid values are:
- `jar': run PlantUML as a JAR file (requires a local install of the PlantUML JAR file, see `plantuml-jar-path'" - `jar': run PlantUML as a JAR file (requires a local install of the PlantUML JAR file, see `plantuml-jar-path'"
:type 'symbol :type 'symbol
:group 'plantuml :group 'plantuml
:options '(jar server)) :options '(jar server executable))
(defcustom plantuml-suppress-deprecation-warning t (defcustom plantuml-suppress-deprecation-warning t
"To silence the deprecation warning when `puml-mode' is found upon loading." "To silence the deprecation warning when `puml-mode' is found upon loading."
@ -165,15 +176,15 @@
(defun plantuml-set-exec-mode (mode) (defun plantuml-set-exec-mode (mode)
"Set the execution mode MODE for PlantUML." "Set the execution mode MODE for PlantUML."
(interactive (let* ((completion-ignore-case t) (interactive (let* ((completion-ignore-case t)
(supported-modes '("jar" "server"))) (supported-modes '("jar" "server" "executable")))
(list (completing-read (format "Exec mode [%s]: " plantuml-exec-mode) (list (completing-read (format "Exec mode [%s]: " plantuml-exec-mode)
supported-modes supported-modes
nil nil
t t
nil nil
nil nil
plantuml-exec-mode)))) plantuml-exec-mode))))
(if (member mode '("jar" "server")) (if (member mode '("jar" "server" "executable"))
(setq plantuml-exec-mode (intern mode)) (setq plantuml-exec-mode (intern mode))
(error (concat "Unsupported mode:" mode)))) (error (concat "Unsupported mode:" mode))))
@ -244,11 +255,19 @@
(with-current-buffer buf (with-current-buffer buf
(url-insert-file-contents lang-url)))) (url-insert-file-contents lang-url))))
(defun plantuml-executable-get-language (buf)
"Retrieve the language specification from the PlantUML executable and paste it into BUF."
(with-current-buffer buf
(let ((cmd-args (append (list plantuml-executable-path nil t nil) (list "-language"))))
(apply 'call-process cmd-args)
(goto-char (point-min)))))
(defun plantuml-get-language (mode buf) (defun plantuml-get-language (mode buf)
"Retrieve the language spec using the preferred PlantUML execution mode MODE. Paste the result into BUF." "Retrieve the language spec using the preferred PlantUML execution mode MODE. Paste the result into BUF."
(let ((get-fn (pcase mode (let ((get-fn (pcase mode
('jar #'plantuml-jar-get-language) ('jar #'plantuml-jar-get-language)
('server #'plantuml-server-get-language)))) ('server #'plantuml-server-get-language)
('executable #'plantuml-executable-get-language))))
(if get-fn (if get-fn
(funcall get-fn buf) (funcall get-fn buf)
(error "Unsupported execution mode %s" mode)))) (error "Unsupported execution mode %s" mode))))
@ -345,6 +364,14 @@ Note that output type `txt' is promoted to `utxt' for better rendering."
,@plantuml-jar-args ,@plantuml-jar-args
"-p"))) "-p")))
(defun plantuml-executable-start-process (buf)
"Run PlantUML as an Emacs process and puts the output into the given buffer (as BUF)."
(apply #'start-process
"PLANTUML" buf plantuml-executable-path
`(,@plantuml-executable-args
,(plantuml-jar-output-type-opt plantuml-output-type)
"-p")))
(defun plantuml-update-preview-buffer (prefix buf) (defun plantuml-update-preview-buffer (prefix buf)
"Show the preview in the preview buffer BUF. "Show the preview in the preview buffer BUF.
Window is selected according to PREFIX: Window is selected according to PREFIX:
@ -398,6 +425,22 @@ Put the result into buffer BUF and place it according to PREFIX:
(copy-to-buffer buf (point-min) (point-max)) (copy-to-buffer buf (point-min) (point-max))
(plantuml-update-preview-buffer prefix buf))))))) (plantuml-update-preview-buffer prefix buf)))))))
(defun plantuml-executable-preview-string (prefix string buf)
"Preview the diagram from STRING by running the PlantUML JAR.
Put the result into 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* ((process-connection-type nil)
(ps (plantuml-executable-start-process buf)))
(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))
(plantuml-update-preview-buffer prefix buf)))))
(defun plantuml-exec-mode-preview-string (prefix mode string buf) (defun plantuml-exec-mode-preview-string (prefix mode string buf)
"Preview the diagram from STRING using the execution mode MODE. "Preview the diagram from STRING using the execution mode MODE.
Put the result into buffer BUF, selecting the window according to PREFIX: Put the result into buffer BUF, selecting the window according to PREFIX:
@ -406,7 +449,8 @@ Put the result into buffer BUF, selecting the window according to PREFIX:
- else -> new buffer" - else -> new buffer"
(let ((preview-fn (pcase mode (let ((preview-fn (pcase mode
('jar #'plantuml-jar-preview-string) ('jar #'plantuml-jar-preview-string)
('server #'plantuml-server-preview-string)))) ('server #'plantuml-server-preview-string)
('executable #'plantuml-executable-preview-string))))
(if preview-fn (if preview-fn
(funcall preview-fn prefix string buf) (funcall preview-fn prefix string buf)
(error "Unsupported execution mode %s" mode)))) (error "Unsupported execution mode %s" mode))))

View File

@ -20,6 +20,8 @@
(should (equal 'server plantuml-exec-mode)) (should (equal 'server plantuml-exec-mode))
(plantuml-set-exec-mode "jar") (plantuml-set-exec-mode "jar")
(should (equal 'jar plantuml-exec-mode)) (should (equal 'jar plantuml-exec-mode))
(plantuml-set-exec-mode "executable")
(should (equal 'executable plantuml-exec-mode))
(setq plantuml-exec-mode orig-mode))) (setq plantuml-exec-mode orig-mode)))