embark icon indicating copy to clipboard operation
embark copied to clipboard

Add file action to insert as org link

Open dangom opened this issue 3 years ago • 5 comments

I don't know if there is a more elegant way to do this, but I bind this to L in embark-file-map. Would there be interest in a PR?

(defun embark-insert-org-link (file)
    "Insert relative path to FILE.
The insert path is relative to `default-directory'."
    (interactive "FFile: ")
    (insert (concat "[[./"(file-relative-name (substitute-in-file-name file)) "]]")))

dangom avatar Aug 29 '22 19:08 dangom

This might be a good fit for the "secret" `embark-org package.

oantolin avatar Sep 16 '22 13:09 oantolin

Something like that would be nice to have :-) Is it possible to only bind it if the major-mode of the current buffer is org-mode?

unhammer avatar Feb 27 '23 19:02 unhammer

Perfect timing, @unhammer! I added support for extended menu-items yesterday! Those have a :filter property which you can use (among other things) to conditionally enable them only in certain situations. See (info "(elisp)Extended Menu Items") for more information. You can use this to bind @dangom's action only when you are in a minibuffer such that the previously selected buffer was an org mode buffer (I'm guessing you don't really want to use this action in org-mode buffers, but rather in minibuffers open from an org mode buffer, right? I imagine the usage being you are in a org mode buffer, and want to insert a relative file link, so you fire up find-file or maybe locate, and then from the minibuffer call this action):

(define-key embark-file-map "L"
  `(menu-item "Insert Org link to file"
              embark-insert-org-link
              :filter
              ,(lambda (item)
                 (with-minibuffer-selected-window 
                   (when (derived-mode-p 'org-mode)
                     item)))))

oantolin avatar Feb 27 '23 19:02 oantolin

I think this action is useful for any major mode. You can use org links as global links in the style of Hyperbole via org-global-open-at-point.

minad avatar Feb 27 '23 20:02 minad

True, @minad. Maybe embark-org's link target finder should find links in all buffers and use org-global-open-at-point instead of the non-global one.

oantolin avatar Feb 27 '23 20:02 oantolin