@@ -90,6 +90,8 @@ defmodule IEx do
9090
9191 """
9292
93+ alias IEx. Util
94+
9395 @doc """
9496 Registers a function to be invoked after IEx process is spawned.
9597 "" "
@@ -150,6 +152,7 @@ defmodule IEx do
150152
151153 set_expand_fun()
152154 run_after_spawn()
155+ config = load_dot_iex(config)
153156 IEx.Server.start(config)
154157 end
155158 end
@@ -201,4 +204,56 @@ defmodule IEx do
201204 defp run_after_spawn do
202205 lc fun inlist Enum.reverse(after_spawn), do: fun.()
203206 end
207+
208+ # Locates and loads an .iex file from one of predefined locations
209+ #
210+ # Sample contents of a local .iex file:
211+ #
212+ # IEx.source " ~/. iex " # source another .iex file
213+ # IO.puts " hello world " # print something before the shell starts
214+ # value = 13 # bind a variable that'll be accessible in the shell
215+ #
216+ # Running the shell then results in
217+ #
218+ # $ iex
219+ # Erlang R15B03 (erts-5.9.3.1) ...
220+ #
221+ # hello world
222+ # Interactive Elixir (0.8.3.dev) - press Ctrl+C to exit (type h() ENTER for help)
223+ # iex(1)> value
224+ # 13
225+ #
226+ defp load_dot_iex(config) do
227+ path = Enum.find [" . iex ", " ~/. iex "], File.regular?(&1)
228+ if nil?(path) do
229+ config
230+ else
231+ try do
232+ code = File.read!(path)
233+
234+ # Evaluate the contents in the same environment IEx.Server will run in
235+ { _result, binding, scope } = :elixir.eval(:unicode.characters_to_list(code), config.binding, 0, config.scope)
236+ config.binding(binding).scope(scope)
237+ rescue
238+ exception ->
239+ Util.print_exception(exception)
240+ :erlang.halt()
241+ catch
242+ kind, error ->
243+ Util.print_error(kind, error)
244+ :erlang.halt()
245+ end
246+ end
247+ end
248+
249+ @doc " ""
250+ Convenience function for use in . iex files . Converts `path` to an absolute
251+ path ( also expanding ~ if present ) and requires the file at resulting path .
252+
253+ In case of an error , prints exception info to stdout and terminates the shell .
254+ "" "
255+ def source(path) do
256+ p = Path.expand(path)
257+ Code.require_file(p)
258+ end
204259end
0 commit comments