<commit_before># coding=utf-8

import vim
import time
import datetime
import re
from os import remove, listdir
from os.path import expanduser, exists, basename, join, getmtime
from shutil import move
from subprocess import Popen, PIPE

class Pad(object):
	"""This handles all the operations of the plugin. 
	It works as a namespace of sorts."""
	
	def __timestamp(self):
		"""__timestamp() -> str:timestamp
		
		Returns a string of digits representing the current time.
		"""
		return str(int(time.time() * 1000000))

	def __natural_timestamp(self, timestamp):
		"""pad_natural_timestamp(str:timestamp) -> str:natural_timestamp
		
		Returns a string representing a datetime object.

			timestamp: a string in the format returned by pad_timestamp.

		The output uses a natural format for timestamps within the previous
		24 hours, and the format %Y-%m-%d %H:%M:%S otherwise.
		"""
		timestamp = basename(timestamp)
		f_timestamp = float(int(timestamp)) / 1000000
		tmp_datetime = datetime.datetime.fromtimestamp(f_timestamp)
		diff = datetime.datetime.now() - tmp_datetime
		days = diff.days
		seconds = diff.seconds
		minutes = seconds/60
		hours = minutes/60
		
		if days > 0:
			return tmp_datetime.strftime("%Y-%m-%d %H:%M:%S")
		if hours < 1:
			if minutes < 1:
				return str(seconds) + "s ago"
			else:
				seconds_diff = seconds - (minutes * 60)
				if seconds_diff != 0:
					return str(minutes) + "m and " + str(seconds_diff) + "s ago"
				else:
					return str(minutes) + "m ago"
		else:
			minutes_diff = minutes - (hours * 60)
			if minutes_diff != 0:
				return str(hours) + "h and " + str(minutes_diff) + "m ago"
			else:
				return str(hours) + "h ago"

	def __init__(self):
		"""
		Updates the internal variables and initializes the caches.

		Adjusts some external options.
		"""
		self.save_dir = expanduser(vim.eval("g:pad_dir"))
		self.save_dir_set = self.save_dir != ""
		self.filetype = vim.eval("g:pad_format")
		self.window_height = str(vim.eval("g:pad_window_height"))
		self.search_backend = vim.eval("g:pad_search_backend")
		self.ignore_case = bool(int(vim.eval("g:pad_search_ignorecase")))
		self.read_chars = int(vim.eval("g:pad_read_nchars_from_files"))

		# create the caches
		self.cached_data = []
		self.cached_timestamps = []
		self.cached_filenames = []

		# Under windows, we need the path to be in a certain format 
		if vim.eval('has("win32")') == "1":
			save_dir = self.save_dir.replace("\\", "\\\\")
		else:
			save_dir = self.save_dir

		# vim-pad pollutes the MRU.vim list quite a lot, if let alone.
		# This should fix that.
		if vim.eval('exists(":MRU")') == "2":
			mru_exclude_files = vim.eval("MRU_Exclude_Files")
			if mru_exclude_files != '':
				tail = "\|" + mru_exclude_files
			else:
				tail = ''
			vim.command("let MRU_Exclude_Files = '^" + 
					join(save_dir, ".*") + tail + "'")

		# we forbid writing backups of the notes
		orig_backupskip = vim.eval("&backupskip")
		vim.command("set backupskip=" + 
				",".join([orig_backupskip, join(save_dir, "*")]))


		# we set listchars, for formatting purposes
		tmp=[i.split(":")[0] for i in vim.eval("&listchars").split(",")]
		# we won't touch listchars if the values we want to change are already set
		if "extends" not in tmp:
			vim.command("set listchars+=extends:»")
		if "precedes" not in tmp:
			vim.command("set listchars+=precedes:«")
		del tmp
	

	# Pads

	def pad_open(self, path=None, first_line=None):
		"""Creates or opens a note.

		path: a valid path for a note.

		first_line: a string to insert to a new note, if given.
		"""
		# we require self.save_dir_set to be set to a valid path
		if not self.save_dir_set:
			vim.command('let tmp = confirm("IMPORTANT:\n'\
					'Please set g:pad_dir to a valid path in your vimrc.", "OK", 1, "Error")')
			return
		
		# if no path is provided, we create one using the current time
		if not path:
			path = join(self.save_dir, self.__timestamp())

		vim.command("silent! botright" + self.window_height + "split " + path)
		
		# set the filetype to our default
		if vim.eval('&filetype') in ('', 'conf'):<commit_msg>Fixing listchars for non multibyte-encodings.<commit_after># coding=utf-8

import vim
import time
import datetime
import re
from os import remove, listdir
from os.path import expanduser, exists, basename, join, getmtime
from shutil import move
from subprocess import Popen, PIPE

