[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Setting custom variable values from elisp

From: Lennart Borgman
Subject: Setting custom variable values from elisp
Date: Tue, 4 Jan 2005 17:20:50 +0100

Maybe I have overlooked something but I did not find any easy way to set
custom variables from elisp. I therefore wrote the three functions below
which I believe can be used for that.

Do they seem correct? Is this the way to divide it? Could they be part of
the custom package?

I use these functions in the setup for w32 to make it possible to

1) Set a bunch of values from a customize like page
2) Fetch values into Emacs

- Lennart

(defun setup-helper-set-as-custom(symbol)
  "Set SYMBOL the same way `custom-set-variables' should do."
  (let* ((value (car (get symbol 'saved-value)))
  (now (get symbol 'force-value))
  (requests (get symbol 'custom-requests))
  (comment (get symbol 'comment))
  (cuslist (list value symbol)))
    (when (or now requests comment) (setq cuslist (cons now cuslist)))
    (when (or requests comment) (setq cuslist (cons requests cuslist)))
    (when comment (setq cuslist (cons comment cuslist)))
    (setq cuslist (reverse cuslist))
    (custom-set-variables cuslist)
    ;;(message "%s" cuslist)

(defun setup-helper-add-custom(symbol value &optional now requests comment)
  "Sets the custom properties of SYMBOL.
For an explanation of VALUE, NOW, REQUESTS and COMMENT see
`custom-set-variables'.  If COMMENT is nil a comment is added by this

The current value of SYMBOL is not set. To set the value use

If you call `custom-save-all' SYMBOL will be saved to the
`custom-set-variables' entry."
  (put symbol 'saved-value (list value))
  (put symbol 'custom-requests (list requests))
  (put symbol 'force-value now)
  (unless comment (setq comment "Added by setup-helper"))
  (put symbol 'saved-variable-comment comment))

;; Use these lines to test:
;;(setup-helper-add-custom 'aaa "test" nil)
;;(setup-helper-set-as-custom 'aaa)
;;(defvar aaa nil)
;;(setup-helper-set-as-custom 'aaa)

;; This is riped from cus-edit.el customize-option, Emacs 21.3.1
(defun setup-helper-customizeable (symbol)
  "Return t if SYMBOL can be customized, nil otherwise."
  (interactive (custom-variable-prompt))
  ;; If we don't have SYMBOL's real definition loaded,
  ;; try to load it.
  (unless (get symbol 'custom-type)
    (let ((loaddefs-file (locate-library "loaddefs.el" t))
      ;; See if it is autoloaded from some library.
      (when loaddefs-file
   (insert-file-contents loaddefs-file)
   (when (re-search-forward (concat "^(defvar " (symbol-name symbol))
       nil t)
     (search-backward "\n;;; Generated autoloads from ")
     (goto-char (match-end 0))
     (setq file (buffer-substring (point)
      (progn (end-of-line) (point)))))))
      ;; If it is, load that library.
      (when file
 (when (string-match "\\.el\\'" file)
   (setq file (substring file 0 (match-beginning 0))))
 (load file))))
  (if (get symbol 'custom-type) t nil))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]