uppercase backward

Original link: https://emacstalk.github.io/post/023/

There are two main ways to enter capital letters:

  1. While holding down Shift , entering letters in sequence is a kind of,

  2. When there are more letters, you can press CapsLock first, then type the letters

But I believe that for most people, words with capital letters are more difficult to read, and there are relevant studies to prove that:

Lowercase letters have a more distinctive shape than capital letters, therefore they can be perceived more quickly than uppercase letters. Because readers are frequently exposed to a word, they no longer have to “read” the word, but instantly recognize the meaning by the familiar shape of the group of letters.

Therefore, many people use the following way to input capital letters:

  • Enter lowercase letters first, then select Change to uppercase

The advantage of this is that it is easy to identify whether there is a spelling error. For Emacs, it is Mu(upcase-word) , but there is a little trouble: before conversion, you need to move Mb backward to the beginning of the letter, and then press Mu Once, if there is a hyphen, it needs to be pressed multiple times to move backwards and capitalize, which is a bit troublesome.

(upcase-word ARG) supports incoming negative numbers to move forward, but when encountering a hyphen in the letter, only the last word will be converted, and the cursor will not move. If you want to continue the conversion, you still need Mb , so it is not competent, only You can write your own code to solve:

 (defun my/upcase-backwards () "Upcase word in reverse direction, back until the first space char or beginning-of-line" (interactive) (save-excursion ;; move to first non-space char ( skip-syntax-backward " " ( line-beginning-position )) (push-mark) (let ((beginning (or ( re-search-backward "[[:space:]]" ( line-beginning-position ) t ) ( line-beginning-position ))) (end (mark))) (unless ( = beginning end) ( upcase-region beginning end))))) (global-set-key (kbd "Mo" ) 'my/upcase-backwards )

The above function uses spaces as word boundaries, so it can process concatenated words like abc in one go. In addition, the following corner cases are also handled:

  1. Only operate within the same line

  2. When the cursor is in a space, backtrack forward until a non-space letter is found

demo

| is the cursor, before processing

 abc |abc abc-abc| 

after processing

 ABC | abc ABC-ABC| 

This article is reprinted from: https://emacstalk.github.io/post/023/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment