2

In dired mode, with the key m, you mark a file.

You can unmark this with the key u.

I find that somewhat cumbersome, so I would bind the key m to mark or unmark a file under the cursor, depending of the status of file. When looking around, I found dired-toggle-marks. But it toggles the marks on all files, but the function dired-toggle-mark doesn't exist yet.

It surprised me, given how big and extensible Dired is.

Then I thought I could devise my own function, to toggle mark on the file. When I do C-h f dired-toggle-marks to read the source code, it doesn't display the source code to me.

So what would be a good way to find out how to detect the status of file under cursor, and mark/unmark it?

2

For me, C-h f dired-toggle-marks describes the command and has a link to its source code (definition). Do you not see a link to file dired.el from C-h f? If you see it, does it take you to the definition? In case it doesn't, this the definition (in Emacs 24.5, for example):

(defun dired-toggle-marks ()
  "Toggle marks: marked files become unmarked, and vice versa.
Files marked with other flags (such as `D') are not affected.
`.' and `..' are never toggled.
As always, hidden subdirs are not affected."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (let ((inhibit-read-only t))
      (while (not (eobp))
        (or (dired-between-files)
            (looking-at-p dired-re-dot)
            ;; use subst instead of insdel because it does not move
            ;; the gap and thus should be faster and because
            ;; other characters are left alone automatically
            (apply 'subst-char-in-region
                   (point) (1+ (point))
                   (if (eq ?\040 (following-char)) ; SPC
                       (list ?\040 dired-marker-char)
                     (list dired-marker-char ?\040))))
        (forward-line 1)))))

Does that give you what you need?

As for why there is no command to toggle the mark status for only the current file/dir, the answer is probably that no one has felt the need for it.

Are you looking for such a function for use in code, or are you looking for it as a command, to use it interactively? For the latter, you can see whether the file/dir is marked or not, so you know whether you want to mark or unmark it.

Or is it that you want to reduce the two key bindings m and u to a single one?

Note that the keys m and u are more general than just marking and unmarking the current line's file/dir. They act on the next N lines. It's probably because of this generality, and the fact that you can see whether you want to mark or unmark the current line, that there is no toggle-mark-only-here command.

I suppose that a command that toggles marking the current or next N lines could be useful, but I'm guessing that people haven't felt the need for it. In any case, that should be easy for you to define, now that you have the source for dired-toggle-marks. (You could alternatively start with the code for dired-mark and `dired-unmark.)

I'm guessing you don't really need the code that does what you want as an answer here, and that you will enjoy writing it. But if not, please say so and I (or someone else) will post some code for it here.


Something related, which might be of interest:

With Dired+ clicking mouse-3 brings up a menu of actions for the file/dir of the current line. If that is unmarked then you'll see menu-item Mark, and if it is marked then you'll see item Unmark. In other words, the menu item name is toggled, along with its action.

  • "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime". That's why I didn't ask if someone could write to toggle the mark/unmark, and then go to the next line. But Emacs says with <KBD>C-h f</KBD> dired-toggle-marks the following without reference to source: i.imgur.com/TegqulU.png any idea why you get the reference to the definition? I'm using Emacs 27.0.5.0 by the way. Thanks for providing source code, then I will try to write. – ReneFroger Nov 20 '17 at 13:35
  • Yeah, sometimes C-h f or C-h v doesn't tell you where something is defined. I think this has gotten worse over the years, but others have argued that it's gotten better. – Drew Nov 20 '17 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.