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

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

Re: How to put this in a macro


From: Pascal J. Bourguignon
Subject: Re: How to put this in a macro
Date: Tue, 04 May 2010 15:45:09 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (darwin)

Cecil Westerhof <Cecil@decebal.nl> writes:

> In my code I use the following code on several places:
>     (if (equal start end)
>         (setq start (point-min)
>               end   (point-max))
>       (setq start (or start (point-min)))
>       (setq end   (or end   (point-max))))
>
> I think it would be good to put this in a macro. How would I write a
> macro for this code?
>
> Or could it be done with a function?

Yes.  


;; In a functional style:

(require 'cl)

(defun normalize-start-end (start end)
   ;; In addition, we will sort the bounds:
   (cond 
     ((null start)  (values (point-min) (or end (point-max))))
     ((null end)    (values start (point-max)))
     ((= start end) (values (point-min) (point-max)))
     ((< start end) (values start end))
     (t             (values end   start))))    

(defun cmd (start end)
  (interactive "r")
  (multiple-value-bind (start end) (normalize-start-end start end)
     ...))


;; In emacs lisp, we can also set the symbols:

(defun set-normalized-start-end (start-sym end-sym)
   (multiple-value-bind (start end) (normalize-start-end    
                                       (symbol-value start-sym)
                                       (symbol-value end-sym))
       (setf (symbol-value start-sym) start
             (symbol-value end-sym)   end)))

(defun cmd (start end)
   (interactive "r")
   (set-normalized-start-end 'start 'end)
   ...)


I would write the macro as:

(defmacro with-normalized-start-end ((start-var end-var) &body body)
  `(multiple-value-bind (,start-var ,end-var) 
                (normalize-start-end ,start-var ,end-var)
       ,@body))

(defun cmd (start end)
   (interactive "r")
   (with-normalized-start-end (start end)
       ...))


-- 
__Pascal Bourguignon__


reply via email to

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