;+ ; NAME: ; www_help_change_tabs ; PURPOSE: ; Changes text written with tab length of tab_len_from to text ; with a tab length of tab_len_to ; CATEGORY: ; www_help ; CALLING SEQUENCE: FUNCTION www_help_change_tabs, line, tab_len_from, tab_len_to ; INPUTS: ; line scalar; type: string ; string to be checked; the comment char at the ; beginning of the line has already been stripped. ; OPTIONAL INPUT PARAMETERS: ; tab_len_from scalar; type: integer; default: 4 ; tab_len_to scalar; type: integer; default: 8 ; OUTPUTS: ; R scalar; type: string ; updated value of input value of 'line' ; INCLUDE: @compile_opt.pro ; On error, return to caller ; PROCEDURE: ; In the IDL editor a tab is usually set to 4 spaces. ; HTML usually sets a tab equal to 8 spaces. This procedure ; changes from IDL to HTML format while retaining text alignment. ; MODIFICATION HISTORY: ; JAN-2001, Paul Hick (UCSD/CASS) ; JUN-2001, Paul Hick (UCSD/CASS); fixed a bug which crashed the procedure when ; processing a line containing just a single tab. ; MAY-2003, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ; Changed to process lines with comment char already removed instead ; of still present. ;- InitVar, tab_len_from, 4 InitVar, tab_len_to , 8 IF tab_len_from EQ tab_len_to THEN RETURN, line IF strlen(strtrim(line)) EQ 0 THEN RETURN, '' ; Empty line tab = 9B space = 32B a = byte(line) n = n_elements(a) ; # chars still needed to be processed (always non-zero) tab_pos = -1 ; Location of last tab WHILE n GT 0 DO BEGIN ; Check for tabs starting at position following previous tab a = a[tab_pos+1:*] tab_pos = (where(a EQ tab))[0] IF tab_pos EQ -1 THEN BEGIN ; No more tabs IF n_elements(b) EQ 0 THEN b = a ELSE b = [b,a] n = 0 ; Stop ENDIF ELSE BEGIN IF n_elements(b) EQ 0 THEN BEGIN ; First tab ; The first tab while b does not exist must be the one immediately following the ; comment char. Replace it by 4 spaces. ; 'add' should be '' (blank) or ' >', unless code is included in the header. ; Do the replacement only if tab_pos is in pos 0..tab_len_from-1. IF tab_pos NE 0 THEN BEGIN b = a[0:tab_pos-1] IF tab_pos LT tab_len_from THEN b = [b,replicate(space,tab_len_to-tab_len_from)] ENDIF ELSE $ b = replicate(space,tab_len_to-tab_len_from) ENDIF ELSE $ IF tab_pos NE 0 THEN b = [b,a[0:tab_pos-1]] ; If tab_pos is the last character in the line than it is tempting to skip it. ; However, if a single-char line containing just a tab is processed then b will not ; exist, and the procedure crashes a few lines down!! i = tab_pos-tab_pos/tab_len_from*tab_len_from ; # char to next tab stop add = replicate(space,tab_len_from-i) ; Fill with spaces IF n_elements(b) EQ 0 THEN b = add ELSE b = [b,add] n = n-tab_pos-1 ENDELSE ENDWHILE b = strtrim(string(b)) n = strlen(b) ; Now reduce the length of the string by replacing sequences of spaces with ; tabs. Tab stops are now at multiples of tab_len_to ; The for loop variable is the last position of a tab field, i.e. 7,15,22 ; if tab_len_to is 8 (processed in reverse order). space = string(space) tab = string(tab ) FOR i=(n-1)/tab_len_to*tab_len_to-1, tab_len_to-1, -tab_len_to DO BEGIN j = 0 WHILE j LT tab_len_to AND strmid(b,i-j,1) EQ space do j = j+1 IF j GT 1 THEN b = strmid(b,0,i-j+1)+tab+strmid(b,i+1) ENDFOR RETURN, b & END