1 ;;; org-eww.el --- Store url and kill from Eww mode for Org
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
7 ;; Homepage: http://orgmode.org
9 ;; This file is not part of GNU Emacs.
11 ;; This program is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; When this module is active `org-store-link' (often on key C-c l) in
28 ;; a eww buffer stores a link to the current url of the eww buffer.
30 ;; In an eww buffer function `org-eww-copy-for-org-mode' kills either
31 ;; a region or the whole buffer if no region is set and transforms the
32 ;; text on the fly so that it can be pasted into an org-mode buffer
35 ;; C-c C-x C-w (and also C-c C-x M-w) trigger
36 ;; `org-eww-copy-for-org-mode'.
38 ;; Hint: A lot of code of this module comes from module org-w3m which
39 ;; has been written by Andy Steward based on the idea of Richard
42 ;; Potential: Since the code for w3m and eww is so similar one could
50 ;; Store Org-link in eww-mode buffer
51 (add-hook 'org-store-link-functions
'org-eww-store-link
)
52 (defun org-eww-store-link ()
53 "Store a link to the url of a eww buffer."
54 (when (eq major-mode
'eww-mode
)
57 :link
(if (< emacs-major-version
25)
61 :description
(if (< emacs-major-version
25)
62 (or eww-current-title eww-current-url
)
63 (or (plist-get eww-data
:title
)
64 (eww-current-url))))))
67 ;; Some auxiliary functions concerning links in eww buffers
68 (defun org-eww-goto-next-url-property-change ()
69 "Move cursor to the start of next link if exists. Else no
72 (or (next-single-property-change (point) 'shr-url
)
75 (defun org-eww-no-next-link-p ()
76 "Whether there is no next link after the cursor.
77 Return t if there is no next link; otherwise, return nil."
79 (and (eq (point) (org-eww-goto-next-url-property-change)) t
)))
81 (defun org-eww-url-below-point ()
82 "Return the url below point if there is an url; otherwise, return nil."
83 (get-text-property (point) 'shr-url
))
86 (defun org-eww-copy-for-org-mode ()
87 "Copy current buffer content or active region with `org-mode' style links.
88 This will encode `link-title' and `link-location' with
89 `org-make-link-string', and insert the transformed test into the kill ring,
90 so that it can be yanked into an Org-mode buffer with links working correctly."
92 (let* ((regionp (org-region-active-p))
93 (transform-start (point-min))
94 (transform-end (point-max))
96 link-location link-title
97 temp-position out-bound
)
99 (setq transform-start
(region-beginning))
100 (setq transform-end
(region-end))
101 ;; Deactivate mark if current mark is activate.
102 (if (fboundp 'deactivate-mark
) (deactivate-mark)))
103 (message "Transforming links...")
105 (goto-char transform-start
)
106 (while (and (not out-bound
) ; still inside region to copy
107 (not (org-eww-no-next-link-p))) ; there is a next link
108 ;; store current point before jump next anchor
109 (setq temp-position
(point))
110 ;; move to next anchor when current point is not at anchor
111 (or (org-eww-url-below-point)
112 (org-eww-goto-next-url-property-change))
113 (assert (org-eww-url-below-point) t
114 "program logic error: point must have an url below but it hasn't")
115 (if (<= (point) transform-end
) ; if point is inside transform bound
117 ;; get content between two links.
118 (if (> (point) temp-position
)
119 (setq return-content
(concat return-content
121 temp-position
(point)))))
122 ;; get link location at current point.
123 (setq link-location
(org-eww-url-below-point))
124 ;; get link title at current point.
128 (org-eww-goto-next-url-property-change)))
129 ;; concat `org-mode' style url to `return-content'.
130 (setq return-content
(concat return-content
131 (org-make-link-string
132 link-location link-title
))))
133 (goto-char temp-position
) ; reset point before jump next anchor
134 (setq out-bound t
) ; for break out `while' loop
136 ;; add the rest until end of the region to be copied
137 (if (< (point) transform-end
)
139 (concat return-content
140 (buffer-substring (point) transform-end
))))
141 (org-kill-new return-content
)
142 (message "Transforming links...done, use C-y to insert text into Org-mode file"))))
145 ;; Additional keys for eww-mode
147 (defun org-eww-extend-eww-keymap ()
148 (define-key eww-mode-map
"\C-c\C-x\M-w" 'org-eww-copy-for-org-mode
)
149 (define-key eww-mode-map
"\C-c\C-x\C-w" 'org-eww-copy-for-org-mode
))
151 (when (and (boundp 'eww-mode-map
)
152 (keymapp eww-mode-map
)) ; eww is already up.
153 (org-eww-extend-eww-keymap))
157 (lambda () (org-eww-extend-eww-keymap)))
162 ;;; org-eww.el ends here