Skip to content
Draft
132 changes: 109 additions & 23 deletions emacs/radian.el
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,18 @@ In either case, eagerly load FEATURE during byte-compilation."

(defmacro radian-flet (bindings &rest body)
"Temporarily override function definitions using `cl-letf*'.
BINDINGS are composed of `defun'-ish forms. NAME is the function
to override. It has access to the original function as a
lexically bound variable by the same name, for use with
BINDINGS are composed of `defun'-ish forms. NAME is the function to
override. It has access to the original function as a lexically bound
variable by the original name prefixed with `orig-', for use with
`funcall'. ARGLIST and BODY are as in `defun'.

In the case that NAME is already defined as a dynamically bound
variable, it cannot be bound lexically again, and attempting the binding
will cause strange things to happen in case of re-entrant calls. Since
it is not uncommon for a symbol to have both a function and variable
binding (this will happen for any mode function, for example), the
`orig-' prefixing helps to avoid conflicts.

\(fn ((defun NAME ARGLIST &rest BODY) ...) BODY...)"
(declare (indent defun))
`(cl-letf* (,@(cl-mapcan
Expand All @@ -129,7 +136,8 @@ lexically bound variable by the same name, for use with
(setq binding (cdr binding)))
(cl-destructuring-bind (name arglist &rest body) binding
(list
`(,name (symbol-function #',name))
`(,(intern (format "orig-%S" name))
(symbol-function #',name))
`((symbol-function #',name)
(lambda ,arglist
,@body)))))
Expand Down Expand Up @@ -260,7 +268,7 @@ This means that FILENAME is a symlink whose target is inside
"Execute BODY, with the function `load' made silent."
(declare (indent 0))
`(radian-flet ((defun load (file &optional noerror _nomessage &rest args)
(apply load file noerror 'nomessage args)))
(apply orig-load file noerror 'nomessage args)))
,@body))

(defmacro radian--with-silent-write (&rest body)
Expand All @@ -269,7 +277,7 @@ This means that FILENAME is a symlink whose target is inside
`(radian-flet ((defun write-region
(start end filename &optional append visit lockname
mustbenew)
(funcall write-region start end filename append 0
(funcall orig-write-region start end filename append 0
lockname mustbenew)
(when (or (stringp visit) (eq visit t))
(setq buffer-file-name
Expand Down Expand Up @@ -306,7 +314,7 @@ also be a single string."
(when (or (null regexp)
(string-match-p regexp str))
(cl-return-from done)))
(funcall message "%s" str)))))
(funcall orig-message "%s" str)))))
,@body))))

(defun radian--advice-silence-messages (func &rest args)
Expand Down Expand Up @@ -761,7 +769,7 @@ This keymap is bound under \\[radian-keymap].")
(dolist (arg args)
(when (equal arg ?\C-g)
(signal 'quit nil)))
(apply insert-and-inherit args)))
(apply orig-insert-and-inherit args)))
(apply quoted-insert args)))

