def GuessNewMessages(old, new):
  """Guess which messages in a message list are new.

  >>> GuessNewMessages([1, 2, 3, 4], [1, 2, 3, 4, 5, 6, 7])
  [5, 6, 7]
  >>> GuessNewMessages([1, 2, 3, 4], [4, 5, 6, 7])
  [5, 6, 7]
  >>> GuessNewMessages([1, 2, 3, 4], [5, 6, 7])
  [5, 6, 7]
  >>> GuessNewMessages([1, 2, 3, 4], [4, 1, 2, 3])
  [1, 2, 3]

  Args:
    old: The old message list.
    new: The new message list.
  Returns:
    The new messages. Probably.
  """
  # This is made complicated by the fact that vim can drop messages, sometimes
  # after as few as 20 messages. When that's the case we have to guess a bit.
  # Technically, it's always possible to miss exactly [MESSAGE_MAX] messages
  # if you echo them out in a perfect cycle in one command. So it goes.
  # Message lists are straight from vim, so oldest is first.
  for i in range(len(old)):
    if old[i:] == new[:len(old) - i]:
      return new[len(old) - i:]
  return new[:]