all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kaushal Modi <kaushal.modi@gmail.com>
To: Thorsten Bonow <thorsten.bonow@withouthat.org>, help-gnu-emacs@gnu.org
Subject: Re: How to get the ~ functionality of vi
Date: Thu, 02 Jun 2016 16:14:12 +0000	[thread overview]
Message-ID: <CAFyQvY3vwx9AB1H0eX01ScH5=WhT7ri9hbZTLmNcJv65DpPCKg@mail.gmail.com> (raw)
In-Reply-To: <877fefg2h0.fsf@talkietoaster.mmweg.rwth-aachen.de>

Check out http://ergoemacs.org/emacs/modernization_upcase-word.html

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))
           (save-excursion
             (goto-char p1)
             (cond
              ;; 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))

(bind-keys
 :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))

[1]:
https://github.com/kaushalmodi/.emacs.d/blob/aae4262ef4ddb6834683f1c72c56153261b779d5/setup-files/setup-editing.el#L688

On Thu, Jun 2, 2016 at 12:06 PM Thorsten Bonow <
thorsten.bonow@withouthat.org> wrote:

> >>>>> Cecil Westerhof <Cecil@decebal.nl> writes:
>
> > Cecil Westerhof <Cecil@decebal.nl> 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


  parent reply	other threads:[~2016-06-02 16:14 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-26 22:36 How to get the ~ functionality of vi Cecil Westerhof
2016-05-27  1:15 ` Emanuel Berg
2016-05-27  4:28   ` Jean-Jacques Rétorré
2016-05-27  8:54     ` Cecil Westerhof
2016-05-27 11:33       ` Thorsten Bonow
2016-05-27 19:41         ` Emanuel Berg
2016-06-02 16:14         ` Kaushal Modi [this message]
     [not found]         ` <mailman.703.1464884068.1216.help-gnu-emacs@gnu.org>
2016-06-02 17:15           ` Emanuel Berg
2016-06-02 17:28             ` Kaushal Modi
     [not found]             ` <mailman.715.1464888555.1216.help-gnu-emacs@gnu.org>
2016-06-02 20:39               ` Emanuel Berg
2016-06-02 21:00                 ` Kaushal Modi
2016-06-03  5:07                   ` Marcin Borkowski
     [not found]                 ` <mailman.730.1464901264.1216.help-gnu-emacs@gnu.org>
2016-06-02 21:07                   ` Emanuel Berg
2016-06-03  9:21                     ` Joost Kremers
2016-06-03 10:51                       ` Emanuel Berg
2016-05-27 19:37       ` Emanuel Berg
2016-05-27 19:37       ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFyQvY3vwx9AB1H0eX01ScH5=WhT7ri9hbZTLmNcJv65DpPCKg@mail.gmail.com' \
    --to=kaushal.modi@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=thorsten.bonow@withouthat.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.