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

Re: An example of writing a custom history file?



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?



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