annotate lisp/modes/c-comment.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children e45d5e7c476e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; c-comment.el --- edit C comments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Keywords: c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;;; Copyright (C) 1987, 1988, 1989 Kyle E. Jones
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;; Verbatim copies of this file may be freely redistributed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;;; Modified versions of this file may be redistributed provided that this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;;; notice remains unchanged, the file contains prominent notice of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;;; author and time of modifications, and redistribution of the file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;;; is not further restricted in any way.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; This file is distributed `as is', without warranties of any kind.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 (provide 'c-comment-edit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 (defvar c-comment-leader " *"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 "*Leader used when rebuilding edited C comments. The value of this variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 should be a two-character string. Values of \" \", \" *\" and \"**\" produce the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 comment styles:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 /* /* /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ... * ... ** ...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ... * ... ** ...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 */ */ */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 respectively.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 (defconst c-comment-leader-regexp "^[ ]*\\(\\*\\*\\|\\*\\)?[ ]?"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 "Regexp used to match C comment leaders.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (defvar c-comment-edit-mode 'text-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 "*Major mode used by `c-comment-edit' when editing C comments.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (defvar c-comment-edit-hook nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 "*Function to call whenever `c-comment-edit' is used.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 The function is called just before the `c-comment-edit' function allows you to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 begin editing the comment.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (defvar c-comment-edit-buffer-alist nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 "Assoc list of C buffers and their associated comment buffers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 Elements are of the form (C-BUFFER COMMENT-BUFFER COMMENT-START COMMENT-END)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 COMMENT-START and COMMENT-END are markers in the C-BUFFER.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (defmacro save-point (&rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 "Save value of point, evalutes FORMS and restore value of point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 If the saved value of point is no longer valid go to (point-max).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 The variable `save-point' is lambda-bound to the value of point for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 the duration of this call."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (list 'let '((save-point (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (list 'unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (cons 'progn body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 '(goto-char (min (point-max) save-point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (defmacro marker (pos &optional buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (list 'set-marker '(make-marker) pos buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;;;### autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (defun c-comment-edit (search-prefix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 "Edit multi-line C comments.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 This command allows the easy editing of a multi-line C comment like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 * ...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 * ...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 The comment may be indented or flush with the left margin.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 If point is within a comment, that comment is used. Otherwise the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 comment to be edited is found by searching forward from point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 With one \\[universal-argument] searching starts after moving back one
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 paragraph.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 With two \\[universal-argument]'s searching starts at the beginning of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 current or proceeding C function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 With three \\[universal-argument]'s searching starts at the beginning of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 current page.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 With four \\[universal-argument]'s searching starts at the beginning of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 current buffer (clipping restrictions apply).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 Once located, the comment is copied into a temporary buffer, the comment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 leaders and delimiters are stripped away and the resulting buffer is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 selected for editing. The major mode of this buffer is controlled by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 the variable `c-comment-edit-mode'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 Use \\[c-comment-edit-end] when you have finished editing the comment. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 comment will be inserted into the original buffer with the appropriate
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 delimiters and indention, replacing the old version of the comment. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 you don't want your edited version of the comment to replace the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 original, use \\[c-comment-edit-abort]."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (interactive "*P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (let ((c-buffer (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 marker tem c-comment-fill-column c-comment-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 c-comment-start c-comment-end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (inhibit-quit t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ;; honor search-prefix
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (cond ((equal search-prefix '(4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (backward-paragraph))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ((equal search-prefix '(16))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (end-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (beginning-of-defun)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (backward-paragraph))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ((equal search-prefix '(64))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (backward-page))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ((equal search-prefix '(256))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (goto-char (point-min))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (if (and (null search-prefix) (setq tem (within-c-comment-p)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (setq c-comment-start (marker (car tem))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 c-comment-end (marker (cdr tem)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (let (start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (condition-case error-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (save-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (search-forward "/*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (setq start (- (point) 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (search-forward "*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (setq end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (search-failed (error "No C comment found.")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (setq c-comment-start (marker start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (setq c-comment-end (marker end))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;; calculate the correct fill-column for the comment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (setq c-comment-fill-column (- fill-column
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (goto-char c-comment-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (current-column))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 ;; create the comment buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (setq c-comment-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (generate-new-buffer (concat (buffer-name) " *C Comment Edit*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;; link into the c-comment-edit-buffer-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (setq c-comment-edit-buffer-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (cons (list (current-buffer) c-comment-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 c-comment-start c-comment-end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 c-comment-edit-buffer-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ;; copy to the comment to the comment-edit buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (copy-to-buffer c-comment-buffer (+ c-comment-start 2) (- c-comment-end 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;; mark the position of point, relative to the beginning of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 ;; comment, in the comment buffer. (iff point is within a comment.)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (or search-prefix (< (point) c-comment-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (setq marker (marker (+ (- (point) c-comment-start 2) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 c-comment-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 ;; select the comment buffer for editing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (switch-to-buffer c-comment-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 ;; remove the comment leaders and delimiters
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (and (re-search-forward c-comment-leader-regexp nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (replace-match "" nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (forward-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 ;; run appropriate major mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (funcall (or c-comment-edit-mode 'fundamental-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; override user's default fill-column here since it will lose if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 ;; the comment is indented in the C buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (setq fill-column c-comment-fill-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 ;; delete one leading whitespace char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (if (looking-at "[ \n\t]")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (delete-char 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 ;; restore cursor if possible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (goto-char (or marker (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (set-buffer-modified-p nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ;; run user hook, if present.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (if c-comment-edit-hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (funcall c-comment-edit-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 ;; final admonition
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (substitute-command-keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 "Type \\[c-comment-edit-end] to end edit, \\[c-comment-edit-abort] to abort with no change.")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (defun c-comment-edit-end ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 "End c-comment-edit.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 C comment is replaced by its edited counterpart in the appropriate C buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 Indentation will be the same as the original."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (let ((tuple (find-c-comment-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (if (null tuple)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (error "Not a c-comment-edit buffer."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (let ((inhibit-quit t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (c-comment-c-buffer (car tuple))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (c-comment-buffer (nth 1 tuple))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (c-comment-start (nth 2 tuple))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (c-comment-end (nth 3 tuple)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ((buffer-modified-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ;; rebuild the comment
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (insert "/*\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 (if (string= c-comment-leader " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 (if (not (eolp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 (insert c-comment-leader " "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 (forward-line))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (insert c-comment-leader (if (eolp) "" " "))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (forward-line)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (if (not (char-equal (preceding-char) ?\n))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (insert "\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (insert (if (string= c-comment-leader " *") " */" "*/"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 ;; indent if necessary
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (let ((indention
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (set-buffer c-comment-c-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (goto-char c-comment-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (current-column))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (cond ((not (zerop indention))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 ;; first line is already indented
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 ;; in the C buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (forward-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (indent-to indention)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (forward-line)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 ;; replace the old comment with the new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (set-buffer c-comment-c-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (save-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (delete-region c-comment-start c-comment-end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (goto-char c-comment-start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (set-buffer c-comment-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (append-to-buffer c-comment-c-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (point-min) (point-max))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (t (message "No change.")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 ;; switch to the C buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (if (get-buffer-window c-comment-c-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (select-window (get-buffer-window c-comment-c-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 (switch-to-buffer c-comment-c-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 ;; delete the window viewing the comment buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 (and (get-buffer-window c-comment-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 (delete-window (get-buffer-window c-comment-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 ;; unlink the tuple from c-comment-edit-buffer-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 (setq c-comment-edit-buffer-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (delq tuple c-comment-edit-buffer-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 ;; let Emacs reclaim various resources
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (set-buffer c-comment-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 (kill-buffer c-comment-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 (set-marker c-comment-start nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (set-marker c-comment-end nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 (defun c-comment-edit-abort ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 "Abort a c-comment-edit with no change."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (let* ((tuple (find-c-comment-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (c-comment-c-buffer (car tuple))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (c-comment-buffer (nth 1 tuple))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (c-comment-start (nth 2 tuple))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (c-comment-end (nth 3 tuple)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (if (null tuple)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (error "Not a c-comment-edit buffer."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 ;; switch to the C buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (if (get-buffer-window c-comment-c-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (select-window (get-buffer-window c-comment-c-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (switch-to-buffer c-comment-c-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (let ((inhibit-quit t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (set-buffer c-comment-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (kill-buffer c-comment-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 ;; unlink the tuple from c-comment-edit-buffer-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (setq c-comment-edit-buffer-alist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 (delq tuple c-comment-edit-buffer-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (set-marker c-comment-start nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (set-marker c-comment-end nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (message "Aborted with no change."))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 ;; this loses on /* /* */ but doing it right would be grim.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (defun within-c-comment-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (condition-case error-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (let (start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (save-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (search-backward "/*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (setq start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (search-forward "*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (setq end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (if (< (point) end) (cons start end) nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (search-failed nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 (defun find-c-comment-buffer (&optional buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (or buffer (setq buffer (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (let ((list c-comment-edit-buffer-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (catch 'return-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (while list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (if (eq (nth 1 (car list)) buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (throw 'return-value (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (setq list (cdr list)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (defun find-c-comment-c-buffer (&optional buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (or buffer (setq buffer (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (let ((list c-comment-edit-buffer-alist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (catch 'return-value
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (while list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (if (eq (car (car list)) buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (throw 'return-value (car list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (setq list (cdr list)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 ;; keys;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (define-key mode-specific-map "\e" 'c-comment-edit-end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (define-key mode-specific-map "\C-]" 'c-comment-edit-abort)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301