Skip to content

Commit ed14ce4

Browse files
committed
Add --dot-iex command line option to IEx
It allows one to override the standard locations where .iex is searched and also makes it possible to disable .iex loading completely.
1 parent 0cb936a commit ed14ce4

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

bin/iex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ if [ $# -gt 0 ] && ([ $1 = "--help" ] || [ $1 = "-h" ]); then
1414
--name \"name\" Makes and assigns a name to the distributed node
1515
--sname \"name\" Makes and assigns a short name to the distributed node
1616
--remsh \"name\" Connects to a node using a remote shell
17+
--dot-iex \"path\" Overrides default .iex file and uses path instead;
18+
path can be empty, then no file will be loaded
1719
1820
** Options marked with (*) can be given more than once
1921
** Options given after the .exs file or -- are passed down to the executed code
@@ -33,4 +35,4 @@ readlink_f () {
3335

3436
SELF=$(readlink_f "$0")
3537
SCRIPT_PATH=$(dirname "$SELF")
36-
ELIXIR_NO_CLI=1 exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user Elixir-IEx-CLI" +iex "$@"
38+
ELIXIR_NO_CLI=1 exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user Elixir-IEx-CLI" +iex "$@"

lib/elixir/lib/kernel/cli.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ defmodule Kernel.CLI do
222222
{ config, t }
223223
end
224224

225+
# This clause is here so that Kernel.CLI does not error out with "unknown
226+
# option"
227+
defp process_iex(["--dot-iex",_|t], config) do
228+
process_iex t, config
229+
end
230+
225231
defp process_iex([opt,_|t], config) when opt in ["--remsh"] do
226232
process_iex t, config
227233
end

lib/iex/lib/iex.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
defrecord IEx.Config, binding: nil, cache: '', counter: 1, scope: nil,
2-
result: nil, load_dot_iex: true
2+
result: nil, dot_iex_path: nil
33

44
defmodule IEx do
55
@moduledoc %B"""
@@ -87,6 +87,9 @@ defmodule IEx do
8787
iex(1)> value
8888
13
8989
90+
It is possible to override the default loading sequence for .iex file by
91+
supplying the --dot-iex option to iex. See `iex --help`.
92+
9093
## Expressions in IEx
9194
9295
As an interactive shell, IEx evalutes expressions. This has some
@@ -206,7 +209,7 @@ defmodule IEx do
206209
IEx.Config[
207210
binding: opts[:binding] || [],
208211
scope: scope,
209-
load_dot_iex: Keyword.get(opts, :load_dot_iex, true),
212+
dot_iex_path: Keyword.get(opts, :dot_iex_path),
210213
]
211214
end
212215

lib/iex/lib/iex/cli.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ defmodule IEx.CLI do
1919
else
2020
:user.start
2121
IO.puts "Warning: could not run smart terminal, falling back to dumb one"
22-
IEx.start([], fn -> :elixir.start_cli end)
22+
config = [dot_iex_path: find_dot_iex(:init.get_plain_arguments)]
23+
IEx.start(config, fn -> :elixir.start_cli end)
2324
end
2425
end
2526

@@ -37,12 +38,15 @@ defmodule IEx.CLI do
3738
end
3839

3940
defp tty do
41+
plain_args = :init.get_plain_arguments
42+
43+
config = [dot_iex_path: find_dot_iex(plain_args)]
4044
function = fn ->
41-
IEx.start([], fn -> :elixir.start_cli end)
45+
IEx.start(config, fn -> :elixir.start_cli end)
4246
end
4347

4448
args =
45-
if remote = get_remsh(:init.get_plain_arguments) do
49+
if remote = get_remsh(plain_args) do
4650
unless is_alive do
4751
function = fn ->
4852
IO.puts(:stderr, "In order to use --remsh, you need to name the current node using --name or --sname. Aborting...")
@@ -60,6 +64,10 @@ defmodule IEx.CLI do
6064
:user_drv.start([:"tty_sl -c -e", args])
6165
end
6266

67+
defp find_dot_iex(['--dot-iex',h|_]), do: :unicode.characters_to_binary(h)
68+
defp find_dot_iex([_|t]), do: find_dot_iex(t)
69+
defp find_dot_iex([]), do: nil
70+
6371
defp get_remsh(['--remsh',h|_]), do: list_to_atom(h)
6472
defp get_remsh([_|t]), do: get_remsh(t)
6573
defp get_remsh([]), do: nil

lib/iex/lib/iex/server.ex

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ defmodule IEx.Server do
1010
1111
"""
1212
def start(config) do
13-
IO.puts "Interactive Elixir (#{System.version}) - press Ctrl+C to exit (type h() ENTER for help)"
1413
Process.put :iex_history, []
14+
1515
{ _, _, scope } = :elixir.eval('require IEx.Helpers', [], 0, config.scope)
1616
config = config.scope(scope)
17-
if config.load_dot_iex, do:
18-
config = load_dot_iex(config)
17+
18+
config = case config.dot_iex_path do
19+
"" -> config # don't load anything
20+
nil -> load_dot_iex(config) # load .iex from predefined locations
21+
path -> load_dot_iex(config, path) # load from `path`
22+
end
23+
24+
IO.puts "Interactive Elixir (#{System.version}) - press Ctrl+C to exit (type h() ENTER for help)"
1925
do_loop(config)
2026
end
2127

@@ -91,8 +97,12 @@ defmodule IEx.Server do
9197

9298
# Locates and loads an .iex file from one of predefined locations. Returns
9399
# new config.
94-
defp load_dot_iex(config) do
95-
candidates = Enum.map [".iex", "~/.iex"], Path.expand(&1)
100+
defp load_dot_iex(config, path // nil) do
101+
candidates = if path do
102+
[path]
103+
else
104+
Enum.map [".iex", "~/.iex"], Path.expand(&1)
105+
end
96106
path = Enum.find candidates, fn path -> File.regular?(path) end
97107
if nil?(path) do
98108
config

0 commit comments

Comments
 (0)