Dotfiles for my tiling window manager + terminal workflow.
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.

137 lines
5.0 KiB

  1. # fish completion for kogito -*- shell-script -*-
  2. function __kogito_debug
  3. set file "$BASH_COMP_DEBUG_FILE"
  4. if test -n "$file"
  5. echo "$argv" >> $file
  6. end
  7. end
  8. function __kogito_perform_completion
  9. __kogito_debug "Starting __kogito_perform_completion with: $argv"
  10. set args (string split -- " " "$argv")
  11. set lastArg "$args[-1]"
  12. __kogito_debug "args: $args"
  13. __kogito_debug "last arg: $lastArg"
  14. set emptyArg ""
  15. if test -z "$lastArg"
  16. __kogito_debug "Setting emptyArg"
  17. set emptyArg \"\"
  18. end
  19. __kogito_debug "emptyArg: $emptyArg"
  20. set requestComp "$args[1] __complete $args[2..-1] $emptyArg"
  21. __kogito_debug "Calling $requestComp"
  22. set results (eval $requestComp 2> /dev/null)
  23. set comps $results[1..-2]
  24. set directiveLine $results[-1]
  25. # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)
  26. # completions must be prefixed with the flag
  27. set flagPrefix (string match -r -- '-.*=' "$lastArg")
  28. __kogito_debug "Comps: $comps"
  29. __kogito_debug "DirectiveLine: $directiveLine"
  30. __kogito_debug "flagPrefix: $flagPrefix"
  31. for comp in $comps
  32. printf "%s%s\n" "$flagPrefix" "$comp"
  33. end
  34. printf "%s\n" "$directiveLine"
  35. end
  36. # This function does three things:
  37. # 1- Obtain the completions and store them in the global __kogito_comp_results
  38. # 2- Set the __kogito_comp_do_file_comp flag if file completion should be performed
  39. # and unset it otherwise
  40. # 3- Return true if the completion results are not empty
  41. function __kogito_prepare_completions
  42. # Start fresh
  43. set --erase __kogito_comp_do_file_comp
  44. set --erase __kogito_comp_results
  45. # Check if the command-line is already provided. This is useful for testing.
  46. if not set --query __kogito_comp_commandLine
  47. set __kogito_comp_commandLine (commandline)
  48. end
  49. __kogito_debug "commandLine is: $__kogito_comp_commandLine"
  50. set results (__kogito_perform_completion "$__kogito_comp_commandLine")
  51. set --erase __kogito_comp_commandLine
  52. __kogito_debug "Completion results: $results"
  53. if test -z "$results"
  54. __kogito_debug "No completion, probably due to a failure"
  55. # Might as well do file completion, in case it helps
  56. set --global __kogito_comp_do_file_comp 1
  57. return 0
  58. end
  59. set directive (string sub --start 2 $results[-1])
  60. set --global __kogito_comp_results $results[1..-2]
  61. __kogito_debug "Completions are: $__kogito_comp_results"
  62. __kogito_debug "Directive is: $directive"
  63. if test -z "$directive"
  64. set directive 0
  65. end
  66. set compErr (math (math --scale 0 $directive / 1) % 2)
  67. if test $compErr -eq 1
  68. __kogito_debug "Received error directive: aborting."
  69. # Might as well do file completion, in case it helps
  70. set --global __kogito_comp_do_file_comp 1
  71. return 0
  72. end
  73. set nospace (math (math --scale 0 $directive / 2) % 2)
  74. set nofiles (math (math --scale 0 $directive / 4) % 2)
  75. __kogito_debug "nospace: $nospace, nofiles: $nofiles"
  76. # Important not to quote the variable for count to work
  77. set numComps (count $__kogito_comp_results)
  78. __kogito_debug "numComps: $numComps"
  79. if test $numComps -eq 1; and test $nospace -ne 0
  80. # To support the "nospace" directive we trick the shell
  81. # by outputting an extra, longer completion.
  82. __kogito_debug "Adding second completion to perform nospace directive"
  83. set --append __kogito_comp_results $__kogito_comp_results[1].
  84. end
  85. if test $numComps -eq 0; and test $nofiles -eq 0
  86. __kogito_debug "Requesting file completion"
  87. set --global __kogito_comp_do_file_comp 1
  88. end
  89. # If we don't want file completion, we must return true even if there
  90. # are no completions found. This is because fish will perform the last
  91. # completion command, even if its condition is false, if no other
  92. # completion command was triggered
  93. return (not set --query __kogito_comp_do_file_comp)
  94. end
  95. # Remove any pre-existing completions for the program since we will be handling all of them
  96. # TODO this cleanup is not sufficient. Fish completions are only loaded once the user triggers
  97. # them, so the below deletion will not work as it is run too early. What else can we do?
  98. complete -c kogito -e
  99. # The order in which the below two lines are defined is very important so that __kogito_prepare_completions
  100. # is called first. It is __kogito_prepare_completions that sets up the __kogito_comp_do_file_comp variable.
  101. #
  102. # This completion will be run second as complete commands are added FILO.
  103. # It triggers file completion choices when __kogito_comp_do_file_comp is set.
  104. complete -c kogito -n 'set --query __kogito_comp_do_file_comp'
  105. # This completion will be run first as complete commands are added FILO.
  106. # The call to __kogito_prepare_completions will setup both __kogito_comp_results abd __kogito_comp_do_file_comp.
  107. # It provides the program's completion choices.
  108. complete -c kogito -n '__kogito_prepare_completions' -f -a '$__kogito_comp_results'