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