Subject: bug#15189: 24.3.50; display-buffer does not work well with custom frames.

From: Keith David Bershatsky <[email-masked]>
Date: Mon, 26 Aug 2013 13:15:09 -0700

Thank you for the clarification.  My goal is to cover the two common types of situations:

     * File is opened -- creating a new one, or opening one that already exists.

     * Buffer is created without opening a specific file.

It appears that display-buffer-alist ONLY works for some of the no-file buffers, like *Help*.  Files that are opened get ignored by the display-buffer-alist.

Using your suggestion of find-file-noselect works well with the initial example, and then just adding (switch-to-buffer (get-file-buffer buffer-filename)) to the tail end of the lawlist-display-buffer-function.

So, correct please if I wrong, it appears that a different solution is needed for each of the two scenarios.

Here is the code that works for display-buffer-alist, but not for actual files being opened.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar system-nofile-regexp nil
  "Regexps matching `buffer-filename` for frame name `SYSTEM`.")
(setq system-nofile-regexp '("\\(\\*Metahelp\\*\\|\\*Help\\*\\)"))

(defvar main-nofile-regexp nil
  "Regexps matching `buffer-filename` for frame name `MAIN`.")
(setq main-nofile-regexp '("\\.pdf"))

(defvar org-nofile-regexp nil
  "Regexps matching `buffer-filename` for frame name `ORG`.")
(setq org-nofile-regexp '("\\*Org Agenda\\*"))

(setq display-buffer-alist `((lawlist-p . (nofile-display-buffer-pop-up-frame))))

(defun lawlist-p (buffer action)
  (declare (ignore action))
  (let ((buffer (get-buffer buffer)))
  (or
    (lawlist-regexps-match-p org-nofile-regexp (buffer-name buffer))
    (lawlist-regexps-match-p main-nofile-regexp (buffer-name buffer))
    (lawlist-regexps-match-p system-nofile-regexp (buffer-name buffer)) )))

(defun nofile-display-buffer-pop-up-frame (buffer alist)
    (when (lawlist-regexps-match-p org-nofile-regexp (buffer-name buffer))
      (if (frame-exists "ORG")
          (switch-to-frame "ORG")
        ;; If unnamed frame exists, then take control of it.
        (catch 'break (dolist (frame (frame-list))
          (if (and
              (not (equal "MAIN" (frame-parameter frame 'name)))
              (not (equal "SYSTEM" (frame-parameter frame 'name)))
              (not (equal "ORG" (frame-parameter frame 'name)))
              (not (equal "WANDERLUST" (frame-parameter frame 'name)))
              (not (equal "MISCELLANEOUS" (frame-parameter frame 'name))) )
            (throw 'break (progn
              (switch-to-frame (frame-parameter frame 'name))
              (set-frame-name "ORG")
              (toggle-frame-maximized))))))
        ;; If dolist found no unnamed frame, then create / name it.
        (if (not (frame-exists "ORG"))
          (progn
            (make-frame)
            (set-frame-name "ORG")
            (toggle-frame-maximized))) ))
    (when (lawlist-regexps-match-p main-nofile-regexp (buffer-name buffer))
      (if (frame-exists "MAIN")
          (switch-to-frame "MAIN")
        ;; If unnamed frame exists, then take control of it.
        (catch 'break (dolist (frame (frame-list))
          (if (and
              (not (equal "MAIN" (frame-parameter frame 'name)))
              (not (equal "SYSTEM" (frame-parameter frame 'name)))
              (not (equal "ORG" (frame-parameter frame 'name)))
              (not (equal "WANDERLUST" (frame-parameter frame 'name)))
              (not (equal "MISCELLANEOUS" (frame-parameter frame 'name))) )
            (throw 'break (progn
              (switch-to-frame (frame-parameter frame 'name))
              (set-frame-name "MAIN")
              (toggle-frame-maximized))))))
        ;; If dolist found no unnamed frame, then create / name it.
        (if (not (frame-exists "MAIN"))
          (progn
            (make-frame)
            (set-frame-name "MAIN")
            (toggle-frame-maximized))) ))
    (when (lawlist-regexps-match-p system-nofile-regexp (buffer-name buffer))
      (if (frame-exists "SYSTEM")
          (switch-to-frame "SYSTEM")
        ;; If unnamed frame exists, then take control of it.
        (catch 'break (dolist (frame (frame-list))
          (if (and
              (not (equal "MAIN" (frame-parameter frame 'name)))
              (not (equal "SYSTEM" (frame-parameter frame 'name)))
              (not (equal "ORG" (frame-parameter frame 'name)))
              (not (equal "WANDERLUST" (frame-parameter frame 'name)))
              (not (equal "MISCELLANEOUS" (frame-parameter frame 'name))) )
            (throw 'break (progn
              (switch-to-frame (frame-parameter frame 'name))
              (set-frame-name "SYSTEM")
              (toggle-frame-maximized))))))
        ;; If dolist found no unnamed frame, then create / name it.
        (if (not (frame-exists "SYSTEM"))
          (progn
            (make-frame)
            (set-frame-name "SYSTEM")
            (toggle-frame-maximized))) ))
    (when (and (not (lawlist-regexps-match-p org-nofile-regexp (buffer-name buffer)))
            (not (lawlist-regexps-match-p main-nofile-regexp (buffer-name buffer)))
            (not (lawlist-regexps-match-p system-nofile-regexp (buffer-name buffer))) )
      (if (frame-exists "MISCELLAENOUS")
          (switch-to-frame "MISCELLAENOUS")
        ;; If unnamed frame exists, then take control of it.
        (catch 'break (dolist (frame (frame-list))
          (if (and
              (not (equal "MAIN" (frame-parameter frame 'name)))
              (not (equal "SYSTEM" (frame-parameter frame 'name)))
              (not (equal "ORG" (frame-parameter frame 'name)))
              (not (equal "WANDERLUST" (frame-parameter frame 'name)))
              (not (equal "MISCELLANEOUS" (frame-parameter frame 'name))) )
            (throw 'break (progn
              (switch-to-frame (frame-parameter frame 'name))
              (set-frame-name "MISCELLAENEOUS")
              (toggle-frame-maximized))))))
        ;; If dolist found no unnamed frame, then create / name it.
        (if (not (frame-exists "MISCELLAENEOUS"))
          (progn
            (make-frame)
            (set-frame-name "MISCELLAENEOUS")
            (toggle-frame-maximized)))))
    (if (and (featurep 'init-frames) frame-bufs-mode)
      (frame-bufs-add-buffer buffer (selected-frame))) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; GENERIC FRAME UTILITIES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun frame-exists (frame-name)
  (not (eq nil (get-frame frame-name))))

(defun get-frame-name (&optional frame)
  "Return the string that names FRAME (a frame).  Default is selected frame."
  (unless frame (setq frame (selected-frame)))
  (if (framep frame)
      (cdr (assq 'name (frame-parameters frame)))
    (error "Function `get-frame-name': Argument not a frame: `%s'" frame)))

(defun get-frame (frame)
  "Return a frame, if any, named FRAME (a frame or a string).
  If none, return nil.
  If FRAME is a frame, it is returned."
  (cond ((framep frame) frame)
        ((stringp frame)
         (catch 'get-a-frame-found
           (dolist (fr (frame-list))
             (when (string= frame (get-frame-name fr))
               (throw 'get-a-frame-found fr)))
           nil))
        (t
         (error
          "Function `get-frame-name': Arg neither a string nor a frame: `%s'"
          frame))))

(defun switch-to-frame (frame-name)
  (let ((frames (frame-list)))
    (catch 'break
      (while frames
        (let ((frame (car frames)))
          (if (equal (frame-parameter frame 'name) frame-name)
              (throw 'break (select-frame-set-input-focus frame))
            (setq frames (cdr frames))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

At Mon, 26 Aug 2013 18:34:33 +0200,
martin rudalics wrote:
> 
> Please keep [email-masked] CCed so others can read your mails
> too.
> 
>  > Yes, that works now if we also add the following line to the end of
>  > lawlist-display-buffer-function: (switch-to-buffer (get-file-buffer
>  > buffer-filename))
> 
> I'm still not quite sure whether I understand what you want to do.
> An idiom like
> 
>      (display-buffer
>        (find-file buffer-filename))
> 
> is practically always wrong because it usually will (1) display the
> buffer via `find-file' which calls `switch-to-buffer' which bypasses
> `display-buffer' by calling `set-window-buffer' directly and (2) display
> the buffer a second time which might result in reusing the window used
> in (1) but may also use another window according to your buffer display
> settings.  In any case, a doc-string like
> 
>    "Locate or create a specific frame, and then open the file."
> 
> is inappropriate because the function nowhere does what you say here.
> 
>  > I expected display-buffer to execute the display-buffer-function first
>  > in time (e.g., before executing find-file), but that is not the case.
> 
> I'm afraid you're confusing things here.  `display-buffer' does execute
> `display-buffer-function' first but its arguments get evaluated before
> its body.  This is something you certainly want too because how would
> `display-buffer' otherwise know the buffer it is supposed to display?
> 
>  > If the purpose of display-buffer is not to execute the
>  > display-buffer-function first in time (e.g., before executing
>  > find-file), then I made an erroneous assumption regarding the reason
>  > for using display-buffer.
> 
> I suppose you made the erroneous assumption that `find-file' does not
> display the file's buffer.  That's why I suggested to use
> `find-file-noselect' instead.  Anyway, using `display-buffer-function'
> is deprecated since Emacs 24.3 and was hardly used in previous versions.
> What you probably want to do is to customize `display-buffer-alist'
> instead.
> 
> martin