1. Blast from the past - "prefix macros" for ed.ex by Mike Nelson

Super Star Euphoria Programmer Mike Nelson wrote this very powerful little routine which is kind of like the .commands in the Vi editor.

Essentially you can assign a series of actions to a label of your choice. When you want to execute the commands you type a semi-colon followed by the label and then hit the space bar. The label is erased together with the prefixed semi-colon and then the commands are executed. The possibilities are endless.


 
-- enter the following routine immediately before the procedure try_auto_complete(char key) 
function prefix_macro()  --  Mike Nelson  10/14/2000                                           
   sequence this_line, macro                                                                   
   integer count,index,c                                                                       
                                                                                               
   this_line=buffer[b_line]                                                                    
   count=0 index=1                                                                             
   for i=b_col-1 to 1 by -1 do                                                                 
      c=this_line[i]                                                                           
      if not ((c>='a' and c<='z') or (c>='A' and c<='Z')                                       
      or (c>='0' and c<='9') or c=';') then                                                    
         if count<2 then return 0 end if                                                       
         index=i+1                                                                             
         exit                                                                                  
      end if                                                                                   
      count+=1                                                                                 
   end for                                                                                     
                                                                                               
   if this_line[index]!=';' then                                                               
    return 0                                                                                   
   end if                                                                                      
                                                                                               
    macro=this_line[index+1..index+count-1] 
   add_queue(repeat(BS,length(macro)+1))                                                  
                                                                                               
 --  insert the labels and commands you write in the following list of elsif clauses: 
 --  I have included several examples to get you started. 
                                                                                              
    if equal(macro, "mc") then                                                                 
     -- a macro to enter macros                                                                
        add_queue(HOME & "\telsif equal(macro,\"\") then" & CR & "\tadd_queue(\"\")"           
                    & END & repeat(BS, 4)                                                      
                    & ARROW_UP & HOME & END                                                    
                    & repeat(ARROW_LEFT,7))                                                    
                                                                                               
    elsif equal(macro,"aq") then                                                               
        -- add_queue, a macro to enter macros                                                  
        -- in case another "add-queue" line is needed                                          
        add_queue( END & CR & HOME & "\t\tadd_queue(\"\")"                                     
        & repeat(ARROW_LEFT,2)) 
 
        elsif equal(macro, "c") then   -- compile the current file 
	shell("euc " & file_name) 
	add_queue(CONTROL_B & CONTROL_T)         
     
    elsif equal(macro,"il") then    -- shroud the current file 
	shell("eushroud " & file_name) 
	add_queue(CONTROL_B & CONTROL_T)         
	     
    elsif equal(macro,"b") then     -- bind the current file 
	shell("eubind " & file_name) 
	add_queue(CONTROL_B & CONTROL_T)         
	 
    elsif equal(macro, "mlc") then                                                             
	add_queue("/*  */" & repeat(ARROW_LEFT,3))                                          
	 
    elsif equal(macro, "sq") then                                                            
	add_queue("\' \'" & repeat(ARROW_LEFT,2))                                           
	 
    elsif equal(macro, "dq") then                                                            
	add_queue("\" \"" & repeat(ARROW_LEFT,2))                                           
	 
    elsif equal(macro, "bt") then                                                             
	add_queue("` `" & repeat(ARROW_LEFT,2))       
 
       else 
	return 0 
    end if 
    return 1 
end function 
                         
-- next insert prefix macro code in the  
procedure try_auto_complete(char key) 
    -- check for a keyword that can be automatically completed 
    sequence word, 
    this_line, white_space, leading_white, begin 
    natural first_non_blank, wordnum 
    
-- insert this code:  
    if prefix_macro() then              -- for the prefix_macro routine 
	return 
    end if 
------------------------------ 
     

And you are set to go!

Kenneth Rhodes

new topic     » topic index » view message » categorize

2. Re: Save as routine

OOPS! I deleted the "sa", "Save as" micro. The file, although renamed, was being saved before the macro label could erase itself.

A "save as" routine could be added to the get_escape elsif clauses like so:

-- insert the following code into: 
procedure get_escape(boolean help) 
 
-- add 'W' to the list of hot-keys in the following code section:  
command = key_gets("hcqswnedfrlmW/`\'\"", {}) & ' ' 
 
    elsif command[1] = 'W' then   
        copy_file(file_name, file_name & ".bak", 1)  -- back up original file. 
	set_top_line(" ") 
	file_name = prompt_string("Save file as: ") 
	save_file(file_name) 
	stop = FALSE 
	refresh_all_windows() 
 
new topic     » goto parent     » topic index » view message » categorize

3. There is more than one way to skin a cat

  
--note that this prefix macro code: 
elsif equal(macro, "il") then 
       shell("eushroud " & file_name) 
	normal_video() 
	goto_line(0, b_col)  -- refresh screen 
 
