summary refs log tree commit diff
path: root/Functions/Chpwd/chpwd_recent_filehandler
blob: 688612be74699c585d3ed25383f21737a0636f32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# With arguments, output those files to the recent directory file.
# With no arguments, read in the directories from the file into $reply.
#
# Handles recent-dirs-file and recent-dirs-max styles.

emulate -L zsh
setopt extendedglob

integer max
local file line
local -a files dir
local default=${ZDOTDIR:-$HOME}/.chpwd-recent-dirs

if zstyle -a ':chpwd:' recent-dirs-file files; then
  files=(${files//(#s)+(#e)/$default})
fi
if (( ${#files} == 0 )); then
  files=($default)
fi

zstyle -s ':chpwd:' recent-dirs-max max || max=20

if (( $# )); then
  if (( max > 0 && ${#argv} > max )); then
    argv=(${argv[1,max]})
  fi
  # Quote on write.
  # Use $'...' quoting... this fixes newlines and other nastiness.
  print -rl ${(qqqq)argv} >${files[1]}
else
  typeset -g reply
  # Unquote on read.
  reply=()
  for file in $files; do
    [[ -r $file ]] || continue
    # Strip anything after the directory from the line.
    # At the moment there isn't anything, but we'll make this
    # future proof.
    for line in ${(f)"$(<$file)"}; do
      dir=(${(z)line})
      reply+=(${(Q)${dir[1]}})
      if (( max > 0 && ${#reply} == max )); then
	break 2
      fi
    done
  done
fi