lcn2 / calc

C-style arbitrary precision calculator
http://www.isthe.com/chongo/tech/comp/calc/index.html
Other
359 stars 52 forks source link

Enhancement: automatic vim no TAB configuration for all calc source except makefiles #151

Closed lcn2 closed 4 months ago

lcn2 commented 6 months ago

PLEASE SEE THE ASSUMPTIONS NEAR THE BOTTOM OF THIS COMMENT

TODO

Inspired by @pmetzger's comment-1939127107 we wish to replace ASCII TAB characters with ASCII SPACE charcters in calc source code as part of the process to plan for calc v3 as noted in issue #103. This issue is here to develop a vim(1) framework to allow this to happen automatically.

Some hints at a solution were suggested by @xexyl in comment-2093121295. We have not tested this solution.

vim and calc

We need a framework where, when using the vim(1) editor on calc source, we have the following modes in effect:

" Use 4-space indents
"
:set shiftwidth=4

" Intelligently use the tab key for indentation instead of for inserting tab characters
"
:set smarttab

" Use space characters, never tab characters
"
:set expandtab

" Set tab stops to be different from the indentation width.
"
:set tabstop=8

" Reduce the chance of tab characters masquerading as proper indents.
"
:set softtabstop=0

Assumption 0: use of vim

ASSUMPTION: This issue is focused on the use of vim(1) as the editor to manage to the calc source.

We are aware that they are other fine editors besides vim. Indeed, we have used other editors in the past such as:

On accession we DO still use some of the above editors from time to time today. Editors have their strengths and their mis-feature weaknesses. We intend no disrespect to other editors. This issue is focused on use of vim(1) for calc.

Assumption 1: using SPACE instead of TAB

ASSUMPTION: This issue assumes that the calc source will NOT USE THE TAB CHARACTER.

We know is a long standing Tabs versus Spaces debate.

If you wish to debate the issue of using the TAB character in calc source, PLEASE COMMENT UNDER issue #103 instead of this issue. This issue assumes that the calc source will not use TABS. In particular if you wish to object to using SPACE characters instead of TAB characters, please feel free to comment under issue #103 (see https://github.com/lcn2/calc/issues/103).

Assumption 2: Not using TABS applies to ONLY calc source

ASSUMPTION: We do NOT want to do this for all files everywhere, only files under the calc source directory.

For purposes of discussion, assume that the calc source directory resides under:

/usr/local/src/bin/calc

Assumption 3: Not enabling via a local directory .vimrc file

ASSUMPTION: We will NOT enable the use of a .vimrc file within the calc source tree.

We do NOT want to enable local directory .vimrc files in general as this is dangerous. Someone could provide a tarball with an evil .vimrc that could do things like autocmd BufEnter * :silent! !echo rm -rf stuff.

While it might be permissible to enable the .vimrc files for the specific directory, but that could also be dangerous as some patch could alter that local .vimrc file and create similar problems.

Assumption 4: vim solution will not not part of the calc source

ASSUMPTION: We are NOT trying to configure this for everyone who happens to have a copy of the calc source.

We really need to configure vim just for ourselves. While we can document HOW we configured vim(1) for editing calc in some someone else wants to do the same. So we don't see a need for this configuration be enforce on anyone who happens to have a copy of the calc source.

It is our guess that the solution will involve editing our own private ~/.vimrc file under our home directory AND / OR file(s) under the ~/.vim/ directory.

We plan to, as part of the calc build process, issue warnings if TAB characters are found in the source source where they should not be. Those warnings will not prevent calc from being compiled.

Assumption 5: automatic use for calc source

ASSUMPTION: We want the NO TAB CHARACTERS mode to be automatically enabled whenever we edit the calc source.

We do NOT want to have to remember to execute some magic vim command to set this stuff, before editing files under that directory, we and others are likely to forget to execute such a magic command. We want it to be automatic.

While we plan as part of our release process, to verify that calc source does NOT contain tab characters, while we are working on the calc source tree, we want vim(1) to automatically enable no TABS for the calc source directory (assume /usr/local/src/bin/calc for the sake of discussion).

Assumption 6: Some calc files will have TAB characters

ASSUMPTION: Some files WILL CONTAIN TAB characters, just not C source.

Files such as Makefiles need TAB characters for commands related to a make rule. While some make tools allow for whitespace indented Makefiles, others do not. Because intending with spaces is not portable across various make tools, calc makefiles WILL continue to use TAB characters.

Calc source has the following make relate files that WILL CONTAIN TAB characters:

