comparison lisp/emacs-lisp/lisp.el @ 55807:eb9d99ced391

(insert-pair-alist): New var. (insert-pair): Make arguments optional. Find character pair from `insert-pair-alist' according to the last input event. (insert-parentheses): Make arguments optional. (raise-sexp, delete-pair): New funs.
author Juri Linkov <juri@jurta.org>
date Fri, 28 May 2004 21:12:25 +0000
parents babf5161afd4
children 3dcc647295da 4c90ffeb71c5
comparison
equal deleted inserted replaced
55806:5e730ddfd23a 55807:eb9d99ced391
335 (setq beg (point))) 335 (setq beg (point)))
336 (goto-char end) 336 (goto-char end)
337 (re-search-backward "^\n" (- (point) 1) t) 337 (re-search-backward "^\n" (- (point) 1) t)
338 (narrow-to-region beg end)))) 338 (narrow-to-region beg end))))
339 339
340 (defun insert-pair (arg &optional open close) 340 (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)
341 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters. 349 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
342 Leave point after the first character. 350 Leave point after the first character.
343 A negative ARG encloses the preceding ARG sexps instead. 351 A negative ARG encloses the preceding ARG sexps instead.
344 No argument is equivalent to zero: just insert characters 352 No argument is equivalent to zero: just insert characters
345 and leave point between. 353 and leave point between.
346 If `parens-require-spaces' is non-nil, this command also inserts a space 354 If `parens-require-spaces' is non-nil, this command also inserts a space
347 before and after, depending on the surrounding characters. 355 before and after, depending on the surrounding characters.
348 If region is active, insert enclosing characters at region boundaries." 356 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."
349 (interactive "P") 363 (interactive "P")
350 (if arg (setq arg (prefix-numeric-value arg)) 364 (if (not (and open close))
351 (setq arg 0)) 365 (let ((pair (or (assq last-command-char insert-pair-alist)
352 (or open (setq open ?\()) 366 (assq (event-basic-type last-command-event)
353 (or close (setq close ?\))) 367 insert-pair-alist))))
354 (if (and transient-mark-mode mark-active) 368 (if pair
355 (progn 369 (if (nth 2 pair)
356 (save-excursion (goto-char (region-end)) (insert close)) 370 (setq open (nth 1 pair) close (nth 2 pair))
357 (save-excursion (goto-char (region-beginning)) (insert open))) 371 (setq open (nth 0 pair) close (nth 1 pair))))))
358 (cond ((> arg 0) (skip-chars-forward " \t")) 372 (if (and open close)
359 ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) 373 (if (and transient-mark-mode mark-active)
360 (and parens-require-spaces 374 (progn
361 (not (bobp)) 375 (save-excursion (goto-char (region-end)) (insert close))
362 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) 376 (save-excursion (goto-char (region-beginning)) (insert open)))
363 (insert " ")) 377 (if arg (setq arg (prefix-numeric-value arg))
364 (insert open) 378 (setq arg 0))
365 (save-excursion 379 (cond ((> arg 0) (skip-chars-forward " \t"))
366 (or (eq arg 0) (forward-sexp arg)) 380 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
367 (insert close) 381 (and parens-require-spaces
368 (and parens-require-spaces 382 (not (bobp))
369 (not (eobp)) 383 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
370 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) 384 (insert " "))
371 (insert " "))))) 385 (insert open)
372 386 (save-excursion
373 (defun insert-parentheses (arg) 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 (&optional arg)
374 "Enclose following ARG sexps in parentheses. Leave point after open-paren. 397 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
375 A negative ARG encloses the preceding ARG sexps instead. 398 A negative ARG encloses the preceding ARG sexps instead.
376 No argument is equivalent to zero: just insert `()' and leave point between. 399 No argument is equivalent to zero: just insert `()' and leave point between.
377 If `parens-require-spaces' is non-nil, this command also inserts a space 400 If `parens-require-spaces' is non-nil, this command also inserts a space
378 before and after, depending on the surrounding characters. 401 before and after, depending on the surrounding characters.
379 If region is active, insert enclosing characters at region boundaries." 402 If region is active, insert enclosing characters at region boundaries."
380 (interactive "P") 403 (interactive "P")
381 (insert-pair arg ?\( ?\))) 404 (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))))
382 423
383 (defun move-past-close-and-reindent () 424 (defun move-past-close-and-reindent ()
384 "Move past next `)', delete indentation before it, then indent after it." 425 "Move past next `)', delete indentation before it, then indent after it."
385 (interactive) 426 (interactive)
386 (up-list 1) 427 (up-list 1)