-1

I am getting the following error, but am finding it difficult to locate the problem. Are there any tools for debugging in such instances?

Warning (initialization): An error occurred while loading ‘/home/hagbard/.emacs’:

Invalid read syntax: )

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.

Have tried to locate the extra right parenthesis in the code, but been unsuccessful. Do not know quite well how to interpret the backtrace after doing emacs --debug-init.

Debugger entered--Lisp error: (invalid-read-syntax ")")
  read(#<buffer  *load*-313398>)
  eval-buffer(#<buffer  *load*-313398> nil "/home/hagbard/Opstk/bin/typex/rk..." nil t)  ; Reading at buffer position 8476
  load-with-code-conversion("/home/hagbard/Opstk/bin/typex/rk..." "/home/hagbard/Opstk/bin/typex/rk..." nil t)
  require(rk "rk-bracemarks")
  mrk-bracemarks()
  launch-marsha()
  typex-marsha-config()
  typex-launch()
  eval-buffer(#<buffer  *load*> nil "/home/hagbard/.emacs" nil t)  ; Reading at buffer position 1368
  load-with-code-conversion("/home/hagbard/.emacs" "/home/hagbard/.emacs" t t)
  load("~/.emacs" noerror nomessage)
  startup--load-user-init-file(#f(compiled-function () #<bytecode 0x155f192fd1f1>) #f(compiled-function () #<bytecode 0x155f192fd205>) t)
  command-line()
  normal-top-level()

Have clicked on the buffer position giving me this part of the code

(defun word-count-analysis (rgn-beg rgn-end &optional skip-words mxlen) "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 MXLEN Maximum length of word

Third optional parameter is a list of words to ignore; fourth means ignore words shortern than len."

(defun word-count-analysis (rgn-beg rgn-end &optional skip-words mxlen)
  "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.
MXLEN       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 reg-beg)
      (while (re-search-forward "\\w+" reg-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 mxlen) (> (length $word-str) mxlen))
                 (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))
2
  • 2
    As you can see: Start Emacs with the ‘--debug-init’ option to view a complete error backtrace. i.e. emacs --debug-init Mar 23, 2022 at 1:41
  • You have an extra right paren in your code. Doing as both Emacs and @TianshuWang suggested is one good way to find the problem.
    – Drew
    Mar 23, 2022 at 1:44

2 Answers 2

2

You should be able to click on the ... to find the complete file name where the error occurred. Then if you scroll all the way to the right in the backtrace you will see that the error occurred at position 8476 in that buffer. So visit the file, then say C-u 8475 C-f (N.B. one less than the buffer position) and you should be on the errant closing paren. Delete it, save the file, close emacs and restart it to check that the error has gone away.

Or click on the buffer position link (as you seem to have done), but note that only takes you to the buffer of the file that is active in this instance of Emacs: you can delete the paren under the cursor, but that is NOT going to change the file, so next time you try it it will still show the error.

If you then get different errors, you will want to pay more attention to the piece of code before that position. @phil's pointers to Emacs's paren navigation features should be valuable to help you read the code and figure out what's wrong with it. And if worse comes to worst, then read the Debugging section in the Emacs Lisp manual.

As for the "could not find anything wrong with the function itself" statement: everything may look right to you but since the parens don't look right to emacs, you should believe emacs. It's easy to miscount parens, but if you can't see it, then cut and paste the function as is into another question and see if somebody can see what's wrong with it. This is where @phil's pointers above can really help.

1

Have tried to locate the extra right parenthesis in the code, but been unsuccessful.

M-x check-parens will find unbalanced parens in the current buffer.

You should also know how to navigate using balanced expressions, which will let you confirm the code structure quickly. Start reading at C-hig (emacs)Parentheses and make yourself familiar with all of the features covered under that section.

If that's insufficient, we can get more specific details from the debugger...

Debugger entered--Lisp error: (invalid-read-syntax ")")
  read(#<buffer  *load*-313398>)
  eval-buffer(#<buffer  *load*-313398> nil "/home/hagbard/Opstk/bin/typex/rk..." nil t)  ; Reading at buffer position 8476
  load-with-code-conversion("/home/hagbard/Opstk/bin/typex/rk..." "/home/hagbard/Opstk/bin/typex/rk..." nil t)
  require(rk "rk-bracemarks")
  ...

With the debugger still open, you can inspect the buffer with the code that Emacs is attempting to read. In this backtrace example we're looking for #<buffer *load*-313398>, which you can switch to by name with C-xb as usual to see what it is that the lisp reader is failing to read (I would expect the cursor to be sitting at the relevant position). Note that the buffer name " *load*-313398" begins with a space (as do all 'hidden' buffers in Emacs).

We can also see from the require call that the library in question is rk-bracemarks and, as NickD pointed out, if you click on the ... Emacs should show the full path to the file. You can visit that file to fix the bug.

11
  • The biggest problem right now is finding the specific file reporting the error.
    – Dilna
    Mar 23, 2022 at 3:19
  • I have done C-M-f. Still traversing the file does not show any problems.
    – Dilna
    Mar 23, 2022 at 17:53
  • Did you click on the ... after the incomplete file name in the backtrace?
    – NickD
    Mar 23, 2022 at 21:34
  • Yes, it gave bu the filename and the comment ; Reading at buffer position 8804. I then clicked on the link when the mouse is placed over buffer position 8804 which directed me to the function, with the cursor on a line. But could not find anything wrong with the function itself. The other functions in the file seem correct as well, from the parentheses point of view.
    – Dilna
    Mar 24, 2022 at 1:20
  • I have done as you explained, I moved the mouse to show the reading position and clicked on it.
    – Dilna
    Mar 24, 2022 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.