Skip to content

[Enhancement] Highlight cells based on content #244

@juanMarinero

Description

@juanMarinero

Highlight cells based on content is explained in the README. Though I cannot find it in the docs.
It's coded here.

I want to add custom highlights for cells starting with not only 'yes', 'no' or '?'.
How to redefine the main Table syntax rule without rewriting the whole syntax group "Table" ? I.e. just expand its contains? Search TODO in code below.

One could fork the plugin and edit the script. Not ideal.

Next code works. Once :TableModeToggle runs I get the desired highlights:

Image

Highlights

hi yesCell          ctermfg=2 guifg=Green
hi noCell           ctermfg=1 guifg=Red
hi maybeCell        ctermfg=3 guifg=Orange
hi highPriorityCell ctermfg=1 guifg=Red   cterm=bold gui=bold
hi completeCell     ctermfg=2 guifg=Green cterm=bold gui=bold
hi link infoCell    Comment " Or make it look like comments in your colorscheme

Code to run if triggered autocmd User TableModeEnabled

function! g:TableModeColorCellsCustom()
  if !exists('b:table_mode_active') || !b:table_mode_active
    return
  endif
  
  " 1. FIRST: Redefine the main Table syntax rule with our custom groups added to contains
  " TODO, do NOT redefine, avoid hard-copy
  syntax clear Table
  syntax match Table /|.\+|/ 
        \ contains=TableBorder,TableSeparator,TableColumnAlign,
        \yesCell,noCell,maybeCell,redCell,greenCell,yellowCell,blueCell,whiteCell,darkCell,
        \highPriorityCell,completeCell,infoCell
        \ containedin=ALL

  " 2. THEN: Define our custom syntax patterns as contained
  syntax match highPriorityCell /|\@<= *\![^|]*/ contained
  syntax match completeCell /|\@<= *X[^|]*/ contained
  syntax match infoCell /|\@<= *\/\/[^|]*/ contained
endfunction

augroup TableModeColorCellsCustom
  autocmd!
  " Call it after a short delay when table mode is enabled
  autocmd User TableModeEnabled call timer_start(10, {-> g:TableModeColorCellsCustom()})
  " If error then manually run ":call g:TableModeColorCellsCustom()"
augroup END

Note, original :execute('syntax list Table') echoes:

--- Syntax items ---
  Table          xxx match /|.\+|/  contains=TableBorder,TableSeparator,TableColumnAlign,yesCell,noCell,maybeCell,redCell,greenCell,yellowCell,blueCell,whiteCell,darkCell containedin=ALL

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions