emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Error handling in org-make-link-string
@ 2018-04-14  2:40 Bob Newell
  2018-04-14 13:17 ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Bob Newell @ 2018-04-14  2:40 UTC (permalink / raw)
  To: emacs-orgmode

Aloha,

I've got a subsystem for capturing snippets of information (I mentioned
it here, scraps.el, quite some while back). Some information is captured
from w3m or eww browser buffers.

To do this I use org-w3m-copy-for-org-mode or
org-eww-copy-for-org-mode. This is nice in that links are converted to
org-mode links, using org-make-link-string.

The problem? When org-make-link-string encounters an empty link (it
doesn't happen often but it does happen), it uses the 'error' function
to say that the link is empty. This means that the entire call to
org-xxx-copy-for-org-mode is aborted, and consequently nothing is
captured.

Should this be the desired behavior?

It doesn't quite work for me, but I'm not sure about other uses, side
effects, etc. Someone more familiar with org-mode code would have to
decide. My workaround, since I don't want to modify org.el directly and
have to maintain the mod, is this fairly primitive idea:

    (defun rjn-around-omls (orig-fun link &rest args)
      (if (org-string-nw-p link)
	  (apply orig-fun link args)
	  nil))
    (advice-add 'org-make-link-string :around #'rjn-around-omls)

This works fine for me in that an empty link is simply returned as nil
and ignored. (I suppose I could add an error message.)

Any thoughts on (a) is the 'error' return the right thing, and (b) this
workaround?

Mahalo,

-- 
Bob Newell
Honolulu, Hawai`i
* Via Gnus/BBDB/Org/Emacs/Linux *

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Error handling in org-make-link-string
  2018-04-14  2:40 Error handling in org-make-link-string Bob Newell
@ 2018-04-14 13:17 ` Nicolas Goaziou
  2018-04-14 16:54   ` Bob Newell
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Goaziou @ 2018-04-14 13:17 UTC (permalink / raw)
  To: Bob Newell; +Cc: emacs-orgmode

Hello,

Bob Newell <bobnewell@bobnewell.net> writes:

> The problem? When org-make-link-string encounters an empty link (it
> doesn't happen often but it does happen), it uses the 'error' function
> to say that the link is empty. This means that the entire call to
> org-xxx-copy-for-org-mode is aborted, and consequently nothing is
> captured.
>
> Should this be the desired behavior?

Your question is twofold. 

OTOH, it seems sane to expect `org-make-link-string' to throw an error
if you try to apply it on garbage. OTOH, I agree it is not desirable to
throw away all captured information because of a bad link.

I think the problem lies in the logic of `org-eww-copy-for-org-mode' and
`org-w3m-copy-for-org-mode', which should handle better errors from
`org-make-link-string'.

For example,

   (if (stringp link-location)
       ;; hint: link-location is different for form-elements.
       (org-make-link-string link-location link-title)
    link-title)

could be replaced with

  (if (org-string-nw-p link-location)
      ...)

or even

  (or (ignore-errors (org-make-link-string ...))
      link-title)

WDYT?

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Error handling in org-make-link-string
  2018-04-14 13:17 ` Nicolas Goaziou
@ 2018-04-14 16:54   ` Bob Newell
  2018-04-14 17:32     ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Bob Newell @ 2018-04-14 16:54 UTC (permalink / raw)
  To: emacs-orgmode

Aloha,

Either of your suggested solutions would work, of course, and limit
effects to org-xxx-copy-for-org-mode. I didn't go that way because I
didn't want to have to continually modify the core product on my own
:)

The idea of

(if (org-string-nw-p link-location

etc. may be best because we can have a guaranteed nil on a bad link,
rather than ignore-errors which (I think?) may have a different
return. I didn't put an error message in my 'advice' workaround but it
would be a good idea.

Regards,


Bob


On Sat, Apr 14, 2018 at 3:17 AM, Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:
> Hello,
>
> Bob Newell <bobnewell@bobnewell.net> writes:
>
>> The problem? When org-make-link-string encounters an empty link (it
>> doesn't happen often but it does happen), it uses the 'error' function
>> to say that the link is empty. This means that the entire call to
>> org-xxx-copy-for-org-mode is aborted, and consequently nothing is
>> captured.
>>
>> Should this be the desired behavior?
>
> Your question is twofold.
>
> OTOH, it seems sane to expect `org-make-link-string' to throw an error
> if you try to apply it on garbage. OTOH, I agree it is not desirable to
> throw away all captured information because of a bad link.
>
> I think the problem lies in the logic of `org-eww-copy-for-org-mode' and
> `org-w3m-copy-for-org-mode', which should handle better errors from
> `org-make-link-string'.
>
> For example,
>
>    (if (stringp link-location)
>        ;; hint: link-location is different for form-elements.
>        (org-make-link-string link-location link-title)
>     link-title)
>
> could be replaced with
>
>   (if (org-string-nw-p link-location)
>       ...)
>
> or even
>
>   (or (ignore-errors (org-make-link-string ...))
>       link-title)
>
> WDYT?
>
> Regards,
>
> --
> Nicolas Goaziou



-- 
Bob Newell
Honolulu, Hawai`i

Sent via Linux Mint 17.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Error handling in org-make-link-string
  2018-04-14 16:54   ` Bob Newell
@ 2018-04-14 17:32     ` Nicolas Goaziou
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2018-04-14 17:32 UTC (permalink / raw)
  To: Bob Newell; +Cc: emacs-orgmode

Hello,

Bob Newell <bobnewell@bobnewell.net> writes:

> Aloha,
>
> Either of your suggested solutions would work, of course, and limit
> effects to org-xxx-copy-for-org-mode. I didn't go that way because I
> didn't want to have to continually modify the core product on my own
> :)
>
> The idea of
>
> (if (org-string-nw-p link-location
>
> etc. may be best because we can have a guaranteed nil on a bad link,
> rather than ignore-errors which (I think?) may have a different
> return. I didn't put an error message in my 'advice' workaround but it
> would be a good idea.

Done. Thank you.

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-04-14 17:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-14  2:40 Error handling in org-make-link-string Bob Newell
2018-04-14 13:17 ` Nicolas Goaziou
2018-04-14 16:54   ` Bob Newell
2018-04-14 17:32     ` Nicolas Goaziou

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).