emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Berry, Charles" <ccberry@ucsd.edu>
To: Win Treese <treese@acm.org>
Cc: Eric S Fraga <esflists@gmail.com>,
	"Emacs-orgmode@gnu.org" <Emacs-orgmode@gnu.org>
Subject: Re: open link in source code block
Date: Sat, 12 Jan 2019 00:12:16 +0000	[thread overview]
Message-ID: <00803EED-3898-4FA2-803E-245F45F897FE@ucsd.edu> (raw)
In-Reply-To: <A46F296C-BCAF-4F6A-A30E-3693D732B519@acm.org>



> On Jan 5, 2019, at 7:41 AM, Win Treese <treese@acm.org> wrote:
> 
> 
> 
>> On Jan 5, 2019, at 5:25 AM, Eric S Fraga <esflists@gmail.com> wrote:
>> 
>> On Friday,  4 Jan 2019 at 19:38, Win Treese wrote:
>>> Suppose I have the following Org file contents:
>>> 
>>> * test
>>> 
>>> #+BEGIN_SRC emacs-lisp
>>> (setq yyy 3)
>>> ;; See https://www.gnu.org
>>> #+END_SRC
>>> 
>>> The URL for gnu.org is highlighted as a clickable link. But if I click
>>> it, the source block is evaluated. This seems rather unexpected,
>>> especially since my intention was to open the link.
>> 
>> Probably expected albeit maybe not your preferred behaviour; all about
>> precedence.  In any case, I would suggest that the easy solution would
>> be to move such links out of the src block.  I usually put these as org
>> comments, along the lines of
>> 
>> # See https://www.gnu.org
>> #+BEGIN_SRC emacs-lisp
>> (setq yyy 3)
>> #+END_SRC
>> 
>> My approach to literate programming is to have no (significant) comments
>> in the code segments themselves.  Each code block is small and has all
>> commentary in the text, not the code.
> 
> Thanks for the note, Eric.
> 
> I think the style you describe is good in most cases. I stumbled over this in a
> more complicated situation in which rearranging things doesn’t work.
> 
> This seems like a slightly bigger problem, though: clicking on a highlighted
> link doesn’t just fail to open the link, it executes the source block, which can
> have very unintended side effects.
> 
> I wonder if the issue is an artifact of implementation rather than intentional
> design: org-open-at-mouse sets point to the location of the click and then
> calls org-open-at-point. The documentation at points says that C-c C-o in 
> a source block opens the results of evaluation (fair enough, although the
> docstring does not).
> 
> So the problem seems to be about the mouse click behavior, not the basic
> function of org-open-at-point.

AFAICS, the issue is with org-open-at-point.

If you really want to `fix' this, there is a hook that allows customization of the behavior of org-open-at-point.

This snippet will add a function to that hook that will attempt to follow a link, when you click on a link in a src-block:

#+begin_src emacs-lisp
  (defun org-follow-link-in-src-block-hook ()
    (and
     (eq 'src-block (org-element-type (org-element-context)))
     (let ((link-be (org-in-regexp org-any-link-re)))
       (if link-be
	   (org-open-link-from-string
	    (buffer-substring-no-properties (car link-be)
					    (cdr link-be))))
       link-be)))
  (add-hook 'org-open-at-point-functions 'org-follow-link-in-src-block-hook)
#+end_src

Beware that this might not be what is wanted if you have code that matches `org-any-link-re' and you happen to click on it.  

For example, if you have code that uses double brackets to denote subscripts (like R), this can cause havoc when you try to edit a subscript in the org buffer (rather than in an edit buffer) by clicking on it.  Without this hook function, clicking on a bracket subscript will prompt for evaluation which is annoying, but maybe less so than interpreting the subscript as a link.

You might use a more limited regex or add a query in `org-follow-link-in-src-block-hook' to be sure following a link is really  what you want.

HTH,

Chuck

  reply	other threads:[~2019-01-12  0:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-05  0:38 open link in source code block Win Treese
2019-01-05 10:25 ` Eric S Fraga
2019-01-05 15:41   ` Win Treese
2019-01-12  0:12     ` Berry, Charles [this message]
2019-01-16 23:43       ` Win Treese
2019-01-17 18:38         ` Berry, Charles
2019-01-18  2:05           ` Win Treese

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=00803EED-3898-4FA2-803E-245F45F897FE@ucsd.edu \
    --to=ccberry@ucsd.edu \
    --cc=Emacs-orgmode@gnu.org \
    --cc=esflists@gmail.com \
    --cc=treese@acm.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).