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. For example:
  (server-eval-at "server" '(emacs-pid))
returns the process ID of the Emacs instance running "server".

This function signals error if it could not contact the server.

This function signals server-return-invalid-read-syntax if read fails on the result returned by the server. This will occur whenever the result of evaluating FORM is something that cannot be printed readably.

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.  For example:
  (server-eval-at \"server\" \\='(emacs-pid))
returns the process ID of the Emacs instance running \"server\".

This function signals `error' if it could not contact the server.

This function signals `server-return-invalid-read-syntax' if
`read' fails on the result returned by the server.
This will occur whenever the result of evaluating FORM is
something that cannot be printed readably."
  (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 ""))
            (condition-case err
                (read
                 (decode-coding-string (server-unquote-arg answer)
				       'emacs-internal))
              ;; Re-signal with a more specific condition.
              (invalid-read-syntax
               (signal 'server-return-invalid-read-syntax
                       (cdr err)))))))))