4

I am essentially trying to build a list of strings, where each of those strings in turn are built by their own functions.

However, I get the following error when the code is evaluated:

Wrong type argument: stringp, nil

Here is the code:

(defun mode-line-segment-git-status ()
  "Build the git status mode-line segment."
  (let (branch-name '(substring-no-properties vc-mode 5)))
  (list "Branch: " 'branch-name))


(defvar mode-line-segments nil)
(defun add-mode-line-segment (segment)
  "Add a modeline segment to the modeline."
  (setq mode-line-segments (append mode-line-segments (funcall segment))))

(add-mode-line-segment 'mode-line-segment-git-status)

(setq-default mode-line-format mode-line-segments)

What am I doing wrong?

Drew
  • 77,472
  • 10
  • 114
  • 243
tam5
  • 141
  • 1
  • 1
  • 4
  • Your let in your first function a) should enclosed your let-bound variable in its own set of parentheses, and b) should not quote the function whose value you want to bind to the variable. – Dan May 03 '18 at 15:21

1 Answers1

5

@Dan's comment is quite correct: you don't need to be quoting inside your let form, and also, you can't access let-bound variables outside the scope of the let form. (You might notice that Emacs lisp has a single global namespace for variables. let is how we encapsulate variables inside functions, keep them from leaking.)

More generally: the Wrong type argument is elisp's way of telling you, "I expected an x but got a y. stringp, nil means, "I expected something that satisfies the stringp predicate, but I received nil." You can get more information about where the error is happening by calling M-x toggle-debug-on-error, but "I'm handing a nil to something that needs a string` might be enough of a hint.

Gastove
  • 1,551
  • 9
  • 15
  • +1 for M-x toggle-debug-on-error. It's really handy when you have a way of reliably reproducing the error. – mindthief Nov 22 '20 at 03:36