Not sure how this would be useful, but…
\documentclass{article}
\usepackage{refcount}
\ExplSyntaxOn
\NewDocumentCommand{\startmacro}{}
{
\tl_clear:N \l__coto_macro_body_tl
}
\NewDocumentCommand{\finishmacro}{smm}
{% #1 = *, #2 = macro name, #3 = number of arguments
\IfBooleanTF{#1}
{
\coto_macro_finish:NNn \cs_set_protected:cn #2 { #3 }
}
{
\coto_macro_finish:NNn \cs_set:cn #2 { #3 }
}
}
\NewDocumentCommand{\addexpanded}{m}
{
\tl_put_right:Nx \l__coto_macro_body_tl { \exp_not:n { #1 } }
}
\NewDocumentCommand{\addunexpanded}{m}
{
\tl_put_right:Nn \l__coto_macro_body_tl { \exp_not:n { #1 } }
}
\NewDocumentCommand{\addcsname}{m}
{
\tl_put_right:Nx \l__coto_macro_body_tl { \exp_not:n { \exp_not:c { #1 } } }
}
\NewDocumentCommand{\addleftbrace}{}
{
\tl_put_right:Nn \l__coto_macro_body_tl { \if_true: { \else: } \fi: }
}
\NewDocumentCommand{\addrightbrace}{}
{
\tl_put_right:Nn \l__coto_macro_body_tl { \if_false: { \else: } \fi: }
}
\NewDocumentCommand{\addparameter}{m}
{
\tl_put_right:Nn \l__coto_macro_body_tl { ## #1 }
}
\tl_new:N \l__coto_macro_body_tl
\cs_generate_variant:Nn \tl_set:Nn { Ne }
\cs_new_protected:Nn \coto_macro_finish:NNn
{
\tl_set:Ne \l__coto_macro_body_tl { \l__coto_macro_body_tl }
\exp_args:NnV #1 { __coto_macro_temp: \prg_replicate:nn { #3 } { n } } \l__coto_macro_body_tl
\cs_new_eq:Nc #2 { __coto_macro_temp: \prg_replicate:nn { #3 } { n } }
}
\ExplSyntaxOff
\begin{document}
\newcommand{\niceand}{NICEAND}
\newcommand{\fancyiv}[1]{FANCYIV-#1-}
\newcounter{mycounter}
\setcounter{mycounter}{4}
\setcounter{section}{6}
\setcounter{subsection}{1}
\refstepcounter{subsection}\label{somelabel}
\startmacro
\addexpanded{[\getrefnumber{somelabel}]: }
\addunexpanded{\niceand}
\addcsname{fancy\roman{mycounter}}
\addleftbrace
\addparameter
\addunexpanded{1}
\addrightbrace
\finishmacro{\mymacro}{1}
\texttt{\meaning\mymacro}
\mymacro{xyz}
\end{document}
With \finishmacro* the macro would be \protected.
