zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh workers <zsh-workers@zsh.org>
Subject: crazy brace match highlighting
Date: Sun, 29 Nov 2009 12:35:47 +0100	[thread overview]
Message-ID: <237967ef0911290335o46d37f85rf64c617f67ddcf5e@mail.gmail.com> (raw)

2009/11/29 Mikael Magnusson <mikachu@gmail.com>:
> 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


                 reply	other threads:[~2009-11-29 11:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=237967ef0911290335o46d37f85rf64c617f67ddcf5e@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).