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.

132 lines
4.5 KiB

3 years ago
  1. zsh-syntax-highlighting / highlighters
  2. ======================================
  3. Syntax highlighting is done by pluggable highlighters:
  4. * `main` - the base highlighter, and the only one [active by default][1].
  5. * `brackets` - [matches brackets][2] and parenthesis.
  6. * `pattern` - matches [user-defined patterns][3].
  7. * `cursor` - matches [the cursor position][4].
  8. * `root` - highlights the whole command line [if the current user is root][5].
  9. * `line` - applied to [the whole command line][6].
  10. [1]: highlighters/main.md
  11. [2]: highlighters/brackets.md
  12. [3]: highlighters/pattern.md
  13. [4]: highlighters/cursor.md
  14. [5]: highlighters/root.md
  15. [6]: highlighters/line.md
  16. Highlighter-independent settings
  17. --------------------------------
  18. By default, all command lines are highlighted. However, it is possible to
  19. prevent command lines longer than a fixed number of characters from being
  20. highlighted by setting the variable `${ZSH_HIGHLIGHT_MAXLENGTH}` to the maximum
  21. length (in characters) of command lines to be highlighter. This is useful when
  22. editing very long comand lines (for example, with the [`fned`][fned] utility
  23. function). Example:
  24. [fned]: http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#index-zed
  25. ```zsh
  26. ZSH_HIGHLIGHT_MAXLENGTH=512
  27. ```
  28. How to activate highlighters
  29. ----------------------------
  30. To activate an highlighter, add it to the `ZSH_HIGHLIGHT_HIGHLIGHTERS` array in
  31. `~/.zshrc`, for example:
  32. ```zsh
  33. ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern cursor)
  34. ```
  35. By default, `$ZSH_HIGHLIGHT_HIGHLIGHTERS` is unset and only the `main`
  36. highlighter is active.
  37. How to tweak highlighters
  38. -------------------------
  39. Highlighters look up styles from the `ZSH_HIGHLIGHT_STYLES` associative array.
  40. Navigate into the [individual highlighters' documentation](highlighters/) to
  41. see what styles (keys) each highlighter defines; the syntax for values is the
  42. same as the syntax of "types of highlighting" of the zsh builtin
  43. `$zle_highlight` array, which is documented in [the `zshzle(1)` manual
  44. page][zshzle-Character-Highlighting].
  45. [zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting
  46. Some highlighters support additional configuration parameters; see each
  47. highlighter's documentation for details and examples.
  48. How to implement a new highlighter
  49. ----------------------------------
  50. To create your own `acme` highlighter:
  51. * Create your script at
  52. `highlighters/acme/acme-highlighter.zsh`.
  53. * Implement the `_zsh_highlight_highlighter_acme_predicate` function.
  54. This function must return 0 when the highlighter needs to be called and
  55. non-zero otherwise, for example:
  56. ```zsh
  57. _zsh_highlight_highlighter_acme_predicate() {
  58. # Call this highlighter in SVN working copies
  59. [[ -d .svn ]]
  60. }
  61. ```
  62. * Implement the `_zsh_highlight_highlighter_acme_paint` function.
  63. This function does the actual syntax highlighting, by calling
  64. `_zsh_highlight_add_highlight` with the start and end of the region to
  65. be highlighted and the `ZSH_HIGHLIGHT_STYLES` key to use. Define the default
  66. style for that key in the highlighter script outside of any function with
  67. `: ${ZSH_HIGHLIGHT_STYLES[key]:=value}`, being sure to prefix
  68. the key with your highlighter name and a colon. For example:
  69. ```zsh
  70. : ${ZSH_HIGHLIGHT_STYLES[acme:aurora]:=fg=green}
  71. _zsh_highlight_highlighter_acme_paint() {
  72. # Colorize the whole buffer with the 'aurora' style
  73. _zsh_highlight_add_highlight 0 $#BUFFER acme:aurora
  74. }
  75. ```
  76. If you need to test which options the user has set, test `zsyh_user_options`
  77. with a sensible default if the option is not present in supported zsh
  78. versions. For example:
  79. ```zsh
  80. [[ ${zsyh_user_options[ignoreclosebraces]:-off} == on ]]
  81. ```
  82. The option name must be all lowercase with no underscores and not an alias.
  83. * Name your own functions and global variables `_zsh_highlight_acme_*`.
  84. - In zsh-syntax-highlighting 0.4.0 and earlier, the entrypoints
  85. `_zsh_highlight_highlighter_acme_predicate` and
  86. `_zsh_highlight_highlighter_acme_paint`
  87. were named
  88. `_zsh_highlight_acme_highlighter_predicate` and
  89. `_zsh_highlight_highlighter_acme_paint` respectively.
  90. These names are still supported for backwards compatibility;
  91. however, support for them will be removed in a future major or minor release (v0.x.0 or v1.0.0).
  92. * Activate your highlighter in `~/.zshrc`:
  93. ```zsh
  94. ZSH_HIGHLIGHT_HIGHLIGHTERS+=(acme)
  95. ```
  96. * [Write tests](../tests/README.md).