about summary refs log tree commit diff stats
path: root/lisp/scule.el
blob: 3ed58bb6b6d97851b429d63224cab4c79ffb3e81 (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
;;; scule.el --- -scule twiddling                    -*- lexical-binding: t; -*-

;; Copyright (C) 2022  Case Duckworth

;; Author: Case Duckworth <case@bob>
;; Keywords: convenience

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; DWIM functions for twiddling "scule", or text case (a fancy word for "upper
;; case" is "magiscule", for example, and "lower case" is "miniscule").

;;; Code:

(require 'thingatpt)

;;; Utility macro
;;;###autoload
(defmacro defscule (name &optional region-fn word-fn)
  (let ((fn-name (intern (format "scule-%s" name)))
        (region-fn (or region-fn (intern (format "%s-region" name))))
        (word-fn (or word-fn (intern (format "%s-word" name)))))
    `(defun ,fn-name (arg)
       ,(concat (capitalize (symbol-name name)) " words in the region if active, or word at point.\n"
                "If the region is active, call `" (symbol-name region-fn) "'.\n"
                "Otherwise, it calls `" (symbol-name word-fn) "' on the word at point and\n"
                "the following ARG - 1 words.")
       (interactive "*p")
       (if (use-region-p)
           (,region-fn (region-beginning) (region-end) (region-noncontiguous-p))
         (let ((word-bound (save-excursion
                             (skip-chars-forward "^[:word:]")
                             (bounds-of-thing-at-point 'word))))
           (when (and (car word-bound) (cdr word-bound))
             (,region-fn (car word-bound) (cdr word-bound))
             (goto-char (cdr word-bound))
             (,word-fn (1- arg))))))))

;;;###autoload
(progn (defscule upcase)
       (autoload 'scule-upcase "scule" nil t)
       (defscule downcase)
       (autoload 'scule-downcase "scule" nil t)
       (defscule capitalize)
       (autoload 'scule-capitalize "scule" nil t))

(provide 'scule)
;;; scule.el ends here