11defmodule IEx.Server do
22 @ moduledoc false
33
4- alias IEx.Util
5-
64 @ doc """
75 Eval loop for an IEx session. Its responsibilities include:
86
@@ -15,7 +13,9 @@ defmodule IEx.Server do
1513 IO . puts "Interactive Elixir (#{ System . version } ) - press Ctrl+C to exit (type h() ENTER for help)"
1614 Process . put :iex_history , [ ]
1715 { _ , _ , scope } = :elixir . eval ( 'require IEx.Helpers' , [ ] , 0 , config . scope )
18- do_loop ( config . scope ( scope ) )
16+ config = config . scope ( scope )
17+ config = load_dot_iex ( config )
18+ do_loop ( config )
1919 end
2020
2121 defp do_loop ( config ) do
@@ -28,11 +28,11 @@ defmodule IEx.Server do
2828 eval ( code , line , counter , config )
2929 rescue
3030 exception ->
31- Util . print_exception ( exception , System . stacktrace )
31+ print_exception ( exception , System . stacktrace )
3232 config . cache ( '' )
3333 catch
3434 kind , error ->
35- Util . print_error ( kind , error , System . stacktrace )
35+ print_error ( kind , error , System . stacktrace )
3636 config . cache ( '' )
3737 end
3838
@@ -88,6 +88,31 @@ defmodule IEx.Server do
8888 end
8989 end
9090
91+ # Locates and loads an .iex file from one of predefined locations. Returns
92+ # new config.
93+ defp load_dot_iex ( config ) do
94+ path = Enum . find [ ".iex" , "~/.iex" ] , fn path -> File . regular? ( Path . expand ( path ) ) end
95+ if nil? ( path ) do
96+ config
97+ else
98+ try do
99+ code = File . read! ( path )
100+
101+ # Evaluate the contents in the same environment do_loop will run in
102+ { _result , binding , scope } = :elixir . eval ( :unicode . characters_to_list ( code ) , config . binding , 0 , config . scope )
103+ config . binding ( binding ) . scope ( scope )
104+ rescue
105+ exception ->
106+ print_exception ( exception , System . stacktrace )
107+ System . halt ( 1 )
108+ catch
109+ kind , error ->
110+ print_error ( kind , error , System . stacktrace )
111+ System . halt ( 1 )
112+ end
113+ end
114+ end
115+
91116 defp update_history ( config ) do
92117 current = Process . get :iex_history
93118 Process . put :iex_history , [ config | current ]
@@ -113,7 +138,33 @@ defmodule IEx.Server do
113138 IO . puts :stdio , IO.ANSI . escape ( "%{yellow}#{ inspect ( result , IEx . inspect_opts ) } " )
114139 end
115140
141+ defp io_error ( result ) do
142+ IO . puts :stdio , result
143+ end
144+
116145 defp remote_prefix do
117146 if node == node ( :erlang . group_leader ) , do: "iex" , else: "rem"
118147 end
148+
149+ defp print_exception ( exception , stacktrace ) do
150+ print_stacktrace stacktrace , fn ->
151+ "** (#{ inspect exception . __record__ ( :name ) } ) #{ exception . message } "
152+ end
153+ end
154+
155+ defp print_error ( kind , reason , stacktrace ) do
156+ print_stacktrace stacktrace , fn ->
157+ "** (#{ kind } ) #{ inspect ( reason ) } "
158+ end
159+ end
160+
161+ defp print_stacktrace ( trace , callback ) do
162+ try do
163+ io_error callback . ( )
164+ io_error Exception . format_stacktrace ( trace )
165+ catch
166+ _ , _ ->
167+ io_error "** (IEx.Error) error when printing exception message and stacktrace"
168+ end
169+ end
119170end
0 commit comments