Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to exclude dedicated-windows #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 117 additions & 9 deletions rotate.el → emacs-rotate.el
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
;;; rotate.el --- Rotate the layout of emacs
;;; emacs-rotate.el --- Rotate the layout of emacs

;; Copyright (C) 2013 daichirata

;; Author: daichi.hirata <hirata.daichi at gmail.com>
;; Version: 0.1.0
;; Keywords: window, layout
;; Keywords: frames, window, layout
;; URL: https://github.com/daichirata/emacs-rotate

;; This file is free software; you can redistribute it and/or modify
Expand All @@ -28,8 +28,113 @@

(eval-when-compile (require 'cl-lib))

(defgroup rotate nil
"tmux like window manager"
:group 'windows)

(defvar rotate-count 0)

(defcustom rotate:exclude-regex-alist
'(
;; e.g. " buffer-name-with-space-padded"
"^ +"
;; e.g. " *auto-generated-buffer*"
"^[ ]+\\*[^*]+\\*" ; Although this case is included in the 1st expression.
)
" Setting this variable sometimes becomes useful when you execute (rotate-window).
buffer-name matching list of regular expression is excluded.
More than a element of regular expression is tedious, but this makes it easy to maintain the list.
"
:group 'rotate
)

(defun rotate:exclude-p (win)
"Return flags of list about which 'win is to be excluded.
according to variable rotate:exclude-regex-alist.
Each flag have value 2^x, which is binary expression corresponding to rotate:exclude-regex-alist.
"
;;(loop with flag = (make-vector (length win) 0)
(cl-loop with flag = (make-vector (length win) 0)
with idx_reg = 0
for regex in rotate:exclude-regex-alist
do
(cl-loop with idx_win = 0
for w in win
do
(if (string-match regex (buffer-name (window-buffer w)))
(setf (aref flag idx_win) (+ (aref flag idx_win) (expt 2 idx_reg)))
)
(incf idx_win)
)
(incf idx_reg)
finally (return (append flag nil)))
)

(defun rotate:no-dedicated-window-p (win)
"Return flag [1:true, 0:false ] of list for which 'win is window-dedicated-p"
(mapcar #'(lambda (x) (if x 1 0)) (mapcar 'window-dedicated-p win))
)

(defun rotate:count-windows:no-dedicated ()
(length (rotate:window-list:no-dedicated))
)

;;notused$ (defun rotate:count-windows:exclude-regex ()
;;notused$ "Return the number of windows each of which is not dedicated window and not matching rotate:exclude-regex-alist"
;;notused$ (length (rotate:exclude-p))
;;notused$ )

(defun rotate:count-windows:no-dedicated ()
"Return the number of not dedicated windows."
(length (delq t (mapcar 'window-dedicated-p (window-list-1))))
)

(defun rotate:one-window-p:no-dedicated ()
"Extended version of one-window-p. Ignore dedicated-windows."
(let ( (num_win (rotate:count-windows:no-dedicated) ))
(if (= num_win 1)
t
nil))
)

;;;###autoload
(defun rotate:window-list:no-dedicated ()
"Return list of windows.
Ignored files are
- window-dedicated-p
"
(let* (
(wl (window-list-1))
(flg2 (rotate:no-dedicated-window-p wl))
)
(cl-loop
for i2 in flg2
for i3 in wl
if (> 1 i2) collect i3
);loop
);let*
)

;;;###autoload
(defun rotate:window-list:exclude-regex ()
"Return list of windows.
Ignored files are
- window-dedicated-p
- named rotate:exclude-regex-alist"
(let* (
(wl (window-list-1))
(flg1 (rotate:exclude-p wl))
(flg2 (rotate:no-dedicated-window-p wl))
)
(cl-loop for i1 in flg1
for i2 in flg2
for i3 in wl
if (> 1 (+ i1 i2))
collect i3
);loop
);let*
)

(defvar rotate-functions
'(rotate:even-horizontal
rotate:even-vertical
Expand All @@ -39,6 +144,8 @@

;;;###autoload
(defun rotate-layout ()
"Switches to the next window layout defined in
`rotate-functions'."
(interactive)
(let* ((len (length rotate-functions))
(func (elt rotate-functions (% rotate-count len))))
Expand All @@ -50,10 +157,11 @@

;;;###autoload
(defun rotate-window ()
"Rotates windows kind of like swapping them."
(interactive)
(let* ((bl (reverse (rotate:buffer-list)))
(nbl (append (cdr bl) (list (car bl)))))
(cl-loop for w in (rotate:window-list)
(cl-loop for w in (rotate:window-list:no-dedicated)
for b in (reverse nbl)
do (set-window-buffer w b))
(select-window (next-window))))
Expand Down Expand Up @@ -141,18 +249,18 @@
(window-list nil nil (minibuffer-window)))

(defun rotate:buffer-list ()
(mapcar (lambda (w) (window-buffer w)) (rotate:window-list)))
(mapcar (lambda (w) (window-buffer w)) (rotate:window-list:no-dedicated)))

(defun rotate:refresh-window (proc)
(when (not (one-window-p))
(let ((window-num (count-windows))
(let ((window-num (rotate:count-windows:no-dedicated))
(buffer-list (rotate:buffer-list))
(current-pos (cl-position (selected-window) (rotate:window-list))))
(current-pos (cl-position (selected-window) (rotate:window-list:no-dedicated))))
(delete-other-windows)
(funcall proc window-num)
(cl-loop for w in (rotate:window-list)
(cl-loop for w in (rotate:window-list:no-dedicated)
for b in buffer-list
do (set-window-buffer w b))
(select-window (nth current-pos (rotate:window-list))))))
(select-window (nth current-pos (rotate:window-list:no-dedicated))))))

(provide 'rotate)
(provide 'emacs-rotate)