From d4879ef719e81a7c436b88d70d234baf993b0c32 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 11 Apr 2026 18:37:32 +0800 Subject: [PATCH] emacs: Fix olivetti mode issue, add a manual toggle check --- .config/emacs/init.org | 80 ++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/.config/emacs/init.org b/.config/emacs/init.org index c274f2f..5d9a11f 100644 --- a/.config/emacs/init.org +++ b/.config/emacs/init.org @@ -348,33 +348,50 @@ Use Dashboard.el. First load `all-the-icons` for nicer rendering #+begin_src emacs-lisp (require 'olivetti) -(setq olivetti-minimum-body-width 160) +(setq olivetti-minimum-body-width 140) -(global-set-key (kbd "C-x x o") 'olivetti-mode) +(defvar-local joe/olivetti-manually-toggled nil + "Non-nil if olivetti-mode was manually toggled via keybinding.") + +(defun joe/olivetti-manual-toggle () + "Toggle olivetti-mode and mark buffer as manually toggled." + (interactive) + (setq joe/olivetti-manually-toggled t) + (olivetti-mode 'toggle) + (message "Olivetti mode %s" (if olivetti-mode "enabled" "disabled"))) + +(global-set-key (kbd "C-x x o") #'joe/olivetti-manual-toggle) (defun joe/toggle-olivetti-based-on-neighbors () - (when (derived-mode-p 'org-mode 'prog-mode) + (when (and (derived-mode-p 'org-mode 'prog-mode) + (not joe/olivetti-manually-toggled)) (if (or (window-in-direction 'left) (window-in-direction 'right)) (olivetti-mode -1) (olivetti-mode 1)))) - (add-hook 'window-configuration-change-hook #'joe/toggle-olivetti-based-on-neighbors) (defun joe/olivetti-minibuffer () (setq-local olivetti-minimum-body-width 180) (olivetti-mode 1)) -(add-hook 'minibuffer-setup-hook #'joe/olivetti-minibuffer) +;; (add-hook 'minibuffer-setup-hook #'joe/olivetti-minibuffer) ;; (remove-hook 'window-configuration-change-hook #'joe/toggle-olivetti-based-on-neighbors) #+end_src -Remove this hook from Olivetti so that lines can truncate [[https://github.com/rnkn/olivetti/issues/76][Github issue]] +Remove this hook from Olivetti so that lines can truncate [[https://github.com/rnkn/olivetti/issues/76][Github issue]], but keep visual-line-mode for org-mode #+begin_src emacs-lisp (remove-hook 'olivetti-mode-on-hook 'visual-line-mode) + +(defun joe/restore-visual-line-mode-for-org () + "Re-enable visual-line-mode when olivetti-mode turns off in org buffers." + (when (derived-mode-p 'org-mode) + (visual-line-mode 1))) + +(add-hook 'olivetti-mode-off-hook #'joe/restore-visual-line-mode-for-org) #+end_src *** Themes @@ -459,7 +476,7 @@ Setup other stuff ;; (right-divider-width . 5) ;; (tab-bar-lines . 0) ;; (internal-border-width . 12))) -(add-hook 'text-mode-hook (lambda () (setq fill-column 80) (turn-on-auto-fill))) +(add-hook 'text-mode-hook (lambda () (setq fill-column 120) (turn-on-auto-fill))) ;; (setq display-line-numbers 'relative) (make-variable-buffer-local 'global-hl-line-mode) @@ -516,8 +533,8 @@ Setup other stuff ;; TODO Likely not needed anymore -(dolist (mode '(dired-mode-hook lsp-help-mode-hook fundamental-mode-hook)) - (add-hook mode (lambda () (setq truncate-lines t)))) +;; (dolist (mode '(dired-mode-hook lsp-help-mode-hook fundamental-mode-hook)) +;; (add-hook mode (lambda () (setq truncate-lines t)))) #+end_src Ligatures... are they that useful? @@ -624,7 +641,8 @@ Emacs is an great operating system, if only it had a good text editor... (global-set-key (kbd "M-o") #'joe/insert-line-below) (global-set-key (kbd "M-O") #'joe/insert-line-above) -(setq-default truncate-lines t) +;; (setq-default truncate-lines nil) +(setq-default truncate-partial-width-windows nil) (defun joe/backward-kill-word-or-kill-region () (interactive) @@ -806,7 +824,6 @@ Fill region is great, except when you don't need it... (kbd "SPC sv") 'vterm-other-window (kbd "SPC mm") 'mu4e (kbd "SPC cl") 'consult-line - (kbd "SPC ci") 'consult-imenu (kbd "SPC cy") 'consult-yank-from-kill-ring (kbd "SPC cg") 'consult-ripgrep (kbd "SPC cF") 'consult-find @@ -849,9 +866,10 @@ Fill region is great, except when you don't need it... (setq evil-goggles-async-duration 0.55) ;; Recenter lines after mark jump -(defun joe/recenter-after-goto-mark () - (recenter)) -(advice-add 'evil-goto-mark :after #'joe/recenter-after-goto-mark) +;; (defun joe/recenter-after-goto-mark () +;; (interactive) +;; (recenter)) +;; (advice-add 'evil-goto-mark :after #'joe/recenter-after-goto-mark) #+end_src *** COMMENT easy-kill #+begin_src emacs-lisp @@ -1952,7 +1970,7 @@ close and remove the hook (with-current-buffer vterm-buf (setq mode-line-format nil) (setq popper-popup-status 'raised) - (olivetti-mode) + ;; (olivetti-mode) (add-hook 'delete-frame-functions #'joe/kill-vterm-scratch) (add-hook 'kill-buffer-hook #'joe/kill-frame)))) #+end_src @@ -2096,8 +2114,6 @@ These help speed eglot up apparently [[https://www.reddit.com/r/emacs/comments/1 (flymake-mode -1)) (add-hook 'lsp-mode-hook #'joe/lsp-mode-hook) -(define-key global-map (kbd "C-c l l") #'lsp) - ;; (setq lsp-ui-doc-position 'at-point) ;; (setq lsp-ui-doc-show-with-mouse nil) @@ -2118,9 +2134,8 @@ These help speed eglot up apparently [[https://www.reddit.com/r/emacs/comments/1 #+end_src -Omnisharp is very slow to start, so I want to start it manually, but not if the -buffer already belongs to the current lsp workspace. This function checks if a -newly opened buffer belongs to an LSP session and if it does, start lsp +Some servers/projects start up really slow, so let's start LSP manually. This function allows buffers to attach only to +existing LSP sessions that were manually started. #+begin_src emacs-lisp (defun lsp-mode-server-exists-p+ (mode &optional all-workspaces) @@ -2139,6 +2154,26 @@ newly opened buffer belongs to an LSP session and if it does, start lsp ;; (setq treesit-load-name-override-list '((csharp "libtree-sitter-c-sharp.so"))) #+end_src +Likewise, because opening buffers doesn't start LSP, there could be a lot of already opened buffers before we decide to +start it, so go through all existing buffers that match the mode and belong to the project. + +#+begin_src emacs-lisp +(defun joe/lsp-activate-for-project-buffers () + (interactive) + (lsp) + (let ((root (lsp-workspace-root)) + (mode major-mode)) + (dolist (buf (buffer-list)) + (with-current-buffer buf + (when (and (eq major-mode mode) + (buffer-file-name) + (not lsp-mode) + (equal (lsp-workspace-root) root)) + (lsp)))))) + +(define-key global-map (kbd "C-c l l") #'joe/lsp-activate-for-project-buffers) +#+end_src + #+end_src *** Flycheck @@ -2245,6 +2280,8 @@ newly opened buffer belongs to an LSP session and if it does, start lsp (evil-define-key 'visual joe/evil-space-mode-map (kbd "SPC a r") #'gptel-rewrite) (evil-define-key 'visual joe/evil-space-mode-map (kbd "SPC a RET") #'gptel-ask) (evil-define-key 'visual joe/evil-space-mode-map (kbd "SPC a c") #'gptel-add) +(setq gptel-model 'claude-sonnet-4-6) +(setq gptel-backend (gptel-get-backend "Claude")) #+end_src gptel-context-remove-all without confirmation @@ -2648,8 +2685,7 @@ Org mode buffers have associated files. (org-fancy-priorities-mode) (visual-line-mode) (org-bullets-mode) - (org-indent-mode) - (olivetti-mode)) + (org-indent-mode)) (add-hook 'org-mode-hook 'joe/org-hook) #+end_src