Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

6 views
Skip to first unread message

Le Wang

unread,
Sep 21, 2011, 1:06:55 AM9/21/11
to GNU Emacs List
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

Stefan Monnier

unread,
Sep 21, 2011, 9:17:50 AM9/21/11
to
> (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))))

Visiting files to which you do not have write access should already put
them in read-only mode. Are you running Emacs as root by any chance?

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

You can prefix your global definitions with "lewang-" or something like
that (other people use "my-").

> 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?

Maybe something like:

(eval-when-compile
(defmacro blabla blibli))

...blabla...

will avoid including "blabla" in the byte-code, but I won't
guarantee it.


Stefan

Stefan Monnier

unread,
Sep 23, 2011, 8:53:58 AM9/23/11
to
>> (defadvice find-function-search-for-symbol (after oxy-adv1 last (symbol type
>> library) activate)
[...]
>> (defadvice find-variable-noselect (after oxy-adv2 last (variable &optional
>> file) activate)

BTW, any reason why you don't use find-file-hook instead?


Stefan

Le Wang

unread,
Sep 23, 2011, 1:46:01 PM9/23/11
to GNU Emacs List
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 <l26...@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
0 new messages