Skip to content

Commit 3539569

Browse files
Merge PR #15689: Update coqdev.el: minor fixes + better ocamldebug breakpoints in plugins/
Reviewed-by: ejgallego Co-authored-by: ejgallego <[email protected]>
2 parents cd406a5 + 48eb770 commit 3539569

File tree

1 file changed

+68
-6
lines changed

1 file changed

+68
-6
lines changed

dev/tools/coqdev.el

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@
3232
;;; Code:
3333
(require 'ocamldebug)
3434

35+
(require 'seq)
36+
(require 'subr-x)
37+
3538
(defun coqdev-default-directory ()
3639
"Return the Coq repository containing `default-directory'."
37-
(let ((dir (locate-dominating-file default-directory "coq-core.opam")))
40+
(let ((dir (seq-some
41+
(lambda (f) (locate-dominating-file default-directory f))
42+
'("META.coq" "META.coq.in" "META.coq-core.in" "coqpp"))))
3843
(when dir (expand-file-name dir))))
3944

4045
(defun coqdev-setup-compile-command ()
4146
"Setup `compile-command' for Coq development."
4247
(let ((dir (coqdev-default-directory)))
43-
;; we add a space at the end to make it easy to add arguments (eg -j or target)
44-
(when dir (setq-local compile-command (concat "make -C " (shell-quote-argument dir) " ")))))
48+
(when dir (setq-local compile-command (concat "cd " (shell-quote-argument dir) "
49+
dune build @check # coq-core.install dev/shim/coqtop-prelude")))))
4550
(add-hook 'hack-local-variables-hook #'coqdev-setup-compile-command)
4651

4752
(defvar camldebug-command-name) ; from camldebug.el (caml package)
@@ -82,12 +87,19 @@ Note that this function is executed before _Coqproject is read if it exists."
8287
(setq-local coq-prog-name (concat dir "_build/default/dev/shim/coqtop-prelude")))))
8388
(add-hook 'hack-local-variables-hook #'coqdev-setup-proofgeneral)
8489

85-
(defvar coqdev-ocamldebug-command "dune exec dev/dune-dbg"
90+
(defvar coqdev-ocamldebug-command "dune exec -- dev/dune-dbg coqc /tmp/foo.v"
8691
"Command run by `coqdev-ocamldebug'")
8792

93+
(declare-function comint-check-proc "comint")
94+
(declare-function tuareg--split-args "tuareg")
95+
(declare-function ocamldebug-filter "ocamldebug")
96+
(declare-function ocamldebug-sentinel "ocamldebug")
97+
(declare-function ocamldebug-mode "ocamldebug")
98+
(declare-function ocamldebug-set-buffer "ocamldebug")
8899
(defun coqdev-ocamldebug ()
89100
"Runs a command in an ocamldebug buffer."
90101
(interactive)
102+
(require 'ocamldebug)
91103
(let* ((dir (read-directory-name "Run from directory: "
92104
(coqdev-default-directory)))
93105
(name "ocamldebug-coq")
@@ -109,7 +121,56 @@ Note that this function is executed before _Coqproject is read if it exists."
109121
(set-process-sentinel (get-buffer-process (current-buffer))
110122
#'ocamldebug-sentinel)
111123
(ocamldebug-mode)))
112-
(ocamldebug-set-buffer)))
124+
(ocamldebug-set-buffer)
125+
(insert "source dune_db_409")))
126+
127+
;; Provide correct breakpoint setting in dune wrapped libraries
128+
;; (assuming only 1 library/dune file)
129+
(defun coqdev--read-from-file (file)
130+
"Read FILE as a list of sexps. If invalid syntax, return nil and message the error."
131+
(with-temp-buffer
132+
(save-excursion
133+
(insert "(\n")
134+
(insert-file-contents file)
135+
(goto-char (point-max))
136+
(insert "\n)\n"))
137+
(condition-case err
138+
(read (current-buffer))
139+
((error err) (progn (message "Error reading file %S: %S" file err) nil)))))
140+
141+
(defun coqdev--find-single-library (sexps)
142+
"If list SEXPS has an element whose `car' is \"library\", return the first one.
143+
Otherwise return `nil'."
144+
(let ((libs (seq-filter (lambda (elt) (equal (car elt) 'library)) sexps)))
145+
(and libs (car libs))))
146+
147+
(defun coqdev--dune-library-name (lib)
148+
"With LIB a dune-syntax library stanza, get its name as a string."
149+
(let ((field (or (seq-find (lambda (field) (and (consp field) (equal (car field) 'name))) lib)
150+
(seq-find (lambda (field) (and (consp field) (equal (car field) 'public\_name))) lib))))
151+
(symbol-name (car (cdr field)))))
152+
153+
(defun coqdev--upcase-first-char (arg)
154+
"Set the first character of ARG to uppercase."
155+
(concat (upcase (substring arg 0 1)) (substring arg 1 (length arg))))
156+
157+
(defun coqdev--real-module-name (filename)
158+
"Return module name for ocamldebug, taking into account dune wrapping.
159+
(for now only understands dune files with a single library stanza)"
160+
(let ((mod (substring filename (string-match "\\([^/]*\\)\\.ml$" filename) (match-end 1)))
161+
(dune (concat (file-name-directory filename) "dune")))
162+
(if (file-exists-p dune)
163+
(if-let* ((contents (coqdev--read-from-file dune))
164+
(lib (coqdev--find-single-library contents))
165+
(is-wrapped (null (seq-contains-p lib '(wrapped false))))
166+
(libname (coqdev--dune-library-name lib)))
167+
(concat libname "__" (coqdev--upcase-first-char mod))
168+
mod)
169+
mod)))
170+
171+
(with-eval-after-load 'ocamldebug
172+
(defun ocamldebug-module-name (arg)
173+
(coqdev--real-module-name arg)))
113174

114175
;; This Elisp snippet adds a regexp parser for the format of Anomaly
115176
;; backtraces (coqc -bt ...), to the error parser of the Compilation
@@ -137,7 +198,8 @@ This does not enable `bug-reference-mode'."
137198
(let ((dir (coqdev-default-directory)))
138199
(when dir
139200
(setq-local bug-reference-bug-regexp "#\\(?2:[0-9]+\\)")
140-
(setq-local bug-reference-url-format "https://github.com/coq/coq/issues/%s"))))
201+
(setq-local bug-reference-url-format "https://github.com/coq/coq/issues/%s")
202+
(when (derived-mode-p 'prog-mode) (bug-reference-prog-mode 1)))))
141203
(add-hook 'hack-local-variables-hook #'coqdev-setup-bug-reference-mode)
142204

143205
(defun coqdev-sphinx-quote-coq-refman-region (left right &optional offset beg end)

0 commit comments

Comments
 (0)