Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-eww.el
blob372b543f512aad75395bce0a5e66e7cdb3f2b12d
1 ;;; org-eww.el --- Store url and kill from Eww mode for Org -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
5 ;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
6 ;; Keywords: link, eww
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 ;; GNU Emacs 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 <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
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 buffer with
33 ;; hot links.
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
40 ;; Riley. Thanks!
42 ;; Potential: Since the code for w3m and eww is so similar one could
43 ;; try to refactor.
46 ;;; Code:
47 (require 'org)
48 (require 'cl-lib)
50 (defvar eww-current-title)
51 (defvar eww-current-url)
52 (defvar eww-data)
53 (defvar eww-mode-map)
55 (declare-function eww-current-url "eww")
58 ;; Store Org-link in eww-mode buffer
59 (org-link-set-parameters "eww" :follow #'eww :store #'org-eww-store-link)
60 (defun org-eww-store-link ()
61 "Store a link to the url of a Eww buffer."
62 (when (eq major-mode 'eww-mode)
63 (org-store-link-props
64 :type "eww"
65 :link (if (< emacs-major-version 25)
66 eww-current-url
67 (eww-current-url))
68 :url (url-view-url t)
69 :description (if (< emacs-major-version 25)
70 (or eww-current-title eww-current-url)
71 (or (plist-get eww-data :title)
72 (eww-current-url))))))
75 ;; Some auxiliary functions concerning links in eww buffers
76 (defun org-eww-goto-next-url-property-change ()
77 "Move to the start of next link if exists.
78 Otherwise point is not moved. Return point."
79 (goto-char
80 (or (next-single-property-change (point) 'shr-url)
81 (point))))
83 (defun org-eww-has-further-url-property-change-p ()
84 "Non-nil if there is a next url property change."
85 (save-excursion
86 (not (eq (point) (org-eww-goto-next-url-property-change)))))
88 (defun org-eww-url-below-point ()
89 "Return the url below point if there is an url; otherwise, return nil."
90 (get-text-property (point) 'shr-url))
93 (defun org-eww-copy-for-org-mode ()
94 "Copy current buffer content or active region with `org-mode' style links.
95 This will encode `link-title' and `link-location' with
96 `org-make-link-string', and insert the transformed test into the kill ring,
97 so that it can be yanked into an Org mode buffer with links working correctly.
99 Further lines starting with a star get quoted with a comma to keep
100 the structure of the Org file."
101 (interactive)
102 (let* ((regionp (org-region-active-p))
103 (transform-start (point-min))
104 (transform-end (point-max))
105 return-content
106 link-location link-title
107 temp-position out-bound)
108 (when regionp
109 (setq transform-start (region-beginning))
110 (setq transform-end (region-end))
111 ;; Deactivate mark if current mark is activate.
112 (when (fboundp 'deactivate-mark) (deactivate-mark)))
113 (message "Transforming links...")
114 (save-excursion
115 (goto-char transform-start)
116 (while (and (not out-bound) ; still inside region to copy
117 (org-eww-has-further-url-property-change-p)) ; there is a next link
118 ;; Store current point before jump next anchor.
119 (setq temp-position (point))
120 ;; Move to next anchor when current point is not at anchor.
121 (or (org-eww-url-below-point)
122 (org-eww-goto-next-url-property-change))
123 (cl-assert
124 (org-eww-url-below-point) t
125 "program logic error: point must have an url below but it hasn't")
126 (if (<= (point) transform-end) ; if point is inside transform bound
127 (progn
128 ;; Get content between two links.
129 (when (< temp-position (point))
130 (setq return-content (concat return-content
131 (buffer-substring
132 temp-position (point)))))
133 ;; Get link location at current point.
134 (setq link-location (org-eww-url-below-point))
135 ;; Get link title at current point.
136 (setq link-title
137 (buffer-substring
138 (point)
139 (org-eww-goto-next-url-property-change)))
140 ;; concat `org-mode' style url to `return-content'.
141 (setq return-content
142 (concat return-content
143 (if (stringp link-location)
144 ;; hint: link-location is different for form-elements.
145 (org-make-link-string link-location link-title)
146 link-title))))
147 (goto-char temp-position) ; reset point before jump next anchor
148 (setq out-bound t) ; for break out `while' loop
150 ;; Add the rest until end of the region to be copied.
151 (when (< (point) transform-end)
152 (setq return-content
153 (concat return-content
154 (buffer-substring (point) transform-end))))
155 ;; Quote lines starting with *.
156 (org-kill-new (replace-regexp-in-string "^\\*" ",*" return-content))
157 (message "Transforming links...done, use C-y to insert text into Org mode file"))))
160 ;; Additional keys for eww-mode
162 (defun org-eww-extend-eww-keymap ()
163 (define-key eww-mode-map "\C-c\C-x\M-w" 'org-eww-copy-for-org-mode)
164 (define-key eww-mode-map "\C-c\C-x\C-w" 'org-eww-copy-for-org-mode))
166 (when (and (boundp 'eww-mode-map)
167 (keymapp eww-mode-map)) ; eww is already up.
168 (org-eww-extend-eww-keymap))
170 (add-hook 'eww-mode-hook #'org-eww-extend-eww-keymap)
173 (provide 'org-eww)
175 ;;; org-eww.el ends here