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))) | |
26bf2b82 | 163 | (setq eww-history-position 0) |
c74cb344 G |
164 | (cond |
165 | (point | |
166 | (goto-char point)) | |
167 | (shr-target-id | |
168 | (let ((point (next-single-property-change | |
169 | (point-min) 'shr-target-id))) | |
170 | (when point | |
171 | (goto-char (1+ point))))))) | |
266c63b5 AK |
172 | (kill-buffer data-buffer)))) |
173 | ||
174 | (defun eww-parse-headers () | |
175 | (let ((headers nil)) | |
d652f4d0 | 176 | (goto-char (point-min)) |
266c63b5 AK |
177 | (while (and (not (eobp)) |
178 | (not (eolp))) | |
179 | (when (looking-at "\\([^:]+\\): *\\(.*\\)") | |
180 | (push (cons (downcase (match-string 1)) | |
181 | (match-string 2)) | |
182 | headers)) | |
183 | (forward-line 1)) | |
184 | (unless (eobp) | |
185 | (forward-line 1)) | |
186 | headers)) | |
187 | ||
db5a34ca KY |
188 | (defun eww-detect-charset (html-p) |
189 | (let ((case-fold-search t) | |
190 | (pt (point))) | |
191 | (or (and html-p | |
192 | (re-search-forward | |
b89fc156 | 193 | "<meta[\t\n\r ]+[^>]*charset=\"?\\([^\t\n\r \"/>]+\\)[\\\"'.*]" nil t) |
db5a34ca KY |
194 | (goto-char pt) |
195 | (match-string 1)) | |
196 | (and (looking-at | |
197 | "[\t\n\r ]*<\\?xml[\t\n\r ]+[^>]*encoding=\"\\([^\"]+\\)") | |
198 | (match-string 1))))) | |
199 | ||
266c63b5 AK |
200 | (defun eww-display-html (charset url) |
201 | (unless (eq charset 'utf8) | |
202 | (decode-coding-region (point) (point-max) charset)) | |
203 | (let ((document | |
204 | (list | |
205 | 'base (list (cons 'href url)) | |
206 | (libxml-parse-html-region (point) (point-max))))) | |
207 | (eww-setup-buffer) | |
208 | (setq eww-current-url url) | |
c74cb344 | 209 | (eww-update-header-line-format) |
2644071e | 210 | (let ((inhibit-read-only t) |
970ad972 | 211 | (after-change-functions nil) |
c74cb344 | 212 | (shr-width nil) |
2644071e | 213 | (shr-external-rendering-functions |
c74cb344 G |
214 | '((title . eww-tag-title) |
215 | (form . eww-tag-form) | |
2644071e | 216 | (input . eww-tag-input) |
c74cb344 G |
217 | (textarea . eww-tag-textarea) |
218 | (body . eww-tag-body) | |
924d6997 G |
219 | (select . eww-tag-select) |
220 | (link . eww-tag-link) | |
221 | (a . eww-tag-a)))) | |
970ad972 | 222 | (shr-insert-document document)) |
266c63b5 AK |
223 | (goto-char (point-min)))) |
224 | ||
924d6997 G |
225 | (defun eww-handle-link (cont) |
226 | (let* ((rel (assq :rel cont)) | |
227 | (href (assq :href cont)) | |
970ad972 G |
228 | (where (assoc |
229 | ;; The text associated with :rel is case-insensitive. | |
230 | (if rel (downcase (cdr rel))) | |
924d6997 | 231 | '(("next" . eww-next-url) |
970ad972 G |
232 | ;; Texinfo uses "previous", but HTML specifies |
233 | ;; "prev", so recognize both. | |
924d6997 | 234 | ("previous" . eww-previous-url) |
970ad972 G |
235 | ("prev" . eww-previous-url) |
236 | ;; HTML specifies "start" but also "contents", | |
237 | ;; and Gtk seems to use "home". Recognize | |
238 | ;; them all; but store them in different | |
239 | ;; variables so that we can readily choose the | |
240 | ;; "best" one. | |
241 | ("start" . eww-start-url) | |
242 | ("home" . eww-home-url) | |
243 | ("contents" . eww-contents-url) | |
924d6997 G |
244 | ("up" . eww-up-url))))) |
245 | (and href | |
246 | where | |
247 | (set (cdr where) (cdr href))))) | |
248 | ||
249 | (defun eww-tag-link (cont) | |
250 | (eww-handle-link cont) | |
251 | (shr-generic cont)) | |
252 | ||
253 | (defun eww-tag-a (cont) | |
254 | (eww-handle-link cont) | |
255 | (shr-tag-a cont)) | |
256 | ||
c74cb344 G |
257 | (defun eww-update-header-line-format () |
258 | (if eww-header-line-format | |
d80a808f LMI |
259 | (setq header-line-format |
260 | (replace-regexp-in-string | |
261 | "%" "%%" | |
262 | (format-spec eww-header-line-format | |
263 | `((?u . ,eww-current-url) | |
264 | (?t . ,eww-current-title))))) | |
c74cb344 G |
265 | (setq header-line-format nil))) |
266 | ||
267 | (defun eww-tag-title (cont) | |
268 | (setq eww-current-title "") | |
269 | (dolist (sub cont) | |
270 | (when (eq (car sub) 'text) | |
271 | (setq eww-current-title (concat eww-current-title (cdr sub))))) | |
272 | (eww-update-header-line-format)) | |
273 | ||
274 | (defun eww-tag-body (cont) | |
275 | (let* ((start (point)) | |
276 | (fgcolor (cdr (or (assq :fgcolor cont) | |
277 | (assq :text cont)))) | |
278 | (bgcolor (cdr (assq :bgcolor cont))) | |
279 | (shr-stylesheet (list (cons 'color fgcolor) | |
280 | (cons 'background-color bgcolor)))) | |
281 | (shr-generic cont) | |
282 | (eww-colorize-region start (point) fgcolor bgcolor))) | |
283 | ||
284 | (defun eww-colorize-region (start end fg &optional bg) | |
285 | (when (or fg bg) | |
286 | (let ((new-colors (shr-color-check fg bg))) | |
287 | (when new-colors | |
288 | (when fg | |
544d4594 | 289 | (add-face-text-property start end |
970ad972 G |
290 | (list :foreground (cadr new-colors)) |
291 | t)) | |
c74cb344 | 292 | (when bg |
544d4594 | 293 | (add-face-text-property start end |
970ad972 G |
294 | (list :background (car new-colors)) |
295 | t)))))) | |
c74cb344 | 296 | |
266c63b5 AK |
297 | (defun eww-display-raw (charset) |
298 | (let ((data (buffer-substring (point) (point-max)))) | |
299 | (eww-setup-buffer) | |
300 | (let ((inhibit-read-only t)) | |
301 | (insert data)) | |
302 | (goto-char (point-min)))) | |
303 | ||
304 | (defun eww-display-image () | |
305 | (let ((data (buffer-substring (point) (point-max)))) | |
306 | (eww-setup-buffer) | |
307 | (let ((inhibit-read-only t)) | |
308 | (shr-put-image data nil)) | |
309 | (goto-char (point-min)))) | |
310 | ||
311 | (defun eww-setup-buffer () | |
312 | (pop-to-buffer (get-buffer-create "*eww*")) | |
313 | (let ((inhibit-read-only t)) | |
8308f184 | 314 | (remove-overlays) |
266c63b5 | 315 | (erase-buffer)) |
8308f184 LMI |
316 | (unless (eq major-mode 'eww-mode) |
317 | (eww-mode))) | |
266c63b5 AK |
318 | |
319 | (defvar eww-mode-map | |
320 | (let ((map (make-sparse-keymap))) | |
321 | (suppress-keymap map) | |
322 | (define-key map "q" 'eww-quit) | |
f22255bd | 323 | (define-key map "g" 'eww-reload) |
7304e4dd LMI |
324 | (define-key map [tab] 'shr-next-link) |
325 | (define-key map [backtab] 'shr-previous-link) | |
266c63b5 AK |
326 | (define-key map [delete] 'scroll-down-command) |
327 | (define-key map "\177" 'scroll-down-command) | |
328 | (define-key map " " 'scroll-up-command) | |
924d6997 | 329 | (define-key map "l" 'eww-back-url) |
d3f0f918 | 330 | (define-key map "f" 'eww-forward-url) |
924d6997 | 331 | (define-key map "n" 'eww-next-url) |
266c63b5 | 332 | (define-key map "p" 'eww-previous-url) |
924d6997 G |
333 | (define-key map "u" 'eww-up-url) |
334 | (define-key map "t" 'eww-top-url) | |
16f74f10 | 335 | (define-key map "&" 'eww-browse-with-external-browser) |
bfbc93a1 | 336 | (define-key map "d" 'eww-download) |
16f74f10 | 337 | (define-key map "w" 'eww-copy-page-url) |
23a75d7f LMI |
338 | (define-key map "C" 'url-cookie-list) |
339 | ||
2b4f0506 LMI |
340 | (define-key map "b" 'eww-add-bookmark) |
341 | (define-key map "B" 'eww-list-bookmarks) | |
342 | (define-key map [(meta n)] 'eww-next-bookmark) | |
343 | (define-key map [(meta p)] 'eww-previous-bookmark) | |
344 | ||
23a75d7f LMI |
345 | (easy-menu-define nil map "" |
346 | '("eww" | |
347 | ["Quit" eww-quit t] | |
348 | ["Reload" eww-reload t] | |
349 | ["Back to previous page" eww-back-url | |
350 | :active (not (zerop (length eww-history)))] | |
351 | ["Forward to next page" eww-forward-url | |
352 | :active (not (zerop eww-history-position))] | |
353 | ["Browse with external browser" eww-browse-with-external-browser t] | |
354 | ["Download" eww-download t] | |
355 | ["Copy page URL" eww-copy-page-url t] | |
2b4f0506 LMI |
356 | ["Add bookmark" eww-add-bookmark t] |
357 | ["List bookmarks" eww-copy-page-url t] | |
23a75d7f | 358 | ["List cookies" url-cookie-list t])) |
266c63b5 AK |
359 | map)) |
360 | ||
d652f4d0 | 361 | (define-derived-mode eww-mode nil "eww" |
266c63b5 AK |
362 | "Mode for browsing the web. |
363 | ||
364 | \\{eww-mode-map}" | |
266c63b5 | 365 | (set (make-local-variable 'eww-current-url) 'author) |
970ad972 G |
366 | (set (make-local-variable 'browse-url-browser-function) 'eww-browse-url) |
367 | (set (make-local-variable 'after-change-functions) 'eww-process-text-input) | |
8308f184 LMI |
368 | (set (make-local-variable 'eww-history) nil) |
369 | (set (make-local-variable 'eww-history-position) 0) | |
843571cb | 370 | (buffer-disable-undo) |
970ad972 G |
371 | ;;(setq buffer-read-only t) |
372 | ) | |
266c63b5 | 373 | |
d3f0f918 | 374 | (defun eww-save-history () |
8308f184 | 375 | (push (list :url eww-current-url |
2b4f0506 | 376 | :title eww-current-title |
8308f184 LMI |
377 | :point (point) |
378 | :text (buffer-string)) | |
379 | eww-history)) | |
d3f0f918 | 380 | |
266c63b5 | 381 | (defun eww-browse-url (url &optional new-window) |
970ad972 G |
382 | (when (and (equal major-mode 'eww-mode) |
383 | eww-current-url) | |
d3f0f918 | 384 | (eww-save-history)) |
5c3087e9 | 385 | (eww url)) |
266c63b5 AK |
386 | |
387 | (defun eww-quit () | |
388 | "Exit the Emacs Web Wowser." | |
389 | (interactive) | |
390 | (setq eww-history nil) | |
391 | (kill-buffer (current-buffer))) | |
392 | ||
924d6997 | 393 | (defun eww-back-url () |
266c63b5 AK |
394 | "Go to the previously displayed page." |
395 | (interactive) | |
d3f0f918 | 396 | (when (>= eww-history-position (length eww-history)) |
266c63b5 | 397 | (error "No previous page")) |
8308f184 LMI |
398 | (eww-save-history) |
399 | (setq eww-history-position (+ eww-history-position 2)) | |
400 | (eww-restore-history (elt eww-history (1- eww-history-position)))) | |
d3f0f918 LMI |
401 | |
402 | (defun eww-forward-url () | |
403 | "Go to the next displayed page." | |
404 | (interactive) | |
405 | (when (zerop eww-history-position) | |
406 | (error "No next page")) | |
8308f184 LMI |
407 | (eww-save-history) |
408 | (eww-restore-history (elt eww-history (1- eww-history-position)))) | |
d3f0f918 LMI |
409 | |
410 | (defun eww-restore-history (elem) | |
411 | (let ((inhibit-read-only t)) | |
e82b0991 | 412 | (erase-buffer) |
d3f0f918 LMI |
413 | (insert (plist-get elem :text)) |
414 | (goto-char (plist-get elem :point)) | |
2b4f0506 LMI |
415 | (setq eww-current-url (plist-get elem :url) |
416 | eww-current-title (plist-get elem :title)))) | |
266c63b5 | 417 | |
924d6997 G |
418 | (defun eww-next-url () |
419 | "Go to the page marked `next'. | |
420 | A page is marked `next' if rel=\"next\" appears in a <link> | |
421 | or <a> tag." | |
422 | (interactive) | |
423 | (if eww-next-url | |
424 | (eww-browse-url (shr-expand-url eww-next-url eww-current-url)) | |
425 | (error "No `next' on this page"))) | |
426 | ||
427 | (defun eww-previous-url () | |
428 | "Go to the page marked `previous'. | |
429 | A page is marked `previous' if rel=\"previous\" appears in a <link> | |
430 | or <a> tag." | |
431 | (interactive) | |
432 | (if eww-previous-url | |
433 | (eww-browse-url (shr-expand-url eww-previous-url eww-current-url)) | |
434 | (error "No `previous' on this page"))) | |
435 | ||
436 | (defun eww-up-url () | |
437 | "Go to the page marked `up'. | |
438 | A page is marked `up' if rel=\"up\" appears in a <link> | |
439 | or <a> tag." | |
440 | (interactive) | |
441 | (if eww-up-url | |
442 | (eww-browse-url (shr-expand-url eww-up-url eww-current-url)) | |
443 | (error "No `up' on this page"))) | |
444 | ||
445 | (defun eww-top-url () | |
446 | "Go to the page marked `top'. | |
970ad972 G |
447 | A page is marked `top' if rel=\"start\", rel=\"home\", or rel=\"contents\" |
448 | appears in a <link> or <a> tag." | |
924d6997 | 449 | (interactive) |
970ad972 G |
450 | (let ((best-url (or eww-start-url |
451 | eww-contents-url | |
452 | eww-home-url))) | |
453 | (if best-url | |
454 | (eww-browse-url (shr-expand-url best-url eww-current-url)) | |
455 | (error "No `top' for this page")))) | |
924d6997 | 456 | |
f22255bd LMI |
457 | (defun eww-reload () |
458 | "Reload the current page." | |
459 | (interactive) | |
460 | (url-retrieve eww-current-url 'eww-render | |
461 | (list eww-current-url (point)))) | |
462 | ||
2644071e LMI |
463 | ;; Form support. |
464 | ||
465 | (defvar eww-form nil) | |
466 | ||
970ad972 G |
467 | (defvar eww-submit-map |
468 | (let ((map (make-sparse-keymap))) | |
469 | (define-key map "\r" 'eww-submit) | |
e854cfc7 | 470 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
471 | map)) |
472 | ||
473 | (defvar eww-checkbox-map | |
474 | (let ((map (make-sparse-keymap))) | |
475 | (define-key map [space] 'eww-toggle-checkbox) | |
476 | (define-key map "\r" 'eww-toggle-checkbox) | |
e854cfc7 | 477 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
478 | map)) |
479 | ||
480 | (defvar eww-text-map | |
481 | (let ((map (make-keymap))) | |
482 | (set-keymap-parent map text-mode-map) | |
483 | (define-key map "\r" 'eww-submit) | |
484 | (define-key map [(control a)] 'eww-beginning-of-text) | |
e854cfc7 | 485 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
486 | (define-key map [(control e)] 'eww-end-of-text) |
487 | (define-key map [tab] 'shr-next-link) | |
488 | (define-key map [backtab] 'shr-previous-link) | |
489 | map)) | |
490 | ||
491 | (defvar eww-textarea-map | |
492 | (let ((map (make-keymap))) | |
493 | (set-keymap-parent map text-mode-map) | |
494 | (define-key map "\r" 'forward-line) | |
e854cfc7 | 495 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
496 | (define-key map [tab] 'shr-next-link) |
497 | (define-key map [backtab] 'shr-previous-link) | |
498 | map)) | |
499 | ||
500 | (defvar eww-select-map | |
501 | (let ((map (make-sparse-keymap))) | |
502 | (define-key map "\r" 'eww-change-select) | |
e854cfc7 | 503 | (define-key map [(control c) (control c)] 'eww-submit) |
970ad972 G |
504 | map)) |
505 | ||
506 | (defun eww-beginning-of-text () | |
507 | "Move to the start of the input field." | |
508 | (interactive) | |
509 | (goto-char (eww-beginning-of-field))) | |
510 | ||
511 | (defun eww-end-of-text () | |
512 | "Move to the end of the text in the input field." | |
513 | (interactive) | |
514 | (goto-char (eww-end-of-field)) | |
515 | (let ((start (eww-beginning-of-field))) | |
516 | (while (and (equal (following-char) ? ) | |
517 | (> (point) start)) | |
518 | (forward-char -1)) | |
519 | (when (> (point) start) | |
520 | (forward-char 1)))) | |
521 | ||
522 | (defun eww-beginning-of-field () | |
523 | (cond | |
524 | ((bobp) | |
525 | (point)) | |
526 | ((not (eq (get-text-property (point) 'eww-form) | |
527 | (get-text-property (1- (point)) 'eww-form))) | |
528 | (point)) | |
529 | (t | |
530 | (previous-single-property-change | |
531 | (point) 'eww-form nil (point-min))))) | |
532 | ||
533 | (defun eww-end-of-field () | |
534 | (1- (next-single-property-change | |
535 | (point) 'eww-form nil (point-max)))) | |
536 | ||
2644071e LMI |
537 | (defun eww-tag-form (cont) |
538 | (let ((eww-form | |
539 | (list (assq :method cont) | |
540 | (assq :action cont))) | |
541 | (start (point))) | |
542 | (shr-ensure-paragraph) | |
543 | (shr-generic cont) | |
3d95242e LMI |
544 | (unless (bolp) |
545 | (insert "\n")) | |
546 | (insert "\n") | |
001b9fbe LMI |
547 | (when (> (point) start) |
548 | (put-text-property start (1+ start) | |
549 | 'eww-form eww-form)))) | |
2644071e | 550 | |
970ad972 G |
551 | (defun eww-form-submit (cont) |
552 | (let ((start (point)) | |
553 | (value (cdr (assq :value cont)))) | |
554 | (setq value | |
555 | (if (zerop (length value)) | |
556 | "Submit" | |
557 | value)) | |
558 | (insert value) | |
559 | (add-face-text-property start (point) 'eww-form-submit) | |
560 | (put-text-property start (point) 'eww-form | |
561 | (list :eww-form eww-form | |
562 | :value value | |
563 | :type "submit" | |
564 | :name (cdr (assq :name cont)))) | |
565 | (put-text-property start (point) 'keymap eww-submit-map) | |
566 | (insert " "))) | |
567 | ||
568 | (defun eww-form-checkbox (cont) | |
569 | (let ((start (point))) | |
570 | (if (cdr (assq :checked cont)) | |
571 | (insert "[X]") | |
572 | (insert "[ ]")) | |
573 | (add-face-text-property start (point) 'eww-form-checkbox) | |
574 | (put-text-property start (point) 'eww-form | |
575 | (list :eww-form eww-form | |
576 | :value (cdr (assq :value cont)) | |
577 | :type (downcase (cdr (assq :type cont))) | |
578 | :checked (cdr (assq :checked cont)) | |
579 | :name (cdr (assq :name cont)))) | |
580 | (put-text-property start (point) 'keymap eww-checkbox-map) | |
581 | (insert " "))) | |
582 | ||
583 | (defun eww-form-text (cont) | |
584 | (let ((start (point)) | |
585 | (type (downcase (or (cdr (assq :type cont)) | |
586 | "text"))) | |
587 | (value (or (cdr (assq :value cont)) "")) | |
588 | (width (string-to-number | |
589 | (or (cdr (assq :size cont)) | |
590 | "40")))) | |
591 | (insert value) | |
592 | (when (< (length value) width) | |
593 | (insert (make-string (- width (length value)) ? ))) | |
594 | (put-text-property start (point) 'face 'eww-form-text) | |
595 | (put-text-property start (point) 'local-map eww-text-map) | |
596 | (put-text-property start (point) 'inhibit-read-only t) | |
597 | (put-text-property start (point) 'eww-form | |
598 | (list :eww-form eww-form | |
599 | :value value | |
600 | :type type | |
601 | :name (cdr (assq :name cont)))) | |
602 | (insert " "))) | |
603 | ||
604 | (defun eww-process-text-input (beg end length) | |
605 | (let* ((form (get-text-property end 'eww-form)) | |
606 | (properties (text-properties-at end)) | |
607 | (type (plist-get form :type))) | |
608 | (when (and form | |
609 | (member type '("text" "password" "textarea"))) | |
610 | (cond | |
611 | ((zerop length) | |
612 | ;; Delete some space at the end. | |
613 | (save-excursion | |
614 | (goto-char | |
615 | (if (equal type "textarea") | |
616 | (1- (line-end-position)) | |
617 | (eww-end-of-field))) | |
618 | (let ((new (- end beg))) | |
619 | (while (and (> new 0) | |
620 | (eql (following-char) ? )) | |
621 | (delete-region (point) (1+ (point))) | |
622 | (setq new (1- new)))) | |
623 | (set-text-properties beg end properties))) | |
624 | ((> length 0) | |
625 | ;; Add padding. | |
626 | (save-excursion | |
627 | (goto-char | |
628 | (if (equal type "textarea") | |
629 | (1- (line-end-position)) | |
630 | (eww-end-of-field))) | |
631 | (let ((start (point))) | |
632 | (insert (make-string length ? )) | |
633 | (set-text-properties start (point) properties))))) | |
634 | (let ((value (buffer-substring-no-properties | |
635 | (eww-beginning-of-field) | |
636 | (eww-end-of-field)))) | |
637 | (when (string-match " +\\'" value) | |
638 | (setq value (substring value 0 (match-beginning 0)))) | |
639 | (plist-put form :value value) | |
640 | (when (equal type "password") | |
641 | ;; Display passwords as asterisks. | |
642 | (let ((start (eww-beginning-of-field))) | |
643 | (put-text-property start (+ start (length value)) | |
644 | 'display (make-string (length value) ?*)))))))) | |
9ddf23f0 | 645 | |
c74cb344 | 646 | (defun eww-tag-textarea (cont) |
970ad972 G |
647 | (let ((start (point)) |
648 | (value (or (cdr (assq :value cont)) "")) | |
649 | (lines (string-to-number | |
650 | (or (cdr (assq :rows cont)) | |
651 | "10"))) | |
652 | (width (string-to-number | |
653 | (or (cdr (assq :cols cont)) | |
654 | "10"))) | |
655 | end) | |
656 | (shr-ensure-newline) | |
657 | (insert value) | |
658 | (shr-ensure-newline) | |
659 | (when (< (count-lines start (point)) lines) | |
660 | (dotimes (i (- lines (count-lines start (point)))) | |
661 | (insert "\n"))) | |
662 | (setq end (point-marker)) | |
663 | (goto-char start) | |
664 | (while (< (point) end) | |
665 | (end-of-line) | |
666 | (let ((pad (- width (- (point) (line-beginning-position))))) | |
667 | (when (> pad 0) | |
668 | (insert (make-string pad ? )))) | |
669 | (add-face-text-property (line-beginning-position) | |
670 | (point) 'eww-form-text) | |
671 | (put-text-property (line-beginning-position) (point) | |
672 | 'local-map eww-textarea-map) | |
673 | (forward-line 1)) | |
674 | (put-text-property start (point) 'eww-form | |
675 | (list :eww-form eww-form | |
676 | :value value | |
677 | :type "textarea" | |
678 | :name (cdr (assq :name cont)))))) | |
679 | ||
680 | (defun eww-tag-input (cont) | |
681 | (let ((type (downcase (or (cdr (assq :type cont)) | |
682 | "text"))) | |
683 | (start (point))) | |
684 | (cond | |
685 | ((or (equal type "checkbox") | |
686 | (equal type "radio")) | |
687 | (eww-form-checkbox cont)) | |
688 | ((equal type "submit") | |
689 | (eww-form-submit cont)) | |
690 | ((equal type "hidden") | |
691 | (let ((form eww-form) | |
692 | (name (cdr (assq :name cont)))) | |
693 | ;; Don't add <input type=hidden> elements repeatedly. | |
694 | (while (and form | |
695 | (or (not (consp (car form))) | |
696 | (not (eq (caar form) 'hidden)) | |
697 | (not (equal (plist-get (cdr (car form)) :name) | |
698 | name)))) | |
699 | (setq form (cdr form))) | |
700 | (unless form | |
701 | (nconc eww-form (list | |
702 | (list 'hidden | |
703 | :name name | |
704 | :value (cdr (assq :value cont)))))))) | |
705 | (t | |
706 | (eww-form-text cont))) | |
707 | (unless (= start (point)) | |
708 | (put-text-property start (1+ start) 'help-echo "Input field")))) | |
c74cb344 | 709 | |
9ddf23f0 LMI |
710 | (defun eww-tag-select (cont) |
711 | (shr-ensure-paragraph) | |
970ad972 | 712 | (let ((menu (list :name (cdr (assq :name cont)) |
9ddf23f0 LMI |
713 | :eww-form eww-form)) |
714 | (options nil) | |
970ad972 G |
715 | (start (point)) |
716 | (max 0)) | |
9ddf23f0 LMI |
717 | (dolist (elem cont) |
718 | (when (eq (car elem) 'option) | |
719 | (when (cdr (assq :selected (cdr elem))) | |
720 | (nconc menu (list :value | |
721 | (cdr (assq :value (cdr elem)))))) | |
970ad972 G |
722 | (let ((display (or (cdr (assq 'text (cdr elem))) ""))) |
723 | (setq max (max max (length display))) | |
724 | (push (list 'item | |
725 | :value (cdr (assq :value (cdr elem))) | |
726 | :display display) | |
727 | options)))) | |
be2aa135 | 728 | (when options |
970ad972 | 729 | (setq options (nreverse options)) |
be2aa135 | 730 | ;; If we have no selected values, default to the first value. |
970ad972 | 731 | (unless (plist-get menu :value) |
be2aa135 LMI |
732 | (nconc menu (list :value (nth 2 (car options))))) |
733 | (nconc menu options) | |
970ad972 G |
734 | (let ((selected (eww-select-display menu))) |
735 | (insert selected | |
736 | (make-string (- max (length selected)) ? ))) | |
737 | (put-text-property start (point) 'eww-form menu) | |
738 | (add-face-text-property start (point) 'eww-form-select) | |
739 | (put-text-property start (point) 'keymap eww-select-map) | |
be2aa135 | 740 | (shr-ensure-paragraph)))) |
2644071e | 741 | |
970ad972 G |
742 | (defun eww-select-display (select) |
743 | (let ((value (plist-get select :value)) | |
744 | display) | |
745 | (dolist (elem select) | |
746 | (when (and (consp elem) | |
747 | (eq (car elem) 'item) | |
748 | (equal value (plist-get (cdr elem) :value))) | |
749 | (setq display (plist-get (cdr elem) :display)))) | |
750 | display)) | |
751 | ||
752 | (defun eww-change-select () | |
753 | "Change the value of the select drop-down menu under point." | |
754 | (interactive) | |
755 | (let* ((input (get-text-property (point) 'eww-form)) | |
756 | (properties (text-properties-at (point))) | |
757 | (completion-ignore-case t) | |
758 | (options | |
759 | (delq nil | |
760 | (mapcar (lambda (elem) | |
761 | (and (consp elem) | |
762 | (eq (car elem) 'item) | |
763 | (cons (plist-get (cdr elem) :display) | |
764 | (plist-get (cdr elem) :value)))) | |
765 | input))) | |
766 | (display | |
767 | (completing-read "Change value: " options nil 'require-match)) | |
768 | (inhibit-read-only t)) | |
769 | (plist-put input :value (cdr (assoc-string display options t))) | |
770 | (goto-char | |
771 | (eww-update-field display)))) | |
772 | ||
773 | (defun eww-update-field (string) | |
774 | (let ((properties (text-properties-at (point))) | |
775 | (start (eww-beginning-of-field)) | |
776 | (end (1+ (eww-end-of-field)))) | |
777 | (delete-region start end) | |
778 | (insert string | |
779 | (make-string (- (- end start) (length string)) ? )) | |
780 | (set-text-properties start end properties) | |
781 | start)) | |
782 | ||
783 | (defun eww-toggle-checkbox () | |
784 | "Toggle the value of the checkbox under point." | |
785 | (interactive) | |
786 | (let* ((input (get-text-property (point) 'eww-form)) | |
787 | (type (plist-get input :type))) | |
788 | (if (equal type "checkbox") | |
789 | (goto-char | |
790 | (1+ | |
791 | (if (plist-get input :checked) | |
792 | (progn | |
793 | (plist-put input :checked nil) | |
794 | (eww-update-field "[ ]")) | |
795 | (plist-put input :checked t) | |
796 | (eww-update-field "[X]")))) | |
797 | ;; Radio button. Switch all other buttons off. | |
798 | (let ((name (plist-get input :name))) | |
799 | (save-excursion | |
800 | (dolist (elem (eww-inputs (plist-get input :eww-form))) | |
801 | (when (equal (plist-get (cdr elem) :name) name) | |
802 | (goto-char (car elem)) | |
803 | (if (not (eq (cdr elem) input)) | |
804 | (progn | |
805 | (plist-put input :checked nil) | |
806 | (eww-update-field "[ ]")) | |
807 | (plist-put input :checked t) | |
808 | (eww-update-field "[X]"))))) | |
809 | (forward-char 1))))) | |
810 | ||
811 | (defun eww-inputs (form) | |
812 | (let ((start (point-min)) | |
813 | (inputs nil)) | |
814 | (while (and start | |
815 | (< start (point-max))) | |
816 | (when (or (get-text-property start 'eww-form) | |
817 | (setq start (next-single-property-change start 'eww-form))) | |
818 | (when (eq (plist-get (get-text-property start 'eww-form) :eww-form) | |
819 | form) | |
820 | (push (cons start (get-text-property start 'eww-form)) | |
821 | inputs)) | |
822 | (setq start (next-single-property-change start 'eww-form)))) | |
823 | (nreverse inputs))) | |
824 | ||
825 | (defun eww-input-value (input) | |
826 | (let ((type (plist-get input :type)) | |
827 | (value (plist-get input :value))) | |
828 | (cond | |
829 | ((equal type "textarea") | |
830 | (with-temp-buffer | |
831 | (insert value) | |
832 | (goto-char (point-min)) | |
833 | (while (re-search-forward "^ +\n\\| +$" nil t) | |
834 | (replace-match "" t t)) | |
835 | (buffer-string))) | |
836 | (t | |
837 | (if (string-match " +\\'" value) | |
838 | (substring value 0 (match-beginning 0)) | |
839 | value))))) | |
840 | ||
841 | (defun eww-submit () | |
842 | "Submit the current form." | |
843 | (interactive) | |
844 | (let* ((this-input (get-text-property (point) 'eww-form)) | |
845 | (form (plist-get this-input :eww-form)) | |
846 | values next-submit) | |
847 | (dolist (elem (sort (eww-inputs form) | |
848 | (lambda (o1 o2) | |
849 | (< (car o1) (car o2))))) | |
850 | (let* ((input (cdr elem)) | |
851 | (input-start (car elem)) | |
852 | (name (plist-get input :name))) | |
853 | (when name | |
854 | (cond | |
855 | ((member (plist-get input :type) '("checkbox" "radio")) | |
856 | (when (plist-get input :checked) | |
857 | (push (cons name (plist-get input :value)) | |
858 | values))) | |
859 | ((equal (plist-get input :type) "submit") | |
860 | ;; We want the values from buttons if we hit a button if | |
861 | ;; we hit enter on it, or if it's the first button after | |
862 | ;; the field we did hit return on. | |
863 | (when (or (eq input this-input) | |
864 | (and (not (eq input this-input)) | |
865 | (null next-submit) | |
866 | (> input-start (point)))) | |
867 | (setq next-submit t) | |
868 | (push (cons name (plist-get input :value)) | |
869 | values))) | |
870 | (t | |
871 | (push (cons name (eww-input-value input)) | |
872 | values)))))) | |
f22255bd LMI |
873 | (dolist (elem form) |
874 | (when (and (consp elem) | |
875 | (eq (car elem) 'hidden)) | |
876 | (push (cons (plist-get (cdr elem) :name) | |
877 | (plist-get (cdr elem) :value)) | |
878 | values))) | |
c74cb344 G |
879 | (if (and (stringp (cdr (assq :method form))) |
880 | (equal (downcase (cdr (assq :method form))) "post")) | |
881 | (let ((url-request-method "POST") | |
882 | (url-request-extra-headers | |
883 | '(("Content-Type" . "application/x-www-form-urlencoded"))) | |
884 | (url-request-data (mm-url-encode-www-form-urlencoded values))) | |
885 | (eww-browse-url (shr-expand-url (cdr (assq :action form)) | |
886 | eww-current-url))) | |
887 | (eww-browse-url | |
888 | (concat | |
889 | (if (cdr (assq :action form)) | |
890 | (shr-expand-url (cdr (assq :action form)) | |
891 | eww-current-url) | |
892 | eww-current-url) | |
893 | "?" | |
894 | (mm-url-encode-www-form-urlencoded values)))))) | |
2644071e | 895 | |
f865b474 IK |
896 | (defun eww-browse-with-external-browser () |
897 | "Browse the current URL with an external browser. | |
0ebd92a3 | 898 | The browser to used is specified by the `shr-external-browser' variable." |
f865b474 | 899 | (interactive) |
0ebd92a3 | 900 | (funcall shr-external-browser eww-current-url)) |
f865b474 | 901 | |
16f74f10 | 902 | (defun eww-copy-page-url () |
b89fc156 | 903 | (interactive) |
16f74f10 | 904 | (message "%s" eww-current-url) |
b89fc156 | 905 | (kill-new eww-current-url)) |
16f74f10 | 906 | |
bfbc93a1 IK |
907 | (defun eww-download () |
908 | "Download URL under point to `eww-download-directory'." | |
909 | (interactive) | |
910 | (let ((url (get-text-property (point) 'shr-url))) | |
911 | (if (not url) | |
912 | (message "No URL under point") | |
913 | (url-retrieve url 'eww-download-callback (list url))))) | |
914 | ||
915 | (defun eww-download-callback (status url) | |
916 | (unless (plist-get status :error) | |
917 | (let* ((obj (url-generic-parse-url url)) | |
918 | (path (car (url-path-and-query obj))) | |
919 | (file (eww-make-unique-file-name (file-name-nondirectory path) | |
920 | eww-download-path))) | |
921 | (write-file file) | |
922 | (message "Saved %s" file)))) | |
923 | ||
924 | (defun eww-make-unique-file-name (file directory) | |
925 | (cond | |
926 | ((zerop (length file)) | |
927 | (setq file "!")) | |
928 | ((string-match "\\`[.]" file) | |
929 | (setq file (concat "!" file)))) | |
930 | (let ((base file) | |
931 | (count 1)) | |
932 | (while (file-exists-p (expand-file-name file directory)) | |
933 | (setq file | |
934 | (if (string-match "\\`\\(.*\\)\\([.][^.]+\\)" file) | |
935 | (format "%s(%d)%s" (match-string 1 file) | |
936 | count (match-string 2 file)) | |
937 | (format "%s(%d)" file count))) | |
938 | (setq count (1+ count))) | |
939 | (expand-file-name file directory))) | |
940 | ||
2b4f0506 LMI |
941 | ;;; Bookmarks code |
942 | ||
943 | (defvar eww-bookmarks nil) | |
944 | ||
945 | (defun eww-add-bookmark () | |
946 | "Add the current page to the bookmarks." | |
947 | (interactive) | |
948 | (eww-read-bookmarks) | |
949 | (dolist (bookmark eww-bookmarks) | |
950 | (when (equal eww-current-url | |
951 | (plist-get bookmark :url)) | |
952 | (error "Already bookmarked"))) | |
a285ce99 LMI |
953 | (let ((title (replace-regexp-in-string "[\n\t\r]" " " eww-current-title))) |
954 | (setq title (replace-regexp-in-string "\\` +\\| +\\'" "" title)) | |
955 | (push (list :url eww-current-url | |
956 | :title title | |
957 | :time (current-time-string)) | |
958 | eww-bookmarks)) | |
2b4f0506 LMI |
959 | (eww-write-bookmarks) |
960 | (message "Bookmarked %s (%s)" eww-current-url eww-current-title)) | |
961 | ||
962 | (defun eww-write-bookmarks () | |
963 | (with-temp-file (expand-file-name "eww-bookmarks" user-emacs-directory) | |
964 | (insert ";; Auto-generated file; don't edit\n") | |
965 | (pp eww-bookmarks (current-buffer)))) | |
966 | ||
967 | (defun eww-read-bookmarks () | |
968 | (with-temp-buffer | |
969 | (insert-file-contents | |
970 | (expand-file-name "eww-bookmarks" user-emacs-directory)) | |
971 | (setq eww-bookmarks (read (current-buffer))))) | |
972 | ||
973 | (defun eww-list-bookmarks () | |
974 | "Display the bookmarks." | |
975 | (interactive) | |
976 | (eww-bookmark-prepare) | |
977 | (pop-to-buffer "*eww bookmarks*")) | |
978 | ||
979 | (defun eww-bookmark-prepare () | |
980 | (eww-read-bookmarks) | |
981 | (when (null eww-bookmarks) | |
982 | (error "No bookmarks are defined")) | |
983 | (set-buffer (get-buffer-create "*eww bookmarks*")) | |
984 | (eww-bookmark-mode) | |
985 | (let ((format "%-40s %s") | |
986 | (inhibit-read-only t) | |
987 | start url) | |
988 | (erase-buffer) | |
989 | (setq header-line-format (concat " " (format format "URL" "Title"))) | |
990 | (dolist (bookmark eww-bookmarks) | |
991 | (setq start (point)) | |
992 | (setq url (plist-get bookmark :url)) | |
993 | (when (> (length url) 40) | |
994 | (setq url (substring url 0 40))) | |
995 | (insert (format format url | |
996 | (plist-get bookmark :title)) | |
997 | "\n") | |
998 | (put-text-property start (1+ start) 'eww-bookmark bookmark)) | |
999 | (goto-char (point-min)))) | |
1000 | ||
1001 | (defvar eww-bookmark-kill-ring nil) | |
1002 | ||
1003 | (defun eww-bookmark-kill () | |
1004 | "Kill the current bookmark." | |
1005 | (interactive) | |
1006 | (let* ((start (line-beginning-position)) | |
1007 | (bookmark (get-text-property start 'eww-bookmark)) | |
1008 | (inhibit-read-only t)) | |
1009 | (unless bookmark | |
1010 | (error "No bookmark on the current line")) | |
1011 | (forward-line 1) | |
1012 | (push (buffer-substring start (point)) eww-bookmark-kill-ring) | |
1013 | (delete-region start (point)) | |
1014 | (setq eww-bookmarks (delq bookmark eww-bookmarks)) | |
1015 | (eww-write-bookmarks))) | |
1016 | ||
1017 | (defun eww-bookmark-yank () | |
1018 | "Yank a previously killed bookmark to the current line." | |
1019 | (interactive) | |
1020 | (unless eww-bookmark-kill-ring | |
1021 | (error "No previously killed bookmark")) | |
1022 | (beginning-of-line) | |
1023 | (let ((inhibit-read-only t) | |
1024 | (start (point)) | |
1025 | bookmark) | |
1026 | (insert (pop eww-bookmark-kill-ring)) | |
1027 | (setq bookmark (get-text-property start 'eww-bookmark)) | |
1028 | (if (= start (point-min)) | |
1029 | (push bookmark eww-bookmarks) | |
1030 | (let ((line (count-lines start (point)))) | |
1031 | (setcdr (nthcdr (1- line) eww-bookmarks) | |
1032 | (cons bookmark (nthcdr line eww-bookmarks))))) | |
1033 | (eww-write-bookmarks))) | |
1034 | ||
1035 | (defun eww-bookmark-quit () | |
1036 | "Kill the current buffer." | |
1037 | (interactive) | |
1038 | (kill-buffer (current-buffer))) | |
1039 | ||
1040 | (defun eww-bookmark-browse () | |
1041 | "Browse the bookmark under point in eww." | |
1042 | (interactive) | |
1043 | (let ((bookmark (get-text-property (line-beginning-position) 'eww-bookmark))) | |
1044 | (unless bookmark | |
1045 | (error "No bookmark on the current line")) | |
1046 | (delete-window) | |
1047 | (eww (plist-get bookmark :url)))) | |
1048 | ||
1049 | (defun eww-next-bookmark () | |
1050 | "Go to the next bookmark in the list." | |
1051 | (interactive) | |
1052 | (let ((first nil) | |
1053 | bookmark) | |
1054 | (unless (get-buffer "*eww bookmarks*") | |
1055 | (setq first t) | |
1056 | (eww-bookmark-prepare)) | |
1057 | (with-current-buffer (get-buffer "*eww bookmarks*") | |
1058 | (when (and (not first) | |
1059 | (not (eobp))) | |
1060 | (forward-line 1)) | |
1061 | (setq bookmark (get-text-property (line-beginning-position) | |
1062 | 'eww-bookmark)) | |
1063 | (unless bookmark | |
1064 | (error "No next bookmark"))) | |
1065 | (eww-browse-url (plist-get bookmark :url)))) | |
1066 | ||
1067 | (defun eww-previous-bookmark () | |
1068 | "Go to the previous bookmark in the list." | |
1069 | (interactive) | |
1070 | (let ((first nil) | |
1071 | bookmark) | |
1072 | (unless (get-buffer "*eww bookmarks*") | |
1073 | (setq first t) | |
1074 | (eww-bookmark-prepare)) | |
1075 | (with-current-buffer (get-buffer "*eww bookmarks*") | |
1076 | (if first | |
1077 | (goto-char (point-max)) | |
1078 | (beginning-of-line)) | |
1079 | ;; On the final line. | |
1080 | (when (eolp) | |
1081 | (forward-line -1)) | |
1082 | (if (bobp) | |
1083 | (error "No previous bookmark") | |
1084 | (forward-line -1)) | |
1085 | (setq bookmark (get-text-property (line-beginning-position) | |
1086 | 'eww-bookmark))) | |
1087 | (eww-browse-url (plist-get bookmark :url)))) | |
1088 | ||
1089 | (defvar eww-bookmark-mode-map | |
1090 | (let ((map (make-sparse-keymap))) | |
1091 | (suppress-keymap map) | |
1092 | (define-key map "q" 'eww-bookmark-quit) | |
1093 | (define-key map [(control k)] 'eww-bookmark-kill) | |
1094 | (define-key map [(control y)] 'eww-bookmark-yank) | |
1095 | (define-key map "\r" 'eww-bookmark-browse) | |
1096 | map)) | |
1097 | ||
1098 | (define-derived-mode eww-bookmark-mode nil "eww bookmarks" | |
1099 | "Mode for listing bookmarks. | |
1100 | ||
1101 | \\{eww-bookmark-mode-map}" | |
1102 | (buffer-disable-undo) | |
1103 | (setq buffer-read-only t | |
1104 | truncate-lines t)) | |
1105 | ||
266c63b5 AK |
1106 | (provide 'eww) |
1107 | ||
1108 | ;;; eww.el ends here |