-- does the same thing as this code: 
elsif equal(macrol, "il") then 
      add_queue(ESCAPE & "d" & " eushroud " & file_name & CR) 
 
-- and this code: 
    elsif equal(macro,"il") then    -- shroud the current file  
	shell("eushroud " & file_name)  
	add_queue(CONTROL_B & CONTROL_T)          
 
------------------------------------------------------------- 
 
-- if you choose to add a hot-key shell command   
-- to the get-escape elsif clauses, to execute a shell command 
-- you can avoid adding the calling command letter to the list  
-- of hot-keys by choosing the second method above: 
 
    elsif command[1] = 'S' then 
      add_queue(ESCAPE & "d" & " eushroud " & file_name & CR)  -- note the final CR 
 
-- one advantage of using a prefix-macro instead of ESCAPE + hotkey 
-- is that the prefix macro calling command or label can be multiple letters 
-- allowing for a more "readable" or easy to remember mnemonic code: 
--   ;il may be easier to associate with shroud than the "S".  
 
-- perhaps a better example - in my original post for multi-line quotes 
-- I chose the prefix macro latel "bt" for back- tick, but "mlq" might be easier to remember: 
 
     elsif equal(macro, "mlq") then  -- called by entering ";mlq ", 5 keystrokes                                                            
	add_queue("` `" & repeat(ARROW_LEFT,2)) 
 
-- on the other hand, you can assign a symbol as an ESCAPE hot-key 
-- such as: 
 
   elsif command [1] = '^' then  -- '^' will not have to be added to the list of hot-keys 
   add_queue(ESCAPE & "d" & " eushroud " & file_name & CR)  -- because of the final CR. 
 
------------------------------------------------ 
 
-- Prefix macro's allow you to use multiple mnemoinic calling characters.  
-- ESCAPE hot-keys are limited to one character or symbol.  
-- In addition to being easier to remember, prefix macros  
-- allow you to keep your fingers closer to the home-keys.  
 
-- ESCAPE hot-keys are somewhat constrained to a fix number of single letters or symbols. 
-- However, you can add additional menu's which would allow you to re-use letter or symbols, 
-- but that would involve more key-strokes. 
 
-- Prefix macros are very good to use to save key strokes 
-- when entering longer commands which are used frequently: 
 
   elsif equal(macro,"ri") then                                                               
        add_queue("routine_id(\"\")" & repeat(ARROW_LEFT,2))  
    
   elsif equal(macro,"pf") then                                                               
        add_queue("printf(STDOUT,\"\", )" & repeat(ARROW_LEFT,4)) 
---------------------------------------------------------------- 
 
-- this little prefix macro might be useful if you have a  
-- long string of code which extends beyond the width of your screen.  
-- It basically executes a CR at the beginning of the last word within  
-- the width of the screen: 
 
    elsif equal(macro,"rm") then                                                               
        -- cuts right margin to format long lines                                        
        add_queue(HOME & repeat(ARROW_RIGHT, screen_width) & CONTROL_L & CR) 
 
new topic     » goto parent     » topic index » view message » categorize

4. db_open on steroids !

-- the following prefix macro saves (;dbo) many key strokes and 
-- also decreases the chances of errors. It even tests the routine! 
 
-- The same routines that ed.ex employs to select files and maintain 
-- command histories are used here to automate the db_open command/ 
-- The database name can be selected from a pick list *.edb's that 
-- already exist in the current directory, or a new *.edb name can be 
-- entered.  
 
--Paste these assignments at the beginning of the prefix_macro routine:    
-- for db_open prefix macro: -------------- 
    sequence lock_method ={} 
    object  current_dir_db = dir("*.edb") 
    sequence tmp = {} 
------------------------------------------- 
-- then paste the following code within the routines elsif clauses: 
 
 elsif equal(macro,"dbo") then 
	 
	set_top_line("Select/Open/Create Database: ") 
	text_color(TOP_LINE_TEXT_COLOR) 
	 
	if not(equal(current_dir_db, -1)) then 
	    if length(current_dir_db)>=1 then 
		for i = 1 to length(current_dir_db) do 
		tmp = update_history(tmp, sprintf("%s", {current_dir_db[i][D_NAME]})) 
		current_dir_db = trim(key_gets("",tmp)) 
		end for 
	    end if 
	else 
	    current_dir_db = trim(key_gets("", {})) 
	end if 
	 
	set_top_line("Database lock method =  ") 
	text_color(TOP_LINE_TEXT_COLOR) 
	lock_method = trim(key_gets(" ",{"DB_LOCK_SHARED", "DB_LOCK_EXCLUSIVE", "DB_LOCK_NO"}) & ' ') 
	 
  
        -- test 
	if db_select(sprintf("%s", {current_dir_db})) != DB_OK then 
	    if db_open(sprintf("%s", {current_dir_db})) != DB_OK then 
	      if db_create(sprintf("%s", {current_dir_db})) != DB_OK then 
		set_top_line(" ") 
		any_key("Could not Select/open/create db!") 
	      end if     
	    end if   
	end if 
	-- end test 
	 
	 
	add_queue("db_open(\"" & sprintf("%s", {current_dir_db}) & "\","  & sprintf("%s", {lock_method}) & ")") 
	add_queue(HOME & repeat(DELETE, 4) & END) 
	normal_video() 
	goto_line(0, b_col) 
 
