1. ed.ex :: auto-loading directory files into ed.ex's file_history

This routine is working pretty well for me. Any suggestions for improvement will certainly be appreciated. Files are loaded from the initialy from the startup directory, and each time a file is loaded from a new directory.

-- 
-- set sequence file_name to "null" 
sequence file_name = "null"   --<<----** 
 
 
 
procedure load_files_from_directory(sequence answer) 
object tmp = {} 
 
-- when a file from a new directory is loaded, 
-- files from the new directory are automatically 
-- loaded into the file_history. 
 
-- make the new directory the current directory, 
-- obtain the path from the filename, if the file_name 
-- does not contain an absolute path, then the "active" 
-- directory is the current directory: 
if absolute_path(file_name) then  ------- at startup, "null" is not an absolute path 
    chdir(dirname(file_name))     ------- so the current directory is read: 
end if 
 
-- for x = 1 to length(E_FILES) do 
--  tmp &= dir(sprintf("%s",E_FILES[x])) 
-- end for 
tmp = sort(dir("*.*")) --<<------- this doesn't really help 
-- tmp = dir("*.e") 
-- tmp &= dir("*.ex") 
if not atom(tmp)then 
    if length(tmp)>1 then 
	for x = 3 to length(tmp) do 
	    file_history = update_history(file_history, tmp[x][D_NAME]) 
	end for 
    end if 
    else file_history &= tmp 
end if  
end procedure 
 
load_files_from_directory() --<<----** 
 
 
-- procedure above inserted just before this routine:------- 
procedure delay(atom n)------------------------------------- 
------------------------------------------------------------ 
-- 
-- the routine is called from the "new file" routine near 
-- the end of the procedure "get_escape(boolean help)": 
--  
          elsif command[1] = 'n' 
----- this code block: 
		if length(answer) != 0 then 
			file_name = answer 
			stop = TRUE 
		end if 
 
------ becomes this: 
 
 	       if length(answer) != 0 then 
	          load_files_from_directory(answer) --<<------- 
	           
                   -- instead of loading an html file, 
                   -- launch a browser. 
                  if match(".html", answer) then 
		    system_exec(BROWSER & " " & answer, 0) 
	           
               else 
		 file_name = answer 
		 stop = TRUE 
	       end if 
-------------------------------------------------------------- 
 
-- finally, in 
procedure ed_main(sequence command) 
 
-- this code block: 
        file_name = delete_trailing_white(file_name) 
	if length(file_name) = 0 then 
		abort(1) -- file_name was just whitespace - quit 
	end if 
-- insert this call to the load_files_from_directory procedure:	 
load_files_from_directory(file_name)  --<------------------------ 
----------------------------------------------------------------- 
        file_history = update_history(file_history, file_name) 
	file_no = open(file_name, "r") 
 
 
 
 
 

My attempts at selectively loading the E_FILES with a for loop failed. Selecting files with dir(*.ex) and dir(*.ex) works ok.

Regards, Kenneth Rhodes

new topic     » topic index » view message » categorize

2. Re: ed.ex :: auto-loading directory files into ed.ex's file_history

True to form, I left out a couple of steps:

The sequence file_name should be initialized with a value that does not contain an absolute path. I simply set it to "null".

sequence file_name = "null" 

Also, the first call to the procedure load_files_from_directory should be made before ed gets started initializes with the first file_name, so you may as well call it immediately after the procedure itself. I edited the original post with two arrows

 --<<----** 

pointing to the two needed changes.

Regards, Kenneth Rhodes

new topic     » goto parent     » topic index » view message » categorize

3. Re: ed.ex :: auto-loading directory files into ed.ex's file_history

Well, making each new directory visited the current directory, proved to be "problematic". My current solution is to make the newly visited directory the current directory, only to load its files into the file_history with the path prepended so that all files not located in the startup directory, have absolute paths. Once the new directory files, with absolute paths, have been loaded into the file_history, the startup directory is immediately made the current directory again.

-- this routine is called only once 
procedure load_startup_directory_files() 
object tmp = {} 
integer r = 0 
tmp = dir("*.*") 
if not atom(tmp)then 
    if length(tmp)>1 then 
	for x = 1 to length(tmp) do 
	    file_history = update_history(file_history, tmp[x][D_NAME]) 
	end for 
    end if 
    else file_history &= tmp 
end if 
end procedure 
load_startup_directory_files() 
 
-- this routine is called when a file is created or 
-- opened in a new directory 
-- the path is received from the "new file" routine: 
procedure load_files_from_new_directory(sequence path) 
object tmp = {} 
integer r = 0 
tmp = dir("*.*") 
if not atom(tmp)then 
    if length(tmp)>1 then 
	 
        for x = 1 to length(tmp) do 
	   -- the path & SLASH are prepended to the file name   
          file_history = update_history(file_history, sprintf("%s", {path & SLASH & tmp[x][D_NAME]})) 
	end for 
        -- command history update with templates, no need to type in long paths: 
	command_history = update_history(command_history, sprintf("ls %s", {path & SLASH})) 
        command_history = update_history(command_history, sprintf("ls *.ex *.e %s", {path & SLASH})) 
    end if 
end if 
 
-- at the end of code which processes the "new file" ESCAPE + 'n' command insert 
-- the following code: 
 
   elsif command[1] = 'n' then 
 
-- insert these lines: 
    
	    if absolute_path(answer) then 
		path = (dirname(answer)) 
		if chdir(dirname(answer)) then 
		    load_files_from_new_directory(path) 
		    chdir(init_curdir()) 
		elsif match(".html", answer) then 
		    system_exec(BROWSER & " " & answer, 0) 
		end if 
	    end if 
		file_name = answer 
		stop = TRUE 
	end if 
-- between this line: 
               if length(answer) != 0 then 
 
-- and these lines: 
   
			file_name = answer 
			stop = TRUE 
		end if 
 
-- in the procedure: 
procedure ed(sequence command) 
 
-- insert this line: 
	 
        load_startup_directory_files() 
 
-- before this line: 
        if length(command) >= 3 then 
 
new topic     » goto parent     » topic index » view message » categorize

4. Using fileext to filter directory files

My latest tweaks to the routines that load directory files into ed.ex's file_history sequence.

fileext is used to select only file specs contained in E_FILES.

procedure load_startup_directory_files() 
-- load E_FILES into file_history: 
 
    object tmp = {} 
    tmp = dir("*.*") 
    if not atom(tmp)then 
	if length(tmp)>1 then 
	    for x = 1 to length(tmp) do 
		if find("." & fileext(tmp[x][D_NAME]), E_FILES) then  --<--------- 
		    file_history = update_history(file_history, tmp[x][D_NAME]) 
		end if  
	    end for 
	end if 
    end if 
end procedure 
 
procedure load_files_from_new_directory(sequence path) 
-- load E_FILES into file_history 
-- an absolute path is created by prepending  
-- the path to the new directory 
 
object tmp = {} 
    tmp = dir("*.*") 
    if not atom(tmp)then 
	if length(tmp)>1 then 
	    for x = 1 to length(tmp) do 
		if find("." & fileext(tmp[x][D_NAME]), E_FILES) then  --<--------- 
		    file_history = update_history(file_history, sprintf("%s", {path & SLASH & tmp[x][D_NAME]})) 
		end if 
	    end for 
	    command_history = update_history(command_history, sprintf("ls %s", {path & SLASH})) 
	    command_history = update_history(command_history, sprintf("ls *.ex *.e %s", {path & SLASH})) 
	end if 
    end if 
end procedure 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu