help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: copy a link + hyperlink & text


From: John Mastro
Subject: Re: copy a link + hyperlink & text
Date: Fri, 3 Jun 2016 21:56:15 -0700

John Mastro <john.b.mastro@gmail.com> wrote:
> Here's something quick-and-dirty that kinda-sorta does what I believe
> you're going for with w3m.

Here's a slight revision that tries to grab the full link text even if
point isn't on the first character. I have the sense there's a better
way to do it, but this seems to work.

(require 'subr-x)

(defun find-property-bounds (pos prop)
  (save-excursion
    (goto-char pos)
    (let* ((val (get-text-property (point) prop))
           (beg (progn (forward-char -1)
                       (while (eq (get-text-property (point) prop) val)
                         (forward-char -1))
                       (1+ (point))))
           (end (next-single-property-change beg prop)))
      (list beg end))))

(defun w3m-link-at-point ()
  "Return a list of (URL LINK-TEXT) for the link at point."
  (save-excursion
    (when-let ((url (w3m-anchor)))
      (let ((bounds (find-property-bounds (point) 'w3m-href-anchor)))
        (list url (apply #'buffer-substring-no-properties bounds))))))

(defun w3m-copy-org-link ()
  "Copy the link at point to the kill ring in Org link format."
  (interactive)
  (if-let ((link (w3m-link-at-point)))
      (let ((org-link (format "[[%s][%s]]" (car link) (cadr link))))
        (message "%s" org-link)
        (kill-new org-link))
    (user-error "No link at point")))

Hope that helps

        John



reply via email to

[Prev in Thread] Current Thread [Next in Thread]