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