7

I'm writing a debugger front end, and in that over the course of program execution a number of marks get added inside the source code. In other buffers such as a buffer showing execution history, or a buffer containing backtrace or callstack, I'd like to have in those buffers text information that click through to the source text.

Looking at http://www.gnu.org/software/emacs/manual/html_node/elisp/Clickable-Text.html , the process to add clickable text seems a bit cumbersome.

For example just to set a region of text to be clickable I need something like:

(setq link-start (point))
(insert (format "%s" (realgud-loc-marker loc)))
(setq link-end (point))
(add-text-properties
 link-start link-end
 '(mouse-face highlight
      help-echo "mouse-2: go to this location"))
(setq map (make-sparse-keymap))
(define-key map [mouse-2] 'realgud:follow-link)
(define-key map [mouse-1] 'realgud:follow-link)
(define-key map [follow-link] 'mouse-face)
(put-text-property link-start link-end 'keymap map)
(put-text-property link-start link-end 'loc loc)

And then I also need a definition to realgud:follow-link:

(defun realgud:follow-link(event)
  (interactive "e")
  (let* ((pos (posn-point (event-end event)))
     (loc (get-text-property pos 'loc)))
    (if (realgud-loc-p loc)
      (realgud-loc-goto loc))))

All of this seems a bit boilerplate and there's a lot of it. I could write my own routines, for this, but since this seems like a pretty basic think to do: link some text in a buffer to a mark somewhere else, I figure there must already be a package that simplifies the above. Is there?

3 Answers 3

8

Emacs provides buttons to abstract away these steps, I'd suggest looking at the Emacs sources for instances of make-button and insert-button.

2
  • 1
    Thanks! This nicely handles the first block of code, but what about the boilerplate for the event handler? The things I want to link are already markers, or buffers, or filenames. The pattern (defun name(event) (interactive "e") (let* ((pos (posn-point (event-end event))) (loc (get-text-property pos 'some-property))) (call-some-fn loc))) is also pretty much boilerplate.
    – rocky
    Commented Jan 19, 2015 at 16:13
  • 1
    (info "(elisp) Button Types") and maybe a macro should get you there.
    – politza
    Commented Aug 22, 2016 at 1:07
4

If your clickable text can be defined by a regular expression, the button-lock library combines buttons with font-lock, and can define clickable patterns in one step. Example:

(button-lock-set-button "http://google.com" 'browse-url-at-mouse)

1
  • wiki-nav seems close and maybe I'll turn the buffer into some sort of wiki buffer so I can use that kind of markup. But what is sad here, is that I already have emacs position marks which are down to the level of granularity of possibly a column position, and I'd have to turn that back into a file and line number.
    – rocky
    Commented Jan 18, 2015 at 17:24
0

Library linkd.el provides a very simple way to add clickable text to your files. From the source file Commentary:

Linkd-mode is a major mode that automatically recognizes and
processes certain S-expressions, called "links", embedded in plain
text files.  Links may be followed by invoking certain interactive
functions when point is on the link text.  Links may also be
interpreted as marking up the surrounding text.  Different types
of links have different behaviors when followed, and they may have
different interpretations as markup.

With Linkd mode, you can do the following:
* Embed hyperlinks to files, webpages, or documentation into
  any type of text file in any major mode.
* Delimit and name regions of text ("blocks") in these text files.
* Extract and send blocks to other programs for processing.
* Identify and mark locations and concepts in source code.
* Embed active data objects ("datablocks") into text files.
* Convert Lisp source-code listings to LaTeX for publication.
* Define new link behaviors.
4
  • Thanks for the suggesting and fixing my lousy grammar/spelling. Although linkd can be obtained via el-get, my concern with this is that it hasn't been updated since 2009 or Emacs 22 and a lot has happened since then. So I've gone with the button.el approach, although it is still only half (or maybe 2/3rds) of the problem
    – rocky
    Commented Jan 19, 2015 at 7:39
  • If your concern is that it might not work, or it might not work as well, with recent Emacs versions, then don't worry about that - it does. If your concern is that it does not use any features added to Emacs since 2009 then that's another story - no, it does not. It remains "a very simple way" to get the job done. See also Org mode, for a fancier, more complete way.
    – Drew
    Commented Jan 19, 2015 at 16:05
  • I just noticed this question and this linkd.el suggestion. I can get to the linkd.el source code, but the link embedded in the source-code comments which points to an example of linkd usage is a dead link. Can anyone point me to one or more working examples of linkd usage? Thank you in advance.
    – HippoMan
    Commented Mar 6 at 17:34
  • 1
    @HippoMan. Sure; I use it in the Commentaries of many of my libraries. Here's one: bookmark+-doc.el. Scroll down a bit from the top, to (@> "Index").
    – Drew
    Commented Mar 6 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.