emacs 配置

WLS2上用的,因为只在terminal里用所以精简了配置,去掉了很多gui相关的东西。

(require 'package)

;;(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
;;			 ("melpa" . "https://melpa.org/packages/")))

(setq package-archives
      '(("gnu"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
	("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))

(when (not (package-installed-p 'use-package))
  (package-refresh-contents)
  (package-install 'use-package)
  (package-install 'diminish))

(eval-when-compile (require 'use-package))

(setq use-package-always-ensure t)

(setq gc-cons-threshold most-positive-fixnum)
(setq read-process-output-max (* 1024 1024)) ;; 1mb
(defvar my/gc-timer (run-with-idle-timer 5 t #'garbage-collect)
  "Run garbarge collection when idle 5s.")
(when (not (package-installed-p 'use-package))
  (package-refresh-contents)
  (package-install 'use-package)
  (package-install 'diminish))

(eval-when-compile (require 'use-package))

(setq use-package-always-ensure t)

(setq gc-cons-threshold most-positive-fixnum)
(setq read-process-output-max (* 1024 1024)) ;; 1mb
(defvar my/gc-timer (run-with-idle-timer 5 t #'garbage-collect)
  "Run garbarge collection when idle 5s.")


(menu-bar-mode -1)
(show-paren-mode t)
(electric-pair-mode t)
(column-number-mode t)
(size-indication-mode t)
(savehist-mode t)
(recentf-mode t)
(save-place-mode t)
(fset 'yes-or-no-p 'y-or-n-p)

(setq scroll-step 1)
(setq scroll-conservatively 10000)
(setq scroll-preserve-screen-position 6)
(setq auto-window-vscroll nil)
(setq select-enable-clipboard t)
(setq line-move-visual nil)
(setq backup-directory-alist '(("."."~/.emacs.d/auto-save-list")))

(defun myh/tab ()
  (interactive)
  (when (looking-at "[])\">`'}]")
    (forward-char)))
(advice-add #'indent-for-tab-command :after #'myh/tab)
(advice-add #'c-indent-line-or-region :after #'myh/tab)

(global-set-key (kbd "M-k") 'delete-other-windows)


(use-package prog-mode
  :ensure nil
  :hook
  (prog-mode
   .
   (lambda ()
     (setq display-line-numbers t)
     (setq-local eldoc-documentation-function #'ignore)
     (hl-line-mode t)
     (hs-minor-mode 1)
     ;; (modify-syntax-entry ?- "w")
     (modify-syntax-entry ?_ "w"))))

(use-package treemacs
  :init
  (setq treemacs-width 35
        treemacs-indentation 0
        treemacs-no-png-images nil
        treemacs-max-git-entries 5000
        treemacs-is-never-other-window nil
        treemacs-silent-filewatch t
        treemacs-follow-after-init t
        treemacs-show-hidden-files t
        treemacs-indentation-string
        (propertize " " 'face 'fill-column-indicator)
        treemacs-space-between-root-nodes nil
        )

  :config
  (treemacs-resize-icons 16)

  (treemacs-follow-mode t)
  (treemacs-filewatch-mode t)
  (treemacs-fringe-indicator-mode -1))

(use-package evil
  :init
  (setq evil-want-integration t)
  (setq evil-want-keybinding nil)
  (setq evil-want-C-i-jump nil)

  :config
  (evil-mode t)
  (define-key evil-insert-state-map (kbd "C-e") 'end-of-line)
  (define-key evil-motion-state-map (kbd "C-e") 'end-of-line)
  (define-key evil-insert-state-map (kbd "C-a") 'beginning-of-line)
  (define-key evil-motion-state-map (kbd "C-a") 'beginning-of-line)
  (define-key evil-normal-state-map (kbd "M-.") 'xref-find-definitions)
  (define-key evil-motion-state-map (kbd "C-s") 'swiper)

  (unbind-key "C-n" evil-insert-state-map)
  (unbind-key "C-p" evil-insert-state-map))

(use-package evil-surround
  :config
  (global-evil-surround-mode 1))

(use-package evil-collection
  :init
  (setq evil-collection-company-use-tng nil)
  :after evil
  :config
  (advice-add #'evil-collection-outline-setup :override #'ignore)
  (evil-collection-init))

(use-package key-chord
  :config
  (setq key-chord-two-keys-delay 0.2)
  (key-chord-define evil-insert-state-map "jj" 'evil-normal-state)
  (key-chord-define evil-normal-state-map "//" 'avy-goto-char)
  (key-chord-mode 1))

(use-package evil-leader
  :config
  (global-evil-leader-mode)
  (evil-leader/set-leader ",")
  (evil-leader/set-key
    "f" 'counsel-find-file
    "d" 'treemacs-select-window
    "r" 'counsel-recentf
    "b" 'ivy-switch-buffer
    "k" 'kill-buffer
    "v" 'vc-diff
    ;; "l" 'flycheck-list-errors
    "l" 'magit-log-buffer-file
    "g" 'magit-status
    "s" 'projectile-find-file
    "," 'counsel-M-x
    "o" 'myh/switch-to-header-or-source
    "i" 'lsp-treemacs-symbols
    "m" '(lambda () (interactive)
	   (mu4e~headers-jump-to-maildir "/INBOX"))
    "cc" '(lambda () (interactive)
	    (if mark-active
		(comment-region (region-beginning)
				(region-end))
	      (progn (comment-line 1)
		     (previous-line))))
    "cu" 'uncomment-region
    "p" 'counsel-org-capture
    "." 'avy-goto-char
    "a" 'org-agenda))

(use-package treemacs-evil
  :after treemacs evil
  :ensure t)

(use-package recentf
  :ensure nil
  :config
  (recentf-mode 1)
  (setq recentf-max-saved-items 100)
  (setq recentf-exclude '("COMMIT_EDITMSG\\'"
			  ".*-autoloads\\.el\\'"
			  ".*\\.elc\\'"
			  ".*\\.el\\.gz\\'"
			  "[/\\]\\.elpa/"
			  "[/\\]\\.cache/")))

(use-package smex
  :init
  (setq smex-history-length 50))

(use-package ivy
  :init
  (setq ivy-extra-directories nil
	ivy-count-format  "%d: "
	ivy-height 16
	ivy-fixed-height-minibuffer t
	ivy-use-virtual-buffers nil
	swiper-use-visual-line nil
	swiper-use-visual-line-p (lambda (a) nil)
	)

  :bind
  (:map ivy-minibuffer-map
	("C-m" . ivy-alt-done)
	:map ivy-switch-buffer-map
	("C-k" . (lambda ()
		   (interactive)
		   (ivy-set-action 'kill-buffer)
		   (ivy-done)))))

(use-package ace-window
  :ensure t
  :custom-face
  (aw-leading-char-face ((t (:inherit font-lock-keyword-face :bold t :height 3.0))))
  (aw-mode-line-face ((t (:inherit mode-line-emphasis :bold t))))

  :custom
  (aw-keys '(?h ?j ?k ?l ?u ?i ?o ?p ?n))
  (aw-background nil)


  :bind
  (("M-o" . ace-window)))

(use-package super-save
  :diminish
  :init
  (setq super-save-auto-save-when-idle t
	super-save-idle-duration 5)
  :config
  (add-to-list 'super-save-triggers 'ace-window)
  (add-to-list 'super-save-hook-triggers 'find-file-hook)
  (add-to-list 'super-save-hook-triggers 'projectile-find-other-file)
  (super-save-mode +1))

(use-package expand-region
  :bind
  (("C-\\" . er/expand-region)))

(use-package browse-kill-ring :ensure t)


(use-package magit
  :bind
  (("C-x g" . magit))

  :commands myh/copy-current-git-branch
  :config
  ;; (setq magit-prefer-remote-upstream t)
  (setq magit-diff-refine-hunk (quote all))
  (defun copy-current-git-branch ()
    (interactive)
    (with-temp-buffer
      (insert (let ((branch (magit-get-current-branch)))
		(if branch branch "null")))
      (kill-ring-save (point-min) (point-max))))

  (magit-define-popup-switch
   'magit-log-popup
   ?m "Omit merge commits" "--no-merges")
  )

(use-package dired
  :ensure nil
  ;; :init
  ;; (setq dired-omit-files
  ;;       (concat dired-omit-files "\\|^\\..+$"))
  :hook
  (dired-load
   .
   (lambda () (load "dired-x")
     (setq dired-guess-shell-gnutar "gtar")))
  (dired-mode . (lambda () (dired-omit-mode 1)))

  :bind
  (:map dired-mode-map
	("q" . kill-this-buffer)
	("RET" . dired-find-alternate-file)
	("^" . (lambda () (interactive) (find-alternate-file ".."))))

  :config
  (put 'dired-find-alternate-file 'disabled nil)
  (require 'dired-x)
  (setq dired-omit-files
	(concat dired-omit-files "\\|^\\..+$")))

(use-package clang-format
  :after cc-mode
  :bind (:map c-mode-base-map
	      ("C-c C-f" . clang-format-buffer)
	      ("C-c C-S-f" . clang-format-region))
  :custom
  (clang-format-style "google"))

(use-package rainbow-delimiters
  :config
  (add-hook 'prog-mode-hook #'rainbow-delimiters-mode)
  :custom-face
  (rainbow-delimiters-base-face ((t (:inherit nil))))
  (rainbow-delimiters-depth-1-face ((t (:foreground "red"))))
  (rainbow-delimiters-depth-2-face ((t (:foreground "orange"))))
  (rainbow-delimiters-depth-3-face ((t (:foreground "forest green"))))
  (rainbow-delimiters-depth-4-face ((t (:foreground "dodger blue"))))
  (rainbow-delimiters-depth-5-face ((t (:foreground "deep pink"))))
  (rainbow-delimiters-depth-6-face ((t (:foreground "dark turquoise"))))
  (rainbow-delimiters-depth-7-face ((t (:foreground "tomato1"))))
  (rainbow-delimiters-depth-8-face ((t (:foreground "PaleVioletRed1"))))
  (rainbow-delimiters-depth-9-face ((t (:foreground "SeaGreen1"))))
  )

(use-package symbol-overlay
  :diminish
  :functions (turn-off-symbol-overlay turn-on-symbol-overlay)

  :bind
  (("M-i" . symbol-overlay-put)
   :map symbol-overlay-map
   ("?" . symbol-overlay-map-help))

  :hook
  ((prog-mode . symbol-overlay-mode))

  :init
  (setq symbol-overlay-idle-time 0.1)
  (setq-default symbol-overlay-scope t)
  :config
  (unbind-key "h" symbol-overlay-map))

(use-package "window"
  :ensure nil
  :custom
  (display-buffer-alist
   '(("\\*e?shell\\*"
      (display-buffer-in-side-window)
      (window-height . 20)
      (side . bottom)
      (slot . -1))
     ("vterm"
      (display-buffer-in-side-window)
      (window-height . 20)
      (side . bottom)
      (slot . -1))
     )))

(use-package highlight-indentation
  :diminish
  :if window-system
  :init
  (setq highlight-indentation-blank-lines t)

  :config
  ;; (set-face-background 'highlight-indentation-face "#e3e3d3")
  ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")

  :hook
  ((prog-mode . highlight-indentation-mode)))

(use-package which-key
  :diminish
  :config
  (which-key-mode t)
  (which-key-setup-side-window-bottom)
  (setq which-key-idle-delay 0.5))

(use-package org-roam
  :ensure t
  :diminish
  :init
  (setq org-roam-v2-ack t)
  :custom
  (org-roam-directory (file-truename "~/code/org-roam/"))
  :bind (("C-c n l" . org-roam-buffer-toggle)
	 ("C-c n f" . org-roam-node-find)
	 ("C-c n g" . org-roam-graph)
	 ("C-c n i" . org-roam-node-insert)
	 ("C-c n c" . org-roam-capture)
	 ;; Dailies
	 ("C-c n j" . org-roam-dailies-capture-today))
  :config
  (org-roam-setup)
  ;; If using org-roam-protocol
  (require 'org-roam-protocol))


(use-package company
  :diminish
  :init
  (setq company-dabbrev-downcase 0)
  (setq company-idle-delay 0.0)
  (setq company-minimum-prefix-length 1)
  (setq company-tooltip-align-annotations t)
  (setq company-backends
        '(
          ;; company-clang
          ;; company-xcode
          ;; company-dabbrev
          ;; company-yasnippet
          company-semantic
          company-cmake
          company-capf
          company-files
          (company-dabbrev-code
           company-gtags
           company-etags
           company-keywords)))

  :bind
  (:map company-active-map

        ("C-n" . company-select-next)
        ("C-p" . company-select-previous))

  :hook
  ;; (prog-mode . company-mode)
  (after-init . global-company-mode)
  :config
  (unbind-key "M-n" company-active-map)
  (unbind-key "M-p" company-active-map))

(use-package swiper
  :config
  (defun myh/before-swiper (&optional arg)
    (ignore arg)
    (if (region-active-p) (deactivate-mark)))
  (advice-add 'swiper :before #'myh/before-swiper)
  :bind (("C-s" . swiper)))

(use-package counsel
  :config
  (setq counsel-find-file-ignore-regexp
        (concat
         ;; File names beginning with # or .
         "\\(?:\\`[#.]\\)"
         ;; File names ending with # or ~
         "\\|\\(?:\\`.+?[#~]\\'\\)"))
  :bind
  (("M-s" . counsel-rg)
   ("M-x" . counsel-M-x)
   ;; ("C-x C-r" . counsel-recentf)
   ("C-x C-r" . counsel-buffer-or-recentf)
   ("C-x C-f" . counsel-find-file)))

(use-package projectile
  :diminish
  :hook (after-init . projectile-mode)
  :init
  (setq projectile-globally-ignored-file-suffixes
        '("#" "~" ".swp" ".o" ".so" ".exe"
          ".dll" ".elc" ".pyc" ".jar" ".class"))
  (setq projectile-globally-ignored-directories
        '(".git" "node_modules" "__pycache__" ".vs" "build" ".cache"
          ".ccls-cache" ".cquery_cached_index" ".vagrant" ".vscode"
          ".idea" "contrib" "qemu"))
  (setq projectile-globally-ignored-files
        '("TAGS" "tags" ".DS_Store" ".ccls-root" "Vagrentfile"))

  (setq projectile-indexing-method 'native
        projectile-completion-system 'ivy
        projectile-enable-caching t
        projectile-use-git-grep t)

  :config
  (when (and (not (executable-find "fd"))
             (executable-find "rg"))
    (setq projectile-generic-command
          (let ((rg-cmd ""))
            (dolist (dir projectile-globally-ignored-directories)
              (setq rg-cmd (format "%s --glob '!%s'" rg-cmd dir)))
            (concat "rg -0 --files --color=never --hidden" rg-cmd)))))

(use-package flycheck
  :ensure t
  :diminish
  :hook
  (prog-mode . flycheck-mode)
  (flycheck-error-list-mode
   . (lambda ()
       (evil-local-mode -1)
       (setq cursor-type nil)))
  :config
  (setq flycheck-indication-mode 'right-fringe)
  ;; Set fringe style
  (when (fboundp 'define-fringe-bitmap)
    (define-fringe-bitmap 'flycheck-fringe-bitmap-double-arrow
      [16 48 112 240 112 48 16] nil nil 'center))
  
  (setq flycheck-check-syntax-automatically '(save mode-enabled))
  
  :bind
  ((:map flycheck-error-list-mode-map
	 ("q" . delete-window)
	 ("j" . flycheck-error-list-next-error)
	 ("k" . flycheck-error-list-previous-error))))

(use-package flycheck-clang-tidy
  :after flycheck
  :hook
  (flycheck-mode . flycheck-clang-tidy-setup))

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注