Occasionally, you may have a big file which refuses to save as ASCII. Here is how to find the non-ASCII character.
The recommended way to search for non-ASCII characters is to use the regexp
"[^\000-\177]". It is portable and efficient. (Interactively, use
C-M-s [ ^ C-q 0 0 0 RET - C-q 1 7 7 RET ].) In Emacs 21 you could also use the char class
(defun find-first-non-ascii-char () "Find the first non-ascii character from point onwards." (interactive) (let (point) (save-excursion (setq point (catch 'non-ascii (while (not (eobp)) (or (eq (char-charset (following-char)) 'ascii) (throw 'non-ascii (point))) (forward-char 1))))) (if point (goto-char point) (message "No non-ascii characters."))))
Generalising the above - this function will allow you to find characters your current coding system cannot encode:
(defun find-next-unsafe-char (&optional coding-system) "Find the next character in the buffer that cannot be encoded by coding-system. If coding-system is unspecified, default to the coding system that would be used to save this buffer. With prefix argument, prompt the user for a coding system." (interactive "Zcoding-system: ") (if (stringp coding-system) (setq coding-system (intern coding-system))) (if coding-system nil (setq coding-system (or save-buffer-coding-system buffer-file-coding-system))) (let ((found nil) (char nil) (csets nil) (safe nil)) (setq safe (coding-system-get coding-system 'safe-chars)) ;; some systems merely specify the charsets as ones they can encode: (setq csets (coding-system-get coding-system 'safe-charsets)) (save-excursion ;;(message "zoom to <") (let ((end (point-max)) (here (point )) (char nil)) (while (and (< here end) (not found)) (setq char (char-after here)) (if (or (eq safe t) (< char ?\177) (and safe (aref safe char)) (and csets (memq (char-charset char) csets))) nil ;; safe char, noop (setq found (cons here char))) (setq here (1+ here))) )) (and found (goto-char (1+ (car found)))) found))
Here’s a simple defun to show non-ascii characters of current buffer in an Occur buffer
(defun occur-non-ascii () "Find any non-ascii characters in the current buffer." (interactive) (occur "[^[:ascii:]]"))
See also markchars-mode in nXhtml.