2

I have a hell lot of the following global declarations in a .c file:

fi_status_t ACLINFRA_CORE_OK = { { e_FI_STAT_MODULE_ACLINFRA, e_FI_STAT_ACLINFRA_SUBMOD_CORE, e_FI_STATUS_OK }, NULL };
fi_status_t ACLINFRA_CORE_ERR_GDFW_REG_FAILED = { { e_FI_STAT_MODULE_ACLINFRA, e_FI_STAT_ACLINFRA_SUBMOD_CORE, e_CORE_ERR_GDFW_REG_FAILED }, NULL };
fi_status_t ACLINFRA_CORE_ERR_DHAS_REG_FAILED = { { e_FI_STAT_MODULE_ACLINFRA, e_FI_STAT_ACLINFRA_SUBMOD_CORE, e_CORE_ERR_DHAS_REG_FAILED }, NULL };
fi_status_t ACLINFRA_CORE_ERR_GDFW_REQ_FAILED = { { e_FI_STAT_MODULE_ACLINFRA, e_FI_STAT_ACLINFRA_SUBMOD_CORE, e_CORE_ERR_GDFW_REQ_FAILED }, NULL };

Now, I have yanked and pasted them into a .h file. But how do I use emacs to easily covert them into the equivalent extern declarations using query-replace-regexp?

The extern declaration would look like the following:

extern fi_status_t ACLINFRA_CORE_OK;
0

1 Answer 1

1

Replace: ^\(\w.*?\) *=.*
by: extern \1;

Explanation: match lines beginning (^) with a word constituent (\w) followed by any number of characters other than newline (.*?) up to the first occurrence of zero or more spaces (​ *) followed by an equal sign and more characters other than a newline (=.*). The part of the line up to and excluding the optional spaces followed by the equal sign is placed in a group which the replacement text can use as \1.

If you have initializers that span multiple lines, you may want to remove lines that start with some whitespace, with M-x flush-lines on the regex ^\s-.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .