annotate lisp/modes/c-comment.el @ 211:78478c60bfcd r20-4b4

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