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

PATCH: list-files style for "ls -l" completion listings



Here are the bare bones of a way of getting more information about
file listings.  Setting the list-files style allows you to have
files listed as for "ls -L" instead of "ls" or "ls -F".  The
style can take a number so list=10 means show in long format only if
listing but not inserting and only if there are no more than 10 matches.

Note I had to add an option -o to compadd:  the problem was that if
you gave a display string it was always used for sorting the matches to
be displayed, and the result wasn't sensible in ls -l format with the
file name at the end.  (If there's a better letter than -o tell me.)
(The result still seems to respect the file-sort style, which amazes me
a bit.)

This is still very basic:
- I have chickened out of adding the framework to _path_files in all but
  the simplest cases where the files are add as a simple array.  If
  anyone thought they understodd _path_files they could do better.
  This definitely needs fixing (any occurrence of "compadd -Qf"
  should handle file-list in the same way).
- The determination of "insert" or "list" context is very basic and
  there's probably useful information somewhere in $compstate, if I
  knew what.  More generally, the value of file-list could possibly
  be more configurable.
- Customization of the ls -l output format wouldn't be too hard.

Actually, as always, I'm hoping someone else is going to improve it for
me, but I don't suppose they are.

Index: Completion/Unix/Type/_list_files
===================================================================
RCS file: Completion/Unix/Type/_list_files
diff -N Completion/Unix/Type/_list_files
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Completion/Unix/Type/_list_files	6 Mar 2006 12:59:49 -0000
@@ -0,0 +1,60 @@
+#autoload
+
+# arguments:
+#  name of parameter containing file matches
+#  directory prefix
+# Sets array reply to result for caller.
+
+local stat f elt what
+local -a stylevals
+integer ok
+
+reply=()
+
+zmodload -i zsh/stat 2>/dev/null || return 1
+
+zstyle -a ":completion:${curcontext}:" file-list stylevals || return 1
+
+# TODO: more flexible way of handling the following?  e.g. use $compstate?
+case $WIDGETSTYLE in
+  (*complete*)
+  what=insert
+  ;;
+
+  (*)
+  what=list
+  ;;
+esac
+
+for elt in $stylevals; do
+  case $elt in
+    (($what|all|true|1|yes)=<->)
+    # use long format if no more than the given number of matches
+    (( ${(P)#1} <= ${elt##*=} )) && (( ok = 1 ))
+    break
+    ;;
+
+    ($what|all|true|1|yes)
+    # always use long format
+    (( ok = 1 ))
+    break
+    ;;
+  esac
+done
+
+(( ok )) || return 1
+
+for f in ${(P)1}; do
+  if [[ ! -e "${2:+$2/}$f" ]]; then
+    reply+=("${2:+$2/}$f")
+    continue
+  fi
+
+  # Borrowed from Functions/Example/zls
+  stat -s -H stat -F "%b %e %H:%M" - "${2:+$2/}$f" >/dev/null 2>&1
+
+  reply+=("$stat[mode] ${(l:3:)stat[nlink]} ${(r:8:)stat[uid]} \
+ ${(r:8:)stat[gid]} ${(l:8:)stat[size]} $stat[mtime] $f")
+done
+
+return 0
Index: Completion/Unix/Type/_path_files
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Type/_path_files,v
retrieving revision 1.24
diff -u -r1.24 _path_files
--- Completion/Unix/Type/_path_files	27 Apr 2004 17:00:23 -0000	1.24
+++ Completion/Unix/Type/_path_files	6 Mar 2006 12:59:50 -0000
@@ -7,6 +7,7 @@
 local tmp1 tmp2 tmp3 tmp4 i orig eorig pre suf tpre tsuf opre osuf cpre
 local pats haspats ignore pfx pfxsfx sopt gopt opt sdirs ignpar cfopt listsfx
 local nm=$compstate[nmatches] menu matcher mopts sort match mid accex fake
+local reply listopts
 
 typeset -U prepaths exppaths
 
@@ -539,6 +540,8 @@
               ( -n "$_comp_correct" ||
                 -z "$compstate[pattern_match]" || "$SUFFIX" != */* ||
                 "${SUFFIX#*/}" = (|*[^\\])[][*?#~^\|\<\>]* ) ]] }; then
+	# We have not been told to insert the match, so we are
+	# listing, or something.
         (( tmp4 )) && zstyle -t ":completion:${curcontext}:paths" ambiguous &&
             compstate[to_end]=
         if [[ "$tmp3" = */* ]]; then
@@ -554,12 +557,16 @@
 		    - "${(@)^tmp1%%/*}/${tmp3#*/}"
           fi
 	else
+	  _list_files tmp1 "$prepath$realpath$testpath"
+	  (( ${#reply} )) && listopts=(-d reply -l -o)
 	  compadd -Qf "$mopts[@]" -p "$linepath$tmp2" \
 	          -W "$prepath$realpath$testpath" \
 		   "$pfxsfx[@]" -M "r:|/=* r:|=*" \
+	           $listopts \
 		   -a tmp1
 	fi
       else
+	# We are inserting the match into the command line.
         if [[ "$tmp3" = */* ]]; then
 	  tmp4=( -Qf "$mopts[@]" -p "$linepath$tmp2"
 	         -W "$prepath$realpath$testpath"
@@ -576,9 +583,12 @@
 	    done
           fi
         else
+	  _list_files tmp1 "$prepath$realpath$testpath"
+	  (( ${#reply} )) && listopts=(-d reply -l -o)
 	  compadd -Qf "$mopts[@]" -p "$linepath$tmp2" \
                   -W "$prepath$realpath$testpath" \
 		  "$pfxsfx[@]" -M "r:|/=* r:|=*" \
+                  $listopts \
 		  -a tmp1
         fi
       fi
@@ -668,8 +678,11 @@
         compadd -Qf -W "$prepath$realpath" "$pfxsfx[@]" "$mopts[@]" \
                 -M "r:|/=* r:|=*" - "$linepath$tmp4${(@)^tmp1}"
       else
+	# Not a pattern match
+	_list_files tmp1 "$prepath$realpath$testpath"
+	(( ${#reply} )) && listopts=(-d reply -l -o)
         compadd -Qf -p "$linepath$tmp4" -W "$prepath$realpath$testpath" \
-	        "$pfxsfx[@]" "$mopts[@]" -M "r:|/=* r:|=*" -a tmp1
+	        "$pfxsfx[@]" "$mopts[@]" -M "r:|/=* r:|=*" $listopts -a tmp1
       fi
     fi
   fi
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.184
diff -u -r1.184 compsys.yo
--- Doc/Zsh/compsys.yo	6 Mar 2006 11:36:03 -0000	1.184
+++ Doc/Zsh/compsys.yo	6 Mar 2006 12:59:54 -0000
@@ -1263,6 +1263,30 @@
 required in the particular context.  Names for which no type is
 specified will always be completed.
 )
+kindex(file-list, completion style)
+item(tt(file-list))(
+This style controls with files completed using the standard builtin
+mechanism are to be listed with a long list similar to tt(ls -l)
+(although note that this feature actually uses the shell module
+tt(zsh/stat) for file information).
+
+The style may either be set to a true value (or `tt(all)'), or
+one of the values tt(insert) or tt(list), indicating that files
+are to be listed in long format in all circumstances, or when
+attempting to insert a file name, or when listing file names
+without attempting to insert one.
+
+More generally, the value may be an array of any of the above values,
+optionally followed by tt(=)var(num).  If var(num) is present it
+gives the maximum number of matches for which long listing style
+will be used.  For example,
+
+example(zstyle ':completion:*' file-list list=20 insert=10)
+
+specifies that long format will be used when listing up to 20 files
+or inserting a file with up to 10 matches, else short format will
+be used.
+)
 kindex(file-patterns, completion style)
 item(tt(file-patterns))(
 This is used by the standard function for completing filenames,
Index: Doc/Zsh/compwid.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compwid.yo,v
retrieving revision 1.37
diff -u -r1.37 compwid.yo
--- Doc/Zsh/compwid.yo	24 Apr 2005 18:38:04 -0000	1.37
+++ Doc/Zsh/compwid.yo	6 Mar 2006 12:59:55 -0000
@@ -436,7 +436,7 @@
 startitem()
 findex(compadd)
 cindex(completion widgets, adding specified matches)
-xitem(tt(compadd) [ tt(-akqQfenUl12C) ] [ tt(-F) var(array) ])
+xitem(tt(compadd) [ tt(-akqQfenUld12C) ] [ tt(-F) var(array) ])
 xitem([ tt(-P) var(prefix) ] [ tt(-S) var(suffix) ])
 xitem([ tt(-p) var(hidden-prefix) ] [ tt(-s) var(hidden-suffix) ])
 xitem([ tt(-i) var(ignored-prefix) ] [ tt(-I) var(ignored-suffix) ])
@@ -530,6 +530,12 @@
 option. If it is given, the display strings are listed one per line,
 not arrayed in columns.
 )
+item(tt(-o))(
+This option only has an effect if used together with the tt(-d)
+option.  If it is given, the order of the output is determined by the
+match strings;  otherwise it is determined by the display strings
+(i.e. the strings given by the tt(-d) option).
+)
 item(tt(-J) var(name))(
 Gives the name of the group of matches the words should be stored in.
 )
Index: Src/Zle/comp.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/comp.h,v
retrieving revision 1.14
diff -u -r1.14 comp.h
--- Src/Zle/comp.h	21 May 2002 08:07:51 -0000	1.14
+++ Src/Zle/comp.h	6 Mar 2006 12:59:55 -0000
@@ -131,6 +131,7 @@
 #define CMF_FMULT    (1<<12)	/* first of multiple equal strings */
 #define CMF_ALL      (1<<13)	/* a match representing all other matches */
 #define CMF_DUMMY    (1<<14)	/* unselectable dummy match */
+#define CMF_MORDER   (1<<15)    /* order by matches, not display strings */
 
 /* Stuff for completion matcher control. */
 
Index: Src/Zle/compcore.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/compcore.c,v
retrieving revision 1.82
diff -u -r1.82 compcore.c
--- Src/Zle/compcore.c	20 Feb 2006 05:51:49 -0000	1.82
+++ Src/Zle/compcore.c	6 Mar 2006 12:59:56 -0000
@@ -2735,7 +2735,7 @@
 static int
 matchcmp(Cmatch *a, Cmatch *b)
 {
-    if ((*a)->disp) {
+    if ((*a)->disp && !((*a)->flags & CMF_MORDER)) {
 	if ((*b)->disp) {
 	    if ((*a)->flags & CMF_DISPLINE) {
 		if ((*b)->flags & CMF_DISPLINE)
@@ -2751,7 +2751,7 @@
 	}
 	return -1;
     }
-    if ((*b)->disp)
+    if ((*b)->disp && !((*b)->flags & CMF_MORDER))
 	return 1;
 
     return strbpcmp(&((*a)->str), &((*b)->str));
Index: Src/Zle/complete.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/complete.c,v
retrieving revision 1.30
diff -u -r1.30 complete.c
--- Src/Zle/complete.c	18 Aug 2005 18:15:45 -0000	1.30
+++ Src/Zle/complete.c	6 Mar 2006 12:59:57 -0000
@@ -569,6 +569,9 @@
 	    case 'l':
 		dat.flags |= CMF_DISPLINE;
 		break;
+	    case 'o':
+		dat.flags |= CMF_MORDER;
+		break;
 	    case 'E':
                 if (p[1]) {
                     dat.dummies = atoi(p + 1);

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



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