def paginate(cur_page, total_pages):
    """ Compute the range of pages that should be displayed to the user. """
    if total_pages <= 9:
        return 1, total_pages
    if cur_page < 5:
        return 1, 9
    if cur_page > total_pages - 4:
        return total_pages - 8, total_pages
    return cur_page - 4, cur_page + 4
