Skip to content

Instantly share code, notes, and snippets.

@mplscorwin
Created December 20, 2019 04:40
Show Gist options
  • Save mplscorwin/89d91ed7117e29a54e7413b937c1ae4b to your computer and use it in GitHub Desktop.
Save mplscorwin/89d91ed7117e29a54e7413b937c1ae4b to your computer and use it in GitHub Desktop.
A small utility to make developing well documented vars inline a little more trival.
;;; defvar-maybe.el --- maybe declare variables
;;; AUTHOR: Corwin Brust <corwin@bru.st>
;;; LICENSE: GPL2 or newer GNU Public License
;;; VERSION: 0.2
;;; Commentary:
;;
;; A drop-in replacement for `defvar' that substities 'setq' during "DEVEL".
;; Place the following lines into a package you are hacking on:
;; (require 'defvar-maybe)
;; (setq defvar-maybe t) ;; nil=setq, t=defvar, 'custom=defcustom
;; (defvar-maybe 'my-var "val" "is documented" :group 'mine :type 'string)
;;
;; Then use `setvar-maybe' just as you would `setvar'. To clean things up
;; (if you don't want provide `defvar-local' along with your work):
;; 1. remove above `require' and `setq' statments
;; 2. M-% (M-x query-replace RET) defvar-maybe RET defvar RET !
;;
;; Version 0.2: var is renamed for the pkg and func; 'custom for defcustom
;;; Code:
(defvar-local defvar-maybe nil
"Control func: `defvar-maybe', which see for detail.")
(defmacro defvar-maybe (var-name &optional i-value d-string &rest customize-options)
"Create or set a variable per the buffer-local var: `defvar-maybe'.
Use `setq' instead of `defvar' when var: `defvar-maybe' is not truthy.
When var: `defvar-maybe' is `custom use `defcustom'.
VAR-NAME is the symbol to declare or set. I-VALUE is either INITVALUE for
`defvar' or VAL for `setq'. D-STRING is DOCSTRING when `defvar' is used.
This is useful when using `eval-buffer' to run test cases hacked directly into
a package's source files."
(declare (indent 2) (doc-string 3))
;;; (message "devel:%s (%s)" defvar-maybe (and (boundp #'defvar-maybe) defvar-maybe))
(if (and (boundp #'defvar-maybe) defvar-maybe)
(if (eq 'custom defvar-maybe)
`(defcustom ,var-name ,i-value ,d-string ,@customize-options)
`(setq ,var-name ,i-value))
`(defvar ,var-name ,i-value ,d-string)))
(provide 'defvar-maybe)
;;; defvar-maybe.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment