1 ;; get rid of custom from my init file
2 (setq custom-file "~/.emacs.d/emacs-custom.el")
5 (setq-default abbrev-mode t)
7 (expand-file-name "~/dotsnew/emacs/abbrev_defs"))
9 (defconst op/backup-dir
10 (expand-file-name "backups" user-emacs-directory))
11 (unless (file-exists-p op/backup-dir)
12 (make-directory op/backup-dir))
13 (setq backup-directory-alist `(("." . ,op/backup-dir)))
15 (setq use-dialog-box nil
17 require-final-newline t
21 enable-recursive-minibuffers t
24 next-error-message-highlight t
25 read-minibuffer-restore-windows nil
26 isearch-allow-motion t
27 calc-make-windows-dedicated t
28 user-mail-address "op@omarpolo.com")
30 (setq completion-ignore-case t
31 read-file-name-completion-ignore-case t
32 read-buffer-completion-ignore-case t)
34 ;; I don't like how compile uses `make -k' by default, I want to stop
35 ;; on errors / warnings.
36 (setq compile-command "make")
38 ;; "diff refinement", i.e. highlighting the changes in a more granular
39 ;; way, is quite awful to have it enabled by default. sometimes is
40 ;; useful, but for me it's more of a visual noise most of the times.
41 (setq diff-refine nil)
43 ;; disable also the syntax highlighting in the diff buffers
44 (setq diff-font-lock-syntax nil)
46 (define-key global-map (kbd "C-x C-b") #'ibuffer)
47 (define-key global-map (kbd "M-g i") #'imenu)
50 "Just like `imenu', but always flattened!"
54 (define-key minibuffer-mode-map (kbd "C-w") #'backward-kill-word)
56 ;; niceties for the standard completion
57 (setq completion-auto-help 'always
58 completion-auto-select 'second-tab
59 completions-max-height 10
60 completions-format 'horizontal
61 completions-header-format ""
62 completion-show-help nil)
64 (define-key minibuffer-local-map (kbd "C-p") #'minibuffer-previous-completion)
65 (define-key minibuffer-local-map (kbd "C-n") #'minibuffer-next-completion)
67 (define-key completion-in-region-mode-map (kbd "C-p")
68 #'minibuffer-previous-completion)
69 (define-key completion-in-region-mode-map (kbd "C-n")
70 #'minibuffer-next-completion)
72 (define-key completion-in-region-mode-map (kbd "M-RET")
73 #'minibuffer-choose-completion)
75 (defun op/reverse-other-window ()
76 "Like `other-window', but reverse."
79 (define-key global-map (kbd "C-x O") #'op/reverse-other-window)
81 (define-key global-map (kbd "C-x v f") #'vc-pull)
83 (setq uniquify-buffer-name-style 'forward
84 uniquify-strip-common-suffix t)
86 (setq-default scroll-up-aggressively 0.0
87 scroll-down-aggressively 0.0
88 scroll-preserve-screen-position t
89 next-screen-context-lines 1)
91 (define-key global-map (kbd "M-Z") #'zap-up-to-char)
94 (setq whitespace-style '(face trailing)
95 backward-delete-char-untabify-method 'hungry
96 tab-always-indent 'complete
98 sentence-end-double-space t)
99 (setq-default indent-tabs-mode t)
101 (defun op/enable-tabs ()
102 "Enable `indent-tabs-mode' in the current buffer."
104 (setq-local indent-tabs-mode t))
106 (defun op/disable-tabs ()
107 "Disable `indent-tabs-mode' in the current buffer."
109 (setq-local indent-tabs-mode nil))
111 (add-hook 'conf-mode-hook #'op/enable-tabs)
112 (add-hook 'text-mode-hook #'op/enable-tabs)
113 (add-hook 'prog-mode-hook #'op/enable-tabs)
114 (add-hook 'prog-mode-hook #'whitespace-mode)
115 (add-hook 'text-mode-hook #'whitespace-mode)
117 (dolist (hook '(emacs-lisp-mode-hook
119 clojurescript-mode-hook
122 (add-hook hook #'op/disable-tabs))
124 (with-eval-after-load 'log-edit
125 (add-hook 'log-edit-mode #'auto-fill-mode))
127 ;; free the c-z binding
128 (define-key global-map (kbd "C-z") nil)
129 (define-key global-map (kbd "C-z V") #'variable-pitch-mode)
130 (define-key global-map (kbd "C-z n") #'display-line-numbers-mode)
132 (define-key global-map (kbd "M-SPC") #'cycle-spacing)
133 (define-key global-map (kbd "M-u") #'upcase-dwim)
134 (define-key global-map (kbd "M-l") #'downcase-dwim)
135 (define-key global-map (kbd "M-c") #'capitalize-dwim)
138 ;; "-misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-iso10646-1"
141 (set-frame-font font nil t)
142 (add-to-list 'default-frame-alist `(font . ,font)))
144 ;; fix the emojis too
145 (set-fontset-font t 'emoji '("Noto Emoji" . "iso10646-1")
151 (setq history-delete-duplicates t
153 savehist-save-minibuffer-history t)
154 (electric-pair-mode +1)
156 (define-key global-map (kbd "M-/") #'hippie-expand)
157 (setq hippie-expand-try-functions-list
159 try-expand-dabbrev-all-buffers
160 try-expand-dabbrev-from-kill
161 try-complete-file-name-partially
162 try-complete-file-name
163 try-expand-all-abbrevs
166 try-complete-lisp-symbol-partially
167 try-complete-lisp-symbol))
169 (setq isearch-lazy-count t
170 search-whitespace-regexp ".*?"
171 isearch-allow-scroll 'unlimited)
173 (defun op/buffer-to-side-window (place)
174 "Place the current buffer in the side window at PLACE."
175 (interactive (list (intern
176 (completing-read "Which side: "
177 '(top left right bottom)))))
178 (let ((buf (current-buffer)))
179 (display-buffer-in-side-window
180 buf `((window-height . 0.15)
183 (window-parameters . ((no-delete-other-windows . t)))))
186 (defun op/visit-new-migration-file (name)
187 "Visit a new SQL migration file named after NAME."
188 (interactive "Mname: ")
189 (let* ((name (replace-regexp-in-string " " "-" (string-trim name)))
190 (f (format "%s-%s.sql"
191 (format-time-string "%Y%m%d%H%M")
195 (defun op/fill-or-unfill (fn &optional justify region)
196 "Meant to be an adviced :around `fill-paragraph'.
197 FN is the original `fill-column'. If `last-command' is
198 `fill-paragraph', unfill it, fill it otherwise. Inspired from a
199 post on endless parentheses. Optional argument JUSTIFY and
200 REGION are passed to `fill-paragraph'."
202 (if (eq last-command 'fill-paragraph)
203 (progn (setq this-command nil)
206 (funcall fn justify region)))
207 (advice-add 'fill-paragraph :around #'op/fill-or-unfill)
209 (defmacro op/deftranspose (name scope key doc)
210 "Macro to produce transposition functions.
211 NAME is the function's symbol. SCOPE is the text object to
212 operate on. Optional DOC is the function's docstring.
214 Transposition over an active region will swap the object at
215 mark (region beginning) with the one at point (region end).
217 It can optionally define a key for the defined function in the
218 `global-map' if KEY is passed.
220 Originally from protesilaos' dotemacs."
221 (declare (indent defun))
226 (let ((x (intern (format "transpose-%s" ,scope))))
231 `(define-key global-map (kbd ,key) #',name))))
233 (op/deftranspose op/transpose-lines "lines" "C-x C-t"
234 "Transpose lines or swap over active region.")
236 (op/deftranspose op/transpose-paragraphs "paragraphs" "C-S-t"
237 "Transpose paragraph or swap over active region.")
239 (op/deftranspose op/transpose-sentences "sentences" "C-x M-t"
240 "Transpose sentences or swap over active region.")
242 (op/deftranspose op/transpose-sexps "sexps" "C-M-t"
243 "Transpose sexps or swap over active region.")
245 (op/deftranspose op/transpose-words "words" "M-t"
246 "Transpose words or swap over active region.")
248 (defun op/narrow-or-widen-dwim (p)
249 "Widen if the buffer is narrowed, narrow-dwim otherwise.
250 Dwim means: region, org-src-block, org-subtree or defun,
251 whichever applies first. Narrowing to org-src-blocks actually
252 calls `org-edit-src-code'.
254 With prefix P, don't widen, just narrow even if buffer is already
255 narrowed. With P being -, narrow to page instead of to defun.
257 Taken from endless parentheses."
259 (declare (interactive-only))
260 (cond ((and (buffer-narrowed-p) (not p)) (widen))
262 (narrow-to-region (region-beginning)
264 ((derived-mode-p 'org-mode)
265 ;; `org-edit-src-code' isn't a real narrowing
266 (cond ((ignore-errors (org-edit-src-code) t))
267 ((ignore-errors (org-narrow-to-block) t))
268 (t (org-narrow-to-subtree))))
269 ((eql p '-) (narrow-to-page))
270 (t (narrow-to-defun))))
272 (define-key global-map (kbd "C-c w") #'op/narrow-or-widen-dwim)
274 (with-eval-after-load 'dired
275 (add-hook 'dired-mode-hook #'dired-hide-details-mode)
276 (add-hook 'dired-mode-hook #'dired-omit-mode)
278 (define-key dired-mode-map (kbd "C-c w") #'wdired-change-to-wdired-mode)
281 (setq dired-listing-switches "-lahF"
283 dired-deletion-confirmer #'y-or-n-p
284 dired-do-revert-buffer t))
286 ;; just like telescope!
287 (with-eval-after-load 'diff-mode
288 (define-key diff-mode-map (kbd "M-SPC") #'scroll-down-command))
290 (with-eval-after-load 'elisp-mode
291 (add-hook 'emacs-lisp-mode-hook #'checkdoc-minor-mode)
292 (add-hook 'emacs-lisp-mode-hook #'prettify-symbols-mode)
293 (let ((map emacs-lisp-mode-map))
294 (define-key map (kbd "C-c C-k") #'eval-buffer)
295 (define-key map (kbd "C-c k") #'op/ert-all)
296 (define-key map (kbd "C-c C-z") #'op/ielm-repl)))
298 (with-eval-after-load 'help
299 (add-hook 'help-mode-hook #'visual-line-mode))
302 (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
304 ;; packages that i want to be installed
305 (dolist (pkg '(vc-got eglot sly cider go-mode web-mode lua-mode
306 markdown-mode yaml-mode gemini-mode
307 form-feed shackle puni orderless))
308 (unless (package-installed-p pkg)
309 (message "Installing %s" pkg)
310 (package-install pkg)))
312 (global-form-feed-mode +1)
314 (add-hook 'text-mode-hook #'puni-mode)
315 (add-hook 'prog-mode-hook #'puni-mode)
316 (define-key puni-mode-map (kbd "C-)") #'puni-slurp-forward)
317 (define-key puni-mode-map (kbd "C-(") #'puni-barf-forward)
319 ;;(setq completion-styles '(basic substring initials flex partial-completion))
320 (setq completion-styles '(orderless basic)
321 completion-category-overrides '((file (styles basic partial-completion))))
323 (setq completions-detailed t)
325 (with-eval-after-load 'cider
326 (define-key cider-repl-mode-map (kbd "C-c M-o") #'cider-repl-clear-buffer))
328 (with-eval-after-load 'go-mode
329 (add-hook 'go-mode-hook #'subword-mode))
331 (with-eval-after-load 'eglot
332 (define-key eglot-mode-map (kbd "<f1>") #'eglot-code-actions)
333 (define-key eglot-mode-map (kbd "<f2>") #'eglot-format)
334 (add-to-list 'eglot-ignored-server-capabilites
335 :documentHighlightProvider)
336 (add-to-list 'eglot-server-programs
337 '(c-mode . ("clangd" "--header-insertion=never"))))
339 (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
340 (with-eval-after-load 'web-mode
341 (setq web-mode-markup-indent-offset 2
342 web-mode-css-indent-offset 2
343 web-mode-style-padding 0
344 web-mode-enable-engine-detection t)
345 (add-hook 'web-mode-hook #'op/disable-tabs)
347 ;; fix .dir-locals.el
348 (defun op/web-mode-fix-dir-locals ()
349 (when (derived-mode-p major-mode 'web-mode)
350 (web-mode-guess-engine-and-content-type)))
351 (add-hook 'hack-local-variables-hook #'op/web-mode-fix-dir-locals)
353 (setq web-mode-enable-auto-pairing nil))
355 (with-eval-after-load 'css-mode
356 (add-hook 'css-mode-hook #'op/disable-tabs))
358 (with-eval-after-load 'flymake
359 (define-key prog-mode-map (kbd "C-c ! n") #'flymake-goto-next-error)
360 (define-key prog-mode-map (kbd "C-c ! p") #'flymake-goto-prev-error))
362 (with-eval-after-load 'cc-mode
363 (setq c-basic-offset 8
364 c-default-style "K&R")
365 (dolist (hook '(c-mode-hook c++-mode-hook))
366 (add-hook hook #'abbrev-mode)
367 (add-hook hook #'subword-mode))
368 (defun op/c-indent ()
370 (c-set-offset 'arglist-intro '+)
371 (c-set-offset 'arglist-cont-nonempty '*)
372 (c-set-offset 'label 1))
373 (add-hook 'c-mode-hook #'op/c-indent)
375 (defun op/c-add-include (path &optional localp)
376 "Include PATH at the start of the file.
377 If LOCALP is non-nil, the include will be \"local\"."
378 (interactive "Mheader to include: \nP")
381 "^#[ \t]*include[ \t]*\""
382 "^#[ \t]*include[ \t]*<"))
383 (ignore-re "^#include \"compat.h\"")
385 (goto-char (point-min))
386 (while (not (or (and (looking-at re)
387 (not (looking-at ignore-re)))
391 (error "Don't know where to insert the header"))
393 (insert "#include " (if localp "\"\"" "<>"))
396 (move-beginning-of-line 1)
399 (while (and (looking-at re)
402 (sort-lines nil start (point)))))
403 (define-key c-mode-map (kbd "C-c C-a") #'op/c-add-include))
405 (with-eval-after-load 'perl-mode
406 (setq perl-indent-level 8))
408 (with-eval-after-load 'sh-script
409 (setq sh-basic-offset 8
410 sh-indent-after-loop-construct 8
411 sh-indent-after-continuation nil))
415 (setq eshell-hist-ignoredups t)
417 (defun op/eshell-bufname (dir)
418 (concat "*eshell " (expand-file-name dir) "*"))
420 (defun op/eshell (arg)
421 "Run or jump to eshell in current project.
422 If called with prefix argument ARG always create a new eshell
425 (let* ((proj (project-current))
426 (dir (if (and proj (not arg))
429 (default-directory dir)
430 (eshell-buffer-name (let ((name (op/eshell-bufname dir)))
432 (generate-new-buffer name)
435 (define-key global-map (kbd "C-c e") #'op/eshell)
437 (with-eval-after-load 'eshell
438 (setq eshell-save-history-on-exit t
439 eshell-history-size 1024
441 eshell-compl-dir-ignore
442 "\\`\\(\\.\\.?\\|CVS\\|\\.svn\\|\\.git\\|\\.got\\)/\\'")
444 (defun op/eshell-after-cd (&rest _)
445 (rename-buffer (op/eshell-bufname default-directory) t))
447 (advice-add #'eshell/cd :after #'op/eshell-after-cd))
449 (with-eval-after-load 'esh-mode
450 (defun op/clear-eshell ()
452 (let ((inhibit-read-only t))
454 (eshell-send-input)))
456 (define-key eshell-command-map (kbd "M-o") #'op/clear-eshell))
460 (unless (package-installed-p 'sndio)
461 (package-install-file "~/w/sndio.el/sndio.el"))
465 (unless (package-installed-p 'saturn)
466 (package-install-file "~/w/saturn/GUI/saturn.el"))
470 (add-to-list 'load-path "~/build/emacs-libpq/")
471 (unless (package-installed-p 'pq)
472 (package-install-file "~/build/emacs-libpq/pq.el"))
476 (unless (package-installed-p 'plass)
478 (package-install-file "~/w/plass/plass.el")))
480 (with-eval-after-load 'plass
481 (setq plass-filter "!2fa"
482 plass-totp-filter "2fa"))
486 (setq shackle-default-rule nil
488 (let ((repls "\\*\\(cider-repl\\|sly-mrepl\\|ielm\\)")
489 (godot "\\*godot - .*\\*")
490 (vcs "\\*\\(Flymake\\|Package-Lint\\|vc-\\(git\\|got\\) :\\).*")
491 (elfeed "\\*elfeed-entry\\*")
492 (vmd "\\*vmd console .*"))
493 `(("*Async Shell Command*" :ignore t)
500 (occur-mode :select t
503 (diff-mode :select t)
521 ;; (setq display-buffer-alist nil)