]> git.donarmstrong.com Git - lib.git/blob - emacs_el/tiny-tools/other/c-comment-edit2.el
add tiny-tools
[lib.git] / emacs_el / tiny-tools / other / c-comment-edit2.el
1 ;;; c-comment-edit2.el --- C Comment Edit
2
3 ;; This file is not part of Emacs
4
5 ;; Copyright (C) 1987 Kyle Jones
6 ;; Author:       Kyle Jones
7 ;; Maintainer:   Jari Aalto
8 ;; Keywords:     extensions
9
10 ;;{{{ id
11
12 ;; COPYRIGHT NOTICE
13 ;;
14 ;; This program is free software; you can redistribute it and/or modify it
15 ;; under the terms of the GNU General Public License as published by the Free
16 ;; Software Foundation; either version 2 of the License, or (at your option)
17 ;; any later version.
18 ;;
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
22 ;; for more details.
23 ;;
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with program; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28 ;;
29 ;; Visit <http://www.gnu.org/copyleft/gpl.html> for more information
30
31 ;;}}}
32 ;;{{{ Docs
33
34 ;;  .................................................. &t-documentation ...
35
36 ;;; Commentary:
37
38 ;;
39 ;;      Date: 12 Jan 89 17:36:19 GMT
40 ;;
41 ;;      Attached is an enhanced version of the `c-comment-edit' package, last
42 ;;      posted sometime in 1987.
43 ;;
44 ;;      c-comment-edit is a command that copies a C comment into a
45 ;;      temporary buffer for editing under a more suitable major mode
46 ;;      (usually text-mode).  Once the comment is edited,
47 ;;      c-comment-edit-end (normally bound to C-c ESC) replaces the old
48 ;;      comment with the edited version, adding comment delimiters and
49 ;;      leaders as necessary.  c-comment-edit is ideal for large comments
50 ;;      of these styles:
51 ;;
52 ;;      /*      /*      /*
53 ;;        ...    * ...  ** ...
54 ;;        ...    * ...  ** ...
55 ;;      */       */     */
56 ;;
57 ;;      Features added:
58 ;;
59 ;;      o c-comment-edit no longer uses a recursive-edit so multiple
60 ;;        c-comment-edit's  be running simultaneously.
61 ;;      o c-comment-edit will now search forward from point for a comment if
62 ;;        point is not within a comment.
63 ;;      o c-comment-edit-hook is provided.
64 ;;      o Bill Carpenter's c-comment-leader-regexp fixed was incorporated.
65 ;;
66 ;;      Kyle Jones
67
68 ;;}}}
69 ;;{{{ history
70
71 ;; ......................................................... &t-history ...
72 ;;; Change Log:
73 ;;
74 ;; Mar  03      2001    [jari]          20.7    v1.18           Released
75 ;; - Added ###autoload items.
76 ;;
77 ;; Mar  23      1998    [jari]          19.34   v1.12           Released
78 ;; - Byte compile errors fixed.
79 ;;
80 ;; Aug  11      1997    [jari]          19.28   v1.11           Released
81 ;; - The example setup at the end worked fine, execept if used inside
82 ;;   the function. The last line was not adjusted to
83 ;;
84 ;;   ***********/   but to   *********
85 ;;                           */
86 ;;
87 ;;   That is; the last */ was not shifted to the end of line.
88 ;; - corrected the example.
89 ;;
90 ;; Feb  24      1997    [jari]          19.28   v1.10           Released
91 ;; - Rick Flower <flower@ms31.sp.trw.com> reported that c-indent-command
92 ;;   wasn't seen by 19.15 byte compiler. Now requires cc-mode.
93 ;;
94 ;; Feb  26      1997    [jari]          19.28   v1.9            Released
95 ;; - Small error in window configuration restore corrected.
96 ;;
97 ;; Dec  11      1996    [jari]          19.28   v1.6-1.8        Released
98 ;; - Some minor byte compilation corrections. Window configuration
99 ;;   bug corrected. c-comment-edit-at-point corrected in C++ mode.
100 ;;
101 ;; May  27      1996    [jari]          19.28   v1.5            Released
102 ;; - Added saving/restoring the frame configuration. Previously the
103 ;;   window layout was destroyed when the c-comment-edit-end was
104 ;;   finished. Now windows are restored as they were.
105 ;; - Made this package 18.xx compatible again.
106 ;;
107 ;; Apr  30      1996    [jari]          19.28   v1.4            Released
108 ;; - Jerome Santini <santini@chambord.univ-orleans.fr> reported problems
109 ;;   in 19.30 with the font-lock code. Now the eval-when-compile
110 ;;   forms take in effect only if font-lock is not present. This was
111 ;;   serious bug.
112 ;; - Renamed all the rest functions to have prefix "c-comment". This
113 ;;   follows the GNU package guidelines.
114 ;; - Added defontifying a comment in separate buffer to get clear look.
115 ;;
116 ;; Oct  14      1995    [jari]          19.28   v1.3            Released
117 ;; - Corrected the LCD entry, that was mistakenly modified.
118 ;;
119 ;; Sep  8       1995    [jari]          19.28   v1.2            NotReleased
120 ;; - Cleaned up the Newsereader headers and presedved only 'date:'
121 ;;   of original introduction
122 ;;   within-c-comment-p         :!  renamed to comment-area, more general
123 ;; - Added new function c-comment-edit-at-point.  If point is inside of a
124 ;;   comment, the comment is edited.  Otherwise, a new comment is created
125 ;;   at point. [jcolman]
126 ;; - Cleared byte-compiler errors. Added optional parameters to main function
127 ;;   c-comment-edit. Main also now returns more gracefully, if comment
128 ;;   syntax isn't defined. This really should be made more general..
129 ;;
130 ;; Sep  4       1995    [jari]          19.28   v1.1            NotReleased
131 ;; - Jake Colman <jcolman@j51.com> sent mail asking to crrect some
132 ;;   things. Great that I get user feedback!
133 ;; - kill-all-local-variables call removed
134 ;; - renamed  c-comment-edit-after-hook --> c-comment-edit-exit-hook
135 ;;   and moved it to the end of defun.
136 ;; - The fill-column setting has been adjusted to include "/*" (-2 from the
137 ;;   original setting).
138 ;; - When edited comment is inserted back, the colors are gone. This has been
139 ;;   corrected to re-fontify the comment if font-lock-mode is on.
140 ;; - Added c-comment-edit-load-hook
141 ;;
142 ;; Apr  28      1995    [jari]          19.28   v0.2            NotReleased
143 ;; - After talking with Kyle directly, he said that I should load
144 ;;   this .el  with some other name, because he didn't plan to have
145 ;;   any enhancement support. He said that I had been among the very
146 ;;   few that had ever asked any changes to this module.
147 ;; - On my behalf, since this is almost identical copy of the
148 ;;   original, I have no objections that someone else modifies this
149 ;;   .el with the "c-comment-edit2.el" -- I do plan to update this if it has
150 ;;   errors that are caused by me of course, but right now I don't
151 ;;   think it misses anything crucial. Just drop me a note, I love to hear
152 ;;   about new improvements.
153 ;;
154 ;; Feb  22      1995    [jari]          19.28   v0.1            NotReleased
155 ;; - I had used this intensively when 18.57 was still around in our
156 ;;   envinronment (about 4 moths ago), but when I moved to 19.28
157 ;;   I found some incompatibilities in keybindings and I dislike the
158 ;;   comment syntax which left first line empty. Now there is variable
159 ;;   to configure 1st line layout.
160 ;; - Now ESC ESC terminates in 19 and ESC in 18.
161 ;; - Added few confortable variables
162 ;;   *  c-comment-edit-bname , c-comment-edit-empty-1-line
163 ;;   *  c-comment-edit-other-buffer , c-comment-edit-C-buf
164 ;;   *  c-comment-edit-[bc,ec]
165 ;; - Added hooks
166 ;;   *  c-comment-edit-end-hook
167 ;;   *  c-comment-edit-after-hook
168 ;;
169 ;; - touched original code:
170 ;;   *  c-comment-edit
171 ;;      - there was one problem with this, the hook was too early
172 ;;        run compared to kill-local-var, so if you turned on some minor
173 ;;        mode, it was effectively lost. --> now the hook runs last.
174 ;;   *  c-comment-edit-end
175 ;;      - Added the new functionality here, many changes, sets global
176 ;;        variables + runs hooks
177 ;;
178 ;; - There is an EXAMPLE section at the end of this file which is one of
179 ;;   my favourite C/C++ function header. And cleanup function to
180 ;;   retain the format after comment has been added too.
181
182 ;;}}}
183
184 ;;{{{ setup: bind, hooks
185
186 ;;; Code:
187
188 (require 'cc-mode)
189 (load "c-mode" 'noerr)                  ;Hm, XEmacs 19.13 lacks this?
190 (eval-when-compile (require 'cl))
191
192 ;;; .......................................................... &v-bind ...
193
194 (defvar c-com-mode-map nil  "C comment edit map")
195
196 (if c-com-mode-map
197     nil
198   (setq c-com-mode-map (make-sparse-keymap))
199   ;; keys;
200   (if (string< emacs-version "19")
201       (progn
202         (define-key c-com-mode-map "\C-c\C-c" 'c-comment-edit-end)
203         (define-key c-com-mode-map "\e" 'c-comment-edit-abort))
204     (define-key c-com-mode-map "\C-c\C-c" 'c-comment-edit-end)
205     (define-key c-com-mode-map "\e\e" 'c-comment-edit-abort)))
206
207 ;;;  ......................................................... &v-hooks ...
208
209 (defvar c-comment-edit-hook nil
210   "*Function to call whenever `c-comment-edit' is used.
211 The function is called just before the `c-comment-edit' function allows you to
212 begin editing the comment.")
213
214 (defvar c-comment-edit-exit-hook nil
215   "*Enables you to do some cleanup after edit is done, not called
216 if user aborted the action. Buffer is already inserted back when this
217 hook is called.")
218
219 (defvar c-comment-edit-end-hook nil
220   "*When user has pressed C-c or ESC to complete editing, the
221 Comment prefix lines are drawn. After it has completed drawing,
222 and the buffer is in ready to be inserted back, this hook will be called. ")
223
224 (defvar c-comment-edit-load-hook nil
225   "*Run when file has been loaded.")
226
227 ;;}}}
228 ;;{{{ setup: read-only vars
229
230 ;;; ....................................................... &v-private ...
231 ;;; These are set by funcs, user can check the values in hooks.
232
233 (defconst c-comment-edit-beg-c nil
234   "After the comment is edited, this variable contains
235 begin MARK of comment.")
236
237 (defconst c-comment-edit-end-c nil
238   "After the comment is edited, this variable contains
239 end MARK of comment.")
240
241 (defconst c-comment-edit-bufc nil
242   "After the comment is edited, this variable contains C-code buffer name,
243 where comment edited belonged.")
244
245 ;;}}}
246 ;;{{{ setup: -- user config
247
248 ;;; .......................................................... &v-conf ...
249
250 (defvar c-comment-window-register  ?w
251   "*Which register to use to save window configuration.")
252
253 ;;;###autoload
254 (defvar c-comment-leader " *"
255   "*Leader used when rebuilding edited C comments.  The value of this variable
256 should be a two-character string.  Values of \"  \", \" *\" and \"**\"
257 produce the comment styles:
258         /*      /*      /*
259           ...    * ...  ** ...
260           ...    * ...  ** ...
261         */       */     */
262 respectively.")
263
264 (defconst c-comment-leader-regexp "^[   ]*\\(\\*\\*\\|\\*\\)?[ ]?"
265   "Regexp used to match C comment leaders.")
266
267 (defvar c-comment-edit-mode 'text-mode
268   "*Major mode used by `c-comment-edit' when editing C comments.")
269
270 (defvar c-comment-edit-buffer-alist nil
271   "Assoc list of C buffers and their associated comment buffers.
272 Elements are of the form (C-BUFFER COMMENT-BUFFER COMMENT-START COMMENT-END)
273 COMMENT-START and COMMENT-END are markers in the C-BUFFER.")
274
275 (defvar c-comment-edit-bname " *C Comment Edit*"
276   "*buffer name to edit the comment")
277
278 (defvar c-comment-edit-empty-1-line nil
279   "*This determines if the first comment line will be left empty
280
281 /*
282  * comment begin, when value is t
283  */
284 ")
285
286 (defvar  c-comment-edit-other-buffer t
287   "*Set to nil if you want to edit in full buffer")
288
289 ;;}}}
290 ;;{{{ code: macros
291
292 ;;;  ########################################################## &Macros ###
293
294 (defmacro c-comment-save-point (&rest body)
295   "Save value of point, evalutes FORMS and restore value of point.
296 If the saved value of point is no longer valid go to (point-max).
297 The variable `save-point' is lambda-bound to the value of point for
298 the duration of this call."
299   (list 'let '((save-point (point)))
300         (list 'unwind-protect
301               (cons 'progn body)
302               '(goto-char (min (point-max) save-point)))))
303
304 (defmacro c-comment-marker (pos &optional buffer)
305   (list 'set-marker '(make-marker) pos buffer))
306
307 ;;}}}
308
309 ;;; ########################################################### &Funcs ###
310
311 ;;{{{ code: general
312
313 ;;; ----------------------------------------------------------------------
314 ;;;
315 (defun c-comment-save-state  ()
316   "Save window configuration."
317   (if (fboundp 'point-to-register-compatibility-binding)
318       (funcall
319        (symbol-function 'point-to-register-compatibility-binding)
320        c-comment-window-register
321        'window-config)))
322
323 ;;; ----------------------------------------------------------------------
324 ;;;
325 (defun c-comment-restore-state  ()
326   "Save window configuration."
327   (if (and c-comment-window-register
328            (get-register c-comment-window-register)
329            (fboundp 'jump-to-register-compatibility-binding))
330       (funcall
331        (symbol-function 'jump-to-register-compatibility-binding)
332        c-comment-window-register)))
333
334 ;;; ----------------------------------------------------------------------
335 ;;;
336 (defun c-comment-area (beg end)
337   "Searches area bounds delimited by strings BEG and END.
338 First searches backward, them forward.
339
340 Returns:
341   (beg-point . end-point)
342   nil."
343   (condition-case nil
344       (let (p pp)
345         (c-comment-save-point
346          (search-backward beg)
347          (setq p (point))
348          (search-forward end)
349          (setq pp (point)))
350         (if (< (point) pp) (cons p pp) nil))
351     (search-failed
352      nil)))
353
354 ;;; ----------------------------------------------------------------------
355 ;;;
356 (defun c-comment-find-buffer (&optional buffer)
357   (or buffer (setq buffer (current-buffer)))
358   (let ((list c-comment-edit-buffer-alist))
359     (catch 'return-value
360       (while list
361         (if (eq (nth 1 (car list)) buffer)
362             (throw 'return-value (car list))
363           (setq list (cdr list)))))))
364
365 ;;; ----------------------------------------------------------------------
366 ;;;
367 (defun c-comment-find-c-buffer (&optional buffer)
368   (or buffer (setq buffer (current-buffer)))
369   (let ((list c-comment-edit-buffer-alist))
370     (catch 'return-value
371       (while list
372         (if (eq (car (car list)) buffer)
373             (throw 'return-value (car list))
374           (setq list (cdr list)))))))
375
376 ;;}}}
377
378 ;;{{{ code: edit
379
380 ;;; ----------------------------------------------------------------------
381 ;;; 1995-09-07 Jake Colman <jcolman@j51.com> supplied basic code
382 ;;;
383 ;;;###autoload
384 (defun c-comment-edit-at-point ()
385   "Edit C comment at point.
386 If point is inside of a comment, the comment is edited.  Otherwise, a new
387 comment is created at point.
388 "
389   (interactive)
390   (let* ((cs       comment-start)
391          (ce       comment-end)
392          comment)
393     (when (memq major-mode '(c-mode c++-mode cc-mode))
394       ;;    override the comment settings, because C++ has
395       ;;    "//" and "" by default and that's not what we want
396       (setq   cs "/*"   ce "*/"))
397
398     (if (c-comment-area cs ce)
399         (c-comment-edit nil)
400       ;;  The catch is, that we first create a comment and then
401       ;;  pass control to the main.
402       (setq comment  (concat cs " "    ce))
403       (insert comment)
404       (goto-char (- (point) (length comment))) ;go inside it
405       (c-indent-command)
406       (c-comment-edit nil))))
407
408 ;;; ----------------------------------------------------------------------
409 ;;;
410 (defun c-comment-edit-end ()
411   "End c-comment-edit.
412 C comment is replaced by its edited counterpart in the appropriate C buffer.
413 Indentation will be the same as the original."
414   (interactive)
415   (let ((tuple          (c-comment-find-buffer))
416         (line1-empty    c-comment-edit-empty-1-line)
417         (i              0)
418         edited
419         char-count)
420
421     (if (null tuple)
422         (error "Not a c-comment-edit buffer."))
423
424     (let ((inhibit-quit         t)
425           (c-comment-c-buffer   (car tuple))
426           (c-comment-buffer     (nth 1 tuple))
427           (c-comment-start      (nth 2 tuple))
428           (c-comment-end        (nth 3 tuple)))
429
430       (cond
431        ((buffer-modified-p)
432         ;; rebuild the comment
433         (goto-char (point-min))
434
435         (if (null line1-empty)
436             (insert "/*")
437           (insert "/*\n"))
438
439         (if (string= c-comment-leader "  ")
440             (while (not (eobp))
441               (setq i (1+ i))
442               (if (eq 1 i)
443                   (insert " ")
444                 (if (not (eolp)) (insert c-comment-leader " ")))
445               (forward-line))
446           (setq i 0)
447           (while (not (eobp))
448             (setq i (1+ i))
449             (if (and (eq 1 i) (null line1-empty))
450                 (insert " ")
451               (insert c-comment-leader (if (eolp) "" " ")))
452             (forward-line)))
453
454         (if (not (char-equal (preceding-char) ?\n))
455             (insert "\n"))
456
457         (insert (if (string= c-comment-leader " *") " */" "*/"))
458
459         ;; indent if necessary
460         (let ((indention
461                (save-excursion
462                  (set-buffer c-comment-c-buffer)
463                  (goto-char c-comment-start)
464                  (current-column))))
465           (goto-char (point-min))
466           (unless (zerop indention)
467             ;; first line is already indented
468             ;; in the C buffer
469             (forward-line)
470             (while (not (eobp))
471               (indent-to indention)
472               (forward-line))))
473
474         (setq edited t)                 ;Raise the Flag
475         (run-hooks 'c-comment-edit-end-hook)
476
477         ;; replace the old comment with the new
478
479         (save-excursion
480           (setq char-count (- (point-max) (point-min)) )
481           (set-buffer c-comment-c-buffer)
482
483           (delete-region c-comment-start c-comment-end)
484           (goto-char c-comment-start)
485
486           (insert-buffer c-comment-buffer)
487
488           ;;  save values for possible hook function
489
490           (setq c-comment-edit-beg-c (marker-position c-comment-start)
491                 c-comment-edit-end-c (+ c-comment-edit-beg-c char-count)
492                 c-comment-edit-bufc  c-comment-c-buffer)
493
494           ;;  The colors vanished, when we deleted that region and inserted
495           ;;  new comment into buffer, lets get them back
496
497           (if (and (featurep 'font-lock)
498                    (symbol-value 'font-lock-mode))
499               (funcall
500                (symbol-function 'font-lock-fontify-region)
501                c-comment-edit-beg-c c-comment-edit-end-c))))
502
503        (t
504         (message "No change.")))
505
506       ;; .................................................. cond
507
508       (c-comment-restore-state)
509
510       ;; switch to the C buffer
511
512       (if (get-buffer-window c-comment-c-buffer)
513           (select-window (get-buffer-window c-comment-c-buffer))
514         (switch-to-buffer c-comment-c-buffer))
515
516       ;; delete the window viewing the comment buffer
517
518       (and (get-buffer-window c-comment-buffer)
519            (delete-window (get-buffer-window c-comment-buffer)))
520
521       ;; unlink the tuple from c-comment-edit-buffer-alist
522
523       (setq c-comment-edit-buffer-alist
524             (delq tuple c-comment-edit-buffer-alist))
525
526       ;; let Emacs reclaim various resources
527
528       (save-excursion
529         (set-buffer             c-comment-buffer)
530         (set-buffer-modified-p  nil)
531         (kill-buffer            c-comment-buffer))
532
533       ;;  Now kill the markers so that they don't consume resources
534
535       (set-marker c-comment-start       nil)
536       (set-marker c-comment-end         nil))
537
538     (if edited                          ;only if touched the contents
539         (run-hooks 'c-comment-edit-exit-hook))))
540
541 ;;}}}
542 ;;{{{ code: abort
543
544 ;;; ----------------------------------------------------------------------
545 ;;;
546 (defun c-comment-edit-abort ()
547   "Abort a c-comment-edit with no change."
548   (interactive)
549   (let* ((tuple (c-comment-find-buffer))
550          (c-comment-c-buffer (car tuple))
551          (c-comment-buffer (nth 1 tuple))
552          (c-comment-start (nth 2 tuple))
553          (c-comment-end (nth 3 tuple)))
554
555     (if (null tuple)
556         (error "Not a c-comment-edit buffer."))
557
558     ;; switch to the C buffer
559
560     (if (get-buffer-window c-comment-c-buffer)
561         (select-window (get-buffer-window c-comment-c-buffer))
562       (switch-to-buffer c-comment-c-buffer))
563
564     (let ((inhibit-quit t))
565       (save-excursion
566         (set-buffer c-comment-buffer)
567         (set-buffer-modified-p nil)
568         (if c-comment-edit-other-buffer
569             (delete-window))
570         (kill-buffer c-comment-buffer))
571       ;; unlink the tuple from c-comment-edit-buffer-alist
572       (setq c-comment-edit-buffer-alist
573             (delq tuple c-comment-edit-buffer-alist))
574       (set-marker c-comment-start nil)
575       (set-marker c-comment-end nil)
576       (message "Aborted with no change.")
577       (c-comment-restore-state))))
578
579 ;;}}}
580 ;;{{{ code: main
581
582 ;;; ----------------------------------------------------------------------
583 ;;;
584 ;;;###autoload
585 (defun c-comment-edit (&optional search-prefix com-start com-end)
586   "Edit multi-line C comments.
587 This command allows the easy editing of a multi-line C comment like this:
588    /*
589     * ...
590     * ...
591     */
592 The comment may be indented or flush with the left margin.
593
594 If point is within a comment, that comment is used.  Otherwise the
595 comment to be edited is found by searching forward from point.
596
597 With one \\[universal-argument] searching starts after moving back one
598   paragraph.
599 With two \\[universal-argument]'s searching starts at the beginning of the
600   current or proceeding C function.
601 With three \\[universal-argument]'s searching starts at the beginning of the
602   current page.
603 With four \\[universal-argument]'s searching starts at the beginning of the
604   current buffer (clipping restrictions apply).
605
606 Once located, the comment is copied into a temporary buffer, the comment
607 leaders and delimiters are stripped away and the resulting buffer is
608 selected for editing.  The major mode of this buffer is controlled by
609 the variable `c-comment-edit-mode'.
610
611 Use \\[c-comment-edit-end] when you have finished editing the comment.  The
612 comment will be inserted into the original buffer with the appropriate
613 delimiters and indention, replacing the old version of the comment.  If
614 you don't want your edited version of the comment to replace the
615 original, use \\[c-comment-edit-abort]."
616   (interactive "*P")
617   (catch 'out
618     (let ((inhibit-quit t)
619           (bname                c-comment-edit-bname)
620           (other-bedit  c-comment-edit-other-buffer)
621           marker
622           cs ce
623           tem
624           c-comment-fill-column
625           c-comment-buffer
626           c-comment-start
627           c-comment-end)
628
629       (cond
630        ((and com-start com-end)         ;don't touch they are set.
631         nil)
632
633        ((memq major-mode '(c-mode c++-mode cc-mode))
634         ;;    override the comment settings, because C++ has
635         ;;    "//" and "" by default and that's not what we want
636         (setq   cs "/*"   ce "*/"))
637
638        (t
639         (setq cs (or comment-start
640                      "cbAnything##"))
641         (setq ce (or comment-end
642                      "ceAnything##"))))
643
644       ;;  What was the prefix arg ?
645
646       (cond ((equal search-prefix '(4))
647              (backward-paragraph))
648             ((equal search-prefix '(16))
649              (end-of-defun)
650              (beginning-of-defun)
651              (backward-paragraph))
652             ((equal search-prefix '(64))
653              (backward-page))
654             ((equal search-prefix '(256))
655              (goto-char (point-min))))
656
657       (if (and (null search-prefix)
658                (setq tem (c-comment-area cs ce)))
659           (setq c-comment-start (c-comment-marker (car tem))
660                 c-comment-end   (c-comment-marker (cdr tem)))
661         (let (start end)
662           (condition-case nil
663               (c-comment-save-point
664                (search-forward cs)
665                (setq start (- (point) (length cs)))
666                (search-forward ce)
667                (setq end (point)))
668             (search-failed
669              (message
670               (concat "No C comment found. Check comment-start: "
671                       cs "|" ce))
672              (throw 'out t)))
673           (setq c-comment-start (c-comment-marker start))
674           (setq c-comment-end   (c-comment-marker end))))
675
676       ;; calculate the correct fill-column for the comment
677
678       (setq c-comment-fill-column
679             (- fill-column
680                (save-excursion
681                  (goto-char c-comment-start)
682                  (+ (length comment-start) (current-column)))))
683
684       ;; create the comment buffer
685
686       (setq c-comment-buffer
687             (generate-new-buffer (concat (buffer-name) bname)))
688
689       ;; link into the c-comment-edit-buffer-alist
690
691       (setq c-comment-edit-buffer-alist
692             (cons (list (current-buffer) c-comment-buffer
693                         c-comment-start c-comment-end)
694                   c-comment-edit-buffer-alist))
695
696       ;; copy to the comment to the comment-edit buffer
697
698       (copy-to-buffer c-comment-buffer
699                       (+ c-comment-start 2) (- c-comment-end 2))
700
701       ;; mark the position of point, relative to the beginning of the
702       ;; comment, in the comment buffer.  (if point is within a comment.)
703
704       (or search-prefix (< (point) c-comment-start)
705           (setq marker (c-comment-marker (+ (- (point) c-comment-start 2) 1)
706                                          c-comment-buffer)))
707       ;; ...............................................................
708       ;; select the comment buffer for editing
709
710       (c-comment-save-state)
711
712       (if (null other-bedit)
713           (switch-to-buffer c-comment-buffer)
714         (switch-to-buffer-other-window c-comment-buffer))
715
716       ;; remove the comment leaders and delimiters
717
718       (goto-char (point-min))
719
720       (while (not (eobp))
721         (and (re-search-forward c-comment-leader-regexp nil t)
722              (replace-match "" nil t))
723         (forward-line))
724
725       ;; run appropriate major mode
726
727       (funcall (or c-comment-edit-mode 'fundamental-mode))
728
729       ;; override user's default fill-column here since it will lose if
730       ;; the comment is indented in the C buffer.
731
732       (setq fill-column c-comment-fill-column)
733
734       ;; delete one leading whitespace char
735
736       (goto-char (point-min))
737
738       (if (looking-at "[ \n\t]")
739           (delete-char 1))
740
741       ;; restore cursor if possible
742
743       (goto-char (or marker (point-min)))
744
745       ;; defontify to get a clear look at text
746
747       (put-text-property (point-min) (point-max) 'face 'default)
748
749       (set-buffer-modified-p nil))
750     ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ let end ^^^
751
752     (use-local-map  c-com-mode-map)
753     (run-hooks 'c-comment-edit-hook)
754
755     (message
756      (substitute-command-keys
757       (concat "Type \\[c-comment-edit-end] to end edit, "
758               "\\[c-comment-edit-abort] to abort with no change.")))))
759
760     ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ catch end ^^^
761
762 ;;}}}
763
764 ;;{{{ Example
765 ;;; ......................................................... &example ...
766
767 ;; - Here is ready setup, which you could use right away.
768 ;; - I am used to program all my C/C++ function like this, where the
769 ;;   header is just before each function:
770 ;;
771 ;; /*************************************************************************
772 ;;  * <PUBLIC> FUNCTION: MyFunc
773 ;;  *************************************************************************
774 ;;  * DESCRIPTION
775 ;;  * - This is function is the main entry point to class myClass.
776 ;;  *   it handles reading the oracle database....
777 ;;  *
778 ;;  * SPECIAL
779 ;;  * - Note, that the oracle connection must be verified before your're
780 ;;  *   using this function....
781 ;;  *
782 ;;  * RETURNS
783 ;;  * - Creates object errorAtom which hold data about the promlem occurred.
784 ;;  *   other
785 ;;  *************************************************************************/
786 ;; errorAtom_c *myClass_c::Execute(char * ptr)
787 ;; {
788 ;;
789 ;; }
790 ;;
791 ;;
792 ;; - In order to maintain the '*****' breaks correctly you have to use some
793 ;;   cleanup function like one below. It detects if the Comment has
794 ;;   '****' in it and does nothing if it's regular comment.
795 ;;
796 ;; - Remember that when the comment has been edited, the comment style you
797 ;;   choosed, affects the function. This supposes you have use the 'one star'
798 ;;   style.
799
800 ;;  Setting proper hooks.
801 ;;  I seldom need M-c (capitalize word) in C/C++
802 ;;  If you use many hooks, use command add-hook instead.
803 ;;
804 ;;  (setq c++-mode-hook  'c++-my-hook)
805 ;;  (defun c++-my-hook ()
806 ;;    (local-set-key "\M-c" 'c-comment-edit))
807 ;;
808 ;; (setq c-mode-hook  'c-my-hook)
809 ;; (defun c-my-hook ()
810 ;;   (local-set-key "\M-c" 'c-comment-edit))
811
812 ;; (defun my-com-end ()
813 ;;   "C- comment edit cleanup."
814 ;;   (let* ((sep (make-string 70 ?* ))  ;what separator you want to use
815 ;;       (fix-re "[-=*] [-=*][-=*]" )   ;the gap " " is in buffer
816 ;;       (back-step 3)                  ;depends on the fix-re
817 ;;       (break-re " +[-=*][-=*][-=*]*") ;at least two continuous chars
818 ;;       )
819 ;;
820 ;;     ;;  To preserve indentation. Remember that C-comment markers are
821 ;;     ;;  added to the beginning
822 ;;
823 ;;     (untabify (point-min) (point-max))
824 ;;
825 ;;
826 ;;     ;;  - We are in comment buffer now, so we can move freely with goto-char
827 ;;     ;;  - fix all break-marked lines to certain length
828 ;;
829 ;;     (goto-char (point-min))
830 ;;     (while (re-search-forward fix-re nil t)
831 ;;       (backward-char back-step)  (kill-line) (insert sep))
832 ;;
833 ;;     ;;  - Check if the last line has separator == it is function header
834 ;;     ;;  - The last line holds "*/", so look at the previous one.
835 ;;
836 ;;     (goto-char (point-max))    (forward-line -1)
837 ;;
838 ;;     (cond
839 ;;      ((looking-at break-re)
840 ;;       ;; Remove that lonely "*/" and shift it one line up
841 ;;       ;;
842 ;;       (goto-char (point-max)) (beginning-of-line)
843 ;;       (kill-line)
844 ;;       (backward-delete-char 1)
845 ;;       (insert "/")))                 ;terminate C comment
846 ;;     nil))                                    ;hook must return this
847
848 ;;}}}
849
850 (provide   'c-comment-edit)
851 (run-hooks 'c-comment-edit-load-hook)
852
853 ;;; c-comment-edit2.el ends here