all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: John Mastro <john.b.mastro@gmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Cc: "Pascal J. Bourguignon" <pjb@informatimago.com>
Subject: Re: Functionality to maintain function prototypes in C
Date: Fri, 28 Oct 2016 18:30:01 -0700	[thread overview]
Message-ID: <CAOj2CQSqtQ9B=sZKAodGzBhAd6-E5DgqUfxxRCERVuxX_QXFYA@mail.gmail.com> (raw)
In-Reply-To: <87wpgsduir.fsf@informatimago.com>

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	other threads:[~2016-10-29  1:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-28 18:55 Functionality to maintain function prototypes in C Nikolai Weibull
2016-10-28 21:38 ` Pascal J. Bourguignon
2016-10-29  1:30   ` John Mastro [this message]
2016-10-29  8:26   ` Nikolai Weibull
2016-10-29  8:34 ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOj2CQSqtQ9B=sZKAodGzBhAd6-E5DgqUfxxRCERVuxX_QXFYA@mail.gmail.com' \
    --to=john.b.mastro@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=pjb@informatimago.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.