0

Have been debugging a minor-mode I am writing. After much torture spend locating the problem, I have now narrowed things down to the following function. Still, I have not been able to fix the problem eval-buffer: Invalid read syntax: ")".


    (defun word-count-analysis (rgn-beg rgn-end &optional skip-words mnlen)
      "Count times each word is used in a region, ignoring punctuation.
    
    RGN-BEG     Start of region
    RGN-END     End of region
    SKIP-WORDS  Words to ignore
    MNLEN       Minimum length of word."
    
      (interactive "rxp")
    
      (let ( ($cur-table (syntax-table))
          $words $word-str $word $cell )
    
        (set-syntax-table word-count-analysis-syntax-table)
    
        (save-excursion
          (goto-char rgn-beg)
          (while (re-search-forward "\\w+" rgn-end t)
            (setq $word-str (downcase (match-string 0)))
    
            ;; Keep punctuation in words (e.g. 5.10 and 4,300) but not if
            ;; punctuation occurs at the end of a word.
        (if (string-match "\\(.+\\)[,.]$" $word-str)
                (setq $word-str (match-string 1 $word-str)))
    
            (setq $word (intern $word-str))
            (setq $cell (assq $word $words))
            (if (and (not (eq $word ',)) ; ignore comma at end of words
                     (not (eq $word '.)) ; ignore period stop at end of words
                     (or (not mnlen) (> (length $word-str) mnlen))
                     (or (not skip-words) (not (memq $word skip-words))))
                (if $cell
                    (setcdr $cell (1+ (cdr $cell)))
                  (setq $words (cons (cons $word 1) $words)))) ))
    
        ;; restore table
        (set-syntax-table $cur-table)
        (when (interactive-p)
          (message "%S" $words))
        $words))
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10
  • 1
    Take a look at [this question](https://emacs.stackexchange.com/questions/13514/how-to-obtain-the-statistic-of-the-the-frequency-of-words-in-a-buffer) for some inspiration of how to calculate word frequencies. – NickD Mar 26 '22 at 01:34

1 Answers1

0

If you do C-M-x (eval-defun), the error message reports the location of the error: it is the last ) of the form (eq $word ',).

I think that the issue is that you can't have a symbol called ,.

TBH, I think your strategy of turning strings into symbols is creating more problems for you than it solves.

Fran Burstall
  • 3,665
  • 10
  • 18
  • Yes, that's where it reports the error. But am still very far from a fix. I want to list the words within a region together with a count of of how many times each word is present. – Dilna Mar 26 '22 at 00:42
  • 2
    You keep doing that: you ask a question, somebody answers it and then you change the question. Why don't you ask *the question that you want answered in the first place*? In this case, why don't you ask: "How do I write a function that returns a list of words found in a region together with how many times each word appears in the region"? Instead, you ask: "What's wrong with this function?", somebody tells you *exactly* what's wrong with this function and then *you change the question*! Don't do that . My $0.02 of advice. – NickD Mar 26 '22 at 01:10
  • 1
    I agree with @NickD. OP Veak: Please try to learn how to interact cooperatively here, or it's likely people will help you less. – Drew Mar 26 '22 at 02:19
  • 1
    You can easily have a symbol `,`, that is, whose `symbol-name` is `","`. Just escape the comma: `(setq foo '\,)`. – Drew Mar 26 '22 at 02:21
  • Emacs kept complaining about the `)`. Seems evident that the problem was not because I had an extra closing parenthesis, but a problem of syntax for what I have been trying to do. I will put the question of how better to list and count words in another post. – Dilna Mar 26 '22 at 03:31
  • A comma `,` causes the elisp reader to read the following expression/argument. It's intended to be used within backquoted expressions, to unquote/evaluate that expression and substitute the evaluated value. Here the argument for the comma was `)` which wasn't a valid expression, and caused your error. – phils Mar 26 '22 at 04:14
  • And to corroborate Drew's comment, I found your mailing list response "Have not played with that, no." to someone's question "Have you tried calling `check-parens` in the files?" absolutely maddening when I'd spent about an hour going back and forth with you in comments on https://emacs.stackexchange.com/a/71077 where my *very first suggestion* had been to use `check-parens`. It wasn't the only attempt at helping that you seemed to pay no attention to, so I probably won't be responding to any other questions of yours. – phils Mar 26 '22 at 04:30
  • I had done many things, commenting parts of the file. Splitting the file. Using C-M-f. Trying `emacs --debug-init`. My impression had been that there was an extra `)`. But it turned out the problem was with symbol `,`. Been trying many suggested approaches, and I missed that one. And tried it later on. All these commands are quite new to me and takes time to absorb. Then there is the task of fixing the problem regarding a strategy that has been described to me as difficult and much prone to difficulties. – Dilna Mar 26 '22 at 05:50