1
0
mirror of https://github.com/skuro/plantuml-mode.git synced 2024-06-21 06:56:44 +02:00

Merging pull request

This commit is contained in:
Carlo Sciolla 2015-12-07 17:06:19 +01:00
commit 2842aad15a

View File

@ -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 ;; Filename: puml-mode.el
;; Description: Major mode for PlantUML diagrams sources ;; Description: Major mode for PlantUML diagrams sources
@ -53,6 +53,8 @@
(defvar puml-mode-debug-enabled nil) (defvar puml-mode-debug-enabled nil)
(defvar puml-font-lock-keywords nil)
(defvar puml-mode-map (defvar puml-mode-map
(let ((keymap (make-keymap))) (let ((keymap (make-keymap)))
(define-key keymap (kbd "C-c C-c") 'puml-preview) (define-key keymap (kbd "C-c C-c") 'puml-preview)
@ -143,30 +145,45 @@
(defconst puml-preview-buffer "*PUML Preview*") (defconst puml-preview-buffer "*PUML Preview*")
(defun puml-output-type () (defvar puml-output-type
"Detects the best output type to use for generated diagrams." (if (not (display-images-p))
(cond ((image-type-available-p 'svg) 'svg) "utxt"
((image-type-available-p 'png) 'png) (cond ((image-type-available-p 'svg) "svg")
('utxt))) ((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 (as 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 () (defun puml-is-image-output-p ()
"Return true if the diagram output format is an image, false if it's text based." "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 () (defun puml-output-type-opt ()
"Create the flag to pass to PlantUML to produce the selected output format." "Create the flag to pass to PlantUML to produce the selected output format."
(let ((type (puml-output-type))) (concat "-t" puml-output-type))
(concat "-t" (symbol-name 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 () (defun puml-preview ()
"Preview diagram." "Preview diagram."
@ -174,10 +191,12 @@
(let ((b (get-buffer puml-preview-buffer))) (let ((b (get-buffer puml-preview-buffer)))
(when b (when b
(kill-buffer b))) (kill-buffer b)))
(let ((process-connection-type nil) (let* ((imagep (and (display-images-p)
(puml-is-image-output-p)))
(process-connection-type nil)
(buf (get-buffer-create puml-preview-buffer)) (buf (get-buffer-create puml-preview-buffer))
(coding-system-for-read 'binary) (coding-system-for-read (and imagep 'binary))
(coding-system-for-write 'binary) (coding-system-for-write (and imagep 'binary))
(command-params (shell-quote-argument puml-plantuml-jar-path))) (command-params (shell-quote-argument puml-plantuml-jar-path)))
(puml-debug "Executing:") (puml-debug "Executing:")
(puml-debug (concat "java -jar " command-params " " (puml-output-type-opt) " -p")) (puml-debug (concat "java -jar " command-params " " (puml-output-type-opt) " -p"))
@ -186,7 +205,14 @@
(puml-output-type-opt) "-p"))) (puml-output-type-opt) "-p")))
(process-send-region ps (point-min) (point-max)) (process-send-region ps (point-min) (point-max))
(process-send-eof ps) (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 (unless puml-plantuml-kwdList
(puml-init) (puml-init)
@ -254,6 +280,7 @@
Shortcuts Command Name Shortcuts Command Name
\\[puml-complete-symbol] `puml-complete-symbol'" \\[puml-complete-symbol] `puml-complete-symbol'"
(make-local-variable 'puml-output-type)
(setq font-lock-defaults '((puml-font-lock-keywords) nil t))) (setq font-lock-defaults '((puml-font-lock-keywords) nil t)))
(provide 'puml-mode) (provide 'puml-mode)