summaryrefslogtreecommitdiff
path: root/c-conf.el
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-01-12 19:22:08 +0800
committerJSDurand <mmemmew@gmail.com>2022-01-12 19:22:08 +0800
commit7001e593e0c4ce000e03327e39642e2f5db43247 (patch)
tree69bb788b733aabe68b8928812008dda883ee855c /c-conf.el
parent279e16bbdab38c61449199c444b2edf0a1c12b91 (diff)
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.
Diffstat (limited to 'c-conf.el')
-rw-r--r--c-conf.el32
1 files changed, 28 insertions, 4 deletions
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