I tried my hand at writing an elisp function today, all works well except for an error that keeps getting thrown when the function returns nil. Here's the function: (Please excuse the formatting, I'm not really sure how to indent this yet.)
(defun byte-is-readable (byte)
"Determine whether BYTE is readable or not.
Returns t or nil."
(if (and (<= byte 126) (>= byte 32)) t nil))
;; Read the 100 bytes of a file
;; If 10 in a row are not 'readable', it is safe to assume the file is a binary
(defun is-file-binary (file)
"Determine whether FILE is a binary file.
Returns t or nil."
(let* ((i 1)
(c 0)
(size (nth 7 (file-attributes file)))
(lim (if (< size 100) size 100)))
(while (< i lim)
(let ((char (with-temp-buffer
(insert-file-contents file)
(buffer-substring i (+ i 1)))))
(if (not (byte-is-readable (aref char 0)))
(setq c (+ c 1))
(setq c 0))
(if (= c 10) (return t)))
(setq i (+ i 1))))
nil)
Calling it like this: (message "%s" (if (is-file-binary "/path/to/some/file") "true" "false")
works file when returning true, but throws "if: No catch for tag: --cl-block-nil--, t" when returning nil. I'm guessing this is because nil isn't evaluated correctly or something.
How do I fix it?