ed.ex modifications
- Posted by Jacques Deschenes <desja at QUEBECTEL.COM> Nov 12, 1996
- 1593 views
I added a modifyIndent procedure to ed.ex to modify the indentation of a number of lines at once. This message include the needed code. 1) make a backup of ed.ex 2) paste this two lines to special input characters constant SHIFT_TAB = 271, -- key for block indent CTRL_TAB = 404 -- key for block undent 3) add those 2 constants to CONTROL_CHARS SHIFT_TAB, CTRL_TAB 4) paste the following lines to edit_file() if key = ... then elsif key = SHIFT_TAB then ModifyIndent(1) elsif key = CTRL_TAB then ModifyIndent(0) 5) paste the following code before edit_file() function TabsFirst(sequence line) -- if the line start with a mix of tabs and spaces, -- place tabs first. -- if number of spaces >= edit_tab_width replace with tabs integer FirstSpace, SpaceCount, j j = 1 FirstSpace = 0 SpaceCount = 0 while find(line[j]," \t") do if line[j] = '\t' and FirstSpace > 0 then line[FirstSpace] = '\t' line[j] = ' ' FirstSpace = FirstSpace + 1 elsif line[j] = ' ' then if FirstSpace = 0 then FirstSpace = j end if SpaceCount = SpaceCount + 1 end if j = j + 1 end while if SpaceCount >= edit_tab_width then line = line[1..FirstSpace-1] & repeat('\t',floor(SpaceCount/edit_tab_width)) & repeat(' ',remainder(SpaceCount,edit_tab_width)) & line[j..length(line)] end if return line end function --TabsFirst() procedure ModifyIndent(integer action) -- if action = 1 add a tab at beginning of each line. -- if action = 0 undent a tab at beginning of each line or if no tab -- suppress a blanck. integer first, last, tab, space, BlkCount, LoopCount sequence inp inp = {1,0} while inp[1] do if action then set_top_line("Number of lines to Indent? (+ down, - up) ") else set_top_line("Number of lines to undent? (+ down, - up) ") end if inp = value(key_gets("")) end while if inp[2] = 0 then normal_video() return end if if inp[2] < 0 then first = b_line + inp[2] + 1 if first < 1 then first = 1 end if last = b_line else first = b_line last = b_line + inp[2] - 1 if last > length(buffer) then last = length(buffer) end if end if inp = {1,0} while inp[1] do if action then set_top_line("Number of blancks to insert? ") else set_top_line("Number of blancks to remove? ") end if inp = value(key_gets("")) end while BlkCount = inp[2] if BlkCount < 1 then normal_video() return end if if action then -- indent for i = first to last do tab = floor(BlkCount/edit_tab_width) space = remainder(BlkCount,edit_tab_width) buffer[i] = repeat('\t',tab) & repeat(' ',space) & buffer[i] buffer[i] = TabsFirst(buffer[i]) end for else -- undent for i = first to last do LoopCount = BlkCount while LoopCount > 0 do if buffer[i][1] = ' ' then buffer[i] = buffer[i][2..length(buffer[i])] LoopCount = LoopCount - 1 elsif buffer[i][1] = '\t' then if LoopCount < edit_tab_width then space = edit_tab_width - LoopCount buffer[i] = repeat(32,space) & buffer[i][2..length(buffer[i])] LoopCount = 0 else buffer[i] = buffer[2..length(buffer[i])] LoopCount = LoopCount - edit_tab_width end if else exit end if buffer[i] = TabsFirst(buffer[i]) end while end for end if b_col = 1 s_col = 1 set_modified() normal_video() DisplayWindow(b_line - s_line + 1, 1) end procedure -- ModifyIndent() Jacques Deschenes Baie-Comeau, Quebec Canada desja at quebectel.com