class Pad(object):
	"""This handles all the operations of the plugin. 
	It works as a namespace of sorts."""
	
	def __timestamp(self):
		"""__timestamp() -> str:timestamp
		
		Returns a string of digits representing the current time.
		"""
		return str(int(time.time() * 1000000))

	def __natural_timestamp(self, timestamp):
		"""pad_natural_timestamp(str:timestamp) -> str:natural_timestamp
		
		Returns a string representing a datetime object.

			timestamp: a string in the format returned by pad_timestamp.

		The output uses a natural format for timestamps within the previous
		24 hours, and the format %Y-%m-%d %H:%M:%S otherwise.
		"""
		timestamp = basename(timestamp)
		f_timestamp = float(int(timestamp)) / 1000000
		tmp_datetime = datetime.datetime.fromtimestamp(f_timestamp)
		diff = datetime.datetime.now() - tmp_datetime
		days = diff.days
		seconds = diff.seconds
		minutes = seconds/60
		hours = minutes/60
		
		if days > 0:
			return tmp_datetime.strftime("%Y-%m-%d %H:%M:%S")
		if hours < 1:
			if minutes < 1:
				return str(seconds) + "s ago"
			else:
				seconds_diff = seconds - (minutes * 60)
				if seconds_diff != 0:
					return str(minutes) + "m and " + str(seconds_diff) + "s ago"
				else:
					return str(minutes) + "m ago"
		else:
			minutes_diff = minutes - (hours * 60)
			if minutes_diff != 0:
				return str(hours) + "h and " + str(minutes_diff) + "m ago"
			else:
				return str(hours) + "h ago"

	def __init__(self):
		"""
		Updates the internal variables and initializes the caches.

		Adjusts some external options.
		"""
		self.save_dir = expanduser(vim.eval("g:pad_dir"))
		self.save_dir_set = self.save_dir != ""
		self.filetype = vim.eval("g:pad_format")
		self.window_height = str(vim.eval("g:pad_window_height"))
		self.search_backend = vim.eval("g:pad_search_backend")
		self.ignore_case = bool(int(vim.eval("g:pad_search_ignorecase")))
		self.read_chars = int(vim.eval("g:pad_read_nchars_from_files"))

		# create the caches
		self.cached_data = []
		self.cached_timestamps = []
		self.cached_filenames = []

		# Under windows, we need the path to be in a certain format 
		if vim.eval('has("win32")') == "1":
			save_dir = self.save_dir.replace("\\", "\\\\")
		else:
			save_dir = self.save_dir

		# vim-pad pollutes the MRU.vim list quite a lot, if let alone.
		# This should fix that.
		if vim.eval('exists(":MRU")') == "2":
			mru_exclude_files = vim.eval("MRU_Exclude_Files")
			if mru_exclude_files != '':
				tail = "\|" + mru_exclude_files
			else:
				tail = ''
			vim.command("let MRU_Exclude_Files = '^" + 
					join(save_dir, ".*") + tail + "'")

		# we forbid writing backups of the notes
		orig_backupskip = vim.eval("&backupskip")
		vim.command("set backupskip=" + 
				",".join([orig_backupskip, join(save_dir, "*")]))


		# we set listchars, for formatting purposes
		tmp=[i.split(":")[0] for i in vim.eval("&listchars").split(",")]
		# we won't touch listchars if the values we want to change are already set
		if vim.eval('has("multi_byte_encoding")') == "1":
			if "extends" not in tmp:
				vim.command("set listchars+=extends:»")
			if "precedes" not in tmp:
				vim.command("set listchars+=precedes:«")
		else:
			if "extends" not in tmp:
				vim.command("set listchars+=extends:<bb>")
			if "precedes" not in tmp:
				vim.command("set listchars+=precedes:<ab>")
		del tmp
	

	# Pads

	def pad_open(self, path=None, first_line=None):
		"""Creates or opens a note.

		path: a valid path for a note.

		first_line: a string to insert to a new note, if given.
		"""
		# we require self.save_dir_set to be set to a valid path
		if not self.save_dir_set:
			vim.command('let tmp = confirm("IMPORTANT:\n'\
					'Please set g:pad_dir to a valid path in your vimrc.", "OK", 1, "Error")')
			return
		
		# if no path is provided, we create one using the current time
		if not path:
			path = join(self.save_dir, self.__timestamp())

		vim.command("silent! botright" + self.window_height + "split " + path)
		
		# set the filetype to our default
		if vim.eval('&filetype') in ('', 'conf'):
{'fraction_non_alphanumeric': 0.133325404376784, 'fraction_numerical': 0.00868220742150333, 'mean_word_length': 3.689905186837702, 'pattern_counts': {'":': 9, '<': 9, '<?xml version=': 0, '>': 11, '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': '1720996', 'n_tokens_mistral': 3297, 'n_tokens_neox': 2818, 'n_words': 841}