texstudio-org / texstudio

TeXstudio is a fully featured LaTeX editor. Our goal is to make writing LaTeX documents as easy and comfortable as possible.
http://www.texstudio.org/
GNU General Public License v3.0
2.76k stars 343 forks source link

Feature Request: add special treatment for commands like "\newcount" #689

Closed muzimuzhi closed 5 years ago

muzimuzhi commented 5 years ago

As far as I guess, since cwl file only supports options inside braces ({}, [], etc), commands that normally used without braces around their arguments and defines (mostly one of) their arguments as new macros are specially treated in latexdocument.cpp. For example,

\def\newcmd<parameter text>{<def>} % and \edef, \gdef, \xdef
\newcount\mycount

However, there are other command that share the same syntax with \newcount but have not been specially treated. This will cause wrong highlight effect in txs, see image The full list of these \newcount-like commands is

\newdimen
\newskip
\newmuskip
\newbox
\newtoks
\newread
\newwrite
\newlanguage
\newfam
\newinsert

PS: These macros, plus \newcount, were originally introduced in plain tex format, and were then adopted by latex2e format. Hence one can see the occurrences of them both in documents about tex with plain tex (like texdoc texbytopic, Chap. 31) and in documents about latex2e (like texdoc source2e, Sec. b.9).

The related source lines could be https://github.com/texstudio-org/texstudio/blob/a7fd0a3dc064f685d68b8a6792bb825f4669a085/src/latexdocument.cpp#L838-L852

sunderme commented 5 years ago

I have implemented two commands in the cwl, see commit 99cafd4f0df6c49319cdc13b9891fc7672b8cdff

You can do the rest ...

muzimuzhi commented 5 years ago

The added lines to cwl file only recognize usages with braces like

\newbox{\somebox} % here txs recognizes \somebox as a new cmd
...
\somebox % here txs highlights \somebox as normal cmd

But what I want, and what is usually used in latex package source files is the form without braces:

\newbox\someBox % here txs highlights both \someBox as "unrecognized command"
...
\someBox

That is to say, commit https://github.com/texstudio-org/texstudio/commit/99cafd4f0df6c49319cdc13b9891fc7672b8cdff does not solve the question shown in the screenshot I have posted: image

PS: Yes, the with-brace form (\newbox{\someBox}) is a valid form, but the no-brace form is the form most latex package writers use. For example, the latex2e kernel consistently uses the no-brace form. This means, txs will provides wrong highlight when openning package source files.


The following may provide a clearer description about my original question (or request)

And following is my trial and guess when I opened this isse

sunderme commented 5 years ago

here it works fine 2019-08-18-193548_3840x2160_scrot

sunderme commented 5 years ago

have you placed a tex.cwl in your .config/texstudio/completion/user ?

muzimuzhi commented 5 years ago

Your commit does meet my original request. I am so sorry.

Before today, I used a cwl-test.cwl file containing a line \newbox{cmd}#dS and a test tex file

\documentclass{article}
\usepackage{cwl-test}
\newbox\xxx
\newcount\zzz
\begin{document}
\xxx
\zzz
\end{document}

Then the problem persists when I directly open txs with the tex file. But when I delete and then re-type in the line \newbox\xxx in tex file, it works.

muzimuzhi commented 5 years ago

I will provide the rest cwl lines.

muzimuzhi commented 5 years ago

Commit https://github.com/texstudio-org/texstudio/commit/d538bab7c91e9b845e0be698d1aac49b7e9b2d77 is also related to (or resulted from) this issue.