zsh-workers
 help / color / mirror / code / Atom feed
* Re: delete-word does not delete the entire word...
       [not found] <3762.1066039448@csr.com>
@ 2003-10-13 12:00 ` Peter Stephenson
  2003-10-13 16:49   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2003-10-13 12:00 UTC (permalink / raw)
  To: Zsh hackers list

Despite the underwhelming response, here is delete-whole-word-match.  It
will now do the right thing if you define it as a `kill' widget (in
principle, the other -match widgets could have a similar treatment, only
in reverse).

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.29
diff -u -r1.29 contrib.yo
--- Doc/Zsh/contrib.yo	16 Sep 2003 00:43:42 -0000	1.29
+++ Doc/Zsh/contrib.yo	13 Oct 2003 11:56:08 -0000
@@ -484,6 +484,21 @@
 the var(X) of tt(foo)var(X)tt(bar), where var(X) can be any character, then
 the resulting expression is tt(bar)var(X)tt(foo).
 
+Here are some examples of use of the styles, actually taken from the
+simplified interface in tt(select-word-style):
+
+example(zstyle ':zle:*' word-style standard
+zstyle ':zle:*' word-chars '')
+
+Implements bash-style word handling for all widgets, i.e. only
+alphanumerics are word characters; equivalent to setting
+the parameter tt(WORDCHARS) empty for the given context.
+
+example(style ':zle:*kill*' word-style space)
+
+Uses space-delimited words for widgets with the word `kill' in the name.
+Neither of the styles tt(word-chars) nor tt(word-class) is used in this case.
+
 The word matching and all the handling of tt(zstyle) settings is actually
 implemented by the function tt(match-words-by-style).  This can be used to
 create new user-defined widgets.  The calling function should set the local
@@ -498,6 +513,22 @@
 non-word characters following that word (7) the remainder of the line.  Any
 of the elements may be an empty string; the calling function should test
 for this to decide whether it can perform its function.
+)
+tindex(delete-whole-word-match)
+item(tt(delete-whole-word-match))(
+This is another function which works like the tt(-match) functions
+described immediately above, i.e. using styles to decide the word
+boundaries.  However, it is not a replacement for any existing function.
+
+The basic behaviour is to delete the word around the cursor.  There is no
+numeric prefix handling; only the single word around the cursor is
+considered.  If the widget contains the string tt(kill), the removed text
+will be placed in the cutbuffer for future yanking.  This can be obtained
+by defining tt(kill-whole-word-match) as follows:
+
+example(zle -N kill-whole-word-match delete-whole-word-match)
+
+and then binding the widget tt(kill-whole-word-match).
 )
 tindex(copy-earlier-word)
 item(tt(copy-earlier-word))(
Index: Functions/Zle/delete-whole-word-match
===================================================================
RCS file: Functions/Zle/delete-whole-word-match
diff -N Functions/Zle/delete-whole-word-match
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Functions/Zle/delete-whole-word-match	13 Oct 2003 11:56:08 -0000
@@ -0,0 +1,56 @@
+# Delete the entire word around the cursor.  Does not handle
+# a prefix argument; either the cursor is in the word or it isn't.
+# The word may be just before the cursor, e.g.
+#   print this is a line
+#             ^ here
+# and then the word before (i.e. `this') will be deleted.
+#
+# If the widget has the name `kill' in, the text deleted will be
+# saved for future yanking in the normal way.
+
+emulate -L zsh
+setopt extendedglob
+
+local curcontext=:zle:delete-whole-word
+local -a matched_words
+# Start and end of range of characters to remove.
+integer pos1 pos2
+
+autoload -U match-words-by-style
+match-words-by-style
+
+if [[ -n "${matched_words[3]}" ]]; then
+    # There's whitespace before the cursor, so the word we are deleting
+    # starts at the cursor position.
+    pos1=$CURSOR
+else
+    # No whitespace before us, so delete any wordcharacters there.
+    pos1="${#matched_words[1]}"
+fi
+
+if [[ -n "${matched_words[4]}" ]]; then
+    # There's whitespace at the cursor position, so only delete
+    # up to the cursor position.
+    pos2=$CURSOR
+else
+    # No whitespace at the cursor position, so delete the
+    # current character and any following wordcharacters.
+    (( pos2 = CURSOR + ${#matched_words[5]} + 1 ))
+fi
+
+# Move the cursor then delete the block in one go for the
+# purpose of undoing (and yanking, if appropriate).
+(( CURSOR = pos1 ))
+
+# If the widget name includes the word `kill', the removed
+# text goes into the cutbuffer in the standard way.
+if [[ $WIDGET = *kill* ]]; then
+  local word="${BUFFER[pos1+1,pos2-1]}"
+  if [[ $LASTWIDGET = *kill* ]]; then
+    CUTBUFFER="$CUTBUFFER$word"
+  else
+    killring=("$CUTBUFFER" "${(@)killring[1,-2]}")
+    CUTBUFFER=$word
+  fi
+fi
+BUFFER="${BUFFER[1,pos1]}${BUFFER[pos2,-1]}"

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: delete-word does not delete the entire word...
  2003-10-13 12:00 ` delete-word does not delete the entire word Peter Stephenson
@ 2003-10-13 16:49   ` Bart Schaefer
  2003-10-13 17:19     ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2003-10-13 16:49 UTC (permalink / raw)
  To: Zsh hackers list

On Oct 13,  1:00pm, Peter Stephenson wrote:
}
} Despite the underwhelming response

Hey, it's a holiday weekend on this side of the pond; give it time.

One question:

} +local curcontext=:zle:delete-whole-word

