zsh-navigation-tools.plugin.zsh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env zsh
  2. #
  3. # No plugin manager is needed to use this file. All that is needed is adding:
  4. # source {where-znt-is}/zsh-navigation-tools.plugin.zsh
  5. #
  6. # to ~/.zshrc.
  7. #
  8. # According to the standard:
  9. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  10. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  11. 0="${${(M)0:#/*}:-$PWD/$0}"
  12. export ZNT_REPO_DIR="${0:h}"
  13. export ZNT_CONFIG_DIR="$HOME/.config/znt"
  14. #
  15. # Update FPATH if:
  16. # 1. Not loading with a plugin manager
  17. # 2. Not having fpath already updated
  18. #
  19. if [[ ${zsh_loaded_plugins[-1]} != */zsh-navigation-tools && -z ${fpath[(r)${0:h}]} ]]
  20. then
  21. fpath+=( "${0:h}" )
  22. fi
  23. #
  24. # Copy configs
  25. #
  26. if [[ ! -d "$HOME/.config" ]]; then
  27. command mkdir "$HOME/.config"
  28. fi
  29. if [[ ! -d "$ZNT_CONFIG_DIR" ]]; then
  30. command mkdir "$ZNT_CONFIG_DIR"
  31. fi
  32. # 9 files
  33. unset __ZNT_CONFIG_FILES
  34. typeset -ga __ZNT_CONFIG_FILES
  35. set +A __ZNT_CONFIG_FILES n-aliases.conf n-env.conf n-history.conf n-list.conf n-panelize.conf n-cd.conf n-functions.conf n-kill.conf n-options.conf
  36. # Check for random 2 files if they exist
  37. # This will shift 0 - 7 elements
  38. shift $(( RANDOM % 8 )) __ZNT_CONFIG_FILES
  39. if [[ ! -f "$ZNT_CONFIG_DIR/${__ZNT_CONFIG_FILES[1]}" || ! -f "$ZNT_CONFIG_DIR/${__ZNT_CONFIG_FILES[2]}" ]]; then
  40. # Something changed - examine every file
  41. set +A __ZNT_CONFIG_FILES n-aliases.conf n-env.conf n-history.conf n-list.conf n-panelize.conf n-cd.conf n-functions.conf n-kill.conf n-options.conf
  42. unset __ZNT_CONFIG_FILE
  43. typeset -g __ZNT_CONFIG_FILE
  44. for __ZNT_CONFIG_FILE in "${__ZNT_CONFIG_FILES[@]}"; do
  45. if [[ ! -f "$ZNT_CONFIG_DIR/$__ZNT_CONFIG_FILE" ]]; then
  46. command cp "$ZNT_REPO_DIR/.config/znt/$__ZNT_CONFIG_FILE" "$ZNT_CONFIG_DIR"
  47. fi
  48. done
  49. unset __ZNT_CONFIG_FILE
  50. fi
  51. unset __ZNT_CONFIG_FILES
  52. #
  53. # Load functions
  54. #
  55. autoload n-aliases n-cd n-env n-functions n-history n-kill n-list n-list-draw n-list-input n-options n-panelize n-help
  56. autoload znt-usetty-wrapper znt-history-widget znt-cd-widget znt-kill-widget
  57. alias naliases=n-aliases ncd=n-cd nenv=n-env nfunctions=n-functions nhistory=n-history
  58. alias nkill=n-kill noptions=n-options npanelize=n-panelize nhelp=n-help
  59. zle -N znt-history-widget
  60. bindkey '^R' znt-history-widget
  61. setopt AUTO_PUSHD HIST_IGNORE_DUPS PUSHD_IGNORE_DUPS
  62. zstyle ':completion::complete:n-kill::bits' matcher 'r:|=** l:|=*'