Search code examples
elisp

Elisp: reading a constant


When reading the constant :hi, I get the error:

test: Wrong type argument: char-or-string-p, :hi

(defun test ()
   :hi "greet"
   (insert :hi)
 )

(test)

What does it mean? Why is "greet" not the output?


Solution

  • Your Error Message

    insert operates on strings or characters, but you gave it a symbol (:hi). Put your cursor on insert and hit C-h f RET (or do C-h f insert RET anywhere) and you will get the description of the function insert in buffer *Help*.

    Your Problem

    My ESP tells me that you think that :hi "greet" in the beginning of your code should bind variable :hi to value "greet" and then (insert :hi) should call insert on the value of :hi that should be "greet".

    This is not the case.

    1. :hi "greet" has no effect whatsoever.
    2. :hi is a keyword, it is always a constant that evaluates to itself and cannot be bound.
    3. To bind a local variable, use let, try C-h f let RET.

    Thus your code should be

    (def test()
      "my function to insert a greeting"
      (let ((hi "greet"))
        (insert hi)))
    

    Remedy

    I urge you to read "An Introduction to Programming in Emacs Lisp", you will not regret it. The best way to do it is in Emacs: C-h i m intro TAB RET.