summaryrefslogtreecommitdiff
path: root/autoload/latex-wordcount.vim
blob: a3dca185f14767021bbd0a9a905267d82a11bf6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
--[[
count_latex_words.lua

Count the number of "real words" in a LaTeX (.tex) document -- i.e. the
words that would actually appear as visible text if the document were
typeset, excluding preamble, command definitions, commands, math, comments,
labels, citations, includes, and similar non-prose content.

This is a pattern/heuristic-based tool, not a full LaTeX parser, so it will
not be 100% perfect on every document (LaTeX is not a regular language).
It mirrors a Python implementation of the same approach and aims to be a
practical approximation that handles the vast majority of real documents.

USAGE AS A NEOVIM PLUGIN MODULE
--------------------------------
    local wc = require("count_latex_words")

    -- Count the current buffer using the default rules:
    local n = wc.count_buffer(0)

    -- Count the current buffer with a custom config file:
    local config = wc.default_config()
    wc.load_config(config, "/path/to/mycmds.json")
    local n = wc.count_buffer(0, config)

    -- Count an arbitrary file or string:
    local n = wc.count_file("paper.tex", config)
    local n = wc.count_text("\\textbf{hello} world", config)

If loaded inside Neovim, this module also registers a `:LatexWordCount`
user command (optionally takes a path to a JSON config file as its arg).

USAGE AS A STANDALONE SCRIPT
------------------------------
    lua count_latex_words.lua paper.tex
    lua count_latex_words.lua paper.tex mycmds.json

CUSTOM COMMANDS (JSON CONFIG)
------------------------------
Pass a JSON file describing how your own macros should be treated:

    {
      "drop_environments":  ["myfigureenv"],
      "drop_commands":      ["mysecretnote"],
      "keep_arg_commands":  ["myterm"],
      "zero_arg_commands":  ["companyname"],
      "custom_arg_commands": {
        "mytranslation": ["drop", "keep"]
      }
    }

  - drop_environments:   environments whose entire contents are discarded.
  - drop_commands:       commands whose arguments carry no visible words;
                          command + all {..} args are removed.
  - keep_arg_commands:   commands wrapping a single word/phrase to keep;
                          command is stripped, its {..} argument is kept.
  - zero_arg_commands:   commands with no argument that expand to nothing
                          countable; just the command token is removed.
  - custom_arg_commands: commands with MULTIPLE {..} arguments where each
                          argument is individually "keep" or "drop", matched
                          in order, e.g. \mytranslation{fr}{en} ->
                          ["drop", "keep"] keeps only the English text.

Config rules extend the built-in defaults. If a command name is listed
under a new category, it is automatically removed from any other category
to avoid ambiguity (so a config file can also *reclassify* a built-in).
--]]

local M = {}

-- ---------------------------------------------------------------------
-- Default rule sets
-- ---------------------------------------------------------------------

local function to_set(list)
  local s = {}
  for _, v in ipairs(list) do
    s[v] = true
  end
  return s
end

local DEFAULT_DROP_ENVIRONMENTS = {
  "equation", "equation*", "align", "align*", "alignat", "alignat*",
  "gather", "gather*", "multline", "multline*", "eqnarray", "eqnarray*",
  "verbatim", "verbatim*", "Verbatim", "lstlisting", "minted", "algorithm",
  "algorithmic", "tikzpicture", "pgfpicture", "comment",
}

local DEFAULT_DROP_COMMANDS = {
  "label", "ref", "eqref", "pageref", "autoref", "nameref", "vref",
  "cite", "citep", "citet", "citeauthor", "citeyear", "citealp", "citealt",
  "nocite", "bibliography", "bibliographystyle",
  "usepackage", "documentclass", "RequirePackage",
  "input", "include", "includeonly", "includegraphics", "includepdf",
  "newcommand", "renewcommand", "providecommand", "DeclareMathOperator",
  "newenvironment", "renewenvironment", "newcolumntype", "definecolor",
  "colorlet", "setlength", "addtolength", "setcounter", "addtocounter",
  "hspace", "vspace", "linespread", "pagestyle",
  "thispagestyle", "graphicspath", "geometry", "hypersetup",
  "bibinput", "newpage", "clearpage", "cleardoublepage",
  "pagebreak", "linebreak", "nopagebreak", "nolinebreak", "appendix",
}

local DEFAULT_KEEP_ARG_COMMANDS = {
  "textbf", "textit", "emph", "underline", "texttt", "textsc", "textsf",
  "textrm", "textnormal", "text", "mbox", "uline", "uwave", "sout",
  "section", "subsection", "subsubsection", "paragraph", "subparagraph",
  "chapter", "part", "caption", "captionof", "footnote", "footnotetext",
  "title", "author", "date", "thanks",
}

