diff options
| author | Stefan Monnier | 2010-04-19 13:05:12 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2010-04-19 13:05:12 -0400 |
| commit | 87926e02dccd90bb8e01ccc648d69809d7811fe0 (patch) | |
| tree | efe6c927f97b7009b1178aca707a10d012439fda | |
| parent | fd3998ffe25d0b44771f110c43b8409656a2b7d3 (diff) | |
| download | emacs-87926e02dccd90bb8e01ccc648d69809d7811fe0.tar.gz emacs-87926e02dccd90bb8e01ccc648d69809d7811fe0.zip | |
* international/mule.el: Help the user choose a valid coding-system.
(read-buffer-file-coding-system): New function.
(set-buffer-file-coding-system): Use it. Prompt the user if the
coding-system cannot encode all the chars.
| -rw-r--r-- | lisp/ChangeLog | 5 | ||||
| -rw-r--r-- | lisp/international/mule.el | 75 |
2 files changed, 75 insertions, 5 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 93a008c8e16..0639aa50884 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,5 +1,10 @@ | |||
| 1 | 2010-04-19 Stefan Monnier <monnier@iro.umontreal.ca> | 1 | 2010-04-19 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 2 | ||
| 3 | * international/mule.el: Help the user choose a valid coding-system. | ||
| 4 | (read-buffer-file-coding-system): New function. | ||
| 5 | (set-buffer-file-coding-system): Use it. Prompt the user if the | ||
| 6 | coding-system cannot encode all the chars. | ||
| 7 | |||
| 3 | * vc-bzr.el: Use standard *vc* and *vc-diff* buffers. | 8 | * vc-bzr.el: Use standard *vc* and *vc-diff* buffers. |
| 4 | (vc-bzr-shelve-show, vc-bzr-shelve-apply) | 9 | (vc-bzr-shelve-show, vc-bzr-shelve-apply) |
| 5 | (vc-bzr-shelve-apply-and-keep, vc-bzr-shelve-snapshot): | 10 | (vc-bzr-shelve-apply-and-keep, vc-bzr-shelve-snapshot): |
diff --git a/lisp/international/mule.el b/lisp/international/mule.el index 244bfca8d28..ac33d26779f 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el | |||
| @@ -1165,6 +1165,64 @@ Internal use only.") | |||
| 1165 | (make-variable-buffer-local 'buffer-file-coding-system-explicit) | 1165 | (make-variable-buffer-local 'buffer-file-coding-system-explicit) |
| 1166 | (put 'buffer-file-coding-system-explicit 'permanent-local t) | 1166 | (put 'buffer-file-coding-system-explicit 'permanent-local t) |
| 1167 | 1167 | ||
| 1168 | (defun read-buffer-file-coding-system () | ||
| 1169 | (let* ((bcss (find-coding-systems-region (point-min) (point-max))) | ||
| 1170 | (css-table | ||
| 1171 | (unless (equal bcss '(undecided)) | ||
| 1172 | (append '("dos" "unix" "mac") | ||
| 1173 | (delq nil (mapcar (lambda (cs) | ||
| 1174 | (if (memq (coding-system-base cs) bcss) | ||
| 1175 | (symbol-name cs))) | ||
| 1176 | coding-system-list))))) | ||
| 1177 | (combined-table | ||
| 1178 | (if css-table | ||
| 1179 | (completion-table-in-turn css-table coding-system-alist) | ||
| 1180 | coding-system-alist)) | ||
| 1181 | (auto-cs | ||
| 1182 | (unless find-file-literally | ||
| 1183 | (save-excursion | ||
| 1184 | (save-restriction | ||
| 1185 | (widen) | ||
| 1186 | (goto-char (point-min)) | ||
| 1187 | (funcall set-auto-coding-function | ||
| 1188 | (or buffer-file-name "") (buffer-size)))))) | ||
| 1189 | (preferred | ||
| 1190 | (let ((bfcs (default-value 'buffer-file-coding-system))) | ||
| 1191 | (cons (and (or (equal bcss '(undecided)) | ||
| 1192 | (memq (coding-system-base bfcs) bcss)) | ||
| 1193 | bfcs) | ||
| 1194 | (mapcar (lambda (cs) | ||
| 1195 | (and (coding-system-p cs) | ||
| 1196 | (coding-system-get cs :mime-charset) | ||
| 1197 | (or (equal bcss '(undecided)) | ||
| 1198 | (memq (coding-system-base cs) bcss)) | ||
| 1199 | cs)) | ||
| 1200 | (coding-system-priority-list))))) | ||
| 1201 | (default | ||
| 1202 | (let ((current (coding-system-base buffer-file-coding-system))) | ||
| 1203 | ;; Generally use as a default the first preferred coding-system | ||
| 1204 | ;; different from the current coding-system, except for | ||
| 1205 | ;; the case of auto-cs since choosing anything else is asking | ||
| 1206 | ;; for trouble (would lead to using a different coding | ||
| 1207 | ;; system than specified in the coding tag). | ||
| 1208 | (or auto-cs | ||
| 1209 | (car (delq nil | ||
| 1210 | (mapcar (lambda (cs) | ||
| 1211 | (if (eq current (coding-system-base cs)) | ||
| 1212 | nil | ||
| 1213 | cs)) | ||
| 1214 | preferred)))))) | ||
| 1215 | (completion-ignore-case t) | ||
| 1216 | (completion-pcm--delim-wild-regex ; Let "u8" complete to "utf-8". | ||
| 1217 | (concat completion-pcm--delim-wild-regex | ||
| 1218 | "\\|\\([[:alpha:]]\\)[[:digit:]]")) | ||
| 1219 | (cs (completing-read | ||
| 1220 | (format "Coding system for saving file (default %s): " default) | ||
| 1221 | combined-table | ||
| 1222 | nil t nil 'coding-system-history | ||
| 1223 | (if default (symbol-name default))))) | ||
| 1224 | (unless (zerop (length cs)) (intern cs)))) | ||
| 1225 | |||
| 1168 | (defun set-buffer-file-coding-system (coding-system &optional force nomodify) | 1226 | (defun set-buffer-file-coding-system (coding-system &optional force nomodify) |
| 1169 | "Set the file coding-system of the current buffer to CODING-SYSTEM. | 1227 | "Set the file coding-system of the current buffer to CODING-SYSTEM. |
| 1170 | This means that when you save the buffer, it will be converted | 1228 | This means that when you save the buffer, it will be converted |
| @@ -1182,19 +1240,26 @@ surely saves the buffer with CODING-SYSTEM. From a program, if you | |||
| 1182 | don't want to mark the buffer modified, specify t for NOMODIFY. | 1240 | don't want to mark the buffer modified, specify t for NOMODIFY. |
| 1183 | If you know exactly what coding system you want to use, | 1241 | If you know exactly what coding system you want to use, |
| 1184 | just set the variable `buffer-file-coding-system' directly." | 1242 | just set the variable `buffer-file-coding-system' directly." |
| 1185 | (interactive "zCoding system for saving file (default nil): \nP") | 1243 | (interactive |
| 1244 | (list (read-buffer-file-coding-system) | ||
| 1245 | current-prefix-arg)) | ||
| 1186 | (check-coding-system coding-system) | 1246 | (check-coding-system coding-system) |
| 1187 | (if (and coding-system buffer-file-coding-system (null force)) | 1247 | (if (and coding-system buffer-file-coding-system (null force)) |
| 1188 | (setq coding-system | 1248 | (setq coding-system |
| 1189 | (merge-coding-systems coding-system buffer-file-coding-system))) | 1249 | (merge-coding-systems coding-system buffer-file-coding-system))) |
| 1250 | (when (called-interactively-p 'interactive) | ||
| 1251 | ;; Check whether save would succeed, and jump to the offending char(s) | ||
| 1252 | ;; if not. | ||
| 1253 | (let ((css (find-coding-systems-region (point-min) (point-max)))) | ||
| 1254 | (unless (or (eq (car css) 'undecided) | ||
| 1255 | (memq (coding-system-base coding-system) css)) | ||
| 1256 | (setq coding-system (select-safe-coding-system-interactively | ||
| 1257 | (point-min) (point-max) css | ||
| 1258 | (list coding-system)))))) | ||
| 1190 | (setq buffer-file-coding-system coding-system) | 1259 | (setq buffer-file-coding-system coding-system) |
| 1191 | (if buffer-file-coding-system-explicit | 1260 | (if buffer-file-coding-system-explicit |
| 1192 | (setcdr buffer-file-coding-system-explicit coding-system) | 1261 | (setcdr buffer-file-coding-system-explicit coding-system) |
| 1193 | (setq buffer-file-coding-system-explicit (cons nil coding-system))) | 1262 | (setq buffer-file-coding-system-explicit (cons nil coding-system))) |
| 1194 | ;; This is in case of an explicit call. Normally, `normal-mode' and | ||
| 1195 | ;; `set-buffer-major-mode-hook' take care of setting the table. | ||
| 1196 | (if (fboundp 'ucs-set-table-for-input) ; don't lose when building | ||
| 1197 | (ucs-set-table-for-input)) | ||
| 1198 | (unless nomodify | 1263 | (unless nomodify |
| 1199 | (set-buffer-modified-p t)) | 1264 | (set-buffer-modified-p t)) |
| 1200 | (force-mode-line-update)) | 1265 | (force-mode-line-update)) |