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