* server.el (server-host, server-port, server-auth-dir): Autoload risky.
[bpt/emacs.git] / lisp / server.el
index f0e88d0..6fbef70 100644 (file)
@@ -110,8 +110,19 @@ If set, the server accepts remote connections; otherwise it is local."
           (string :tag "Name or IP address")
           (const :tag "Local" nil))
   :version "22.1")
+;;;###autoload
 (put 'server-host 'risky-local-variable t)
 
+(defcustom server-port nil
+  "The port number that the server process should listen on."
+  :group 'server
+  :type '(choice
+          (string :tag "Port number")
+          (const :tag "Random" nil))
+  :version "24.1")
+;;;###autoload
+(put 'server-port 'risky-local-variable t)
+
 (defcustom server-auth-dir (locate-user-emacs-file "server/")
   "Directory for server authentication files.
 
@@ -122,6 +133,7 @@ directory residing in a NTFS partition instead."
   :group 'server
   :type 'directory
   :version "22.1")
+;;;###autoload
 (put 'server-auth-dir 'risky-local-variable t)
 
 (defcustom server-raise-frame t
@@ -564,8 +576,8 @@ server or call `M-x server-force-delete' to forcibly disconnect it.")
                       ;; The other args depend on the kind of socket used.
                       (if server-use-tcp
                           (list :family 'ipv4  ;; We're not ready for IPv6 yet
-                                :service t
-                                :host (or server-host "127.0.0.1") ;; See bug#6781
+                                :service (or server-port t)
+                                :host (or server-host 'local)
                                 :plist '(:authenticated nil))
                         (list :family 'local
                               :service server-file
@@ -586,7 +598,7 @@ server or call `M-x server-force-delete' to forcibly disconnect it.")
                (setq buffer-file-coding-system 'no-conversion)
                (insert (format-network-address
                         (process-contact server-process :local))
-                       " " (int-to-string (emacs-pid))
+                       " " (number-to-string (emacs-pid)) ; Kept for compatibility
                        "\n" auth-key)))))))))
 
 ;;;###autoload
@@ -706,9 +718,6 @@ Server mode runs a process that accepts commands from the
     ;; Display *scratch* by default.
     (switch-to-buffer (get-buffer-create "*scratch*") 'norecord)
 
-    ;; Reply with our pid.
-    (server-send-string proc (concat "-emacs-pid "
-                                     (number-to-string (emacs-pid)) "\n"))
     frame))
 
 (defun server-create-window-system-frame (display nowait proc parent-id)
@@ -862,7 +871,7 @@ The following commands are accepted by the client:
   returned by -eval.
 
 `-error DESCRIPTION'
-  Signal an error (but continue processing).
+  Signal an error and delete process PROC.
 
 `-suspend'
   Suspend this terminal, i.e., stop the client process.
@@ -879,6 +888,9 @@ The following commands are accepted by the client:
       (server-log "Authentication failed" proc)
       (server-send-string
        proc (concat "-error " (server-quote-arg "Authentication failed")))
+      ;; Before calling `delete-process', give emacsclient time to
+      ;; receive the error string and shut down on its own.
+      (sit-for 1)
       (delete-process proc)
       ;; We return immediately
       (return-from server-process-filter)))
@@ -889,6 +901,9 @@ The following commands are accepted by the client:
   (condition-case err
       (progn
        (server-add-client proc)
+       ;; Send our pid
+       (server-send-string proc (concat "-emacs-pid "
+                                        (number-to-string (emacs-pid)) "\n"))
        (if (not (string-match "\n" string))
             ;; Save for later any partial line that remains.
             (when (> (length string) 0)
@@ -1129,6 +1144,9 @@ The following commands are accepted by the client:
      proc (concat "-error " (server-quote-arg
                              (error-message-string err))))
     (server-log (error-message-string err) proc)
+    ;; Before calling `delete-process', give emacsclient time to
+    ;; receive the error string and shut down on its own.
+    (sit-for 5)
     (delete-process proc)))
 
 (defun server-goto-line-column (line-col)
@@ -1467,5 +1485,4 @@ only these files will be asked to be saved."
 \f
 (provide 'server)
 
-;; arch-tag: 1f7ecb42-f00a-49f8-906d-61995d84c8d6
 ;;; server.el ends here