Skip to content

Instantly share code, notes, and snippets.

@bravosierrasierra
Created May 26, 2017 07:08
Show Gist options
  • Save bravosierrasierra/9a52fccc2607ee1064736a487ba74f1c to your computer and use it in GitHub Desktop.
Save bravosierrasierra/9a52fccc2607ee1064736a487ba74f1c to your computer and use it in GitHub Desktop.
;; RECODE ENGLISH TO RUSSIAN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; punto switcher
;; rus<->lat перекодировка
;; К сожалению не помню автора :(
;; Иногда начинаешь набирать текст, забыв переключить регистр - очень обидно
;; M^x rus<->lat и M^x lat<->rus поможет этому горю
;;
(use-package cl)
;; http://www.emacswiki.org/emacs/%D0%9F%D0%B5%D1%80%D0%B5%D0%BA%D0%BE%D0%B4%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%BE%D0%B3%D0%BE%D0%A2%D0%B5%D0%BA%D1%81%D1%82%D0%B0
(defvar usr:*en/ru-table* '((?q . ?й) (?w . ?ц) (?e . ?у) (?r . ?к)
(?t . ?е) (?y . ?н) (?u . ?г) (?i . ?ш)
(?o . ?щ) (?p . ?з) (?\[ . ?х) (?\] . ?ъ)
(?a . ?ф) (?s . ?ы) (?d . ?в) (?f . ?а)
(?g . ?п) (?h . ?р) (?j . ?о) (?k . ?л)
(?l . ?д) (?\; . ?ж) (?' . ?э) (?z . ?я)
(?x . ?ч) (?c . ?с) (?v . ?м) (?b . ?и)
(?n . ?т) (?m . ?ь) (?, . ?б) (?. . ?ю)
(?Q . ?Й) (?W . ?Ц) (?E . ?У) (?R . ?К)
(?T . ?Е) (?Y . ?Н) (?U . ?Г) (?I . ?Ш)
(?O . ?Щ) (?P . ?З) (?{ . ?Х) (?} . ?Ъ)
(?A . ?Ф) (?S . ?Ы) (?D . ?В) (?F . ?А)
(?G . ?П) (?H . ?Р) (?J . ?О) (?K . ?Л)
(?L . ?Д) (?: . ?Ж) (?\" . ?Э) (?Z . ?Я)
(?X . ?Ч) (?C . ?С) (?V . ?М) (?B. ?И)
(?N . ?Т) (?M . ?Ь) (?< . ?Б) (?> . ?Ю)
(?\\ . ?ё) (?| . ?Ё)
(?` . ?ё) (?~ . ?Ё) (?@ . ?\") (?# . ?\')
(?$ . ?*) (?% . ?:) (?? . ?,) (?/ . ?.)
(?* . ?\;)))
;; (assoc ?\[ usr:*en/ru-table*)
;; (rassoc ?х usr:*en/ru-table*)
;; ?х ?\[
;; \;br
(defun lat<->rus (beg end &optional arg)
"Recode region that contains russain text typed in english into russian. With
ARG given recode region in the other direction."
(interactive "*r\nP")
(save-excursion
(goto-char beg)
(do () ((>= (point) end))
(let* ((en-char (char-after (point)))
(ru-char (if arg
(car (rassoc en-char usr:*en/ru-table*))
(cdr (assoc en-char usr:*en/ru-table*)))))
(delete-char 1)
(insert (if ru-char ru-char en-char))))))
(defun rus<->lat (beg end &optional arg)
"Recode region that contains russain text typed in russian into enflish. With
ARG given recode region in the other direction."
(interactive "*r\nP")
(save-excursion
(goto-char beg)
(do () ((>= (point) end))
(let* ((en-char (char-after (point)))
(ru-char (if arg
(cdr (assoc en-char usr:*en/ru-table*))
(car (rassoc en-char usr:*en/ru-table*)))))
(delete-char 1)
(insert (if ru-char ru-char en-char))))))
(transient-mark-mode 1)
;; - используем не (line-beginning-position) а (org-beginning-of-line &optional ARG)
;; major-mode 'org-mode
(defun fix-wrong-input-method-on-line-and-toggle-input-method ()
"fix wrong input method and toggle input method on entire line"
(interactive)
(if (equal current-input-method nil)
(progn ;; мы писали по русски на английской раскладке
(if (eq major-mode 'org-mode)
(progn
(org-beginning-of-line)
(lat<->rus (point) (line-end-position))
(end-of-line)
)
(progn
(lat<->rus (line-beginning-position) (line-end-position))
(end-of-line)))
(set-input-method russian-input-method) ;(toggle-input-method)
)
(progn ;; мы писали по английски на русской раскладке
(if (eq major-mode 'org-mode)
(progn
(org-beginning-of-line)
(rus<->lat (point) (line-end-position))
(end-of-line))
(progn
(rus<->lat (line-beginning-position) (line-end-position))
(end-of-line)))
(set-input-method nil) ;;(toggle-input-method)
)
))
(defun fix-wrong-input-method-and-toggle-input-method ()
"fix wrong input method and toggle input method"
(interactive)
(setq temp-point (point))
(if (equal current-input-method nil)
(progn ;; мы писали по русски на английской раскладке
(if mark-active
(lat<->rus (region-beginning) (region-end))
(progn
(set-mark (point))
(evil-backward-WORD-begin)
(lat<->rus (region-beginning) (region-end))))
(set-input-method russian-input-method) ;(toggle-input-method)
)
(progn ;; мы писали по английски на русской раскладке
(if mark-active
(lat<->rus (region-beginning) (region-end))
(progn
(set-mark (point))
(evil-backward-WORD-begin)
(rus<->lat (region-beginning) (region-end))))
(set-input-method nil) ;;(toggle-input-method)
))
(goto-char temp-point)
)
(global-set-key (kbd "<C-pause>") 'fix-wrong-input-method-on-line-and-toggle-input-method)
(global-set-key (kbd "M-C") 'fix-wrong-input-method-on-line-and-toggle-input-method)
(global-set-key (kbd "C-M-c") 'fix-wrong-input-method-and-toggle-input-method)
(global-set-key (kbd "s-v") 'fix-wrong-input-method-on-line-and-toggle-input-method)
(global-set-key (kbd "s-c") 'fix-wrong-input-method-and-toggle-input-method)
(global-set-key (kbd "<pause>") 'fix-wrong-input-method-and-toggle-input-method)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment