Mercurial > dotfiles
comparison .shell.d/99.mcfly.zsh @ 485:e4d4cd0d120e
zsh: start using mcfly for ^R history search
This is a modified version of mcfly.zsh from upstream
422557e1ba48c33c5753262bd294bf3e92a40476. I then expect to install
mcfly using cargo.
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Tue, 30 Jun 2020 10:37:13 -0400 |
parents | |
children | 4b2472372fad |
comparison
equal
deleted
inserted
replaced
484:c85072e620a8 | 485:e4d4cd0d120e |
---|---|
1 #!/bin/zsh | |
2 | |
3 # Ensure stdin is a tty | |
4 [[ -t 0 ]] || return | |
5 | |
6 # Make sure mcfly is installed | |
7 if ! which mcfly > /dev/null ; then | |
8 echo nope | |
9 fi | |
10 | |
11 # Avoid loading this file more than once | |
12 if [[ "$__MCFLY_LOADED" == "loaded" ]]; then | |
13 return 0 | |
14 fi | |
15 __MCFLY_LOADED="loaded" | |
16 | |
17 emulate -L zsh | |
18 | |
19 # Ensure HISTFILE exists. | |
20 export HISTFILE="${HISTFILE:-$HOME/.zsh_history}" | |
21 if [[ ! -r "${HISTFILE}" ]]; then | |
22 echo "McFly: ${HISTFILE} does not exist or is not readable. Please fix this or set HISTFILE to something else before using McFly." | |
23 return 1 | |
24 fi | |
25 | |
26 # MCFLY_SESSION_ID is used by McFly internally to keep track of the commands from a particular terminal session. | |
27 export MCFLY_SESSION_ID=$(dd if=/dev/urandom bs=256 count=1 2> /dev/null | env LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 24) | |
28 | |
29 # Find the binary | |
30 MCFLY_PATH=${MCFLY_PATH:-$(which mcfly)} | |
31 | |
32 # Required for commented out mcfly search commands to work. | |
33 setopt interactive_comments # allow comments in interactive shells (like Bash does) | |
34 | |
35 # McFly's temporary, per-session history file. | |
36 if [[ ! -f "${MCFLY_HISTORY}" ]]; then | |
37 export MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX) | |
38 fi | |
39 | |
40 # Setup a function to be used by $PROMPT_COMMAND. | |
41 function mcfly_prompt_command { | |
42 local exit_code=$? # Record exit status of previous command. | |
43 | |
44 # Populate McFly's temporary, per-session history file from recent commands in the shell's primary HISTFILE. | |
45 if [[ ! -f "${MCFLY_HISTORY}" ]]; then | |
46 export MCFLY_HISTORY=$(mktemp -t mcfly.XXXXXXXX) | |
47 tail -n100 "${HISTFILE}" >| ${MCFLY_HISTORY} | |
48 fi | |
49 | |
50 # Write history to $MCFLY_HISTORY. | |
51 fc -W "${MCFLY_HISTORY}" | |
52 | |
53 # Run mcfly with the saved code. It fill find the text of the last command in $MCFLY_HISTORY and save it to the database. | |
54 [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Run mcfly add --exit ${exit_code}" | |
55 $MCFLY_PATH add --exit ${exit_code} | |
56 return ${exit_code} # Restore the original exit code by returning it. | |
57 } | |
58 precmd_functions+=(mcfly_prompt_command) | |
59 | |
60 # Cleanup $MCFLY_HISTORY tmp files on exit. | |
61 exit_logger() { | |
62 [ -n "$MCFLY_DEBUG" ] && echo "mcfly.zsh: Exiting and removing $MCFLY_HISTORY" | |
63 rm -f $MCFLY_HISTORY | |
64 } | |
65 zshexit_functions+=(exit_logger) | |
66 | |
67 # If this is an interactive shell, take ownership of ctrl-r. | |
68 if [[ $- =~ .*i.* ]]; then | |
69 mcfly-history-widget() { | |
70 () { | |
71 echoti rmkx | |
72 exec </dev/tty | |
73 local mcfly_output=$(mktemp -t mcfly.output.XXXXXXXX) | |
74 $MCFLY_PATH search -o "${mcfly_output}" "${LBUFFER}" | |
75 local mode=$(sed -n 1p $mcfly_output) | |
76 local selected=$(sed 1d $mcfly_output) | |
77 rm -f $mcfly_output | |
78 echoti smkx | |
79 if [[ -n $selected ]]; then | |
80 RBUFFER="" | |
81 LBUFFER="${selected}" | |
82 fi | |
83 if [[ "${mode}" == "run" ]]; then | |
84 zle accept-line | |
85 fi | |
86 zle redisplay | |
87 } | |
88 } | |
89 zle -N mcfly-history-widget | |
90 bindkey '^R' mcfly-history-widget | |
91 fi |