+ }
+ $d;
+}
+
+sub list {
+ my ($lei, $pfx) = @_;
+ my $lss_dir = $lei->share_path.'/saved-searches';
+ return () unless -d $lss_dir;
+ # TODO: persist the cache? Use another format?
+ my $f = $lei->cache_dir."/saved-tmp.$$.".time.'.config';
+ open my $fh, '>', $f or die "open $f: $!";
+ print $fh "[include]\n";
+ for my $p (glob("$lss_dir/*/lei.saved-search")) {
+ print $fh "\tpath = ", cquote_val($p), "\n";
+ }
+ close $fh or die "close $f: $!";
+ my $cfg = PublicInbox::Config->git_config_dump($f);
+ unlink($f);
+ my $out = $cfg->get_all('lei.q.output') or return ();
+ map {;
+ s!$LOCAL_PFX!!;
+ $_;
+ } @$out
+}
+
+sub translate_dedupe ($$$) {
+ my ($self, $lei, $dd) = @_;
+ $dd //= 'content';
+ return 1 if $dd eq 'content'; # the default
+ return $self->{"-dedupe_$dd"} = 1 if ($dd eq 'oid' || $dd eq 'mid');
+ $lei->fail("--dedupe=$dd unsupported with --save");
+}
+
+sub up { # updating existing saved search via "lei up"
+ my ($cls, $lei, $dst) = @_;
+ my $f;
+ my $self = bless { ale => $lei->ale }, $cls;
+ my $dir = $dst;
+ output2lssdir($self, $lei, \$dir, \$f) or
+ return $lei->fail("--save was not used with $dst cwd=".
+ $lei->rel2abs('.'));
+ $self->{-cfg} = PublicInbox::Config->git_config_dump($f);
+ $self->{-ovf} = "$dir/over.sqlite3";
+ $self->{'-f'} = $f;
+ $self->{lock_path} = "$self->{-f}.flock";
+ $self;
+}
+
+sub new { # new saved search "lei q --save"
+ my ($cls, $lei) = @_;
+ my $self = bless { ale => $lei->ale }, $cls;
+ my $dst = $lei->{ovv}->{dst};
+ my $dir = lss_dir_for($lei, \$dst);
+ require File::Path;
+ File::Path::make_path($dir); # raises on error
+ $self->{-cfg} = {};
+ my $f = $self->{'-f'} = "$dir/lei.saved-search";
+ my $dd = $lei->{opt}->{dedupe};
+ translate_dedupe($self, $lei, $dd) or return;
+ open my $fh, '>', $f or return $lei->fail("open $f: $!");
+ my $sq_dst = PublicInbox::Config::squote_maybe($dst);
+ my $q = $lei->{mset_opt}->{q_raw} // die 'BUG: {q_raw} missing';
+ if (ref $q) {
+ $q = join("\n", map { "\tq = ".cquote_val($_) } @$q);
+ } else {
+ $q = "\tq = ".cquote_val($q);
+ }
+ $dst = "$lei->{ovv}->{fmt}:$dst" if $dst !~ m!\Aimaps?://!i;
+ print $fh <<EOM;
+; to refresh with new results, run: lei up $sq_dst
+[lei]
+$q
+[lei "q"]
+ output = $dst
+EOM
+ print $fh "\tdedupe = $dd\n" if $dd;
+ for my $k (ARRAY_FIELDS) {
+ my $ary = $lei->{opt}->{$k} // next;
+ for my $x (@$ary) {
+ print $fh "\t$k = ".cquote_val($x)."\n";