(defvar server-program "server" "\
*The program to use as the edit server")

(defvar server-process nil "\
the current server process")

(defvar server-previous-string "")

(defvar server-clients nil "\
List of current server clients.
Each element is (CLIENTID FILES...) where CLIENTID is a string
that can be given to the server process to identify a client.
When a buffer is marked as \"done\", it is removed from this list.")

(defvar server-buffer-clients nil "\
List of clientids for clients requesting editing of current buffer.")

(make-variable-buffer-local (quote server-buffer-clients))

(setq-default server-buffer-clients nil)

(or (assq (quote server-buffer-clients) minor-mode-alist) (setq minor-mode-alist (cons (quote (server-buffer-clients " Server")) minor-mode-alist)))

(defun server-log (string) (byte-code "!qdbco )" [string get-buffer "*server*" newline] 3))

(defun server-sentinel (proc msg) (byte-code "!=!!!=!!" [proc process-status exit server-log message "Server subprocess exited" signal "Server subprocess killed"] 7))

(defun server-start (&optional leave-dead) "\
Allow this Emacs process to be a server for client processes.
This starts a server communications subprocess through which
client \"editors\" can send your editing commands to this Emacs job.
To use the server, set up the program `etc/emacsclient' in the
Emacs distribution as your standard \"editor\".

Prefix arg means just kill any existing server communications subprocess." (interactive "P") (byte-code "\"ȏʏ
@8!)/N8!!
#\"\"!" [server-process nil server-clients buffer leave-dead server-program set-process-sentinel (byte-code "!" [server-process delete-process] 2) ((error (byte-code "" [nil] 1))) (byte-code "!" [delete-file "~/.emacs_server"] 2) ((error (byte-code "" [nil] 1))) 1 server-buffer-done server-log message "Restarting server" start-process "server" server-sentinel set-process-filter server-process-filter process-kill-without-query] 9))

(defun server-process-filter (proc string) (byte-code "!	PGSH=\"=?$!OΉ\"OC!O\"!!SO!O\"vO!
8!!!+" [string server-previous-string client files nil lineno arg server-clients server-log 10 0 string-match "Client: " match-end "" " " 1 "[^ ]+ " match-beginning "\\`\\+[0-9]+\\'" read server-visit-files switch-to-buffer message substitute-command-keys "When done with a buffer, type \\[server-edit]."] 16))

(defun server-visit-files (files client) "\
Finds FILES and returns the list CLIENT with the buffers nconc'd.
FILES is an alist whose elements are (FILENAME LINENUMBER)." (byte-code "	g	@@
!q*	@8!@BpB)	A\")" [client-record files filen obuf t nil server-buffer-clients client get-file-buffer file-exists-p verify-visited-file-modtime buffer-modified-p revert-buffer y-or-n-p "File no longer exists: " ", write buffer to file? " write-file find-file-noselect goto-line 1 nconc] 12))

(defun server-buffer-done (buffer) "\
Mark BUFFER as \"done\" for its client(s).
Buries the buffer, and returns another server buffer
as a suggestion for what to select next." (byte-code "	!=
Y@
>8\"A1ÂOG	@\"\"@\"!
\")A
+" [running server-process next-buffer nil old-clients server-clients client buffer server-buffer-clients process-status run 1 delq send-string format "Close: %s Done
" server-log buffer-name bury-buffer] 10))

(defun mh-draft-p (buffer) "\
Return non-nil if this BUFFER is an mh <draft> file.
Since MH deletes draft *BEFORE* it is edited, the server treats them specially." (byte-code "!\"" [buffer string= buffer-name "draft"] 4))

(defun server-done nil "\
Offer to save current buffer, mark it as \"done\" for clients,
bury it, and return a suggested buffer to select next." (byte-code "p	2! ed
P#!. (
Q!.!!)" [buffer server-buffer-clients buffer-file-name mh-draft-p save-buffer write-region "~" kill-buffer buffer-modified-p y-or-n-p "Save file " "? " server-buffer-done] 9))

(defun server-edit (&optional arg) "\
Switch to next server editing buffer; say \"Done\" for current buffer.
If a server buffer is current, it is marked \"done\" and optionally saved.
MH <draft> files are always saved and backed up, no questions asked.
When all of a client's buffers are marked as \"done\", the client is notified.

If invoked with a prefix argument, or if there is no server process running, 
starts server process and that is all.  Invoked by \\[server-edit]." (interactive "P") (byte-code "ˆ	?	!>! !" [arg server-process nil process-status (signal exit) server-start server-switch-buffer server-done] 5))

(defun server-switch-buffer (next-buffer) "\
Switch to another buffer, preferably one that has a client.
Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it." (byte-code "!
!!!!/	+	@8!/ !" [next-buffer server-clients bufferp buffer-name switch-to-buffer server-switch-buffer server-buffer-done 1 other-buffer] 9))

(global-set-key "#" (quote server-edit))