You do not need LuaLaTeX for this, it can be done quite easily with expl3:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand\lip{s O{3}}{
$
\prg_replicate:nn{#2}{\ldotp}
$
\IfBooleanT{#1}{\spacefactor\sfcode`\.\relax}
}
\ExplSyntaxOff
\begin{document}
Hello \lip, I know how to write dots: \lip*[10] Anyway \lip[3] not all dots end sentences.
\end{document}
Of course, if you use it like some \lips words, TeX will gobble the space. This is very hard to avoid even with LuaTeX because LuaTeX does not change the TeX parsing rules. Of course this problem does not exists if you use a star or the optional argument.
If you really need it, there are three options I can think of:
- Always add the space if no argument has been given. This would break e.g. at
\lip, in the example above.
- Use
xspace but remember the drawbacks
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse,xspace}
\ExplSyntaxOn
\NewDocumentCommand\lip{s o}{
$
\prg_replicate:nn{\IfValueTF{#2}{#2}{3}}{\ldotp}
$
\IfBooleanTF{#1}{
\spacefactor\sfcode`\.\relax
}{
\IfValueF{#2}{\xspace}
}
}
\ExplSyntaxOff
\begin{document}
Hello \lip, I know how to write dots: \lip*[10] Anyway \lip not all dots end sentences.
\end{document}
- The "LuaTeX solution": Use a
process_input_buffer callback to detect all input lines where \lip is used and always add an explicit space afterwards. This would be extremely fragile and will not interact properly with macros etc.
So it is much more reliable to just manually add \ or [3] when this occurs.