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.

120 lines
5.0 KiB

  1. # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  2. # Copyright (c) 2018 Sebastian Gniazdowski
  3. #
  4. # Example chroma function. It colorizes first two arguments as `builtin' style,
  5. # third and following arguments as `globbing' style. First two arguments may
  6. # be "strings", they will be passed through to normal higlighter (by returning 1).
  7. #
  8. # $1 - 0 or 1, denoting if it's first call to the chroma, or following one
  9. #
  10. # $2 - the current token, also accessible by $__arg from the above scope -
  11. # basically a private copy of $__arg; the token can be eg.: "grep"
  12. #
  13. # $3 - a private copy of $_start_pos, i.e. the position of the token in the
  14. # command line buffer, used to add region_highlight entry (see man),
  15. # because Zsh colorizes by *ranges* in command line buffer
  16. #
  17. # $4 - a private copy of $_end_pos from the above scope
  18. #
  19. #
  20. # Overall functioning is: when command "example" is occured, this function
  21. # is called with $1 == 1, it ("example") is the first token ($2), then for any
  22. # following token, this function is called with $1 == 0, until end of command
  23. # is occured (i.e. till enter is pressed or ";" is put into source, or the
  24. # command line simply ends).
  25. #
  26. # Other tips are:
  27. # - $CURSOR holds cursor position
  28. # - $BUFFER holds whole command line buffer
  29. # - $LBUFFER holds command line buffer that is left from the cursor, i.e. it's a
  30. # BUFFER substring 1 .. $CURSOR
  31. # - $RBUFFER is the same as LBUFFER but holds part of BUFFER right to the cursor
  32. #
  33. # The function receives $BUFFER but via sequence of tokens, which are shell words,
  34. # e.g. "a b c" is a shell word, while a b c are 3 shell words.
  35. #
  36. # FAST_HIGHLIGHT is a friendly hash array which allows to store strings without
  37. # creating global parameters (variables). If you need hash, just use it first
  38. # declaring, under some distinct name like: typeset -gA CHROMA_EXPLE_DICT.
  39. # Remember to reset the hash and others at __first_call == 1, so that you have
  40. # a fresh state for new command.
  41. # Keep chroma-takever state meaning: until ;, handle highlighting via chroma.
  42. # So the below 8192 assignment takes care that next token will be routed to chroma.
  43. (( next_word = 2 | 8192 ))
  44. local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  45. local __style
  46. integer __idx1 __idx2
  47. (( __first_call )) && {
  48. # Called for the first time - new command.
  49. # FAST_HIGHLIGHT is used because it survives between calls, and
  50. # allows to use a single global hash only, instead of multiple
  51. # global string variables.
  52. FAST_HIGHLIGHT[chroma-example-counter]=0
  53. # Set style for region_highlight entry. It is used below in
  54. # '[[ -n "$__style" ]] ...' line, which adds highlight entry,
  55. # like "10 12 fg=green", through `reply' array.
  56. #
  57. # Could check if command `example' exists and set `unknown-token'
  58. # style instead of `command'
  59. __style=${FAST_THEME_NAME}command
  60. } || {
  61. # Following call, i.e. not the first one
  62. # Check if chroma should end – test if token is of type
  63. # "starts new command", if so pass-through – chroma ends
  64. [[ "$__arg_type" = 3 ]] && return 2
  65. if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
  66. return 1
  67. fi
  68. if [[ "$__wrd" = -* ]]; then
  69. # Detected option, add style for it.
  70. [[ "$__wrd" = --* ]] && __style=${FAST_THEME_NAME}double-hyphen-option || \
  71. __style=${FAST_THEME_NAME}single-hyphen-option
  72. else
  73. # Count non-option tokens
  74. (( FAST_HIGHLIGHT[chroma-example-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-example-counter] ))
  75. # Colorize 1..2 as builtin, 3.. as glob
  76. if (( FAST_HIGHLIGHT[chroma-example-counter] <= 2 )); then
  77. if [[ "$__wrd" = \"* ]]; then
  78. # Pass through, fsh main code will do the highlight!
  79. return 1
  80. else
  81. __style=${FAST_THEME_NAME}builtin
  82. fi
  83. else
  84. __style=${FAST_THEME_NAME}globbing
  85. fi
  86. fi
  87. }
  88. # Add region_highlight entry (via `reply' array).
  89. # If 1 will be added to __start_pos, this will highlight "oken".
  90. # If 1 will be subtracted from __end_pos, this will highlight "toke".
  91. # $PREBUFFER is for specific situations when users does command \<ENTER>
  92. # i.e. when multi-line command using backslash is entered.
  93. #
  94. # This is a common place of adding such entry, but any above code can do
  95. # it itself (and it does in other chromas) and skip setting __style to
  96. # this way disable this code.
  97. [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
  98. # We aren't passing-through, do obligatory things ourselves.
  99. # _start_pos=$_end_pos advainces pointers in command line buffer.
  100. #
  101. # To pass through means to `return 1'. The highlighting of
  102. # this single token is then done by fast-syntax-highlighting's
  103. # main code and chroma doesn't have to do anything.
  104. (( this_word = next_word ))
  105. _start_pos=$_end_pos
  106. return 0
  107. # vim:ft=zsh:et:sw=4