unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* options and variables, interesting code
@ 2022-11-09 17:48 Emanuel Berg
  0 siblings, 0 replies; only message in thread
From: Emanuel Berg @ 2022-11-09 17:48 UTC (permalink / raw)
  To: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-09 17:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09 17:48 options and variables, interesting code Emanuel Berg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).