local DEFAULT_ZERO_ARG_COMMANDS = {
  "maketitle", "tableofcontents", "listoffigures", "listoftables",
  "noindent", "indent", "par", "newline", "smallskip", "medskip",
  "bigskip", "today", "LaTeX", "LaTeXe", "TeX", "item",
}

-- Commands with MULTIPLE {..} arguments, each individually "keep"/"drop".
local DEFAULT_CUSTOM_ARG_COMMANDS = {
  textcolor = { "drop", "keep" },
  colorbox = { "drop", "keep" },
  href = { "drop", "keep" },
}

function M.default_config()
  local custom = {}
  for k, v in pairs(DEFAULT_CUSTOM_ARG_COMMANDS) do
    custom[k] = { v[1], v[2] }
  end
  return {
    drop_environments = to_set(DEFAULT_DROP_ENVIRONMENTS),
    drop_commands = to_set(DEFAULT_DROP_COMMANDS),
    keep_arg_commands = to_set(DEFAULT_KEEP_ARG_COMMANDS),
    zero_arg_commands = to_set(DEFAULT_ZERO_ARG_COMMANDS),
    custom_arg_commands = custom,
  }
end

function M.build_example_config()
  return {
    drop_environments = { "myfigureenv" },
    drop_commands = { "mysecretnote", "internalcomment" },
    keep_arg_commands = { "myterm", "highlightphrase" },
    zero_arg_commands = { "companyname" },
    custom_arg_commands = {
      mytooltip = { "keep", "drop" },
      mytranslation = { "drop", "keep" },
    },
  }
end

-- ---------------------------------------------------------------------
-- Minimal JSON decoder (fallback for use outside Neovim; Neovim's
-- built-in vim.json.decode / vim.fn.json_decode is preferred when present)
-- ---------------------------------------------------------------------

local json_parse_value -- forward declaration

local function json_skip_ws(s, i)
  while i <= #s and s:sub(i, i):match("%s") do
    i = i + 1
  end
  return i
end

local function json_parse_string(s, i)
  local j = i + 1
  local buf = {}
  local escmap = { ['"'] = '"', ['\\'] = '\\', ['/'] = '/', b = '\b', f = '\f', n = '\n', r = '\r', t = '\t' }
  while j <= #s do
    local c = s:sub(j, j)
    if c == '"' then
      return table.concat(buf), j + 1
    elseif c == "\\" then
      local nc = s:sub(j + 1, j + 1)
      if escmap[nc] then
        table.insert(buf, escmap[nc])
        j = j + 2
      elseif nc == "u" then
        local hex = s:sub(j + 2, j + 5)
        local code = tonumber(hex, 16) or 63
        if code < 0x80 then
          table.insert(buf, string.char(code))
        elseif utf8 and utf8.char then
          table.insert(buf, utf8.char(code))
        else
          table.insert(buf, "?")
        end
        j = j + 6
      else
        table.insert(buf, nc)
        j = j + 2
      end
    else
      table.insert(buf, c)
      j = j + 1
    end
  end
  error("count_latex_words: unterminated string in JSON config")
end

local function json_parse_number(s, i)
  local j = i
  while j <= #s and s:sub(j, j):match("[%d%+%-%.eE]") do
    j = j + 1
  end
  return tonumber(s:sub(i, j - 1)), j
end

local function json_parse_array(s, i)
  local arr = {}
  i = json_skip_ws(s, i + 1)
  if s:sub(i, i) == "]" then
    return arr, i + 1
  end
  while true do
    local val
    val, i = json_parse_value(s, i)
    table.insert(arr, val)
    i = json_skip_ws(s, i)
    local c = s:sub(i, i)
    if c == "," then
      i = json_skip_ws(s, i + 1)
    elseif c == "]" then
      return arr, i + 1
    else
      error("count_latex_words: malformed JSON array in config")
    end
  end
end

local function json_parse_object(s, i)
  local obj = {}
  i = json_skip_ws(s, i + 1)
  if s:sub(i, i) == "}" then
    return obj, i + 1
  end
  while true do
    i = json_skip_ws(s, i)
    if s:sub(i, i) ~= '"' then
      error("count_latex_words: expected string key in JSON config")
    end
    local key
    key, i = json_parse_string(s, i)
    i = json_skip_ws(s, i)
    if s:sub(i, i) ~= ":" then
      error("count_latex_words: expected ':' in JSON config")
    end
    i = json_skip_ws(s, i + 1)
    local val
    val, i = json_parse_value(s, i)
    obj[key] = val
    i = json_skip_ws(s, i)
    local c = s:sub(i, i)
    if c == "," then
      i = i + 1
    elseif c == "}" then
      return obj, i + 1
    else
      error("count_latex_words: malformed JSON object in config")
    end
  end
