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