From 1f7e7363cf6776415251e1cd3e2f5c5b90366174 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Thu, 15 Jun 2017 12:57:12 +0100 Subject: [PATCH] Update :emacs sheet Sync with v1.5 of cheat-sh.el --- share/emacs.txt | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/share/emacs.txt b/share/emacs.txt index 7210224..6e630b3 100644 --- a/share/emacs.txt +++ b/share/emacs.txt @@ -2,7 +2,7 @@ ;; Copyright 2017 by Dave Pearson ;; Author: Dave Pearson -;; 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