From e793a9404da8d8cb0d318f5ba87998e2be6ecb50 Mon Sep 17 00:00:00 2001 From: Lars Magne Ingebrigtsen Date: Mon, 2 May 2011 04:06:53 +0200 Subject: [PATCH] Implement and document `server-eval-at'. --- doc/emacs/ChangeLog | 4 ++++ doc/emacs/misc.texi | 9 +++++++++ lisp/ChangeLog | 4 ++++ lisp/server.el | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/doc/emacs/ChangeLog b/doc/emacs/ChangeLog index 92cd765b492..efe031d465b 100644 --- a/doc/emacs/ChangeLog +++ b/doc/emacs/ChangeLog @@ -1,3 +1,7 @@ +2011-05-02 Lars Magne Ingebrigtsen + + * misc.texi (Emacs Server): Document `server-eval-at'. + 2011-04-24 Chong Yidong * maintaining.texi (List Tags): Document next-file. Suggested by diff --git a/doc/emacs/misc.texi b/doc/emacs/misc.texi index 1299895a06e..06267851d4c 100644 --- a/doc/emacs/misc.texi +++ b/doc/emacs/misc.texi @@ -1495,6 +1495,15 @@ server-name @key{RET} foo @key{RET}} sets the server name to @samp{foo}. The @code{emacsclient} program can specify a server by name, using the @samp{-s} option (@pxref{emacsclient Options}). +@findex server-eval-at + If you have defined a server by a unique server name, you can +connect to this server from other Emacs instances and evaluate forms +on it by using the @code{server-eval-at} function. + +@code{(server-eval-at "foo" '(+ 1 2))} gives the result @code{3}, if +there's a server with that name that is listening. If not, an error +will be signaled. + @menu * Invoking emacsclient:: Connecting to the Emacs server. * emacsclient Options:: Emacs client startup options. diff --git a/lisp/ChangeLog b/lisp/ChangeLog index a111f30f6d5..f303f5d1e32 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,7 @@ +2011-05-02 Lars Magne Ingebrigtsen + + * server.el (server-eval-at): New function. + 2011-05-01 Lars Magne Ingebrigtsen * net/network-stream.el (open-network-stream): Take a :nowait diff --git a/lisp/server.el b/lisp/server.el index ce14f133f0a..ab7dd409736 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -1484,6 +1484,41 @@ only these files will be asked to be saved." ;; continue standard unloading nil) +(defun server-eval-at (server form) + "Eval FORM on Emacs Server SERVER." + (let ((auth-file (expand-file-name server server-auth-dir)) + ;;(coding-system-for-read 'binary) + ;;(coding-system-for-write 'binary) + address port secret process) + (unless (file-exists-p auth-file) + (error "No such server definition: %s" auth-file)) + (with-temp-buffer + (insert-file-contents auth-file) + (unless (looking-at "\\([0-9.]+\\):\\([0-9]+\\)") + (error "Invalid auth file")) + (setq address (match-string 1) + port (string-to-number (match-string 2))) + (forward-line 1) + (setq secret (buffer-substring (point) (line-end-position))) + (erase-buffer) + (unless (setq process (open-network-stream "eval-at" (current-buffer) + address port)) + (error "Unable to contact the server")) + (set-process-query-on-exit-flag process nil) + (process-send-string + process + (concat "-auth " secret " -eval " + (replace-regexp-in-string + " " "&_" (format "%S" form)) + "\n")) + (while (memq (process-status process) '(open run)) + (accept-process-output process 0 10)) + (goto-char (point-min)) + ;; If the result is nil, there's nothing in the buffer. If the + ;; result is non-nil, it's after "-print ". + (and (search-forward "\n-print" nil t) + (read (current-buffer)))))) + (provide 'server) -- 2.39.2