emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Boruch Baum <boruch_baum@gmx.com>
To: Bastien <bzg@gnu.org>
Cc: 47088@debbugs.gnu.org
Subject: bug#47088: org-w3m: handle w3m-image link information
Date: Thu, 6 May 2021 18:05:15 -0400	[thread overview]
Message-ID: <20210506220515.fr7ebcvvtb3jj5yp@E15-2016.optimum.net> (raw)
In-Reply-To: <87zgx9ioi7.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1348 bytes --]

On 2021-05-05 10:16, Bastien wrote:
> Thanks, it looks good.
>
> Can you try updating the patch against Org's upstream repository at
> https://code.orgmode.org/bzg/org-mode/?
>
> Note that the file is lisp/ol-w3m.el there.

1] Attached.

2] Here's a commit message in the Changelog style:

   2021-05-06  Boruch Baum  <boruch_baum@gmx.com>

	* ol-w3m.el (org-w3m-copy-for-org-mode)
	(org-w3m-get-next-link-start, org-w3m-get-prev-link-start):
	Account for w3m-img links.
	(org-w3m-get-anchor-start, org-w3m-get-prev-link-start)
	(org-w3m-no-prev-link-p): Unused function notes.
	(org-w3m-get-image-end): New function, for w3m-img links.

3] As mentioned in the patch, and in the commit message, there seem to
   be several functioned unused in the code base that could be
   candidates for removal...

        org-w3m-get-anchor-start
        org-w3m-no-prev-link-p
        org-w3m-get-prev-link-start

4] Additionally, in performing the merge I came across function
   org-string-nw-p, which seems to be an unnecessary duplicate of the
   emacs core function string-blank-p. It may be that historically you
   once needed your own home-made functions/macros, but now that emacs
   core has them, you may want to consider a refactor.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0

[-- Attachment #2: ol-w3m.patch --]
[-- Type: text/x-diff, Size: 7208 bytes --]

diff --git a/ol-w3m.el b/ol-w3m.el
index ebb11ce..2738e01 100644
--- a/ol-w3m.el
+++ b/ol-w3m.el
@@ -82,26 +82,41 @@ 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.
-              (when (> (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
-			    (if (org-string-nw-p link-location)
-				(org-link-make-string link-location link-title)
-			      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
+                       (if (org-string-nw-p link-location)
+                         (org-link-make-string link-location link-title)
+                        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
+                       (if (org-string-nw-p link-location)
+                         (org-link-make-string link-location link-title)
+                        link-title))))
+            (t nil))); current point is neither a link nor an image
+         (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
       (when (< (point) transform-end)
         (setq return-content
@@ -114,6 +129,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))))

@@ -123,26 +139,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 ()
@@ -154,6 +190,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))))


  reply	other threads:[~2021-05-06 22:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210312062614.usandeuwplmn7fu3@E15-2016.optimum.net>
2021-05-05  8:16 ` bug#47088: org-w3m: handle w3m-image link information Bastien
2021-05-06 22:05   ` Boruch Baum [this message]
2021-05-06 23:56     ` Nick Savage
2021-05-08  7:44       ` Bastien
2021-05-08 11:35         ` Nicholas Savage
2021-05-09  6:50     ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210506220515.fr7ebcvvtb3jj5yp@E15-2016.optimum.net \
    --to=boruch_baum@gmx.com \
    --cc=47088@debbugs.gnu.org \
    --cc=bzg@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).