dykstrom / basic-mode

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

Decouple auto-number from line-number-cols #13

Closed hackerb9 closed 1 year ago

hackerb9 commented 2 years ago

Auto-number does not work unless line-number-cols is also set, which is not always desirable.

It is not clear to me why it is required. If I understand correctly, line-number-cols reserves a margin so that line numbers will be right aligned, like so:

    10 GOTO 100
   100 GOSUB 1000
  1000 RUN 10

Line-number-cols can cause problems when collaborating with people who have not yet accepted Emacs as the one true editor :wink: and who still left-align their line numbers.

It would be nice if auto-number was a separate feature and could be turned on independently without having to even know about line-number-cols. I suggest that if a person has line-number-cols set to the default (0), line numbers should simply be left aligned when auto-number is set. Like so:

10 RETURN
100 NEXT
1000 ERROR 42

Thanks!

dykstrom commented 2 years ago

This sounds like a good idea. I will have a look and see if I can do something about it. However, as you probably saw, I haven't worked with basic-mode for a while, so it will take me some time to get into it again.

hackerb9 commented 2 years ago

Hopefully it will not take you much time to fix. I have discovered that setting line-number-cols to 1 seems to do exactly what I believe it should do when set to 0.

dykstrom commented 2 years ago

Well, now it is fixed, maybe. I meant to push the change to branch, but instead I pushed it to master, so now it has been released, I guess. Please try it out!

hackerb9 commented 2 years ago

Thanks! Maybe I'm doing something wrong but it is not working for me.

It appears the basic-newline-and-number does not like it when auto-number is nil. (Or t, for that matter.)

Debugger entered--Lisp error: (wrong-type-argument numberp nil)
  int-to-string(nil)
  (insert (int-to-string new-line-number))
  (let* ((current-line-number (basic-current-line-number)) (next-line-number (s$
  basic-newline-and-number()
  funcall-interactively(basic-newline-and-number)
  call-interactively(basic-newline-and-number nil nil)
  command-execute(basic-newline-and-number)

When I (setq basic-auto-number 10), I am able to increment numbers if the cursor is on a numbered line, but even then, I get an error if I hit Enter on a blank line.

hackerb9 commented 2 years ago

It looks like the problem is at line no. 497 where two whens got folded into one. When I add back in the missing (when new-line-number ..., I stop getting errors. (But see below).


Edit: The nested when syntax is a bit confusing. It may be clearer to leave line 497 alone and instead make line 505 conditional, like so:

(if new-line-number (basic-indent-line))))


Edit 2: Oops. I did say the nested whens were confusing, right? To avoid that, the correct line to edit is line no. 504, which should look like this:

(if new-line-number (insert (int-to-string new-line-number)))
dykstrom commented 2 years ago

Oh, bad testing on my side. I will try to fix it as soon as possible. Thanks for the debugging!

dykstrom commented 2 years ago

Now there should be a fixed version published. Let's hope it works this time. :)

hackerb9 commented 2 years ago

Works perfectly! That will make collaboration much easier.

By the way, the documentation for basic-line-number-cols may need to be updated.

Lines 62–65 currently read:

;; You can also customize the number of columns to allocate for line            
;; numbers, see variable `basic-line-number-cols'.  The default value           
;; is 0, which means not using line numbers at all.                             

I suggest changing that to say:

;; You can also customize the number of columns to allocate for            
;; line numbers using the variable `basic-line-number-cols'. The           
;; default value of 0 (no space reserved) is appropriate for              
;; programs with no line numbers and for left aligned numbering.           
;; Use a positive integer, such as 6, if you prefer right alignment.                                     

Lines 113–118 currently read:

(defcustom basic-line-number-cols 0
  "*Specifies the number of columns to allocate to line numbers.           
This number should include the single space between the line number and    
the actual code.  Set this variable to 0 if you do not use line numbers."
  :type 'integer
  :group 'basic)

I suggest changing that to:

(defcustom basic-line-number-cols 0
  "*Specifies the number of columns to allocate to line numbers.           
This number includes the single space between the line number and          
the actual code. Leave this variable at 0 if you do not use line           
numbers or if you prefer left aligned numbering. A positive value          
adds sufficient padding to right align a line number and add a             
space afterward. The value 6 is reasonable for older dialects of           
BASIC which used at most five digits for line numbers."
  :type 'integer
  :group 'basic)

Lines 690 to 697 currently read:

(define-derived-mode basic-mode prog-mode "Basic"
  "Major mode for editing BASIC code.                                           
Commands:                                                                       
TAB indents for BASIC code. RET will insert a new line starting                 
with a fresh line number if line numbers are turned on.                         

To turn on line numbers, customize variables `basic-auto-number'                
and `basic-line-number-cols'.                                                   

\\{basic-mode-map}"

I suggest changing that to

(define-derived-mode basic-mode prog-mode "Basic"
  "Major mode for editing BASIC code.                                      

Commands:                                                                  

\\[indent-for-tab-command] indents for BASIC code.                         

\\[newline] can automatically insert a fresh line number if                
`basic-auto-number' is set. (Default is disabled).                         

Customization:                                                             

You can customize the indentation of code blocks, see variable             
`basic-indent-offset'.  The default value is 4.                            

Formatting is also affected by the customizable variables                  
`basic-delete-trailing-whitespace' and `delete-trailing-lines'             
(from simple.el).                                                          

You can also customize the number of columns to allocate for line          
numbers using the variable `basic-line-number-cols'. The default           
value of 0, no space reserved, is appropriate for programs with            
no line numbers and for left aligned numbering. Use a larger               
value if you prefer right aligned numbers. Note that the value             
includes the space after the line number, so 6 right aligns                
5-digit numbers.                                                           

The other line number features can be configured by customizing            
the variables `basic-auto-number', `basic-renumber-increment' and          
`basic-renumber-unnumbered-lines'.                                         

\\{basic-mode-map}"