zsh-users
 help / color / mirror / code / Atom feed
* re-source rc atomically
@ 2014-07-23 12:02 shawn wilson
  2014-07-23 12:56 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: shawn wilson @ 2014-07-23 12:02 UTC (permalink / raw)
  To: Zsh Users

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.


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

* Re: re-source rc atomically
  2014-07-23 12:02 re-source rc atomically shawn wilson
@ 2014-07-23 12:56 ` Peter Stephenson
  2014-07-23 13:56   ` shawn wilson
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2014-07-23 12:56 UTC (permalink / raw)
  To: Zsh Users

On Wed, 23 Jul 2014 08:02:27 -0400
shawn wilson <ag4ve.us@gmail.com> 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


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

* Re: re-source rc atomically
  2014-07-23 12:56 ` Peter Stephenson
@ 2014-07-23 13:56   ` shawn wilson
  2014-07-23 14:42     ` Павлов Николай Александрович
  0 siblings, 1 reply; 5+ messages in thread
From: shawn wilson @ 2014-07-23 13:56 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

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@samsung.com> wrote:
> On Wed, 23 Jul 2014 08:02:27 -0400
> shawn wilson <ag4ve.us@gmail.com> 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


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

* Re: re-source rc atomically
  2014-07-23 13:56   ` shawn wilson
@ 2014-07-23 14:42     ` Павлов Николай Александрович
  2014-07-23 16:05       ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Павлов Николай Александрович @ 2014-07-23 14:42 UTC (permalink / raw)
  To: shawn wilson, Peter Stephenson; +Cc: Zsh Users

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

On July 23, 2014 5:56:29 PM GMT+03:00, shawn wilson <ag4ve.us@gmail.com> 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@samsung.com> wrote:
>> On Wed, 23 Jul 2014 08:02:27 -0400
>> shawn wilson <ag4ve.us@gmail.com> 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-----


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

* Re: re-source rc atomically
  2014-07-23 14:42     ` Павлов Николай Александрович
@ 2014-07-23 16:05       ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-07-23 16:05 UTC (permalink / raw)
  To: Zsh Users

On Jul 23,  6:42pm, 0x041F0x04300x04320x043B0x043E0x0432 0x041D0x04380 wrote:
}
} 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.

The array is a relatively recent addition to zsh, and it avoids a few
steps of explanation to refer directly to the precmd function.

} [The array] 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.

Since we're moving past the "put this in precmd" shorthand ... that
problem can be avoided by using the add-zsh-hook utility.

    autoload -Uz add-zsh-hook
    add-zsh-hook precmd reload-zshrc

(assuming an appropriate reload-zshrc function was already defined).

-- 
Barton E. Schaefer


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

end of thread, other threads:[~2014-07-23 16:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-23 12:02 re-source rc atomically shawn wilson
2014-07-23 12:56 ` Peter Stephenson
2014-07-23 13:56   ` shawn wilson
2014-07-23 14:42     ` Павлов Николай Александрович
2014-07-23 16:05       ` 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).