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