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

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

Re: Macros in dired - Is it possible?


From: Anselm Helbig
Subject: Re: Macros in dired - Is it possible?
Date: Sat, 18 Jul 2009 23:08:42 +0200

Hi!

> [ *Question* ]
> Is it possible to mark files in dired and then run a macro on all
> the selected files?
> 
> [ *Background* ]
> The macro finds and deletes certain portions/paragraphs from text
> files to cut them down in size so that I can read them easier and quicker.
> 
> But I am getting tired of loading one file at a time into Emacs to
> run the macro ( via ^u 0 MX macro1 ). I do this about 100x/day. So is there a
> quicker/better way to run a macro en mass?
> 
> [ *What I Have Tried, but Hasn't Worked* ]
> As an example
> 1. I load 10 text files into emacs, thus creating 10 separate
> buffers.
> 2. Then tried creating another macro that calls the first macro,
> and also saves the edited and much smaller file, and then kills the buffer
> 3.  So, for 10 files, I would call macro2 via [ ^u 10 Mx macro2 ]
> 4. The problem, I am having is that the 2nd macro doesn't record
> the save or the kill. So, I tried to edit the saved macro2 via [
> Mx edit-named-k TAB RET macro2 ]. I add the commands [ ^x ^s ]
> and [ ^x k RET ], and do a [ c ^c ]. The changes are saved, but
> when I try again, the 2nd macro calls macro1 to do its magic, but
> the modifications aren't saved, nor is the buffer killed. So,
> only one of the 10 files files has been modified.

I didn't do any experiments myself, but I guess what happens is this: 

  - You're executing your text-mangling macro in a buffer with a
    numeric prefix argument, a large number, larger than the number of
    times your macro actually needs to get called.

  - At the end of the buffer, your macro beeps (i.e., signals an
    error) and finishes.

  - When you record your second macro, the macro definition is aborted
    because your first macro beeps. This makes sense, sort of, since a
    beep signals an error. Therefore, the macro definition never comes
    to the point where you save and then kill your buffer

  - Even when you add these commands manually, they are never executed
    because the beep comes first. 

This is a bit unfortunate. I can't think of a solution that doesn't
include some programming, replacing either one of the macros with a
bit of elisp code. Replacing the outer macro could look like this
(assuming that you named your keyboard macro `foo'):

  (defun dired-foo-marked-files ()
    (interactive)
    (mapc (lambda (file) 
            (find-file file)
            (goto-char (point-min))
            (condition-case nil
                (dotimes (i 100)
                  (execute-kbd-macro 'foo))
              (error nil))
            (save-buffer)
            (kill-buffer))
          (dired-get-marked-files)))

HTH, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


reply via email to

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