emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob cb52c2e2448351eec526d7140a3de8f5c7db5ae9 6142 bytes (raw)
name: lisp/ol-eww.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
 
;;; ol-eww.el --- Store URL and kill from Eww mode    -*- lexical-binding: t -*-

;; Copyright (C) 2014-2023 Free Software Foundation, Inc.

;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
;; Keywords: link, eww
;; URL: https://orgmode.org

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

\f
;;; Commentary:

;; When this module is active `org-store-link' (often on key C-c l) in
;; an EWW buffer stores a link to the current url of the eww buffer.

;; In an EWW buffer function `org-eww-copy-for-org-mode' kills either
;; a region or the whole buffer if no region is set and transforms the
;; text on the fly so that it can be pasted into an Org buffer with
;; hot links.

;; C-c C-x C-w (and also C-c C-x M-w) trigger
;; `org-eww-copy-for-org-mode'.

;; Hint: A lot of code of this module comes from module org-w3m which
;; has been written by Andy Steward based on the idea of Richard
;; Riley.  Thanks!

;; Potential: Since the code for w3m and eww is so similar one could
;; try to refactor.

\f
;;; Code:

(require 'org-macs)
(unless (version< emacs-version "29")
  (org-assert-version))

(require 'ol)
(require 'cl-lib)
(require 'eww)

\f
;; Store Org link in Eww mode buffer
(org-link-set-parameters "eww"
			 :follow #'org-eww-open
			 :store #'org-eww-store-link)

(defun org-eww-open (url _)
  "Open URL with Eww in the current buffer."
  (eww url))

(defun org-eww-store-link ()
  "Store a link to the url of an EWW buffer."
  (when (eq major-mode 'eww-mode)
    (org-link-store-props
     :type "eww"
     :link (eww-current-url)
     :url (url-view-url t)
     :description (or (plist-get eww-data :title)
                      (eww-current-url)))))

\f
;; Some auxiliary functions concerning links in Eww buffers
(defun org-eww-goto-next-url-property-change ()
  "Move to the start of next link if exists.
Otherwise point is not moved.  Return point."
  (goto-char
   (or (next-single-property-change (point) 'shr-url)
       (point))))

(defun org-eww-has-further-url-property-change-p ()
  "Non-nil if there is a next url property change."
  (save-excursion
    (not (eq (point) (org-eww-goto-next-url-property-change)))))

(defun org-eww-url-below-point ()
  "Return the url below point if there is an url; otherwise, return nil."
  (get-text-property (point) 'shr-url))

\f
(defun org-eww-copy-for-org-mode ()
  "Copy current buffer content or active region with `org-mode' style links.
This will encode `link-title' and `link-location' with
`org-link-make-string' and insert the transformed text into the
kill ring, so that it can be yanked into an Org mode buffer with
links working correctly.

Further lines starting with a star get quoted with a comma to
keep the structure of the Org file."
  (interactive)
  (let* ((regionp (org-region-active-p))
         (transform-start (point-min))
         (transform-end (point-max))
         return-content
         link-location link-title
         temp-position out-bound)
    (when regionp
      (setq transform-start (region-beginning))
      (setq transform-end (region-end))
      ;; Deactivate mark if current mark is activate.
      (deactivate-mark))
    (message "Transforming links...")
    (save-excursion
      (goto-char transform-start)
      (while (and (not out-bound)	; still inside region to copy
                  (org-eww-has-further-url-property-change-p)) ; there is a next link
        ;; Store current point before jump next anchor.
        (setq temp-position (point))
        ;; Move to next anchor when current point is not at anchor.
        (or (org-eww-url-below-point)
	    (org-eww-goto-next-url-property-change))
	(cl-assert
	 (org-eww-url-below-point) t
	 "program logic error: point must have an url below but it hasn't")
	(if (<= (point) transform-end) ; if point is inside transform bound
	    (progn
	      ;; Get content between two links.
	      (when (< temp-position (point))
		(setq return-content (concat return-content
					     (buffer-substring
					      temp-position (point)))))
	      ;; Get link location at current point.
	      (setq link-location (org-eww-url-below-point))
	      ;; Get link title at current point.
	      (setq link-title
		    (buffer-substring
		     (point)
		     (org-eww-goto-next-url-property-change)))
              ;; concat `org-mode' style url to `return-content'.
	      (setq return-content
		    (concat return-content
			    (if (org-string-nw-p link-location)
				;; Hint: link-location is different
				;; for form-elements.
				(org-link-make-string link-location link-title)
			      link-title))))
	  (goto-char temp-position) ; reset point before jump next anchor
	  (setq out-bound t)))	    ; for break out `while' loop
      ;; Add the rest until end of the region to be copied.
      (when (< (point) transform-end)
	(setq return-content
	      (concat return-content
		      (buffer-substring (point) transform-end))))
      ;; Quote lines starting with *.
      (org-kill-new (replace-regexp-in-string "^\\*" ",*" return-content))
      (message "Transforming links...done, use C-y to insert text into Org mode file"))))

\f
;; Additional keys for eww-mode

(defun org-eww-extend-eww-keymap ()
  (define-key eww-mode-map "\C-c\C-x\M-w" 'org-eww-copy-for-org-mode)
  (define-key eww-mode-map "\C-c\C-x\C-w" 'org-eww-copy-for-org-mode))

(when (and (boundp 'eww-mode-map)
           (keymapp eww-mode-map)) ; eww is already up.
  (org-eww-extend-eww-keymap))

(add-hook 'eww-mode-hook #'org-eww-extend-eww-keymap)

\f
(provide 'ol-eww)

;;; ol-eww.el ends here

debug log:

solving cb52c2e24 ...
found cb52c2e24 in https://list.orgmode.org/orgmode/87o7obn0dp.fsf@localhost/
found ee970b6aa in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 ee970b6aaa1cd4824553fb2a326da0c26597b903	lisp/ol-eww.el

applying [1/1] https://list.orgmode.org/orgmode/87o7obn0dp.fsf@localhost/
diff --git a/lisp/ol-eww.el b/lisp/ol-eww.el
index ee970b6aa..cb52c2e24 100644

Checking patch lisp/ol-eww.el...
Applied patch lisp/ol-eww.el cleanly.

index at:
100644 cb52c2e2448351eec526d7140a3de8f5c7db5ae9	lisp/ol-eww.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).