From f7badfe94a3c6c7275b9c76ef47a7c0e0dfa6ec5 Mon Sep 17 00:00:00 2001 From: Vincent Zhang Date: Thu, 8 Aug 2024 19:12:00 +0800 Subject: [PATCH] refactor --- lisp/init-completion.el | 4 ++-- lisp/init-funcs.el | 42 +++++++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lisp/init-completion.el b/lisp/init-completion.el index be315da65..c6cb5598b 100644 --- a/lisp/init-completion.el +++ b/lisp/init-completion.el @@ -222,12 +222,12 @@ value of the selected COLOR." ;; :preview-key on a per-command basis using the `consult-customize' macro. (consult-customize consult-line consult-line-multi - :initial (thing-at-point 'symbol t) + :initial (selected-region-or-symbol-at-point) :preview-key 'any consult-buffer consult-recent-file consult-theme :preview-key '(:debounce 1.0 any) consult-goto-line :preview-key '(:debounce 0.5 any) consult-ripgrep consult-git-grep consult-grep - :initial (thing-at-point 'symbol t) + :initial (selected-region-or-symbol-at-point) :preview-key '(:debounce 0.5 any)) ;; Optionally configure the narrowing key. diff --git a/lisp/init-funcs.el b/lisp/init-funcs.el index c9e6830d5..b55453e7e 100644 --- a/lisp/init-funcs.el +++ b/lisp/init-funcs.el @@ -132,6 +132,30 @@ Same as '`replace-string' `C-q' `C-m' `RET' `RET''." (message "Copied '%s'" filename)) (warn "Current buffer is not attached to a file!")))) +(defun create-scratch-buffer () + "Create a scratch buffer." + (interactive) + (switch-to-buffer (get-buffer-create "*scratch*")) + (lisp-interaction-mode)) + +(defun save-buffer-as-utf8 (coding-system) + "Revert a buffer with `CODING-SYSTEM' and save as UTF-8." + (interactive "zCoding system for visited file (default nil):") + (revert-buffer-with-coding-system coding-system) + (set-buffer-file-coding-system 'utf-8) + (save-buffer)) + +(defun save-buffer-gbk-as-utf8 () + "Revert a buffer with GBK and save as UTF-8." + (interactive) + (save-buffer-as-utf8 'gbk)) + +(defun selected-region-or-symbol-at-point () + "Return the selected region, otherwise return the symbol at point." + (if (region-active-p) + (buffer-substring-no-properties (region-beginning) (region-end)) + (thing-at-point 'symbol t))) + ;; Browse URL (defun centaur-browse-url (url) "Open URL using a configurable method. @@ -195,24 +219,6 @@ See `browse-url' for more details." (find-file-other-window centaur-custom-post-file))) ;; Misc -(defun create-scratch-buffer () - "Create a scratch buffer." - (interactive) - (switch-to-buffer (get-buffer-create "*scratch*")) - (lisp-interaction-mode)) - -(defun save-buffer-as-utf8 (coding-system) - "Revert a buffer with `CODING-SYSTEM' and save as UTF-8." - (interactive "zCoding system for visited file (default nil):") - (revert-buffer-with-coding-system coding-system) - (set-buffer-file-coding-system 'utf-8) - (save-buffer)) - -(defun save-buffer-gbk-as-utf8 () - "Revert a buffer with GBK and save as UTF-8." - (interactive) - (save-buffer-as-utf8 'gbk)) - (defun byte-compile-elpa () "Compile packages in elpa directory. Useful if you switch Emacs versions." (interactive)