emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Max Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Lazy load of org-protocol
Date: Sat, 5 Feb 2022 18:54:37 +0700	[thread overview]
Message-ID: <stlohv$17d8$1@ciao.gmane.io> (raw)


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)' 

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 
		     (ad-get-arg 0)))
	 (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.
		       (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?

             reply	other threads:[~2022-02-05 11:55 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-05 11:54 Max Nikulin [this message]
2022-02-05 18:27 ` Lazy load of org-protocol 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
     [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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='stlohv$17d8$1@ciao.gmane.io' \
    --to=manikulin@gmail.com \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).