1

This is a simple logic test that makes use of down-list and asks if another nested pair of parenthesis exits or not.

  (not (eql (point)
        (progn (ignore-errors (down-list))
           (point))))

A weird thing occurred when this is re-written as a function that accepts another function as argument.

(defun next-pair-exists-in (func)
  "Check if inner or outer pair exists. 
Function employs `(up-list)` or `(down-list)` as argument to work."
  (not (eql (point)
        (progn (ignore-errors (func))
           (point)))))

While the eql point test code returns t for positions to the left of 1, 2 and 3, the function, while moving the cursor inwards as expected, returns nil for all positions.

(1(2(3)4)5)

How do we get the function to work?

3

Each symbol has a value cell which you can address with symbol-value and a function cell which you can address with symbol-function.

The func in the defun for next-pair-exists-in is the name of the local variable whose value is bound to the first argument in a function call of next-pair-exists-in. The variable func does not have any binding for its function cell. In (ignore-errors (func)) you try to use the function binding of func which is undefined.

The ignore-errors catches the error thrown because of the unbound function definition of fun.

You can evaluate the function stored in the value cell of func by (funcall func).

Your code should look like:

(defun next-pair-exists-in (func)
  "Check if inner or outer pair exists. 
Function employs `#'up-list` or `#'down-list` as argument to work."
  (not (eql (point)
            (progn (ignore-errors (funcall func))
                   (point)))))

Two examples for calling next-pair-exists-in are
(next-pair-exists-in #'down-list) and (next-pair-exists-in #'up-list).

| improve this answer | |
0

Further to my comment on your OP, see here:-

 (defun temp()
   (message "temp called")
   "done")

 (defun ese-55443-2 (func)
   "Check if inner or outer pair exists. 
 Function employs `(up-list)` or `(down-list)` as argument to work."
   (not (eql (point)
         (progn (ignore-errors (funcall func))
            (point)))))
   (ese-55443-2 'up-list)
   ;;(funcall 'temp)
   ;;(ese-55443-2 'temp)

Notice the use of funcall. The (f) syntax is when f is actually a lambda (I think thats the terminology). When it's a symbol you need to specifically use (funcall f).

| improve this answer | |
  • 2
    No, f in (f) is a symbol and f in f too. The actual difference is that when you evaluate the form (f) the function cell of the symbol f is looked up while when you evaluate f the value cell of f is looked up. Many minor modes use both cells. As an example you can see the difference between the contents of a symbols function cell and a symbols value cell with (symbol-function 'indent-tabs-mode) and (symbol-value 'indent-tabs-mode). The contents of a function cell might be a lambda that is going to be evaluated. It may also be a key sequence or an function alias. – Tobias Feb 11 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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