vars.d.elv 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #elvdoc:fn add-var
  2. #
  3. # ```elvish
  4. # edit:add-var $name $init
  5. # ```
  6. #
  7. # Defines a new variable in the interactive REPL with an initial value. The new variable becomes
  8. # available during the next REPL cycle.
  9. #
  10. # Equivalent to running `var $name = $init` at a REPL prompt, but `$name` can be
  11. # dynamic.
  12. #
  13. # This is most useful for modules to modify the REPL namespace. Example:
  14. #
  15. # ```elvish-transcript
  16. # ~> cat .config/elvish/lib/a.elv
  17. # for i [(range 10)] {
  18. # edit:add-var foo$i $i
  19. # }
  20. # ~> use a
  21. # ~> put $foo1 $foo2
  22. # ▶ (num 1)
  23. # ▶ (num 2)
  24. # ```
  25. #
  26. # Note that if you use a variable as the `$init` argument, `edit:add-var`
  27. # doesn't add the variable "itself" to the REPL namespace. The variable in the
  28. # REPL namespace will have the initial value set to the variable's value, but
  29. # it is not an alias of the original variable:
  30. #
  31. # ```elvish-transcript
  32. # ~> cat .config/elvish/lib/b.elv
  33. # var foo = foo
  34. # edit:add-var foo $foo
  35. # ~> use b
  36. # ~> put $foo
  37. # ▶ foo
  38. # ~> set foo = bar
  39. # ~> echo $b:foo
  40. # foo
  41. # ```
  42. #
  43. # ### Importing definition from a module into the REPL
  44. #
  45. # One common use of this command is to put the definitions of functions intended for REPL use in a
  46. # module instead of your [`rc.elv`](command.html#rc-file). For example, if you want to define `ll`
  47. # as `ls -l`, you can do so in your `rc.elv` directly:
  48. #
  49. # ```elvish
  50. # fn ll {|@a| ls -l $@a }
  51. # ```
  52. #
  53. # But if you move the definition into a module (say `util.elv` in one of the
  54. # [module search directories](command.html#module-search-directories), this
  55. # function can only be used as `util:ll` (after `use util`). To make it usable
  56. # directly as `ll`, you can add the following to `util.elv`:
  57. #
  58. # ```elvish
  59. # edit:add-var ll~ $ll~
  60. # ```
  61. #
  62. # ### Conditionally importing a module
  63. #
  64. # Another use case is to add a module or function to the REPL namespace
  65. # conditionally. For example, to only import [the `unix` module](unix.html)
  66. # when actually running on UNIX, a straightforward solution is to do the
  67. # following in `rc.elv`:
  68. #
  69. # ```elvish
  70. # use platform
  71. # if $platform:is-unix {
  72. # use unix
  73. # }
  74. # ```
  75. #
  76. # This doesn't work however, since what `use` does is introducing a variable
  77. # named `$unix:`. Since all variables in Elvish are lexically scoped, the
  78. # `$unix:` variable is only valid inside the `if` block.
  79. #
  80. # This can be fixed by explicitly introducing the `$unix:` variable to the REPL
  81. # namespace. The following works both from `rc.elv` and from a module:
  82. #
  83. # ```elvish
  84. # use platform
  85. # if $platform:is-unix {
  86. # use unix
  87. # edit:add-var unix: $unix:
  88. # }
  89. # ```
  90. #elvdoc:fn add-vars
  91. #
  92. # ```elvish
  93. # edit:add-vars $map
  94. # ```
  95. #
  96. # Takes a map from strings to arbitrary values. Equivalent to calling
  97. # `edit:add-var` for each key-value pair in the map.