changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / emacs/default.el

changeset 698: 96958d3eb5b0
parent: 38e9c3be2392
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 ;;; default.el --- default config -*- lexical-binding: t -*-
2 
3 ;;; Code:
4 ;;; Settings
5 (require 'util)
6 (put 'upcase-region 'disabled nil)
7 (put 'list-threads 'disabled nil)
8 (put 'list-timers 'disabled nil)
9 (setq show-paren-context-when-offscreen 'overlay)
10 (setopt
11  org-safe-remote-resources '("\\`https://cdn\\.compiler\\.company/org/clean\\.theme\\'")
12  ;; tabs = bad (unless in makefile..)
13  indent-tabs-mode nil
14  make-backup-files nil
15  auto-save-list-file-prefix (expand-file-name "auto-save/." user-emacs-directory)
16  tramp-auto-save-directory (expand-file-name "auto-save/tramp/" user-emacs-directory)
17  dired-free-space nil
18  mml-attach-file-at-the-end t
19  dired-mouse-drag-files t
20  confirm-kill-emacs nil
21  confirm-kill-processes nil
22  use-short-answers t
23  display-time-format "%Y-%m-%d %H:%M"
24  ring-bell-function 'ignore
25  completion-ignore-case t
26  ;; NOTE 2023-11-04: you need to add the following lines to ~/.gnupg/gpg-agent.conf:
27  ;; allow-emacs-pinentry
28  ;; allow-loopback-pinentry
29  epg-pinentry-mode 'loopback
30  shr-use-colors nil
31  shr-use-fonts nil
32  shr-max-image-proportion 0.6
33  shr-image-animate nil
34  shr-discard-aria-hidden t
35  bookmark-default-file (expand-file-name "bookmarks" user-emacs-directory)
36  set-mark-command-repeat-pop t
37  tempo-interactive t
38  emms-directory (expand-file-name "emms" user-emacs-directory)
39  gnus-cache-directory (expand-file-name "gnus" user-emacs-directory)
40  url-cache-directory (expand-file-name "url" user-emacs-directory)
41  tab-always-indent 'complete
42  shr-cookie-policy nil
43  ;; NOTE 2023-11-04: EXPERIMENTAL
44  ediff-floating-control-frame t
45  register-use-preview nil
46  shr-use-xwidgets-for-media t
47  which-key-mode t
48  view-read-only t)
49 
50 ;;; Treesitter
51 
52 ;;(add-to-list 'treesit-extra-load-path "/usr/local/lib/")
53 
54 ;; (let ((grammar-dir "/usr/local/share/tree-sitter/"))
55 ;; (when (file-exists-p grammar-dir)
56 ;; (setq treesit-extra-load-path
57 ;; (append
58 ;; (flatten
59 ;; (mapcar
60 ;; (lambda (f)
61 ;; (unless (or (string= "." f) (string= ".." f))
62 ;; (concat grammar-dir f)))
63 ;; (directory-files "/usr/local/share/tree-sitter")))
64 ;; treesit-extra-load-path))))
65 
66 ;;; Variables
67 (defvar user-emacs-lib-directory (expand-file-name (join-paths user-emacs-directory "lib")))
68 (defvar user-custom-file (expand-file-name (format "%s.el" user-login-name) user-emacs-directory))
69 (defvar user-home-directory (expand-file-name "~"))
70 (defvar user-lab-directory (expand-file-name "lab" user-home-directory))
71 (defvar user-stash-directory (expand-file-name ".stash" user-home-directory))
72 (defvar user-store-directory (expand-file-name ".store" user-home-directory))
73 (defvar user-mail-directory (expand-file-name "mail" user-home-directory))
74 (defvar user-org-stash-directory (expand-file-name "org" user-stash-directory))
75 (defvar default-theme 'leuven-dark)
76 (defvar company-source-directory (join-paths user-home-directory "comp"))
77 (defvar company-org-directory (join-paths company-source-directory "org"))
78 (defvar company-domain "compiler.company")
79 (defvar company-name "The Compiler Company, LLC")
80 (defvar company-vc-domain "vc.compiler.company")
81 (defvar company-home "the.compiler.company")
82 (defvar company-cdn-url "https://cdn.compiler.company")
83 
84 (add-to-load-path user-emacs-lib-directory)
85 
86 ;;; Theme
87 (defun load-default-theme (&optional theme)
88  (interactive)
89  (when theme (setq default-theme theme))
90  (load-theme default-theme))
91 
92 ;;; Packages
93 (with-eval-after-load 'package
94  (setq package-archives
95  '(("gnu" . "https://elpa.gnu.org/packages/")
96  ("nongnu" . "https://elpa.nongnu.org/nongnu/")
97  ("melpa" . "https://melpa.org/packages/")))
98  (setopt
99  use-package-always-ensure t
100  use-package-expand-minimally t)
101  (add-packages
102  ;; eglot-x ;; LSP extensions
103  org-web-tools ;; web parsing
104  citeproc ;; citations
105  htmlize ;; html export
106  ;; all-the-icons all-the-icons-dired all-the-icons-ibuffer ;; icons
107  nerd-icons nerd-icons-dired nerd-icons-ibuffer nerd-icons-corfu nerd-icons-completion
108  hide-mode-line) ;; ui
109  ;; bbdb
110  (package-install-selected-packages t))
111 
112 ;;; Env
113 (require 'exec-path-from-shell)
114 (exec-path-from-shell-copy-envs (list "SSH_AGENT_PID"
115  "SSH_AUTH_SOCK"
116  "PATH"
117  "CARGO_HOME"
118  "CC"
119  "LD"
120  "LD_LIBRARY_PATH"
121  "RUSTUP_HOME"
122  "QUICKLISP_HOME"
123  "DEV" "DEV_ID" "DEV_HOME"
124  "WORKER" "WORKER_ID" "WORKER_HOME"
125  "SBCL_HOME"
126  "STASH"
127  "STORE"
128  "LISP_HOME"))
129 
130 (add-to-list 'exec-path (expand-file-name "~/.cargo/bin/"))
131 (add-to-list 'exec-path (expand-file-name "~/.local/bin/"))
132 (add-to-list 'exec-path "/bin/")
133 (add-to-list 'exec-path "/usr/local/sbin/")
134 (add-to-list 'exec-path "/usr/local/bin/")
135 (add-to-list 'exec-path "/usr/local/share/lisp/bin/")
136 
137 ;;; Completions
138 (use-package cape :ensure t)
139 (use-package orderless
140  :ensure t
141  :init
142  (setq completion-styles '(orderless partial-completion basic)
143  completion-category-overrides '((file (styles basic partial-completion))
144  (eglot (styles orderless))
145  (eglot-capf (styles orderless)))))
146 
147 (use-package corfu
148  :ensure t
149  :config
150  (global-corfu-mode)
151  (corfu-popupinfo-mode)
152  (corfu-echo-mode)
153  (dolist (c (list (cons "SPC" " ")
154  (cons "." ".")
155  (cons "," ",")
156  (cons ":" ":")
157  (cons ")" ")")
158  (cons "}" "}")
159  (cons "]" "]")))
160  (define-key corfu-map (kbd (car c)) `(lambda ()
161  (interactive)
162  (corfu-insert)
163  (insert ,(cdr c)))))
164  ;; (add-to-list 'completion-at-point-functions #'cape-dabbrev t)
165  ;; (add-to-list 'completion-at-point-functions #'cape-abbrev t)
166  ;; (add-to-list 'completion-at-point-functions #'cape-file)
167  (defun corfu-move-to-minibuffer ()
168  (interactive)
169  (pcase completion-in-region--data
170  (`(,beg ,end ,table ,pred ,extras)
171  (let ((completion-extra-properties extras)
172  completion-cycle-threshold completion-cycling)
173  (consult-completion-in-region beg end table pred)))))
174  (keymap-set corfu-map "M-m" #'corfu-move-to-minibuffer)
175  (add-to-list 'corfu-continue-commands #'corfu-move-to-minibuffer)
176  (unless (display-graphic-p)
177  (use-package corfu-terminal :ensure t :config (corfu-terminal-mode 1))))
178 
179 (use-package kind-icon
180  :ensure t
181  :after corfu
182  ;:custom
183  ; (kind-icon-blend-background t)
184  ; (kind-icon-default-face 'corfu-default) ; only needed with blend-background
185  :config
186  (add-to-list 'corfu-margin-formatters #'kind-icon-margin-formatter))
187 
188 
189 (use-package vertico
190  :ensure t
191  :config (vertico-mode)
192  (keymap-set vertico-map "M-q" #'vertico-quick-insert)
193  (keymap-set vertico-map "C-q" #'vertico-quick-exit))
194 
195 (use-package marginalia :ensure t
196  :config (marginalia-mode))
197 (use-package embark
198  :ensure t)
199 (use-package embark-consult :ensure t)
200 (use-package consult :ensure t)
201 
202 ;;; Desktop
203 (setopt desktop-dirname (expand-file-name "sessions" user-emacs-directory))
204 
205 ;;; Multisession
206 (setq multisession-storage 'sqlite)
207 
208 ;;; Kill Ring
209 (kill-ring-deindent-mode)
210 
211 ;;; VC
212 ;; use rhg, fallback to hg. see hgrc
213 (if (file-exists-p "~/.local/bin/rhg")
214  (setq hg-binary "~/.local/bin/rhg"))
215 
216 ;;; Dired
217 ;;; Projects
218 (setopt project-list-file (expand-file-name "projects" user-emacs-directory)
219  project-mode-line t
220  project-file-history-behavior 'relativize)
221 
222 ;;; Tabs
223 (add-hook 'tab-bar-mode-hook #'tab-bar-history-mode)
224 
225 ;;; Lisp
226 (use-package company :ensure t)
227 
228 (setq inferior-lisp-program "sbcl --dynamic-space-size=8G --control-stack-size 8"
229  scheme-program-name "gsi"
230  guile-program "guile"
231  cmulisp-program "lisp"
232  scsh-program "scsh")
233 
234 (defvar slime-toggle nil)
235 (defun slime-toggle ()
236  "toggle between lisp file and slime-repl"
237  (interactive)
238  (cond
239  ((eq major-mode 'slime-repl-mode)
240  (setq slime-toggle (pop-to-buffer (or slime-toggle (read-buffer "lisp buffer: ")))))
241  ((not (eq major-mode 'slime-repl-mode))
242  (if (slime-connected-p)
243  (progn
244  (setq slime-toggle (current-buffer))
245  (slime-switch-to-output-buffer))
246  (setq slime-toggle (current-buffer))
247  (slime)))))
248 
249 (use-package slime
250  :ensure t
251  :config
252  (require 'slime-company "slime-company")
253  (require 'slime-cape "slime-cape")
254  (require 'slime-repl-ansi-color "slime-repl-ansi-color")
255  (setq slime-contribs '(slime-fancy
256  slime-quicklisp
257  slime-hyperdoc
258  ;; slime-listener-hooks
259  ;; slime-enclosing-context
260  ;; slime-media
261  ;; slime-mrepl
262  ;; slime-company
263  slime-sbcl-exts
264  slime-cape ;; ext
265  slime-repl-ansi-color
266  ;; slime-cl-indent
267  ;; slime-snapshot
268  slime-sprof
269  slime-tramp
270  ;; slime-typeout-frame
271  slime-xref-browser
272  slime-repl-ansi-color
273  ;; slime-highlight-edits
274  slime-asdf))
275  (put 'make-instance 'common-lisp-indent-function 1)
276  (put 'reinitialize-instance 'common-lisp-indent-function 1)
277  (slime-setup slime-contribs)
278  ;; X11-only (mcclim requires clx)
279  (defun clouseau-inspect (string)
280  "Inspect a lisp value with Clouseau. make sure to load clouseau
281 with a custom core or in your init file before using this
282 function: '(ql:quickload :clouseau)'."
283  (interactive
284  (list (slime-read-from-minibuffer
285  "Inspect value (evaluated): "
286  (slime-sexp-at-point))))
287  (let ((inspector 'cl-user::*clouseau-inspector*))
288  (slime-eval-async
289  `(cl:progn
290  (cl:defvar ,inspector nil)
291  ;; (Re)start the inspector if necessary.
292  (cl:unless (cl:and (clim:application-frame-p ,inspector)
293  (clim-internals::frame-process ,inspector))
294  (cl:setf ,inspector (cl:nth-value 1 (clouseau:inspect nil :new-process t))))
295  ;; Tell the inspector to visualize the correct datum.
296  (cl:setf (clouseau:root-object ,inspector :run-hook-p t)
297  (cl:eval (cl:read-from-string ,string)))
298  ;; Return nothing.
299  (cl:values)))))
300 
301  (define-common-lisp-style "core" "Core Common Lisp Indentation Style"
302  (:inherit "sbcl")
303  (:indentation
304  (defpkg (as defpackage))
305  (define-package (as defpackage))))
306 
307  ;; lisp font-lock defaults: https://www.n16f.net/blog/custom-font-lock-configuration-in-emacs/
308  ;; (defface cl-character-face
309  ;; '((default :inherit font-lock-constant-face))
310  ;; "The face used to highlight Common Lisp character literals.")
311 
312  ;; (defface cl-standard-function-face
313  ;; '((default :inherit font-lock-keyword-face))
314  ;; "The face used to highlight standard Common Lisp function symbols.")
315 
316  ;; (defface cl-standard-value-face
317  ;; '((default :inherit font-lock-variable-name-face))
318  ;; "The face used to highlight standard Common Lisp value symbols.")
319 
320  ;; (defvar cl-font-lock-keywords
321  ;; (let* ((character-re (concat "#\\\\" lisp-mode-symbol-regexp "\\_>"))
322  ;; (function-re (concat "(" (regexp-opt cl-function-names t) "\\_>"))
323  ;; (value-re (regexp-opt cl-value-names 'symbols)))
324  ;; `((,character-re . 'cl-character-face)
325  ;; (,function-re
326  ;; (1 'cl-standard-function-face))
327  ;; (,value-re . 'cl-standard-value-face))))
328 
329  (setq common-lisp-style-default "core")
330  ;; (define-key slime-prefix-map (kbd "i") 'clouseau-inspect)
331  (setq slime-threads-update-interval 1)
332  ;; (add-hook 'slime-mode-hook 'slime-cape-maybe-enable)
333  ;; (add-hook 'slime-repl-mode-hook 'slime-cape-maybe-enable)
334  )
335 
336 ;;; Eglot
337 ;; (with-eval-after-load 'eglot
338 ;; (unless (package-installed-p 'eglot-x)
339 ;; (package-vc-install '(eglot-x :url "https://vc.compiler.company/packy/eglot-x")))
340 ;; (require 'eglot-x)
341 ;; (with-eval-after-load 'eglot-x
342 ;; (add-to-list 'eglot-server-programs
343 ;; '((rust-ts-mode rust-mode) .
344 ;; ("rust-analyzer" :initializationOptions (:check (:command "clippy")))))
345 ;; (eglot-x-setup)))
346 
347 ;;; Rust
348 (add-hook 'rust-mode-hook 'eglot-ensure)
349 
350 (setq rust-rustfmt-switches nil
351  rust-indent-offset 2)
352 
353 ;;; Python
354 (setq python-indent-offset 2)
355 (add-hook 'python-mode-hook 'eglot-ensure)
356 
357 ;;; Javascript
358 (setq js-indent-level 2)
359 
360 ;;; Bash
361 (setq sh-basic-offset 2)
362 
363 ;;; Graphviz
364 ;; (use-package graphviz-dot-mode
365 ;; :ensure t
366 ;; :config
367 ;; (setq graphviz-dot-indent-width 2))
368 
369 ;;; Comments
370 (defcustom prog-comment-keywords
371  '("TODO" "REVIEW" "FIX" "HACK" "RESEARCH")
372  "List of strings with comment keywords."
373  :group 'default
374  :type 'list)
375 
376 (defcustom prog-comment-timestamp-format-concise "%F"
377  "Specifier for date in `prog-comment-timestamp-keyword'.
378 Refer to the doc string of `format-time-string' for the available
379 options."
380  :group 'default
381  :type 'string)
382 
383 (defcustom prog-comment-timestamp-format-verbose "%F %T %z"
384  "Like `prog-comment-timestamp-format-concise', but longer."
385  :group 'default
386  :type 'string)
387 
388 ;;;###autoload
389 (defun prog-comment-dwim (arg)
390  "Flexible, do-what-I-mean commenting.
391 
392 If region is active and ARG is either a numeric argument greater
393 than one or a universal prefix (\\[universal-argument]), then
394 apply `comment-kill' on all comments in the region.
395 
396 If the region is active and no ARG is supplied, or is equal to a
397 numeric prefix of 1, then toggle the comment status of the region.
398 
399 Else toggle the comment status of the line at point. With a
400 numeric prefix ARG, do so for ARGth lines (negative prefix
401 operates on the lines before point)."
402  (interactive "p")
403  (cond
404  ((and (> arg 1) (use-region-p))
405  (let* ((beg (region-beginning))
406  (end (region-end))
407  (num (count-lines beg end)))
408  (save-excursion
409  (goto-char beg)
410  (comment-kill num))))
411  ((use-region-p)
412  (comment-or-uncomment-region (region-beginning) (region-end)))
413  (t
414  (save-excursion (comment-line (or arg 1))))))
415 
416 (defvar prog-comment--keyword-hist '()
417  "Input history of selected comment keywords.")
418 
419 (defun prog-comment--keyword-prompt (keywords)
420  "Prompt for candidate among KEYWORDS."
421  (let ((def (car prog-comment--keyword-hist)))
422  (completing-read
423  (format "Select keyword [%s]: " def)
424  keywords nil nil nil 'prog-comment--keyword-hist def)))
425 
426 
427 ;;;###autoload
428 (defun prog-comment-timestamp-keyword (keyword &optional verbose)
429  "Add timestamped comment with KEYWORD.
430 
431 When called interactively, the list of possible keywords is that
432 of `prog-comment-keywords', though it is possible to
433 input arbitrary text.
434 
435 If point is at the beginning of the line or if line is empty (no
436 characters at all or just indentation), the comment is started
437 there in accordance with `comment-style'. Any existing text
438 after the point will be pushed to a new line and will not be
439 turned into a comment.
440 
441 If point is anywhere else on the line, the comment is indented
442 with `comment-indent'.
443 
444 The comment is always formatted as 'DELIMITER KEYWORD DATE:',
445 with the date format being controlled by the variable
446 `prog-comment-timestamp-format-concise'.
447 
448 With optional VERBOSE argument (such as a prefix argument
449 `\\[universal-argument]'), use an alternative date format, as
450 specified by `prog-comment-timestamp-format-verbose'."
451  (interactive
452  (list
453  (prog-comment--keyword-prompt prog-comment-keywords)
454  current-prefix-arg))
455  (let* ((date (if verbose
456  comment-timestamp-format-verbose
457  prog-comment-timestamp-format-concise))
458  (string (format "%s %s: " keyword (format-time-string date)))
459  (beg (point)))
460  (cond
461  ((or (eq beg (pos-bol))
462  (default-line-regexp-p 'empty))
463  (let* ((maybe-newline (unless (default-line-regexp-p 'empty 1) "\n")))
464  ;; NOTE 2021-07-24: we use this `insert' instead of
465  ;; `comment-region' because of a yet-to-be-determined bug that
466  ;; traps `undo' to the two states between the insertion of the
467  ;; string and its transformation into a comment.
468  (insert
469  (concat comment-start
470  ;; NOTE 2021-07-24: See function `comment-add' for
471  ;; why we need this.
472  (make-string
473  (comment-add nil)
474  (string-to-char comment-start))
475  comment-padding
476  string
477  comment-end))
478  (indent-region beg (point))
479  (when maybe-newline
480  (save-excursion (insert maybe-newline)))))
481  (t
482  (comment-indent t)
483  (insert (concat " " string))))))
484 
485 (setq hexl-bits 8)
486 (setq tab-width 4)
487 
488 ;;; Keyboard Macros
489 (defun toggle-macro-recording ()
490  (interactive)
491  (if defining-kbd-macro
492  (end-kbd-macro)
493  (start-kbd-macro nil)))
494 
495 (defun play-macro-if-not-playing ()
496  (interactive)
497  (if defining-kbd-macro
498  (end-kbd-macro)
499  (call-last-kbd-macro)))
500 
501 ;;; Registers
502 ;; - additional register vtypes: buffer
503 (defun decrement-register (number register)
504  "Subtract NUMBER from the contents of register REGISTER.
505 Interactively, NUMBER is the prefix arg."
506  (interactive "p\ncDecrement register: ")
507  (increment-register (- number) register))
508 
509 (defun copy-register (a b)
510  "Copy register A to B."
511  (interactive
512  (list (register-read-with-preview "From register: ")
513  (register-read-with-preview "To register: ")))
514  (set-register b (get-register a)))
515 
516 (defun buffer-to-register (register &optional delete)
517  "Put current buffer in register - this would also work for
518  just buffers, as switch-to-buffer can use both, but it
519  facilitates for easier saving/restoring of registers."
520  (interactive "cPut current buffername in register: \nP.")
521  (set-register register (cons 'buffer (buffer-name (current-buffer)))))
522 
523 (defun file-to-register (register &optional delete)
524  "This is better than put-buffer-in-register for file-buffers, because a closed
525  file can be opened again, but does not work for no-file-buffers."
526  (interactive "cPut the filename of current buffer in register: \nP")
527  (set-register register (cons 'file (buffer-file-name (current-buffer)))))
528 
529 (defun file-query-to-register (register &optional delete)
530  (interactive
531  (list
532  (register-read-with-preview "File query to register: ")))
533  (set-register register (list 'file-query (buffer-file-name (current-buffer)) (point))))
534 
535 ;; additional register-val handlers
536 ;; (cl-defmethod register-val-jump-to :around ((val cons) delete)
537 ;; (cond
538 ;; (t (cl-call-next-method val delete))))
539 
540 ;;; Outlines
541 (defun outline-hook (&optional rx)
542  "Enable `outline-minor-mode' and set `outline-regexp'."
543  (when rx (setq-local outline-regexp rx))
544  (outline-minor-mode 1))
545 
546 (setq outline-minor-mode-use-buttons nil)
547 
548 (defun add-outline-hook (mode &optional rx)
549  (let ((sym (symb mode "-hook")))
550  (add-hook sym (lambda () (outline-hook rx)))))
551 
552 (defmacro outline-hooks (&rest pairs)
553  `(mapc (lambda (x) (add-outline-hook (car x) (cadr x))) ',pairs))
554 
555 (outline-hooks (asm-mode ";;;+")
556  (nasm-mode ";;;+")
557  (rust-mode "\\(//!\\|////+\\)")
558  (sh-mode "###+")
559  (sh-script-mode "###+")
560  (makefile-mode "###+")
561  (conf-mode "###+")
562  (common-lisp-mode)
563  (emacs-lisp-mode)
564  (lisp-data-mode)
565  (org-mode)
566  (css-mode)
567  (html-mode)
568  (skel-mode))
569 
570 ;;; Scratch
571 (defcustom default-scratch-buffer-mode 'lisp-interaction-mode
572  "Default major mode for new scratch buffers"
573  :group 'default
574  :type 'symbol)
575 
576 ;; Adapted from the `scratch.el' package by Ian Eure.
577 (defun default-scratch-list-modes ()
578  "List known major modes."
579  (cl-loop for sym the symbols of obarray
580  for name = (symbol-name sym)
581  when (and (functionp sym)
582  (not (member sym minor-mode-list))
583  (string-match "-mode$" name)
584  (not (string-match "--" name)))
585  collect name))
586 
587 (defun default-scratch-buffer-setup (region &optional mode)
588  "Add contents to `scratch' buffer and name it accordingly.
589 
590 REGION is added to the contents to the new buffer.
591 
592 Use the current buffer's major mode by default. With optional
593 MODE use that major mode instead."
594  (let* ((major (or mode major-mode))
595  (string (format "Scratch buffer for: %s\n\n" major))
596  (text (concat string region))
597  (buf (format "*Scratch for %s*" major)))
598  (with-current-buffer (get-buffer-create buf)
599  (funcall major)
600  (save-excursion
601  (insert text)
602  (goto-char (point-min))
603  (comment-region (pos-bol) (pos-eol)))
604  (vertical-motion 2))
605  (pop-to-buffer buf)))
606 
607 ;;;###autoload
608 (defun default-scratch-buffer (&optional arg)
609  "Produce a bespoke scratch buffer matching current major mode.
610 
611 With optional ARG as a prefix argument (\\[universal-argument]),
612 use `default-scratch-buffer-mode'.
613 
614 With ARG as a double prefix argument, prompt for a major mode
615 with completion.
616 
617 If region is active, copy its contents to the new scratch
618 buffer."
619  (interactive "P")
620  (let* ((default-mode default-scratch-buffer-mode)
621  (modes (default-scratch-list-modes))
622  (region (with-current-buffer (current-buffer)
623  (if (region-active-p)
624  (buffer-substring-no-properties
625  (region-beginning)
626  (region-end))
627  "")))
628  (m))
629  (pcase (prefix-numeric-value arg)
630  (16 (progn
631  (setq m (intern (completing-read "Select major mode: " modes nil t)))
632  (default-scratch-buffer-setup region m)))
633  (4 (default-scratch-buffer-setup region default-mode))
634  (_ (default-scratch-buffer-setup region)))))
635 
636 ;;;###autoload
637 (defun scratch-new ()
638  "create a new scratch buffer. (could be *scratch* - *scratchN*)"
639  (interactive)
640  (let ((n 0)
641  bufname)
642  (while (progn
643  (setq bufname
644  (concat "*scratch"
645  (if (= n 0) "" (int-to-string n))
646  "*"))
647  (setq n (1+ n))
648  (get-buffer bufname)))
649  (switch-to-buffer (get-buffer-create bufname))
650  (insert initial-scratch-message)
651  (lisp-interaction-mode)))
652 
653 ;;; Shell
654 (defun set-no-process-query-on-exit ()
655  (let ((proc (get-buffer-process (current-buffer))))
656  (when (processp proc)
657  (set-process-query-on-exit-flag proc nil))))
658 
659 (add-hook 'shell-mode-hook 'set-no-process-query-on-exit)
660 (add-hook 'term-exec-hook 'set-no-process-query-on-exit)
661 
662 ;;; Eshell
663 (defun eshell-new()
664  "Open a new instance of eshell."
665  (interactive)
666  (eshell 'Z))
667 
668 (setq eshell-highlight-prompt t
669  eshell-hist-ignoredups t
670  eshell-save-history-on-exit t
671  eshell-prefer-lisp-functions nil
672  eshell-destroy-buffer-when-process-dies t)
673 
674 (add-hook 'eshell-mode-hook
675  (lambda ()
676  (eshell/alias "d" "dired $1")
677  (eshell/alias "ff" "find-file $1")
678  (eshell/alias "hgfe" "hg-fast-export.sh")))
679 
680 (defun eshell/clear ()
681  "Clear the eshell buffer."
682  (let ((inhibit-read-only t))
683  (erase-buffer)
684  (eshell-send-input)))
685 
686 (defun eshell-quit-or-delete-char (arg)
687  (interactive "p")
688  (if (and (eolp) (looking-back eshell-prompt-regexp))
689  (progn
690  (eshell-life-is-too-much) ; Why not? (eshell/exit)
691  (ignore-errors
692  (delete-window)))
693  (delete-forward-char arg)))
694 
695 (add-hook 'eshell-mode-hook
696  (lambda ()
697  (bind-keys :map eshell-mode-map
698  ("C-d" . eshell-quit-or-delete-char))))
699 
700 (defun eshell-next-prompt (n)
701  "Move to end of Nth next prompt in the buffer. See `eshell-prompt-regexp'."
702  (interactive "p")
703  (re-search-forward eshell-prompt-regexp nil t n)
704  (when eshell-highlight-prompt
705  (while (not (get-text-property (line-beginning-position) 'read-only) )
706  (re-search-forward eshell-prompt-regexp nil t n)))
707  (eshell-skip-prompt))
708 
709 (defun eshell-previous-prompt (n)
710  "Move to end of Nth previous prompt in the buffer. See `eshell-prompt-regexp'."
711  (interactive "p")
712  (backward-char)
713  (eshell-next-prompt (- n)))
714 
715 (defun eshell-insert-history ()
716  "Displays the eshell history to select and insert back into your eshell."
717  (interactive)
718  (insert (ido-completing-read "Eshell history: "
719  (delete-dups
720  (ring-elements eshell-history-ring)))))
721 
722 ;;; Eww
723 (setopt
724  browse-url-browser-function 'eww
725  eww-auto-rename-buffer 'title
726  eww-search-prefix "https://google.com/search?q=")
727 
728 ;; ref: https://github.com/oantolin/emacs-config/blob/master/my-lisp/shr-heading.el
729 (defun shr-heading-next (&optional arg)
730  "Move forward by ARG headings (any h1-h4).
731 If ARG is negative move backwards, ARG defaults to 1."
732  (interactive "p")
733  (unless arg (setq arg 1))
734  (catch 'return
735  (dotimes (_ (abs arg))
736  (when (> arg 0) (end-of-line))
737  (if-let ((match
738  (funcall (if (> arg 0)
739  #'text-property-search-forward
740  #'text-property-search-backward)
741  'face '(shr-h1 shr-h2 shr-h3 shr-h4)
742  (lambda (tags face)
743  (cl-loop for x in (if (consp face) face (list face))
744  thereis (memq x tags))))))
745  (goto-char
746  (if (> arg 0) (prop-match-beginning match) (prop-match-end match)))
747  (throw 'return nil))
748  (when (< arg 0) (beginning-of-line)))
749  (beginning-of-line)
750  (point)))
751 
752 (defun shr-heading-previous (&optional arg)
753  "Move backward by ARG headings (any h1-h4).
754 If ARG is negative move forwards instead, ARG defaults to 1."
755  (interactive "p")
756  (shr-heading-next (- (or arg 1))))
757 
758 (defun shr-heading--line-at-point ()
759  "Return the current line."
760  (buffer-substring (line-beginning-position) (line-end-position)))
761 
762 (defun shr-heading-setup-imenu ()
763  "Setup imenu for h1-h4 headings in eww buffer.
764 Add this function to appropriate major mode hooks such as
765 `eww-mode-hook' or `elfeed-show-mode-hook'."
766  (setq-local
767  imenu-prev-index-position-function #'shr-heading-previous
768  imenu-extract-index-name-function #'shr-heading--line-at-point))
769 
770 (defvar shr-heading-map
771  (let ((map (make-sparse-keymap)))
772  (define-key map "n" #'shr-heading-next)
773  (define-key map "\C-n" #'shr-heading-next)
774  (define-key map "p" #'shr-heading-previous)
775  (define-key map "\C-p" #'shr-heading-previous)
776  map))
777 
778 (add-hook 'eww-mode-hook 'shr-heading-setup-imenu)
779 (add-hook 'eww-mode-hook (lambda () (define-key eww-mode-map "i" shr-heading-map)))
780 
781 ;;; Tramp
782 (setopt tramp-default-method "ssh"
783  tramp-default-user user-login-name
784  tramp-default-host "localhost")
785 
786 ;;; Imenu
787 ;; (use-package imenu-list :ensure t)
788 
789 ;;; Org
790 (require 'org)
791 (require 'org-agenda)
792 (require 'org-id)
793 (require 'org-protocol)
794 
795 (setq org-html-htmlize-output-type 'css
796  org-html-head-include-default-style nil
797  ;; cc default
798  org-ascii-text-width 80)
799 
800 (org-crypt-use-before-save-magic)
801 
802 (setq org-structure-template-alist
803  '(("s" . "src")
804  ("e" . "src emacs-lisp")
805  ("x" . "src shell")
806  ("l" . "src lisp")
807  ("h" . "export html")
808  ("p" . "src python")
809  ("r" . "src rust")
810  ("E" . "example")
811  ("q" . "quote")
812  ("c" . "center")
813  ("C" . "comment")
814  ("v" . "verse")))
815 
816 ;; org-sbx [[https://list.orgmode.org/d429d29b-42fa-7d7b-6f3a-9fe692fd6dc7@grinta.net/T/]]
817 (defun %org-sbx (name header args)
818  (let* ((args (mapconcat
819  (lambda (x)
820  (format "%s=%S" (symbol-name (car x)) (cadr x)))
821  args ", "))
822  (ctx (list 'babel-call (list :call name
823  :name name
824  :inside-header header
825  :arguments args
826  :end-header ":results silent")))
827  (info (org-babel-lob-get-info ctx)))
828  (when info (org-babel-execute-src-block nil info))))
829 
830 (defmacro org-sbx (name &rest args)
831  (let* ((header (if (stringp (car args)) (car args) nil))
832  (args (if (stringp (car args)) (cdr args) args)))
833  (unless (stringp name)
834  (setq name (symbol-name name)))
835  (let ((result (%org-sbx name header args)))
836  (org-trim (if (stringp result) result (format "%S" result))))))
837 
838 (defun org-babel-execute-region (beg end &optional arg)
839  (interactive "r")
840  (narrow-to-region beg end)
841  (org-babel-execute-buffer arg)
842  (widen))
843 
844 (defun org-schedule-effort ()
845  (interactive)
846  (save-excursion
847  (org-back-to-heading t)
848  (let* ((element (org-element-at-point))
849  (effort (org-element-property :EFFORT element))
850  (scheduled (org-element-property :scheduled element))
851  (ts-year-start (org-element-property :year-start scheduled))
852  (ts-month-start (org-element-property :month-start scheduled))
853  (ts-day-start (org-element-property :day-start scheduled))
854  (ts-hour-start (org-element-property :hour-start scheduled))
855  (ts-minute-start (org-element-property :minute-start scheduled)) )
856  (org-schedule nil (concat
857  (format "%s" ts-year-start)
858  "-"
859  (if (< ts-month-start 10)
860  (concat "0" (format "%s" ts-month-start))
861  (format "%s" ts-month-start))
862  "-"
863  (if (< ts-day-start 10)
864  (concat "0" (format "%s" ts-day-start))
865  (format "%s" ts-day-start))
866  " "
867  (if (< ts-hour-start 10)
868  (concat "0" (format "%s" ts-hour-start))
869  (format "%s" ts-hour-start))
870  ":"
871  (if (< ts-minute-start 10)
872  (concat "0" (format "%s" ts-minute-start))
873  (format "%s" ts-minute-start))
874  "+"
875  effort)) )))
876 
877 (setopt org-preview-latex-image-directory "~/.emacs.d/.cache/ltximg"
878  org-latex-image-default-width "8cm"
879  org-refile-use-cache t
880  org-refile-allow-creating-parent-nodes 'confirm
881 
882  org-refile-targets '((nil :maxlevel . 3)
883  (org-agenda-files :maxlevel . 3))
884  ;; org-agenda-files (list "inbox.org")
885  org-agenda-include-diary t
886  org-agenda-include-inactive-timestamps t
887  org-confirm-babel-evaluate nil
888  org-src-fontify-natively t
889  org-src-tabs-act-natively t
890  org-footnote-section nil
891  org-log-into-drawer t
892  org-log-refile 'time
893  org-log-redeadline 'time
894  org-log-states-order-reversed nil
895  org-clock-persist 'history)
896 
897 (add-hook 'after-init-hook #'org-clock-persistence-insinuate)
898 
899 ;; archive
900 (defun extract-org-directory-titles-as-list (&optional dir)
901  (interactive "D")
902  (print
903  (delete nil
904  (let ((case-fold-search t))
905  (mapcar (lambda (f)
906  (when (string-match "org$" f)
907  (with-temp-buffer
908  (insert-file-contents-literally
909  (concat (file-name-as-directory dir) f))
910  (while (and (not (looking-at-p "#\\+TITLE:"))
911  (not (eobp)))
912  (forward-line))
913  (when (not (eobp))
914  (cons f (substring (thing-at-point 'line) 9 -1))))))
915  (directory-files dir))))))
916 
917 (defun insert-directory-org-file-titles (&optional dir)
918  (interactive "D")
919  (let ((files-titles (extract-org-directory-titles-as-list dir)))
920  (dolist (ft files-titles)
921  (insert (concat "[[file:" (car ft)"][" (cdr ft) "]]\n")))))
922 
923 (defun insert-directory-org-files (&optional dir)
924  (interactive "D")
925  (let ((files (directory-files dir)))
926  (dolist (f files)
927  (insert (concat "[[file:" f "][" (file-name-base f) "]]\n")))))
928 
929 (defun include-directory-org-files (&optional dir)
930  (interactive "D")
931  (let ((files (directory-files dir)))
932  (dolist (f files)
933  (insert (concat "#+INCLUDE: " f "\n")))))
934 
935 (defun org-todo-at-date (date)
936  "create a todo entry for a given date."
937  (interactive (list (org-time-string-to-time (org-read-date))))
938  (cl-flet ((org-current-effective-time (&rest r) date)
939  (org-today (&rest r) (time-to-days date)))
940  (cond ((eq major-mode 'org-mode) (org-todo))
941  ((eq major-mode 'org-agenda-mode) (org-agenda-todo)))))
942 
943 (defun org-agenda-show-week-all (&optional arg ) (interactive "P") (org-agenda arg "n"))
944 
945 (defun org-ask-location ()
946  "prompt for a location."
947  (let* ((org-refile-targets '((nil :maxlevel . 9)))
948  (hd (condition-case nil
949  (car (org-refile-get-location))
950  (error (car org-refile-history)))))
951  (goto-char (point-min))
952  (outline-next-heading)
953  (if (re-search-forward
954  (format org-complex-heading-regexp-format (regexp-quote hd))
955  nil t)
956  (goto-char (line-beginning-position))
957  (goto-char (point-max))
958  (or (bolp) (insert "\n"))
959  (insert "* " hd "\n")))
960  (end-of-line))
961 
962 (defun org-capture-fileref-snippet (f type headers func-name)
963  (let* ((code-snippet
964  (buffer-substring-no-properties (mark) (- (point) 1)))
965  (file-name (buffer-file-name))
966  (file-base (file-name-nondirectory file-name))
967  (line-number (line-number-at-pos (region-beginning)))
968  (initial-txt (if (null func-name)
969  (format "From [[file:%s::%s][%s]]:"
970  file-name line-number file-base)
971  (format "From ~%s~ (in [[file:%s::%s][%s]]):"
972  func-name file-name line-number
973  file-base))))
974  (format "
975  %s
976  #+BEGIN_%s %s
977  %s
978  #+END_%s" initial-txt type headers code-snippet type)))
979 
980 (defun org-capture-clip-snippet (f)
981  "Given a file, F, this captures the currently selected text
982  within an Org EXAMPLE block and a backlink to the file."
983  (with-current-buffer (find-buffer-visiting f)
984  (org-capture-fileref-snippet f "EXAMPLE" "" nil)))
985 
986 (defun org-capture-code-snippet (f)
987  "Given a file, F, this captures the currently selected text
988  within an Org SRC block with a language based on the current mode
989  and a backlink to the function and the file."
990  (with-current-buffer (find-buffer-visiting f)
991  (let ((org-src-mode (replace-regexp-in-string "-mode" "" (format "%s" major-mode)))
992  (func-name (which-function)))
993  (org-capture-fileref-snippet f "SRC" org-src-mode func-name))))
994 
995 (defun region-to-clocked-task (start end)
996  "Copies the selected text to the currently clocked in org-mode task."
997  (interactive "r")
998  (org-capture-string (buffer-substring-no-properties start end) "3"))
999 
1000 (setq org-global-properties
1001  '(quote (("EFFORT_ALL" . "0:15 0:30 0:45 1:00 2:00 3:00 4:00 5:00 6:00 0:00")
1002  ("STYLE_ALL" . "habit"))))
1003 
1004 (defun org-mode-ask-effort ()
1005  "Ask for an effort estimate when clocking in."
1006  (unless (org-entry-get (point) "Effort")
1007  (let ((effort
1008  (completing-read
1009  "Effort: "
1010  (org-entry-get-multivalued-property (point) "Effort"))))
1011  (unless (equal effort "")
1012  (org-set-property "Effort" effort)))))
1013 
1014 (add-hook 'org-clock-in-prepare-hook
1015  'org-mode-ask-effort)
1016 
1017 ;;;###autoload
1018 (defun org-adjust-tags-column-reset-tags ()
1019  "In org-mode buffers it will reset tag position according to
1020 `org-tags-column'."
1021  (when (and
1022  (not (string= (buffer-name) "*Remember*"))
1023  (eql major-mode 'org-mode))
1024  (let ((b-m-p (buffer-modified-p)))
1025  (condition-case nil
1026  (save-excursion
1027  (goto-char (point-min))
1028  (command-execute 'outline-next-visible-heading)
1029  ;; disable (message) that org-set-tags generates
1030  (cl-flet ((message (&rest ignored) nil))
1031  (org-set-tags 1 t))
1032  (set-buffer-modified-p b-m-p))
1033  (error nil)))))
1034 
1035 ;; TODO 2024-08-05: infer logbook column-titles/props
1036 (defun column-display-value-transformer (column-title value)
1037  "Modifies the value to display in column view."
1038  (let ((title (upcase column-title)))
1039  (when (and (member title '("UPDATED" "NOTE")))
1040  (org-back-to-heading)
1041  (re-search-forward
1042  "Note taken on \\[\\(.*\\)\\] \\\\\\\\\\\n +\\(.*\\) *$"
1043  (org-entry-end-position) t))
1044  (if (equal column-title "UPDATED")
1045  (match-string-no-properties 1)
1046  (match-string-no-properties 2))))
1047 
1048 (setq org-columns-modify-value-for-display-function
1049  #'column-display-value-transformer)
1050 
1051 ;;;###autoload
1052 (defun org-align-all-tables ()
1053  "align all tables in current buffer"
1054  (interactive)
1055  (org-table-map-tables 'org-table-align 'quietly))
1056 
1057 (defun org-remove-redundant-tags ()
1058  "Remove redundant tags of headlines in current buffer.
1059 
1060 A tag is considered redundant if it is local to a headline and
1061 inherited by a parent headline."
1062  (interactive)
1063  (when (eq major-mode 'org-mode)
1064  (save-excursion
1065  (org-map-entries
1066  (lambda ()
1067  (let ((alltags (split-string (or (org-entry-get (point) "ALLTAGS") "") ":"))
1068  local inherited tag)
1069  (dolist (tag alltags)
1070  (if (get-text-property 0 'inherited tag)
1071  (push tag inherited) (push tag local)))
1072  (dolist (tag local)
1073  (if (member tag inherited) (org-toggle-tag tag 'off)))))
1074  t nil))))
1075 
1076 ;;;; Agenda
1077 (cl-pushnew '("i" "Work in progress tasks" ((todo "WIP") (agenda))) org-agenda-custom-commands)
1078 
1079 (defvar org-agenda-overriding-header)
1080 (defvar org-agenda-sorting-strategy)
1081 (defvar org-agenda-restrict)
1082 (defvar org-agenda-restrict-begin)
1083 (defvar org-agenda-restrict-end)
1084 
1085 ;;;###autoload
1086 (defun org-agenda-reschedule-to-today ()
1087  (interactive)
1088  (cl-flet ((org-read-date (&rest rest) (current-time)))
1089  (call-interactively 'org-agenda-schedule)))
1090 
1091 ;; Patch org-mode to use vertical splitting
1092 (defadvice org-prepare-agenda (after org-fix-split)
1093  (toggle-window-split))
1094 (ad-activate 'org-prepare-agenda)
1095 
1096 (add-hook 'org-agenda-mode-hook (lambda () (hl-line-mode 1)))
1097 
1098 (defun org-agenda-log-mode-colorize-block ()
1099  "Set different line spacing based on clock time duration."
1100  (save-excursion
1101  (let* ((colors (cl-case (alist-get 'background-mode (frame-parameters))
1102  (light
1103  (list "#F6B1C3" "#FFFF9D" "#BEEB9F" "#ADD5F7"))
1104  (dark
1105  (list "#aa557f" "DarkGreen" "DarkSlateGray" "DarkSlateBlue"))))
1106  pos
1107  duration)
1108  (nconc colors colors)
1109  (goto-char (point-min))
1110  (while (setq pos (next-single-property-change (point) 'duration))
1111  (goto-char pos)
1112  (when (and (not (equal pos (pos-bol)))
1113  (setq duration (org-get-at-bol 'duration)))
1114  ;; larger duration bar height
1115  (let ((line-height (if (< duration 15) 1.0 (+ 0.5 (/ duration 30))))
1116  (ov (make-overlay (pos-bol) (1+ (pos-eol)))))
1117  (overlay-put ov 'face `(:background ,(car colors) :foreground "black"))
1118  (setq colors (cdr colors))
1119  (overlay-put ov 'line-height line-height)
1120  (overlay-put ov 'line-spacing (1- line-height))))))))
1121 
1122 (add-hook 'org-agenda-finalize-hook #'org-agenda-log-mode-colorize-block)
1123 
1124 ;;;###autoload
1125 (defun org-agenda-current-subtree-or-region (only-todos)
1126  "Display an agenda view for the current subtree or region.
1127  With prefix, display only TODO-keyword items."
1128  (interactive "P")
1129  (let ((starting-point (point))
1130  header)
1131  (with-current-buffer (or (buffer-base-buffer (current-buffer))
1132  (current-buffer))
1133  (if (use-region-p)
1134  (progn
1135  (setq header "Region")
1136  (put 'org-agenda-files 'org-restrict (list (buffer-file-name (current-buffer))))
1137  (setq org-agenda-restrict (current-buffer))
1138  (move-marker org-agenda-restrict-begin (region-beginning))
1139  (move-marker org-agenda-restrict-end
1140  (save-excursion
1141  ;; If point is at beginning of line, include
1142  ;; heading on that line by moving forward 1.
1143  (goto-char (1+ (region-end)))
1144  (org-end-of-subtree))))
1145  ;; No region; restrict to subtree.
1146  (save-excursion
1147  (save-restriction
1148  ;; In case the command was called from an indirect buffer, set point
1149  ;; in the base buffer to the same position while setting restriction.
1150  (widen)
1151  (goto-char starting-point)
1152  (setq header "Subtree")
1153  (org-agenda-set-restriction-lock))))
1154  ;; NOTE: Unlike other agenda commands, binding `org-agenda-sorting-strategy'
1155  ;; around `org-search-view' seems to have no effect.
1156  (let ((org-agenda-sorting-strategy '(priority-down timestamp-up))
1157  (org-agenda-overriding-header header))
1158  (org-search-view (if only-todos t nil) "*"))
1159  (org-agenda-remove-restriction-lock t)
1160  (message nil))))
1161 
1162 (defun org-export-translate-to-lang (term-translations &optional lang)
1163  "Adds desired translations to `org-export-dictionary'.
1164  TERM-TRANSLATIONS is alist consisted of term you want to translate
1165  and its corresponding translation, first as :default then as :html and
1166  :utf-8. LANG is language you want to translate to."
1167  (dolist (term-translation term-translations)
1168  (let* ((term (car term-translation))
1169  (translation-default (nth 1 term-translation))
1170  (translation-html (nth 2 term-translation))
1171  (translation-utf-8 (nth 3 term-translation))
1172  (term-list (assoc term org-export-dictionary))
1173  (term-langs (cdr term-list)))
1174  (setcdr term-list (append term-langs
1175  (list
1176  (list lang
1177  :default translation-default
1178  :html translation-html
1179  :utf-8 translation-utf-8)))))))
1180 
1181 ;;; Dictionary
1182 (setq dictionary-server "compiler.company"
1183  switch-to-buffer-obey-display-actions t)
1184 
1185 ;;; Ispell
1186 ;; requires aspell and a hunspell dictionary (hunspell-en_us)
1187 (setq-default ispell-program-name "aspell")
1188 (add-hook 'mail-send-hook #'ispell-message)
1189 
1190 ;;; Skel
1191 (require 'sk)
1192 (require 'skt)
1193 
1194 (provide 'default)
1195 ;; default.el ends here