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
)
59 :description
(or eww-current-title eww-current-url
))))
62 ;; Some auxiliary functions concerning links in eww buffers
63 (defun org-eww-goto-next-url-property-change ()
64 "Move cursor to the start of next link if exists. Else no
67 (or (next-single-property-change (point) 'shr-url
)
70 (defun org-eww-no-next-link-p ()
71 "Whether there is no next link after the cursor.
72 Return t if there is no next link; otherwise, return nil."
74 (and (eq (point) (org-eww-goto-next-url-property-change)) t
)))
76 (defun org-eww-url-below-point ()
77 "Return the url below point if there is an url; otherwise, return nil."
78 (get-text-property (point) 'shr-url
))
81 (defun org-eww-copy-for-org-mode ()
82 "Copy current buffer content or active region with `org-mode' style links.
83 This will encode `link-title' and `link-location' with
84 `org-make-link-string', and insert the transformed test into the kill ring,
85 so that it can be yanked into an Org-mode buffer with links working correctly."
87 (let* ((regionp (org-region-active-p))
88 (transform-start (point-min))
89 (transform-end (point-max))
91 link-location link-title
92 temp-position out-bound
)
94 (setq transform-start
(region-beginning))
95 (setq transform-end
(region-end))
96 ;; Deactivate mark if current mark is activate.
97 (if (fboundp 'deactivate-mark
) (deactivate-mark)))
98 (message "Transforming links...")
100 (goto-char transform-start
)
101 (while (and (not out-bound
) ; still inside region to copy
102 (not (org-eww-no-next-link-p))) ; there is a next link
103 ;; store current point before jump next anchor
104 (setq temp-position
(point))
105 ;; move to next anchor when current point is not at anchor
106 (or (org-eww-url-below-point)
107 (org-eww-goto-next-url-property-change))
108 (assert (org-eww-url-below-point) t
109 "program logic error: point must have an url below but it hasn't")
110 (if (<= (point) transform-end
) ; if point is inside transform bound
112 ;; get content between two links.
113 (if (> (point) temp-position
)
114 (setq return-content
(concat return-content
116 temp-position
(point)))))
117 ;; get link location at current point.
118 (setq link-location
(org-eww-url-below-point))
119 ;; get link title at current point.
123 (org-eww-goto-next-url-property-change)))
124 ;; concat `org-mode' style url to `return-content'.
125 (setq return-content
(concat return-content
126 (org-make-link-string
127 link-location link-title
))))
128 (goto-char temp-position
) ; reset point before jump next anchor
129 (setq out-bound t
) ; for break out `while' loop
131 ;; add the rest until end of the region to be copied
132 (if (< (point) transform-end
)
134 (concat return-content
135 (buffer-substring (point) transform-end
))))
136 (org-kill-new return-content
)
137 (message "Transforming links...done, use C-y to insert text into Org-mode file"))))
140 ;; Additional keys for eww-mode
142 (defun org-eww-extend-eww-keymap ()
143 (define-key eww-mode-map
"\C-c\C-x\M-w" 'org-eww-copy-for-org-mode
)
144 (define-key eww-mode-map
"\C-c\C-x\C-w" 'org-eww-copy-for-org-mode
))
146 (when (and (boundp 'eww-mode-map
)
147 (keymapp eww-mode-map
)) ; eww is already up.
148 (org-eww-extend-eww-keymap))
152 (lambda () (org-eww-extend-eww-keymap)))
157 ;;; org-eww.el ends here