zsh-workers
 help / color / mirror / code / Atom feed
* Re: Finding the first history event of the current instance
       [not found]   ` <150530154236.ZM17511@torch.brasslantern.com>
@ 2015-05-31  9:29     ` Oliver Kiddle
  2015-05-31 23:59       ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Oliver Kiddle @ 2015-05-31  9:29 UTC (permalink / raw)
  To: Zsh workers

Bart wrote:
> } Or there's this patch.  I didn't add an option for listing only the

I know I've missed fc's ability to do this so that definitely looks
useful.

When it gets finalised, don't forget _fc.

> The options -N (new), -O (old), and -F (foreign) are all currently unused
> by "fc" if we wanted to add multiple filterings.

I think I'd favour +L for foreign (i.e. not local). It's easier to
remember that way. I'm not sure how the option parser works but that may
change the meaning of fc + which isn't ideal.

Rather than using the HIST_OLD or whatever flags, is it possible to
instead identify what the history number was when the shell started?

For new/old, one option would be to have a special token such as "-",
".", "^" or "0" that can be used to indicate the history number
when the shell started. It might need to be inclusive when used at the
start of the range and exclusive when used at the end so not exactly the
same as the number. So for example, this would be all old:
  fc -l 1 -
And this would be all new:
  fc -l -
Admittedly, this doesn't allow you to get a history entry matching a
pattern from the old entries.

Oliver


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

* Re: Finding the first history event of the current instance
  2015-05-31  9:29     ` Finding the first history event of the current instance Oliver Kiddle
@ 2015-05-31 23:59       ` Bart Schaefer
  2015-06-01  1:50         ` Mikael Magnusson
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2015-05-31 23:59 UTC (permalink / raw)
  To: Zsh workers

On May 31, 11:29am, Oliver Kiddle wrote:
} Subject: Re: Finding the first history event of the current instance
}
} > The options -N (new), -O (old), and -F (foreign) are all currently unused
} > by "fc" if we wanted to add multiple filterings.
} 
} I think I'd favour +L for foreign (i.e. not local). It's easier to
} remember that way.

That'd be fine by me, but read on.

} I'm not sure how the option parser works but that may
} change the meaning of fc + which isn't ideal.

I don't think it'll affect that at all.  However, it will mean that
"fc +l" has the same meaning as "fc -l", unless we do a lot of special
processing.  (Same for +r/-r etc.)  So maybe we don't want that.

} For new/old, one option would be to have a special token such as "-",
} ".", "^" or "0" that can be used to indicate the history number
} when the shell started.

What if someone loads (or reloads) history with "fc -R"?  Ought those
be considered "new" in the current shell?

} Admittedly, this doesn't allow you to get a history entry matching a
} pattern from the old entries.

You can use -m with a range:

schaefer<502> fc -l -m echo\* 101 200
fc: no matching events found
schaefer<503> fc -l -m echo\* 201 300
  250  echo $CPATH



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

* Re: Finding the first history event of the current instance
  2015-05-31 23:59       ` Bart Schaefer
@ 2015-06-01  1:50         ` Mikael Magnusson
  2015-06-01  4:02           ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Mikael Magnusson @ 2015-06-01  1:50 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh workers

On Mon, Jun 1, 2015 at 1:59 AM, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On May 31, 11:29am, Oliver Kiddle wrote:
> } Subject: Re: Finding the first history event of the current instance
> }
> } > The options -N (new), -O (old), and -F (foreign) are all currently unused
> } > by "fc" if we wanted to add multiple filterings.
> }
> } I think I'd favour +L for foreign (i.e. not local). It's easier to
> } remember that way.
>
> That'd be fine by me, but read on.
>
> } I'm not sure how the option parser works but that may
> } change the meaning of fc + which isn't ideal.
>
> I don't think it'll affect that at all.  However, it will mean that
> "fc +l" has the same meaning as "fc -l", unless we do a lot of special
> processing.  (Same for +r/-r etc.)  So maybe we don't want that.

It's also a lot less discoverable by fc -<tab>. And maybe a bit
confusing that neither -L nor +L would be the default.

-- 
Mikael Magnusson


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

* Re: Finding the first history event of the current instance
  2015-06-01  1:50         ` Mikael Magnusson
@ 2015-06-01  4:02           ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2015-06-01  4:02 UTC (permalink / raw)
  To: Zsh workers

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


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

* Re: Finding the first history event of the current instance
       [not found] ` <150529171657.ZM15077__24226.5334389687$1432945137$gmane$org@torch.brasslantern.com>
@ 2015-06-05  8:47   ` Christian Neukirchen
  2015-06-06 16:54     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Neukirchen @ 2015-06-05  8:47 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer <schaefer@brasslantern.com> writes:

> On May 23,  4:22pm, Christian Neukirchen wrote:
> }
> } How can I list *only* the history lines that I typed into the current
> } session?  For this, it would be enough to find the first event of the
> } new instance, but $HISTCMD is not set at .zshrc yet.
>
> I presume this means you have INC_APPEND_HISTORY but not SHARE_HISTORY.

Exactly.  I just realized this already landed in zsh 5.0.8, sorry I
could not test it before.

It doesn't seem to work here:

juno ~% zsh
juno ~% fc -L -l
 8986  history -iDL
 8987  man zshall
 8988  zman history
 8989  fc -liD
 8990  fc -liDL
 8991  fc -liD -L
 8992  fc -lL
 8993  fc -lLI
 8994  zman fc
 8995  fc -L -l
 8996  fc -l
 8997  date
 8998  fc -l
 8999  fc -l -L
 9000  less .zshrc
 9001  zsh

I'd have expected it to print nothing, because the local shell history
is empty?

-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org


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

* Re: Finding the first history event of the current instance
  2015-06-05  8:47   ` Christian Neukirchen
@ 2015-06-06 16:54     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2015-06-06 16:54 UTC (permalink / raw)
  To: zsh-workers

On Jun 5, 10:47am, Christian Neukirchen wrote:
}
} I'd have expected it to print nothing, because the local shell history
} is empty?

The documentation was updated after the 5.0.8 release to clarify this:

    -L
          restricts to only local events (not from other shells, see
          SHARE_HISTORY in *Note Description of Options:: - note that
          $HISTFILE is considered local when read at startup)

At the same time, this was added:

    -I
          restricts to only internal events (not from $HISTFILE)

You want the latter, but unfortunately 5.0.8 doesn't have it.


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

end of thread, other threads:[~2015-06-06 16:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <87r3q7nzkr.fsf@gmail.com>
     [not found] ` <150529171657.ZM15077@torch.brasslantern.com>
     [not found]   ` <150530154236.ZM17511@torch.brasslantern.com>
2015-05-31  9:29     ` Finding the first history event of the current instance Oliver Kiddle
2015-05-31 23:59       ` Bart Schaefer
2015-06-01  1:50         ` Mikael Magnusson
2015-06-01  4:02           ` Bart Schaefer
     [not found] ` <150529171657.ZM15077__24226.5334389687$1432945137$gmane$org@torch.brasslantern.com>
2015-06-05  8:47   ` Christian Neukirchen
2015-06-06 16:54     ` Bart Schaefer

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