#+title: Emacs Tool Setup

* Vi Keybindings
Use =vi= keybindings with =evil=. Set the undo system to =undo-fu=. Wrapped lines can be moved between with =j= and =k=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package evil
    :init
    (setq evil-want-keybinding nil)
    :config
    (evil-mode)
    (evil-global-set-key 'motion "j" 'evil-next-visual-line)
    (evil-global-set-key 'motion "k" 'evil-previous-visual-line)
    (evil-global-set-key 'motion (kbd "RET") nil)
    :custom
    (evil-undo-system 'undo-fu))
#+end_src

Use =evil-collection= to include =vi= keybindings in extra modes.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package evil-collection
    :after evil
    :config
    (evil-collection-init))
#+end_src

* Undo
Better undo with =undo-fu=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package undo-fu)
#+end_src

Make undo persistent when closing Emacs with =undo-fu-session=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package undo-fu-session
    :init (undo-fu-session-global-mode 1))
#+end_src

* Lookup
Better lookup with =dumb-jump=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package dumb-jump
    :init (add-hook 'xref-backend-functions #'dumb-jump-xref-activate))
#+end_src

* Version Control
Install Magit for Git integration.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package magit)
#+end_src

* Document Viewing
Replace =DocView= with a better document viewer from =pdf-tools=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package pdf-tools
    :config
    (pdf-tools-install)
    :init
    (add-hook 'pdf-view-mode-hook #'(lambda () (display-line-numbers-mode -1)))
    (add-hook 'TeX-after-compilation-finished-functions
              #'TeX-revert-document-buffer)
    :config
    (setq TeX-view-program-selection '((output-pdf "PDF Tools"))
          TeX-view-program-list '(("PDF Tools" TeX-pdf-tools-sync-view))
          TeX-source-correlate-start-server t))
#+end_src

Save place in PDFs with =saveplace-pdf-view=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package saveplace-pdf-view
    :config (save-place-mode 1))
#+end_src

* Org-Mode
Ensure that =org= is set up before any of this.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package org)
#+end_src
Set my =org-mode= directory.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (setq org-directory "~/org")
#+end_src

Hide emphasis markers because I can see if something is *bold*, /italic/, or =monospace= without needing to see the markers.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (setq org-hide-emphasis-markers t)
#+end_src

Set up nicer looking bullet points.
- they look like circles
- instead of hyphens
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (font-lock-add-keywords 'org-mode
                          '(("^ *\\([-]\\) "
                             (0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "•"))))))
#+end_src

Set up fonts. Don't use =monospace= by default. Do use it where necessary though. Also, make different heading levels different sizes.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (add-hook 'org-mode-hook 'variable-pitch-mode)
  (custom-set-faces
   '(variable-pitch ((t (:family "CMU Serif" :height 130 :weight thin))))
   '(fixed-pitch ((t (:family "SauceCodePro Nerd Font" :height 110 :weight regular))))
   '(org-block ((t (:inherit fixed-pitch))))
   '(org-code ((t (:inherit (shadow fixed-pitch)))))
   '(org-document-info-keyword ((t (:inherit (shadow fixed-pitch)))))
   '(org-meta-line ((t (:inherit (font-lock-comment-face fixed-pitch)))))
   '(org-verbatim ((t (:inherit (shadow fixed-pitch)))))
   '(org-table ((t (:inherit (shadow fixed-pitch)))))
   '(org-document-title ((t (:inherit title :height 2.0 :underline nil))))
   '(org-level-1 ((t (:inherit outline-1 :weight: bold :height 1.75))))
   '(org-level-2 ((t (:inherit outline-2 :weight: bold :height 1.5))))
   '(org-level-2 ((t (:inherit outline-3 :weight: bold :height 1.25))))
   '(org-level-2 ((t (:inherit outline-4 :weight: bold :height 1.1))))
   '(org-level-4 ((t (:inherit outline-4 :height 1.1))))
   '(org-level-5 ((t (:inherit outline-5 :height 1.0))))
   )
#+end_src

Wrap lines and centre the view to make for a nicer reading experience.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package visual-fill-column)
  (add-hook 'org-mode-hook 'visual-line-mode)
  (add-hook 'org-mode-hook #'(lambda () (display-line-numbers-mode -1)))
  (defun jj/org-mode-visual-fill ()
    (setq visual-fill-column-width 100
          visual-fill-column-center-text t)
     (visual-fill-column-mode 1))
  (add-hook 'org-mode-hook #'jj/org-mode-visual-fill)
#+end_src

Increase the size of LaTeX previews.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))
#+end_src

Follow links with the return key.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (setq org-return-follows-link t)
#+end_src

Tangle on save.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (defun org-babel-tangle-config ()
    (when (string-suffix-p ".org" (buffer-file-name))
      (org-babel-tangle)))
  (add-hook 'org-mode-hook
    (lambda ()
      (add-hook 'after-save-hook #'org-babel-tangle-config)))
#+end_src

* Shell
Use =eshell= as an integrated shell.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package eshell)
  (global-set-key (kbd "C-c e") 'eshell)
#+end_src

* Language Servers
Add =eglot= keybindings.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (global-set-key (kbd "C-c r") 'eglot-rename)
  (global-set-key (kbd "C-c a") 'eglot-code-actions)
#+end_src

Install =tree-sitter=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package tree-sitter)
  (use-package tree-sitter-langs)
#+end_src

Define function to set up =eglot= automatically.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (defun jj/eglot-setup ()
    (eglot-ensure)
    (tree-sitter-mode 1)
    (tree-sitter-hl-mode 1))
#+end_src

* Completions
Use company for completions with no delay, starting immediately after first character is typed.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package company
    :config
    (add-hook 'after-init-hook 'global-company-mode)
    (setq company-idle-delay 0
          company-minimum-prefix-length 1
          company-selection-wrap-around t))
#+end_src

Use =vertico= as a completion user interface.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package vertico
    :custom
    (vertico-cycle t)
    :init
    (vertico-mode))
