;; $Id: DONc.el,v 2.1 2000/12/04 10:49:30 tag Exp $ ;; Version $Name: R2_0_1 $ ;; ;; DONc.el - C-mode hook for DON Lab usage ;; by T.A. Gonsalves, 4-Aug-94 ;; bug-fixes, TAG, 3-10-98 ;; ;; Last modified: Mon 4-Dec-2000 16:15:09 by tag ;; ;; c-for-loop derived from R.K. Hariram's for-loop ;; (require 'time-stamp) (defvar c-default-access "private" "*Default access specifier for functions and classes") (make-variable-buffer-local 'c-default-access) (defvar c-comment-prefix " *" "String for start of subsequent lines in a comment block") (make-variable-buffer-local 'c-comment-prefix) (defvar c-comment-block-start "/*-------------------------------------------------------------------------" "String for start of a comment block") (make-variable-buffer-local 'c-comment-block-start) (defvar c-comment-block-end " -------------------------------------------------------------------------*/" "String for end of a comment block") (make-variable-buffer-local 'c-comment-block-end) (defvar c-file-comment-start-line 1 "Line on which the file-comment starts") (make-variable-buffer-local 'c-file-comment-start-line) (defvar c-file-comment-body-hook nil "Function for special processing at the end of the top-of-file template") (make-variable-buffer-local 'c-file-comment-body-hook) (defvar c-comment-author-label "Author:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-author-label) (defvar c-comment-version-label "Version:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-version-label) (defvar c-comment-module-label "Module:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-module-label) (defvar c-comment-created-label "Created:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-created-label) (defvar c-comment-args-label "Args:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-args-label) (defvar c-comment-returns-label "Returns:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-returns-label) (defvar c-comment-throws-label "Throws:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-throws-label) (defvar c-comment-bugs-label "Bugs:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-bugs-label) (defvar c-comment-see-label "See:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-see-label) (defvar c-comment-purpose-label "Purpose:" "To allow customisation for Javadoc") (make-variable-buffer-local 'c-comment-purpose-label) ;;; c-comment-blank-line - insert a blank line in a comment block (defun c-comment-blank-line () (insert c-comment-prefix "\n")) ;;; ;;; c-file-comment - setup a comment block template at the top of the file ;;; (defun c-file-comment () "Sets up a comment template at the top of the file" (interactive "*") (save-match-data (goto-char (point-min)) (forward-line c-file-comment-start-line) (if (search-backward c-comment-block-start () t) (error "File already has a comment at the start")) (goto-char (point-min)) (forward-line c-file-comment-start-line) (insert c-comment-block-start "\n") (insert c-comment-prefix " ") (insert (relative-buffer-file-name)) (insert " - ") (save-excursion (insert "\n" c-comment-prefix " " c-comment-version-label "\t$Name")(insert "$") (insert "\n" c-comment-prefix " " c-comment-module-label "\t") (insert "\n" c-comment-prefix "\n" c-comment-prefix " " c-comment-purpose-label "\t\n") (insert c-comment-prefix " " c-comment-see-label "\t\n") (c-comment-blank-line) (insert c-comment-prefix " " c-comment-author-label "\t" (user-full-name) " (" user-mail-address ")\n") (c-comment-blank-line) (insert c-comment-prefix " " c-comment-created-label " " (format-time-string "%a %d-%b-%Y %H:%M:%S")) (insert "\n" c-comment-prefix " Last modified: " (format-time-string "%a %d-%b-%Y %H:%M:%S") " by " (getenv "USER") "\n") (insert c-comment-prefix " $Id") (insert "$\n") ; split to fool CVS/RCS! (c-comment-blank-line) (insert c-comment-prefix " " c-comment-bugs-label "\t\n") (insert c-comment-prefix "\n" c-comment-prefix " Change Log:\t \n") (insert c-comment-prefix " \t\t\n") (insert c-comment-block-end "\n\n") (if c-file-comment-body-hook (run-hooks 'c-file-comment-body-hook)) (goto-char (point-max)) (insert "\n\n") (insert c-comment-block-start "\n") (insert c-comment-prefix " $Log")(insert "$\n") ; split to fool CVS/RCS! (insert c-comment-prefix "\n") ;;; Note: local variables below must match setq in c-set-local-variables (insert c-comment-prefix " Local Variables:\n") (insert c-comment-prefix " time-stamp-active: t\n") (insert c-comment-prefix " time-stamp-line-limit: 20\n") (insert c-comment-prefix " time-stamp-start: \"Last modified:[ ]+\"\n") (insert c-comment-prefix " time-stamp-format: \"%3a %02d-%3b-%:y %02H:%02M:%02S by %u\"\n") (insert c-comment-prefix " time-stamp-end: \"$\"\n") (insert c-comment-prefix " End:\n") (insert c-comment-prefix " End of ") (insert (relative-buffer-file-name)) (insert "\n") (insert c-comment-block-end "\n"))) nil) ;;; Note: setq's below must match local variables set in c-file-comment (defun c-set-local-variables () (progn (make-local-variable 'time-stamp-active) (setq time-stamp-active t) (make-local-variable 'time-stamp-line-limit) (setq time-stamp-line-limit 20) (make-local-variable 'time-stamp-start) (setq time-stamp-start "Last modified:[ ]+") (make-local-variable 'time-stamp-format) (setq time-stamp-format "%3a %:d-%3b-%:y %02H:%02M:%02S by %u") (make-local-variable 'time-stamp-end) (setq time-stamp-end "$"))) (defun relative-buffer-file-name () "Returns buffer-file-name with path stripped off" (string-match "\\([^/]*\\)$" buffer-file-name) (match-string 1 buffer-file-name)) (defun base-buffer-file-name () "Returns buffer-file-name with path and extension stripped off" (let ((relName (relative-buffer-file-name))) (string-match "^\\([^.]*\\)\\.?" relName) (match-string 1 relName))) (defun c-class-comment (className &optional accessp) "Sets up a comment template at the start of a class" (interactive "*sEnter class name: ") (let ((accessType "")) (if accessp (setq accessType (read-string "Enter access type (private/public/protected): "))) (if (not (bolp)) (end-of-line)) (insert "\n" c-comment-block-start "\n") (insert c-comment-prefix " " className " -- ") (save-excursion (insert "\n" c-comment-prefix "\n") (insert c-comment-prefix " " c-comment-author-label "\t" (user-full-name) " (" user-mail-address ")\n") (insert c-comment-prefix " " c-comment-version-label "\t$Name")(insert "$") (insert "\n" c-comment-prefix " " c-comment-see-label "\t\n") (insert c-comment-prefix " " c-comment-bugs-label "\t") (insert "\n" c-comment-block-end "\n") (if accessp (insert accessType " ")) (insert "class " className) (DONc-statement-block) (insert "\t" comment-start " End of " className "\t\tEnd of " className " " comment-end) (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (insert "//---------------- Member Variables ----------------") (newline-and-indent) (insert "// public:\n\n") (newline-and-indent) (insert "// private:\n\n") (insert "//---------------- Public Member Functions ----------------") (newline-and-indent) (insert "public " className "()") (DONc-statement-block) (insert "\n\n") (insert "//---------------- Private Member Functions ----------------") (insert "\n\n"))) ) ; c-class-comment (defun c-function-comment (funName retType) "Sets up a comment template at the start of a function" (interactive "*sEnter function name: \nsEnter return type: ") (if (string= retType "") (setq retType "void")) (if (not (bolp)) (end-of-line)) (insert "\n" c-comment-block-start "\n") (insert c-comment-prefix " " funName " -- ") (save-excursion (insert "\n" c-comment-prefix " " c-comment-args-label "\t\n" c-comment-prefix " " c-comment-returns-label "\t") (if (string= retType "void") (insert "Nothing") (insert retType)) (insert "\n" c-comment-prefix " " c-comment-throws-label "\t") (insert "\n") (insert c-comment-prefix " " c-comment-see-label "\t\n") (insert c-comment-prefix " " c-comment-bugs-label "\t") (insert "\n" c-comment-block-end "\n") (insert c-default-access " " retType " " funName "()") (DONc-statement-block) (insert "\t" comment-start " End of " funName "\t\tEnd of " funName " " comment-end) (if (not c-auto-newline) (newline-and-indent)))) (defun c-do-while-loop (expr) (interactive "sDo..while expression: ") (insert "do") (indent-for-comment) (insert " do..while (" expr ") ") (end-of-line) (DONc-statement-block) (insert " while (" expr ");") (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (funcall indent-line-function) ) (defun c-while-loop (expr) (interactive "sWhile expression: ") (insert (concat "while (" expr ")")) (DONc-statement-block) (insert " " comment-start " while (" expr ") " comment-end) (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (funcall indent-line-function) ) (defun c-if-then (expr) (interactive "sIf..then expression: ") (insert (concat "if (" expr ")")) (DONc-statement-block) (insert " " comment-start " if (" expr ") " comment-end) (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (funcall indent-line-function) ) (defun c-if-then-else (expr) (interactive "sIf..else expression: ") (insert (concat "if (" expr ")")) (DONc-statement-block) (newline-and-indent) (insert "else") (indent-for-comment) (insert " not (" expr ") ") (end-of-line) (DONc-statement-block) (insert " " comment-start " if..else (" expr ") " comment-end) (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (funcall indent-line-function) ) (defun c-for-loop (c1 c2 c3) (interactive "sFor initial: \nsFor condition: \nsFor increment: ") (insert (concat "for (" c1 "; " c2 "; " c3 ")")) (DONc-statement-block) (insert (concat " " comment-start " for (.." c2 "..) " comment-end)) (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (funcall indent-line-function) ) (defun c-switch (expr) (interactive "sSwitch expression: ") (insert (concat "switch (" expr ")")) (DONc-statement-block) (insert (concat " " comment-start " switch (" expr ") " comment-end)) (if c-auto-newline (previous-line 1) (progn (newline-and-indent) (previous-line 2))) (funcall indent-line-function) (insert "default: ") (previous-line 1) (end-of-line) (newline-and-indent) ) (defun DONc-statement-block () "Insert {\n\n} with proper indentation" (newline-and-indent) (DONc-brace "{") (newline-and-indent) (if (not c-auto-newline) (newline-and-indent)) (DONc-brace "}") (if c-auto-newline (progn (previous-line 1) (end-of-line)))) ;; DONc-brace -- from electric-c-brace (c-mode.el) ;; modified to remove self-insert-command ;; arg is the brace to insert ("{" or "}") ;; (defun DONc-brace (arg) "Insert character and correct line's indentation." (let (insertpos) (if (and (eolp) (or (save-excursion (skip-chars-backward " \t") (bolp)) (if c-auto-newline (progn (funcall indent-line-function) (newline) t) nil))) (progn (insert arg) (funcall indent-line-function) (if c-auto-newline (progn (newline) ;; (newline) may have done auto-fill (setq insertpos (- (point) 2)) (funcall indent-line-function))) (save-excursion (if insertpos (goto-char (1+ insertpos))) (delete-char -1)))) (if insertpos (save-excursion (goto-char insertpos) (insert arg)) (insert arg)))) ;;; To go to matching parenthesis From vi.el!!! (defun vi-find-matching-paren () "Locate the matching paren. It's a hack right now." (interactive) (cond ((looking-at "[[({]") (forward-sexp 1) (backward-char 1)) ((looking-at "[])}]") (forward-char 1) (backward-sexp 1)) (t (ding)))) (defun DONc-setup () "Set indentation style a la DONLab and bind keys" (interactive) (let ((my-map (if (string= mode-name "C++") 'c++-mode-map (if (string= mode-name "Java") 'java-mode-map 'c-mode-map)))) (add-hook 'write-file-hooks 'time-stamp) (setq c-indent-level 2) (setq c-continued-statement-offset 4) (setq c-continued-brace-offset -4) (setq c-brace-offset 2) (setq c-argdecl-indent 4) (setq c-label-offset -2) (setq c-auto-newline nil) (eval (list 'define-key my-map "\C-cs" ''c-function-comment)) (eval (list 'define-key my-map "\ep" ''vi-find-matching-paren)) (eval (list 'define-key my-map "\C-c<" ''gud-up)) (eval (list 'define-key my-map "\C-c>" ''gud-down)) (eval (list 'define-key my-map "\C-c\C-b" ''gud-break)) (eval (list 'define-key my-map "\C-c\C-d" ''gud-remove)) (eval (list 'define-key my-map "\C-c\C-f" ''gud-finish)) (eval (list 'define-key my-map "\C-c\C-i" ''gud-stepi)) (eval (list 'define-key my-map "\C-c\C-n" ''gud-next)) (eval (list 'define-key my-map "\C-c\C-p" ''gud-print)) (eval (list 'define-key my-map "\C-c\C-r" ''gud-cont)) (eval (list 'define-key my-map "\C-c\C-s" ''gud-step)) (eval (list 'define-key my-map "\C-c\C-t" ''gud-tbreak)) (eval (list 'define-key my-map "\C-cc" ''c-class-comment)) (eval (list 'define-key my-map "\C-cd" ''c-do-while-loop)) (eval (list 'define-key my-map "\C-ce" ''c-if-then-else)) (eval (list 'define-key my-map "\C-cf" ''c-for-loop)) (eval (list 'define-key my-map "\C-ci" ''c-if-then)) (eval (list 'define-key my-map "\C-cs" ''c-switch)) (eval (list 'define-key my-map "\C-cw" ''c-while-loop)) (eval (list 'define-key my-map "\C-ct" ''c-file-comment)) (eval (list 'define-key my-map "\C-cu" ''c-function-comment)) (c-set-local-variables) )) (provide 'DONc) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; $Log: DONc.el,v $ ;;; Revision 2.1 2000/12/04 10:49:30 tag ;;; Bugfix: was putting the Perl interpreter name after the file comment ;;; ;;; Revision 2.0 2000/11/11 09:54:02 tag ;;; Made DONel into a module, added README and INSTALL files ;;; ;;; Revision 1.10 2000/11/10 12:35:22 tag ;;; Corrected some typos ;;; ;;; Revision 1.9 2000/11/10 11:36:42 tag ;;; Added Version tag to DON\*.el ;;; ;;; Revision 1.8 2000/11/10 11:27:42 tag ;;; Modified to handle javadoc-style comments ;;; ;;; Revision 1.7 2000/02/03 11:41:40 tag ;;; Bug: was inserting top comment after first line ;;; ;;; Revision 1.6 2000/02/03 11:33:54 tag ;;; Added $Name: R2_0_1 $ to c-file-comment ;;; ;;; Revision 1.5 1999/10/15 13:05:07 tag ;;; Modified DONc-setup to use the appropriate keymap for C/C++/Java ;;; ;;; Revision 1.4 1999/09/18 06:52:28 tag ;;; In c-file-comment: set time-stamp-* variables ;;; ;;; Revision 1.3 1999/09/18 06:25:33 tag ;;; Added time-stamp, made c-file-comment callable from perl-file-comment ;;; ;;; Revision 1.2 1998/10/27 10:38:51 tag ;;; Fixed problem in CVS keywords that appear in the code ;;; ;;; Revision 1.1 1998/10/16 06:25:56 tag ;;; Templates for DONlab coding style ;;; ;;; Local Variables: ;;; time-stamp-active: t ;;; time-stamp-line-limit: 20 ;;; time-stamp-start: "Last modified:[ ]+" ;;; time-stamp-format: "%3a %:d-%3b-%:y %02H:%02M:%02S by %u" ;;; time-stamp-end: "$" ;;; End: ;;; End of DONc.el -----------------------------------------------