2020-03-09 00:58:01 +01:00
|
|
|
(require 'chart)
|
|
|
|
(require 'dash)
|
2020-04-02 02:35:16 +02:00
|
|
|
(require 'map)
|
2020-03-09 00:58:01 +01:00
|
|
|
|
2023-08-29 14:43:13 +02:00
|
|
|
(require 'gio-list-apps) ;; native module!
|
|
|
|
|
2013-08-05 11:54:38 +02:00
|
|
|
(defun goto-line-with-feedback ()
|
|
|
|
"Show line numbers temporarily, while prompting for the line number input"
|
|
|
|
(interactive)
|
|
|
|
(unwind-protect
|
|
|
|
(progn
|
2018-06-01 17:07:46 +02:00
|
|
|
(setq-local display-line-numbers t)
|
|
|
|
(let ((target (read-number "Goto line: ")))
|
|
|
|
(avy-push-mark)
|
|
|
|
(goto-line target)))
|
|
|
|
(setq-local display-line-numbers nil)))
|
2013-08-05 11:54:38 +02:00
|
|
|
|
2013-07-08 01:15:05 +02:00
|
|
|
(defun esk-add-watchwords ()
|
|
|
|
(font-lock-add-keywords
|
2013-08-08 00:54:35 +02:00
|
|
|
nil '(("\\<\\(FIX\\(ME\\)?\\|TODO\\|DEBUG\\|HACK\\|REFACTOR\\|NOCOMMIT\\)"
|
2013-07-08 01:15:05 +02:00
|
|
|
1 font-lock-warning-face t))))
|
|
|
|
|
2023-12-02 20:42:47 +01:00
|
|
|
(add-hook 'prog-mode-hook 'esk-add-watchwords)
|
|
|
|
|
2013-07-08 01:15:05 +02:00
|
|
|
(defun esk-sudo-edit (&optional arg)
|
|
|
|
(interactive "p")
|
|
|
|
(if (or arg (not buffer-file-name))
|
2017-10-15 13:30:10 +02:00
|
|
|
(find-file (concat "/sudo:root@localhost:" (read-file-name "File: ")))
|
2013-07-08 01:15:05 +02:00
|
|
|
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
|
|
|
|
|
2017-11-12 19:00:49 +01:00
|
|
|
;; Get the nix store path for a given derivation.
|
|
|
|
;; If the derivation has not been built before, this will trigger a build.
|
|
|
|
(defun nix-store-path (derivation)
|
|
|
|
(let ((expr (concat "with import <nixos> {}; " derivation)))
|
|
|
|
(s-chomp (shell-command-to-string (concat "nix-build -E '" expr "'")))))
|
|
|
|
|
|
|
|
(defun insert-nix-store-path ()
|
|
|
|
(interactive)
|
|
|
|
(let ((derivation (read-string "Derivation name (in <nixos>): ")))
|
2017-11-12 22:41:54 +01:00
|
|
|
(insert (nix-store-path derivation))))
|
2017-11-12 19:00:49 +01:00
|
|
|
|
2017-11-22 23:55:21 +01:00
|
|
|
(defun toggle-force-newline ()
|
|
|
|
"Buffer-local toggle for enforcing final newline on save."
|
|
|
|
(interactive)
|
|
|
|
(setq-local require-final-newline (not require-final-newline))
|
|
|
|
(message "require-final-newline in buffer %s is now %s"
|
|
|
|
(buffer-name)
|
|
|
|
require-final-newline))
|
|
|
|
|
2018-05-01 16:02:53 +02:00
|
|
|
(defun list-external-commands ()
|
|
|
|
"Creates a list of all external commands available on $PATH
|
|
|
|
while filtering NixOS wrappers."
|
|
|
|
(cl-loop
|
|
|
|
for dir in (split-string (getenv "PATH") path-separator)
|
|
|
|
when (and (file-exists-p dir) (file-accessible-directory-p dir))
|
|
|
|
for lsdir = (cl-loop for i in (directory-files dir t)
|
|
|
|
for bn = (file-name-nondirectory i)
|
|
|
|
when (and (not (s-contains? "-wrapped" i))
|
|
|
|
(not (member bn completions))
|
|
|
|
(not (file-directory-p i))
|
|
|
|
(file-executable-p i))
|
|
|
|
collect bn)
|
|
|
|
append lsdir into completions
|
|
|
|
finally return (sort completions 'string-lessp)))
|
|
|
|
|
2019-12-14 14:23:20 +01:00
|
|
|
(defvar external-command-flag-overrides
|
|
|
|
'(("google-chrome" . "--force-device-scale-factor=1.4"))
|
|
|
|
|
|
|
|
"This setting lets me add additional flags to specific commands
|
2020-12-09 14:57:25 +01:00
|
|
|
that are run interactively via `run-external-command'.")
|
2019-12-14 14:23:20 +01:00
|
|
|
|
2020-12-09 14:57:25 +01:00
|
|
|
(defun run-external-command--handler (cmd)
|
2019-12-14 14:23:20 +01:00
|
|
|
"Execute the specified command and notify the user when it
|
2018-10-31 10:29:49 +01:00
|
|
|
finishes."
|
2019-12-14 14:23:20 +01:00
|
|
|
(let* ((extra-flags (cdr (assoc cmd external-command-flag-overrides)))
|
|
|
|
(cmd (if extra-flags (s-join " " (list cmd extra-flags)) cmd)))
|
|
|
|
(message "Starting %s..." cmd)
|
|
|
|
(set-process-sentinel
|
|
|
|
(start-process-shell-command cmd nil cmd)
|
|
|
|
(lambda (process event)
|
|
|
|
(when (string= event "finished\n")
|
|
|
|
(message "%s process finished." process))))))
|
2018-10-31 10:29:49 +01:00
|
|
|
|
2020-12-09 14:57:25 +01:00
|
|
|
(defun run-external-command ()
|
2018-05-01 16:02:53 +02:00
|
|
|
"Prompts the user with a list of all installed applications and
|
|
|
|
lets them select one to launch."
|
|
|
|
|
|
|
|
(interactive)
|
|
|
|
(let ((external-commands-list (list-external-commands)))
|
2020-12-09 14:57:25 +01:00
|
|
|
(run-external-command--handler
|
|
|
|
(completing-read "Command: " external-commands-list
|
|
|
|
nil ;; predicate
|
|
|
|
t ;; require-match
|
|
|
|
nil ;; initial-input
|
|
|
|
;; hist
|
|
|
|
'external-commands-history))))
|
|
|
|
|
|
|
|
(defun password-store-lookup (&optional password-store-dir)
|
|
|
|
"Interactive password-store lookup function that actually uses
|
|
|
|
the GPG agent correctly."
|
2018-06-06 09:46:01 +02:00
|
|
|
|
|
|
|
(interactive)
|
2020-12-09 14:57:25 +01:00
|
|
|
|
|
|
|
(let* ((entry (completing-read "Copy password of entry: "
|
|
|
|
(password-store-list (or password-store-dir
|
|
|
|
(password-store-dir)))
|
|
|
|
nil ;; predicate
|
|
|
|
t ;; require-match
|
|
|
|
))
|
2023-08-23 22:18:05 +02:00
|
|
|
(password (or (let ((epa-suppress-error-buffer t))
|
|
|
|
(auth-source-pass-get 'secret entry))
|
|
|
|
(error "failed to decrypt '%s', wrong password?" entry))))
|
2020-12-09 14:57:25 +01:00
|
|
|
(password-store-clear)
|
|
|
|
(kill-new password)
|
|
|
|
(setq password-store-kill-ring-pointer kill-ring-yank-pointer)
|
|
|
|
(message "Copied %s to the kill ring. Will clear in %s seconds."
|
|
|
|
entry (password-store-timeout))
|
|
|
|
(setq password-store-timeout-timer
|
|
|
|
(run-at-time (password-store-timeout)
|
|
|
|
nil 'password-store-clear))))
|
|
|
|
|
2019-12-15 18:09:52 +01:00
|
|
|
(defhydra mc/mark-more-hydra (:color pink)
|
2022-02-26 11:49:35 +01:00
|
|
|
("<up>" mc/mmlte--up "Mark previous like this")
|
2019-12-15 18:09:52 +01:00
|
|
|
("<down>" mc/mmlte--down "Mark next like this")
|
|
|
|
("<left>" mc/mmlte--left (if (eq mc/mark-more-like-this-extended-direction 'up)
|
|
|
|
"Skip past the cursor furthest up"
|
|
|
|
"Remove the cursor furthest down"))
|
|
|
|
("<right>" mc/mmlte--right (if (eq mc/mark-more-like-this-extended-direction 'up)
|
|
|
|
"Remove the cursor furthest up"
|
|
|
|
"Skip past the cursor furthest down"))
|
|
|
|
("f" nil "Finish selecting"))
|
|
|
|
|
|
|
|
;; Mute the message that mc/mmlte wants to print on its own
|
|
|
|
(advice-add 'mc/mmlte--message :around (lambda (&rest args) (ignore)))
|
|
|
|
|
|
|
|
(defun mc/mark-dwim (arg)
|
|
|
|
"Select multiple things, but do what I mean."
|
|
|
|
|
|
|
|
(interactive "p")
|
|
|
|
(if (not (region-active-p)) (mc/mark-next-lines arg)
|
|
|
|
(if (< 1 (count-lines (region-beginning)
|
|
|
|
(region-end)))
|
|
|
|
(mc/edit-lines arg)
|
|
|
|
;; The following is almost identical to `mc/mark-more-like-this-extended',
|
|
|
|
;; but uses a hydra (`mc/mark-more-hydra') instead of a transient key map.
|
|
|
|
(mc/mmlte--down)
|
|
|
|
(mc/mark-more-hydra/body))))
|
|
|
|
|
2022-02-26 11:49:35 +01:00
|
|
|
(setq mc/cmds-to-run-for-all '(kill-region paredit-newline))
|
|
|
|
|
|
|
|
(setq mc/cmds-to-run-once '(mc/mark-dwim
|
|
|
|
mc/mark-more-hydra/mc/mmlte--down
|
|
|
|
mc/mark-more-hydra/mc/mmlte--left
|
|
|
|
mc/mark-more-hydra/mc/mmlte--right
|
|
|
|
mc/mark-more-hydra/mc/mmlte--up
|
|
|
|
mc/mark-more-hydra/mmlte--up
|
|
|
|
mc/mark-more-hydra/nil))
|
|
|
|
|
2019-12-17 13:03:57 +01:00
|
|
|
(defun insert-todo-comment (prefix todo)
|
|
|
|
"Insert a comment at point with something for me to do."
|
|
|
|
|
|
|
|
(interactive "P\nsWhat needs doing? ")
|
|
|
|
(save-excursion
|
|
|
|
(move-end-of-line nil)
|
|
|
|
(insert (format " %s TODO(%s): %s"
|
2020-01-12 00:23:46 +01:00
|
|
|
(s-trim-right comment-start)
|
2019-12-17 13:03:57 +01:00
|
|
|
(if prefix (read-string "Who needs to do this? ")
|
|
|
|
(getenv "USER"))
|
|
|
|
todo))))
|
|
|
|
|
2019-12-17 18:30:52 +01:00
|
|
|
;; Custom text scale adjustment functions that operate on the entire instance
|
|
|
|
(defun modify-text-scale (factor)
|
|
|
|
(set-face-attribute 'default nil
|
|
|
|
:height (+ (* factor 5) (face-attribute 'default :height))))
|
|
|
|
|
|
|
|
(defun increase-default-text-scale (prefix)
|
|
|
|
"Increase default text scale in all Emacs frames, or just the
|
|
|
|
current frame if PREFIX is set."
|
|
|
|
|
|
|
|
(interactive "P")
|
|
|
|
(if prefix (text-scale-increase 1)
|
|
|
|
(modify-text-scale 1)))
|
|
|
|
|
|
|
|
(defun decrease-default-text-scale (prefix)
|
|
|
|
"Increase default text scale in all Emacs frames, or just the
|
|
|
|
current frame if PREFIX is set."
|
|
|
|
|
|
|
|
(interactive "P")
|
|
|
|
(if prefix (text-scale-decrease 1)
|
|
|
|
(modify-text-scale -1)))
|
|
|
|
|
|
|
|
(defun set-default-text-scale (prefix &optional to)
|
|
|
|
"Set the default text scale to the specified value, or the
|
|
|
|
default. Restores current frame's text scale only, if PREFIX is
|
|
|
|
set."
|
|
|
|
|
|
|
|
(interactive "P")
|
|
|
|
(if prefix (text-scale-adjust 0)
|
|
|
|
(set-face-attribute 'default nil :height (or to 120))))
|
|
|
|
|
2023-04-01 17:17:23 +02:00
|
|
|
(defun screenshot-select (filename)
|
2020-01-26 20:13:42 +01:00
|
|
|
"Take a screenshot based on a mouse-selection and save it to
|
|
|
|
~/screenshots."
|
2023-04-01 17:17:23 +02:00
|
|
|
(interactive "sScreenshot filename: ")
|
|
|
|
(let* ((path (f-join "~/screenshots"
|
|
|
|
(format "%s-%d.png"
|
|
|
|
(if (string-empty-p filename) "shot" filename)
|
|
|
|
(time-convert nil 'integer)))))
|
|
|
|
(shell-command (format "maim --select %s" path))
|
|
|
|
(message "Wrote screenshot to %s" path)))
|
2020-01-26 20:13:42 +01:00
|
|
|
|
2020-03-09 00:58:01 +01:00
|
|
|
(defun graph-unread-mails ()
|
|
|
|
"Create a bar chart of unread mails based on notmuch tags.
|
|
|
|
Certain tags are excluded from the overview."
|
|
|
|
|
|
|
|
(interactive)
|
|
|
|
(let ((tag-counts
|
|
|
|
(-keep (-lambda ((name . search))
|
|
|
|
(let ((count
|
|
|
|
(string-to-number
|
|
|
|
(s-trim
|
|
|
|
(notmuch-command-to-string "count" search "and" "tag:unread")))))
|
|
|
|
(when (>= count 1) (cons name count))))
|
2020-03-11 06:55:22 +01:00
|
|
|
(notmuch-hello-generate-tag-alist '("unread" "signed" "attachment" "important")))))
|
2020-03-09 00:58:01 +01:00
|
|
|
|
|
|
|
(chart-bar-quickie
|
|
|
|
(if (< (length tag-counts) 6)
|
|
|
|
'vertical 'horizontal)
|
|
|
|
"Unread emails"
|
|
|
|
(-map #'car tag-counts) "Tag:"
|
|
|
|
(-map #'cdr tag-counts) "Count:")))
|
|
|
|
|
2020-04-02 22:59:18 +02:00
|
|
|
(defun notmuch-show-open-or-close-subthread (&optional prefix)
|
2020-04-02 02:35:16 +02:00
|
|
|
"Open or close the subthread from (and including) the message at point."
|
2020-04-02 22:59:18 +02:00
|
|
|
(interactive "P")
|
2020-04-01 23:28:23 +02:00
|
|
|
(save-excursion
|
|
|
|
(let ((current-depth (map-elt (notmuch-show-get-message-properties) :depth 0)))
|
2020-04-02 22:59:18 +02:00
|
|
|
(loop do (notmuch-show-message-visible (notmuch-show-get-message-properties) prefix)
|
2020-04-01 23:28:23 +02:00
|
|
|
until (or (not (notmuch-show-goto-message-next))
|
2020-04-02 22:59:18 +02:00
|
|
|
(= (map-elt (notmuch-show-get-message-properties) :depth) current-depth)))))
|
2020-04-01 23:28:23 +02:00
|
|
|
(force-window-update))
|
|
|
|
|
2020-05-26 02:21:02 +02:00
|
|
|
(defun vterm-send-ctrl-x ()
|
|
|
|
"Sends `C-x' to the libvterm."
|
|
|
|
(interactive)
|
|
|
|
(vterm-send-key "x" nil nil t))
|
|
|
|
|
2020-08-23 23:39:42 +02:00
|
|
|
(defun find-depot-project (dir)
|
|
|
|
"Function used in the `project-find-functions' hook list to
|
|
|
|
determine the current project root of a depot project."
|
|
|
|
(when (s-starts-with? "/depot" dir)
|
|
|
|
(if (f-exists-p (f-join dir "default.nix"))
|
|
|
|
(cons 'transient dir)
|
|
|
|
(find-depot-project (f-parent dir)))))
|
|
|
|
|
|
|
|
(add-to-list 'project-find-functions #'find-depot-project)
|
|
|
|
|
2022-08-26 19:20:05 +02:00
|
|
|
(defun find-cargo-project (dir)
|
|
|
|
"Attempt to find the current project in `project-find-functions'
|
|
|
|
by looking for a `Cargo.toml' file."
|
2023-07-09 00:12:26 +02:00
|
|
|
(when dir
|
|
|
|
(unless (equal "/" dir)
|
|
|
|
(if (f-exists-p (f-join dir "Cargo.toml"))
|
|
|
|
(cons 'transient dir)
|
|
|
|
(find-cargo-project (f-parent dir))))))
|
2022-08-26 19:20:05 +02:00
|
|
|
|
|
|
|
(add-to-list 'project-find-functions #'find-cargo-project)
|
|
|
|
|
2020-08-24 01:42:50 +02:00
|
|
|
(defun magit-find-file-worktree ()
|
|
|
|
(interactive)
|
|
|
|
"Find a file in the current (ma)git worktree."
|
|
|
|
(magit-find-file--internal "{worktree}"
|
|
|
|
(magit-read-file-from-rev "HEAD" "Find file")
|
|
|
|
#'pop-to-buffer-same-window))
|
|
|
|
|
2023-08-04 18:31:45 +02:00
|
|
|
(defun zoxide-open-project ()
|
|
|
|
"Query Zoxide for paths, and open the result as appropriate (magit or dired)."
|
2021-12-20 09:49:23 +01:00
|
|
|
(interactive)
|
2023-08-04 18:31:45 +02:00
|
|
|
(zoxide-open-with
|
|
|
|
nil
|
|
|
|
(lambda (path)
|
|
|
|
(condition-case err (magit-status-setup-buffer path)
|
|
|
|
(magit-outside-git-repo (dired path))))))
|
2021-12-20 09:49:23 +01:00
|
|
|
|
2022-08-10 18:44:55 +02:00
|
|
|
(defun toggle-nix-test-and-exp ()
|
|
|
|
"Switch between the .nix and .exp file in a Tvix/Nix test."
|
|
|
|
(interactive)
|
|
|
|
(let* ((file (buffer-file-name))
|
|
|
|
(other (if (s-suffix? ".nix" file)
|
|
|
|
(s-replace-regexp ".nix$" ".exp" file)
|
|
|
|
(if (s-suffix? ".exp" file)
|
|
|
|
(s-replace-regexp ".exp$" ".nix" file)
|
|
|
|
(error "Not a .nix/.exp file!")))))
|
|
|
|
(find-file other)))
|
|
|
|
|
2023-08-23 22:03:29 +02:00
|
|
|
(defun reliably-switch-buffer ()
|
|
|
|
"Reliably and interactively switch buffers, without ending up in a
|
|
|
|
situation where the buffer was renamed during selection and an
|
|
|
|
empty new buffer is created.
|
|
|
|
|
2023-08-27 00:45:11 +02:00
|
|
|
This is done by, in contrast to most buffer-switching functions,
|
|
|
|
retaining a list of the buffer *objects* and their associated
|
|
|
|
names, instead of only their names (which might change)."
|
2023-08-23 22:03:29 +02:00
|
|
|
|
|
|
|
(interactive)
|
|
|
|
(let* ((buffers (seq-map (lambda (b) (cons (buffer-name b) b))
|
|
|
|
(seq-filter (lambda (b) (not (string-prefix-p " " (buffer-name b))))
|
|
|
|
(buffer-list))))
|
2023-08-27 00:45:11 +02:00
|
|
|
|
|
|
|
;; Annotate buffers that display remote files. I frequently
|
|
|
|
;; want to see it, because I might have identically named
|
|
|
|
;; files open locally and remotely at the same time, and it
|
|
|
|
;; helps with differentiating them.
|
|
|
|
(completion-extra-properties
|
|
|
|
'(:annotation-function
|
|
|
|
(lambda (name)
|
|
|
|
(if-let* ((file (buffer-file-name (cdr (assoc name buffers))))
|
|
|
|
(remote (file-remote-p file)))
|
|
|
|
(format " [%s]" remote)))))
|
|
|
|
|
2023-08-23 22:03:29 +02:00
|
|
|
(name (completing-read "Switch to buffer: " (seq-map #'car buffers)))
|
|
|
|
(selected (or (cdr (assoc name buffers))
|
|
|
|
;; Allow users to manually select invisible buffers ...
|
|
|
|
(get-buffer name))))
|
|
|
|
(switch-to-buffer (or selected name) nil 't)))
|
|
|
|
|
2023-08-27 01:22:20 +02:00
|
|
|
(defun run-xdg-app ()
|
|
|
|
"Use `//users/tazjin/gio-list-apps' to retrieve a list of
|
|
|
|
installed (and visible) XDG apps, and let users launch them."
|
|
|
|
(interactive)
|
2023-08-29 14:43:13 +02:00
|
|
|
(let* ((apps (taz-list-xdg-apps))
|
2023-08-27 01:22:20 +02:00
|
|
|
|
|
|
|
;; Display the command that will be run as an annotation
|
|
|
|
(completion-extra-properties
|
|
|
|
'(:annotation-function (lambda (app) (format " [%s]" (cdr (assoc app apps)))))))
|
|
|
|
|
|
|
|
(run-external-command--handler (cdr (assoc (completing-read "App: " apps nil t) apps)))))
|
|
|
|
|
2023-08-28 21:20:09 +02:00
|
|
|
(defun advice-remove-all (sym)
|
|
|
|
"Remove all advices from symbol SYM."
|
|
|
|
(interactive "aFunction symbol: ")
|
|
|
|
(advice-mapc (lambda (advice _props) (advice-remove sym advice)) sym))
|
|
|
|
|
2024-01-10 16:19:33 +01:00
|
|
|
(defun M-x-always-same-window ()
|
|
|
|
"Run `execute-extended-command', but ensure that whatever it does
|
|
|
|
always opens in the same window in which the command was invoked."
|
|
|
|
(interactive)
|
|
|
|
(let ((display-buffer-overriding-action
|
|
|
|
'((display-buffer-same-window) . ((inhibit-same-window . nil)))))
|
|
|
|
(call-interactively #'execute-extended-command)))
|
|
|
|
|
2014-10-21 19:37:19 +02:00
|
|
|
(provide 'functions)
|