Skip to content

Commit 17ef076

Browse files
dbuenzliArmael
authored andcommitted
.ocamlinit: XDG base directory lookup. (ocaml#8834)
1 parent 776fcc3 commit 17ef076

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

Changes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ Working version
131131
(Whitequark and Jacques-Henri Jourdan, review by Gabriel Scherer
132132
and Xavier Clerc)
133133

134+
* #8834, `ocaml`: adhere to the XDG base directory specification to
135+
locate an `.ocamlinit` file. Reads an `$XDG_CONFIG_HOME/ocaml/init.ml`
136+
file before trying to lookup `~/.ocamlinit`. On Windows the behaviour
137+
is unchanged.
138+
(Daniel C. Bünzli, review by David Allsopp, Armaël Guéneau and
139+
Nicolás Ojeda Bär)
140+
134141
### Standard library:
135142

136143
- #8832: List.find_map : ('a -> 'b option) -> 'a list -> 'b option

man/ocaml.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,14 @@ use heuristics to enable colors only if the output supports them (an
302302
giving control to the user. The default file is
303303
.B .ocamlinit
304304
in the current directory if it exists, otherwise
305+
.B XDG_CONFIG_HOME/ocaml/init.ml
306+
according to the XDG base directory specification lookup if it exists (on
307+
Windows this is skipped), otherwise
305308
.B .ocamlinit
306-
in the user's home directory. You can specify a different initialization file
309+
in the user's home directory (
310+
.B HOME
311+
variable).
312+
You can specify a different initialization file
307313
by using the
308314
.BI \-init \ file
309315
option, and disable initialization files by using the
@@ -327,7 +333,10 @@ use heuristics to enable colors only if the output supports them (an
327333
attempts to underline visually the location of the error. It
328334
consults the TERM variable to determines the type of output terminal
329335
and look up its capabilities in the terminal database.
330-
336+
.TP
337+
.B XDG_CONFIG_HOME HOME
338+
.B .ocamlinit
339+
lookup procedure (see above).
331340
.SH SEE ALSO
332341
.BR ocamlc (1), \ ocamlopt (1), \ ocamlrun (1).
333342
.br

manual/manual/cmds/top.etex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ its contents are read as a sequence of OCaml phrases
6666
and executed as per the "#use" directive
6767
described in section~\ref{s:toplevel-directives}.
6868
The evaluation outcode for each phrase are not displayed.
69-
If the current directory does not contain an ".ocamlinit" file, but
70-
the user's home directory (environment variable "HOME") does, the
71-
latter is read and executed as described below.
69+
If the current directory does not contain an ".ocamlinit" file,
70+
the file "XDG_CONFIG_HOME/ocaml/init.ml" is looked up according
71+
to the XDG base directory specification and used instead (on Windows
72+
this is skipped). If that file doesn't exist then an [.ocamlinit] file
73+
in the users' home directory (determined via environment variable "HOME") is
74+
used if existing.
7275

7376
The toplevel system does not perform line editing, but it can
7477
easily be used in conjunction with an external line editor such as
@@ -132,7 +135,8 @@ attempts to underline visually the location of the error. It
132135
consults the "TERM" variable to determines the type of output terminal
133136
and look up its capabilities in the terminal database.
134137

135-
\item["HOME"] Directory where the ".ocamlinit" file is searched.
138+
\item["XDG_CONFIG_HOME", "HOME"]
139+
".ocamlinit" lookup procedure (see above).
136140
\end{options}
137141
\end{unix}
138142

manual/manual/cmds/unified-options.etex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ the toplevel is running with the "#directory" directive
287287
\item["-init" \var{file}]
288288
Load the given file instead of the default initialization file.
289289
The default file is ".ocamlinit" in the current directory if it
290-
exists, otherwise ".ocamlinit" in the user's home directory.
290+
exists, otherwise "XDG_CONFIG_HOME/ocaml/init.ml" or
291+
".ocamlinit" in the user's home directory.
291292
}%top
292293

293294
\notop{%

toplevel/opttoploop.ml

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,17 +544,42 @@ let _ =
544544
Clflags.dlcode := true;
545545
()
546546

547+
let find_ocamlinit () =
548+
let ocamlinit = ".ocamlinit" in
549+
if Sys.file_exists ocamlinit then Some ocamlinit else
550+
let getenv var = match Sys.getenv var with
551+
| exception Not_found -> None | "" -> None | v -> Some v
552+
in
553+
let exists_in_dir dir file = match dir with
554+
| None -> None
555+
| Some dir ->
556+
let file = Filename.concat dir file in
557+
if Sys.file_exists file then Some file else None
558+
in
559+
let home_dir () = getenv "HOME" in
560+
let config_dir () =
561+
if Sys.win32 then None else
562+
match getenv "XDG_CONFIG_HOME" with
563+
| Some _ as v -> v
564+
| None ->
565+
match home_dir () with
566+
| None -> None
567+
| Some dir -> Some (Filename.concat dir ".config")
568+
in
569+
let init_ml = Filename.concat "ocaml" "init.ml" in
570+
match exists_in_dir (config_dir ()) init_ml with
571+
| Some _ as v -> v
572+
| None -> exists_in_dir (home_dir ()) ocamlinit
573+
547574
let load_ocamlinit ppf =
548575
if !Clflags.noinit then ()
549576
else match !Clflags.init_file with
550577
| Some f -> if Sys.file_exists f then ignore (use_silently ppf f)
551578
else fprintf ppf "Init file not found: \"%s\".@." f
552579
| None ->
553-
if Sys.file_exists ".ocamlinit" then ignore (use_silently ppf ".ocamlinit")
554-
else try
555-
let home_init = Filename.concat (Sys.getenv "HOME") ".ocamlinit" in
556-
if Sys.file_exists home_init then ignore (use_silently ppf home_init)
557-
with Not_found -> ()
580+
match find_ocamlinit () with
581+
| None -> ()
582+
| Some file -> ignore (use_silently ppf file)
558583
;;
559584

560585
let set_paths () =

toplevel/toploop.ml

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,17 +498,42 @@ let _ =
498498
Env.import_crcs ~source:Sys.executable_name crc_intfs;
499499
()
500500

501+
let find_ocamlinit () =
502+
let ocamlinit = ".ocamlinit" in
503+
if Sys.file_exists ocamlinit then Some ocamlinit else
504+
let getenv var = match Sys.getenv var with
505+
| exception Not_found -> None | "" -> None | v -> Some v
506+
in
507+
let exists_in_dir dir file = match dir with
508+
| None -> None
509+
| Some dir ->
510+
let file = Filename.concat dir file in
511+
if Sys.file_exists file then Some file else None
512+
in
513+
let home_dir () = getenv "HOME" in
514+
let config_dir () =
515+
if Sys.win32 then None else
516+
match getenv "XDG_CONFIG_HOME" with
517+
| Some _ as v -> v
518+
| None ->
519+
match home_dir () with
520+
| None -> None
521+
| Some dir -> Some (Filename.concat dir ".config")
522+
in
523+
let init_ml = Filename.concat "ocaml" "init.ml" in
524+
match exists_in_dir (config_dir ()) init_ml with
525+
| Some _ as v -> v
526+
| None -> exists_in_dir (home_dir ()) ocamlinit
527+
501528
let load_ocamlinit ppf =
502529
if !Clflags.noinit then ()
503530
else match !Clflags.init_file with
504531
| Some f -> if Sys.file_exists f then ignore (use_silently ppf f)
505532
else fprintf ppf "Init file not found: \"%s\".@." f
506533
| None ->
507-
if Sys.file_exists ".ocamlinit" then ignore (use_silently ppf ".ocamlinit")
508-
else try
509-
let home_init = Filename.concat (Sys.getenv "HOME") ".ocamlinit" in
510-
if Sys.file_exists home_init then ignore (use_silently ppf home_init)
511-
with Not_found -> ()
534+
match find_ocamlinit () with
535+
| None -> ()
536+
| Some file -> ignore (use_silently ppf file)
512537
;;
513538

514539
let set_paths () =

0 commit comments

Comments
 (0)