There may be other exceptions in the calc source tree, but for now assume that all files other than the above list WILL NOT CONTAIN TAB characters.

xexyl commented 6 months ago

PLEASE SEE THE ASSUMPTIONS NEAR THE BOTTOM OF THIS COMMENT

...

There may be other exceptions in the calc source tree, but for now assume that all files other than the above list WILL NOT CONTAIN TAB characters.

If I don't reply to this tomorrow please remind me. I can't do anything with it today but I should be able to tomorrow though I hope to work more on the html files 'over there'.

xexyl commented 6 months ago

Question for you Landon: how do you want me to prioritise this along with the IOCCC ? As you know I am making good progress with that top priority issue but I still have a ways to go.

I have to look at this issue more thoroughly tomorrow but depending on how you answer this question I will try and figure out how to process both issues.

Of course the other issue I already know well but hopefully tomorrow I will have enough energy to parse this issue too. I will ask any questions if I come across anything that is unclear but as always I am happy to help where I can!

... and with that I will bid you a good night!

xexyl commented 6 months ago

Okay I quickly read the top comment and it seems good to me. The question is how do you want me to present any solution? An example portion of the .vimrc file maybe?

Without looking at it I think this can be done relatively quickly but as always there might be something that comes up or that I haven't thought of or something else.

The above question about prioritising this with the IOCCC also still stands of course.

Going to bed now. Good night!

xexyl commented 6 months ago

Caveat about usage below

Okay this long command can list the files not excluded in your list of files to exclude. It seems it might be possible to use with glob() but I have never used that in the buffer pre write events so I have to play with it but this is a way (albeit maybe not efficient as another way but which is less easier to maintain[0]) but here's a start:

find /usr/local/src/bin/calc  -name '.git' -prune -o -name '.github' -prune -o \( ! -name Makefile ! -name Makefile.config ! -name Makefile.local ! -name Makefile.target ! -path cal/Makefile ! -path cscript/Makefile ! -path custom/Makefile ! -path help/Makefile ! -name rpm.mk \)

and knowing you I would not be surprised if you have a more efficient way. But the purpose of course is to find a list of files to act on and which to not act on.

[0] Otherwise you have to maintain a long list which might or might not be more efficient with vim but I can't say for sure. Another way of course is to set the defaults you want and then change the settings for the files that match but the way I'm pondering will just set the settings you need on those files.

Caveat

