Skip to content

Commit

Permalink
Merge pull request #1713 from LeeLaffan/vi-mode-extend-window-managment
Browse files Browse the repository at this point in the history
vi-mode: Adding additional C-w functionality (Window management)
  • Loading branch information
cxxxr authored Dec 24, 2024
2 parents 3e64ef5 + 933d809 commit 6ae0cf9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 13 deletions.
33 changes: 23 additions & 10 deletions extensions/vi-mode/binds.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,32 @@
(define-key *normal-keymap* "z ^" 'vi-scroll-top-line-to-bottom)
(define-key *normal-keymap* "Z Z" 'vi-write-quit)
(define-key *normal-keymap* "Z Q" 'vi-quit)
(define-key *motion-keymap* "C-w s" 'split-active-window-vertically)
(define-key *motion-keymap* "C-w C-s" 'split-active-window-vertically)

;; Window Management
(define-key *motion-keymap* "C-w h" 'vi-window-move-left)
(define-key *motion-keymap* "C-w C-h" 'vi-window-move-left)
(define-key *motion-keymap* "C-w j" 'vi-window-move-down)
(define-key *motion-keymap* "C-w C-j" 'vi-window-move-down)
(define-key *motion-keymap* "C-w k" 'vi-window-move-up)
(define-key *motion-keymap* "C-w C-k" 'vi-window-move-up)
(define-key *motion-keymap* "C-w l" 'vi-window-move-right)
(define-key *motion-keymap* "C-w C-l" 'vi-window-move-right)
(define-key *motion-keymap* "C-w s" 'vi-window-split-vertically)
(define-key *motion-keymap* "C-w S" 'vi-window-split-vertically)
(define-key *motion-keymap* "C-w C-s" 'vi-window-split-vertically)
(define-key *motion-keymap* "C-w v" 'vi-window-split-horizontally)
(define-key *motion-keymap* "C-w C-v" 'vi-window-split-horizontally)
(define-key *motion-keymap* "C-w n" 'vi-window-split-vertically-new)
(define-key *motion-keymap* "C-w C-n" 'vi-window-split-vertically-new)
(define-key *motion-keymap* "C-w w" 'next-window)
(define-key *motion-keymap* "C-w C-w" 'next-window)
(define-key *motion-keymap* "C-w p" 'previous-window)
(define-key *motion-keymap* "C-w C-p" 'previous-window)
(define-key *motion-keymap* "C-w q" 'vi-quit)
(define-key *motion-keymap* "C-w h" 'window-move-left)
(define-key *motion-keymap* "C-w C-h" 'undefined-key)
(define-key *motion-keymap* "C-w l" 'window-move-right)
(define-key *motion-keymap* "C-w C-l" 'undefined-key)
(define-key *motion-keymap* "C-w k" 'window-move-up)
(define-key *motion-keymap* "C-w C-k" 'undefined-key)
(define-key *motion-keymap* "C-w j" 'window-move-down)
(define-key *motion-keymap* "C-w C-j" 'undefined-key)
(define-key *motion-keymap* "C-w c" 'vi-close)
(define-key *motion-keymap* "C-w o" 'delete-other-windows)
(define-key *motion-keymap* "C-w C-o" 'delete-other-windows)

(define-key *motion-keymap* "C-o" 'vi-jump-back)
(define-key *motion-keymap* "C-i" 'vi-jump-next)
(define-key *motion-keymap* "' '" 'vi-jump-previous)
Expand Down
65 changes: 64 additions & 1 deletion extensions/vi-mode/commands.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,18 @@
:vi-inner-paren
:vi-repeat
:vi-normal
:vi-keyboard-quit))
:vi-keyboard-quit
:vi-close
:vi-window-move-left
:vi-window-move-down
:vi-window-move-up
:vi-window-move-right
:vi-window-split-horizontally
:vi-window-split-vertically
:vi-window-split-new-buffer
:vi-window-split-horizontally-new
:vi-window-split-vertically-new
:vi-switch-to-buffer))
(in-package :lem-vi-mode/commands)

(defun extract-count-keys (keys)
Expand Down Expand Up @@ -979,6 +990,12 @@ on the same line or at eol if there are none."
(vi-write)
(vi-quit nil))

(define-command vi-close (&optional (n 1)) (:universal)
(dotimes (i n)
(if (one-window-p)
(lem:message "Cannot close last window")
(delete-active-window))))

(define-command vi-end-insert () ()
(change-state 'normal)
(vi-backward-char 1))
Expand Down Expand Up @@ -1092,3 +1109,49 @@ on the same line or at eol if there are none."
(error 'editor-abort))
(vi-visual-end)
(keyboard-quit))

(define-command vi-window-move-left (&optional (n 1)) (:universal)
"Go to the window on the left N times."
(dotimes (i n) (window-move-left)))

(define-command vi-window-move-down (&optional (n 1)) (:universal)
"Go to the window on the down N times."
(dotimes (i n) (window-move-down)))

(define-command vi-window-move-up (&optional (n 1)) (:universal)
"Go to the window on the up N times."
(dotimes (i n) (window-move-up)))

(define-command vi-window-move-right (&optional (n 1)) (:universal)
"Go to the window on the right N times."
(dotimes (i n) (window-move-right)))

(define-command vi-window-split-horizontally (&optional (n 1)) (:universal)
"Split the window horizontally and moves N times."
(dotimes (i n)
(split-active-window-horizontally)
(window-move-right)))

(define-command vi-window-split-vertically (&optional (n 1)) (:universal)
"Split the window vertically and moves N times."
(dotimes (i n)
(split-active-window-vertically)
(window-move-down)))

(define-command vi-switch-to-buffer (&optional (filename nil)) (:universal-nil)
"Opens the specified file name or creates a blank buffer."
(switch-to-buffer (if (or (null filename) (string= filename ""))
(make-buffer nil :temporary t)
(execute-find-file *find-file-executor* (get-file-mode filename) filename))))

(define-command vi-window-split-horizontally-new (&optional (n 1) (filename nil)) (:universal)
"Creates an empty buffer in a new window N times."
(dotimes (i (or n 1))
(vi-window-split-horizontally)
(vi-switch-to-buffer filename)))

(define-command vi-window-split-vertically-new (&optional (n 1) (filename nil)) (:universal)
"Creates an empty buffer in a new window N times."
(dotimes (i n)
(vi-window-split-vertically)
(vi-switch-to-buffer filename)))
24 changes: 22 additions & 2 deletions extensions/vi-mode/ex-command.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,26 @@
(define-ex-command "^(w|write)$" (range filename)
(ex-write range filename t))

(define-ex-command "^wa(?:ll)$" (range argument)
(define-ex-command "^wa(?:ll)?$" (range argument)
(declare (ignore range argument))
(ex-write-all nil))

(define-ex-command "^wa(?:ll)!$" (range argument)
(define-ex-command "^wa(?:ll)?!$" (range argument)
(declare (ignore range argument))
(ex-write-all t))

(define-ex-command "^new$" (range filename)
(declare (ignore range))
(lem-vi-mode/commands:vi-window-split-vertically-new 1 filename))

(define-ex-command "^vne(?:w)?$" (range filename)
(declare (ignore range filename))
(lem-vi-mode/commands:vi-window-split-horizontally-new 1 filename))

(define-ex-command "^ene(?:w)?$" (range filename)
(declare (ignore range))
(lem-vi-mode/commands:vi-switch-to-buffer filename))

(define-ex-command "^update$" (range filename)
(when (lem:buffer-modified-p (lem:current-buffer))
(ex-write range filename t)))
Expand Down Expand Up @@ -106,12 +118,20 @@
(ex-write-all t)
(lem:exit-lem nil))

(define-ex-command "^clo(?:se)?$" (range filename)
(declare (ignore range filename))
(lem-vi-mode/commands:vi-close 1))

(define-ex-command "^(x|xit)$" (range filename)
(ex-write-quit range filename nil nil))

(define-ex-command "^(x|xit)!$" (range filename)
(ex-write-quit range filename t nil))

(define-ex-command "^on(?:ly)?$" (range filename)
(declare (ignore range filename))
(lem:delete-other-windows))

(defun copy-current-jumplist-to-next-window ()
(let* ((window-list
(lem:compute-window-list (lem:current-window)))
Expand Down

0 comments on commit 6ae0cf9

Please sign in to comment.