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