···1818 end
19192020 -- do not update with duplicate values
2121+ -- BUG: there seems to be some issues here when trying to add on pipup open
2122 for _, entry in ipairs(yanks) do
2223 if entry == text then
2324 return
···3334 end
34353536 -- add entry to persistent store
3636- persistence.add_entry(yanks, reg_types, opts)
3737+ persistence.add_entry(text, reg_type, opts)
3738end
3939+4040+-- TODO: autocmd for focus gained (check system clipboard?)
38413942-- autocommand to listen for yank events
4043---@param yanks table
···4548 callback = function()
4649 -- get register information
4750 local rn = vim.v.event.regname
4848- local reg_type = vim.fn.getregtype("+")
5151+ -- local reg_type = vim.fn.getregtype("+")
49525053 -- check changes wwere made to default register
5151- if vim.v.event.regname == "" then
5454+ if vim.v.event.regname == "" or vim.v.event.regname == "+" then
5555+ local reg_type = vim.fn.getregtype(rn)
5256 local yanked_text = vim.fn.getreg(rn)
5357 if #yanked_text <= 1 then
5458 return
+10-4
lua/yankbank/data.lua
···11-- data.lua
22local M = {}
3344--- reformat yanks table for popup
44+---reformat yanks table for popup
55---@param yanks table
66---@param sep string
77---@return table, table
···1313 -- calculate the maximum width needed for the yank numbers
1414 local max_digits = #tostring(#yanks)
15151616+ -- assumes yanks is table of strings
1617 for i, yank in ipairs(yanks) do
1718 yank_num = yank_num + 1
18191919- -- remove trailing newlines
2020- yank = yank:gsub("\n$", "")
2121- local yank_lines = vim.split(yank, "\n", { plain = true })
2020+ -- FIX: there were changes here, might need further changes
2121+ local yank_lines = yank
2222+ if type(yank) == "string" then
2323+ -- remove trailing newlines
2424+ yank = yank:gsub("\n$", "")
2525+ yank_lines = vim.split(yank, "\n", { plain = true })
2626+ end
2727+2228 local leading_space, leading_space_length
23292430 -- determine the number of leading whitespaces on the first line
+15-7
lua/yankbank/helpers.lua
···4545end
46464747-- customized paste function that functions like 'p'
4848----@param text string
4848+---@param text string|table
4949---@param reg_type string
5050function M.smart_paste(text, reg_type)
5151- -- convert text string to string list
5251 local lines = {}
5353- for line in text:gmatch("([^\n]*)\n?") do
5454- table.insert(lines, line)
5252+ if type(text) == "string" then
5353+ -- convert text string to string list
5454+ for line in text:gmatch("([^\n]*)\n?") do
5555+ table.insert(lines, line)
5656+ end
5757+ if #lines > 1 then
5858+ table.remove(lines)
5959+ end
6060+ else
6161+ -- text is already table
6262+ lines = text
5563 end
56645765 -- remove last newline character to replicate base put behavior
5858- if #lines > 1 then
5959- table.remove(lines)
6060- end
6666+ -- if lines[#lines] == "" then
6767+ -- table.remove(lines)
6868+ -- end
6169 vim.api.nvim_put(lines, reg_type, true, true)
6270end
6371
···1616function M.create_and_fill_buffer(yanks, reg_types, opts)
1717 -- check the content of the system clipboard register
1818 -- TODO: this could be replaced with some sort of polling of the + register
1919- local text = vim.fn.getreg("+")
2020- local most_recent_yank = yanks[1] or ""
2121- if text ~= most_recent_yank then
2222- local reg_type = vim.fn.getregtype("+")
2323- clipboard.add_yank(yanks, reg_types, text, reg_type, opts)
2424- end
1919+ -- local text = vim.fn.getreg("+")
2020+ -- local most_recent_yank = yanks[1] or ""
2121+ -- local reg_type = vim.fn.getregtype("+")
2222+ -- clipboard.add_yank(yanks, reg_types, text, reg_type, opts)
25232624 -- stop if yank table is empty
2725 if #yanks == 0 then
···3634 local current_filetype = vim.bo.filetype
3735 vim.api.nvim_set_option_value("filetype", current_filetype, { buf = bufnr })
38363737+ -- TODO: need to update yanks from bank file before get_display_lines
3938 local display_lines, line_yank_map = data.get_display_lines(yanks, opts.sep)
40394140 -- replace current buffer contents with updated table
+17-15
lua/yankbank/persistence.lua
···11-- persistence.lua
22local M = {}
3344----comment
55----@param yanks table
66----@param reg_types table
44+local persistence = {}
55+66+---add entry from bank to
77+---@param entry string|table
88+---@param reg_type string
79---@param opts table
88-function M.add_entry(yanks, reg_types, opts)
1010+function M.add_entry(entry, reg_type, opts)
911 if not opts.persist_type then
1010- return
1111- elseif opts.persist_type == "memory" then
1212 return
1313 elseif opts.persist_type == "file" then
1414- -- TODO:
1414+ persistence.add_to_bankfile(opts.persist_path, entry, reg_type)
1515 elseif opts.persist_type == "sqlite" then
1616+ -- TODO: implement sqlite persist
1617 end
1718end
1819···2021---@param yanks table
2122---@param reg_types table
2223---@param opts table
2424+---@return table
2525+---@return table
2326function M.setup(yanks, reg_types, opts)
2427 if not opts.persist_type then
2525- return
2828+ return {}, {}
2629 elseif opts.persist_type == "file" then
2727- -- TODO:
2828- require("yankbank.persistence.file").setup_persistence(
3030+ persistence = require("yankbank.persistence.file")
3131+ return persistence.setup_persistence(
2932 opts.persist_path,
3033 opts.max_entries,
3134 yanks,
···3336 )
3437 elseif opts.persist_type == "sqlite" then
3538 -- TODO:
3636- require("yankbank.persistence.sql").init_db(
3737- yanks,
3838- reg_types,
3939- opts.persist_path
4040- )
3939+ persistence = require("yankbank.persistence.sql")
4040+ persistence.init_db(yanks, reg_types, opts.persist_path)
4141 end
4242+4343+ return {}, {}
4244end
43454446return M
+47-53
lua/yankbank/persistence/file.lua
···11-- persistence/file.lua
22-32local M = {}
4354local n_entries = 0
65local m_entries = 10
7688--- function that checks if a file exists
77+---function that checks if a file exists
98---@param file string: file path
109---@return boolean
1110local function file_exists(file)
···1615 return f ~= nil
1716end
18171919--- function that reads all lines of file into a table
1818+---function that reads all lines of file into a table
2019---@param file string: file path
2120---@return table
2221local function read_lines(file)
···3231 return lines
3332end
34333535--- check first line from file for presence of yankbank list header.
3636--- if it exists, populate current number of entries.
3434+---check first line from file for presence of yankbank list header.
3535+---if it exists, populate current number of entries.
3736---@param line string
3837---@return boolean
3938local function check_for_header(line)
···4544 return false
4645end
47464848--- function that checks for the presence of a yankbank header on a given line.
4949--- returns t/f and index, length for entries that exist
4747+---function that checks for the presence of a yankbank header on a given line.
4848+---returns t/f and index, length for entries that exist
5049---@param line string: line from file being checked
5150---@return table|nil
5251local function check_for_entry(line)
···6059 end
6160end
62616363--- function that reads a yankbank entry from an index to an offset.
6262+---get line count of a string
6363+---@param str string
6464+---@return integer
6565+local function get_line_count(str)
6666+ local lines = 1
6767+ for i = 1, #str do
6868+ local c = str:sub(i, i)
6969+ if c == "\n" then
7070+ lines = lines + 1
7171+ end
7272+ end
7373+ return lines
7474+end
7575+7676+---function that reads a yankbank entry from an index to an offset.
6477---@param i integer: starting index
6578---@param offset integer: stopping point = i+offset
6679---@param lines table: file contents
···7083 for j = i, i + offset - 1 do
7184 entry[#entry + 1] = lines[j]
7285 end
8686+ -- handle extra newline added to end of entry in bank file
8787+ if #entry > 1 then
8888+ table.remove(entry)
8989+ end
7390 return entry
7491end
75927676--- remove entry from bankfile
9393+---remove entry from bankfile
7794---@param file string: bank file name
7895local function remove_last_entry(file)
7996 local f, err = io.open(file, "r+")
8097 if not f then
8198 error("Could not open file for reading: " .. err)
8299 end
8383- -- FIX: extra newline on entries inserted after removal
8410085101 -- read lines from file until matching entry is found
86102 local lines = {}
···89105 string.match(line, "<YANKBANK_ENTRY:" .. n_entries .. ",%d+,%a+>")
90106 then
91107 n_entries = n_entries - 1
9292- lines[1] = "<YANKBANK_LIST:" .. n_entries .. ">\n"
108108+ lines[1] = "<YANKBANK_LIST:" .. n_entries .. ">"
93109 break
94110 else
95111 lines[#lines + 1] = line
···103119 error("Could not open file for writing: " .. err)
104120 end
105121 for i = 1, #lines do
106106- -- TODO: check if newline is necessary for table
107122 f:write(lines[i] .. "\n")
108123 end
109124 f:close()
110125end
111126112112--- TODO: docs or remove function
113113-local function open_file(file, mode)
114114- local f, err = io.open(file, mode)
115115- if not f then
116116- error("Could not open file: " .. err)
117117- end
118118- return f
119119-end
120120-121121--- add entry bankfile. (this function needs to be callable from outside the module)
127127+---add entry bankfile. (this function needs to be callable from outside the module)
122128---@param file string
123129---@param entry table|string
124130---@param reg_type string
125125--- TODO: trigger in add_yank in clipboard.lua
126126--- Function scope probably needs to change to a different level (or be callable from persistence.lua)
127127-local function add_to_bankfile(file, entry, reg_type)
131131+function M.add_to_bankfile(file, entry, reg_type)
132132+ -- remove last entry if new capacity would exceed maximum
128133 if n_entries >= m_entries then
129134 remove_last_entry(file)
130135 end
131136 n_entries = n_entries + 1
132137133138 local lines = read_lines(file)
134134- local f = open_file(file, "w+")
139139+ local f, err = io.open(file, "w+")
140140+ if not f then
141141+ error("Could not open file: " .. err)
142142+ end
135143136144 -- add list header
137145 f:write("<YANKBANK_LIST:" .. n_entries .. ">\n")
138146147147+ -- get line count of entry (special case for strings)
148148+ local len = #entry
149149+ if type(entry) == "string" then
150150+ len = get_line_count(entry)
151151+ end
152152+139153 -- write entry header
140140- -- FIX: #entry doesn't match number of lines when it is string (number of chars instead of lines)
141141- f:write("<YANKBANK_ENTRY:1," .. #entry .. "," .. reg_type .. ">\n")
142142- -- write entry
154154+ f:write("<YANKBANK_ENTRY:1," .. len .. "," .. reg_type .. ">\n")
155155+ -- write new entry
143156 if type(entry) == "string" then
144144- f:write(entry)
157157+ f:write(entry .. "\n")
145158 else
146159 for i = 1, #entry do
147147- -- TODO: check if newline is necessary for table
148160 f:write(entry[i] .. "\n")
149161 end
150162 end
151163152152- -- write remaining lines
164164+ -- write back previous entries
153165 for i = 2, #lines do
154166 local n, l, rt =
155167 string.match(lines[i], "<YANKBANK_ENTRY:(%d+),(%d+),(%a+)>")
···162174 .. rt
163175 .. ">"
164176 end
165165- -- TODO: check headers
166177 f:write(lines[i] .. "\n")
167178 end
168179169180 f:close()
170181end
171182172172--- populate yankbank with entries contained in file.
183183+---populate yankbank with entries contained in file.
173184---@param yanks table: table to populate with yanks
174185---@param file string: yankbank persistence file
175186---@param max_entries integer: maximum number of yankbank entries
···200211 return yanks, reg_types
201212end
202213203203--- setup function for a persistence file.
204204--- should be called in plugin setup function
214214+---setup function for a persistence file.
215215+---should be called in plugin setup function
205216---@param file string: file path
206217---@param max_entries integer: maximum number of yankbank entries
207218---@param yanks table: table to populate with yanks
···223234 m_entries = max_entries
224235 return populate_yankbank(file, max_entries, yanks, reg_types)
225236end
226226-227227--- TEST: remove later
228228-local yanks = {}
229229-local reg_types = {}
230230-M.setup_persistence("test.txt", 10, yanks, reg_types)
231231--- print(vim.inspect(yanks))
232232--- print(vim.inspect(reg_types))
233233-yanks = {}
234234-reg_types = {}
235235--- print(vim.inspect(yanks))
236236--- print(vim.inspect(reg_types))
237237-M.setup_persistence("test1.txt", 10, yanks, reg_types)
238238-m_entries = 10
239239-add_to_bankfile("test1.txt", "text11", "v")
240240-os.execute("sleep .1")
241241--- add_to_bankfile("test1.txt", "text10", "V")
242242--- remove_last_entry("test1.txt")
243237244238return M