-- The macro should be executed from the start of a new line 
     
new topic     » goto parent     » topic index » view message » categorize

5. Re: db_open on steroids !

-- I think this is a little improvement over the code listed in the previous post 
 
-- The code block written is the same as the test code.  Essentially, the macro combines 
-- db_select, db_open, & db_create. 
 
-- I added a variable for facilitate testing the LOCK_METHOD, 
-- so the assignment statements at the top of the prefix_macro routine 
-- now looks like this: 
sequence lock_method ={}  
    object  current_dir_db = dir("*.edb")  
    sequence tmp = {}  
    integer lm = 0 
------------------------------------------------------------------------ 
------------------------------------------------------------------------ 
 elsif equal(macro,"dbo") then 
	-- select/open/create euphoria database 
	 
        set_top_line("Select/Open/Create Database: ") 
	text_color(TOP_LINE_TEXT_COLOR) 
	 
	if equal(current_dir_db, -1) then 
		current_dir_db = trim(key_gets("", {})) 
	else  
	    for i = 1 to length(current_dir_db) do 
		tmp = update_history(tmp, sprintf("%s", {current_dir_db[i][D_NAME]})) 
	    end for 
	    current_dir_db = trim(key_gets("",tmp)) 
	end if 
	 
	set_top_line("Database lock method =  ") 
	text_color(TOP_LINE_TEXT_COLOR) 
	lock_method = trim(key_gets(" ",{"DB_LOCK_SHARED", "DB_LOCK_EXCLUSIVE", "DB_LOCK_NO"})) 
	if equal(lock_method, "DB_LOCK_SHARED") then 
	    lm = DB_LOCK_SHARED 
	elsif equal(lock_method, "DB_LOCK_EXCLUSIVE") then 
	    lm = DB_LOCK_EXCLUSIVE 
	else 
	    lm = DB_LOCK_NO 
	end if 
	 
	-- test 
	if db_select(sprintf("%s", {current_dir_db}), lm) != DB_OK then 
	    if db_open(sprintf("%s", {current_dir_db}), lm) != DB_OK then 
	      if db_create(sprintf("%s", {current_dir_db}), lm) != DB_OK then 
		set_top_line(" ") 
		text_color(TOP_LINE_TEXT_COLOR) 
		any_key("Could not Select/open/create db!")  
	      end if     
	    end if   
	end if 
	-- end test 
	 
	-- write code block 
        add_queue(repeat(BS,length(macro)+1))  -- erase calling macrol label 
	add_queue("\nif db_select(\"" & sprintf("%s", {current_dir_db}) & "\","  
		& sprintf("%s", {lock_method}) & ") != DB_OK then " 
		    & "\n\tif db_open(\"" & sprintf("%s", {current_dir_db}) & "\","  
		    & sprintf("%s", {lock_method}) & ") != DB_OK then" 
			& "\n\tif db_create(\"" & sprintf("%s", {current_dir_db}) & "\","  
			& sprintf("%s", {lock_method}) & ") != DB_OK " 
		    & ARROW_DOWN & "\tend if" & ARROW_DOWN 
		    & "\tend if" & CR & BS & END & CR) 
	 
	normal_video()                -- refresh 
	goto_line(0, b_col)           -- screen   
	 
 
-- comments & suggestions for improvements will be appreciated! 
 
 
new topic     » goto parent     » topic index » view message » categorize

6. several prefix_macro commands

 
     elsif equal(macro,"pf") then 
	    -- public function 
	    add_queue("public function ()" & CR & HOME &  
	    "\nreturn" & CR & HOME &  
	    "end function" &  
	    repeat(ARROW_UP, 3) & END &  
	    repeat(ARROW_LEFT, 2))       
	     
      elsif equal(macro,"pp") then 
	    -- public procedure 
	    add_queue("public procedure ()" & CR & HOME & 
	    "\nend procedure" & HOME & 
	    repeat(ARROW_UP, 2) & END &  
	    repeat(ARROW_LEFT, 2))       
	     
      elsif equal(macro, "cl") then 
	    -- change log: yy/dd/mm hh:mm 
	    time_stamp = date() 
	    time_stamp[1] += 1900 
	    add_queue(sprintf("-- change log: %4d/%02d 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu