forked from cjohansen/.emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet-helpers.el
71 lines (55 loc) · 2.39 KB
/
snippet-helpers.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
;;; snippet-helpers.el --- Making snippet code look snazzy since 2011
;; Copyright (C) 2011 Magnar Sveen
;; Author: Magnar Sveen <[email protected]>
;; Keywords: snippets
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Helper methods used in snippets
;;; Code:
(defun chop-suffix (suffix s)
"Remove string 'suffix' if it is at end of string 's'"
(let ((pos (- (length suffix))))
(if (string= suffix (substring s pos))
(substring s 0 pos)
s)))
(defun buffer-file-name-body ()
"Buffer file name stripped of directory and extension"
(file-name-nondirectory (file-name-sans-extension (buffer-file-name))))
(defun split-name (s)
"Split name into list of words"
(split-string
(let ((case-fold-search nil))
(downcase
(replace-regexp-in-string "\\([a-z]\\)\\([A-Z]\\)" "\\1 \\2" s)))
"[^A-Za-z0-9]+"))
(defun mapcar-head (fn-head fn-rest list)
"Like MAPCAR, but applies a different function to the first element."
(if list
(cons (funcall fn-head (car list)) (mapcar fn-rest (cdr list)))))
(defun lower-camel-case (s)
"Convert string 's' to camelCase string."
(mapconcat 'identity (mapcar-head
'(lambda (word) (downcase word))
'(lambda (word) (capitalize (downcase word)))
(split-name s)) ""))
(defun upper-camel-case (s)
"Convert string 's' to CamelCase string."
(mapconcat 'identity (mapcar
'(lambda (word) (capitalize (downcase word)))
(split-name s)) ""))
(defun capitalized-words (s)
"Convert string 's' to Capitalized Words string."
(mapconcat 'identity (mapcar
'(lambda (word) (capitalize (downcase word)))
(split-name s)) " "))
(provide 'snippet-helpers)
;;; snippet-helpers.el ends here