fxbois / web-mode

web template editing mode for emacs
https://web-mode.org
GNU General Public License v3.0
1.64k stars 260 forks source link

Custom variables marked as :safe are considered unsafe when loading file local variables on startup #1302

Closed sfllaw closed 5 months ago

sfllaw commented 6 months ago

I have an HTML file with the following file local variables:

<!-- Local Variables:
     web-mode-code-indent-offset: 1
     End: -->

If web-mode is already required, visiting this file is just fine. However, if web-mode is auto-loaded, then Emacs thinks this setting is unsafe, despite it being marked as :safe #'integerp: https://github.com/fxbois/web-mode/blob/a9d21841224da3295f2dd0a90022f5e435e48046/web-mode.el#L105-L110

According to Emacs Lisp: File Local Variables:

However, a safety predicate defined using :safe will only be known once the package that contains the defcustom is loaded, which is often too late. As an alternative, you can use the autoload cookie (see Autoload) to assign the option its safety predicate, like this:

;;;###autoload (put 'var 'safe-local-variable 'pred)

So it seems to me that the definition for web-mode-code-indent-offset should be revised to be:

(defcustom web-mode-code-indent-offset
  (if (and (boundp 'standard-indent) standard-indent) standard-indent 2)
  "Code (javascript, php, etc.) indentation level."
  :type 'integer
  :group 'web-mode)
;;;###autoload
(put 'web-mode-code-indent-offset 'safe-local-variable #'integerp)

If you would like, I can prepare a patch. Please let me know how you’d like me to contribute.

sfllaw commented 5 months ago

Thanks, @fxbois!