Skip to content

Instantly share code, notes, and snippets.

@marsam
Created July 30, 2014 19:53
Show Gist options
  • Select an option

  • Save marsam/44744fcb109aba91b00d to your computer and use it in GitHub Desktop.

Select an option

Save marsam/44744fcb109aba91b00d to your computer and use it in GitHub Desktop.
Ansible documentation from Emacs
;;; ansibledoc.el --- Ansible documentation from Emacs -*- lexical-binding: t -*-
;; Author: Mario Rodas <[email protected]>
;; Version: 0.0.1
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Original idea: http://www.lunaryorn.com/2014/07/18/ansible-docs-in-emacs.html
;;; Code:
(require 'cl-lib)
(require 's)
(defgroup ansibledoc nil
"Ansible documentation from emacs."
:prefix "ansibledoc-"
:group 'applications)
(defcustom ansibledoc-executable (executable-find "ansible-doc")
"Ansible doc executable path."
:type 'string
:group 'ansibledoc)
(defconst ansibledoc-list-buffer "*Ansible modules list*"
"Ansible modules list buffer.")
(defconst ansibledoc-show-buffer "*Ansible Doc*"
"The Ansible Doc buffer.")
(defvar ansibledoc-list nil
"List of all Ansible modules.")
;;;###autoload
(defun ansibledoc-show (module)
"Show ansible doc for MODULE."
(interactive (list (ido-completing-read "Ansible Module: "
(mapcar 'first (ansibledoc-list))
nil nil nil nil
(symbol-name (symbol-at-point)))))
(with-current-buffer (get-buffer-create ansibledoc-show-buffer)
(setq buffer-read-only t)
(view-mode)
(let ((inhibit-read-only t))
(erase-buffer)
(call-process ansibledoc-executable nil t t module))
(goto-char (point-min))
(switch-to-buffer (current-buffer))))
(defun ansibledoc-list ()
"Return a list of ansible modules."
(unless ansibledoc-list
(let ((modlist (process-lines ansibledoc-executable "--list")))
(setq ansibledoc-list
(mapcar (lambda (line) (s-split-up-to (rx (one-or-more space)) line 1 t))
modlist))))
ansibledoc-list)
(defun ansibledoc--table-entries ()
"Make the table entries for `tabulated-list-mode'."
(mapcar (lambda (e)
(list (car e) (apply 'vector e)))
(ansibledoc-list)))
(define-derived-mode ansibledoc-mode tabulated-list-mode "Ansible documentation"
"Major mode for ansible documentation."
(setq tabulated-list-entries 'ansibledoc--table-entries)
(setq tabulated-list-format [("name" 20 nil) ("description" 60 nil)])
(define-key ansibledoc-mode-map (kbd "O") (lambda () (interactive) (ansibledoc-show (tabulated-list-get-id))))
(tabulated-list-init-header))
;;;###autoload
(defun ansibledoc()
"Show the list of ansible modules documentation."
(interactive)
(with-current-buffer (get-buffer-create ansibledoc-list-buffer)
(ansibledoc-mode)
(tabulated-list-print)
(switch-to-buffer (current-buffer))))
(provide 'ansibledoc)
;;; ansibledoc.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment