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

Re: Finding the first history event of the current instance



On Jun 1,  3:50am, Mikael Magnusson wrote:
}
} > } > The options -N (new), -O (old), and -F (foreign) are all unused
} > }
} > } I think I'd favour +L for foreign (i.e. not local). It's easier to
} > } remember that way.
} >
} > I don't think it'll affect that at all.  However, it will mean that
} > "fc +l" has the same meaning as "fc -l"
} 
} It's also a lot less discoverable by fc -<tab>. And maybe a bit
} confusing that neither -L nor +L would be the default.

OK, so here's another step in this direction.  I chose to overload the -I
option ("internal"/"incremental") rather than add -N or +L.  Turns out the
HIST_READ flag is set for anything read from a file, so its negation is
the criterion for "internal" (think of as "interactive") history.

Included are updates to _fc and to the doc (which has been reordered a
bit and references to "commands" mostly changed to "events").

I'm not sure how useful the inverses are.  Note that foreign history is
already marked with a "*" in the "fc -l" output unless line numbering is
suppressed with -n.  I suppose that could be mentioned in the doc, but I
didn't.


diff --git a/Completion/Zsh/Command/_fc b/Completion/Zsh/Command/_fc
index c298721..1589244 100644
--- a/Completion/Zsh/Command/_fc
+++ b/Completion/Zsh/Command/_fc
@@ -12,8 +12,10 @@ words=( "${(@)words[1,CURRENT-1]:#*=*}" "${(@)words[CURRENT,-1]}" )
 
 fc_common=(
   -s -S
-  '(-A -R -W -I -p -P)-r[reverse order of the commands]'
-  '(-A -R -W -I -e -p -P)-n[suppress line numbers]'
+  '(-A -R -W -p -P)-I[include internal (new) events only]'
+  '(-A -R -W -p -P)-L[include local events only]'
+  '(-A -R -W -p -P)-r[reverse order of the events]'
+  '(-A -R -W -e -p -P)-n[suppress line numbers]'
 )
 
 if [[ -n ${words[(r)-[pa](|[ap])]} ]]; then
@@ -32,20 +34,20 @@ else
 fi
 
 fc_hist=(
-  '(-A -R -W -I -a -p -P 2)-m[treat argument as a pattern]'
-  '(-A -R -W -I -e -f -E -i -t -a -p -P)-d[print time-stamps]'
-  '(-A -R -W -I -e -d -E -i -t -a -p -P)-f[mm/dd/yyyy format time-stamps]'
-  '(-A -R -W -I -e -d -f -i -t -a -p -P)-E[dd.mm.yyyy format time-stamps]'
-  '(-A -R -W -I -e -d -f -E -t -a -p -P)-i[yyyy-mm-dd format time-stamps]'
-  '(-A -R -W -I -e -d -f -E -i -a -p -P)-t[print time-stamps in specified format]:date format'
-  '(-A -R -W -I -e -a -p -P)-D[print elapsed times]'
+  '(-A -R -W -a -p -P 2)-m[treat argument as a pattern]'
+  '(-A -R -W -e -f -E -i -t -a -p -P)-d[print time-stamps]'
+  '(-A -R -W -e -d -E -i -t -a -p -P)-f[mm/dd/yyyy format time-stamps]'
+  '(-A -R -W -e -d -f -i -t -a -p -P)-E[dd.mm.yyyy format time-stamps]'
+  '(-A -R -W -e -d -f -E -t -a -p -P)-i[yyyy-mm-dd format time-stamps]'
+  '(-A -R -W -e -d -f -E -i -a -p -P)-t[print time-stamps in specified format]:date format'
+  '(-A -R -W -e -a -p -P)-D[print elapsed times]'
 
   '(-A -R -W -I -e -d -f -i -l -m -n -r -D -E -t -P)-a[with -p, automatically pop history on function return]'
   '(-A -R -W -I -e -d -f -i -l -m -n -r -D -E -t -P)-p[push current history to stack]'
   '(- *)-P[pop history from stack]'
 )
 
-fc_r='(-A -R -W -I -e)-l[list resulting commands on stdout]'
+fc_r='(-A -R -W -e)-l[list resulting commands on stdout]'
 
 case $service in
   history)
@@ -56,12 +58,11 @@ case $service in
   ;;
   *)
     _arguments "$fc_common[@]" "$fc_hist[@]" "$fc_r" \
-      '(-A -R -W -I -a -l -n -d -f -E -i -r -t -D -p -P)-e+[specify editor to invoke]:editor to invoke:_command_names -e' \
-      '(-a -l -m -e -r -n -d -f -t -E -i -R -D -A -W -p -P *)-'{\
+      '(-A -R -W -a -l -n -d -f -E -i -r -t -D -p -P)-e+[specify editor to invoke]:editor to invoke:_command_names -e' \
+      '(-a -l -L -m -e -r -n -d -f -t -E -i -R -D -A -W -p -P *)-'{\
 'R[read history from file]',\
 'A[append history to file]',\
-'W[write history to file]',\
-'I[read/write new events only]'} && ret=0
+'W[write history to file]'} && ret=0
   ;;
 esac
 
diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 1fcc7c2..2c55fab 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -662,8 +662,8 @@ findex(fc)
 cindex(history, editing)
 cindex(editing history)
 redef(SPACES)(0)(tt(ifztexi(NOTRANS(@ @ @ @ @ @ ))ifnztexi(      )))
-xitem(tt(fc) [ tt(-e) var(ename) ] [-L] [ tt(-m) var(match) ] [ var(old)tt(=)var(new) ... ] [ var(first) [ var(last) ] ])
-xitem(tt(fc -l )[ tt(-LnrdfEiD) ] [ tt(-t) var(timefmt) ] [ tt(-m) var(match) ])
+xitem(tt(fc) [ tt(-e) var(ename) ] [ tt(-LI) ] [ tt(-m) var(match) ] [ var(old)tt(=)var(new) ... ] [ var(first) [ var(last) ] ])
+xitem(tt(fc -l )[ tt(-LI) ] [ tt(-nrdfEiD) ] [ tt(-t) var(timefmt) ] [ tt(-m) var(match) ])
 xitem(SPACES()[ var(old)tt(=)var(new) ... ] [ var(first) [ var(last) ] ])
 xitem(tt(fc -p )[ tt(-a) ] [ var(filename) [ var(histsize) [ var(savehistsize) ] ] ])
 xitem(tt(fc) tt(-P))
@@ -674,31 +674,24 @@ shell is interactive.  Usually this is detected automatically, but
 it can be forced by setting the tt(interactive) option when starting the
 shell.
 
-Select a range of commands from var(first) to var(last) from the
-history list.
-The arguments var(first) and var(last) may be specified as a
-number or as a string.  A negative number is used as an offset
-to the current history event number.
-A string specifies the most recent event beginning with the given string.
-All substitutions var(old)tt(=)var(new), if any, are then performed
-on the commands.
+The first two forms of this command select a range of events from
+var(first) to var(last) from the history list.  The arguments var(first)
+and var(last) may be specified as a number or as a string.  A negative
+number is used as an offset to the current history event number.  A string
+specifies the most recent event beginning with the given string.  All
+substitutions var(old)tt(=)var(new), if any, are then performed on the
+text of the events.
 
-If the tt(-L) flag is given, only the local history is considered (see
+In addition to the the number range,
+startsitem()
+sitem(tt(-I))(restricts to only internal events (not from tt($HISTFILE)))
+sitem(tt(-L))(restricts to only local events (not from other shells, see
 tt(SHARE_HISTORY) in ifzman(zmanref(zshoptions))\
-ifnzman(noderef(Description of Options))).
-If the tt(-m) flag is given, the first argument is taken as a
-pattern (should be quoted) and only the history events matching this
-pattern are considered.
-
-When the tt(-l) flag is given, the resulting commands are listed on
-standard output.
-Otherwise the editor program var(ename) is invoked on a file containing
-these history events.  If var(ename) is not given, the value
-of the parameter tt(FCEDIT) is used; if that is not set the value of the
-parameter tt(EDITOR) is used; if that is not set a builtin default, usually
-`tt(vi)' is used.  If var(ename) is `tt(-)',
-no editor is invoked.  When editing is complete, the edited
-command is executed.
+ifnzman(noderef(Description of Options)) -- note that tt($HISTFILE) is
+considered local when read at startup))
+sitem(tt(-m))(takes the first argument as a pattern (should be quoted) and
+only the history events matching this pattern are considered)
+endsitem()
 
 If var(first) is not specified, it will be set to -1 (the most recent
 event), or to -16 if the tt(-l) flag is given.
@@ -708,12 +701,20 @@ However, if the current event has added entries to the history with
 `tt(print -s)' or `tt(fc -R)', then the default var(last) for tt(-l)
 includes all new history entries since the current event began.
 
-The flag tt(-r) reverses the order of the commands and the
-flag tt(-n) suppresses command numbers when listing.
+When the tt(-l) flag is given, the resulting events are listed on
+standard output.  Otherwise the editor program var(ename) is invoked on a
+file containing these history events.  If var(ename) is not given, the
+value of the parameter tt(FCEDIT) is used; if that is not set the value of
+the parameter tt(EDITOR) is used; if that is not set a builtin default,
+usually `tt(vi)' is used.  If var(ename) is `tt(-)', no editor is invoked.
+When editing is complete, the edited command is executed.
+
+The flag tt(-r) reverses the order of the events and the
+flag tt(-n) suppresses event numbers when listing.
 
 Also when listing,
 startsitem()
-sitem(tt(-d))(prints timestamps for each command)
+sitem(tt(-d))(prints timestamps for each event)
 sitem(tt(-f))(prints full time-date stamps in the US
 `var(MM)tt(/)var(DD)tt(/)var(YY) var(hh):var(mm)' format)
 sitem(tt(-E))(prints full time-date stamps in the European
@@ -725,15 +726,14 @@ var(fmt) is formatted with the strftime function with the zsh extensions
 described for the tt(%D{)var(string)tt(}) prompt format in
 ifzman(the section EXPANSION OF PROMPT SEQUENCES in zmanref(zshmisc))\
 ifnzman(noderef(Prompt Expansion)).  The resulting formatted string must be
-no more than 256 characters or will not be printed.
+no more than 256 characters or will not be printed
 )
 sitem(tt(-D))(prints elapsed times; may be combined with one of the
-options above.)
+options above)
 endsitem()
 
 cindex(history, stack)
 cindex(stack, history)
-
 `tt(fc -p)' pushes the current history list onto a stack and switches to a
 new history list.  If the tt(-a) option is also specified, this history list
 will be automatically popped when the current function scope is exited, which
diff --git a/Src/builtin.c b/Src/builtin.c
index 9358e8b..cfa6bc2 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -104,7 +104,7 @@ static struct builtin builtins[] =
     BUILTIN("pushd", BINF_SKIPINVALID | BINF_SKIPDASH | BINF_DASHDASHVALID, bin_cd, 0, 2, BIN_PUSHD, "qsPL", NULL),
     BUILTIN("pushln", 0, bin_print, 0, -1, BIN_PRINT, NULL, "-nz"),
     BUILTIN("pwd", 0, bin_pwd, 0, 0, 0, "rLP", NULL),
-    BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "nrlL", NULL),
+    BUILTIN("r", 0, bin_fc, 0, -1, BIN_R, "IlLnr", NULL),
     BUILTIN("read", 0, bin_read, 0, -1, 0, "cd:ek:%lnpqrst:%zu:AE", NULL),
     BUILTIN("readonly", BINF_PLUSOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AE:%F:%HL:%R:%TUZ:%afghi:%lptux", "r"),
     BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
@@ -1435,10 +1435,6 @@ bin_fc(char *nam, char **argv, Options ops, int func)
 	unqueue_signals();
 	return 0;
     }
-    if (OPT_ISSET(ops,'I')) {
-	zwarnnam(nam, "-I requires one of -R/-W/-A");
-	return 1;
-    }
 
     if (zleactive) {
 	zwarnnam(nam, "no interactive history within ZLE");
@@ -1672,7 +1668,7 @@ static int
 fclist(FILE *f, Options ops, zlong first, zlong last,
        struct asgment *subs, Patprog pprog, int is_command)
 {
-    int fclistdone = 0;
+    int fclistdone = 0, xflags = 0;
     zlong tmp;
     char *s, *tdfmt, *timebuf;
     Histent ent;
@@ -1722,11 +1718,19 @@ fclist(FILE *f, Options ops, zlong first, zlong last,
 	tdfmt = timebuf = NULL;
     }
 
+    /* xflags exclude events */
+    if (OPT_ISSET(ops,'L')) {
+	xflags |= HIST_FOREIGN;
+    }
+    if (OPT_ISSET(ops,'I')) {
+	xflags |= HIST_READ;
+    }
+
     for (;;) {
-	if (!OPT_ISSET(ops,'L') || !(ent->node.flags & HIST_FOREIGN))
-	    s = dupstring(ent->node.nam);
-	else
+	if (ent->node.flags & xflags)
 	    s = NULL;
+	else
+	    s = dupstring(ent->node.nam);
 	/* this if does the pattern matching, if required */
 	if (s && (!pprog || pattry(pprog, s))) {
 	    /* perform substitution */
@@ -1782,7 +1786,7 @@ fclist(FILE *f, Options ops, zlong first, zlong last,
     if (!fclistdone) {
 	if (subs)
 	    zwarnnam("fc", "no substitutions performed");
-	else if (OPT_ISSET(ops,'L') || pprog)
+	else if (xflags || pprog)
 	    zwarnnam("fc", "no matching events found");
 	return 1;
     }

-- 
Barton E. Schaefer



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