The following message is a courtesy copy of an article
that has been posted to comp.emacs.xemacs as well.
Hi, for some time now, I have some coding in my ~/.emacs (appended),
which is IMHO quite useful if you work under both Unix and Windows:
button2 on the multibyte extension status in the modeline (ISO8, ISO8:T
etc) toggles the end-of-line type of the current buffer, i.e., toggles
between Unix- (LF) and Dos- (CRLF) line endings.
If this is considered generally useful, I would send corresponding
patches for coding.el and code-files.el. I need some feedback whether
the patches should also include the following patches for command
`toggle-buffer-file-coding-system' which does (or should do) the same:
1. Also toggles eol type for new files (technical: switches from eol
type nil to crlf, of course not for coding systems like binary).
2. Sets buffer as modified (if not read-only). Why do you want to
toggle the eol type if you don't want to save the buffer?
BTW, any possibility to get ":dos" instead of ":T" in the modeline?
- Christoph
(defvar modeline-coding-system-extent (make-extent nil nil)
"Extent covering the %C specification of the string in the modeline.")
(defvar modeline-coding-system-map
(let ((map (make-sparse-keymap)))
(define-key map 'button2 'mouse-toggle-buffer-file-coding-system)
map))
(set-extent-face modeline-coding-system-extent 'modeline-mousable-minor-mode)
(set-extent-property modeline-coding-system-extent 'help-echo
"button2 toggles the buffer's end-of-line type")
(set-extent-keymap modeline-coding-system-extent modeline-coding-system-map)
;; From mule/mule-init.el, now coding.el:
(if (or (featurep 'mule) (featurep 'file-coding))
(let ((modeline (cdr (default-value 'modeline-format)))) ; w/o initial
""
(setq-default modeline-format
(cons ""
(cons (cons modeline-coding-system-extent
'modeline-multibyte-status)
(if (eq (car-safe modeline) ; coding.el
'modeline-multibyte-status)
(cdr modeline)
modeline))))))
(defun mouse-toggle-buffer-file-coding-system (event)
(interactive "e")
(save-excursion
(set-buffer (event-buffer event))
;; Next 9 lines: like `toggle-buffer-file-coding-system', but also toggles
;; if none of the end-of-line variants is the current one (assuming -unix),
;; should be probably part of `toggle-buffer-file-coding-system'
(let ((eol-type
(coding-system-eol-type buffer-file-coding-system)))
(setq buffer-file-coding-system
(subsidiary-coding-system
(coding-system-base buffer-file-coding-system)
(cond ((eq eol-type 'lf) 'crlf)
((eq eol-type 'crlf) 'lf)
((eq eol-type 'cr) 'lf)
(t 'crlf)))))
;; Next line should be part of `toggle-buffer-file-coding-system'
(or buffer-read-only (set-buffer-modified-p t))
(redraw-modeline)))