Created
March 10, 2025 21:36
-
-
Save jasonpott/151d02c4d4f001cee9113aa5ff1b6ce6 to your computer and use it in GitHub Desktop.
Plugin to run languagetool CLI on markdown files and populate a quicklist with the message and suggested corrections. Written with ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local M = {} | |
| -- Configuration options | |
| M.config = { | |
| enable_in_all_folders = false, -- Set to true to enable word count globally for all directories | |
| } | |
| -- Function to check if word count should be enabled | |
| local function should_enable_wordcount() | |
| -- Check for the .wordcount_enable file in the current directory | |
| local cwd = vim.fn.getcwd() | |
| local enable_file = cwd .. "/.wordcount_enable" | |
| if M.config.enable_in_all_folders then | |
| -- Enable word count for all directories containing markdown files | |
| return true | |
| end | |
| -- Check if the .wordcount_enable file exists in the current directory | |
| return vim.fn.filereadable(enable_file) == 1 | |
| end | |
| -- Function to log word count when a markdown file is saved | |
| local function log_word_count() | |
| if should_enable_wordcount() then | |
| local file_name = vim.fn.expand("%:p") | |
| local word_count = vim.fn.wordcount().words | |
| local word_count_change = word_count - (vim.b.word_count or 0) | |
| -- Update the previous word count | |
| vim.b.word_count = word_count | |
| -- Get current time | |
| local timestamp = os.date("%Y-%m-%d %H:%M:%S") | |
| -- Log to CSV file | |
| local csv_file = vim.fn.getcwd() .. "/word_count_log.csv" | |
| local file = io.open(csv_file, "a") | |
| file:write(string.format("%s, %s, %d, %d\n", timestamp, file_name, word_count, word_count_change)) | |
| file:close() | |
| end | |
| end | |
| -- Setup function | |
| function M.setup(config) | |
| if config then | |
| M.config = vim.tbl_extend("force", M.config, config) | |
| end | |
| -- Attach the word count logger to markdown file saving | |
| vim.api.nvim_create_autocmd("BufWritePost", { | |
| pattern = "*.md", | |
| callback = log_word_count, | |
| }) | |
| -- Command to enable word count in the current directory | |
| vim.api.nvim_create_user_command("EnableWordCount", function() | |
| print("EnableWordCount command triggered!") | |
| local cwd = vim.fn.getcwd() | |
| local enable_file = cwd .. "/.wordcount_enable" | |
| -- Create the .wordcount_enable file if it doesn't exist | |
| -- Attempt to open the file for writing | |
| local file = io.open(enable_file, "w") | |
| if file then | |
| -- File opened successfully, write to it | |
| file:write("Word count tracking enabled for this directory.\n") | |
| file:close() | |
| print("Word count tracking enabled for this directory.") | |
| else | |
| -- Handle the case where the file could not be opened | |
| print("Error: Could not create .wordcount_enable file in " .. cwd) | |
| end | |
| end, {}) | |
| end | |
| return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment