ol-eww.el 5.9 KB

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