From 750e563f99c53f42392134c78148ca61bbc968c7 Mon Sep 17 00:00:00 2001 From: "Richard M. Stallman" Date: Thu, 29 Jan 2004 17:56:42 +0000 Subject: (beginning-of-defun-raw, end-of-defun): Iterate the hook function if arg is given. (mark-defun, narrow-to-defun): Change order of finding the limits. --- lisp/emacs-lisp/lisp.el | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'lisp/emacs-lisp/lisp.el') diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index 7f059d3f99f..4d90abd9f4e 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -188,7 +188,8 @@ If variable `beginning-of-defun-function' is non-nil, its value is called as a function to find the defun's beginning." (interactive "p") (if beginning-of-defun-function - (funcall beginning-of-defun-function) + (dotimes (i (or arg 1)) + (funcall beginning-of-defun-function)) (and arg (< arg 0) (not (eobp)) (forward-char 1)) (and (re-search-backward (if defun-prompt-regexp (concat (if open-paren-in-column-0-is-defun-start @@ -219,7 +220,8 @@ If variable `end-of-defun-function' is non-nil, its value is called as a function to find the defun's end." (interactive "p") (if end-of-defun-function - (funcall end-of-defun-function) + (dotimes (i (or arg 1)) + (funcall end-of-defun-function)) (if (or (null arg) (= arg 0)) (setq arg 1)) (let ((first t)) (while (and (> arg 0) (< (point) (point-max))) @@ -267,10 +269,14 @@ already marked." (end-of-defun) (point)))) (t + ;; Do it in this order for the sake of languages with nested + ;; functions where several can end at the same place as with + ;; the offside rule, e.g. Python. (push-mark (point)) - (end-of-defun) - (push-mark (point) nil t) (beginning-of-defun) + (push-mark (point) nil t) + (end-of-defun) + (exchange-point-and-mark) (re-search-backward "^\n" (- (point) 1) t)))) (defun narrow-to-defun (&optional arg) @@ -280,10 +286,13 @@ Optional ARG is ignored." (interactive) (save-excursion (widen) - (end-of-defun) - (let ((end (point))) - (beginning-of-defun) - (narrow-to-region (point) end)))) + ;; Do it in this order for the sake of languages with nested + ;; functions where several can end at the same place as with the + ;; offside rule, e.g. Python. + (beginning-of-defun) + (let ((beg (point))) + (end-of-defun) + (narrow-to-region beg (point))))) (defun insert-parentheses (arg) "Enclose following ARG sexps in parentheses. Leave point after open-paren. -- cgit v1.2.3 From 44b254cc4f3aa7a3f14691f0098782c35c0abdab Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Wed, 14 Apr 2004 18:20:23 +0000 Subject: (beginning-of-defun-raw, end-of-defun): Correctly handle negative arguments when calling hook functions. --- lisp/emacs-lisp/lisp.el | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'lisp/emacs-lisp/lisp.el') diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index 4d90abd9f4e..e1ed508b865 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -1,6 +1,6 @@ ;;; lisp.el --- Lisp editing commands for Emacs -;; Copyright (C) 1985, 1986, 1994, 2000 Free Software Foundation, Inc. +;; Copyright (C) 1985, 86, 1994, 2000, 2004 Free Software Foundation, Inc. ;; Maintainer: FSF ;; Keywords: lisp, languages @@ -188,8 +188,12 @@ If variable `beginning-of-defun-function' is non-nil, its value is called as a function to find the defun's beginning." (interactive "p") (if beginning-of-defun-function - (dotimes (i (or arg 1)) - (funcall beginning-of-defun-function)) + (if (> (setq arg (or arg 1)) 0) + (dotimes (i arg) + (funcall beginning-of-defun-function)) + ;; Better not call end-of-defun-function directly, in case + ;; it's not defined. + (end-of-defun (- arg))) (and arg (< arg 0) (not (eobp)) (forward-char 1)) (and (re-search-backward (if defun-prompt-regexp (concat (if open-paren-in-column-0-is-defun-start @@ -219,13 +223,17 @@ matches the open-parenthesis that starts a defun; see function If variable `end-of-defun-function' is non-nil, its value is called as a function to find the defun's end." (interactive "p") + (if (or (null arg) (= arg 0)) (setq arg 1)) (if end-of-defun-function - (dotimes (i (or arg 1)) - (funcall end-of-defun-function)) - (if (or (null arg) (= arg 0)) (setq arg 1)) + (if (> arg 0) + (dotimes (i arg) + (funcall end-of-defun-function)) + ;; Better not call beginning-of-defun-function + ;; directly, in case it's not defined. + (beginning-of-defun (- arg))) (let ((first t)) (while (and (> arg 0) (< (point) (point-max))) - (let ((pos (point)) npos) + (let ((pos (point))) (while (progn (if (and first (progn -- cgit v1.2.3 From 5891bf24d99843c76f56ec6f540b98d83d942426 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Sat, 1 May 2004 03:58:43 +0000 Subject: (beginning-of-defun, end-of-defun): Push mark on the first call of successive command calls. (insert-pair): New fun created from `insert-parentheses' with `open' and `close' arguments added. Enclose active regions in paired characters. Compare adjacent characters syntax with inserted characters syntax before inserting a space. (insert-parentheses): Call `insert-pair' with ?\( ?\). --- lisp/emacs-lisp/lisp.el | 57 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'lisp/emacs-lisp/lisp.el') diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index e1ed508b865..8fe839b474d 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -175,6 +175,8 @@ open-parenthesis, and point ends up at the beginning of the line. If variable `beginning-of-defun-function' is non-nil, its value is called as a function to find the defun's beginning." (interactive "p") + (and (eq this-command 'beginning-of-defun) + (or (eq last-command 'beginning-of-defun) (push-mark))) (and (beginning-of-defun-raw arg) (progn (beginning-of-line) t))) @@ -223,6 +225,8 @@ matches the open-parenthesis that starts a defun; see function If variable `end-of-defun-function' is non-nil, its value is called as a function to find the defun's end." (interactive "p") + (and (eq this-command 'end-of-defun) + (or (eq last-command 'end-of-defun) (push-mark))) (if (or (null arg) (= arg 0)) (setq arg 1)) (if end-of-defun-function (if (> arg 0) @@ -302,29 +306,48 @@ Optional ARG is ignored." (end-of-defun) (narrow-to-region beg (point))))) -(defun insert-parentheses (arg) - "Enclose following ARG sexps in parentheses. Leave point after open-paren. +(defun insert-pair (arg &optional open close) + "Enclose following ARG sexps in a pair of OPEN and CLOSE characters. +Leave point after the first character. A negative ARG encloses the preceding ARG sexps instead. -No argument is equivalent to zero: just insert `()' and leave point between. +No argument is equivalent to zero: just insert characters +and leave point between. If `parens-require-spaces' is non-nil, this command also inserts a space -before and after, depending on the surrounding characters." +before and after, depending on the surrounding characters. +If region is active, insert enclosing characters at region boundaries." (interactive "P") (if arg (setq arg (prefix-numeric-value arg)) (setq arg 0)) - (cond ((> arg 0) (skip-chars-forward " \t")) - ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) - (and parens-require-spaces - (not (bobp)) - (memq (char-syntax (preceding-char)) '(?w ?_ ?\) )) - (insert " ")) - (insert ?\() - (save-excursion - (or (eq arg 0) (forward-sexp arg)) - (insert ?\)) + (or open (setq open ?\()) + (or close (setq close ?\))) + (if (and transient-mark-mode mark-active) + (progn + (save-excursion (goto-char (region-end)) (insert close)) + (save-excursion (goto-char (region-beginning)) (insert open))) + (cond ((> arg 0) (skip-chars-forward " \t")) + ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) (and parens-require-spaces - (not (eobp)) - (memq (char-syntax (following-char)) '(?w ?_ ?\( )) - (insert " ")))) + (not (bobp)) + (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) + (insert " ")) + (insert open) + (save-excursion + (or (eq arg 0) (forward-sexp arg)) + (insert close) + (and parens-require-spaces + (not (eobp)) + (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) + (insert " "))))) + +(defun insert-parentheses (arg) + "Enclose following ARG sexps in parentheses. Leave point after open-paren. +A negative ARG encloses the preceding ARG sexps instead. +No argument is equivalent to zero: just insert `()' and leave point between. +If `parens-require-spaces' is non-nil, this command also inserts a space +before and after, depending on the surrounding characters. +If region is active, insert enclosing characters at region boundaries." + (interactive "P") + (insert-pair arg ?\( ?\))) (defun move-past-close-and-reindent () "Move past next `)', delete indentation before it, then indent after it." -- cgit v1.2.3 From d3d6bc9b3bbdf4301020ded17f80c4f842883f85 Mon Sep 17 00:00:00 2001 From: "Richard M. Stallman" Date: Sat, 22 May 2004 07:41:55 +0000 Subject: (mark-defun, narrow-to-defun): If moving back then fwd gets a defun that ends before point, try again moving fwd then back. --- lisp/emacs-lisp/lisp.el | 61 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 15 deletions(-) (limited to 'lisp/emacs-lisp/lisp.el') diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index 8fe839b474d..53b9e7507ef 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -281,15 +281,31 @@ already marked." (end-of-defun) (point)))) (t - ;; Do it in this order for the sake of languages with nested - ;; functions where several can end at the same place as with - ;; the offside rule, e.g. Python. - (push-mark (point)) - (beginning-of-defun) - (push-mark (point) nil t) - (end-of-defun) - (exchange-point-and-mark) - (re-search-backward "^\n" (- (point) 1) t)))) + (let ((opoint (point)) + beg end) + (push-mark opoint) + ;; Try first in this order for the sake of languages with nested + ;; functions where several can end at the same place as with + ;; the offside rule, e.g. Python. + (beginning-of-defun) + (setq beg (point)) + (end-of-defun) + (setq end (point)) + (while (looking-at "^\n") + (forward-line 1)) + (if (> (point) opoint) + (progn + ;; We got the right defun. + (push-mark beg nil t) + (goto-char end) + (exchange-point-and-mark)) + ;; beginning-of-defun moved back one defun + ;; so we got the wrong one. + (goto-char opoint) + (end-of-defun) + (push-mark (point) nil t) + (beginning-of-defun)) + (re-search-backward "^\n" (- (point) 1) t))))) (defun narrow-to-defun (&optional arg) "Make text outside current defun invisible. @@ -298,13 +314,28 @@ Optional ARG is ignored." (interactive) (save-excursion (widen) - ;; Do it in this order for the sake of languages with nested - ;; functions where several can end at the same place as with the - ;; offside rule, e.g. Python. - (beginning-of-defun) - (let ((beg (point))) + (let ((opoint (point)) + beg end) + ;; Try first in this order for the sake of languages with nested + ;; functions where several can end at the same place as with + ;; the offside rule, e.g. Python. + (beginning-of-defun) + (setq beg (point)) (end-of-defun) - (narrow-to-region beg (point))))) + (setq end (point)) + (while (looking-at "^\n") + (forward-line 1)) + (unless (> (point) opoint) + ;; beginning-of-defun moved back one defun + ;; so we got the wrong one. + (goto-char opoint) + (end-of-defun) + (setq end (point)) + (beginning-of-defun) + (setq beg (point))) + (goto-char end) + (re-search-backward "^\n" (- (point) 1) t) + (narrow-to-region beg end)))) (defun insert-pair (arg &optional open close) "Enclose following ARG sexps in a pair of OPEN and CLOSE characters. -- cgit v1.2.3 From d97c8198994758aa47bb2f43592ca994b2d1796d Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Fri, 28 May 2004 21:12:25 +0000 Subject: (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. --- lisp/emacs-lisp/lisp.el | 93 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 26 deletions(-) (limited to 'lisp/emacs-lisp/lisp.el') diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index 53b9e7507ef..25fde86cd96 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -337,7 +337,15 @@ Optional ARG is ignored." (re-search-backward "^\n" (- (point) 1) t) (narrow-to-region beg end)))) -(defun insert-pair (arg &optional open close) +(defvar insert-pair-alist + '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\')) + "Alist of paired characters inserted by `insert-pair'. +Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR +OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR +of the pair whose key is equal to the last input character with +or without modifiers, are inserted by `insert-pair'.") + +(defun insert-pair (&optional arg open close) "Enclose following ARG sexps in a pair of OPEN and CLOSE characters. Leave point after the first character. A negative ARG encloses the preceding ARG sexps instead. @@ -345,32 +353,47 @@ No argument is equivalent to zero: just insert characters and leave point between. If `parens-require-spaces' is non-nil, this command also inserts a space before and after, depending on the surrounding characters. -If region is active, insert enclosing characters at region boundaries." +If region is active, insert enclosing characters at region boundaries. + +If arguments OPEN and CLOSE are nil, the character pair is found +from the variable `insert-pair-alist' according to the last input +character with or without modifiers. If no character pair is +found in the variable `insert-pair-alist', then the last input +character is inserted ARG times." (interactive "P") - (if arg (setq arg (prefix-numeric-value arg)) - (setq arg 0)) - (or open (setq open ?\()) - (or close (setq close ?\))) - (if (and transient-mark-mode mark-active) - (progn - (save-excursion (goto-char (region-end)) (insert close)) - (save-excursion (goto-char (region-beginning)) (insert open))) - (cond ((> arg 0) (skip-chars-forward " \t")) - ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) - (and parens-require-spaces - (not (bobp)) - (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) - (insert " ")) - (insert open) - (save-excursion - (or (eq arg 0) (forward-sexp arg)) - (insert close) - (and parens-require-spaces - (not (eobp)) - (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) - (insert " "))))) - -(defun insert-parentheses (arg) + (if (not (and open close)) + (let ((pair (or (assq last-command-char insert-pair-alist) + (assq (event-basic-type last-command-event) + insert-pair-alist)))) + (if pair + (if (nth 2 pair) + (setq open (nth 1 pair) close (nth 2 pair)) + (setq open (nth 0 pair) close (nth 1 pair)))))) + (if (and open close) + (if (and transient-mark-mode mark-active) + (progn + (save-excursion (goto-char (region-end)) (insert close)) + (save-excursion (goto-char (region-beginning)) (insert open))) + (if arg (setq arg (prefix-numeric-value arg)) + (setq arg 0)) + (cond ((> arg 0) (skip-chars-forward " \t")) + ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) + (and parens-require-spaces + (not (bobp)) + (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) + (insert " ")) + (insert open) + (save-excursion + (or (eq arg 0) (forward-sexp arg)) + (insert close) + (and parens-require-spaces + (not (eobp)) + (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) + (insert " ")))) + (insert-char (event-basic-type last-command-event) + (prefix-numeric-value arg)))) + +(defun insert-parentheses (&optional arg) "Enclose following ARG sexps in parentheses. Leave point after open-paren. A negative ARG encloses the preceding ARG sexps instead. No argument is equivalent to zero: just insert `()' and leave point between. @@ -380,6 +403,24 @@ If region is active, insert enclosing characters at region boundaries." (interactive "P") (insert-pair arg ?\( ?\))) +(defun delete-pair () + "Delete a pair of characters enclosing the sexp that follows point." + (interactive) + (save-excursion (forward-sexp 1) (delete-char -1)) + (delete-char 1)) + +(defun raise-sexp (&optional arg) + "Raise ARG sexps higher up the tree." + (interactive "p") + (let ((s (if (and transient-mark-mode mark-active) + (buffer-substring (region-beginning) (region-end)) + (buffer-substring + (point) + (save-excursion (forward-sexp arg) (point)))))) + (backward-up-list 1) + (delete-region (point) (save-excursion (forward-sexp 1) (point))) + (save-excursion (insert s)))) + (defun move-past-close-and-reindent () "Move past next `)', delete indentation before it, then indent after it." (interactive) -- cgit v1.2.3