I'd like to change the way org-links behave on clicking only when it's a text file etc.

i.e. I'd like to have org-links opens up a new frame with the buffer of the link in it only when it's the link of a .txt, .el, or .png file. I already found a snippet below that basically does this, but I can't figure out how to assign this command to the left click so that it runs the command only when it's on org-links. How can I do that?

(defun zin/org-open-other-frame ()
  "Jump to bookmark in another frame. See `bookmark-jump' for more."
  (let ((org-link-frame-setup (acons 'file 'find-file-other-frame org-link-frame-setup)))

2 Answers 2

(require 'org)

(defun my-find-file-fn (file)
"If the filename extension of FILE ends in `.txt' or `.el' or `.png', then use
`find-file-other-frame'; otherwise, use `find-file-other-window'.  The three file
extensions txt/el/png are hard-coded into the let-bound variable `regex'."
  (let* (
      ;; regexp-matchp-fn from:  https://github.com/kentaro/auto-save-buffers-enhanced
      ;; regexp-matchp-fn modified by @sds:  http://stackoverflow.com/a/20343715/2112489
        (lambda (regexps string)
          (and string
             (catch 'matched
               (let ((inhibit-changing-match-data t))
                 (dolist (regexp regexps)
                   (when (string-match regexp string)
                     (throw 'matched t))))))))
      (ext (file-name-extension file))
      (regex '("txt" "el" "png")))
    (if (funcall regexp-matchp-fn regex ext)
      (find-file-other-frame file)
      (find-file-other-window file))))

(setq org-link-frame-setup '(
  (vm . vm-visit-folder-other-frame)
  (vm-imap . vm-visit-imap-folder-other-frame)
  (gnus . org-gnus-no-new-news)
  (file . my-find-file-fn)
  (wl . wl-other-frame)))

Try customizing either display-buffer-alist (for an Emacs-wide setting) or org-file-apps (for an Org-mode-only setting).

Assuming you want an Org-mode-only setting, try:

(defun ph/find-file-new-frame (filename)
  (select-frame (make-frame))
  (find-file filename))

(add-to-list 'org-file-apps
  '("\\.\\(?:txt\\|el\\|png\\)\\'" . (ph/find-file-new-frame file)))

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.