How to switch letters to regions in emacs

How can I switch case letters (convert uppercase letters to lowercase and uppercase letters) of region text in Emacs?

Commands for conversion are listed here, but nothing for switching.

Example:

PLease toggLE MY LETTER case

should become:

SERVE CASE MY CASE

+6
source share
5 answers

I wrote this for you; he did not have rigorous testing, but he seems to be doing what you are looking for.

The logic behind it is to sort out all the characters in the text. If the character is equal to a lowercase character, add it to the return string in upper case. If not, add it to lowercase. At the end, delete the region and insert the returned row.

It works right on the text page, although I would use it with caution on huge texts (it must be all the same).

(defun toggle-case () (interactive) (when (region-active-p) (let ((i 0) (return-string "") (input (buffer-substring-no-properties (region-beginning) (region-end)))) (while (< i (- (region-end) (region-beginning))) (let ((current-char (substring input i (+ i 1)))) (if (string= (substring input i (+ i 1)) (downcase (substring input i (+ i 1)))) (setq return-string (concat return-string (upcase (substring input i (+ i 1))))) (setq return-string (concat return-string (downcase (substring input i (+ i 1))))))) (setq i (+ i 1))) (delete-region (region-beginning) (region-end)) (insert return-string)))) 
+2
source

You can do this by replacing regexp:

 Mx replace-regexp RET \([[:upper:]]+\)?\([[:lower:]]+\)? RET \,(concat (downcase (or \1 "")) (upcase (or \2 ""))) RET 

You need to associate a key with this.

+7
source

If you mean letter case, this function works beautifully: http://ergoemacs.org/emacs/modernization_upcase-word.html

 (defun toggle-letter-case () "Toggle the letter case of current word or text selection. Toggles between: "all lower", "Init Caps", "ALL CAPS"." (interactive) (let (p1 p2 (deactivate-mark nil) (case-fold-search nil)) (if (region-active-p) (setq p1 (region-beginning) p2 (region-end)) (let ((bds (bounds-of-thing-at-point 'word) ) ) (setq p1 (car bds) p2 (cdr bds)) ) ) (when (not (eq last-command this-command)) (save-excursion (goto-char p1) (cond ((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower")) ((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps") ) ((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps") ) ((looking-at "[[:lower:]]") (put this-command 'state "all lower")) ((looking-at "[[:upper:]]") (put this-command 'state "all caps") ) (t (put this-command 'state "all lower") ) ) ) ) (cond ((string= "all lower" (get this-command 'state)) (upcase-initials-region p1 p2) (put this-command 'state "init caps")) ((string= "init caps" (get this-command 'state)) (upcase-region p1 p2) (put this-command 'state "all caps")) ((string= "all caps" (get this-command 'state)) (downcase-region p1 p2) (put this-command 'state "all lower")) ) ) ) 
+1
source

The upcase-region , downcase-region and capitalize-region commands do not switch, and are probably the "conversion" commands that you mentioned. Here is a team that cycle between.

  (defvar cycle-region-capitalization-last 'upper) (defun cycle-region-capitalization (&optional msgp) "Cycle the region text among uppercase, lowercase and capitalized (title case)." (interactive "p") (setq cycle-region-capitalization-last (case cycle-region-capitalization-last (upper (call-interactively #'downcase-region) 'lower) (lower (call-interactively #'capitalize-region) 'title) (title (call-interactively #'upcase-region) 'upper))) (when msgp (message "Region is now %scase" cycle-region-capitalization-last))) 
+1
source

I liked the different technique for comparing this-command and last-command , so I included it in my old function. Here is the result:

 (defun upcase-word-toggle () (interactive) (let ((bounds (bounds-of-thing-at-point 'symbol)) beg end regionp) (if (eq this-command last-command) (setq regionp (get this-command 'regionp)) (put this-command 'regionp nil)) (cond ((or (region-active-p) regionp) (setq beg (region-beginning) end (region-end)) (put this-command 'regionp t)) (bounds (setq beg (car bounds) end (cdr bounds))) (t (setq beg (point) end (1+ beg)))) (save-excursion (goto-char (1- beg)) (and (re-search-forward "[A-Za-z]" end t) (funcall (if (char-upcasep (char-after)) 'downcase-region 'upcase-region) beg end))))) (defun char-upcasep (letter) (eq letter (upcase letter))) (global-set-key (kbd "C->") 'upcase-word-toggle) 
0
source

Source: https://habr.com/ru/post/951816/


All Articles