<commit_before>////////////////////////////////////////////////////////////////////////////////
// Name:      address.cpp
// Purpose:   Implementation of class wxExAddress and wxExAddressRange
// Author:    <NAME>
// Copyright: (c) 2014 <NAME>
////////////////////////////////////////////////////////////////////////////////

#include <sstream>
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/textfile.h>
#include <wx/tokenzr.h>
#include <wx/extension/address.h>
#include <wx/extension/ex.h>
#include <wx/extension/managedframe.h>
#include <wx/extension/process.h>
#include <wx/extension/stc.h>
#include <wx/extension/util.h>
#include <wx/extension/vimacros.h>

#if wxUSE_GUI

wxExAddress::wxExAddress(wxExEx* ex, const wxString& address)
  : wxString(address)
  , m_Ex(ex)
  , m_Line(-1)
{
}

int wxExAddress::ToLine() const
{
  if (m_Line >= 0)
  {
    return m_Line;
  }
   
  if (Contains("/"))
  {
    return 0;
  }

  int width = 0;
  const int sum = wxExCalculator(*this, m_Ex, width);
  
  // Limit the range of what is returned.
  if (sum < 0)
  {
    return 1;
  }
  else if (sum > m_Ex->GetSTC()->GetLineCount())
  {
    return m_Ex->GetSTC()->GetLineCount();
  }  
  else
  {
    return sum;
  }
}

wxExAddressRange::wxExAddressRange(wxExEx* ex, const wxString& range)
  : m_Begin(ex, "")
  , m_End(ex, "")
  , m_Ex(ex)
  , m_STC(ex->GetSTC())
{
  if (range == ".")
  {
    Set(".", ".");
  }
  else if (range == "%")
  {
    Set("1", "$");
  }
  else if (range == "*")
  {
    std::stringstream ss;
    ss << m_STC->GetFirstVisibleLine() + 1;
    std::stringstream tt;
    tt << m_STC->GetFirstVisibleLine() + m_STC->LinesOnScreen() + 1;
    Set(wxString(ss.str()), wxString(tt.str()));
  }
  else if (range.Contains(","))
  {
    Set(range.BeforeFirst(','), range.AfterFirst(','));
  }
  else
  {
    Set(range, range);
  }
}
  
wxExAddressRange::wxExAddressRange(wxExEx* ex, int lines)
  : m_Begin(ex, "")
  , m_End(ex, "")
  , m_Ex(ex)
  , m_STC(ex->GetSTC())
{
  if (lines > 0)
  {
    m_Begin.m_Line = m_STC->LineFromPosition(m_STC->GetCurrentPos()) + 1;
    m_End.m_Line = m_Begin.m_Line + lines - 1; 
    
    if (m_End.m_Line > m_STC->GetLineCount())
    {
      m_End.m_Line = m_STC->GetLineCount();
    }
  }
  else if (lines == 0)
  {
    // this is illegal
    m_Begin.m_Line = 0;
    m_End.m_Line = 0;
  }
  else
  {
    m_End.m_Line = m_STC->LineFromPosition(m_STC->GetCurrentPos()) + 1;
    m_Begin.m_Line = m_End.m_Line + lines - 1;
    
    if (m_Begin.m_Line < 1)
    {
      m_Begin.m_Line = 1;
    }
  }
}

bool wxExAddressRange::Delete(bool show_message) const
{
  if (!IsOk() || m_STC->GetReadOnly() || m_STC->HexMode())
  {
    return false;
  }

  if (m_STC->GetSelectedText().empty())
  {
    if (!SetSelection())
    {
      return false;
    }
  }

  const int lines = wxExGetNumberOfLines(m_STC->GetSelectedText());
  
  if (m_STC->GetSelectedText().empty())
  {
    m_STC->DeleteBack();
  }
  else
  {
    if (m_Ex->GetRegister())
    {
      m_Ex->GetMacros().SetRegister(
        m_Ex->GetRegister(), m_STC->GetSelectedText());
      m_STC->ReplaceSelection(wxEmptyString);
    }
    else
    {
      m_STC->Cut();
    }
  }

  if (m_Begin.StartsWith("'"))
  {
    if (m_Begin.size() > 1)
    {
      m_Ex->MarkerDelete(m_Begin.GetChar(1));
    }
  }

  if (m_End.StartsWith("'"))
  {
    if (m_End.size() > 1)
    {
      m_Ex->MarkerDelete(m_End.GetChar(1));
    }
  }

  if (lines >= 3 && show_message)
  {
    m_Ex->GetFrame()->ShowExMessage(wxString::Format(_("%d fewer lines"), lines - 1));
  }

  return true;
}

