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)
89 (c-build-template-specs (aref state 0) cbuf)
93 (insert-buffer-substring cbuf (car (aref state 1)) (cdr (aref state 1)))
97 (c-kill-special-keywords)))))
100 (insert add-words " ")))
101 (goto-char (point-max))
104 (if (> (length prefix) 0)
109 (insert-buffer-substring cbuf (car (aref state 2)) (cdr (aref state 2))))
111 (while (re-search-forward "\\s-+" nil t)
115 (c-with-temporary-syntax-table c-mode-syntax-table
116 (setq p2 (car (last (c-forward-scoped-name))))
118 (setq p2 (+ p2 2))))))
119 (goto-char (point-max))
120 (setq fname (buffer-substring-no-properties p1 (point)))
121 (if (> (current-column) c-max-def-column)
130 (setq c (current-column)
133 (loop for arg in (aref state 3)
134 for next = nil then t
135 if next do (insert ",\n")
138 (insert-buffer-substring cbuf (car arg) (cdr arg)))
140 (if (search-forward "=" nil t)
143 (c-backward-syntactic-ws)
145 (delete-region (1- (point)) (progn (end-of-line) (point))))))
146 (replace-regexp "\\s-+" " ")
148 (let ((cc (current-column)))
151 (if (> (+ c maxc) c-max-def-column)
156 (> (1- (current-column))
157 (- (+ c maxc) c-max-def-column))))
165 (setq p3 (1+ p3)))))))
167 (setq c (current-column))
168 (loop for next = nil then t
172 (if next (insert " "))
175 (if (and next (> (current-column) c-max-def-column))
179 (insert "\n" (make-string c ? ))
186 (insert-buffer-substring cbuf (car (aref state 4)) (cdr (aref state 4))))
187 (replace-regexp "\\s-+" " ")
193 for initializer in (aref state 5)
194 for next = nil then t
197 (if next (insert ", "))
199 (insert-buffer-substring cbuf (car initializer)
201 (replace-regexp "\\s-+" " ")
204 (if (> (current-column) c-max-def-column)
211 (setq first nil))))))
214 (buffer-substring-no-properties (point-min) (point-max))
216 (kill-buffer tbuf)))))
218 (defun c-build-create-constructor ()
220 (c-beginning-of-defun-or-decl)
221 (let* ((tbuf (get-buffer-create " *cc-temp-buffer*"))
222 (cbuf (current-buffer))
223 (state (c-get-defun-state))
224 (indent (make-string (current-indentation) ? ))
225 (fname (buffer-substring-no-properties (car (aref state 2))
226 (cdr (aref state 2)))))
229 (error "Not a constructor"))
230 (insert indent "static ptr create(")
231 (let ((indent2 (make-string (current-column) ? ))
235 (loop for arg in (aref state 3)
236 for next = nil then t
237 if next do (insert ",\n")
240 (insert-buffer-substring cbuf (car arg) (cdr arg)))
241 (replace-regexp "\\s-+" " ")
243 (loop for next = nil then t
247 (if next (insert " "))
250 (if (and next (> (current-column) c-max-def-column))
255 (insert "\n" indent2)
263 (insert-buffer-substring cbuf
265 (cdr (aref state 4))))
266 (replace-regexp "\\s-+" " ")
268 (if (or ml (> (current-column) c-max-def-column))
271 (insert "\n" indent (make-string c-basic-offset ? ))
276 (buffer-substring-no-properties (point-min) (point-max))
278 (kill-buffer tbuf)))))
280 (defun c-build-create-constructor-impl (&optional add-words no-kill)
282 (let* ((proto (c-build-defun add-words no-kill))
283 (cbuf (current-buffer))
284 (tbuf (get-buffer-create " *cc-temp-buffer*"))
285 (indent (make-string c-basic-offset ? )))
288 (insert (nth 1 proto) "\n{\n" indent "return ptr(new "
291 (c-scope-name (aref (car (last (aref (nth 2 proto) 8))) 1)))
293 (let ((indent2 (make-string (current-column) ? )))
296 (loop for arg in (aref (nth 2 proto) 3)
297 for next = nil then t
298 if next do (insert ",\n")
299 do (insert (save-excursion
301 (c-get-template-argument-name (car arg) (cdr arg))))))
302 (loop for next = nil then t
306 (if next (insert " "))
309 (if (and next (> (current-column) c-max-def-column))
313 (insert "\n" indent2)
318 (buffer-substring-no-properties (point-min) (point-max))
320 (kill-buffer tbuf)))))
322 (eval-when-compile (autoload 'ccide-reformat-defun "cc-ide"))
324 (defun c-build-default-funcions-impl ()
326 (let* ((scope (c-get-block-scope))
327 (templates (c-get-templates scope))
328 (prefix (c-get-full-prefix scope))
329 (class (c-parse-class scope))
330 (bases (c-get-base-classes class))
331 (variables (c-get-variable-members class))
332 (name (c-scope-name (aref (car (last scope)) 1)))
333 (in (make-string c-basic-offset ? ))
334 (cbuf (current-buffer))
335 (tbuf (get-buffer-create " *cc-temp-buffer-2*"))
339 (c-build-template-specs templates cbuf)
340 (setq template-specs (buffer-substring (point-min) (point-max)))
342 (insert "prefix_ " prefix "::" name "()\n")
344 (insert in "throw_(())\n"))
347 (ccide-reformat-defun)
348 (goto-char (point-max))
349 (insert template-specs)
351 (insert "prefix_ " prefix "::" name "(const " name "& other)\n")
353 (insert in "throw_(())\n"))
355 (mapconcat (function (lambda (x) (concat x "(other)")))
357 (if (and bases variables) ", " "")
358 (mapconcat (function (lambda (x) (concat x "(other." x ")")))
361 (ccide-reformat-defun)
362 (goto-char (point-max))
363 (insert template-specs)
365 (insert "prefix_ " prefix " & " prefix "::operator=(const "
368 (insert in "throw_(())\n"))
370 (mapconcat (function (lambda (x)
371 (concat in "*((" x "*)this) = other;\n")))
373 (mapconcat (function (lambda (x)
374 (concat in x " = other." x ";\n")))
378 (ccide-reformat-defun)
379 (goto-char (point-max))
380 (insert template-specs)
382 (insert "prefix_ " prefix "::~" name "()\n{}\n"))
383 (ccide-reformat-defun)
386 (buffer-substring-no-properties (point-min) (point-max)))
387 (kill-buffer tbuf)))))
393 ;;; elisp-project-autoload-file-name: "cc-autoload.el"