branch: elpa/systemd commit 37541b27ce04000eb4a596083ce0e1bae32e7914 Author: Mark Oteiza <mvote...@udel.edu> Commit: Mark Oteiza <mvote...@udel.edu>
add facility for browsing documentation --- systemd-mode.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/systemd-mode.el b/systemd-mode.el index 38a82ca..90ef1c8 100644 --- a/systemd-mode.el +++ b/systemd-mode.el @@ -22,10 +22,18 @@ ;;; Code: +(require 'thingatpt) +(require 'url-parse) + (defgroup systemd () "Major mode for editing systemd units." :group 'tools) +(defcustom systemd-browse-url-function 'eww + "Browser to use for HTTP(S) documentation." + :group 'systemd + :type '(choice function)) + (defcustom systemd-comment-start "#" "String to insert to start a new comment." :group 'systemd @@ -53,6 +61,48 @@ "Default expressions to highlight in `systemd-mode'. See systemd.unit(5) for details on unit file syntax.") +(defun systemd-get-value (start) + "Joins lines in the key value starting at buffer position START, +possibly broken by a backslash, and returns a string containing +the value." + (save-excursion + (let ((break "\\\\\n") + end) + (while (progn (goto-char (- (line-end-position) 1)) + (looking-at break)) + (forward-line)) + (setq end (line-end-position)) + (replace-regexp-in-string break " " (buffer-substring start end))))) + +(defun systemd-doc-find () + "Find the value of the unit's “Documentation” keys and return +as a list of strings, otherwise nil." + (let ((key "^Documentation=") + string) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward key nil t) + (setq string (concat string " " (systemd-get-value (point)))))) + (when string + (split-string string)))) + +(defun systemd-doc-open (url) + "Open URL. Interactively completes the documentation in the +current unit file, defaulting to the link under point, if any." + (interactive + (let* ((uri (thing-at-point-url-at-point)) + (prompt (concat "URL" + (when uri (format " (default %s)" uri)) + ": "))) + (list (completing-read prompt (systemd-doc-find) nil nil nil nil uri)))) + (let ((link (url-generic-parse-url url))) + (pcase (url-type link) + ("file" (find-file (url-filename link))) + ("man" (url-man link)) + ("info" (url-info link)) + ((or "http" "https") (apply systemd-browse-url-function `(,url))) + (_ (user-error "Invalid link"))))) + (defvar systemd-mode-syntax-table (let ((table (make-syntax-table))) (modify-syntax-entry ?\" ". " table) @@ -61,6 +111,12 @@ for details on unit file syntax.") table) "Syntax table used in `systemd-mode' buffers.") +(defvar systemd-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-c C-o") 'systemd-doc-open) + map) + "Keymap used in `systemd-mode' buffers.") + ;;;###autoload (add-to-list 'auto-mode-alist '("\\.automount\\'" . systemd-mode)) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.busname\\'" . systemd-mode)) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.mount\\'" . systemd-mode)) @@ -77,7 +133,10 @@ for details on unit file syntax.") "Major mode for editing systemd unit files. See http://www.freedesktop.org/wiki/Software/systemd/ for more information about systemd. The hook `systemd-mode-hook' is run -at mode initialization." +at mode initialization. + +Key bindings: +\\{systemd-mode-map}" (set-syntax-table systemd-mode-syntax-table) (setq-local comment-start systemd-comment-start) (setq-local font-lock-defaults '(systemd-font-lock-keywords))