[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

org-w3m: handle w3m-image link information



Attached is a patch that adds to function `org-w3m-copy-for-org-mode'
support for w3m-image links. Note that because function
`org-make-link-string' is used for constructing the buffer substring to
be copied, w3m's special face to distinguish an image link is not
transfered.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
diff --git a/org-w3m.el b/org-w3m.el
index 9690c8e..9b3a092 100644
--- a/org-w3m.el
+++ b/org-w3m.el
@@ -82,25 +82,42 @@ so that it can be yanked into an Org  buffer with links working correctly."
         (setq temp-position (point))
         ;; move to next anchor when current point is not at anchor
         (or (get-text-property (point) 'w3m-href-anchor) (org-w3m-get-next-link-start))
-        (if (<= (point) transform-end)  ; if point is inside transform bound
-            (progn
-              ;; get content between two links.
-              (if (> (point) temp-position)
-                  (setq return-content (concat return-content
-                                               (buffer-substring
-                                                temp-position (point)))))
-              ;; get link location at current point.
-              (setq link-location (get-text-property (point) 'w3m-href-anchor))
-              ;; get link title at current point.
-              (setq link-title (buffer-substring (point)
-                                                 (org-w3m-get-anchor-end)))
-              ;; concat Org style url to `return-content'.
-              (setq return-content (concat return-content
-                                           (org-make-link-string
-                                            link-location link-title))))
+        (cond
+         ((<= (point) transform-end) ; point is inside transform bound
+           ;; get content between two links.
+           (when (> (point) temp-position)
+             (setq return-content (concat return-content
+                                          (buffer-substring
+                                           temp-position (point)))))
+           (cond
+            ((setq link-location (get-text-property (point) 'w3m-href-anchor))
+             ;; current point is a link
+             ;; (we thus also got link location at current point)
+             ;; get link title at current point.
+             (setq link-title (buffer-substring (point)
+                                                (org-w3m-get-anchor-end)))
+             ;; concat Org style url to `return-content'.
+             (setq return-content (concat return-content
+                                          (org-make-link-string
+                                           link-location link-title))))
+            ((setq link-location (get-text-property (point) 'w3m-image))
+             ;; current point is an image
+             ;; (we thus also got image link location at current point)
+             ;; get link title at current point.
+             (setq link-title (buffer-substring (point) (org-w3m-get-image-end)))
+             ;; concat Org style url to `return-content'.
+             (setq return-content (concat return-content
+                                          (org-make-link-string
+                                           link-location link-title))))
+            (t ; current point is neither a link nor an image
+             ;; NOTE: Should this point ever legitimately be reached?
+             ;; Let's explicitly unset some variables just to be
+             ;; safe...
+             (setq link-location  nil
+                   link-title     nil))))
+         (t ; point is NOT inside transform bound
           (goto-char temp-position)     ; reset point before jump next anchor
-          (setq out-bound t)            ; for break out `while' loop
-          ))
+          (setq out-bound t))))            ; for break out `while' loop
       ;; add the rest until end of the region to be copied
       (if (< (point) transform-end)
           (setq return-content
@@ -113,6 +130,7 @@ so that it can be yanked into an Org  buffer with links working correctly."
 (defun org-w3m-get-anchor-start ()
   "Move cursor to the start of current anchor.  Return point."
   ;; get start position of anchor or current point
+  ;; NOTE: This function seems never to be used. Should it be removed?
   (goto-char (or (previous-single-property-change (point) 'w3m-anchor-sequence)
                  (point))))

@@ -122,26 +140,46 @@ so that it can be yanked into an Org  buffer with links working correctly."
   (goto-char (or (next-single-property-change (point) 'w3m-anchor-sequence)
 		 (point))))

+(defun org-w3m-get-image-end ()
+  "Move cursor to the end of current image.  Return point."
+  ;; get end position of image or point
+  ;; NOTE: Function `org-w3m-get-image-start' was not created because
+  ;;       function `org-w3m-get-anchor-start' is never used.
+  (goto-char (or (next-single-property-change (point) 'w3m-image)
+		 (point))))
+
 (defun org-w3m-get-next-link-start ()
-  "Move cursor to the start of next link.  Return point."
-  (catch 'reach
-    (while (next-single-property-change (point) 'w3m-anchor-sequence)
-      ;; jump to next anchor
-      (goto-char (next-single-property-change (point) 'w3m-anchor-sequence))
-      (when (get-text-property (point) 'w3m-href-anchor)
-        ;; return point when current is valid link
-        (throw 'reach nil))))
-  (point))
+  "Move cursor to the start of next link or image.  Return point."
+  (let (pos start-pos anchor-pos image-pos)
+    (setq pos (setq start-pos (point)))
+    (setq anchor-pos
+      (catch 'reach
+        (while (setq pos (next-single-property-change pos 'w3m-anchor-sequence))
+          (when (get-text-property pos 'w3m-href-anchor)
+            (throw 'reach pos)))))
+    (setq pos start-pos)
+    (setq image-pos
+      (catch 'reach
+        (while (setq pos (next-single-property-change pos 'w3m-image))
+          (when (get-text-property pos 'w3m-image)
+            (throw 'reach pos)))))
+    (goto-char (min (or anchor-pos (point-max)) (or image-pos (point-max))))))

 (defun org-w3m-get-prev-link-start ()
   "Move cursor to the start of previous link.  Return point."
+  ;; NOTE: This function is only called by `org-w3m-no-prev-link-p',
+  ;;       which itself seems never to be used. Should it be removed?
+  ;;
+  ;; WARNING: This function has not been updated to account for
+  ;;      `w3m-image'. See `org-w3m-get-next-link-start'.
   (catch 'reach
-    (while (previous-single-property-change (point) 'w3m-anchor-sequence)
-      ;; jump to previous anchor
-      (goto-char (previous-single-property-change (point) 'w3m-anchor-sequence))
-      (when (get-text-property (point) 'w3m-href-anchor)
-        ;; return point when current is valid link
-        (throw 'reach nil))))
+    (let ((pos (point)))
+      (while (setq pos (previous-single-property-change pos 'w3m-anchor-sequence))
+        (when (get-text-property pos 'w3m-href-anchor)
+          ;; jump to previous anchor
+          (goto-char pos)
+          ;; return point when current is valid link
+          (throw 'reach nil)))))
   (point))

 (defun org-w3m-no-next-link-p ()
@@ -153,6 +191,7 @@ Return t if there is no next link; otherwise, return nil."
 (defun org-w3m-no-prev-link-p ()
   "Whether there is no previous link after the cursor.
 Return t if there is no previous link; otherwise, return nil."
+  ;; NOTE: This function seems never to be used. Should it be removed?
   (save-excursion
     (equal (point) (org-w3m-get-prev-link-start))))