org-eww.el 6.1 KB

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