From 5f7928a2581036ce5ef400ed376e05d05d3ac966 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sat, 6 Jan 2024 13:40:21 -0500 Subject: update --- emacs/.config/emacs-kj/lisp/kj-history.el | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 emacs/.config/emacs-kj/lisp/kj-history.el (limited to 'emacs/.config/emacs-kj/lisp/kj-history.el') diff --git a/emacs/.config/emacs-kj/lisp/kj-history.el b/emacs/.config/emacs-kj/lisp/kj-history.el new file mode 100644 index 0000000..4119c92 --- /dev/null +++ b/emacs/.config/emacs-kj/lisp/kj-history.el @@ -0,0 +1,47 @@ +(defvar kj/history-command "fc -R $HISTFILE && fc -l 1" + "History command used to get shell history.") + +(defvar kj/history-regexp + (rx bol (* " ") + ;; Index + (* digit) + (* " ") + ;; Command + (group (1+ (not "\n"))))) + +(defun kj/get-shell-history () + (with-temp-buffer + (unless (process-file + "zsh" + nil + (current-buffer) + nil + "-c" + kj/history-command) + (error "Getting history failed: %s" (buffer-string))) + (goto-char (point-min)) + (let ((result)) + (save-match-data + (while (re-search-forward kj/history-regexp nil t) + (push (match-string-no-properties 1) result))) + result))) + +(defun kj/consult-history () + (interactive) + (if (not + (or (and (minibufferp) + (eq minibuffer-history-variable 'shell-command-history)) + (derived-mode-p '(term-mode shell-mode)) + )) + (call-interactively 'consult-history) + (consult--read + (kj/get-shell-history) + :prompt "Shell history: " + :history t + :sort nil + :initial (if (minibufferp) + (buffer-substring-no-properties + (minibuffer-prompt-end) (point-max)) + nil)))) + +(provide 'kj-history) -- cgit v1.2.3