org-eww.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; org-eww.el --- Store url and kill from Eww mode for Org
  2. ;; Copyright (C) 2014, 2015 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. (add-hook 'org-store-link-functions '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-no-next-link-p ()
  58. "Whether there is no next link after the cursor.
  59. Return t if there is no next link; otherwise, return nil."
  60. (save-excursion
  61. (and (eq (point) (org-eww-goto-next-url-property-change)) t)))
  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-make-link-string', and insert the transformed test into the kill ring,
  69. so that it can be yanked into an Org-mode buffer with links working correctly.
  70. Further lines starting with a star get quoted with a comma to keep
  71. the structure of the org file."
  72. (interactive)
  73. (let* ((regionp (org-region-active-p))
  74. (transform-start (point-min))
  75. (transform-end (point-max))
  76. return-content
  77. link-location link-title
  78. temp-position out-bound)
  79. (when regionp
  80. (setq transform-start (region-beginning))
  81. (setq transform-end (region-end))
  82. ;; Deactivate mark if current mark is activate.
  83. (if (fboundp 'deactivate-mark) (deactivate-mark)))
  84. (message "Transforming links...")
  85. (save-excursion
  86. (goto-char transform-start)
  87. (while (and (not out-bound) ; still inside region to copy
  88. (not (org-eww-no-next-link-p))) ; there is a next link
  89. ;; store current point before jump next anchor
  90. (setq temp-position (point))
  91. ;; move to next anchor when current point is not at anchor
  92. (or (org-eww-url-below-point)
  93. (org-eww-goto-next-url-property-change))
  94. (assert (org-eww-url-below-point) t
  95. "program logic error: point must have an url below but it hasn't")
  96. (if (<= (point) transform-end) ; if point is inside transform bound
  97. (progn
  98. ;; get content between two links.
  99. (if (> (point) temp-position)
  100. (setq return-content (concat return-content
  101. (buffer-substring
  102. temp-position (point)))))
  103. ;; get link location at current point.
  104. (setq link-location (org-eww-url-below-point))
  105. ;; get link title at current point.
  106. (setq link-title
  107. (buffer-substring
  108. (point)
  109. (org-eww-goto-next-url-property-change)))
  110. ;; concat `org-mode' style url to `return-content'.
  111. (setq return-content (concat return-content
  112. (org-make-link-string
  113. link-location link-title))))
  114. (goto-char temp-position) ; reset point before jump next anchor
  115. (setq out-bound t) ; for break out `while' loop
  116. ))
  117. ;; add the rest until end of the region to be copied
  118. (if (< (point) transform-end)
  119. (setq return-content
  120. (concat return-content
  121. (buffer-substring (point) transform-end))))
  122. ;; quote lines starting with *
  123. (org-kill-new
  124. (with-temp-buffer
  125. (insert return-content)
  126. (goto-char 0)
  127. (replace-regexp "^\*" ",*")
  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