help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Functionality to maintain function prototypes in C


From: John Mastro
Subject: Re: Functionality to maintain function prototypes in C
Date: Fri, 28 Oct 2016 18:30:01 -0700

Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> The first time, move the point where you want the forward signatures to
> be inserted and M-x c-update-forward-defun-signatures RET
>
> Then  M-x c-update-forward-defun-signatures RET again will update the
> same section, wherever the point is.
>
> Now, you will have to move all the type definition used in the function
> signature before this section…

Thanks for posting this - there have definitely been times I could have
used it.

I made one modification to the part where it searches for an existing
block of declarations, changing the regexp to:

    (format "%s\\(.\\|\n\\)*%s" section-begin section-end)

Because "." doesn't match newlines.

I also gave both functions an optional argument STATIC-ONLY to only
collect functions declared static, since that's generally what I've
wanted.

The result with those tweaks, in case it's useful to anyone:

(defun c-collect-defun-signatures (&optional static-only)
  "Return a list of C function signatures found in the buffer."
  (let ((regexp (if static-only
                    "\\(static .*(.*)\\)[^(){}]*{"
                  "\\(.*(.*)\\)[^(){}]*{"))
        (signatures '())
        (last-defun -1))
    (goto-char (point-min))
    (end-of-defun)
    (beginning-of-defun)
    (while (/= last-defun (point))
      (setf last-defun (point))
      (when (re-search-forward regexp nil t)
        (push (match-string 1) signatures))
      (end-of-defun)
      (end-of-defun)
      (beginning-of-defun))
    signatures))

(defun c-update-forward-defun-signatures (&optional static-only)
  "Update the forward function signatures.
If a section doesn't already exist, creates it at point."
  (interactive "*P")
  (let ((signatures (save-excursion
                      (nreverse (c-collect-defun-signatures static-only))))
        (section-begin "// BEGIN FORWARD FUNCTION SIGNATURES\n")
        (section-end   "// END FORWARD FUNCTION SIGNATURES\n"))
    (goto-char (or (save-excursion
                     (goto-char (point-min))
                     (when (re-search-forward
                            (format "%s\\(.\\|\n\\)*%s" section-begin
section-end)
                            nil t)
                       (delete-region (match-beginning 0) (match-end 0))
                       (match-beginning 0)))
                   (point)))
    (insert section-begin)
    (dolist (signature signatures)
      (insert signature ";\n"))
    (insert section-end)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]