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