From 268d7c57b8dbe990e0fce4976cb25b180cf3c2f0 Mon Sep 17 00:00:00 2001 From: Akib Azmain Turja Date: Sat, 16 Sep 2023 18:48:25 +0600 Subject: [PATCH] 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. --- eat.el | 35 +++++++++++++++++++++++------------ integration/bash | 4 ++-- integration/zsh | 11 +++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/eat.el b/eat.el index 8cb314d..1040e1b 100644 --- a/eat.el +++ b/eat.el @@ -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 ; 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." diff --git a/integration/bash b/integration/bash index 993f229..3118e12 100644 --- a/integration/bash +++ b/integration/bash @@ -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 } diff --git a/integration/zsh b/integration/zsh index 66ea0e6..2e30d58 100644 --- a/integration/zsh +++ b/integration/zsh @@ -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.