Смена кодировки буфера
Модератор: /dev/random
-
- Бывший модератор
- Сообщения: 1100
- Статус: киборг
- ОС: Cyborg OS 0.0.1.3
Смена кодировки буфера
а как на лету переключать кодировки ? koi8-r, cp1251, cp866, utf8
I'm a tragic hero
In this game called life
My chances go to zero
But I always will survive
In this game called life
My chances go to zero
But I always will survive
-
- Сообщения: 256
- Статус: Emacs geek
- ОС: Emacs
Re: Смена кодировки буфера
я заметил что часто спрашивают как сменить текущую кодировку файла. Емакс позволяет указать кодировку файла перед открытием файла, но гдето в рунете я нашёл вот такую удобную функцию:
;; Первая функция меняет кодировку рид-онли буфера, а вторая - обычного. Очень удобно.
;; Использовать - M-x recode-buffer-safe (я себе сделал алиас - charset)
(defun recode-buffer-dangerous (target-coding-system)
"* Recode buffer as if it were encoded with `target-coding-system'.
If current buffer is write-protected (`buffer-read-only'), temporarily toggle
read-only flag, recode, then turn it back."
(interactive "zEnter target coding system: ")
(labels ((do-recode nil
(encode-coding-region (point-min)
(point-max)
buffer-file-coding-system)
(decode-coding-region (point-min)
(point-max)
target-coding-system)
(set-buffer-file-coding-system target-coding-system)))
(if buffer-read-only
(let ((buffer-read-only nil))
(do-recode)
(set-buffer-modified-p nil))
(do-recode))))
(defun recode-buffer-safe (target-coding-system)
"* Recode buffer as if it were encoded with `target-coding-system'.
If current buffer is write-protected (`buffer-read-only'), do nothing."
(interactive "zEnter target coding system: ")
(unless buffer-read-only
(encode-coding-region (point-min)
(point-max)
buffer-file-coding-system)
(decode-coding-region (point-min)
(point-max)
target-coding-system)
(set-buffer-file-coding-system target-coding-system)))
;; Первая функция меняет кодировку рид-онли буфера, а вторая - обычного. Очень удобно.
;; Использовать - M-x recode-buffer-safe (я себе сделал алиас - charset)
(defun recode-buffer-dangerous (target-coding-system)
"* Recode buffer as if it were encoded with `target-coding-system'.
If current buffer is write-protected (`buffer-read-only'), temporarily toggle
read-only flag, recode, then turn it back."
(interactive "zEnter target coding system: ")
(labels ((do-recode nil
(encode-coding-region (point-min)
(point-max)
buffer-file-coding-system)
(decode-coding-region (point-min)
(point-max)
target-coding-system)
(set-buffer-file-coding-system target-coding-system)))
(if buffer-read-only
(let ((buffer-read-only nil))
(do-recode)
(set-buffer-modified-p nil))
(do-recode))))
(defun recode-buffer-safe (target-coding-system)
"* Recode buffer as if it were encoded with `target-coding-system'.
If current buffer is write-protected (`buffer-read-only'), do nothing."
(interactive "zEnter target coding system: ")
(unless buffer-read-only
(encode-coding-region (point-min)
(point-max)
buffer-file-coding-system)
(decode-coding-region (point-min)
(point-max)
target-coding-system)
(set-buffer-file-coding-system target-coding-system)))