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))
55 (loop for arg in (c-parse-template-declaration)
56 for sep = "" then ", "
59 (buffer-substring-no-properties
60 (car arg) (if (c-move-to-initializer
64 (c-backward-syntactic-ws)
70 (defun c-build-defun (&optional add-words no-kill)
71 ;; build a function definition header for the current defun. if
72 ;; ADD-WORDS is non-nil, it is prepended to the definition header
73 ;; after any template specifications. the return value is a cons of
74 ;; the name of the function and the complete text of the header.
75 ;; c-build-defun tries hard to keep the with of the declaration
76 ;; below c-max-def-column
78 (c-beginning-of-defun-or-decl)
79 (let* ((tbuf (get-buffer-create " *cc-temp-buffer*"))
80 (state (c-get-defun-state))
81 (prefix (c-get-full-prefix (aref state 8)))
82 (cbuf (current-buffer))
83 p1 p2 p3 c maxc fname)
86 (c-build-template-specs (aref state 0) cbuf)
90 (insert-buffer-substring cbuf (car (aref state 1)) (cdr (aref state 1)))
94 (c-kill-special-keywords)))))
97 (insert add-words " ")))
98 (goto-char (point-max))
101 (if (> (length prefix) 0)
106 (insert-buffer-substring cbuf (car (aref state 2)) (cdr (aref state 2))))
108 (while (re-search-forward "\\s-+" nil t)
112 (c-with-temporary-syntax-table c-mode-syntax-table
113 (setq p2 (car (last (c-forward-scoped-name))))
115 (setq p2 (+ p2 2))))))
116 (goto-char (point-max))
117 (setq fname (buffer-substring-no-properties p1 (point)))
118 (if (> (current-column) c-max-def-column)
127 (setq c (current-column)
130 (loop for arg in (aref state 3)
131 for next = nil then t
132 if next do (insert ",\n")
135 (insert-buffer-substring cbuf (car arg) (cdr arg)))
137 (if (search-forward "=" nil t)
140 (c-backward-syntactic-ws)
142 (delete-region (1- (point)) (progn (end-of-line) (point))))))
143 (replace-regexp "\\s-+" " ")
145 (let ((cc (current-column)))
148 (if (> (+ c maxc) c-max-def-column)
153 (> (1- (current-column))
154 (- (+ c maxc) c-max-def-column))))
162 (setq p3 (1+ p3)))))))
164 (setq c (current-column))
165 (loop for next = nil then t
169 (if next (insert " "))
172 (if (and next (> (current-column) c-max-def-column))
176 (insert "\n" (make-string c ? ))
183 (insert-buffer-substring cbuf (car (aref state 4)) (cdr (aref state 4))))
184 (replace-regexp "\\s-+" " ")
190 for initializer in (aref state 5)
191 for next = nil then t
194 (if next (insert ", "))
196 (insert-buffer-substring cbuf (car initializer)
198 (replace-regexp "\\s-+" " ")
201 (if (> (current-column) c-max-def-column)
208 (setq first nil))))))
211 (buffer-substring-no-properties (point-min) (point-max))
213 (kill-buffer tbuf)))))
215 (defun c-build-create-constructor ()
217 (c-beginning-of-defun-or-decl)
218 (let* ((tbuf (get-buffer-create " *cc-temp-buffer*"))
219 (cbuf (current-buffer))
220 (state (c-get-defun-state))
221 (indent (make-string (current-indentation) ? ))
222 (fname (buffer-substring-no-properties (car (aref state 2))
223 (cdr (aref state 2)))))
226 (error "Not a constructor"))
227 (insert indent "static ptr create(")
228 (let ((indent2 (make-string (current-column) ? ))
232 (loop for arg in (aref state 3)
233 for next = nil then t
234 if next do (insert ",\n")
237 (insert-buffer-substring cbuf (car arg) (cdr arg)))
238 (replace-regexp "\\s-+" " ")
240 (loop for next = nil then t
244 (if next (insert " "))
247 (if (and next (> (current-column) c-max-def-column))
252 (insert "\n" indent2)
260 (insert-buffer-substring cbuf
262 (cdr (aref state 4))))
263 (replace-regexp "\\s-+" " ")
265 (if (or ml (> (current-column) c-max-def-column))
268 (insert "\n" indent (make-string c-basic-offset ? ))
273 (buffer-substring-no-properties (point-min) (point-max))
275 (kill-buffer tbuf)))))
277 (defun c-build-create-constructor-impl (&optional add-words no-kill)
279 (let* ((proto (c-build-defun add-words no-kill))
280 (cbuf (current-buffer))
281 (tbuf (get-buffer-create " *cc-temp-buffer*"))
282 (indent (make-string c-basic-offset ? )))
285 (insert (nth 1 proto) "\n{\n" indent "return ptr(new "
288 (c-scope-name (aref (car (last (aref (nth 2 proto) 8))) 1)))
290 (let ((indent2 (make-string (current-column) ? )))
293 (loop for arg in (aref (nth 2 proto) 3)
294 for next = nil then t
295 if next do (insert ",\n")
296 do (insert (save-excursion
298 (c-get-template-argument-name (car arg) (cdr arg))))))
299 (loop for next = nil then t
303 (if next (insert " "))
306 (if (and next (> (current-column) c-max-def-column))
310 (insert "\n" indent2)
315 (buffer-substring-no-properties (point-min) (point-max))
317 (kill-buffer tbuf)))))
319 (eval-when-compile (autoload 'ccide-reformat-defun "cc-ide"))
321 (defun c-build-default-funcions-impl ()
323 (let* ((scope (c-get-block-scope))
324 (templates (c-get-templates scope))
325 (prefix (c-get-full-prefix scope))
326 (class (c-parse-class scope))
327 (bases (c-get-base-classes class))
328 (variables (c-get-variable-members class))
329 (name (c-scope-name (aref (car (last scope)) 1)))
330 (in (make-string c-basic-offset ? ))
331 (cbuf (current-buffer))
332 (tbuf (get-buffer-create " *cc-temp-buffer-2*"))
336 (c-build-template-specs templates cbuf)
337 (setq template-specs (buffer-substring (point-min) (point-max)))
339 (insert "prefix_ " prefix "::" name "()\n")
341 (insert in "throw_(())\n"))
344 (ccide-reformat-defun)
345 (goto-char (point-max))
346 (insert template-specs)
348 (insert "prefix_ " prefix "::" name "(const " name "& other)\n")
350 (insert in "throw_(())\n"))
352 (mapconcat (function (lambda (x) (concat x "(other)")))
354 (if (and bases variables) ", " "")
355 (mapconcat (function (lambda (x) (concat x "(other." x ")")))
358 (ccide-reformat-defun)
359 (goto-char (point-max))
360 (insert template-specs)
362 (insert "prefix_ " prefix " & " prefix "::operator=(const "
365 (insert in "throw_(())\n"))
367 (mapconcat (function (lambda (x)
368 (concat in "*((" x "*)this) = other;\n")))
370 (mapconcat (function (lambda (x)
371 (concat in x " = other." x ";\n")))
375 (ccide-reformat-defun)
376 (goto-char (point-max))
377 (insert template-specs)
379 (insert "prefix_ " prefix "::~" name "()\n{}\n"))
380 (ccide-reformat-defun)
383 (buffer-substring-no-properties (point-min) (point-max)))
384 (kill-buffer tbuf)))))
390 ;;; elisp-project-autoload-file-name: "cc-autoload.el"