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