1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
;;; C comment mode - An auto-filled comment mode for gnu c-mode.
;;;
;;; Author: Robert Mecklenburg
;;; Computer Science Dept.
;;; University of Utah
;;; From: mecklen@utah-gr.UUCP (Robert Mecklenburg)
;;; Also hartzell@Boulder.Colorado.EDU
;;; (c) 1986, University of Utah
;;;
;;; Everyone is granted permission to copy, modify and redistribute
;;; this file, provided the people they give it to can.
;;; Synched up with: Not in FSF.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; I have written a "global comment" minor-mode which performs auto-fill,
;;; fill-paragraph, and auto-indentation functions. This function only
;;; works for comments which occupy an entire line (not comments to the
;;; right of code). The mode has several options set through variables.
;;; If the variable c-comment-starting-blank is non-nil multi-line
;;; comments come out like this:
;;;
;;; /*
;;; * Your favorite
;;; * multi-line comment.
;;; */
;;;
;;; otherwise they look like this:
;;;
;;; /* Your Favorite
;;; * multi-line comment.
;;; */
;;;
;;; If the variable c-comment-hanging-indent is non-nil K&R style comments
;;; are indented automatically like this:
;;;
;;; /* my_func - For multi-line comments with hanging indent
;;; * the text is lined up after the dash.
;;; */
;;;
;;; otherwise the text "the text" (!) is lined up under my_func. If a
;;; comment fits (as typed) on a single line it remains a single line
;;; comment even if c-comment-starting-blank is set. If
;;; c-comment-indenting is non-nil hitting carriage return resets the
;;; indentation for the next line to the current line's indentation
;;; (within the comment) like this:
;;;
;;; /* Typing along merrily....
;;; * Now I indent with spaces, when I hit return
;;; * the indentation is automatically set to
;;; * ^ here.
;;; */
;;;
;;; Due to my lack of understanding of keymaps this permanently resets M-q
;;; to my own fill function. I would like to have the comment mode
;;; bindings only in comment mode but I can't seem to get that to work.
;;; If some gnu guru can clue me in, I'd appreciate it.
;;;
(defvar c-comment-starting-blank t
"*Controls whether global comments have an initial blank line.")
(defvar c-comment-indenting t
"*If set global comments are indented to the level of the previous line.")
(defvar c-comment-hanging-indent t
"*If true, comments will be automatically indented to the dash.")
(defvar c-hang-already-done t
"If true we have performed the haning indent already for this comment.")
;;;
;;; c-comment-map - This is a sparse keymap for comment mode which
;;; gets inserted when c-comment is called.
;;;
(defvar c-comment-mode-map ()
"Keymap used in C comment mode.")
(if c-comment-mode-map
()
(setq c-comment-mode-map (copy-keymap c-mode-map))
(define-key c-comment-mode-map "\e\r" 'newline)
(define-key c-comment-mode-map "\eq" 'set-fill-and-fill)
(define-key c-comment-mode-map "\r" 'set-fill-and-return))
;;;
;;; c-comment - This is a filled comment mode which can format
;;; indented text, do hanging indents, and symetric
;;; placement of comment delimiters.
;;;
(defun c-comment ()
"Edit a C comment with filling and indentation.
This performs hanging indentation, symmetric placement of delimiters,
and Indented-Text mode style indentation. Type 'M-x apropos
c-comment' for information on options."
(interactive)
(let
;; Save old state.
((auto-fill-function (if c-comment-indenting
'do-indented-auto-fill 'do-auto-fill))
; (comment-start nil)
(comment-multi-line t)
(comment-start-skip "/*\\*+[ ]*")
(paragraph-start-ref paragraph-start)
fill-prefix paragraph-start paragraph-separate opoint)
;; Determine if we are inside a comment.
(setq in-comment
(save-excursion
(and (re-search-backward "/\\*\\|\\*/" 0 t)
(string= "/*" (buffer-substring (point) (+ (point) 2))))))
;; Indent the comment and set the fill prefix to comment continuation
;; string. If we are already in a comment get the indentation on
;; the current line.
(setq c-hang-already-done nil)
;; Set the beginning of the comment and insert the blank line if needed.
(use-local-map c-comment-mode-map)
(if (not in-comment)
(progn (c-indent-line)
(insert "/* ")
(setq fill-prefix (get-current-fill (point)))
(recursive-edit)
;; If the comment fits on one line, place the close
;; comment at the end of the line. Otherwise, newline.
(setq opoint (point))
(if (and (save-excursion (beginning-of-line)
(search-forward "/*" opoint t))
(<= (+ (current-column) 3) 79))
(insert " */")
(insert "\n*/"))
(c-indent-line))
(progn (setq fill-prefix (get-current-fill (point)))
(recursive-edit)
(search-forward "*/" (buffer-size) t)
(forward-line 1)))
;; If starting blank enabled, insert a newline, etc., but only if
;; this comment requires multiple lines.
(if c-comment-starting-blank
(save-excursion
(setq opoint (point))
(forward-line -1)
(if (or (null (search-forward "/*" opoint t))
(null (search-forward "*/" opoint t)))
(progn
(search-backward "/*")
(re-search-forward comment-start-skip opoint t)
(setq fill-prefix (get-current-fill (point)))
(if (not (looking-at "\n"))
(insert ?\n fill-prefix))))))
; (indent-new-comment-line))))))
;; Move cursor to indentation.
(c-indent-line)
(use-local-map c-mode-map)
)
)
;;;
;;; set-fill-and-fill - Get the current fill for this line and fill
;;; the paragraph.
;;;
(defun set-fill-and-fill (arg)
"Get the fill-prefix and fill the current paragraph."
(interactive "P")
(setq fill-prefix (get-current-fill (point)))
(fill-paragraph arg))
;;;
;;; set-fill-and-return - Set the current fill prefix and
;;; indent-new-comment-line.
;;;
(defun set-fill-and-return ()
"Set the current fill prefix and move to the next line."
(interactive)
(if c-comment-indenting
(setq fill-prefix (get-current-fill (point))))
(insert ?\n fill-prefix))
;;;
;;; do-indented-auto-fill - Perform the auto-fill function, but get
;;; the fill-prefix first.
;;;
(defun do-indented-auto-fill ()
"Perform auto-fill, but get fill-prefix first."
(let ((opoint (point)))
(save-excursion
(move-to-column (1+ fill-column))
(skip-chars-backward "^ \t\n")
(if (bolp)
(re-search-forward "[ \t]" opoint t))
;; If there is a space on the line before fill-point,
;; and nonspaces precede it, break the line there.
(if (save-excursion
(skip-chars-backward " \t")
(not (bolp)))
;; If we are wrapping to a new line, figure out the indentation on
;; the current line first.
(progn
(setq fill-prefix (get-current-fill opoint))
(insert ?\n fill-prefix)))))
; (indent-new-comment-line)))))
)
;;;
;;; get-current-fill - Get the fill-prefix for the current line. This
;;; assumes that the valid fill prefix is between
;;; (beginning-of-line) and (point).
;;;
(defun get-current-fill (pnt)
"Get the current fill prefix.
A valid fill prefix must be between the beginning of the line and point."
(let ((opoint pnt) fill last-char)
(save-excursion
(beginning-of-line)
(setq fill
(buffer-substring (point)
(progn
(re-search-forward comment-start-skip opoint t)
(point))))
;; Be sure there is trailing white space.
(setq last-char (substring fill (1- (length fill)) (length fill)))
(if (and (not (string= " " last-char))
(not (string= " " last-char)))
(setq fill (concat fill " ")))
(setq fill (replace-letter fill "/" " "))
;; Get the hanging indentation if we haven't already.
(if (and c-comment-hanging-indent (not c-hang-already-done))
(let ((curr (point))
(opnt (progn (end-of-line) (point))))
(beginning-of-line)
(if (search-forward " - " opnt t)
(progn
(setq fill (concat fill (make-string (- (point) curr) 32)))
(setq c-hang-already-done t)))))
;; Set the paragraph delimiters.
(setq paragraph-start (concat paragraph-start-ref
"\\|^"
(regexp-quote
(substring fill
0 (1- (length fill))))
"$"))
(setq paragraph-separate paragraph-start))
fill)
)
;;;
;;; replace-letter - Given a string, an old letter and a new letter,
;;; perform the substitution.
;;;
(defun replace-letter (str old-letter new-letter)
(let (new-str c
(sp 0)
(size (length str)))
(while (< sp size)
(setq c (substring str sp (1+ sp)))
(setq new-str (concat new-str (if (string= c old-letter) new-letter c)))
(setq sp (1+ sp)))
new-str))
|