From 7001e593e0c4ce000e03327e39642e2f5db43247 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Wed, 12 Jan 2022 19:22:08 +0800 Subject: c-conf: add test files by a key-binding Add a function to generate a skeleton test file automatically, and bind that to a key. --- c-conf.el | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'c-conf.el') diff --git a/c-conf.el b/c-conf.el index b6cad71..24f3472 100644 --- a/c-conf.el +++ b/c-conf.el @@ -33,18 +33,18 @@ (define-key c-mode-map (vector 'S-tab) #'ff-find-other-file) ;;; prefix map -(defvar durand-c-prefix-map '(keymap) +(defvar durand-c-prefix-map (make-sparse-keymap) "The keymap of my custom functions. This will be bound to the key ù.") (let ((map durand-c-prefix-map)) (define-key map (vector ?i) #'durand-c-include) - (define-key map (vector ?p) #'c-generate-pragma)) + (define-key map (vector ?p) #'c-generate-pragma) + (define-key map (vector ?t) #'c-generate-test)) ;; generate pragmas automatically. (define-key c-mode-map (vector ?ù) durand-c-prefix-map) -;;;###autoload (defun c-generate-pragma () "Generate default pragmas for a C header file. If invoked on a C file, include the corresponding header file." @@ -74,7 +74,6 @@ If invoked on a C file, include the corresponding header file." "c\\(p*\\)$" "h\\1" (buffer-name)))) (newline)))) -;;;###autoload (defun durand-c-include (&optional arg) "Include a library. If ARG is non-nil, ask for the type of the include: whether the @@ -106,5 +105,30 @@ angle bracket type or the quote type." ((eq type 'quote) "\"")) "\n"))))) +(defun c-generate-test () + "Generate or find a test file for the current C file." + (interactive) + (cond + ((derived-mode-p 'c-mode)) + ((user-error "Not in C-mode"))) + (let* ((f-name (buffer-file-name)) + (base (file-name-base f-name)) + (t-name + (cond + ((null f-name) + (user-error "Buffer not associated to a file")) + ((expand-file-name + (expand-file-name + (format "check_%s.c" base) "test") + (file-name-directory f-name)))))) + (cond + ((file-exists-p t-name)) + ((write-region + (format "#include \"../%s.h\"\n\n%s" + base + "int main(int argc, char **argv)\n{\n return 0;\n}\n") + nil t-name))) + (find-file t-name))) + (provide 'c-conf) ;;; c-conf.el ends here -- cgit v1.2.3-18-g5258