org-w3m.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ;;; org-w3m.el --- Support from copy and paste from w3m to Org-mode
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;; Author: Andy Stewart <lazycat dot manatee at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.13trans
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file implements copying HTML content from a w3m buffer and
  24. ;; transfomring the text on the fly so that it can be pasted into
  25. ;; an org-mode buffer with hot links. It will also work for regions
  26. ;; in gnus buffers that have ben washed with w3m.
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28. ;;
  29. ;;; Acknowledgments:
  30. ;; Richard Riley <rileyrgdev at googlemail dot com>
  31. ;;
  32. ;; The idea that transfomring the HTML content with org-mode style is
  33. ;; proposed by Richard, i'm just code it.
  34. ;;
  35. (defun org-w3m-copy-for-org-mode ()
  36. "Copy current buffer content or active region with `org-mode' style links.
  37. This will encode `link-title' and `link-location' with
  38. `org-make-link-string', and insert the transformed test into the kill ring,
  39. so that it can be yanked into an Org-mode buffer with links working correctly."
  40. (interactive)
  41. (let ((regionp (org-region-active-p))
  42. transform-start transform-end
  43. return-content
  44. link-location link-title
  45. temp-position out-bound)
  46. (setq transform-start (if regionp (region-beginning) (point-min))
  47. transform-end (if regionp (region-end) (point-max)))
  48. (message "Transforming links...")
  49. (save-excursion
  50. (goto-char transform-start)
  51. (while (and (not out-bound) ; still inside region to copy
  52. (not (org-w3m-no-next-link-p))) ; no next link current buffer
  53. ;; store current point before jump next anchor
  54. (setq temp-position (point))
  55. ;; move to next anchor when current point is not at anchor
  56. (or (w3m-anchor (point)) (org-w3m-get-next-link-start))
  57. (if (<= (point) transform-end) ; if point is inside transform bound
  58. (progn
  59. ;; get content between two links.
  60. (if (> (point) temp-position)
  61. (setq return-content (concat return-content
  62. (buffer-substring
  63. temp-position (point)))))
  64. ;; get link location at current point.
  65. (setq link-location (w3m-anchor (point)))
  66. ;; get link title at current point.
  67. (setq link-title (buffer-substring (point)
  68. (org-w3m-get-anchor-end)))
  69. ;; concat `org-mode' style url to `return-content'.
  70. (setq return-content (concat return-content
  71. (org-make-link-string
  72. link-location link-title))))
  73. (goto-char temp-position) ; reset point before jump next anchor
  74. (setq out-bound t) ; for break out `while' loop
  75. ))
  76. ;; add the rest until en end of the region to be copied
  77. (if (< (point) transform-end)
  78. (setq return-content
  79. (concat return-content
  80. (buffer-substring (point) transform-end))))
  81. (kill-new return-content)
  82. (message "Transforming links...done, use C-y to insert text into Org-mode file")
  83. (message "Copy with link transformation complete."))))
  84. (defun org-w3m-get-anchor-start ()
  85. "Move to and return `point' for the start of the current anchor."
  86. ;; get start position of anchor or current point
  87. (goto-char (or (previous-single-property-change (point) 'w3m-anchor-sequence)
  88. (point))))
  89. (defun org-w3m-get-anchor-end ()
  90. "Move and return `point' after the end of current anchor."
  91. ;; get end position of anchor or point
  92. (goto-char (or (next-single-property-change (point) 'w3m-anchor-sequence)
  93. (point))))
  94. (defun org-w3m-get-next-link-start ()
  95. "Move and return `point' for that start of the current link."
  96. (catch 'reach
  97. (while (next-single-property-change (point) 'w3m-anchor-sequence)
  98. ;; jump to next anchor
  99. (goto-char (next-single-property-change (point) 'w3m-anchor-sequence))
  100. (when (w3m-anchor (point))
  101. ;; return point when current is valid link
  102. (throw 'reach nil))))
  103. (point))
  104. (defun org-w3m-get-prev-link-start ()
  105. "Move and return `point' for that end of the current link."
  106. (catch 'reach
  107. (while (previous-single-property-change (point) 'w3m-anchor-sequence)
  108. ;; jump to previous anchor
  109. (goto-char (previous-single-property-change (point) 'w3m-anchor-sequence))
  110. (when (w3m-anchor (point))
  111. ;; return point when current is valid link
  112. (throw 'reach nil))))
  113. (point))
  114. (defun org-w3m-no-next-link-p ()
  115. "Return t if no next link after cursor.
  116. Otherwise, return nil."
  117. (save-excursion
  118. (equal (point) (org-w3m-get-next-link-start))))
  119. (defun org-w3m-no-prev-link-p ()
  120. "Return t if no prevoius link after cursor.
  121. Otherwise, return nil."
  122. (save-excursion
  123. (equal (point) (org-w3m-get-prev-link-start))))
  124. ;; Install keys into the w3m keymap
  125. (defvar w3m-mode-map)
  126. (when (and (boundp 'w3m-mode-map)
  127. (keymapp w3m-mode-map))
  128. (define-key w3m-mode-map "\C-c\C-x\M-w" 'org-w3m-copy-for-org-mode)
  129. (define-key w3m-mode-map "\C-c\C-x\C-w" 'org-w3m-copy-for-org-mode))
  130. (when (and (boundp 'w3m-minor-mode-map)
  131. (keymapp w3m-minor-mode-map))
  132. (define-key w3m-minor-mode-map "\C-c\C-x\M-w" 'org-w3m-copy-for-org-mode)
  133. (define-key w3m-minor-mode-map "\C-c\C-x\C-w" 'org-w3m-copy-for-org-mode))
  134. (add-hook
  135. 'w3m-mode-hook
  136. (lambda ()
  137. (define-key w3m-mode-map "\C-c\C-x\M-w" 'org-w3m-copy-for-org-mode)
  138. (define-key w3m-mode-map "\C-c\C-x\C-w" 'org-w3m-copy-for-org-mode)))
  139. (add-hook
  140. 'w3m-minor-mode-hook
  141. (lambda ()
  142. (define-key w3m-minor-mode-map "\C-c\C-x\M-w" 'org-w3m-copy-for-org-mode)
  143. (define-key w3m-minor-mode-map "\C-c\C-x\C-w" 'org-w3m-copy-for-org-mode)))
  144. (provide 'org-w3m)
  145. ;; arch-tag: 851d7447-488d-49f0-a14d-46c092e84352
  146. ;;; org-w3m.el ends here