Function: server-eval-at

server-eval-at is a byte-compiled function defined in server.el.gz.

Signature

(server-eval-at SERVER FORM)

Documentation

Contact the Emacs server named SERVER and evaluate FORM there.

Returns the result of the evaluation, or signals an error if it cannot contact the specified server. For example:
  (server-eval-at "server" '(emacs-pid))
returns the process ID of the Emacs instance running "server".

View in manual

Probably introduced at or before Emacs version 24.1.

Source Code

;; Defined in /usr/src/emacs/lisp/server.el.gz
(defun server-eval-at (server form)
  "Contact the Emacs server named SERVER and evaluate FORM there.
Returns the result of the evaluation, or signals an error if it
cannot contact the specified server.  For example:
  (server-eval-at \"server\" \\='(emacs-pid))
returns the process ID of the Emacs instance running \"server\"."
  (let* ((server-dir (if server-use-tcp server-auth-dir server-socket-dir))
         (server-file (expand-file-name server server-dir))
         (coding-system-for-read 'binary)
         (coding-system-for-write 'binary)
         address port secret process)
    (unless (file-exists-p server-file)
      (error "No such server: %s" server))
    (with-temp-buffer
      (when server-use-tcp
	(let ((coding-system-for-read 'no-conversion))
	  (insert-file-contents server-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 (make-network-process
			     :name "eval-at"
			     :buffer (current-buffer)
			     :host address
			     :service (if server-use-tcp port server-file)
			     :family (if server-use-tcp 'ipv4 'local)
			     :noquery t))
	       (error "Unable to contact the server"))
      (if server-use-tcp
	  (process-send-string process (concat "-auth " secret "\n")))
      (process-send-string process
			   (concat "-eval "
				   (server-quote-arg (format "%S" form))
				   "\n"))
      (while (memq (process-status process) '(open run))
	(accept-process-output process 0.01))
      (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 ".
      (let ((answer ""))
	(while (re-search-forward "\n-print\\(-nonl\\)? " nil t)
	  (setq answer
		(concat answer
			(buffer-substring (point)
					  (progn (skip-chars-forward "^\n")
						 (point))))))
	(if (not (equal answer ""))
	    (read (decode-coding-string (server-unquote-arg answer)
					'emacs-internal)))))))