helper.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ;;; helper.el --- utility help package supporting help in electric modes
  2. ;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: K. Shane Hartman
  4. ;; Maintainer: FSF
  5. ;; Keywords: help
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Code:
  20. ;; hey, here's a helping hand.
  21. ;; Bind this to a string for <blank> in "... Other keys <blank>".
  22. ;; Helper-help uses this to construct help string when scrolling.
  23. ;; Defaults to "return"
  24. (defvar Helper-return-blurb nil)
  25. ;; Keymap implementation doesn't work too well for non-standard loops.
  26. ;; But define it anyway for those who can use it. Non-standard loops
  27. ;; will probably have to use Helper-help. You can't autoload the
  28. ;; keymap either.
  29. (defvar Helper-help-map nil)
  30. (if Helper-help-map
  31. nil
  32. (setq Helper-help-map (make-keymap))
  33. ;(fillarray Helper-help-map 'undefined)
  34. (define-key Helper-help-map "m" 'Helper-describe-mode)
  35. (define-key Helper-help-map "b" 'Helper-describe-bindings)
  36. (define-key Helper-help-map "c" 'Helper-describe-key-briefly)
  37. (define-key Helper-help-map "k" 'Helper-describe-key)
  38. ;(define-key Helper-help-map "f" 'Helper-describe-function)
  39. ;(define-key Helper-help-map "v" 'Helper-describe-variable)
  40. (define-key Helper-help-map "?" 'Helper-help-options)
  41. (define-key Helper-help-map (char-to-string help-char) 'Helper-help-options)
  42. (fset 'Helper-help-map Helper-help-map))
  43. (defun Helper-help-scroller ()
  44. (let ((blurb (or (and (boundp 'Helper-return-blurb)
  45. Helper-return-blurb)
  46. "return")))
  47. (save-window-excursion
  48. (goto-char (window-start (selected-window)))
  49. (if (get-buffer-window "*Help*")
  50. (pop-to-buffer "*Help*")
  51. (switch-to-buffer "*Help*"))
  52. (goto-char (point-min))
  53. (let ((continue t) state)
  54. (while continue
  55. (setq state (+ (* 2 (if (pos-visible-in-window-p (point-max)) 1 0))
  56. (if (pos-visible-in-window-p (point-min)) 1 0)))
  57. (message
  58. (nth state
  59. '("Space forward, Delete back. Other keys %s"
  60. "Space scrolls forward. Other keys %s"
  61. "Delete scrolls back. Other keys %s"
  62. "Type anything to %s"))
  63. blurb)
  64. (setq continue (read-event))
  65. (cond ((and (memq continue '(?\s ?\C-v)) (< state 2))
  66. (scroll-up))
  67. ((= continue ?\C-l)
  68. (recenter))
  69. ((and (= continue ?\177) (zerop (% state 2)))
  70. (scroll-down))
  71. (t (setq continue nil))))))))
  72. (defun Helper-help-options ()
  73. "Describe help options."
  74. (interactive)
  75. (message "c (key briefly), m (mode), k (key), b (bindings)")
  76. ;(message "c (key briefly), m (mode), k (key), v (variable), f (function)")
  77. (sit-for 4))
  78. (defun Helper-describe-key-briefly (key)
  79. "Briefly describe binding of KEY."
  80. (interactive "kDescribe key briefly: ")
  81. (describe-key-briefly key)
  82. (sit-for 4))
  83. (defun Helper-describe-key (key)
  84. "Describe binding of KEY."
  85. (interactive "kDescribe key: ")
  86. (save-window-excursion (describe-key key))
  87. (Helper-help-scroller))
  88. (defun Helper-describe-function ()
  89. "Describe a function. Name read interactively."
  90. (interactive)
  91. (save-window-excursion (call-interactively 'describe-function))
  92. (Helper-help-scroller))
  93. (defun Helper-describe-variable ()
  94. "Describe a variable. Name read interactively."
  95. (interactive)
  96. (save-window-excursion (call-interactively 'describe-variable))
  97. (Helper-help-scroller))
  98. (defun Helper-describe-mode ()
  99. "Describe the current mode."
  100. (interactive)
  101. (let ((name (format-mode-line mode-name))
  102. (documentation (documentation major-mode)))
  103. (with-current-buffer (get-buffer-create "*Help*")
  104. (setq buffer-read-only nil)
  105. (erase-buffer)
  106. (insert name " Mode\n" documentation)
  107. (help-mode)))
  108. (Helper-help-scroller))
  109. ;;;###autoload
  110. (defun Helper-describe-bindings ()
  111. "Describe local key bindings of current mode."
  112. (interactive)
  113. (message "Making binding list...")
  114. (save-window-excursion (describe-bindings))
  115. (Helper-help-scroller))
  116. ;;;###autoload
  117. (defun Helper-help ()
  118. "Provide help for current mode."
  119. (interactive)
  120. (let ((continue t) c)
  121. (while continue
  122. (message "Help (Type ? for further options)")
  123. (setq c (read-key-sequence nil))
  124. (setq c (lookup-key Helper-help-map c))
  125. (cond ((eq c 'Helper-help-options)
  126. (Helper-help-options))
  127. ((commandp c)
  128. (call-interactively c)
  129. (setq continue nil))
  130. (t
  131. (ding)
  132. (setq continue nil))))))
  133. (provide 'helper)
  134. ;;; helper.el ends here