org-w3m.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27. ;;
  28. ;;; Acknowledgments:
  29. ;; Richard Riley <rileyrgdev at googlemail dot com>
  30. ;;
  31. ;; The idea that transfomring the HTML content with org-mode style is
  32. ;; proposed by Richard, i'm just code it.
  33. ;;
  34. (defun org-w3m-copy-for-org-mode ()
  35. "Copy current buffer content or active region with `org-mode' style links.
  36. This will encode `link-title' and `link-location' with
  37. `org-make-link-string', and insert the transformed test into the kill ring,
  38. so that it can be yanked into an Org-mode buffer with links working correctly."
  39. (interactive)
  40. (let ((regionp (org-region-active-p))
  41. transform-start transform-end
  42. return-content
  43. link-location link-title
  44. temp-position out-bound)
  45. (setq transform-start (if regionp (region-beginning) (point-min))
  46. transform-end (if regionp (region-end) (point-max)))
  47. (message "Transforming links...")
  48. (save-excursion
  49. (goto-char transform-start)
  50. (while (and (not out-bound) ; still inside region to copy
  51. (not (org-w3m-no-next-link-p))) ; no next link current buffer
  52. ;; store current point before jump next anchor
  53. (setq temp-position (point))
  54. ;; move to next anchor when current point is not at anchor
  55. (or (w3m-anchor (point)) (org-w3m-get-next-link-start))
  56. (if (<= (point) transform-end) ; if point is inside transform bound
  57. (progn
  58. ;; get content between two links.
  59. (if (> (point) temp-position)
  60. (setq return-content (concat return-content
  61. (buffer-substring
  62. temp-position (point)))))
  63. ;; get link location at current point.
  64. (setq link-location (w3m-anchor (point)))
  65. ;; get link title at current point.
  66. (setq link-title (buffer-substring (point)
  67. (org-w3m-get-anchor-end)))
  68. ;; concat `org-mode' style url to `return-content'.
  69. (setq return-content (concat return-content
  70. (org-make-link-string
  71. link-location link-title))))
  72. (goto-char temp-position) ; reset point before jump next anchor
  73. (setq out-bound t) ; for break out `while' loop
  74. ))
  75. ;; add the rest until en end of the region to be copied
  76. (if (< (point) transform-end)
  77. (setq return-content
  78. (concat return-content
  79. (buffer-substring (point) transform-end))))
  80. (kill-new return-content)
  81. (message "Transforming links...done, use C-y to insert text into Org-mode file")
  82. (message "Copy with link transformation complete."))))
  83. (defun org-w3m-get-anchor-start ()
  84. "Move to and return `point' for the start of the current anchor."
  85. ;; get start position of anchor or current point
  86. (goto-char (or (previous-single-property-change (point) 'w3m-anchor-sequence)
  87. (point))))
  88. (defun org-w3m-get-anchor-end ()
  89. "Move and return `point' after the end of current anchor."
  90. ;; get end position of anchor or point
  91. (goto-char (or (next-single-property-change (point) 'w3m-anchor-sequence)
  92. (point))))
  93. (defun org-w3m-get-next-link-start ()
  94. "Move and return `point' for that start of the current link."
  95. (catch 'reach
  96. (while (next-single-property-change (point) 'w3m-anchor-sequence)
  97. ;; jump to next anchor
  98. (goto-char (next-single-property-change (point) 'w3m-anchor-sequence))
  99. (when (w3m-anchor (point))
  100. ;; return point when current is valid link
  101. (throw 'reach nil))))
  102. (point))
  103. (defun org-w3m-get-prev-link-start ()
  104. "Move and return `point' for that end of the current link."
  105. (catch 'reach
  106. (while (previous-single-property-change (point) 'w3m-anchor-sequence)
  107. ;; jump to previous anchor
  108. (goto-char (previous-single-property-change (point) 'w3m-anchor-sequence))
  109. (when (w3m-anchor (point))
  110. ;; return point when current is valid link
  111. (throw 'reach nil))))
  112. (point))
  113. (defun org-w3m-no-next-link-p ()
  114. "Return t if no next link after cursor.
  115. Otherwise, return nil."
  116. (save-excursion
  117. (equal (point) (org-w3m-get-next-link-start))))
  118. (defun org-w3m-no-prev-link-p ()
  119. "Return t if no prevoius link after cursor.
  120. Otherwise, return nil."
  121. (save-excursion
  122. (equal (point) (org-w3m-get-prev-link-start))))
  123. ;; Install keys into the w3m keymap
  124. (when (and (boundp 'w3m-mode-map)
  125. (keymapp w3m-mode-map))
  126. (define-key w3m-mode-map "\C-c\C-x\M-w" 'org-w3m-copy-for-org-mode)
  127. (define-key w3m-mode-map "\C-c\C-x\C-w" 'org-w3m-copy-for-org-mode))
  128. (add-hook
  129. 'w3m-mode-hook
  130. (lambda ()
  131. (define-key w3m-mode-map "\C-c\C-x\M-w" 'org-w3m-copy-for-org-mode)
  132. (define-key w3m-mode-map "\C-c\C-x\C-w" 'org-w3m-copy-for-org-mode)))
  133. (provide 'org-w3m)
  134. ;; arch-tag: 851d7447-488d-49f0-a14d-46c092e84352
  135. ;;; org-w3m.el ends here