end

json_parse_value = function(s, i)
  i = json_skip_ws(s, i)
  local c = s:sub(i, i)
  if c == '"' then
    return json_parse_string(s, i)
  elseif c == "{" then
    return json_parse_object(s, i)
  elseif c == "[" then
    return json_parse_array(s, i)
  elseif s:sub(i, i + 3) == "true" then
    return true, i + 4
  elseif s:sub(i, i + 4) == "false" then
    return false, i + 5
  elseif s:sub(i, i + 3) == "null" then
    return nil, i + 4
  else
    return json_parse_number(s, i)
  end
end

function M.json_decode(str)
  if vim ~= nil then
    if vim.json and vim.json.decode then
      local ok, result = pcall(vim.json.decode, str)
      if ok then
        return result
      end
    end
    if vim.fn and vim.fn.json_decode then
      local ok, result = pcall(vim.fn.json_decode, str)
      if ok then
        return result
      end
    end
  end
  local val = json_parse_value(str, 1)
  return val
end

-- ---------------------------------------------------------------------
-- Config loading
-- ---------------------------------------------------------------------

--- Merge rules from a JSON config file into an existing config table.
-- @param config table returned by M.default_config()
-- @param path string path to a JSON config file
function M.load_config(config, path)
  local f, err = io.open(path, "r")
  if not f then
    error("count_latex_words: could not open config file: " .. tostring(err))
  end
  local content = f:read("*a")
  f:close()

  local data = M.json_decode(content)
  if type(data) ~= "table" then
    return config
  end

  local function merge_list(key, target)
    if data[key] then
      for _, name in ipairs(data[key]) do
        target[name] = true
      end
    end
  end

  merge_list("drop_environments", config.drop_environments)
  merge_list("drop_commands", config.drop_commands)
  merge_list("keep_arg_commands", config.keep_arg_commands)
  merge_list("zero_arg_commands", config.zero_arg_commands)

  if data.custom_arg_commands then
    for name, pattern in pairs(data.custom_arg_commands) do
      config.custom_arg_commands[name] = pattern
    end
  end

  -- Reclassify: a command named under a new category is removed from any
  -- other category so config-file rules take priority over defaults.
  local plain_keys = { "drop_commands", "keep_arg_commands", "zero_arg_commands" }
  for _, key in ipairs(plain_keys) do
    if data[key] then
      for _, name in ipairs(data[key]) do
        for _, other in ipairs(plain_keys) do
          if other ~= key then
            config[other][name] = nil
          end
        end
        config.custom_arg_commands[name] = nil
      end
    end
  end
  if data.custom_arg_commands then
    for name, _ in pairs(data.custom_arg_commands) do
      config.drop_commands[name] = nil
      config.keep_arg_commands[name] = nil
      config.zero_arg_commands[name] = nil
    end
  end

  return config
end

-- ---------------------------------------------------------------------
-- Cleaning pipeline
-- ---------------------------------------------------------------------

--- Remove LaTeX comments (unescaped % to end of line).
function M.strip_comments(text)
  local out_lines = {}
  for line in (text .. "\n"):gmatch("(.-)\n") do
    local buf = {}
    local i, n = 1, #line
    while i <= n do
      local c = line:sub(i, i)
      if c == "\\" then
        table.insert(buf, c)
        if i < n then
          table.insert(buf, line:sub(i + 1, i + 1))
          i = i + 2
        else
          i = i + 1
        end
      elseif c == "%" then
        break
      else
        table.insert(buf, c)
        i = i + 1
      end
    end
    table.insert(out_lines, table.concat(buf))
  end
  -- (.."\n"):gmatch("(.-)\n") yields one trailing empty line; drop it if
  -- the original text didn't end with a newline itself is not needed here
  -- since callers only care about the joined result below.
  return table.concat(out_lines, "\n")
end

--- Keep only the content between \begin{document} and \end{document}.
function M.extract_body(text)
  local _, _, body = text:find("\\begin{document}(.-)\\end{document}")
  return body or text
end

