<commit_before>-- © 2008 <NAME>.
-- WordGrinder is licensed under the BSD open source license. See the COPYING
-- file in this distribution for the full text.
--
-- $Id$
-- $URL$

local table_remove = table.remove
local table_insert = table.insert
local table_concat = table.concat
local Write = wg.write
local WriteStyled = wg.writestyled
local ClearToEOL = wg.cleartoeol
local SetNormal = wg.setnormal
local SetBold = wg.setbold
local SetUnderline = wg.setunderline
local SetReverse = wg.setreverse
local SetDim = wg.setdim
local GetStringWidth = wg.getstringwidth
local GetBytesOfCharacter = wg.getbytesofcharacter
local GetWordText = wg.getwordtext

DocumentSetClass =
{
	-- remove any cached data prior to saving
	purge = function(self)
		for _, l in ipairs(self.documents) do
			l:purge()
		end
	end,
	
	touch = function(self)
		self.changed = true
		self.justchanged = true
	end,
	
	clean = function(self)
		self.changed = nil
		self.justchanged = nil
	end,
	
	getDocumentList = function(self)
		return self.documents
	end,
	
	_findDocument = function(self, name)
		for i, d in ipairs(self.documents) do
			if (d.name == name) then
				return i
			end
		end
		return nil
	end,
	
	findDocument = function(self, name)
		local document = self.documents[name]
		if not document then
			document = self.documents[self:_findDocument(name)]
			if document then
				ModalMessage("Document index inconsistency corrected",
					"Something freaky happened to '"..name.."'.")
				self.documents[name] = document
			end
		end
		return document
	end,
	
	addDocument = function(self, document, name, index)
		document.name = name
		
		local n = self:_findDocument(name) or (#self.documents + 1)
		self.documents[n] = document
		self.documents[name] = document
		if not self.current or (self.current.name == name) then
			self:setCurrent(name)
		end
		
		self:touch()
		RebuildDocumentsMenu(self.documents)
	end,
	
	moveDocumentIndexTo = function(self, name, targetIndex)
		local n = self:_findDocument(name)
		if not n then
			return
		end
		local document = self.documents[n]
		
		table_remove(self.documents, n)
		table_insert(self.documents, targetIndex, document)
		self:touch()
		RebuildDocumentsMenu(self.documents)
	end,
	
	deleteDocument = function(self, name)
		if (#self.documents == 1) then
			return false
		end
		
		local n = self:_findDocument(name)
		if not n then
			return
		end
		local document = self.documents[n]
		
		table_remove(self.documents, n)
		self.documents[name] = nil
		
		self:touch()
		RebuildDocumentsMenu(self.documents)
		
		if (Document == document) then
			document = self.documents[n]
			if not document then
				document = self.documents[#self.documents]
			end
			
			self:setCurrent(document.name)
		end
		
		return true
	end,
	
	setCurrent = function(self, name)
		Document = self.documents[name]
		if not Document then
			Document = self.documents[1]
		end

		self.current = Document
		ResizeScreen()
	end,
	
	renameDocument = function(self, oldname, newname)
		if self.documents[newname] then
			return false
		end
		
		local d = self.documents[oldname]
		self.documents[oldname] = nil
		self.documents[newname] = d
		d.name = newname
		
		self:touch()
		RebuildDocumentsMenu(self.documents)
		return true
	end,
	
	setClipboard = function(self, clipboard)<commit_msg>We now fire Event.Changed when changing documents, because otherwise they'd be left with stale housekeeping data.<commit_after>-- © 2008 <NAME>.
-- WordGrinder is licensed under the BSD open source license. See the COPYING
-- file in this distribution for the full text.
--
-- $Id$
-- $URL$

local table_remove = table.remove
local table_insert = table.insert
local table_concat = table.concat
local Write = wg.write
local WriteStyled = wg.writestyled
local ClearToEOL = wg.cleartoeol
local SetNormal = wg.setnormal
local SetBold = wg.setbold
local SetUnderline = wg.setunderline
local SetReverse = wg.setreverse
local SetDim = wg.setdim
local GetStringWidth = wg.getstringwidth
local GetBytesOfCharacter = wg.getbytesofcharacter
local GetWordText = wg.getwordtext

DocumentSetClass =
{
	-- remove any cached data prior to saving
	purge = function(self)
		for _, l in ipairs(self.documents) do
			l:purge()
		end
	end,
	
	touch = function(self)
		self.changed = true
		self.justchanged = true
	end,
	
	clean = function(self)
		self.changed = nil
		self.justchanged = nil
	end,
	
	getDocumentList = function(self)
		return self.documents
	end,
	
	_findDocument = function(self, name)
		for i, d in ipairs(self.documents) do
			if (d.name == name) then
				return i
			end
		end
		return nil
	end,
	
	findDocument = function(self, name)
		local document = self.documents[name]
		if not document then
			document = self.documents[self:_findDocument(name)]
			if document then
				ModalMessage("Document index inconsistency corrected",
					"Something freaky happened to '"..name.."'.")
				self.documents[name] = document
			end
		end
		return document
	end,
	
	addDocument = function(self, document, name, index)
		document.name = name
		
		local n = self:_findDocument(name) or (#self.documents + 1)
		self.documents[n] = document
		self.documents[name] = document
		if not self.current or (self.current.name == name) then
			self:setCurrent(name)
		end
		
		self:touch()
		RebuildDocumentsMenu(self.documents)
	end,
	
	moveDocumentIndexTo = function(self, name, targetIndex)
		local n = self:_findDocument(name)
		if not n then
			return
		end
		local document = self.documents[n]
		
		table_remove(self.documents, n)
		table_insert(self.documents, targetIndex, document)
		self:touch()
		RebuildDocumentsMenu(self.documents)
	end,
	
	deleteDocument = function(self, name)
		if (#self.documents == 1) then
			return false
		end
		
		local n = self:_findDocument(name)
		if not n then
			return
		end
		local document = self.documents[n]
		
		table_remove(self.documents, n)
		self.documents[name] = nil
		
		self:touch()
		RebuildDocumentsMenu(self.documents)
		
		if (Document == document) then
			document = self.documents[n]
			if not document then
				document = self.documents[#self.documents]
			end
			
			self:setCurrent(document.name)
		end
		
		return true
	end,
	
	setCurrent = function(self, name)
		-- Ensure any housekeeping on the current document gets done.
		
		if Document and Document.changed then
			FireEvent(Event.Changed)
		end

		Document = self.documents[name]
		if not Document then
			Document = self.documents[1]
		end

		self.current = Document
		ResizeScreen()
	end,
	
	renameDocument = function(self, oldname, newname)
		if self.documents[newname] then
			return false
		end
		
		local d = self.documents[oldname]
		self.documents[oldname] = nil
		self.documents[newname] = d
		d.name = newname
		
		self:touch()
		RebuildDocumentsMenu(self.documents)
		return true
	end,
	
	setClipboard = function(self, clipboard)
{'fraction_non_alphanumeric': 0.0924087591240876, 'fraction_numerical': 0.0020437956204379564, 'mean_word_length': 3.6828434723171566, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2841466', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2137, 'n_words': 626}