1

Emacs Lisp setf macro allows you to set generalized variables. You can, for example change the size of the current window with the following form:

(setf (window-height) 10)

The macro will translate the above code to:

(progn                                                                                           
  (enlarge-window                                                                                
   (- 10                                                                                         
      (window-height)))                                                                          
  10)

My question: Is there a way to find out dynamically whether an Emacs function is "setf-able" like window-height is?

I searched in the GNU EMacs Common Lisp Emulation manual and could not find and answer to my question.

Drew
  • 75,699
  • 9
  • 109
  • 225
PRouleau
  • 744
  • 3
  • 10
  • 2
    They are [(elisp) Generalized Variables](https://www.gnu.org/software/emacs/manual/html_node/elisp/Generalized-Variables.html) which seems have become a core part of Emacs Lisp language, you can find infomation there, such as list of predefined setf-able forms, you can also know if it's setf-able by M-x emacs-lisp-macroexpand, besides (get 'window-height 'gv-expander) returns a function if it's setf-able. – xuchunyang Dec 05 '20 at 09:30
  • @xuchunyang: Consider posting that as an answer. – Drew Dec 05 '20 at 16:24
  • @xuchunyang. Using (get 'window-height 'gv-expander) is the answer I was looking for. Note that the list of functions in the Setf Extensions section of the Emacs Common Lisp Extensions and the one in the section "The set Maco" in the Elisp Manual differ. I wanted to be able to detect them via code. Thanks! I agree with Drew, you should post it as an answer. – PRouleau Dec 05 '20 at 16:45
  • @Drew and PRouleau Done. – xuchunyang Dec 05 '20 at 18:46

1 Answers1

4

Check the value of symbol's gv-expander property, for "setf-able" functions, the value is non-nil (and a function), e.g.,

(functionp (get 'window-height 'gv-expander))
;; => t

for other functions, the value is nil, e.g.,

(get 'length 'gv-expander)
;; => nil
xuchunyang
  • 14,302
  • 1
  • 18
  • 39