org-eww.el 5.6 KB

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