helper.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;; helper - utility help package for modes which want to provide help
  2. ;; without relinquishing control, e.g. `electric' modes.
  3. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  4. ;; Principal author K. Shane Hartman
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is distributed in the hope that it will be useful,
  7. ;; but WITHOUT ANY WARRANTY. No author or distributor
  8. ;; accepts responsibility to anyone for the consequences of using it
  9. ;; or for whether it serves any particular purpose or works at all,
  10. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  11. ;; License for full details.
  12. ;; Everyone is granted permission to copy, modify and redistribute
  13. ;; GNU Emacs, but only under the conditions described in the
  14. ;; GNU Emacs General Public License. A copy of this license is
  15. ;; supposed to have been given to you along with GNU Emacs so you
  16. ;; can know your rights and responsibilities. It should be in a
  17. ;; file named COPYING. Among other things, the copyright notice
  18. ;; and this notice must be preserved on all copies.
  19. (provide 'helper) ; hey, here's a helping hand.
  20. ;; Bind this to a string for <blank> in "... Other keys <blank>".
  21. ;; Helper-help uses this to construct help string when scrolling.
  22. ;; Defaults to "return"
  23. (defvar Helper-return-blurb nil)
  24. ;; Keymap implementation doesn't work too well for non-standard loops.
  25. ;; But define it anyway for those who can use it. Non-standard loops
  26. ;; will probably have to use Helper-help. You can't autoload the
  27. ;; keymap either.
  28. (defvar Helper-help-map nil)
  29. (if Helper-help-map
  30. nil
  31. (setq Helper-help-map (make-keymap))
  32. ;(fillarray Helper-help-map 'undefined)
  33. (define-key Helper-help-map "m" 'Helper-describe-mode)
  34. (define-key Helper-help-map "b" 'Helper-describe-bindings)
  35. (define-key Helper-help-map "c" 'Helper-describe-key-briefly)
  36. (define-key Helper-help-map "k" 'Helper-describe-key)
  37. ;(define-key Helper-help-map "f" 'Helper-describe-function)
  38. ;(define-key Helper-help-map "v" 'Helper-describe-variable)
  39. (define-key Helper-help-map "?" 'Helper-help-options)
  40. (define-key Helper-help-map (char-to-string help-char) 'Helper-help-options)
  41. (fset 'Helper-help-map Helper-help-map))
  42. (defun Helper-help-scroller ()
  43. (let ((blurb (or (and (boundp 'Helper-return-blurb)
  44. Helper-return-blurb)
  45. "return")))
  46. (save-window-excursion
  47. (goto-char (window-start (selected-window)))
  48. (if (get-buffer-window "*Help*")
  49. (pop-to-buffer "*Help*")
  50. (switch-to-buffer "*Help*"))
  51. (goto-char (point-min))
  52. (let ((continue t) state)
  53. (while continue
  54. (setq state (+ (* 2 (if (pos-visible-in-window-p (point-max)) 1 0))
  55. (if (pos-visible-in-window-p (point-min)) 1 0)))
  56. (message
  57. (nth state
  58. '("Space forward, Delete back. Other keys %s"
  59. "Space scrolls forward. Other keys %s"
  60. "Delete scrolls back. Other keys %s"
  61. "Type anything to %s"))
  62. blurb)
  63. (setq continue (read-char))
  64. (cond ((and (memq continue '(?\ ?\C-v)) (< state 2))
  65. (scroll-up))
  66. ((= continue ?\C-l)
  67. (recenter))
  68. ((and (= continue ?\177) (zerop (% state 2)))
  69. (scroll-down))
  70. (t (setq continue nil))))))))
  71. (defun Helper-help-options ()
  72. "Describe help options."
  73. (interactive)
  74. (message "c (key briefly), m (mode), k (key), b (bindings)")
  75. ;(message "c (key briefly), m (mode), k (key), v (variable), f (function)")
  76. (sit-for 4))
  77. (defun Helper-describe-key-briefly (key)
  78. "Briefly describe binding of KEYS."
  79. (interactive "kDescribe key briefly: ")
  80. (describe-key-briefly key)
  81. (sit-for 4))
  82. (defun Helper-describe-key (key)
  83. "Describe binding of KEYS."
  84. (interactive "kDescribe key: ")
  85. (save-window-excursion (describe-key key))
  86. (Helper-help-scroller))
  87. (defun Helper-describe-function ()
  88. "Describe a function. Name read interactively."
  89. (interactive)
  90. (save-window-excursion (call-interactively 'describe-function))
  91. (Helper-help-scroller))
  92. (defun Helper-describe-variable ()
  93. "Describe a variable. Name read interactively."
  94. (interactive)
  95. (save-window-excursion (call-interactively 'describe-variable))
  96. (Helper-help-scroller))
  97. (defun Helper-describe-mode ()
  98. "Describe the current mode."
  99. (interactive)
  100. (let ((name mode-name)
  101. (documentation (documentation major-mode)))
  102. (save-excursion
  103. (set-buffer (get-buffer-create "*Help*"))
  104. (erase-buffer)
  105. (insert name " Mode\n" documentation)))
  106. (Helper-help-scroller))
  107. (defun Helper-describe-bindings ()
  108. "Describe current local key bindings."
  109. (interactive)
  110. (message "Making binding list...")
  111. (save-window-excursion (describe-bindings))
  112. (Helper-help-scroller))
  113. (defun Helper-help ()
  114. "Provide help for current mode."
  115. (interactive)
  116. (let ((continue t) c)
  117. (while continue
  118. (message "Help (Type ? for further options)")
  119. (setq c (char-to-string (upcase (read-char))))
  120. (setq c (lookup-key Helper-help-map c))
  121. (cond ((eq c 'Helper-help-options)
  122. (Helper-help-options))
  123. ((commandp c)
  124. (call-interactively c)
  125. (setq continue nil))
  126. (t
  127. (ding)
  128. (setq continue nil))))))