 
 ;;; Layout Activation
 
+(defun jail-windows--space-available-check (available-space
+                                            minimum-space
+                                            size
+                                            ratio)
+  "[Internal]"
+  (if size
+      (<= minimum-space
+          (- available-space
+             size))
+    (when ratio
+      (<= minimum-space
+          (* (/ available-space
+                ratio)
+             (- 1.0 ratio))))))
+
 (defun jail-windows--add-to-groups (window groups)
   "[Internal]"
   (when groups


;;; Core functionality

(defun jail-windows--display-buffer-override (buffer alist)
  "Take over window placement... ALIST is ignored"
  (let ((buffer-name (buffer-name buffer))
        (available-windows (window-list (selected-frame) 'nominibuf))
        (matched-group))
    ;; For each group defined in the jail-windows' window-options
    (--each-while (jail-windows--get-option nil 'groups)
        ;; Stop iterating when a group has matched
        (not matched-group)
      ;; Pull the regexp list and the window list from the group
      (let ((regexp-list (car it))
            (window-list (cdr it))
            (-compare-fn (-flip 'string-match-p)))
        ;; Check if this list's regexp matches the BUFFER argument's name
        (if (-contains? regexp-list buffer-name)
            ;; When it does match, only use windows from this group
            (progn
              (setq matched-group t)

            (setq available-windows
                  (-difference available-windows window-list))))))

    ;; Pick among the remaining windows
    (when available-windows
      (let ((selected-window))
        ;; TODO: Look for an active one
        (setq selected-window (car available-windows))

        ;; Return the selected window
        selected-window))))

(defun jail-windows--display-buffer-override (buffer alist)
  "[Internal] Take over window placement... ALIST is ignored"
  (let ((selected-window (jail-windows--find-window-for-buffer (selected-frame)
                                                               buffer
                                                               alist)))
    (when selected-window
      (window--display-buffer buffer
                              selected-window
                              'reuse
                              alist))))

(defadvice display-buffer (around jail-window-hook activate)
  "Handles jailing the window"
  (if (jail-windows/active-p)
      (let ((display-buffer-overriding-action
             '((jail-windows--display-buffer-override))))
        ad-do-it)
    (progn ad-do-it)))

;; TODO: (defadvice display-buffer-other-frame ...)?
