1

Am using the following function, but getting the error

In bracemk-balance:
fencones.el:109:9:Warning: assignment to free variable
    ‘skeleton-pair’

What is wrong with this?

(defun bracemk-balance ()
  "Completing brace marks group."

  (setq skeleton-pair t)

  (global-set-key (kbd "(")  'skeleton-pair-insert-maybe)
  (global-set-key (kbd "[")  'skeleton-pair-insert-maybe)
  (global-set-key (kbd "{")  'skeleton-pair-insert-maybe)
  (global-set-key (kbd "<")  'skeleton-pair-insert-maybe)

  (global-set-key (kbd "`")  'skeleton-pair-insert-maybe)
  (global-set-key (kbd "'")  'skeleton-pair-insert-maybe)
  (global-set-key (kbd "\"") 'skeleton-pair-insert-maybe))
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

2

It's just a warning.

With your setq you're assigning a value to a global variable. Emacs is just letting you know this.

You didn't explicitly tell Emacs that skeleton-pair is a global variable. To do that, use (defvar skeleton-pair) (no initial value). Then you won't see the warning. That's the way to tell Emacs that a variable is "special" - scoped dynamically.

If you don't tell Emacs that, then Emacs tells you when you use such a variable -- in particular in case you weren't aware of it. (If you're aware of it then you should tell Emacs: (defvar skeleton-pair).)

Emacs is letting you know that within your function bracemk-balance variable skeleton-pair is a free variable - it's not bound by or within your function (not a function parameter and not bound with let, for example). It's not a lexically scoped variable; it's a dynamically scoped variable. It's not local to your function.

Emacs tells you this, for one thing, to let you know in case you misspelled a variable that is bound in your function. By misspelling it you can end up using a different, likely new, likely global variable.

See the Elisp manual, node Variable Scoping.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Is there a better way to set up completion for parentheses? Not quite so sure if I require `(setq skeleton-pair t)`. – Dilna Jul 12 '22 at 05:35