Page 1 of 1

Fill marked down, right (Insert/Over), 4 Lua scripts

Posted: Tue Jul 07, 2009 10:44 am
by petar12345
Find below four very nice Lua scripts written by Petar Turkovic 8)

I edited Petar's original posting by splitting the scripts into four individual post and adding a few examples to make them easier to understand ;)

I also made a few minor changes to the scripts (i.e. search for the JAJ comments) to make the scritps run correctly without the need for the Brief column paste mode editor option.

Cheers Jussi

Posted: Wed Jul 08, 2009 1:33 am
by jussij
Lua script written by: Petar Turkovic

The f_FillDnIns.LUA takes a marked area and takes the contents of the first line of the marked area and inserts it into all the other lines defined by the marked area.

So for example take the following text and mark the first three columns of all these lines:

Code: Select all

SetText1(String &szBuffer);
Text2(String &szBuffer);
Text3(String &szBuffer);
Text4(String &szBuffer);
Running this macro results in this new text:

Code: Select all

SetText1(String &szBuffer);
SetText2(String &szBuffer);
SetText3(String &szBuffer);
SetText4(String &szBuffer);
Here is the f_FillDnIns.LUA code:

Code: Select all

-- ======================================================================== 
--  PROJECT      : A5: Lua Macro 
--  PROGRAM      : f_FillDnIns.LUA  Fill Down Marked Area - Insert 
--  PROJECTOR    : Turkovic Petar 
--  PROGRAMER    : Turkovic Petar 
--  VERIFICATION : Jussi Jumppanen 
-- == Standard: =========================================================== 
--  07.07.09  A5.50.1.1.2 
-- == Description: ======================================================== 
--  This a simple Lua macro will fill down first row in the marked area. 
--  Insert, shift text right 
-- ======================================================================== 

------ Version_5 ---------------------------------------------------------- 
function key_macro() 
--------------------------------------------------------------------------- 
  -- macro only works for read/write documents. 
  if (is_read_only() == 1) or (is_document() == 0) then 
    message("This macro can only be used with a writable document.") 
    beep() 
    return 
  end 
  -- macro only works for marked documents 
  if is_marked() == 0 then 
    message("This macro needs a column marked area.") 
    beep() 
    return 
  end 

  -- get the marked text details 
  local l_top     = get_marked_top() 
  local l_left    = get_marked_left() 
  local l_bottom  = get_marked_bottom() 
  local l_right   = get_marked_right() 
  if l_top == l_bottom then 
    message("This macro needs at least 2 lines in marked area.") 
    beep() 
    return 
  end 
  if l_left == l_right then 
    message("This macro needs at least 1 column in marked area.") 
    beep() 
    return 
  end 
  -- disable screen updates 
  screen_update_disable() 
  cursor_save() 
  MarkHide() 

-- copy first row 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_cursor_pos(l_right) 
  MarkCopyEx()         -- copy marked area to clipboard 

-- fill marked area 
  for i = 1, l_bottom - l_top, 1 do 
    -- JAJ position the cursor and don't rely on brief paste mode 
    set_line_pos(l_top + i) 
    set_cursor_pos(l_left) 
    MarkPasteEx() 
  end 
-- mark area 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_right) 
  cursor_restore() 

  -- restore screen updates 
  screen_update_enable() 
  return 
end 

key_macro() -- run the macro 

-- ========================================================================

Posted: Wed Jul 08, 2009 1:34 am
by jussij
Lua script written by: Petar Turkovic

The f_FillDnOver.LUA takes a marked area and takes the contents of the first line of the marked area and overwites the text into all the other lines defined by the marked area.

So for example take the following text and mark the first three columns of all these lines:

Code: Select all

SetText1(String &szBuffer);
GetText2(String &szBuffer);
GetText3(String &szBuffer);
GetText4(String &szBuffer);
Running this macro results in this new text:

Code: Select all

SetText1(String &szBuffer);
SetText2(String &szBuffer);
SetText3(String &szBuffer);
SetText4(String &szBuffer);
Here is the f_FillDnOver.LUA code:

Code: Select all

-- ======================================================================== 
--  PROJECT      : A5: Lua Macro 
--  PROGRAM      : f_FillDnOver.LUA  Fill Down Marked Area - Over 
--  PROJECTOR    : Turkovic Petar 
--  PROGRAMER    : Turkovic Petar 
--  VERIFICATION : Jussi Jumppanen 
-- == Standard: =========================================================== 
--  07.07.09  A5.50.1.1.2 
-- == Description: ======================================================== 
--  This a simple Lua macro will fill down first row in the marked area. 
--  Fill over marked text 
-- ======================================================================== 

