如何在emacs中切换区域中的字母大小写

时间:2013-08-15 16:53:07

标签: emacs elisp

如何切换Emacs中区域文本的字母大小写(将大写字母切换为小写和小写字母为大写)?

列出了转换命令,但没有用于切换的命令。

示例:

转换我的信件

应该成为:

请写我的信CASE

5 个答案:

答案 0 :(得分:7)

您可以使用正则表达式替换:

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

由你来绑定一个密钥。

答案 1 :(得分:2)

我是为你写的;它没有经过彻底的测试,但似乎做了你想要的。

它背后的逻辑是遍历文本中的每个字符。如果字符等于小写字符,则将其以大写形式附加到返回字符串。如果没有,请将其以小写形式附加。最后,删除区域并插入返回字符串。

它立即在一个文本页面上工作,但我会谨慎地在大文本上使用它(应该还是可以的)。

(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 :(得分:1)

如果你的意思是字母大小写,那么这个功能很好用: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")) )
    ) )

答案 3 :(得分:1)

命令upcase-regiondowncase-regioncapitalize-region不会切换,也许是&#34;转换&#34;你提到的命令。这是一个在它们之间循环的命令。

 (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)))

答案 4 :(得分:0)

我喜欢其他答案的比较this-commandlast-command的技巧, 所以我把它合并到我的旧功能中。结果如下:

(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)