all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Lazy load of org-protocol
@ 2022-02-05 11:54 Max Nikulin
  2022-02-05 18:27 ` Jim Porter
  0 siblings, 1 reply; 15+ messages in thread
From: Max Nikulin @ 2022-02-05 11:54 UTC (permalink / raw)
  To: emacs-orgmode

Hi.

I would prefer to avoid
    (require 'org-protocol)
in emacs init file and to postpone loading till invocation of 
emacsclient with org-protocol URI.

The problem is a hack in org-protocol. URIs are actually treated as 
(relative) file names and magic is achieved in an advice for 
`server-visit-files' function. So the advice must be installed in advance.

My first idea was to avoid such magic and to create autoload function 
org-protocol-from-argv with body similar to that advice. If it were 
possible to get arguments from `command-line-args-left' then invocation 
would look like

    emacsclient --eval '(org-protocol-from-argv)' 
'org-protocol:/store-link?url=u1&title=t1'

Unfortunately it is against design of emacs server protocol. If --eval 
option is given than everything other is considered as independent 
expressions. At the lower level there is no way to transfer `argv` as a 
list from client to server process.

It seems, it works if I add another advice to init.el that loads 
org-protocol on demand:

(defadvice server-visit-files (before org-protocol-lazy-load activate)
   (and (not (featurep 'org-protocol))
        (memq nil
	     (mapcar (lambda (loc)
		       ;; loc: (file-name . (line . column))
		       (not (string-match-p "\\(?:^\\|[/\\\\]\\)org-protocol:" (car 
loc))))
		     (ad-get-arg 0)))
        (progn
	 (require 'org-protocol)
	 ;; copy of org-protocol-detect-protocol-server advice,
          ;; move to a dedicated function
	 (let ((flist (if org-protocol-reverse-list-of-files
			  (reverse  (ad-get-arg 0))
			(ad-get-arg 0)))
	       (client (ad-get-arg 1)))
	   (catch 'greedy
	     (dolist (var flist)
	       ;; `\' to `/' on windows.  FIXME: could this be done any better?
	       (let ((fname  (expand-file-name (car var))))
		 (setq fname (org-protocol-check-filename-for-protocol
			      fname (member var flist)  client))
		 (if (eq fname t) ;; greedy? We need the t return value.
		     (progn
		       (ad-set-arg 0 nil)
		       (throw 'greedy t))
		   (if (stringp fname) ;; probably filename
		       (setcar var fname)
		     (ad-set-arg 0 (delq var (ad-get-arg 0))))))))))))

I hope, copy of original advice may be avoided by moving its body to a 
separate function in org-protocol.

Do you have have better ideas how to avoid eager loading of org-protocol 
from init file?



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-03-20 10:43 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.61.1644253327.32758.emacs-orgmode@gnu.org>
2022-02-08 19:02 ` [PATCH] lisp/org-capture.el: Add hook & hook options to org-capture (Valentin Herrmann) No Wayman
2022-02-09  4:10   ` Ihor Radchenko
2022-02-09  7:11     ` No Wayman
2022-03-20 10:43       ` Ihor Radchenko
2022-02-10 19:32   ` Greg Minshall
2022-02-05 11:54 Lazy load of org-protocol Max Nikulin
2022-02-05 18:27 ` Jim Porter
2022-02-06 16:42   ` Max Nikulin
2022-02-06 19:40     ` Jim Porter
2022-02-07 14:57       ` Max Nikulin
2022-02-07 19:06         ` Jim Porter
2022-02-09 16:46           ` Max Nikulin
2022-02-09 19:22             ` Jim Porter
2022-02-10 14:44               ` Max Nikulin
2022-02-08 10:44         ` Emacs-orgmode Digest, Vol 192, Issue 8 Tianshu Wang

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.