------ Version_5 ---------------------------------------------------------- 
function key_macro() 
--------------------------------------------------------------------------- 

  if (is_read_only() == 1) or (is_document() == 0) then 
    message("This macro can only be used with a writable document.") 
    beep() 
    return 
  end 
  -- macro only works for marked documents 
  if is_marked() == 0 then 
    message("This macro needs a column marked area.") 
    beep() 
    return 
  end 

  -- get the marked text details 
  local l_top     = get_marked_top() 
  local l_left    = get_marked_left() 
  local l_bottom  = get_marked_bottom() 
  local l_right   = get_marked_right() 
  if l_top == l_bottom then 
    message("This macro needs at least 2 lines in marked area.") 
    beep() 
    return 
  end 
  if l_left == l_right then 
    message("This macro needs at least 1 column in marked area.") 
    beep() 
    return 
  end 
  -- disable screen updates 
  screen_update_disable() 
  cursor_save() 
  MarkHide() 

-- copy first row 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_cursor_pos(l_right) 
  MarkCopyEx()         -- copy marked area to clipboard 
-- delete marked text 
  set_line_pos(l_top + 1) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_right) 
  MarkDelete() 

-- fill marked area 
  for i = 1, l_bottom - l_top, 1 do 
    -- JAJ position the cursor and don't rely on brief paste mode 
    set_line_pos(l_top + i) 
    set_cursor_pos(l_left) 
    MarkPasteEx() 
  end 
-- mark area 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_right) 
  cursor_restore() 

  -- restore screen updates 
  screen_update_enable() 
  return 
end 

key_macro() -- run the macro 

-- =========================================================================

Posted: Wed Jul 08, 2009 1:41 am
by jussij
Lua script written by: Petar Turkovic

The f_FillRightIns.LUA takes a marked area and takes left most character and insert it into the contents of each of the lines contained in the marked area. It uses the with of the marked area to determine how many times the first character need to be inserted.

So for example take the following text and mark the first four columns of all these lines:

Code: Select all

SetText1(String &szBuffer); 
SetText2(String &szBuffer); 
SetText3(String &szBuffer); 
SetText4(String &szBuffer);

Running this macro results in this new text:

Code: Select all

SSSSetText1(String &szBuffer); 
SSSSetText2(String &szBuffer); 
SSSSetText3(String &szBuffer); 
SSSSetText4(String &szBuffer);

Here is the f_FillRightIns.LUA code:

Code: Select all

-- ========================================================================= 
--  PROJECT      : A5: Lua Macro 
--  PROGRAM      : f_FillRightIns.LUA  Fill Right Marked Area - Insert 
--  PROJECTOR    : Turkovic Petar 
--  PROGRAMER    : Turkovic Petar 
--  VERIFICATION : Jussi Jumppanen 
-- == Standard: ============================================================ 
--  07.07.09  A5.50.1.1.2 
-- == Description: ========================================================= 
--  This a simple Lua macro will fill right first column in the marked area. 
--  Insert, shift marked text right 
-- ========================================================================= 

------ Version_5 ----------------------------------------------------------- 
function key_macro() 
---------------------------------------------------------------------------- 

  if (is_read_only() == 1) or (is_document() == 0) then 
    message("This macro can only be used with a writable document.") 
    beep() 
    return 
  end 
  -- macro only works for marked documents 
  if is_marked() == 0 then 
    message("This macro needs a column marked area.") 
    beep() 
    return 
  end 

  -- get the marked text details 
  local l_top     = get_marked_top() 
  local l_left    = get_marked_left() 
  local l_bottom  = get_marked_bottom() 
  local l_right   = get_marked_right() 
  -- JAJ changed 2 to be 1 
  if l_right <= l_left + 1  then 
    message("This macro needs at least 2 column in marked area.") 
    beep() 
    return 
  end 
  -- disable screen updates 
  screen_update_disable() 
  cursor_save() 
  MarkHide() 

-- copy first column 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_left + 1) 
  MarkCopyEx()         -- copy marked area to clipboard 

-- fill marked area 
  for i = 1, l_right - l_left - 1, 1 do 
    set_line_pos(l_top) 
    set_cursor_pos(l_left + 1) 
    MarkPasteEx() 
  end 
-- mark area 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_right) 
  cursor_restore() 

  -- restore screen updates 
  screen_update_enable() 
  return 
end 

key_macro() -- run the macro 

-- =========================================================================

Posted: Wed Jul 08, 2009 1:44 am
by jussij
Lua script written by: Petar Turkovic

The f_FillRightOver.LUA takes a marked area and takes left most character and overwrites it into the contents of each of the lines contained in the marked area. It uses the width of the marked area to determine how many times the first character need to be inserted.

So for example take the following text and mark the first four columns of all these lines:

Code: Select all

GetText1(String &szBuffer);
GetText2(String &szBuffer);
GetText3(String &szBuffer);
GetText4(String &szBuffer);

Running this macro results in this new text:

Code: Select all

GGGGext1(String &szBuffer);
GGGGext2(String &szBuffer);
GGGGext3(String &szBuffer);
GGGGext4(String &szBuffer);

Here is the f_FillRightOver.LUA code:

Code: Select all

-- ========================================================================= 
--  PROJECT      : A5: Lua Macro 
--  PROGRAM      : f_FillRightOver.LUA  Fill Right Marked Area - Over 
--  PROJECTOR    : Turkovic Petar 
--  PROGRAMER    : Turkovic Petar 
--  VERIFICATION : Jussi Jumppanen 
-- == Standard: ============================================================ 
--  07.07.09  A5.50.1.1.2 
-- == Description: ========================================================= 
--  This a simple Lua macro will fill right first column in the marked area. 
--  Fill over marked text 
-- ========================================================================= 

