On MacOS, if a folder has custom icon, there would be an invisible file named Icon^M in it. ^M being carriage return. Kindly refer to https://superuser.com/questions/298785/icon-file-on-os-x-desktop for more details about such a file.

If I try to add the parent folder with M-x file-cache-add-directory, I get error "Filecache: file /xxx/xxx/Icon does not exist"

Is it possible to let Emacs ignore this file?

  • Curious. Emacs might be (undesirably) applying EOL normalisation? In the output of directory-files the carriage return (which is the old-style MacOS EOL syntax) has been converted into a newline (which is Unix EOL syntax). – phils May 29 '17 at 2:29
  • Is Icon^M a file or a folder (directory)? Twice you call it a file, but it sounds like you are trying to add it as a directory using M-x file-cache-add-directory (?). – Drew May 29 '17 at 2:51
  • @Drew I was trying to add its parent folder with file-cache-add-directory – SparedWhisle May 29 '17 at 2:53
  • That's what I guessed, but you might want to edit to say that more clearly. Thx. – Drew May 29 '17 at 2:53
  • David Dai: please M-x report-emacs-bug about directory-files converting the trailing carriage-return into a newline. – phils May 29 '17 at 2:57

If Icon^M is a file, and you want to ignore it, it looks like you can customize option file-cache-filter-regexps to do that. Have you tried?

file-cache-filter-regexps is a variable defined in filecache.el.

Its value is

("~$" "\\.o$" "\\.exe$" "\\.a$" "\\.elc$" ",v$" "\\.output$" "\\.$" "#$" "\\.class$")


List of regular expressions used as filters by the file cache.

File names which match these expressions will not be added to the cache.

Note that the functions file-cache-add-file and file-cache-add-file-list do not use this variable.

You can customize this variable.

| improve this answer | |
  • 1
    You do still need to convert the \r to a \n, but this is a nicer solution than my suggestion. e.g. (add-to-list 'file-cache-filter-regexps "Icon\n$") – phils May 29 '17 at 2:56

Somewhere in directory_files_internal the carriage return gets converted into a newline, which triggers the mismatch.

You can work around this with a custom replacement command that anticipates this issue:

(defun my-file-cache-add-directory (directory)
  "Add all files in DIRECTORY to the file cache.

Exclude files ending in newlines, on account of a bug whereby filenames
ending in a carriage return (such as OSX Icon^M files) are corrupted to
end in a newline, triggering a filename mismatch."
  (interactive (list (read-directory-name "Add files from directory: "
                                          nil nil t)))
  (file-cache-add-directory directory "[^\n]\\'"))
| improve this answer | |
  • the regex you used in above script looks very confusing to me. could you help me understand what \\' does? – SparedWhisle May 29 '17 at 14:32
  • M-x elisp-index-search RET \' RET. The double-backslash in the code is on account of the double-quoted string read syntax (and noting also that the \n is a string escape code, not a regexp escape code, which is why that one only has a single backslash). – phils May 29 '17 at 14:33

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.