]> code.delx.au - gnu-emacs/blobdiff - lisp/server.el
(server-visit-files): Restore current-buffer by hand,
[gnu-emacs] / lisp / server.el
index e81acbd21ac60fd8b87f24fed3412d5a9f3b1ed2..416cadebd81d84a049c74ff896c67ea19cd96d2f 100644 (file)
@@ -77,6 +77,9 @@
   "*The program to use as the edit server")
 
 (defvar server-visit-hook nil
+  "*List of hooks to call when visiting a file for the Emacs server.")
+
+(defvar server-switch-hook nil
   "*List of hooks to call when switching to a buffer for the Emacs server.")
 
 (defvar server-process nil 
@@ -95,6 +98,11 @@ When a buffer is marked as \"done\", it is removed from this list.")
 ;; Changing major modes should not erase this local.
 (put 'server-buffer-clients 'permanent-local t)
 
+(defvar server-window nil
+  "*The window to use for selecting Emacs server buffers.
+If nil, use the selected window.
+If it is a frame, use the frame's selected window.")
+
 (defvar server-temp-file-regexp "^/tmp/Re\\|/draft$"
   "*Regexp which should match filenames of temporary files
 which are deleted and reused after each edit
@@ -180,37 +188,42 @@ Prefix arg means just kill any existing server communications subprocess."
       (server-visit-files files client)
       ;; CLIENT is now a list (CLIENTNUM BUFFERS...)
       (setq server-clients (cons client server-clients))
-      (switch-to-buffer (nth 1 client))
+      (server-switch-buffer (nth 1 client))
+      (run-hooks 'server-switch-hook)
       (message (substitute-command-keys
                "When done with a buffer, type \\[server-edit].")))))
 
 (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)."
-  (let (client-record)
-    (while files
-      (save-excursion
-       ;; If there is an existing buffer modified or the file is modified,
-       ;; revert it.
-       ;; If there is an existing buffer with deleted file, offer to write it.
-       (let* ((filen (car (car files)))
-              (obuf (get-file-buffer filen)))
-         (if (and obuf (set-buffer obuf))
-             (if (file-exists-p filen)
-                 (if (or (not (verify-visited-file-modtime obuf))
-                         (buffer-modified-p obuf))
-                     (revert-buffer t nil))
-               (if (y-or-n-p
-                    (concat "File no longer exists: "
-                            filen
-                            ", write buffer to file? "))
-                   (write-file filen)))
-           (set-buffer (find-file-noselect filen))
-           (run-hooks 'server-visit-hook)))
-       (goto-line (nth 1 (car files)))
-       (setq server-buffer-clients (cons (car client) server-buffer-clients))
-       (setq client-record (cons (current-buffer) client-record)))
-        (setq files (cdr files)))
+  (let (client-record (obuf (current-buffer)))
+    ;; Restore the current buffer afterward, but not using save-excursion,
+    ;; because we don't want to save point in this buffer
+    ;; if it happens to be one of those specified by the server.
+    (unwind-protect
+       (while files
+         ;; If there is an existing buffer modified or the file is modified,
+         ;; revert it.
+         ;; If there is an existing buffer with deleted file, offer to write it.
+         (let* ((filen (car (car files)))
+                (obuf (get-file-buffer filen)))
+           (if (and obuf (set-buffer obuf))
+               (if (file-exists-p filen)
+                   (if (or (not (verify-visited-file-modtime obuf))
+                           (buffer-modified-p obuf))
+                       (revert-buffer t nil))
+                 (if (y-or-n-p
+                      (concat "File no longer exists: "
+                              filen
+                              ", write buffer to file? "))
+                     (write-file filen)))
+             (set-buffer (find-file-noselect filen))
+             (run-hooks 'server-visit-hook)))
+         (goto-line (nth 1 (car files)))
+         (setq server-buffer-clients (cons (car client) server-buffer-clients))
+         (setq client-record (cons (current-buffer) client-record))
+         (setq files (cdr files)))
+      (set-buffer obuf))
     (nconc client client-record)))
 \f
 (defun server-buffer-done (buffer)
@@ -259,10 +272,12 @@ Then bury it, and return a suggested buffer to select next."
     (if server-buffer-clients
        (progn
          (if (server-temp-file-p buffer)
-             (progn (save-buffer)
-                    (write-region (point-min) (point-max)
-                                  (concat buffer-file-name "~"))
-                    (kill-buffer buffer))
+             ;; For a temp file, save, and do make a non-numeric backup
+             ;; (unless make-backup-files is nil).
+             (let ((version-control nil)
+                   (buffer-backed-up nil))
+               (save-buffer)
+               (kill-buffer buffer))
            (if (and (buffer-modified-p)
                     (y-or-n-p (concat "Save file " buffer-file-name "? ")))
                (save-buffer buffer)))
@@ -274,7 +289,9 @@ If a server buffer is current, it is marked \"done\" and optionally saved.
 When all of a client's buffers are marked as \"done\", the client is notified.
 
 Temporary files such as MH <draft> files are always saved and backed up,
-no questions asked.  The variable `server-temp-file-regexp' controls
+no questions asked.  (The variable `make-backup-files', if nil, still
+inhibits a backup; you can set it locally in a particular buffer to
+prevent a backup for it.)  The variable `server-temp-file-regexp' controls
 which filenames are considered temporary.
 
 If invoked with a prefix argument, or if there is no server process running, 
@@ -290,6 +307,10 @@ starts server process and that is all.  Invoked by \\[server-edit]."
 (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."
+  (cond ((windowp server-window)
+        (select-window server-window))
+       ((framep server-window)
+        (select-window (frame-selected-window server-window))))
   (if next-buffer
       (if (and (bufferp next-buffer)
               (buffer-name next-buffer))