all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Redefining functions and variables
Date: Thu, 29 Jul 2010 12:03:28 +0200	[thread overview]
Message-ID: <877hkeiozz.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: 08217e84-b4a0-4dd0-afbf-abd4d762c759@w30g2000yqw.googlegroups.com

Elena <egarrulo@gmail.com> writes:

> My modified macro is below. Why ',name is expanded as ... (as shown by
> `macroexpand-all'?
>
> (defvar old-defun 'defun)           ; the symbol!
> (unintern 'defun)
>
> (defmacro defun (name args &rest body)
>     `(progn
>          ;; `load-file-name' is not null only if we are loading a
> file.
>          (when (and load-file-name
>                     (fboundp ',name))
>              (message "Warning: %s is being redefined in %s."
>                       (symbol-name ',name)
>                       load-file-name)
>              (,old-defun ,name ,args
>                          ,@body))))

It is not.

(macroexpand '(defun. test (a) (+ 1 a)))
;; --> (progn (when (and load-file-name (fboundp (quote test))) (message "Warning: %s is being redefined in %s." (symbol-name (quote test)) load-file-name) (defun test (a) (+ 1 a))))

the "..." are only used to display the list when print-length or
eval-expression-print-length are not nil.  See also print-level and
eval-expression-print-level.


%s can format symbols too:

(format ">> %s <<" 'example)
;; --> ">> example <<"

And you had your the old defun inside the when!

(defmacro defun (name args &rest body)
  `(progn
     ;; `load-file-name' is not null only if we are loading a file.
     (when (and load-file-name (fboundp ',name))
       (message "Warning: %s is being redefined in %s." ',name load-file-name))
     (,old-defun ,name ,args  ,@body)))

Note: while name is known at macroexpansion time, you should refrain
to insert it in the string like this:

      (message ,(format "Warning: %s is being redefined in %%s." name) load-file-name)

since name could contain percents and then you'd have build a wrong
format string for message.   If you want to do that, you must escape
the percents:

      (message ,(format "Warning: %s is being redefined in %%s." 
                        (escape-percent name))
               load-file-name)

with:

(defun escape-percent (string-designator)
    (let ((string (etypecase string-designator
                     (string string-designator) 
                     (symbol (symbol-name string-designator))
                     (character (string string-designator)))))
      (unsplit-string (split-string string "%" nil) "%%")))

(defun unsplit-string (string-list &rest separator)
  "Does the inverse than split-string. If no separator is provided 
then a simple space is used."
  (if (null separator)
      (setq separator " ")
      (if (= 1 (length separator))
          (setq separator (car separator))
          (error "unsplit-string: Too many separator arguments.")))
  (if (not (char-or-string-p separator))
      (error "unsplit-string: separator must be a string or a char."))
  (apply 'concat (list-insert-separator string-list separator)))

(mapcar 'escape-percent '("abc" abc % %make- make-10%-of-profit %%internal%%))
;; --> ("abc" "abc" "%%" "%%make-" "make-10%%-of-profit" "%%%%internal%%%%")


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


  parent reply	other threads:[~2010-07-29 10:03 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-27 15:12 Redefining functions and variables Elena
2010-07-27 20:21 ` Stefan Monnier
2010-07-27 22:16   ` Elena
2010-07-28  0:35     ` Andreas Politz
2010-07-28  7:51       ` Elena
2010-07-28 14:29         ` Elena
2010-07-28 18:48           ` Andreas Politz
2010-07-28 20:37             ` Pascal J. Bourguignon
2010-07-29  8:32               ` Elena
2010-07-29  9:57                 ` Stefan Monnier
2010-07-29 10:16                   ` Elena
2010-07-29 13:14                     ` Stefan Monnier
2010-07-30 19:32                     ` Uday S Reddy
2010-07-29 10:31                   ` Andreas Politz
2010-07-29 11:46                     ` Johan Bockgård
2010-07-29 15:54                       ` Andreas Politz
2010-07-29 12:41                   ` Tim X
2010-07-29 10:03                 ` Pascal J. Bourguignon [this message]
2010-07-29 10:28                   ` Elena
2010-07-29 20:35                 ` Johan Bockgård
2010-07-29 22:22                   ` Tim X

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

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

  git send-email \
    --in-reply-to=877hkeiozz.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.