1 ;;; cc-helper.el --- helper and generator functions for C++
5 ;; Copyright (C) 2000 Stefan Bund
7 ;; cc-helper.el is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation; either version 2, or (at
10 ;; your option) any later version.
12 ;; cc-helper.el is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;; General Public License for more details.
26 (defvar c-max-def-column 95
27 "*Maximum length of generated argdef lines")
29 (defconst c-special-key "\\(static\\|virtual\\|friend\\|explicit\\)\\b")
33 (require 'cc-engine-2)
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; generators (functions generating new sourcecode)
39 (defun c-kill-special-keywords ()
40 ;; kill all keywords in c-special-key directly after point
41 (c-forward-syntactic-ws)
42 (while (looking-at c-special-key)
43 (delete-region (point) (match-end 1))
44 (delete-region (point) (progn (c-forward-syntactic-ws) (point)))))
46 (defun c-build-template-specs (templates cbuf)
47 ;; build temlate specs for TEMPLATES
48 (loop for template in templates
53 (goto-char (car template))
54 (let ((args (c-parse-template-declaration)))
58 for sep = "" then ", "
61 (buffer-substring-no-properties
62 (car arg) (if (c-move-to-initializer
66 (c-backward-syntactic-ws)
72 (defun c-build-defun (&optional add-words no-kill)
73 ;; build a function definition header for the current defun. if
74 ;; ADD-WORDS is non-nil, it is prepended to the definition header
75 ;; after any template specifications. the return value is a cons of
76 ;; the name of the function and the complete text of the header.
77 ;; c-build-defun tries hard to keep the with of the declaration
78 ;; below c-max-def-column
80 (c-beginning-of-defun-or-decl)
81 (let* ((tbuf (get-buffer-create " *cc-temp-buffer*"))
82 (state (c-get-defun-state))
83 (prefix (c-get-full-prefix (aref state 8)))
84 (cbuf (current-buffer))
85 p1 p2 p3 c maxc fname)
88 (c-build-template-specs (aref state 0) cbuf)
92 (insert-buffer-substring cbuf (car (aref state 1)) (cdr (aref state 1)))
96 (c-kill-special-keywords)))))
99 (insert add-words " ")))
100 (goto-char (point-max))
103 (if (> (length prefix) 0)
108 (insert-buffer-substring cbuf (car (aref state 2)) (cdr (aref state 2))))
110 (while (re-search-forward "\\s-+" nil t)
114 (c-with-temporary-syntax-table c-mode-syntax-table
115 (setq p2 (car (last (c-forward-scoped-name))))
117 (setq p2 (+ p2 2))))))
118 (goto-char (point-max))
119 (setq fname (buffer-substring-no-properties p1 (point)))
120 (if (> (current-column) c-max-def-column)
129 (setq c (current-column)
132 (loop for arg in (aref state 3)
133 for next = nil then t
134 if next do (insert ",\n")
137 (insert-buffer-substring cbuf (car arg) (cdr arg)))
139 (if (search-forward "=" nil t)
142 (c-backward-syntactic-ws)
144 (delete-region (1- (point)) (progn (end-of-line) (point))))))
145 (replace-regexp "\\s-+" " ")
147 (let ((cc (current-column)))
150 (if (> (+ c maxc) c-max-def-column)
155 (> (1- (current-column))
156 (- (+ c maxc) c-max-def-column))))
164 (setq p3 (1+ p3)))))))
166 (setq c (current-column))
167 (loop for next = nil then t
171 (if next (insert " "))
174 (if (and next (> (current-column) c-max-def-column))
178 (insert "\n" (make-string c ? ))
185 (insert-buffer-substring cbuf (car (aref state 4)) (cdr (aref state 4))))
186 (replace-regexp "\\s-+" " ")
192 for initializer in (aref state 5)
193 for next = nil then t
196 (if next (insert ", "))
198 (insert-buffer-substring cbuf (car initializer)
200 (replace-regexp "\\s-+" " ")
203 (if (> (current-column) c-max-def-column)
210 (setq first nil))))))
213 (buffer-substring-no-properties (point-min) (point-max))
215 (kill-buffer tbuf)))))
217 (defun c-build-create-constructor ()
219 (c-beginning-of-defun-or-decl)
220 (let* ((tbuf (get-buffer-create " *cc-temp-buffer*"))
221 (cbuf (current-buffer))
222 (state (c-get-defun-state))
223 (indent (make-string (current-indentation) ? ))
224 (fname (buffer-substring-no-properties (car (aref state 2))
225 (cdr (aref state 2)))))
228 (error "Not a constructor"))
229 (insert indent "static ptr create(")
230 (let ((indent2 (make-string (current-column) ? ))
234 (loop for arg in (aref state 3)
235 for next = nil then t
236 if next do (insert ",\n")
239 (insert-buffer-substring cbuf (car arg) (cdr arg)))
240 (replace-regexp "\\s-+" " ")
242 (loop for next = nil then t
246 (if next (insert " "))
249 (if (and next (> (current-column) c-max-def-column))
254 (insert "\n" indent2)
262 (insert-buffer-substring cbuf
264 (cdr (aref state 4))))
265 (replace-regexp "\\s-+" " ")
267 (if (or ml (> (current-column) c-max-def-column))
270 (insert "\n" indent (make-string c-basic-offset ? ))
275 (buffer-substring-no-properties (point-min) (point-max))
277 (kill-buffer tbuf)))))
279 (defun c-build-create-constructor-impl (&optional add-words no-kill)
281 (let* ((proto (c-build-defun add-words no-kill))
282 (cbuf (current-buffer))
283 (tbuf (get-buffer-create " *cc-temp-buffer*"))
284 (indent (make-string c-basic-offset ? )))
287 (insert (nth 1 proto) "\n{\n" indent "return ptr(new "
290 (c-scope-name (aref (car (last (aref (nth 2 proto) 8))) 1)))
292 (let ((indent2 (make-string (current-column) ? )))
295 (loop for arg in (aref (nth 2 proto) 3)
296 for next = nil then t
297 if next do (insert ",\n")
298 do (insert (save-excursion
300 (c-get-template-argument-name (car arg) (cdr arg))))))
301 (loop for next = nil then t
305 (if next (insert " "))
308 (if (and next (> (current-column) c-max-def-column))
312 (insert "\n" indent2)
317 (buffer-substring-no-properties (point-min) (point-max))
319 (kill-buffer tbuf)))))
321 (eval-when-compile (autoload 'ccide-reformat-defun "cc-ide"))
323 (defun c-build-default-funcions-impl ()
325 (let* ((scope (c-get-block-scope))
326 (templates (c-get-templates scope))
327 (prefix (c-get-full-prefix scope))
328 (class (c-parse-class scope))
329 (bases (c-get-base-classes class))
330 (variables (c-get-variable-members class))
331 (name (c-scope-name (aref (car (last scope)) 1)))
332 (in (make-string c-basic-offset ? ))
333 (cbuf (current-buffer))
334 (tbuf (get-buffer-create " *cc-temp-buffer-2*"))
338 (c-build-template-specs templates cbuf)
339 (setq template-specs (buffer-substring (point-min) (point-max)))
341 (insert "prefix_ " prefix "::" name "()\n")
343 (insert in "throw_(())\n"))
346 (ccide-reformat-defun)
347 (goto-char (point-max))
348 (insert template-specs)
350 (insert "prefix_ " prefix "::" name "(const " name "& other)\n")
352 (insert in "throw_(())\n"))
354 (mapconcat (function (lambda (x) (concat x "(other)")))
356 (if (and bases variables) ", " "")
357 (mapconcat (function (lambda (x) (concat x "(other." x ")")))
360 (ccide-reformat-defun)
361 (goto-char (point-max))
362 (insert template-specs)
364 (insert "prefix_ " prefix " & " prefix "::operator=(const "
367 (insert in "throw_(())\n"))
369 (mapconcat (function (lambda (x)
370 (concat in "*((" x "*)this) = other;\n")))
372 (mapconcat (function (lambda (x)
373 (concat in x " = other." x ";\n")))
377 (ccide-reformat-defun)
378 (goto-char (point-max))
379 (insert template-specs)
381 (insert "prefix_ " prefix "::~" name "()\n{}\n"))
382 (ccide-reformat-defun)
385 (buffer-substring-no-properties (point-min) (point-max)))
386 (kill-buffer tbuf)))))
392 ;;; elisp-project-autoload-file-name: "cc-autoload.el"