1

I want to enable mode association with backup files as well. E.g., if I backup my my-markdown.md file as my-markdown.md.orig or my-markdown.md.lastweek etc, I want them still be associated with the markdown mode. This is how I'm trying to fix it:

 (use-package markdown-mode
-  :mode ("\\.\\(txt\\|markdown\\|md\\)\\'" . markdown-mode))
+  :mode ("\\.\\(txt\\|markdown\\|md\\)\\(\\'\\|\\.)" . markdown-mode))

It was working fine before my hack, but now neither the .md files nor the .md.lastweek are associated.

UPDATE, for the record, for all my questions, I gather all the answers to here.

  • Are you perhaps interested in using the auto-mode-alist and defining the backup file extension names with your markdown-mode? – lawlist Jan 10 '15 at 18:04
  • 1
    @lawlist I believe the use-package macro is doing exactly that -- it creates auto-mode-alist entries based on the specific :mode config. – glucas Jan 10 '15 at 18:10
  • You have not escaped the final ) in your regex string. Maybe you meant this: \\.\\(txt\\|markdown\\|md\\)\\(\\'\\|\\.\\)? – glucas Jan 10 '15 at 18:13
  • 1
    One unrelated tip: With use-package :mode you don't need to specify the actual mode name unless it differs from the package. So for markdown you could just do this: (use-package markdown-mode :mode "\\.\\(txt\\|markdown\\|md\\)\\(\\'\\|\\.\\)") – glucas Jan 10 '15 at 21:55
2

I think you were on the right track but your regular expression does not escape the final ) character, like so:

"\\.\\(txt\\|markdown\\|md\\)\\(\\'\\|\\.\\)"

As a general tip, you can test out Emacs regular expressions using regexp-builder. Put some example text in a scratch buffer, say:

foo.md
foo.md.lastweek
foo.md.orig
foo.mdnomatch

Then do M-x regexp-builder. This opens a RE-Builder buffer where you can put in the regular expression and see what matches. (One note if you use this approach to test your expressions: \\' only matches the end of the content being tested, not the end of a line. Using $ would be easier to test in this way.)

  • Ahh! The last ) character! Thanks, and for the tip for regexp-builder too! – xpt Jan 10 '15 at 21:41
  • For another approach, see emacs.stackexchange.com/a/2654/780. You can configure auto-mode-alist to drop an extension and then continue to look for matches. This would allow you to append .orig to any file name and allow Emacs to still recognize the original extension. – glucas Jan 10 '15 at 22:19
  • My backup file extension is actually time based. E.g., for today, it'll be foo.md.2015-01-10, but thanks! – xpt Jan 10 '15 at 22:55
1

I suspect the regex, "\\.\\(txt\\|markdown\\|md\\)\\(\\'\\|\\.)" is the problem.

try this

This regular expression should match .txt, .markdown, .md, .md.lastweek, and .md.orig file extensions.

"\\.\\(txt\\|markdown\\|md\\([.]\\(lastweek\\|orig\\)\\)?\\)"

an alternative method

Explicitly define the major mode inside the files by placing the following markdown comment at the top of each file.

[//]: # ( -*- mode: markdown; -*- )

The comment should be hidden when you post it.

As a test, I explicitly inserted the example comment to the top of this answer.

I recommend using the alternative method because it provides the flexibility and convenience to assign the correct major mode as needed.

For example, what if you try another extension pattern in the future:

e.g.
.md.2015011000.your_username
.md.2015011001.my_username

If you like the new pattern, then you can always update the regex later and you won't risk break your current mapping during testing.

This also provides a safe method to test out new major modes, e.g. super-special-markdown-mode, before remapping the extensions in emacs.

Hope that helped!

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.