Wouldn't

	local curcontext=:zle:$WIDGET
or
	local curcontext=:zle:${WIDGET%-match}

be better?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: delete-word does not delete the entire word...
  2003-10-13 16:49   ` Bart Schaefer
@ 2003-10-13 17:19     ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2003-10-13 17:19 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> One question:
> 
> } +local curcontext=:zle:delete-whole-word
> 
> Wouldn't
> 
> 	local curcontext=:zle:$WIDGET
> or
> 	local curcontext=:zle:${WIDGET%-match}
> 
> be better?

Yes.  The former is how all the others work.

Talking of underwhelming responses :-), did anyone ever look at the
additions I posted to match-words-by-style which would allow you to have
different subcontexts in different bits of the command line?  The one
where you could do

   zstyle ':zle:*' word-context "[[:space:]]" whitespace "*/*" \
       filename "" end "*" other
   zstyle ':zle:transpose-words:whitespace' word-style shell
   zstyle ':zle:transpose-words:filename' word-style normal
   zstyle ':zle:transpose-words:filename' word-chars ''

Anyway, I'm not happy with this; I think there must be a better way of
picking context.  For example, instead of putting the onus on the user
to pick out the contexts, it would probably be more natural simply to
extend the existing contexts.  Perhaps something like
"<context>:<pattern>" or "<context>:<wordnum>:<pattern>" where context
is just an alphanumeric string would carry more information.  In other
words, word-context would go and the other styles would look like

   zstyle ':zle:transpose-words:between:<->:*' word-style shell
   zstyle ':zle:transpose-words:word:<->:*/*' word-style normal
   zstyle ':zle:transpose-words:word:<->:*/*' word-chars ''

where that third word would be a set like
  empty		an empty line
  before	the unusual case of whitespace before any text
  word		a shell word, matched by the last element of the style
  between	whitespace between words (next arg is number of word before)
  end		after all words

and the fourth word could be e.g. 1 or <2-> to pick the command word or
not the command word.

Half an input is better than no bread.


Index: Functions/Zle/delete-whole-word-match
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Zle/delete-whole-word-match,v
retrieving revision 1.1
diff -u -r1.1 delete-whole-word-match
--- Functions/Zle/delete-whole-word-match	13 Oct 2003 16:50:19 -0000	1.1
+++ Functions/Zle/delete-whole-word-match	13 Oct 2003 17:05:19 -0000
@@ -11,7 +11,7 @@
 emulate -L zsh
 setopt extendedglob
 
-local curcontext=:zle:delete-whole-word
+local curcontext=:zle:$WIDGET
 local -a matched_words
 # Start and end of range of characters to remove.
 integer pos1 pos2

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-10-13 17:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <3762.1066039448@csr.com>
2003-10-13 12:00 ` delete-word does not delete the entire word Peter Stephenson
2003-10-13 16:49   ` Bart Schaefer
2003-10-13 17:19     ` Peter Stephenson

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).