bool wxExAddressRange::Filter(const wxString& command) const
{
  const int begin_line = m_Begin.ToLine();
  const int end_line = m_End.ToLine();

  if (!IsOk() || end_line < begin_line)
  {
    return false;
  }

  char buffer[255];
  tmpnam(buffer);
  
  wxTextFile file(buffer);
  
  if (!file.Create())
  {
    return false;
  }

  for (int i = begin_line - 1; i <= end_line - 1; i++)
  {
    file.AddLine(m_STC->GetLine(i).Trim());
  }
  <commit_msg>Changed the use of tmpnam to mkstemp<commit_after>////////////////////////////////////////////////////////////////////////////////
// Name:      address.cpp
// Purpose:   Implementation of class wxExAddress and wxExAddressRange
// Author:    <NAME>
// Copyright: (c) 2014 <NAME>
////////////////////////////////////////////////////////////////////////////////

#include <sstream>
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/textfile.h>
#include <wx/tokenzr.h>
#include <wx/extension/address.h>
#include <wx/extension/ex.h>
#include <wx/extension/managedframe.h>
#include <wx/extension/process.h>
#include <wx/extension/stc.h>
#include <wx/extension/util.h>
#include <wx/extension/vimacros.h>

#if wxUSE_GUI

wxExAddress::wxExAddress(wxExEx* ex, const wxString& address)
  : wxString(address)
  , m_Ex(ex)
  , m_Line(-1)
{
}

int wxExAddress::ToLine() const
{
  if (m_Line >= 0)
  {
    return m_Line;
  }
   
  if (Contains("/"))
  {
    return 0;
  }

  int width = 0;
  const int sum = wxExCalculator(*this, m_Ex, width);
  
  // Limit the range of what is returned.
  if (sum < 0)
  {
    return 1;
  }
  else if (sum > m_Ex->GetSTC()->GetLineCount())
  {
    return m_Ex->GetSTC()->GetLineCount();
  }  
  else
  {
    return sum;
  }
}

wxExAddressRange::wxExAddressRange(wxExEx* ex, const wxString& range)
  : m_Begin(ex, "")
  , m_End(ex, "")
  , m_Ex(ex)
  , m_STC(ex->GetSTC())
{
  if (range == ".")
  {
    Set(".", ".");
  }
  else if (range == "%")
  {
    Set("1", "$");
  }
  else if (range == "*")
  {
    std::stringstream ss;
    ss << m_STC->GetFirstVisibleLine() + 1;
    std::stringstream tt;
    tt << m_STC->GetFirstVisibleLine() + m_STC->LinesOnScreen() + 1;
    Set(wxString(ss.str()), wxString(tt.str()));
  }
  else if (range.Contains(","))
  {
    Set(range.BeforeFirst(','), range.AfterFirst(','));
  }
  else
  {
    Set(range, range);
  }
}
  
wxExAddressRange::wxExAddressRange(wxExEx* ex, int lines)
  : m_Begin(ex, "")
  , m_End(ex, "")
  , m_Ex(ex)
  , m_STC(ex->GetSTC())
{
  if (lines > 0)
  {
    m_Begin.m_Line = m_STC->LineFromPosition(m_STC->GetCurrentPos()) + 1;
    m_End.m_Line = m_Begin.m_Line + lines - 1; 
    
    if (m_End.m_Line > m_STC->GetLineCount())
    {
      m_End.m_Line = m_STC->GetLineCount();
    }
  }
  else if (lines == 0)
  {
    // this is illegal
    m_Begin.m_Line = 0;
    m_End.m_Line = 0;
  }
  else
  {
    m_End.m_Line = m_STC->LineFromPosition(m_STC->GetCurrentPos()) + 1;
    m_Begin.m_Line = m_End.m_Line + lines - 1;
    
    if (m_Begin.m_Line < 1)
    {
      m_Begin.m_Line = 1;
    }
  }
}

bool wxExAddressRange::Delete(bool show_message) const
{
  if (!IsOk() || m_STC->GetReadOnly() || m_STC->HexMode())
  {
    return false;
  }

  if (m_STC->GetSelectedText().empty())
  {
    if (!SetSelection())
    {
      return false;
    }
  }

  const int lines = wxExGetNumberOfLines(m_STC->GetSelectedText());
  
  if (m_STC->GetSelectedText().empty())
  {
    m_STC->DeleteBack();
  }
  else
  {
    if (m_Ex->GetRegister())
    {
      m_Ex->GetMacros().SetRegister(
        m_Ex->GetRegister(), m_STC->GetSelectedText());
      m_STC->ReplaceSelection(wxEmptyString);
    }
    else
    {
      m_STC->Cut();
    }
  }

  if (m_Begin.StartsWith("'"))
  {
    if (m_Begin.size() > 1)
    {
      m_Ex->MarkerDelete(m_Begin.GetChar(1));
    }
  }

  if (m_End.StartsWith("'"))
  {
    if (m_End.size() > 1)
    {
      m_Ex->MarkerDelete(m_End.GetChar(1));
    }
  }

  if (lines >= 3 && show_message)
  {
    m_Ex->GetFrame()->ShowExMessage(wxString::Format(_("%d fewer lines"), lines - 1));
  }

  return true;
}

bool wxExAddressRange::Filter(const wxString& command) const
{
  const int begin_line = m_Begin.ToLine();
  const int end_line = m_End.ToLine();

  if (!IsOk() || end_line < begin_line)
  {
    return false;
  }

  char buffer[255];
  mkstemp(buffer);
  
  wxTextFile file(buffer);
  
  if (!file.Create())
  {
    return false;
  }

  for (int i = begin_line - 1; i <= end_line - 1; i++)
  {
    file.AddLine(m_STC->GetLine(i).Trim());
  }
  
{'fraction_non_alphanumeric': 0.21068176224464683, 'fraction_numerical': 0.008368200836820083, 'mean_word_length': 2.419015565839293, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4978749', 'n_tokens_mistral': 3287, 'n_tokens_neox': 3202, 'n_words': 556}