DeleteLines deletes 1 too many.

Posted by Smorly on Wed 13 Feb 2013 08:09 PM — 9 posts, 30,420 views.

#0
I'm trying to deduplicate blank lines. When I match ^\s*$ with the following code (within a script, not send_to_script),


line_number = self.world.GetLinesInBufferCount
line_1 = self.world.GetLineInfo(line_number, 1).strip()
line_2 = self.world.GetLineInfo(line_number - 1, 1).strip()
line_3 = self.world.GetLineInfo(line_number - 2, 1).strip()
    
if line_1 == line_2 and line_1 in ['', '[OOC]', '[AFK]']:
  self.debug('Deduplicated line %d!\nLine_3: %s\nLine_2: %s\nLine_1: %s' %
        (line_number, line_3, line_2, line_1))
  self.world.DeleteLines(1)


I get this debug output.


DEBUG 0: Deduplicated line 104!
Line_3: You are hungry.
Line_2: 
Line_1:


This is all as expected. The output, however, looks like this:


You are hungry.
3:04 [INFO] Someone has left Antrippa.
Amended on Wed 13 Feb 2013 08:11 PM by Smorly
Australia Forum Administrator #1
Where does the debug output go?

Is this a trigger? Try doing it in "send to script".
#2
The debug output goes to a different world.

I have a catchall trigger that enters all lines from the MUD onto a message bus. A handler on that bus detects the blank lines via regex and calls the aforementioned code.

Doing it in send to script does not delete any lines, as can be expected from the documentation:

Quote:

Warning - do NOT call DeleteLines from a trigger, alias or timer using send-to-script. For design reasons this will not work.
Australia Forum Administrator #3
You can do DeleteLines in send-to-script-after-omit. Example:


<triggers>
  <trigger
   enabled="y"
   match="Exits:*"
   send_to="14"
   sequence="100"
  >
  <send>
DeleteLines (1)
</send>
  </trigger>
</triggers>

#4
Okay, I was running an old version. An upgrade fixed the problem. Sorry about the bug report. ><
#5
Hmmmm.... I guess I didn't test thoroughly enough. Must have been tired last night. The problem still occurs exactly as described above using the same code.

To eliminate any other factor, I disabled my script and entered the following code into a trigger for ^\s*$ using send to script after omit:


line_number = world.GetLinesInBufferCount
line_1 = world.GetLineInfo(line_number, 1).strip()
line_2 = world.GetLineInfo(line_number - 1, 1).strip()
    
if line_1 == line_2 and line_1 in ['', '[OOC]', '[AFK]']:
  world.DeleteLines(1)



I will note that this problem only occurs when there are two blank lines in a row. DeleteLine(1) correctly removes 1 line of text in other cases. I tested with OOC and AFK modes, and those deduplicate properly.
Amended on Thu 14 Feb 2013 04:46 PM by Smorly
Australia Forum Administrator #6
Looking at the source, I think the lines in bold are probably responsible:


void CMUSHclientDoc::DeleteLines(long Count) 
{

POSITION pos;

/*

  I can't delete lines when in send-to-script, please don't try to make me. ;)

  The problem is that in ProcessPreviousLine we have established the start and end line
  of the paragraph we are processing, if that is deleted by a trigger in the middle, all 
  hell breaks loose.

  */

  if (m_bInSendToScript)
    return;   // can't do it

  if (Count <= 0) 
     return;        // nothing to do

  // if we have the empty line at the end of the buffer, delete that too
  if (m_pCurrentLine && m_pCurrentLine->len == 0)
    Count++;



Now the reason for that code is that, once a line finishes, the client starts a new empty line (so there is always a "current" line).

Without that code, attempting to delete the last (non-blank) line only deleted the new, empty, line.

I suspect that in this case the line is not the "new empty" line, but the "old blank" one.
Australia Forum Administrator #7
Here's one workaround. :)

In the case of the de-duplication, put that empty line back.

Eg. in Lua:


<triggers>
  <trigger
   enabled="y"
   match="^\s*$"
   regexp="y"
   send_to="14"
   sequence="100"
  >
  <send>

line_number = GetLinesInBufferCount ()
line_1 = Trim (world.GetLineInfo(line_number, 1))
line_2 = Trim (GetLineInfo(line_number - 1, 1))
    
if line_1 == line_2 and line_1 == "" then
  DeleteLines(1)
end -- if

Note ""  -- put one blank line back

</send>
  </trigger>
</triggers>

#8
Right, I'm using world.Note('') as a workaround right now :)

Okay, glad to know that it is indeed a bug.