4

I have an installed packages (say mypackage.el) in \.emacs.d\lisp\ directory. In that package there is

(defcustom xyz "path1" "Path to the program"
  :group 'blablabla
  :type 'string)

I would like to change the above as

(defcustom xyz "path2" "Path to the program"
  :group 'blablabla
  :type 'string)

I prefer to do this modification not in the package el file but in my init file. However adding the above in the init file has no effect and for the package the variable xyz is always path1.

Name
  • 7,689
  • 4
  • 38
  • 84

1 Answers1

5

defcustom (and defvar) are only for the library that defines the variable. You don't want to copy those.

As a user, you can either use the customize interface:

M-x customize-option RET xyz RET

or else you can use setq to set the value in your init file:

(setq xyz "path2")

As you'll have guessed from the names, the customize interface only works on defcustom variables (i.e. "user options").


However adding the above in the init file has no effect and for the package the variable xyz is always path1.

This is intentional. When a variable already has a value, and a defcustom or defvar for that variable is evaluated, the pre-existing value is kept. This means that you can (setq xyz "path2") in your init without needing to first load the library which defines the variable, because if you do subsequently load (or re-load) that library, the default value won't clobber your custom value.

phils
  • 48,657
  • 3
  • 76
  • 115
  • 1
    n.b. `setq` doesn't *necessarily* work with `defcustom` variables, as they might have a custom setter function which does more complex things with the value, and using `setq` would mean that the setter function was not invoked. It *will* work in most cases, but using the customize interface is the *guaranteed* safe option. – phils Sep 05 '18 at 01:37
  • And to do this from file, this is the command, right? `(custom-set-variables '(xyz "path2"))`? – Gauthier Feb 25 '22 at 21:28
  • 1
    I think `(customize-set-variable 'xyz "path2")` is preferred, with the caveat that (either way) your custom file will still be updated with that value the next time it is saved, and so there may be limited value to setting things outside of the customize UI (as you're then effectively doing it in two places). Emacs 29 will introduce a macro (probably named `setopt`) for doing this in code *without* touching your custom file. – phils Feb 25 '22 at 23:06
  • 1
    FYI `setopt` currently looks like this: https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/cus-edit.el?id=136f1cb54962f5dcae9e8b63e41e4df70995599c#n1047 – phils Feb 25 '22 at 23:18