config.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. ;;; config.el -*- lexical-binding: t; -*-
  2. ;; ============================================================
  3. ;; Custom
  4. ;; ============================================================
  5. (let ((custom-dir (expand-file-name "custom/" user-emacs-directory)))
  6. (when (file-directory-p custom-dir)
  7. (mapc #'load (directory-files custom-dir t "\\.el$"))))
  8. (add-to-list 'custom-theme-load-path (expand-file-name "themes/" user-emacs-directory))
  9. ;; ============================================================
  10. ;; Keybindings
  11. ;; ============================================================
  12. (use-package general
  13. :config
  14. (general-def
  15. :keymaps 'global
  16. "C-S-c" #'my/system-copy
  17. "C-S-x" #'my/system-cut
  18. "C-S-v" #'my/system-paste
  19. "C-z" #'undo
  20. "C-S-z" #'undo-redo
  21. )
  22. (general-def
  23. :keymaps 'override
  24. "C-k" #'avy-kill-whole-line
  25. "C-s C-f" #'avy-goto-char-timer
  26. "C-s C-s C-f" #'my/avy-char-region
  27. "C-s C-S-s" #'my/avy-line-region
  28. "C-s C-l" #'consult-line
  29. "C-s C-e" #'er/expand-region
  30. "C-x C-b" #'buffer-table
  31. "M-g g" #'avy-goto-line
  32. "C-r" #'recenter-top-bottom
  33. "C-s w" #'my/vterm-sync-here
  34. "C-b" #'scroll-down-command
  35. "C-<backspace>" #'my/delete-word-backward)
  36. (general-define-key
  37. :keymaps 'global
  38. "C-w" nil)
  39. ;; If you want certain keys to ONLY work in Meow Normal state, use :states.
  40. )
  41. ;; ============================================================
  42. ;; Settings, UI
  43. ;; ============================================================
  44. (setq kill-ring-max 1
  45. select-enable-clipboard nil
  46. select-enable-primary nil
  47. save-interprogram-paste-before-kill nil
  48. xterm-extra-capabilities '(getSelection setSelection modifyOtherKeys)
  49. mouse-drag-copy-region nil
  50. truncate-lines nil
  51. display-line-numbers-type t
  52. cursor-type 'bar
  53. savehist-additional-variables '(kill-ring search-ring)
  54. recentf-max-saved-items 75
  55. )
  56. ;; Modes
  57. (global-hl-line-mode t )
  58. (global-display-line-numbers-mode 1)
  59. (hl-line-mode 1)
  60. (savehist-mode 1)
  61. (global-clipetty-mode)
  62. ;; Fonts
  63. (set-face-attribute 'default nil :family "Fira Code" :height 120)
  64. (set-face-attribute 'variable-pitch nil :family "Noto Sans" :height 140)
  65. ;; Apply variable pitch to specific modes
  66. (dolist (hook '(markdown-mode-hook org-mode-hook json-mode-hook))
  67. (add-hook hook (lambda () (buffer-face-set '(:family "Noto Sans" :height 140)))))
  68. (add-to-list 'auto-mode-alist '("\\.qd$" . markdown-mode))
  69. (add-to-list 'term-file-aliases '("xterm-256" . "xterm"))
  70. ;; Recentf cleanup
  71. (with-eval-after-load 'recentf
  72. (run-at-time nil 600 #'recentf-cleanup)
  73. (setq recentf-exclude '(".*/$"))) ;; don't include dirs
  74. ;; ============================================================
  75. ;; packages
  76. ;; ============================================================
  77. (use-package emacs
  78. :ensure nil
  79. :custom
  80. (tab-always-indent 'complete)
  81. (TEXT-mode-ispell-word-completion nil)
  82. (read-extended-command-predicate #'command-completion-default-include-p)
  83. )
  84. (use-package corfu
  85. :init
  86. (global-corfu-mode)
  87. (corfu-popupinfo-mode)
  88. :custom
  89. (corfu-auto t)
  90. (corfu-auto-delay 0.2)
  91. (corfu-auto-prefix 2)
  92. (corfu-popupinfo-delay '(0.5 . 0.3)))
  93. (use-package vertico
  94. :config
  95. :init
  96. (vertico-mode))
  97. (use-package mason
  98. :config
  99. :init
  100. (mason-ensure))
  101. (use-package marginalia
  102. :config
  103. :init
  104. (marginalia-mode))
  105. (use-package orderless
  106. :custom
  107. (completion-styles '(orderless basic))
  108. (completion-category-overrides '((file (styles partial-completion))))
  109. (completion-pcm-leading-wildcard t)) ;; Emacs 31: partial-completion behaves like substring
  110. (use-package meow
  111. :init (setq meow--kbd-kill-region "C-S-x")
  112. :config
  113. (setq meow-use-clipboard nil)
  114. (meow-setup)
  115. (meow-global-mode 1)
  116. (setq meow-use-clipboard t))
  117. (use-package avy
  118. :config
  119. (setq avy-timeout-seconds 0.25
  120. avy-style 'at-full
  121. avy-keys (number-sequence ?a ?z)))
  122. (use-package treesit-auto
  123. :custom
  124. (treesit-auto-install 'prompt)
  125. :config
  126. (treesit-auto-add-to-auto-mode-alist 'all)
  127. (global-treesit-auto-mode)
  128. )
  129. (use-package treesit-fold
  130. :commands (treesit-fold-close
  131. treesit-fold-close-all
  132. treesit-fold-open
  133. treesit-fold-toggle
  134. treesit-fold-open-all
  135. treesit-fold-mode
  136. global-treesit-fold-mode
  137. treesit-fold-open-recursively
  138. treesit-fold-line-comment-mode)
  139. :custom
  140. (treesit-fold-line-count-show t)
  141. (treesit-fold-line-count-format " ▼")
  142. :config
  143. (set-face-attribute 'treesit-fold-replacement-face nil
  144. :foreground "#808080"
  145. :box nil
  146. :weight 'bold))
  147. ;; Systems and General Purpose
  148. (add-hook 'c-ts-mode-hook #'treesit-fold-mode)
  149. (add-hook 'c++-ts-mode-hook #'treesit-fold-mode)
  150. (add-hook 'java-ts-mode-hook #'treesit-fold-mode)
  151. (add-hook 'rust-ts-mode-hook #'treesit-fold-mode)
  152. (add-hook 'go-ts-mode-hook #'treesit-fold-mode)
  153. (add-hook 'ruby-ts-mode-hook #'treesit-fold-mode)
  154. ;; Web and Frontend
  155. (add-hook 'js-ts-mode-hook #'treesit-fold-mode)
  156. (add-hook 'typescript-ts-mode-hook #'treesit-fold-mode)
  157. (add-hook 'tsx-ts-mode-hook #'treesit-fold-mode)
  158. (add-hook 'css-ts-mode-hook #'treesit-fold-mode)
  159. (add-hook 'html-ts-mode-hook #'treesit-fold-mode)
  160. ;; Scripting and Infrastructure
  161. (add-hook 'bash-ts-mode-hook #'treesit-fold-mode)
  162. (add-hook 'cmake-ts-mode-hook #'treesit-fold-mode)
  163. (add-hook 'dockerfile-ts-mode-hook #'treesit-fold-mode)
  164. ;; Data and Configuration
  165. (add-hook 'json-ts-mode-hook #'treesit-fold-mode)
  166. (add-hook 'toml-ts-mode-hook #'treesit-fold-mode)
  167. (add-hook 'emacs-lisp-mode-hook #'outline-minor-mode)
  168. (add-hook 'lisp-interaction-mode-hook #'hs-minor-mode) ; scratch
  169. (add-hook 'lisp-mode-hook #'outline-minor-mode)
  170. (add-hook 'conf-mode-hook #'outline-minor-mode)
  171. (add-hook 'markdown-mode-hook #'outline-minor-mode)
  172. (add-hook 'diff-mode-hook #'outline-minor-mode)
  173. (use-package highlight-quoted
  174. :hook (emacs-lisp-mode . highlight-quoted-mode))
  175. (use-package highlight-defined
  176. :hook (emacs-lisp-mode . highlight-defined-mode))
  177. (use-package color-identifiers-mode
  178. :hook (emacs-lisp-mode . color-identifiers-mode))
  179. (use-package rainbow-delimiters
  180. :hook
  181. (prog-mode . rainbow-delimiters-mode)
  182. (inferior-ess-mode . rainbow-delimiters-mode))
  183. (use-package nerd-icons-completion
  184. :after marginalia
  185. :config
  186. (nerd-icons-completion-mode)
  187. (add-hook 'marginalia-mode-hook #'nerd-icons-completion-marginalia-setup))
  188. (use-package nerd-icons-corfu)
  189. (use-package nerd-icons-dired
  190. :hook
  191. (dired-mode . nerd-icons-dired-mode))
  192. (use-package smartparens
  193. :init (smartparens-global-mode 1)
  194. :config
  195. (sp-pair "\"" "\"" :wrap "C-c \"")
  196. (sp-pair "'" "'" :wrap "C-c '")
  197. (sp-pair "<<" ">>" :wrap "C-c <"))
  198. (use-package indent-bars
  199. :config
  200. (setq indent-bars-prefer-character ?.
  201. indent-bars-pattern "true"
  202. indent-bars-width-frac 1
  203. indent-bars-pad-frac 0.2
  204. indent-bars-color-by-depth '(:regexp "outline-\\([0-9]+\\)" :blend 0.9)
  205. indent-bars-highlight-current-depth '(:face default :blend 0.4)
  206. )
  207. :init
  208. (add-hook 'prog-mode-hook #'indent-bars-mode))
  209. (use-package kirigami
  210. :commands (kirigami-open-fold kirigami-open-fold-rec kirigami-close-fold
  211. kirigami-toggle-fold kirigami-open-folds
  212. kirigami-close-folds-except-current
  213. kirigami-close-folds))
  214. (use-package clipetty
  215. :hook (after-init . global-clipetty-mode))
  216. (use-package denote
  217. :config
  218. (setq denote-directory (list (expand-file-name "~/lyfi/SOR/Notes/"))
  219. denote-file-type 'markdown-yaml
  220. denote-prompts '(title subdirectory keywords)))
  221. (use-package colorful-mode
  222. :init (setq colorful-allow-mouse-clicks nil))
  223. ;; Tree-sitter & LSP tweaks
  224. (with-eval-after-load 'treesit
  225. (setq treesit-font-lock-level 4))
  226. (with-eval-after-load 'typescript-ts-mode
  227. (setq typescript-ts-mode-indent-offset 2)
  228. (defvaralias 'typescript-indent-level 'typescript-ts-mode-indent-offset))
  229. (with-eval-after-load 'lsp-mode
  230. (setq lsp-completion-provider :none))
  231. ;; ============================================================
  232. ;; Functions
  233. ;; ============================================================
  234. (defun my/system-copy (beg end)
  235. "Copy region directly to system clipboard, bypassing Emacs' kill ring.
  236. Uses wl-copy on Wayland GUI, and Clipetty (OSC 52) in TUI/SSH."
  237. (interactive "r")
  238. (unless (region-active-p)
  239. (user-error "No active region to copy"))
  240. (let ((text (buffer-substring-no-properties beg end)))
  241. (if (display-graphic-p)
  242. ;; GUI: Pipe text directly to Wayland clipboard
  243. (let ((process-connection-type nil))
  244. (let ((proc (start-process "wl-copy" nil "wl-copy")))
  245. (process-send-string proc text)
  246. (process-send-eof proc)))
  247. ;; TUI/SSH: Use clipetty to send OSC 52 sequence to kitty
  248. (if (fboundp 'clipetty-set-selection)
  249. (clipetty-set-selection text)
  250. (error "Clipetty not loaded. Ensure (global-clipetty-mode) is active")))
  251. (deactivate-mark)
  252. (message "Copied to system clipboard")))
  253. (defun my/system-cut (beg end)
  254. "Cut region directly to system clipboard, bypassing the kill ring."
  255. (interactive "r")
  256. (my/system-copy beg end)
  257. ;; delete-region deletes without adding to the kill ring
  258. (delete-region beg end))
  259. (defun my/system-paste ()
  260. "Paste from system clipboard directly to point, bypassing the kill ring."
  261. (interactive)
  262. (if (display-graphic-p)
  263. ;; GUI: Read directly from Wayland clipboard and insert
  264. (let ((text (shell-command-to-string "wl-paste -n")))
  265. (insert text))
  266. ;; TUI/SSH: Warn the user to use the terminal's native paste
  267. (message "In a terminal, use your terminal emulator's native paste shortcut (e.g., Ctrl-Shift-V).")))
  268. (defun dired-follow-symlink ()
  269. "In dired, visit the file or directory at point by its true physical path."
  270. (interactive)
  271. (let ((filename (dired-get-file-for-visit)))
  272. (if filename
  273. (find-file (file-truename filename))
  274. (message "No file at point"))))
  275. (defun my/vterm-sync-here ()
  276. "Switch to vterm and instantly 'cd' to the current buffer's directory."
  277. (interactive)
  278. (let ((cwd default-directory))
  279. (vterm-toggle)
  280. (vterm-send-string (format "cd %s" (shell-quote-argument cwd)))
  281. (vterm-send-return)))
  282. (defun my/delete-word-backward ()
  283. "Delete the word and any preceding whitespace without affecting the kill ring."
  284. (interactive)
  285. (let ((start (point)))
  286. (skip-chars-backward "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
  287. (skip-chars-backward " \t")
  288. (delete-region (point) start)))
  289. (defun my-modern-delete-word-backward ()
  290. "Delete a block of similar characters backward without using the kill ring.
  291. Similar characters are grouped as:
  292. 1. Whitespace (including newlines)
  293. 2. Alphanumerics
  294. 3. Special characters/symbols"
  295. (interactive)
  296. (let ((end (point))
  297. (start (1- (point))))
  298. (when (>= start 1)
  299. (let* ((first-char (char-before end))
  300. (first-type (my-get-char-type first-char)))
  301. ;; Move start point backward as long as the character type matches
  302. (while (and (>= start 1)
  303. (string= first-type (my-get-char-type (char-before start))))
  304. (setq start (1- start)))
  305. ;; Delete the range without adding to the kill ring
  306. (delete-region start end)))))
  307. (defun my-get-char-type (char)
  308. "Helper to categorize characters into three groups."
  309. (let ((cat (char-category char)))
  310. (cond
  311. ((member cat '("space" "newline")) "whitespace")
  312. ((member cat '("letter" "number")) "alphanum")
  313. (t "special"))))
  314. ;; Bind it to Ctrl-Backspace
  315. ;; Note: In many terminals, C-Backspace is sent as C-h or C-Delete.
  316. ;; This binding works in GUI Emacs.
  317. (global-set-key (kbd "C-<backspace>") 'my-modern-delete-word-backward)
  318. ;; ============================================================
  319. ;; Theming, ui
  320. ;; ============================================================
  321. ;;
  322. (my/apply-theme (selected-frame))
  323. ;; Apply to all future frames (important for emacsclient users)
  324. (add-hook 'after-make-frame-functions 'my/apply-theme)
  325. ;;fonts
  326. (setq doom-font "Fira Code 12")
  327. (setq doom-variable-pitch-font "Noto Sans 14")
  328. (setq nerd-icons-font-family "Symbols Nerd Font Mono")
  329. (defun my-set-buffer-font ()
  330. "Set a specific font for the current buffer only."
  331. (buffer-face-set '(:family "Noto Sans" :height 140)))
  332. (add-hook 'after-make-frame-functions #'my/apply-theme)