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

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

options and variables, interesting code


From: Emanuel Berg
Subject: options and variables, interesting code
Date: Wed, 09 Nov 2022 18:48:39 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Here is the idea from the other day ...

Note an interesting unintentional thing, this forces
options, i.e. global dynamic/special variables, to be defined
even before they are used (macro expand time? I have no
experience from macros, almost)

Or is that a side-effect of byte-compilation?

Anyway, it it were like this (maybe not exactly like this)
with 'vars' and 'opts' one could have the 'dlet', 'dalet',
'alet', 'salet', and 'slet' (see the other post) for
completeness for anyone who would like them for whatever
reason, the rest could just use 'vars' for local variables and
'opts' for options, one could enforce lexical/static scope,
remove the cookie, and one could forget about the scope - as
the dlet people would know it, anyway - and the rest wouldn't
care and they wouldn't need to as those (local variable and
options) are intuitive concepts by now ...

Or am I wrong?

Oh, that should be `let*' BTW ...

PS. Those macros are so similar, can you replace one with
    a hypermacro or metamacro? Higher-order macro
    programming LOL ...

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/vars-opts.el

(require 'cl-lib)

(defmacro vars (binders &rest body)
  (declare (indent 1) (debug let))
  (cl-loop
   with name-colls = ()
   for (n _) in binders
   do (when (special-variable-p n)
        (push (symbol-name n) name-colls) )
   finally (when name-colls
             (error "Name collision%s: %s"
                    (if (< 1 (length name-colls)) "s" "")
                    (mapconcat #'identity (reverse name-colls) ", ") )) )
  `(let ,binders ,@body) )

(defmacro opts (binders &rest body)
  (declare (indent 1) (debug let))
  (cl-loop
   with name-colls = ()
   for (n _) in binders
   do (unless (special-variable-p n)
        (push (symbol-name n) name-colls) )
   finally (when name-colls
             (error "No option%s: %s"
                    (if (< 1 (length name-colls)) "s" "")
                    (mapconcat #'identity (reverse name-colls) ", ") )) )
  `(let ,binders ,@body) )

(defvar dynavar 2000)

(vars ((dynavar 3000)
       (fill-column 1)
       (a 0)
       (y 2) )
  y) ; Name collisions: dynavar, fill-column

(vars ((fill-column 1)
       (a 0)
       (y 2) )
  y) ; Name collision: fill-column <-- cute B)

(vars ((a 0)
       (y 2) )
  y) ; 2

(opts ((dynavar 3000)
       (fill-column 1)
       (a 0)
       (y 2) )
  y) ; No options: a, y

(opts ((dynavar 3000)
       (fill-column 10) )
  (delete-char 2)
  (fill-paragraph) ) ; eval me

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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