This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
org-zk-titlecase.el
71 lines (64 loc) · 1.78 KB
/
org-zk-titlecase.el
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
(defvar org-zk-titlecase-prepositions
(list
"vs"
"in"
"on"
"at"
"for"
"ago"
"before"
"since"
"to"
"into"
;; "past" (only in "half past ten")
"till"
"until"
"by"
;; "next to"
"above"
"across"
"through"
"towards"
"onto"
"from"
;; "out of"
"about"
"of"
;; "off" (get off the train)
))
(defvar org-zk-titlecase-articles (list "a" "an" "the"))
(defvar org-zk-titlecase-conjunctions (list
"and" "but" "or" "although" "because" "if" "since" "unless" "until" "while" "either" "or" "neither" "nor" "with"))
;; missing: "not only" "but also"
(defun org-zk-titlecase--downcase-p (word)
"Determine if WORD should be lowercase."
(let ((word (downcase word)))
(or
(member word org-zk-titlecase-prepositions)
(member word org-zk-titlecase-articles)
(member word org-zk-titlecase-conjunctions))))
(defun org-zk-titlecase--keep-word-p (word)
"Check if a word contains only uppercase characters."
(or (equal (upcase word) word)
;; org code syntax
(string-match-p (rx bol "~" (* any) "~" (? (any ";,.")) eol) word)
(string-match-p (rx bol "=" (* any) "=" (? (any ";,.")) eol) word)))
(defun org-zk-titlecase--process-word (word)
(cond
((org-zk-titlecase--keep-word-p word) word)
((org-zk-titlecase--downcase-p word) (downcase word))
(t (capitalize word))))
(defun org-zk-titlecase (input)
"Convert a string to title-case."
(let* ((words (split-string input))
(first (pop words)))
(if (null words)
(if (org-zk-titlecase--keep-word-p first)
first
(capitalize first))
(concat (if (org-zk-titlecase--keep-word-p first)
first
(capitalize first))
" "
(mapconcat #'org-zk-titlecase--process-word words " ")))))
(provide 'org-zk-titlecase)