reddit / emacs

Renumber elisp script?

waspentalive · 2022-04-27 10:27
Anyone have an eLisp script that will renumber a BASIC program being edited? (specifically bwbasic) Is there a place where elisp scripts are shared or do they tend to be special-purpose mostly?
waspentalive · 2022-04-27 10:35
p.s. I know bwbasic can be numberless - but it can also be numbered and it does not have it's own renumber command.
wasamasa · 2022-04-27 12:04
I think it would help to actually specify what renumbering means, since most people here are familiar with structured programming. Here's what I found so far: A BASIC program uses numbered lines so that commands like 'GOTO' can specify a label to jump to. When the numbers are changed, the arguments for these commands need to be changed accordingly. Renumbering would replace the line numbers so that they'd be in ascending order with gaps between them to easily insert new lines. https://github.com/pangfengliu/programmingtasks/issues/255 shows an actual example with sample input/output.
flexibeast · 2022-04-27 12:13
> I think it would help to actually specify what renumbering means, since most people here are familiar with structured programming. i used various BASICs - like on a BBC B and a TRS-80, amongst others - before any other programming language .... Yes, i'm old. :-)
waspentalive · 2022-04-27 12:16
What would need to be done... Optional - examine the program to determine a good range of numbers so that each line number is the same length (100 110 120 ... 990 or 1010 1020 ... 9990) maybe even spacing the numbers (please do not use consecutive numbers so the programer can add lines between existing lines) so the number completely fill the range ex: 1000 to 9990 1 for each line in the program record the new line number 3) strip out the old numbers and replace them with the new numbers on n gosub ...) and change them to match the new numbers. Print Using statements can reference image statements by line number too. 3) strip out the old numbers and replace with the new numbers
wasamasa · 2022-04-27 12:47
Here's a script to get you started: (require 'rx) (defun my-renumber () (interactive) (let* ((line-count (count-lines (point-min) (point-max))) (line-table (make-hash-table :test #'equal)) (line-number-rx (rx (group (one-or-more (any "0-9"))) " ")) (line-ref-rx (rx (or "GOTO" "GOSUB" "THEN" "RESTORE") " " (group (one-or-more (any "0-9")))))) (save-excursion (goto-char (point-min)) (let ((line 1)) (while (not (eobp)) (when (looking-at line-number-rx) (let ((old-line-number (match-string 1)) (new-line-number (number-to-string (* line 100)))) (replace-match new-line-number nil nil nil 1) (puthash old-line-number new-line-number line-table))) (forward-line 1) (setq line (1+ line)))) (goto-char (point-min)) (while (re-search-forward line-ref-rx nil t) (let* ((old-line-number (match-string 1)) (new-line-number (gethash old-line-number line-table))) (if new-line-number (replace-match new-line-number nil nil nil 1) (warn "Unknown line reference: %s" old-line-number))))))) As for the questions where to find such scripts, there is not really a central exchange. People have put them on the Emacs wiki, on discussion forums (such as this one or Stackoverflow/Stackexchange), on code forges (like Github), turned them into packages and uploaded to package archives, ...
waspentalive · 2022-04-27 12:49
Thanks - I will study this.
_viz_ · 2022-04-27 14:42
I immediately understood it. :P I was taught QBASIC in sixth or seventh grade, that should be mid-2010s. We did some basic logic, and used a graphic library. I don't remember if it was turtle however.
trs_80 · 2022-04-27 19:22
Is there some BASIC (or similar) mode in Emacs? That would have been the first place I would have looked. If you get wasamasa script working, it might be a nice addition to something like that (if it exists) or maybe you end up making your own (new/improved?) BASIC mode. I suppose it would be nice for syntax highlighting and stuff like this at least.
thevoidzero · 2022-04-27 19:59
Fellow comrade who started with QBASIC. I refuse to accept I'm old dammit.
thevoidzero · 2022-04-27 20:02
There seems to be basic mode basic-mode 20210316.1253 new melpa major mode for editing BASIC code Haven't tried it but considering python and other modes have refactor and rename variables, basic probably have it. Wait just read the description it has it. > When line numbers are turned or, hitting the return key will insert a new line starting with a fresh line number. Typing C-c C-r will renumber all lines in the region, or the entire buffer, including any jumps in the code. https://melpa.org/#/basic-mode Here you go OP u/waspentalive
trs_80 · 2022-04-27 20:11
> a TRS-80 You rang? :D
trs_80 · 2022-04-27 20:15
It looks like there may be a BASIC mode with a function that does this already, but I upvoted you anyway for effort. And also because, I have often enough found myself (re-?)implementing stuff, then announcing it in #emacs with much excitement, only to have wgreenhouse tell me it already exists. :D Cheers, wasamasa!
[deleted] · 2022-04-28 02:24
[deleted]
waspentalive · 2022-04-28 05:44
Woot - I will have to study to implement but this seems to fit the bill nicely. For example - I will have to find out where my emacs init file lives so I can add the needed lines to it.