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