32
32
; ;; Code:
33
33
(require 'ocamldebug )
34
34
35
+ (require 'seq )
36
+ (require 'subr-x )
37
+
35
38
(defun coqdev-default-directory ()
36
39
" 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" ))))
38
43
(when dir (expand-file-name dir))))
39
44
40
45
(defun coqdev-setup-compile-command ()
41
46
" Setup `compile-command' for Coq development."
42
47
(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 " )))))
45
50
(add-hook 'hack-local-variables-hook #'coqdev-setup-compile-command )
46
51
47
52
(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."
82
87
(setq-local coq-prog-name (concat dir " _build/default/dev/shim/coqtop-prelude" )))))
83
88
(add-hook 'hack-local-variables-hook #'coqdev-setup-proofgeneral )
84
89
85
- (defvar coqdev-ocamldebug-command " dune exec dev/dune-dbg"
90
+ (defvar coqdev-ocamldebug-command " dune exec -- dev/dune-dbg coqc /tmp/foo.v "
86
91
" Command run by `coqdev-ocamldebug' " )
87
92
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" )
88
99
(defun coqdev-ocamldebug ()
89
100
" Runs a command in an ocamldebug buffer."
90
101
(interactive )
102
+ (require 'ocamldebug )
91
103
(let* ((dir (read-directory-name " Run from directory: "
92
104
(coqdev-default-directory)))
93
105
(name " ocamldebug-coq" )
@@ -109,7 +121,56 @@ Note that this function is executed before _Coqproject is read if it exists."
109
121
(set-process-sentinel (get-buffer-process (current-buffer ))
110
122
#'ocamldebug-sentinel )
111
123
(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)))
113
174
114
175
; ; This Elisp snippet adds a regexp parser for the format of Anomaly
115
176
; ; backtraces (coqc -bt ...), to the error parser of the Compilation
@@ -137,7 +198,8 @@ This does not enable `bug-reference-mode'."
137
198
(let ((dir (coqdev-default-directory)))
138
199
(when dir
139
200
(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 )))))
141
203
(add-hook 'hack-local-variables-hook #'coqdev-setup-bug-reference-mode )
142
204
143
205
(defun coqdev-sphinx-quote-coq-refman-region (left right &optional offset beg end )
0 commit comments