;; Package `which-key' displays the key bindings and associated
Expand Down Expand Up @@ -1578,8 +1586,9 @@ password that the user has decided not to save.")
(if (member key blacklist)
?n
(radian-flet ((defun auth-source-read-char-choice (prompt choices)
(let ((choice (funcall auth-source-read-char-choice
prompt choices)))
(let ((choice (funcall
orig-auth-source-read-char-choice
prompt choices)))
(when (= choice ?N)
(push key blacklist)
(make-directory
Expand Down Expand Up @@ -1621,6 +1630,50 @@ permission."

(bind-key* "s-x" #'radian-set-executable-permission)

;;; Remote files

;; Feature `tramp' provides the facility for editing remote files from
;; within Emacs.
(use-feature tramp
:config

;; Some suggestions taken from
;; https://coredumped.dev/2025/06/18/making-tramp-go-brrrr./

;; Make more aggressive use of inline copying until getting to truly
;; large files.
(setq tramp-copy-size-limit (* 1024 1024))

;; Use "direct async processes", which are apparently faster.

(connection-local-set-profile-variables
'remote-direct-async-process
'((tramp-direct-async-process . t)))

(connection-local-set-profiles
'(:application tramp :protocol "scp")
'remote-direct-async-process)

(use-feature magit
:config

(setq magit-tramp-pipe-stty-settings 'pty))

(defalias 'radian--advice-tramp-locking-inhibit #'ignore
"Inhibit file locking for TRAMP.
We already disable `create-lockfiles' globally, but `lock-file' and
`unlock-file' (invoked directly by the edit loop) also check for file
modifications, to warn the user if the file was changed since it was
loaded. We want to inhibit that for remote files, because otherwise it
causes an arbitrarily long synchronous hang before your keystrokes show
up.")

(advice-add #'tramp-handle-lock-file :override
#'radian--advice-tramp-locking-inhibit)

(advice-add #'tramp-handle-unlock-file :override
#'radian--advice-tramp-locking-inhibit))

;;; Editing
;;;; Text formatting

Expand Down Expand Up @@ -2041,7 +2094,7 @@ multiple files will miss any match that occurs earlier in a
visited file than point happens to be currently in that
buffer."
(radian-flet ((defun perform-replace (&rest args)
(apply perform-replace
(apply orig-perform-replace
(append args (list (point-min) (point-max))))))
(apply func args)))))

Expand Down Expand Up @@ -2208,21 +2261,23 @@ buffer."
;; inserting a pair, add an extra newline and indent. See
;; <https://github.com/Fuco1/smartparens/issues/80#issuecomment-18910312>.

(defun radian--smartparens-pair-setup (mode delim)
"In major mode MODE, set up DELIM with newline-and-indent."
(sp-local-pair mode delim nil :post-handlers
(defun radian--smartparens-pair-setup (mode open &optional close)
"In major mode MODE, set up delimiter with newline-and-indent.
OPEN is the opening delimiter, CLOSE is the closing delimiter which
defaults to OPEN."
(sp-local-pair mode open close :post-handlers
'((radian--smartparens-indent-new-pair "RET")
(radian--smartparens-indent-new-pair "<return>"))))

(dolist (delim '("(" "[" "{"))
(dolist (pair '(("(" ")") ("[" "]") ("{" "}")))
(dolist (mode '(
fundamental-mode
javascript-mode
protobuf-mode
prog-mode
text-mode
))
(radian--smartparens-pair-setup mode delim)))
(apply #'radian--smartparens-pair-setup mode pair)))

(radian--smartparens-pair-setup #'python-mode "\"\"\"")
(radian--smartparens-pair-setup #'markdown-mode "```")
Expand Down Expand Up @@ -2721,6 +2776,17 @@ menu to disappear and then come back after `company-idle-delay'."

:blackout t)

;; Feature `company-etags' is a built-in completion backend that reads
;; TAGS files. We don't use it.
(use-feature company-etags
:config

(radian-defadvice radian--advice-company-etags-tramp-disable (&rest _)
:before-until #'company-etags
"Disable Company etags in remote buffers.
It hangs the editor because it wants to make remote process calls."
(and buffer-file-name (file-remote-p buffer-file-name))))

;; Package `company-prescient' provides intelligent sorting and
;; filtering for candidates in Company completions.
(radian-use-package company-prescient
Expand Down Expand Up @@ -2803,11 +2869,11 @@ order."
"Prevent `eldoc' from trampling on existing messages."
(radian-flet ((defun eldoc-message (&optional string)
(if string
(funcall eldoc-message string)
(funcall orig-eldoc-message string)
(setq eldoc-last-message nil)))
(defun eldoc--message (&optional string)
(if string
(funcall eldoc--message string)
(funcall orig-eldoc--message string)
(setq eldoc-last-message nil))))
(apply func args))))

Expand Down Expand Up @@ -2879,7 +2945,7 @@ was printed, and only have ElDoc display if one wasn't."
(radian-flet ((defun completing-read (prompt collection &rest args)
(if (= (safe-length collection) 1)
(car collection)
(apply completing-read prompt collection args))))
(apply orig-completing-read prompt collection args))))
(apply orig-fun args)))

(use-feature lsp-mode
Expand All @@ -2905,7 +2971,7 @@ was printed, and only have ElDoc display if one wasn't."
(regexp rep string &rest args)
(if (equal regexp "`\\([\n]+\\)")
string
(apply replace-regexp-in-string
(apply orig-replace-regexp-in-string
regexp rep string args))))
(apply func args))))

Expand Down Expand Up @@ -3357,7 +3423,19 @@ Return either a string or nil."
(goto-char (point-min))
(let ((venv (string-trim (buffer-string))))
(when (file-directory-p venv)
(cl-return venv)))))))))))
(cl-return venv))))))))))

(radian-defadvice radian--advice-python-eldoc-tramp-disable (&rest _)
:before-until #'python-eldoc-function
"Disable Python ElDoc in remote buffers.
It hangs the editor because it wants to make remote process calls."
(and buffer-file-name (file-remote-p buffer-file-name)))

(radian-defadvice radian--advice-python-capf-tramp-disable ()
:before-until #'python-completion-at-point
"Disable Python completion-at-point in remote buffers.
It hangs the editor because it wants to make remote process calls."
(and buffer-file-name (file-remote-p buffer-file-name))))

;; Package `lsp-pyright' downloads Microsoft's LSP server for Python.
;; We hate Microsoft and think they are going to try to kill off
Expand Down Expand Up @@ -3533,7 +3611,7 @@ Return either a string or nil."
noerror _nomessage
nosuffix must-suffix)
(funcall
load file noerror 'nomessage nosuffix must-suffix)))
orig-load file noerror 'nomessage nosuffix must-suffix)))
(funcall TeX-load-style-file file)))

(radian-defadvice radian--advice-inhibit-tex-removing-duplicates-message
Expand Down Expand Up @@ -4881,6 +4959,14 @@ anything significant at package load time) since it breaks CI."
;; Don't prompt when reverting hunk.
(setq git-gutter:ask-p nil)

(radian-defadvice radian--advice-git-gutter-no-remote (func &rest args)
:around #'git-gutter--turn-on
"Inhibit `git-gutter' in TRAMP buffers to improve performance."
(radian-flet ((defun git-gutter-mode (&rest args)
(unless (file-remote-p buffer-file-name)
(apply orig-git-gutter-mode args))))
(apply func args)))

(global-git-gutter-mode +1)

(defun radian-git-gutter:beginning-of-hunk ()
Expand Down Expand Up @@ -4995,7 +5081,7 @@ changes, which means that `git-gutter' needs to be re-run.")
Instead, display simply a flat colored region in the fringe."
(radian-flet ((defun fringe-helper-insert-region
(beg end _bitmap &rest args)
(apply fringe-helper-insert-region
(apply orig-fringe-helper-insert-region
beg end 'radian--git-gutter-blank args)))
(apply func args)))))

Expand Down