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

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

Re: How to tame compiler?


From: Jean Louis
Subject: Re: How to tame compiler?
Date: Sun, 2 May 2021 14:18:23 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* tomas@tuxteam.de <tomas@tuxteam.de> [2021-05-02 12:08]:
> On Sun, May 02, 2021 at 10:45:10AM +0300, Jean Louis wrote:
> > * Michael Heerdegen <michael_heerdegen@web.de> [2021-05-02 08:43]:
> > > Stefan Monnier <monnier@iro.umontreal.ca> writes:
> > > 
> > > > I know I sound like a broken clock, but [...]
> > > 
> > > I think you would sound more like an unbroken clock if you would shortly
> > > tell the disadvantages of using `eval', then people would be more open
> > > to alternatives.
> > 
> > If possible, let me know one bad example in my context of expanding
> > plain text with embedded Emacs Lisp.
> 
> I tried to offer an explanation which you chose to ignore [1] . I
> think the problem is more subtle, and thus difficult to explain in
> just a few words.

As I am asking questions it is demonstration that I am not ignoring
it. In background I am running business, so the templating engine does
help me, it works, I have sent now quite number of messages to
people. But in foreground, here, I am interested for any kinds of
suggestions.

It is not the option to change text to sex-pressions, as that would
cause way too much work and minimize collaborative capacity, as that
demands teaching people not only how to write text, but how to write
kind of Lisp.

Purpose of it is to interpolate values into text, to pre-process text
or templates. Usually both are pre-processed, the text first, then
text and variables are interpolated into the template. It could be for
letters, it could be like for LaTeX or other markup, mostly HTML,
emails and SMS message.

Related keywords are "web template system" and "templating engines":

https://en.wikipedia.org/wiki/Web_template_system

https://en.wikipedia.org/wiki/Comparison_of_web_template_engines

It is by the same principle as:

- Org Babel, which uses `eval'

- Emacs Muse.

Both are used for publishing.

> > Like how it can go wrong that in same time cannot go wrong in same
> > replacement method?
> > 
> > And which replacement method could I use?
> > 
> > Like why did Lisp authors the `read-from-string' function if it should
> > not be used? It is quite common that its result will be given to `eval'.
> 
> It's not as clear-cut as "should not be used". Rather something along
> the lines of "should be used with care" [2].

It may be evil, just that I don't find practical evilness, but I would
like one simple example how it can be evil.

IMHO, functions are here, if you see something wrong with it, let me
know. It is in a package that does not have lexical-binding t, and it
is one particular case where it should stay so, as I wish to
interpolate any variables.

One problem I had is with (match-beginning 0) and (match-end
(match-end 0)) as with some functions it would not work, maybe because
`eval' was using it deeply somewhere.

That is why I have captured the values in `let':

(let* ((match-beginning (match-beginning 0))
       (match-end (match-end 0))

as otherwise it would not be able to:
(delete-region (match-beginning 0) (match-end 0))
that is why this works:
(delete-region match-beginning match-end) with captured values.

If you see something else, let me know.

(defcustom rcd-template-delimiter-open "⟦"
  "The opening delimiter for RCD Template Interpolation System."
  :group 'rcd
  :type 'string)

(defcustom rcd-template-delimiter-close "⟧"
  "The closing delimiter for RCD Template Interpolation System."
  :group 'rcd
  :type 'string)

(defun rcd-template-eval (string &optional delimiters)
  "Evaluates Emacs Lisp enclosed by `rcd-template-delimiter-open' and 
`rcd-template-delimiter-close'.

Optional DELIMITERS list may be provided to change default
delimiters, first list member has to be the opening delimiter and
second the closing delimiter.

Space or new line has to follow `rcd-template-delimiter-open' and
precede `rcd-template-delimiter-close' for evaluation to get
invoked."
  (let* ((delimiters (or delimiters (list rcd-template-delimiter-open 
rcd-template-delimiter-close)))
         (open (car delimiters))
         (close (cadr delimiters))
         (add (length open))
         (minus (length close)))
    (with-temp-buffer
      (insert string)
      (goto-char 0)
      (while (re-search-forward (rx (literal open)
                                    (one-or-more (or blank "\n"))
                                    (group (minimal-match (one-or-more 
anything)))
                                    (one-or-more (or blank "\n"))
                                    (literal close))
                                nil t)
        (let* ((match-beginning (match-beginning 0))
               (match-end (match-end 0))
               (lisp (condition-case nil
                         (read-from-string
                          (buffer-substring-no-properties
                           (+ add match-beginning) (- match-end minus)))
                       (error "")))
               (lisp (if (listp lisp) (car lisp) lisp))
               (value (condition-case nil
                          (eval lisp)
                        (error "")))
               (value (cond ((null value) "")
                            (t (format "%s" value)))))
          (delete-region match-beginning match-end)
          (insert (format "%s" value))))
      (buffer-string))))

(defun rcd-template-buffer-preview ()
  (interactive)
  (let* ((current (current-buffer))
         (buffer (format "Preview of buffer %s" current))
         (string (buffer-string))
         (mode major-mode)
         (point (point)))
    (pop-to-buffer-same-window buffer)
    (insert (rcd-template-eval string))
    (goto-char point)
    (funcall mode)
    (message buffer)))

(defun rcd-template-insert-delimiters ()
  "Quickly insert the template evaluation snippet such as ⟦ () ⟧
and move cursor into parenthesis."
  (interactive)
  (insert (format "%s () %s" rcd-template-delimiter-open 
rcd-template-delimiter-close))
  (backward-char (+ 2 (length rcd-template-delimiter-close))))

> Template expansion is one of those borderline cases, but if you look
> at all those modern template expanders out there [3], you'll realise
> that they all have some kind of "custom evaluator", where you
> explicitly provide an environment, instead of just punting to `eval'
> and saying "use... uh, whatever".

For string expansion they will provide environment and "manually"
replace some words into provided alist for example. CL-EMB in Common
Lisp uses `read-from-string'.

So far I see the use of it and I used it years before, just with
Common Lisp.

Here is one example page with its source that uses `rcd-template-eval':

https://hyperscope.link/3/7/1/3/6/Emacs-Lisp-interpolation-of-embedded-PostgreSQL-queries-within-plain-text.html

Purpose of it is to give liberation to HTML, SMS, email templates and
make static HTML pages kind of dynamic, as some dynamically generated
data can be expanded before publishing HTML pages.

I understand so far:

- there are opposing arguments to `eval', but...

- there is no replacement for `eval' in the context of templates with
  programming code; or other contexts;

- `eval' is already heavily used in Org mode and Muse, so many other
  large number packages;

Those contradictions are confusing.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/




reply via email to

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