2
|
1 ;;; w3-forms.el --- Emacs-w3 forms parsing code for new display engine
|
0
|
2 ;; Author: wmperry
|
80
|
3 ;; Created: 1997/01/02 20:20:29
|
|
4 ;; Version: 1.32
|
0
|
5 ;; Keywords: faces, help, comm, data, languages
|
|
6
|
|
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2
|
8 ;;; Copyright (c) 1996 by William M. Perry (wmperry@cs.indiana.edu)
|
80
|
9 ;;; Copyright (c) 1996 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
|
0
|
39 ;; A form entry area is a vector
|
70
|
40 ;; [ type name default-value value maxlength options widget]
|
0
|
41 ;; Where:
|
|
42 ;; type = symbol defining what type of form entry area it is
|
|
43 ;; (ie: file, radio)
|
|
44 ;; name = the name of the form element
|
|
45 ;; default-value = the value this started out with
|
|
46
|
|
47 (defsubst w3-form-element-type (obj) (aref obj 0))
|
|
48 (defsubst w3-form-element-name (obj) (aref obj 1))
|
|
49 (defsubst w3-form-element-default-value (obj) (aref obj 2))
|
|
50 (defsubst w3-form-element-value (obj) (aref obj 3))
|
|
51 (defsubst w3-form-element-size (obj) (aref obj 4))
|
|
52 (defsubst w3-form-element-maxlength (obj) (aref obj 5))
|
|
53 (defsubst w3-form-element-options (obj) (aref obj 6))
|
|
54 (defsubst w3-form-element-action (obj) (aref obj 7))
|
|
55 (defsubst w3-form-element-widget (obj) (aref obj 8))
|
|
56
|
|
57 (defsubst w3-form-element-set-type (obj val) (aset obj 0 val))
|
|
58 (defsubst w3-form-element-set-name (obj val) (aset obj 1 val))
|
|
59 (defsubst w3-form-element-set-default-value (obj val) (aset obj 2 val))
|
|
60 (defsubst w3-form-element-set-value (obj val) (aset obj 3 val))
|
|
61 (defsubst w3-form-element-set-size (obj val) (aset obj 4 val))
|
|
62 (defsubst w3-form-element-set-maxlength (obj val) (aset obj 5 val))
|
|
63 (defsubst w3-form-element-set-options (obj val) (aset obj 6 val))
|
|
64 (defsubst w3-form-element-set-action (obj val) (aset obj 7 val))
|
|
65 (defsubst w3-form-element-set-widget (obj val) (aset obj 8 val))
|
|
66
|
70
|
67 ;; The main function - this adds a single widget to the form
|
80
|
68 (defun w3-form-add-element (type name value size maxlength default
|
|
69 action options number id checked
|
|
70 face)
|
|
71 (let* ((name (or name (case type
|
|
72 ((submit reset) nil)
|
|
73 (otherwise (symbol-name type)))))
|
|
74 (el (vector type
|
|
75 name
|
|
76 default
|
|
77 value
|
|
78 size
|
|
79 maxlength
|
|
80 options
|
|
81 action nil))
|
|
82 (size (if size
|
|
83 (+ 2 size)
|
|
84 (case type
|
|
85 ((checkbox radio) 3)
|
|
86 ((reset submit)
|
|
87 (+ 2 (length (or value (symbol-name type)))))
|
|
88 (multiline 21)
|
|
89 (hidden nil)
|
|
90 (otherwise 22))))
|
|
91 )
|
|
92 (if size
|
|
93 (set-text-properties (point)
|
|
94 (progn (insert-char ?T size) (point))
|
|
95 (list 'w3-form-info el
|
|
96 'start-open t
|
|
97 'end-open t
|
|
98 'rear-nonsticky t)))))
|
|
99
|
|
100 (defun w3-form-resurrect-widgets ()
|
|
101 (let ((st (point-min))
|
|
102 info nd node action)
|
|
103 (while st
|
|
104 (if (setq info (get-text-property st 'w3-form-info))
|
|
105 (progn
|
|
106 (setq nd (next-single-property-change st 'w3-form-info)
|
|
107 action (w3-form-element-action info)
|
|
108 node (assoc action w3-form-elements))
|
|
109 (goto-char st)
|
|
110 (delete-region st nd)
|
|
111 (if (not (w3-form-element-size info))
|
|
112 (w3-form-element-set-size info 20))
|
|
113 (if node
|
|
114 (setcdr node (cons info (cdr node)))
|
|
115 (setq w3-form-elements (cons (cons action (list info))
|
|
116 w3-form-elements)))
|
|
117 (w3-form-add-element-internal info)
|
|
118 (setq st (next-single-property-change st 'w3-form-info)))
|
|
119 (setq st (next-single-property-change st 'w3-form-info))))))
|
|
120
|
|
121 (defun w3-form-add-element-internal (el)
|
0
|
122 (let* ((widget nil)
|
|
123 (buffer-read-only nil)
|
|
124 (inhibit-read-only t)
|
80
|
125 (widget-creation-function nil))
|
|
126 (setq widget-creation-function (or (get (w3-form-element-type el)
|
0
|
127 'w3-widget-creation-function)
|
|
128 'w3-form-default-widget-creator)
|
80
|
129 widget (funcall widget-creation-function el nil))
|
0
|
130 (if (not widget)
|
|
131 nil
|
80
|
132 (w3-form-element-set-widget el widget)
|
|
133 (widget-put widget 'w3-form-data el))))
|
0
|
134
|
|
135 ;; These properties tell the add-element function how to actually create
|
|
136 ;; each type of widget.
|
|
137 (put 'checkbox 'w3-widget-creation-function 'w3-form-create-checkbox)
|
|
138 (put 'multiline 'w3-widget-creation-function 'w3-form-create-multiline)
|
|
139 (put 'radio 'w3-widget-creation-function 'w3-form-create-radio-button)
|
|
140 (put 'reset 'w3-widget-creation-function 'w3-form-create-submit-button)
|
|
141 (put 'submit 'w3-widget-creation-function 'w3-form-create-submit-button)
|
|
142 (put 'hidden 'w3-widget-creation-function 'ignore)
|
|
143 (put 'file 'w3-widget-creation-function 'w3-form-create-file-browser)
|
|
144 (put 'option 'w3-widget-creation-function 'w3-form-create-option-list)
|
|
145 (put 'keygen 'w3-widget-creation-function 'w3-form-create-keygen-list)
|
|
146 (put 'button 'w3-widget-creation-function 'w3-form-create-button)
|
|
147 (put 'image 'w3-widget-creation-function 'w3-form-create-image)
|
|
148
|
|
149 (defun w3-form-create-checkbox (el face)
|
70
|
150 (widget-create 'checkbox :value-face face
|
0
|
151 (and (w3-form-element-default-value el) t)))
|
|
152
|
|
153 (defun w3-form-create-radio-button (el face)
|
|
154 (let* ((name (w3-form-element-name el))
|
70
|
155 (formobj (cdr (assoc name w3-form-radio-elements)))
|
0
|
156 (widget nil)
|
|
157 )
|
|
158 (if formobj
|
|
159 (progn
|
|
160 (setq widget (w3-form-element-widget formobj))
|
2
|
161 (widget-radio-add-item widget
|
|
162 (list 'item
|
|
163 :format "%t"
|
|
164 :tag ""
|
|
165 :value (w3-form-element-value el)))
|
|
166 (if (w3-form-element-default-value el)
|
70
|
167 (widget-value-set widget (w3-form-element-value el)))
|
0
|
168 nil)
|
80
|
169 (setq widget (widget-create 'radio-button-choice
|
70
|
170 :value (w3-form-element-value el)
|
|
171 (list 'item
|
|
172 :format "%t"
|
|
173 :tag ""
|
|
174 :value (w3-form-element-value el)))
|
|
175 w3-form-radio-elements (cons (cons name el)
|
0
|
176 w3-form-radio-elements))
|
|
177 widget)))
|
|
178
|
|
179 (defun w3-form-create-button (el face)
|
|
180 ;; This handles dealing with the bogus Netscape 'button' input type
|
|
181 ;; that lots of places have been using to slap javascript shit onto
|
|
182 (let ((val (w3-form-element-value el)))
|
|
183 (if (or (not val) (string= val ""))
|
|
184 (setq val "Push Me"))
|
80
|
185 (widget-create 'push-button :notify 'ignore :button-face face val)))
|
0
|
186
|
|
187 (defun w3-form-create-image (el face)
|
80
|
188 (let ((widget (widget-create 'push-button
|
70
|
189 :notify 'w3-form-submit/reset-callback
|
|
190 :value "Form-Image")))
|
|
191 widget))
|
0
|
192
|
|
193 (defun w3-form-create-submit-button (el face)
|
|
194 (let ((val (w3-form-element-value el)))
|
|
195 (if (or (not val) (string= val ""))
|
|
196 (setq val (if (eq (w3-form-element-type el) 'submit)
|
|
197 "Submit"
|
|
198 "Reset")))
|
80
|
199 (widget-create 'push-button
|
|
200 :notify 'w3-form-submit/reset-callback
|
0
|
201 :button-face face val)))
|
|
202
|
|
203 (defun w3-form-create-file-browser (el face)
|
70
|
204 (widget-create 'file :value-face face :value (w3-form-element-value el)))
|
0
|
205
|
70
|
206 (defvar w3-form-valid-key-sizes
|
|
207 '(
|
|
208 ("1024 (Premium)" . 1024)
|
|
209 ("896 (Regular)" . 896)
|
|
210 ("768 (Unleaded)" . 768)
|
|
211 ("512 (Low Grade)" . 512)
|
|
212 ("508 (Woos)" . 508)
|
|
213 ("256 (Test Grade)" . 256)
|
|
214 )
|
|
215 "An assoc list of available key sizes and meaningful descriptions.")
|
|
216
|
0
|
217 (defun w3-form-create-keygen-list (el face)
|
70
|
218 (let ((tmp w3-form-valid-key-sizes)
|
|
219 (longest 0)
|
|
220 (options nil))
|
|
221 (while tmp
|
|
222 (if (> (length (caar tmp)) longest)
|
|
223 (setq longest (length (caar tmp))))
|
|
224 (setq options (cons (list 'choice-item :tag (caar tmp)
|
|
225 :value (cdar tmp)) options)
|
|
226 tmp (cdr tmp)))
|
80
|
227 (apply 'widget-create 'menu-choice
|
|
228 :value 1024
|
|
229 :ignore-case t
|
0
|
230 :tag "Key Length"
|
70
|
231 :size (1+ longest)
|
0
|
232 :value-face face
|
|
233 options)))
|
|
234
|
|
235 (defun w3-form-create-option-list (el face)
|
80
|
236 (let ((widget (apply 'widget-create 'menu-choice
|
|
237 :value (w3-form-element-value el)
|
|
238 :ignore-case t
|
0
|
239 :tag "Choose"
|
2
|
240 :format "%v"
|
70
|
241 :size (w3-form-element-size el)
|
0
|
242 :value-face face
|
|
243 (mapcar
|
|
244 (function
|
|
245 (lambda (x)
|
|
246 (list 'choice-item :format "%[%t%]"
|
70
|
247 :tag (car x) :value (car x))))
|
80
|
248 (w3-form-element-options el)))))
|
2
|
249 (widget-value-set widget (w3-form-element-value el))
|
0
|
250 widget))
|
|
251
|
|
252 ;(defun w3-form-create-multiline (el face)
|
70
|
253 ; ;; FIX THIS! - need to padd out with newlines or something...
|
|
254 ; (widget-create 'field :value-face face (w3-form-element-value el)))
|
0
|
255
|
|
256 (defun w3-form-create-multiline (el face)
|
80
|
257 (widget-create 'push-button :notify 'w3-do-text-entry "Multiline text area"))
|
0
|
258
|
|
259 (defun w3-form-default-widget-creator (el face)
|
|
260 (widget-create 'link
|
|
261 :notify 'w3-form-default-button-callback
|
|
262 :size (w3-form-element-size el)
|
80
|
263 :tag (mule-truncate-string (w3-form-element-value el)
|
|
264 (w3-form-element-size el) ?_)
|
0
|
265 :value-face face
|
|
266 (w3-form-element-value el)))
|
|
267
|
|
268 (defun w3-form-default-button-callback (widget &rest ignore)
|
70
|
269 (let* ((obj (widget-get widget 'w3-form-data))
|
0
|
270 (typ (w3-form-element-type obj))
|
|
271 (def (widget-value widget))
|
|
272 (val nil)
|
|
273 )
|
|
274 (case typ
|
|
275 (password
|
70
|
276 (setq val (funcall url-passwd-entry-func "Password: " def))
|
80
|
277 (widget-put widget :tag (mule-truncate-string
|
70
|
278 (make-string (length val) ?*)
|
|
279 (w3-form-element-size obj) ?_)))
|
0
|
280 (otherwise
|
|
281 (setq val (read-string
|
70
|
282 (concat (capitalize (symbol-name typ)) ": ") def))
|
80
|
283 (widget-put widget :tag (mule-truncate-string
|
70
|
284 val (w3-form-element-size obj) ?_))))
|
0
|
285 (widget-value-set widget val))
|
|
286 (apply 'w3-form-possibly-submit widget ignore))
|
16
|
287
|
0
|
288 (defun w3-form-possibly-submit (widget &rest ignore)
|
70
|
289 (let* ((formobj (widget-get widget 'w3-form-data))
|
0
|
290 (ident (w3-form-element-action formobj))
|
|
291 (widgets (w3-all-widgets ident))
|
|
292 (text-fields 0)
|
|
293 (text-p nil))
|
|
294 ;;
|
|
295 ;; Gack. Netscape auto-submits forms of one text field
|
|
296 ;; here we go through the list of widgets in this form and
|
|
297 ;; determine which are not submit/reset/button inputs.
|
|
298 ;; If the # == 1, then submit the form.
|
|
299 ;;
|
|
300 (while widgets
|
|
301 (setq text-fields (+
|
|
302 text-fields
|
|
303 (case (w3-form-element-type (car widgets))
|
|
304 ((submit reset image button)
|
|
305 0)
|
|
306 (text
|
|
307 (setq text-p t)
|
|
308 1)
|
|
309 (otherwise
|
|
310 1)))
|
|
311 widgets (cdr widgets)))
|
|
312 (if (and (= text-fields 1) text-p)
|
|
313 (w3-submit-form ident))))
|
|
314
|
|
315 (defun w3-form-submit/reset-callback (widget &rest ignore)
|
70
|
316 (let* ((formobj (widget-get widget 'w3-form-data))
|
0
|
317 (w3-submit-button formobj))
|
|
318 (case (w3-form-element-type formobj)
|
|
319 (submit (w3-submit-form (w3-form-element-action formobj)))
|
|
320 (reset (w3-revert-form (w3-form-element-action formobj)))
|
|
321 (image (w3-submit-form (w3-form-element-action formobj)))
|
|
322 (otherwise
|
|
323 (error
|
|
324 "Impossible widget type %s triggered w3-form-submit/reset-callback"
|
|
325 (w3-form-element-type formobj))))))
|
|
326
|
|
327 (defun w3-do-text-entry (widget &rest ignore)
|
|
328 (let* ((data (list widget (current-buffer)))
|
70
|
329 (formobj (widget-get widget 'w3-form-data))
|
0
|
330 (buff (get-buffer-create (format "Form Entry: %s"
|
|
331 (w3-form-element-name formobj)))))
|
|
332 (switch-to-buffer-other-window buff)
|
|
333 (indented-text-mode)
|
|
334 (erase-buffer)
|
|
335 (if (w3-form-element-value formobj)
|
|
336 (insert (w3-form-element-value formobj)))
|
|
337 (setq w3-current-last-buffer data)
|
|
338 (message "Press C-c C-c when finished with text entry.")
|
|
339 (local-set-key "\C-c\C-c" 'w3-finish-text-entry)))
|
|
340
|
|
341 (defun w3-finish-text-entry ()
|
|
342 (interactive)
|
|
343 (if w3-current-last-buffer
|
|
344 (let* ((widget (nth 0 w3-current-last-buffer))
|
70
|
345 (formobj (widget-get widget 'w3-form-data))
|
0
|
346 (buff (nth 1 w3-current-last-buffer))
|
|
347 (valu (buffer-string))
|
|
348 (inhibit-read-only t)
|
|
349 )
|
|
350 (local-set-key "\C-c\C-c" 'undefined)
|
|
351 (kill-buffer (current-buffer))
|
|
352 (condition-case ()
|
|
353 (delete-window)
|
|
354 (error nil))
|
|
355 (if (not (and buff (bufferp buff) (buffer-name buff)))
|
|
356 (message "Could not find the form buffer for this text!")
|
|
357 (switch-to-buffer buff)
|
|
358 (w3-form-element-set-value formobj valu)))))
|
|
359
|
|
360 (defsubst w3-all-widgets (actn)
|
|
361 ;; Return a list of data entry widgets in form number ACTN
|
|
362 (cdr-safe (assoc actn w3-form-elements)))
|
|
363
|
|
364 (defun w3-revert-form (actn)
|
|
365 (save-excursion
|
|
366 (let* ((formobjs (w3-all-widgets actn))
|
|
367 (inhibit-read-only t)
|
|
368 deft type widget formobj)
|
|
369 (while formobjs
|
|
370 (setq formobj (car formobjs)
|
|
371 widget (w3-form-element-widget formobj)
|
|
372 formobjs (cdr formobjs)
|
|
373 deft (w3-form-element-default-value formobj)
|
|
374 type (w3-form-element-type formobj))
|
|
375 (case type
|
70
|
376 ((submit reset image) nil)
|
0
|
377 (radio
|
70
|
378 ;; Ack - how!?
|
|
379 )
|
0
|
380 (checkbox
|
|
381 (if deft
|
|
382 (widget-value-set widget t)
|
|
383 (widget-value-set widget nil)))
|
|
384 (file
|
|
385 (widget-value-set widget deft))
|
|
386 (otherwise
|
70
|
387 (widget-value-set widget deft)))))))
|
0
|
388
|
|
389 (defun w3-form-encode-helper (formobjs)
|
|
390 (let (
|
|
391 (submit-button-data w3-submit-button)
|
|
392 formobj result widget temp type)
|
|
393 (while formobjs
|
|
394 (setq formobj (car formobjs)
|
|
395 type (w3-form-element-type formobj)
|
|
396 widget (w3-form-element-widget formobj)
|
|
397 formobjs (cdr formobjs)
|
|
398 temp (case type
|
|
399 (reset nil)
|
|
400 (image
|
|
401 (if (and (eq submit-button-data formobj)
|
|
402 (w3-form-element-name formobj))
|
|
403 (setq result (append
|
|
404 (list
|
|
405 (cons
|
|
406 (concat (w3-form-element-name formobj)
|
|
407 ".x") "0")
|
|
408 (cons
|
|
409 (concat (w3-form-element-name formobj)
|
|
410 ".y") "0"))
|
|
411 result)))
|
|
412 nil)
|
|
413 (submit
|
|
414 (if (and (eq submit-button-data formobj)
|
|
415 (w3-form-element-name formobj))
|
|
416 (cons (w3-form-element-name formobj)
|
|
417 (w3-form-element-value formobj))))
|
|
418 (radio
|
2
|
419 (let* ((radio-name (w3-form-element-name formobj))
|
|
420 (radio-object (cdr-safe
|
70
|
421 (assoc radio-name
|
|
422 w3-form-radio-elements)))
|
2
|
423 (chosen-widget (and radio-object
|
|
424 (widget-radio-chosen
|
|
425 (w3-form-element-widget
|
|
426 radio-object)))))
|
|
427 (if (assoc radio-name result)
|
0
|
428 nil
|
2
|
429 (cons radio-name (widget-value chosen-widget)))))
|
0
|
430 (checkbox
|
|
431 (if (widget-value widget)
|
|
432 (cons (w3-form-element-name formobj)
|
|
433 (w3-form-element-value formobj))))
|
|
434 (file
|
|
435 (let ((dat nil)
|
|
436 (fname (widget-value widget)))
|
|
437 (save-excursion
|
|
438 (set-buffer (get-buffer-create " *w3-temp*"))
|
|
439 (erase-buffer)
|
|
440 (setq dat
|
|
441 (condition-case ()
|
|
442 (insert-file-contents-literally fname)
|
|
443 (error (concat "Error accessing " fname))))
|
|
444 (cons (w3-form-element-name formobj) dat))))
|
|
445 (option
|
|
446 (cons (w3-form-element-name formobj)
|
2
|
447 (cdr-safe
|
|
448 (assoc (widget-value widget)
|
|
449 (w3-form-element-options formobj)))))
|
0
|
450 (keygen
|
70
|
451 (cons (w3-form-element-name formobj)
|
|
452 (format "Should create a %d bit RSA key"
|
|
453 (widget-value widget))))
|
0
|
454 ((multiline hidden)
|
|
455 (cons (w3-form-element-name formobj)
|
|
456 (w3-form-element-value formobj)))
|
|
457 (otherwise
|
|
458 (cons (w3-form-element-name formobj)
|
|
459 (widget-value widget)))))
|
|
460 (if temp
|
|
461 (setq result (cons temp result))))
|
|
462 result))
|
|
463
|
|
464 (defun w3-form-encode-make-mime-part (id data separator)
|
|
465 (concat separator "\nContent-id: " id
|
|
466 "\nContent-length: " (length data)
|
|
467 "\n\n" data))
|
|
468
|
|
469 (defun w3-form-encode-multipart/x-www-form-data (formobjs)
|
|
470 ;; Create a multipart form submission.
|
|
471 ;; Returns a cons of two strings. Car is the separator used.
|
|
472 ;; cdr is the body of the MIME message."
|
|
473 (let ((separator "---some-separator-for-www-form-data"))
|
|
474 (cons separator
|
|
475 (mapconcat
|
|
476 (function
|
|
477 (lambda (formobj)
|
|
478 (w3-form-encode-make-mime-part (car formobj) (cdr formobj)
|
|
479 separator)))
|
|
480 (w3-form-encode-helper formobjs)
|
|
481 "\n"))))
|
|
482
|
|
483 (fset 'w3-form-encode-multipart/form-data
|
|
484 'w3-form-encode-multipart/x-www-form-data)
|
|
485 (fset 'w3-form-encode- 'w3-form-encode-application/x-www-form-urlencoded)
|
|
486
|
|
487 (defun w3-next-widget (pos)
|
|
488 (let* ((next (cond ((get-text-property pos 'button)
|
|
489 (next-single-property-change pos 'button))
|
|
490 ((get-text-property pos 'field)
|
|
491 (next-single-property-change pos 'field))
|
|
492 (t pos)))
|
80
|
493 (button (and next (next-single-property-change next 'button)))
|
|
494 (field (and next (next-single-property-change next 'field))))
|
0
|
495 (setq next
|
|
496 (cond
|
|
497 ((and button field) (min button field))
|
|
498 (button button)
|
|
499 (field field)
|
|
500 (t nil)))
|
|
501 (and next
|
|
502 (or (get-text-property next 'button)
|
|
503 (get-text-property next 'field)))))
|
|
504
|
|
505 (defun w3-form-encode (result &optional enctype)
|
|
506 "Create a string suitably encoded for a URL request."
|
|
507 (let ((func (intern (concat "w3-form-encode-" enctype))))
|
2
|
508 (if (fboundp func)
|
|
509 (funcall func result)
|
|
510 (w3-warn 'html (format "Bad encoding type for form data: %s" enctype))
|
|
511 (w3-form-encode-application/x-www-form-urlencoded result))))
|
0
|
512
|
|
513 (defun w3-form-encode-text/plain (result)
|
|
514 (let ((query ""))
|
|
515 (setq query
|
|
516 (mapconcat
|
|
517 (function
|
|
518 (lambda (widget)
|
|
519 (let ((nam (car widget))
|
|
520 (val (cdr widget)))
|
|
521 (if (string-match "\n" nam)
|
|
522 (setq nam (mapconcat
|
|
523 (function
|
|
524 (lambda (x)
|
|
525 (if (= x ?\n) "," (char-to-string x))))
|
|
526 nam "")))
|
|
527 (concat nam " " val))))
|
|
528 (w3-form-encode-helper result) "\n"))
|
|
529 query))
|
|
530
|
70
|
531 (defun w3-form-encode-application/x-w3-wais (result)
|
|
532 (cdr (car (w3-form-encode-helper result))))
|
|
533
|
0
|
534 (defun w3-form-encode-application/x-gopher-query (result)
|
2
|
535 (concat "\t" (cdr (car (w3-form-encode-helper result)))))
|
0
|
536
|
|
537 (defun w3-form-encode-xwfu (chunk)
|
|
538 "Escape characters in a string for application/x-www-form-urlencoded.
|
|
539 Blasphemous crap because someone didn't think %20 was good enough for encoding
|
|
540 spaces. Die Die Die."
|
|
541 (mapconcat
|
|
542 (function
|
|
543 (lambda (char)
|
|
544 (cond
|
|
545 ((= char ? ) "+")
|
80
|
546 ((memq char url-unreserved-chars) (char-to-string char))
|
2
|
547 (t (upcase (format "%%%02x" char))))))
|
80
|
548 (mule-encode-string chunk) ""))
|
0
|
549
|
|
550 (defun w3-form-encode-application/x-www-form-urlencoded (result)
|
|
551 (mapconcat
|
|
552 (function
|
|
553 (lambda (data)
|
|
554 (concat (w3-form-encode-xwfu (car data)) "="
|
|
555 (w3-form-encode-xwfu (cdr data)))))
|
|
556 (w3-form-encode-helper result) "&"))
|
|
557
|
|
558 (defun w3-form-encode-application/x-w3-isindex (result)
|
|
559 (let* ((info (w3-form-encode-helper result))
|
|
560 (query (cdr-safe (assoc "isindex" info))))
|
|
561 (if query
|
|
562 (url-hexify-string query)
|
|
563 "")))
|
|
564
|
|
565 (defun w3-form-encode-application/gopher-ask-block (result)
|
|
566 (let ((query ""))
|
|
567 ;;; gopher+ will expect all the checkboxes/etc, even if they are
|
|
568 ;;; not turned on. Should still ignore RADIO boxes that are not
|
|
569 ;;; active though.
|
|
570 (while result
|
|
571 (if (and (not (and (string= (nth 2 (car result)) "RADIO")
|
|
572 (not (nth 6 (car result)))))
|
|
573 (not (member (nth 2 (car result)) '("SUBMIT" "RESET"))))
|
|
574 (setq query (format "%s\r\n%s" query (nth 5 (car result)))))
|
|
575 (setq result (cdr result)))
|
|
576 (concat query "\r\n.\r\n")))
|
|
577
|
|
578 (defun w3-submit-form (ident)
|
|
579 ;; Submit form entry fields matching ACTN as their action identifier.
|
|
580 (let* ((result (w3-all-widgets ident))
|
80
|
581 (enctype (or (cdr (assq 'enctype ident))
|
|
582 "application/x-www-form-urlencoded"))
|
0
|
583 (query (w3-form-encode result enctype))
|
|
584 (themeth (upcase (or (cdr (assq 'method ident)) "get")))
|
|
585 (theurl (cdr (assq 'action ident))))
|
|
586 (if (and (string= "GET" themeth)
|
|
587 (string-match "\\([^\\?]*\\)\\?" theurl))
|
|
588 (setq theurl (url-match theurl 1)))
|
|
589 (cond
|
|
590 ((or (string= "POST" themeth)
|
|
591 (string= "PUT" themeth))
|
|
592 (if (consp query)
|
|
593 (setq enctype (concat enctype "; separator=\""
|
|
594 (substring (car query) 3 nil)
|
|
595 "\"")
|
|
596 query (cdr query)))
|
|
597 (let ((url-request-method themeth)
|
|
598 (url-request-data query)
|
|
599 (url-request-extra-headers
|
|
600 (cons (cons "Content-type" enctype) url-request-extra-headers)))
|
|
601 (w3-fetch theurl)))
|
|
602 ((string= "GET" themeth)
|
2
|
603 (let ((theurl (concat theurl (if (string-match "gopher" enctype)
|
|
604 "" "?") query)))
|
0
|
605 (w3-fetch theurl)))
|
|
606 (t
|
|
607 (w3-warn 'html (format "Unknown submit method: %s" themeth))
|
|
608 (let ((theurl (concat theurl "?" query)))
|
|
609 (w3-fetch theurl))))))
|
|
610
|
|
611 (provide 'w3-forms)
|