org-eww.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; org-eww.el --- Store url and kill from Eww mode for Org -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2014-2016 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 not 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-mode buffer
  25. ;; with 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. ;; Store Org-link in eww-mode buffer
  36. (org-link-set-parameters "eww" :store #'org-eww-store-link)
  37. (defun org-eww-store-link ()
  38. "Store a link to the url of a eww buffer."
  39. (when (eq major-mode 'eww-mode)
  40. (org-store-link-props
  41. :type "eww"
  42. :link (if (< emacs-major-version 25)
  43. eww-current-url
  44. (eww-current-url))
  45. :url (url-view-url t)
  46. :description (if (< emacs-major-version 25)
  47. (or eww-current-title eww-current-url)
  48. (or (plist-get eww-data :title)
  49. (eww-current-url))))))
  50. ;; Some auxiliary functions concerning links in eww buffers
  51. (defun org-eww-goto-next-url-property-change ()
  52. "Move cursor to the start of next link if exists. Else no
  53. move. Return point."
  54. (goto-char
  55. (or (next-single-property-change (point) 'shr-url)
  56. (point))))
  57. (defun org-eww-has-further-url-property-change-p ()
  58. "Return t if there is a next url property change else nil."
  59. (save-excursion
  60. (not (eq (point) (org-eww-goto-next-url-property-change)))))
  61. (defun org-eww-url-below-point ()
  62. "Return the url below point if there is an url; otherwise, return nil."
  63. (get-text-property (point) 'shr-url))
  64. (defun org-eww-copy-for-org-mode ()
  65. "Copy current buffer content or active region with `org-mode' style links.
  66. This will encode `link-title' and `link-location' with
  67. `org-make-link-string', and insert the transformed test into the kill ring,
  68. so that it can be yanked into an Org-mode buffer with links working correctly.
  69. Further lines starting with a star get quoted with a comma to keep
  70. the structure of the org file."
  71. (interactive)
  72. (let* ((regionp (org-region-active-p))
  73. (transform-start (point-min))
  74. (transform-end (point-max))
  75. return-content
  76. link-location link-title
  77. temp-position out-bound)
  78. (when regionp
  79. (setq transform-start (region-beginning))
  80. (setq transform-end (region-end))
  81. ;; Deactivate mark if current mark is activate.
  82. (if (fboundp 'deactivate-mark) (deactivate-mark)))
  83. (message "Transforming links...")
  84. (save-excursion
  85. (goto-char transform-start)
  86. (while (and (not out-bound) ; still inside region to copy
  87. (org-eww-has-further-url-property-change-p)) ; there is a next link
  88. ;; store current point before jump next anchor
  89. (setq temp-position (point))
  90. ;; move to next anchor when current point is not at anchor
  91. (or (org-eww-url-below-point)
  92. (org-eww-goto-next-url-property-change))
  93. (assert (org-eww-url-below-point) t
  94. "program logic error: point must have an url below but it hasn't")
  95. (if (<= (point) transform-end) ; if point is inside transform bound
  96. (progn
  97. ;; get content between two links.
  98. (if (< temp-position (point))
  99. (setq return-content (concat return-content
  100. (buffer-substring
  101. temp-position (point)))))
  102. ;; get link location at current point.
  103. (setq link-location (org-eww-url-below-point))
  104. ;; get link title at current point.
  105. (setq link-title
  106. (buffer-substring
  107. (point)
  108. (org-eww-goto-next-url-property-change)))
  109. ;; concat `org-mode' style url to `return-content'.
  110. (setq return-content (concat return-content
  111. (org-make-link-string
  112. link-location link-title))))
  113. (goto-char temp-position) ; reset point before jump next anchor
  114. (setq out-bound t) ; for break out `while' loop
  115. ))
  116. ;; add the rest until end of the region to be copied
  117. (if (< (point) transform-end)
  118. (setq return-content
  119. (concat return-content
  120. (buffer-substring (point) transform-end))))
  121. ;; quote lines starting with *
  122. (org-kill-new
  123. (with-temp-buffer
  124. (insert return-content)
  125. (goto-char 0)
  126. (while (re-search-forward "^\*" nil t)
  127. (replace-match ",*"))
  128. (buffer-string)))
  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
  138. 'eww-mode-hook
  139. (lambda () (org-eww-extend-eww-keymap)))
  140. (provide 'org-eww)
  141. ;;; org-eww.el ends here