Of course you might not want to use shell commands. I also do not know if a certain setting (you know what I mean but I can't think of the name) that will prevent shell commands will cause this to fail or not. I would not be surprised if it did fail. Anyway this is a work in progress and might be called a workaround for the fact that you can have new files added etc.

xexyl commented 6 months ago

Okay doing something like the below in the .vimrc (with removed extraneous \( and \)):

let exclude_files = glob("`find /usr/local/src/bin/calc  -name '.git' -prune -o -name '.github' -prune -o ! -name Makefile ! -name Makefile.config ! -name Makefile.local ! -name Makefile.target ! -path cal/Makefile ! -path cscript/Makefile ! -path custom/Makefile ! -path help/Makefile ! -name rpm.mk`")

allows one to do:

:echo exclude_files

which gives a list of the files. The question is how to get that to work in the events. I'm not sure of that yet.

xexyl commented 6 months ago

I have discovered how to refer to variables but the problem is that it wants to list a character at a time. Again this is just playing with the events in a way I have never had to deal with it. But if you put this in your .vimrc you will see that it will take a few seconds to load the buffer. Then it will echo a character at a time:

let exclude_files = glob("`find /usr/local/src/bin/calc  -name '.git' -prune -o -name '.github' -prune -o ! -name Makefile ! -name Makefile.config ! -name Makefile.local ! -name Makefile.target ! -path cal/Makefile ! -path cscript/Makefile ! -path custom/Makefile ! -path help/Makefile ! -name rpm.mk`")

for file in exclude_files
    echo file
endfor

Which might explain why:

for file in exclude_files
    autocmd! BufNewFile,BufWritePre,BufReadPre file setf shiftwidth=4
endfor

does not work.

Perhaps there is another way to loop by line but I do not know yet. I must go afk a while though .. will try later to see if I can figure it out.

xexyl commented 6 months ago

But doing:

let exclude_files = [ glob("`find /usr/local/src/bin/calc  -name '.git' -prune -o -name '.github' -prune -o ! -name Makefile ! -name Makefile.config ! -name Makefile.local ! -name Makefile.target ! -path cal/Makefile ! -path cscript/Makefile ! -path custom/Makefile ! -path help/Makefile ! -name rpm.mk`") ]

does allow one to see it a line at a time. The set does not work in an auto command however, unless I have the wrong event maybe.

xexyl commented 6 months ago

I have some thought that it might be just doing it as one string with a newline after each so that it appears to be different lines but isn't. Adding a "t" to the echo command shows only one t and at the end of the long list. So somehow or another the list has to be constructed differently (list = variable).

Anyway I must be away for a while and not sure if I can return to this today but if not then tomorrow. I'd like to get some of the other repo worked on today too but I'm doubting I will.

xexyl commented 6 months ago

I just remembered that I have some other things I need to take care of today so I will very likely have to stop for the day.

There has to be a way other than the way I thought of some weeks ago.

I have thought of another possibility but it's much more of a hack. It might work but I will have to play with that another time if necessary. It involves the after loading of the .vimrc file feature. But even that is a very vague thought at this time and by that I mean the feature just popped into my head.

But perhaps some of the above will give you some thoughts or perhaps you might be against generating a list of files entirely. I can see that also being the case in which case something else will have to be done.

xexyl commented 6 months ago

Possible solution depending on your preference

Is it okay to have to manually update the list of files that you don't want these settings for? If that's okay then I have an idea that might work: along the lines of what I brought up some weeks ago.

The long list would through a single glob have the settings set up and then the much smaller list would have the settings undone.

If that would work then I think it would be a lot easier but you would have to update the smaller list if new files have to be added.

--

Must leave for now and probably the day. Good luck with the infrastructure project! Depending on the answer to this question and how I come along with the other things I have to take care of today I might be able to do it but I guess we will see.

Let me know. It might be a case where the manual set up way is simpler and more efficient.

lcn2 commented 6 months ago

Is it okay to have to manually update the list of files that you don't want these settings for?

Yes. The list of files under /usr/local/src/bin/calc for which those settings should not apply something that will rarely change. It is perfectly fine to have to manually update the "exclude under /usr/local/src/bin/calc" list.

xexyl commented 6 months ago

Is it okay to have to manually update the list of files that you don't want these settings for?

Yes. The list of files under /usr/local/src/bin/calc for which those settings should not apply something that will rarely change. It is perfectly fine to have to manually update the "exclude under /usr/local/src/bin/calc" list.

That should make it easier. The next question is what do you want the settings to be for the excluded files? Should the settings be set to the default? Or do you have something else in mind?

lcn2 commented 6 months ago

Is it okay to have to manually update the list of files that you don't want these settings for?

Yes. The list of files under /usr/local/src/bin/calc for which those settings should not apply something that will rarely change. It is perfectly fine to have to manually update the "exclude under /usr/local/src/bin/calc" list.

That should make it easier. The next question is what do you want the settings to be for the excluded files? Should the settings be set to the default? Or do you have something else in mind?

First to recap

As stated in the TODO at the top, these files under /usr/local/src/bin/calc are the files (that can be edited) that WILL CONTAIN TAB characters:

For all other files under /usr/local/src/bin/calc. when edited by vim(1) the editor should not allow ASCII TAB character to be added, but instead insert SPACE characters into the file.

Initially calc source (with the exception of the above list of files) can be modified to replace ASCII TAB characters with the appropriate number of SPACE characters. Then when editing files under /usr/local/src/bin/calc other than those exceptions, should not add ASCII TAB characters but instead add the appropriate number of SPACE characters.

FYI: The new calc source will use 4 character intention, so pressing the TAB key should add spaces to the next 4-character "tab stop". Thus, if we understand vim(1) modes correcty we need to use:

" Use 4-space indents
"
:set shiftwidth=4

" Intelligently use the tab key for indentation instead of for inserting tab characters
"
:set smarttab

" Use space characters, never tab characters
"
:set expandtab

" Set tab stops to be different from the indentation width.
"
:set tabstop=8

" Reduce the chance of tab characters masquerading as proper indents.
"
:set softtabstop=0

Settings for the exception files

For files under /usr/local/src/bin/calc that are:

For those files we suspect we need:

" Set tabstop to 8
"
:set tabstop=8

" Set shiftwidth to 4
"
:set shiftwidth=4

" Let the key (in insert mode) insert tab characters (and possibly tabs)
"
:set softtabstop=4

" Make the tab key insert tab characters
"
:set noexpandtab

" Make the key (in insert mode) insert spaces or tabs to go to the next indent tabstop
" when cursor is at the beginning of a line with only preceding whitespace characters.
"
:set smarttab

etc.

We are open to further suggestions and corrections in regards to both vim(1) settings: those happen to be our current guess as to what is needed.

xexyl commented 6 months ago

Is it okay to have to manually update the list of files that you don't want these settings for?

Yes. The list of files under /usr/local/src/bin/calc for which those settings should not apply something that will rarely change. It is perfectly fine to have to manually update the "exclude under /usr/local/src/bin/calc" list.

That should make it easier. The next question is what do you want the settings to be for the excluded files? Should the settings be set to the default? Or do you have something else in mind?

First to recap

As stated in the TODO at the top, these files under /usr/local/src/bin/calc are the files (that can be edited) that WILL CONTAIN TAB characters:

  • Makefile

  • Makefile.config

  • Makefile.local

  • Makefile.target

  • cal/Makefile

  • cscript/Makefile

  • custom/Makefile

  • help/Makefile

  • rpm.mk

For all other files under /usr/local/src/bin/calc. when edited by vim(1) the editor should not allow ASCII TAB character to be added, but instead insert SPACE characters into the file.

Initially calc source (with the exception of the above list of files) can be modified to replace ASCII TAB characters with the appropriate number of SPACE characters. Then when editing files under /usr/local/src/bin/calc other than those exceptions, should not add ASCII TAB characters but instead add the appropriate number of SPACE characters.

FYI: The new calc source will use 4 character intention, so pressing the TAB key should add spaces to the next 4-character "tab stop". Thus, if we understand vim(1) modes correcty we need to use:


" Use 4-space indents

"

:set shiftwidth=4

" Intelligently use the tab key for indentation instead of for inserting tab characters

"

:set smarttab

" Use space characters, never tab characters

"

:set expandtab

" Set tab stops to be different from the indentation width.

"

:set tabstop=8

" Reduce the chance of tab characters masquerading as proper indents.

"

:set softtabstop=0

Settings for the exception files

For files under /usr/local/src/bin/calc that are:

  • Makefile

  • Makefile.config

  • Makefile.local

  • Makefile.target

  • cal/Makefile

  • cscript/Makefile

  • custom/Makefile

  • help/Makefile

  • rpm.mk

For those files we suspect we need:


" Set tabstop to 8

"

:set tabstop=8

" Set shiftwidth to 4

"

:set shiftwidth=4

" Let the key (in insert mode) insert tab characters (and possibly tabs)

"

:set softtabstop=4

" Make the tab key insert tab characters

"

:set noexpandtab

" Make the key (in insert mode) insert spaces or tabs to go to the next indent tabstop

" when cursor is at the beginning of a line with only preceding whitespace characters.

"

:set smarttab

etc.

We are open to further suggestions and corrections in regards to both vim(1) settings: those happen to be our current guess as to what is needed.

This comment is I believe excellent and I believe I can come up with a .vimrc set of commands to solve the problem for you. I can't do it just yet but I should be able to in a little while.

If that all goes well I hope that I can do a bit of work on the other repo today too but if not I should be able to return to it tomorrow.

I do have a zoom call today and of course other things I have to do too but I hope that at least this issue will be resolved for you.

But for now I must take care of some other things.

Anyway this comment I am replying to is (at a quick glance with the phone) exactly what I need so thank you! I can actually already picture what has to be done I just have to be at the laptop to do it!

xexyl commented 6 months ago

Sorry .. I keep putting in the wrong output for each. Trying again and then hopefully will get it right. I'm having a frustrating day (not related to this). Hold please ..

xexyl commented 6 months ago

Solution

Based on your comment above, comment 2144322942, if you add to your .vimrc towards the bottom (making sure no other autocmds that might affect the settings come after this) it should solve the problem:

if has("autocmd")
    augroup calc
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/* set shiftwidth=4 smarttab expandtab tabstop=8 softtabstop=0
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/*/Makefile set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/rpm.mk set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    augroup END
endif

If you were to then edit one of the included files (e.g. config.c, alloc.h, custom/c_argv.c or cscript/mersenne.calc (or any other of the non-excluded files) and then type) and put in the command:

:set softtabstop

you should get 0 as an answer.

But if you were to open say one of the Makefiles then

:set softtabstop

(san typos of course .. config should have no typos: tested it all) then you should get the expected 0.

should show 4.

That should solve the problem though again if I have mixed up the order of the description that's likely a mistake in writing out what will happen (but of course if you ever had to change it or if for some reason I messed up the order in the config you could swap them).

And with that I hope to look at the other repo a bit and then go afk a while. I still am not sure what files I'll be looking at next. I was pondering whether looking at the entry html files might be better. But now I think on it I remembered that there are comments to reply to and I think I better reply first.

Let me know if this solves your problem here though! It should but of course there might be other settings you need. The above as you can see shows how to confirm settings too.

UPDATE 0

Btw there are other events you can look at if necessary but I think I got the important ones (and maybe some that aren't necessary). Try:

:help BufReadPre

and scroll in the help for example.

UPDATE 1

I will keep the directory there and the vim settings in my system for a bit to make sure things are okay so that if I have to make any fixes I can test it easily without having to re-download or add the settings back in. But if you let me know that this is what you need I will then delete that directory and the settings from my .vimrc file. Let me know please and hopefully it's all you need! It should also give you the idea of how it works.

I will now look at the comments in the other thread and then I might be able to do some work there. If not I should be able to tomorrow. I might add that I believe this week (it will be Thursday anyway) that I go back to the doctor post surgery (I imagine I would have to go back one more time but it's possible that this is the last time too) so that will eat away time to work on the IOCCC repo. If I work on the entry index.html files I guess it'll look and feel like more progress is made and it would give time to add to the other files (thus not checking the files a second time). I think I'll do that but we'll see. Anyway I have other things to do before my appointment too so I'm not sure how much of that I will get done today. Still hopefully this issue is at least resolved and I can focus on the other ones tomorrow.

xexyl commented 6 months ago

Made a fix above .. edited in comment.

lcn2 commented 4 months ago

UPDATE 0

Btw there are other events you can look at if necessary but I think I got the important ones (and maybe some that aren't necessary). Try:

:help ButBufReadPre

and scroll in the help for example.

E149: Sorry, no help for ButBufReadPre

Was there a typo on "ButBufReadPre" @xexyl ?

xexyl commented 4 months ago

UPDATE 0

Btw there are other events you can look at if necessary but I think I got the important ones (and maybe some that aren't necessary). Try:


:help ButBufReadPre

and scroll in the help for example.

E149: Sorry, no help for ButBufReadPre

Was there a typo on "ButBufReadPre" @xexyl ?

Probably remove the But. Not sure how that got there. So make it BufReadPre. If that doesn't work I will reply tomorrow morning.

lcn2 commented 4 months ago

So far, things look good.

As yes, ":help BufReadPre" was helpful.

We will now close this issue with our thanks, @xexyl

lcn2 commented 4 months ago

Under macOS vim, the solution outlined in issuecomment-2145594040 works very well.

Under Debian Linux vim, it also works well.

Under RHEL vim, the settings for the various Makefiles do not work.

We at the bottom of the our ~/.vimrc file we have:

""""""""""""""""""""""""""""""""""""""""""""""
""""""""  On tabs and vim
""""""""""""""""""""""""""""""""""""""""""""""

" See: //stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces
"
" tabstop
"
"    The width of a hard tabstop measured in "spaces" -- effectively the
"    (maximum) width of an actual tab character.
"
:set tabstop=8
"
" shiftwidth
"
"    The size of an "indent". It's also measured in spaces, so if your
"    code base indents with tab characters then you want shiftwidth to
"    equal the number of tab characters times tabstop. This is also used
"    by things like the =, > and < commands.
"
:set shiftwidth=4
"
" softtabstop
"
"    Setting this to a non-zero value other than tabstop will make the
"    tab key (in insert mode) insert a combination of spaces (and possibly
"    tabs) to simulate tab stops at this width.
"
:set softtabstop=4
"
" expandtab
"
"    Enabling this will make the tab key (in insert mode) insert spaces
"    instead of tab characters. This also affects the behavior of the
"    retab command.
"
:set noexpandtab
"
" smarttab
"
"    Enabling this will make the tab key (in insert mode) insert spaces
"    or tabs to go to the next indent of the next tabstop when the cursor
"    is at the beginning of a line (i.e. the only preceding characters
"    are whitespace).
"
:set smarttab

""""""""""""""""""""""""""""""""""""""""""""""
"""""""" Special calc settings for calc source that is NOT a Makefile or *.mk
""""""""
"""""""" see: https://github.com/lcn2/calc/issues/151#issuecomment-2145594040
""""""""
"""""""" To test, try and see if 0 is the answer:
""""""""
""""""""    :set softtabstop
""""""""
""""""""""""""""""""""""""""""""""""""""""""""

if has("autocmd")
    augroup calc
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/* set shiftwidth=4 smarttab expandtab tabstop=8 softtabstop=0
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/*/Makefile set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/rpm.mk set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    augroup END
endif

NOTE: We explicitly set various tab related files to a "default" before the augroup calc set.

We test using the following command:

vi ~/.vimrc /usr/local/src/bin/calc/{calc.c,Makefile,help/sin,help/Makefile}

While going thru the various files, we look at:

:set softtabstop

For macOS and Debian Linux vim, we see that :set softtabstop is 0 for only:

In all other cases :set softtabstop is set to 4 as it should be.

Under RHEL vim, however, :set softtabstop is 0 for:

while it is 4 for files outside of /usr/local/src/bin/calc such as ~/.vimrc is it should.

What is wrong is that the Makefiles under RHEL Linux vim, have :set softtabstop is 0 which is NOT correct.

Changing, under RHEL Linux, the order of files visited does not change the situation. This still has the same problem:

vi /usr/local/src/bin/calc/{Makefile,help/Makefile} ~/.vimrc /usr/local/src/bin/calc/{calc.c,help/sin}

NOTE:

The macOS vim version is: VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Apr 13 2024 12:28:29)

The Debian vim version is: VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 04 2023 10:24:44)

The RHEL vim version is: VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 09 2023 00:00:00)

Under RHEL vim, as an experiment, if we change augroup calc to be so that the /usr/local/src/bin/calc/* line is removed:

if has("autocmd")
    augroup calc
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/*/Makefile set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/rpm.mk set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab
    augroup END
endif

We find that :set softtabstop is set to 4 for ALL BUT THE Makefiles. In the above test when the /usr/local/src/bin/calc/* line is removed, the two Makefiles have :set softtabstop is set to 0!!!

QUESTION

What is it, under RHEL vim, that causes lines such as:

autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

set :set softtabstop is set to 0 for the Makefiles?

We can explicitly :set softtabstop=4 for a calc Makefile and it holds, but the autocmd! that matches the Makefile in the augroup calc does NOT set it to 4!

xexyl commented 4 months ago

Under macOS vim, the solution outlined in issuecomment-2145594040 works very well.

Under Debian Linux vim, it also works well.

Under RHEL vim, the settings for the various Makefiles do not work.

We at the bottom of the our ~/.vimrc file we have:


""""""""""""""""""""""""""""""""""""""""""""""

""""""""  On tabs and vim

""""""""""""""""""""""""""""""""""""""""""""""

" See: //stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces

"

" tabstop

"

"    The width of a hard tabstop measured in "spaces" -- effectively the

"    (maximum) width of an actual tab character.

"

:set tabstop=8

"

" shiftwidth

"

"    The size of an "indent". It's also measured in spaces, so if your

"    code base indents with tab characters then you want shiftwidth to

"    equal the number of tab characters times tabstop. This is also used

"    by things like the =, > and < commands.

"

:set shiftwidth=4

"

" softtabstop

"

"    Setting this to a non-zero value other than tabstop will make the

"    tab key (in insert mode) insert a combination of spaces (and possibly

"    tabs) to simulate tab stops at this width.

"

:set softtabstop=4

"

" expandtab

"

"    Enabling this will make the tab key (in insert mode) insert spaces

"    instead of tab characters. This also affects the behavior of the

"    retab command.

"

:set noexpandtab

"

" smarttab

"

"    Enabling this will make the tab key (in insert mode) insert spaces

"    or tabs to go to the next indent of the next tabstop when the cursor

"    is at the beginning of a line (i.e. the only preceding characters

"    are whitespace).

"

:set smarttab

""""""""""""""""""""""""""""""""""""""""""""""

"""""""" Special calc settings for calc source that is NOT a Makefile or *.mk

""""""""

"""""""" see: https://github.com/lcn2/calc/issues/151#issuecomment-2145594040

""""""""

"""""""" To test, try and see if 0 is the answer:

""""""""

""""""""    :set softtabstop

""""""""

""""""""""""""""""""""""""""""""""""""""""""""

if has("autocmd")

    augroup calc

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/* set shiftwidth=4 smarttab expandtab tabstop=8 softtabstop=0

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/*/Makefile set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/rpm.mk set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

    augroup END

endif

NOTE: We explicitly set various tab related files to a "default" before the augroup calc set.

We test using the following command:


vi ~/.vimrc /usr/local/src/bin/calc/{calc.c,Makefile,help/sin,help/Makefile}

While going thru the various files, we look at:


:set softtabstop

For macOS and Debian Linux vim, we see that :set softtabstop is 0 for only:

  • /usr/local/src/bin/calc/calc.c

  • /usr/local/src/bin/calc/help/sin

In all other cases :set softtabstop is set to 4 as it should be.

Under RHEL vim, however, :set softtabstop is 0 for:

  • /usr/local/src/bin/calc/calc.c

  • /usr/local/src/bin/calc/Makefile

  • /usr/local/src/bin/calc/help/sin

  • /usr/local/src/bin/calc/help/Makefile

while it is 4 for files outside of /usr/local/src/bin/calc such as ~/.vimrc is it should.

What is wrong is that the Makefiles under RHEL Linux vim, have :set softtabstop is 0 which is NOT correct.

Changing, under RHEL Linux, the order of files visited does not change the situation. This still has the same problem:


vi /usr/local/src/bin/calc/{Makefile,help/Makefile} ~/.vimrc /usr/local/src/bin/calc/{calc.c,help/sin}

NOTE:

The macOS vim version is: VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Apr 13 2024 12:28:29)

The Debian vim version is: VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 04 2023 10:24:44)

