comparison lisp/progmodes/c-mode.el @ 3906:a03d6cac1cd1

(c-forward-conditional): New function. (c-up-conditional): Use c-forward-conditional. (c-backward-conditional): New function. (c-mode-map): Make bindings for them.
author Richard M. Stallman <rms@gnu.org>
date Mon, 28 Jun 1993 04:48:51 +0000
parents c8af07496d54
children 8b53e0fa08b4
comparison
equal deleted inserted replaced
3905:5669887e3692 3906:a03d6cac1cd1
46 (define-key c-mode-map "\e\C-h" 'mark-c-function) 46 (define-key c-mode-map "\e\C-h" 'mark-c-function)
47 (define-key c-mode-map "\e\C-q" 'indent-c-exp) 47 (define-key c-mode-map "\e\C-q" 'indent-c-exp)
48 (define-key c-mode-map "\ea" 'c-beginning-of-statement) 48 (define-key c-mode-map "\ea" 'c-beginning-of-statement)
49 (define-key c-mode-map "\ee" 'c-end-of-statement) 49 (define-key c-mode-map "\ee" 'c-end-of-statement)
50 (define-key c-mode-map "\eq" 'c-fill-paragraph) 50 (define-key c-mode-map "\eq" 'c-fill-paragraph)
51 (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional)
52 (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional)
53 (define-key c-mode-map "\C-c\C-u" 'c-up-conditional)
51 (define-key c-mode-map "\177" 'backward-delete-char-untabify) 54 (define-key c-mode-map "\177" 'backward-delete-char-untabify)
52 (define-key c-mode-map "\t" 'c-indent-command)) 55 (define-key c-mode-map "\t" 'c-indent-command))
53 56
54 ;; cmacexp is lame because it uses no preprocessor symbols. 57 ;; cmacexp is lame because it uses no preprocessor symbols.
55 ;; It isn't very extensible either -- hardcodes /lib/cpp. 58 ;; It isn't very extensible either -- hardcodes /lib/cpp.
1250 A prefix argument acts as a repeat count. With a negative argument, 1253 A prefix argument acts as a repeat count. With a negative argument,
1251 move forward to the end of the containing preprocessor conditional. 1254 move forward to the end of the containing preprocessor conditional.
1252 When going backwards, `#elif' is treated like `#else' followed by `#if'. 1255 When going backwards, `#elif' is treated like `#else' followed by `#if'.
1253 When going forwards, `#elif' is ignored." 1256 When going forwards, `#elif' is ignored."
1254 (interactive "p") 1257 (interactive "p")
1255 (let* ((forward (< count 0)) 1258 (c-forward-conditional (- count) t))
1259
1260 (defun c-backward-conditional (count &optional up-flag)
1261 "Move back across a preprocessor conditional, leaving mark behind.
1262 A prefix argument acts as a repeat count. With a negative argument,
1263 move forward across a preprocessor conditional."
1264 (interactive "p")
1265 (c-forward-conditional (- count) up-flag))
1266
1267 (defun c-forward-conditional (count &optional up-flag)
1268 "Move forward across a preprocessor conditional, leaving mark behind.
1269 A prefix argument acts as a repeat count. With a negative argument,
1270 move backward across a preprocessor conditional."
1271 (interactive "p")
1272 (let* ((forward (> count 0))
1256 (increment (if forward -1 1)) 1273 (increment (if forward -1 1))
1257 (search-function (if forward 're-search-forward 're-search-backward)) 1274 (search-function (if forward 're-search-forward 're-search-backward))
1258 (opoint (point)) 1275 (opoint (point))
1259 (new)) 1276 (new))
1260 (save-excursion 1277 (save-excursion
1261 (while (/= count 0) 1278 (while (/= count 0)
1262 (if forward (end-of-line)) 1279 (let ((depth (if up-flag 0 -1)) found)
1263 (let ((depth 0) found)
1264 (save-excursion 1280 (save-excursion
1265 ;; Find the "next" significant line in the proper direction. 1281 ;; Find the "next" significant line in the proper direction.
1266 (while (and (not found) 1282 (while (and (not found)
1267 ;; Rather than searching for a # sign that comes 1283 ;; Rather than searching for a # sign that comes
1268 ;; at the beginning of a line aside from whitespace, 1284 ;; at the beginning of a line aside from whitespace,
1274 "#[ \t]*\\(if\\|elif\\|endif\\)" 1290 "#[ \t]*\\(if\\|elif\\|endif\\)"
1275 nil t)) 1291 nil t))
1276 (beginning-of-line) 1292 (beginning-of-line)
1277 ;; Now verify it is really a preproc line. 1293 ;; Now verify it is really a preproc line.
1278 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)") 1294 (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)")
1279 (progn 1295 (let ((prev depth))
1280 ;; Update depth according to what we found. 1296 ;; Update depth according to what we found.
1281 (beginning-of-line) 1297 (beginning-of-line)
1282 (cond ((looking-at "[ \t]*#[ \t]*endif") 1298 (cond ((looking-at "[ \t]*#[ \t]*endif")
1283 (setq depth (+ depth increment))) 1299 (setq depth (+ depth increment)))
1284 ((looking-at "[ \t]*#[ \t]*elif") 1300 ((looking-at "[ \t]*#[ \t]*elif")
1285 (if (and forward (= depth 0)) 1301 (if (and forward (= depth 0))
1286 (setq found (point)))) 1302 (setq found (point))))
1287 (t (setq depth (- depth increment)))) 1303 (t (setq depth (- depth increment))))
1304 ;; If we are trying to move across, and we find
1305 ;; an end before we find a beginning, get an error.
1306 (if (and (< prev 0) (< depth prev))
1307 (error (if forward
1308 "No following conditional at this level"
1309 "No previous conditional at this level")))
1310 ;; When searching forward, start from next line
1311 ;; so that we don't find the same line again.
1312 (if forward (forward-line 1))
1288 ;; If this line exits a level of conditional, exit inner loop. 1313 ;; If this line exits a level of conditional, exit inner loop.
1289 (if (< depth 0) 1314 (if (< depth 0)
1290 (setq found (point))) 1315 (setq found (point)))))))
1291 ;; When searching forward, start from end of line
1292 ;; so that we don't find the same line again.
1293 (if forward (end-of-line))))))
1294 (or found 1316 (or found
1295 (error "No containing preprocessor conditional")) 1317 (error "No containing preprocessor conditional"))
1296 (goto-char (setq new found))) 1318 (goto-char (setq new found)))
1297 (setq count (- count increment)))) 1319 (setq count (+ count increment))))
1298 (push-mark) 1320 (push-mark)
1299 (goto-char new))) 1321 (goto-char new)))
1300 1322
1301 ;;; c-mode.el ends here 1323 ;;; c-mode.el ends here