org-eww: Fix docstrings
[org-mode/org-tableheadings.git] / contrib / lisp / org-eww.el
blobebabf82b5e06088206c14a811eda14a90074941c
1 ;;; org-eww.el --- Store url and kill from Eww mode for Org -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 ;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
6 ;; Keywords: link, eww
7 ;; Homepage: http://orgmode.org
8 ;;
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/>.
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-mode buffer
33 ;; with 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)
50 ;; Store Org-link in eww-mode buffer
51 (org-link-set-parameters "eww" :follow #'eww :store #'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)
55 (org-store-link-props
56 :type "eww"
57 :link (if (< emacs-major-version 25)
58 eww-current-url
59 (eww-current-url))
60 :url (url-view-url t)
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 to the start of next link if exists.
70 Otherwise point is not moved. Return point."
71 (goto-char
72 (or (next-single-property-change (point) 'shr-url)
73 (point))))
75 (defun org-eww-has-further-url-property-change-p ()
76 "Non-nil if there is a next url property change."
77 (save-excursion
78 (not (eq (point) (org-eww-goto-next-url-property-change)))))
80 (defun org-eww-url-below-point ()
81 "Return the url below point if there is an url; otherwise, return nil."
82 (get-text-property (point) 'shr-url))
85 (defun org-eww-copy-for-org-mode ()
86 "Copy current buffer content or active region with `org-mode' style links.
87 This will encode `link-title' and `link-location' with
88 `org-make-link-string', and insert the transformed test into the kill ring,
89 so that it can be yanked into an Org mode buffer with links working correctly.
91 Further lines starting with a star get quoted with a comma to keep
92 the structure of the Org file."
93 (interactive)
94 (let* ((regionp (org-region-active-p))
95 (transform-start (point-min))
96 (transform-end (point-max))
97 return-content
98 link-location link-title
99 temp-position out-bound)
100 (when regionp
101 (setq transform-start (region-beginning))
102 (setq transform-end (region-end))
103 ;; Deactivate mark if current mark is activate.
104 (if (fboundp 'deactivate-mark) (deactivate-mark)))
105 (message "Transforming links...")
106 (save-excursion
107 (goto-char transform-start)
108 (while (and (not out-bound) ; still inside region to copy
109 (org-eww-has-further-url-property-change-p)) ; there is a next link
110 ;; store current point before jump next anchor
111 (setq temp-position (point))
112 ;; move to next anchor when current point is not at anchor
113 (or (org-eww-url-below-point)
114 (org-eww-goto-next-url-property-change))
115 (assert (org-eww-url-below-point) t
116 "program logic error: point must have an url below but it hasn't")
117 (if (<= (point) transform-end) ; if point is inside transform bound
118 (progn
119 ;; get content between two links.
120 (if (< temp-position (point))
121 (setq return-content (concat return-content
122 (buffer-substring
123 temp-position (point)))))
124 ;; get link location at current point.
125 (setq link-location (org-eww-url-below-point))
126 ;; get link title at current point.
127 (setq link-title
128 (buffer-substring
129 (point)
130 (org-eww-goto-next-url-property-change)))
131 ;; concat `org-mode' style url to `return-content'.
132 (setq return-content (concat return-content
133 (org-make-link-string
134 link-location link-title))))
135 (goto-char temp-position) ; reset point before jump next anchor
136 (setq out-bound t) ; for break out `while' loop
138 ;; add the rest until end of the region to be copied
139 (if (< (point) transform-end)
140 (setq return-content
141 (concat return-content
142 (buffer-substring (point) transform-end))))
143 ;; quote lines starting with *
144 (org-kill-new
145 (with-temp-buffer
146 (insert return-content)
147 (goto-char 0)
148 (while (re-search-forward "^\*" nil t)
149 (replace-match ",*"))
150 (buffer-string)))
151 (message "Transforming links...done, use C-y to insert text into Org-mode file"))))
154 ;; Additional keys for eww-mode
156 (defun org-eww-extend-eww-keymap ()
157 (define-key eww-mode-map "\C-c\C-x\M-w" 'org-eww-copy-for-org-mode)
158 (define-key eww-mode-map "\C-c\C-x\C-w" 'org-eww-copy-for-org-mode))
160 (when (and (boundp 'eww-mode-map)
161 (keymapp eww-mode-map)) ; eww is already up.
162 (org-eww-extend-eww-keymap))
164 (add-hook
165 'eww-mode-hook
166 (lambda () (org-eww-extend-eww-keymap)))
169 (provide 'org-eww)
171 ;;; org-eww.el ends here