337 |
(re-search-backward "^\n" (- (point) 1) t) |
(re-search-backward "^\n" (- (point) 1) t) |
338 |
(narrow-to-region beg end)))) |
(narrow-to-region beg end)))) |
339 |
|
|
340 |
(defun insert-pair (arg &optional open close) |
(defvar insert-pair-alist |
341 |
|
'((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\')) |
342 |
|
"Alist of paired characters inserted by `insert-pair'. |
343 |
|
Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR |
344 |
|
OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR |
345 |
|
of the pair whose key is equal to the last input character with |
346 |
|
or without modifiers, are inserted by `insert-pair'.") |
347 |
|
|
348 |
|
(defun insert-pair (&optional arg open close) |
349 |
"Enclose following ARG sexps in a pair of OPEN and CLOSE characters. |
"Enclose following ARG sexps in a pair of OPEN and CLOSE characters. |
350 |
Leave point after the first character. |
Leave point after the first character. |
351 |
A negative ARG encloses the preceding ARG sexps instead. |
A negative ARG encloses the preceding ARG sexps instead. |
353 |
and leave point between. |
and leave point between. |
354 |
If `parens-require-spaces' is non-nil, this command also inserts a space |
If `parens-require-spaces' is non-nil, this command also inserts a space |
355 |
before and after, depending on the surrounding characters. |
before and after, depending on the surrounding characters. |
356 |
If region is active, insert enclosing characters at region boundaries." |
If region is active, insert enclosing characters at region boundaries. |
357 |
|
|
358 |
|
If arguments OPEN and CLOSE are nil, the character pair is found |
359 |
|
from the variable `insert-pair-alist' according to the last input |
360 |
|
character with or without modifiers. If no character pair is |
361 |
|
found in the variable `insert-pair-alist', then the last input |
362 |
|
character is inserted ARG times." |
363 |
(interactive "P") |
(interactive "P") |
364 |
(if arg (setq arg (prefix-numeric-value arg)) |
(if (not (and open close)) |
365 |
(setq arg 0)) |
(let ((pair (or (assq last-command-char insert-pair-alist) |
366 |
(or open (setq open ?\()) |
(assq (event-basic-type last-command-event) |
367 |
(or close (setq close ?\))) |
insert-pair-alist)))) |
368 |
(if (and transient-mark-mode mark-active) |
(if pair |
369 |
(progn |
(if (nth 2 pair) |
370 |
(save-excursion (goto-char (region-end)) (insert close)) |
(setq open (nth 1 pair) close (nth 2 pair)) |
371 |
(save-excursion (goto-char (region-beginning)) (insert open))) |
(setq open (nth 0 pair) close (nth 1 pair)))))) |
372 |
(cond ((> arg 0) (skip-chars-forward " \t")) |
(if (and open close) |
373 |
((< arg 0) (forward-sexp arg) (setq arg (- arg)))) |
(if (and transient-mark-mode mark-active) |
374 |
(and parens-require-spaces |
(progn |
375 |
(not (bobp)) |
(save-excursion (goto-char (region-end)) (insert close)) |
376 |
(memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) |
(save-excursion (goto-char (region-beginning)) (insert open))) |
377 |
(insert " ")) |
(if arg (setq arg (prefix-numeric-value arg)) |
378 |
(insert open) |
(setq arg 0)) |
379 |
(save-excursion |
(cond ((> arg 0) (skip-chars-forward " \t")) |
380 |
(or (eq arg 0) (forward-sexp arg)) |
((< arg 0) (forward-sexp arg) (setq arg (- arg)))) |
381 |
(insert close) |
(and parens-require-spaces |
382 |
(and parens-require-spaces |
(not (bobp)) |
383 |
(not (eobp)) |
(memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) |
384 |
(memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) |
(insert " ")) |
385 |
(insert " "))))) |
(insert open) |
386 |
|
(save-excursion |
387 |
|
(or (eq arg 0) (forward-sexp arg)) |
388 |
|
(insert close) |
389 |
|
(and parens-require-spaces |
390 |
|
(not (eobp)) |
391 |
|
(memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) |
392 |
|
(insert " ")))) |
393 |
|
(insert-char (event-basic-type last-command-event) |
394 |
|
(prefix-numeric-value arg)))) |
395 |
|
|
396 |
(defun insert-parentheses (arg) |
(defun insert-parentheses (&optional arg) |
397 |
"Enclose following ARG sexps in parentheses. Leave point after open-paren. |
"Enclose following ARG sexps in parentheses. Leave point after open-paren. |
398 |
A negative ARG encloses the preceding ARG sexps instead. |
A negative ARG encloses the preceding ARG sexps instead. |
399 |
No argument is equivalent to zero: just insert `()' and leave point between. |
No argument is equivalent to zero: just insert `()' and leave point between. |
403 |
(interactive "P") |
(interactive "P") |
404 |
(insert-pair arg ?\( ?\))) |
(insert-pair arg ?\( ?\))) |
405 |
|
|
406 |
|
(defun delete-pair () |
407 |
|
"Delete a pair of characters enclosing the sexp that follows point." |
408 |
|
(interactive) |
409 |
|
(save-excursion (forward-sexp 1) (delete-char -1)) |
410 |
|
(delete-char 1)) |
411 |
|
|
412 |
|
(defun raise-sexp (&optional arg) |
413 |
|
"Raise ARG sexps higher up the tree." |
414 |
|
(interactive "p") |
415 |
|
(let ((s (if (and transient-mark-mode mark-active) |
416 |
|
(buffer-substring (region-beginning) (region-end)) |
417 |
|
(buffer-substring |
418 |
|
(point) |
419 |
|
(save-excursion (forward-sexp arg) (point)))))) |
420 |
|
(backward-up-list 1) |
421 |
|
(delete-region (point) (save-excursion (forward-sexp 1) (point))) |
422 |
|
(save-excursion (insert s)))) |
423 |
|
|
424 |
(defun move-past-close-and-reindent () |
(defun move-past-close-and-reindent () |
425 |
"Move past next `)', delete indentation before it, then indent after it." |
"Move past next `)', delete indentation before it, then indent after it." |
426 |
(interactive) |
(interactive) |