[Top][All Lists]

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

Re: How to get the ~ functionality of vi

From: Kaushal Modi
Subject: Re: How to get the ~ functionality of vi
Date: Thu, 02 Jun 2016 16:14:12 +0000

Check out

Here is my implementation based off that [1].
(needs use-package, hydra and region-bindings-mode packages: all of which I
consider as good as core packages)

(defun xah-cycle-letter-case (arg)
  "Cycle the letter case of the selected region or the current word.
Cycles from 'lower' -> 'Capitalize' -> 'UPPER' -> 'lower' -> ..

        C-u M-x xah-cycle-letter-case -> Force convert to upper case.
    C-u C-u M-x xah-cycle-letter-case -> Force convert to lower case.
C-u C-u C-u M-x xah-cycle-letter-case -> Force capitalize."
  (interactive "p")
  (let (p1 p2
           (deactivate-mark nil)
           (case-fold-search nil))
    (if (use-region-p)
        (setq p1 (region-beginning)
              p2 (region-end))
      (let ((bds (bounds-of-thing-at-point 'word)))
        (setq p1 (car bds)
              p2 (cdr bds))))

    (cl-case arg
      (4  (put this-command 'next-state "UPPER"))      ; Force convert to
upper case
      (16 (put this-command 'next-state "lower"))      ; Force convert to
lower case
      (64 (put this-command 'next-state "Capitalize")) ; Force capitalize
      (t (when (not (eq last-command this-command))
             (goto-char p1)
              ;; lower -> Capitalize
              ((looking-at "[[:lower:]]")            (put this-command
'next-state "Capitalize"))
              ;; Capitalize -> UPPER
              ((looking-at "[[:upper:]][[:lower:]]") (put this-command
'next-state "UPPER"))
              ;; Default: UPPER -> lower
              (t                                     (put this-command
'next-state "lower")))))))

    (cl-case (string-to-char (get this-command 'next-state)) ;
`string-to-char' returns first character in string
      (?U (upcase-region p1 p2)
          ;; UPPER -> lower
          (put this-command 'next-state "lower"))
      (?l (downcase-region p1 p2)
          ;; lower -> Capitalize
          (put this-command 'next-state "Capitalize"))
      ;; Capitalization is a better option here than upcasing the initials
      ;; because (upcase-initials "abc") -> "Abc" (good)
      ;;         (upcase-initials "ABC") -> "ABC" (not what I expect most
of the times)
      ;;         (capitalize "abc")      -> "Abc" (good)
      ;;         (capitalize "ABC")      -> "Abc" (good)
      (t (capitalize-region p1 p2)
         ;; Capitalize -> UPPER
         (put this-command 'next-state "UPPER")))))
(defun modi/upcase ()     (interactive) (xah-cycle-letter-case 4))
(defun modi/downcase ()   (interactive) (xah-cycle-letter-case 16))
(defun modi/capitalize () (interactive) (xah-cycle-letter-case 64))

 :map region-bindings-mode-map
  ("~" . xah-cycle-letter-case))

(defhydra hydra-change-case (:color blue
                             :hint nil)
_C_apitalize        _U_PCASE        _d_owncase        _<SPC>_ →Cap→UP→down→
  ("C"     modi/capitalize)
  ("c"     modi/capitalize)
  ("U"     modi/upcase)
  ("u"     modi/upcase)
  ("d"     modi/downcase)
  ("l"     modi/downcase)
  ("<SPC>" xah-cycle-letter-case :color red)
  ("M-c"   xah-cycle-letter-case :color red)
  ("q"     nil "cancel" :color blue))


On Thu, Jun 2, 2016 at 12:06 PM Thorsten Bonow <
address@hidden> wrote:

> >>>>> Cecil Westerhof <address@hidden> writes:
> > Cecil Westerhof <address@hidden> writes:
> > In vi you can use ~ to flip the case of a character. Does Emacs has
> something
> > like that?
> Hi,
> "viper", the vi emulation already included has `viper-toggle-case'.
> So what about this?
> (require 'viper); loading viper but not enabling it
> (global-set-key "\C-cf" 'viper-toggle-case)
> If you don't mind the overhead of loading the whole package for just one
> function. But you don't have to reinvent the wheel and use a maintained
> defun.
> Toto
> --
> Sent from my GNU Emacs running on GNU/Linux

Kaushal Modi

reply via email to

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