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

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

Re: use Elisp to improve your Elisp - some code issues


From: Pascal J. Bourguignon
Subject: Re: use Elisp to improve your Elisp - some code issues
Date: Sat, 01 Aug 2015 10:54:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Emanuel Berg <address@hidden> writes:

>   ;; find the construct (if a a b) if you want to replace it with (or a b)
>   ;; if it works, when applied to this file, it should find the example above!
>   (search-regexp-in-files (buffer-file-name)
>    
> "([[:space:]\n]*if[[:space:]\n]+\\(.*\\)[[:space:]\n]+\\1[[:space:]\n]+\\(.*\\))"

Perhaps you have notied some discrepancy here.
Check: https://en.wikipedia.org/wiki/Chomsky_hierarchy

You may use read, or forward-sexp / backward-sexp and down-lisp /
up-list, to walk the sexps of the buffer in such a way that now you can
check for source patterns instead of text patterns.

Not that in this case you may want to use it, but it might still be
better to write:

    (map-sexp-in-files
       (lambda (form)
         (cond
           ((atom form) form)
           ((and (eql (first form) 'if)
                 (eql (second form) (third form))) ; eql ensures we don't
                 ;;  substitute expressions that may have side effects.
             `(or ,(second form) ,@(cdddr form)))
           (t form)))
       (list  (buffer-file-name)))

or:

    (require 'pjb-pmatch)
    (map-sexp-in-files
       (lambda (form)
         (match-case form
            ((if (!v a) (!v a) (!! (!x e)))
              `(or ,a ,e))
            (otherwise form)))
        (list (buffer-file-name)))


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

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