help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get the ~ functionality of vi


From: Cecil Westerhof
Subject: Re: How to get the ~ functionality of vi
Date: Fri, 27 May 2016 10:54:17 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

On Friday 27 May 2016 06:28 CEST, Jean-Jacques Rétorré wrote:

> ven. 27 mai 2016,  Emanuel Berg <embe8573@student.uu.se> disait :
>
>> Cecil Westerhof <Cecil@decebal.nl> writes:
>>
>>> In vi you can use ~ to flip the case of
>>> a character. Does Emacs has something
>>> like that?
>>
>> Try this:
>>
>> (defun flip-case ()
>> (interactive)
>> (let ((c (string-to-char (thing-at-point 'char t))))
>> (delete-char 1)
>> (cond ((and (>= c ?A) (<= c ?Z)) (insert-char (downcase c)))
>> ((and (>= c ?a) (<= c ?z)) (insert-char (upcase   c)))
>> (t                         (insert-char c)))) )
>> (global-set-key "\C-cf" #'flip-case)
>
> That won't work for accented chars.
> Probably this should :
>
> (defun flip-case ()
> (interactive)
> (let ((c (string-to-char (thing-at-point 'char t))))
> (delete-char 1)
> (insert-char
> (if (eq c (upcase c))
> (downcase c)
> (upcase c))) ) )

I expected that there was a function I overlooked, but I did not.
Luckily it is easy to add the missing functionality in Emacs. :-D

The thing-at-point did not work for me. I rewrote it to:
    (defun flip-case ()
      (interactive)
      (let ((c (char-after)))
        (when c
          (delete-char 1)
          (insert-char
           (if (eq c (upcase c))
               (downcase c)
             (upcase c))))))

I also added a check for there being a current character.

For the keyboard shortcut I use:
    (global-set-key (kbd "C-~") 'flip-case)

I like to flip a lot of characters in one swell swoop.


Thanks for putting me in the right direction.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


reply via email to

[Prev in Thread] Current Thread [Next in Thread]