main
   1;;; init --- vdemeester's emacs configuration -*- lexical-binding: t -*-
   2
   3;; Copyright (C) 2025 Vincent Demeester
   4;; Author: Vincent Demeester <vincent@sbr.pm>
   5
   6;; This file is NOT part of GNU Emacs.
   7;;; Commentary:
   8;; This is the "mini" version for now, but aims to become the default one.
   9;;; Code:
  10
  11;;; Ensure critical directories exist
  12;; Create auto-save and backup directories if they don't exist
  13(let ((auto-save-dir (expand-file-name "~/.local/share/emacs/auto-saves/"))
  14      (backup-dir (expand-file-name "~/.local/share/emacs/backups/")))
  15  (unless (file-directory-p auto-save-dir)
  16    (make-directory auto-save-dir t))
  17  (unless (file-directory-p backup-dir)
  18    (make-directory backup-dir t)))
  19
  20;;; Silence native-comp warnings to avoid cascading display-warning loops.
  21(setq native-comp-async-report-warnings-errors 'silent)
  22
  23;;; Some constants I am using across the configuration.
  24(defconst org-directory "~/desktop/org/"
  25  "`org-mode' directory, where most of the org-mode file lives.")
  26(defconst org-notes-directory (expand-file-name "notes" org-directory)
  27  "`org-mode' notes directory, for notes obviously, most likely managed by denote.")
  28(defconst org-inbox-file (expand-file-name "inbox.org" org-directory)
  29  "`org-mode' inbox file, where we collect entries to be triaged.")
  30(defconst org-todos-file (expand-file-name "todos.org" org-directory)
  31  "`org-mode' file for TODOs.  This is the main file for the org angenda entries.")
  32(defconst org-habits-file (expand-file-name "habits.org" org-directory)
  33  "`org-mode' file for habits.  This is the file for habits that I need to
  34share elsewhere, with Flat habits on iOS for example.")
  35(defconst org-reading-list-file (expand-file-name "reading-list.org" org-directory)
  36  "`org-mode' file for list of things to read.
  37Most likely these needs to be added to readwise reader or ditch.")
  38(defconst org-calendar-file (expand-file-name "calendar.org" org-directory)
  39  "`org-mode' calendar file, auto-generated from Google Calendar (read-only).")
  40(defconst org-journal-file (expand-file-name "20250620T144103--journal__journal.org" org-notes-directory)
  41  "`org-mode' journal file, for journal-ling.")
  42(defconst org-archive-dir (expand-file-name "archive" org-directory)
  43  "`org-mode' directory of archived files.")
  44(defconst org-people-dir (expand-file-name "people" org-notes-directory)
  45  "`org-mode' people files directory, most likely managed by denote.")
  46
  47;;; The configuration.
  48
  49;;; Quick access to certain key file using registers
  50(set-register ?e `(file . ,(locate-user-emacs-file "init.el")))
  51(set-register ?i `(file . ,org-inbox-file))
  52(set-register ?t `(file . ,org-todos-file))
  53(set-register ?c `(file . ,org-calendar-file))
  54(set-register ?j `(file . ,org-journal-file))
  55(set-register ?o `(file . ,org-directory))
  56(set-register ?n `(file . ,org-notes-directory))
  57(set-register ?P `(file . ,org-people-dir))
  58
  59;;; Some GC optimizations
  60(defun my-minibuffer-setup-hook ()
  61  (setq gc-cons-threshold most-positive-fixnum))
  62
  63(defun my-minibuffer-exit-hook ()
  64  (setq gc-cons-threshold 800000000))
  65
  66(setq gc-cons-threshold most-positive-fixnum)
  67
  68(run-with-idle-timer 1.2 t 'garbage-collect)
  69
  70(defconst emacs-start-time (current-time))
  71
  72(let ((minver 29))
  73  (unless (>= emacs-major-version minver)
  74    (error "Your Emacs is too old -- this configuration requires v%s or higher" minver)))
  75
  76(setq inhibit-default-init t)           ; Disable the site default settings
  77
  78(setq confirm-kill-emacs #'y-or-n-p)
  79
  80(setq custom-file (locate-user-emacs-file "custom.el"))
  81(require 'cus-edit)
  82(setq
  83 custom-buffer-done-kill nil          ; Kill when existing
  84 custom-buffer-verbose-help nil       ; Remove redundant help text
  85 custom-unlispify-tag-names nil       ; Show me the real variable name
  86 custom-unlispify-menu-entries nil)
  87;; Create the custom-file if it doesn't exists
  88(unless (file-exists-p custom-file)
  89  (write-region "" nil custom-file))
  90(load custom-file :no-error-if-file-is-missing)
  91
  92(setq echo-keystrokes 0.1) ;; display command keystrokes quickly
  93
  94(global-unset-key (kbd "C-z"))
  95(global-unset-key (kbd "C-x C-z"))
  96(global-unset-key (kbd "C-h h"))
  97
  98;; Make C-w behave like in terminal: delete word when no region is selected
  99(defun kill-region-or-backward-word ()
 100  "If the region is active and non-empty, call `kill-region'.
 101Otherwise, call `backward-kill-word'."
 102  (interactive)
 103  (call-interactively
 104   (if (use-region-p) 'kill-region 'backward-kill-word)))
 105(global-set-key (kbd "C-w") 'kill-region-or-backward-word)
 106
 107;; Disable owerwrite-mode, iconify-frame and diary
 108(mapc
 109 (lambda (command)
 110   (put command 'disabled t))
 111 '(overwrite-mode iconify-frame diary))
 112;; And enable those commands (disabled by default)
 113(mapc
 114 (lambda (command)
 115   (put command 'disabled nil))
 116 '(list-timers narrow-to-region narrow-to-page upcase-region downcase-region))
 117
 118(unless noninteractive
 119  (defconst font-height 130
 120    "Default font-height to use.")
 121  ;; 2024-10-05: Switching from Ubuntu Mono to Cascadia Mono
 122  ;; 2024-96-06: Switching from Cascadia Mono to JetBrains Mono
 123  (defconst font-family-mono
 124    (font-spec :family "MonaspiceNe Nerd Font" :features '(calt liga ss01 ss02 ss03 ss04 ss05 ss06 ss07 ss08))
 125    ;; "JetBrains Mono"
 126    "Default monospace font-family to use.")
 127  (defconst font-family-sans "Ubuntu Sans"
 128    "Default sans font-family to use.")
 129  ;; Middle/Near East: שלום, السّلام عليكم
 130  (when (and (fboundp 'set-fontset-font) (member "Noto Sans Arabic" (font-family-list)))
 131    (set-fontset-font t 'arabic "Noto Sans Arabic"))
 132  (when (and (fboundp 'set-fontset-font) (member "Noto Sans Hebrew" (font-family-list)))
 133    (set-fontset-font t 'arabic "Noto Sans Hebrew"))
 134  ;; Africa: ሠላም
 135  (when (and (fboundp 'set-fontset-font) (member "Noto Sans Ethiopic" (font-family-list)))
 136    (set-fontset-font t 'ethiopic "Noto Sans Ethiopic"))
 137
 138  ;; Use Monaspace Radon (handwriting) for italic faces — matches Kitty config
 139  (defconst font-family-mono-italic
 140    (font-spec :family "MonaspiceRn Nerd Font" :features '(calt liga ss01 ss02 ss03 ss04 ss05 ss06 ss07 ss08))
 141    "Italic monospace font-family (Monaspace Radon handwriting variant).")
 142
 143  ;; Font setup must run when a GUI frame exists (daemon starts with no frame)
 144  (defun vd/setup-fonts (&optional frame)
 145    "Configure fonts for graphical frames."
 146    (when-let* ((f (or frame (selected-frame))))
 147      (when (display-graphic-p f)
 148	(set-face-attribute 'default f
 149			    :font font-family-mono
 150			    :height font-height
 151			    :weight 'regular)
 152	(set-face-attribute 'fixed-pitch f
 153			    :font font-family-mono
 154			    :weight 'medium
 155			    :height font-height)
 156	(set-face-attribute 'variable-pitch f
 157			    :family font-family-sans
 158			    :weight 'regular)
 159	(set-face-attribute 'italic f
 160			    :font font-family-mono-italic
 161			    :slant 'italic)
 162	(set-face-attribute 'bold-italic f
 163			    :font font-family-mono-italic
 164			    :weight 'bold
 165			    :slant 'italic))))
 166  ;; Apply to future frames (daemon mode)
 167  (add-hook 'server-after-make-frame-hook #'vd/setup-fonts)
 168  ;; Apply now if already in a GUI frame (non-daemon)
 169  (vd/setup-fonts)
 170
 171  (when (fboundp 'set-fontset-font)
 172    (set-fontset-font t 'symbol "Apple Color Emoji")
 173    (set-fontset-font t 'symbol "Noto Color Emoji" nil 'append)
 174    (set-fontset-font t 'symbol "Segoe UI Emoji" nil 'append)
 175    (set-fontset-font t 'symbol "Symbola" nil 'append))
 176
 177  (require 'modus-themes)
 178  (require 'modus-alabaster-bg)
 179  (defvar modus-themes-preset-overrides-cooler)
 180  (setopt modus-themes-common-palette-overrides
 181	  `((border-mode-line-active unspecified)
 182            (border-mode-line-inactive unspecified)
 183	    ,@modus-themes-preset-overrides-cooler)
 184	  modus-themes-to-rotate '(modus-operandi modus-vivendi)
 185	  modus-themes-mixed-fonts t
 186	  modus-themes-headings '((0 . (variable-pitch semilight 1.5))
 187				  (1 . (regular 1.4))
 188				  (2 . (regular 1.3))
 189				  (3 . (regular 1.2))
 190				  (agenda-structure . (variable-pitch light 2.2))
 191				  (agenda-date . (variable-pitch regular 1.3))
 192				  (t . (regular 1.15))))
 193
 194  ;; Color scheme management - sync with system dconf settings
 195  (defvar vde/themes-plist
 196    '(:dark modus-vivendi :light modus-operandi)
 197    "Themes to use for dark and light modes.")
 198
 199  (defun vde/color-scheme-get-system ()
 200    "Get current system color scheme from dconf."
 201    (when (eq system-type 'gnu/linux)
 202      (let* ((raw-scheme (shell-command-to-string
 203                          "/run/current-system/sw/bin/dconf read /org/gnome/desktop/interface/color-scheme"))
 204             (scheme (string-trim raw-scheme)))
 205        (if (string-match-p "prefer-dark" scheme)
 206            :dark
 207          :light))))
 208
 209  (defun vde/color-scheme-set-emacs (scheme)
 210    "Set Emacs theme based on SCHEME (:dark or :light)."
 211    (let ((theme (plist-get vde/themes-plist scheme)))
 212      (when theme
 213        (mapc #'disable-theme custom-enabled-themes)
 214        (load-theme theme :no-confirm))))
 215
 216  (defun vde/color-scheme-sync ()
 217    "Sync Emacs theme with system color scheme."
 218    (interactive)
 219    (let ((scheme (vde/color-scheme-get-system)))
 220      (when scheme
 221        (vde/color-scheme-set-emacs scheme))))
 222
 223  (defun vde/color-scheme-toggle ()
 224    "Toggle system color scheme and sync Emacs."
 225    (interactive)
 226    (when (eq system-type 'gnu/linux)
 227      (start-process "toggle-color-scheme" nil "toggle-color-scheme")
 228      ;; Wait a bit for the system to update, then sync
 229      (run-with-timer 0.5 nil #'vde/color-scheme-sync)))
 230
 231  (declare-function vde/color-scheme-sync "init")
 232  (declare-function vde/color-scheme-get-system "init")
 233  (declare-function vde/color-scheme-set-emacs "init")
 234
 235  ;; Initial theme setup
 236  (if (display-graphic-p)
 237      (vde/color-scheme-sync)
 238    (load-theme 'modus-vivendi :no-confirm))
 239
 240  ;; Watch for dbus signals when color-scheme changes
 241  (when (and (eq system-type 'gnu/linux)
 242             (require 'dbus nil t))
 243    (declare-function dbus-register-signal "dbus")
 244    (dbus-register-signal
 245     :session
 246     "ca.desrt.dconf"
 247     "/ca/desrt/dconf/Writer/user"
 248     "ca.desrt.dconf.Writer"
 249     "Notify"
 250     (lambda (&rest args)
 251       (when (string-match-p "color-scheme" (format "%s" args))
 252         (vde/color-scheme-sync)))))
 253
 254  ;; Keybinding for toggling color scheme
 255  (declare-function vde/color-scheme-toggle "init")
 256  (global-set-key (kbd "C-c t t") #'vde/color-scheme-toggle))
 257
 258(setopt load-prefer-newer t)              ; Always load newer compiled files
 259(setopt ad-redefinition-action 'accept)   ; Silence advice redefinition warnings
 260;; (setopt debug-on-error t)
 261(setopt byte-compile-debug t)
 262
 263;; Configure `use-package' prior to loading it.
 264(eval-and-compile
 265  (setq use-package-always-ensure nil)
 266  (setq use-package-always-defer nil)
 267  (setq use-package-always-demand nil)
 268  (setq use-package-expand-minimally nil)
 269  (setq use-package-enable-imenu-support t)
 270  (setq use-package-compute-statistics t))
 271
 272(eval-when-compile
 273  (require 'use-package))
 274
 275(require 'info) ;; XXX ensure the var exists even before loading `info.el'.
 276
 277;; Add site-lisp to load-path for local elisp files
 278(add-to-list 'load-path (expand-file-name "site-lisp" user-emacs-directory))
 279(add-to-list 'custom-theme-load-path (expand-file-name "site-lisp" user-emacs-directory))
 280
 281(use-package vde-toggle
 282  :bind ("C-c t m" . vde-toggle-modes))
 283
 284;; TODO: Re-enable init-func when needed for custom macros
 285;; (use-package init-func)
 286;; ;; TODO: do useful stuff with the macro instead
 287;; (vde/run-and-delete-frame my-greet-and-close ()
 288;; 			  "Displays a greeting and closes the frame after a short delay."
 289;; 			  (message "Hello from a macro-defined function! Closing soon...")
 290;; 			  (sit-for 2))
 291
 292(use-package emacs
 293  :bind
 294  ("C-x m" . mark-defun)
 295  ("C-x C-b" . bs-show)
 296  ("M-o" . other-window)
 297  ("C-c j d" . duplicate-dwim)
 298  ("<f5>" . revert-buffer)
 299  ;; (:map completion-preview-active-mode-map
 300  ;; ("M-n" . #'completion-preview-next-candidate)
 301  ;; ("M-p" . #'completion-preview-prev-candidate))
 302  :custom
 303  (create-lockfiles nil)
 304  ;; Versioned backups in central directory
 305  (make-backup-files t)
 306  (version-control t)                    ; Numbered backups
 307  (kept-new-versions 10)                 ; Keep 10 newest
 308  (kept-old-versions 2)                  ; Keep 2 oldest
 309  (delete-old-versions t)                ; Auto-delete excess
 310  (backup-by-copying t)                  ; Don't clobber symlinks
 311  (backup-directory-alist '(("." . "~/.local/share/emacs/backups/")))
 312  ;; Auto-save files in central directory
 313  (auto-save-file-name-transforms '((".*" "~/.local/share/emacs/auto-saves/" t)))
 314  (use-short-answers t "Use short answer y/n")
 315  ;; (tab-always-indent 'complete)
 316  ;; (tab-first-completion 'word-or-paren-or-punct)
 317  (enable-local-variables :all)
 318  ;; (select-enable-clipboard t)
 319  ;; (select-enable-primary t)
 320  (comment-multi-line t)
 321  (read-extended-command-predicate #'command-completion-default-include-p)
 322  (mouse-autoselect 1)
 323  (completion-cycle-threshold 2)
 324  (completion-ignore-case t)
 325  (completion-show-inline-help nil)
 326  (completions-detailed t)
 327  (enable-recursive-minibuffers t)
 328  (read-buffer-completion-ignore-case t)
 329  (read-file-name-completion-ignore-case t)
 330  (find-ls-option '("-exec ls -ldh {} +" . "-ldh"))  ; find-dired results with human readable sizes
 331  (global-auto-revert-non-file-buffers t "Auto revert non-file buffers")
 332  (switch-to-buffer-obey-display-actions t "Don't distuingish automatic and manual window switching")
 333  (window-combination-resize t "Resize window proportionally")
 334  (isearch-lazy-count t "Show size of search results")
 335  (lazy-count-prefix-format "(%s/%s) " "Format of search results")
 336  (lazy-highlight-initial-delay 0 "No delay before highlight search matches")
 337  (isearch-allow-scroll t "Allow scrolling while searching")
 338  (isearch-allow-motion t "Allow movement commands while searching")
 339  :hook
 340  (after-init . global-hl-line-mode)
 341  (after-init . global-completion-preview-mode)
 342  (after-init . auto-insert-mode)
 343  (after-init . pixel-scroll-mode)
 344  :config
 345  (with-current-buffer "*Messages*" (emacs-lock-mode 'kill))
 346  (with-current-buffer "*scratch*" (emacs-lock-mode 'kill))
 347  (display-time-mode -1)
 348  (when (fboundp 'tooltip-mode) (tooltip-mode -1))
 349  (when (fboundp 'blink-cursor-mode) (blink-cursor-mode -1))
 350  (setenv "GIT_EDITOR" (format "emacs --init-dir=%s " (shell-quote-argument user-emacs-directory)))
 351  (setenv "EDITOR" (format "emacs --init-dir=%s " (shell-quote-argument user-emacs-directory)))
 352  (delete-selection-mode 1)
 353  (declare-function minibuffer-keyboard-quit "delsel")
 354  (defun er-keyboard-quit ()
 355    "Smater version of the built-in `keyboard-quit'.
 356
 357The generic `keyboard-quit' does not do the expected thing when
 358the minibuffer is open.  Whereas we want it to close the
 359minibuffer, even without explicitly focusing it."
 360    (interactive)
 361    (if (active-minibuffer-window)
 362	(if (minibufferp)
 363            (minibuffer-keyboard-quit)
 364          (abort-recursive-edit))
 365      (keyboard-quit)))
 366  (declare-function er-keyboard-quit "init")
 367  (global-set-key [remap keyboard-quit] #'er-keyboard-quit))
 368
 369(use-package view
 370  :commands (view-mode)
 371  :init
 372  (declare-function View-scroll-page-backward "view")
 373  (declare-function View-scroll-page-forward "view")
 374  :custom
 375  (view-read-only t "Enable view-mode when entering read-only")
 376  :bind (("C-<escape>" . view-mode)
 377	 :map view-mode-map
 378	 ;; Navigation
 379	 ("n" . next-line)
 380	 ("p" . previous-line)
 381	 ("f" . forward-char)
 382	 ("b" . backward-char)
 383
 384	 ;; Beginning/end of line
 385	 ("a" . beginning-of-line)
 386	 ("e" . end-of-line)
 387
 388	 ;; Quick exit to edit mode
 389	 ("i" . View-exit)
 390	 ("j" . next-line)
 391	 ("k" . previous-line)
 392	 ("h" . backward-char)
 393	 ("l" . forward-char)
 394
 395	 ;; Page movement
 396	 ("u" . (lambda()
 397		  (interactive)
 398		  (View-scroll-page-backward 3)))
 399	 ("d" . (lambda()
 400		  (interactive)
 401		  (View-scroll-page-forward 3)))
 402
 403	 ;; Beginning/end of line (Vim style)
 404	 ("0" . beginning-of-line)
 405	 ("$" . end-of-line)
 406
 407	 ;; Beginning/end of buffers
 408	 ("g" . beginning-of-buffer)
 409	 ("G" . end-of-buffer)
 410
 411	 ;; Other bespoke bindings
 412	 (";" . other-window)
 413
 414	 ("SPC" . nil))
 415  :hook
 416  ;; Visual feedback - box cursor in view mode, bar when editing
 417  (view-mode . view-mode-hookee+)
 418  :config
 419  (defun view-mode-hookee+ ()
 420    (setq cursor-type (if view-mode 'box 'bar))))
 421
 422(use-package tramp
 423  :custom
 424  ;; Tramp
 425  (remote-file-name-inhibit-locks t "No lock files on remote files")
 426  (tramp-use-scp-direct-remote-copying t "Use direct copying between two remote hosts")
 427  (remote-file-name-inhibit-auto-save-visited t "Do not auto-save remote files")
 428  (tramp-copy-size-limit (* 1024 1024)) ;; 1MB
 429  (tramp-verbose 2)
 430  :config
 431  (declare-function tramp-compile-disable-ssh-controlmaster-options "tramp")
 432  ;; Add custom SSH options to disable YubiKey agent for TRAMP connections
 433  (with-eval-after-load 'tramp-sh
 434    (defvar tramp-use-ssh-controlmaster-options)
 435    (setq tramp-use-ssh-controlmaster-options nil)
 436    (let ((ssh-method (assoc "ssh" tramp-methods)))
 437      (when ssh-method
 438        (let* ((login-args (cadr (assq 'tramp-login-args (cdr ssh-method))))
 439               (new-login-args (append login-args
 440                                      '(("-o" "IdentitiesOnly=yes")
 441                                        ("-o" "IdentityAgent=none")))))
 442          (setf (cadr (assq 'tramp-login-args (cdr ssh-method))) new-login-args)))))
 443  (connection-local-set-profile-variables
 444   'remote-direct-async-process
 445   '((tramp-direct-async-process . t)))
 446
 447  (connection-local-set-profiles
 448   '(:application tramp :protocol "scp")
 449   'remote-direct-async-process)
 450
 451  (defvar magit-tramp-pipe-stty-settings)
 452  (with-eval-after-load 'magit
 453    (setq magit-tramp-pipe-stty-settings 'pty))
 454  (with-eval-after-load 'tramp
 455    (with-eval-after-load 'compile
 456      (remove-hook 'compilation-mode-hook #'tramp-compile-disable-ssh-controlmaster-options)))
 457  
 458  (add-to-list 'tramp-remote-path 'tramp-own-remote-path)
 459  (add-to-list 'tramp-remote-path "/home/vincent/.local/state/nix/profile/bin/")
 460  (add-to-list 'tramp-remote-path "~/bin/")
 461  ;; (add-to-list 'tramp-connection-properties
 462  ;; 	       (list (regexp-quote "/ssh:aomi.home:")
 463  ;; 		     "remote-shell" "/home/vincent/.local/state/nix/profile/bin/zsh"))
 464  )
 465
 466(use-package passage
 467  :commands (passage-get))
 468
 469(use-package find-file
 470  :bind ("C-x C-g" . ff-find-other-file))
 471
 472(use-package icomplete
 473  :unless noninteractive
 474  :hook
 475  (icomplete-minibuffer-setup
 476   . (lambda()(interactive)
 477       (setq-local completion-styles '(flex partial-completion initials basic))))
 478  (after-init . fido-vertical-mode)
 479  :custom
 480  (icomplete-compute-delay 0.01)
 481  :config
 482  ;; Unbind C-, from icomplete to allow embark-act to work
 483  (define-key icomplete-minibuffer-map (kbd "C-,") nil)
 484  (define-key icomplete-fido-mode-map (kbd "C-,") nil))
 485
 486(use-package display-line-numbers
 487  :unless noninteractive
 488  :hook (prog-mode . display-line-numbers-mode)
 489  :config
 490  (setq-default display-line-numbers-type 'relative)
 491  (defun vde/toggle-line-numbers ()
 492    "Toggles the display of line numbers.  Applies to all buffers."
 493    (interactive)
 494    (if (bound-and-true-p display-line-numbers-mode)
 495        (display-line-numbers-mode -1)
 496      (display-line-numbers-mode)))
 497  :bind ("<f7>" . vde/toggle-line-numbers))
 498
 499(use-package kkp
 500  :if (not (display-graphic-p))
 501  :hook (after-init . global-kkp-mode))
 502
 503(use-package helpful
 504  :unless noninteractive
 505  :bind (("C-h f" . helpful-callable)
 506         ("C-h F" . helpful-function)
 507         ("C-h M" . helpful-macro)
 508         ("C-c h S" . helpful-at-point)
 509         ("C-h k" . helpful-key)
 510         ("C-h v" . helpful-variable)
 511         ("C-h C" . helpful-command)))
 512
 513(use-package flymake
 514  :bind
 515  ("C-c f b" . flymake-show-buffer-diagnostics)
 516  (:map flymake-mode-map
 517        ("M-n" . flymake-goto-next-error)
 518        ("M-p" . flymake-goto-prev-error))
 519  :hook
 520  (prog-mode . flymake-mode))
 521
 522(use-package treesit-fold
 523  :hook
 524  (after-init . global-treesit-fold-mode)
 525  :custom
 526  (treesit-fold-line-count-show t)  ; Show line count in folded regions
 527  (treesit-fold-line-count-format " <%d lines> ")
 528  :config
 529  (global-set-key (kbd "C-c f f") 'treesit-fold-close)
 530  (global-set-key (kbd "C-c f o") 'treesit-fold-open))
 531
 532(use-package scopeline
 533  :hook prog-mode
 534  :custom-face
 535  (scopeline-face ((t (:height 0.8 :inherit shadow)))))
 536
 537(use-package aggressive-indent
 538  :commands (aggressive-indent-mode)
 539  :hook
 540  (emacs-lisp-mode . aggressive-indent-mode))
 541
 542(use-package save-place
 543  :defer 1
 544  :config (save-place-mode 1))
 545
 546(use-package symbol-overlay
 547  :custom
 548  (symbol-overlay-idle-time 0.2)
 549  :bind
 550  ("M-s s i" . symbol-overlay-put)
 551  ("M-N" . symbol-overlay-jump-next)
 552  ("M-P" . symbol-overlay-jump-prev)
 553  ("M-s s r" . symbol-overlay-rename)
 554  ("M-s s c" . symbol-overlay-remove-all)
 555  :hook
 556  (prog-mode . symbol-overlay-mode))
 557
 558(use-package savehist
 559  :unless noninteractive
 560  :hook (after-init . savehist-mode)
 561  :custom
 562  (history-length 10000)
 563  (savehist-save-minibuffer-history t)
 564  (savehist-delete-duplicates t)
 565  (savehist-autosave-interval 180)
 566  (savehist-additional-variables '(extended-command-history
 567				   search-ring
 568				   regexp-search-ring
 569				   comint-input-ring
 570				   compile-history
 571				   last-kbd-macro
 572				   shell-command-history)))
 573
 574(use-package newcomment
 575  :unless noninteractive
 576  :custom
 577  (comment-empty-lines t)
 578  (comment-fill-column nil)
 579  (comment-multi-line t)
 580  (comment-style 'multi-line)
 581  :config
 582  (defun prot/comment-dwim (&optional arg)
 583    "Alternative to `comment-dwim': offers a simple wrapper
 584    around `comment-line' and `comment-dwim'.
 585
 586    If the region is active, then toggle the comment status of the
 587    region or, if the major mode defines as much, of all the lines
 588    implied by the region boundaries.
 589
 590    Else toggle the comment status of the line at point."
 591    (interactive "*P")
 592    (if (use-region-p)
 593        (comment-dwim arg)
 594      (save-excursion
 595        (comment-line arg))))
 596  :bind (("C-;" . prot/comment-dwim)
 597	 ("C-:" . comment-kill)
 598	 ("M-;" . comment-indent)
 599	 ("C-x C-;" . comment-box)))
 600
 601(use-package dired
 602  :custom
 603  (dired-hide-details-hide-information-lines 'nil)
 604  (dired-kill-when-opening-new-dired-buffer 't)
 605  (dired-dwim-target t)
 606  :bind
 607  (:map dired-mode-map
 608	("E"   . wdired-change-to-wdired-mode)
 609	("l"   . dired-find-file))
 610  :custom
 611  (dired-vc-rename-file t)
 612  :hook
 613  (dired-mode . dired-omit-mode)
 614  (dired-mode . dired-hide-details-mode)
 615  ;; (dired-mode . dired-sort-toggle-or-edit) ; I don't like the "default by date" behavior
 616  )
 617
 618(use-package alert
 619  :defer 2
 620  :init
 621  (declare-function alert "alert")
 622  (defun alert-after-finish-in-background (buf str)
 623    (when (or (not (get-buffer-window buf 'visible)) (not (frame-focus-state)))
 624      (alert str :buffer buf)))
 625  :config
 626  (defvar alert-default-style)
 627  (setq alert-default-style 'libnotify))
 628
 629(use-package elec-pair
 630  :hook (after-init-hook . electric-pair-mode))
 631
 632(use-package uniquify
 633  :custom
 634  (uniquify-buffer-name-style 'forward)
 635  (uniquify-strip-common-suffix t)
 636  (uniquify-after-kill-buffer-p t))
 637
 638(use-package compile
 639  :unless noninteractive
 640  :commands (compile)
 641  :custom
 642  (compilation-always-kill t)
 643  (compilation-scroll-output t)
 644  (ansi-color-for-compilation-mode t)
 645  :config
 646  (declare-function alert-after-finish-in-background "init")
 647  (add-hook 'compilation-finish-functions #'alert-after-finish-in-background))
 648
 649(use-package subword
 650  :diminish
 651  :hook (prog-mode-hook . subword-mode))
 652
 653;; Recentf
 654(use-package recentf
 655  :defer t
 656  :hook
 657  (after-init . recentf-mode)
 658  :bind (("C-x C-r" . recentf-open)))
 659
 660(use-package autorevert
 661  :ensure nil
 662  :custom
 663  (global-auto-revert-non-file-buffers t)
 664  (auto-revert-verbose nil)
 665  :init
 666  (global-auto-revert-mode 1))
 667
 668(use-package prog-mode
 669  :hook
 670  (prog-mode . eldoc-mode)
 671  :custom
 672  (eldoc-idle-delay 0.2))
 673
 674(use-package eglot
 675  :bind
 676  (:map eglot-mode-map
 677        ("C-c e a" . eglot-code-actions)
 678        ("C-c e r" . eglot-reconnect)
 679        ("<f2>" . eglot-rename)
 680        ("C-c e ?" . eldoc-print-current-symbol-info))
 681  :custom
 682  (eglot-autoshutdown t)
 683  (eglot-confirm-server-initiated-edits nil)
 684  :config
 685  (declare-function eglot-format-buffer "eglot")
 686  (declare-function eglot-managed-p "eglot")
 687  (add-to-list 'eglot-ignored-server-capabilities :documentHighlightProvider)
 688  (add-to-list 'eglot-server-programs `(json-mode  "vscode-json-language-server" "--stdio"))
 689  (add-to-list 'eglot-server-programs '(nix-mode . ("nil")))
 690  ;; (add-to-list 'eglot-server-programs
 691  ;; '(go-mode . ("harper-ls" "--stdio")))
 692  ;; (add-to-list 'eglot-server-programs
 693  ;; 	       '(text-mode . ("harper-ls" "--stdio")))
 694  ;; (add-to-list 'eglot-server-programs
 695  ;;              '(org-mode . ("harper-ls" "--stdio")))
 696  (add-to-list 'eglot-server-programs
 697               '(markdown-mode . ("harper-ls" "--stdio")))
 698  (setq-default eglot-workspace-configuration
 699		'(
 700		  :gopls (
 701			  :usePlaceholders t
 702			  ;; See https://github.com/golang/tools/blob/master/gopls/doc/analyzers.md
 703			  :analyses (
 704				     :QF1006 t
 705				     :QF1007 t
 706				     :S1002 t
 707				     :S1005 t
 708				     :S1006 t
 709				     :S1008 t
 710				     :S1025 t
 711				     :SA1003 t
 712				     :SA1014 t
 713				     :SA1015 t
 714				     :SA1023 t
 715				     :SA1032 t
 716				     :SA2002 t
 717				     :SA4023 t
 718				     :SA4031 t
 719				     :SA5000 t
 720				     :SA5010 t
 721				     :SA5000 t
 722				     :SA6000 t
 723				     :SA6001 t
 724				     :SA6002 t
 725				     :SA6003 t
 726				     :SA9003 t
 727				     :SA9007 t
 728				     :ST1000 t
 729				     :ST1001 t
 730				     :ST1005 t
 731				     :ST1013 t
 732				     :ST1015 t
 733				     :ST1016 t
 734				     :ST1017 t
 735				     :ST1019 t
 736				     :ST1020 t
 737				     :ST1021 t
 738				     :ST1022 t
 739				     :ST1023 t
 740				     :shadow t
 741				     )
 742			  ;; See https://github.com/golang/tools/blob/master/gopls/doc/inlayHints.md
 743			  :hints (:constantValues t :compositeLiteralTypes t :compositeLiteralFields t))
 744		  :nil (
 745			:formatting (:command ["nixfmt"])
 746			:nix (
 747			      :maxMemoryMB 2560
 748			      :autoEvalInputs t
 749			      :nixpkgsInputName "nixpkgs"
 750			      )
 751			)
 752		  :pylsp (
 753			  :configurationSources ["flake8"]
 754			  :plugins (:pycodestyle (:enabled nil)
 755						 :black (:enabled t)
 756						 :mccabe (:enabled nil)
 757						 :flake8 (:enabled t)))))
 758  (defun eglot-format-buffer-on-save ()
 759    (if (and (project-current) (eglot-managed-p))
 760        (add-hook 'before-save-hook #'eglot-format-buffer nil 'local)
 761      (remove-hook 'before-save-hook #'eglot-format-buffer 'local)))
 762  (declare-function eglot-format-buffer-on-save "init")
 763  (add-hook 'eglot-managed-mode-hook #'eglot-format-buffer-on-save)
 764
 765  ;; Don't auto-start eglot on TRAMP files (too slow/unreliable)
 766  (defun eglot-ensure-unless-tramp ()
 767    "Start eglot unless we're on a TRAMP remote file."
 768    (unless (file-remote-p default-directory)
 769      (eglot-ensure)))
 770  (declare-function eglot-ensure-unless-tramp "init")
 771
 772  :hook
 773  ;; (before-save . gofmt-before-save)
 774  ;; (before-save . eglot-format-buffer)
 775  (nix-mode . eglot-ensure-unless-tramp)
 776  (nix-ts-mode . eglot-ensure-unless-tramp)
 777  (rust-mode . eglot-ensure-unless-tramp)
 778  (rust-ts-mode . eglot-ensure-unless-tramp)
 779  (python-mode . eglot-ensure-unless-tramp)
 780  (python-ts-mode . eglot-ensure-unless-tramp)
 781  (go-mode . eglot-ensure-unless-tramp)
 782  (go-ts-mode . eglot-ensure-unless-tramp)
 783  (sh-mode . eglot-ensure-unless-tramp)
 784  (sh-script-mode . eglot-ensure-unless-tramp)
 785  (org-mode . eglot-ensure-unless-tramp)
 786  (markdown-mode . eglot-ensure-unless-tramp)
 787  (js-ts-mode . eglot-ensure-unless-tramp)
 788  (typescript-ts-mode . eglot-ensure-unless-tramp)
 789  (tsx-ts-mode . eglot-ensure-unless-tramp))
 790
 791(setq major-mode-remap-alist
 792      '((python-mode . python-ts-mode)
 793	(go-mode . go-ts-mode)
 794	(javascript-mode . js-ts-mode)
 795	(js-mode . js-ts-mode)
 796	(typescript-mode . typescript-ts-mode)))
 797
 798(use-package markdown-mode
 799  :mode (("README\\.md\\'" . gfm-mode)
 800         ("\\.md\\'" . markdown-mode)
 801         ("\\.markdown\\'" . markdown-mode))
 802  :hook ((markdown-mode . visual-line-mode)
 803	 (mardown-mode . visual-wrap-prefix-mode)
 804	 (gfm-mode . visual-line-mode)
 805	 (gfm-mode . visual-wrap-prefix-mode)))
 806
 807
 808(use-package yaml-ts-mode
 809  :mode "\\.ya?ml\\'"
 810  :hook ((yaml-ts-mode . display-line-numbers-mode)
 811	 (yaml-ts-mode . outline-minor-mode)
 812	 (yaml-ts-mode . electric-pair-local-mode))
 813  :config
 814  (setq-local outline-regexp "^ *\\([A-Za-z0-9_-]*: *[>|]?$\\|-\\b\\)")
 815  (font-lock-add-keywords
 816   'yaml-ts-mode
 817   '(("\\($(\\(workspaces\\|context\\|params\\)\.[^)]+)\\)" 1 'font-lock-constant-face prepend)
 818     ("kind:\s*\\(.*\\)\n" 1 'font-lock-keyword-face prepend))))
 819
 820;; Tekton LSP for YAML files containing tekton.dev apiVersion
 821;; TODO: replace with just "tekton-lsp" once it's in $PATH
 822(with-eval-after-load 'eglot
 823  (add-to-list 'eglot-server-programs
 824	       `(yaml-ts-mode . (,(expand-file-name "~/src/tektoncd/tekton-lsp-go/tekton-lsp")))))
 825(defun vde/maybe-start-tekton-lsp ()
 826  "Start tekton-lsp via eglot if the buffer contains a Tekton apiVersion."
 827  (when (and buffer-file-name
 828	     (not (file-remote-p default-directory))
 829	     (save-excursion
 830	       (goto-char (point-min))
 831	       (re-search-forward "tekton\\.dev\\|triggers\\.tekton\\.dev" nil t)))
 832    (eglot-ensure)))
 833(add-hook 'yaml-ts-mode-hook #'vde/maybe-start-tekton-lsp)
 834
 835(use-package orgalist
 836  :commands (orgalist-mode)
 837  :hook ((markdown-mode . orgalist-mode)
 838	 (gfm-mode . orgalist-mode)))
 839
 840(use-package dockerfile-ts-mode
 841  :mode (("Dockerfile\\'" . dockerfile-ts-mode)
 842	 ("\\.Dockerfile\\'" . dockerfile-ts-mode-map)
 843	 ("Containerfile\\'" . dockerfile-ts-mode)
 844	 ("\\.Containerfile\\'" . dockerfile-ts-mode-map)))
 845
 846(use-package go-ts-mode
 847  :mode (("\\.go$" . go-ts-mode)
 848         ("\\.go" . go-ts-mode)
 849         ("\\.go\\'" . go-ts-mode))
 850  :hook ((go-ts-mode . vde/go-mode-setup))
 851  :config
 852  (defun vde/go-mode-setup ()
 853    "Setup for go-mode."
 854    (setq-local ff-other-file-alist
 855		'(("_test\\.go\\'" (".go"))
 856		  ("\\.go\\'" ("_test.go"))
 857		  ))))
 858
 859(use-package gotest-ts
 860  :after go-ts-mode
 861  :hook (go-ts-mode . gotest-ts-setup))
 862
 863(use-package lua-ts-mode
 864  :mode "\\.lua\\'"
 865  :hook (lua-ts-mode . eglot-ensure-unless-tramp))
 866
 867(use-package nix-ts-mode
 868  :if (executable-find "nix")
 869  :mode ("\\.nix\\'" "\\.nix.in\\'"))
 870
 871(use-package terraform-ts-mode
 872  :if (executable-find "terraform-ls")
 873  :mode ("\\.tf\\'" "\\.tfvars\\'"))
 874
 875(use-package js-ts-mode
 876  :mode (("\\.js\\'" . js-ts-mode)
 877         ("\\.mjs\\'" . js-ts-mode)
 878         ("\\.cjs\\'" . js-ts-mode)))
 879
 880(use-package typescript-ts-mode
 881  :mode (("\\.ts\\'" . typescript-ts-mode)
 882         ("\\.mts\\'" . typescript-ts-mode)
 883         ("\\.cts\\'" . typescript-ts-mode)))
 884
 885(use-package tsx-ts-mode
 886  :mode ("\\.tsx\\'" "\\.jsx\\'"))
 887
 888(use-package nix-drv-mode
 889  :if (executable-find "nix")
 890  :after nix-mode
 891  :mode "\\.drv\\'")
 892
 893(use-package nix-shell
 894  :if (executable-find "nix")
 895  :after nix-mode
 896  :commands (nix-shell-unpack nix-shell-configure nix-shell-build))
 897
 898(use-package nixpkgs-fmt
 899  :if (executable-find "nix")
 900  :after nix-ts-mode
 901  :custom
 902  (nixpkgs-fmt-command "nixfmt")
 903  :config
 904  (add-hook 'nix-ts-mode-hook 'nixpkgs-fmt-on-save-mode))
 905
 906(use-package minions
 907  :hook (after-init . minions-mode)
 908  :config
 909  (defvar minions-prominent-modes)
 910  (add-to-list 'minions-prominent-modes 'flymake-mode))
 911
 912(use-package vundo
 913  :bind (("M-u"   . undo)
 914         ("M-U"   . undo-redo)
 915         ("C-x u" . vundo)))
 916
 917(use-package vde-vcs
 918  :commands (vde/gh-get-current-repo vde/vc-browse-remote)
 919  :bind (("C-x v B" . vde/vc-browse-remote)))
 920
 921(use-package project-func
 922  :commands (vde/project-magit-status vde/project-eat vde/project-vterm vde/project-run-in-vterm vde/project-ghostel vde/project-try-local vde/open-readme))
 923
 924(use-package project
 925  :commands (project-find-file project-find-regexp)
 926  :custom
 927  (project-switch-commands '((?f "File" project-find-file)
 928			     (?g "Grep" project-find-regexp)
 929			     (?d "Dired" project-dired)
 930			     (?b "Buffer" project-switch-to-buffer)
 931			     (?q "Query replace" project-query-replace-regexp)
 932			     (?v "VC dir" project-vc-dir)
 933			     (?m "Magit" vde/project-magit-status)
 934			     (?e "Eshell" project-eshell)
 935			     (?E "Eat" vde/project-eat)
 936			     (?s "Vterm" vde/project-vterm)
 937			     (?t "Ghostel" vde/project-ghostel)
 938			     (?R "README" vde/open-readme)
 939			     (?g "Checkout GitHub PR" checkout-github-pr)))
 940  (project-mode-line t)
 941  (project-compilation-buffer-name-function 'project-prefixed-buffer-name)
 942  ;; (project-vc-extra-root-markers '(".project" "Cargo.toml" "pyproject.toml" "requirements.txt" "go.mod"))
 943  :bind
 944  ("C-x p v" . vde/project-magit-status)
 945  ("C-x p s" . vde/project-vterm)
 946  ("C-x p X" . vde/project-run-in-vterm)
 947  ("C-x p E" . vde/project-eat)
 948  ("C-x p t" . vde/project-ghostel)
 949  ("C-x p G" . checkout-github-pr)
 950  ("C-x p F" . flymake-show-project-diagnostics)
 951  ("C-x p R" . vde/project-refresh-list)
 952  :config
 953  (setq project-list-exclude
 954        '("/build\\(/\\|\\'\\)" "/node_modules\\(/\\|\\'\\)"
 955          "/.cache\\(/\\|\\'\\)" "/target\\(/\\|\\'\\)" "/.direnv\\(/\\|\\'\\)"))
 956
 957  (defun vde/project-refresh-list ()
 958    "Rescan ~/src for projects."
 959    (interactive)
 960    (setq project--list nil)  ; Clear instead of checking each zombie
 961    (project-remember-projects-under "~/src" t)
 962    (message "Project list refreshed"))
 963
 964  ;; Populate project list on first run if empty
 965  (unless project--list
 966    (project-remember-projects-under "~/src" t)))
 967
 968(use-package eshell
 969  :commands (eshell eshell-here)
 970  :config
 971  (defvar eshell-last-dir-ring)
 972  (defvar consult-dir-sources)
 973  (declare-function eshell-find-previous-directory "eshell")
 974  (declare-function consult-dir--pick "consult-dir")
 975  (declare-function ring-elements "ring")
 976  (declare-function eshell/cd "eshell")
 977  (add-to-list 'eshell-modules-list 'eshell-rebind)
 978
 979  (defun eshell-here ()
 980    "Open eshell in the directory of the current buffer's file."
 981    (interactive)
 982    (let* ((parent (if (buffer-file-name)
 983                       (file-name-directory (buffer-file-name))
 984                     default-directory))
 985           (name (car (last (split-string parent "/" t)))))
 986      (eshell "new")
 987      (rename-buffer (concat "*eshell: " name "*"))))
 988
 989  ;; Handy aliases
 990  (defalias 'ff 'find-file)
 991  (defalias 'e 'find-file)
 992  (defalias 'd 'dired)
 993
 994  (defun eshell/cdg ()
 995    "Change directory to the project's root."
 996    (eshell/cd (locate-dominating-file default-directory ".git")))
 997
 998  (defun eshell/j (&optional regexp)
 999    "Navigate to a previously visited directory in eshell."
1000    (let ((eshell-dirs (delete-dups
1001                        (mapcar 'abbreviate-file-name
1002                                (ring-elements eshell-last-dir-ring)))))
1003      (cond
1004       ((and (not regexp) (featurep 'consult-dir))
1005        (let* ((consult-dir--source-eshell `(:name "Eshell"
1006                                                   :narrow ?e
1007                                                   :category file
1008                                                   :face consult-file
1009                                                   :items ,eshell-dirs))
1010               (consult-dir-sources (cons consult-dir--source-eshell
1011                                          consult-dir-sources)))
1012          (eshell/cd (substring-no-properties
1013                      (consult-dir--pick "Switch directory: ")))))
1014       (t (eshell/cd (if regexp (eshell-find-previous-directory regexp)
1015                       (completing-read "cd: " eshell-dirs)))))))
1016
1017  ;; Use system su/sudo instead of eshell builtins
1018  (with-eval-after-load "em-unix"
1019    (unintern 'eshell/su nil)
1020    (unintern 'eshell/sudo nil))
1021
1022  :hook (eshell-mode . with-editor-export-editor))
1023
1024(use-package eat
1025  :commands (eat)
1026  :init
1027  (defvar eat-kill-buffer-on-exit)
1028  (defvar eat-enable-yank-to-terminal)
1029  (setq-default explicit-shell-file-name "zsh"
1030                shell-file-name "zsh")
1031  (setq eat-kill-buffer-on-exit t
1032        eat-enable-yank-to-terminal t)
1033  :hook ((eshell-mode . eat-eshell-mode)
1034         (eshell-mode . eat-eshell-visual-command-mode)))
1035
1036(use-package vterm
1037  :commands (vterm)
1038  :custom
1039  (vterm-kill-buffer-on-exit t)
1040  (vterm-max-scrollback 100000))
1041
1042(use-package ghostel
1043  :commands (ghostel)
1044  :bind ("C-c t g" . vde/ghostel-toggle)
1045  :custom
1046  (ghostel-keymap-exceptions
1047   '("C-c" "C-x" "C-u" "C-h" "M-x" "M-:" "C-\\"
1048     "S-<up>" "S-<down>" "S-<left>" "S-<right>"
1049     "M-S-<up>" "M-S-<down>" "M-S-<left>" "M-S-<right>"))
1050  :config
1051  (defun vde/ghostel-buffer ()
1052    "Return the active ghostel buffer, or nil if none exists."
1053    (seq-find (lambda (buf)
1054                (string-match-p "\\*ghostel:" (buffer-name buf)))
1055              (buffer-list)))
1056  (defun vde/ghostel-toggle ()
1057    "Toggle ghostel terminal buffer."
1058    (interactive)
1059    (let ((buf (vde/ghostel-buffer)))
1060      (cond
1061       ((not buf) (ghostel))
1062       ((eq (current-buffer) buf) (bury-buffer))
1063       (t (switch-to-buffer buf))))))
1064
1065;; TODO adapt this to my needs
1066;; (defun tkj/vc-git-grep-current-line ()
1067;;   "Search Git project for the current line."
1068;;   (interactive)
1069;;   (let ((current-line (string-trim (thing-at-point 'line t))))
1070;;     (if (string-empty-p current-line)
1071;;         (message "No line in sight")
1072;;       (vc-git-grep current-line "*" (vc-git-root default-directory)))))
1073;; (global-set-key (kbd "C-c p l") 'tkj/vc-git-grep-current-line)
1074;; 
1075;; (defun tkj/vc-git-grep-symbol ()
1076;;   "Search Git project for the symbol at point. This could be any word or
1077;; programming symbol, like a function or variable."
1078;;   (interactive)
1079;;   (let ((symbol (string-trim (thing-at-point 'symbol t))))
1080;;     (if (string-empty-p symbol)
1081;;         (message "You point at nothing")
1082;;       (vc-git-grep symbol "*" (vc-git-root default-directory)))))
1083;; (global-set-key (kbd "C-c p s") 'tkj/vc-git-grep-symbol)
1084
1085(use-package vc
1086  :custom
1087  (vc-allow-rewriting-published-history nil "safety guard for history rewriting")
1088  (vc-async-checkin t "async commits for git/hg")
1089  (vc-no-confirm-moving-changes nil "ask before moving changes between worktrees"))
1090
1091(use-package vc-dir
1092  :bind (("C-x v D" . project-vc-dir)
1093         :map vc-dir-mode-map
1094         ("=" . vc-diff)
1095         ("v" . vc-next-action)
1096         ("C-c C-c" . vc-next-action))
1097  :custom
1098  (vc-dir-hide-up-to-date-on-revert t "hide up-to-date files after refresh"))
1099
1100(use-package vc-git
1101  :custom
1102  (vc-git-diff-switches '("--histogram" "--stat"))
1103  (vc-git-print-log-follow t "follow renames in file log"))
1104
1105(use-package log-edit
1106  :custom
1107  (log-edit-hook '(log-edit-insert-cvs-template
1108                   log-edit-show-files
1109                   log-edit-maybe-show-diff))
1110  :bind (:map log-edit-mode-map
1111         ("C-c C-d" . log-edit-show-diff)))
1112
1113(use-package diff-mode
1114  :bind (:map diff-mode-map
1115         ("v" . vc-next-action)))
1116
1117(use-package magit
1118  :unless noninteractive
1119  :commands (magit-status magit-clone magit-pull magit-blame magit-log-buffer-file magit-log magit-file-dispatch)
1120  :bind (("C-c v c" . magit-commit)
1121         ("C-c v C" . magit-checkout)
1122         ("C-c v b" . magit-branch)
1123         ("C-c v d" . magit-dispatch)
1124         ("C-c v f" . magit-fetch)
1125         ("C-c v g" . magit-blame)
1126         ("C-c v l" . magit-log-buffer-file)
1127         ("C-c v L" . magit-log)
1128         ("C-c v p" . magit-pull)
1129         ("C-c v P" . magit-push)
1130         ("C-c v r" . magit-rebase)
1131	 ("C-c v s" . magit-stage)
1132         ("C-c v v" . magit-status)
1133	 )
1134  :custom
1135  (magit-save-repository-buffers 'dontask)
1136  (magit-refs-show-commit-count 'all)
1137  (magit-branch-prefer-remote-upstream '("main"))
1138  (magit-display-buffer-function #'magit-display-buffer-fullframe-status-v1)
1139  (magit-bury-buffer-function #'magit-restore-window-configuration)
1140  (magit-refresh-status-buffer nil "don't automatically refresh the status buffer after running a git command")
1141  (magit-commit-show-diff nil "don't show the diff by default in the commit buffer. Use `C-c C-d' to display it")
1142  (magit-branch-direct-configure nil "don't show git variables in magit branch")
1143  (magit-diff-visit-prefer-worktree t "prefer visiting the worktree file")
1144  :config
1145  (declare-function magit-insert-worktrees "magit")
1146  (add-hook 'magit-status-sections-hook #'magit-insert-worktrees t)
1147  ;; cargo-culted from https://github.com/magit/magit/issues/3717#issuecomment-734798341
1148  ;; valid gitlab options are defined in https://docs.gitlab.com/ee/user/project/push_options.html
1149  ;;
1150  ;; the second argument to transient-append-suffix is where to append
1151  ;; to, not sure what -u is, but this works
1152  (transient-append-suffix 'magit-push "-u"
1153    '(1 "=s" "Skip gitlab pipeline" "--push-option=ci.skip"))
1154  (transient-append-suffix 'magit-push "=s"
1155    '(1 "=m" "Create gitlab merge-request" "--push-option=merge_request.create"))
1156  (transient-append-suffix 'magit-push "=m"
1157    '(1 "=o" "Set push option" "--push-option="))  ;; Will prompt, can only set one extra
1158  )
1159
1160(use-package git-commit
1161  :init
1162  (defvar git-commit-mode-map)
1163  :bind (:map git-commit-mode-map
1164	      ("C-c C-h" . git-commit-co-authored)))
1165
1166(use-package ediff
1167  :commands (ediff ediff-files ediff-merge ediff3 ediff-files3 ediff-merge3)
1168  :custom
1169  (ediff-window-setup-function 'ediff-setup-windows-plain)
1170  (ediff-split-window-function 'split-window-horizontally)
1171  (ediff-diff-options "-w")
1172  :hook
1173  (ediff-after-quit-hook-internal . winner-undo))
1174
1175(use-package diff
1176  :custom
1177  (diff-default-read-only nil)
1178  (diff-advance-after-apply-hunk t)
1179  (diff-update-on-the-fly t)
1180  (diff-refine 'font-lock)
1181  (diff-font-lock-prettify nil)
1182  (diff-font-lock-syntax nil))
1183
1184(use-package gitconfig-mode
1185  :commands (gitconfig-mode)
1186  :mode (("/\\.gitconfig\\'"  . gitconfig-mode)
1187         ("/\\.git/config\\'" . gitconfig-mode)
1188         ("/git/config\\'"    . gitconfig-mode)
1189         ("/\\.gitmodules\\'" . gitconfig-mode)))
1190
1191(use-package gitignore-mode
1192  :commands (gitignore-mode)
1193  :mode (("/\\.gitignore\\'"        . gitignore-mode)
1194         ("/\\.git/info/exclude\\'" . gitignore-mode)
1195         ("/git/ignore\\'"          . gitignore-mode)))
1196
1197(use-package gitattributes-mode
1198  :commands (gitattributes-mode)
1199  :mode (("/\\.gitattributes" . gitattributes-mode)))
1200
1201(use-package diff-hl
1202  :init
1203  (defvar diff-hl-command-map)
1204  :hook (find-file . diff-hl-mode)
1205  :hook (prog-mode . diff-hl-mode)
1206  :hook (magit-post-refresh . diff-hl-magit-post-refresh)
1207  :bind
1208  (:map diff-hl-command-map
1209	("n" . diff-hl-next-hunk)
1210	("p" . diff-hl-previous-hunk)
1211	("[" . nil)
1212	("]" . nil)
1213	("DEL"   . diff-hl-revert-hunk)
1214	("<delete>" . diff-hl-revert-hunk)
1215	("SPC" . diff-hl-mark-hunk)
1216	:map vc-prefix-map
1217	("n" . diff-hl-next-hunk)
1218	("p" . diff-hl-previous-hunk)
1219	("s" . diff-hl-stage-dwim)
1220	("DEL"   . diff-hl-revert-hunk)
1221	("<delete>" . diff-hl-revert-hunk)
1222	("SPC" . diff-hl-mark-hunk))
1223  :config
1224  (put 'diff-hl-inline-popup-hide
1225       'repeat-map 'diff-hl-command-map))
1226
1227(use-package diff-hl-show-hunk
1228  :after (diff-hl))
1229
1230(use-package diff-hl-dired
1231  :after (diff-hl)
1232  :hook (dired-mode . diff-hl-dired-mode))
1233
1234(use-package corfu
1235  :init
1236  (defvar corfu-map)
1237  :custom
1238  (corfu-auto 't)
1239  (corfu-auto-delay 1)
1240  (corfu-cycle 't)
1241  (corfu-preselect 'prompt)
1242  :bind
1243  (:map corfu-map
1244        ("TAB" . corfu-next)
1245	("C-c" . corfu-quit)
1246        ([tab] . corfu-next)
1247        ("S-TAB" . corfu-previous)
1248        ([backtab] . corfu-previous))
1249  :hook
1250  (after-init . global-corfu-mode))
1251
1252(use-package corfu-history
1253  :after (corfu)
1254  :hook
1255  (after-init . corfu-history-mode))
1256
1257(use-package corfu-popupinfo
1258  :after corfu
1259  :init
1260  (declare-function corfu-popupinfo-mode "corfu-popupinfo")
1261  :config
1262  (corfu-popupinfo-mode 1))
1263
1264(use-package corfu-terminal
1265  :unless (or (display-graphic-p) (>= emacs-major-version 31))
1266  :ensure t
1267  :hook
1268  (after-init . corfu-terminal-mode))
1269
1270(use-package envrc
1271  :defer 2
1272  :if (executable-find "direnv")
1273  :init
1274  (defvar envrc-mode-map)
1275  (declare-function envrc-global-mode "envrc")
1276  :bind (:map envrc-mode-map
1277              ("C-c e" . envrc-command-map))
1278  :custom
1279  (envrc-remote t)
1280  :config (envrc-global-mode))
1281
1282(use-package cape
1283  :init
1284  (declare-function cape-dabbrev "cape")
1285  (declare-function cape-file "cape")
1286  (declare-function cape-elisp-block "cape")
1287  (add-hook 'completion-at-point-functions #'cape-dabbrev)
1288  (add-hook 'completion-at-point-functions #'cape-file)
1289  (add-hook 'completion-at-point-functions #'cape-elisp-block))
1290
1291(use-package winner
1292  :unless noninteractive
1293  :hook
1294  (after-init . winner-mode))
1295
1296(use-package windmove
1297  :bind
1298  ("S-<up>" . windmove-up)
1299  ("S-<left>" . windmove-left)
1300  ("S-<right>" . windmove-right)
1301  ("S-<down>" . windmove-down)
1302  ("M-S-<up>" . windmove-swap-states-up)
1303  ("M-S-<left>" . windmove-swap-states-left)
1304  ("M-S-<right>" . windmove-swap-states-right)
1305  ("M-S-<down>" . windmove-swap-states-down))
1306
1307(use-package window
1308  :unless noninteractive
1309  :commands (shrink-window-horizontally shrink-window enlarge-window-horizontally enlarge-window)
1310  :bind (("S-C-<left>" . shrink-window-horizontally)
1311         ("S-C-<right>" . enlarge-window-horizontally)
1312         ("S-C-<down>" . shrink-window)
1313         ("S-C-<up>" . enlarge-window)))
1314
1315;; Prefer ripgrep (rg) if present (instead of grep)
1316(setq xref-search-program
1317      (cond
1318       ((or (executable-find "ripgrep")
1319            (executable-find "rg"))
1320        'ripgrep)
1321       ((executable-find "ugrep")
1322        'ugrep)
1323       (t
1324        'grep)))
1325
1326(use-package rg
1327  :if (executable-find "rg")
1328  :commands (rg rg-project rg-dwim)
1329  :bind (("M-s r r" . rg)
1330         ("M-s r p" . rg-project)
1331         ("M-s r s" . rg-dwim))
1332  :custom
1333  (rg-group-result t)
1334  (rg-hide-command t)
1335  (rg-show-columns nil)
1336  (rg-show-header t)
1337  (rg-default-alias-fallback "all")
1338  :config
1339  (defvar rg-custom-type-aliases)
1340  (defvar rg-buffer-name)
1341  (cl-pushnew '("tmpl" . "*.tmpl") rg-custom-type-aliases :test #'equal)
1342  (cl-pushnew '("gotest" . "*_test.go") rg-custom-type-aliases :test #'equal)
1343  (defun vde/rg-buffer-name ()
1344    "Generate a rg buffer name from project if in one"
1345    (let ((p (project-root (project-current))))
1346      (if p
1347	  (format "rg: %s" (abbreviate-file-name p))
1348	"rg")))
1349  (declare-function vde/rg-buffer-name "init")
1350  (setq rg-buffer-name #'vde/rg-buffer-name))
1351
1352(use-package wgrep
1353  :unless noninteractive
1354  :commands (wgrep-change-to-wgrep-mode)
1355  :init
1356  (defvar grep-mode-map)
1357  :custom
1358  (wgrep-auto-save-buffer t)
1359  (wgrep-change-readonly-file t)
1360  :bind (:map grep-mode-map
1361              ("e" . wgrep-change-to-wgrep-mode)
1362              ("C-x C-q" . wgrep-change-to-wgrep-mode)))
1363
1364(use-package abbrev
1365  :ensure nil
1366  :custom
1367  (save-abbrevs nil)
1368  :config
1369  (define-abbrev-table 'global-abbrev-table
1370    '(;; Arrows
1371      ("ra" "→")
1372      ("la" "←")
1373      ("ua" "↑")
1374      ("da" "↓")
1375
1376      ;; Emojis for context markers
1377      ;; ("todo"  "👷 TODO:")
1378      ;; ("fixme" "🔥 FIXME:")
1379      ;; ("note"  "📎 NOTE:")
1380      ;; ("hack"  "👾 HACK:")
1381      ("smile"  "😄")
1382      ("party" "🎉")
1383      ("up"  "☝️")
1384      ("applause" "👏")
1385      ("manyapplauses" "👏👏👏👏👏👏👏👏")
1386      ("heart" "❤️")
1387
1388      ;; NerdFonts
1389      ("nerdfolder" "")
1390      ("nerdgit" "")
1391      ("nerdemacs" "")
1392
1393      ;; Markdown
1394      ("cb" "```@\n\n```"
1395       (lambda () (search-backward "@") (delete-char 1)))
1396
1397      ;; ORG
1398      ("ocb" "#+BEGIN_SRC @\n\n#+END_SRC"
1399       (lambda () (search-backward "@") (delete-char 1))))))
1400
1401(use-package tempel
1402  :custom (tempel-path (expand-file-name "templates" user-emacs-directory))
1403  :bind (("M-+" . tempel-complete) ;; Alternative tempel-expand
1404         ("M-*" . tempel-insert)))
1405
1406(use-package consult
1407  :bind
1408  ("M-g M-g" . consult-goto-line)
1409  ("M-K" . consult-keep-lines)
1410  ("M-s M-b" . consult-buffer)
1411  ("M-s M-f" . consult-find)
1412  ("M-s M-g" . consult-grep)
1413  ("M-s M-r" . consult-ripgrep)
1414  ("M-s M-h" . consult-history)
1415  ("M-s M-l" . consult-line)
1416  ("M-s M-m" . consult-mark)
1417  ("M-s M-y" . consult-yank-pop)
1418  ("M-s M-s" . consult-outline))
1419
1420(use-package consult-vc-modified-files
1421  :bind
1422  ("C-c v ." . consult-vc-log-select-files)
1423  ("C-c v m" . consult-vc-modified-files))
1424
1425(use-package surround
1426  :bind-keymap ("M-'" . surround-keymap))
1427
1428(use-package visual-regexp
1429  :bind (([remap query-replace-regexp] . vr/query-replace)))
1430
1431(use-package dape
1432  :commands (dape dape-breakpoint-toggle))
1433
1434(use-package editorconfig
1435  :config
1436  (editorconfig-mode 1))
1437
1438(use-package eldoc-box
1439  :commands (eldoc-box-help-at-point)
1440  :bind ("C-c h" . eldoc-box-help-at-point))
1441
1442(use-package dired-narrow
1443  :after dired
1444  :bind (:map dired-mode-map
1445         ("/" . dired-narrow)))
1446
1447(use-package dwim-shell-command
1448  :bind (([remap shell-command] . dwim-shell-command)
1449         ([remap async-shell-command] . dwim-shell-command)))
1450
1451(use-package org-rich-yank
1452  :after org
1453  :bind (:map org-mode-map
1454         ("C-M-y" . org-rich-yank)))
1455
1456(use-package org-tree-slide
1457  :after org
1458  :commands (org-tree-slide-mode)
1459  :bind (:map org-mode-map
1460         ("<f8>" . org-tree-slide-mode))
1461  :custom
1462  (org-tree-slide-slide-in-effect nil))
1463
1464(use-package avy
1465  :unless noninteractive
1466  :commands (avy-goto-char avy-goto-line avy-goto-word-1 avy-pop-mark avy-goto-char-timer)
1467  :bind (("M-j" . avy-goto-char-timer)
1468         ("C-c j w" . avy-goto-word-1)
1469         ("C-c j b" . avy-pop-mark)
1470         ("C-c j l" . avy-goto-line))
1471  :custom
1472  (avy-dispatch-alist
1473   '((?k . avy-action-kill-move)
1474     (?K . avy-action-kill-stay)
1475     (?y . avy-action-copy)
1476     (?m . avy-action-mark)
1477     (?o . avy-action-embark)
1478     (?z . avy-action-zap-to-char)))
1479  :config
1480  (defun avy-action-embark (pt)
1481    (unwind-protect
1482        (save-excursion
1483          (goto-char pt)
1484          (embark-act))
1485      (select-window
1486       (cdr (ring-ref avy-ring 0))))
1487    t))
1488
1489(use-package embark
1490  :unless noninteractive
1491  :commands (embark-act embark-dwim embark-prefix-help-command)
1492  :bind
1493  ("C-," . embark-act)
1494  ("M-," . embark-dwim)
1495  ("C-h b" . embark-bindings)
1496  ("C-h B" . embark-bindings-at-point)
1497  ("C-h M" . embark-bindings-in-keymap)
1498  (:map completion-list-mode-map
1499        ("," . embark-act))
1500  :custom
1501  (prefix-help-command #'embark-prefix-help-command)
1502  (embark-cycle-key ".")
1503  (embark-help-key "?")
1504  :config
1505  (defvar embark-action-indicator)
1506  (defvar embark-become-indicator)
1507  (defvar embark-symbol-map)
1508  (defvar embark-file-map)
1509  (defvar embark-buffer-map)
1510  (defvar embark-general-map)
1511  (defvar embark-keymap-alist)
1512  (defvar embark-target-finders)
1513  ;; which-key integration for better discoverability
1514  (declare-function which-key--show-keymap "which-key")
1515  (declare-function which-key--hide-popup-ignore-command "which-key")
1516  (setq embark-action-indicator
1517        (lambda (map _target)
1518          (which-key--show-keymap "Embark" map nil nil 'no-paging)
1519          #'which-key--hide-popup-ignore-command)
1520        embark-become-indicator embark-action-indicator)
1521
1522  ;; Symbol overlay actions
1523  (keymap-set embark-symbol-map "S i" #'symbol-overlay-put)
1524  (keymap-set embark-symbol-map "S c" #'symbol-overlay-remove-all)
1525  (keymap-set embark-symbol-map "S r" #'symbol-overlay-rename)
1526
1527  ;; File management actions
1528  (define-key embark-file-map "c" #'copy-file)
1529  (define-key embark-file-map "m" #'rename-file)
1530  (define-key embark-file-map "D" #'delete-file)
1531  (define-key embark-file-map "=" #'ediff-files)
1532
1533  ;; Open file as root
1534  (defun my/sudo-find-file (file)
1535    "Open FILE as root."
1536    (interactive "fFile: ")
1537    (find-file (concat "/sudo::" (expand-file-name file))))
1538  (declare-function my/sudo-find-file "init")
1539  (define-key embark-file-map "S" #'my/sudo-find-file)
1540
1541  ;; Buffer actions
1542  (defun my/kill-buffer-and-window ()
1543    "Kill current buffer and close its window."
1544    (interactive)
1545    (kill-buffer-and-window))
1546  (declare-function my/kill-buffer-and-window "init")
1547  (define-key embark-buffer-map "K" #'my/kill-buffer-and-window)
1548  (define-key embark-buffer-map "r" #'revert-buffer)
1549
1550  ;; Project/Git actions
1551  (define-key embark-file-map "p" #'project-find-file)
1552  (define-key embark-file-map "g" #'magit-file-dispatch)
1553
1554  (declare-function vc-root-dir "vc")
1555  (declare-function magit-status "magit")
1556  (defun my/magit-status-from-file ()
1557    "Open magit-status for the file's repository."
1558    (interactive)
1559    (magit-status (vc-root-dir)))
1560  (declare-function my/magit-status-from-file "init")
1561  (define-key embark-file-map "G" #'my/magit-status-from-file)
1562
1563  ;; Org-mode heading actions
1564  (declare-function org-refile "org")
1565  (declare-function org-todo "org")
1566  (declare-function org-schedule "org")
1567  (declare-function org-deadline "org")
1568  (declare-function org-archive-subtree "org")
1569  (declare-function org-narrow-to-subtree "org")
1570  (defvar-keymap embark-org-heading-map
1571    :doc "Actions for org headings"
1572    :parent embark-general-map
1573    "r" #'org-refile
1574    "t" #'org-todo
1575    "s" #'org-schedule
1576    "d" #'org-deadline
1577    "a" #'org-archive-subtree
1578    "n" #'org-narrow-to-subtree)
1579
1580  (add-to-list 'embark-keymap-alist '(org-heading . embark-org-heading-map))
1581
1582  ;; Custom target finders
1583  (declare-function thing-at-point-looking-at "thingatpt")
1584  (defun embark-target-github-issue ()
1585    "Target GitHub issue numbers like #123 or owner/repo#456."
1586    (when (thing-at-point-looking-at
1587           "\\(?:\\([a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+\\)?#\\([0-9]+\\)\\)")
1588      (let ((_repo (match-string 1))
1589            (_number (match-string 2)))
1590        `(github-issue
1591          ,(match-string 0)
1592          ,(match-beginning 0)
1593          . ,(match-end 0)))))
1594
1595  (defun embark-target-jira-ticket ()
1596    "Target Jira tickets like PROJ-123."
1597    (when (thing-at-point-looking-at "\\([A-Z]+-[0-9]+\\)")
1598      `(jira-ticket
1599        ,(match-string 1)
1600        ,(match-beginning 0)
1601        . ,(match-end 0))))
1602
1603  (add-to-list 'embark-target-finders 'embark-target-github-issue)
1604  (add-to-list 'embark-target-finders 'embark-target-jira-ticket)
1605
1606  ;; GitHub issue actions
1607  (declare-function my/open-github-issue "init")
1608  (declare-function my/copy-github-issue-url "init")
1609  (defvar-keymap embark-github-issue-map
1610    :doc "Actions for GitHub issues/PRs"
1611    :parent embark-general-map
1612    "o" #'my/open-github-issue
1613    "c" #'my/copy-github-issue-url
1614    "b" #'browse-url)
1615
1616  (declare-function my/open-jira-ticket "init")
1617  (declare-function my/copy-jira-ticket-url "init")
1618  (defvar-keymap embark-jira-ticket-map
1619    :doc "Actions for Jira tickets"
1620    :parent embark-general-map
1621    "o" #'my/open-jira-ticket
1622    "c" #'my/copy-jira-ticket-url
1623    "b" #'browse-url)
1624
1625  (add-to-list 'embark-keymap-alist '(github-issue . embark-github-issue-map))
1626  (add-to-list 'embark-keymap-alist '(jira-ticket . embark-jira-ticket-map))
1627
1628  ;; Helper functions
1629  (defun my/open-github-issue (issue)
1630    "Open GitHub ISSUE in browser."
1631    (let* ((parts (split-string issue "#"))
1632           (repo (or (car parts) (vde/gh-get-current-repo)))
1633           (number (cadr parts))
1634           (url (format "https://github.com/%s/issues/%s" repo number)))
1635      (browse-url url)))
1636
1637  (defun my/copy-github-issue-url (issue)
1638    "Copy GitHub ISSUE URL to clipboard."
1639    (let* ((parts (split-string issue "#"))
1640           (repo (or (car parts) (vde/gh-get-current-repo)))
1641           (number (cadr parts))
1642           (url (format "https://github.com/%s/issues/%s" repo number)))
1643      (kill-new url)
1644      (message "Copied: %s" url)))
1645
1646  (defun my/open-jira-ticket (ticket)
1647    "Open Jira TICKET in browser."
1648    (let ((url (format "https://issues.redhat.com/browse/%s" ticket)))
1649      (browse-url url)))
1650
1651  (defun my/copy-jira-ticket-url (ticket)
1652    "Copy Jira TICKET URL to clipboard."
1653    (let ((url (format "https://issues.redhat.com/browse/%s" ticket)))
1654      (kill-new url)
1655      (message "Copied: %s" url)))
1656
1657  ;; Schedule actions (daily-plan integration)
1658  (declare-function my/schedule-jira-ticket "init")
1659  (declare-function my/schedule-github-issue "init")
1660
1661  (defun my/schedule-jira-ticket (ticket)
1662    "Schedule Jira TICKET via daily-plan."
1663    (let ((date (org-read-date nil nil nil (format "Schedule %s for: " ticket))))
1664      (message "%s" (string-trim (shell-command-to-string
1665                                  (format "daily-plan schedule %s %s" ticket date))))))
1666
1667  (defun my/schedule-github-issue (issue)
1668    "Schedule GitHub ISSUE via daily-plan."
1669    (let ((date (org-read-date nil nil nil (format "Schedule %s for: " issue))))
1670      (message "%s" (string-trim (shell-command-to-string
1671                                  (format "daily-plan schedule %s %s" issue date))))))
1672
1673  (define-key embark-jira-ticket-map "s" #'my/schedule-jira-ticket)
1674  (define-key embark-github-issue-map "s" #'my/schedule-github-issue)
1675  )
1676
1677(use-package embark-consult
1678  :after (embark consult)
1679  :unless noninteractive)
1680
1681(use-package consult-gh
1682  :after (consult)
1683  :custom
1684  (consult-gh-show-preview t)
1685  (consult-gh-preview-key "C-o")
1686  (consult-gh-large-file-warning-threshold 2500000)
1687  (consult-gh-default-interactive-command #'consult-gh-transient)
1688  (consult-gh-prioritize-local-folder nil)
1689  (consult-gh-group-dashboard-by :reason)
1690  ;;;; Optional
1691  (consult-gh-repo-preview-major-mode nil) ; show readmes in their original format
1692  (consult-gh-preview-major-mode 'org-mode) ; use 'org-mode for editing comments, commit messages, ...
1693  :config
1694  ;; Remember visited orgs and repos across sessions
1695  (add-to-list 'savehist-additional-variables 'consult-gh--known-orgs-list)
1696  (add-to-list 'savehist-additional-variables 'consult-gh--known-repos-list))
1697
1698;; (consult-gh-issue-list "tektoncd/pipeline -- --assignee \"@me\"" t)
1699;; (consult-gh-pr-list "tektoncd/pipeline -- --assignee \"@me\"" t)
1700
1701(use-package consult-gh-embark
1702  :after (embark consult)
1703  :init
1704  (declare-function consult-gh-embark-mode "consult-gh-embark")
1705  :config
1706  (consult-gh-embark-mode +1))
1707
1708(use-package pr-review
1709  :commands (pr-review pr-review-open pr-review-submit-review)
1710  :custom
1711  (pr-review-ghub-host "api.github.com")
1712  (pr-review-notification-include-read nil)
1713  (pr-review-notification-include-unsubscribed nil))
1714
1715(use-package pr-review-search
1716  :commands (pr-review-search pr-review-search-open pr-review-current-repository pr-review-current-repository-search)
1717  :config
1718  (defun pr-review-current-repository-search (query)
1719    "Run pr-review-search on the current repository."
1720    (interactive "sSearch query: ")
1721    (pr-review-search (format "is:pr archived:false is:open repo:%s %s" (vde/gh-get-current-repo) query)))
1722  
1723  (defun pr-review-current-repository ()
1724    "Run pr-review-search on the current repository."
1725    (interactive)
1726    (pr-review-search (format "is:pr archived:false is:open repo:%s" (vde/gh-get-current-repo)))))
1727
1728(use-package shipit
1729  :commands (shipit)
1730  :after magit
1731  :custom
1732  (shipit-notifications-enabled nil) ; start with polling disabled, enable manually
1733  :config
1734  (shipit-init))
1735
1736(use-package jinx
1737  :hook (emacs-startup . global-jinx-mode)
1738  :bind (([remap ispell-word] . jinx-correct) ;; ("M-$" . jinx-correct)
1739         ("C-M-$" . jinx-languages)))
1740
1741(use-package eljira
1742  :commands (eljira)
1743  :ensure nil
1744  :load-path "~/src/github.com/sawwheet/eljira/"
1745  :custom
1746  (eljira-token (passage-get "redhat/issues/atlassian/token"))
1747  (eljira-username "vdemeest@redhat.com")
1748  (eljira-url "https://issues.redhat.com"))
1749
1750
1751;; TODO window management
1752;; TODO ORG mode configuration (BIG one)
1753(defvar org-link-any-re)
1754(declare-function dom-by-tag "dom")
1755(declare-function dom-text "dom")
1756(declare-function org-in-regexp "org")
1757(declare-function org-make-link-string "org")
1758(defun ar/org-insert-link-dwim ()
1759  "Like `org-insert-link' but with personal dwim preferences."
1760  (interactive)
1761  (let* ((point-in-link (org-in-regexp org-link-any-re 1))
1762         (clipboard-url (when (string-match-p "^http" (current-kill 0))
1763                          (current-kill 0)))
1764         (region-content (when (region-active-p)
1765                           (buffer-substring-no-properties (region-beginning)
1766                                                           (region-end)))))
1767    (cond ((and region-content clipboard-url (not point-in-link))
1768           (delete-region (region-beginning) (region-end))
1769           (insert (org-make-link-string clipboard-url region-content)))
1770          ((and clipboard-url (not point-in-link))
1771           (insert (org-make-link-string
1772                    clipboard-url
1773                    (read-string "title: "
1774                                 (with-current-buffer (url-retrieve-synchronously clipboard-url)
1775                                   (dom-text (car
1776                                              (dom-by-tag (libxml-parse-html-region
1777                                                           (point-min)
1778                                                           (point-max))
1779                                                          'title))))))))
1780          (t
1781           (call-interactively 'org-insert-link)))))
1782
1783(use-package org
1784  :if (file-exists-p org-directory)
1785  :init
1786  (declare-function bind-key--remove "bind-key")
1787  :mode (("\\.org$" . org-mode)
1788         ("\\.org.draft$" . org-mode))
1789  :commands (org-agenda org-capture)
1790  :bind (("C-c o l" . org-store-link)
1791         ("C-c o r r" . org-refile)
1792	 ;; ("C-c o r R" . vde/reload-org-refile-targets)
1793         ("C-c o a a" . org-agenda)
1794	 ;; ("C-c o a r" . vde/reload-org-agenda-files)
1795	 ;; ("C-c C-x i" . vde/org-clock-in-any-heading)
1796         ("C-c o s" . org-sort)
1797	 ("C-c O" . org-open-at-point-global)
1798         ("<f12>" . org-agenda)
1799	 :map org-mode-map
1800	 ("C-c C-l" . ar/org-insert-link-dwim))
1801  :custom
1802  (org-use-speed-commands t)
1803  (org-special-ctrl-a/e t)
1804  (org-special-ctrl-k t)
1805  (org-hide-emphasis-markers t)
1806  (org-pretty-entities t)
1807  (org-ellipsis "…")
1808  (org-return-follows-link t)
1809  (org-todo-keywords '((sequence "STRT(s)" "NEXT(n)" "TODO(t)" "WAIT(w)" "|" "DONE(d!)" "CANX(c@/!)")))
1810  (org-use-fast-todo-selection 'expert) ; Always show selection buffer with hints
1811  (org-todo-state-tags-triggers '(("CANX" ("CANX" . t))
1812                                  ("WAIT" ("WAIT" . t))
1813                                  (done ("WAIT"))
1814                                  ("TODO" ("WAIT") ("CANX"))
1815                                  ("NEXT" ("WAIT") ("CANX"))
1816                                  ("DONE" ("WAIT") ("CANX"))))
1817  (org-tag-alist
1818   '((:startgroup)
1819     ("Handson" . ?o)
1820     (:grouptags)
1821     ("Write" . ?w) ("Code" . ?c)
1822     (:endgroup)
1823
1824     (:startgroup)
1825     ("Handsoff" . ?f)
1826     (:grouptags)
1827     ("Read" . ?r) ("Watch" . ?W) ("Listen" . ?l)
1828     (:endgroup)))
1829  (org-log-done 'time)
1830  (org-log-redeadline 'time)
1831  (org-log-reschedule 'time)
1832  (org-log-into-drawer t)
1833  ;; https://jeffbradberry.com/posts/2025/05/orgmode-priority-cookies/
1834  ;; 1 2 and 3 are high, 4 is default, 5 is "hide / whenever or maybe never"
1835  (org-priority-highest 1)
1836  (org-priority-lowest  5)
1837  (org-priority-default 4)
1838  (org-list-demote-modify-bullet '(("+" . "-") ("-" . "+")))
1839  (org-agenda-file-regexp "^[a-zA-Z0-9-_]+.org$")
1840  (org-agenda-files `(,org-inbox-file ,org-todos-file ,org-habits-file ,org-reading-list-file ,org-calendar-file))
1841  ;; (org-refile-targets '((org-agenda-files :maxlevel . 3)))
1842  (org-refile-targets (vde/org-refile-targets))
1843  (org-refile-use-outline-path 'file)
1844  (org-refile-allow-creating-parent-nodes 'confirm)
1845  (org-agenda-remove-tags t)
1846  (org-agenda-span 'day)
1847  (org-agenda-start-on-weekday 1)
1848  (org-agenda-window-setup 'other-window)
1849  (org-agenda-sticky t)
1850  (org-agenda-sorting-strategy
1851   '((agenda deadline-up scheduled-up time-up todo-state-up priority-down)
1852     (todo todo-state-up priority-down deadline-up)
1853     (tags todo-state-up priority-down deadline-up)
1854     (search todo-state-up priority-down deadline-up)))
1855  (org-agenda-custom-commands
1856   '(
1857     ;; Archive tasks
1858     ("#" "To archive" todo "DONE|CANX")
1859     ;; TODO take inspiration from those
1860     ;; ("$" "Appointments" agenda* "Appointments")
1861     ;; ("b" "Week tasks" agenda "Scheduled tasks for this week"
1862     ;;  ((org-agenda-category-filter-preset '("-RDV")) ; RDV for Rendez-vous
1863     ;;   (org-agenda-use-time-grid nil)))
1864     ;; 
1865     ;; ;; Review started and next tasks
1866     ;; ("j" "STRT/NEXT" tags-todo "TODO={STRT\\|NEXT}")
1867     ;; 
1868     ;; ;; Review other non-scheduled/deadlined to-do tasks
1869     ;; ("k" "TODO" tags-todo "TODO={TODO}+DEADLINE=\"\"+SCHEDULED=\"\"")
1870     ;; 
1871     ;; ;; Review other non-scheduled/deadlined pending tasks
1872     ;; ("l" "WAIT" tags-todo "TODO={WAIT}+DEADLINE=\"\"+SCHEDULED=\"\"")
1873     ;; 
1874     ;; ;; Review upcoming deadlines for the next 60 days
1875     ;; ("!" "Deadlines all" agenda "Past/upcoming deadlines"
1876     ;;  ((org-agenda-span 1)
1877     ;;   (org-deadline-warning-days 60)
1878     ;;   (org-agenda-entry-types '(:deadline))))
1879
1880     ("d" "Daily Agenda"
1881      ((tags-todo "+PRIORITY=\"1\""
1882		  ((org-agenda-overriding-header "🔴 High Priority")))
1883       (agenda ""
1884	       ((org-agenda-span 'day)
1885		(org-deadline-warning-days 30)
1886		(org-agenda-overriding-header "📅 Schedule & Deadlines")))
1887       (todo "NEXT"
1888	     ((org-agenda-overriding-header "➡️ Focus (NEXT)")))))
1889     ("i" "Inbox (triage)"
1890      ((tags-todo ".*"
1891		  ((org-agenda-files `(,org-inbox-file)) ;; FIXME use constant here
1892		   (org-agenda-overriding-header "Unprocessed Inbox Item")))))
1893     ("u" "Untagged Tasks"
1894      ((tags-todo "-{.*}"
1895		  ((org-agenda-overriding-header "Untagged tasks")))))
1896   ))
1897  ;; TODO cleanup this list a bit
1898  (org-agenda-category-icon-alist `(("personal"  ,(list (propertize "🏡")))
1899				    ("work"  ,(list (propertize "🏢")))
1900				    ("appointments"  ,(list (propertize "📅")))
1901				    ("health"  ,(list (propertize "⚕️")))
1902				    ("systems"  ,(list (propertize "🖥️")))
1903				    ("journal"  ,(list (propertize "📝")))
1904				    ("project--" ,(list (propertize "💼" )))
1905				    ("tekton", (list (propertize "😼")))
1906				    ("openshift-pipelines", (list (propertize "🎩")))
1907				    ("redhat", (list (propertize "🎩")))
1908				    ("area--"  ,(list (propertize"🏢" )))
1909				    ("area--home"  ,(list (propertize "🏡")))
1910				    ("home"  ,(list (propertize "🏡")))
1911				    ("home-services" ,(list (propertize "☕ ")))
1912				    ("email"  ,(list (propertize"📨" )))
1913				    ("people"  ,(list (propertize"👤" )))
1914				    ("machine" ,(list (propertize "🖥️")))
1915				    ("website" ,(list (propertize "🌍")))
1916				    ("bike" ,(list (propertize "🚴‍♂️")))
1917				    ("security" ,(list (propertize "🛡️")))
1918				    ("i*" ,(list (propertize "📒")))))
1919  (org-agenda-prefix-format '((agenda . " %i %?-12t%s%(my/org-agenda-repeater)")
1920			      (todo . " %i")
1921			      (tags . " %i")
1922			      (search . " %i")))
1923  ;; Shorten scheduled leaders to free space for the repeater; give
1924  ;; deadlines their own ⏳ glyph (🔴 stays reserved for priority).
1925  (org-agenda-scheduled-leaders '("Sched " "S.%2dx "))
1926  (org-agenda-deadline-leaders '("⏳ 0d " "⏳%2dd " "⏳-%1dd "))
1927  (org-insert-heading-respect-content t)
1928  (org-M-RET-may-split-line '((default . nil)))
1929  (org-goto-interface 'outline-path-completion)
1930  (org-outline-path-complete-in-steps nil)
1931  (org-goto-max-level 2)
1932  :hook
1933  (org-mode . auto-fill-mode)
1934  (org-mode . auto-revert-mode)
1935  (org-mode . visual-line-mode)
1936  (org-mode . visual-wrap-prefix-mode)
1937  :bind
1938  (:map org-mode-map
1939	("C-<left>" . org-shiftleft)
1940	("C-<right>" . org-shiftright)
1941	("C-<up>" . org-shiftup)
1942	("C-<down>" . org-shiftdown)
1943	("C-c C-S-o" . vde/org-open-at-point-in-new-tab))
1944  :config
1945  ;; Org Agenda display rules:
1946  ;; - Agenda opens as a dedicated left side-window
1947  ;; - Org files from agenda reuse the other (non-side) window
1948  (defun vde/display-buffer-org-agenda-managed-p (buffer-name _action)
1949    "Determine whether BUFFER-NAME is an org-agenda managed buffer."
1950    (with-current-buffer buffer-name
1951      (and (derived-mode-p 'org-mode)
1952           (buffer-file-name)
1953           (member (buffer-file-name) (org-agenda-files)))))
1954
1955  (add-to-list 'display-buffer-alist
1956               '("\\*Org Agenda"
1957                 (display-buffer-in-side-window)
1958                 (side . left)
1959                 (window-width . 0.4)
1960                 (dedicated . side)
1961                 (inhibit-same-window . t)))
1962  (add-to-list 'display-buffer-alist
1963               '(vde/display-buffer-org-agenda-managed-p
1964                 (display-buffer-reuse-mode-window
1965                  display-buffer-use-some-window)
1966                 (inhibit-same-window . t)))
1967
1968  ;; Unbind C-, to allow embark-act to work
1969  (unbind-key "C-," org-mode-map)
1970
1971  (unbind-key "S-<left>" org-mode-map)
1972  (unbind-key "S-<right>" org-mode-map)
1973  (unbind-key "S-<up>" org-mode-map)
1974  (unbind-key "S-<down>" org-mode-map)
1975  (unbind-key "M-S-<left>" org-mode-map)
1976  (unbind-key "M-S-<right>" org-mode-map)
1977  (unbind-key "M-S-<up>" org-mode-map)
1978  (unbind-key "M-S-<down>" org-mode-map)
1979  (unbind-key "C-S-<left>" org-mode-map)
1980  (unbind-key "C-S-<right>" org-mode-map)
1981  (unbind-key "C-S-<up>" org-mode-map)
1982  (unbind-key "C-S-<down>" org-mode-map)
1983
1984  (defun vde/org-open-at-point-in-new-tab ()
1985    "Open org link at point in a new tab-bar tab.
1986Works with any org link type. Creates a new tab, then opens the link."
1987    (interactive)
1988    (let ((link-info (org-element-context)))
1989      (when (eq (org-element-type link-info) 'link)
1990        (tab-bar-new-tab)
1991        (org-open-at-point))))
1992
1993  (declare-function org-get-repeat "org")
1994  (defun my/org-agenda-repeater ()
1995    "Return the repeat cookie (e.g. \"++1w\") for the current agenda entry.
1996Evaluated by `org-agenda-format-item': for real items point sits on the
1997heading in the source Org buffer; for grid lines it runs in the agenda
1998buffer.  Show the repeater (so recurring tasks are visually distinct)
1999only when present, and never call `org-element-at-point' outside an Org
2000buffer."
2001    (if (derived-mode-p 'org-mode)
2002	(let ((rep (org-get-repeat)))
2003	  (if rep (format "%-5s " rep) ""))
2004      ""))
2005
2006  (require 'dash)
2007  (require 's)
2008  (declare-function --remove "dash")
2009  (declare-function --map "dash")
2010  (declare-function ->> "dash")
2011  (declare-function s-starts-with? "s")
2012  (declare-function s-contains? "s")
2013  (defun vde/org-refile-targets ()
2014    (append '((org-inbox-file :level . 0)
2015	      (org-todos-file :maxlevel . 3)
2016	      (org-habits-file :maxlevel . 3))
2017	    (->>
2018	     (directory-files org-notes-directory nil ".org$")
2019	     (--remove (s-starts-with? "." it))
2020	     (--remove (s-contains? "==readwise=" it))
2021	     (--map (format "%s/%s" org-notes-directory it))
2022	     (--map `(,it :maxlevel . 3)))
2023	    )))
2024
2025(defun vde/org-todo-subtree (state)
2026    "Set TODO entries in the current subtree to STATE.
2027By default, only changes entries with an active TODO keyword,
2028skipping plain headings and already-done items.
2029With prefix argument (C-u), force ALL entries to STATE regardless
2030of their current state."
2031    (interactive
2032     (list (completing-read "State: " org-todo-keywords-1 nil t)))
2033    (let ((match (if current-prefix-arg nil "/!")))
2034      (org-map-entries (lambda () (org-todo state)) match 'tree)))
2035
2036(use-package org-appear
2037  :hook (org-mode . org-appear-mode))
2038
2039(use-package org-id
2040  :after org
2041  :custom
2042  (org-id-method 'ts))
2043
2044(use-package org-attach
2045  :after org
2046  :custom
2047  (org-attach-id-to-path-function-list
2048   '(org-attach-id-ts-folder-format
2049     org-attach-id-uuid-folder-format))
2050  (org-attach-store-link-p 'attached))
2051
2052(use-package org-agenda
2053  :after org
2054  :commands (org-agenda)
2055  :config
2056  (unbind-key "S-<left>" org-agenda-mode-map)
2057  (unbind-key "S-<right>" org-agenda-mode-map)
2058  (unbind-key "S-<up>" org-agenda-mode-map)
2059  (unbind-key "S-<down>" org-agenda-mode-map)
2060  (unbind-key "C-S-<left>" org-agenda-mode-map)
2061  (unbind-key "C-S-<right>" org-agenda-mode-map))
2062
2063;; Make sure we load org-protocol
2064(use-package org-protocol
2065  :after org)
2066
2067(use-package org-tempo
2068  :after (org)
2069  :custom
2070  (org-structure-template-alist '(("a" . "aside")
2071				  ("c" . "center")
2072				  ("C" . "comment")
2073				  ("e" . "example")
2074				  ("E" . "export")
2075				  ("Ea" . "export ascii")
2076				  ("Eh" . "export html")
2077				  ("El" . "export latex")
2078				  ("q" . "quote")
2079				  ("s" . "src")
2080				  ("se" . "src emacs-lisp")
2081				  ("sE" . "src emacs-lisp :results value code :lexical t")
2082				  ("sg" . "src go")
2083				  ("sr" . "src rust")
2084				  ("sp" . "src python")
2085				  ("v" . "verse"))))
2086
2087;;; Web capture helpers — used in org-capture templates
2088;; These functions fetch web content for org-capture %(sexp) expansion.
2089;; They require: org-web-tools (eww-readable + pandoc), monolith (single-file HTML archiver).
2090
2091(defconst vde/web-archive-dir (expand-file-name "web-archive" org-directory)
2092  "Directory for monolith web page archives.")
2093
2094(defun vde/web-archive--ensure-dir ()
2095  "Ensure `vde/web-archive-dir' exists."
2096  (unless (file-directory-p vde/web-archive-dir)
2097    (make-directory vde/web-archive-dir t)))
2098
2099(defun vde/web-archive--monolith (url)
2100  "Archive URL with monolith, return path to saved file or nil on failure.
2101Saves to `vde/web-archive-dir' with a timestamped filename."
2102  (vde/web-archive--ensure-dir)
2103  (let* ((timestamp (format-time-string "%Y%m%dT%H%M%S"))
2104         (safe-name (replace-regexp-in-string
2105                     "[^a-zA-Z0-9._-]" "_"
2106                     (url-host (url-generic-parse-url url))))
2107         (filename (format "%s--%s.html" timestamp safe-name))
2108         (filepath (expand-file-name filename vde/web-archive-dir)))
2109    (message "Archiving %s with monolith..." url)
2110    (if (zerop (call-process "monolith" nil nil nil
2111                             url "-o" filepath
2112                             "-I" "-j" "-t" "30"))
2113        (progn
2114          (message "Archived to %s" filepath)
2115          filepath)
2116      (message "monolith failed for %s" url)
2117      nil)))
2118
2119(defun vde/web-capture--read-url ()
2120  "Read URL for web capture, prompting with clipboard URL as default.
2121When called from org-protocol, use the protocol-provided URL via
2122`org-store-link-plist'."
2123  (require 'org-web-tools)
2124  (let ((proto-url (and (boundp 'org-store-link-plist)
2125                        (plist-get org-store-link-plist :link)))
2126        (clip-url (org-web-tools--get-first-url)))
2127    (or proto-url
2128        (read-string "URL: " clip-url))))
2129
2130(defun vde/org-capture-web-page-readable ()
2131  "Return Org entry with readable content of URL.
2132Prompts for URL with clipboard as default.  Suitable for %(sexp) in capture templates."
2133  (require 'org-web-tools)
2134  (let ((url (vde/web-capture--read-url)))
2135    (or (ignore-errors (org-web-tools--url-as-readable-org url))
2136        (format "* [[%s][%s]] :website:\n\n%s\n\n(Failed to extract readable content)"
2137                url url (format-time-string (org-time-stamp-format 'with-time 'inactive))))))
2138
2139(defun vde/org-capture-web-page-archived ()
2140  "Return Org entry for URL with monolith archive.
2141Prompts for URL with clipboard as default.  Suitable for %(sexp) in capture templates."
2142  (require 'org-web-tools)
2143  (let* ((url (vde/web-capture--read-url))
2144         (title (or (ignore-errors
2145                      (let* ((dom (plz 'get url :as #'org-web-tools--sanitized-dom))
2146                             (result (org-web-tools--eww-readable dom)))
2147                        (org-web-tools--cleanup-title (or (car result) ""))))
2148                    url))
2149         (link (org-link-make-string url title))
2150         (timestamp (format-time-string (org-time-stamp-format 'with-time 'inactive)))
2151         (archive-path (vde/web-archive--monolith url))
2152         (archive-link (if archive-path
2153                          (format "[[file:%s][Local archive]]" archive-path)
2154                        "(archive failed)")))
2155    (format "* %s :website:archive:\n\n%s\n\nArchive: %s\n" link timestamp archive-link)))
2156
2157(defun vde/org-capture-web-page-both ()
2158  "Return Org entry with readable content AND monolith archive.
2159Prompts for URL with clipboard as default.  Suitable for %(sexp) in capture templates."
2160  (require 'org-web-tools)
2161  (let* ((url (vde/web-capture--read-url))
2162         (dom (ignore-errors (plz 'get url :as #'org-web-tools--sanitized-dom)))
2163         (readable-result (when dom (ignore-errors (org-web-tools--eww-readable dom))))
2164         (title (org-web-tools--cleanup-title (or (car readable-result) "")))
2165         (readable-html (cdr readable-result))
2166         (converted (when readable-html
2167                      (ignore-errors (org-web-tools--html-to-org-with-pandoc readable-html))))
2168         (link (org-link-make-string url (if (string-empty-p title) url title)))
2169         (timestamp (format-time-string (org-time-stamp-format 'with-time 'inactive)))
2170         (archive-path (vde/web-archive--monolith url))
2171         (archive-link (if archive-path
2172                          (format "[[file:%s][Local archive]]" archive-path)
2173                        "(archive failed)")))
2174    (with-temp-buffer
2175      (org-mode)
2176      (if converted
2177          (progn
2178            (insert converted)
2179            (org-web-tools--demote-headings-below 2)
2180            (goto-char (point-min))
2181            (insert "* " link " :website:archive:" "\n\n"
2182                    timestamp "\n\n"
2183                    "Archive: " archive-link "\n\n"
2184                    "** Article" "\n\n"))
2185        ;; Fallback if readable extraction failed
2186        (insert "* " link " :website:archive:" "\n\n"
2187                timestamp "\n\n"
2188                "Archive: " archive-link "\n\n"
2189                "(Failed to extract readable content)\n"))
2190      (buffer-string))))
2191
2192(use-package org-capture
2193  :demand t ;; Load eagerly so org-capture-templates is available for raffi/emacsclient queries
2194  :bind (("C-c o c" . org-capture))
2195  :config
2196  (add-to-list 'org-capture-templates
2197	       `("t" "📥 Tasks")
2198	       t)
2199  (add-to-list 'org-capture-templates
2200	       `("tt" " New task" entry
2201		 (file ,org-inbox-file)
2202		 "* TODO %?\n:PROPERTIES:\n:CREATED:\t%U\n:END:\n\n%i\n\nFrom: %a"
2203		 :empty-lines 1)
2204	       t)
2205  (add-to-list 'org-capture-templates
2206	       `("tl" " New task (from capture)" entry
2207		 (file ,org-inbox-file)
2208		 "* TODO %a\n:PROPERTIES:\n:CREATED:\t%U\n:END:\n\n%i\n\nFrom: %a"
2209		 :empty-lines 1)
2210	       t)
2211  (add-to-list 'org-capture-templates
2212	       `("td" "✅ Done (log)")
2213	       t)
2214  (add-to-list 'org-capture-templates
2215	       `("tdw" "✅ Work" entry
2216		 (file+headline ,org-todos-file "Work")
2217		 "* DONE %?\nCLOSED: %U\n:PROPERTIES:\n:CREATED:\t%U\n:END:"
2218		 :empty-lines 1)
2219	       t)
2220  (add-to-list 'org-capture-templates
2221	       `("tds" "✅ Systems" entry
2222		 (file+headline ,org-todos-file "Systems")
2223		 "* DONE %?\nCLOSED: %U\n:PROPERTIES:\n:CREATED:\t%U\n:END:"
2224		 :empty-lines 1)
2225	       t)
2226  (add-to-list 'org-capture-templates
2227	       `("tdp" "✅ Personal" entry
2228		 (file+headline ,org-todos-file "Personal")
2229		 "* DONE %?\nCLOSED: %U\n:PROPERTIES:\n:CREATED:\t%U\n:END:"
2230		 :empty-lines 1)
2231	       t)
2232  ;; Refine this
2233  (add-to-list 'org-capture-templates
2234	       `("tr" " PR Review" entry
2235		 (file ,org-inbox-file)
2236		 "* TODO review gh:%^{issue} :review:\n:PROPERTIES:\n:CREATED:%U\n:END:\n\n%i\n%?\nFrom: %a"
2237		 :empty-lines 1)
2238	       t)
2239  (add-to-list 'org-capture-templates
2240	       `("l" "🔗 Links")
2241	       t)
2242  (add-to-list 'org-capture-templates
2243	       `("ll" "🔗 Link" entry
2244		 (file ,org-inbox-file)
2245		 "* %a\n%U\n%?\n%i"
2246		 :empty-lines 1)
2247	       t)
2248  (add-to-list 'org-capture-templates
2249	       `("lw" "🌐 Web page (readable)" entry
2250		 (file ,org-inbox-file)
2251		 "%(vde/org-capture-web-page-readable)")
2252	       t)
2253  (add-to-list 'org-capture-templates
2254	       `("la" "📦 Web page (archived)" entry
2255		 (file ,org-inbox-file)
2256		 "%(vde/org-capture-web-page-archived)")
2257	       t)
2258  (add-to-list 'org-capture-templates
2259	       `("lb" "📦🌐 Web page (both)" entry
2260		 (file ,org-inbox-file)
2261		 "%(vde/org-capture-web-page-both)")
2262	       t)
2263  (add-to-list 'org-capture-templates
2264	       `("lk" "🔖 Bookmark (flux)" entry
2265		 (file+function ,(expand-file-name "bookmarks.org" org-directory)
2266				 (lambda () (goto-char (point-min))
2267					   (re-search-forward "^\\*" nil t)
2268					   (beginning-of-line)))
2269		 "* %a%^g\n\n<%<%Y-%m-%d %a>>\n\n%?\n"
2270		 :empty-lines 1)
2271	       t)
2272  (add-to-list 'org-capture-templates
2273	       `("i" "💡 TIL")
2274	       t)
2275  (add-to-list 'org-capture-templates
2276	       `("ii" "💡 Today I Learned" entry
2277		 (file+function ,(expand-file-name "til.org" org-directory)
2278				 (lambda () (goto-char (point-min))
2279					   (re-search-forward "^\\*" nil t)
2280					   (beginning-of-line)))
2281		 "* %^{Title}%^g\n\n<%<%Y-%m-%d %a>>\n\n%?\n"
2282		 :empty-lines 1)
2283	       t)
2284  (add-to-list 'org-capture-templates
2285	       `("m" "✉ Email Workflow")
2286	       t)
2287  ;; Forward declaration - defined in mu4e config
2288  (declare-function vde-mu4e--body-summary-for-capture "init")
2289  (add-to-list 'org-capture-templates
2290	       `("mf" "Follow Up" entry
2291		 (file ,org-inbox-file)
2292		 "* TODO Follow up: %:subject :email:
2293:PROPERTIES:
2294:CREATED:\t%U
2295:FROM:\t%:from
2296:DATE:\t%:date
2297:END:
2298SCHEDULED:%t
2299DEADLINE: %(org-insert-time-stamp (org-read-date nil t \"+2d\"))
2300%(vde-mu4e--body-summary-for-capture)
2301%i
2302Link: %a"
2303		 :immediate-finish t)
2304	       t)
2305  (add-to-list 'org-capture-templates
2306	       `("mr" "Read Later" entry
2307		 (file ,org-inbox-file)
2308		 "* TODO Read: %:subject :email:
2309:PROPERTIES:
2310:CREATED:\t%U
2311:FROM:\t%:from
2312:DATE:\t%:date
2313:END:
2314SCHEDULED:%t
2315DEADLINE: %(org-insert-time-stamp (org-read-date nil t \"+2d\"))
2316%(vde-mu4e--body-summary-for-capture)
2317%i
2318Link: %a"
2319		 :immediate-finish t)
2320	       t)
2321  (add-to-list 'org-capture-templates
2322	       `("mt" "Task from email" entry
2323		 (file ,org-inbox-file)
2324		 "* TODO %:subject :email:
2325:PROPERTIES:
2326:CREATED:\t%U
2327:FROM:\t%:from
2328:DATE:\t%:date
2329:END:
2330%(vde-mu4e--body-summary-for-capture)
2331%?
2332Link: %a"
2333		 :empty-lines 1)
2334	       t)
2335
2336  (defvar vde/org-capture-refile-templates '("mt")
2337    "List of capture template keys that should trigger refile after capture.")
2338
2339  (defun vde/org-capture-refile-if-needed ()
2340    "Refile the captured entry if the template key is in `vde/org-capture-refile-templates'.
2341After a successful refile, delete the popup frame if applicable."
2342    (when (and (not org-note-abort)
2343               (member (plist-get org-capture-plist :key) vde/org-capture-refile-templates))
2344      (let ((is-popup (frame-parameter nil 'vde/window-popup-frame)))
2345        (org-capture-goto-last-stored)
2346        (call-interactively 'org-refile)
2347        (when is-popup
2348          (delete-frame)))))
2349
2350  (add-hook 'org-capture-after-finalize-hook #'vde/org-capture-refile-if-needed)
2351  (add-to-list 'org-capture-templates
2352	       `("J" "🗞 Journal (antidated) entry" item
2353		 (file+datetree+prompt ,org-journal-file)
2354		 "%U %?\n%i")
2355	       t)
2356  ;; TODO: refine this, create a function that reset this
2357  ;; emails
2358  ;; (add-to-list 'org-capture-templates
2359  ;;              `("m" "Meeting notes" entry
2360  ;;                (file+datetree ,org-meeting-notes-file)
2361  ;;                (file ,(concat user-emacs-directory "/etc/orgmode/meeting-notes.org"))))
2362  
2363  ;; Make *Org Select* use the full window instead of splitting
2364  (add-to-list 'display-buffer-alist
2365               '("\\*Org Select\\*"
2366                 (display-buffer-same-window)))
2367
2368  (defun vde/window-delete-popup-frame (&rest _)
2369    "Kill selected selected frame if it has parameter `vde/window-popup-frame'.
2370Use this function via a hook.
2371Skip deletion when:
2372- `org-capture-refile' (C-c C-w) is running (it handles cleanup via advice)
2373- the capture template auto-refiles via `vde/org-capture-refile-templates'"
2374    (when (and (frame-parameter nil 'vde/window-popup-frame)
2375               (not (eq this-command 'org-capture-refile))
2376               (not (and (bound-and-true-p org-capture-plist)
2377                         (member (plist-get org-capture-plist :key)
2378                                 vde/org-capture-refile-templates)
2379                         (not org-note-abort))))
2380      (delete-frame)))
2381
2382  (advice-add 'org-capture-refile :around
2383              (lambda (orig-fn &rest args)
2384                "Delete the popup frame after org-capture-refile completes."
2385                (let ((is-popup (frame-parameter nil 'vde/window-popup-frame)))
2386                  (apply orig-fn args)
2387                  (when is-popup
2388                    (delete-frame)))))
2389
2390  ;; (add-to-list 'org-capture-templates
2391  ;; `("w" "Writing"))
2392  (declare-function vde/window-delete-popup-frame "init")
2393  (add-hook 'org-capture-after-finalize-hook #'vde/window-delete-popup-frame)
2394
2395  (defun vde/org-capture-full-frame ()
2396    "Make org-capture use the full frame in popup frames.
2397When capture opens in a dedicated popup frame (with `vde/window-popup-frame'
2398parameter), remove all other windows so the capture buffer fills the frame."
2399    (when (frame-parameter nil 'vde/window-popup-frame)
2400      (delete-other-windows)))
2401
2402  (add-hook 'org-capture-mode-hook #'vde/org-capture-full-frame))
2403
2404
2405;; Usage Metrics - dumps loaded features and command frequency for usage-metrics collector
2406(use-package usage-metrics
2407  :demand t)
2408
2409;; Daily Plan - Jira/GitHub → org-mode scheduling
2410(use-package daily-plan
2411  :commands (daily-plan-show daily-plan-inbox daily-plan-weekly
2412             daily-plan-review daily-plan-schedule
2413             daily-plan-schedule-at-point daily-plan-yank-markdown)
2414  :bind-keymap ("C-c d" . daily-plan-prefix-map)
2415  :config
2416  (defvar daily-plan-prefix-map
2417    (let ((map (make-sparse-keymap)))
2418      (define-key map "d" #'daily-plan-show)
2419      (define-key map "i" #'daily-plan-inbox)
2420      (define-key map "w" #'daily-plan-weekly)
2421      (define-key map "r" #'daily-plan-review)
2422      (define-key map "s" #'daily-plan-schedule)
2423      map)
2424    "Keymap for daily-plan commands under C-c d."))
2425
2426
2427(use-package org-habit
2428  :after org
2429  :custom
2430  (org-habit-show-habits-only-for-today nil)
2431  (org-habit-graph-column 80))
2432
2433;; org-ql is used by org-batch-functions / pi-org-todos
2434(use-package org-ql
2435  :commands (org-ql-search org-ql-query))
2436
2437;; org-batch-functions and pi-org-todos are site-lisp packages called
2438;; externally via emacsclient — their require chains pull in org/org-ql.
2439(use-package org-batch-functions
2440  :commands (org-batch-list-todos
2441             org-batch-scheduled-today
2442             org-batch-by-section
2443             org-batch-count-by-state
2444             org-batch-search
2445             org-batch-get-sections
2446             org-batch-get-children
2447             org-batch-get-todo-content
2448             org-batch-get-overdue
2449             org-batch-get-upcoming
2450             org-batch-get-statistics
2451             org-batch-append-content
2452             org-batch-update-state
2453             org-batch-add-todo
2454             org-batch-schedule-task
2455             org-batch-set-deadline
2456             org-batch-set-priority
2457             org-batch-archive-done
2458             org-batch-get-all-entries
2459             org-batch-get-refile-targets
2460             org-batch-refile-entry))
2461(use-package pi-org-todos
2462  :commands (pi/org-todo-list
2463             pi/org-todo-list-all
2464             pi/org-todo-scheduled
2465             pi/org-todo-upcoming
2466             pi/org-todo-overdue
2467             pi/org-todo-search
2468             pi/org-todo-get
2469             pi/org-todo-sections
2470             pi/org-todo-by-section
2471             pi/org-todo-statistics
2472             pi/org-todo-done
2473             pi/org-todo-state
2474             pi/org-todo-schedule
2475             pi/org-todo-deadline
2476             pi/org-todo-priority
2477             pi/org-todo-add
2478             pi/org-todo-append
2479             pi/org-todo-archive-done
2480             pi/org-todo-add-tags
2481             pi/org-todo-remove-tags
2482             pi/org-todo-all-tags
2483             pi/org-todo-get-property
2484             pi/org-todo-set-property
2485             pi/org-todo-inbox-all
2486             pi/org-todo-get-refile-targets
2487             pi/org-todo-refile))
2488
2489(use-package ox-tufte
2490  :after org
2491  :ensure t
2492  :commands (org-tufte-export-to-html
2493             org-tufte-export-as-html
2494             vde/org-add-tufte-headers
2495             vde/org-add-tufte-headers-to-file
2496             vde/org-export-notes-with-export-tag)
2497  :custom
2498  ;; Path to tufte.css relative to website root
2499  (org-tufte-htmlize-output-type 'css)
2500  :config
2501  ;; Default HTML head includes for Tufte CSS
2502  (setq org-html-head-include-default-style nil)
2503  (setq org-html-head-include-scripts nil)
2504
2505  (defun vde/org-add-tufte-headers ()
2506    "Add Tufte CSS export headers to current org-mode buffer.
2507Inserts the headers after the frontmatter (#+title, #+date, etc.)
2508if they don't already exist."
2509    (interactive)
2510    (unless (derived-mode-p 'org-mode)
2511      (user-error "Not in an org-mode buffer"))
2512    (save-excursion
2513      ;; Check if headers already exist
2514      (goto-char (point-min))
2515      (if (re-search-forward "^#\\+HTML_HEAD:.*tufte\\.css" nil t)
2516          (message "Tufte headers already present")
2517        ;; Find insertion point after frontmatter
2518        (goto-char (point-min))
2519        ;; Skip past all the #+KEYWORD: lines at the top
2520        (while (and (not (eobp))
2521                    (looking-at "^#\\+\\(title\\|date\\|filetags\\|identifier\\|category\\|author\\|email\\|description\\):"))
2522          (forward-line 1))
2523        ;; Insert headers
2524        (insert "#+HTML_HEAD: <link rel=\"stylesheet\" href=\"tufte.css\" type=\"text/css\" />\n")
2525        (insert "#+HTML_HEAD: <link rel=\"stylesheet\" href=\"ox-tufte.css\" type=\"text/css\" />\n")
2526        (insert "#+OPTIONS: toc:nil num:nil html-style:nil html-scripts:nil\n")
2527        (message "Tufte headers added"))))
2528
2529  (defun vde/org-add-tufte-headers-to-file (file)
2530    "Add Tufte CSS export headers to an org-mode FILE."
2531    (interactive "fOrg file: ")
2532    (with-current-buffer (find-file-noselect file)
2533      (vde/org-add-tufte-headers)
2534      (save-buffer)))
2535
2536  (defun vde/org-export-notes-with-export-tag (notes-dir output-dir)
2537    "Export all org files in NOTES-DIR with _export tag to OUTPUT-DIR using ox-tufte.
2538Creates OUTPUT-DIR if it doesn't exist."
2539    (interactive "DNotes directory: \nDOutput directory: ")
2540    (unless (file-directory-p output-dir)
2541      (make-directory output-dir t))
2542    (let ((files (directory-files notes-dir t "\\.org$"))
2543          (exported 0))
2544      (dolist (file files)
2545        (with-temp-buffer
2546          (insert-file-contents file)
2547          (when (re-search-forward ":_export:" nil t)
2548            (let* ((basename (file-name-base file))
2549                   (output-file (expand-file-name
2550                                (concat basename ".html")
2551                                output-dir)))
2552              (with-current-buffer (find-file-noselect file)
2553                (org-tufte-export-to-html)
2554                (when (file-exists-p (concat (file-name-sans-extension file) ".html"))
2555                  (rename-file (concat (file-name-sans-extension file) ".html")
2556                             output-file t)
2557                  (message "Exported: %s -> %s" (file-name-nondirectory file) output-file)
2558                  (setq exported (1+ exported))))))))
2559      (message "Exported %d file(s) with _export tag" exported))))
2560
2561(use-package ob-ditaa
2562  :after org
2563  :commands (org-babel-execute:ditaa)
2564  :config
2565  (setq org-ditaa-jar-path "/home/vincent/local/state/nix/profiles/home-manager/home-path/lib/ditaa.jar"))
2566
2567(use-package ob-dot
2568  :after org
2569  :commands (org-babel-execute:dot))
2570
2571(use-package xeft
2572  :commands (xeft)
2573  :custom
2574  (xeft-directory org-notes-directory)
2575  (xeft-recursive 'follow-symlinks)
2576  (xeft-extensions '("md" "org")))
2577
2578(use-package denote
2579  :commands (denote)
2580  :init
2581  (declare-function denote-rename-buffer-mode "denote")
2582  :bind (("C-c n c" . denote-region)
2583	 ("C-c n i" . denote-link-or-create)
2584	 ("C-c n b" . denote-backlinks)
2585	 ("C-c n F f" . denote-find-link)
2586	 ("C-c n F b" . denote-find-backlink)
2587	 ("C-c n r" . vde/org-refile-to-denote))
2588  :custom
2589  (denote-directory org-notes-directory)
2590  (denote-rename-buffer-format "📝 %t")
2591  (denote-date-prompt-denote-date-prompt-use-org-read-date t)
2592  (denote-prompts '(title keywords))
2593  (denote-backlinks-display-buffer-action
2594   '((display-buffer-reuse-window
2595      display-buffer-in-side-window)
2596     (side . bottom)
2597     (slot . 99)
2598     (window-width . 0.3)
2599     (dedicated . t)
2600     (preserve-size . (t . t))))
2601  :hook (dired-mode . denote-dired-mode)
2602  :config
2603  (denote-rename-buffer-mode 1)
2604  (declare-function denote-file-is-note-p "denote")
2605  (declare-function denote-rename-file-using-front-matter "denote")
2606  (defun my-denote-always-rename-on-save-based-on-front-matter ()
2607    "Rename the current Denote file, if needed, upon saving the file.
2608Rename the file based on its front matter, checking for changes in the
2609title or keywords fields.
2610
2611Add this function to the `after-save-hook'."
2612    (let ((_denote-rename-confirmations nil)
2613          (_denote-save-buffers t)) ; to save again post-rename
2614      (when (and buffer-file-name (denote-file-is-note-p buffer-file-name))
2615	(ignore-errors (denote-rename-file-using-front-matter buffer-file-name))
2616	(message "Buffer saved; Denote file renamed"))))
2617
2618  (declare-function my-denote-always-rename-on-save-based-on-front-matter "init")
2619  (add-hook 'after-save-hook #'my-denote-always-rename-on-save-based-on-front-matter)
2620
2621  (declare-function denote-sluggify "denote")
2622  (declare-function denote--retrieve-title-or-filename "denote")
2623  (defun vde/org-category-from-buffer ()
2624    "Get the org category (#+category:) value from the buffer"
2625    (cond
2626     ((string-match "__journal.org$" (buffer-file-name))
2627      "journal")
2628     (t
2629      (denote-sluggify (denote--retrieve-title-or-filename (buffer-file-name) 'org)))))
2630
2631  (declare-function org-refile-get-targets "org")
2632  (declare-function org-refile-cache-clear "org")
2633  (declare-function denote-retrieve-title-value "denote")
2634  (declare-function denote-directory-files "denote")
2635  (declare-function denote-title-prompt "denote")
2636  (defun vde/org-refile-to-denote ()
2637    "Refile to a denote note (existing or new)."
2638    (interactive)
2639    (let* ((source-buffer (current-buffer))
2640	   (files (denote-directory-files))
2641	   (choices (mapcar (lambda (f)
2642			      (cons (denote-retrieve-title-value f 'org) f))
2643			    files))
2644	   (selection (completing-read "Refile to note: " (mapcar #'car choices)))
2645	   (target (if-let ((match (cdr (assoc selection choices))))
2646		       match
2647		     (cl-letf (((symbol-function 'denote-title-prompt)
2648                                (lambda (&rest _) selection)))
2649		       (call-interactively #'denote))))
2650	   (target-buffer (find-file-noselect target)))
2651      (with-current-buffer target-buffer
2652        (org-mode)
2653        (save-excursion
2654          (goto-char (point-min))
2655          (unless (re-search-forward "^\\*+ " nil t)
2656	    (goto-char (point-max))
2657	    (insert "\n* Notes\n")))
2658        (save-buffer))
2659      (with-current-buffer source-buffer
2660        (let ((org-refile-targets `((,target :maxlevel . 3)))
2661	      (org-refile-use-outline-path 'file)
2662              (org-refile-target-verify-function nil))
2663          (org-refile-cache-clear)
2664          ;; Force org to rebuild targets
2665          (org-refile-get-targets)
2666          (call-interactively #'org-refile))))))
2667
2668(use-package denote-org
2669  :after (denote org)
2670  :defer 2)
2671
2672(use-package popper
2673  :commands (popper-mode)
2674  :bind (("C-#"   . popper-toggle)
2675	 ;; ("C-~"   . popper-kill-latest-popup)
2676         ("M-#"   . popper-cycle)
2677	 ;; alt keybind for mac mode, possibly disposable? TODO: should bind only on mac?
2678	 ("C-`"   . popper-toggle-latest)
2679         ("C-M-#" . popper-toggle-type))
2680  :custom
2681  (popper-reference-buffers
2682   '("\\*Messages\\*"
2683     "\\*Warnings\\*"
2684     "Output\\*$"
2685     "\\*Async Shell Command\\*"
2686     help-mode
2687     compilation-mode))
2688  (popper-window-height 0.3)
2689  :config
2690  (popper-mode))
2691
2692(use-package popper-echo
2693  :commands (popper-echo-mode popper-tab-line-mode)
2694  :init
2695  (popper-tab-line-mode))
2696
2697(use-package mu4e
2698  :commands (mu4e)
2699  :init
2700  (declare-function make-mu4e-context "mu4e")
2701  (declare-function mu4e~draft-message-filename-construct "mu4e")
2702  (declare-function mu4e~proc-add "mu4e")
2703  (declare-function mu4e~mark-check-target "mu4e")
2704  (declare-function mu4e-flags-to-string "mu4e")
2705  (declare-function mu4e-message-field "mu4e")
2706  (declare-function mu4e-message-at-point "mu4e")
2707  (declare-function mu4e-root-maildir "mu4e")
2708  (declare-function mu4e-join-paths "mu4e")
2709  (declare-function mu4e-create-maildir-maybe "mu4e")
2710  (declare-function mu4e-ask-maildir "mu4e")
2711  (declare-function mu4e-file-is-note-p "mu4e")
2712  :custom
2713  (mu4e-mu-home (expand-file-name "mu" (or (getenv "XDG_DATA_HOME")
2714                                           (expand-file-name ".local/share" (getenv "HOME")))))
2715  (mu4e-context-policy 'pick-first)
2716  (mu4e-change-filenames-when-moving t)
2717  (mu4e-attachment-dir "~/desktop/downloads")
2718  :config
2719  ;; No need for get-mail-command - goimapnotify handles syncing via IMAP IDLE
2720  ;; Disable automatic polling - mail arrives instantly via goimapnotify
2721  (setq mu4e-update-interval nil)
2722
2723  ;; Friendlier buffer names for frame titles
2724  (setq mu4e-headers-buffer-name "*mail - headers*"
2725        mu4e-view-buffer-name "*mail - article*")
2726
2727  ;; Configure msmtp for sending mail
2728  (setq sendmail-program "msmtp"
2729        send-mail-function 'smtpmail-send-it
2730        message-sendmail-f-is-evil t
2731        message-sendmail-extra-arguments '("--read-envelope-from")
2732        message-send-mail-function 'message-send-mail-with-sendmail)
2733
2734  (defun vde-mu4e--extract-body-summary (&optional max-lines)
2735    "Extract a summary from the current mu4e message body.
2736Returns the first MAX-LINES lines (default 10) of the plain text body,
2737with quoted lines and signatures removed."
2738    (let ((max-lines (or max-lines 10))
2739          (msg (mu4e-message-at-point 'noerror)))
2740      (if (not msg)
2741          ""
2742        (let* ((path (mu4e-message-field msg :path))
2743               (body-txt (when (and path (file-exists-p path))
2744                           (with-temp-buffer
2745                             (insert-file-contents path)
2746                             (goto-char (point-min))
2747                             ;; Skip headers - find empty line
2748                             (when (re-search-forward "^$" nil t)
2749                               (forward-line 1)
2750                               (buffer-substring-no-properties (point) (point-max)))))))
2751          (if (not body-txt)
2752              ""
2753            ;; Clean up the body
2754            (with-temp-buffer
2755              (insert body-txt)
2756              ;; Try to decode if it looks like quoted-printable
2757              (goto-char (point-min))
2758              (when (re-search-forward "Content-Transfer-Encoding: quoted-printable" nil t)
2759                (goto-char (point-min))
2760                (when (re-search-forward "^$" nil t)
2761                  (forward-line 1)
2762                  (ignore-errors
2763                    (quoted-printable-decode-region (point) (point-max)))))
2764              ;; Remove MIME boundaries and headers
2765              (goto-char (point-min))
2766              (while (re-search-forward "^--.*$\\|^Content-Type:.*$\\|^Content-Transfer-Encoding:.*$" nil t)
2767                (replace-match ""))
2768              ;; Remove HTML tags if present
2769              (goto-char (point-min))
2770              (while (re-search-forward "<[^>]+>" nil t)
2771                (replace-match ""))
2772              ;; Remove quoted lines (starting with >)
2773              (goto-char (point-min))
2774              (while (re-search-forward "^>.*$" nil t)
2775                (replace-match ""))
2776              ;; Remove signature (everything after -- )
2777              (goto-char (point-min))
2778              (when (re-search-forward "^-- $" nil t)
2779                (delete-region (match-beginning 0) (point-max)))
2780              ;; Remove excessive whitespace
2781              (goto-char (point-min))
2782              (while (re-search-forward "^[ \t]*\n\\([ \t]*\n\\)+" nil t)
2783                (replace-match "\n"))
2784              ;; Get first N non-empty lines
2785              (goto-char (point-min))
2786              (let ((lines nil)
2787                    (count 0))
2788                (while (and (< count max-lines) (not (eobp)))
2789                  (let ((line (string-trim (buffer-substring-no-properties
2790                                            (line-beginning-position)
2791                                            (line-end-position)))))
2792                    (when (and (> (length line) 0)
2793                               (not (string-match-p "^\\s-*$" line)))
2794                      (push line lines)
2795                      (setq count (1+ count))))
2796                  (forward-line 1))
2797                (string-join (nreverse lines) "\n"))))))))
2798
2799  (defun vde-mu4e--body-summary-for-capture ()
2800    "Return email body summary for org-capture template."
2801    (let ((summary (vde-mu4e--extract-body-summary 8)))
2802      (if (string-empty-p summary)
2803          ""
2804        (concat "\n#+begin_quote\n" summary "\n#+end_quote"))))
2805
2806  (defun vde-mu4e--mark-get-copy-target ()
2807    "Ask for a copy target, and propose to create it if it does not exist."
2808    (let* ((target (mu4e-ask-maildir "Copy message to: "))
2809           (target (if (string= (substring target 0 1) "/")
2810                       target
2811                     (concat "/" target)))
2812           (fulltarget (mu4e-join-paths (mu4e-root-maildir) target)))
2813      (when (mu4e-create-maildir-maybe fulltarget)
2814	target)))
2815
2816  (defun copy-message-to-target(_docid msg target)
2817    (let (
2818          (new_msg_path nil) ;; local variable                                                                
2819          (msg_flags (mu4e-message-field msg :flags))                                                                                                       
2820          )                                                                                                   
2821      ;; 1. target is already determined interactively when executing the mark (:ask-target)                     
2822
2823      ;; 2. Determine the path for the new file: we use mu4e~draft-message-filename-construct from            
2824      ;; mu4e-draft.el to create a new random filename, and append the original's msg_flags                   
2825      (setq new_msg_path (format "%s/%s/cur/%s" mu4e-maildir target (mu4e~draft-message-filename-construct    
2826								     (mu4e-flags-to-string msg_flags))))                                                                     
2827
2828      ;; 3. Copy the message using file system call (copy-file) to new_msg_path:                              
2829      ;; (See e.g. mu4e-draft.el > mu4e-draft-open > resend)                                             
2830      (copy-file (mu4e-message-field msg :path) new_msg_path)                                                 
2831
2832      ;; 4. Add the information to the database (may need to update current search query with 'g' if duplicating to current box. Try also 'V' to toggle the display of duplicates) 
2833      (mu4e~proc-add new_msg_path (mu4e~mark-check-target target))                                              
2834      )                                                                                                       
2835    )
2836
2837  (defun vde-mu4e--refile (msg)
2838    "Refile function to smartly move `MSG' to a given folder."
2839    (cond
2840     ;; FIXME
2841     ((string= (plist-get (car-safe (mu4e-message-field msg :cc)) :email) "ci_activity@noreply.github.com")
2842      "/icloud/Deleted Messages")
2843     (t
2844      (let ((year (format-time-string "%Y" (mu4e-message-field msg :date))))
2845	(format "/icloud/Archives/%s" year)))))
2846
2847  (setq
2848   mu4e-headers-draft-mark     '("D" . "💈")
2849   mu4e-headers-flagged-mark   '("F" . "📍")
2850   mu4e-headers-new-mark       '("N" . "🔥")
2851   mu4e-headers-passed-mark    '("P" . "❯")
2852   mu4e-headers-replied-mark   '("R" . "❮")
2853   mu4e-headers-seen-mark      '("S" . "☑")
2854   mu4e-headers-trashed-mark   '("T" . "💀")
2855   mu4e-headers-attach-mark    '("a" . "📎")
2856   mu4e-headers-encrypted-mark '("x" . "🔒")
2857   mu4e-headers-signed-mark    '("s" . "🔑")
2858   mu4e-headers-unread-mark    '("u" . "⎕")
2859   mu4e-headers-list-mark      '("l" . "🔈")
2860   mu4e-headers-personal-mark  '("p" . "👨")
2861   mu4e-headers-calendar-mark  '("c" . "📅"))
2862
2863  (setopt mu4e-completing-read-function completing-read-function)
2864  (setq mu4e-refile-folder 'vde-mu4e--refile)
2865
2866  ;; === Compose & Reply Settings ===
2867  ;; Don't include own addresses in reply-all
2868  (setq mu4e-compose-reply-ignore-address
2869        '("no-?reply" "vincent@demeester.fr" "vdemeest@redhat.com"))
2870
2871  ;; Better citation format for replies
2872  (setq message-citation-line-format "On %a %d %b %Y at %R, %f wrote:\n"
2873        message-citation-line-function 'message-insert-formatted-citation-line)
2874
2875  ;; Fix improperly formatted sender addresses in replies
2876  (setq rfc2047-quote-decoded-words-containing-tspecials t)
2877
2878  ;; Enable format=flowed for better plain text display on mobile/other clients
2879  (setq mu4e-compose-format-flowed t)
2880
2881  ;; Open compose in a new frame
2882  (setq mu4e-compose-switch 'frame)
2883
2884  ;; Build contexts only for accounts whose maildir actually exists on this
2885  ;; host, so a machine missing icloud (or redhat) doesn't error out.
2886  (setq mu4e-contexts
2887	(let ((maildir-root (expand-file-name "~/desktop/mails")))
2888	  (delq nil
2889	   (list
2890	    (when (file-directory-p (expand-file-name "icloud" maildir-root))
2891	      (make-mu4e-context
2892			   :name "icloud"
2893			   :match-func (lambda (msg) (when msg
2894						       (string-prefix-p "/icloud" (mu4e-message-field msg :maildir))))
2895			   :vars '(
2896				   (user-mail-address . "vincent@demeester.fr")
2897				   (mu4e-trash-folder . "/icloud/Deleted Messages")
2898				   (mu4e-sent-folder . "/icloud/Sent Messages")
2899				   (mu4e-draft-folder . "/icloud/Drafts")
2900				   ;; (mu4e-get-mail-command . "mbsync icloud")
2901				   )))
2902	    (when (file-directory-p (expand-file-name "redhat" maildir-root))
2903	      (make-mu4e-context
2904			   :name "redhat"
2905			   :match-func (lambda (msg) (when msg
2906						       (string-prefix-p "/redhat" (mu4e-message-field msg :maildir))))
2907			   :vars '(
2908				   (user-mail-address . "vdemeest@redhat.com")
2909				   (mu4e-drafts-folder  . "/redhat/[Gmail]/Drafts")
2910				   (mu4e-sent-folder  . "/redhat/[Gmail]/Sent Mail")
2911				   ;; (mu4e-refile-folder  . "/redhat/[Gmail]/All Mail")
2912				   (mu4e-trash-folder  . "/redhat/[Gmail]/Trash")
2913				   ;; (mu4e-get-mail-command . "mbsync redhat")
2914				   )))))))
2915  ;; === Priority & Unread ===
2916  (add-to-list 'mu4e-bookmarks
2917	       '(:name "All Inboxes"
2918		       :query "maildir:/icloud/Inbox OR maildir:/redhat/Inbox"
2919		       :key ?b))
2920
2921  (add-to-list 'mu4e-bookmarks
2922	       '(:name "Unread (All)"
2923		       :query "flag:unread AND NOT flag:trashed"
2924		       :key ?u))
2925
2926  (add-to-list 'mu4e-bookmarks
2927	       '(:name "Unread Inboxes"
2928		       :query "flag:unread AND (maildir:/icloud/Inbox OR maildir:/redhat/Inbox)"
2929		       :key ?U))
2930
2931  (add-to-list 'mu4e-bookmarks
2932	       '(:name "Flagged"
2933		       :query "flag:flagged AND NOT flag:trashed"
2934		       :key ?f))
2935
2936  (add-to-list 'mu4e-bookmarks
2937	       '(:name "Today"
2938		       :query "date:today..now AND NOT flag:trashed"
2939		       :key ?t))
2940
2941  (add-to-list 'mu4e-bookmarks
2942	       '(:name "This Week"
2943		       :query "date:7d..now AND NOT flag:trashed"
2944		       :key ?w))
2945
2946  ;; === Work - RedHat Projects ===
2947  (add-to-list 'mu4e-bookmarks
2948	       '(:name "Pipelines/Tekton"
2949		       :query "maildir:/redhat/pipelines/* OR maildir:/redhat/tekton/*"
2950		       :key ?p))
2951
2952  (add-to-list 'mu4e-bookmarks
2953	       '(:name "Konflux"
2954		       :query "maildir:/redhat/konflux/*"
2955		       :key ?k))
2956
2957  (add-to-list 'mu4e-bookmarks
2958	       '(:name "Serverless (Knative)"
2959		       :query "maildir:/redhat/serverless/* OR maildir:/redhat/knative/*"
2960		       :key ?s))
2961
2962  (add-to-list 'mu4e-bookmarks
2963	       '(:name "DevTools Team"
2964		       :query "maildir:/redhat/devtools/*"
2965		       :key ?d))
2966
2967  (add-to-list 'mu4e-bookmarks
2968	       '(:name "Cloud & Insights"
2969		       :query "maildir:/redhat/cloud/* OR maildir:/redhat/insights/*"
2970		       :key ?c))
2971
2972  ;; === Work - Management & Communication ===
2973  (add-to-list 'mu4e-bookmarks
2974	       '(:name "Managers"
2975		       :query "maildir:/redhat/managers/*"
2976		       :key ?m))
2977
2978  (add-to-list 'mu4e-bookmarks
2979	       '(:name "Announcements"
2980		       :query "maildir:/redhat/announce/* OR maildir:/redhat/area/*"
2981		       :key ?a))
2982
2983  (add-to-list 'mu4e-bookmarks
2984	       '(:name "Local (France/Remote)"
2985		       :query "maildir:/redhat/local/*"
2986		       :key ?l))
2987
2988  ;; === Work - Trackers & Automation ===
2989  (add-to-list 'mu4e-bookmarks
2990	       '(:name "Jira Tickets"
2991		       :query "maildir:/redhat/_tracker/jira/*"
2992		       :key ?j))
2993
2994  (add-to-list 'mu4e-bookmarks
2995	       '(:name "All Trackers"
2996		       :query "maildir:/redhat/_tracker/*"
2997		       :key ?T))
2998
2999  (add-to-list 'mu4e-bookmarks
3000	       '(:name "Build Notifications"
3001		       :query "maildir:/redhat/_build/*"
3002		       :key ?B))
3003
3004  (add-to-list 'mu4e-bookmarks
3005	       '(:name "Receipts"
3006		       :query "maildir:/icloud/Receipts/*"
3007		       :key ?r))
3008
3009  (add-to-list 'mu4e-bookmarks
3010	       '(:name "Newsletters"
3011		       :query "maildir:/icloud/Newsletters/*"
3012		       :key ?n))
3013
3014  (add-to-list 'mu4e-bookmarks
3015	       '(:name "House Hunting"
3016		       :query "maildir:\"/icloud/house hunting/*\""
3017		       :key ?h))
3018
3019  ;; === Sent & Drafts ===
3020  (add-to-list 'mu4e-bookmarks
3021	       '(:name "Sent (All)"
3022		       :query "maildir:\"/icloud/Sent Messages\" OR maildir:\"/redhat/[Gmail]/Sent Mail\""
3023		       :key ?S))
3024
3025  (add-to-list 'mu4e-bookmarks
3026	       '(:name "Drafts (All)"
3027		       :query "maildir:/icloud/Drafts OR maildir:\"/redhat/[Gmail]/Drafts\""
3028		       :key ?D))
3029
3030  ;; === Advanced Queries ===
3031  (add-to-list 'mu4e-bookmarks
3032	       '(:name "With Attachments"
3033		       :query "flag:attach AND NOT flag:trashed"
3034		       :key ?A))
3035
3036  (add-to-list 'mu4e-bookmarks
3037	       '(:name "Large Emails (>1MB)"
3038		       :query "size:1M..500M AND NOT flag:trashed"
3039		       :key ?L))
3040
3041  (add-to-list 'mu4e-bookmarks
3042	       '(:name "To Me Directly"
3043		       :query "(to:vincent@demeester.fr OR to:vdemeest@redhat.com) AND NOT flag:trashed AND NOT maildir:/_tracker/*"
3044		       :key ?M))
3045
3046  ;; === Recent Activity ===
3047  (add-to-list 'mu4e-bookmarks
3048	       '(:name "Unread This Week (Work)"
3049		       :query "flag:unread AND date:7d..now AND maildir:/redhat/*"
3050		       :key ?W))
3051
3052  (add-to-list 'mu4e-bookmarks
3053	       '(:name "Unread This Week (Personal)"
3054		       :query "flag:unread AND date:7d..now AND maildir:/icloud/*"
3055		       :key ?P))
3056
3057  ;; Custom sorting: oldest-first for active folders, newest-first for archives
3058  (declare-function mu4e-search-change-sorting "mu4e")
3059  (declare-function vde/mu4e-set-sort-order-by-bookmark nil)
3060
3061  (defcustom vde/mu4e-oldest-first-bookmarks '(
3062					       "maildir:/icloud/Inbox"
3063					       "maildir:/redhat/Inbox"
3064					       "maildir:/icloud/Inbox OR maildir:/redhat/Inbox"  ; All Inboxes
3065					       "maildir:/icloud/Drafts"
3066					       "maildir:/redhat/[Gmail]/Drafts"
3067					       "flag:unread AND NOT flag:trashed"  ; Unread (All)
3068					       "flag:unread AND (maildir:/icloud/Inbox OR maildir:/redhat/Inbox)"  ; Unread Inboxes
3069					       "flag:flagged AND NOT flag:trashed"  ; Flagged
3070					       "(to:vincent@demeester.fr OR to:vdemeest@redhat.com) AND NOT flag:trashed AND NOT maildir:/_tracker/*"  ; To Me Directly
3071					       )
3072    "Bookmarks/folders to sort oldest-first (ascending).
3073Active folders like INBOX and Drafts show oldest messages first to prioritize
3074pending items by age. All other folders (Sent, Archives) will sort newest-first."
3075    :type '(repeat string)
3076    :group 'mu4e)
3077
3078  (defun vde/mu4e-set-sort-order-by-bookmark (search)
3079    "Set sort order based on bookmark type.
3080Active folders (INBOX, Drafts) sort ascending (oldest first) to prioritize
3081pending items. Archives and Sent folders sort descending (newest first) to
3082show recent activity."
3083    (if (member search vde/mu4e-oldest-first-bookmarks)
3084        (mu4e-search-change-sorting :date 'ascending)
3085      (mu4e-search-change-sorting :date 'descending)))
3086
3087  (add-hook 'mu4e-search-bookmark-hook #'vde/mu4e-set-sort-order-by-bookmark)
3088
3089  ;; Open mu4e links in a new tab
3090  ;; This overrides the default mu4e link handler to open in tab-bar
3091  (defun vde/mu4e-org-open-in-new-tab (link)
3092    "Open mu4e LINK in a new tab-bar tab.
3093LINK is the part after 'mu4e:' in the org link."
3094    (tab-bar-new-tab)
3095    (require 'mu4e-org)
3096    (mu4e-org-open link))
3097
3098  (with-eval-after-load 'ol
3099    (org-link-set-parameters "mu4e"
3100                             :follow #'vde/mu4e-org-open-in-new-tab))
3101
3102  ;; imapfilter integration
3103  (defun vde/mu4e-imapfilter-add-rule ()
3104    "Add an imapfilter rule from the current email message.
3105Prompts for rule type (from, domain, subject, header) and category
3106(delete, receipts, newsletters, archive), then appends the rule to
3107the appropriate file in ~/.local/share/imapfilter-rules/"
3108    (interactive)
3109    (unless (mu4e-message-at-point)
3110      (user-error "No message at point"))
3111    (let* ((msg (mu4e-message-at-point))
3112           (from (mu4e-message-field msg :from))
3113           (from-email (when from (plist-get (car from) :email)))
3114           (subject (mu4e-message-field msg :subject))
3115           (rules-dir (expand-file-name "~/.local/share/imapfilter-rules"))
3116
3117           ;; Prompt for rule type
3118           (rule-type (completing-read "Rule type: "
3119                                       '("from" "domain" "subject" "header")
3120                                       nil t))
3121
3122           ;; Generate rule pattern based on type
3123           (pattern
3124            (pcase rule-type
3125              ("from" from-email)
3126              ("domain" (when from-email
3127                          (concat "@" (replace-regexp-in-string "^.*@" "" from-email))))
3128              ("subject" (read-string "Subject pattern: " subject))
3129              ("header" (let ((header-name (read-string "Header name: "))
3130                              (header-value (read-string "Header value: ")))
3131                          (format "%s:%s" header-name header-value)))))
3132
3133           ;; Prompt for category
3134           (category (completing-read "Category: "
3135                                      '("delete" "receipts" "newsletters" "archive")
3136                                      nil t))
3137
3138           ;; Build rule line
3139           (rule-line (format "%s:%s" rule-type pattern))
3140           (rule-file (expand-file-name (format "%s.txt" category) rules-dir)))
3141
3142      ;; Validate
3143      (unless (file-directory-p rules-dir)
3144        (user-error "Rules directory does not exist: %s" rules-dir))
3145      (unless pattern
3146        (user-error "Could not extract pattern for rule type: %s" rule-type))
3147
3148      ;; Show preview and confirm
3149      (when (y-or-n-p (format "Add rule '%s' to %s.txt? " rule-line category))
3150        ;; Append to file
3151        (with-temp-buffer
3152          (insert rule-line)
3153          (insert "\n")
3154          (append-to-file (point-min) (point-max) rule-file))
3155
3156        (message "Added rule: %s → %s.txt" rule-line category)
3157
3158        ;; Offer to commit and push
3159        (when (y-or-n-p "Commit and push this rule? ")
3160          (let ((default-directory rules-dir))
3161            (shell-command (format "git add %s.txt" category))
3162            (shell-command (format "git commit -m 'Add %s rule: %s'" category rule-line))
3163            (shell-command "git push")
3164            (message "Rule committed and pushed"))))))
3165
3166  (with-eval-after-load "mm-decode"
3167    (add-to-list 'mm-discouraged-alternatives "text/html")
3168    (add-to-list 'mm-discouraged-alternatives "text/richtext")))
3169
3170;; TODO gptel configuration (and *maybe* copilot)
3171
3172
3173(use-package mcp
3174  :commands (mcp-hub-start-all-server)
3175  :after gptel
3176  :custom (mcp-hub-servers
3177	   `(("jira"
3178	      :command "/home/vincent/src/github.com/chmouel/jayrah/.venv/bin/jayrah"
3179	      :args ("mcp"))
3180	     ("github"
3181	      :command "github-mcp-server"
3182	      :args ("stdio")
3183	      :env (:GITHUB_PERSONAL_ACCESS_TOKEN ,(passage-get "github/vdemeester/github-mcp-server")))
3184	     ("playwright"
3185	      :command "npx @playwright/mcp@latest"
3186	      :args ("--executable-path", "/run/current-system/sw/bin/chromium"))))
3187  :config (require 'mcp-hub))
3188
3189(use-package gptel
3190  :commands (gptel gptel-mode)
3191  :init
3192  (declare-function gptel-make-ollama "gptel")
3193  (declare-function gptel-make-openai "gptel")
3194  (declare-function gptel-make-gemini "gptel")
3195  (declare-function gptel-mcp-connect "gptel")
3196  (defvar gptel-mode-map)
3197  :bind (("C-c a g" . gptel))
3198  :hook
3199  (gptel-mode . visual-line-mode)
3200  (gptel-mode . visual-wrap-prefix-mode)
3201  :bind
3202  (:map gptel-mode-map
3203        ("C-c C-k" . gptel-abort)
3204        ("C-c C-m" . gptel-menu)
3205        ("C-c C-c" . gptel-send))
3206  :custom
3207  (gptel-default-mode #'markdown-mode)
3208  :config
3209  ;; (require 'gptel-curl)
3210  (require 'gptel-gemini)
3211  (require 'gptel-transient)
3212  (require 'gptel-integrations)
3213  (require 'gptel-rewrite)
3214  (require 'gptel-org)
3215  (require 'gptel-openai)
3216  (require 'gptel-openai-extras)
3217  (require 'gptel-autoloads)
3218  (gptel-mcp-connect)
3219  
3220  (setq gptel-model 'gemini-2.5-flash
3221	gptel-backend (gptel-make-gemini "Gemini"
3222					 :key (passage-get "ai/gemini/api_key"))
3223	)
3224
3225  (gptel-make-gemini "Gemini Red Hat"
3226		     :key (passage-get "redhat/google/osp/vdeemest-api-key"))
3227  
3228  )
3229
3230(use-package acp
3231  :defer t)
3232
3233(use-package agent-shell
3234  :commands (agent-shell agent-shell-toggle agent-shell-new-shell)
3235  :bind (("C-c a a" . vde/agent-shell-dwim)
3236	 ("C-c a n" . agent-shell-new-shell)
3237	 ("C-c a A" . agent-shell-toggle)
3238	 ("C-c a ?" . agent-shell-help-menu))
3239  :init
3240  (declare-function agent-shell-google-make-authentication "agent-shell")
3241  (declare-function agent-shell-make-environment-variables "agent-shell")
3242  (declare-function agent-shell-project-buffers "agent-shell")
3243  (declare-function agent-shell--read-shell-buffer "agent-shell")
3244  (declare-function agent-shell--display-buffer "agent-shell")
3245  (defun vde/agent-shell-dwim (&optional arg)
3246    "Project-aware agent-shell launcher.
3247
3248No prefix ARG:
3249- 0 shells in this project -> start a new one
3250- 1 shell  -> toggle its window (show/hide)
3251- N shells -> `completing-read' to pick one, then display.
3252
3253With \\[universal-argument]: always force a brand-new shell.
3254With \\[universal-argument] \\[universal-argument]: always prompt with completion."
3255    (interactive "P")
3256    (cond
3257     ((equal arg '(16)) (agent-shell '(16)))
3258     (arg (agent-shell '(4)))
3259     (t
3260      (let ((buffers (agent-shell-project-buffers)))
3261	(pcase (length buffers)
3262	  (0 (agent-shell))
3263	  (1 (let* ((buf (car buffers))
3264		    (win (get-buffer-window buf)))
3265	       (if win
3266		   (quit-restore-window win 'bury)
3267		 (agent-shell--display-buffer buf))))
3268	  (_ (agent-shell--display-buffer
3269	      (agent-shell--read-shell-buffer
3270	       :prompt "Project agent shell: "
3271	       :buffers buffers))))))))
3272  :config
3273  (setq agent-shell-google-authentication
3274	(agent-shell-google-make-authentication :api-key (passage-get "redhat/google/osp/vdeemest-api-key")))
3275  (setq agent-shell-anthropic-claude-environment
3276	(agent-shell-make-environment-variables
3277	 "CLAUDE_CODE_USE_VERTEX" "1"
3278	 "CLOUD_ML_REGION" "global"
3279	 "ANTHROPIC_VERTEX_PROJECT_ID" "itpc-gcp-pnd-pe-eng-claude")))
3280
3281(use-package agent-shell-pi
3282  :after agent-shell
3283  :defer t
3284  :config
3285  (setq agent-shell-pi-environment
3286	(agent-shell-make-environment-variables
3287	 "GOOGLE_CLOUD_PROJECT" "itpc-gcp-pnd-pe-eng-claude"
3288	 "GOOGLE_CLOUD_LOCATION" "global"
3289	 "GEMINI_API_KEY" (passage-get "redhat/google/osp/vdeemest-api-key"))))
3290
3291(use-package phscroll
3292  :defer t)
3293
3294(use-package pi-from-github
3295  :after pilish
3296  :commands (pi-from-github-pr pi-from-github-issue)
3297  :bind (("C-c a r" . pi-from-github-pr)
3298         ("C-c a i" . pi-from-github-issue)))
3299
3300(use-package pilish
3301  :commands (pilish pilish-toggle)
3302  :bind (("C-c a p" . vde/pilish-dwim)
3303         ("C-c a P" . pilish))
3304  :init
3305  (defalias 'pi #'pilish)
3306  (declare-function pilish-project-buffers "pilish-ui")
3307  (declare-function pilish-toggle "pilish")
3308  (declare-function pilish--show-session-buffers "pilish")
3309  (defun vde/pilish-dwim (&optional arg)
3310    "Project-aware pilish launcher.
3311
3312No prefix ARG:
3313- 0 sessions in this project -> start the default one
3314- 1 session  -> toggle its windows (show/hide)
3315- N sessions -> `completing-read' to pick one, then show.
3316
3317With \\[universal-argument]: prompt for a session name (new named session).
3318With \\[universal-argument] \\[universal-argument]: always prompt with completion."
3319    (interactive "P")
3320    (require 'pilish-ui nil t)
3321    (cond
3322     ((equal arg '(16))
3323      (let* ((buffers (or (pilish-project-buffers)
3324			  (user-error "No pi session for this project")))
3325	     (alist (mapcar (lambda (b) (cons (buffer-name b) b)) buffers))
3326	     (choice (completing-read "Pi session: " alist nil t))
3327	     (chat-buf (cdr (assoc choice alist)))
3328	     (input-buf (buffer-local-value 'pilish--input-buffer
3329					    chat-buf)))
3330	(pilish--show-session-buffers chat-buf input-buf)))
3331     (arg
3332      (let ((current-prefix-arg '(4)))
3333	(call-interactively #'pilish)))
3334     (t
3335      (let ((buffers (pilish-project-buffers)))
3336	(pcase (length buffers)
3337	  (0 (pilish))
3338	  (1 (pilish-toggle))
3339	  (_ (let* ((alist (mapcar (lambda (b) (cons (buffer-name b) b)) buffers))
3340		    (choice (completing-read "Pi session: " alist nil t))
3341		    (chat-buf (cdr (assoc choice alist)))
3342		    (input-buf (buffer-local-value 'pilish--input-buffer
3343						   chat-buf)))
3344	       (pilish--show-session-buffers chat-buf input-buf))))))))
3345  :custom
3346  (pilish-input-window-height 10)
3347  (pilish-tool-preview-lines 10)
3348  (pilish-bash-preview-lines 5)
3349  (pilish-executable '("pir"))
3350  (pilish-context-warning-threshold 70)
3351  (pilish-context-error-threshold 90)
3352  ;; Trust project-local .pi resources (prompts, skills, settings, themes,
3353  ;; extensions) by default — frontend equivalent of --approve (v2.5.0).
3354  (pilish-project-trust-policy 'approve)
3355  :config
3356  ;; Shorten buffer names (and thus the Emacs frame/window title) from
3357  ;; "*pilish-input:~/src/home*" to "*pi:input:~/src/home*".
3358  (declare-function pilish--buffer-name "pilish-ui")
3359  (defun vde/pilish-buffer-name (type dir &optional session)
3360    "Shorter buffer name for pilish TYPE in DIR with SESSION."
3361    (let ((type-str (pcase type (:chat "chat") (:input "input")))
3362          (abbrev-dir (abbreviate-file-name dir)))
3363      (if (and session (not (string-empty-p session)))
3364          (format "*pi:%s:%s<%s>*" type-str abbrev-dir session)
3365        (format "*pi:%s:%s*" type-str abbrev-dir))))
3366  (advice-add 'pilish--buffer-name :override
3367              #'vde/pilish-buffer-name)
3368
3369  ;; NOTE: thinking blocks are visible by default since v2.5.0, which also
3370  ;; batches history replay and avoids the O(n²) re-render that previously
3371  ;; forced us to hide thinking. Re-enable the advice below only if long
3372  ;; chats become sluggish again.
3373  ;; Notify when a Pi session goes idle (task finished) so background work
3374  ;; surfaces without watching the buffer (v2.5.0 activity phase hooks).
3375  (add-hook 'pilish-activity-phase-functions
3376            (lambda (chat-buffer _input-buffer old-phase new-phase reason)
3377              (when (and (eq reason 'phase-change)
3378                         (equal new-phase "idle")
3379                         (not (equal old-phase "idle"))
3380                         (buffer-live-p chat-buffer)
3381                         (not (eq chat-buffer (window-buffer (selected-window)))))
3382                (alert "Pi session idle" :title "pilish"
3383                       :buffer chat-buffer))))
3384  ;; Remove ispell from completion in pi input buffer (conflicts with corfu)
3385  (add-hook 'pilish-input-mode-hook
3386            (lambda ()
3387              (remove-hook 'completion-at-point-functions #'ispell-completion-at-point t)))
3388  ;; Auto-resync status when pi reports idle but Emacs thinks it's streaming
3389  ;; Workaround for OSC escape sequences in RPC output breaking event parsing
3390  (add-hook 'pilish-input-mode-hook
3391            (lambda ()
3392              (add-hook 'post-command-hook
3393                        (lambda ()
3394                          (when-let ((chat-buf (pilish--get-chat-buffer)))
3395                            (with-current-buffer chat-buf
3396                              (when (and (eq pilish--status 'streaming)
3397                                         (pilish--state-needs-verification-p))
3398                                (pilish--rpc-async
3399                                 pilish--process
3400                                 '(:type "get_state")
3401                                 (lambda (r)
3402                                   (when (buffer-live-p chat-buf)
3403                                     (with-current-buffer chat-buf
3404                                       (pilish--update-state-from-response r)))))))))
3405                        nil t)))
3406  ;; Refresh header after extension slash commands (e.g. /mode) so the model
3407  ;; name stays in sync.  Extension-driven pi.setModel() calls emit no RPC
3408  ;; model-change event, so we re-query get_state after the prompt response.
3409  (define-advice pilish--send-prompt (:around (orig text &rest args) refresh-after-ext-cmd)
3410    "After non-builtin slash commands, refresh state so the header updates."
3411    (if (and (string-prefix-p "/" text)
3412             (not (assoc (car (split-string (substring text 1)))
3413                         pilish--builtin-commands)))
3414        (let ((proc (pilish--get-process))
3415              (chat-buf (pilish--get-chat-buffer)))
3416          (apply orig text args)
3417          (when (and proc (process-live-p proc) (buffer-live-p chat-buf))
3418            ;; Delay: let the extension handler finish its async work
3419            (run-at-time 2.0 nil
3420			 (lambda ()
3421			   (when (and (process-live-p proc) (buffer-live-p chat-buf))
3422			     (pilish--rpc-async proc '(:type "get_state")
3423							 (lambda (r)
3424							   (when (and (plist-get r :success) (buffer-live-p chat-buf))
3425							     (with-current-buffer chat-buf
3426							       (pilish--update-state-from-response r)
3427							       (pilish--refresh-header))))))))))
3428      (apply orig text args))))
3429
3430;; Make markdown links [text](url) clickable in pi chat buffers.
3431;; Tree-sitter markdown hides the URL portion; this jit-lock function
3432;; finds inline links and adds button properties so RET/click opens them.
3433(with-eval-after-load 'pilish
3434  (defvar vde/pi-link-keymap
3435    (let ((map (make-sparse-keymap)))
3436      (define-key map [mouse-1]
3437		  (lambda (e)
3438		    (interactive "e")
3439		    (when-let ((url (get-text-property
3440				     (posn-point (event-start e))
3441				     'pi-link-url)))
3442		      (browse-url url))))
3443      (define-key map (kbd "RET")
3444		  (lambda ()
3445		    (interactive)
3446		    (when-let ((url (get-text-property (point) 'pi-link-url)))
3447		      (browse-url url))))
3448      map)
3449    "Keymap for clickable markdown links in pi chat buffers.")
3450
3451  (defun vde/pi-buttonize-markdown-links (beg end)
3452    "Buttonize markdown inline links in BEG..END for pi chat buffers."
3453    (save-excursion
3454      (goto-char beg)
3455      (while (re-search-forward "\\[\\([^]]+\\)\\](\\([^)]+\\))" end t)
3456	(let ((text-beg (match-beginning 1))
3457	      (text-end (match-end 1))
3458	      (url (match-string-no-properties 2)))
3459	  (unless (get-text-property text-beg 'pi-link-url)
3460	    (let ((inhibit-read-only t))
3461	      (put-text-property text-beg text-end 'pi-link-url url)
3462	      (put-text-property text-beg text-end 'help-echo url)
3463	      (put-text-property text-beg text-end 'mouse-face 'highlight)
3464	      (put-text-property text-beg text-end 'keymap vde/pi-link-keymap)
3465	      (add-face-text-property text-beg text-end
3466					'(:underline t) 'append)))))))
3467
3468  (add-hook 'pilish-chat-mode-hook
3469	    (lambda ()
3470	      (jit-lock-register #'vde/pi-buttonize-markdown-links))))
3471
3472(use-package devdocs
3473  :commands (devdocs-lookup devdocs-install vde/install-devdocs)
3474  :bind (("C-h D" . devdocs-lookup))
3475  :config
3476  (defun vde/install-devdocs ()
3477    "Install the devdocs I am using the most."
3478    (interactive)
3479    (dolist (docset '("bash"
3480		      "c"
3481		      "click"
3482		      "cpp"
3483		      "css"
3484		      "elisp"
3485		      "flask"
3486		      "git"
3487		      "gnu_make"
3488		      "go"
3489		      "html"
3490		      "htmx"
3491		      "http"
3492		      "javascript"
3493		      "jq"
3494		      "jquery"
3495		      "kubectl"
3496		      "kubernetes"
3497		      "lua~5.4"
3498		      "nix"
3499		      "python~3.13"
3500		      "python~3.12"
3501		      "requests"
3502		      "sqlite"
3503		      "terraform"
3504		      "werkzeug"
3505		      "zig"))
3506      (devdocs-install docset))))
3507
3508(use-package proced
3509  :ensure nil
3510  :defer t
3511  :custom
3512  (proced-enable-color-flag t)
3513  (proced-tree-flag t)
3514  (proced-auto-update-flag 'visible)
3515  (proced-auto-update-interval 1)
3516  (proced-descend t)
3517  (proced-format 'medium) ;; can be changed interactively with `F'
3518  (proced-filter 'user))  ;; can be changed interactively with `f'
3519
3520(use-package ytr
3521  :commands (ytr)
3522  :bind ("C-c m y" . ytr))
3523
3524(use-package empv
3525  :commands (empv-subsonic-search
3526             empv-subsonic-artists
3527             empv-subsonic-albums
3528             empv-subsonic-songs)
3529  :bind (("C-c M s" . empv-subsonic-search)
3530         ("C-c M a" . empv-subsonic-albums)
3531         ("C-c M P" . empv-subsonic-playlists)
3532         ("C-c M R" . empv-subsonic-radios)
3533         ("C-c M SPC" . empv-toggle)
3534         ("C-c M i" . empv-display-current)
3535         ("C-c M n" . empv-playlist-next)
3536         ("C-c M p" . empv-playlist-prev)
3537         :prefix "C-c M q"
3538         :prefix-map empv-queue-map
3539         ("s" . empv-playlist-select)
3540         ("c" . empv-playlist-clear)
3541         ("r" . empv-playlist-shuffle)
3542         ("l" . empv-playlist-loop-on)
3543         ("L" . empv-playlist-loop-off)
3544         ("S" . my/empv-save-queue-to-playlist))
3545  :config
3546  (require 'my-empv-playlists)
3547  (setq empv-subsonic-url "https://music.sbr.pm"
3548        empv-subsonic-username (passage-get "home/music.sbr.pm/username")
3549        empv-subsonic-password (passage-get "home/music.sbr.pm/password"))
3550
3551  ;; Subsonic playlist support (not yet upstream in empv.el)
3552  ;; Adds formatting, act-on-candidate handling, and interactive command
3553  ;; for getPlaylists/getPlaylist Subsonic API endpoints.
3554
3555  (defun empv--subsonic-format-playlist (cand)
3556    "Format a Subsonic playlist candidate for display."
3557    (format "%s %s"
3558            (propertize (alist-get 'name cand) 'face 'bold)
3559            (propertize (format "[%s songs]" (or (alist-get 'songCount cand) "?")) 'face 'italic)))
3560
3561  ;; Extend the formatter to handle playlist kind
3562  (advice-add 'empv--subsonic-format-candidate :around
3563              (lambda (orig cand)
3564                (if (eq (alist-get 'kind cand) 'playlist)
3565                    (empv--with-text-properties
3566                     (empv--subsonic-format-playlist cand)
3567                     :item cand)
3568                  (funcall orig cand))))
3569
3570  ;; Extend act-on-candidate to handle playlist kind
3571  ;; Fetches playlist tracks via getPlaylist and enqueues all songs
3572  (advice-add 'empv--subsonic-act-on-candidate :around
3573              (lambda (orig selected &optional on-quit)
3574                (if (eq (alist-get 'kind selected) 'playlist)
3575                    (let ((id (alist-get 'id selected))
3576                          (name (alist-get 'name selected)))
3577                      (empv--subsonic-request
3578                       "getPlaylist.view"
3579                       :id id
3580                       (lambda (response)
3581                         ;; getPlaylist returns entries under 'entry key
3582                         ;; but empv's processor puts them under 'results
3583                         ;; if they match result-fields. Since 'entry is
3584                         ;; not in result-fields, we extract manually.
3585                         (let* ((raw (alist-get 'entry response))
3586                                (songs (or (alist-get 'results response) raw)))
3587                           (if songs
3588                               (empv-play-or-enqueue
3589                                (mapcar #'empv--subsonic-item-extract-url
3590                                        (if (alist-get 'results response)
3591                                            songs
3592                                          ;; raw entries need kind annotation
3593                                          (mapcar (lambda (s)
3594                                                    (thread-first s
3595                                                      (map-insert 'kind 'song)
3596                                                      (map-insert 'type 'subsonic)))
3597                                                  songs))))
3598                             (message "Playlist '%s' is empty" name))))))
3599                  (funcall orig selected on-quit))))
3600
3601  (defun my/empv-subsonic-act-on-playlist (orig action obj)
3602    "Apply ACTION to playlist OBJ, or delegate to ORIG."
3603    (if (eq (alist-get 'kind obj) 'playlist)
3604        (empv--subsonic-request
3605         "getPlaylist.view" :id (alist-get 'id obj)
3606         (lambda (response)
3607           (let ((songs (or (alist-get 'results response)
3608                            (alist-get 'entry response))))
3609             (if songs
3610                 (funcall (or action #'empv-play)
3611                          (mapcar (lambda (song)
3612                                    (empv--subsonic-item-extract-url
3613                                     (thread-first song
3614                                       (map-insert 'kind 'song)
3615                                       (map-insert 'type 'subsonic))))
3616                                  songs))
3617               (message "Playlist '%s' is empty" (alist-get 'name obj))))))
3618      (funcall orig action obj)))
3619
3620  (advice-add 'empv--subsonic-act :around #'my/empv-subsonic-act-on-playlist)
3621
3622  (defun empv-subsonic-playlists ()
3623    "List Subsonic playlists and act on selection."
3624    (interactive)
3625    (empv--subsonic-request
3626     "getPlaylists.view"
3627     (empv--subsonic-result-handler "Select playlist:")))
3628
3629  ;; Subsonic internet radio support
3630  ;; Uses getInternetRadioStations Subsonic API endpoint.
3631
3632  (defun empv--subsonic-format-radio (cand)
3633    "Format a Subsonic radio candidate for display."
3634    (format "%s %s"
3635            (propertize (alist-get 'name cand) 'face 'bold)
3636            (propertize (or (alist-get 'homePageUrl cand) "") 'face 'italic)))
3637
3638  ;; Extend the formatter to handle radio kind
3639  (advice-add 'empv--subsonic-format-candidate :around
3640              (lambda (orig cand)
3641                (if (eq (alist-get 'kind cand) 'radio)
3642                    (empv--with-text-properties
3643                     (empv--subsonic-format-radio cand)
3644                     :item cand)
3645                  (funcall orig cand))))
3646
3647  ;; Extend act-on-candidate to handle radio kind
3648  ;; Radio stations have a direct streamUrl, no need to fetch sub-items
3649  (advice-add 'empv--subsonic-act-on-candidate :around
3650              (lambda (orig selected &optional on-quit)
3651                (if (eq (alist-get 'kind selected) 'radio)
3652                    (let ((url (alist-get 'streamUrl selected)))
3653                      (if url
3654                          (empv-play url)
3655                        (message "Radio '%s' has no stream URL" (alist-get 'name selected))))
3656                  (funcall orig selected on-quit))))
3657
3658  (defun empv-subsonic-radios ()
3659    "List Subsonic internet radio stations and play selection."
3660    (interactive)
3661    (empv--subsonic-request
3662     "getInternetRadioStations.view"
3663     (lambda (response)
3664       ;; empv's result-fields doesn't include internetRadioStation,
3665       ;; so we extract manually from the raw response info.
3666       (let* ((raw (alist-get 'internetRadioStation response))
3667              (stations (mapcar (lambda (s)
3668                                 (thread-first s
3669                                   (map-insert 'kind 'radio)
3670                                   (map-insert 'type 'subsonic)))
3671                               raw)))
3672         (if stations
3673             (empv--subsonic-act-on-candidate
3674              (empv--completing-read-object
3675               "Select radio:"
3676               stations
3677               :formatter #'empv--subsonic-format-candidate
3678               :category 'empv-subsonic-item))
3679           (message "No internet radio stations found")))))))
3680
3681(use-package ready-player
3682  :init
3683  (declare-function ready-player-mode "ready-player")
3684  :config
3685  (ready-player-mode +1))
3686
3687(defvar highlight-codetags-keywords
3688  '(("\\<\\(TODO\\|FIXME\\|BUG\\|XXX\\)\\>" 1 font-lock-warning-face prepend)
3689    ("\\<\\(NOTE\\|HACK\\)\\>" 1 font-lock-doc-face prepend)))
3690
3691(define-minor-mode highlight-codetags-local-mode
3692  "Highlight codetags like TODO, FIXME..."
3693  :global nil
3694  (if highlight-codetags-local-mode
3695      (font-lock-add-keywords nil highlight-codetags-keywords)
3696    (font-lock-remove-keywords nil highlight-codetags-keywords))
3697
3698  ;; Fontify the current buffer
3699  (when (bound-and-true-p font-lock-mode)
3700    (if (fboundp 'font-lock-flush)
3701        (font-lock-flush)
3702      (with-no-warnings (font-lock-fontify-buffer)))))
3703
3704(add-hook 'prog-mode-hook #'highlight-codetags-local-mode)
3705
3706(defun vde/wtype-text (text)
3707  "Process TEXT for wtype, handling newlines properly."
3708  (let* ((has-final-newline (string-match-p "\n$" text))
3709         (lines (split-string text "\n"))
3710         (last-idx (1- (length lines))))
3711    (string-join
3712     (cl-loop for line in lines
3713              for i from 0
3714              collect (cond
3715                       ;; Last line without final newline
3716                       ((and (= i last-idx) (not has-final-newline))
3717                        (format "wtype -s 50 \"%s\"" 
3718                                (replace-regexp-in-string "\"" "\\\\\"" line)))
3719                       ;; Any other line
3720                       (t
3721                        (format "wtype -s 50 \"%s\" && wtype -k Return" 
3722                                (replace-regexp-in-string "\"" "\\\\\"" line)))))
3723     " && ")))
3724
3725(define-minor-mode vde/type-mode
3726  "Minor mode for inserting text via wtype."
3727  :keymap `((,(kbd "C-c C-c") . ,(lambda () (interactive)
3728                                   (call-process-shell-command
3729                                    (vde/wtype-text (buffer-string))
3730                                    nil 0)
3731                                   (delete-frame)))
3732            (,(kbd "C-c C-k") . ,(lambda () (interactive)
3733                                   (kill-buffer (current-buffer))))))
3734
3735(defun vde/type ()
3736  "Launch a temporary frame with a clean buffer for typing."
3737  (interactive)
3738  (let ((frame (make-frame '((name . "emacs-float")
3739                             (fullscreen . 0)
3740                             (undecorated . t)
3741                             (width . 70)
3742                             (height . 20))))
3743        (buf (get-buffer-create "emacs-float")))
3744    (select-frame frame)
3745    (switch-to-buffer buf)
3746    (with-current-buffer buf
3747      (erase-buffer)
3748      ;; (org-mode)
3749      (markdown-mode) ;; more common ?
3750      (flyspell-mode)
3751      (vde/type-mode)
3752      (setq-local header-line-format
3753                  (format " %s to insert text or %s to cancel."
3754                          (propertize "C-c C-c" 'face 'help-key-binding)
3755			  (propertize "C-c C-k" 'face 'help-key-binding)))
3756      ;; Make the frame more temporary-like
3757      (set-frame-parameter frame 'delete-before-kill-buffer t)
3758      (set-window-dedicated-p (selected-window) t))))
3759
3760(defvar-local vde/herdr-prompt--agent nil
3761  "Herdr agent metadata for the current prompt buffer.")
3762
3763(defun vde/herdr-prompt--cancel ()
3764  "Cancel the current Herdr prompt."
3765  (interactive)
3766  (let ((frame (selected-frame)))
3767    (kill-buffer (current-buffer))
3768    (when (frame-live-p frame)
3769      (delete-frame frame))))
3770
3771(defun vde/herdr-prompt--submit ()
3772  "Submit the current buffer to its Herdr agent."
3773  (interactive)
3774  (when (equal (plist-get vde/herdr-prompt--agent :agent_status) "blocked")
3775    (user-error "Herdr reports this agent as blocked; inspect it before prompting"))
3776  (let ((text (string-trim (buffer-string)))
3777        (pane-id (plist-get vde/herdr-prompt--agent :pane_id))
3778        (prompt-buffer (current-buffer))
3779        (frame (selected-frame)))
3780    (when (string-empty-p text)
3781      (user-error "Prompt is empty"))
3782    (with-temp-buffer
3783      (let ((status (process-file "herdr" nil t nil "agent" "prompt" pane-id text)))
3784        (if (zerop status)
3785            (progn
3786              (message "Prompt submitted to %s" pane-id)
3787              (kill-buffer prompt-buffer)
3788              (when (frame-live-p frame)
3789                (delete-frame frame)))
3790          (user-error "Herdr rejected prompt: %s" (string-trim (buffer-string))))))))
3791
3792(defvar vde/herdr-prompt-mode-map
3793  (let ((map (make-sparse-keymap)))
3794    (define-key map (kbd "C-c C-c") #'vde/herdr-prompt--submit)
3795    (define-key map (kbd "C-c C-k") #'vde/herdr-prompt--cancel)
3796    map)
3797  "Keymap for `vde/herdr-prompt-mode'.")
3798
3799(define-derived-mode vde/herdr-prompt-mode markdown-mode "Herdr Prompt"
3800  "Major mode for composing a prompt to a Herdr agent."
3801  (flyspell-mode 1))
3802
3803(defun vde/herdr-prompt-from-base64 (encoded-agent)
3804  "Open a transient frame for the base64-encoded Herdr agent metadata."
3805  (let* ((agent (json-parse-string (base64-decode-string encoded-agent)
3806                                   :object-type 'plist))
3807         (status (plist-get agent :agent_status))
3808         (pane-id (plist-get agent :pane_id))
3809         (title (or (plist-get agent :terminal_title_stripped) pane-id))
3810         (cwd (or (plist-get agent :cwd) "unknown directory"))
3811         (frame (make-frame '((name . "herdr-prompt")
3812                              (fullscreen . 0)
3813                              (undecorated . t)
3814                              (width . 90)
3815                              (height . 24))))
3816         (buffer (generate-new-buffer (format "*Herdr prompt: %s*" pane-id))))
3817    (select-frame frame)
3818    (switch-to-buffer buffer)
3819    (with-current-buffer buffer
3820      (vde/herdr-prompt-mode)
3821      (setq-local vde/herdr-prompt--agent agent)
3822      (setq-local header-line-format
3823                  (format " %s — %s — %s%s  |  C-c C-c: send  C-c C-k: cancel"
3824                          title status cwd
3825                          (if (equal status "working")
3826                              "  WARNING: this will steer active work"
3827                            "")))
3828      (set-frame-parameter frame 'delete-before-kill-buffer t)
3829      (set-window-dedicated-p (selected-window) t))))
3830
3831(defun vde/agenda ()
3832  "Launch a frame with the org-agenda and the `org-todos-file' buffer."
3833  (interactive)
3834  (let ((frame (make-frame '((name . "emacs-org-agenda")))))
3835    (select-frame frame)
3836    (org-agenda nil "d")
3837    (split-window-horizontally)
3838    (other-window 1)
3839    (find-file org-todos-file)))
3840
3841(defun memoize-remote (key cache orig-fn &rest args)
3842  "Memoize a value if the key is a remote path."
3843  (if (and key
3844           (file-remote-p key))
3845      (if-let ((current (assoc key (symbol-value cache))))
3846          (cdr current)
3847        (let ((current (apply orig-fn args)))
3848          (set cache (cons (cons key current) (symbol-value cache)))
3849          current))
3850    (apply orig-fn args)))
3851;; Memoize current project
3852(defvar project-current-cache nil)
3853(defun memoize-project-current (orig &optional prompt directory)
3854  (memoize-remote (or directory
3855                      project-current-directory-override
3856                      default-directory)
3857                  'project-current-cache orig prompt directory))
3858
3859(advice-add 'project-current :around #'memoize-project-current)
3860
3861;; Memoize magit top level
3862(defvar magit-toplevel-cache nil)
3863(defun memoize-magit-toplevel (orig &optional directory)
3864  (memoize-remote (or directory default-directory)
3865                  'magit-toplevel-cache orig directory))
3866(advice-add 'magit-toplevel :around #'memoize-magit-toplevel)
3867
3868;; memoize vc-git-root
3869(defvar vc-git-root-cache nil)
3870(defun memoize-vc-git-root (orig file)
3871  (let ((value (memoize-remote (file-name-directory file) 'vc-git-root-cache orig file)))
3872    ;; sometimes vc-git-root returns nil even when there is a root there
3873    (when (null (cdr (car vc-git-root-cache)))
3874      (setq vc-git-root-cache (cdr vc-git-root-cache)))
3875    value))
3876(advice-add 'vc-git-root :around #'memoize-vc-git-root)
3877
3878;; BIND this
3879(defun mu-date-at-point (date)
3880  "Insert current DATE at point via `completing-read'."
3881  (interactive
3882   (let* ((formats '("%Y%m%d" "%F" "%Y%m%d%H%M" "%Y-%m-%dT%T"))
3883          (vals (mapcar #'format-time-string formats))
3884          (opts
3885           (lambda (string pred action)
3886             (if (eq action 'metadata)
3887                 '(metadata (display-sort-function . identity))
3888               (complete-with-action action vals string pred)))))
3889     (list (completing-read "Insert date: " opts nil t))))
3890  (insert date))
3891
3892(provide 'init)
3893
3894;; Local Variables:
3895;; byte-compile-warnings: (not free-vars)
3896;; End:
3897
3898;;; init.el ends here