Reacting to the "wrong argument"

So I start to learn a little lisp / elisp to optimize the emacs environment and I started building a simple emacs library, the main road block lets you know if the nested parenthesis has a match or not. I was looking at the emacs source (paren.el.gz) and realized that I can use a function show-paren-function

to determine if it is a match or not.

Here's what I have so far:

(defun it-is-a-paren()
    (insert ")")
    (if (show-paren-function)
        (message "%s" "it is a match") 
      (message "%s" "it is not")))


So it's pretty simple, and "this match" works as it should, but when it should get out "it isn't," it isn't, instead it gives me "Invalid argument of type: integer- or-marker-p, t ".

Is anyone familiar enough to advise using a different function, or maybe I should write myself instead of using show-paren-function

. Or is there a way for this error (like handling exceptions)?


source to share

2 answers

The construct used as the "exception handling" you are looking for is condition-case


(defun its-a-paren()
  (insert ")")
  (condition-case ex
      (if (show-paren-function)
          (message "its a match")
        (message "its not"))
    (error (message "its not"))))


Edit: Looking at the code show-paren-function

, it seems to me that this error is an error as it comes from an expression (goto-char pos)

where pos

- t


In any case, show-paren-function

uses scan-sexps

to find the appropriate partner. Adapting to how it's done in show-paren-function

, a simplified function for your case:

(defun its-a-paren()
  (insert ")")
  (condition-case ()
        (scan-sexps (point) -1)
        (message "It a match"))
    (error (message "It not a match"))))




Using the show-paren function for this purpose is overkill (for example if your car in the garage checks if the oil level has changed to determine if the car needs more oil) and does not work correctly as you noticed.

I would recommend that you try

(condition-case nil
    (progn (forward-sexp -1)
           (message "Moved to the matching opener"))
  (scan-error (message "No matching opener")))




All Articles