summaryrefslogtreecommitdiffhomepage
path: root/lib/skels/jao-skel-cpp.el
blob: 0ddb61e6e62d64106db806fab4f823c612ed1d73 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
;;; jao-skel-cpp.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2004, 2005, 2008, 2009, 2022 Jose Antonio Ortega Ruiz

;; Author: Jose A Ortega Ruiz <jao@gnu.org>
;; Keywords: tools

;; This file 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, or (at your option)
;; any later version.

;; This file 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 GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; C/C++ skeletons.

;;; Code:

(require 'jao-skel)
(require 'thingatpt)

;;; Variables
(defvar jao-skel-cpp-root-namespace nil
  "The root C++ namespace")

(defvar jao-skel-cpp-brief-header-p nil
  "If non-nil, generate brief header comments")

(defvar jao-skel-cpp-make-guard-function #'jao-skel-cpp-make-guard-name
  "Function generating #include guards")

(defvar jao-skel-cpp-use-namespaces t
  "Whether to generate namespaces")

(defvar jao-skel-cpp-single-line-namespaces t
  "Whether to put consecutive namespace decls in a single line")

(defvar jao-skel-cpp-header-extension "hpp")

;;; Auxiliar functions
(defun jao-skel-cpp--find-other (ext)
  (file-name-nondirectory
   (or (ff-other-file-name)
       (concat (file-name-sans-extension (buffer-name)) "." ext))))

(defun jao-skel-cpp-make-guard-name (ns)
  "Create a standard include guard name"
  (upcase (mapconcat #'identity
                     `(,@ns ,(jao-skel-basename) ,(jao-skel-extension)
                            ,(user-login-name)
                            ,(format-time-string "%y%m%d%H%M"))
                     "_")))

;; namespaces
(defsubst jao-skel-cpp--read-ns (curr)
  (read-string (format "Add namespace (current: %s): " (or curr "[none]"))))

(defsubst jao-skel-cpp--ns2str (ns) (mapconcat 'identity ns "::"))

(defun jao-skel-cpp--get-ns-list (&optional acc)
  (do* ((result acc (cons next result))
        (next (jao-skel-cpp--read-ns (jao-skel-cpp--ns2str acc))
              (jao-skel-cpp--read-ns (jao-skel-cpp--ns2str (reverse result)))))
      ((string= next "") (reverse result))))

(defun jao-skel-cpp--insert-open-ns-list (ns)
  (dolist (n ns)
    (insert (format "namespace %s {%s"
                    n
                    (if jao-skel-cpp-single-line-namespaces " " "\n")))
    (indent-according-to-mode))
  (when jao-skel-cpp-single-line-namespaces
    (newline)
    (indent-according-to-mode)))

(defun jao-skel-cpp--insert-close-ns-list (ns)
  (if jao-skel-cpp-single-line-namespaces
      (insert (format "%s // namespace %s\n"
                      (make-string (length ns) ?})
                      (jao-skel-cpp--ns2str ns)))
    (dolist (n (reverse ns))
      (insert (format "}  // namespace %s\n" n)))))

(defun jao-skel-cpp--copy-ns-lines ()
  (let ((lines))
    (save-excursion
      (goto-char (point-min))
      (while (re-search-forward "namespace\\s-\\w+\\s-{\\|}+\\s-//\\s-namespace"
                                nil t)
        (push (thing-at-point 'line) lines)
        (next-line)))
    lines))

(defun jao-skel-cpp--copy-namespace ()
  (let* ((name (ff-other-file-name))
         (buff (and name (find-file-noselect name)))
         (nlines))
    (when buff
      (let ((lines (save-current-buffer
                     (set-buffer buff)
                     (jao-skel-cpp--copy-ns-lines))))
        (dolist (line lines)
          (push line nlines)
          (when (string-match "}" line)
            (push "\n\n\n\n" nlines)))))
    (mapconcat #'identity nlines "\n")))

(defsubst jao-skel-cpp--get-new-namespace ()
  (when jao-skel-cpp-use-namespaces
    (jao-skel-cpp--get-ns-list
     (and jao-skel-cpp-root-namespace (list jao-skel-cpp-root-namespace)))))

;; skeletons
(define-skeleton jao-skel-cpp-header-long
  "Initial file header blurb"
  "Brief file description: "
  "/**"
  > \n
  "* @file     " (file-name-nondirectory (buffer-file-name))
  > \n
  "* @brief    " str
  > \n
  "* @author   " (user-full-name) " <"user-mail-address">"
  > \n
  "* @date     " (format-time-string "%a %b %d, %Y %H:%M")
  > \n
  "*"
  > \n
  (jao-skel-copyright-line "* " "")
  "*"
  > ?\n
  (jao-skel-insert-license)
  > \n \n _)

(define-skeleton jao-skel-cpp-header-brief
  "Brief initial header blurb"
  nil
  (jao-skel-copyright-line "/* " " */")
  \n)

(define-skeleton jao-skel-cpp-header-comment
  "Insert a standard comment block"
  nil
  '(if jao-skel-cpp-brief-header-p
       (jao-skel-cpp-header-brief)
     (jao-skel-cpp-header-long)))

;; source C/C++ file ------------------------------------------------------
(define-skeleton jao-skel-cpp-source-header
  "Insert a standard C++ source header"
  nil
  '(jao-skel-cpp-header-comment)
  ? \n
  "#include \"" (jao-skel-cpp--find-other jao-skel-cpp-header-extension) "\""
  > \n \n _
  (jao-skel-cpp--copy-namespace)
  \n)

(define-skeleton jao-skel-c-source-header
  "Insert a standard C source header"
  nil
  '(jao-skel-cpp-header-comment)
  "#include \"" (jao-skel-cpp--find-other "h") "\""
  > _ \n \n \n \n
  > \n)


;; header C/C++ files ------------------------------------------------------
;; header guard

;; class definition
(define-skeleton jao-skel-cpp-class-def
  "Insert a class definition"
  nil
  '(setq v1 (jao-skel-basename))
  > \n
  "/**"
  > \n
  "*"
  > \n
  "*"
  > \n
  "*/"
  > \n
  "class " v1
  > \n
  "{"
  > \n
  "public:"
  > \n
  "~" v1 "();"
  > \n
  v1 "();"
  > \n
  v1 "(const " v1 "& other);"
  > \n \n
  "private:"
  > \n
  "};"
  > \n)

(define-skeleton jao-skel-cpp-header
  "Insert a standard C++ header (hpp files)"
  nil
  '(setq v1 (jao-skel-cpp--get-new-namespace))
  '(setq v2 (funcall jao-skel-cpp-make-guard-function v1))
  '(jao-skel-cpp-header-comment)
  > \n
  "#ifndef " v2
  > \n
  "#define " v2
  > \n \n
  '(when v1 (jao-skel-cpp--insert-open-ns-list v1))
  _ '(jao-skel-cpp-class-def)
  > \n \n
  '(when v1 (jao-skel-cpp--insert-close-ns-list v1))
  > \n \n
  "#endif // " v2
  > \n)

(define-skeleton jao-skel-c-header
  "Insert a standard C header (.h files)"
  nil
  '(jao-skel-cpp-header-comment)
  > \n
  '(setq v1 (funcall jao-skel-cpp-make-guard-function nil))
  "#ifndef " v1
  > \n
  "#define " v1
  > _ \n \n \n \n
  "#endif /* " v1 " */"
  > \n > \n)

(jao-skel-install*
 '(("\\.cpp$" jao-skel-cpp-source-header)
   ("\\.hpp$" jao-skel-cpp-header)
   ("\\.c$" jao-skel-c-source-header)
   ("\\.h$" jao-skel-c-header)))

(provide 'jao-skel-cpp)
;;; jao-skel-cpp.el ends here