cHelper/main.el

57 lines
2.4 KiB
EmacsLisp

;;; this file contains funcqtions which write a c header file for you
;;; automatically
;;; Code:
(defun get-header-file-name ()
"Calculates the .h filename from the .c file in the current buffer."
(let ((c-buffer (buffer-name (current-buffer))))
(concat (file-name-base c-buffer) ".h")))
(defun create-c-function-declarations (c-buffer-file-name)
"Create the function declarations from the output of main.py.
C-BUFFER-FILE-NAME is the name of the source code file we want to get the
declarations of"
(with-temp-buffer
(insert (shell-command-to-string
(concat "python3 " user-emacs-directory "personal/cHelper/main.py "
c-buffer-file-name " " user-emacs-directory "personal/cHelper/")))
(goto-char (point-min))
(replace-regexp "[ ]+" " ")
(goto-char (point-min))
(replace-regexp " *\{" ";")
(buffer-string)))
(defun insert-define-clause ()
"Insert #ifndef bla #define bla in the buffer if not yes present."
(let ((def-clause-open-string (format "#ifndef __%s\n#define __%s\n"
(string-replace "." "_" (upcase (get-header-file-name)))
(string-replace "." "_" (upcase (get-header-file-name))))))
(progn (goto-char (point-min))
(if (eq nil (search-forward def-clause-open-string nil t))
(progn (goto-char (point-min))
(insert def-clause-open-string)
(goto-char (point-max))
;; we assume there is no closing if there was no opening
(insert "#endif"))))))
(defun add-function-definitions (c-buffer-file-name)
(let ((func-decs (create-c-function-declarations c-buffer-file-name))
(h-buffer (find-file (get-header-file-name))))
(with-current-buffer h-buffer
(goto-char (point-max))
(goto-char (search-backward "#endif" nil t))
(insert func-decs))))
(defun chelper-make-header-from-source ()
"Create a C haeder file from the .c file in your current buffer."
(interactive)
(if (eq nil (file-exists-p (get-header-file-name)))
(let ((h-buffer (create-file-buffer (get-header-file-name)))
(c-buffer-name (buffer-name (current-buffer))))
(with-current-buffer h-buffer
(goto-char (point-min))
(insert (create-c-function-declarations c-buffer-name))
(insert-define-clause)))
(add-function-definitions (buffer-name (current-buffer)))))