[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/5] server.el: accept FDs from emacsclient
[PATCH 2/5] server.el: accept FDs from emacsclient
Mon, 6 Jun 2016 21:25:03 -0400
The emacs server passes :ancillary t to make-network-process, and stores
any file descriptors it receives from a client.
lisp/server.el | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 55 insertions(+), 3 deletions(-)
diff --git a/lisp/server.el b/lisp/server.el
index e4cf431..894f8ac 100644
@@ -352,6 +352,9 @@ server-delete-client
(when (and terminal (eq (terminal-live-p terminal) t))
+ ;; Delete associated processes using this client's fds
+ (mapc #'delete-process (process-get proc :pipelines))
;; Delete the client's process.
(if (eq (process-status proc) 'open)
@@ -670,6 +673,7 @@ server-start
:plist '(:authenticated nil))
(list :family 'local
+ :ancillary-data t
:plist '(:authenticated t)))))
(unless server-process (error "Could not start server process"))
(process-put server-process :server-file server-file)
@@ -915,7 +919,7 @@ server-execute-continuation
(process-put proc 'continuation nil)
(if continuation (ignore-errors (funcall continuation)))))
-(cl-defun server-process-filter (proc string)
+(cl-defun server-process-filter (proc string &optional ancillary)
"Process a request from the server to edit some files.
PROC is the server process. STRING consists of a sequence of
commands prefixed by a dash. Some commands have arguments;
@@ -1015,6 +1019,9 @@ server-execute-continuation
Suspend this terminal, i.e., stop the client process.
Sent when the user presses C-z."
(server-log (concat "Received " string) proc)
+ (when ancillary
+ (server-log (format "Received fds %s" ancillary) proc)
+ (process-put proc :fds ancillary))
;; First things first: let's check the authentication
(unless (process-get proc :authenticated)
(if (and (string-match "-auth \\([!-~]+\\)\n?" string)
@@ -1262,6 +1269,50 @@ server-execute-continuation
(error (server-return-error proc err))))
+(defvar server-emacsclient-proc nil
+ "Non-nil if running commands for a client of our server.
+If we are currently evaluating Lisp in response to client commands,
+this variable contains the process for communicating with that
+(defun server-pager-sentinel (proc event)
+ (internal-default-process-sentinel proc event)
+ (when (equal event "finished\n")
+ (let ((emacsclient (process-get proc :emacsclient)))
+ (setf (process-get emacsclient :pipelines)
+ (delq proc (process-get emacsclient :pipelines)))
+ (server-delete-client emacsclient))))
+(defun server-pager ()
+ "Start a process reading from FDs passed in by the current client.
+This function will start a process which will begin reading from the
+FDs passed in by the current client and copying their input to a
+This function should only be run by passing --eval to an emacsclient
+that also has the -l or --pipeline option, like so:
+ echo some data | emacsclient -l --eval '(server-pager)'"
+ ;; we remove two fds from the emacsclient process, and add ourselves
+ ;; in for later deletion when the emacsclient quits
+ (if (null server-emacsclient-proc)
+ (error "Cannot be run out of emacsclient --eval context")
+ (let ((buf (get-buffer "*pager*")))
+ (when buf (kill-buffer buf)))
+ (let* ((infd (pop (process-get server-emacsclient-proc :fds)))
+ (outfd (pop (process-get server-emacsclient-proc :fds)))
+ (buffer (generate-new-buffer "*pager*"))
+ (proc (make-fd-process :name "pager-proc"
+ :buffer buffer
+ :noquery t
+ :sentinel #'server-pager-sentinel
+ :infd infd
+ :outfd outfd
+ :plist (list :emacsclient
+ (push proc (process-get server-emacsclient-proc :pipelines))
+ (pop-to-buffer buffer)
(defun server-execute (proc files nowait commands dontkill frame tty-name)
;; This is run from timers and process-filters, i.e. "asynchronously".
;; But w.r.t the user, this is not really asynchronous since the timer
@@ -1272,7 +1323,8 @@ server-execute
;; including code that needs to wait.
- (let ((buffers (server-visit-files files proc nowait)))
+ (let ((buffers (server-visit-files files proc nowait))
+ (server-emacsclient-proc proc))
(mapc 'funcall (nreverse commands))
;; If we were told only to open a new client, obey
@@ -1294,7 +1346,7 @@ server-execute
;; Client requested nowait; return immediately.
(server-log "Close nowait client" proc)
- ((and (not dontkill) (null buffers))
+ ((and (not dontkill) (null buffers) (null (process-get proc
;; This client is empty; get rid of it immediately.
(server-log "Close empty client" proc)
- Teaching emacsclient to act as a pager, and more, Spencer Baugh, 2016/06/06
- [PATCH 3/5] emacsclient: support passing stdin/out/err to emacs, Spencer Baugh, 2016/06/06
- [PATCH 4/5] server: add pager tapping and show-active, Spencer Baugh, 2016/06/06
- [PATCH 5/5] emacsclient: add extra-quiet mode, Spencer Baugh, 2016/06/06
- [PATCH 1/5] process: add features for direct use of FDs, Spencer Baugh, 2016/06/06
- [PATCH 2/5] server.el: accept FDs from emacsclient,
Spencer Baugh <=
- Re: Teaching emacsclient to act as a pager, and more, Tassilo Horn, 2016/06/08
- Re: Teaching emacsclient to act as a pager, and more, H. Dieter Wilhelm, 2016/06/09
- Re: Teaching emacsclient to act as a pager, and more, Ole JørgenBrønner, 2016/06/27