help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Need some education on defmacro define-toggle


From: Lennart Borgman
Subject: Re: Need some education on defmacro define-toggle
Date: Mon, 28 Dec 2009 20:28:23 +0100

On Mon, Dec 28, 2009 at 6:16 AM, Lennart Borgman
<lennart.borgman@gmail.com> wrote:
> I just got into some trouble because of a badly written macro I
> believe. After the long discussions about defmacro here I wonder if
> anyone wants to help me out. How should I write the defmacro below?


I resorted to self education (which is not too bad). However I still
got problems - just on a more complicated level. The macro now looks
like this:

(defmacro define-toggle (symbol value doc &rest args)
  "Declare SYMBOL as a customizable variable with a toggle function."
  (declare   (doc-string 3)   (debug t))
  (let* ((SYMBOL-toggle (intern (concat (symbol-name symbol) "-toggle")))
         (SYMBOL-name (symbol-name symbol))
         (var-doc doc)
         (fun-doc (concat "Toggles the \(boolean) value of `" SYMBOL-name
                          "'.\nFor how to set it permanently see this
variable.\n")))
    (let ((var (append `(defcustom ,symbol ,value ,var-doc)
                args
                nil))
          (fun `(defun ,SYMBOL-toggle ()
                   ,fun-doc
                   (interactive)
                   (customize-set-variable (quote ,symbol) (not ,symbol)))))
      `(list 'progn ,var ,fun))))

This seems to work, but if the call to define-toggle already has been
done then loading a file with this define-toggle gives trouble:

Debugger entered--Lisp error: (void-variable
rngalt-display-validation-header-toggle)
  (progn rngalt-display-validation-header
rngalt-display-validation-header-toggle)
  (define-toggle rngalt-display-validation-header t "Display XML
validation headers at the top of buffer when t.\nThe validation header
is only displayed in buffers where the main\nmajor mode is derived
from `nxml-mode'." :set (lambda (sym val) (set-default sym val)
(rngalt-update-validation-header-overlay-everywhere)) :group (quote
relax-ng) :group (quote nxhtml))
  eval-buffer(#<buffer  *load*<3>> nil
"c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/rngalt.el" nil t)  ; Reading
at buffer position 24737
  load-with-code-conversion("c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/rngalt.el"
"c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/rngalt.el" t t)
  require(rngalt nil t)
  (progn (require (quote rngalt) nil t))
  (eval-and-compile (require (quote rngalt) nil t))
  eval-buffer(#<buffer  *load*<2>> nil
"c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-mode.el" nil t)  ;
Reading at buffer position 2959
  
load-with-code-conversion("c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-mode.el"
"c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-mode.el" t t)
  require(nxhtml-mode nil t)
  (progn (require (quote nxhtml-mode) nil t))
  (if (fboundp (quote nxml-mode)) (progn (require ... nil t)))
  (when (fboundp (quote nxml-mode)) (require (quote nxhtml-mode) nil t))
  (progn (when (fboundp ...) (require ... nil t)))
  (eval-when-compile (when (fboundp ...) (require ... nil t)))
  eval-buffer(#<buffer  *load*> nil
"c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-menu.el" nil t)  ;
Reading at buffer position 3227
  
load-with-code-conversion("c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-menu.el"
"c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-menu.el" nil nil)
  load("c:/emacs/p/091204/EmacsW32/nxhtml/nxhtml/nxhtml-menu.el")
  (let* ((vcs ...) (base-url ...) (rel-url ...) (base-dir ...)
(rel-url-el ...) file-url dl-file) (unless (stringp base-url) (setq
base-url ...)) (unless (stringp base-dir) (setq base-dir ...)) (setq
dl-file (expand-file-name rel-url-el base-dir)) (message "dl-file=%s"
dl-file) (unless (file-exists-p dl-file)
(web-vcs-get-missing-matching-files vcs base-url base-dir rel-url-el)
(unless ... ...)) (load dl-file) (unless (symbol-function ...) (setq
err ...)))
  (if (not (listp ...)) (let (...) (load ...) (unless ... ...)) (let*
(... ... ... ... ... file-url dl-file) (unless ... ...) (unless ...
...) (setq dl-file ...) (message "dl-file=%s" dl-file) (unless ... ...
...) (load dl-file) (unless ... ...)))
  (let* ((lib-web ...) (old-hist-elt ...) (auto-fun ...) err) (fset
(quote nxhtml-global-minor-mode) nil) (if (not ...) (let ... ... ...)
(let* ... ... ... ... ... ... ... ...)) (if (not err) (progn ... ...)
(fset ... auto-fun) (error "web-autoload: %s" err)))
  nxhtml-global-minor-mode(1)
  (let* ((util-dir ...) (related-dir ...) (nxhtml-dir ...)
(company-dir ...) (tests-dir ...)) (add-to-list (quote load-path)
nxhtml-dir) (add-to-list (quote load-path) related-dir) (add-to-list
(quote load-path) util-dir) (add-to-list (quote load-path)
nxhtml-install-dir) (add-to-list (quote load-path) company-dir)
(add-to-list (quote load-path) tests-dir) (setq
web-autoload-default-filename-element nxhtml-install-dir) (message
"... nXhtml loading %.1f seconds elapsed ..." (- ...
nxhtml-load-time-start)) (load (expand-file-name "nxhtml-loaddefs"
nxhtml-install-dir)) (message "... nXhtml loading %.1f seconds elapsed
..." (- ... nxhtml-load-time-start)) (message "Turn on
`nxhtml-global-minor-mode' unconditionally") (nxhtml-global-minor-mode
1) (message "... nXhtml loading %.1f seconds elapsed ..." (- ...
nxhtml-load-time-start)) (when (fboundp ...) (load ...)
(rncpp-patch-xhtml-loader)) (message "... nXhtml loading %.1f seconds
elapsed ..." (- ... nxhtml-load-time-start)) (load (expand-file-name
"nxhtml/nxhtml-autoload" nxhtml-install-dir)))
  (if nil nil (provide (quote nxhtml-autostart)) (if (<
emacs-major-version 23) (load ...) (let ... ...)) (let* (... ... ...
... ...) (add-to-list ... nxhtml-dir) (add-to-list ... related-dir)
(add-to-list ... util-dir) (add-to-list ... nxhtml-install-dir)
(add-to-list ... company-dir) (add-to-list ... tests-dir) (setq
web-autoload-default-filename-element nxhtml-install-dir) (message
"... nXhtml loading %.1f seconds elapsed ..." ...) (load ...) (message
"... nXhtml loading %.1f seconds elapsed ..." ...) (message "Turn on
`nxhtml-global-minor-mode' unconditionally") (nxhtml-global-minor-mode
1) (message "... nXhtml loading %.1f seconds elapsed ..." ...) (when
... ... ...) (message "... nXhtml loading %.1f seconds elapsed ..."
...) (load ...)) (message "... nXhtml loading %.1f seconds elapsed
..." (- ... nxhtml-load-time-start)) (when nxhtml-flymake-setup
(flymake-js-load) (flymake-css-load) (flymake-java-1-load) (add-hook
... ...)) (nxhtml-list-loaded-features nil) (message "Nxml/Nxhtml
Autostart.el loaded in %.1f seconds" (- ... nxhtml-load-time-start)))
  (unless nil (provide (quote nxhtml-autostart)) (if (<
emacs-major-version 23) (load ...) (let ... ...)) (let* (... ... ...
... ...) (add-to-list ... nxhtml-dir) (add-to-list ... related-dir)
(add-to-list ... util-dir) (add-to-list ... nxhtml-install-dir)
(add-to-list ... company-dir) (add-to-list ... tests-dir) (setq
web-autoload-default-filename-element nxhtml-install-dir) (message
"... nXhtml loading %.1f seconds elapsed ..." ...) (load ...) (message
"... nXhtml loading %.1f seconds elapsed ..." ...) (message "Turn on
`nxhtml-global-minor-mode' unconditionally") (nxhtml-global-minor-mode
1) (message "... nXhtml loading %.1f seconds elapsed ..." ...) (when
... ... ...) (message "... nXhtml loading %.1f seconds elapsed ..."
...) (load ...)) (message "... nXhtml loading %.1f seconds elapsed
..." (- ... nxhtml-load-time-start)) (when nxhtml-flymake-setup
(flymake-js-load) (flymake-css-load) (flymake-java-1-load) (add-hook
... ...)) (nxhtml-list-loaded-features nil) (message "Nxml/Nxhtml
Autostart.el loaded in %.1f seconds" (- ... nxhtml-load-time-start)))
  eval-buffer()  ; Reading at buffer position 8253
  call-interactively(eval-buffer nil nil)


There is obviously something I got wrong in the evaluation order since
now, in this situation, the macro now tries to evaluate

  (progn rngalt-display-validation-header
rngalt-display-validation-header-toggle)

I expected it to redefine the defcustom and the defun instead.
Can someone please try to explain what is going on?




reply via email to

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