------ Version_5 ----------------------------------------------------------- 
function key_macro() 
---------------------------------------------------------------------------- 

  if (is_read_only() == 1) or (is_document() == 0) then 
    message("This macro can only be used with a writable document.") 
    beep() 
    return 
  end 
  -- macro only works for marked documents 
  if is_marked() == 0 then 
    message("This macro needs a column marked area.") 
    beep() 
    return 
  end 

  -- get the marked text details 
  local l_top     = get_marked_top() 
  local l_left    = get_marked_left() 
  local l_bottom  = get_marked_bottom() 
  local l_right   = get_marked_right() 
  -- JAJ changed 2 to be 1 
  if l_right <= l_left + 1  then 
    message("This macro needs at least 2 column in marked area.") 
    beep() 
    return 
  end 
  -- disable screen updates 
  screen_update_disable() 
  cursor_save() 
  MarkHide() 

-- copy first column 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_left + 1) 
  MarkCopyEx()         -- copy marked area to clipboard 
-- delete marked text 
  set_line_pos(l_top) 
  set_cursor_pos(l_left + 1) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_right) 
  MarkDelete() 

-- fill marked area 
  for i = 1, l_right - l_left - 1, 1 do 
    set_line_pos(l_top) 
    set_cursor_pos(l_left + 1) 
    MarkPasteEx() 
  end 
-- mark area 
  set_line_pos(l_top) 
  set_cursor_pos(l_left) 
  MarkColumnToggle()   -- start marking 
  set_line_pos(l_bottom) 
  set_cursor_pos(l_right) 
  cursor_restore() 

  -- restore screen updates 
  screen_update_enable() 
  return 
end 

key_macro() -- run the macro 

-- =========================================================================

Posted: Wed Jul 08, 2009 5:14 pm
by petar12345
When I start learning Lua I read examples like this.
If that example have lines like ones which I delete, with no functionality but that lines not generate bug in execute, I was confused much better.
I was not see why this lines are there, I was know so litle about lua.
In that case I read that example over and over to try find why that lines must be there.
Please insert new code over old ones.
Edited by jussi: I have updated these macros with your new versions. Nice work ;)

By!

Posted: Wed Jul 08, 2009 5:52 pm
by petar12345
This is my favorite one. New one. 8)

Lua script written by: Petar Turkovic

The f_FillDnLineIns.LUA takes a marked area and takes the entire contents of the first line of the marked area and inserts multiple copies of that line into the document. The number of copies of the line that are inserted is determined by the number of lines of the original marked area.

So for example take the following text and mark the first three columns of all these lines:

Code: Select all

SetText1(String &szBuffer);
Text2(String &szBuffer);
Text3(String &szBuffer);
Text4(String &szBuffer);
Running this macro will result in the following new text which includes 3 new lines:

Code: Select all

SetText1(String &szBuffer);
SetText1(String &szBuffer);
SetText1(String &szBuffer);
SetText1(String &szBuffer);
Text2(String &szBuffer);
Text3(String &szBuffer);
Text4(String &szBuffer);
Here is the f_FillDnLineIns.LUA code:

Code: Select all

-- =========================================================================
--  PROJECT      : A5: Lua Macro
--  PROGRAM      : f_FillDnLineIns.LUA  Fill Down Line - Insert
--  PROJECTOR    : Turkovic Petar
--  PROGRAMER    : Turkovic Petar
--  VERIFICATION :
-- == Standard: ============================================================
--  08.07.09 19:47   A5.50.1.1.2
-- == Description: =========================================================
--  This a simple Lua macro will fill down first line in the marked area.
--  Lines will be inserted
-- =========================================================================

------ Version_5 -----------------------------------------------------------
function key_macro()
----------------------------------------------------------------------------

  if (is_read_only() == 1) or (is_document() == 0) then
    message("This macro can only be used with a writable document.")
    beep()
    return
  end
  -- macro only works for marked documents
  if is_marked() == 0 then
    message("This macro needs a column marked area.")
    beep()
    return
  end

  -- get the marked text details
  local l_top     = get_marked_top()
  local l_bottom  = get_marked_bottom()
  local l_line
  -- disable screen updates
  screen_update_disable()
  MarkHide()

  set_line_pos(l_top)   -- cursor in first line
  l_line = get_line_text() -- get first line text
  for i = 1, l_bottom - l_top, 1 do
    MoveDocumentEndEx()  -- Cursor to end of line
    print("\n")            -- New line
    MoveDocumentStartEx()  -- Cursor to start of line
    print(l_line)
  end

  set_line_pos(l_bottom)
  set_cursor_pos(1)
  MarkLineToggle()   -- start marking
  set_line_pos(l_top)

  -- restore screen updates
  screen_update_enable()
  return
end

key_macro() -- run the macro

-- =========================================================================