home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / block-cmnt.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  4.8 KB  |  181 lines

  1. ;From ark1!uakari!zaphod.mps.ohio-state.edu!uwm.edu!ogicse!decwrl!shelby!portia!stergios Mon Feb 26 13:16:30 1990
  2. ;Article 1214 of gnu.emacs:
  3. ;Path: ark1!uakari!zaphod.mps.ohio-state.edu!uwm.edu!ogicse!decwrl!shelby!portia!stergios
  4. ;>From stergios@portia.portia.stanford.edu (Stergios)
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: Re: What's the best way to handle these comments in GNU emacs
  7. ;Message-ID: <STERGIOS.90Feb23193045@portia.portia.stanford.edu>
  8. ;Date: 24 Feb 90 03:30:45 GMT
  9. ;References: <1990Feb21.055602.14270@csis.dit.csiro.au>
  10. ;Sender: Stergios <stergios@portia.stanford.edu>
  11. ;Reply-To: stergios@jessica.stanford.edu
  12. ;Organization: Stanford University
  13. ;Lines: 163
  14. ;In-reply-to: stuarth@csis.dit.csiro.au's message of 21 Feb 90 05:56:02 GMT
  15. ;
  16. ;
  17. ;
  18. ;# Occasionally I use comment "boxes" like this:
  19. ;
  20. ;# /*------------------------------------------------------------*/
  21. ;# /* This is a comment in a box.                                */
  22. ;# /*------------------------------------------------------------*/
  23. ;
  24. ;# Is there a neat way to insert this kind of comment?
  25. ;
  26. ;#                     Stuart Hungerford
  27. ;#                     stuart@csis.dit.csiro.au
  28. ;
  29. ;Well, you asked for it, you got it.  Be careful with this.  Its not
  30. ;pretty, hey I'm no elisp guru, but I use it anyway.
  31.  
  32. ; com.el, origional code snarfed from a gosling translation bye dsm
  33. ; and modified by stergios to find the comment block rather than
  34. ; having to set point and mark by hand.
  35. ; suggested use:
  36. ;        M-x comment-region
  37. ;        M-x uncomment-region
  38. ;        M-x recomment-block
  39. ; Stergios Marinopoulos. copy, steal, sell this code. do want you want
  40. ; with it - just dont blame me. 
  41. ;
  42.  
  43. (defun recomment-block ()
  44.   "Fills paragarph for the block-styled comment point is in.  Point must
  45. be in a block comment, it cannot not be on the 1st or last line.
  46. My definition of a comment block looks like this:
  47. /*********************************************************/
  48. /* You mean you actually comment code?             */
  49. /*                             */
  50. /*                             */
  51. /*********************************************************/
  52. You can change the regexps for different looking comment lines.
  53. "
  54.   (interactive)
  55.   ; locate comment region point is in, then call uncomment it, then fill it.
  56.   (save-excursion
  57.     (end-of-line)
  58.     (re-search-backward "/\\*[*]*\\*/\n")
  59.     (push-mark)
  60.     (next-line 1)
  61.     (re-search-forward "/\\*[*]*\\*/\n")
  62.     (uncomment-region)
  63.     (backward-word 1)
  64.     (fill-paragraph 'nil)
  65.     (set-fill-column 63)
  66.     
  67.     ; locate region filled paragraph is in, and wrap it in comments.
  68.     (backward-paragraph 1)
  69.     (next-line 1)
  70.     (push-mark)
  71.     (forward-paragraph 1)
  72.     (comment-region)
  73.     )
  74.   )
  75.  
  76. (defun comment-region ()
  77. "Wrap block style comments around region."
  78.   (interactive)
  79.   (narrow-to-region (point) (mark))
  80.   (goto-char (point-min))
  81.   
  82.   ; clear out blank lines
  83.   (while (and (eolp) (not (eobp)))
  84.     (delete-char 1))
  85.  
  86.   ; Now count the length of the longest line
  87.   (setq longest 0)
  88.   (while (not (eobp))
  89.     (end-of-line)
  90.     (delete-horizontal-space)
  91.     (if (> (setq len (current-column)) longest)
  92.     (setq longest len))
  93.     (forward-char))
  94.  
  95.   (if (< longest 65)
  96.       (setq longest 65))
  97.  
  98.  
  99.   ; Clear out blank lines at the end of region
  100.   (goto-char (point-max))
  101.   (while (and (not (bobp)) (= (preceding-char) 10))
  102.     (delete-char -1))
  103.   (insert "\n")
  104.  
  105.   ; Now insert the top of the comment
  106.   (goto-char (point-min))
  107.   (insert "/*")
  108.   (setq len 1)
  109.   (while (<= len longest)
  110.     (insert "*")
  111.     (setq len (+ len 1)))
  112.   (insert "*/\n")
  113.  
  114.   ; Insert sides of comment
  115.   (while (not (eobp))
  116.     (insert "/* ")
  117.     (end-of-line)
  118.     (while (< (current-column) (1+ longest))
  119.       (insert " "))
  120.     (insert " */")
  121.     (forward-char))
  122.  
  123.   ; Now insert last line of comment
  124.   (goto-char (point-max))
  125.   (insert "/*")
  126.   (setq len 1)
  127.   (while (<= len longest)
  128.     (insert "*")
  129.     (setq len (1+ len)))
  130.   (insert "*/\n")
  131.  
  132.   ; Count the number of lines in the comment
  133.   (setq longest (count-lines (point-min) (point-max)))
  134.   (beginning-of-buffer)
  135.   (set-mark (point))
  136.   (goto-char (point-max))
  137.   (widen)
  138.  
  139.   (while (looking-at "[ \n]")
  140.     (forward-char))
  141.   (setq len (current-column))
  142.   (exchange-point-and-mark)
  143.   (set-mark (point))
  144.   (while (> longest 0)
  145.     (setq longest (- longest 1))
  146.     (setq loop len)
  147.     (while (> loop 0)
  148.       (insert " ")
  149.       (setq loop (- loop 1)))
  150.     (next-line 1)
  151.     (beginning-of-line))
  152.   )
  153.  
  154. (defun uncomment-region ()
  155. "Strip out block style comments in region"
  156.   (interactive)
  157.   (save-excursion
  158.     (narrow-to-region (point) (mark))
  159.  
  160.     ; remove spaces
  161.     (beginning-of-buffer)
  162.     (replace-regexp "^[ ]*" "")
  163.     
  164.     ; remove comment top and bottom
  165.     (beginning-of-buffer)
  166.     (replace-regexp "/\\*[*]*\\*/\n" "")  
  167.     
  168.     ; remove left side
  169.     (beginning-of-buffer)
  170.     (replace-regexp "^/\\* " "")
  171.     
  172.     ; remove right side
  173.     (beginning-of-buffer)
  174.     (replace-regexp "\\*/$" "")
  175.     
  176.     (widen)
  177.     ) 
  178.   )
  179.  
  180.  
  181.