#+end_src

Use =orderless= to allow typing any portion of a word that you want to search for.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package orderless
    :ensure t
    :custom
    (completion-styles '(orderless basic))
    (completion-category-overrides '((file (styles basic partial-completion)))))
#+end_src

Get descriptions of items in =vertico= with =marginalia=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package marginalia
    :bind (:map minibuffer-local-map
           ("M-A" . marginalia-cycle))
    :init
    (marginalia-mode))
#+end_src

Get nerd font icons in completions.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package nerd-icons-completion
    :config
    (nerd-icons-completion-mode))
#+end_src

Use consult with =vertico= for extra functionality to various functions.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package consult
    :bind (
           ("C-c M-x" . consult-mode-command)
           ("C-c h" . consult-history)
           ("C-c k" . consult-kmacro)
           ("C-c m" . consult-man)
           ("C-c i" . consult-info)
           ([remap Info-search] . consult-info)
           ("C-x M-:" . consult-complex-command)
           ("C-x b" . consult-buffer)
           ("C-x 4 b" . consult-buffer-other-window)
           ("C-x 5 b" . consult-buffer-other-frame)
           ("C-x t b" . consult-buffer-other-tab)
           ("C-x r b" . consult-bookmark)
           ("C-x p b" . consult-project-buffer)
           ("M-#" . consult-register-load)
           ("M-'" . consult-register-store)
           ("C-M-#" . consult-register)
           ("M-y" . consult-yank-pop)
           ("M-g e" . consult-compile-error)
           ("M-g f" . consult-flycheck)
           ("M-g g" . consult-goto-line)
           ("M-g M-g" . consult-goto-line)
           ("M-g o" . consult-outline)
           ("M-g m" . consult-mark)
           ("M-g k" . consult-global-mark)
           ("M-g i" . consult-imenu)
           ("M-g I" . consult-imenu-multi)
           ("M-s d" . consult-fd)
           ("M-s c" . consult-locate)
           ("M-s g" . consult-grep)
           ("M-s G" . consult-git-grep)
           ("M-s r" . consult-ripgrep)
           ("M-s l" . consult-line)
           ("M-s L" . consult-line-multi)
           ("M-s k" . consult-keep-lines)
           ("M-s u" . consult-focus-lines)
           ("M-s e" . consult-isearch-history)
           :map isearch-mode-map
           ("M-e" . consult-isearch-history)
           ("M-s e" . consult-isearch-history)
           ("M-s l" . consult-line)
           ("M-s L" . consult-line-multi)
           :map minibuffer-local-map
           ("M-s" . consult-history)
           ("M-r" . consult-history))
    :hook (completion-list-mode . consult-preview-at-point-mode)
    :init
    (setq register-preview-delay 0.5
          register-preview-function #'consult-register-format)
    (advice-add #'register-preview :override #'consult-register-window)
    (setq xref-show-xrefs-function #'consult-xref
          xref-show-definitions-function #'consult-xref)
    :config
    (consult-customize
     consult-theme :preview-key '(:debounce 0.2 any)
     consult-ripgrep consult-git-grep consult-grep
     consult-bookmark consult-recent-file consult-xref
     consult--source-bookmark consult--source-file-register
     consult--source-recent-file consult--source-project-recent-file
     :preview-key '(:debounce 0.4 any))
    (setq consult-narrow-key "<"))
#+end_src

Use Flycheck for syntax checking.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package flycheck
    :config
    (add-hook 'after-init-hook #'global-flycheck-mode))
#+end_src

Use Flyspell for spell checking.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (dolist (hook '(text-mode-hook))
    (add-hook hook (lambda () (flyspell-mode 1))))
  (use-package flyspell-correct
    :after flyspell
    :bind (:map flyspell-mode-map ("C-;" . flyspell-correct-wrapper)))
#+end_src

* Snippets
Use =yasnippet= for snippets so I don't need to type as much.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package yasnippet
    :init
    (yas-global-mode 1)
    :config
    (global-set-key (kbd "C-c s") 'yas-insert-snippet))
#+end_src

Install snippet collection for =yasnippet=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package yasnippet-snippets)
#+end_src

* Formatting
Automatically format with Apheleia and =clang-format=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package apheleia
    :init (apheleia-global-mode +1))
  (use-package clang-format)
#+end_src

* RSS
Use Emacs as an RSS feed with =elfeed=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package elfeed
    :config
    (global-set-key (kbd "C-c f") 'elfeed)
    (global-set-key (kbd "C-c M-f") 'elfeed-update))
#+end_src

Make =elfeed= more powerful with =elfeed-goodies=.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package elfeed-goodies
    :after elfeed
    :config
    (elfeed-goodies/setup))
#+end_src

Store my feed in Org-mode [[./feed.org][here]].
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package elfeed-org
    :config
    (elfeed-org)
    (setq rmh-elfeed-org-files (list "~/.config/emacs/feed.org")))
#+end_src

* Deft
Use the Deft package to manage notes.
#+begin_src emacs-lisp :tangle ~/.config/emacs/tools.el :mkdirp yes
  (use-package deft
    :config
    (global-set-key (kbd "C-c d") 'deft)
    (setq deft-directory "~/notes/"
          deft-default-extension "org"))
#+end_src