accept-line 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ### vim:ft=zsh:foldmethod=marker
  2. ###
  3. ### a generic accept-line wrapper
  4. ###
  5. ### Frank Terbeck <ft@bewatermyfriend.org>
  6. ### Last-Modified: Wed Dec 26 14:37:47 2007
  7. ###
  8. ### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
  9. ###
  10. function Accept-Line() {
  11. setopt localoptions noksharrays
  12. local -a subs
  13. local -xi aldone
  14. local sub
  15. zstyle -a ":acceptline:${alcontext}" actions subs
  16. (( ${#subs} < 1 )) && return 0
  17. (( aldone = 0 ))
  18. for sub in ${subs} ; do
  19. [[ ${sub} == 'accept-line' ]] && sub='.accept-line'
  20. zle ${sub}
  21. (( aldone > 0 )) && break
  22. done
  23. }
  24. function Accept-Line-getdefault() {
  25. local default_action
  26. zstyle -s ":acceptline:${alcontext}" default_action default_action
  27. case ${default_action} in
  28. ((accept-line|))
  29. printf ".accept-line"
  30. ;;
  31. (*)
  32. printf ${default_action}
  33. ;;
  34. esac
  35. }
  36. function accept-line() {
  37. setopt localoptions noksharrays
  38. local -a cmdline
  39. local -x alcontext
  40. local buf com fname format msg default_action
  41. alcontext='default'
  42. buf="${BUFFER}"
  43. cmdline=(${(z)BUFFER})
  44. com="${cmdline[1]}"
  45. fname="_${com}"
  46. zstyle -t ":acceptline:${alcontext}" rehash \
  47. && [[ -z ${commands[$com]} ]] \
  48. && rehash
  49. if [[ -n ${reswords[(r)$com]} ]] \
  50. || [[ -n ${aliases[$com]} ]] \
  51. || [[ -n ${functions[$com]} ]] \
  52. || [[ -n ${builtins[$com]} ]] \
  53. || [[ -n ${commands[$com]} ]] ; then
  54. # there is something sensible to execute, just do it.
  55. alcontext='normal'
  56. zle Accept-Line
  57. default_action=$(Accept-Line-getdefault)
  58. zstyle -T ":acceptline:${alcontext}" call_default \
  59. && zle ${default_action}
  60. return
  61. fi
  62. if [[ -o correct ]] \
  63. || [[ -o correctall ]] \
  64. && [[ -n ${functions[$fname]} ]] ; then
  65. # nothing there to execute but there is a function called
  66. # _command_name; a completion widget. Makes no sense to
  67. # call it on the commandline, but the correct{,all} options
  68. # will ask for it nevertheless, so warn the user.
  69. if [[ ${LASTWIDGET} == 'accept-line' ]] ; then
  70. # Okay, we warned the user before, he called us again,
  71. # so have it his way.
  72. alcontext='force'
  73. zle Accept-Line
  74. default_action=$(Accept-Line-getdefault)
  75. zstyle -T ":acceptline:${alcontext}" call_default \
  76. && zle ${default_action}
  77. return
  78. fi
  79. # prepare warning message for the user, configurable via zstyle.
  80. zstyle -s ":acceptline:${alcontext}" compwarnfmt msg
  81. if [[ -z ${msg} ]] ; then
  82. msg="%c will not execute and completion %f exists."
  83. fi
  84. zformat -f msg "${msg}" "c:${com}" "f:${fname}"
  85. zle -M -- "${msg}"
  86. return
  87. elif [[ -n ${buf//[$' \t\n']##/} ]] ; then
  88. # If we are here, the commandline contains something that is not
  89. # executable, which is neither subject to _command_name correction
  90. # and is not empty. might be a variable assignment
  91. alcontext='misc'
  92. zle Accept-Line
  93. default_action=$(Accept-Line-getdefault)
  94. zstyle -T ":acceptline:${alcontext}" call_default \
  95. && zle ${default_action}
  96. return
  97. fi
  98. # If we got this far, the commandline only contains whitespace, or is empty.
  99. alcontext='empty'
  100. zle Accept-Line
  101. default_action=$(Accept-Line-getdefault)
  102. zstyle -T ":acceptline:${alcontext}" call_default \
  103. && zle ${default_action}
  104. }
  105. zle -N accept-line
  106. zle -N Accept-Line