Files
.emacs.d/lisp/init-kbd-func.el
andsy10 ccd5c085f8 refactor(config): Clean up and reorganize Emacs configuration
Remove unused packages (EMMS, jinx, god-mode), simplify terminal
setup with ido instead of vertico/orderless, and reorganize
keybindings. Add new packages (debase, discomfort, git-timemachine,
edit-indirect) and improve EXWM, dired, and theme
configurations. Move restart-fcitx5 to kbd-func and update Linux
PATH for Guix.
2026-05-30 23:02:29 +08:00

200 lines
6.2 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-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