/
wid-edit+.el
341 lines (306 loc) · 12.4 KB
/
wid-edit+.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
;;; wid-edit+.el --- Extensions to standard library `wid-edit.el'.
;;
;; Filename: wid-edit+.el
;; Description: Extensions to standard library `wid-edit.el'.
;; Author: Drew Adams, Lennart Borgman
;; Maintainer: Drew Adams (concat "drew.adams" "@" "oracle" ".com")
;; Copyright (C) 2007-2021, Drew Adams, all rights reserved.
;; Created: Fri Dec 21 10:25:32 2007
;; Version: 0
;; Package-Requires: ()
;; Last-Updated: Sun Jan 17 12:57:56 2021 (-0800)
;; By: dradams
;; Update #: 294
;; URL: https://www.emacswiki.org/emacs/download/wid-edit%2b.el
;; Doc URL: https://emacswiki.org/emacs/UseCustomizeForKeyBindings
;; Keywords: widget, color
;; Compatibility: GNU Emacs: 20.x, 21.x, 22.x, 23.x, 24.x, 25.x, 26.x
;;
;; Features that might be required by this library:
;;
;; `cl-lib', `macroexp', `wid-edit'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Extensions to standard library `wid-edit.el'.
;;
;; New widgets defined here:
;;
;; `conditional-key-definition', `key-definition'.
;;
;; Functions defined here:
;;
;; `widgetp-color-match', `widgetp-color-validate',
;; `widgetp-define-key-from-key-def',
;; `widgetp-display-Completions', `widgetp-keyboard-quit',
;; `widgetp-key-def-set', `widgetp-remove-Completions',
;; `widgetp-rgb-hex-string-p'.
;;
;;
;; ***** NOTE: The following widgets defined in `wid-edit.el'
;; have been REDEFINED HERE:
;;
;; `color' - Added :match and :validate values.
;; Increased :size value to max color name length.
;;
;; `editable-field' - `C-g' also removes *Completions*.
;;
;;
;; ***** NOTE: The following functions defined in `wid-edit.el' have
;; been REDEFINED HERE:
;;
;; `widget-color-complete' - Made compatible with Emacs 20.
;; Don't use `facemenu-color-alist'.
;; Delete *Completions* window when done.
;; Keep focus in Customize frame.
;;
;; `widget-color-notify' - Remove *Completions* if color changes.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;; 2021/01/17 dadams
;; widgetp-color-match: Use (x-)color-defined-p.
;; color widget: Handle empty-list value of defined-colors (bug #45829).
;; widgetp-rgb-hex-string-p: Minor improvement for later Emacs releases.
;; widgetp-key-def-set:
;; Added missing widgetp- prefix to define-key-from-key-def
;; 2012/03/20 dadams
;; editable-field: Updated for Emacs 24: added :value-set entry.
;; 2008/05/11 dadams
;; Added: (conditional-)key-definition, widgetp-define-key-from-key-def,
;; widgetp-key-def-set.
;; 2008/03/28 dadams
;; widgetp-color-match: Ensure string and downcase before member test.
;; 2007/12/26 dadams
;; Redefined: widget-color-complete: show/remove Completions.
;; Redefined: editable-field: Bind C-g to also remove Completions.
;; Redefined: widget-color-notify: Remove Completions if color changes.
;; Redefined: widget-color-complete: Remove Completions if color changes.
;; Show Completions if complete prefix.
;; Added: widgetp-display-Completions, widgetp-remove-Completions,
;; widgetp-keyboard-quit.
;; Removed: widgetp-max-color-name-length.
;; 2007/12/22 dadams
;; Created.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 2, or
;; (at your option) any later version.
;;
;; This program 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'wid-edit)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; `editable-field' widget
;;;###autoload
(defun widgetp-keyboard-quit ()
"Same as `keyboard-quit', but also removes *Completions* window."
(interactive)
(widgetp-remove-Completions)
(keyboard-quit))
(define-key widget-field-keymap "\C-g" 'widgetp-keyboard-quit)
;;; REPLACE ORIGINAL `editable-field' in `wid-edit.el'.
;;; No change from standard definition.
;;; Redefine it just to pick up the keymap change (`C-g').
;;;
(define-widget 'editable-field 'default
"An editable-text field widget.
In an `editable-field' widget, `%v' must be preceded by some other
text in the `:format' string (if specified)."
:convert-widget 'widget-value-convert-widget
:keymap widget-field-keymap
:format "%v"
:help-echo "M-TAB: complete field; RET: enter value"
:value ""
:prompt-internal 'widget-field-prompt-internal
:prompt-history 'widget-field-history
:prompt-value 'widget-field-prompt-value
:action 'widget-field-action
:validate 'widget-field-validate
:valid-regexp ""
:error "Field's value doesn't match allowed forms"
:value-create 'widget-field-value-create
:value-set (if (fboundp 'widget-field-value-set)
'widget-field-value-set
'widget-default-value-set)
:value-delete 'widget-field-value-delete
:value-get 'widget-field-value-get
:match 'widget-field-match)
;;; `color' widget
(defun widgetp-color-match (_widget value)
"Non-nil if VALUE is a defined color or an RGB hex string.
\(`:match' function for `color' widget.\)"
(and (stringp value)
(or (if (fboundp 'color-defined-p) ; Emacs 22+
(color-defined-p value)
(x-color-defined-p value))
(widgetp-rgb-hex-string-p value))))
(defun widgetp-color-validate (widget)
"Return nil if WIDGET's value is a valid color value.
\(`:validate' function for `color' widget.\)"
(let ((value (widget-value widget)))
(unless (widgetp-color-match widget value)
(widget-put widget :error (format "Invalid color: `%S'" value))
widget)))
;;; REPLACE ORIGINAL `widget-color-notify' in `wid-edit.el'.
;;; Remove *Completions* window if color changes.
;;;
(defun widget-color-notify (widget child &optional event)
"Update WIDGET's sample, and notify its PARENT."
(let* ((ovly (widget-get widget :sample-overlay))
(old-color (overlay-get ovly 'face))
(new-color (widget-apply widget :sample-face-get)))
(unless (equal old-color new-color) (widgetp-remove-Completions))
(overlay-put ovly 'face new-color))
(widget-default-notify widget child event))
;;; REPLACE ORIGINAL `widget-color-complete' in `wid-edit.el'.
;;; Make compatible with older Emacs than 22 (`x-defined-colors' etc.)
;;; Don't use `facemenu-color-alist' or load `facemenu'.
;;; Delete *Completions* window when appropriate.
;;; Keep focus in Customize frame (not *Completions* frame).
;;;
(defun widget-color-complete (widget)
"Complete the color value in `color' widget WIDGET."
(let* ((prefix (buffer-substring-no-properties
(widget-field-start widget) (point)))
(colors (mapcar #'list (x-defined-colors)))
(completion (try-completion prefix colors)))
(cond ((eq completion t)
(widgetp-remove-Completions)
(message "Sole completion"))
((null completion)
(widgetp-remove-Completions)
(error "No completion for \"%s\"" prefix))
((not (string-equal prefix completion))
(insert-and-inherit (substring completion (length prefix)))
(message "Making completion list...")
(widgetp-display-Completions prefix colors)
(message "Complete but not unique"))
(t
(message "Making completion list...")
(widgetp-display-Completions prefix colors)
(message "Complete but not unique")))))
(defun widgetp-rgb-hex-string-p (string)
"Return non-nil if STRING is an RGB hex string #XXXXXXXXXXXX.
Each X is a hexadecimal digit. The number of Xs must be a multiple of
three, with the same number of Xs for each of red, green, and blue."
(let ((regx "^#\\(?:[[:xdigit:]]\\{3\\}\\)\\{1,4\\}$"))
(if (fboundp 'string-match-p) ; Emacs 23+
(string-match-p regx string)
(save-match-data
(string-match (if (> emacs-major-version 21) ; Emacs 22
regx
"^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$")
string)))))
;;; REPLACE ORIGINAL `color' widget in `wid-edit.el'.
;;; Base :size on longest color name.
;;; Add :match and :validate.
;;;
(define-widget 'color 'editable-field
"A color widget (with sample)."
:format "%{%t%}: %v (%{sample%})\n"
:size (1+ (apply #'max
13 ; Longest RGB hex string
(mapcar #'length (x-defined-colors))))
:tag "Color"
:match 'widgetp-color-match
:validate 'widgetp-color-validate
:value "black"
:complete 'widget-color-complete
:sample-face-get 'widget-color-sample-face-get
:notify 'widget-color-notify
:action 'widget-color-action)
;;; Key definition widgets
(define-widget 'conditional-key-definition 'lazy
"Conditional key definition.
A list of four components: KEYMAP, KEY, COMMAND, CONDITION, that
represents a binding of command COMMAND in keymap KEYMAP according to
KEY, if CONDITION evaluates to non-nil.
KEY is either a key sequence (string or vector) or a command.
COMMAND is a command.
CONDITION is a sexp.
If KEY is a command, then the binding represented is its remapping to
COMMAND."
:tag "Key Definition" :indent 1 :offset 0
:type
'(list
(restricted-sexp :tag "Keymap"
:match-alternatives ((lambda (x) (keymapp (eval x))))
:value global-map)
(choice
(key-sequence :tag "Key" :value [ignore])
(restricted-sexp :tag "Command to remap"
:match-alternatives (commandp) :value ignore))
(restricted-sexp :tag "Command"
:match-alternatives (commandp) :value ignore)
(sexp :tag "Condition")))
(define-widget 'key-definition 'lazy
"Key definition.
A list of three components: KEYMAP, KEY, COMMAND, that represents a
binding of command COMMAND in keymap KEYMAP according to KEY.
KEY is either a key sequence (string or vector) or a command.
If KEY is a command, then the binding represented is its remapping to
command COMMAND."
:tag "Key Definition" :indent 1 :offset 0
:type
'(list
(restricted-sexp :tag "Keymap"
:match-alternatives ((lambda (x) (keymapp (eval x))))
:value global-map)
(choice
(key-sequence :tag "Key" :value [ignore])
(restricted-sexp :tag "Command to remap"
:match-alternatives (commandp) :value ignore))
(restricted-sexp :tag "Command"
:match-alternatives (commandp) :value ignore)))
;;; Functions useful in :set for key-definition `defcustom's.
(defun widgetp-define-key-from-key-def (key-def)
"Define a key using a key-definition data structure
Argument KEY-DEF is a list of type `key-definition' or
`conditional-key-definition'."
(let ((map (eval (car key-def)))
(key (cadr key-def))
(command (caddr key-def))
(condition (cadddr key-def)))
(when (or (not condition) (eval condition))
(if (symbolp key)
(define-key map (vector 'remap key) command)
(define-key map key command)))))
(defun widgetp-key-def-set (var key-def)
"Set function for types `key-definition', `conditional-key-definition'."
(custom-set-default var key-def)
(widgetp-define-key-from-key-def key-def))
;;; Helper functions
(defun widgetp-remove-Completions ()
"Remove *Completions* window, if shown."
(when (get-buffer-window "*Completions*" 0)
(delete-window (get-buffer-window "*Completions*" 0))))
(defun widgetp-display-Completions (string table)
"Display matches of STRING against TABLE in *Completions* window.
Arguments are as for `all-completions'."
(let ((orig-frame (selected-frame)))
(with-output-to-temp-buffer "*Completions*"
(display-completion-list (all-completions string table nil)))
(select-frame-set-input-focus orig-frame)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(provide 'wid-edit+)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; wid-edit+.el ends here