1
1
defmodule IEx.Server do
2
2
@ moduledoc false
3
3
4
- alias IEx.Util
5
-
6
4
@ doc """
7
5
Eval loop for an IEx session. Its responsibilities include:
8
6
@@ -15,7 +13,9 @@ defmodule IEx.Server do
15
13
IO . puts "Interactive Elixir (#{ System . version } ) - press Ctrl+C to exit (type h() ENTER for help)"
16
14
Process . put :iex_history , [ ]
17
15
{ _ , _ , 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 )
19
19
end
20
20
21
21
defp do_loop ( config ) do
@@ -28,11 +28,11 @@ defmodule IEx.Server do
28
28
eval ( code , line , counter , config )
29
29
rescue
30
30
exception ->
31
- Util . print_exception ( exception , System . stacktrace )
31
+ print_exception ( exception , System . stacktrace )
32
32
config . cache ( '' )
33
33
catch
34
34
kind , error ->
35
- Util . print_error ( kind , error , System . stacktrace )
35
+ print_error ( kind , error , System . stacktrace )
36
36
config . cache ( '' )
37
37
end
38
38
@@ -88,6 +88,31 @@ defmodule IEx.Server do
88
88
end
89
89
end
90
90
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
+
91
116
defp update_history ( config ) do
92
117
current = Process . get :iex_history
93
118
Process . put :iex_history , [ config | current ]
@@ -113,7 +138,33 @@ defmodule IEx.Server do
113
138
IO . puts :stdio , IO.ANSI . escape ( "%{yellow}#{ inspect ( result , IEx . inspect_opts ) } " )
114
139
end
115
140
141
+ defp io_error ( result ) do
142
+ IO . puts :stdio , result
143
+ end
144
+
116
145
defp remote_prefix do
117
146
if node == node ( :erlang . group_leader ) , do: "iex" , else: "rem"
118
147
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
119
170
end
0 commit comments