home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / cc-lobotomy.el < prev    next >
Encoding:
Text File  |  1995-06-16  |  7.2 KB  |  201 lines

  1. ;;; cc-lobotomy.el --- excise portions of cc-mode's brain... for speed
  2.  
  3. ;; Copyright (C) 1985-1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author:        1995 Barry A. Warsaw
  6. ;; Maintainer:    cc-mode-help@merlin.cnri.reston.va.us
  7. ;; Created:       March 1995, split from cc-mode.el
  8. ;; Version:       1.8
  9. ;; Last Modified: 1995/06/11 21:42:31
  10. ;; Keywords: c languages oop
  11.  
  12. ;; This file is not part of GNU Emacs.
  13.  
  14. ;; This program is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2 of the License, or
  17. ;; (at your option) any later version.
  18. ;; 
  19. ;; This program is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23. ;; 
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with this program; if not, write to the Free Software
  26. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  
  28. ;;; Commentary:
  29. ;;
  30. ;; Every effort has been made to improve the performance of
  31. ;; cc-mode. However, due to the nature of the C, C++, and Objective-C
  32. ;; language definitions, a trade-off is often required between
  33. ;; accuracy of construct recognition and speed. I believe it is always
  34. ;; best to be correct, and that the mode is currently fast enough for
  35. ;; most normal usage.  Others disagree.  I have no intention of
  36. ;; including these hacks in the main distribution.  When cc-mode
  37. ;; version 5 comes out, it will include a rewritten indentation engine
  38. ;; so that performance will be greatly improved automatically.  This
  39. ;; was not included in this release of version 4 so that Emacs 18
  40. ;; could still be supported.  Note that this implies that cc-mode
  41. ;; version 5 will *not* work on Emacs 18!
  42. ;;
  43. ;; To use, see the variable cc-lobotomy-pith-list and the function
  44. ;; cc-lobotomize.   The variable contains a good explanation of the
  45. ;; speed/accuracy trade-offs for each option.  Set it to what you'd
  46. ;; like, and call cc-lobotomy in your c-mode-hook.
  47. ;;
  48. ;; This will redefine certain cc-mode functions and affect all cc-mode
  49. ;; buffers globally.
  50. ;;
  51. ;; This file is completely unsupported!  I have no idea whether this
  52. ;; will work with such things as cc-mode-18.el.
  53.  
  54.  
  55. ;;; Code:
  56. (require 'cc-mode)
  57.  
  58. (defvar cc-lobotomy-pith-list ()
  59.   "*List of things to dumb-ify to speed up cc-mode.  Note that each
  60. incurs a penalty in correct identification of certain code constructs.
  61. Possible values to put on this list:
  62.  
  63.   'literal -- `c-in-literal' is lobotomized.  This will significantly
  64.               speed up parsing over large lists of cpp macros, as seen
  65.           for instance in header files.  The penalty is that you
  66.           cannot put the `#' character as the first non-whitespace
  67.           character on a line inside other multi-line literals
  68.           (i.e. comments or strings)
  69.  
  70.   'class   -- `c-narrow-out-enclosing-class' and `c-search-uplist for
  71.               classkey' are lobotomized.  This speeds up some
  72.           indenting inside and around class and struct
  73.           definitions.  The penalty is that elements inside of
  74.           classes and structs may not indent correctly.
  75.  
  76.   'lists   -- `c-inside-bracelist-p' is lobotomized.  This speeds up
  77.               indenting inside and around brace lists (e.g. aggregate
  78.           initializers, enum lists, etc.).  The penalty is that
  79.           elements inside these lists may not indent correctly.")
  80.  
  81. (defun cc-lobotomize ()
  82.   "Perform lobotomies on cc-mode as described in `cc-lobotomy-pith-list'."
  83.   (let (pithedp)
  84.     (if (memq 'literal cc-lobotomy-pith-list)
  85.     (progn
  86.       (fset 'c-in-literal 'cc-in-literal-lobotomized)
  87.       (setq pithedp t)))
  88.     (if (memq 'class cc-lobotomy-pith-list)
  89.     (progn
  90.       (fset 'c-narrow-out-enclosing-class
  91.         'cc-narrow-out-enclosing-class-lobotomized)
  92.       (fset 'c-search-uplist-for-classkey
  93.         'cc-search-uplist-for-classkey-lobotomized)
  94.       (setq pithedp t)))
  95.     (if (memq 'lists cc-lobotomy-pith-list)
  96.     (progn
  97.       (fset 'c-inside-bracelist-p 'cc-inside-bracelist-p-lobotomized)
  98.       (setq pithedp t)))
  99.     (if pithedp
  100.     (fset 'c-submit-bug-report 'cc-submit-bug-report-lobotomized))
  101.     ))
  102.  
  103.  
  104. ;; This is a faster version of c-in-literal.  It trades speed for one
  105. ;; approximation, namely that within other literals, the `#' character
  106. ;; cannot be the first non-whitespace on a line.
  107. (defun cc-in-literal-lobotomized (&optional lim)
  108.   ;; first check the cache
  109.   (if (and (boundp 'c-in-literal-cache)
  110.        c-in-literal-cache
  111.        (= (point) (aref c-in-literal-cache 0)))
  112.       (aref c-in-literal-cache 1)
  113.     ;; quickly check for cpp macro. this breaks if the `#' character
  114.     ;; appears as the first non-whitespace on a line inside another
  115.     ;; literal.
  116.     (let* (state
  117.        (char-at-boi (char-after (c-point 'boi)))
  118.        (rtn (cond
  119.          ((and char-at-boi (= char-at-boi ?#))
  120.           'pound)
  121.          ((nth 3 (setq state (save-excursion
  122.                        (parse-partial-sexp
  123.                     (or lim (c-point 'bod))
  124.                     (point)))))
  125.           'string)
  126.          ((nth 4 state) (if (nth 7 state) 'c++ 'c))
  127.          (t nil))))
  128.       ;; cache this result if the cache is enabled
  129.       (and (boundp 'c-in-literal-cache)
  130.        (setq c-in-literal-cache (vector (point) rtn)))
  131.       rtn)))
  132.  
  133. (defun cc-narrow-out-enclosing-class-lobotomized (dummy1 dummy2) nil)
  134.  
  135. (defun cc-search-uplist-for-classkey-lobotomized (dummy) nil)
  136.  
  137. (defun cc-inside-bracelist-p-lobotomized (dummy1 dummy2) nil)
  138.  
  139. (defun cc-submit-bug-report-lobotomized ()
  140.   "Submit via mail a bug report on cc-mode."
  141.   (interactive)
  142.   ;; load in reporter
  143.   (let ((reporter-prompt-for-summary-p t)
  144.     (reporter-dont-compact-list '(c-offsets-alist)))
  145.     (and
  146.      (y-or-n-p "Do you want to submit a report on cc-mode? ")
  147.      (require 'reporter)
  148.      (reporter-submit-bug-report
  149.       c-mode-help-address
  150.       (concat "cc-mode " c-version " ("
  151.           (cond ((eq major-mode 'c++-mode)  "C++")
  152.             ((eq major-mode 'c-mode)    "C")
  153.             ((eq major-mode 'objc-mode) "ObjC"))
  154.           ")")
  155.       (let ((vars (list
  156.            ;; report only the vars that affect indentation
  157.            'c-basic-offset
  158.            'c-offsets-alist
  159.            'c-block-comments-indent-p
  160.            'c-cleanup-list
  161.            'c-comment-only-line-offset
  162.            'c-backslash-column
  163.            'c-delete-function
  164.            'c-electric-pound-behavior
  165.            'c-hanging-braces-alist
  166.            'c-hanging-colons-alist
  167.            'c-hanging-comment-ender-p
  168.            'c-tab-always-indent
  169.            'c-recognize-knr-p
  170.            'defun-prompt-regexp
  171.            'tab-width
  172.            )))
  173.     (if (not (boundp 'defun-prompt-regexp))
  174.         (delq 'defun-prompt-regexp vars)
  175.       vars))
  176.       (function
  177.        (lambda ()
  178.      (insert
  179.       (if c-special-indent-hook
  180.           (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
  181.               "c-special-indent-hook is set to '"
  182.               (format "%s" c-special-indent-hook)
  183.               ".\nPerhaps this is your problem?\n"
  184.               "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
  185.         "\n")
  186.       (format "c-emacs-features: %s\n" c-emacs-features)
  187.       )))
  188.       (function
  189.        (lambda ()
  190.      (insert
  191.       "You are using cc-lobotomy.el.  You realize that by doing\n"
  192.       "so you have already made the decision to trade off accuracy\n"
  193.       "for speed?  Don't set your hopes too high that your problem\n"
  194.       "will be fixed.\n\n"
  195.       )))
  196.       "Dear Barry,"
  197.       ))))
  198.  
  199. (provide 'cc-lobotomy)
  200. ;;; cc-lobotomy.el ends here
  201.