--- Escape Lua pattern magic characters in a literal string.
local function escape_pattern(s)
  return (s:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1"))
end
M.escape_pattern = escape_pattern

--- Remove the entire contents of math/code/graphics-language environments.
function M.drop_environments(text, config)
  for env, _ in pairs(config.drop_environments) do
    local pat = "\\begin{" .. escape_pattern(env) .. "}.-\\end{" .. escape_pattern(env) .. "}"
    text = text:gsub(pat, " ")
  end
  return text
end

-- Lua patterns have no lookbehind, so a plain gsub can't tell an escaped
-- "\$" apart from a real math delimiter. Scan manually instead, treating
-- "$$" as display math and a lone "$" as inline math, skipping either kind
-- when immediately preceded by a backslash.
local function strip_dollar_math(text)
  local out = {}
  local i, n = 1, #text
  while i <= n do
    local c = text:sub(i, i)
    if c == "$" and text:sub(i - 1, i - 1) ~= "\\" then
      local double = text:sub(i + 1, i + 1) == "$"
      local delim = double and "$$" or "$"
      local dlen = double and 2 or 1
      local j = i + dlen
      local found = nil
      while j <= n do
        if text:sub(j, j + dlen - 1) == delim and text:sub(j - 1, j - 1) ~= "\\" then
          found = j
          break
        end
        j = j + 1
      end
      if found then
        table.insert(out, " ")
        i = found + dlen
      else
        -- No closing delimiter found; treat as literal and move on.
        table.insert(out, c)
        i = i + 1
      end
    else
      table.insert(out, c)
      i = i + 1
    end
  end
  return table.concat(out)
end

--- Remove inline/display math: $...$, $$...$$, \(...\), \[...\].
function M.drop_math(text)
  text = strip_dollar_math(text)
  text = text:gsub("\\%(.-\\%)", " ")
  text = text:gsub("\\%[.-\\%]", " ")
  return text
end

local ESCAPED_CHARS = {
  { "\\%%", "%%" },
  { "\\&", "&" },
  { "\\%$", "$" },
  { "\\#", "#" },
  { "\\_", "_" },
  { "\\{", "{" },
  { "\\}", "}" },
}

function M.replace_escaped_chars(text)
  for _, pair in ipairs(ESCAPED_CHARS) do
    text = text:gsub(pair[1], pair[2])
  end
  return text
end

--- If text[pos] starts an optional [..] argument, return the position just
-- past its matching closing bracket; otherwise return pos unchanged.
function M.strip_optional_arg(text, pos)
  if text:sub(pos, pos) == "[" then
    local s, e = text:find("^%b[]", pos)
    if s then
      return e + 1
    end
  end
  return pos
end

--- Given text:sub(pos,pos) == "{", return (content, end_index) where
-- end_index is the index just past the matching closing brace. Handles
-- nesting via Lua's %b{} balanced-match pattern item.
function M.read_braced_group(text, pos)
  local s, e = text:find("^%b{}", pos)
  if s then
    return text:sub(s + 1, e - 1), e + 1
  end
  -- Unbalanced braces -- bail out, consume to end of string.
  return text:sub(pos + 1), #text + 1
end

--- Walk through text once, handling \commandname[opt]{arg}{arg2}... per the
-- config's rule sets, with a fallback for unknown commands that keeps the
-- first {..} argument's text (most custom macros just wrap visible text).
function M.process_commands(text, config)
  local out = {}
  local i, n = 1, #text

  while i <= n do
    local next_bs = text:find("\\", i, true)
    if not next_bs then
      table.insert(out, text:sub(i))
      break
    end
    if next_bs > i then
      table.insert(out, text:sub(i, next_bs - 1))
    end
    i = next_bs

    local s, e, name = text:find("^\\(%a+)%*?", i)
    if s then
      local j = e + 1

      if config.custom_arg_commands[name] then
        j = M.strip_optional_arg(text, j)
        for _, role in ipairs(config.custom_arg_commands[name]) do
          if text:sub(j, j) == "{" then
            local content
            content, j = M.read_braced_group(text, j)
            if role == "keep" then
              table.insert(out, M.process_commands(content, config))
            end
          else
            break
          end
        end
        i = j
      elseif config.drop_commands[name] then
        j = M.strip_optional_arg(text, j)
        while text:sub(j, j) == "{" do
          local _, jn = M.read_braced_group(text, j)
          j = jn
        end
        i = j
      elseif config.zero_arg_commands[name] then
        j = M.strip_optional_arg(text, j)
        i = j
      elseif config.keep_arg_commands[name] then
        j = M.strip_optional_arg(text, j)
        if text:sub(j, j) == "{" then
          local content
          content, j = M.read_braced_group(text, j)
          table.insert(out, M.process_commands(content, config))
        end
        i = j
      elseif name == "begin" or name == "end" then
        j = M.strip_optional_arg(text, j)
        if text:sub(j, j) == "{" then
          local _, jn = M.read_braced_group(text, j)
          j = jn
        end
        i = j
      else
        -- Unknown command: drop the name, skip an optional [..], keep the
        -- first {..} argument's text.
        j = M.strip_optional_arg(text, j)
        if text:sub(j, j) == "{" then
          local content
          content, j = M.read_braced_group(text, j)
          table.insert(out, M.process_commands(content, config))
        end
        i = j
      end
    else
      -- Backslash not followed by a letter: "\\" line break, or stray
      -- backslash (already-escaped chars like \% were handled earlier).
      if text:sub(i, i + 1) == "\\\\" then
        table.insert(out, " ")
        i = i + 2
      else
        i = i + 1
      end
    end
  end

  return table.concat(out)
end

--- Remove stray braces, alignment/table markup, and collapse whitespace.
function M.clean_leftovers(text)
  text = text:gsub("{", " ")
  text = text:gsub("}", " ")
  text = text:gsub("&", " ")
  text = text:gsub("~", " ")
  text = text:gsub("%-%-%-?", " ") -- en/em dashes (-- or ---) as separators
  text = text:gsub("%s+", " ")
  text = text:match("^%s*(.-)%s*$")
  return text
end

--- Tokenize cleaned text into words. A "word" must contain at least one
-- letter or digit (filters out stray punctuation / leftover markup).
function M.count_words(text)
  local words = {}
  for tok in text:gmatch("%S+") do
    local trimmed = tok:gsub("^[%.,;:!%?%(%)%[%]\"'`]+", "")
    trimmed = trimmed:gsub("[%.,;:!%?%(%)%[%]\"'`]+$", "")
    if trimmed:match("%w") then
      table.insert(words, trimmed)
    end
  end
  return words
end

-- ---------------------------------------------------------------------
-- Top-level entry points
-- ---------------------------------------------------------------------

--- Run the full pipeline over a raw LaTeX string.
-- @return count, words (table), cleaned_text
function M.count_text(text, config)
  config = config or M.default_config()
  local t = text
  t = M.strip_comments(t)
  t = M.extract_body(t)
  t = M.drop_environments(t, config)
  t = M.drop_math(t)
  t = M.replace_escaped_chars(t)
  t = M.process_commands(t, config)
  t = M.clean_leftovers(t)
  local words = M.count_words(t)
  return #words, words, t
end

--- Run the full pipeline over a file on disk.
function M.count_file(path, config)
  local f, err = io.open(path, "r")
  if not f then
    error("count_latex_words: could not open file: " .. tostring(err))
  end
  local content = f:read("*a")
  f:close()
  return M.count_text(content, config)
end

--- Run the full pipeline over a Neovim buffer's contents.
-- @param bufnr number|nil buffer handle (0 or nil = current buffer)
function M.count_buffer(bufnr, config)
  if vim == nil then
    error("count_latex_words: count_buffer() requires Neovim's 'vim' API")
  end
  bufnr = bufnr or 0
  local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
  local text = table.concat(lines, "\n")
  return M.count_text(text, config)
end

-- ---------------------------------------------------------------------
-- Neovim plugin wiring (only active when loaded inside Neovim)
-- ---------------------------------------------------------------------

if vim ~= nil and vim.api ~= nil and vim.api.nvim_create_user_command ~= nil then
  vim.api.nvim_create_user_command("LatexWordCount", function(opts)
    local config = M.default_config()
    if opts.args and opts.args ~= "" then
      local ok, err = pcall(M.load_config, config, opts.args)
      if not ok then
        vim.notify(tostring(err), vim.log.levels.ERROR)
        return
      end
    end
    local count = M.count_buffer(0, config)
    vim.notify(string.format("Real words: %d", count))
  end, { nargs = "?", complete = "file", desc = "Count real (visible) words in the current LaTeX buffer" })
end

-- ---------------------------------------------------------------------
-- Standalone CLI usage: `lua count_latex_words.lua paper.tex [config.json]`
-- ---------------------------------------------------------------------

if vim == nil and arg ~= nil and arg[0] and arg[1] then
  local config = M.default_config()
  if arg[2] then
    M.load_config(config, arg[2])
  end
  local count, _, cleaned = M.count_file(arg[1], config)
  if os.getenv("LATEX_WORDS_VERBOSE") then
    io.stderr:write("--- Cleaned text ---\n" .. cleaned .. "\n--- End cleaned text ---\n\n")
  end
  print(string.format("%s: %d words", arg[1], count))
end

return M