Integrating Emacs and Hook, with org-mode

I’m testing out org-mode on spacemacs to deal with my organisational needs. I can easily capture url links into my org-mode documents, but emacs (or spacemacs) doesn’t seem to have access to the hook:// url scheme on my mac and I’m not sure how to make this work.

I’m not sure if it’s a hook question or an emac question, to be honest, but I figured anybody who’s looked into it would be lurking around these parts!

Hook works out of the box with several apps. When an app doesn’t work straight out of the box with Hook, if it has AppleScript or JavaScript automation to get /set a few key pieces of information, then we or others can develop integration scripts so that Hook can do its magic with them. That’s summarized here:

Creating Integration Scripts – Hook

If the app doesn’t support AppleScript or JavaScript, we and/or the community investigate whether screen scripting is an option. We reach out to third-party app developers to request they consider adding some automation; automation is easy and benefits everyone. Several developers understand the value of indexing and have added support (e.g., Benny Kjær Nielsen did this for his MailMate app; nvUltra has the right support).

Hook integration scripts are in its Preferences > Scripts pane. There’s an integration server enabling CogSci Apps to make integrations available without users needing to restart the entire app.

We use various editors at CogSci Apps. I haven’t tried emacs / spacemacs with Hook yet. We’ll have a look.

(Aside: when I was a student in the UK [quite some time ago], I used the VED editor (for programming in Pop-11; reading/writing email; reading newsgroups; etc). VED is quite similar to emacs, and I absolutely loved it. VED is written in Pop-11 – a most delightful “classical” AI language. VED is to Pop-11 what emacs was to Lisp. That was on SunOS, Solaris and later Linux. Poplog was ported to Mac OS, but that didn’t survive the transition from PPC to Intel.)

sorry, I didn’t pay close enough attention to who was asking the question! So my answer is quite redundant for you since you’ve been using Hook for a while.

the short of it is that we’ll have a look. As you said, maybe someone on the forum has already looked into it.

No worries - I really should have a look at the scripting options anyway. Also, the link to VED alone was worth it .
Thanks!

1 Like

I’ve never allowed myself to get into emacs (afraid I might have too much fun and never come out of it), but I think the trick would be to derive a file path for the current window / buffer.

(Hook can derive links for text files based on their paths)

The Emacs.app wrapper does implement a basic osascript interface, but it doesn’t return a document value for the front window. You might be able to do something by calling a shell script from inside AppleScript or JS for Automation.

Flying blind here, but I find that in Emacs.app I can use the window name to display the file path of the current buffer using:

(setq frame-title-format '((:eval buffer-file-name)))

and from AppleScript that then enables me to write:

tell application "Emacs"
    name of front window
end tell

to obtain a full file-path.

So empirically, as long as the Emacs.app setup includes the incantation:

(setq frame-title-format '((:eval buffer-file-name)))

I find that I can get Hook to make links if I add a Get Address entry for Emacs.app in Hook > Preferences > Scripts along the lines of:

(Using JavaScript for Automation, which makes url-encoding easier than in AppleScript. See the JXA script below the screenshot)

//JavaScript
(() => {
    'use strict';

    const main = () => {
        const fp = Application('Emacs').windows.at(0).name();
        return doesFileExist(fp) ? (
            `[${fp}](${encodeURI('file://' + fp)})`
        ) : ''
    }

    // doesFileExist :: FilePath -> IO Bool
    const doesFileExist = strPath => {
        const ref = Ref();
        return $.NSFileManager.defaultManager
            .fileExistsAtPathIsDirectory(
                $(strPath)
                .stringByStandardizingPath, ref
            ) && 1 !== ref[0];
    };

    return main();
})();
2 Likes

I echo your concern: I keep getting pulled down configuration rabbit-holes for emacs, and org-mode, and I don’t have ten percent of your hacking chops! I know I’m procrastinating on something really big when the emacs urge takes me …

Anyway, I’ll try out your suggestions, but at first glance it seems to me it’s a (clever) solution to the opposite of my problem. I’ve got a hook:///file … url inside an org-mode document, but I can’t access it because emacs won’t recognize it as a valid url scheme. In your case, I think you’ve answered the problem: how to get a link to an emacs buffer i’ve opened.

Got it – sounds like you need something that parses the URL and launches a sub-process to open it.

Perhaps something like the BrowseUrl described here ?

https://www.emacswiki.org/emacs/BrowseUrl

Yes. However:

  • i don’t know the first thing about e-lisp, hence my request for help
  • in my initial overview of BrowseUrl or simple the URL package, it seems the “url schemes” are fairly limited (and hook, sadly, is not one of them) and more importantly, not “expandable” easily …

Frankly, if nobody’s looked into it, it’s fine. I can always open my org files with some other software … And I wouldn’t want you to fall into the rabbit hole!
Thanks for looking.

a couple of work-arounds for unsupported apps:

First, you can paste links into files, assuming the language supports comments. E.g., if it’s a Markdown file, you can add a link like this <!-- [2019-10-20-Hook-in-three-points](hook://file/cU9GBzBji?p=QkxPRy8yMDE5&n=2019-10-20-Hook-in-three-points) -->, which is a link to a Finder folder which can be used by anyone at CogSci Apps who has a copy of the SVN repository in which we store the source of our blog posts. ( WordPress is not enough :slight_smile: ) .

Second, you can paste unique ID’s (which Hook can generate for you in the Gear menu, but any unique ID will do) in a comment, like this <!-- 21U92-G9K77 -->. Then you can create a Search link with it, again via the Gear menu, Copy as Search Link from Clipboard, which yields hook://search/?q=21U92-G9K77. You can then paste those links in the Hook window, or in in fields/files of other apps.

The latter is described here , and elsewhere on this forum. Search links are hardly discussed on the forum, and they’re a bit nerdy (hence a Pro feature); but they’re actually quite handy for many purposes. You can use them to store various spotlight searches into a compact link. You can also put them in .hook files.

I’ve just started using Hook, and as an org-mode fan, I searched for integration with it and was very happy to find this thread!

Thanks @RobTrew for your script - that helped me enormously. I would like to also be able to link to an org headline in the way this is possible when you store an org-link within Emacs, but I can work on that.

I think I have found a solution for @seishonagon too: you can add custom URL formats to emacs org-mode fairly easily which use the macOS command open to open the hook: URL.

What you need to do is add the following code somewhere in your init.el file (or wherever you customise Emacs in your setup):

(defun my/hook (hook)
  "Create an org-link target string using `hook://` url scheme."
  (shell-command (concat "open \"" hook "\"")))

  (org-add-link-type "hook" 'my/hook)

The function tells Emacs what to do when you click a custom hook:// link. We just wrap the hook URL in double quotes and then use the open command to open it. The last line adds our new link type and tells it to call the function we have just written to handle those links.

There are fancier ways of building these links when allow autocompletion of possibilities and so on, but this works well enough for me.

Once you have restarted Emacs (or just executed that code) you should be able to call (org-insert-link) (however you normally call it), then filter the list to find your new hook link. Paste the URL in and hit return, then add a description when prompted. Now you should be able to click that link and be taken to the file linked by Hook.

I’ve also set up a template.org file in the custom templates folder and set that as the default for notes, so I am now a really happy bunny! Thanks so much for Hook Luc - it’s a really clever way to save each context of your work, and I’m really happy I can now use it with Emacs!

4 Likes

Ha, @bsag, from micro.blog to hook!

Thanks for the tip. I will try … once I reinstall emacs. It’s really too much of a rabbit hole for me, and I was clearly procrastinating :frowning: Although I’m this :pinching_hand: close to reinstalling it again!

:wave:

No problem - I know the rabbit hole well, and understand the reasons for wanting to avoid it :wink:

1 Like