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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Sati
  • 775
  • 6
  • 21
  • MIght I suggest you include the code that calls the function, just to make it easier for others to try out? Also, you probably mean 'up-list or 'down-list and not '(up-list) etc? – RichieHH Feb 11 '20 at 13:29
  • Does this answer your question? [practical relevance of storing value and function into one symbol](https://emacs.stackexchange.com/questions/33950/practical-relevance-of-storing-value-and-function-into-one-symbol) – Drew Feb 11 '20 at 15:55
  • 1
    There is maybe a better-match question for which this is a duplicate. Maybe someone could search better or perhaps create a community question for this. Other candidates as duplicate: https://emacs.stackexchange.com/q/32544/105, https://emacs.stackexchange.com/q/53915/105. – Drew Feb 11 '20 at 15:57
  • Reviewers and answerers: Now that this site has many Q&A, it's important to search for duplicates before answering. Yeah, that takes more time... Fixing tags can maybe help some. And we should probably create some community questions, to factor out the important, common part from the more extraneous, immediate-need part. – Drew Feb 11 '20 at 15:59
  • Does this answer your question? [function that takes a function as argument and returns a new function](https://emacs.stackexchange.com/questions/32544/function-that-takes-a-function-as-argument-and-returns-a-new-function) – Stefan Feb 11 '20 at 23:11

2 Answers2

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).

Tobias
  • 32,569
  • 1
  • 34
  • 75
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).

RichieHH
  • 848
  • 4
  • 9
  • 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 '20 at 14:18