summary refs log tree commit diff stats
path: root/lisp/+casing.el
blob: a21e7101e03ddc68b9a8df665328ff2e586a2050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
;;; +casing.el --- Word-case-twiddling things -*- lexical-binding: t; -*-

;;; Code:

(require 'thingatpt)
(require '+key)

;;;###autoload
(defun +upcase-dwim (arg)
  "Upcase words in the region, or upcase word at point.
If the region is active, this function calls `upcase-region'.
Otherwise, it calls `upcase-word' on the word at point (using
`thingatpt'), and the following ARG - 1 words."
  (interactive "*p")
  (if (use-region-p)
      (upcase-region (region-beginning) (region-end) (region-noncontiguous-p))
    (let ((following (1- arg))
          (word-bound (save-excursion
                        (skip-chars-forward "^[:word:]")
                        (bounds-of-thing-at-point 'word))))
      (when (and (car word-bound) (cdr word-bound))
        (upcase-region (car word-bound) (cdr word-bound))
        (goto-char (cdr word-bound))
        (upcase-word following)))))

;;;###autoload
(defun +downcase-dwim (arg)
  "Downcase words in the region, or downcase word at point.
If the region is active, this function calls `downcase-region'.
Otherwise, it calls `downcase-word' on the word at point (using
`thingatpt'), and the following ARG - 1 words."
  (interactive "*p")
  (if (use-region-p)
      (downcase-region (region-beginning) (region-end) (region-noncontiguous-p))
    (let ((following (1- arg))
          (word-bound (save-excursion
                        (skip-chars-forward "^[:word:]")
                        (bounds-of-thing-at-point 'word))))
      (when (and (car word-bound) (cdr word-bound))
        (downcase-region (car word-bound) (cdr word-bound))
        (goto-char (cdr word-bound))
        (downcase-word following)))))

;;;###autoload
(defun +capitalize-dwim (arg)
  "Capitalize words in the region, or capitalize word at point.
If the region is active, this function calls `capitalize-region'.
Otherwise, it calls `capitalize-word' on the word at point (using
`thingatpt'), and the following ARG - 1 words."
  (interactive "*p")
  (if (use-region-p)
      (capitalize-region (region-beginning) (region-end) (region-noncontiguous-p))
    (let ((following (1- arg))
          (word-bound (save-excursion
                        (skip-chars-forward "^[:word:]")
                        (bounds-of-thing-at-point 'word))))
      (when (and (car word-bound) (cdr word-bound))
        (capitalize-region (car word-bound) (cdr word-bound))
        (goto-char (cdr word-bound))
        (capitalize-word following)))))

;; Later on, I'll add repeat maps and stuff in here...

(define-minor-mode +casing-mode
  "Enable easy case-twiddling commands."
  :lighter " cC"
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map "u" #'+upcase-dwim)
            (define-key map (kbd "M-u") #'+upcase-dwim)
            (define-key map "l" #'+downcase-dwim)
            (define-key map (kbd "M-l") #'+downcase-dwim)
            (define-key map "c" #'+capitalize-dwim)
            (define-key map (kbd "M-c") #'+capitalize-dwim)
            map)
  (define-key +key-mode-map (kbd "M-c") (when +casing-mode
                                          +casing-mode-map)))

(provide '+casing)
;;; +casing.el ends here