| 1 |
;;; coqdev.el --- Emacs helpers for Rocq development -*- lexical-binding:t -*- |
| 2 |
|
| 3 |
;; Copyright (C) 2018 The Rocq Development Team |
| 4 |
|
| 5 |
;; Maintainer: [email protected] |
| 6 |
|
| 7 |
;;; Commentary: |
| 8 |
|
| 9 |
;; Helpers to set compilation commands, proof general variables, etc |
| 10 |
;; for Rocq development |
| 11 |
|
| 12 |
;; You can disable individual features without editing this file by |
| 13 |
;; using `remove-hook', for instance |
| 14 |
;; (remove-hook 'hack-local-variables-hook #'coqdev-setup-compile-command) |
| 15 |
|
| 16 |
;;; Installation: |
| 17 |
|
| 18 |
;; To use this, with coqdev.el located at /path/to/coqdev.el, add the |
| 19 |
;; following to your init: |
| 20 |
|
| 21 |
;; (add-to-list 'load-path "/path/to/coqdev/") |
| 22 |
;; (require 'coqdev) |
| 23 |
|
| 24 |
;; If you load this file from a git repository, checking out an old |
| 25 |
;; commit will make it disappear and cause errors for your Emacs |
| 26 |
;; startup. To ignore those errors use (require 'coqdev nil t). If you |
| 27 |
;; check out a malicious commit Emacs startup would allow it to run |
| 28 |
;; arbitrary code, to avoid this you can copy coqdev.el to any |
| 29 |
;; location and adjust the load path accordingly (of course if you run |
| 30 |
;; ./configure to compile Rocq it is already too late). |
| 31 |
|
| 32 |
;;; Code: |
| 33 |
(require 'ocamldebug nil 'noerror) |
| 34 |
|
| 35 |
(require 'seq) |
| 36 |
(require 'subr-x) |
| 37 |
|
| 38 |
;; locate-dominating-file would call tramp handlers which call |
| 39 |
;; hack-local-variables-hook which call this recursively |
| 40 |
;; a proper fix would use some hook other than hack-local-variables |
| 41 |
;; for now just return nil for remote files |
| 42 |
(defun coqdev-default-directory () |
| 43 |
"Return the Rocq repository containing `default-directory'." |
| 44 |
(unless (file-remote-p default-directory) |
| 45 |
(let ((dir (seq-some |
| 46 |
(lambda (f) (locate-dominating-file default-directory f)) |
| 47 |
'("META.coq" "META.coq.in" "META.coq-core.in" "coqpp")))) |
| 48 |
(when dir (expand-file-name dir))))) |
| 49 |
|
| 50 |
(defun coqdev-setup-compile-command () |
| 51 |
"Setup `compile-command' for Rocq development." |
| 52 |
(let ((dir (coqdev-default-directory))) |
| 53 |
(when dir (setq-local compile-command (concat "cd " (shell-quote-argument dir) " |
| 54 |
dune build @check # rocq-runtime.install"))))) |
| 55 |
(add-hook 'hack-local-variables-hook #'coqdev-setup-compile-command) |
| 56 |
|
| 57 |
(defvar camldebug-command-name) ; from camldebug.el (caml package) |
| 58 |
(defvar ocamldebug-command-name) ; from ocamldebug.el (tuareg package) |
| 59 |
(defun coqdev-setup-camldebug () |
| 60 |
"Setup ocamldebug for Rocq development. |
| 61 |
|
| 62 |
Specifically `camldebug-command-name' and `ocamldebug-command-name'." |
| 63 |
(let ((dir (coqdev-default-directory))) |
| 64 |
(when dir |
| 65 |
(setq-local camldebug-command-name |
| 66 |
(concat dir "dev/ocamldebug-coq")) |
| 67 |
(setq-local ocamldebug-command-name |
| 68 |
(concat dir "dev/ocamldebug-coq"))))) |
| 69 |
(add-hook 'hack-local-variables-hook #'coqdev-setup-camldebug) |
| 70 |
|
| 71 |
(defun coqdev-setup-tags () |
| 72 |
"Setup `tags-file-name' for Rocq development." |
| 73 |
(let ((dir (coqdev-default-directory))) |
| 74 |
(when dir (setq-local tags-file-name (concat dir "TAGS"))))) |
| 75 |
(add-hook 'hack-local-variables-hook #'coqdev-setup-tags) |
| 76 |
|
| 77 |
(defvar coq-prog-args) |
| 78 |
(defvar coq-prog-name) |
| 79 |
|
| 80 |
;; Lets us detect whether there are file local variables |
| 81 |
;; even though PG sets it with `setq' when there's a _Coqproject. |
| 82 |
;; Also makes sense generally, so might make it into PG someday. |
| 83 |
(make-variable-buffer-local 'coq-prog-args) |
| 84 |
(setq-default coq-prog-args nil) |
| 85 |
|
| 86 |
(defun coqdev-setup-proofgeneral () |
| 87 |
"Setup Proofgeneral variables for Rocq development. |
| 88 |
|
| 89 |
Note that this function is executed before _Coqproject is read if it exists." |
| 90 |
(let ((dir (coqdev-default-directory))) |
| 91 |
(when dir |
| 92 |
(setq-local coq-prog-name (concat dir "_build/install/default/bin/coqtop"))))) |
| 93 |
(add-hook 'hack-local-variables-hook #'coqdev-setup-proofgeneral) |
| 94 |
|
| 95 |
(defvar coqdev-ocamldebug-command "dune exec -- dev/dune-dbg -emacs coqc /tmp/foo.v" |
| 96 |
"Command run by `coqdev-ocamldebug'") |
| 97 |
|
| 98 |
(declare-function comint-check-proc "comint") |
| 99 |
(declare-function tuareg--split-args "tuareg") |
| 100 |
(declare-function ocamldebug-filter "ocamldebug") |
| 101 |
(declare-function ocamldebug-sentinel "ocamldebug") |
| 102 |
(declare-function ocamldebug-mode "ocamldebug") |
| 103 |
(declare-function ocamldebug-set-buffer "ocamldebug") |
| 104 |
(defun coqdev-ocamldebug () |
| 105 |
"Runs a command in an ocamldebug buffer." |
| 106 |
(interactive) |
| 107 |
(require 'ocamldebug) |
| 108 |
(let* ((dir (read-directory-name "Run from directory: " |
| 109 |
(coqdev-default-directory))) |
| 110 |
(name "ocamldebug-coq") |
| 111 |
(buffer-name (concat "*" name "*"))) |
| 112 |
(pop-to-buffer buffer-name) |
| 113 |
(unless (comint-check-proc buffer-name) |
| 114 |
(setq default-directory dir) |
| 115 |
(setq coqdev-ocamldebug-command |
| 116 |
(read-from-minibuffer "Command to run: " |
| 117 |
coqdev-ocamldebug-command)) |
| 118 |
(let* ((cmdlist (tuareg--split-args coqdev-ocamldebug-command)) |
| 119 |
(cmdlist (mapcar #'substitute-in-file-name cmdlist))) |
| 120 |
(apply #'make-comint name |
| 121 |
(car cmdlist) |
| 122 |
nil |
| 123 |
(cdr cmdlist)) |
| 124 |
(set-process-filter (get-buffer-process (current-buffer)) |
| 125 |
#'ocamldebug-filter) |
| 126 |
(set-process-sentinel (get-buffer-process (current-buffer)) |
| 127 |
#'ocamldebug-sentinel) |
| 128 |
(ocamldebug-mode))) |
| 129 |
(ocamldebug-set-buffer) |
| 130 |
(insert "source db"))) |
| 131 |
|
| 132 |
;; Provide correct breakpoint setting in dune wrapped libraries |
| 133 |
;; (assuming only 1 library/dune file) |
| 134 |
(defun coqdev--read-from-file (file) |
| 135 |
"Read FILE as a list of sexps. If invalid syntax, return nil and message the error." |
| 136 |
(with-temp-buffer |
| 137 |
(save-excursion |
| 138 |
(insert "(\n") |
| 139 |
(insert-file-contents file) |
| 140 |
(goto-char (point-max)) |
| 141 |
(insert "\n)\n")) |
| 142 |
(condition-case err |
| 143 |
(read (current-buffer)) |
| 144 |
((error err) (progn (message "Error reading file %S: %S" file err) nil))))) |
| 145 |
|
| 146 |
(defun coqdev--find-single-library (sexps) |
| 147 |
"If list SEXPS has an element whose `car' is \"library\", return the first one. |
| 148 |
Otherwise return `nil'." |
| 149 |
(let ((libs (seq-filter (lambda (elt) (equal (car elt) 'library)) sexps))) |
| 150 |
(and libs (car libs)))) |
| 151 |
|
| 152 |
(defun coqdev--dune-library-name (lib) |
| 153 |
"With LIB a dune-syntax library stanza, get its name as a string." |
| 154 |
(let ((field (or (seq-find (lambda (field) (and (consp field) (equal (car field) 'name))) lib) |
| 155 |
(seq-find (lambda (field) (and (consp field) (equal (car field) 'public\_name))) lib)))) |
| 156 |
(symbol-name (car (cdr field))))) |
| 157 |
|
| 158 |
(defun coqdev--upcase-first-char (arg) |
| 159 |
"Set the first character of ARG to uppercase." |
| 160 |
(concat (upcase (substring arg 0 1)) (substring arg 1 (length arg)))) |
| 161 |
|
| 162 |
(defun coqdev--real-module-name (filename) |
| 163 |
"Return module name for ocamldebug, taking into account dune wrapping. |
| 164 |
(for now only understands dune files with a single library stanza)" |
| 165 |
(let ((mod (substring filename (string-match "\\([^/]*\\)\\.ml$" filename) (match-end 1))) |
| 166 |
(dune (concat (file-name-directory filename) "dune"))) |
| 167 |
(if (file-exists-p dune) |
| 168 |
(if-let* ((contents (coqdev--read-from-file dune)) |
| 169 |
(lib (coqdev--find-single-library contents)) |
| 170 |
(is-wrapped (null (seq-contains-p lib '(wrapped false)))) |
| 171 |
(libname (coqdev--dune-library-name lib))) |
| 172 |
(concat libname "__" (coqdev--upcase-first-char mod)) |
| 173 |
mod) |
| 174 |
mod))) |
| 175 |
|
| 176 |
(with-eval-after-load 'ocamldebug |
| 177 |
(defun ocamldebug-module-name (arg) |
| 178 |
(coqdev--real-module-name arg))) |
| 179 |
|
| 180 |
;; This Elisp snippet adds a regexp parser for the format of Anomaly |
| 181 |
;; backtraces (coqc -bt ...), to the error parser of the Compilation |
| 182 |
;; mode (C-c C-c: "Compile command: ..."). File locations in traces |
| 183 |
;; are recognized and can be jumped from easily in the *compilation* |
| 184 |
;; buffer. |
| 185 |
(defvar compilation-error-regexp-alist-alist) |
| 186 |
(defvar compilation-error-regexp-alist) |
| 187 |
(with-eval-after-load 'compile |
| 188 |
(add-to-list |
| 189 |
'compilation-error-regexp-alist-alist |
| 190 |
'(coq-backtrace |
| 191 |
"^ *\\(?:raise\\|frame\\) @ file \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1,\ |
| 192 |
lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\ |
| 193 |
\\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:?\\)?\\)" |
| 194 |
2 (3 . 4) (5 . 6))) |
| 195 |
(add-to-list 'compilation-error-regexp-alist 'coq-backtrace)) |
| 196 |
|
| 197 |
(defvar bug-reference-bug-regexp) |
| 198 |
(defvar bug-reference-url-format) |
| 199 |
(defun coqdev-setup-bug-reference-mode () |
| 200 |
"Setup `bug-reference-bug-regexp' and `bug-reference-url-format' for Rocq. |
| 201 |
|
| 202 |
This does not enable `bug-reference-mode'." |
| 203 |
(let ((dir (coqdev-default-directory))) |
| 204 |
(when dir |
| 205 |
(setq-local bug-reference-bug-regexp "\\(#\\(?2:[0-9]+\\)\\)") |
| 206 |
(setq-local bug-reference-url-format "https://github.com/rocq-prover/rocq/issues/%s") |
| 207 |
(when (derived-mode-p 'prog-mode) (bug-reference-prog-mode 1))))) |
| 208 |
(add-hook 'hack-local-variables-hook #'coqdev-setup-bug-reference-mode) |
| 209 |
|
| 210 |
(defun coqdev-sphinx-quote-coq-refman-region (left right &optional offset beg end) |
| 211 |
"Add LEFT and RIGHT around the BEG..END. |
| 212 |
Leave the point after RIGHT. BEG and END default to the bounds |
| 213 |
of the current region. Leave point OFFSET characters after the |
| 214 |
left quote (if OFFSET is nil, leave the point after the right |
| 215 |
quote)." |
| 216 |
(unless beg |
| 217 |
(if (region-active-p) |
| 218 |
(setq beg (region-beginning) end (region-end)) |
| 219 |
(setq beg (point) end nil))) |
| 220 |
(save-excursion |
| 221 |
(goto-char (or end beg)) |
| 222 |
(insert right)) |
| 223 |
(save-excursion |
| 224 |
(goto-char beg) |
| 225 |
(insert left)) |
| 226 |
(if (and end (not offset)) ;; Second test handles the ::`` case |
| 227 |
(goto-char (+ end (length left) (length right))) |
| 228 |
(goto-char (+ beg (or offset (length left)))))) |
| 229 |
|
| 230 |
(defun coqdev-sphinx-rst-coq-action () |
| 231 |
"Insert a Sphinx role template or quote the current region." |
| 232 |
(interactive) |
| 233 |
(pcase (read-char "Command [gntm:`]?") |
| 234 |
(?g (coqdev-sphinx-quote-coq-refman-region ":g:`" "`")) |
| 235 |
(?n (coqdev-sphinx-quote-coq-refman-region ":n:`" "`")) |
| 236 |
(?t (coqdev-sphinx-quote-coq-refman-region ":token:`" "`")) |
| 237 |
(?m (coqdev-sphinx-quote-coq-refman-region ":math:`" "`")) |
| 238 |
(?: (coqdev-sphinx-quote-coq-refman-region "::`" "`" 1)) |
| 239 |
(?` (coqdev-sphinx-quote-coq-refman-region "``" "``")))) |
| 240 |
|
| 241 |
(provide 'coqdev) |
| 242 |
;;; coqdev ends here |