dykstrom / basic-mode

Emacs major mode for editing BASIC code
GNU General Public License v3.0
7 stars 10 forks source link

Fix tabification bug when indenting code with line numbers #28

Closed hackerb9 closed 1 year ago

hackerb9 commented 1 year ago

Previously, basic-indent-line-to removed the line number and called indent-line-to which inserts tabs, presuming tab stops every tab-width spaces. Then it prepended the line number to the beginning, which had the side effect of changing how many spaces the first tab was equivalent to.

For example, presuming a tab-width of 8, then (basic-indent-line-to 4) uses four spaces, but (basic-indent-line-to 8) uses one tab. Since spaces are for relative positioning, while tabs provide (relatively) absolute positioning, prepending less than 8 characters of text would shift over the four spaces but not change the tab.

This patch fixes that by simply untabifying the result from indent-line-to; that is, all tabs are turned into the appropriate number of spaces so everything is using relative positioning.

ADDITIONAL DETAILS

A better fix might be to retabify the line after prepending the line number. After all, tabs can be useful and some programmers prefer them. However, after adding the code to do that, I realized there was a fundamental flaw: the benefits of tabs only make sense if they are actually used for tabulation. That is, the code has to align, at least partially, with the tab-width, which essentially means that basic-line-number-cols would be fixed at a multiple of tab-width.

Since alignment is unlikely to be the case, tabs would actually make navigation in Emacs more difficult. For example, imagine point is on column 14, at the start of a FOR statement and the next line is indented to column 18. Where does C-n (next line) go to? If tabs are used, the answer is 16, which is ridiculous and not helpful to anyone.

Trying to align the code with the tab-width -- or checking if it is -- doesn't seem worth it to me, so I left the re-tabification code commented out. At least for now. Feel free to delete it, if you wish.

dykstrom commented 1 year ago

I realize there is a problem with using line numbers and tabs together, as you describe. But I think the tab problem only occurs when also using line numbers. Therefore, I think we should only untabify the line if there actually is a line number on it. Some people prefer tabs, and without the line numbers the tabs work (mostly). What do you think?

hackerb9 commented 1 year ago

I realize there is a problem with using line numbers and tabs together, as you describe. But I think the tab problem only occurs when also using line numbers. Therefore, I think we should only untabify the line if there actually is a line number on it. Some people prefer tabs, and without the line numbers the tabs work (mostly). What do you think?

I do not have enough experience editing BASIC without line numbers, but I think you are correct that untabification would not be necessary if there are no line numbers. This would also be true if basic-line-number-cols equals eight.