Copy and Copy JSON: essential Listener hacks.

Something I’ve really wanted in the Listener for a while is the ability to right-click and copy a string to the clipboard. I’ve been working around that by having my own “>clipboard” word that places a single string argument on the clipboard — extremely handy for debugging long HTTP responses which get truncated at the right-side of the window.

: >clipboard ( str  )
clipboard get set-clipboard-contents ;

This is from Factor’s useful ui.clipboards vocab, which is the most concise and least ugly way I’ve seen to handle clipboards in a cross-platform GUI. clipboard is a global reference to the current clipboard object whose methods are overridden to handle the dirty details of the current platform’s clipboard.

Along came this useful post about adding an ‘open url’ command for URLs in the listener, and I saw a path to my goal: ui.operations and the define-operation word.

Combined with unparse to get a string representation of an object, this adds a “Copy” command to the Listener’s context menu for all objects:

: copy ( obj  )
unparse >clipboard ;

[ drop t ] \ copy H{ } define-operation

In this brave new Web 2.0 world, it’s dangerous to go alone! Take this JSON with you*:

: copy-json ( obj  )
>json >clipboard ;

[ string? not ] \ copy-json H{ } define-operation

Load up the vocab (or paste those definitions in the Listener), and voilá — the GUI is instantly extended with new context menu options:

As usual, this is all available on GitHub

*(Sorry for the Zelda reference, just spent a happy weekend playing a big chunk of the awesome LD20 48-hour game compo entries)

This entry was posted in code, factor. Bookmark the permalink.

3 Responses to Copy and Copy JSON: essential Listener hacks.

  1. Losgann says:

    This is a really useful tool. It works great in Windows 7. However, in Linux, although “Copy” and “Copy Json” show up in the context menu, when I select either one I get the error message:

    Generic word set-clipboard-contents does not define a method for the x-clipboard class. Dispatching on object: T{ x-clipboard f 460 “” }

    I’m using version 0.94 of Factor. Any ideas?

  2. Fred says:

    Looks like a bug in Factor’s X11 UI backend. Try running this in the listener:


    USE: ui.clipboards
    "TEST STRING" f clipboard get copy-clipboard

    See if that puts “TEST STRING” on the X11 clipboard. Looks like set-clipboard-contents isn’t implemented in ui.backends.x11.

  3. Losgann says:

    Trying your test results in this error message:

    Generic word handle>> does not define a method for the POSTPONE: f class.
    Dispatching on object: f

    I agree that it looks like set-clipboard-contents isn’t implemented in ui.backend.X11. In fact I searched for set-clipboard-contents in ui.backend and got hits for only windows.factor and cocoa.factor. At least now I know where to look to get this working. I’m somewhat of a newb, but I’m fearless and tenacious!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>