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

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

Re: refactor emacs lisp code, macros, byte-compiling, fun fun fun


From: Le Wang
Subject: Re: refactor emacs lisp code, macros, byte-compiling, fun fun fun
Date: Sat, 24 Sep 2011 01:46:01 +0800

Drew's e-mail http://lists.gnu.org/archive/html/help-gnu-emacs/2011-09/msg00199.html got me to thinking about quoting, this does exactly what I want:

(macrolet ((doit ()
                 ''(with-current-buffer (car ad-return-value)
                    (when  (or (backup-file-name-p buffer-file-name)
                               (not (string-match (concat "\\`" (regexp-quote (expand-file-name "~")))
                                                  buffer-file-name)))
                      ;; (view-mode 1)
                      (setq buffer-read-only t)))))
  (eval `(defadvice find-function-search-for-symbol (after oxy-adv1 last (symbol type library) activate)
                       "mark system files `read-only'"
                       ,(doit)))

  (eval `(defadvice find-variable-noselect (after oxy-adv2 last (variable &optional file) activate)
                       "mark system files `read-only'"
                       ,(doit))))

On Wed, Sep 21, 2011 at 1:06 PM, Le Wang <l26wang@gmail.com> wrote:
I have this in an init file which gets compiled:

(defadvice find-function-search-for-symbol (after oxy-adv1 last (symbol type library) activate)
 "mark system files `read-only'"
 (with-current-buffer (car ad-return-value)
   (when  (or (backup-file-name-p buffer-file-name)
              (not (string-match (concat "\\`" (regexp-quote (expand-file-name "~")))
                                 buffer-file-name)))
     ;; (view-mode 1)
     (setq buffer-read-only t))))

(defadvice find-variable-noselect (after oxy-adv2 last (variable &optional file) activate)
 "mark system files `read-only'"
 (with-current-buffer (car ad-return-value)
   (when  (or (backup-file-name-p buffer-file-name)
              (not (string-match (concat "\\`" (regexp-quote (expand-file-name "~")))
                                 buffer-file-name)))
     (setq buffer-read-only t))))

I want to factor out the common logic in the defadvice, *without* polluting the global name-space.

I got to:

(labels ((doit ()
               (with-current-buffer (car ad-return-value)
                 (when  (or (backup-file-name-p buffer-file-name)
                            (not (string-match (concat "\\`" (regexp-quote (expand-file-name "~")))
                                               buffer-file-name)))
                   ;; (view-mode 1)
                   (setq buffer-read-only t)))))
  (defadvice find-function-search-for-symbol (after oxy-adv1 last (symbol type library) activate)
    "mark system files `read-only'"
    (doit))

  (defadvice find-variable-noselect (after oxy-adv2 last (variable &optional file) activate)
    "mark system files `read-only'"
    (do-it)))

Which is great.  I'm really liking this lisp thing now.  But is there a way to make `do-it' not appear in the byte-code at all, but its expansion does.  Like a macro

I've tried:

(macrolet ((doit ()
                 `(with-current-buffer (car ad-return-value)
                    (when  (or (backup-file-name-p buffer-file-name)
                               (not (string-match (concat "\\`" (regexp-quote (expand-file-name "~")))
                                                  buffer-file-name)))
                      (setq buffer-read-only t)))))
  (defadvice find-function-search-for-symbol (after oxy-adv1 last (symbol type library) activate)
    "mark system files `read-only'"
    (doit))

  (defadvice find-variable-noselect (after oxy-adv2 last (variable &optional file) activate)
    "mark system files `read-only'"
    (do-it)))

But macrolet does dynamic binding, so it's not what I want. And (do-it) still appears in the byte-code.  Is there something that does what I want?


--
Le




--
Le

reply via email to

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