unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: options and variables, interesting code
Date: Wed, 09 Nov 2022 18:48:39 +0100	[thread overview]
Message-ID: <87pmdwvx1k.fsf@dataswamp.org> (raw)

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	other threads:[~2022-11-09 17:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pmdwvx1k.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).