1

I am using a mode that has the following construct:

(defun robot-indent()
  "Returns the string used in indation.
Set indent-tabs-mode to non-nil to use tabs in indentation. If indent-tabs-mode is 
set to nil c-basic-offset defines how many spaces are used for indentation. If c-basic-offset is
not set 4 spaces are used.
 "
 (if indent-tabs-mode
     "\t"
   (make-string (if (boundp 'c-basic-offset) 
                    c-basic-offset 4)
        ?\ )
   )
 )

The attempt to use c-basic-offset if it is defined (bound?) fails. Evaluating c-basic-offset return set-from-style instead of 4.

I've tried to replace it with (symbol-value c-basic-offset), but it didn't change anything.

I've read about style variables, but didn't get everything. I set c-basic-offset only in custom C style definitions in my init file.

Several questions:

  • How do I fix it?
  • Is this something that worked in older versions of emacs?

Emacs 24.5.1

Gauthier
  • 499
  • 2
  • 13
  • Keep in mind that an `if` statement might return `nil` (depending upon how it is written), and any function that uses that returned value must be able to utilize a `nil` value. So, the first thing I asked myself is what would happen if the first argument to `make-string` is `nil`? It is probably not an answer to your question, but is nevertheless something you should consider when this situation exists. – lawlist Nov 13 '17 at 15:15

1 Answers1

1

Re-reading about style variables, I assumed boundp was not doing what I expected. I guess this function was written before CC mode 5.26, and that it's what footnote 1 in the link above refers too.

Now, c-basic-offset is automatically set to set-from-style if not globally set.

I changed the function to this and got it working as I expect:

(defun robot-indent()
  "Returns the string used in indation.
Set indent-tabs-mode to non-nil to use tabs in indentation. If indent-tabs-mode is 
set to nil c-basic-offset defines how many spaces are used for indentation. If c-basic-offset is
not set 4 spaces are used.
 "
 (if indent-tabs-mode
     "\t"
   (make-string (if (eq c-basic-offset 'set-from-style)
                    4 c-basic-offset)
        ?\ )
   )
 )
Gauthier
  • 499
  • 2
  • 13
  • `(boundp 'c-basic-offset)` will return `nil` before `c-basic-offset` gets a value (typically by loading cc-mode). And trying to get the value of `c-basic-offset` (like you do in your function) will signal a `void-variable` error. – npostavs Nov 14 '17 at 13:01