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.

382 lines
8.7 KiB

3 years ago
  1. typeset -grA __p9k_pb_cmd_skip=(
  2. '}' 'always' # handled specially
  3. '{' ''
  4. '{' ''
  5. '|' ''
  6. '||' ''
  7. '&' ''
  8. '&&' ''
  9. '|&' ''
  10. '&!' ''
  11. '&|' ''
  12. ')' ''
  13. '(' ''
  14. '()' ''
  15. '!' ''
  16. ';' ''
  17. 'if' ''
  18. 'fi' ''
  19. 'elif' ''
  20. 'else' ''
  21. 'then' ''
  22. 'while' ''
  23. 'until' ''
  24. 'do' ''
  25. 'done' ''
  26. 'esac' ''
  27. 'end' ''
  28. 'coproc' ''
  29. 'nocorrect' ''
  30. 'noglob' ''
  31. 'time' ''
  32. '[[' '\]\]'
  33. '((' '\)\)'
  34. 'case' '\)|esac'
  35. ';;' '\)|esac'
  36. ';&' '\)|esac'
  37. ';|' '\)|esac'
  38. 'foreach' '\(*\)'
  39. )
  40. typeset -grA __p9k_pb_precommand=(
  41. '-' ''
  42. 'builtin' ''
  43. 'command' ''
  44. 'exec' '-[^a]#[a]'
  45. 'nohup' ''
  46. 'setsid' ''
  47. 'eatmydata' ''
  48. 'catchsegv' ''
  49. 'pkexec' '--user'
  50. 'doas' '-[^aCu]#[acU]'
  51. 'nice' '-[^n]#[n]|--adjustment'
  52. 'stdbuf' '-[^ioe]#[ioe]|--(input|output|error)'
  53. 'sudo' '-[^aghpuUCcrtT]#[aghpuUCcrtT]|--(close-from|group|host|prompt|role|type|other-user|command-timeout|user)'
  54. 'ssh-agent' '-[^aEPt]#[aEPt]'
  55. 'tabbed' '-[^gnprtTuU]#[gnprtTuU]'
  56. 'chronic' ''
  57. 'ifne' ''
  58. )
  59. typeset -grA __p9k_pb_redirect=(
  60. '&>' ''
  61. '>' ''
  62. '>&' ''
  63. '<' ''
  64. '<&' ''
  65. '<>' ''
  66. '&>|' ''
  67. '>|' ''
  68. '&>>' ''
  69. '>>' ''
  70. '>>&' ''
  71. '&>>|' ''
  72. '>>|' ''
  73. '<<<' ''
  74. )
  75. typeset -grA __p9k_pb_term=(
  76. '|' ''
  77. '||' ''
  78. ';' ''
  79. '&' ''
  80. '&&' ''
  81. '|&' ''
  82. '&!' ''
  83. '&|' ''
  84. ';;' ''
  85. ';&' ''
  86. ';|' ''
  87. '(' ''
  88. ')' ''
  89. '()' '' # handled specially
  90. '}' '' # handled specially
  91. )
  92. typeset -grA __p9k_pb_term_skip=(
  93. '(' '\)'
  94. ';;' '\)|esac'
  95. ';&' '\)|esac'
  96. ';|' '\)|esac'
  97. )
  98. # Usage: _p9k_parse_buffer <buffer> [token-limit]
  99. #
  100. # Parses the specified command line buffer and pupulates array P9K_COMMANDS
  101. # with commands from it. Terminates early and returns 1 if there are more
  102. # tokens than the specified limit.
  103. #
  104. # Broken:
  105. #
  106. # ---------------
  107. # : $(x)
  108. # ---------------
  109. # : `x`
  110. # ---------------
  111. # ${x/}
  112. # ---------------
  113. # - -- x
  114. # ---------------
  115. # command -p -p x
  116. # ---------------
  117. # *
  118. # ---------------
  119. # x=$y; $x
  120. # ---------------
  121. # alias x=y; y
  122. # ---------------
  123. # x <<END
  124. # ; END
  125. # END
  126. # ---------------
  127. # Setup:
  128. # setopt interactive_comments
  129. # alias x='#'
  130. # Punchline:
  131. # x; y
  132. # ---------------
  133. #
  134. # More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.).
  135. function _p9k_parse_buffer() {
  136. [[ ${2:-0} == <-> ]] || return 2
  137. local rcquotes
  138. [[ -o rcquotes ]] && rcquotes=rcquotes
  139. eval "$__p9k_intro"
  140. setopt no_nomatch $rcquotes
  141. typeset -ga P9K_COMMANDS=()
  142. local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)'
  143. local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\""
  144. local -i e ic c=${2:-'1 << 62'}
  145. local skip n s r state token cmd prev
  146. local -a aln alp alf v
  147. if [[ -o interactive_comments ]]; then
  148. ic=1
  149. local tokens=(${(Z+C+)1})
  150. else
  151. local tokens=(${(z)1})
  152. fi
  153. {
  154. while (( $#tokens )); do
  155. (( e = $#state ))
  156. while (( $#tokens == alp[-1] )); do
  157. aln[-1]=()
  158. alp[-1]=()
  159. if (( $#tokens == alf[-1] )); then
  160. alf[-1]=()
  161. (( e = 0 ))
  162. fi
  163. done
  164. while (( c-- > 0 )) || return; do
  165. token=$tokens[1]
  166. tokens[1]=()
  167. if (( $+galiases[$token] )); then
  168. (( $aln[(eI)p$token] )) && break
  169. s=$galiases[$token]
  170. n=p$token
  171. elif (( e )); then
  172. break
  173. elif (( $+aliases[$token] )); then
  174. (( $aln[(eI)p$token] )) && break
  175. s=$aliases[$token]
  176. n=p$token
  177. elif [[ $token == ?*.?* ]] && (( $+saliases[${token##*.}] )); then
  178. r=${token##*.}
  179. (( $aln[(eI)s$r] )) && break
  180. s=${saliases[$r]%% #}
  181. n=s$r
  182. else
  183. break
  184. fi
  185. aln+=$n
  186. alp+=$#tokens
  187. [[ $s == *' ' ]] && alf+=$#tokens
  188. (( ic )) && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s})
  189. done
  190. case $token in
  191. '<<'(|-))
  192. state=h
  193. continue
  194. ;;
  195. *('`'|['<>=$']'(')*)
  196. if [[ $token == ('`'[^'`']##'`'|'"`'[^'`']##'`"'|'$('[^')']##')'|'"$('[^')']##')"'|['<>=']'('[^')']##')') ]]; then
  197. s=${${token##('"'|)(['$<>']|)?}%%?('"'|)}
  198. (( ic )) && tokens+=(';' ${(Z+C+)s}) || tokens+=(';' ${(z)s})
  199. fi
  200. ;;
  201. esac
  202. case $state in
  203. *r)
  204. state[-1]=
  205. continue
  206. ;;
  207. a)
  208. if [[ $token == $skip ]]; then
  209. if [[ $token == '{' ]]; then
  210. P9K_COMMANDS+=$cmd
  211. cmd=
  212. state=
  213. else
  214. skip='{'
  215. fi
  216. continue
  217. else
  218. state=t
  219. fi
  220. ;& # fall through
  221. t|p*)
  222. if (( $+__p9k_pb_term[$token] )); then
  223. if [[ $token == '()' ]]; then
  224. state=
  225. else
  226. P9K_COMMANDS+=$cmd
  227. if [[ $token == '}' ]]; then
  228. state=a
  229. skip=always
  230. else
  231. skip=$__p9k_pb_term_skip[$token]
  232. state=${skip:+s}
  233. fi
  234. fi
  235. cmd=
  236. continue
  237. elif [[ $state == t ]]; then
  238. continue
  239. elif [[ $state == *x ]]; then
  240. if (( $+__p9k_pb_redirect[$token] )); then
  241. prev=
  242. state[-1]=r
  243. continue
  244. else
  245. state[-1]=
  246. fi
  247. fi
  248. ;;
  249. s)
  250. if [[ $token == $~skip ]]; then
  251. state=
  252. fi
  253. continue
  254. ;;
  255. h)
  256. while (( $#tokens )); do
  257. (( e = ${tokens[(i)${(Q)token}]} ))
  258. if [[ $tokens[e-1] == ';' && $tokens[e+1] == ';' ]]; then
  259. tokens[1,e]=()
  260. break
  261. else
  262. tokens[1,e]=()
  263. fi
  264. done
  265. while (( $#alp && alp[-1] >= $#tokens )); do
  266. aln[-1]=()
  267. alp[-1]=()
  268. done
  269. state=t
  270. continue
  271. ;;
  272. esac
  273. if (( $+__p9k_pb_redirect[${token#<0-255>}] )); then
  274. state+=r
  275. continue
  276. fi
  277. if [[ $token == *'$'* ]]; then
  278. if [[ $token == $~var ]]; then
  279. n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]}
  280. [[ $token == *'"' ]] && v=("${(P)n}") || v=(${(P)n})
  281. tokens[1,0]=(${(@qq)v})
  282. continue
  283. fi
  284. fi
  285. case $state in
  286. '')
  287. if (( $+__p9k_pb_cmd_skip[$token] )); then
  288. skip=$__p9k_pb_cmd_skip[$token]
  289. [[ $token == '}' ]] && state=a || state=${skip:+s}
  290. continue
  291. fi
  292. if [[ $token == *=* ]]; then
  293. v=${(S)token/#(<->|([[:alpha:]_][[:IDENT:]]#(|'['*[^\\](\\\\)#']')))(|'+')=}
  294. if (( $#v < $#token )); then
  295. if [[ $v == '(' ]]; then
  296. state=s
  297. skip='\)'
  298. fi
  299. continue
  300. fi
  301. fi
  302. : ${token::=${(Q)${~token}}}
  303. ;;
  304. p2)
  305. if [[ -n $prev ]]; then
  306. prev=
  307. else
  308. : ${token::=${(Q)${~token}}}
  309. if [[ $token == '{'$~id'}' ]]; then
  310. state=p2x
  311. prev=$token
  312. else
  313. state=p
  314. fi
  315. continue
  316. fi
  317. ;& # fall through
  318. p)
  319. if [[ -n $prev ]]; then
  320. token=$prev
  321. prev=
  322. else
  323. : ${token::=${(Q)${~token}}}
  324. case $token in
  325. '{'$~id'}') prev=$token; state=px; continue;;
  326. [^-]*) ;;
  327. --) state=p1; continue;;
  328. $~skip) state=p2; continue;;
  329. *) continue;;
  330. esac
  331. fi
  332. ;;
  333. p1)
  334. if [[ -n $prev ]]; then
  335. token=$prev
  336. prev=
  337. else
  338. : ${token::=${(Q)${~token}}}
  339. if [[ $token == '{'$~id'}' ]]; then
  340. state=p1x
  341. prev=$token
  342. continue
  343. fi
  344. fi
  345. ;;
  346. esac
  347. if (( $+__p9k_pb_precommand[$token] )); then
  348. prev=
  349. state=p
  350. skip=$__p9k_pb_precommand[$token]
  351. cmd+=$token$'\0'
  352. else
  353. state=t
  354. [[ $token == ('(('*'))'|'`'*'`'|'$'*|['<>=']'('*')'|*$'\0'*) ]] || cmd+=$token$'\0'
  355. fi
  356. done
  357. } always {
  358. [[ $state == (px|p1x) ]] && cmd+=$prev
  359. P9K_COMMANDS+=$cmd
  360. P9K_COMMANDS=(${(u)P9K_COMMANDS%$'\0'})
  361. }
  362. }