From d5b37749e24079d10400a4cb53d9876102552aa9 Mon Sep 17 00:00:00 2001 From: Ben Bellick Date: Mon, 11 Dec 2023 23:50:38 -0600 Subject: [PATCH 1/2] Configurable start directory for utop utop can be starting from either: - the directory of the opened buffer, or - the project-root directory (via project.el) This is useful if you have an `.ocamlinit` file in your project root. Because there is not a clear reason for setting a specific directory (no difference for utop other than `.ocamlinit`, that option has been omitted. Modeled after elpy-shell.el --- src/top/utop.el | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/top/utop.el b/src/top/utop.el index b473cf6c..0a948ee8 100644 --- a/src/top/utop.el +++ b/src/top/utop.el @@ -75,6 +75,18 @@ with Emacs to provide an enhanced environment." :type 'string :safe 'stringp) +(defcustom utop-starting-directory 'current-directory + "Directory in which utop will be started. + +Can be the `current-directory' (default) to use the current buffer's directory or +`project-root' to use the project root as implied by project.el. For most +users, `current-directory' is completely sufficient. +However, if using an .ocamlinit file in the root directory, +it may be desirable to run the project from the root directory." + + :type '(choice (const :tag "Project root" project-root) + (const :tag "Current directory" current-directory))) + (defcustom utop-edit-command t "Whether to read the command from the minibuffer before running utop. @@ -1073,9 +1085,16 @@ defaults to 0." ;; Set the state to done to allow utop to be restarted if ;; start-process fails (setq utop-state 'done) - - ;; Create the sub-process - (setq utop-process (apply 'start-process "utop" (current-buffer) (car arguments) (cdr arguments))) + (let ((default-directory + (cond ((eq utop-starting-directory 'project-root) + (project-root (project-current))) + ((eq utop-starting-directory 'current-directory) + default-directory) + (t + (error "Wrong value for `utop-starting-directory', please check this variable documentation and set it to a proper value"))))) + + ;; Create the sub-process + (setq utop-process (apply 'start-process "utop" (current-buffer) (car arguments) (cdr arguments)))) ;; Set the initial state: we are waiting for ocaml to send the ;; initial prompt From cb90551bcb7a847033ff5e21aa2f13840ecc7e58 Mon Sep 17 00:00:00 2001 From: Ben Bellick Date: Tue, 12 Dec 2023 23:17:43 -0600 Subject: [PATCH 2/2] catch case when `project-current` is nil --- src/top/utop.el | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/top/utop.el b/src/top/utop.el index 0a948ee8..cfca47c8 100644 --- a/src/top/utop.el +++ b/src/top/utop.el @@ -1086,12 +1086,14 @@ defaults to 0." ;; start-process fails (setq utop-state 'done) (let ((default-directory - (cond ((eq utop-starting-directory 'project-root) - (project-root (project-current))) - ((eq utop-starting-directory 'current-directory) - default-directory) - (t - (error "Wrong value for `utop-starting-directory', please check this variable documentation and set it to a proper value"))))) + (cond ((eq utop-starting-directory 'project-root) + (if (project-current) + (project-root (project-current)) + (error "`utop-starting-directory' is set to project-root but not currently inside a project"))) + ((eq utop-starting-directory 'current-directory) + default-directory) + (t + (error "Wrong value for `utop-starting-directory', please check this variable documentation and set it to a proper value"))))) ;; Create the sub-process (setq utop-process (apply 'start-process "utop" (current-buffer) (car arguments) (cdr arguments))))