Files
.emacs.d/lisp/init-kbd-func.el
andsy10 8db37d1c6d feat(config): Improve dired, keybindings, terminal UI and add packages
- Add dired buffer killing and custom dirvish layout toggle
- Replace ido with fido-mode in terminal config
- Add custom terminal color theme with 24-bit color support
- Add new packages: tokei, helpful, gptel-magit
- Refactor project keybindings under C-x p prefix
- Add mwim navigation bindings globally
- Improve ibuffer filter groups and consult buffer filters
- Add diff-mode keybindings and refine magit diff display
2026-06-03 20:29:01 +08:00

209 lines
6.6 KiB
EmacsLisp

;;; init-kbd-func.el --- functions for keybindings -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(defun open-init-file()
"Open user's init.el file in ~/.emacs.d ."
(interactive)
(find-file "~/.emacs.d/init.el"))
(when *is-windows*
(defun restart-emacs ()
"Restart emacs (for Windows)"
(interactive)
(save-some-buffers)
(w32-shell-execute "open" "runemacs.exe")
(kill-emacs)))
(defun my/jinx-smart-toggle ()
"Toggle jinx-mode smartly.
Disable jinx-mode if it's enable and global-jinx-mode is disable , otherwise toggle global-jinx-mode."
(interactive)
(unless (featurep 'jinx)
(require 'jinx))
(let ((local-on (bound-and-true-p jinx-mode))
(global-on (bound-and-true-p global-jinx-mode)))
(cond
((and local-on (not global-on))
(message "disable jinx-mode in current buffer")
(jinx-mode -1))
(t
(if global-on
(progn
(message "disable global-jinx-mode")
(global-jinx-mode -1))
(progn
(message "enable global-jinx-mode")
(global-jinx-mode 1)))))))
(defun popper-toggle-type-delete-other-window()
"Toggle type of popper window and delete other window."
(interactive)
(popper-toggle-type)
(delete-other-windows))
(defun consult-directory-externally (file)
"Open FILE externally using the default application of the system."
(interactive "fOpen externally: ")
(if (and (eq system-type 'windows-nt)
(fboundp 'w32-shell-execute))
(shell-command-to-string (encode-coding-string (replace-regexp-in-string "/" "\\\\"
(format "explorer.exe %s" (file-name-directory (expand-file-name file))))
'gbk))
(call-process (pcase system-type
('darwin "open")
('cygwin "cygstart")
(_ "xdg-open"))
nil 0 nil
(file-name-directory (expand-file-name file)))))
(defun consult-fd-home ()
"Search for all files in home folder use fd."
(interactive)
(let ((consult-fd-args "fd -H -I -i -c never --full-path")
(default-directory "~/"))
(consult-fd)))
(defun my-open-current-directory ()
"Open current directory in default file explorer."
(interactive)
(consult-directory-externally default-directory))
(defun org-cycle-parent-subtree()
"Cycle parent org subtree."
(interactive)
(if (org-at-heading-p)
(progn
(org-up-heading 1)
(org-cycle))
(org-back-to-heading)
(org-cycle)))
(defun my-denote-journal-path-to-existing-entry (&optional date interval)
"Return the path of current denote journal if it exists, otherwise return nil."
(require 'denote-journal)
(let* ((internal-date (denote-journal--date-in-interval-p (or date (current-time)) interval))
(files (denote-journal--get-entry internal-date interval)))
(if files
(denote-journal-select-file-prompt files))))
(defun my-denote-journal-open-entry ()
"Open current denote-journal entry."
(interactive)
(if (my-denote-journal-path-to-existing-entry)
(find-file (my-denote-journal-path-to-existing-entry))
(message "No journal for today yet")))
(defun org-table-align-all ()
"Ajust all table in current buffer."
(interactive)
(save-excursion
(goto-char (point-min))
(org-table-map-tables 'org-table-align t)))
(defun org-align-description-list ()
"Ajust all `::' in active region. "
(interactive)
(align-regexp (region-beginning) (region-end) "\\(\\s-*\\)::" 1 1 nil))
(defun my-dired-next-line()
"Move to forward line in dired buffer"
(interactive)
(forward-line 1)
(dired-move-to-filename))
(defun my-dirvish-layout-toggle ()
"Toggle layout using first recipe in `dirvish-layout-recipes' instead of fallback."
(interactive)
(let ((dv (dirvish-curr)))
(unless dv (user-error "Not a dirvish buffer"))
(unless (dv-curr-layout dv)
(setf (dv-ff-layout dv) (car dirvish-layout-recipes)))
(dirvish-layout-toggle)))
(defun my-magit-toggle-parent-section ()
"Toggle parent magit section."
(interactive)
(magit-section-up)
(magit-section-toggle (magit-section-at)))
(defun my-persp-remove-marked-ibuffers ()
"Remove marked buffers from perspective."
(interactive)
(let ((buffers (ibuffer-get-marked-buffers)))
(dolist (buf buffers)
(persp-remove-buffer buf))))
(defun my-persp-add-marked-ibuffers (persp-name)
"Add marked buffers to perspective."
(interactive
(list (completing-read "Perspective: "
(persp-names)
nil nil)))
(let ((buffers (ibuffer-get-marked-buffers)))
(persp-switch persp-name)
(dolist (buf buffers)
(persp-add-buffer buf))))
(defun my-persp-set-marked-ibuffers (persp-name)
"Set marked buffers to perspective."
(interactive
(list (completing-read "Perspective: "
(persp-names)
nil nil)))
(let ((buffers (ibuffer-get-marked-buffers)))
(persp-switch persp-name)
(dolist (buf buffers)
(persp-set-buffer buf))))
(defun gptel-agent-project ()
"Run `gptel-agent' in current project's root (by run it with prefix argument)."
(interactive)
(let ((current-prefix-arg '-))
(call-interactively 'gptel-agent)))
(defun consult-ripgrep-project ()
"Run `consult-ripgrep' in current project's root."
(interactive)
(when-let* ((proj (project-current t)))
(consult-ripgrep (project-root proj))))
(when *is-linux*
(defun reboot ()
"Execute loginctl reboot command."
(interactive)
(eshell-command "loginctl reboot"))
(defun poweroff ()
"Execute loginctl poweroff command"
(interactive)
(eshell-command "loginctl poweroff"))
(defun suspend ()
"Execute loginctl suspend command"
(interactive)
(eshell-command "loginctl suspend"))
(defun my-desktop-init ()
"Launch something for linux desktop."
(interactive)
(unless (zerop (shell-command "pgrep -x mihomo"))
(start-process-shell-command "mihomo" nil "mihomo -d ~/.config/mihomo/"))
(start-process-shell-command "xrandr-refresh" nil "xrandr --output DP-4 --mode 2560x1440 --rate 165.00")
(start-process-shell-command "xinput" nil "xinput set-prop 12 'libinput Accel Speed' -0.45"))
(defun restart-fcitx5 ()
"Kill and restart fcitx5, to fix random issues in exwm."
(interactive)
(if (zerop (shell-command "pgrep fcitx5"))
(progn
(eshell-command "pkill fcitx5")
(eshell-command "fcitx5 -d"))
(eshell-command "fcitx5 -d"))))
(provide 'init-kbd-func)
;;; init-kbd-func.el ends here