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

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

Example use of macro to minimize and generalize the code


From: Jean Louis
Subject: Example use of macro to minimize and generalize the code
Date: Tue, 1 Jun 2021 00:40:02 +0300
User-agent: Mutt/2.0.7+183 (3d24855) (2021-05-28)

* Emanuel Berg via Users list for the GNU Emacs text editor 
<help-gnu-emacs@gnu.org> [2021-05-31 22:21]:
> What kind of things or problems could or should you solve
> with macros?

Example use of macro:

I have noticed that I am often repeating some structures, really
boring, something like:

(defun my-fun ()
  (interactive)
  (let ((id (tabulated-list-get-id)))
    (if (and id (string= rcd-current-table "hyobjects"))
        (do-more)
      (warn something))))

instead:

(defmacro when-tabulated-id (table id &rest body)
  (declare (indent 2) (debug t))
  (ignore id)
  `(if id
       (progn
         ,@body)
     (if (or (eq ,table 'any)
             (string-equal ,table rcd-current-table))
         (let ((id (tabulated-list-get-id)))
           (if id
               (progn ,@body)
             (message "Did not get ID")))
       (message "This function is for table `%s' only" ,table))))

solves the problem and I get less code:

(defun my-fun (&optional id)
  (interactive)
  (when-tabulated-id "hyobjects" id
    (do-something)))

Imagine having 300+ functions where each function should maybe
report something in case of error, like you can see in the
macro. In this case I am minimizing code by using macro.


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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