Function: server-switch-buffer

server-switch-buffer is a byte-compiled function defined in server.el.gz.

Signature

(server-switch-buffer &optional NEXT-BUFFER KILLED-ONE FILEPOS THIS-FRAME-ONLY)

Documentation

Switch to another buffer, preferably one that has a client.

Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it.

KILLED-ONE is t in a recursive call if we have already killed one temp-file server buffer. This means we should avoid the final
"switch to some other buffer" since we've already effectively
done that.

FILEPOS specifies a new buffer position for NEXT-BUFFER, if we visit NEXT-BUFFER in an existing window. If non-nil, it should be a cons cell (LINENUMBER . COLUMNNUMBER).

This function has :around advice: server-switch-buffer@with-editor-server-window-alist.

Source Code

;; Defined in /usr/src/emacs/lisp/server.el.gz
(defun server-switch-buffer (&optional next-buffer killed-one filepos
                                       this-frame-only)
  "Switch to another buffer, preferably one that has a client.
Arg NEXT-BUFFER is a suggestion; if it is a live buffer, use it.

KILLED-ONE is t in a recursive call if we have already killed one
temp-file server buffer.  This means we should avoid the final
\"switch to some other buffer\" since we've already effectively
done that.

FILEPOS specifies a new buffer position for NEXT-BUFFER, if we
visit NEXT-BUFFER in an existing window.  If non-nil, it should
be a cons cell (LINENUMBER . COLUMNNUMBER)."
  (if (null next-buffer)
      (progn
	(let ((rest server-clients))
	  (while (and rest (not next-buffer))
	    (let ((proc (car rest)))
	      ;; Only look at frameless clients, or those in the selected
	      ;; frame.
	      (when (or (not (process-get proc 'frame))
			(eq (process-get proc 'frame) (selected-frame)))
		(setq next-buffer (car (process-get proc 'buffers))))
	      (setq rest (cdr rest)))))
	(and next-buffer (server-switch-buffer next-buffer killed-one))
	(unless (or next-buffer killed-one (window-dedicated-p))
	  ;; (switch-to-buffer (other-buffer))
	  (message "No server buffers remain to edit")))
    (if (not (buffer-live-p next-buffer))
	;; If NEXT-BUFFER is a dead buffer, remove the server records for it
	;; and try the next surviving server buffer.
	(apply #'server-switch-buffer (server-buffer-done next-buffer))
      ;; OK, we know next-buffer is live, let's display and select it.
      (if (functionp server-window)
	  (funcall server-window next-buffer)
	(let ((win (get-buffer-window next-buffer
                                      (if this-frame-only nil 0))))
	  (if (and win (not server-window))
	      ;; The buffer is already displayed: just reuse the
	      ;; window.  If FILEPOS is non-nil, use it to replace the
	      ;; window's own value of point.
              (progn
                (select-window win)
                (set-buffer next-buffer)
		(when filepos
		  (server-goto-line-column filepos)))
	    ;; Otherwise, let's find an appropriate window.
	    (cond ((window-live-p server-window)
		   (select-window server-window))
		  ((framep server-window)
		   (unless (frame-live-p server-window)
		     (setq server-window (make-frame)))
		   (select-window (frame-selected-window server-window))))
	    (when (window-minibuffer-p)
	      (select-window (next-window nil 'nomini
                                          (if this-frame-only nil 0))))
	    ;; Move to a non-dedicated window, if we have one.
	    (when (window-dedicated-p)
	      (select-window
	       (get-window-with-predicate
		(lambda (w)
		  (and (not (window-dedicated-p w))
		       (equal (frame-terminal (window-frame w))
			      (frame-terminal))))
		'nomini 'visible (selected-window))))
	    (condition-case nil
                ;; If the client specified a new buffer position,
                ;; treat that as an explicit point-move command, and
                ;; override switch-to-buffer-preserve-window-point.
                (let ((switch-to-buffer-preserve-window-point
                       (if filepos
                           nil
                         switch-to-buffer-preserve-window-point)))
                  (switch-to-buffer next-buffer))
	      ;; After all the above, we might still have ended up with
	      ;; a minibuffer/dedicated-window (if there's no other).
	      (error (pop-to-buffer next-buffer)))))))
    (when server-raise-frame
      (select-frame-set-input-focus (window-frame)))))