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

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

Re: Redefining functions and variables


From: Pascal J. Bourguignon
Subject: Re: Redefining functions and variables
Date: Wed, 08 Dec 2010 15:21:45 -0000
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.2 (gnu/linux)

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/


reply via email to

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