2
|
1 ;;; w3-forms.el --- Emacs-w3 forms parsing code for new display engine
|
0
|
2 ;; Author: wmperry
|
118
|
3 ;; Created: 1997/04/03 14:23:37
|
|
4 ;; Version: 1.84
|
0
|
5 ;; Keywords: faces, help, comm, data, languages
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
82
|
9 ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
|
0
|
10 ;;;
|
80
|
11 ;;; This file is part of GNU Emacs.
|
0
|
12 ;;;
|
|
13 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
14 ;;; it under the terms of the GNU General Public License as published by
|
|
15 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;;; any later version.
|
|
17 ;;;
|
|
18 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
19 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;;; GNU General Public License for more details.
|
|
22 ;;;
|
|
23 ;;; You should have received a copy of the GNU General Public License
|
80
|
24 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
25 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;;; Boston, MA 02111-1307, USA.
|
0
|
27 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
28
|
|
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
30 ;;; FORMS processing for html 2.0/3.0
|
|
31 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
98
|
32 (eval-when-compile
|
|
33 (require 'cl))
|
|
34
|
70
|
35 (eval-and-compile
|
80
|
36 (require 'w3-display)
|
98
|
37 (require 'widget)
|
108
|
38 (condition-case nil
|
|
39 (require 'wid-edit)
|
|
40 (error (require 'widget-edit))))
|
70
|
41
|
80
|
42 (require 'w3-vars)
|
|
43 (require 'mule-sysdp)
|
16
|
44
|
98
|
45 (defvar w3-form-use-old-style nil
|
|
46 "*Non-nil means use the old way of interacting for form fields.")
|
|
47
|
82
|
48 (define-widget-keywords :emacspeak-help :w3-form-data)
|
|
49
|
98
|
50 (defvar w3-form-keymap
|
|
51 (let ((map (copy-keymap global-map))
|
116
|
52 (eol-loc (where-is-internal 'end-of-line global-map t)))
|
98
|
53 (if widget-keymap
|
|
54 (cl-map-keymap (function
|
|
55 (lambda (key binding)
|
|
56 (define-key map
|
|
57 (if (vectorp key) key (vector key))
|
|
58 (case binding
|
|
59 (widget-backward 'w3-widget-backward)
|
|
60 (widget-forward 'w3-widget-forward)
|
|
61 (otherwise binding)))))
|
|
62 widget-keymap))
|
|
63 (define-key map [return] 'w3-form-maybe-submit-by-keypress)
|
|
64 (define-key map "\r" 'w3-form-maybe-submit-by-keypress)
|
|
65 (define-key map "\n" 'w3-form-maybe-submit-by-keypress)
|
|
66 (define-key map "\t" 'w3-widget-forward)
|
|
67 (define-key map "\C-k" 'widget-kill-line)
|
|
68 (define-key map "\C-a" 'widget-beginning-of-line)
|
|
69 (if eol-loc
|
|
70 (define-key map eol-loc 'widget-end-of-line))
|
|
71 map))
|
82
|
72
|
0
|
73 ;; A form entry area is a vector
|
86
|
74 ;; [ type name default-value value maxlength options widget plist]
|
0
|
75 ;; Where:
|
|
76 ;; type = symbol defining what type of form entry area it is
|
|
77 ;; (ie: file, radio)
|
|
78 ;; name = the name of the form element
|
|
79 ;; default-value = the value this started out with
|
|
80
|
|
81 (defsubst w3-form-element-type (obj) (aref obj 0))
|
|
82 (defsubst w3-form-element-name (obj) (aref obj 1))
|
|
83 (defsubst w3-form-element-default-value (obj) (aref obj 2))
|
|
84 (defsubst w3-form-element-value (obj) (aref obj 3))
|
|
85 (defsubst w3-form-element-size (obj) (aref obj 4))
|
|
86 (defsubst w3-form-element-maxlength (obj) (aref obj 5))
|
|
87 (defsubst w3-form-element-options (obj) (aref obj 6))
|
|
88 (defsubst w3-form-element-action (obj) (aref obj 7))
|
|
89 (defsubst w3-form-element-widget (obj) (aref obj 8))
|
86
|
90 (defsubst w3-form-element-plist (obj) (aref obj 9))
|
0
|
91
|
|
92 (defsubst w3-form-element-set-type (obj val) (aset obj 0 val))
|
|
93 (defsubst w3-form-element-set-name (obj val) (aset obj 1 val))
|
|
94 (defsubst w3-form-element-set-default-value (obj val) (aset obj 2 val))
|
|
95 (defsubst w3-form-element-set-value (obj val) (aset obj 3 val))
|
|
96 (defsubst w3-form-element-set-size (obj val) (aset obj 4 val))
|
|
97 (defsubst w3-form-element-set-maxlength (obj val) (aset obj 5 val))
|
|
98 (defsubst w3-form-element-set-options (obj val) (aset obj 6 val))
|
|
99 (defsubst w3-form-element-set-action (obj val) (aset obj 7 val))
|
|
100 (defsubst w3-form-element-set-widget (obj val) (aset obj 8 val))
|
86
|
101 (defsubst w3-form-element-set-plist (obj val) (aset obj 9 val))
|
0
|
102
|
116
|
103 (defvar w3-form-valid-key-sizes
|
|
104 '(
|
|
105 ("1024 (Premium)" . 1024)
|
|
106 ("896 (Regular)" . 896)
|
|
107 ("768 (Unleaded)" . 768)
|
|
108 ("512 (Low Grade)" . 512)
|
|
109 ("508 (Woos)" . 508)
|
|
110 ("256 (Test Grade)" . 256)
|
|
111 )
|
|
112 "An assoc list of available key sizes and meaningful descriptions.")
|
|
113
|
86
|
114 (defun w3-form-determine-size (el size)
|
|
115 (case (w3-form-element-type el)
|
|
116 (checkbox 3)
|
|
117 (radio 4)
|
|
118 ((reset submit) (+ 2 (length (or (w3-form-element-value el)
|
|
119 (symbol-name
|
|
120 (w3-form-element-type el))))))
|
|
121 (multiline 21)
|
|
122 (hidden nil)
|
|
123 (file (or size 26))
|
98
|
124 ((float password text int)
|
|
125 (if w3-form-use-old-style
|
|
126 (or size 22)
|
|
127 (or size 20)))
|
88
|
128 (image (+ 2 (length (or
|
|
129 (plist-get (w3-form-element-plist el) 'alt)
|
|
130 "Form-Image"))))
|
86
|
131 (option
|
98
|
132 (let ((options (copy-sequence (w3-form-element-options el))))
|
|
133 (or size
|
|
134 (length (caar (sort options
|
|
135 (function
|
|
136 (lambda (x y)
|
|
137 (>= (length (car x))
|
|
138 (length (car y)))))))))))
|
116
|
139 (keygen
|
|
140 (+ (length "Key Length: ")
|
|
141 (apply 'max
|
|
142 (mapcar (function (lambda (pair)
|
|
143 (length (car pair))))
|
|
144 w3-form-valid-key-sizes))))
|
86
|
145 (otherwise (or size 22))))
|
|
146
|
|
147 ;;###autoload
|
|
148 (defun w3-form-add-element (plist face)
|
|
149 (let* ((action (plist-get plist 'action))
|
|
150 (el (vector (plist-get plist 'type)
|
|
151 (plist-get plist 'name)
|
|
152 (plist-get plist 'default)
|
|
153 (plist-get plist 'value)
|
|
154 (plist-get plist 'size)
|
|
155 (plist-get plist 'maxlength)
|
|
156 (plist-get plist 'options)
|
|
157 action
|
|
158 nil
|
|
159 plist))
|
|
160 (size (w3-form-determine-size el (plist-get plist 'size)))
|
82
|
161 (node (assoc action w3-form-elements)))
|
118
|
162 (if (not (assq '*table-autolayout w3-display-open-element-stack))
|
|
163 (if node
|
|
164 (setcdr node (cons el (cdr node)))
|
|
165 (setq w3-form-elements (cons (cons action (list el))
|
|
166 w3-form-elements))))
|
80
|
167 (if size
|
|
168 (set-text-properties (point)
|
|
169 (progn (insert-char ?T size) (point))
|
98
|
170 (list 'w3-form-info (cons el face)
|
80
|
171 'start-open t
|
|
172 'end-open t
|
|
173 'rear-nonsticky t)))))
|
|
174
|
|
175 (defun w3-form-resurrect-widgets ()
|
|
176 (let ((st (point-min))
|
98
|
177 ;; FIXME! For some reason this loses on long lines right now.
|
|
178 (widget-push-button-gui nil)
|
|
179 info nd node action face)
|
80
|
180 (while st
|
|
181 (if (setq info (get-text-property st 'w3-form-info))
|
|
182 (progn
|
82
|
183 (setq nd (or (next-single-property-change st 'w3-form-info)
|
|
184 (point-max))
|
98
|
185 face (cdr info)
|
|
186 info (car info)
|
80
|
187 action (w3-form-element-action info)
|
|
188 node (assoc action w3-form-elements))
|
|
189 (goto-char st)
|
|
190 (delete-region st nd)
|
|
191 (if (not (w3-form-element-size info))
|
|
192 (w3-form-element-set-size info 20))
|
98
|
193 (w3-form-add-element-internal info face)
|
80
|
194 (setq st (next-single-property-change st 'w3-form-info)))
|
|
195 (setq st (next-single-property-change st 'w3-form-info))))))
|
|
196
|
82
|
197 (defsubst w3-form-mark-widget (widget el)
|
|
198 (let ((widgets (list widget))
|
|
199 (children (widget-get widget :children))
|
|
200 (parent (widget-get widget :parent)))
|
|
201 (w3-form-element-set-widget el widget)
|
|
202 ;; Get _all_ the children associated with this widget
|
|
203 (while children
|
|
204 (setq widgets (cons (car children) widgets))
|
|
205 (if (widget-get (car children) :children)
|
|
206 (setq children (append children
|
|
207 (widget-get (car children) :children))))
|
|
208 (setq children (cdr children)))
|
|
209 (while (widget-get widget :parent)
|
|
210 (setq widget (widget-get widget :parent)
|
|
211 widgets (cons widget widgets)))
|
|
212 (setq children (widget-get widget :buttons))
|
|
213 ;; Special case for radio buttons
|
|
214 (while children
|
|
215 (setq widgets (cons (car children) widgets))
|
|
216 (if (widget-get (car children) :children)
|
|
217 (setq children (append children
|
|
218 (widget-get (car children) :children))))
|
|
219 (setq children (cdr children)))
|
|
220 (while widgets
|
|
221 (setq widget (pop widgets))
|
|
222 (widget-put widget :emacspeak-help 'w3-form-summarize-field)
|
98
|
223 (widget-put widget :help-echo 'w3-form-summarize-field)
|
82
|
224 (widget-put widget :w3-form-data el))))
|
|
225
|
98
|
226 (defun w3-form-add-element-internal (el face)
|
0
|
227 (let* ((widget nil)
|
|
228 (buffer-read-only nil)
|
|
229 (inhibit-read-only t)
|
80
|
230 (widget-creation-function nil))
|
|
231 (setq widget-creation-function (or (get (w3-form-element-type el)
|
0
|
232 'w3-widget-creation-function)
|
|
233 'w3-form-default-widget-creator)
|
82
|
234 widget (and (fboundp widget-creation-function)
|
98
|
235 (funcall widget-creation-function el face)))
|
0
|
236 (if (not widget)
|
|
237 nil
|
82
|
238 (w3-form-mark-widget widget el))))
|
0
|
239
|
|
240 ;; These properties tell the add-element function how to actually create
|
|
241 ;; each type of widget.
|
|
242 (put 'checkbox 'w3-widget-creation-function 'w3-form-create-checkbox)
|
|
243 (put 'multiline 'w3-widget-creation-function 'w3-form-create-multiline)
|
|
244 (put 'radio 'w3-widget-creation-function 'w3-form-create-radio-button)
|
|
245 (put 'reset 'w3-widget-creation-function 'w3-form-create-submit-button)
|
|
246 (put 'submit 'w3-widget-creation-function 'w3-form-create-submit-button)
|
|
247 (put 'hidden 'w3-widget-creation-function 'ignore)
|
|
248 (put 'file 'w3-widget-creation-function 'w3-form-create-file-browser)
|
|
249 (put 'option 'w3-widget-creation-function 'w3-form-create-option-list)
|
|
250 (put 'keygen 'w3-widget-creation-function 'w3-form-create-keygen-list)
|
|
251 (put 'button 'w3-widget-creation-function 'w3-form-create-button)
|
|
252 (put 'image 'w3-widget-creation-function 'w3-form-create-image)
|
82
|
253 (put 'int 'w3-widget-creation-function 'w3-form-create-integer)
|
|
254 (put 'float 'w3-widget-creation-function 'w3-form-create-float)
|
86
|
255 (put 'custom 'w3-widget-creation-function 'w3-form-create-custom)
|
|
256 (put 'text 'w3-widget-creation-function 'w3-form-create-text)
|
88
|
257 (put 'password 'w3-widget-creation-function 'w3-form-create-password)
|
86
|
258
|
|
259 ;; Custom support.
|
|
260 (defvar w3-custom-options nil)
|
|
261 (make-variable-buffer-local 'w3-custom-options)
|
|
262
|
|
263 (defun w3-form-create-custom (el face)
|
114
|
264 (condition-case ()
|
|
265 (require 'cus-edit)
|
|
266 (error (require 'custom-edit)))
|
86
|
267 (let* ((name (w3-form-element-name el))
|
|
268 (var-name (w3-form-element-value el))
|
|
269 (type (plist-get (w3-form-element-plist el) 'custom-type))
|
|
270 (widget (widget-create (cond ((string-equal type "variable")
|
|
271 'custom-variable)
|
|
272 ((string-equal type "face")
|
|
273 'custom-face)
|
|
274 ((string-equal type "group")
|
|
275 'custom-group)
|
|
276 (t 'item)) (intern var-name))))
|
|
277 (custom-magic-reset widget)
|
|
278 (push widget w3-custom-options)
|
|
279 widget))
|
0
|
280
|
|
281 (defun w3-form-create-checkbox (el face)
|
82
|
282 (widget-create 'checkbox
|
98
|
283 :button-face face
|
0
|
284 (and (w3-form-element-default-value el) t)))
|
|
285
|
82
|
286 (defun w3-form-radio-button-update (widget child event)
|
|
287 (widget-radio-action widget child event)
|
|
288 (w3-form-mark-widget widget (widget-get widget :w3-form-data)))
|
|
289
|
0
|
290 (defun w3-form-create-radio-button (el face)
|
|
291 (let* ((name (w3-form-element-name el))
|
82
|
292 (action (w3-form-element-action el))
|
|
293 (uniqid (cons name action))
|
|
294 (formobj (cdr (assoc uniqid w3-form-radio-elements)))
|
0
|
295 (widget nil)
|
|
296 )
|
|
297 (if formobj
|
|
298 (progn
|
|
299 (setq widget (w3-form-element-widget formobj))
|
2
|
300 (widget-radio-add-item widget
|
|
301 (list 'item
|
118
|
302 :button-face face
|
2
|
303 :format "%t"
|
|
304 :tag ""
|
|
305 :value (w3-form-element-value el)))
|
82
|
306 (w3-form-mark-widget widget el)
|
2
|
307 (if (w3-form-element-default-value el)
|
82
|
308 (progn
|
|
309 (widget-put widget 'w3-form-default-value
|
|
310 (w3-form-element-value el))
|
|
311 (widget-value-set widget (w3-form-element-value el))))
|
0
|
312 nil)
|
82
|
313 (setq widget (widget-create
|
|
314 'radio-button-choice
|
|
315 :value (w3-form-element-value el)
|
|
316 :action 'w3-form-radio-button-update
|
|
317 (list 'item
|
118
|
318 :button-face face
|
82
|
319 :format "%t"
|
|
320 :tag ""
|
|
321 :value (w3-form-element-value el)))
|
|
322 w3-form-radio-elements (cons (cons uniqid el)
|
0
|
323 w3-form-radio-elements))
|
82
|
324 (widget-put widget 'w3-form-default-value (w3-form-element-value el))
|
0
|
325 widget)))
|
|
326
|
|
327 (defun w3-form-create-button (el face)
|
|
328 ;; This handles dealing with the bogus Netscape 'button' input type
|
|
329 ;; that lots of places have been using to slap javascript shit onto
|
|
330 (let ((val (w3-form-element-value el)))
|
|
331 (if (or (not val) (string= val ""))
|
|
332 (setq val "Push Me"))
|
82
|
333 (widget-create 'push-button
|
|
334 :notify 'ignore
|
|
335 :button-face face
|
98
|
336 :value-face face
|
82
|
337 val)))
|
0
|
338
|
|
339 (defun w3-form-create-image (el face)
|
88
|
340 (widget-create 'push-button
|
118
|
341 :button-face face
|
|
342 :value-face face
|
88
|
343 :notify 'w3-form-submit/reset-callback
|
|
344 :value (or
|
|
345 (plist-get (w3-form-element-plist el) 'alt)
|
108
|
346 (w3-form-element-value el)
|
88
|
347 "Form-Image")))
|
0
|
348
|
|
349 (defun w3-form-create-submit-button (el face)
|
|
350 (let ((val (w3-form-element-value el)))
|
|
351 (if (or (not val) (string= val ""))
|
|
352 (setq val (if (eq (w3-form-element-type el) 'submit)
|
|
353 "Submit"
|
|
354 "Reset")))
|
80
|
355 (widget-create 'push-button
|
|
356 :notify 'w3-form-submit/reset-callback
|
0
|
357 :button-face face val)))
|
|
358
|
|
359 (defun w3-form-create-file-browser (el face)
|
82
|
360 (widget-create 'file
|
98
|
361 :button-face face
|
82
|
362 :value-face face
|
|
363 :size (w3-form-element-size el)
|
|
364 :must-match t
|
|
365 :value (w3-form-element-value el)))
|
0
|
366
|
|
367 (defun w3-form-create-keygen-list (el face)
|
116
|
368 (let* ((size (apply 'max (mapcar (function (lambda (pair) (length (car pair))))
|
|
369 w3-form-valid-key-sizes)))
|
|
370 (options (mapcar (function (lambda (pair)
|
|
371 (list 'choice-item
|
|
372 :format "%[%t%]"
|
118
|
373 :tab-order -1
|
|
374 :button-face face
|
|
375 :value-face face
|
116
|
376 :menu-tag-get `(lambda (zed) ,(car pair))
|
|
377 :tag (mule-truncate-string (car pair) size ? )
|
|
378 :value (cdr pair))))
|
|
379 w3-form-valid-key-sizes)))
|
80
|
380 (apply 'widget-create 'menu-choice
|
118
|
381 :emacspeak-help 'w3-form-summarize-field
|
80
|
382 :value 1024
|
|
383 :ignore-case t
|
0
|
384 :tag "Key Length"
|
116
|
385 :size size
|
98
|
386 :button-face face
|
0
|
387 :value-face face
|
|
388 options)))
|
|
389
|
|
390 (defun w3-form-create-option-list (el face)
|
86
|
391 (let* ((size (w3-form-determine-size el nil))
|
|
392 (widget (apply 'widget-create 'menu-choice
|
80
|
393 :value (w3-form-element-value el)
|
|
394 :ignore-case t
|
0
|
395 :tag "Choose"
|
2
|
396 :format "%v"
|
86
|
397 :size size
|
0
|
398 :value-face face
|
98
|
399 :button-face face
|
0
|
400 (mapcar
|
|
401 (function
|
|
402 (lambda (x)
|
|
403 (list 'choice-item :format "%[%t%]"
|
82
|
404 :emacspeak-help 'w3-form-summarize-field
|
98
|
405 :menu-tag-get (` (lambda (zed) (, (car x))))
|
86
|
406 :tag (mule-truncate-string (car x) size ? )
|
98
|
407 :button-face face
|
|
408 :value-face face
|
82
|
409 :value (car x))))
|
80
|
410 (w3-form-element-options el)))))
|
2
|
411 (widget-value-set widget (w3-form-element-value el))
|
0
|
412 widget))
|
|
413
|
|
414 ;(defun w3-form-create-multiline (el face)
|
82
|
415 ; (widget-create 'text :value-face face (w3-form-element-value el)))
|
0
|
416
|
|
417 (defun w3-form-create-multiline (el face)
|
82
|
418 (widget-create 'push-button
|
118
|
419 :button-face face
|
82
|
420 :notify 'w3-do-text-entry
|
|
421 "Multiline text area"))
|
|
422
|
|
423 (defun w3-form-create-integer (el face)
|
98
|
424 (if w3-form-use-old-style
|
|
425 (w3-form-default-widget-creator el face)
|
|
426 (widget-create 'integer
|
|
427 :size (w3-form-element-size el)
|
|
428 :value-face face
|
|
429 :tag ""
|
|
430 :format "%v"
|
|
431 :keymap w3-form-keymap
|
|
432 :w3-form-data el
|
|
433 (w3-form-element-value el))))
|
82
|
434
|
|
435 (defun w3-form-create-float (el face)
|
98
|
436 (if w3-form-use-old-style
|
|
437 (w3-form-default-widget-creator el face)
|
|
438 (widget-create 'number
|
|
439 :size (w3-form-element-size el)
|
|
440 :value-face face
|
|
441 :format "%v"
|
|
442 :tag ""
|
|
443 :keymap w3-form-keymap
|
|
444 :w3-form-data el
|
|
445 (w3-form-element-value el))))
|
0
|
446
|
86
|
447 (defun w3-form-create-text (el face)
|
98
|
448 (if w3-form-use-old-style
|
|
449 (w3-form-default-widget-creator el face)
|
|
450 (widget-create 'editable-field
|
|
451 :keymap w3-form-keymap
|
|
452 :size (w3-form-element-size el)
|
|
453 :value-face face
|
|
454 :w3-form-data el
|
|
455 (w3-form-element-value el))))
|
86
|
456
|
88
|
457 (defun w3-form-create-password (el face)
|
|
458 ;; *sigh* This will fail under XEmacs, but I can yell at them about
|
|
459 ;; upgrading separately for the release of 19.15 and 20.0
|
98
|
460 (if w3-form-use-old-style
|
|
461 (w3-form-default-widget-creator el face)
|
|
462 (widget-create 'editable-field
|
|
463 :secret ?*
|
|
464 :keymap w3-form-keymap
|
|
465 :size (w3-form-element-size el)
|
|
466 :value-face face
|
|
467 :button-face face
|
|
468 :w3-form-data el
|
|
469 (w3-form-element-value el))))
|
88
|
470
|
0
|
471 (defun w3-form-default-widget-creator (el face)
|
|
472 (widget-create 'link
|
|
473 :notify 'w3-form-default-button-callback
|
82
|
474 :value-to-internal 'w3-form-default-button-update
|
0
|
475 :size (w3-form-element-size el)
|
|
476 :value-face face
|
98
|
477 :button-face face
|
82
|
478 :w3-form-data el
|
0
|
479 (w3-form-element-value el)))
|
|
480
|
82
|
481 (defun w3-form-default-button-update (w v)
|
|
482 (let ((info (widget-get w :w3-form-data)))
|
|
483 (widget-put w :tag
|
|
484 (if info
|
|
485 (mule-truncate-string
|
|
486 (if (eq 'password (w3-form-element-type info))
|
|
487 (make-string (length v) ?*)
|
|
488 v)
|
98
|
489 (w3-form-element-size info) ? )))
|
82
|
490 v))
|
|
491
|
0
|
492 (defun w3-form-default-button-callback (widget &rest ignore)
|
82
|
493 (let* ((obj (widget-get widget :w3-form-data))
|
0
|
494 (typ (w3-form-element-type obj))
|
|
495 (def (widget-value widget))
|
|
496 (val nil)
|
|
497 )
|
|
498 (case typ
|
|
499 (password
|
82
|
500 (setq val (funcall url-passwd-entry-func "Password: " def)))
|
0
|
501 (otherwise
|
|
502 (setq val (read-string
|
82
|
503 (concat (capitalize (symbol-name typ)) ": ") def))))
|
0
|
504 (widget-value-set widget val))
|
|
505 (apply 'w3-form-possibly-submit widget ignore))
|
82
|
506
|
|
507 ;; These properties tell the help-echo function how to summarize each
|
|
508 ;; type of widget.
|
|
509 (put 'checkbox 'w3-summarize-function 'w3-form-summarize-checkbox)
|
|
510 (put 'multiline 'w3-summarize-function 'w3-form-summarize-multiline)
|
|
511 (put 'radio 'w3-summarize-function 'w3-form-summarize-radio-button)
|
|
512 (put 'reset 'w3-summarize-function 'w3-form-summarize-submit-button)
|
|
513 (put 'submit 'w3-summarize-function 'w3-form-summarize-submit-button)
|
|
514 (put 'button 'w3-summarize-function 'w3-form-summarize-submit-button)
|
|
515 (put 'file 'w3-summarize-function 'w3-form-summarize-file-browser)
|
|
516 (put 'option 'w3-summarize-function 'w3-form-summarize-option-list)
|
|
517 (put 'keygen 'w3-summarize-function 'w3-form-summarize-keygen-list)
|
|
518 (put 'image 'w3-summarize-function 'w3-form-summarize-image)
|
102
|
519 (put 'password 'w3-summarize-function 'w3-form-summarize-password)
|
98
|
520 (put 'hidden 'w3-summarize-function 'ignore)
|
16
|
521
|
82
|
522 (defun w3-form-summarize-field (widget &rest ignore)
|
|
523 "Sumarize a widget that should be a W3 form entry area.
|
|
524 This can be used as the :help-echo property of all w3 form entry widgets."
|
|
525 (let ((info nil)
|
|
526 (func nil)
|
|
527 (msg nil)
|
|
528 )
|
|
529 (setq info (widget-get widget :w3-form-data))
|
|
530 (if info
|
|
531 nil
|
|
532 (while (widget-get widget :parent)
|
|
533 (setq widget (widget-get widget :parent)))
|
|
534 (setq info (widget-get widget :w3-form-data)))
|
|
535 (if (not info)
|
|
536 (signal 'wrong-type-argument (list 'w3-form-widget widget)))
|
|
537 (setq func (or (get (w3-form-element-type info) 'w3-summarize-function)
|
|
538 'w3-form-summarize-default)
|
|
539 msg (and (fboundp func) (funcall func info widget)))
|
|
540 ;; FIXME! This should be removed once emacspeak is updated to
|
|
541 ;; more closely follow the widget-y way of just returning the string
|
|
542 ;; instead of having the underlying :help-echo or :emacspeak-help
|
|
543 ;; implementation do it.
|
102
|
544 (and msg (message "%s" msg))))
|
82
|
545
|
|
546 (defsubst w3-form-field-label (data)
|
|
547 ;;; FIXXX!!! Need to reimplement using the new forms implementation!
|
|
548 (declare (special w3-form-labels))
|
108
|
549 (cdr-safe
|
|
550 (assoc (or (plist-get (w3-form-element-plist data) 'id)
|
|
551 (plist-get (w3-form-element-plist data) 'label))
|
|
552 w3-form-labels)))
|
82
|
553
|
|
554 (defun w3-form-summarize-default (data widget)
|
|
555 (let ((label (w3-form-field-label data))
|
|
556 (name (w3-form-element-name data))
|
|
557 (value (widget-value (w3-form-element-widget data))))
|
|
558 (format "Text field %s set to: %s" (or label (concat "called " name))
|
|
559 value)))
|
|
560
|
102
|
561 (defun w3-form-summarize-password (data widget)
|
|
562 (let ((label (w3-form-field-label data))
|
|
563 (name (w3-form-element-name data)))
|
|
564 (format "Password field %s is a secret. Shhh."
|
|
565 (or label (concat "called " name)))))
|
|
566
|
82
|
567 (defun w3-form-summarize-multiline (data widget)
|
|
568 (let ((name (w3-form-element-name data))
|
|
569 (label (w3-form-field-label data))
|
|
570 (value (w3-form-element-value data)))
|
|
571 (format "Multiline text input %s set to: %s"
|
|
572 (or label (concat "called " name))
|
|
573 value)))
|
|
574
|
|
575 (defun w3-form-summarize-checkbox (data widget)
|
|
576 (let ((name (w3-form-element-name data))
|
|
577 (label (w3-form-field-label data))
|
|
578 (checked (widget-value (w3-form-element-widget data))))
|
|
579 (format "Checkbox %s is %s" (or label name) (if checked "on" "off"))))
|
|
580
|
|
581 (defun w3-form-summarize-option-list (data widget)
|
|
582 (let ((name (w3-form-element-name data))
|
|
583 (label (w3-form-field-label data))
|
|
584 (default (w3-form-element-default-value data)))
|
|
585 (format "Option list (%s) set to: %s" (or label name)
|
|
586 (widget-value (w3-form-element-widget data)))))
|
|
587
|
|
588 (defun w3-form-summarize-image (data widget)
|
|
589 (let ((name (w3-form-element-name data))
|
|
590 (label (w3-form-field-label data)))
|
|
591 (concat "Image entry " (or label (concat "called " name)))))
|
|
592
|
|
593 (defun w3-form-summarize-submit-button (data widget)
|
|
594 (let* ((type (w3-form-element-type data))
|
|
595 (label (w3-form-field-label data))
|
|
596 (button-text (widget-value (w3-form-element-widget data)))
|
|
597 (type-desc (case type
|
|
598 (submit "Submit Form")
|
|
599 (reset "Reset Form")
|
|
600 (button "A Button"))))
|
|
601 (format "%s: %s" type-desc (or label button-text ""))))
|
|
602
|
|
603 (defun w3-form-summarize-radio-button (data widget)
|
|
604 (let ((name (w3-form-element-name data))
|
|
605 (label (w3-form-field-label data))
|
|
606 (cur-value (widget-value (w3-form-element-widget data)))
|
98
|
607 (this-value (widget-value (widget-get-sibling widget))))
|
102
|
608 (if (equal this-value cur-value)
|
|
609 (format "Radio group %s has %s pressed"
|
|
610 (or label name) this-value)
|
|
611 (format "Press this to change radio group %s from %s to %s" (or label name) cur-value
|
|
612 this-value))))
|
82
|
613
|
|
614 (defun w3-form-summarize-file-browser (data widget)
|
|
615 (let ((name (w3-form-element-name data))
|
|
616 (label (w3-form-field-label data))
|
|
617 (file (widget-value (w3-form-element-widget data))))
|
|
618 (format "File entry %s pointing to: %s" (or label name) (or file
|
|
619 "[nothing]"))))
|
|
620
|
|
621 (defun w3-form-summarize-keygen-list (data widget)
|
118
|
622 (format "Submitting this form will generate a %d bit key (not)"
|
|
623 (widget-value (w3-form-element-widget data))))
|
82
|
624
|
86
|
625 (defun w3-form-maybe-submit-by-keypress ()
|
|
626 (interactive)
|
|
627 (let ((widget (widget-at (point))))
|
|
628 (if widget
|
|
629 (w3-form-possibly-submit widget))))
|
|
630
|
0
|
631 (defun w3-form-possibly-submit (widget &rest ignore)
|
82
|
632 (let* ((formobj (widget-get widget :w3-form-data))
|
0
|
633 (ident (w3-form-element-action formobj))
|
|
634 (widgets (w3-all-widgets ident))
|
|
635 (text-fields 0)
|
|
636 (text-p nil))
|
|
637 ;;
|
|
638 ;; Gack. Netscape auto-submits forms of one text field
|
|
639 ;; here we go through the list of widgets in this form and
|
|
640 ;; determine which are not submit/reset/button inputs.
|
|
641 ;; If the # == 1, then submit the form.
|
|
642 ;;
|
|
643 (while widgets
|
|
644 (setq text-fields (+
|
|
645 text-fields
|
|
646 (case (w3-form-element-type (car widgets))
|
|
647 ((submit reset image button)
|
|
648 0)
|
|
649 (text
|
|
650 (setq text-p t)
|
|
651 1)
|
|
652 (otherwise
|
|
653 1)))
|
|
654 widgets (cdr widgets)))
|
|
655 (if (and (= text-fields 1) text-p)
|
|
656 (w3-submit-form ident))))
|
|
657
|
|
658 (defun w3-form-submit/reset-callback (widget &rest ignore)
|
82
|
659 (let* ((formobj (widget-get widget :w3-form-data))
|
0
|
660 (w3-submit-button formobj))
|
|
661 (case (w3-form-element-type formobj)
|
|
662 (submit (w3-submit-form (w3-form-element-action formobj)))
|
|
663 (reset (w3-revert-form (w3-form-element-action formobj)))
|
|
664 (image (w3-submit-form (w3-form-element-action formobj)))
|
|
665 (otherwise
|
|
666 (error
|
|
667 "Impossible widget type %s triggered w3-form-submit/reset-callback"
|
|
668 (w3-form-element-type formobj))))))
|
|
669
|
|
670 (defun w3-do-text-entry (widget &rest ignore)
|
|
671 (let* ((data (list widget (current-buffer)))
|
82
|
672 (formobj (widget-get widget :w3-form-data))
|
0
|
673 (buff (get-buffer-create (format "Form Entry: %s"
|
|
674 (w3-form-element-name formobj)))))
|
|
675 (switch-to-buffer-other-window buff)
|
|
676 (indented-text-mode)
|
|
677 (erase-buffer)
|
|
678 (if (w3-form-element-value formobj)
|
|
679 (insert (w3-form-element-value formobj)))
|
|
680 (setq w3-current-last-buffer data)
|
|
681 (message "Press C-c C-c when finished with text entry.")
|
|
682 (local-set-key "\C-c\C-c" 'w3-finish-text-entry)))
|
|
683
|
|
684 (defun w3-finish-text-entry ()
|
|
685 (interactive)
|
|
686 (if w3-current-last-buffer
|
|
687 (let* ((widget (nth 0 w3-current-last-buffer))
|
82
|
688 (formobj (widget-get widget :w3-form-data))
|
0
|
689 (buff (nth 1 w3-current-last-buffer))
|
|
690 (valu (buffer-string))
|
|
691 (inhibit-read-only t)
|
|
692 )
|
|
693 (local-set-key "\C-c\C-c" 'undefined)
|
|
694 (kill-buffer (current-buffer))
|
|
695 (condition-case ()
|
|
696 (delete-window)
|
|
697 (error nil))
|
|
698 (if (not (and buff (bufferp buff) (buffer-name buff)))
|
|
699 (message "Could not find the form buffer for this text!")
|
|
700 (switch-to-buffer buff)
|
|
701 (w3-form-element-set-value formobj valu)))))
|
|
702
|
|
703 (defsubst w3-all-widgets (actn)
|
|
704 ;; Return a list of data entry widgets in form number ACTN
|
|
705 (cdr-safe (assoc actn w3-form-elements)))
|
|
706
|
|
707 (defun w3-revert-form (actn)
|
|
708 (save-excursion
|
|
709 (let* ((formobjs (w3-all-widgets actn))
|
|
710 (inhibit-read-only t)
|
|
711 deft type widget formobj)
|
|
712 (while formobjs
|
|
713 (setq formobj (car formobjs)
|
|
714 widget (w3-form-element-widget formobj)
|
|
715 formobjs (cdr formobjs)
|
|
716 deft (w3-form-element-default-value formobj)
|
|
717 type (w3-form-element-type formobj))
|
|
718 (case type
|
98
|
719 ((submit reset image hidden) nil)
|
0
|
720 (radio
|
82
|
721 (setq deft (widget-get widget 'w3-form-default-value))
|
|
722 (if (and widget deft)
|
|
723 (widget-value-set widget deft)))
|
0
|
724 (checkbox
|
|
725 (if deft
|
|
726 (widget-value-set widget t)
|
|
727 (widget-value-set widget nil)))
|
82
|
728 (multiline
|
|
729 (w3-form-element-set-value formobj (w3-form-element-default-value
|
|
730 formobj)))
|
0
|
731 (file
|
|
732 (widget-value-set widget deft))
|
|
733 (otherwise
|
86
|
734 (widget-value-set widget deft))))
|
|
735 (widget-setup))))
|
0
|
736
|
|
737 (defun w3-form-encode-helper (formobjs)
|
|
738 (let (
|
|
739 (submit-button-data w3-submit-button)
|
|
740 formobj result widget temp type)
|
|
741 (while formobjs
|
|
742 (setq formobj (car formobjs)
|
|
743 type (w3-form-element-type formobj)
|
|
744 widget (w3-form-element-widget formobj)
|
|
745 formobjs (cdr formobjs)
|
|
746 temp (case type
|
|
747 (reset nil)
|
86
|
748 (button nil)
|
0
|
749 (image
|
|
750 (if (and (eq submit-button-data formobj)
|
|
751 (w3-form-element-name formobj))
|
|
752 (setq result (append
|
|
753 (list
|
|
754 (cons
|
|
755 (concat (w3-form-element-name formobj)
|
|
756 ".x") "0")
|
|
757 (cons
|
|
758 (concat (w3-form-element-name formobj)
|
|
759 ".y") "0"))
|
|
760 result)))
|
|
761 nil)
|
|
762 (submit
|
|
763 (if (and (eq submit-button-data formobj)
|
|
764 (w3-form-element-name formobj))
|
|
765 (cons (w3-form-element-name formobj)
|
|
766 (w3-form-element-value formobj))))
|
|
767 (radio
|
2
|
768 (let* ((radio-name (w3-form-element-name formobj))
|
|
769 (radio-object (cdr-safe
|
82
|
770 (assoc
|
|
771 (cons
|
|
772 radio-name
|
|
773 (w3-form-element-action formobj))
|
|
774 w3-form-radio-elements)))
|
2
|
775 (chosen-widget (and radio-object
|
|
776 (widget-radio-chosen
|
|
777 (w3-form-element-widget
|
|
778 radio-object)))))
|
|
779 (if (assoc radio-name result)
|
0
|
780 nil
|
2
|
781 (cons radio-name (widget-value chosen-widget)))))
|
82
|
782 ((int float)
|
|
783 (cons (w3-form-element-name formobj)
|
|
784 (number-to-string (or (condition-case ()
|
|
785 (widget-value widget)
|
|
786 (error nil)) 0))))
|
0
|
787 (checkbox
|
|
788 (if (widget-value widget)
|
|
789 (cons (w3-form-element-name formobj)
|
|
790 (w3-form-element-value formobj))))
|
|
791 (file
|
|
792 (let ((dat nil)
|
|
793 (fname (widget-value widget)))
|
|
794 (save-excursion
|
|
795 (set-buffer (get-buffer-create " *w3-temp*"))
|
|
796 (erase-buffer)
|
|
797 (setq dat
|
|
798 (condition-case ()
|
|
799 (insert-file-contents-literally fname)
|
|
800 (error (concat "Error accessing " fname))))
|
|
801 (cons (w3-form-element-name formobj) dat))))
|
|
802 (option
|
|
803 (cons (w3-form-element-name formobj)
|
2
|
804 (cdr-safe
|
|
805 (assoc (widget-value widget)
|
|
806 (w3-form-element-options formobj)))))
|
0
|
807 (keygen
|
116
|
808 (condition-case ()
|
|
809 (require 'ssl)
|
|
810 (error (error "Not configured for SSL, please read the info pages.")))
|
|
811 (if (fboundp 'ssl-req-user-cert) nil
|
|
812 (error "This version of SSL isn't capable of requesting certificates."))
|
|
813 (let ((challenge (plist-get (w3-form-element-plist formobj) 'challenge))
|
|
814 (size (widget-value widget)))
|
|
815 (cons (w3-form-element-name formobj)
|
|
816 (ssl-req-user-cert size challenge))))
|
0
|
817 ((multiline hidden)
|
|
818 (cons (w3-form-element-name formobj)
|
|
819 (w3-form-element-value formobj)))
|
|
820 (otherwise
|
|
821 (cons (w3-form-element-name formobj)
|
|
822 (widget-value widget)))))
|
|
823 (if temp
|
|
824 (setq result (cons temp result))))
|
|
825 result))
|
|
826
|
|
827 (defun w3-form-encode-make-mime-part (id data separator)
|
|
828 (concat separator "\nContent-id: " id
|
|
829 "\nContent-length: " (length data)
|
|
830 "\n\n" data))
|
|
831
|
|
832 (defun w3-form-encode-multipart/x-www-form-data (formobjs)
|
|
833 ;; Create a multipart form submission.
|
|
834 ;; Returns a cons of two strings. Car is the separator used.
|
|
835 ;; cdr is the body of the MIME message."
|
|
836 (let ((separator "---some-separator-for-www-form-data"))
|
|
837 (cons separator
|
|
838 (mapconcat
|
|
839 (function
|
|
840 (lambda (formobj)
|
|
841 (w3-form-encode-make-mime-part (car formobj) (cdr formobj)
|
|
842 separator)))
|
|
843 (w3-form-encode-helper formobjs)
|
|
844 "\n"))))
|
|
845
|
|
846 (fset 'w3-form-encode-multipart/form-data
|
|
847 'w3-form-encode-multipart/x-www-form-data)
|
|
848 (fset 'w3-form-encode- 'w3-form-encode-application/x-www-form-urlencoded)
|
|
849
|
|
850 (defun w3-next-widget (pos)
|
|
851 (let* ((next (cond ((get-text-property pos 'button)
|
|
852 (next-single-property-change pos 'button))
|
|
853 ((get-text-property pos 'field)
|
|
854 (next-single-property-change pos 'field))
|
|
855 (t pos)))
|
80
|
856 (button (and next (next-single-property-change next 'button)))
|
|
857 (field (and next (next-single-property-change next 'field))))
|
0
|
858 (setq next
|
|
859 (cond
|
|
860 ((and button field) (min button field))
|
|
861 (button button)
|
|
862 (field field)
|
|
863 (t nil)))
|
|
864 (and next
|
|
865 (or (get-text-property next 'button)
|
|
866 (get-text-property next 'field)))))
|
|
867
|
|
868 (defun w3-form-encode (result &optional enctype)
|
|
869 "Create a string suitably encoded for a URL request."
|
|
870 (let ((func (intern (concat "w3-form-encode-" enctype))))
|
2
|
871 (if (fboundp func)
|
|
872 (funcall func result)
|
|
873 (w3-warn 'html (format "Bad encoding type for form data: %s" enctype))
|
|
874 (w3-form-encode-application/x-www-form-urlencoded result))))
|
0
|
875
|
|
876 (defun w3-form-encode-text/plain (result)
|
|
877 (let ((query ""))
|
|
878 (setq query
|
|
879 (mapconcat
|
|
880 (function
|
|
881 (lambda (widget)
|
|
882 (let ((nam (car widget))
|
|
883 (val (cdr widget)))
|
|
884 (if (string-match "\n" nam)
|
|
885 (setq nam (mapconcat
|
|
886 (function
|
|
887 (lambda (x)
|
|
888 (if (= x ?\n) "," (char-to-string x))))
|
|
889 nam "")))
|
|
890 (concat nam " " val))))
|
|
891 (w3-form-encode-helper result) "\n"))
|
|
892 query))
|
|
893
|
|
894 (defun w3-form-encode-application/x-gopher-query (result)
|
2
|
895 (concat "\t" (cdr (car (w3-form-encode-helper result)))))
|
0
|
896
|
|
897 (defun w3-form-encode-xwfu (chunk)
|
|
898 "Escape characters in a string for application/x-www-form-urlencoded.
|
|
899 Blasphemous crap because someone didn't think %20 was good enough for encoding
|
|
900 spaces. Die Die Die."
|
|
901 (mapconcat
|
|
902 (function
|
|
903 (lambda (char)
|
|
904 (cond
|
|
905 ((= char ? ) "+")
|
80
|
906 ((memq char url-unreserved-chars) (char-to-string char))
|
2
|
907 (t (upcase (format "%%%02x" char))))))
|
80
|
908 (mule-encode-string chunk) ""))
|
0
|
909
|
|
910 (defun w3-form-encode-application/x-www-form-urlencoded (result)
|
|
911 (mapconcat
|
|
912 (function
|
|
913 (lambda (data)
|
|
914 (concat (w3-form-encode-xwfu (car data)) "="
|
|
915 (w3-form-encode-xwfu (cdr data)))))
|
|
916 (w3-form-encode-helper result) "&"))
|
|
917
|
|
918 (defun w3-form-encode-application/x-w3-isindex (result)
|
|
919 (let* ((info (w3-form-encode-helper result))
|
|
920 (query (cdr-safe (assoc "isindex" info))))
|
|
921 (if query
|
|
922 (url-hexify-string query)
|
|
923 "")))
|
|
924
|
|
925 (defun w3-form-encode-application/gopher-ask-block (result)
|
|
926 (let ((query ""))
|
|
927 ;;; gopher+ will expect all the checkboxes/etc, even if they are
|
|
928 ;;; not turned on. Should still ignore RADIO boxes that are not
|
|
929 ;;; active though.
|
|
930 (while result
|
|
931 (if (and (not (and (string= (nth 2 (car result)) "RADIO")
|
|
932 (not (nth 6 (car result)))))
|
|
933 (not (member (nth 2 (car result)) '("SUBMIT" "RESET"))))
|
|
934 (setq query (format "%s\r\n%s" query (nth 5 (car result)))))
|
|
935 (setq result (cdr result)))
|
|
936 (concat query "\r\n.\r\n")))
|
|
937
|
|
938 (defun w3-submit-form (ident)
|
|
939 ;; Submit form entry fields matching ACTN as their action identifier.
|
|
940 (let* ((result (w3-all-widgets ident))
|
80
|
941 (enctype (or (cdr (assq 'enctype ident))
|
|
942 "application/x-www-form-urlencoded"))
|
0
|
943 (query (w3-form-encode result enctype))
|
|
944 (themeth (upcase (or (cdr (assq 'method ident)) "get")))
|
|
945 (theurl (cdr (assq 'action ident))))
|
|
946 (if (and (string= "GET" themeth)
|
|
947 (string-match "\\([^\\?]*\\)\\?" theurl))
|
|
948 (setq theurl (url-match theurl 1)))
|
|
949 (cond
|
|
950 ((or (string= "POST" themeth)
|
|
951 (string= "PUT" themeth))
|
|
952 (if (consp query)
|
|
953 (setq enctype (concat enctype "; separator=\""
|
|
954 (substring (car query) 3 nil)
|
|
955 "\"")
|
|
956 query (cdr query)))
|
|
957 (let ((url-request-method themeth)
|
|
958 (url-request-data query)
|
|
959 (url-request-extra-headers
|
|
960 (cons (cons "Content-type" enctype) url-request-extra-headers)))
|
|
961 (w3-fetch theurl)))
|
|
962 ((string= "GET" themeth)
|
2
|
963 (let ((theurl (concat theurl (if (string-match "gopher" enctype)
|
|
964 "" "?") query)))
|
0
|
965 (w3-fetch theurl)))
|
|
966 (t
|
|
967 (w3-warn 'html (format "Unknown submit method: %s" themeth))
|
|
968 (let ((theurl (concat theurl "?" query)))
|
|
969 (w3-fetch theurl))))))
|
|
970
|
|
971 (provide 'w3-forms)
|