ol-eww.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ;;; ol-eww.el --- Store URL and kill from Eww mode -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2014-2022 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 'ol)
  33. (require 'cl-lib)
  34. (require 'eww)
  35. ;; For Emacsen < 25.
  36. (defvar eww-current-title)
  37. (defvar eww-current-url)
  38. ;; Store Org link in Eww mode buffer
  39. (org-link-set-parameters "eww"
  40. :follow #'org-eww-open
  41. :store #'org-eww-store-link)
  42. (defun org-eww-open (url _)
  43. "Open URL with Eww in the current buffer."
  44. (eww url))
  45. (defun org-eww-store-link ()
  46. "Store a link to the url of an EWW buffer."
  47. (when (eq major-mode 'eww-mode)
  48. (org-link-store-props
  49. :type "eww"
  50. :link (if (< emacs-major-version 25)
  51. eww-current-url
  52. (eww-current-url))
  53. :url (url-view-url t)
  54. :description (if (< emacs-major-version 25)
  55. (or eww-current-title eww-current-url)
  56. (or (plist-get eww-data :title)
  57. (eww-current-url))))))
  58. ;; Some auxiliary functions concerning links in Eww buffers
  59. (defun org-eww-goto-next-url-property-change ()
  60. "Move to the start of next link if exists.
  61. Otherwise point is not moved. Return point."
  62. (goto-char
  63. (or (next-single-property-change (point) 'shr-url)
  64. (point))))
  65. (defun org-eww-has-further-url-property-change-p ()
  66. "Non-nil if there is a next url property change."
  67. (save-excursion
  68. (not (eq (point) (org-eww-goto-next-url-property-change)))))
  69. (defun org-eww-url-below-point ()
  70. "Return the url below point if there is an url; otherwise, return nil."
  71. (get-text-property (point) 'shr-url))
  72. (defun org-eww-copy-for-org-mode ()
  73. "Copy current buffer content or active region with `org-mode' style links.
  74. This will encode `link-title' and `link-location' with
  75. `org-link-make-string' and insert the transformed text into the
  76. kill ring, so that it can be yanked into an Org mode buffer with
  77. links working correctly.
  78. Further lines starting with a star get quoted with a comma to
  79. keep the structure of the Org file."
  80. (interactive)
  81. (let* ((regionp (org-region-active-p))
  82. (transform-start (point-min))
  83. (transform-end (point-max))
  84. return-content
  85. link-location link-title
  86. temp-position out-bound)
  87. (when regionp
  88. (setq transform-start (region-beginning))
  89. (setq transform-end (region-end))
  90. ;; Deactivate mark if current mark is activate.
  91. (deactivate-mark))
  92. (message "Transforming links...")
  93. (save-excursion
  94. (goto-char transform-start)
  95. (while (and (not out-bound) ; still inside region to copy
  96. (org-eww-has-further-url-property-change-p)) ; there is a next link
  97. ;; Store current point before jump next anchor.
  98. (setq temp-position (point))
  99. ;; Move to next anchor when current point is not at anchor.
  100. (or (org-eww-url-below-point)
  101. (org-eww-goto-next-url-property-change))
  102. (cl-assert
  103. (org-eww-url-below-point) t
  104. "program logic error: point must have an url below but it hasn't")
  105. (if (<= (point) transform-end) ; if point is inside transform bound
  106. (progn
  107. ;; Get content between two links.
  108. (when (< temp-position (point))
  109. (setq return-content (concat return-content
  110. (buffer-substring
  111. temp-position (point)))))
  112. ;; Get link location at current point.
  113. (setq link-location (org-eww-url-below-point))
  114. ;; Get link title at current point.
  115. (setq link-title
  116. (buffer-substring
  117. (point)
  118. (org-eww-goto-next-url-property-change)))
  119. ;; concat `org-mode' style url to `return-content'.
  120. (setq return-content
  121. (concat return-content
  122. (if (org-string-nw-p link-location)
  123. ;; Hint: link-location is different
  124. ;; for form-elements.
  125. (org-link-make-string link-location link-title)
  126. link-title))))
  127. (goto-char temp-position) ; reset point before jump next anchor
  128. (setq out-bound t))) ; for break out `while' loop
  129. ;; Add the rest until end of the region to be copied.
  130. (when (< (point) transform-end)
  131. (setq return-content
  132. (concat return-content
  133. (buffer-substring (point) transform-end))))
  134. ;; Quote lines starting with *.
  135. (org-kill-new (replace-regexp-in-string "^\\*" ",*" return-content))
  136. (message "Transforming links...done, use C-y to insert text into Org mode file"))))
  137. ;; Additional keys for eww-mode
  138. (defun org-eww-extend-eww-keymap ()
  139. (define-key eww-mode-map "\C-c\C-x\M-w" 'org-eww-copy-for-org-mode)
  140. (define-key eww-mode-map "\C-c\C-x\C-w" 'org-eww-copy-for-org-mode))
  141. (when (and (boundp 'eww-mode-map)
  142. (keymapp eww-mode-map)) ; eww is already up.
  143. (org-eww-extend-eww-keymap))
  144. (add-hook 'eww-mode-hook #'org-eww-extend-eww-keymap)
  145. (provide 'ol-eww)
  146. ;;; ol-eww.el ends here