The RHEL vim version is: VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 09 2023 00:00:00)

Under RHEL vim, as an experiment, if we change augroup calc to be so that the /usr/local/src/bin/calc/* line is removed:


if has("autocmd")

    augroup calc

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/*/Makefile set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

    autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/rpm.mk set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

    augroup END

endif

We find that :set softtabstop is set to 4 for ALL BUT THE Makefiles. In the above test when the /usr/local/src/bin/calc/* line is removed, the two Makefiles have :set softtabstop is set to 0!!!

QUESTION

What is it, under RHEL vim, that causes lines such as:


autocmd! BufNewFile,BufWritePre,BufReadPre /usr/local/src/bin/calc/Makefile* set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab

set :set softtabstop is set to 0 for the Makefiles?

Perhaps you can share your entire .vimrc file and I can test it under rocky linux. Not sure if I will see the same behaviour but since it's RH maybe I will.

I am replying with the GitHub app which makes it harder to look at the comments so I can't see the version but I do know that at least some RH systems are behind so to say. Maybe that's related but I don't know.

But if you share the vimrc file I would be happy to do some testing tomorrow. If for some reason you don't want to share it here you can email it to me.

Perhaps I don't need the full file but want to make sure that I have the same state.

I am glad it works in the other systems at least.

Must get some sleep now. Good night! Will reply here and at the other repos tomorrow morning most likely before I have to leave for the ENT (but if not it's in my city so I should not be gone for very long).

.. one thought that might be completely unrelated but one should not need to have the autocmd groups. That was just to keep it kind of organised. You might not need the check of having the feature too.

Anyway I will get back tomorrow. Have a great sleep!

lcn2 commented 4 months ago

Here is the ~/.vimrc file in zip form to keep GitHub happy about the file tape:

vimrc.zip

UPDATE 0

This may be an important clue: When we remove the entire if has("autocmd") ... endif set from ~/.vimrc and them vim edit any Makefile (either under /usr/local/src/bin/calc OR anywhere else), we find that :set softtabstop is 0 for the Makefile and NOT for other non-Makefiles.

UPDATE 1

This may be an important clue too: Even when we copy /usr/local/src/bin/calc/rpm.mk to /tmp/rpm.mk, with the entire if has("autocmd") ... endif set from ~/.vimrc removed, we find that :set softtabstop is 0.

xexyl commented 4 months ago

I wonder what would happen if you installed a later version of vim in RH.

If it works then we might be able to guess that the problem is the version. I know for me with CentOS (more so than with Rocky Linux) there were a lot of things like that. But Rocky Linux also is behind in some packages.

Its pandoc is behind which is one of the reasons I have to generate html files for the IOCCC with the laptop (probably it would be fine under fedora but you know about that situation).

The same problem with flex and bison though that I think I was able to easily work out (it wasn't necessary but I wanted to test it).

Anyway I will look at this in more detail tomorrow.

If you can install a later version in RH that would be a good test. I can likewise test this issue with Rocky Linux and if the problem is there too then it is maybe the version.

In that case the question is if there are any workaround.

Okay I typed more than I expected but I will be going now. I see you attached the rc file. Thanks! Will look at it tomorrow.

Is it possible to do the version test too ?

Anyway good night! Hopefully we can work this out soon.

Is that a test you can do?

lcn2 commented 4 months ago

I wonder what would happen if you installed a later version of vim in RH.

If it works then we might be able to guess that the problem is the version. I know for me with CentOS (more so than with Rocky Linux) there were a lot of things like that. But Rocky Linux also is behind in some packages.

Its pandoc is behind which is one of the reasons I have to generate html files for the IOCCC with the laptop (probably it would be fine under fedora but you know about that situation).

The same problem with flex and bison though that I think I was able to easily work out (it wasn't necessary but I wanted to test it).

Anyway I will look at this in more detail tomorrow.

If you can install a later version in RH that would be a good test. I can likewise test this issue with Rocky Linux and if the problem is there too then it is maybe the version.

In that case the question is if there are any workaround.

Okay I typed more than I expected but I will be going now. I see you attached the rc file. Thanks! Will look at it tomorrow.

Is it possible to do the version test too ?

Anyway good night! Hopefully we can work this out soon.

Is that a test you can do?

Given issuecomment-2224269696, perhaps there is some system wide "Makefile-like" setting that is overriding vim settings?

We can explicitly set :set softtabstop=4 for a Makefile and that works. It seems as if vim detects a Makefile and forces :set softtabstop rot be 0.

That may be a configuration issue that a vim version upgrade might not fix?

UPDATE 0

We can copy any calc Makefile to /tmp/Makefile, remove the entire if has("autocmd") ... endif set from ~/.vimrc and when we vim /tmp/Makefile we find that :set softtabstop defaults to 0.

We can manually do :set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab while vim editing /tmp/Makefile and then the :set softtabstop is 4.

UPDATE 1

We installed version 9.1.550 on both macOS and RHEL9. It seems that was some default behaviors that changed from Vim8 to Vim9. Now the if has("autocmd") ... endif works as expected.

Here is the updated ~/vimrc in zip form to keep GitHub happy: comments and corrections welcome.

vimrc.zip

xexyl commented 4 months ago

I wonder what would happen if you installed a later version of vim in RH. If it works then we might be able to guess that the problem is the version. I know for me with CentOS (more so than with Rocky Linux) there were a lot of things like that. But Rocky Linux also is behind in some packages. Its pandoc is behind which is one of the reasons I have to generate html files for the IOCCC with the laptop (probably it would be fine under fedora but you know about that situation). The same problem with flex and bison though that I think I was able to easily work out (it wasn't necessary but I wanted to test it). Anyway I will look at this in more detail tomorrow. If you can install a later version in RH that would be a good test. I can likewise test this issue with Rocky Linux and if the problem is there too then it is maybe the version. In that case the question is if there are any workaround. Okay I typed more than I expected but I will be going now. I see you attached the rc file. Thanks! Will look at it tomorrow. Is it possible to do the version test too ? Anyway good night! Hopefully we can work this out soon. Is that a test you can do?

Given issuecomment-2224269696, perhaps there is some system wide "Makefile-like" setting that is overriding vim settings?

We can explicitly set :set softtabstop=4 for a Makefile and that works. It seems as if vim detects a Makefile and forces :set softtabstop rot be 0.

That may be a configuration issue that a vim version upgrade might not fix?

UPDATE 0

We can copy any calc Makefile to /tmp/Makefile, remove the entire if has("autocmd") ... endif set from ~/.vimrc and when we vim /tmp/Makefile we find that :set softtabstop defaults to 0.

We can manually do :set tabstop=8 shiftwidth=4 softtabstop=4 noexpandtab smarttab while vim editing /tmp/Makefile and then the :set softtabstop is 4.

UPDATE 1

We installed version 9.1.550 on both macOS and RHEL9. It seems that was some default behaviors that changed from Vim8 to Vim9. Now the if has("autocmd") ... endif works as expected.

Here is the updated ~/vimrc in zip form to keep GitHub happy: comments and corrections welcome.

vimrc.zip

I wondered about it. So would it work with the older version without the if has ... part?

And you closed this issue so should I concern myself further with this? Let me know and I'll go from there! I'll check your vimrc file just as I'm curious what you have.

lcn2 commented 4 months ago

I wondered about it. So would it work with the older version without the if has ... part?

It did. But the behavior of vim8 had different consequences for files it detected as Makefiles that changes the defaults even when that if has section was completely removed with vim8.

That is why we have switched to compiling the latest vim (9.1.something) from the vim repo. The compile and install is fairly straightforward and now we have a consistent vim9 environment across all platforms.

And you closed this issue so should I concern myself further with this? Let me know and I'll go from there! I'll check your vimrc file just as I'm curious what you have.

Comments in the .vimrc would be appreciated, @xexyl .