# HG changeset patch # User Alan Mackenzie # Date 1253823598 0 # Node ID d6ddd512e29c848121c00c31a40e1c7a9878518f # Parent e2c8e2a49fd5495fe1b6bbbec7b46ae45e15bf39 (c-scan-conditionals): A new function like c-forward-conditionals, but it doesn't move point and doesn't set the mark. (c-up-conditional, c-up-conditional-with-else, c-down-conditional) (c-down-conditional-with-else, c-backward-conditional) (c-forward-conditional): Refactor to use c-scan-conditionals. diff -r e2c8e2a49fd5 -r d6ddd512e29c lisp/progmodes/cc-cmds.el --- a/lisp/progmodes/cc-cmds.el Thu Sep 24 20:18:56 2009 +0000 +++ b/lisp/progmodes/cc-cmds.el Thu Sep 24 20:19:58 2009 +0000 @@ -2808,7 +2808,9 @@ function stops at them when going backward, but not when going forward." (interactive "p") - (c-forward-conditional (- count) -1) + (let ((new-point (c-scan-conditionals (- count) -1))) + (push-mark) + (goto-char new-point)) (c-keep-region-active)) (defun c-up-conditional-with-else (count) @@ -2816,7 +2818,9 @@ Just like `c-up-conditional', except it also stops at \"#else\" directives." (interactive "p") - (c-forward-conditional (- count) -1 t) + (let ((new-point (c-scan-conditionals (- count) -1 t))) + (push-mark) + (goto-char new-point)) (c-keep-region-active)) (defun c-down-conditional (count) @@ -2828,7 +2832,9 @@ function stops at them when going forward, but not when going backward." (interactive "p") - (c-forward-conditional count 1) + (let ((new-point (c-scan-conditionals count 1))) + (push-mark) + (goto-char new-point)) (c-keep-region-active)) (defun c-down-conditional-with-else (count) @@ -2836,15 +2842,24 @@ Just like `c-down-conditional', except it also stops at \"#else\" directives." (interactive "p") - (c-forward-conditional count 1 t) + (let ((new-point (c-scan-conditionals count 1 t))) + (push-mark) + (goto-char new-point)) (c-keep-region-active)) (defun c-backward-conditional (count &optional target-depth with-else) "Move back across a preprocessor conditional, leaving mark behind. A prefix argument acts as a repeat count. With a negative argument, -move forward across a preprocessor conditional." +move forward across a preprocessor conditional. + +The optional arguments TARGET-DEPTH and WITH-ELSE are historical, +and have the same meanings as in `c-scan-conditionals'. If you +are calling c-forward-conditional from a program, you might want +to call `c-scan-conditionals' directly instead." (interactive "p") - (c-forward-conditional (- count) target-depth with-else) + (let ((new-point (c-scan-conditionals (- count) target-depth with-else))) + (push-mark) + (goto-char new-point)) (c-keep-region-active)) (defun c-forward-conditional (count &optional target-depth with-else) @@ -2852,21 +2867,42 @@ A prefix argument acts as a repeat count. With a negative argument, move backward across a preprocessor conditional. +If there aren't enough conditionals after \(or before) point, an +error is signalled. + +\"#elif\" is treated like \"#else\" followed by \"#if\", except that +the nesting level isn't changed when tracking subconditionals. + +The optional arguments TARGET-DEPTH and WITH-ELSE are historical, +and have the same meanings as in `c-scan-conditionals'. If you +are calling c-forward-conditional from a program, you might want +to call `c-scan-conditionals' directly instead." + (interactive "p") + (let ((new-point (c-scan-conditionals count target-depth with-else))) + (push-mark) + (goto-char new-point))) + +(defun c-scan-conditionals (count &optional target-depth with-else) + "Scan forward across COUNT preprocessor conditionals. +With a negative argument, scan backward across preprocessor +conditionals. Return the end position. Point is not moved. + +If there aren't enough preprocessor conditionals, throw an error. + \"#elif\" is treated like \"#else\" followed by \"#if\", except that the nesting level isn't changed when tracking subconditionals. The optional argument TARGET-DEPTH specifies the wanted nesting depth -after each scan. I.e. if TARGET-DEPTH is -1, the function will move -out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH +after each scan. E.g. if TARGET-DEPTH is -1, the end position will be +outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH counts as -1. If the optional argument WITH-ELSE is non-nil, \"#else\" directives are treated as conditional clause limits. Normally they are ignored." - (interactive "p") (let* ((forward (> count 0)) (increment (if forward -1 1)) (search-function (if forward 're-search-forward 're-search-backward)) - (new)) + new) (unless (integerp target-depth) (setq target-depth (if target-depth -1 0))) (save-excursion @@ -2935,9 +2971,8 @@ (error "No containing preprocessor conditional")) (goto-char (setq new found))) (setq count (+ count increment)))) - (push-mark) - (goto-char new)) - (c-keep-region-active)) + (c-keep-region-active) + new)) ;; commands to indent lines, regions, defuns, and expressions