1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-12-03 03:24:22 +03:00

Update :emacs sheet

Sync with v1.5 of cheat-sh.el
This commit is contained in:
Dave Pearson 2017-06-15 12:57:12 +01:00
parent fb3bc455a8
commit 1f7e7363cf

View File

@ -2,7 +2,7 @@
;; Copyright 2017 by Dave Pearson <davep@davep.org>
;; Author: Dave Pearson <davep@davep.org>
;; Version: 1.3
;; Version: 1.5
;; Keywords: docs, help
;; URL: https://github.com/davep/cheat-sh.el
;; Package-Requires: ((emacs "24"))
@ -18,6 +18,20 @@
;;; Code:
(defgroup cheat-sh nil
"Interact with cheat.sh."
:group 'docs)
(defface cheat-sh-caption
'((t :inherit (bold font-lock-function-name-face)))
"Face used on captions in the cheat-sh output window."
:group 'cheat-sh)
(defcustom cheat-sh-list-timeout (* 60 60 4)
"Seconds to wait before deciding the cached sheet list is \"stale\"."
:type 'integer
:group 'cheat-sh)
(defconst cheat-sh-url "http://cheat.sh/%s?T"
"URL for cheat.sh.")
@ -44,15 +58,37 @@ text.")
(defvar cheat-sh-sheet-list nil
"List of all available sheets.")
(defvar cheat-sh-sheet-list-acquired nil
"The time when variable `cheat-sh-sheet-list' was populated.")
(defun cheat-sh-sheet-list-cache ()
"Return the list of sheets.
The list is cached in memory, and is considered \"stale\" and is
refreshed after `cheat-sh-list-timeout' seconds."
(when (and cheat-sh-sheet-list-acquired
(> (- (time-to-seconds) cheat-sh-sheet-list-acquired) cheat-sh-list-timeout))
(setq cheat-sh-sheet-list nil))
(or cheat-sh-sheet-list
(progn
(setq cheat-sh-sheet-list-acquired (time-to-seconds))
(setq cheat-sh-sheet-list (split-string (cheat-sh-get ":list") "\n")))))
(defun cheat-sh-read (prompt)
"Read input from the user, showing PROMPT to prompt them.
This function is used by some `interactive' functions in
cheat-sh.el to get the item to look up. It provides completion
based of the sheets that are available on cheat.sh."
(completing-read prompt
(or cheat-sh-sheet-list
(setq cheat-sh-sheet-list (split-string (cheat-sh-get ":list") "\n")))))
(completing-read prompt (cheat-sh-sheet-list-cache)))
(defun cheat-sh-decorate-results (buffer)
"Decorate BUFFER with properties to highlight results."
(with-current-buffer buffer
(save-excursion
(setf (point) (point-min))
(while (search-forward-regexp "^\\(#.*\\)$" nil t)
(replace-match (propertize (match-string 1) 'font-lock-face 'cheat-sh-caption) nil t)))))
;;;###autoload
(defun cheat-sh (thing)
@ -61,7 +97,8 @@ based of the sheets that are available on cheat.sh."
(let ((result (cheat-sh-get thing)))
(if result
(with-help-window "*cheat.sh*"
(princ result))
(princ result)
(cheat-sh-decorate-results standard-output))
(error "Can't find anything for %s on cheat.sh" thing))))
;;;###autoload