Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Comments in words file #25

Open
cjpbirkbeck opened this issue Sep 1, 2021 · 9 comments
Open

Suggestion: Comments in words file #25

cjpbirkbeck opened this issue Sep 1, 2021 · 9 comments

Comments

@cjpbirkbeck
Copy link

Hello, I have been using this program for a bit and I have noticed that the words file doesn't allow for comments. I think have some comments would useful even for a small words file.

Here's my proposal:

  • There would only be whole-line comments, where clac would just ignore the line if it starts with a comment character. Probably having inline comments wouldn't be that much harder, but given how simple this program is, I think it wouldn't be worth it.
  • The comment character would be the pound sign #, as in it is various UNIX(-like) shells, python, and various configuration languages.
  • Played a bit with the words file, it seems like "#" is already interpreted as just #, so I thinking there would be no need for any escape sequences here, just an update to the man page.

I'm not exactly the world's greatest C programmer, but I would be willing to try doing it if you think it's a reasonable idea.

@soveran
Copy link
Owner

soveran commented Sep 1, 2021

Hey @cjpbirkbeck, I think it is a good idea. I have been using # as an alias of count, but I will happily give it away in order to have comments. The patch is simple:

diff --git a/clac.c b/clac.c
index 7d08fa5..3fce9d2 100644
--- a/clac.c
+++ b/clac.c
@@ -260,6 +260,10 @@ static void load(sds filename) {
 	for (i = 0; i < linecount; i++) {
 		lines[i] = sdstrim(lines[i], " \t\r\n");
 
+		if (lines[i][0] == '#') {
+		  continue;
+		}
+
 		if (parse(lines[i]) != 0) {
 			sdsfreesplitres(lines, linecount);
 

Before making a definitive decision, I would like to have the opinion of @waltertross, another big user of clac.

@waltertross
Copy link

hi, it would work, but it's a bit of a pity if the # cannot be used as a word any more. I thought of //, but that can also be useful as a word for integer division (if you come from Python). I don't know, maybe ' or ". Also /* closed by */ would be nice, but spreading this over lines, or handling more than one on a line, would make the code definitely more complicated. An idea which would break lots of rules would be a /* at start of line, NOT CLOSED. But that's too revolutionary, probably ;-) A very conservative idea, OTOH, would be to NOT allow line comments, and to allow a comment string (enclosed in quotes) to follow the word definition string. This would stay associated with the word, and would be printed out by the words command.

@wtross-eg
Copy link

wtross-eg commented Sep 2, 2021

Ah, but in search for a solution I found that the # at start of line is perfectly viable, because already now the word being defined can be enclosed in double quotes, so that your alias for count, @soveran, can be defined as "#"! So... green light!

@waltertross
Copy link

Woops, I left my last comment as wtross-eg. It's still me, but as an EG employee...

@soveran
Copy link
Owner

soveran commented Sep 3, 2021

Wow, that's a great finding! I tested it and having comments looks like a good improvement. I'll document it and push the changes.

@soveran
Copy link
Owner

soveran commented Sep 3, 2021

Added in this commit: ba208c0

@waltertross @cjpbirkbeck If you are OK with the implementation and the comment, I will release a new version.

@waltertross
Copy link

Maybe for completeness you might want to add something similar to the following, somewhere:

Note that to define the word # you have to use "#"
in order for the definition not to be interpreted as a comment

If you don't write something like this anywhere, most users will be unable to define the # word.

@cjpbirkbeck
Copy link
Author

Maybe for completeness you might want to add something similar to the following, somewhere:

Note that to define the word # you have to use "#"
in order for the definition not to be interpreted as a comment

If you don't write something like this anywhere, most users will be unable to define the # word.

I agree and I would suggest that an extra paragraph is added to the man page documenting how comments are written and how to escape # . Again, I'm not sure if I have written the troff syntax correctly, but I would suggest something like the following:

.
.Ss Comments
.
Any lines that begin with
.Sy #
are considered to be a comment, and are ignored. There are no inline comments, and any 
.Sy #
characters that is not the first character is interpreted literally. To assign 
.Sy #
as an operation, use 
.Sy "#"
. So, for example:
.Pp
.Dl # This is a comment.
.Dl "#" "count"
.Pp
This assigns
.Sy #
to the operation
.Sy count.
.

@soveran
Copy link
Owner

soveran commented Sep 6, 2021

Excellent, I added a paragraph to both the README and the man page: beae8c4

Can you check it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants