Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run mode EXECUTABLE #102

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
- `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.

Expand Down
66 changes: 55 additions & 11 deletions plantuml-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
:type 'string
: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.")

(defconst plantuml-mode-version "1.3.1" "The plantuml-mode version string.")
Expand Down Expand Up @@ -118,12 +124,17 @@
:type 'string
: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
"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'"
:type 'symbol
:group 'plantuml
:options '(jar server))
:options '(jar server executable))

(defcustom plantuml-suppress-deprecation-warning t
"To silence the deprecation warning when `puml-mode' is found upon loading."
Expand Down Expand Up @@ -165,15 +176,15 @@
(defun plantuml-set-exec-mode (mode)
"Set the execution mode MODE for PlantUML."
(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)
supported-modes
nil
t
nil
nil
plantuml-exec-mode))))
(if (member mode '("jar" "server"))
supported-modes
nil
t
nil
nil
plantuml-exec-mode))))
(if (member mode '("jar" "server" "executable"))
(setq plantuml-exec-mode (intern mode))
(error (concat "Unsupported mode:" mode))))

Expand Down Expand Up @@ -244,11 +255,19 @@
(with-current-buffer buf
(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)
"Retrieve the language spec using the preferred PlantUML execution mode MODE. Paste the result into BUF."
(let ((get-fn (pcase mode
('jar #'plantuml-jar-get-language)
('server #'plantuml-server-get-language))))
('server #'plantuml-server-get-language)
('executable #'plantuml-executable-get-language))))
(if get-fn
(funcall get-fn buf)
(error "Unsupported execution mode %s" mode))))
Expand Down Expand Up @@ -345,6 +364,14 @@ Note that output type `txt' is promoted to `utxt' for better rendering."
,@plantuml-jar-args
"-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)
"Show the preview in the preview buffer BUF.
Window is selected according to PREFIX:
Expand Down Expand Up @@ -398,6 +425,22 @@ Put the result into buffer BUF and place it according to PREFIX:
(copy-to-buffer buf (point-min) (point-max))
(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)
"Preview the diagram from STRING using the execution mode MODE.
Put the result into buffer BUF, selecting the window according to PREFIX:
Expand All @@ -406,7 +449,8 @@ Put the result into buffer BUF, selecting the window according to PREFIX:
- else -> new buffer"
(let ((preview-fn (pcase mode
('jar #'plantuml-jar-preview-string)
('server #'plantuml-server-preview-string))))
('server #'plantuml-server-preview-string)
('executable #'plantuml-executable-preview-string))))
(if preview-fn
(funcall preview-fn prefix string buf)
(error "Unsupported execution mode %s" mode))))
Expand Down
2 changes: 2 additions & 0 deletions test/plantuml-config-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
(should (equal 'server plantuml-exec-mode))
(plantuml-set-exec-mode "jar")
(should (equal 'jar plantuml-exec-mode))
(plantuml-set-exec-mode "executable")
(should (equal 'executable plantuml-exec-mode))

(setq plantuml-exec-mode orig-mode)))

Expand Down