Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

crazy brace match highlighting



2009/11/29 Mikael Magnusson <mikachu@xxxxxxxxx>:
> Oops, this was due to an experiment I was doing with brace
> highlighting, sorry for the noise. I added a shell hook in zrefresh
> which is probably quite crazy, which ended up overwriting the static
> variables used in the isearch pattern matching.

I changed it to use =~ instead and it seems to work ;). So this is
sort of a crazy experiment  just to see if it would be useful.

Patch to c code:

diff --git a/Src/Zle/zle_refresh.c b/Src/Zle/zle_refresh.c
--- a/Src/Zle/zle_refresh.c
+++ b/Src/Zle/zle_refresh.c
@@ -1039,6 +1039,14 @@ zrefresh(void)
        tmpalloced = 0;
     }

+    if ((initthingy = rthingy_nocreate("zle-line-pre-redraw"))) {
+       char *args[2];
+       args[0] = initthingy->nam;
+       args[1] = NULL;
+       execzlefunc(initthingy, args, 1);
+       unrefthingy(initthingy);
+    }
+
     /* this will create region_highlights if it's still NULL */
     zle_set_highlight();



.zshrc stuff:

function _line_redraw_dummy() {
}

function _line_redraw_brace_detect() {
  local char=$BUFFER[pos]
  if [[ $char =~ '\(' ]]; then
    dir=1
    that=')'
  elif [[ $char =~ '\)' ]]; then
    dir=-1
    that='('
  elif [[ $char =~ '\]' ]]; then
    dir=-1
    that='['
  elif [[ $char =~ '\[' ]]; then
    dir=-1
    that=']'
  elif [[ $char =~ '\}' ]]; then
    dir=-1
    that='{'
  elif [[ $char =~ '\{' ]]; then
    dir=-1
    that='}'
  fi
}

function _line_redraw() {
  unset region_highlight

  #set this var to 2 in your accept-line hook to remove the hilight
  #when accepting a line
  [[ $__zle_line_accepted -gt 0 ]] && {
    (( __zle_line_accepted-- ))
    return
  }
  #this stuff is so slow
  [[ $#BUFFER -gt 250 ]] && { zle -N zle-line-pre-redraw
_line_redraw_dummy; return }

  #hilight matching parens,braces,brackets
  local ct=1 pos=$((CURSOR+1)) cpos dir this that
  _line_redraw_brace_detect
  (( ! dir )) && {
    (( pos-- ))
    _line_redraw_brace_detect
  }
  (( ! dir )) && return
  this=$BUFFER[pos]
  cpos=$pos
  while (( ((dir > 0) ? (pos < $#BUFFER) : pos > 0) && ct )) {
    (( pos+=dir ))
    [[ $BUFFER[pos] == $that ]] && (( ct-- ))
    [[ $BUFFER[pos] == $this ]] && (( ct++ ))
  }
  (( ct )) ||
    region_highlight=("$((cpos-1)) $cpos bold,bg=cyan,fg=black"
                      "$((pos-1)) $pos bold,bg=cyan,fg=black")

#this just colors each word, assumes terminal supports 88 colors, change the 88s
#to 16 if it doesn't. crazy slow.
#  local index=1 pos oldpos=0 region_highlight_tmp
#  while [[ ${pos::="$BUFFER[(in:index++:)[[:space:]]]"} -lt $#BUFFER ]]; do
#    region_highlight_tmp+=("$oldpos $((pos-1)) bg=$((index%88))")
#    oldpos=$pos
#  done
#  region_highlight_tmp+=("$oldpos $#BUFFER bg=$((index%88))")
#  region_highlight=($region_highlight_tmp)
}

function _zle_line_init() {
  zle -N zle-line-pre-redraw _line_redraw
}
zle -N zle-line-init _zle_line_init

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author