Kde/WWatch: Fix gimpc.sh
[emacsstuff.git] / cc-ide / cc-ide.el
1 ;;; cc-ide.el --- C++ IDE
2 ;;
3 ;; $Id$
4 ;;
5 ;; Copyright (C) 2000 Stefan Bund
6
7 ;; cc-ide.el is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
11
12 ;; cc-ide.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.
16
17 ;;; Commentary:
18
19 ;;; Change-Log:
20
21 ;; $Log$
22 ;;
23
24 ;;; Variables:
25
26 (defvar ccide-compile-opts "DEBUG=1"
27   "*Additional options to make command")
28
29 (defvar ccide-file-vars nil)
30
31 (defvar ccide-default-author "")
32 (defvar ccide-default-copyright "")
33
34 (defvar ccide-all-includes-guard nil)
35
36 (defvar ccide-corba-skel-dir "")
37 (defvar ccide-corba-idl-dir "")
38 (defvar ccide-corba-idl-command "omniidl2 -w")
39
40 (defvar c-user-prefixes '("inline" "static" "prefix_")
41   "*List of possible prefixes for function definitions.")
42
43 (defvar ccide-default-prefix "prefix_"
44   "*Prefix added to every implementation header. Probably eiter empty or 'prefix_'")
45
46 (defvar ccide-gen-throw nil
47   "*If non-nil, generate throw_ specs")
48
49 (defvar ccide-project-root)
50
51 (defconst c-user-prefix-re (regexp-opt c-user-prefixes t))
52
53 (defconst ccide-doxy-tag-re 
54   (concat "\\\\\\(group\\|defgroup\\|see\\|author\\|version\\|id\\|since"
55           "\\|returns?\\|throws?\\|exception\\|raises\\|param\\|li\\|brief"
56           "\\|internal\\|bug\\|fixme\\|todo\\|idea\\|implementation"
57           "\\|note\\|attention\\|warning\\|par\\|code\\|endcode"
58           "\\|post\\|pre\\)\\b"))
59
60 (defconst ccide-special-extensions
61   '(".h" ".hh" ".mpp" ".ih" ".cc" ".cpp" ".ct" ".cti" ".cci" ".dox"))
62
63 (defconst ccide-implementation-extensions
64   '(".h" ".hh" ".ih" ".cc" ".cpp" ".ct" ".cti" ".cci"))
65
66 (defconst ccide-class-defaults-word 
67   "// \\(default\\|no\\|protected\\|private\\|disabled\\|my\\)")
68
69 (defconst  ccide-bindings
70   '(
71     ;; file level
72     ("fc"  ccide-file-comment                 "File comment")
73     ("fs"  ccide-syncronize-includes          "Sync includes")
74     (nil   nil                                separator)
75                                           
76     ;; class level                        
77     ("cc"  ccide-class-comment                "Class comment")
78     ("cg"  ccide-gen-class                    "Generate class")
79     ("cd"  ccide-gen-class-defaults           "Generate class defaults")
80     ("cD"  ccide-gen-class-defaults-impl      "Generate class defaults impl")
81
82     ("csd" ccide-set-class-defaults-default   "Set class defaults comment" "Default")
83     ("csn" ccide-set-class-defaults-no        "Set class defaults comment" "No")
84     ("csp" ccide-set-class-defaults-protected "Set class defaults comment" "Protected")
85     ("csr" ccide-set-class-defaults-private   "Set class defaults comment" "Private")
86     ("csx" ccide-set-class-defaults-disabled  "Set class defaults comment" "Disabled")
87     ("csm" ccide-set-class-defaults-my        "Set class defaults comment" "My")
88
89     ("cS"  ccide-gen-struct-constructors      "Generate structure constructors")
90
91     ("ci"  ccide-class-impl-comment           "Generate class implemenation comment")
92
93     ("ce"  ccide-gen-exception                "Generate an exception class")
94
95     (nil   nil                                separator)
96                                           
97     ;; method level                       
98     ("mc"  ccide-function-comment             "Method comment")
99     ("mp"  ccide-grab-prototype               "Grab prototype")
100     ("mr"  ccide-reformat-defun               "Reformat defun")
101     ("mx"  ccide-replace-defun                "Replace defun")
102     ("mt"  ccide-prefix-defun-type-with-class "Prefix defun type with class")
103     ("mn"  ccide-prefix-defun-type-with-namespace "Prefix defun type with namespace")
104     ("mi"  ccide-grab-inline-decl             "Grab inline decl")
105     ("mA"  ccide-grab-all-inlines             "Grab ALL inline decls")
106     ("mC"  ccide-grab-create-constructor      "Grab CREATE constructor")
107     ("mI"  ccide-grab-create-constructor-impl "Build CREATE cosntructor")
108     ("mf"  ccide-find-implementation          "Find method implementation")
109     ("mT"  ccide-insert-defun-prefix          "Insert current defun prefix at point")
110     (nil   nil                                separator)
111                                           
112     ;; variable level                     
113     ("vc"  ccide-variable-comment             "Variable comment")
114     ("vf"  ccide-grab-access-fn               "Grab access methods")
115     (nil   nil                                separator)
116
117     ;; documentation
118     ("h"  ccide-hide-all-doxy-comments        "Hide all Doxygen comments")
119     ("s"  ccide-show-all-comments             "Show all Doxygen comments")
120                                           
121 ;    ;; CORBA                             
122 ;    ("Cg"  ccide-gen-corba-impl                      "Generate CORBA impl")
123 ;    ("Cm"  ccide-gen-corba-impl-methods       "Generate CORBA impl methods")
124 ;    (nil   nil                               separator)
125                                           
126     ;; templates                          
127 ;    ("ts"  ccide-scan-mantemps               "Scan mantemps")
128 ;    (nil   nil                                separator)
129
130 ;    ;; other
131 ;    ("of"  ccide-open-compilation-frame       "Open *compilation* frame")
132 ;    ("oc"  ccide-compile-compile              "Make -> Compile")
133 ;    ("ox"  ccide-compile-clean                "Make -> Clean")
134 ;    ("od"  ccide-compile-cleandepends         "Make -> Clean depends")
135 ;    ("ok"  ccide-compile-kill                 "Kill compilation")
136 ;    ("oh"  ccide-hide-compilation             "Hide *compilation* buffer")
137
138     ))
139
140 ;;; Code:
141
142 (require 'cc-engine-2)
143 (require 'cc-helper)
144 (require 'c++)
145 (require 'cl)
146 (require 'hideshow)
147 ;(require 'mantemp)
148 (require 'locate)
149 (require 'lucid)
150 (require 'varcmd)
151 (require 'misc-local)
152
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154 ;; utils
155
156 (defsubst ccide-match-string (n)
157   (buffer-substring-no-properties (match-beginning n) (match-end n)))
158
159 (defun ccide-file-macro-name (&optional file-name)
160   (concat (upcase (file-name-extension (or file-name (buffer-file-name))))
161           "_" 
162           (string-replace "\\." "_" (file-name-sans-extension 
163                                      (file-name-nondirectory 
164                                       (or file-name (buffer-file-name))))
165                           t nil t t)
166           "_"))
167
168 (defun ccide-file-name (&optional extension file-name directory)
169   (concat (if directory (file-name-as-directory directory) "")
170           (file-name-sans-extension
171            (file-name-nondirectory 
172             (or file-name (buffer-file-name))))
173           extension))
174
175 (defun ccide-in-doxy-comment ()
176   (save-excursion
177     (back-to-indentation)
178     (let ((lit (c-in-literal)))
179       (if (cond ((eq lit 'c)
180                  (goto-char (car (c-literal-limits)))
181                  (looking-at "/\\*\\*<?[ \t\n\r@]"))
182                 ((eq lit 'c++)
183                  (goto-char (car (c-literal-limits)))
184                  (looking-at "///<?[ \t\n\r@]"))
185                 (t nil))
186           (progn
187             (goto-char (match-end 0))
188             (current-column))))))
189
190 (defun ccide-shell-command (command)
191   (let ((obuf (get-buffer-create "*ccide shell command*"))
192         exit-status)
193     (save-excursion
194       (set-buffer obuf)
195       (erase-buffer)
196       (insert command "\n"))
197     (setq exit-status (call-process shell-file-name nil "*ccide shell command*" nil
198                                     shell-command-switch command))
199     (and exit-status (equal exit-status 0))))
200
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;; file level
203
204 (defun ccide-file-comment ()
205   "Add a comment to this source file."
206   (interactive)
207   (let ((mode "c++")
208         point add-file-vars)
209     (push-mark)
210     (goto-char (point-min))
211     (insert "// Copyright (C) " (number-to-string (nth 5 (decode-time)))
212             " " ccide-default-author "\n"
213             ccide-default-copyright
214             "\n")
215
216     (cond ((string-match "\\.hh?$" (buffer-file-name))
217            (insert "/** \\file\n"
218                    "    \\brief " (ccide-file-name) " public header */\n\n"
219                    "#ifndef " (ccide-file-macro-name) "\n"
220                    "#define " (ccide-file-macro-name) " 1\n\n"
221                    "// Custom includes\n\n"
222                    "//#include \"" (ccide-file-name ".mpp") "\"\n"
223                    "///////////////////////////////hh.p////////////////////////////////////////\n\n")
224            (setq point (point))
225            (goto-char (point-max))
226            (insert "\n\n///////////////////////////////hh.e////////////////////////////////////////\n")
227            (if ccide-all-includes-guard
228                (insert "#endif\n"
229                        "#if !defined(" ccide-all-includes-guard ") && !defined(" (ccide-file-macro-name) "i_)\n"
230                        "#define " (ccide-file-macro-name) "i_\n"))
231            (insert "//#include \"" (ccide-file-name ".cci") "\"\n"
232                    "//#include \"" (ccide-file-name ".ct") "\"\n"
233                    "//#include \"" (ccide-file-name ".cti") "\"\n"
234                    "#endif"))
235
236           ((string-match "\\.mpp$" (buffer-file-name))
237            (insert "/** \\file\n"
238                    "    \\brief " (ccide-file-name) " Boost.Preprocesser external iteration include */\n\n"
239
240
241                    "#if !BOOST_PP_IS_ITERATING && !defined(" (ccide-file-macro-name) ")\n"
242                    "#define " (ccide-file-macro-name) " 1\n\n"
243                    "// Custom includes\n\n\n"
244                    "// ///////////////////////////mpp.p////////////////////////////////////////\n"
245                    "#elif BOOST_PP_IS_ITERATING // ////////////////////////////////////////////\n"
246                    "// ////////////////////////////////////////////////////////////////////////\n"
247                    "// Local Macros\n\n\n"
248                    "// ////////////////////////////////////////////////////////////////////////\n"
249                    "#if BOOST_PP_ITERATION_FLAGS()==1 // //////////////////////////////////////\n"
250                    "// ////////////////////////////////////////////////////////////////////////\n\n")
251            (setq point (point))
252            (goto-char (point-max))
253            (insert "\n\n// ////////////////////////////////////////////////////////////////////////\n"
254                    "#endif // /////////////////////////////////////////////////////////////////\n"
255                    "// ////////////////////////////////////////////////////////////////////////\n"
256                    "// Undefine local Macros\n\n\n"
257                    "// ////////////////////////////////////////////////////////////////////////\n"
258                    "#endif // /////////////////////////////////////////////////////////////////\n"
259                    "// ///////////////////////////mpp.e////////////////////////////////////////"))
260
261           ((string-match "\\.ih$" (buffer-file-name))
262            (insert "/** \\file\n"
263                    "    \\brief " (ccide-file-name) " internal header */\n\n"
264                    "#ifndef " (ccide-file-macro-name) "\n"
265                    "#define " (ccide-file-macro-name) " 1\n\n"
266                    "// Custom includes\n\n"
267                    "///////////////////////////////ih.p////////////////////////////////////////\n\n")
268            (setq point (point))
269            (goto-char (point-max))
270            (insert "\n\n///////////////////////////////ih.e////////////////////////////////////////\n"
271                    "#endif"))
272
273           ((or (string-match "\\.test\\.cc$" (buffer-file-name))
274                (string-match "\\.test\\.cpp$" (buffer-file-name)))
275            (insert "/** \\file\n"
276                    "    \\brief " (ccide-file-name) " unit tests */\n\n"
277                    "//#include \"" (ccide-file-name ".hh") "\"\n"
278                    "//#include \"" (ccide-file-name ".ih") "\"\n\n"
279                    "// Custom includes\n"
280                    "#include \"" (ccide-file-name ".hh" (ccide-file-name)) "\"\n\n"
281                    "#include <boost/test/auto_unit_test.hpp>\n"
282                    "#include <boost/test/test_tools.hpp>\n\n"
283                    "#define prefix_\n"
284                    "///////////////////////////////cc.p////////////////////////////////////////\n\n")
285            (setq point (point))
286            (goto-char (point-max))
287            (insert "\n\n///////////////////////////////cc.e////////////////////////////////////////\n"
288                    "#undef prefix_"))
289
290           ((or (string-match "\\.cc$" (buffer-file-name))
291                (string-match "\\.cpp$" (buffer-file-name)))
292            (insert "/** \\file\n"
293                    "    \\brief " (ccide-file-name) " non-inline non-template implementation */\n\n"
294                    "//#include \"" (ccide-file-name ".hh") "\"\n"
295                    "//#include \"" (ccide-file-name ".ih") "\"\n\n"
296                    "// Custom includes\n\n"
297                    "//#include \"" (ccide-file-name ".mpp") "\"\n"
298                    "#define prefix_\n"
299                    "///////////////////////////////cc.p////////////////////////////////////////\n\n")
300            (setq point (point))
301            (goto-char (point-max))
302            (insert "\n\n///////////////////////////////cc.e////////////////////////////////////////\n"
303                    "#undef prefix_\n"
304                    "//#include \"" (ccide-file-name ".mpp") "\""))
305
306           ((string-match "\\.cci$" (buffer-file-name))
307            (insert "/** \\file\n"
308                    "    \\brief " (ccide-file-name) " inline non-template implementation */\n\n"
309                    "// Custom includes\n\n"
310                    "#define prefix_ inline\n"
311                    "///////////////////////////////cci.p///////////////////////////////////////\n\n")
312            (setq point (point))
313            (goto-char (point-max))
314            (insert "\n\n///////////////////////////////cci.e///////////////////////////////////////\n"
315                    "#undef prefix_"))
316
317           ((string-match "\\.ct$" (buffer-file-name))
318            (insert "/** \\file\n"
319                    "    \\brief " (ccide-file-name) " non-inline template implementation  */\n\n"
320                    "//#include \"" (ccide-file-name ".ih") "\"\n\n"
321                    "// Custom includes\n\n"
322                    "#define prefix_\n"
323                    "///////////////////////////////ct.p////////////////////////////////////////\n\n")
324            (setq point (point))
325            (goto-char (point-max))
326            (insert "\n\n///////////////////////////////ct.e////////////////////////////////////////\n"
327                    "#undef prefix_"))
328
329           ((string-match "\\.cti$" (buffer-file-name))
330            (insert "/** \\file\n"
331                    "    \\brief " (ccide-file-name) " inline template implementation */\n\n"
332                    "//#include \"" (ccide-file-name ".ih") "\"\n\n"
333                    "// Custom includes\n\n"
334                    "#define prefix_ inline\n"
335                    "///////////////////////////////cti.p///////////////////////////////////////\n\n")
336            (setq point (point))
337            (goto-char (point-max))
338            (insert "\n\n///////////////////////////////cti.e///////////////////////////////////////\n"
339                    "#undef prefix_"))
340
341           ((string-match "\\.dox$" (buffer-file-name))
342            (insert "/** \\mainpage\n\n    ")
343            (setq point (point))
344            (insert "\n */")
345            (setq add-file-vars '(( mode . flyspell)
346                                  ( mode . auto-fill))))
347
348           ((string-match "\\.java$" (buffer-file-name))
349            (setq mode "jde")
350            (setq point (point))
351            (goto-char (point-max)))
352
353           (t
354            (setq point (point))
355            (goto-char (point-max))))
356
357     (insert "\n\n\f\n"
358             "// Local Variables:\n"
359             "// mode: " mode "\n")
360     (loop for (var . value) in ccide-file-vars
361           do (insert "// " (symbol-name var) ": " (prin1-to-string value) "\n"))
362     (loop for (var . value) in add-file-vars
363           do (insert "// " (symbol-name var) ": " (prin1-to-string value) "\n"))
364     (insert "// End:\n")
365     (if point
366         (goto-char point))
367     (if (equal mode "jde")
368         (let ((package (file-name-directory (buffer-file-name))))
369           (jdeap-initialize-setup)
370           (if (not (equal jdeap-current-source-directory "."))
371               (if (string-match 
372                    (concat "^" (regexp-quote jdeap-current-source-directory))
373                    package)
374                   (progn
375                     (setq package (substring package 
376                                              (match-end 0)
377                                              (1- (length package))))
378                     (insert "package "
379                             (string-replace "/" "." package t)
380                             ";\n\n"))))
381           (insert "class " (file-name-sans-extension
382                             (file-name-nondirectory 
383                              (buffer-file-name))) "\n{}")
384           (beginning-of-line)))))
385
386 (defun ccide-sync-file-variables ()
387   "Syncronize file variables to the current value of ccide-file-vars"
388   (interactive)
389   (save-excursion
390     (goto-char (point-max))
391     (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
392     (let ((case-fold-search t))
393       (if (search-forward "Local Variables:" nil t)
394           (let (prefix suffix vars)
395             (skip-chars-forward " \t")
396             (or (eolp)
397                 (setq suffix (buffer-substring (point) (progn (end-of-line) (point)))))
398             (goto-char (match-beginning 0))
399             (or (bolp)
400                 (setq prefix (buffer-substring (point) (progn (beginning-of-line) (point)))))
401             (loop do (progn
402                        (forward-line 1)
403                        (if (and prefix (looking-at prefix))
404                            (goto-char (match-end 0)))
405                        (skip-chars-forward " \t"))
406                   while (not (looking-at "end:"))
407                   do (progn
408                        (setq vars (cons (intern (buffer-substring 
409                                                  (point) 
410                                                  (progn (skip-chars-forward "^:\n") (point))))
411                                         vars))))
412             (beginning-of-line)
413             (loop for (var . value) in ccide-file-vars
414                   do (if (not (memq var vars))
415                          (insert (or prefix "")
416                                  (symbol-name var) ": " (prin1-to-string value) 
417                                  (or suffix "") "\n"))))))))
418
419 (defun ccide-syncronize-includes ()
420   "Syncronize include's in all other files"
421   (interactive)
422   (let (buffer-map)
423     (loop for extension in ccide-special-extensions
424           for file-name = (ccide-file-name extension)
425           do (setq buffer-map
426                    (cons (cons file-name
427                                (or (find-buffer-visiting file-name)
428                                    (and (file-readable-p file-name)
429                                         (find-file-noselect file-name))))
430                          buffer-map)))
431     (save-excursion
432       (loop for buffer in buffer-map
433             if (cdr buffer)
434               do (progn 
435                    (set-buffer (cdr buffer))
436                    (save-excursion
437                      (loop for include in buffer-map
438                            do (progn 
439                                 (goto-char (point-min))
440                                 (while (re-search-forward 
441                                         (concat "^\\(//\\)?#\\s-*include \""
442                                                 (regexp-quote (car include))
443                                                 "\"\\s-*$")
444                                         nil t)
445                                   (goto-char (match-beginning 0))
446                                   (if (looking-at "//")
447                                       (if (cdr include)
448                                           (delete-char 2))
449                                     (if (not (cdr include))
450                                         (insert "//")))
451                                   (forward-line 1))))))))))
452
453 (defun ccide-auto-decorate-new-files ()
454   (if (= (point-min) (point-max))
455       (let ((status (buffer-modified-p)))
456         (ccide-file-comment)
457         (set-buffer-modified-p status))))
458
459 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
460 ;; class level
461
462 (defun ccide-class-comment ()
463   "Add comment to start of current class definition"
464   (interactive)
465   (let ((class (c-get-class-at-point)))
466     (if (not class)
467         (error "No class found")
468       (goto-char (or (aref (car class) 3)
469                      (aref (car class) 1)))
470       (if (save-excursion 
471             (forward-line -1)
472             (ccide-in-doxy-comment))
473           (progn
474             (search-backward "/**" nil t)
475             (forward-char 4))
476         (let ((indent (make-string (current-indentation) ? )))
477           (insert "/** \\brief  ")
478           (save-excursion
479             (insert "\n"
480                     indent " */\n"
481                     indent)))))))
482
483 (defun ccide-gen-class (name &optional defns)
484   "Generate class declaration template"
485   (interactive (list (read-string (concat "Class name (default "
486                                           (ccide-file-name)
487                                           "): ")
488                                   nil nil (ccide-file-name))))
489   (insert "class " name)
490   (indent-according-to-mode)
491   (let ((in (make-string c-basic-offset ? ))
492         (ofs (make-string (current-indentation) ? )))
493     (save-excursion
494       (beginning-of-line)
495       (open-line 1)
496       (insert ofs "/** \\brief\n"
497               ofs "  */"))
498     (insert "\n" ofs)
499     (save-excursion
500       (insert "{\n"
501               ofs "public:\n"
502               ofs in "///////////////////////////////////////////////////////////////////////////\n"
503               ofs in "// Types\n\n"
504               ofs in "///////////////////////////////////////////////////////////////////////////\n"
505               ofs in "///\\name Structors and default members\n"
506               ofs in "///@{\n\n"
507               ofs in "// default default constructor\n"
508               ofs in "// default copy constructor\n"
509               ofs in "// default copy assignment\n"
510               ofs in "// default destructor\n\n"
511               ofs in "// no conversion constructors\n\n"
512               ofs in "///@}\n"
513               ofs in "///////////////////////////////////////////////////////////////////////////\n"
514               ofs in "///\\name Accessors\n"
515               ofs in "///@{\n\n"
516               ofs in "///@}\n"
517               ofs in "///////////////////////////////////////////////////////////////////////////\n"
518               ofs in "///\\name Mutators\n"
519               ofs in "///@{\n\n"
520               ofs in "///@}\n\n")
521       (loop for defn in defns
522             do (insert ofs in defn ";\n"))
523       (if defns
524           (insert "\n"))
525       (insert ofs "protected:\n\n"
526               ofs "private:\n\n"
527               ofs "};\n"))))
528
529 (defun ccide-gen-class-defaults ()
530   "Generate signatures of the default functions: default constructor,
531 copy constructor, assignment operator and destructor."
532   (indent-according-to-mode)
533   (let* ((name (c-scope-name (aref (car (c-get-class-at-point)) 1)))
534          (in (make-string c-basic-offset ? ))
535          (ofs (make-string (current-indentation) ? ))
536          (tspec (if ccide-gen-throw (concat "\n" ofs in "throw_(());\n") ";\n"))
537          (colon 0))
538     (while (string-match "::" name colon)
539       (setq colon (match-end 0)))
540     (setq name (substring name colon))
541     (beginning-of-line)
542     (delete-horizontal-space)
543     (loop with exit = nil
544           do (message (concat "1-dflt constr, 2-destr, "
545                               "3-copy constr, 4-copy assmnt, "
546                               "c-all copy, d-all dflt, RET-all/done: "))
547           for ch = (read-event)
548           for first = t then nil
549           do (cond ((eq ch 'return)
550                     (if first
551                         (insert ofs name "()" 
552                                 tspec
553                                 ofs name "(const " name "& other)" 
554                                 tspec
555                                 ofs "~" name "();\n"
556                                 ofs name "& operator=(const " name "& other)"
557                                 tspec))
558                     (setq exit t))
559                    ((eq ch ?1)
560                     (insert ofs name "()" 
561                             tspec))
562                    ((eq ch ?2)
563                     (insert ofs "~" name "();\n"))
564                    ((eq ch ?3)
565                     (insert ofs name "(const " name "& other)" 
566                             tspec))
567                    ((eq ch ?4)
568                     (insert ofs name "& operator=(const " name "& other)"
569                             tspec))
570                    ((eq ch ?c)
571                     (insert ofs name "(const " name "& other)" 
572                             tspec
573                             ofs name "& operator=(const " name "& other)"
574                             tspec))
575                    ((eq ch ?d)
576                     (insert ofs name "()" 
577                             tspec
578                             ofs "~" name "();\n"))
579                    (t (setq unread-command-events (cons ch unread-command-events))
580                       (setq exit t)))
581           while (not exit))))
582
583 (defun ccide-gen-class-defaults-impl ()
584   "Generate default implementations for class default functions"
585   (interactive)
586   (let ((defn (c-build-default-funcions-impl)))
587     (kill-new (cadr defn))
588     (message (concat (car defn) " default members"))))
589
590 (defun ccide-set-class-defaults-comment (word)
591   (save-excursion
592     (back-to-indentation)
593     (if (not (looking-at ccide-class-defaults-word))
594         (message "Not at class defaults commnet")
595       (replace-match word t t nil 1))))
596
597 (defmacro ccide-build-class-defaults-f (sym)
598   (let ((fn (intern (concat "ccide-set-class-defaults-" 
599                             (symbol-name sym)))))
600     `(defun ,fn ()
601        (interactive)
602        (ccide-set-class-defaults-comment ,(symbol-name sym)))))
603
604 (ccide-build-class-defaults-f no)
605 (ccide-build-class-defaults-f default)
606 (ccide-build-class-defaults-f my)
607 (ccide-build-class-defaults-f protected)
608 (ccide-build-class-defaults-f private)
609 (ccide-build-class-defaults-f disabled)
610
611 (defun ccide-gen-struct-constructors ()
612   (save-excursion
613     (beginning-of-line)
614     (open-line 1)
615     (indent-according-to-mode)
616     (let* ((scope (c-get-block-scope))
617            (class (c-parse-class scope))
618            (variables (c-get-variable-members-with-type class))
619            (name (c-scope-name (aref (car (last scope)) 1)))
620            (in (make-string (current-indentation) ? ))
621            (inin (make-string (+ (current-indentation) c-basic-offset) ? )))
622       (insert name "()\n" inin ": ")
623       (indent-according-to-mode)
624       (loop for var in variables
625             for first = t then nil
626             if (not first) do (insert ", ")
627             do (insert (car var) "()"))
628       (insert "\n" in "{}\n"
629               in name "(")
630       (loop for var in variables
631             for first = t then nil
632             if (not first) do (insert ", ")
633             do (insert (cdr var) " " (car var) "_"))
634       (insert ")\n" inin ": ")
635       (loop for var in variables
636             for first = t then nil
637             if (not first) do (insert ", ")
638             do (insert (car var) "(" (car var) "_)"))
639       (insert "\n" in "{}"))))
640
641 (defun ccide-class-impl-comment ()
642   "Get implementation comment for current class"
643   (interactive)
644   (let* ((scope (c-get-block-scope))
645          (name (c-get-full-prefix scope)))
646     (kill-new (concat (make-string 75 ?/) "\n"
647                       "// " name "\n\n"
648                       "// protected\n\n"
649                       "// private\n\n"))
650     (message name)))
651
652 (defun ccide-gen-exception (class &optional description)
653   (interactive "sException name: ")
654   (beginning-of-line)
655   (open-line 1)
656   (indent-according-to-mode)
657   (save-excursion
658     (let ((in (make-string c-basic-offset ? ))
659           (ofs (make-string (current-indentation) ? ))
660           (prefix (c-get-full-prefix (c-get-block-scope)))
661           p)
662       (insert "struct " class " : public std::exception\n"
663               ofs "{ virtual char const * what() const throw() ")
664       (setq p (point))
665       (insert "{ return \"" prefix "::" class "\"; } };")
666       (if (> (current-column) fill-column)
667           (save-excursion
668             (goto-char p)
669             (insert "\n" ofs in in))))))
670
671 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
672 ;; function/method level
673
674 (defun ccide-function-comment ()
675   "Add comment for current function"
676   (interactive)
677   (if (c-in-literal)
678       ; Assume, we are in the functions comment ...
679       (progn
680         (c-forward-out-of-comment)
681         (c-backward-syntactic-ws)
682         (c-backward-sexp)))
683   (let ((defun (c-get-defun-state))
684         (indent (make-string comment-column ? ))
685         place)
686     (goto-char (or (aref defun 7) (car (aref defun 6))))
687     (c-backward-syntactic-ws)
688     (if (looking-at "[ \t\n\r]*///<")
689         (progn
690           (delete-region (point) (progn (skip-chars-forward " \t\n\r") (point)))
691           (if (> (current-column) comment-column)
692               (insert "\n"))
693           (indent-to-column comment-column)
694           (search-forward "*/")
695           (forward-char -2))
696       (if (> (current-column) comment-column)
697           (insert "\n"))
698       (indent-to-column comment-column)
699       (insert "///< ")
700       (setq place (point))
701       (insert "\n" 
702               indent "/**< ")
703       (insert             "\\pre \n"
704                           indent "     \\post */")
705       (save-excursion
706         (goto-char (car (aref defun 2)))
707         (setq defun (c-get-defun-state)))
708       (forward-char -2))
709     (ccide-function-comment-adjust defun (concat indent "     "))
710     (if place (goto-char place))))
711
712 (defun ccide-function-comment-grab-args ()
713   (let ((limit (save-excursion
714                  (search-backward "/**" nil t)
715                  (point)))
716         (end  (point))
717         begin start args argend)
718     (setq argend end)
719     (while (or (search-backward "\\param" limit t)
720                (search-backward "\\return" limit t)))
721     (beginning-of-line)
722     (setq start (point))
723     (setq begin start)
724     (while (search-forward "\\param" argend t)
725       (or (search-forward "\\param" argend t)
726           (search-forward "\\return" argend t)
727           (goto-char argend))
728       (beginning-of-line)
729       (setq args (cons (ccide-function-comment-parse-arg start (point))
730                        args))
731       (setq start (point)))
732     (prog1
733         (if (not (search-forward "\return" argend t))
734             (cons nil args)
735           (beginning-of-line)
736           (cons (buffer-substring (point) argend) args))
737       (delete-region begin end))))
738
739 (defun ccide-function-comment-parse-arg (start end)
740   (save-excursion
741     (goto-char start)
742     (re-search-forward "\\param\\(\\[[^]*\\]\\)?\\s-*\\(\\S-*\\)" end t)
743     (cons (match-string 2) 
744           (cons (buffer-substring start (match-beginning 2))
745                 (buffer-substring (match-end 2) end)))))
746   
747 (defun ccide-function-comment-get-throws (defun)
748   (if (aref defun 4)
749       (save-excursion
750         (goto-char (car (aref defun 4)))
751         (if (re-search-forward "\\(throw_\\|throw\\)((?\\s-*\\([^()]*\\))?)" 
752                                (cdr (aref defun 4)) t)
753             (let ((spec (match-string 2)))
754               (if (> (length spec) 0)
755                   spec))))))
756
757 (defun ccide-function-comment-adjust (defun indent)
758   (insert "\n")
759   (let* ((defargs (mapcar (function (lambda (x)
760                                       (c-get-template-argument-name (car x) (cdr x))))
761                           (aref defun 3)))
762          (defret (and (aref defun 1)
763                       (not (string-match (concat "^\\("
764                                                  c-special-key 
765                                                  "\\s-*\\)*\\s-*void$")
766                                          (buffer-substring (car (aref defun 1))
767                                                            (cdr (aref defun 1)))))))
768          (throws (ccide-function-comment-get-throws defun))
769          (xargs (ccide-function-comment-grab-args))
770          (docargs (cdr xargs))
771          (docret (car xargs))
772          (def-in-doc (loop for defarg in defargs always (assoc defarg docargs)))
773          (doc-in-def (loop for docarg in docargs always (member (car docarg) defargs)))
774          (size-eq (= (length defargs) (length docargs))))
775     ;; We differentiate four types changes
776     ;;  - new arguments
777     ;;  - removed arguments
778     ;;  - reordered arguments
779     ;;  - renamed arguments
780     ;; 
781     ;; If the change cannot be described by one of the above, it has
782     ;; to be resolved manually
783     (if throws
784         (insert indent "\\throws " throws "\n"))
785     (cond (doc-in-def
786            ;; reordered arguments or new arguments (or no change)
787            (loop for defarg in defargs
788                  for docarg = (assoc defarg docargs)
789                  do (if docarg
790                         (insert (cadr docarg) (car docarg) (cddr docarg))
791                       (insert indent "\\param " defarg " \n"))))
792           (size-eq ; and (not doc-in-def)
793            ;; renamed arguments
794            (loop for defarg in defargs
795                  for docarg in docargs
796                  do (insert (cadr docarg) defarg (cddr docarg))))
797           (def-in-doc
798             ;; removed arguments
799             (loop for defarg in defargs
800                   for docarg = (assoc defarg docargs)
801                   do (insert (cadr docarg) (car docarg) (cddr docarg))))
802           (t (error "Arg change too complex. Resolve manualy.")))
803     ;; return value is simple
804     (if defret
805         (if docret
806             (insert docret)
807           (insert indent "\\return \n"))))
808   (delete-char -1)
809   (delete-horizontal-space)
810   (insert " "))
811
812 (defun ccide-grab-prototype (&optional prefix)
813   "Grab prototype of function defined or declared at point. Prefix
814 arg, if given, specifies the kind of prefix (inline, static, ...) to use."
815   (interactive "P")
816   (let* ((prfx (or (and prefix (nth (prefix-numeric-value prefix) c-user-prefixes))
817                    ccide-default-prefix))
818          (defn (c-build-defun prfx)))
819     (kill-new (concat (cadr defn) "\n{}\n"))
820     (message (concat (or prfx "")
821                      (if prfx " " "")
822                      (car defn)))))
823
824 (defun ccide-reformat-defun ()
825   "Reformat the defn of the current defun."
826   (interactive)
827   (save-excursion
828     (c-beginning-of-defun-or-decl)
829     (let ((defn (c-build-defun nil t)))
830       (delete-region (or (caar (aref (caddr defn) 0))
831                          (car (aref (caddr defn) 1))
832                          (car (aref (caddr defn) 2)))
833                      (or (car (aref (caddr defn) 6))
834                          (aref (caddr defn) 7)))
835       (insert (cadr defn) "\n"))))
836
837 (defun ccide-replace-defun ()
838   "Replace the function header with the one on the top of the kill
839 ring (presumably placed there using c++-grab-prototype)."
840   (interactive)
841   (save-excursion
842     (c-beginning-of-defun-or-decl)
843     (let ((parse (c-parse-defun)))
844       (delete-region (or (aref parse 0)
845                          (aref parse 1)
846                          (aref parse 2))
847                      (or (aref parse 5)
848                          (aref parse 6)))
849       (yank)
850       (delete-char -3))))
851
852 (defun ccide-prefix-defun-type-with-class (&optional strip)
853   "If a non-keyword type symbol is found prefixing the current defun,
854 it will be prefixed with the current class prefix."
855   (interactive "p")
856   (save-excursion
857     (c-beginning-of-defun-or-decl)
858     (let* ((parse (c-parse-defun))
859            (prefix (c-scope-name (aref parse 2) (+ (or strip 0) 0))))
860       (goto-char (aref parse 1))
861       (while (and (or (looking-at c-any-key)
862                       (looking-at c-user-prefix-re)
863                       (not (c-at-symbol-p)))
864                   (< (point) (aref parse 2))
865                   (not (eobp)))
866         (c-forward-token-1)
867         (c-forward-syntactic-ws))
868       (if (and (c-at-symbol-p)
869                (< (point) (aref parse 2))
870                (not (looking-at (regexp-quote prefix))))
871           (let ((pos (string-match "<" prefix)))
872             (if pos
873                 (insert "typename "))
874             (if (and pos (looking-at (concat (substring prefix 0 pos)
875                                              "\\b[^_]")))
876                 (progn
877                   (goto-char (match-end 0))
878                   (c-backward-syntactic-ws)
879                   (insert (substring prefix pos)))
880               (insert prefix "::"))
881             (ccide-reformat-defun))))))
882
883 (defun ccide-prefix-defun-type-with-namespace (&optional strip)
884   (interactive "p")
885   (ccide-prefix-defun-type-with-class (+ (or strip 0) 1)))
886
887 (defun ccide-insert-defun-prefix (&optional strip)
888   "Insert the current defun prefix at point."
889   (interactive "p")
890   (let* ((parse (c-parse-defun))
891          (prefix (c-scope-name (aref parse 2) (+ (or strip 0) 0))))
892     (insert prefix "::")))
893
894 (defun ccide-kill-inline-decl (defn)
895   (save-excursion
896     (if (aref (caddr defn) 6)
897         (progn
898           (goto-char (cdr (aref (caddr defn) 6)))
899           (let ((end-mark (point-marker)))
900             (goto-char (car (aref (caddr defn) 6)))
901             (indent-rigidly (point) end-mark
902                             (- (current-column)))
903             (prog1
904                 (concat (cadr defn)
905                         "\n"
906                         (buffer-substring-no-properties (point) end-mark)
907                         "\n")
908               (when (aref (caddr defn) 5)
909                 (goto-char (caar (aref (caddr defn) 5)))
910                 (c-backward-syntactic-ws)
911                 (skip-chars-backward ":"))
912               (c-backward-syntactic-ws)
913               (delete-region (point) end-mark)
914               (insert ";"))))
915       (concat (cadr defn) "\n{}\n"))))
916
917 (defun ccide-grab-inline-decl ()
918   "Grab the inline decl at point at turn it into an out-of-line inline
919 declaration at the top of the kill ring."
920   (interactive)
921   (let ((defn (c-build-defun (or ccide-default-prefix "inline"))))
922     (kill-new (ccide-kill-inline-decl defn))
923     (message (concat (or ccide-default-prefix "indline") 
924                      " " 
925                      (car defn)))))
926
927 (defun ccide-grab-all-inlines ()
928   "Grab all inline decls in the current class"
929   (interactive)
930   (let ((class (c-parse-class (c-get-block-scope)))
931         defns)
932     (when class
933       (loop for method in (nreverse (aref class 4))
934             do (when (eq (car method) 'method)
935                  (let ((defn (save-excursion
936                                (goto-char (cdr method))
937                                (c-build-defun (or ccide-default-prefix "inline")))))
938                    (if (aref (caddr defn) 6)
939                        (setq defns (nconc defns (list (ccide-kill-inline-decl defn))))))))
940       (kill-new (loop for defn in (nreverse defns)
941                       for next = nil then t
942                       if next concat "\n";
943                       concat defn))
944       (message (format "%d inlines grabed to kill ring" (length defns))))))
945                        
946
947 (defun ccide-grab-create-constructor ()
948   (interactive)
949   (let ((defn (c-build-create-constructor)))
950     (kill-new (cadr defn))
951     (message (car defn))))
952
953 (defun ccide-grab-create-constructor-impl (&optional prefix)
954   (interactive "P")
955   (let* ((prfx (or (and prefix (nth (prefix-numeric-value prefix) c-user-prefixes))
956                    ccide-default-prefix))
957          (defn (c-build-create-constructor-impl prfx)))
958     (kill-new (cadr defn))
959     (message (concat (or prfx "")
960                      (if prfx " " "")
961                      (car defn)))))
962
963 (defun ccide-find-implementation (&optional other-window)
964   "Find implementation of method declared at point."
965   (interactive "P")
966   (let* ((state (c-get-defun-state))
967          (name (c-defun-short-name state))
968          (scoped-name (c-defun-full-name state))
969          (args (ccide-implementation-args state))
970          (targs (ccide-implementation-template-args state))
971          rv fallback)
972
973     (loop for ext in ccide-implementation-extensions
974           for filename = (ccide-file-name ext)
975           while (not rv)
976           do (progn
977                (let ((buf (or (find-buffer-visiting filename)
978                                   (and (file-readable-p filename)
979                                        (find-file-noselect filename)))))
980                  (when buf
981                    (let ((found (save-excursion
982                                   (set-buffer buf)
983                                   (ccide-find-implementation-1 name scoped-name args targs
984                                                                (not (aref state 6))
985                                                                (car (aref state 2))))))
986                      (if found
987                          (if (cdr found)
988                              (setq rv (cons buf found))
989                            (if (not fallback) (setq fallback (cons buf found))))))))))
990     (if (not rv) (setq rv fallback))
991     (if rv
992         (let* ((buf (car rv))
993                (pos (cadr rv))
994                (win (get-buffer-window buf)))
995           (if win 
996               (select-window win)
997             (if other-window
998                 (switch-to-buffer-other-window buf)
999               (switch-to-buffer buf)))
1000           (goto-char pos)
1001           (forward-char -1)
1002           (c-beginning-of-defun-or-decl))
1003       (message (concat "Implementation of " scoped-name " not found.")))))
1004
1005 (defun ccide-implementation-args (state)
1006   (string-replace "[ \t\n\r]+" ""
1007                   (loop for (start . end) in (aref state 3)
1008                         for sep = "" then ","
1009                         concat sep
1010                         concat (buffer-substring-no-properties 
1011                                 start (save-excursion
1012                                         (goto-char start)
1013                                         (if (search-forward "=" end 'move) (forward-char -1))
1014                                         (point))))
1015                   
1016                   t))
1017
1018 (defun ccide-implementation-template-args (state)
1019   (and (aref state 0)
1020        (string-replace "[ \t\n\r]+" ""
1021                        (loop for (start . end) in   (save-excursion
1022                                                       (goto-char (caar (last (aref state 0))))
1023                                                       (c-parse-template-declaration))
1024                              for sep = "" then ","
1025                              concat sep
1026                              concat (buffer-substring-no-properties 
1027                                      start (save-excursion
1028                                              (goto-char start)
1029                                              (if (search-forward "=" end 'move) (forward-char -1))
1030                                              (point))))
1031                        t)))
1032
1033 (defun ccide-find-implementation-1 (name scoped-name args targs with-body skip-def)
1034   ;; Within the current buffer, search for all implementations of the
1035   ;; given function. The rv is a list of conses. The car holds the
1036   ;; buffer position of the implementation, the cdr is t if the name,
1037   ;; scoped-name and args are matched, otherwise the args did not match.
1038   (save-excursion
1039     (goto-char (point-min))
1040     (let ((re (concat (if (eq (char-syntax (aref name 0)) ?w) "\\<" "")
1041                       (regexp-quote name)
1042                       (if (eq (char-syntax (aref name (1- (length name)))) ?w) "\\>" "")))
1043           fallback rv check-state)
1044       (while (and (not rv) (re-search-forward re nil t))
1045         (if (and (c-at-toplevel-p) 
1046                  (not (c-in-literal))
1047                  (setq check-state (condition-case nil (c-get-defun-state) (error nil)))
1048                  (not (= (car (aref check-state 2)) skip-def)))
1049             (if (string= scoped-name (c-defun-full-name check-state))
1050                 (if (and (if with-body (aref check-state 6) (not (aref check-state 6)))
1051                          (string= args (ccide-implementation-args check-state))
1052                          (string= targs (ccide-implementation-template-args check-state)))
1053                     (setq rv (cons (point) t))
1054                   (if (not fallback) 
1055                       (setq fallback (cons (point) nil)))))))
1056       (or rv fallback))))
1057
1058 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1059 ;; variable/type level
1060   
1061 (defun ccide-variable-comment ()
1062   "Add a comment to current variable declaration."
1063   (interactive)
1064   (c-forward-out-of-comment)
1065   (c-forward-syntactic-ws)
1066   (while (not (looking-at ";"))
1067     (c-forward-sexp)
1068     (c-forward-syntactic-ws))
1069   (forward-char 1)
1070   (if (> (current-column) comment-column)
1071       (insert "\n" (make-string comment-column ? ) "///< ")
1072     (indent-to-column comment-column)
1073     (insert "///< ")))
1074
1075 (defun ccide-grab-access-fn ()
1076   (interactive)
1077   (save-excursion
1078     (beginning-of-line)
1079     (if (looking-at (concat c++-simple-type-regexp "[ \t\n\r][a-zA-Z0-9_]+[ \t\n\r]*;"))
1080         (let ((vardef (match-string 0))
1081               (in (make-string c-basic-offset ? ))
1082               type reftype varname fnname argname ws)
1083           (forward-line -1)
1084           (back-to-indentation)
1085           (string-match "^[ \t\n\r]*\\(.*\\)[ \t\n\r]\\([a-zA-Z0-9_]+\\)[ \t\n\r]*;$"
1086                         vardef)
1087           (setq varname (match-string 2 vardef)
1088                 type (match-string 1 vardef)
1089                 ws (substring vardef 0 (match-beginning 1)))
1090           (if (string-match "_$" varname)
1091               (setq fnname (string-replace "_$" "" varname)
1092                     argname (concat "a_" fnname))
1093             (setq fnname (concat "q_" varname)
1094                   argname (concat "a_" varname)))
1095           (if (string-match "^[ \t\n\r]*" type)
1096               (setq type (substring type (match-end 0))))
1097           (if (string-match "^[A-Z]" type)
1098               (setq reftype (concat type " const &"))
1099             (setq reftype type))
1100           (kill-new (concat ws type " " fnname "(" reftype " " argname ")\n"
1101                             ws in "{\n"
1102                             ws in in type " old" varname " = " varname ";\n"
1103                             ws in in varname " = " argname ";\n"
1104                             ws in in "return old" varname ";\n"
1105                             ws in "}\n\n"
1106                             ws reftype " " fnname "() const\n"
1107                             ws in "{ return " varname "; }\n"))
1108           
1109           (message varname))
1110       (message "No variable found"))))
1111
1112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1113 ;; doxy comment support functions
1114
1115 (defun ccide-special-indent-function ()
1116   "Function to indent doxy comments correctly"
1117   (let ((indent (ccide-in-doxy-comment)))
1118     (if indent
1119         (let ((lim (save-excursion
1120                      (back-to-indentation)
1121                      (c-literal-limits)))
1122               (pos (- (point-max) (point))))
1123           (save-excursion
1124             (back-to-indentation)
1125             (if (looking-at "*/")
1126                 (incf indent -3)
1127               (let ((para (or (save-excursion (re-search-backward "^\\s-*$" (car lim) t))
1128                               (car lim))))
1129                 (if (and (not (looking-at ccide-doxy-tag-re))
1130                          (re-search-backward (concat "^\\s-*"
1131                                                      ccide-doxy-tag-re)
1132                                              para t))
1133                     (incf indent 4)))))
1134           (delete-region (progn (beginning-of-line) (point))
1135                          (progn (back-to-indentation) (point)))
1136           (indent-to indent)
1137           (if (> (- (point-max) pos) (point))
1138               (goto-char (- (point-max) pos)))))))
1139   
1140 (defun ccide-fill-function ()
1141   "auto-fill function for doxy comments"
1142   (if (do-auto-fill)
1143       (if (not fill-prefix)
1144           (indent-according-to-mode))))
1145
1146 (defun ccide-hide-all-doxy-comments ()
1147   "Hide all doxy comments"
1148   (interactive)
1149   (save-excursion
1150     (goto-char (point-min))
1151     (while (re-search-forward "^\\s-*/\\*\\*" nil t)
1152       (beginning-of-line)
1153       (forward-line -1)
1154       (if (not (looking-at "\\s-*$"))
1155           (forward-line 1))
1156       (forward-char -1)
1157       (let ((start (point)))
1158         (if (re-search-forward "\\*/" nil t)
1159             (progn
1160               (if (looking-at "\\s-*\n")
1161                   (forward-line 1))
1162               (forward-char -1)
1163               (let ((overlay (make-overlay start (point))))
1164                 (overlay-put overlay 'intangible 'hs)
1165                 (overlay-put overlay 'invisible 'hs)))))))
1166   (message "Done."))
1167
1168 (defun ccide-show-all-comments ()
1169   "Show all comments"
1170   (interactive)
1171   (save-excursion
1172     (goto-char (point-min))
1173     (while (not (eobp))
1174       (goto-char (next-overlay-change (point)))
1175       (loop for overlay in (overlays-at (point))
1176             if (eq (overlay-get overlay 'invisible) 'hs)
1177             do (delete-overlay overlay))))
1178   (message "Done."))
1179
1180 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1181 ;; CORBA support (omniORB2)
1182
1183 (defun ccide-get-corba-headers ()
1184   (let (files)
1185     (save-excursion
1186       (goto-char (point-min))
1187       (while (re-search-forward "#include\\s-*[\"<]\\([^\">]*\\)\\.hh[\">]" nil t)
1188         (setq files (cons (ccide-match-string 1) files)))
1189       (nreverse files))))
1190
1191 (defun ccide-corba-maybe-build-hh (file)
1192   (let ((skel (ccide-file-name ".hh" file ccide-corba-skel-dir))
1193         (idl (ccide-file-name ".idl" file ccide-corba-idl-dir)))
1194     (if (and (file-readable-p idl)
1195              (or (not (file-readable-p skel))
1196                  (file-newer-than-file-p idl skel)))
1197         (let ((buffer (find-buffer-visiting (ccide-file-name ".hh" file))))
1198           (if buffer
1199               (kill-buffer buffer))
1200           (message "Please wait ... building %s" (ccide-file-name ".hh" file))
1201           (if (ccide-shell-command (concat "cd " 
1202                                            (real-path-name ccide-corba-skel-dir) 
1203                                            " && " 
1204                                            ccide-corba-idl-command 
1205                                            (if (> (length ccide-corba-idl-dir) 0)
1206                                                (concat " -I" ccide-corba-idl-dir))
1207                                            " " 
1208                                            idl))
1209               ()
1210             (display-buffer (get-buffer-create "*ccide shell command*"))
1211             (error "Generation of %s failed" (ccide-file-name ".hh")))))
1212     (if (not (file-readable-p skel))
1213         (error "No file %s or %s" 
1214                (ccide-file-name ".hh" file) (ccide-file-name ".idl" file)))))
1215
1216 (defun ccide-corba-list-skeletons-1 (hh-file)
1217   (ccide-corba-maybe-build-hh hh-file)
1218   (let ((hh-buf (find-file-noselect (ccide-file-name ".hh" hh-file)))
1219         skels)
1220     (save-excursion
1221       (set-buffer hh-buf)
1222       (save-excursion
1223         (goto-char (point-min))
1224         (while (re-search-forward "^\\s-*class\\s-+_sk_\\([a-zA-Z0-9_]+\\)\\s-+:"
1225                                   nil t)
1226           (setq skels (cons (ccide-match-string 1) skels)))))
1227     (mapcar (function (lambda (x) (cons x hh-file)))
1228             (sort skels 'string-lessp))))
1229
1230 (defun ccide-corba-list-skeletons ()
1231   (let ((files (ccide-get-corba-headers)))
1232     (loop for file in files
1233           append (ccide-corba-list-skeletons-1 file))))
1234
1235 (defun ccide-gen-corba-impl (class)
1236   (interactive (list (completing-read "Class name of skeleton: "
1237                                       (ccide-corba-list-skeletons)
1238                                       nil t)))
1239   (let* ((skels (ccide-corba-list-skeletons))
1240          (hh-file (ccide-file-name ".hh" (cdr (assoc class skels))
1241                                    ccide-corba-skel-dir))
1242          (hh-buf (find-file-noselect (ccide-file-name ".hh" hh-file
1243                                                       ccide-corba-skel-dir))))
1244     (ccide-gen-class (concat class "_i"))
1245     (insert (make-string c-basic-offset ? ) ": public virtual _sk_" class "\n")
1246     (save-excursion
1247       (search-forward "protected:" nil t)
1248       (forward-line -1)
1249       (ccide-gen-corba-impl-methods)
1250       (insert "\n"))))
1251
1252 (defun ccide-get-corba-defns (hh-file class)
1253   (let ((hh-buf (find-file-noselect hh-file))
1254         defns)
1255     (save-excursion
1256       (set-buffer hh-buf)
1257       (save-excursion
1258         (goto-char (point-min))
1259         (if (not (re-search-forward (concat "^\\s-*class\\s-+_sk_" class "\\s-+:")
1260                                     nil t))
1261             (error "CORBA skeleton class not found.")
1262           (search-forward "{")
1263           (forward-char -1)
1264           (let ((end (save-excursion (forward-sexp) (point))))
1265             (while (and (< (point) end)
1266                         (< (forward-line 1) 1))
1267               (if (looking-at "\\s-+virtual\\s-+\\(.*)\\)\\s-*=\\s-*0;\\s-*$")
1268                   (setq defns (cons (match-string 1) defns))))))))
1269     (nreverse defns)))
1270
1271 (defun ccide-gen-corba-impl-methods ()
1272   (interactive)
1273   (let* ((class (c-get-class-at-point))
1274          (point (point)))
1275     (if (not class)
1276         (error "No class at point."))
1277     (save-excursion
1278       (goto-char (aref (car class) 1))
1279       (if (not (re-search-forward ":\\s-*public\\s-*virtual\\s-*_sk_\\([^ \t\n\r{},:]*\\)"
1280                                   nil t))
1281           (error "No CORBA impl at point."))
1282       (let* ((name (ccide-match-string 1))
1283              (skels (ccide-corba-list-skeletons))
1284              (hh-file (ccide-file-name ".hh" (cdr (assoc name skels))
1285                                        ccide-corba-skel-dir))
1286              (defns (ccide-get-corba-defns hh-file name))
1287              end)
1288         (goto-char (aref (car class) 2))
1289         (save-excursion
1290           (c-forward-sexp)
1291           (setq end (point)))
1292         (if (re-search-forward "^\\s-*// CORBA$" end t)
1293             (let ((start (match-beginning 0)))
1294               (if (re-search-forward "^\\s-*// END-CORBA$" end t)
1295                   (let ((eend (match-end 0)))
1296                     (goto-char start)
1297                     (forward-line 1)
1298                     (if (re-search-forward "/\\*\\|//" (match-beginning 0) t)
1299                         (if (y-or-n-p "Remove CORBA Funktion comments? (y/n)")
1300                             (delete-region start (1+ eend))
1301                           (goto-char eend)
1302                           (beginning-of-line)
1303                           (delete-region (point) (progn 
1304                                                    (end-of-line)
1305                                                    (1+ (point))))
1306                           (save-excursion
1307                             (goto-char start)
1308                             (delete-region (point) (progn 
1309                                                      (end-of-line)
1310                                                      (1+ (point)))))
1311                           (insert "\n"))
1312                       (delete-region start (1+ eend))))))
1313           (goto-char point))
1314         (indent-according-to-mode)
1315         (insert "// CORBA\n")
1316         (loop for defn in defns
1317               do (progn
1318                    (save-excursion (insert defn ";"))
1319                    (indent-according-to-mode)
1320                    (let ((start (point)) end)
1321                      (end-of-line)
1322                      (setq end (point))
1323                      (goto-char start)
1324                      (while (re-search-forward "\\s-+" end t)
1325                        (replace-match " ")
1326                        (setq end (- end (- (match-end 0) (match-beginning 0) 1))))
1327                      (end-of-line)
1328                      (loop with done = nil
1329                            while (> (current-column) c-max-def-column)
1330                            do (while (and (> (current-column) c-max-def-column)
1331                                           (search-backward "," start t)))
1332                            do (if (looking-at ",")
1333                                   (progn
1334                                     (forward-char 1)
1335                                     (insert "\n")
1336                                     (open-line 1)
1337                                     (indent-according-to-mode)
1338                                     (delete-char 2)
1339                                     (setq start (point))
1340                                     (end-of-line))
1341                                 (setq done t))
1342                            while (not done)))
1343                    (insert "\n")))
1344         (indent-according-to-mode)
1345         (insert "// END-CORBA\n")))))
1346
1347 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1348 ;; template support
1349
1350 (defun ccide-scan-mantemps ()
1351   "Scan *compilation* buffer for errors and generate manual template
1352 instatiations at point."
1353   (interactive)
1354   (save-excursion
1355     (set-buffer "*compilation*")
1356     (goto-char (point-min)))
1357   (save-excursion
1358     (set-buffer (get-buffer-create "*mantemps*"))
1359     (erase-buffer)
1360     (loop for temp = (ccide-get-mantemp)
1361           while temp
1362           do (insert temp "\n"))
1363     (mantemp-make-mantemps-buffer)
1364     (goto-char (point-min))
1365     (while (progn
1366              (ccide-fix-mantemp)
1367              (< (forward-line 1) 1))))
1368   (insert-buffer-substring "*mantemps*"))
1369
1370 (defun ccide-get-mantemp ()
1371   (save-excursion
1372     (set-buffer "*compilation*")
1373     (if (search-forward "undefined reference to `" nil t)
1374         (let ((start (point)))
1375           (end-of-line)
1376           (search-backward "'" nil t)
1377           (buffer-substring start (point))))))
1378
1379 (defun ccide-fix-mantemp ()
1380   (let ((end (save-excursion
1381                (end-of-line) (point))))
1382     (if (and (save-excursion (search-forward "(" end t))
1383              (search-forward " class" end t))
1384         (progn
1385           (forward-char -6)
1386           (delete-char 6)))))
1387       
1388 (provide 'cc-ide)
1389
1390 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1391 ;; other stuff
1392
1393 (defun ccide-open-compilation-frame ()
1394   (interactive)
1395   (let ((c-frame (selected-frame))
1396         (compilation-frame (make-frame '((minibuffer . nil) 
1397                                          (unsplittable . t) 
1398                                          (menu-bar-lines . 0) 
1399                                          (top . -87) 
1400                                          (left . 36) 
1401                                          (width . 169) 
1402                                          (height . 9)))))
1403     (select-frame compilation-frame)
1404     (switch-to-buffer "*compilation*")
1405     (set-window-dedicated-p (selected-window) t)))
1406
1407 (defun ccide-compile (command)
1408   (delete-other-windows)
1409   (split-window-horizontally)
1410   (compile command)
1411   (save-excursion
1412     (set-buffer "*compilation*")
1413     (let ((point (point-max)))
1414       (goto-char point)
1415       (loop for window in (get-buffer-window-list "*compilation*" nil t)
1416             do (set-window-point window point)))))
1417
1418 (defun ccide-compile-compile ()
1419   (interactive)
1420   (ccide-compile (concat "make -k " ccide-compile-opts)))
1421
1422 (defun ccide-compile-clean ()
1423   (interactive)
1424   (ccide-compile (concat "make -k " ccide-compile-opts " clean")))
1425
1426 (defun ccide-compile-cleandepends ()
1427   (interactive)
1428   (ccide-compile (concat "make -k " ccide-compile-opts " cleandepends")))
1429
1430 (defun ccide-compile-kill ()
1431   (interactive)
1432   (set-buffer "*compilation*")
1433   (kill-compilation))
1434
1435 (defun ccide-hide-compilation ()
1436   (interactive)
1437   (let ((active (selected-window)))
1438     (unwind-protect
1439         (loop for window in (get-buffer-window-list "*compilation*")
1440               do (progn (select-window window)
1441                         (switch-to-buffer (other-buffer "*compilation*"))))
1442       (select-window active))))
1443
1444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1445 ;; keymap and installation
1446
1447 (defun ccide-bind-keys (prefix map)
1448   (loop for binding in ccide-bindings
1449         do (apply 'vcmd-define-key
1450                   map
1451                   (concat prefix (car binding))
1452                   (cadr binding)
1453                   "IDE"
1454                   (cddr binding))))
1455
1456 (defun ccide-install-it ()
1457   (save-excursion
1458     (hs-minor-mode 1)
1459     (hs-show-all))
1460   (local-unset-key "\C-c;")
1461   (local-unset-key [menu-bar IDE])
1462   (ccide-bind-keys "\C-c;" (current-local-map))
1463   (local-set-key "\C-cC" 'ccide-hide-all-doxy-comments)
1464   (local-set-key "\C-cS" 'ccide-show-all-comments)
1465   (set (make-local-variable 'auto-fill-function) 'ccide-fill-function)
1466   (set (make-local-variable 'paragraph-start) (concat "[ \t\f]*$\\|[ \t\f]*" ccide-doxy-tag-re))
1467   (set (make-local-variable 'paragraph-separate) "[ \t\f]*$")
1468   (auto-fill-mode -1)
1469   (ccide-project-load-config)
1470   (ccide-directory-load-config)
1471   (ccide-auto-decorate-new-files))
1472
1473 (defun ccide-project-load-config ()
1474   (let ((conf (ccide-project-search-upwards "project.el" (file-name-directory (buffer-file-name)))))
1475     (when conf
1476       (set (make-local-variable 'ccide-project-root) (file-name-directory conf))
1477       (load-file conf))))
1478
1479 (defun ccide-project-search-upwards (file &optional dir)
1480   "Search for FILE in all directories starting at DIR and going up the directory hierarchy.
1481 DIR defaults to ccide-project-root"
1482   (let (conf last-dir)
1483     (setq dir (expand-file-name "x" (or dir ccide-project-root)))
1484     (while (and (not (string= (setq last-dir dir 
1485                                     dir (directory-file-name (file-name-directory dir))) last-dir))
1486                 (setq conf (expand-file-name file dir))
1487                 (not (file-readable-p conf))))
1488     (and (file-readable-p conf) conf)))
1489
1490 (defun ccide-directory-load-config ()
1491   (if (file-readable-p ".dir.el")
1492       (load-file ".dir.el")))
1493
1494 (add-hook 'c-mode-hook 'ccide-install-it)
1495 (add-hook 'c++-mode-hook 'ccide-install-it)
1496 (add-hook 'c-special-indent-hook 'ccide-special-indent-function)
1497
1498 (loop for extension in ccide-special-extensions
1499       for re = (concat (regexp-quote extension) "$")
1500       if (not (assoc re auto-mode-alist))
1501         do (setq auto-mode-alist (append auto-mode-alist
1502                                          (list (cons re 'c++-mode)))))
1503
1504 (defadvice c-indent-line (after c-indent-less compile disable) ;activate
1505   ;; new indent function for c-mode: do standard indentation first. If line
1506   ;; is to long using standard indentation, just indent by c-basic-indentation.
1507   (let ((cc (save-excursion (end-of-line) (current-column)))
1508         indent)
1509     (if (> cc  85)
1510         (let ((pos (- (point-max) (point))))
1511           (beginning-of-line)
1512           (let ((point (point))
1513                 (line (1+ (count-lines 1 (point))))
1514                 indent)
1515             (c-beginning-of-statement-2)
1516             (if (and (not (c-crosses-statement-barrier-p (point) point))
1517                      (not (eq (+ (count-lines 1 (point))
1518                                  (if (bolp) 1 0))
1519                               line)))
1520                 (progn
1521                   (setq indent (+ (current-indentation) c-basic-offset))
1522                   (goto-char point)
1523                   (if (< indent (current-indentation))
1524                       (progn
1525                         (setq ad-return-value
1526                               (+ ad-return-value
1527                                  (- (current-indentation) indent)))
1528                         (delete-region (c-point 'bol) (c-point 'boi))
1529                         (indent-to indent))))))
1530           (if (< (point) (c-point 'boi))
1531               (back-to-indentation)
1532             (if (> (- (point-max) pos) (point))
1533                 (goto-char (- (point-max) pos))))))))
1534
1535
1536 \f
1537 ;;; Local Variables:
1538 ;;; elisp-project-autoload-file-name: "cc-autoload.el"
1539 ;;; End: