-
Notifications
You must be signed in to change notification settings - Fork 2
/
guide-key-tip.el
175 lines (147 loc) · 6.31 KB
/
guide-key-tip.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
;;; guide-key-tip.el --- Show guide-key.el hints using pos-tip.el
;; Copyright (C) 2014 Hiroaki Otsu
;; Author: Hiroaki Otsu <[email protected]>
;; Keywords: help convenience Tooltip
;; URL: https://github.com/aki2o/guide-key-tip
;; Version: 0.0.1
;; Package-Requires: ((guide-key "1.2.3") (pos-tip "0.4.5"))
;; 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 file 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:
;;
;; This extension shows guide-key.el hints using pos-tip.el.
;;; Dependency:
;;
;; - guide-key.el ( see <https://github.com/kbkbkbkb1/guide-key> )
;; - pos-tip.el
;;; Installation:
;;
;; Put this to your load-path.
;; And put the following lines in your .emacs or site-start.el file.
;;
;; (require 'guide-key-tip)
;;; Configuration:
;;
;; ;; Make config suit for you. About the config item, see Customization or eval the following sexp.
;; ;; (customize-group "guide-key-tip")
;;
;; (setq guide-key-tip/enabled t)
;;; Customization:
;;
;; [EVAL] (autodoc-document-lisp-buffer :type 'user-variable :prefix "guide-key-tip/" :docstring t)
;; `guide-key-tip/enabled'
;; Whether enable to use pos-tip.el for `guide-key/popup-function'.
;;
;; *** END auto-documentation
;;; API:
;;
;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "guide-key-tip/" :docstring t)
;; `guide-key-tip/toggle-enable'
;; Toggle `guide-key-tip/enabled'.
;;
;; *** END auto-documentation
;; [Note] Functions and variables other than listed above, Those specifications may be changed without notice.
;;; Tested On:
;;
;; - Emacs ... GNU Emacs 24.3.1 (i686-pc-linux-gnu, GTK+ Version 3.4.2) of 2013-08-22 on chindi02, modified by Debian
;; - guide-key.el ... Version 1.2.3
;; - pos-tip.el ... Version 0.4.5
;; Enjoy!!!
(require 'cl-lib)
(require 'guide-key)
(require 'pos-tip)
(defgroup guide-key-tip nil
"Show guide-key.el hints using pos-tip.el."
:group 'guide-key
:prefix "guide-key-tip/")
(defcustom guide-key-tip/enabled nil
"Whether to use pos-tip.el for `guide-key/popup-function'."
:type 'boolean
:group 'guide-key-tip)
(defface guide-key-tip/pos-tip-face '((t (:bold t)))
"Face for the tip of pos-tip.el."
:group 'guide-key-tip)
(defun guide-key-tip--get-pos-tip-size (string)
"Return (WIDTH . HEIGHT) of the tip of pos-tip.el generated from STRING."
(let* ((w-h (pos-tip-string-width-height string))
(width (pos-tip-tooltip-width (car w-h) (frame-char-width)))
(height (pos-tip-tooltip-height (cdr w-h) (frame-char-height))))
(cons width height)))
(defun guide-key-tip--get-pos-tip-location ()
"Return (WND RIGHT BOTTOM) as the location to show the tip of pos-tip.el."
(let ((leftpt 0)
(toppt 0)
wnd rightpt bottompt)
(dolist (w (window-list))
(let* ((edges (when (not (minibufferp (window-buffer w)))
(window-pixel-edges w)))
(currleftpt (or (nth 0 edges) -1))
(currtoppt (or (nth 1 edges) -1)))
(when (and (= currleftpt 0)
(= currtoppt 0))
(setq wnd w))
(when (or (not rightpt)
(> currleftpt leftpt))
(setq rightpt (nth 2 edges))
(setq leftpt currleftpt))
(when (or (not bottompt)
(> currtoppt toppt))
(setq bottompt (nth 3 edges))
(setq toppt currtoppt))))
(list wnd rightpt bottompt)))
(defun guide-key-tip/pos-tip-show (&optional input)
"Popup function called after delay of `guide-key/idle-delay' seconds."
(if (or (not window-system)
(not (featurep 'pos-tip)))
(guide-key/popup-function input)
(let ((key-seq (or input (this-command-keys-vector)))
(dsc-buf (current-buffer)))
(cl-multiple-value-bind (wnd rightpt bottompt) (guide-key-tip--get-pos-tip-location)
(with-temp-buffer
(setq truncate-lines t) ; don't fold line
(setq indent-tabs-mode nil) ; don't use tab as white space
(text-scale-set guide-key/text-scale-amount)
(describe-buffer-bindings dsc-buf key-seq)
(when (> (guide-key/format-guide-buffer key-seq) 0)
(guide-key/turn-off-idle-timer)
(copy-face 'guide-key-tip/pos-tip-face 'pos-tip-temp)
(when (eq (face-attribute 'pos-tip-temp :font) 'unspecified)
(set-face-font 'pos-tip-temp (frame-parameter nil 'font)))
(set-face-bold 'pos-tip-temp (face-bold-p 'guide-key-tip/pos-tip-face))
(let* ((string (buffer-string))
(string (propertize string 'face 'pos-tip-temp))
(max-width (pos-tip-x-display-width))
(max-height (pos-tip-x-display-height))
(tipsize (guide-key-tip--get-pos-tip-size string))
(tipsize (cond ((or (> (car tipsize) max-width)
(> (cdr tipsize) max-height))
(setq string (pos-tip-truncate-string string max-width max-height))
(guide-key-tip--get-pos-tip-size string))
(t
tipsize)))
(tipwidth (car tipsize))
(tipheight (cdr tipsize))
(dx (- rightpt tipwidth 10))
(dy (- bottompt tipheight)))
(pos-tip-show-no-propertize
string 'pos-tip-temp 1 wnd 300 tipwidth tipheight nil dx dy))))))))
(defadvice guide-key/popup-function (around guide-key-tip activate)
(if guide-key-tip/enabled
(guide-key-tip/pos-tip-show (ad-get-arg 0))
ad-do-it))
;;;###autoload
(defun guide-key-tip/toggle-enable ()
"Toggle `guide-key-tip/enabled'."
(interactive)
(message "guide-key-tip/enabled is %s"
(setq guide-key-tip/enabled (not guide-key-tip/enabled))))
(provide 'guide-key-tip)
;;; guide-key-tip.el ends here