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

Re: re-source rc atomically



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On July 23, 2014 5:56:29 PM GMT+03:00, shawn wilson <ag4ve.us@xxxxxxxxx> wrote:
>Second time in a row precmd is a big part of the solution to my
>problem - maybe I'll remember this next time :)
>
>Thanks
>
>On Wed, Jul 23, 2014 at 8:56 AM, Peter Stephenson
><p.stephenson@xxxxxxxxxxx> wrote:
>> On Wed, 23 Jul 2014 08:02:27 -0400
>> shawn wilson <ag4ve.us@xxxxxxxxx> wrote:
>>> Is there a way to source my zshrc somewhat atomically?
>>>
>>> I wouldn't want to do this to all of my sessions in case I mess
>>> something up. But it would be nice if this were re-sourced in most
>as
>>> automatic as possible so that when I change an alias or add a new
>>> function I don't have to remember what was added when and manually
>>> source my config when something doesn't work as expected.
>>
>> I guess you know about ". ~/.zshrc" and are asking if there's a way
>to
>> do that without you having to type it.
>>
>> You can do stuff like this.  Use the array precmd_functions if you
>> have other things in precmd.
>>
>> precmd() {
>>    local -a stat
>>    integer last_change
>>    zmodload -F zsh/stat b:zstat
>>    zstat -A stat +mtime ~/.zshrc
>>    last_change=$stat[1]
>>    if [[ -n $ZSHRC_LAST_CHANGE && last_change -gt ZSHRC_LAST_CHANGE
>]]; then
>>      # We don't want this to occur in function scope.
>>      trap '. ~/.zshrc' EXIT
>>    fi
>>    typeset -ig ZSHRC_LAST_CHANGE
>>    ZSHRC_LAST_CHANGE=last_change
>> }
>>
>>
>> You'd better make sure .zshrc doesn't define a conflicting precmd(),
>of
>> course (as mine just did...)
>>
>> You could loop over other functions if you wanted.
>>
>> pws

There is precmd_functions array. I do not know why each first advice suggests using precmd function in place of just adding an item to this array. It has two disadvantages regarding your case though:

1. You need to guard add against double inclusion if you want to be able to resource your zshrc.
2. You cannot rename your function and just resource zshrc: this way two functions will be there in the array.

If you clear the array in zshrc you may trade this to one rather minor disadvantage: you need to make sure this array will be correctly repopulated on resourcing.
-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQJNBAEBCgA3BQJTz8nKMBwfMDI7PjIgHTg6PjswOSAQOzU6QTA9NEA+MjhHIDxr
cC1wYXZAeWFuZGV4LnJ1PgAKCRBu+P2/AXZZIhUgD/4zwDlATRUSjeHLErtVUX37
n3rneuto6ZWWOn1lMNFZX5y0BMqsyr9lLevTQ3jWDpdR1Ht72tryr2IHNiQXcsZv
qoy7ao+YubZoFlStEICm3uDi+EgKnnIBgzkfLYICsUBfbD1kA4MjhZmN1CsHL89Q
l6JailXsqOL/ffYK+QtnoDGUoI9TWeFidiHnkf68kqLhyA3xVVdK7tysfqE/e9v6
aGy6aYs6wm/e8hzvWtnNJWImLsPnh+PPsyz/k760h8piU52eSwRpaFKPXRpFLK1i
ZDBQR4oJA0GjGtQ2ZSvE5HY84ZRH7NaYFFCAegzK12zQbGt5gNxOrdM8pGM2bUJO
RcGLtJYRz0v8wDFS+VHMCu3kp3ew81/OETeairOLyAPv68m72sxeBUzr8Fj0Zjd4
kRrUu4oA0qkf2LjYivXm7kdh/w7kK/OYlZaX0S5ZnixookapqwquHTTKA7XRuWtQ
W9Nm2FESEMRfTgUDbHo1/cvBj+L9dT6vXxUdtYKm1DP3G+QCQ7HthszrMXhRDdUa
HlzAancmNR1CHjPftcdBv4NJXMYjY2imaH4/XHiM0rjsKvvTBwCdorDrZNc8u8O8
VkvPocKb/HBSt019JcitBRzkAWRVLdSuatb1M6a0x9ZMrws6Nde7AgkcBthAGjEF
eiNCaN/akNW5SR9MQ+qnxw==
=0t6A
-----END PGP SIGNATURE-----



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