0

I created the following function to remove macrons from vowels (this is a reduced version that only searches for the vowel "ā"):

(defun nv/eliminatemacron ()
  ""
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "ā" nil t)
      (replace-match "a"))))

However, it always fails and prints Replaced 0 occurrences.

I also tried with query-replace but with the same negative results.

Any ideas on what's going on here?

1

I think this is a Unicode problem rather than an Emacs one. I tried your code out in the scratch buffer and it seemed to work fine.

I can't say for sure without looking at your text, but the problem may be that your "ā" characters aren't actually "ā" (U+0101). Instead, I suspect that they may instead be a combining character sequence of ("a" and the combining character ̄ (U+304)), the result of which is an identical but different "ā". (You can see the difference by copying and pasting each of "ā" and "ā" and then trying to backspace over them.) If this is the case, search forward won't match because this combined character is distinct from "ā".

The following function should remove the combining character, leaving behind just "a" in each case.

(defun nv/eliminatemacron2 ()
  ""
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "ā" nil t)
      (replace-match "a"))
    )
  )

You could also just generally replace all of the combining macrons with the empty string to get rid of this kind of sequence ((replace-string "̄" "")). Note that the combining character tries to combine with the double quote which doesn't display properly on Stack Exchange, but it appears to copy and paste properly for me. On Emacs it should appear that the starting quote has a macron, but really the macron character comes afterwards. If this doesn't work, you can manually type the character in with C-x 8 RET COMBINING MACRON.

  • Thanks for your message. I just arrived home, where I have a Linux box, and my code worked fine here. I wrote it at the office, where I have a Mac box, and it was there where the code didn't work. I'll try your suggestion tomorrow. – NVaughan Apr 9 at 19:09
  • 1
    If this doesn't work either, I would try using describe-char one of the characters that isn't matching that you think should. That may help you get to the bottom of the issue. – D. Gillis Apr 9 at 19:13
  • I'll do so. Thanks. – NVaughan Apr 9 at 19:23
  • Yes, you were right. The was a character sequence. Thanks. – NVaughan Apr 10 at 18:30
  • 2
    I didn't think about it before, but it's also worth noting that enabling Emacs' character folding would allow your original code to work. This can be done for replace (in Emacs >25.1) by setting the variable replace-char-fold to a non-nil value. Perhaps this setting is why it worked on your Linux but not your Mac (Emacs version differences could also be a contributing factor). – D. Gillis Apr 10 at 23:31

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.