Skip to content

Instantly share code, notes, and snippets.

@liuchong
Created December 14, 2018 12:44
Show Gist options
  • Select an option

  • Save liuchong/2d1d74851ea2df877504c7d1143f3ce5 to your computer and use it in GitHub Desktop.

Select an option

Save liuchong/2d1d74851ea2df877504c7d1143f3ce5 to your computer and use it in GitHub Desktop.
elisp functions for emacs window spawning
;;
;; Add window width/height to frame.
;;
(defun my/add-window-to-frame-width ()
"Resize selected frame to add width of current window to current frame."
(let* ((f-width (frame-width))
(w-width (window-width)))
(set-frame-width (selected-frame) (+ f-width w-width))))
(defun my/add-window-to-frame-height ()
"Resize selected frame to add height of current window to current frame."
(let* ((f-height (frame-height))
(w-height (window-height)))
(set-frame-height (selected-frame) (+ f-height w-height))))
;;
;; Del window width/height from frame.
;;
(defun my/del-window-from-frame-width ()
"Resize selected frame to del width of current window from current frame."
(let* ((f-width (frame-width))
(w-width (window-width)))
(set-frame-width (selected-frame) (- f-width w-width))))
(defun my/del-window-from-frame-height ()
"Resize selected frame to del height of current window from current frame."
(let* ((f-height (frame-height))
(w-height (window-height)))
(set-frame-height (selected-frame) (- f-height w-height))))
;;
;; Double frame width/height
;;
(defun my/double-frame-width ()
"Resize selected frame to set width double of current."
(let* ((current-width (window-width)))
(set-frame-width (selected-frame) (* current-width 2))))
(defun my/double-frame-height ()
"Resize selected frame to set height double of current."
(let* ((current-height (window-height)))
(set-frame-height (selected-frame) (* current-height 2))))
;;
;; Half frame width/height
;;
(defun my/half-frame-width ()
"Resize selected frame to set width half of current."
(let* ((current-width (window-width)))
(set-frame-width (selected-frame) (/ current-width 2))))
(defun my/half-frame-height ()
"Resize selected frame to set height half of current."
(let* ((current-height (window-height)))
(set-frame-height (selected-frame) (/ current-height 2))))
;;
;; Spawn new window on right/below
;;
(defun my/spawn-window-right ()
"split a same window on the right."
(my/double-frame-width)
(split-window-right))
(defun my/spawn-window-below ()
"split a same window on the below."
(my/double-frame-height)
(split-window-below))
;;
;; Erase window from horizontally/vertically
;;
(defun my/erase-window-h ()
"Erase another window horizontally."
(delete-other-windows-internal)
(my/half-frame-width))
(defun my/erase-window-v ()
"Erase another window vertically."
(delete-other-windows-internal)
(my/half-frame-height))
;;
;; Copied codes from
;; https://emacs.stackexchange.com/questions/24964/resize-move-frame-window
;;
(defun my/frame-move-resize (position)
"Resize selected frame to cover exactly 1/3 of screen area, and
move frame to given third of current screen. Symbol POSITION can
be either left, center, right."
(pcase position
('left (setf x 0))
('center (setf x 1))
('right (setf x 2)))
(let* ((HEIGHT (display-pixel-height))
(WIDTH (display-pixel-width)))
(set-frame-size (selected-frame) (/ WIDTH 3) HEIGHT t)
(set-frame-position (selected-frame) (* x (/ WIDTH 3)) 0)))
;;
;; Shortcuts
;;
(global-set-key (kbd "C-x x 2") (lambda () (interactive) (my/spawn-window-below)))
(global-set-key (kbd "C-x x 3") (lambda () (interactive) (my/spawn-window-right)))
(global-set-key (kbd "C-x x 1 2") (lambda () (interactive) (my/erase-window-v)))
(global-set-key (kbd "C-x x 1 3") (lambda () (interactive) (my/erase-window-h)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment