From 28abe5e279255c2b19564bd662f87e978c79702d Mon Sep 17 00:00:00 2001
From: Alan Mackenzie <acm@muc.de>
Date: Mon, 9 Apr 2007 10:51:29 +0000
Subject: [PATCH] Changes to make `narrow-to-defun' and `mark-defun' work
 properly in CC Mode:

cc-defs.el (c-beginning-of-defun-1):
cc-cmds.el (c-beginning-of-defun, c-end-of-defun): bind
beginning/end-of-defun-function to nil around calls to
beginning/end-of-defun.

cc-langs.el (beginning-of-defun-function, end-of-defun-function): new
c-lang-setvar's.

cc-awk.el (c-awk-beginning-of-defun): Add "(or arg (setq arg 1))" to
enable non-interactive call.
---
 lisp/progmodes/cc-awk.el   |  1 +
 lisp/progmodes/cc-cmds.el  |  6 ++++--
 lisp/progmodes/cc-defs.el  | 18 ++++++++++--------
 lisp/progmodes/cc-langs.el | 25 ++++++++++++++++++++-----
 4 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/lisp/progmodes/cc-awk.el b/lisp/progmodes/cc-awk.el
index b0c15bff903..b3e0e381135 100644
--- a/lisp/progmodes/cc-awk.el
+++ b/lisp/progmodes/cc-awk.el
@@ -988,6 +988,7 @@ nor helpful.
 Note that this function might do hidden buffer changes.  See the
 comment at the start of cc-engine.el for more info."
   (interactive "p")
+  (or arg (setq arg 1))
   (save-match-data
     (c-save-buffer-state                ; ensures the buffer is writable.
      nil
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index 7b28851b3bb..79043c87b63 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -1511,7 +1511,8 @@ defun."
   (or arg (setq arg 1))
 
   (c-save-buffer-state
-      ((start (point))
+      (beginning-of-defun-function end-of-defun-function
+       (start (point))
        where paren-state pos)
 
     ;; Move back out of any macro/comment/string we happen to be in.
@@ -1613,7 +1614,8 @@ the open-parenthesis that starts a defun; see `beginning-of-defun'."
   (or arg (setq arg 1))
 
   (c-save-buffer-state
-      ((start (point))
+      (beginning-of-defun-function end-of-defun-function
+       (start (point))
        where paren-state pos)
 
     ;; Move back out of any macro/comment/string we happen to be in.
diff --git a/lisp/progmodes/cc-defs.el b/lisp/progmodes/cc-defs.el
index 43fa0ab5937..8c10df649de 100644
--- a/lisp/progmodes/cc-defs.el
+++ b/lisp/progmodes/cc-defs.el
@@ -74,12 +74,12 @@
 ; (eval-after-load "font-lock"  ; 2006-07-09.  font-lock is now preloaded
 ;   '
 (if (and (not (featurep 'cc-fix)) ; only load the file once.
-	    (featurep 'xemacs) ; There is now (2005/12) code in GNU Emacs CVS
-			       ; to make the call to f-l-c-k throw an error.
-            (let (font-lock-keywords)
-              (font-lock-compile-keywords '("\\<\\>"))
-	      font-lock-keywords))     ; did the previous call foul this up?
-       (load "cc-fix")) ;)
+	 (featurep 'xemacs)	; There is now (2005/12) code in GNU Emacs CVS
+				; to make the call to f-l-c-k throw an error.
+	 (let (font-lock-keywords)
+	   (font-lock-compile-keywords '("\\<\\>"))
+	   font-lock-keywords))     ; did the previous call foul this up?
+    (load "cc-fix")) ;)
 
 ;; The above takes care of the delayed loading, but this is necessary
 ;; to ensure correct byte compilation.
@@ -708,7 +708,8 @@ be after it."
 	    ;; c-parse-state to between 3 and 60 times faster when
 	    ;; braces are hung.  It can also degrade performance by
 	    ;; about as much when braces are not hung.
-	    '(let (pos)
+	    '(let (beginning-of-defun-function end-of-defun-function
+					       pos)
 	       (while (not pos)
 		 (save-restriction
 		   (widen)
@@ -731,7 +732,8 @@ be after it."
 		  ))
 	       (goto-char pos)))
        ;; Emacs, which doesn't have buffer-syntactic-context-depth
-       (beginning-of-defun))
+       (let (beginning-of-defun-function end-of-defun-function)
+	 (beginning-of-defun)))
      ;; if defun-prompt-regexp is non-nil, b-o-d won't leave us at the
      ;; open brace.
      (and defun-prompt-regexp
diff --git a/lisp/progmodes/cc-langs.el b/lisp/progmodes/cc-langs.el
index fe01ab02ae3..906cbfb19a7 100644
--- a/lisp/progmodes/cc-langs.el
+++ b/lisp/progmodes/cc-langs.el
@@ -221,11 +221,6 @@ the evaluated constant value at compile time."
     ;; with the group symbol for each group and should return non-nil
     ;; if that group is to be included.
     ;;
-    ;; OP-FILTER filters the individual operators in each group.  It
-    ;; can be t to choose all operators, a regexp to test against each
-    ;; operator, or a function which will be called for each operator
-    ;; and should return non-nil for those to include.
-    ;;
     ;; If XLATE is given, it's a function which is called for each
     ;; matching operator and its return value is collected instead.
     ;; If it returns a list, the elements are spliced directly into
@@ -1375,6 +1370,26 @@ EOL terminated statements."
 (c-lang-defvar c-vsemi-status-unknown-p-fn
   (c-lang-const c-vsemi-status-unknown-p-fn))
 
+
+;;; Defun functions
+
+;; The Emacs variables beginning-of-defun-function and
+;; end-of-defun-function will be set so that commands like
+;; `mark-defun' and `narrow-to-defun' work right.  The key sequences
+;; C-M-a and C-M-e are, however, bound directly to the CC Mode
+;; functions, allowing optimisation for large n.
+(c-lang-defconst beginning-of-defun-function
+  "Function to which beginning-of-defun-function will be set."
+  t 'c-beginning-of-defun
+  awk 'c-awk-beginning-of-defun)
+(c-lang-setvar beginning-of-defun-function
+	       (c-lang-const beginning-of-defun-function))
+
+(c-lang-defconst end-of-defun-function
+  "Function to which end-of-defun-function will be set."
+  t 'c-end-of-defun
+  awk 'c-awk-end-of-defun)
+(c-lang-setvar end-of-defun-function (c-lang-const end-of-defun-function))
 
 ;;; In-comment text handling.
 
-- 
2.39.5