my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
17 KiB

3 years ago
  1. # $1, $2, ... - elements of the list
  2. # $NLIST_NONSELECTABLE_ELEMENTS - array of indexes (1-based) that cannot be selected
  3. # $REPLY is the output variable - contains index (1-based) or -1 when no selection
  4. # $reply (array) is the second part of the output - use the index (REPLY) to get selected element
  5. #
  6. # Copy this file into /usr/share/zsh/site-functions/
  7. # and add 'autoload n-list` to .zshrc
  8. #
  9. # This function outputs a list of elements that can be
  10. # navigated with keyboard. Uses curses library
  11. emulate -LR zsh
  12. setopt typesetsilent extendedglob noshortloops
  13. _nlist_has_terminfo=0
  14. zmodload zsh/curses
  15. zmodload zsh/terminfo 2>/dev/null && _nlist_has_terminfo=1
  16. trap "REPLY=-2; reply=(); return" TERM INT QUIT
  17. trap "_nlist_exit" EXIT
  18. # Drawing and input
  19. autoload n-list-draw n-list-input
  20. # Cleanup before any exit
  21. _nlist_exit() {
  22. setopt localoptions
  23. setopt extendedglob
  24. [[ "$REPLY" = -(#c0,1)[0-9]## || "$REPLY" = F<-> || "$REPLY" = "EDIT" || "$REPLY" = "HELP" ]] || REPLY="-1"
  25. zcurses 2>/dev/null delwin inner
  26. zcurses 2>/dev/null delwin main
  27. zcurses 2>/dev/null refresh
  28. zcurses end
  29. _nlist_alternate_screen 0
  30. _nlist_cursor_visibility 1
  31. unset _nlist_has_terminfo
  32. }
  33. # Outputs a message in the bottom of the screen
  34. _nlist_status_msg() {
  35. # -1 for border, -1 for 0-based indexing
  36. zcurses move main $(( term_height - 1 - 1 )) 2
  37. zcurses clear main eol
  38. zcurses string main "$1"
  39. #status_msg_strlen is localized in caller
  40. status_msg_strlen=$#1
  41. }
  42. # Prefer tput, then module terminfo
  43. _nlist_cursor_visibility() {
  44. if type tput 2>/dev/null 1>&2; then
  45. [ "$1" = "1" ] && { tput cvvis; tput cnorm }
  46. [ "$1" = "0" ] && tput civis
  47. elif [ "$_nlist_has_terminfo" = "1" ]; then
  48. [ "$1" = "1" ] && { [ -n $terminfo[cvvis] ] && echo -n $terminfo[cvvis];
  49. [ -n $terminfo[cnorm] ] && echo -n $terminfo[cnorm] }
  50. [ "$1" = "0" ] && [ -n $terminfo[civis] ] && echo -n $terminfo[civis]
  51. fi
  52. }
  53. # Reason for this function is that on some systems
  54. # smcup and rmcup are not knowing why left empty
  55. _nlist_alternate_screen() {
  56. [ "$_nlist_has_terminfo" -ne "1" ] && return
  57. [[ "$1" = "1" && -n "$terminfo[smcup]" ]] && return
  58. [[ "$1" = "0" && -n "$terminfo[rmcup]" ]] && return
  59. case "$TERM" in
  60. *rxvt*)
  61. [ "$1" = "1" ] && echo -n $'\x1b7\x1b[?47h'
  62. [ "$1" = "0" ] && echo -n $'\x1b[2J\x1b[?47l\x1b8'
  63. ;;
  64. *)
  65. [ "$1" = "1" ] && echo -n $'\x1b[?1049h'
  66. [ "$1" = "0" ] && echo -n $'\x1b[?1049l'
  67. # just to remember two other that work: $'\x1b7\x1b[r\x1b[?47h', $'\x1b[?47l\x1b8'
  68. ;;
  69. esac
  70. }
  71. _nlist_compute_user_vars_difference() {
  72. if [[ "${(t)NLIST_NONSELECTABLE_ELEMENTS}" != "array" &&
  73. "${(t)NLIST_NONSELECTABLE_ELEMENTS}" != "array-local" ]]
  74. then
  75. last_element_difference=0
  76. current_difference=0
  77. else
  78. last_element_difference=$#NLIST_NONSELECTABLE_ELEMENTS
  79. current_difference=0
  80. local idx
  81. for idx in "${(n)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  82. [ "$idx" -le "$NLIST_CURRENT_IDX" ] && current_difference+=1 || break
  83. done
  84. fi
  85. }
  86. # List was processed, check if variables aren't off range
  87. _nlist_verify_vars() {
  88. [ "$NLIST_CURRENT_IDX" -gt "$last_element" ] && NLIST_CURRENT_IDX="$last_element"
  89. [[ "$NLIST_CURRENT_IDX" -eq 0 && "$last_element" -ne 0 ]] && NLIST_CURRENT_IDX=1
  90. (( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=0+((NLIST_CURRENT_IDX-1)/page_height)*page_height+1 ))
  91. }
  92. # Compute the variables which are shown to the user
  93. _nlist_setup_user_vars() {
  94. if [ "$1" = "1" ]; then
  95. # Basic values when there are no non-selectables
  96. NLIST_USER_CURRENT_IDX="$NLIST_CURRENT_IDX"
  97. NLIST_USER_LAST_ELEMENT="$last_element"
  98. else
  99. _nlist_compute_user_vars_difference
  100. NLIST_USER_CURRENT_IDX=$(( NLIST_CURRENT_IDX - current_difference ))
  101. NLIST_USER_LAST_ELEMENT=$(( last_element - last_element_difference ))
  102. fi
  103. }
  104. _nlist_colorify_disp_list() {
  105. local col=$'\x1b[00;34m' reset=$'\x1b[0m'
  106. [ -n "$NLIST_COLORING_COLOR" ] && col="$NLIST_COLORING_COLOR"
  107. [ -n "$NLIST_COLORING_END_COLOR" ] && reset="$NLIST_COLORING_END_COLOR"
  108. if [ "$NLIST_COLORING_MATCH_MULTIPLE" -eq 1 ]; then
  109. disp_list=( "${(@)disp_list//(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
  110. else
  111. disp_list=( "${(@)disp_list/(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
  112. fi
  113. }
  114. #
  115. # Main code
  116. #
  117. # Check if there is proper input
  118. if [ "$#" -lt 1 ]; then
  119. echo "Usage: n-list element_1 ..."
  120. return 1
  121. fi
  122. REPLY="-1"
  123. typeset -ga reply
  124. reply=()
  125. integer term_height="$LINES"
  126. integer term_width="$COLUMNS"
  127. if [[ "$term_height" -lt 1 || "$term_width" -lt 1 ]]; then
  128. local stty_out=$( stty size )
  129. term_height="${stty_out% *}"
  130. term_width="${stty_out#* }"
  131. fi
  132. integer inner_height=term_height-3
  133. integer inner_width=term_width-3
  134. integer page_height=inner_height
  135. integer page_width=inner_width
  136. typeset -a list disp_list
  137. integer last_element=$#
  138. local action
  139. local final_key
  140. integer selection
  141. integer last_element_difference=0
  142. integer current_difference=0
  143. local prev_search_buffer=""
  144. integer prev_uniq_mode=0
  145. integer prev_start_idx=-1
  146. local MBEGIN MEND MATCH mbegin mend match
  147. # Iteration over predefined keywords
  148. integer curkeyword nkeywords
  149. local keywordisfresh="0"
  150. if [[ "${(t)keywords}" != *array* ]]; then
  151. local -a keywords
  152. keywords=()
  153. fi
  154. curkeyword=0
  155. nkeywords=${#keywords}
  156. # Iteration over themes
  157. integer curtheme nthemes
  158. local themeisfresh="0"
  159. if [[ "${(t)themes}" != *array* ]]; then
  160. local -a themes
  161. themes=()
  162. fi
  163. curtheme=0
  164. nthemes=${#themes}
  165. # Ability to remember the list between calls
  166. if [[ -z "$NLIST_REMEMBER_STATE" || "$NLIST_REMEMBER_STATE" -eq 0 || "$NLIST_REMEMBER_STATE" -eq 2 ]]; then
  167. NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=1
  168. NLIST_CURRENT_IDX=1
  169. NLIST_IS_SEARCH_MODE=0
  170. NLIST_SEARCH_BUFFER=""
  171. NLIST_TEXT_OFFSET=0
  172. NLIST_IS_UNIQ_MODE=0
  173. NLIST_IS_F_MODE=0
  174. # Zero - because it isn't known, unless we
  175. # confirm that first element is selectable
  176. NLIST_USER_CURRENT_IDX=0
  177. [[ ${NLIST_NONSELECTABLE_ELEMENTS[(r)1]} != 1 ]] && NLIST_USER_CURRENT_IDX=1
  178. NLIST_USER_LAST_ELEMENT=$(( last_element - $#NLIST_NONSELECTABLE_ELEMENTS ))
  179. # 2 is init once, then remember
  180. [ "$NLIST_REMEMBER_STATE" -eq 2 ] && NLIST_REMEMBER_STATE=1
  181. fi
  182. if [ "$NLIST_START_IN_SEARCH_MODE" -eq 1 ]; then
  183. NLIST_START_IN_SEARCH_MODE=0
  184. NLIST_IS_SEARCH_MODE=1
  185. fi
  186. if [ -n "$NLIST_SET_SEARCH_TO" ]; then
  187. NLIST_SEARCH_BUFFER="$NLIST_SET_SEARCH_TO"
  188. NLIST_SET_SEARCH_TO=""
  189. fi
  190. if [ "$NLIST_START_IN_UNIQ_MODE" -eq 1 ]; then
  191. NLIST_START_IN_UNIQ_MODE=0
  192. NLIST_IS_UNIQ_MODE=1
  193. fi
  194. _nlist_alternate_screen 1
  195. zcurses init
  196. zcurses delwin main 2>/dev/null
  197. zcurses delwin inner 2>/dev/null
  198. zcurses addwin main "$term_height" "$term_width" 0 0
  199. zcurses addwin inner "$inner_height" "$inner_width" 1 2
  200. # From n-list.conf
  201. [ "$colorpair" = "" ] && colorpair="white/black"
  202. [ "$border" = "0" ] || border="1"
  203. local background="${colorpair#*/}"
  204. local backuptheme="$colorpair/$bold"
  205. zcurses bg main "$colorpair"
  206. zcurses bg inner "$colorpair"
  207. if [ "$NLIST_IS_SEARCH_MODE" -ne 1 ]; then
  208. _nlist_cursor_visibility 0
  209. fi
  210. zcurses refresh
  211. #
  212. # Listening for input
  213. #
  214. local key keypad
  215. # Clear input buffer
  216. zcurses timeout main 0
  217. zcurses input main key keypad
  218. zcurses timeout main -1
  219. key=""
  220. keypad=""
  221. # This loop makes script faster on some Zsh's (e.g. 5.0.8)
  222. repeat 1; do
  223. list=( "$@" )
  224. done
  225. last_element="$#list"
  226. zcurses clear main redraw
  227. zcurses clear inner redraw
  228. while (( 1 )); do
  229. # Do searching (filtering with string)
  230. if [ -n "$NLIST_SEARCH_BUFFER" ]; then
  231. # Compute new list?
  232. if [[ "$NLIST_SEARCH_BUFFER" != "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode"
  233. || "$NLIST_IS_F_MODE" -ne "$prev_f_mode" ]]
  234. then
  235. prev_search_buffer="$NLIST_SEARCH_BUFFER"
  236. prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
  237. prev_f_mode="$NLIST_IS_F_MODE"
  238. # regenerating list -> regenerating disp_list
  239. prev_start_idx=-1
  240. # Take all elements, including duplicates and non-selectables
  241. typeset +U list
  242. repeat 1; do
  243. list=( "$@" )
  244. done
  245. # Remove non-selectable elements
  246. [ "$#NLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] && for i in "${(nO)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  247. if [[ "$i" = <-> ]]; then
  248. list[$i]=()
  249. fi
  250. done
  251. # Remove duplicates
  252. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
  253. last_element="$#list"
  254. # Next do the filtering
  255. local search_buffer="${NLIST_SEARCH_BUFFER%% ##}"
  256. search_buffer="${search_buffer## ##}"
  257. search_buffer="${search_buffer//(#m)[][*?|#~^()><\\]/\\$MATCH}"
  258. local search_pattern=""
  259. local colsearch_pattern=""
  260. if [ -n "$search_buffer" ]; then
  261. # The repeat will make the matching work on a fresh heap
  262. repeat 1; do
  263. if [ "$NLIST_IS_F_MODE" -eq "1" ]; then
  264. search_pattern="${search_buffer// ##/*~^(#a1)*}"
  265. colsearch_pattern="${search_buffer// ##/|(#a1)}"
  266. list=( "${(@M)list:#(#ia1)*$~search_pattern*}" )
  267. elif [ "$NLIST_IS_F_MODE" -eq "2" ]; then
  268. search_pattern="${search_buffer// ##/*~^(#a2)*}"
  269. colsearch_pattern="${search_buffer// ##/|(#a2)}"
  270. list=( "${(@M)list:#(#ia2)*$~search_pattern*}" )
  271. else
  272. # Pattern will be *foo*~^*bar* (inventor: Mikael Magnusson)
  273. search_pattern="${search_buffer// ##/*~^*}"
  274. # Pattern will be (foo|bar)
  275. colsearch_pattern="${search_buffer// ##/|}"
  276. list=( "${(@M)list:#(#i)*$~search_pattern*}" )
  277. fi
  278. done
  279. last_element="$#list"
  280. fi
  281. # Called after processing list
  282. _nlist_verify_vars
  283. fi
  284. _nlist_setup_user_vars 1
  285. integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
  286. [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
  287. if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  288. prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  289. disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  290. if [ -n "$colsearch_pattern" ]; then
  291. local red=$'\x1b[00;31m' reset=$'\x1b[00;00m'
  292. # The repeat will make the matching work on a fresh heap
  293. repeat 1; do
  294. if [ "$NLIST_IS_F_MODE" -eq "1" ]; then
  295. disp_list=( "${(@)disp_list//(#mia1)($~colsearch_pattern)/$red${MATCH}$reset}" )
  296. elif [ "$NLIST_IS_F_MODE" -eq "2" ]; then
  297. disp_list=( "${(@)disp_list//(#mia2)($~colsearch_pattern)/$red${MATCH}$reset}" )
  298. else
  299. disp_list=( "${(@)disp_list//(#mi)($~colsearch_pattern)/$red${MATCH}$reset}" )
  300. fi
  301. done
  302. fi
  303. # We have display list, lets replace newlines with "\n" when needed (1/2)
  304. [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  305. fi
  306. # Output colored list
  307. zcurses clear inner
  308. n-list-draw "$(( (NLIST_CURRENT_IDX-1) % page_height + 1 ))" \
  309. "$page_height" "$page_width" 0 0 "$NLIST_TEXT_OFFSET" inner \
  310. "$disp_list[@]"
  311. else
  312. # There is no search, but there was in previous loop
  313. # OR
  314. # Uniq mode was entered or left out
  315. # -> compute new list
  316. if [[ -n "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
  317. prev_search_buffer=""
  318. prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
  319. # regenerating list -> regenerating disp_list
  320. prev_start_idx=-1
  321. # Take all elements, including duplicates and non-selectables
  322. typeset +U list
  323. repeat 1; do
  324. list=( "$@" )
  325. done
  326. # Remove non-selectable elements only when in uniq mode
  327. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && [ "$#NLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] &&
  328. for i in "${(nO)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  329. if [[ "$i" = <-> ]]; then
  330. list[$i]=()
  331. fi
  332. done
  333. # Remove duplicates when in uniq mode
  334. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
  335. last_element="$#list"
  336. # Called after processing list
  337. _nlist_verify_vars
  338. fi
  339. # "1" - shouldn't bother with non-selectables
  340. _nlist_setup_user_vars "$NLIST_IS_UNIQ_MODE"
  341. integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
  342. [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
  343. if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  344. prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  345. disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  346. [ -n "$NLIST_COLORING_PATTERN" ] && _nlist_colorify_disp_list
  347. # We have display list, lets replace newlines with "\n" when needed (2/2)
  348. [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  349. fi
  350. # Output the list
  351. zcurses clear inner
  352. n-list-draw "$(( (NLIST_CURRENT_IDX-1) % page_height + 1 ))" \
  353. "$page_height" "$page_width" 0 0 "$NLIST_TEXT_OFFSET" inner \
  354. "$disp_list[@]"
  355. fi
  356. local status_msg_strlen
  357. local keywordmsg=""
  358. if [ "$keywordisfresh" = "1" ]; then
  359. keywordmsg="($curkeyword/$nkeywords) "
  360. keywordisfresh="0"
  361. fi
  362. local thememsg=""
  363. if [ "$themeisfresh" = "1" ]; then
  364. local theme="$backuptheme"
  365. [ "$curtheme" -gt 0 ] && theme="${themes[curtheme]}"
  366. thememsg="($curtheme/$nthemes $theme) "
  367. themeisfresh="0"
  368. fi
  369. local _txt2="" _txt3=""
  370. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
  371. [ "$NLIST_IS_F_MODE" -eq 1 ] && _txt3="[-FIX-] "
  372. [ "$NLIST_IS_F_MODE" -eq 2 ] && _txt3="[-FIX2-] "
  373. if [ "$NLIST_IS_SEARCH_MODE" = "1" ]; then
  374. _nlist_status_msg "${_txt2}${_txt3}${keywordmsg}${thememsg}Filtering with: ${NLIST_SEARCH_BUFFER// /+}"
  375. elif [[ ${NLIST_NONSELECTABLE_ELEMENTS[(r)$NLIST_CURRENT_IDX]} != $NLIST_CURRENT_IDX ||
  376. -n "$NLIST_SEARCH_BUFFER" || "$NLIST_IS_UNIQ_MODE" -eq 1 ]]; then
  377. local _txt=""
  378. [ -n "$NLIST_GREP_STRING" ] && _txt=" [$NLIST_GREP_STRING]"
  379. _nlist_status_msg "${_txt2}${_txt3}${keywordmsg}${thememsg}Current #$NLIST_USER_CURRENT_IDX (of #$NLIST_USER_LAST_ELEMENT entries)$_txt"
  380. else
  381. _nlist_status_msg "${keywordmsg}${thememsg}"
  382. fi
  383. [ "$border" = "1" ] && zcurses border main
  384. local top_msg=" ${(C)ZSH_NAME} $ZSH_VERSION, shell level $SHLVL "
  385. if [[ "${NLIST_ENABLED_EVENTS[(r)F1]}" = "F1" ]]; then
  386. top_msg=" F1-change view,$top_msg"
  387. fi
  388. zcurses move main 0 $(( term_width / 2 - $#top_msg / 2 ))
  389. zcurses string main $top_msg
  390. zcurses refresh main inner
  391. zcurses move main $(( term_height - 1 - 1 )) $(( status_msg_strlen + 2 ))
  392. # Wait for input
  393. zcurses input main key keypad
  394. # Get the special (i.e. "keypad") key or regular key
  395. if [ -n "$key" ]; then
  396. final_key="$key"
  397. elif [ -n "$keypad" ]; then
  398. final_key="$keypad"
  399. else
  400. _nlist_status_msg "Inproper input detected"
  401. zcurses refresh main inner
  402. fi
  403. n-list-input "$NLIST_CURRENT_IDX" "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" \
  404. "$page_height" "$page_width" "$last_element" "$NLIST_TEXT_OFFSET" \
  405. "$final_key" "$NLIST_IS_SEARCH_MODE" "$NLIST_SEARCH_BUFFER" \
  406. "$NLIST_IS_UNIQ_MODE" "$NLIST_IS_F_MODE"
  407. selection="$reply[1]"
  408. action="$reply[2]"
  409. NLIST_CURRENT_IDX="$reply[3]"
  410. NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN="$reply[4]"
  411. NLIST_TEXT_OFFSET="$reply[5]"
  412. NLIST_IS_SEARCH_MODE="$reply[6]"
  413. NLIST_SEARCH_BUFFER="$reply[7]"
  414. NLIST_IS_UNIQ_MODE="$reply[8]"
  415. NLIST_IS_F_MODE="$reply[9]"
  416. if [ -z "$action" ]; then
  417. continue
  418. elif [ "$action" = "SELECT" ]; then
  419. REPLY="$selection"
  420. reply=( "$list[@]" )
  421. break
  422. elif [ "$action" = "QUIT" ]; then
  423. REPLY=-1
  424. reply=( "$list[@]" )
  425. break
  426. elif [ "$action" = "REDRAW" ]; then
  427. zcurses clear main redraw
  428. zcurses clear inner redraw
  429. elif [[ "$action" = F<-> ]]; then
  430. REPLY="$action"
  431. reply=( "$list[@]" )
  432. break
  433. elif [[ "$action" = "EDIT" ]]; then
  434. REPLY="EDIT"
  435. reply=( "$list[@]" )
  436. break
  437. elif [[ "$action" = "HELP" ]]; then
  438. REPLY="HELP"
  439. reply=( "$list[@]" )
  440. break
  441. fi
  442. done
  443. # vim: set filetype=zsh: