org-eww.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; org-eww.el --- Store url and kill from Eww mode for Org -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2014-2019 Free Software Foundation, Inc.
  3. ;; Author: Marco Wahl <marcowahlsoft>a<gmailcom>
  4. ;; Keywords: link, eww
  5. ;; Homepage: https://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; When this module is active `org-store-link' (often on key C-c l) in
  19. ;; an EWW buffer stores a link to the current url of the eww buffer.
  20. ;; In an EWW buffer function `org-eww-copy-for-org-mode' kills either
  21. ;; a region or the whole buffer if no region is set and transforms the
  22. ;; text on the fly so that it can be pasted into an Org buffer with
  23. ;; hot links.
  24. ;; C-c C-x C-w (and also C-c C-x M-w) trigger
  25. ;; `org-eww-copy-for-org-mode'.
  26. ;; Hint: A lot of code of this module comes from module org-w3m which
  27. ;; has been written by Andy Steward based on the idea of Richard
  28. ;; Riley. Thanks!
  29. ;; Potential: Since the code for w3m and eww is so similar one could
  30. ;; try to refactor.
  31. ;;; Code:
  32. (require 'org)
  33. (require 'cl-lib)
  34. (defvar eww-current-title)
  35. (defvar eww-current-url)
  36. (defvar eww-data)
  37. (defvar eww-mode-map)
  38. (declare-function eww-current-url "eww")
  39. ;; Store Org-link in eww-mode buffer
  40. (org-link-set-parameters "eww" :follow #'eww :store #'org-eww-store-link)
  41. (defun org-eww-store-link ()
  42. "Store a link to the url of an EWW buffer."
  43. (when (eq major-mode 'eww-mode)
  44. (org-store-link-props
  45. :type "eww"
  46. :link (if (< emacs-major-version 25)
  47. eww-current-url
  48. (eww-current-url))
  49. :url (url-view-url t)
  50. :description (if (< emacs-major-version 25)
  51. (or eww-current-title eww-current-url)
  52. (or (plist-get eww-data :title)
  53. (eww-current-url))))))
  54. ;; Some auxiliary functions concerning links in eww buffers
  55. (defun org-eww-goto-next-url-property-change ()
  56. "Move to the start of next link if exists.
  57. Otherwise point is not moved. Return point."
  58. (goto-char
  59. (or (next-single-property-change (point) 'shr-url)
  60. (point))))
  61. (defun org-eww-has-further-url-property-change-p ()
  62. "Non-nil if there is a next url property change."
  63. (save-excursion
  64. (not (eq (point) (org-eww-goto-next-url-property-change)))))
  65. (defun org-eww-url-below-point ()
  66. "Return the url below point if there is an url; otherwise, return nil."
  67. (get-text-property (point) 'shr-url))
  68. (defun org-eww-copy-for-org-mode ()
  69. "Copy current buffer content or active region with `org-mode' style links.
  70. This will encode `link-title' and `link-location' with
  71. `org-make-link-string' and insert the transformed text into the
  72. kill ring, so that it can be yanked into an Org mode buffer with
  73. links working correctly.
  74. Further lines starting with a star get quoted with a comma to
  75. keep the structure of the Org file."
  76. (interactive)
  77. (let* ((regionp (org-region-active-p))
  78. (transform-start (point-min))
  79. (transform-end (point-max))
  80. return-content
  81. link-location link-title
  82. temp-position out-bound)
  83. (when regionp
  84. (setq transform-start (region-beginning))
  85. (setq transform-end (region-end))
  86. ;; Deactivate mark if current mark is activate.
  87. (when (fboundp 'deactivate-mark) (deactivate-mark)))
  88. (message "Transforming links...")
  89. (save-excursion
  90. (goto-char transform-start)
  91. (while (and (not out-bound) ; still inside region to copy
  92. (org-eww-has-further-url-property-change-p)) ; there is a next link
  93. ;; Store current point before jump next anchor.
  94. (setq temp-position (point))
  95. ;; Move to next anchor when current point is not at anchor.
  96. (or (org-eww-url-below-point)
  97. (org-eww-goto-next-url-property-change))
  98. (cl-assert
  99. (org-eww-url-below-point) t
  100. "program logic error: point must have an url below but it hasn't")
  101. (if (<= (point) transform-end) ; if point is inside transform bound
  102. (progn
  103. ;; Get content between two links.
  104. (when (< temp-position (point))
  105. (setq return-content (concat return-content
  106. (buffer-substring
  107. temp-position (point)))))
  108. ;; Get link location at current point.
  109. (setq link-location (org-eww-url-below-point))
  110. ;; Get link title at current point.
  111. (setq link-title
  112. (buffer-substring
  113. (point)
  114. (org-eww-goto-next-url-property-change)))
  115. ;; concat `org-mode' style url to `return-content'.
  116. (setq return-content
  117. (concat return-content
  118. (if (org-string-nw-p link-location)
  119. ;; Hint: link-location is different
  120. ;; for form-elements.
  121. (org-make-link-string link-location link-title)
  122. link-title))))
  123. (goto-char temp-position) ; reset point before jump next anchor
  124. (setq out-bound t))) ; for break out `while' loop
  125. ;; Add the rest until end of the region to be copied.
  126. (when (< (point) transform-end)
  127. (setq return-content
  128. (concat return-content
  129. (buffer-substring (point) transform-end))))
  130. ;; Quote lines starting with *.
  131. (org-kill-new (replace-regexp-in-string "^\\*" ",*" return-content))
  132. (message "Transforming links...done, use C-y to insert text into Org mode file"))))
  133. ;; Additional keys for eww-mode
  134. (defun org-eww-extend-eww-keymap ()
  135. (define-key eww-mode-map "\C-c\C-x\M-w" 'org-eww-copy-for-org-mode)
  136. (define-key eww-mode-map "\C-c\C-x\C-w" 'org-eww-copy-for-org-mode))
  137. (when (and (boundp 'eww-mode-map)
  138. (keymapp eww-mode-map)) ; eww is already up.
  139. (org-eww-extend-eww-keymap))
  140. (add-hook 'eww-mode-hook #'org-eww-extend-eww-keymap)
  141. (provide 'org-eww)
  142. ;;; org-eww.el ends here