2019-03-02 00:01:32 +00:00
|
|
|
|
;;; ol-eww.el --- Store URL and kill from Eww mode -*- lexical-binding: t -*-
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2021-01-01 19:55:31 +00:00
|
|
|
|
;; Copyright (C) 2014-2021 Free Software Foundation, Inc.
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
|
|
|
|
;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
|
|
|
|
|
;; Keywords: link, eww
|
2018-01-16 16:22:00 +00:00
|
|
|
|
;; Homepage: https://orgmode.org
|
2017-07-05 18:08:35 +00:00
|
|
|
|
|
2016-08-20 00:59:56 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
2017-07-05 18:08:35 +00:00
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2014-09-23 07:46:34 +00:00
|
|
|
|
;; 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.
|
|
|
|
|
|
2017-07-05 18:08:35 +00:00
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2014-09-23 07:46:34 +00:00
|
|
|
|
;; 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
|
2017-09-13 22:52:52 +00:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; When this module is active `org-store-link' (often on key C-c l) in
|
2018-02-16 20:16:15 +00:00
|
|
|
|
;; an EWW buffer stores a link to the current url of the eww buffer.
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2018-02-16 20:16:15 +00:00
|
|
|
|
;; In an EWW buffer function `org-eww-copy-for-org-mode' kills either
|
2014-10-07 14:07:15 +00:00
|
|
|
|
;; a region or the whole buffer if no region is set and transforms the
|
2016-08-23 20:13:56 +00:00
|
|
|
|
;; text on the fly so that it can be pasted into an Org buffer with
|
|
|
|
|
;; hot links.
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2014-10-07 14:07:15 +00:00
|
|
|
|
;; 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.
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
2018-11-26 23:04:41 +00:00
|
|
|
|
(require 'ol)
|
2016-08-20 00:59:56 +00:00
|
|
|
|
(require 'cl-lib)
|
2020-02-17 10:28:50 +00:00
|
|
|
|
(require 'eww)
|
2016-08-20 00:59:56 +00:00
|
|
|
|
|
2020-02-17 10:28:50 +00:00
|
|
|
|
;; For Emacsen < 25.
|
2016-08-20 00:59:56 +00:00
|
|
|
|
(defvar eww-current-title)
|
|
|
|
|
(defvar eww-current-url)
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2014-10-07 14:07:15 +00:00
|
|
|
|
|
2019-03-02 00:01:32 +00:00
|
|
|
|
;; Store Org link in Eww mode buffer
|
2020-02-17 10:28:50 +00:00
|
|
|
|
(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))
|
|
|
|
|
|
2014-09-23 07:46:34 +00:00
|
|
|
|
(defun org-eww-store-link ()
|
2018-02-16 20:16:15 +00:00
|
|
|
|
"Store a link to the url of an EWW buffer."
|
2014-09-23 07:46:34 +00:00
|
|
|
|
(when (eq major-mode 'eww-mode)
|
2018-11-26 23:04:41 +00:00
|
|
|
|
(org-link-store-props
|
2014-09-23 07:46:34 +00:00
|
|
|
|
:type "eww"
|
2014-11-26 09:38:43 +00:00
|
|
|
|
:link (if (< emacs-major-version 25)
|
|
|
|
|
eww-current-url
|
|
|
|
|
(eww-current-url))
|
2014-09-23 07:46:34 +00:00
|
|
|
|
:url (url-view-url t)
|
2014-11-26 09:38:43 +00:00
|
|
|
|
:description (if (< emacs-major-version 25)
|
|
|
|
|
(or eww-current-title eww-current-url)
|
|
|
|
|
(or (plist-get eww-data :title)
|
2016-08-08 09:28:12 +00:00
|
|
|
|
(eww-current-url))))))
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2014-10-07 14:07:15 +00:00
|
|
|
|
|
2019-03-02 00:01:32 +00:00
|
|
|
|
;; Some auxiliary functions concerning links in Eww buffers
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(defun org-eww-goto-next-url-property-change ()
|
2016-08-08 09:23:24 +00:00
|
|
|
|
"Move to the start of next link if exists.
|
|
|
|
|
Otherwise point is not moved. Return point."
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(goto-char
|
|
|
|
|
(or (next-single-property-change (point) 'shr-url)
|
|
|
|
|
(point))))
|
|
|
|
|
|
2016-01-09 22:14:47 +00:00
|
|
|
|
(defun org-eww-has-further-url-property-change-p ()
|
2016-08-08 09:23:24 +00:00
|
|
|
|
"Non-nil if there is a next url property change."
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(save-excursion
|
2016-01-09 22:14:47 +00:00
|
|
|
|
(not (eq (point) (org-eww-goto-next-url-property-change)))))
|
2014-10-07 14:07:15 +00:00
|
|
|
|
|
|
|
|
|
(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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(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
|
2018-11-26 23:04:41 +00:00
|
|
|
|
`org-link-make-string' and insert the transformed text into the
|
2018-04-26 07:56:45 +00:00
|
|
|
|
kill ring, so that it can be yanked into an Org mode buffer with
|
|
|
|
|
links working correctly.
|
2015-07-11 10:01:02 +00:00
|
|
|
|
|
2018-04-26 07:56:45 +00:00
|
|
|
|
Further lines starting with a star get quoted with a comma to
|
|
|
|
|
keep the structure of the Org file."
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(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.
|
2016-08-08 09:28:12 +00:00
|
|
|
|
(when (fboundp 'deactivate-mark) (deactivate-mark)))
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(message "Transforming links...")
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char transform-start)
|
2016-08-08 09:28:12 +00:00
|
|
|
|
(while (and (not out-bound) ; still inside region to copy
|
2016-01-09 22:14:47 +00:00
|
|
|
|
(org-eww-has-further-url-property-change-p)) ; there is a next link
|
2016-08-08 09:28:12 +00:00
|
|
|
|
;; Store current point before jump next anchor.
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(setq temp-position (point))
|
2016-08-08 09:28:12 +00:00
|
|
|
|
;; Move to next anchor when current point is not at anchor.
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(or (org-eww-url-below-point)
|
|
|
|
|
(org-eww-goto-next-url-property-change))
|
2016-08-20 00:59:56 +00:00
|
|
|
|
(cl-assert
|
|
|
|
|
(org-eww-url-below-point) t
|
|
|
|
|
"program logic error: point must have an url below but it hasn't")
|
2016-08-08 09:28:12 +00:00
|
|
|
|
(if (<= (point) transform-end) ; if point is inside transform bound
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(progn
|
2016-08-08 09:28:12 +00:00
|
|
|
|
;; 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.
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(setq link-location (org-eww-url-below-point))
|
2016-08-08 09:28:12 +00:00
|
|
|
|
;; Get link title at current point.
|
2014-10-07 14:07:15 +00:00
|
|
|
|
(setq link-title
|
|
|
|
|
(buffer-substring
|
|
|
|
|
(point)
|
|
|
|
|
(org-eww-goto-next-url-property-change)))
|
|
|
|
|
;; concat `org-mode' style url to `return-content'.
|
2017-02-12 08:00:56 +00:00
|
|
|
|
(setq return-content
|
|
|
|
|
(concat return-content
|
2018-04-14 17:30:17 +00:00
|
|
|
|
(if (org-string-nw-p link-location)
|
|
|
|
|
;; Hint: link-location is different
|
|
|
|
|
;; for form-elements.
|
2018-11-26 23:04:41 +00:00
|
|
|
|
(org-link-make-string link-location link-title)
|
2017-02-12 08:00:56 +00:00
|
|
|
|
link-title))))
|
2016-08-08 09:28:12 +00:00
|
|
|
|
(goto-char temp-position) ; reset point before jump next anchor
|
2018-04-14 17:30:17 +00:00
|
|
|
|
(setq out-bound t))) ; for break out `while' loop
|
2016-08-08 09:28:12 +00:00
|
|
|
|
;; 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"))))
|
2014-10-07 14:07:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; 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))
|
|
|
|
|
|
2016-08-08 09:28:12 +00:00
|
|
|
|
(add-hook 'eww-mode-hook #'org-eww-extend-eww-keymap)
|
2014-10-07 14:07:15 +00:00
|
|
|
|
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2019-03-02 00:01:32 +00:00
|
|
|
|
(provide 'ol-eww)
|
2014-09-23 07:46:34 +00:00
|
|
|
|
|
2019-03-02 00:01:32 +00:00
|
|
|
|
;;; ol-eww.el ends here
|