Commit | Line | Data |
---|---|---|
266c63b5 AK |
1 | ;;; eww.el --- Emacs Web Wowser |
2 | ||
3 | ;; Copyright (C) 2013 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
6 | ;; Keywords: html | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation, either version 3 of the License, or | |
13 | ;; (at your option) any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ||
25 | ;;; Code: | |
26 | ||
27 | (eval-when-compile (require 'cl)) | |
7545bd25 | 28 | (require 'format-spec) |
266c63b5 AK |
29 | (require 'shr) |
30 | (require 'url) | |
2644071e | 31 | (require 'mm-url) |
266c63b5 | 32 | |
c74cb344 G |
33 | (defgroup eww nil |
34 | "Emacs Web Wowser" | |
35 | :version "24.4" | |
36 | :group 'hypermedia | |
37 | :prefix "eww-") | |
38 | ||
39 | (defcustom eww-header-line-format "%t: %u" | |
40 | "Header line format. | |
41 | - %t is replaced by the title. | |
42 | - %u is replaced by the URL." | |
a3ca09b9 IK |
43 | :version "24.4" |
44 | :group 'eww | |
45 | :type 'string) | |
46 | ||
47 | (defcustom eww-search-prefix "https://duckduckgo.com/html/?q=" | |
48 | "Prefix URL to search engine" | |
49 | :version "24.4" | |
c74cb344 G |
50 | :group 'eww |
51 | :type 'string) | |
52 | ||
970ad972 G |
53 | (defface eww-form-submit |
54 | '((((type x w32 ns) (class color)) ; Like default mode line | |
55 | :box (:line-width 2 :style released-button) | |
56 | :background "#808080" :foreground "black")) | |
57 | "Face for eww buffer buttons." | |
58 | :version "24.4" | |
59 | :group 'eww) | |
60 | ||
61 | (defface eww-form-checkbox | |
62 | '((((type x w32 ns) (class color)) ; Like default mode line | |
63 | :box (:line-width 2 :style released-button) | |
64 | :background "lightgrey" :foreground "black")) | |
65 | "Face for eww buffer buttons." | |
66 | :version "24.4" | |
67 | :group 'eww) | |
68 | ||
69 | (defface eww-form-select | |
be2aa135 LMI |
70 | '((((type x w32 ns) (class color)) ; Like default mode line |
71 | :box (:line-width 2 :style released-button) | |
72 | :background "lightgrey" :foreground "black")) | |
73 | "Face for eww buffer buttons." | |
74 | :version "24.4" | |
75 | :group 'eww) | |
76 | ||
970ad972 G |
77 | (defface eww-form-text |
78 | '((t (:background "#505050" | |
79 | :foreground "white" | |
80 | :box (:line-width 1)))) | |
81 | "Face for eww text inputs." | |
82 | :version "24.4" | |
83 | :group 'eww) | |
84 | ||
266c63b5 | 85 | (defvar eww-current-url nil) |
c74cb344 G |
86 | (defvar eww-current-title "" |
87 | "Title of current page.") | |
266c63b5 | 88 | (defvar eww-history nil) |
d3f0f918 | 89 | (defvar eww-history-position 0) |
266c63b5 | 90 | |
924d6997 G |
91 | (defvar eww-next-url nil) |
92 | (defvar eww-previous-url nil) | |
93 | (defvar eww-up-url nil) | |
970ad972 G |
94 | (defvar eww-home-url nil) |
95 | (defvar eww-start-url nil) | |
96 | (defvar eww-contents-url nil) | |
924d6997 | 97 | |
d583b36b | 98 | ;;;###autoload |
266c63b5 | 99 | (defun eww (url) |
a3ca09b9 IK |
100 | "Fetch URL and render the page. |
101 | If the input doesn't look like an URL or a domain name, the | |
102 | word(s) will be searched for via `eww-search-prefix'." | |
103 | (interactive "sEnter URL or keywords: ") | |
104 | (if (and (= (length (split-string url)) 1) | |
105 | (> (length (split-string url "\\.")) 1)) | |
71d4c19d IK |
106 | (progn |
107 | (unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" url) | |
108 | (setq url (concat "http://" url))) | |
109 | ;; some site don't redirect final / | |
110 | (when (string= (url-filename (url-generic-parse-url url)) "") | |
111 | (setq url (concat url "/")))) | |
be549ce6 | 112 | (unless (string-match-p "\\'file:" url) |
b89fc156 IK |
113 | (setq url (concat eww-search-prefix |
114 | (replace-regexp-in-string " " "+" url))))) | |
266c63b5 AK |
115 | (url-retrieve url 'eww-render (list url))) |
116 | ||
924d6997 G |
117 | ;;;###autoload |
118 | (defun eww-open-file (file) | |
119 | "Render a file using EWW." | |
120 | (interactive "fFile: ") | |
121 | (eww (concat "file://" (expand-file-name file)))) | |
122 | ||
266c63b5 | 123 | (defun eww-render (status url &optional point) |
c74cb344 G |
124 | (let ((redirect (plist-get status :redirect))) |
125 | (when redirect | |
126 | (setq url redirect))) | |
924d6997 G |
127 | (set (make-local-variable 'eww-next-url) nil) |
128 | (set (make-local-variable 'eww-previous-url) nil) | |
129 | (set (make-local-variable 'eww-up-url) nil) | |
970ad972 G |
130 | (set (make-local-variable 'eww-home-url) nil) |
131 | (set (make-local-variable 'eww-start-url) nil) | |
132 | (set (make-local-variable 'eww-contents-url) nil) | |
266c63b5 | 133 | (let* ((headers (eww-parse-headers)) |
c74cb344 G |
134 | (shr-target-id |
135 | (and (string-match "#\\(.*\\)" url) | |
136 | (match-string 1 url))) | |
266c63b5 AK |
137 | (content-type |
138 | (mail-header-parse-content-type | |
139 | (or (cdr (assoc "content-type" headers)) | |
140 | "text/plain"))) | |
141 | (charset (intern | |
142 | (downcase | |
143 | (or (cdr (assq 'charset (cdr content-type))) | |
d652f4d0 G |
144 | (eww-detect-charset (equal (car content-type) |
145 | "text/html")) | |
266c63b5 AK |
146 | "utf8")))) |
147 | (data-buffer (current-buffer))) | |
148 | (unwind-protect | |
149 | (progn | |
150 | (cond | |
151 | ((equal (car content-type) "text/html") | |
152 | (eww-display-html charset url)) | |
153 | ((string-match "^image/" (car content-type)) | |
154 | (eww-display-image)) | |
155 | (t | |
156 | (eww-display-raw charset))) | |
c74cb344 G |
157 | (cond |
158 | (point | |
159 | (goto-char point)) | |
160 | (shr-target-id | |
161 | (let ((point (next-single-property-change | |
162 | (point-min) 'shr-target-id))) | |
163 | (when point | |
164 | (goto-char (1+ point))))))) | |
266c63b5 AK |
165 | (kill-buffer data-buffer)))) |
166 | ||
167 | (defun eww-parse-headers () | |
168 | (let ((headers nil)) | |
d652f4d0 | 169 | (goto-char (point-min)) |
266c63b5 AK |
170 | (while (and (not (eobp)) |
171 | (not (eolp))) | |
172 | (when (looking-at "\\([^:]+\\): *\\(.*\\)") | |
173 | (push (cons (downcase (match-string 1)) | |
174 | (match-string 2)) | |
175 | headers)) | |
176 | (forward-line 1)) | |
177 | (unless (eobp) | |
178 | (forward-line 1)) | |
179 | headers)) | |
180 | ||
db5a34ca KY |
181 | (defun eww-detect-charset (html-p) |
182 | (let ((case-fold-search t) | |
183 | (pt (point))) | |
184 | (or (and html-p | |
185 | (re-search-forward | |
b89fc156 | 186 | "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t) |
db5a34ca KY |
187 | (goto-char pt) |
188 | (match-string 1)) | |
189 | (and (looking-at | |
190 | "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)") | |
191 | (match-string 1))))) | |
192 | ||
266c63b5 AK |
193 | (defun eww-display-html (charset url) |
194 | (unless (eq charset 'utf8) | |
195 | (decode-coding-region (point) (point-max) charset)) | |
196 | (let ((document | |
197 | (list | |
198 | 'base (list (cons 'href url)) | |
199 | (libxml-parse-html-region (point) (point-max))))) | |
200 | (eww-setup-buffer) | |
201 | (setq eww-current-url url) | |
c74cb344 | 202 | (eww-update-header-line-format) |
2644071e | 203 | (let ((inhibit-read-only t) |
970ad972 | 204 | (after-change-functions nil) |
c74cb344 | 205 | (shr-width nil) |
2644071e | 206 | (shr-external-rendering-functions |
c74cb344 G |
207 | '((title . eww-tag-title) |
208 | (form . eww-tag-form) | |
2644071e | 209 | (input . eww-tag-input) |
c74cb344 G |
210 | (textarea . eww-tag-textarea) |
211 | (body . eww-tag-body) | |
924d6997 G |
212 | (select . eww-tag-select) |
213 | (link . eww-tag-link) | |
214 | (a . eww-tag-a)))) | |
970ad972 | 215 | (shr-insert-document document)) |
266c63b5 AK |
216 | (goto-char (point-min)))) |
217 | ||
924d6997 G |
218 | (defun eww-handle-link (cont) |
219 | (let* ((rel (assq :rel cont)) | |
220 | (href (assq :href cont)) | |
970ad972 G |
221 | (where (assoc |
222 | ;; The text associated with :rel is case-insensitive. | |
223 | (if rel (downcase (cdr rel))) | |
924d6997 | 224 | '(("next" . eww-next-url) |
970ad972 G |
225 | ;; Texinfo uses "previous", but HTML specifies |
226 | ;; "prev", so recognize both. | |
924d6997 | 227 | ("previous" . eww-previous-url) |
970ad972 G |
228 | ("prev" . eww-previous-url) |
229 | ;; HTML specifies "start" but also "contents", | |
230 | ;; and Gtk seems to use "home". Recognize | |
231 | ;; them all; but store them in different | |
232 | ;; variables so that we can readily choose the | |
233 | ;; "best" one. | |
234 | ("start" . eww-start-url) | |
235 | ("home" . eww-home-url) | |
236 | ("contents" . eww-contents-url) | |
924d6997 G |
237 | ("up" . eww-up-url))))) |
238 | (and href | |
239 | where | |
240 | (set (cdr where) (cdr href))))) | |
241 | ||
242 | (defun eww-tag-link (cont) | |
243 | (eww-handle-link cont) | |
244 | (shr-generic cont)) | |
245 | ||
246 | (defun eww-tag-a (cont) | |
247 | (eww-handle-link cont) | |
248 | (shr-tag-a cont)) | |
249 | ||
c74cb344 G |
250 | (defun eww-update-header-line-format () |
251 | (if eww-header-line-format | |
d80a808f LMI |
252 | (setq header-line-format |
253 | (replace-regexp-in-string | |
254 | "%" "%%" | |
255 | (format-spec eww-header-line-format | |
256 | `((?u . ,eww-current-url) | |
257 | (?t . ,eww-current-title))))) | |
c74cb344 G |
258 | (setq header-line-format nil))) |
259 | ||
260 | (defun eww-tag-title (cont) | |
261 | (setq eww-current-title "") | |
262 | (dolist (sub cont) | |
263 | (when (eq (car sub) 'text) | |
264 | (setq eww-current-title (concat eww-current-title (cdr sub))))) | |
265 | (eww-update-header-line-format)) | |
266 | ||
267 | (defun eww-tag-body (cont) | |
268 | (let* ((start (point)) | |
269 | (fgcolor (cdr (or (assq :fgcolor cont) | |
270 | (assq :text cont)))) | |
271 | (bgcolor (cdr (assq :bgcolor cont))) | |
272 | (shr-stylesheet (list (cons 'color fgcolor) | |
273 | (cons 'background-color bgcolor)))) | |
274 | (shr-generic cont) | |
275 | (eww-colorize-region start (point) fgcolor bgcolor))) | |
276 | ||
277 | (defun eww-colorize-region (start end fg &optional bg) | |
278 | (when (or fg bg) | |
279 | (let ((new-colors (shr-color-check fg bg))) | |
280 | (when new-colors | |
281 | (when fg | |
544d4594 | 282 | (add-face-text-property start end |
970ad972 G |
283 | (list :foreground (cadr new-colors)) |
284 | t)) | |
c74cb344 | 285 | (when bg |
544d4594 | 286 | (add-face-text-property start end |
970ad972 G |
287 | (list :background (car new-colors)) |
288 | t)))))) | |
c74cb344 | 289 | |
266c63b5 AK |
290 | (defun eww-display-raw (charset) |
291 | (let ((data (buffer-substring (point) (point-max)))) | |
292 | (eww-setup-buffer) | |
293 | (let ((inhibit-read-only t)) | |
294 | (insert data)) | |
295 | (goto-char (point-min)))) | |
296 | ||
297 | (defun eww-display-image () | |
298 | (let ((data (buffer-substring (point) (point-max)))) | |
299 | (eww-setup-buffer) | |
300 | (let ((inhibit-read-only t)) | |
301 | (shr-put-image data nil)) | |
302 | (goto-char (point-min)))) | |
303 | ||
304 | (defun eww-setup-buffer () | |
305 | (pop-to-buffer (get-buffer-create "*eww*")) | |
2644071e | 306 | (remove-overlays) |
266c63b5 AK |
307 | (let ((inhibit-read-only t)) |
308 | (erase-buffer)) | |
309 | (eww-mode)) | |
310 | ||
311 | (defvar eww-mode-map | |
312 | (let ((map (make-sparse-keymap))) | |
313 | (suppress-keymap map) | |
314 | (define-key map "q" 'eww-quit) | |
f22255bd | 315 | (define-key map "g" 'eww-reload) |
7304e4dd LMI |
316 | (define-key map [tab] 'shr-next-link) |
317 | (define-key map [backtab] 'shr-previous-link) | |
266c63b5 AK |
318 | (define-key map [delete] 'scroll-down-command) |
319 | (define-key map "\177" 'scroll-down-command) | |
320 | (define-key map " " 'scroll-up-command) | |
924d6997 | 321 | (define-key map "l" 'eww-back-url) |
d3f0f918 | 322 | (define-key map "f" 'eww-forward-url) |
924d6997 | 323 | (define-key map "n" 'eww-next-url) |
266c63b5 | 324 | (define-key map "p" 'eww-previous-url) |
924d6997 G |
325 | (define-key map "u" 'eww-up-url) |
326 | (define-key map "t" 'eww-top-url) | |
f865b474 | 327 | (define-key map "w" 'eww-browse-with-external-browser) |
b89fc156 | 328 | (define-key map "y" 'eww-yank-page-url) |
266c63b5 AK |
329 | map)) |
330 | ||
d652f4d0 | 331 | (define-derived-mode eww-mode nil "eww" |
266c63b5 AK |
332 | "Mode for browsing the web. |
333 | ||
334 | \\{eww-mode-map}" | |
266c63b5 | 335 | (set (make-local-variable 'eww-current-url) 'author) |
970ad972 G |
336 | (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url) |
337 | (set (make-local-variable 'after-change-functions) 'eww-process-text-input) | |
338 | ;;(setq buffer-read-only t) | |
339 | ) | |
266c63b5 | 340 | |
d3f0f918 LMI |
341 | (defun eww-save-history () |
342 | (let ((elem (list :url eww-current-url | |
343 | :point (point) | |
344 | :text (buffer-string)))) | |
345 | (if (or (zerop eww-history-position) | |
346 | (= eww-history-position (length eww-history))) | |
347 | (push elem eww-history) | |
348 | (setcdr (nthcdr eww-history-position eww-history) | |
349 | (cons elem (nthcdr eww-history-position eww-history)))))) | |
350 | ||
266c63b5 | 351 | (defun eww-browse-url (url &optional new-window) |
970ad972 G |
352 | (when (and (equal major-mode 'eww-mode) |
353 | eww-current-url) | |
d3f0f918 | 354 | (eww-save-history)) |
5c3087e9 | 355 | (eww url)) |
266c63b5 AK |
356 | |
357 | (defun eww-quit () | |
358 | "Exit the Emacs Web Wowser." | |
359 | (interactive) | |
360 | (setq eww-history nil) | |
361 | (kill-buffer (current-buffer))) | |
362 | ||
924d6997 | 363 | (defun eww-back-url () |
266c63b5 AK |
364 | "Go to the previously displayed page." |
365 | (interactive) | |
d3f0f918 | 366 | (when (>= eww-history-position (length eww-history)) |
266c63b5 | 367 | (error "No previous page")) |
d3f0f918 LMI |
368 | (eww-restore-history |
369 | (if (not (zerop eww-history-position)) | |
370 | (elt eww-history eww-history-position) | |
371 | (eww-save-history) | |
372 | (elt eww-history (1+ eww-history-position)))) | |
373 | (setq eww-history-position (1+ eww-history-position))) | |
374 | ||
375 | (defun eww-forward-url () | |
376 | "Go to the next displayed page." | |
377 | (interactive) | |
378 | (when (zerop eww-history-position) | |
379 | (error "No next page")) | |
380 | (eww-restore-history (elt eww-history (1- eww-history-position))) | |
381 | (setq eww-history-position (1- eww-history-position))) | |
382 | ||
383 | (defun eww-restore-history (elem) | |
384 | (let ((inhibit-read-only t)) | |
e82b0991 | 385 | (erase-buffer) |
d3f0f918 LMI |
386 | (insert (plist-get elem :text)) |
387 | (goto-char (plist-get elem :point)) | |
388 | (setq eww-current-url (plist-get elem :url)))) | |
266c63b5 | 389 | |
924d6997 G |
390 | (defun eww-next-url () |
391 | "Go to the page marked `next'. | |
392 | A page is marked `next' if rel=\"next\" appears in a <link> | |
393 | or <a> tag." | |
394 | (interactive) | |
395 | (if eww-next-url | |
396 | (eww-browse-url (shr-expand-url eww-next-url eww-current-url)) | |
397 | (error "No `next' on this page"))) | |
398 | ||
399 | (defun eww-previous-url () | |
400 | "Go to the page marked `previous'. | |
401 | A page is marked `previous' if rel=\"previous\" appears in a <link> | |
402 | or <a> tag." | |
403 | (interactive) | |
404 | (if eww-previous-url | |
405 | (eww-browse-url (shr-expand-url eww-previous-url eww-current-url)) | |
406 | (error "No `previous' on this page"))) | |
407 | ||
408 | (defun eww-up-url () | |
409 | "Go to the page marked `up'. | |
410 | A page is marked `up' if rel=\"up\" appears in a <link> | |
411 | or <a> tag." | |
412 | (interactive) | |
413 | (if eww-up-url | |
414 | (eww-browse-url (shr-expand-url eww-up-url eww-current-url)) | |
415 | (error "No `up' on this page"))) | |
416 | ||
417 | (defun eww-top-url () | |
418 | "Go to the page marked `top'. | |
970ad972 G |
419 | A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\" |
420 | appears in a <link> or <a> tag." | |
924d6997 | 421 | (interactive) |
970ad972 G |
422 | (let ((best-url (or eww-start-url |
423 | eww-contents-url | |
424 | eww-home-url))) | |
425 | (if best-url | |
426 | (eww-browse-url (shr-expand-url best-url eww-current-url)) | |
427 | (error "No `top' for this page")))) | |
924d6997 | 428 | |
f22255bd LMI |
429 | (defun eww-reload () |
430 | "Reload the current page." | |
431 | (interactive) | |
432 | (url-retrieve eww-current-url 'eww-render | |
433 | (list eww-current-url (point)))) | |
434 | ||
2644071e LMI |
435 | ;; Form support. |
436 | ||
437 | (defvar eww-form nil) | |
438 | ||
970ad972 G |
439 | (defvar eww-submit-map |
440 | (let ((map (make-sparse-keymap))) | |
441 | (define-key map "\r" 'eww-submit) | |
e854cfc7 | 442 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
443 | map)) |
444 | ||
445 | (defvar eww-checkbox-map | |
446 | (let ((map (make-sparse-keymap))) | |
447 | (define-key map [space] 'eww-toggle-checkbox) | |
448 | (define-key map "\r" 'eww-toggle-checkbox) | |
e854cfc7 | 449 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
450 | map)) |
451 | ||
452 | (defvar eww-text-map | |
453 | (let ((map (make-keymap))) | |
454 | (set-keymap-parent map text-mode-map) | |
455 | (define-key map "\r" 'eww-submit) | |
456 | (define-key map [(control a)] 'eww-beginning-of-text) | |
e854cfc7 | 457 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
458 | (define-key map [(control e)] 'eww-end-of-text) |
459 | (define-key map [tab] 'shr-next-link) | |
460 | (define-key map [backtab] 'shr-previous-link) | |
461 | map)) | |
462 | ||
463 | (defvar eww-textarea-map | |
464 | (let ((map (make-keymap))) | |
465 | (set-keymap-parent map text-mode-map) | |
466 | (define-key map "\r" 'forward-line) | |
e854cfc7 | 467 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
468 | (define-key map [tab] 'shr-next-link) |
469 | (define-key map [backtab] 'shr-previous-link) | |
470 | map)) | |
471 | ||
472 | (defvar eww-select-map | |
473 | (let ((map (make-sparse-keymap))) | |
474 | (define-key map "\r" 'eww-change-select) | |
e854cfc7 | 475 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
476 | map)) |
477 | ||
478 | (defun eww-beginning-of-text () | |
479 | "Move to the start of the input field." | |
480 | (interactive) | |
481 | (goto-char (eww-beginning-of-field))) | |
482 | ||
483 | (defun eww-end-of-text () | |
484 | "Move to the end of the text in the input field." | |
485 | (interactive) | |
486 | (goto-char (eww-end-of-field)) | |
487 | (let ((start (eww-beginning-of-field))) | |
488 | (while (and (equal (following-char) ? ) | |
489 | (> (point) start)) | |
490 | (forward-char -1)) | |
491 | (when (> (point) start) | |
492 | (forward-char 1)))) | |
493 | ||
494 | (defun eww-beginning-of-field () | |
495 | (cond | |
496 | ((bobp) | |
497 | (point)) | |
498 | ((not (eq (get-text-property (point) 'eww-form) | |
499 | (get-text-property (1- (point)) 'eww-form))) | |
500 | (point)) | |
501 | (t | |
502 | (previous-single-property-change | |
503 | (point) 'eww-form nil (point-min))))) | |
504 | ||
505 | (defun eww-end-of-field () | |
506 | (1- (next-single-property-change | |
507 | (point) 'eww-form nil (point-max)))) | |
508 | ||
2644071e LMI |
509 | (defun eww-tag-form (cont) |
510 | (let ((eww-form | |
511 | (list (assq :method cont) | |
512 | (assq :action cont))) | |
513 | (start (point))) | |
514 | (shr-ensure-paragraph) | |
515 | (shr-generic cont) | |
3d95242e LMI |
516 | (unless (bolp) |
517 | (insert "\n")) | |
518 | (insert "\n") | |
001b9fbe LMI |
519 | (when (> (point) start) |
520 | (put-text-property start (1+ start) | |
521 | 'eww-form eww-form)))) | |
2644071e | 522 | |
970ad972 G |
523 | (defun eww-form-submit (cont) |
524 | (let ((start (point)) | |
525 | (value (cdr (assq :value cont)))) | |
526 | (setq value | |
527 | (if (zerop (length value)) | |
528 | "Submit" | |
529 | value)) | |
530 | (insert value) | |
531 | (add-face-text-property start (point) 'eww-form-submit) | |
532 | (put-text-property start (point) 'eww-form | |
533 | (list :eww-form eww-form | |
534 | :value value | |
535 | :type "submit" | |
536 | :name (cdr (assq :name cont)))) | |
537 | (put-text-property start (point) 'keymap eww-submit-map) | |
538 | (insert " "))) | |
539 | ||
540 | (defun eww-form-checkbox (cont) | |
541 | (let ((start (point))) | |
542 | (if (cdr (assq :checked cont)) | |
543 | (insert "[X]") | |
544 | (insert "[ ]")) | |
545 | (add-face-text-property start (point) 'eww-form-checkbox) | |
546 | (put-text-property start (point) 'eww-form | |
547 | (list :eww-form eww-form | |
548 | :value (cdr (assq :value cont)) | |
549 | :type (downcase (cdr (assq :type cont))) | |
550 | :checked (cdr (assq :checked cont)) | |
551 | :name (cdr (assq :name cont)))) | |
552 | (put-text-property start (point) 'keymap eww-checkbox-map) | |
553 | (insert " "))) | |
554 | ||
555 | (defun eww-form-text (cont) | |
556 | (let ((start (point)) | |
557 | (type (downcase (or (cdr (assq :type cont)) | |
558 | "text"))) | |
559 | (value (or (cdr (assq :value cont)) "")) | |
560 | (width (string-to-number | |
561 | (or (cdr (assq :size cont)) | |
562 | "40")))) | |
563 | (insert value) | |
564 | (when (< (length value) width) | |
565 | (insert (make-string (- width (length value)) ? ))) | |
566 | (put-text-property start (point) 'face 'eww-form-text) | |
567 | (put-text-property start (point) 'local-map eww-text-map) | |
568 | (put-text-property start (point) 'inhibit-read-only t) | |
569 | (put-text-property start (point) 'eww-form | |
570 | (list :eww-form eww-form | |
571 | :value value | |
572 | :type type | |
573 | :name (cdr (assq :name cont)))) | |
574 | (insert " "))) | |
575 | ||
576 | (defun eww-process-text-input (beg end length) | |
577 | (let* ((form (get-text-property end 'eww-form)) | |
578 | (properties (text-properties-at end)) | |
579 | (type (plist-get form :type))) | |
580 | (when (and form | |
581 | (member type '("text" "password" "textarea"))) | |
582 | (cond | |
583 | ((zerop length) | |
584 | ;; Delete some space at the end. | |
585 | (save-excursion | |
586 | (goto-char | |
587 | (if (equal type "textarea") | |
588 | (1- (line-end-position)) | |
589 | (eww-end-of-field))) | |
590 | (let ((new (- end beg))) | |
591 | (while (and (> new 0) | |
592 | (eql (following-char) ? )) | |
593 | (delete-region (point) (1+ (point))) | |
594 | (setq new (1- new)))) | |
595 | (set-text-properties beg end properties))) | |
596 | ((> length 0) | |
597 | ;; Add padding. | |
598 | (save-excursion | |
599 | (goto-char | |
600 | (if (equal type "textarea") | |
601 | (1- (line-end-position)) | |
602 | (eww-end-of-field))) | |
603 | (let ((start (point))) | |
604 | (insert (make-string length ? )) | |
605 | (set-text-properties start (point) properties))))) | |
606 | (let ((value (buffer-substring-no-properties | |
607 | (eww-beginning-of-field) | |
608 | (eww-end-of-field)))) | |
609 | (when (string-match " +\\'" value) | |
610 | (setq value (substring value 0 (match-beginning 0)))) | |
611 | (plist-put form :value value) | |
612 | (when (equal type "password") | |
613 | ;; Display passwords as asterisks. | |
614 | (let ((start (eww-beginning-of-field))) | |
615 | (put-text-property start (+ start (length value)) | |
616 | 'display (make-string (length value) ?*)))))))) | |
9ddf23f0 | 617 | |
c74cb344 | 618 | (defun eww-tag-textarea (cont) |
970ad972 G |
619 | (let ((start (point)) |
620 | (value (or (cdr (assq :value cont)) "")) | |
621 | (lines (string-to-number | |
622 | (or (cdr (assq :rows cont)) | |
623 | "10"))) | |
624 | (width (string-to-number | |
625 | (or (cdr (assq :cols cont)) | |
626 | "10"))) | |
627 | end) | |
628 | (shr-ensure-newline) | |
629 | (insert value) | |
630 | (shr-ensure-newline) | |
631 | (when (< (count-lines start (point)) lines) | |
632 | (dotimes (i (- lines (count-lines start (point)))) | |
633 | (insert "\n"))) | |
634 | (setq end (point-marker)) | |
635 | (goto-char start) | |
636 | (while (< (point) end) | |
637 | (end-of-line) | |
638 | (let ((pad (- width (- (point) (line-beginning-position))))) | |
639 | (when (> pad 0) | |
640 | (insert (make-string pad ? )))) | |
641 | (add-face-text-property (line-beginning-position) | |
642 | (point) 'eww-form-text) | |
643 | (put-text-property (line-beginning-position) (point) | |
644 | 'local-map eww-textarea-map) | |
645 | (forward-line 1)) | |
646 | (put-text-property start (point) 'eww-form | |
647 | (list :eww-form eww-form | |
648 | :value value | |
649 | :type "textarea" | |
650 | :name (cdr (assq :name cont)))))) | |
651 | ||
652 | (defun eww-tag-input (cont) | |
653 | (let ((type (downcase (or (cdr (assq :type cont)) | |
654 | "text"))) | |
655 | (start (point))) | |
656 | (cond | |
657 | ((or (equal type "checkbox") | |
658 | (equal type "radio")) | |
659 | (eww-form-checkbox cont)) | |
660 | ((equal type "submit") | |
661 | (eww-form-submit cont)) | |
662 | ((equal type "hidden") | |
663 | (let ((form eww-form) | |
664 | (name (cdr (assq :name cont)))) | |
665 | ;; Don't add <input type=hidden> elements repeatedly. | |
666 | (while (and form | |
667 | (or (not (consp (car form))) | |
668 | (not (eq (caar form) 'hidden)) | |
669 | (not (equal (plist-get (cdr (car form)) :name) | |
670 | name)))) | |
671 | (setq form (cdr form))) | |
672 | (unless form | |
673 | (nconc eww-form (list | |
674 | (list 'hidden | |
675 | :name name | |
676 | :value (cdr (assq :value cont)))))))) | |
677 | (t | |
678 | (eww-form-text cont))) | |
679 | (unless (= start (point)) | |
680 | (put-text-property start (1+ start) 'help-echo "Input field")))) | |
c74cb344 | 681 | |
9ddf23f0 LMI |
682 | (defun eww-tag-select (cont) |
683 | (shr-ensure-paragraph) | |
970ad972 | 684 | (let ((menu (list :name (cdr (assq :name cont)) |
9ddf23f0 LMI |
685 | :eww-form eww-form)) |
686 | (options nil) | |
970ad972 G |
687 | (start (point)) |
688 | (max 0)) | |
9ddf23f0 LMI |
689 | (dolist (elem cont) |
690 | (when (eq (car elem) 'option) | |
691 | (when (cdr (assq :selected (cdr elem))) | |
692 | (nconc menu (list :value | |
693 | (cdr (assq :value (cdr elem)))))) | |
970ad972 G |
694 | (let ((display (or (cdr (assq 'text (cdr elem))) ""))) |
695 | (setq max (max max (length display))) | |
696 | (push (list 'item | |
697 | :value (cdr (assq :value (cdr elem))) | |
698 | :display display) | |
699 | options)))) | |
be2aa135 | 700 | (when options |
970ad972 | 701 | (setq options (nreverse options)) |
be2aa135 | 702 | ;; If we have no selected values, default to the first value. |
970ad972 | 703 | (unless (plist-get menu :value) |
be2aa135 LMI |
704 | (nconc menu (list :value (nth 2 (car options))))) |
705 | (nconc menu options) | |
970ad972 G |
706 | (let ((selected (eww-select-display menu))) |
707 | (insert selected | |
708 | (make-string (- max (length selected)) ? ))) | |
709 | (put-text-property start (point) 'eww-form menu) | |
710 | (add-face-text-property start (point) 'eww-form-select) | |
711 | (put-text-property start (point) 'keymap eww-select-map) | |
be2aa135 | 712 | (shr-ensure-paragraph)))) |
2644071e | 713 | |
970ad972 G |
714 | (defun eww-select-display (select) |
715 | (let ((value (plist-get select :value)) | |
716 | display) | |
717 | (dolist (elem select) | |
718 | (when (and (consp elem) | |
719 | (eq (car elem) 'item) | |
720 | (equal value (plist-get (cdr elem) :value))) | |
721 | (setq display (plist-get (cdr elem) :display)))) | |
722 | display)) | |
723 | ||
724 | (defun eww-change-select () | |
725 | "Change the value of the select drop-down menu under point." | |
726 | (interactive) | |
727 | (let* ((input (get-text-property (point) 'eww-form)) | |
728 | (properties (text-properties-at (point))) | |
729 | (completion-ignore-case t) | |
730 | (options | |
731 | (delq nil | |
732 | (mapcar (lambda (elem) | |
733 | (and (consp elem) | |
734 | (eq (car elem) 'item) | |
735 | (cons (plist-get (cdr elem) :display) | |
736 | (plist-get (cdr elem) :value)))) | |
737 | input))) | |
738 | (display | |
739 | (completing-read "Change value: " options nil 'require-match)) | |
740 | (inhibit-read-only t)) | |
741 | (plist-put input :value (cdr (assoc-string display options t))) | |
742 | (goto-char | |
743 | (eww-update-field display)))) | |
744 | ||
745 | (defun eww-update-field (string) | |
746 | (let ((properties (text-properties-at (point))) | |
747 | (start (eww-beginning-of-field)) | |
748 | (end (1+ (eww-end-of-field)))) | |
749 | (delete-region start end) | |
750 | (insert string | |
751 | (make-string (- (- end start) (length string)) ? )) | |
752 | (set-text-properties start end properties) | |
753 | start)) | |
754 | ||
755 | (defun eww-toggle-checkbox () | |
756 | "Toggle the value of the checkbox under point." | |
757 | (interactive) | |
758 | (let* ((input (get-text-property (point) 'eww-form)) | |
759 | (type (plist-get input :type))) | |
760 | (if (equal type "checkbox") | |
761 | (goto-char | |
762 | (1+ | |
763 | (if (plist-get input :checked) | |
764 | (progn | |
765 | (plist-put input :checked nil) | |
766 | (eww-update-field "[ ]")) | |
767 | (plist-put input :checked t) | |
768 | (eww-update-field "[X]")))) | |
769 | ;; Radio button. Switch all other buttons off. | |
770 | (let ((name (plist-get input :name))) | |
771 | (save-excursion | |
772 | (dolist (elem (eww-inputs (plist-get input :eww-form))) | |
773 | (when (equal (plist-get (cdr elem) :name) name) | |
774 | (goto-char (car elem)) | |
775 | (if (not (eq (cdr elem) input)) | |
776 | (progn | |
777 | (plist-put input :checked nil) | |
778 | (eww-update-field "[ ]")) | |
779 | (plist-put input :checked t) | |
780 | (eww-update-field "[X]"))))) | |
781 | (forward-char 1))))) | |
782 | ||
783 | (defun eww-inputs (form) | |
784 | (let ((start (point-min)) | |
785 | (inputs nil)) | |
786 | (while (and start | |
787 | (< start (point-max))) | |
788 | (when (or (get-text-property start 'eww-form) | |
789 | (setq start (next-single-property-change start 'eww-form))) | |
790 | (when (eq (plist-get (get-text-property start 'eww-form) :eww-form) | |
791 | form) | |
792 | (push (cons start (get-text-property start 'eww-form)) | |
793 | inputs)) | |
794 | (setq start (next-single-property-change start 'eww-form)))) | |
795 | (nreverse inputs))) | |
796 | ||
797 | (defun eww-input-value (input) | |
798 | (let ((type (plist-get input :type)) | |
799 | (value (plist-get input :value))) | |
800 | (cond | |
801 | ((equal type "textarea") | |
802 | (with-temp-buffer | |
803 | (insert value) | |
804 | (goto-char (point-min)) | |
805 | (while (re-search-forward "^ +\n\\| +$" nil t) | |
806 | (replace-match "" t t)) | |
807 | (buffer-string))) | |
808 | (t | |
809 | (if (string-match " +\\'" value) | |
810 | (substring value 0 (match-beginning 0)) | |
811 | value))))) | |
812 | ||
813 | (defun eww-submit () | |
814 | "Submit the current form." | |
815 | (interactive) | |
816 | (let* ((this-input (get-text-property (point) 'eww-form)) | |
817 | (form (plist-get this-input :eww-form)) | |
818 | values next-submit) | |
819 | (dolist (elem (sort (eww-inputs form) | |
820 | (lambda (o1 o2) | |
821 | (< (car o1) (car o2))))) | |
822 | (let* ((input (cdr elem)) | |
823 | (input-start (car elem)) | |
824 | (name (plist-get input :name))) | |
825 | (when name | |
826 | (cond | |
827 | ((member (plist-get input :type) '("checkbox" "radio")) | |
828 | (when (plist-get input :checked) | |
829 | (push (cons name (plist-get input :value)) | |
830 | values))) | |
831 | ((equal (plist-get input :type) "submit") | |
832 | ;; We want the values from buttons if we hit a button if | |
833 | ;; we hit enter on it, or if it's the first button after | |
834 | ;; the field we did hit return on. | |
835 | (when (or (eq input this-input) | |
836 | (and (not (eq input this-input)) | |
837 | (null next-submit) | |
838 | (> input-start (point)))) | |
839 | (setq next-submit t) | |
840 | (push (cons name (plist-get input :value)) | |
841 | values))) | |
842 | (t | |
843 | (push (cons name (eww-input-value input)) | |
844 | values)))))) | |
f22255bd LMI |
845 | (dolist (elem form) |
846 | (when (and (consp elem) | |
847 | (eq (car elem) 'hidden)) | |
848 | (push (cons (plist-get (cdr elem) :name) | |
849 | (plist-get (cdr elem) :value)) | |
850 | values))) | |
c74cb344 G |
851 | (if (and (stringp (cdr (assq :method form))) |
852 | (equal (downcase (cdr (assq :method form))) "post")) | |
853 | (let ((url-request-method "POST") | |
854 | (url-request-extra-headers | |
855 | '(("Content-Type" . "application/x-www-form-urlencoded"))) | |
856 | (url-request-data (mm-url-encode-www-form-urlencoded values))) | |
857 | (eww-browse-url (shr-expand-url (cdr (assq :action form)) | |
858 | eww-current-url))) | |
859 | (eww-browse-url | |
860 | (concat | |
861 | (if (cdr (assq :action form)) | |
862 | (shr-expand-url (cdr (assq :action form)) | |
863 | eww-current-url) | |
864 | eww-current-url) | |
865 | "?" | |
866 | (mm-url-encode-www-form-urlencoded values)))))) | |
2644071e | 867 | |
f865b474 IK |
868 | (defun eww-browse-with-external-browser () |
869 | "Browse the current URL with an external browser. | |
0ebd92a3 | 870 | The browser to used is specified by the `shr-external-browser' variable." |
f865b474 | 871 | (interactive) |
0ebd92a3 | 872 | (funcall shr-external-browser eww-current-url)) |
f865b474 | 873 | |
b89fc156 IK |
874 | (defun eww-yank-page-url () |
875 | (interactive) | |
876 | (message eww-current-url) | |
877 | (kill-new eww-current-url)) | |
266c63b5 AK |
878 | (provide 'eww) |
879 | ||
880 | ;;; eww.el ends here |