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

Re: About insert-last-word and "command args | less"



On Oct 15, 12:39pm, Vincent Lefevre wrote:
}
} OK. But is it possible
}   * to skip a history line when all its words match the pattern?

This is difficult because of handling a numeric argument, e.g. when
called as "zle smart-insert-last-word 7" [%] it's supposed to back up 
exactly 7 commands and take the last word from that one.  The pattern
is not matched against the words until after the history line to
examine is chosen.

I believe the patch below does this the way you want ... however, it
makes a liar of the documentation, which implies that line-selection
takes precedence over interesting-word-selection; so it's not suitable
to be committed without some more work.

[%] As contrasted with "zle smart-insert-last-word -n 7" which is
supposed to select the 7th-from-last word of the previous command.

}   * to always get a word that wasn't proposed before (when one uses
}     ^]_ several times) by skipping history lines when needed?

That would require both that we solve the previous problem *and* store
somewhere all previous words that have been offered *and* do some kind
of negated match (e.g., the $lastcmd[(I)$pattern] change in the patch
below would have to be made back into a loop with two comparisons on
every word).


Index: smart-insert-last-word
===================================================================
diff -c -r1.2 smart-insert-last-word
--- smart-insert-last-word	15 Mar 2003 17:43:15 -0000	1.2
+++ smart-insert-last-word	15 Oct 2006 18:16:30 -0000
@@ -35,7 +35,7 @@
 #   bindkey '\e=' insert-last-assignment
 
 emulate -L zsh
-setopt extendedglob
+setopt extendedglob nohistignoredups
 
 # Not strictly necessary:
 # (($+_ilw_hist)) || integer -g _ilw_hist _ilw_count _ilw_cursor _ilw_lcursor
@@ -64,27 +64,34 @@
 _ilw_hist=$HISTNO
 _ilw_count=$NUMERIC
 
+if [[ -z "$numeric" ]]
+then
+    zstyle -s :$WIDGET match pattern ||
+	pattern='*[[:alpha:]/\\]*'
+fi
+
 zle .up-history || return 1      # Retrieve previous command
 lastcmd=( ${${(z)BUFFER}:#\;} )  # Split into shell words
+if [[ -n "$pattern" ]]
+then
+    integer n=0 found=$lastcmd[(I)$pattern]
+    while (( found == 0 && ++n ))
+    do
+        zle .up-history || return 1
+        lastcmd=( ${${(z)BUFFER}:#\;} )
+        found=$lastcmd[(I)$pattern]
+    done
+    (( found-- && ( NUMERIC += n ) ))
+fi
 zle .down-history                # Return to current command
 CURSOR=$cursor                   # Restore cursor position
 NUMERIC=${numeric:-1}            # In case of fall through
 
 (( NUMERIC > $#lastcmd )) && return 1
 
-if [[ -z "$numeric" ]]
+if [[ -n "$pattern" ]]
 then
-    integer i=1
-    zstyle -s :$WIDGET match pattern ||
-	pattern='*[[:alpha:]/\\]*'
-    while ((i <= $#lastcmd)); do
-	if [[ $lastcmd[-i] == $~pattern ]]; then
-	    NUMERIC=$i
-	    break
-	else
-	    ((++i))
-	fi
-    done
+    NUMERIC=$(( $#lastcmd - found ))
 fi
 LBUFFER[lcursor+1,cursor+1]=$lastcmd[-NUMERIC]
 _ilw_cursor=$CURSOR


-- 



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