n-options 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copy this file into /usr/share/zsh/site-functions/
  2. # and add 'autoload n-options` to .zshrc
  3. #
  4. # This function allows to browse and toggle shell's options
  5. #
  6. # Uses n-list
  7. #emulate -L zsh
  8. zmodload zsh/curses
  9. local IFS="
  10. "
  11. unset NLIST_COLORING_PATTERN
  12. [ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
  13. [ -f ~/.config/znt/n-options.conf ] && builtin source ~/.config/znt/n-options.conf
  14. # TODO restore options
  15. unsetopt localoptions
  16. integer kshoptionprint=0
  17. [[ -o kshoptionprint ]] && kshoptionprint=1
  18. setopt kshoptionprint
  19. local list
  20. local selected
  21. local option
  22. local state
  23. # 0 - don't remember, 1 - remember, 2 - init once, then remember
  24. NLIST_REMEMBER_STATE=2
  25. local NLIST_GREP_STRING="${1:=}"
  26. while (( 1 )); do
  27. list=( `setopt` )
  28. list=( "${(M)list[@]:#*${1:=}*}" )
  29. list=( "${list[@]:#kshoptionprint*}" )
  30. if [ "$#list" -eq 0 ]; then
  31. echo "No matching options"
  32. break
  33. fi
  34. local red=$'\x1b[00;31m' green=$'\x1b[00;32m' reset=$'\x1b[00;00m'
  35. list=( "${list[@]/ off/${red} off$reset}" )
  36. #list=( "${list[@]/ on/${green} on$reset}" )
  37. list=( "${(i)list[@]}" )
  38. n-list "${list[@]}"
  39. if [ "$REPLY" -gt 0 ]; then
  40. [[ -o ksharrays ]] && selected="${reply[$(( REPLY - 1 ))]}" || selected="${reply[$REPLY]}"
  41. option="${selected%% *}"
  42. state="${selected##* }"
  43. if [[ -o globsubst ]]; then
  44. unsetopt globsubst
  45. state="${state%$reset}"
  46. setopt globsubst
  47. else
  48. state="${state%$reset}"
  49. fi
  50. # Toggle the option
  51. if [ "$state" = "on" ]; then
  52. echo "Setting |$option| to off"
  53. unsetopt "$option"
  54. else
  55. echo "Setting |$option| to on"
  56. setopt "$option"
  57. fi
  58. else
  59. break
  60. fi
  61. done
  62. NLIST_REMEMBER_STATE=0
  63. [[ "$kshoptionprint" -eq 0 ]] && unsetopt kshoptionprint
  64. # vim: set filetype=zsh: