Mercurial > emacs
comparison lisp/progmodes/cc-cmds.el @ 105186:d6ddd512e29c
(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.
author | Alan Mackenzie <acm@muc.de> |
---|---|
date | Thu, 24 Sep 2009 20:19:58 +0000 |
parents | eee42a220506 |
children | b27aeda9c1fd |
comparison
equal
deleted
inserted
replaced
105185:e2c8e2a49fd5 | 105186:d6ddd512e29c |
---|---|
2806 | 2806 |
2807 \"#elif\" is treated like \"#else\" followed by \"#if\", so the | 2807 \"#elif\" is treated like \"#else\" followed by \"#if\", so the |
2808 function stops at them when going backward, but not when going | 2808 function stops at them when going backward, but not when going |
2809 forward." | 2809 forward." |
2810 (interactive "p") | 2810 (interactive "p") |
2811 (c-forward-conditional (- count) -1) | 2811 (let ((new-point (c-scan-conditionals (- count) -1))) |
2812 (push-mark) | |
2813 (goto-char new-point)) | |
2812 (c-keep-region-active)) | 2814 (c-keep-region-active)) |
2813 | 2815 |
2814 (defun c-up-conditional-with-else (count) | 2816 (defun c-up-conditional-with-else (count) |
2815 "Move back to the containing preprocessor conditional, including \"#else\". | 2817 "Move back to the containing preprocessor conditional, including \"#else\". |
2816 Just like `c-up-conditional', except it also stops at \"#else\" | 2818 Just like `c-up-conditional', except it also stops at \"#else\" |
2817 directives." | 2819 directives." |
2818 (interactive "p") | 2820 (interactive "p") |
2819 (c-forward-conditional (- count) -1 t) | 2821 (let ((new-point (c-scan-conditionals (- count) -1 t))) |
2822 (push-mark) | |
2823 (goto-char new-point)) | |
2820 (c-keep-region-active)) | 2824 (c-keep-region-active)) |
2821 | 2825 |
2822 (defun c-down-conditional (count) | 2826 (defun c-down-conditional (count) |
2823 "Move forward into the next preprocessor conditional, leaving mark behind. | 2827 "Move forward into the next preprocessor conditional, leaving mark behind. |
2824 A prefix argument acts as a repeat count. With a negative argument, | 2828 A prefix argument acts as a repeat count. With a negative argument, |
2826 | 2830 |
2827 \"#elif\" is treated like \"#else\" followed by \"#if\", so the | 2831 \"#elif\" is treated like \"#else\" followed by \"#if\", so the |
2828 function stops at them when going forward, but not when going | 2832 function stops at them when going forward, but not when going |
2829 backward." | 2833 backward." |
2830 (interactive "p") | 2834 (interactive "p") |
2831 (c-forward-conditional count 1) | 2835 (let ((new-point (c-scan-conditionals count 1))) |
2836 (push-mark) | |
2837 (goto-char new-point)) | |
2832 (c-keep-region-active)) | 2838 (c-keep-region-active)) |
2833 | 2839 |
2834 (defun c-down-conditional-with-else (count) | 2840 (defun c-down-conditional-with-else (count) |
2835 "Move forward into the next preprocessor conditional, including \"#else\". | 2841 "Move forward into the next preprocessor conditional, including \"#else\". |
2836 Just like `c-down-conditional', except it also stops at \"#else\" | 2842 Just like `c-down-conditional', except it also stops at \"#else\" |
2837 directives." | 2843 directives." |
2838 (interactive "p") | 2844 (interactive "p") |
2839 (c-forward-conditional count 1 t) | 2845 (let ((new-point (c-scan-conditionals count 1 t))) |
2846 (push-mark) | |
2847 (goto-char new-point)) | |
2840 (c-keep-region-active)) | 2848 (c-keep-region-active)) |
2841 | 2849 |
2842 (defun c-backward-conditional (count &optional target-depth with-else) | 2850 (defun c-backward-conditional (count &optional target-depth with-else) |
2843 "Move back across a preprocessor conditional, leaving mark behind. | 2851 "Move back across a preprocessor conditional, leaving mark behind. |
2844 A prefix argument acts as a repeat count. With a negative argument, | 2852 A prefix argument acts as a repeat count. With a negative argument, |
2845 move forward across a preprocessor conditional." | 2853 move forward across a preprocessor conditional. |
2854 | |
2855 The optional arguments TARGET-DEPTH and WITH-ELSE are historical, | |
2856 and have the same meanings as in `c-scan-conditionals'. If you | |
2857 are calling c-forward-conditional from a program, you might want | |
2858 to call `c-scan-conditionals' directly instead." | |
2846 (interactive "p") | 2859 (interactive "p") |
2847 (c-forward-conditional (- count) target-depth with-else) | 2860 (let ((new-point (c-scan-conditionals (- count) target-depth with-else))) |
2861 (push-mark) | |
2862 (goto-char new-point)) | |
2848 (c-keep-region-active)) | 2863 (c-keep-region-active)) |
2849 | 2864 |
2850 (defun c-forward-conditional (count &optional target-depth with-else) | 2865 (defun c-forward-conditional (count &optional target-depth with-else) |
2851 "Move forward across a preprocessor conditional, leaving mark behind. | 2866 "Move forward across a preprocessor conditional, leaving mark behind. |
2852 A prefix argument acts as a repeat count. With a negative argument, | 2867 A prefix argument acts as a repeat count. With a negative argument, |
2853 move backward across a preprocessor conditional. | 2868 move backward across a preprocessor conditional. |
2854 | 2869 |
2870 If there aren't enough conditionals after \(or before) point, an | |
2871 error is signalled. | |
2872 | |
2855 \"#elif\" is treated like \"#else\" followed by \"#if\", except that | 2873 \"#elif\" is treated like \"#else\" followed by \"#if\", except that |
2856 the nesting level isn't changed when tracking subconditionals. | 2874 the nesting level isn't changed when tracking subconditionals. |
2857 | 2875 |
2876 The optional arguments TARGET-DEPTH and WITH-ELSE are historical, | |
2877 and have the same meanings as in `c-scan-conditionals'. If you | |
2878 are calling c-forward-conditional from a program, you might want | |
2879 to call `c-scan-conditionals' directly instead." | |
2880 (interactive "p") | |
2881 (let ((new-point (c-scan-conditionals count target-depth with-else))) | |
2882 (push-mark) | |
2883 (goto-char new-point))) | |
2884 | |
2885 (defun c-scan-conditionals (count &optional target-depth with-else) | |
2886 "Scan forward across COUNT preprocessor conditionals. | |
2887 With a negative argument, scan backward across preprocessor | |
2888 conditionals. Return the end position. Point is not moved. | |
2889 | |
2890 If there aren't enough preprocessor conditionals, throw an error. | |
2891 | |
2892 \"#elif\" is treated like \"#else\" followed by \"#if\", except that | |
2893 the nesting level isn't changed when tracking subconditionals. | |
2894 | |
2858 The optional argument TARGET-DEPTH specifies the wanted nesting depth | 2895 The optional argument TARGET-DEPTH specifies the wanted nesting depth |
2859 after each scan. I.e. if TARGET-DEPTH is -1, the function will move | 2896 after each scan. E.g. if TARGET-DEPTH is -1, the end position will be |
2860 out of the enclosing conditional. A non-integer non-nil TARGET-DEPTH | 2897 outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH |
2861 counts as -1. | 2898 counts as -1. |
2862 | 2899 |
2863 If the optional argument WITH-ELSE is non-nil, \"#else\" directives | 2900 If the optional argument WITH-ELSE is non-nil, \"#else\" directives |
2864 are treated as conditional clause limits. Normally they are ignored." | 2901 are treated as conditional clause limits. Normally they are ignored." |
2865 (interactive "p") | |
2866 (let* ((forward (> count 0)) | 2902 (let* ((forward (> count 0)) |
2867 (increment (if forward -1 1)) | 2903 (increment (if forward -1 1)) |
2868 (search-function (if forward 're-search-forward 're-search-backward)) | 2904 (search-function (if forward 're-search-forward 're-search-backward)) |
2869 (new)) | 2905 new) |
2870 (unless (integerp target-depth) | 2906 (unless (integerp target-depth) |
2871 (setq target-depth (if target-depth -1 0))) | 2907 (setq target-depth (if target-depth -1 0))) |
2872 (save-excursion | 2908 (save-excursion |
2873 (while (/= count 0) | 2909 (while (/= count 0) |
2874 (let ((depth 0) | 2910 (let ((depth 0) |
2933 (if forward (forward-line 1))))) | 2969 (if forward (forward-line 1))))) |
2934 (or found | 2970 (or found |
2935 (error "No containing preprocessor conditional")) | 2971 (error "No containing preprocessor conditional")) |
2936 (goto-char (setq new found))) | 2972 (goto-char (setq new found))) |
2937 (setq count (+ count increment)))) | 2973 (setq count (+ count increment)))) |
2938 (push-mark) | 2974 (c-keep-region-active) |
2939 (goto-char new)) | 2975 new)) |
2940 (c-keep-region-active)) | |
2941 | 2976 |
2942 | 2977 |
2943 ;; commands to indent lines, regions, defuns, and expressions | 2978 ;; commands to indent lines, regions, defuns, and expressions |
2944 (defun c-indent-command (&optional arg) | 2979 (defun c-indent-command (&optional arg) |
2945 "Indent current line as C code, and/or insert some whitespace. | 2980 "Indent current line as C code, and/or insert some whitespace. |