1

I would like to have an org-protocol that supports line numbers. For example

org-protocol://open-file?file=/foo/ss/subdir/foo.js&line=132

This would open file /foo/ss/subdir/foo.js at line 132.

How do I create an org-protocol that supports opening a file at a particular line?

2
  • You made a statement. What is your question?
    – Tobias
    Aug 9, 2019 at 14:40
  • I added a question at the end Aug 9, 2019 at 15:08

1 Answer 1

3

The following Elisp code enables you to open files at specified lines with emacsclient via org-protocol.

The format of the org-protocol is like you specified it with the addition of the key read-only. You can give that key any value to open the file in read-only-mode.

(defvar org-protocol-open-file-at-line nil
  "A property list used by `org-protocol-server-visit-hook-fun'.")

(defun org-protocol-server-visit-hook-fun ()
  "Jump to line number detected by `org-protocol-open-file'.
Transferred from `org-protocol-open-file' via variable `org-protocol-open-file-at-line'."
  (let ((file (plist-get org-protocol-open-file-at-line :file))
    (line (1- (string-to-number (or (plist-get org-protocol-open-file-at-line :line) "1")))))
    (when (file-equal-p file (buffer-file-name))
      (unless (plist-get org-protocol-open-file-at-line :read-only)
    (read-only-mode -1))
      (goto-char (point-min)) ;; should actually already be there...
      (forward-line line))))

(add-hook 'server-visit-hook #'org-protocol-server-visit-hook-fun)

(defun org-protocol-open-file (pars)
  "Act as org-protocol-protocol-alist function with PARS as args.
Handle links like: org-protocol://open-file?file=/foo/ss/subdir/foo.js&line=132"
  (let ((file (plist-get pars :file)))
    (setq org-protocol-open-file-at-line pars)
    (and (file-exists-p file)
     file)))

(eval-after-load 'org-protocol
  (lambda ()
    (add-to-list 'org-protocol-protocol-alist
         '("open-file"
           :protocol "open-file"
           :function org-protocol-open-file))))

My previous answer was:

See the the page on External Links in the org manual.

There you find the entry:

file:sometextfile::NNN file, jump to line number

The NNNN stands for a number.

Your example would look like follows: file:/foo/ss/subdir/foo.js::132

3
  • 1
    If I understand your answer correctly, it applies to org documents. What I am looking for is a way to handle org-protocol URLs (as in org-protocol.el) so that I can open a particular file+line from an external application. Aug 9, 2019 at 14:51
  • @DamienCassou I added the solution for org-protocol links.
    – Tobias
    Aug 9, 2019 at 17:16
  • Thank you so much for your answer, this is a very nice approach. Apparently, org-protocol uses an advice on server-visit-files to do its job and this function already supports line numbers so I was hoping we could have a better solution. Nevertheless, my idea requires changing org-mode whereas yours doesn't :-). Aug 12, 2019 at 9:20

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.