From 59cbb1b7da162ccb399de36c19096b2e39820ee1 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Thu, 20 Oct 2022 02:06:32 +0800 Subject: [PATCH] add keybindings --- init.el | 1 + lisp/init-edit.el | 7 --- lisp/init-func.el | 92 ++++++++++++++++++++++++++++++++++++++++ lisp/init-git.el | 10 +++++ lisp/init-keybindings.el | 74 ++++++++++++++++++++++++++++++++ lisp/init-vertico.el | 4 +- 6 files changed, 179 insertions(+), 9 deletions(-) create mode 100644 lisp/init-func.el create mode 100644 lisp/init-keybindings.el diff --git a/init.el b/init.el index 6ca9978..9152af5 100644 --- a/init.el +++ b/init.el @@ -109,6 +109,7 @@ If freezing sometimes, decrease it. If stuttering, increase it.") (require 'init-edit) (require 'init-theme) (require 'init-vertico) +(require 'init-keybindings) (require 'which-key) (setq which-key-show-early-on-C-h t) diff --git a/lisp/init-edit.el b/lisp/init-edit.el index 9a1e236..d817b21 100644 --- a/lisp/init-edit.el +++ b/lisp/init-edit.el @@ -20,13 +20,6 @@ "symbol-overlay") -;;; mwim -(lazy-load-global-keys - '(("C-a" . mwim-beginning-of-code-or-line) - ("C-e" . mwim-end-of-code-or-line)) - "mwim") - - (setq backward-delete-char-untabify-method 'hungry) diff --git a/lisp/init-func.el b/lisp/init-func.el new file mode 100644 index 0000000..9b578df --- /dev/null +++ b/lisp/init-func.el @@ -0,0 +1,92 @@ +;; -*- coding: utf-8; lexical-binding: t; -*- + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; open file ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defun creature/open-init-file () + "Open init file." + (interactive) + (find-file (expand-file-name "init.el" creature/config-dir))) + +(defun creature/open-in-external-app (file-path) + (if (eq system-type 'windows-nt) + (w32-shell-execute "open" (replace-regexp-in-string "/" "\\\\" file-path)) + (start-process "" nil "xdg-open" file-path))) + +(defun creature/open-file-or-directory-in-external-app (arg) + (interactive "P") + (if arg + (creature/open-in-external-app (expand-file-name default-directory)) + (let ((file-path (if (derived-mode-p 'dired-mode) + (dired-get-file-for-visit) + buffer-file-name))) + (if file-path + (creature/open-in-external-app file-path) + (message "No file associated to this buffer"))))) + +(defun creature/sudo-edit (&optional arg) + (interactive "P") + (let ((fname (if (or arg (not buffer-file-name)) + (read-file-name "File: ") + buffer-file-name))) + (find-file + (cond ((string-match-p "^/ssh:" fname) + (with-temp-buffer + (insert fname) + (search-backward ":") + (let ((last-match-end nil) + (last-ssh-hostname nil)) + (while (string-match "@\\\([^:|]+\\\)" fname last-match-end) + (setq last-ssh-hostname (or (match-string 1 fname) + last-ssh-hostname)) + (setq last-match-end (match-end 0))) + (insert (format "|sudo:%s" (or last-ssh-hostname "localhost")))) + (buffer-string))) + (t (concat "/sudo:root@localhost:" fname)))))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; indentation ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(defvar creature/indent-sensitive-modes + '(asm-mode + coffee-mode + elm-mode + haml-mode + haskell-mode + slim-mode + makefile-mode + makefile-bsdmake-mode + makefile-gmake-mode + makefile-imake-mode + python-mode + yaml-mode) + "Modes which disable auto-indenting.") + +(defun creature/fallback-indent-func () + (unless (member major-mode creature/indent-sensitive-modes) + (save-excursion + (if (region-active-p) + (progn + (indent-region (region-beginning) (region-end)) + (message "Indented selected region.")) + (progn + (indent-region (point-min) (point-max)) + (message "Indented buffer."))) + (whitespace-cleanup)))) + +(defun creature/indent-region-or-buffer () + "Indent a region if selected, otherwise the whole buffer." + (interactive) + (if (and (bound-and-true-p prettier-mode) + (bound-and-true-p prettier-version) + (not (region-active-p))) + + (progn + (save-excursion + (prettier-prettify)) + (message "Indented prettier buffer.")) + + (creature/fallback-indent-func))) + + +(provide 'init-func) diff --git a/lisp/init-git.el b/lisp/init-git.el index 4ea60c5..e3da5de 100644 --- a/lisp/init-git.el +++ b/lisp/init-git.el @@ -5,6 +5,16 @@ '(("C-x g" . magit-status)) "magit") +(lazy-load-local-keys + '(("gc" . magit-clone) + ("gff" . magit-find-file) + ("gfc" . magit-find-git-config-file) + ("gfs" . magit-stage-file) + ("gi" . magit-init) + ("gl" . magit-list-repositories) + ("gs" . magit-status)) + creature/map "magit") + ;; gpg config (defun kill-gpg-agent-when-emacs-exit () "Kill `gpg-agent' for security when Emacs be killed." diff --git a/lisp/init-keybindings.el b/lisp/init-keybindings.el new file mode 100644 index 0000000..ca452e6 --- /dev/null +++ b/lisp/init-keybindings.el @@ -0,0 +1,74 @@ +;; -*- coding: utf-8; lexical-binding: t; -*- + +;;; mwim +(lazy-load-global-keys + '(("C-a" . mwim-beginning-of-code-or-line) + ("C-e" . mwim-end-of-code-or-line)) + "mwim") + + +(require 'init-func) +(lazy-load-set-keys + '(("C-x k" . kill-this-buffer) + ("C-x K" . kill-buffer) + ("C-M-\\" . creature/indent-region-or-buffer))) + + +(lazy-load-set-keys + '(("bb" . switch-to-buffer) + ("bd" . kill-current-buffer) + ("be" . eval-buffer) + + ("fi" . creature/open-init-file) + ("fo" . creature/open-file-or-directory-in-external-app) + ("fj" . dired-jump) + ("ff" . find-file) + ("fp" . find-file-at-point) + ("fs" . save-buffer) + + ("hf" . describe-function) + ("hF" . describe-face) + ("hv" . describe-variable) + ("hk" . describe-key) + ("ho" . describe-symbol) + ("hp" . describe-package) + ("qk" . save-buffers-kill-emacs) + ("qq" . save-buffers-kill-terminal) + + ("w" . creature/transient-window) + ("SPC" . execute-extended-command)) + creature/map) + + +(require 'transient) +(transient-define-prefix creature/transient-window () + "Show menu buffer for window operations." + :transient-suffix 'transient--do-stay + :transient-non-suffix 'transient--do-exit + [["Jump" + ("d" "Window down" windmove-down) + ("u" "Window up" windmove-up) + ("l" "Window left" windmove-left) + ("r" "Window right" windmove-right) + ("p" "Window Previous" (lambda () + (interactive) + (other-window -1)) + ) + ("n" "Window next" other-window)] + + ["Size reset" + ("{" "Height shrink" shrink-window) + ("}" "Height enlarge" enlarge-window) + ("[" "Width shrink" shrink-window-horizontally) + ("]" "Width enlarge" enlarge-window-horizontally) + ("=" "Banlance" balance-windows :transient nil)] + + ["Operation" + ("-" "Split below" split-window-below) + ("/" "Split right" split-window-right) + ("m" "Delete Others" delete-other-windows :transient nil) + ("k" "Delete window" delete-window) + ]]) + + +(provide 'init-keybindings) diff --git a/lisp/init-vertico.el b/lisp/init-vertico.el index 5a3c0eb..26a4f14 100644 --- a/lisp/init-vertico.el +++ b/lisp/init-vertico.el @@ -34,9 +34,9 @@ "cape") -(lazy-load-global-keys +(lazy-load-local-keys '(("fr" . consult-recent-file)) - "consult" creature/prefix-key) + creature/map "consult") (with-eval-after-load 'consult (recentf-mode 1))