home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / c-support.el < prev    next >
Encoding:
Text File  |  1990-03-21  |  25.7 KB  |  726 lines

  1. ;From ark1!nap1!ames!sun-barr!lll-winken!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!indetech.com!lrs Mon Nov  6 13:04:18 1989
  2. ;Article 356 of gnu.emacs.bug
  3. ;Path: ark1!nap1!ames!sun-barr!lll-winken!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!indetech.com!lrs
  4. ;>From lrs@indetech.com (Lynn Slater)
  5. ;Newsgroups: gnu.emacs.bug
  6. ;Subject: C and C++ function headers. Table of contents. Fcn synopsys extraction
  7. ;Message-ID: <m0gNXRu-0000F7C@fire.indetech.com>
  8. ;Date: 4 Nov 89 20:57:00 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Distribution: gnu
  11. ;Organization: GNUs Not Usenet
  12. ;Lines: 710
  13. ;
  14. ;> Lastly, it would be nice to have a way to comment function headers
  15. ;> automatically . Do you have something like this?
  16. ;
  17. ;Yep, but it is not up to general usage quality.  Emacs hackers are welcome.
  18. ;Please mail as well as post (if applicable) any replies as our gnu news feed
  19. ;may be down.
  20. ;
  21. ;===============================================================
  22. ;Lynn Slater -- lrs@indetech.com or {sun, ames, pacbell}!indetech!lrs
  23. ;42075 Lawrence Place, Fremont Ca 94538
  24. ;Office (415) 438-2048; Home (415) 796-4149; Fax (415) 438-2034
  25. ;===============================================================
  26. ;
  27. ;-*- File: ~/local/c-support.el
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ;; c-support.el -- Partial support for team C or C++ development
  30. ;; Copyright (c) 1989, Lynn R. Slater Jr.
  31. ;; Author          : Lynn Slater
  32. ;; Created On      : Tue Aug  8 12:26:17 1989
  33. ;; Last Modified By: Lynn Slater
  34. ;; Last Modified On: Sat Nov  4 12:50:16 1989
  35. ;; Update Count    : 46
  36. ;; Status          : Alpha released
  37. ;; 
  38. ;; HISTORY
  39. ;; PURPOSE
  40. ;;    This file provides:
  41. ;; 1) Function header templates for C++ or C. C++ is better supported. 
  42. ;; 2) Fcn synopsys maintenance.
  43. ;; 3) Table of contents maintenacne (lisp amd make file also).
  44. ;; 4) Help-for help like support available.
  45. ;; 5) C++ to C comment conversion
  46. ;; 6) Command line batch maintanance operations
  47. ;; 7) Main and module insertation of ident strings.
  48. ;; 8) Highlight, selection, and replacement of "templates" much like some
  49. ;;    of the dumb sun editors.
  50. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  51.  
  52. ;; Warning: this file is not yet ready for insertion into just any emacs.
  53. ;; Only someone with some experience and patience should try it as I have
  54. ;; not yet seperated out local site quirks or otherwise sanitized this file.
  55. ;; It is also not as customizable as I would like. That is why this is an
  56. ;; "alpha" release and not a "beta". Improvements are welcome.
  57.  
  58. ;; Save as 'c-support and byte-compile.
  59. (provide 'c-support)
  60. (require 'header)    ;; Package from lrs@indetech.com
  61.  
  62. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63. ;; Function headers.
  64. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  65. (defun header-prefix-sstring ()
  66.   "Returns the header prefix string stripped of trailing blanks"
  67.   (let* ((hps (header-prefix-string))
  68.      (match (string-match "[ \t]+$" hps)))
  69.     (if match (substring hps 0  match) hps)))
  70.  
  71. (defun document-c-function (note-copyright)
  72.   "Insert a comment block containing the module title, author, etc.  To
  73.    use, place the cursor on or before the line starting the function definition.
  74.  
  75.    Functions with these headers can be automatically placed into the table
  76.    of contents by the update-table-of-contents command.
  77.  
  78.    The synopsis can be automatically constructed by the
  79.    update-function-synopsis (\\[update-function-synopsis]) command.
  80.  
  81.    If given a prefix arg, make a copyright notice instead of an Author: entry.
  82.  
  83.    Note: this currently works much better with c++ than it does with C."
  84.   (interactive "P")
  85.   (let* ((start-col (current-column))
  86.      (fname (next-c-function-name))
  87.      header-prefix-string
  88.      new-point)
  89.     (beginning-of-line)
  90.     (setq header-prefix-string
  91.       (concat (buffer-substring (point)
  92.                     (progn (indent-to start-col) (point)))
  93.           (header-prefix-sstring)))
  94.     (make-divisor)
  95.     (insert "\n")
  96.     (indent-to start-col)
  97.     (insert comment-start fname " -- \n")
  98.     (setq new-point (1- (point)))
  99.     (insert header-prefix-string
  100.         (if note-copyright
  101.         (concat "Copyright "
  102.             (substring (current-time-string)
  103.                    -4)
  104.             " ")
  105.           " AUTHOR:      ")
  106.         (user-full-name)
  107.         " <" (user-login-name) "@" (system-name)
  108.         ">\n")
  109.     (insert
  110.         header-prefix-string " SYNOPSIS\n"
  111.         header-prefix-string " DESCRIPTION\n"
  112.         header-prefix-string "   |>Description of function<|\n"
  113.         header-prefix-string " NOTES\n"    
  114.         header-prefix-string "   |><|\n"
  115.         header-prefix-string " CAVEATS AND BUGS\n"
  116.         header-prefix-string "   |> describe any peculiarities <|\n"
  117.         )
  118.     (if (and comment-end (not (string-equal comment-end "")))
  119.     (progn
  120.       (indent-to start-col)
  121.       (insert comment-end "\n")))
  122.     (forward-line -1)
  123.     (update-function-synopsis)
  124.     (goto-char new-point)
  125.     ))
  126.  
  127. (defun next-c-function-name ()
  128.   "Returns the name of the next c function.
  129.   Should only be called from before a function as it cannot reliable  tell
  130.   function calls from function definitions and certain c constructs such as
  131.   for and while loops." 
  132.   (save-excursion
  133.     (if (re-search-forward "\\(\\sw\\|\\s_\\|::\\)+[ \t]*(" nil t)
  134.     (buffer-substring (match-beginning 1) (match-end 1))
  135.       "")))
  136.  
  137. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  138. ;; Function synopsys extraction
  139. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  140. (defun delete-synopsis ()
  141.   "Deletes current synopsis, places cursor at start of next line"
  142.   (interactive)
  143.   (let* ((header-prefix-pat (concat "^[ \t]*" (regexp-quote (header-prefix-string))))
  144.      (synopsis-pat (concat header-prefix-pat "SYNOPSIS")))
  145.     (beginning-of-line)
  146.     ;; Move back to synopsis line
  147.     (while (and (not (looking-at synopsis-pat)) (looking-at header-prefix-pat) (forward-line -1)))
  148.     (if (not (looking-at synopsis-pat))
  149.     (progn
  150.       (forward-line 1)
  151.       ;; Move forward to synopsis line
  152.       (while (and (not (looking-at synopsis-pat)) (looking-at header-prefix-pat) (forward-line 1)))))
  153.     (beginning-of-line)
  154.     (if (looking-at synopsis-pat)
  155.     (progn
  156.       ;; we have the start of a synopsis
  157.       (forward-line 1)
  158.       (delete-region (point)
  159.              (progn
  160.                (while (and (looking-at (concat header-prefix-pat
  161.                                "[\n \t]"))
  162.                        (forward-line 1)))
  163.                (point)
  164.                ))
  165.       t
  166.       ))))
  167.  
  168. (defun update-function-synopsis ()
  169.   "Updates the synopsis in a function header.  Grabs the function
  170.   declaration (up to the first bracket in c++ or the first blank in c),
  171.   converts comments, and inserts it under the synopsis line.
  172.  
  173.   Removes the old synopsis."
  174.   (interactive)
  175.   (if (delete-synopsis)
  176.       (let* ((header-prefix-sstring (header-prefix-sstring))
  177.          (prefix (buffer-substring
  178.               (point)
  179.               (progn (back-to-indentation)
  180.                  (if (char-equal
  181.                   (aref header-prefix-sstring 0) 32)
  182.                  (backward-char 1))
  183.                  (point))))
  184.          (here (progn (beginning-of-line) (point)))
  185.          (header-prefix-pat (concat "^[ \t]*"
  186.                     (regexp-quote header-prefix-sstring)))
  187.          fcn-decl
  188.          start)
  189.     (beginning-of-line)
  190.     ;; skip to start of defn
  191.     (while (and (looking-at header-prefix-pat) (forward-line 1)))
  192.     (setq start (point))
  193.     (if (re-search-forward "\\(\\sw\\|\\s_\\|::\\|<<\\)+[ \t]*(" nil t)
  194.         (progn
  195.           (goto-char (1- (match-end 0)))
  196.           (forward-sexp)   ;; Skip args. In C++, we are done
  197.           (if (eq major-mode 'c-mode)
  198.           (progn
  199.             ;; In C, we need to find the declarations.
  200.             ;; Heiristic: Skip to blank or a '{'
  201.             (re-search-forward "\\(^[ \t]*$\\|\\{\\)")
  202.             (goto-char (1- (match-beginning 0)))))
  203.           (setq fcn-decl (buffer-substring start (point)))
  204.           (goto-char here)
  205.           (insert fcn-decl "\n")
  206.           (insert-box here (point)
  207.               (concat prefix header-prefix-sstring "   "))
  208.           )
  209.       (error "No function defn here!")))))
  210.  
  211. (defun update-all-synopsis ()
  212.   "Updates the synopsys of all function headers."
  213.   (interactive)
  214.   (save-excursion
  215.     (beginning-of-buffer)
  216.     (while (re-search-forward (concat (regexp-quote (header-prefix-string)) "SYNOPSIS") nil t)
  217.       (update-function-synopsis)
  218.       (sit-for 0)
  219.       )
  220.     (message "All Function synopsis updated.")
  221.     ))
  222.  
  223. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  224. ;; Useful things for all emacs
  225. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  226. (defun insert-box (start end text)
  227.   "Insert a text prefix at a column in all the lines in the region.
  228.    Called from a program, takes three arguments, START, END, and TEXT.
  229.    The column is taken from that of START.
  230.    The rough inverse of this function is kill-rectangle."
  231.   (interactive "r\nsText To Insert: ")
  232.   (save-excursion
  233.     (let (cc)
  234.       ;; the point-marker stuff is needed to keep the edits from changing
  235.       ;; where end is
  236.       (goto-char end)
  237.       (setq end (point-marker))
  238.       (goto-char start)
  239.       (setq cc  (current-column))
  240.       (while (< (point) end) ;; modified 2/2/88
  241.     ;; I should here check for tab chars
  242.     (insert text)
  243.     (forward-line 1)
  244.     (move-to-column-force cc)) ;; Alternate: use move-to-column if you must.
  245.       (move-marker end nil))))
  246.  
  247. (defun insert-end (start end text)
  248.   "Insert a text prefix at the end in all the lines in the region.
  249.    Called from a program, takes three arguments, START, END, and TEXT.
  250.    The column is taken from that of START."
  251.   (interactive "r\nsText To Insert: ")
  252.   (save-excursion
  253.     (let (cc)
  254.       ;; the point-marker stuff is needed to keep the edits from changing
  255.       ;; where end is
  256.       (goto-char end)
  257.       (setq end (point-marker))
  258.       (goto-char start)
  259.       (end-of-line)    
  260.       (while (< (point) end);; modified 2/2/88
  261.     ;; I should here check for tab chars
  262.     (insert text)
  263.     (forward-line 1)
  264.     (end-of-line)    
  265.     )
  266.       (move-marker end nil))))
  267.  
  268. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  269. ;; Table of contents maintenance.  Works for make mode and lisp files as
  270. ;; well as C and C++.
  271. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  272. (defun update-table-of-contents ()
  273.   "Updates the table of contents in the file header.
  274.    Removes the old table of contents."
  275.   (interactive)
  276.   (if (header-goto-table-of-contents)
  277.       (save-excursion
  278.     (let ((insert-point (point))
  279.           (header-prefix-string (header-prefix-string))
  280.           str)
  281.       (while (setq str (get-next-function-description))
  282.         (save-excursion
  283.           (goto-char insert-point)
  284.           (insert "\n" header-prefix-string "  " str)
  285.           (setq insert-point (point))))
  286.       ;; Now, get rid of the old table of contents
  287.       (goto-char insert-point)
  288.       (insert "\n" header-prefix-string "  ")
  289.       (setq str (regexp-quote (buffer-substring (1+ insert-point) (point))))
  290.       (beginning-of-line)
  291.       (while (looking-at str)
  292.         (kill-line 1))
  293.       (message "Table of contents updated.")
  294.       ))
  295.     (error "There is no table of contents!")))
  296.  
  297. (defun get-next-function-description ()
  298.   "Returns the line defining or describing the next function.
  299.    What a 'function' is depends upon the mode."
  300.   (cond
  301.    ((eq major-mode 'emacs-lisp-mode)
  302.     ;; All defuns, defvars, defmacros go to table of contents
  303.     (if (re-search-forward "^(def.*$" nil t)
  304.     (buffer-substring (match-beginning 0) (match-end 0))))
  305.    ((eq major-mode 'make-mode)
  306.     ;; In make mode, a "function" is a target. However, the only ones we
  307.     ;; notice are those with not-null actions.
  308.     (if (re-search-forward "^\\([^# \t\=\n]* *:\\).*\n\t" nil t)
  309.     (buffer-substring (match-beginning 1) (match-end 1))))
  310.    (t;; Presume C or C++. Only notice those fcns marked with a standard
  311.     ;; fcn header. 
  312.     (let ((qhps (regexp-quote (header-prefix-string)))
  313.       (chps (regexp-quote comment-start)))
  314.       (if (re-search-forward (concat chps "\\(.*\\)[ \t]*\n.*" qhps "AUTHOR") nil t)
  315.       (buffer-substring (match-beginning 1 ) (match-end 1)))))))
  316.  
  317. (defun header-goto-table-of-contents ()
  318.   "Moves to the table of contents in the header"
  319.   (interactive)
  320.   (let ((qhps (regexp-quote (header-prefix-string)))
  321.     (here (point)))
  322.     (beginning-of-buffer)
  323.     (if (re-search-forward (concat qhps "TABLE OF CONTENTS") nil t)
  324.     (point)
  325.       (goto-char here)
  326.       (beep)
  327.       (message "There is no table of contents!")
  328.       nil)))
  329.  
  330. (defun header-goto-purpose ()
  331.   "Moves to the purpose in the header"
  332.   (interactive)
  333.   (let ((qhps (regexp-quote (header-prefix-string)))
  334.     (here (point)))
  335.     (beginning-of-buffer)
  336.     (if (re-search-forward (concat qhps "PURPOSE") nil t)
  337.     (progn
  338.       (skip-chars-forward "/ \t\n"
  339.                   (save-excursion (forward-line 2) (1- (point))))
  340.       (point))
  341.       (goto-char here)
  342.       (beep)
  343.       (message "There is no purpose in the header! :->")
  344.       nil)))
  345.  
  346. (defun header-goto-end ()
  347.   "Moves to the end of the header box"
  348.   (interactive)
  349.   (beginning-of-buffer)
  350.   (forward-line 1)
  351.   (let ((hps (regexp-quote (header-prefix-sstring))))
  352.     (while (looking-at hps)
  353.       (forward-line 1))))
  354.  
  355. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  356. ;; Easy to use help.
  357. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  358. (defun help-for-c-templates ()
  359.   "You have discovered 'C-ct', the C and C++ templates facility.  
  360.    All templates start with this sequence. From there, type a templates option:
  361.  
  362. f, C-f  Document a C function
  363. s, C-s  Update the synopsis of a C function
  364.    C-h  Make a file header
  365. r, C-r  Document a revision to the file
  366. c       Goto table of contents
  367.    C-c  Update the table of contents 
  368. t, C-t  Move among and edit the |> <| constructs.
  369. b, C-b  Make a box comment
  370. d, C-d  Make a visual dividing line
  371. e, C-e  Goto end of header
  372. p, C-p  Goto purpose in header
  373. m       Insert AFS c or c++ module header code line
  374. C-m     Insert AFS c or c++ main   header code line
  375.         (There are no special header code liens for include files.)
  376.  
  377. New Feature: \\[remake-header] -- Insert a new header, copy details from
  378.              old header.  May still need hand cleanup afterwards.
  379.  
  380. Please use \\[describe-key] to find out more about any of these keys."
  381.   (interactive)
  382.   (let ((line-prompt
  383.      (substitute-command-keys (concat "f s t b d e p m C-m C-h C-r c C-c. Type ? for more help: "))))
  384.     (message line-prompt)
  385.     (let ((char (read-char)))
  386.       (if (or (= char ??) (= char help-ch))
  387.       (save-window-excursion
  388.         (switch-to-buffer-other-window "*Help*")
  389.         (erase-buffer)
  390.         (insert (documentation 'help-for-c-templates))
  391.         (goto-char (point-min))
  392.         (while (memq char (cons help-ch '(?? ?\C-v ?\ ?\177 ?\M-v)))
  393.           (if (memq char '(?\C-v ?\ ))
  394.           (scroll-up))
  395.           (if (memq char '(?\177 ?\M-v))
  396.           (scroll-down))
  397.           (message "%s%s: "
  398.                line-prompt
  399.                (if (pos-visible-in-window-p (point-max))
  400.                "" " or Space to scroll"))
  401.           (let ((cursor-in-echo-area t))
  402.         (setq char (read-char))))))
  403.       (let ((defn (cdr (assq (downcase char) c-template-map))))
  404.     (if defn (call-interactively defn) (ding))))))
  405.  
  406. (defvar c-template-map nil
  407.   "Keymap used in c or c++ mode for smart template operations.")
  408.  
  409. (let ((c-mp (make-sparse-keymap)))
  410.   (define-key c-mp "?"    'help-for-c-templates)
  411.   (define-key c-mp "\C-h" 'help-for-c-templates)
  412.   (define-key c-mp help-character 'help-for-c-templates)
  413.   (define-key c-mp "\C-f" 'document-c-function)
  414.   (define-key c-mp "f"    'document-c-function)
  415.   (define-key c-mp "s"    'update-function-synopsis)
  416.   (define-key c-mp "\C-s" 'update-function-synopsis)
  417.   (define-key c-mp "\C-h" 'make-header)
  418.   (define-key c-mp "\C-r" 'make-revision)
  419.   (define-key c-mp "r"    'make-revision)
  420.   (define-key c-mp "\C-t" 'enter-template-mode)
  421.   (define-key c-mp "t"    'enter-template-mode)
  422.   (define-key c-mp "b"    'make-box-comment)
  423.   (define-key c-mp "\C-b" 'make-box-comment)
  424.   (define-key c-mp "d"    'make-divisor)
  425.   (define-key c-mp "\C-d" 'make-divisor)
  426.   (define-key c-mp "\C-c" 'update-table-of-contents)
  427.   (define-key c-mp "c"    'header-goto-table-of-contents)
  428.   (define-key c-mp "e"    'header-goto-end)
  429.   (define-key c-mp "\C-e" 'header-goto-end)
  430.   (define-key c-mp "p"    'header-goto-purpose)
  431.   (define-key c-mp "\C-p" 'header-goto-purpose)
  432.   (define-key c-mp "\C-m" 'afs-main)
  433.   (define-key c-mp "m"    'afs-module)
  434.   (setq c-template-map c-mp))
  435.  
  436. ;;(require 'c-mode)
  437. (require 'c++-mode)
  438. (progn
  439.   (define-key c-mode-map "\C-ct" c-template-map)
  440.   (define-key c-mode-map "\C-c\C-t" c-template-map)
  441.   (define-key c++-mode-map "\C-ct" c-template-map)
  442.   (define-key c++-mode-map "\C-c\C-t" c-template-map)
  443.   )
  444.  
  445. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  446. ;; I do not like the current behavior of / or { in c++ mode
  447. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  448. (defun electric-c++-open-brace (arg)
  449.   "Insert character and correct line's indentation."
  450.   (interactive "P")
  451.   (let (insertpos)
  452.     (if (and (not arg)
  453.          (eolp)
  454.          (or (save-excursion
  455.            (skip-chars-backward " \t")
  456.            (bolp))
  457.          (if c-auto-newline
  458.              (progn ;;(c++-indent-line) (newline)
  459.                t)
  460.            nil)))
  461.     (progn
  462.       (insert last-command-char)
  463.       (c++-indent-line)
  464.       (if c-auto-newline
  465.           (progn
  466.         (setq insertpos (1- (point)))
  467.         (newline)
  468.         (c++-indent-line)))
  469.       (save-excursion
  470.         (if insertpos (goto-char (1+ insertpos)))
  471.         (delete-char -1))))
  472.     (if insertpos
  473.     (save-excursion
  474.       (goto-char insertpos)
  475.       (self-insert-command (prefix-numeric-value arg)))
  476.       (self-insert-command (prefix-numeric-value arg)))))
  477.  
  478.  
  479. (defun electric-c++-slash (arg)
  480.   "Insert character and correct line's indentation."
  481.   (interactive "P")
  482.   (if (and (char-equal (preceding-char) ?/)
  483.        (save-excursion (beginning-of-line)
  484.                (not (looking-at "[ \t]*/"))))
  485.       (progn
  486.       (forward-char -1)
  487.       (indent-to 33)
  488.       (forward-char 1)))
  489.     (self-insert-command (prefix-numeric-value arg)))
  490.  
  491. (progn
  492.   (define-key c++-mode-map "{" 'electric-c++-open-brace)
  493.   (define-key c++-mode-map "/" 'electric-c++-slash)
  494.   )
  495.   
  496. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  497. ;; Format conversions
  498. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  499. (defun convert-to-c-comments ()
  500.   "Conterts // stype comments to /* style */. Good for crippled
  501.    preprocessors such as supplied with most non-gnu C++ systems. (YUCH!)."
  502.   (interactive)
  503.   (replace-regexp "//\\(.*\\)$" "/* \\1 */" nil))
  504.  
  505. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  506. ;; Command line header maintenance for bi diehards or batch operations.
  507. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  508. (setq command-line-hooks (cons 'do-update-table-of-contents command-line-hooks))
  509. (defun do-update-table-of-contents ()
  510.   (if (string= (upcase argi) "-TOC") 
  511.       (let ((trim-versions-without-asking t)
  512.         (executing-macro "true"));; suppress "Mark Set" messages
  513.     ;; Consume all following arguments until one starts with a "-"
  514.     (while (and command-line-args-left
  515.             (not (char-equal ?- (aref (car command-line-args-left) 0))))
  516.      
  517.       (if (headerable-file-p (car command-line-args-left))
  518.           (progn
  519.         (find-file (car command-line-args-left))
  520.         (update-table-of-contents)
  521.         (write-file nil)
  522.         (kill-buffer (current-buffer))))
  523.       (setq command-line-args-left (cdr command-line-args-left))
  524.       ))))
  525.  
  526. (setq command-line-hooks (cons 'do-update-all-synopsis command-line-hooks))
  527. (defun do-update-all-synopsis ()
  528.   (if (string= (upcase argi) "-SYNOPSIS") 
  529.       (let ((trim-versions-without-asking t)
  530.         (executing-macro "true"));; suppress "Mark Set" messages
  531.     ;; Consume all following arguments until one starts with a "-"
  532.     (while (and command-line-args-left
  533.             (not (char-equal ?- (aref (car command-line-args-left) 0))))
  534.      
  535.       (if (headerable-file-p (car command-line-args-left))
  536.           (progn
  537.         (find-file (car command-line-args-left))
  538.         (update-all-synopsis)
  539.         (write-file nil)
  540.         (kill-buffer (current-buffer))))
  541.       (setq command-line-args-left (cdr command-line-args-left))
  542.       ))))
  543.  
  544.  
  545. ;;(setq command-line-hooks (cons 'list-args command-line-hooks))
  546. ;;(defun list-args ()
  547. ;;  (if (string= (upcase argi) "-LIST")
  548. ;;      (message "%s" command-line-args-left)))
  549.  
  550. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  551. ;; QA and corperate legal want stings such as the following in all
  552. ;; non-copylefted code. (Apologies to rms, we engineers keep as much as we
  553. ;; can under copyleft.)
  554. ;;
  555. ;; Note: this code is NOT copyright to ITI. This would only happen if you
  556. ;; distributed fiels edited with these functions and without first
  557. ;; customizing the strings to something more suitable (such as a copyleft).
  558. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  559. (defvar afs-main-header-string "
  560. static char iDeNt[] = \"@(#)$__Header$, Copyright 1989 ITI\";
  561. static char *cOpYrIgHt[] =
  562.         {
  563.         \"Confidential and Proprietary to Independence\",
  564.         \"Technologies, Unpublished and Copyrighted Work\"
  565.         };
  566. ")
  567.  
  568. (defvar afs-module-header-string "
  569. static char iDeNt[] = \"@(#)$__Header$, Copyright 1989 ITI\";
  570. ")
  571.  
  572. (defun afs-main ()
  573.   "Inserts the AFS header for a main c or c++ program"
  574.   (interactive)
  575.   (header-goto-end)
  576.   (insert afs-main-header-string))
  577.  
  578. (defun afs-module ()
  579.   "Inserts the AFS header for a module (a .o file) in a  c or c++ program"
  580.   (interactive)
  581.   (header-goto-end)
  582.   (insert afs-module-header-string))
  583.  
  584. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  585. ;; Highlight of templates
  586. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  587. (require 'highlight)
  588.  
  589. (defvar current-highlighted-template nil)
  590.  
  591. (defun highlight-template (start stop)
  592.   ;;(sit-for 0)
  593.   (unhighlight-template)
  594.   ;;(sit-for 0)
  595.   (setq current-highlighted-template (cons start stop))
  596.     ;;(message "%s" current-highlighted-template)
  597.   (highlight-region start stop))
  598.  
  599. (defun unhighlight-template ()
  600.   ;;(message "%s" current-highlighted-template)
  601.   (if (and current-highlighted-template
  602.        (pos-visible-in-window-p (car current-highlighted-template))
  603.        (pos-visible-in-window-p (cdr current-highlighted-template)))
  604.       (unhighlight-region (car current-highlighted-template)
  605.               (cdr current-highlighted-template)))
  606.   (setq current-highlighted-template nil))
  607.   
  608. (defun next-template ()
  609.   (interactive)
  610.   (if current-highlighted-template
  611.       (goto-char (cdr current-highlighted-template)))
  612.   (if (re-search-forward "|>.*<|" nil t)
  613.       (progn
  614.     (highlight-template (match-beginning 0) (match-end 0))
  615.     )
  616.     (beep)
  617.     (message "No more templates")))
  618.  
  619. (defun previous-template ()
  620.   (interactive)
  621.   (if current-highlighted-template
  622.       (goto-char (car current-highlighted-template)))
  623.   (if (re-search-backward "|>.*<|" nil t)
  624.       (progn
  625.     (highlight-template (match-beginning 0) (match-end 0))
  626.     )
  627.     (beep)
  628.         (message "No more templates")))
  629.  
  630. ;; Need recursive edit for this?  arrows move, c-g quits (and unhightlights),
  631. ;; space zaps
  632. ;; Others exit and forget that they were in use?
  633.  
  634. (defun stop-template-mode ()
  635.   (interactive)
  636.   (beep)
  637.   (unhighlight-template)
  638.   (throw 'exit nil))
  639.  
  640. (defun exit-template-mode ()
  641.   (interactive)
  642.   (if current-highlighted-template
  643.       (delete-region (car current-highlighted-template)
  644.              (cdr current-highlighted-template)))
  645.   (setq current-highlighted-template nil)
  646.   (throw 'exit nil))
  647.  
  648. (defvar template-mode-map (make-sparse-keymap)
  649.   "Keymap used in template mode.")
  650.  
  651. ;;(setq template-mode-map (make-sparse-keymap))
  652.  
  653. (progn
  654.   (define-key template-mode-map "n" 'next-template)
  655.   (define-key template-mode-map "\C-n" 'next-template)
  656.   (define-key template-mode-map "\C-f" 'next-template)
  657.   (define-key template-mode-map "f, \C-f" 'next-template)
  658.   (define-key template-mode-map "\C-p" 'previous-template)
  659.   (define-key template-mode-map "p" 'previous-template)
  660.   (define-key template-mode-map "\C-b" 'previous-template)
  661.   (define-key template-mode-map "b" 'previous-template)
  662.   (define-key template-mode-map "\C-g" 'stop-template-mode)
  663.   (define-key template-mode-map " "   'exit-template-mode)
  664.   (define-key template-mode-map "\el"   'eval-expression)
  665.   (define-key template-mode-map "\ex"   'execute-extended-command)
  666.   (define-key template-mode-map help-character 'help-for-template-mode)
  667.   (define-key template-mode-map "?" 'help-for-template-mode)
  668.   (define-key template-mode-map "\C-h" 'help-for-template-mode)
  669.   )
  670.  
  671. (defun help-for-template-mode ()
  672.   "You are in the template recursive mode. All you can do is move around
  673.    the templates or quit. You may type
  674.  
  675. n, C-n  To move to the next template
  676. p, C-p  To move to the previous template
  677. SPC     To kill the current template, exit template mode, and start editing.
  678. C-g     To leave template mode"
  679.   (interactive)
  680.   (let ((line-prompt
  681.      (substitute-command-keys (concat "n p SPC C-g. Type ? for more help: "))))
  682.     (message line-prompt)
  683.     (let ((char (read-char)))
  684.       (if (or (= char ??) (= char help-ch))
  685.       (save-window-excursion
  686.         (switch-to-buffer-other-window "*Help*")
  687.         (erase-buffer)
  688.         (insert (documentation 'help-for-template-mode))
  689.         (goto-char (point-min))
  690.         (while (memq char (cons help-ch '(?? ?\C-v ?\ ?\177 ?\M-v)))
  691.           (if (memq char '(?\C-v ?\ ))
  692.           (scroll-up))
  693.           (if (memq char '(?\177 ?\M-v))
  694.           (scroll-down))
  695.           (message "%s%s: "
  696.                line-prompt
  697.                (if (pos-visible-in-window-p (point-max))
  698.                "" " or Space to scroll"))
  699.           (let ((cursor-in-echo-area t))
  700.         (setq char (read-char))))))
  701.       (let ((defn (cdr (assq (downcase char)template-mode-map))))
  702.     (if defn (call-interactively defn) (ding))))))
  703.  
  704. (defun enter-template-mode ()
  705.   "Enter a special mode where you can move around the incomplete templates.
  706.    Once inside, type the normal help sequences to see what you can do."
  707.   (interactive)
  708.   (next-template)
  709.   (let ((previous-lkeymap (current-local-map))
  710.     (previous-gkeymap (current-global-map)))
  711.     (use-local-map template-mode-map)
  712.     (use-global-map (make-keymap))
  713.     (recursive-edit)
  714.     (use-local-map  previous-lkeymap)
  715.     (use-global-map previous-gkeymap)
  716.     ))
  717.  
  718. ;;(define-key c-mode-map "\C-ct" 'enter-template-mode)
  719. ;;(define-key c++-mode-map "\C-ct" 'enter-template-mode)
  720.  
  721. ;; need help for arrows
  722.  
  723. -*- End File: ~/local/c-support.el
  724.  
  725.  
  726.