Add history reporting in Zsh integration script

* eat.el (eat--get-shell-history): New argument FORMAT, pass it
to 'eat--prompt-populate-input-ring'.
* eat.el (eat--handle-uic): Accept FORMAT argument in input
history reporting sequence and pass it to
'eat--get-shell-history'.
* eat.el (eat--prompt-populate-input-ring): New argument
FORMAT.  Support Zsh extended history file format.
* integration/bash (__eat_enable_integration): Send "bash" as
the history file format.
* integration/zsh (__eat_enable_integration): Send shell
history.
This commit is contained in:
Akib Azmain Turja 2023-09-16 18:48:25 +06:00
parent cbce601b8d
commit 268d7c57b8
No known key found for this signature in database
GPG key ID: 5535FCF54D88616B
3 changed files with 36 additions and 14 deletions

35
eat.el
View file

@ -5174,8 +5174,8 @@ BUFFER is the terminal buffer."
;; We'll update the mark later when the prompt appears.
(setq eat--shell-command-status code)))
(defun eat--get-shell-history (hist)
"Get shell history from HIST."
(defun eat--get-shell-history (hist format)
"Get shell history from HIST in format FORMAT."
(pcase hist
(`(,host . ,file)
(setq host (ignore-errors
@ -5194,7 +5194,7 @@ BUFFER is the terminal buffer."
(with-temp-buffer
(insert-file-contents file)
(setq str (buffer-string)))
(eat--prompt-populate-input-ring str))
(eat--prompt-populate-input-ring str format))
(eat-term-send-string
eat--terminal
(format "\e]51;e;I;%s\e\\"
@ -5203,7 +5203,8 @@ BUFFER is the terminal buffer."
(eat--prompt-populate-input-ring
(ignore-errors
(decode-coding-string (base64-decode-string hist)
locale-coding-system))))))
locale-coding-system))
format))))
(defun eat--handle-uic (_ cmd)
"Handle UI Command sequence CMD."
@ -5249,14 +5250,16 @@ BUFFER is the terminal buffer."
(eat--set-cmd-status (string-to-number status)))
;; UIC e ; I ; <n> ST.
((rx string-start "e;I;0;"
(let host (zero-or-more (not ?\;)))
(let format (zero-or-more (not ?\;)))
?\; (let host (zero-or-more (not ?\;)))
?\; (let path (zero-or-more anything))
string-end)
(eat--get-shell-history (cons host path)))
(eat--get-shell-history (cons host path) format))
((rx string-start "e;I;1;"
(let hist (zero-or-more anything))
(let format (zero-or-more (not ?\;)))
?\; (let hist (zero-or-more anything))
string-end)
(eat--get-shell-history hist))))
(eat--get-shell-history hist format))))
(defun eat-previous-shell-prompt (&optional arg)
"Go to the previous shell prompt.
@ -5851,11 +5854,19 @@ character."
(setq eat--prompt-stored-incomplete-input nil)
(setq eat--prompt-matching-input-from-input-string ""))
(defun eat--prompt-populate-input-ring (hist)
"Populate `eat--prompt-input-ring' from HIST."
(defun eat--prompt-populate-input-ring (hist format)
"Populate `eat--prompt-input-ring' from HIST in format FORMAT."
(setq eat--prompt-input-ring (make-ring eat-prompt-input-ring-size))
(dolist (item (string-split hist "\n" 'omit-nulls))
(ring-insert eat--prompt-input-ring item)))
(pcase format
("bash"
(dolist (item (string-split hist "\n" 'omit-nulls))
(ring-insert eat--prompt-input-ring item)))
("zsh"
(dolist (item (string-split hist "\n" 'omit-nulls))
(ring-insert eat--prompt-input-ring
(string-trim item (rx ": " (zero-or-more digit)
?: (zero-or-more digit)
?\;)))))))
(defun eat--prompt-ask-for-regexp-arg (prompt)
"Return list of regexp and prefix arg using PROMPT."

View file

@ -103,14 +103,14 @@ __eat_enable_integration ()
# Step 2.2: Assign the first element.
PROMPT_COMMAND[0]=__eat_before_prompt_command
# Send the history, for native shell prompt.
printf '\e]51;e;I;0;%s;%s\e\\' \
printf '\e]51;e;I;0;bash;%s;%s\e\\' \
"$(printf "%s" "$HOSTNAME" | base64)" \
"$(printf "%s" "$HISTFILE" | base64)"
local REPLY
IFS=$';\e' read -r -s -t 10 -d "\\" -a REPLY
if test "${REPLY[4]}" != 0
then
printf '\e]51;e;I;1;%s\e\\' \
printf '\e]51;e;I;1;bash;%s\e\\' \
"$(tail -n "${REPLY[4]}" "$HISTFILE" | base64)"
fi
}

View file

@ -65,6 +65,17 @@ __eat_enable_integration ()
autoload -Uz add-zsh-hook
add-zsh-hook precmd __eat_precmd
add-zsh-hook preexec __eat_preexec
# Send the history, for native shell prompt.
printf '\e]51;e;I;0;bash;%s;%s\e\\' \
"$(printf "%s" "$HOSTNAME" | base64)" \
"$(printf "%s" "$HISTFILE" | base64)"
local REPLY
IFS=$';\e' read -r -s -t 10 -d "\\" -A REPLY
if test "${REPLY[5]}" != 0
then
printf '\e]51;e;I;1;zsh;%s\e\\' \
"$(tail -n "${REPLY[5]}" "$HISTFILE" | base64)"
fi
}
# Enable.