zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: <zsh-users@zsh.org>
Subject: Re: An example of writing a custom history file?
Date: Mon, 15 Dec 2014 10:26:52 -0800	[thread overview]
Message-ID: <141215102652.ZM25198@torch.brasslantern.com> (raw)
In-Reply-To: <CANCp2gbheXK_et_xr55Tg3jyb9HmS8wtUOzz5cZpZu3SgcNOfA@mail.gmail.com>

On Dec 15, 12:05pm, Rocky Bernstein wrote:
}
} Making the change suggested, adding 1000 doesn't change the behavior - no
} file is written. Again, here is the entire 12-line program:
} 
}     #!/usr/bin/zsh
}     fc -ap /tmp/example_history 1000
} 
}     local line
}     # Read lines and add them to history
}     while vared -h -p "hey: " line
}     do
}         [[ $line == 'quit' ]] && exit 0
}         # The -s option below adds the line to the history
}         print -s $line
}         line=''
}     done

This worked for me exactly as written; I got lines saved in the
/tmp/example_history file.  However, I was working with an interactive
shell, which normally saves history at exit.  If you are trying to run
this as a stand-alone script, you probably don't want "exit 0" there.
More likely you just want to "break" and let the loop finish.  Also
if running standalone, there's no "function scope" so I suspect that
foils the usual action of "fc -a".

Try it like this:

    fc -ap /tmp/example_history 1000 1000

    local line
    # Read lines and add them to history
    while vared -h -p "hey: " line
    do
        [[ $line == 'quit' ]] && break
        # The -s option below adds the line to the history
        # The -R option avoids other "print" processing
        print -sR $line
        line=''
    done
    # Save/pop the pushed history
    fc -P

You might also try adding

    setopt localoptions incappendhistory

so that the lines are written to the file as soon as "print -s" adds
them, rather than waiting for the "fc -P".

} When the basic history mechanism isn't working, it doesn't help to pour
} over a 138-line program that performs several functions in one program,
} adds key bindings, beeps at the terminal, has color themes, and uses some
} sort of "{ } aways { }" construct that probably is a Zsh extension and not
} POSIX shell construct.

You asked to be pointed at an example; I can't point to something that
doesn't exist, so I pointed to something that does.

However, if you're writing a zsh debugger, presumably you need to know
about and potentially use zsh extensions?


  parent reply	other threads:[~2014-12-15 18:27 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-15  1:38 Rocky Bernstein
2014-12-15  2:53 ` Bart Schaefer
2014-12-15  7:09   ` Rocky Bernstein
2014-12-15  8:37     ` Bart Schaefer
2014-12-15 14:26       ` Rocky Bernstein
2014-12-15 16:14         ` Bart Schaefer
2014-12-15 17:05           ` Rocky Bernstein
2014-12-15 17:46             ` Peter Stephenson
2014-12-15 18:22             ` Mikael Magnusson
2014-12-16  3:58               ` Rocky Bernstein
2014-12-15 18:26             ` Bart Schaefer [this message]
2014-12-15 18:34               ` Mikael Magnusson
2014-12-16 11:36                 ` Peter Stephenson
2014-12-16  5:57               ` Rocky Bernstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=141215102652.ZM25198@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).