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

Re: Identify "active" region?



2008/5/4 Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>:
> I'd like to write a widget that does
>
>   if [[ there is an active region ]]
>   then zle kill-region
>   else zle backward-kill-word
>   fi
>
>  (In fact, I'm tempted to write it in C and make it the default binding
>  for ^W, but that's somewhat beside the point of this message.)
>
>  There doesn't seem to be any way to determine that there's an active
>  region except to override set-mark-command and exchange-point-and-mark
>  to stash something in a global variable.  Have I missed something?
>
>  (I thought perhaps I could peek into $region_highlight in the latest
>  from CVS, but that doesn't update on the fly.)
>
>  Opinions on usefulness going forward of detecting the active region?
>  A distinguished value of MARK appears too problematic, so it'd have to
>  be another zle variable, or ...?
>
>  For the time being I'm using the less-than-satisfactory
>   (( MARK > 0 && MARK != CURSOR ))
>  which isn't *that* bad since if I meant to kill all the way to the
>  beginning of the line I'd probably use kill-whole-line, and at worst
>  I just have to do exchange-point-and-mark first.

fwiw, i'd like to second this, i've been meaning to ask it for a while
but didn't get around to it. My widget looks like this:

autoload -U narrow-to-region
function _narrow_to_region_marked()
{
  local right
  local left
  if ((MARK == 0)); then
    zle set-mark-command
  fi
  if ((MARK < CURSOR)); then
    left="$LBUFFER[0,$((MARK-CURSOR-1))]"
    right="$RBUFFER"
  else
    left="$LBUFFER"
    right="$BUFFER[$((MARK+1)),-1]"
  fi
  narrow-to-region -p "$left>>|" -P "|<<$right"
}

I'd like to replace the "MARK == 0" with "is there an active region?".

-- 
Mikael Magnusson



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