ol-eww.el 6.0 KB

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