aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorStefan Monnier2014-07-08 12:51:35 -0400
committerStefan Monnier2014-07-08 12:51:35 -0400
commit5e799349fde90ef1ad9c73261ecdac0a11ef57ff (patch)
treeefc7bb2f0e2a63b74f3d3fbfcb1723c6316d6e4e /lisp
parenta897d1b1febb05b7d45d51351356f25e3c0c0b89 (diff)
downloademacs-5e799349fde90ef1ad9c73261ecdac0a11ef57ff.tar.gz
emacs-5e799349fde90ef1ad9c73261ecdac0a11ef57ff.zip
* lisp/progmodes/perl-mode.el: Use syntax-ppss; fix one indentation case.
(perl-indent-line): Use syntax-ppss to detect we're in a doc-section. (perl-continuation-line-p): Don't skip over anything else than labels. Return the previous char. (perl-calculate-indent): Use syntax-ppss instead of parse-start and update callers accordingly. For continuation lines, check the the case of array hashes. (perl-backward-to-noncomment): Make it non-interactive. (perl-backward-to-start-of-continued-exp): Rewrite. * test/indent/perl.perl: Add indentation pattern for hash-table entries.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/ChangeLog16
-rw-r--r--lisp/progmodes/perl-mode.el287
2 files changed, 143 insertions, 160 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index ca7077b3d55..29e55de249d 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,15 @@
12014-07-08 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * progmodes/perl-mode.el: Use syntax-ppss; fix one indentation case.
4 (perl-indent-line): Use syntax-ppss to detect we're in a doc-section.
5 (perl-continuation-line-p): Don't skip over anything else than labels.
6 Return the previous char.
7 (perl-calculate-indent): Use syntax-ppss instead of parse-start
8 and update callers accordingly. For continuation lines, check the
9 the case of array hashes.
10 (perl-backward-to-noncomment): Make it non-interactive.
11 (perl-backward-to-start-of-continued-exp): Rewrite.
12
12014-07-08 Sam Steingold <sds@gnu.org> 132014-07-08 Sam Steingold <sds@gnu.org>
2 14
3 * progmodes/inf-lisp.el (lisp-eval-paragraph, lisp-eval-form-and-next): 15 * progmodes/inf-lisp.el (lisp-eval-paragraph, lisp-eval-form-and-next):
@@ -25,8 +37,8 @@
25 (with-temp-buffer-window, with-current-buffer-window): 37 (with-temp-buffer-window, with-current-buffer-window):
26 Use `macroexp-let2' to evaluate and bind variables 38 Use `macroexp-let2' to evaluate and bind variables
27 in the same order as macro arguments. 39 in the same order as macro arguments.
28 (display-buffer--action-function-custom-type): Add 40 (display-buffer--action-function-custom-type):
29 `display-buffer-below-selected' and `display-buffer-at-bottom'. 41 Add `display-buffer-below-selected' and `display-buffer-at-bottom'.
30 42
31 * minibuffer.el (minibuffer-completion-help): Replace 43 * minibuffer.el (minibuffer-completion-help): Replace
32 `with-output-to-temp-buffer' with `with-displayed-buffer-window' 44 `with-output-to-temp-buffer' with `with-displayed-buffer-window'
diff --git a/lisp/progmodes/perl-mode.el b/lisp/progmodes/perl-mode.el
index 092aa2b2fac..476a98926e2 100644
--- a/lisp/progmodes/perl-mode.el
+++ b/lisp/progmodes/perl-mode.el
@@ -748,7 +748,7 @@ following list:
748 (bof (perl-beginning-of-function)) 748 (bof (perl-beginning-of-function))
749 (delta (progn 749 (delta (progn
750 (goto-char oldpnt) 750 (goto-char oldpnt)
751 (perl-indent-line "\f\\|;?#" bof)))) 751 (perl-indent-line "\f\\|;?#"))))
752 (and perl-tab-to-comment 752 (and perl-tab-to-comment
753 (= oldpnt (point)) ; done if point moved 753 (= oldpnt (point)) ; done if point moved
754 (if (listp delta) ; if line starts in a quoted string 754 (if (listp delta) ; if line starts in a quoted string
@@ -786,28 +786,23 @@ following list:
786 (ding t))))))))) 786 (ding t)))))))))
787(make-obsolete 'perl-indent-command 'indent-according-to-mode "24.4") 787(make-obsolete 'perl-indent-command 'indent-according-to-mode "24.4")
788 788
789(defun perl-indent-line (&optional nochange parse-start) 789(defun perl-indent-line (&optional nochange)
790 "Indent current line as Perl code. 790 "Indent current line as Perl code.
791Return the amount the indentation 791Return the amount the indentation
792changed by, or (parse-state) if line starts in a quoted string." 792changed by, or (parse-state) if line starts in a quoted string."
793 (let ((case-fold-search nil) 793 (let ((case-fold-search nil)
794 (pos (- (point-max) (point))) 794 (pos (- (point-max) (point)))
795 (bof (or parse-start (save-excursion
796 ;; Don't consider text on this line as a
797 ;; valid BOF from which to indent.
798 (goto-char (line-end-position 0))
799 (perl-beginning-of-function))))
800 beg indent shift-amt) 795 beg indent shift-amt)
801 (beginning-of-line) 796 (beginning-of-line)
802 (setq beg (point)) 797 (setq beg (point))
803 (setq shift-amt 798 (setq shift-amt
804 (cond ((eq (char-after bof) ?=) 0) 799 (cond ((eq 1 (nth 7 (syntax-ppss))) 0) ;For doc sections!
805 ((listp (setq indent (perl-calculate-indent bof))) indent) 800 ((listp (setq indent (perl-calculate-indent))) indent)
806 ((eq 'noindent indent) indent) 801 ((eq 'noindent indent) indent)
807 ((looking-at (or nochange perl-nochange)) 0) 802 ((looking-at (or nochange perl-nochange)) 0)
808 (t 803 (t
809 (skip-chars-forward " \t\f") 804 (skip-chars-forward " \t\f")
810 (setq indent (perl-indent-new-calculate nil indent bof)) 805 (setq indent (perl-indent-new-calculate nil indent))
811 (- indent (current-column))))) 806 (- indent (current-column)))))
812 (skip-chars-forward " \t\f") 807 (skip-chars-forward " \t\f")
813 (if (and (numberp shift-amt) (/= 0 shift-amt)) 808 (if (and (numberp shift-amt) (/= 0 shift-amt))
@@ -819,23 +814,21 @@ changed by, or (parse-state) if line starts in a quoted string."
819 (goto-char (- (point-max) pos))) 814 (goto-char (- (point-max) pos)))
820 shift-amt)) 815 shift-amt))
821 816
822(defun perl-continuation-line-p (limit) 817(defun perl-continuation-line-p ()
823 "Move to end of previous line and return non-nil if continued." 818 "Move to end of previous line and return non-nil if continued."
824 ;; Statement level. Is it a continuation or a new statement? 819 ;; Statement level. Is it a continuation or a new statement?
825 ;; Find previous non-comment character. 820 ;; Find previous non-comment character.
826 (perl-backward-to-noncomment) 821 (perl-backward-to-noncomment)
827 ;; Back up over label lines, since they don't 822 ;; Back up over label lines, since they don't
828 ;; affect whether our line is a continuation. 823 ;; affect whether our line is a continuation.
829 (while (or (eq (preceding-char) ?\,) 824 (while (and (eq (preceding-char) ?:)
830 (and (eq (preceding-char) ?:) 825 (memq (char-syntax (char-after (- (point) 2)))
831 (memq (char-syntax (char-after (- (point) 2))) 826 '(?w ?_)))
832 '(?w ?_)))) 827 (beginning-of-line)
833 (if (eq (preceding-char) ?\,)
834 (perl-backward-to-start-of-continued-exp limit)
835 (beginning-of-line))
836 (perl-backward-to-noncomment)) 828 (perl-backward-to-noncomment))
837 ;; Now we get the answer. 829 ;; Now we get the answer.
838 (not (memq (preceding-char) '(?\; ?\} ?\{)))) 830 (unless (memq (preceding-char) '(?\; ?\} ?\{))
831 (preceding-char)))
839 832
840(defun perl-hanging-paren-p () 833(defun perl-hanging-paren-p ()
841 "Non-nil if we are right after a hanging parenthesis-like char." 834 "Non-nil if we are right after a hanging parenthesis-like char."
@@ -843,173 +836,151 @@ changed by, or (parse-state) if line starts in a quoted string."
843 (save-excursion 836 (save-excursion
844 (skip-syntax-backward " (") (not (bolp))))) 837 (skip-syntax-backward " (") (not (bolp)))))
845 838
846(defun perl-indent-new-calculate (&optional virtual default parse-start) 839(defun perl-indent-new-calculate (&optional virtual default)
847 (or 840 (or
848 (and virtual (save-excursion (skip-chars-backward " \t") (bolp)) 841 (and virtual (save-excursion (skip-chars-backward " \t") (bolp))
849 (current-column)) 842 (current-column))
850 (and (looking-at "\\(\\w\\|\\s_\\)+:[^:]") 843 (and (looking-at "\\(\\w\\|\\s_\\)+:[^:]")
851 (max 1 (+ (or default (perl-calculate-indent parse-start)) 844 (max 1 (+ (or default (perl-calculate-indent))
852 perl-label-offset))) 845 perl-label-offset)))
853 (and (= (char-syntax (following-char)) ?\)) 846 (and (= (char-syntax (following-char)) ?\))
854 (save-excursion 847 (save-excursion
855 (forward-char 1) 848 (forward-char 1)
856 (when (condition-case nil (progn (forward-sexp -1) t) 849 (when (condition-case nil (progn (forward-sexp -1) t)
857 (scan-error nil)) 850 (scan-error nil))
858 (perl-indent-new-calculate 851 (perl-indent-new-calculate 'virtual))))
859 ;; Recalculate the parsing-start, since we may have jumped
860 ;; dangerously close (typically in the case of nested functions).
861 'virtual nil (save-excursion (perl-beginning-of-function))))))
862 (and (and (= (following-char) ?{) 852 (and (and (= (following-char) ?{)
863 (save-excursion (forward-char) (perl-hanging-paren-p))) 853 (save-excursion (forward-char) (perl-hanging-paren-p)))
864 (+ (or default (perl-calculate-indent parse-start)) 854 (+ (or default (perl-calculate-indent))
865 perl-brace-offset)) 855 perl-brace-offset))
866 (or default (perl-calculate-indent parse-start)))) 856 (or default (perl-calculate-indent))))
867 857
868(defun perl-calculate-indent (&optional parse-start) 858(defun perl-calculate-indent ()
869 "Return appropriate indentation for current line as Perl code. 859 "Return appropriate indentation for current line as Perl code.
870In usual case returns an integer: the column to indent to. 860In usual case returns an integer: the column to indent to.
871Returns (parse-state) if line starts inside a string. 861Returns (parse-state) if line starts inside a string."
872Optional argument PARSE-START should be the position of `beginning-of-defun'."
873 (save-excursion 862 (save-excursion
874 (let ((indent-point (point)) 863 (let ((indent-point (point))
875 (case-fold-search nil) 864 (case-fold-search nil)
876 (colon-line-end 0) 865 (colon-line-end 0)
866 prev-char
877 state containing-sexp) 867 state containing-sexp)
878 (if parse-start ;used to avoid searching 868 (setq containing-sexp (nth 1 (syntax-ppss indent-point)))
879 (goto-char parse-start)
880 (perl-beginning-of-function))
881 ;; We might be now looking at a local function that has nothing to
882 ;; do with us because `indent-point' is past it. In this case
883 ;; look further back up for another `perl-beginning-of-function'.
884 (while (and (looking-at "{")
885 (save-excursion
886 (beginning-of-line)
887 (looking-at "\\s-+sub\\>"))
888 (> indent-point (save-excursion
889 (condition-case nil
890 (forward-sexp 1)
891 (scan-error nil))
892 (point))))
893 (perl-beginning-of-function))
894 (while (< (point) indent-point) ;repeat until right sexp
895 (setq state (parse-partial-sexp (point) indent-point 0))
896 ;; state = (depth_in_parens innermost_containing_list
897 ;; last_complete_sexp string_terminator_or_nil inside_commentp
898 ;; following_quotep minimum_paren-depth_this_scan)
899 ;; Parsing stops if depth in parentheses becomes equal to third arg.
900 (setq containing-sexp (nth 1 state)))
901 (cond 869 (cond
902 ;; Don't auto-indent in a quoted string or a here-document. 870 ;; Don't auto-indent in a quoted string or a here-document.
903 ((or (nth 3 state) (eq 2 (nth 7 state))) 'noindent) 871 ((or (nth 3 state) (eq 2 (nth 7 state))) 'noindent)
904 ((null containing-sexp) ; Line is at top level. 872 ((null containing-sexp) ; Line is at top level.
905 (skip-chars-forward " \t\f") 873 (skip-chars-forward " \t\f")
906 (if (memq (following-char) 874 (if (memq (following-char)
907 (if perl-indent-parens-as-block '(?\{ ?\( ?\[) '(?\{))) 875 (if perl-indent-parens-as-block '(?\{ ?\( ?\[) '(?\{)))
908 0 ; move to beginning of line if it starts a function body 876 0 ; move to beginning of line if it starts a function body
909 ;; indent a little if this is a continuation line 877 ;; indent a little if this is a continuation line
910 (perl-backward-to-noncomment) 878 (perl-backward-to-noncomment)
911 (if (or (bobp) 879 (if (or (bobp)
912 (memq (preceding-char) '(?\; ?\}))) 880 (memq (preceding-char) '(?\; ?\})))
913 0 perl-continued-statement-offset))) 881 0 perl-continued-statement-offset)))
914 ((/= (char-after containing-sexp) ?{) 882 ((/= (char-after containing-sexp) ?{)
915 ;; line is expression, not statement: 883 ;; line is expression, not statement:
916 ;; indent to just after the surrounding open. 884 ;; indent to just after the surrounding open.
917 (goto-char (1+ containing-sexp)) 885 (goto-char (1+ containing-sexp))
918 (if (perl-hanging-paren-p) 886 (if (perl-hanging-paren-p)
919 ;; We're indenting an arg of a call like: 887 ;; We're indenting an arg of a call like:
920 ;; $a = foobarlongnamefun ( 888 ;; $a = foobarlongnamefun (
921 ;; arg1 889 ;; arg1
922 ;; arg2 890 ;; arg2
923 ;; ); 891 ;; );
924 (progn 892 (progn
925 (skip-syntax-backward "(") 893 (skip-syntax-backward "(")
926 (condition-case nil 894 (condition-case nil
927 (while (save-excursion 895 (while (save-excursion
928 (skip-syntax-backward " ") (not (bolp))) 896 (skip-syntax-backward " ") (not (bolp)))
929 (forward-sexp -1)) 897 (forward-sexp -1))
930 (scan-error nil)) 898 (scan-error nil))
931 (+ (current-column) perl-indent-level)) 899 (+ (current-column) perl-indent-level))
932 (if perl-indent-continued-arguments 900 (if perl-indent-continued-arguments
933 (+ perl-indent-continued-arguments (current-indentation)) 901 (+ perl-indent-continued-arguments (current-indentation))
934 (skip-chars-forward " \t") 902 (skip-chars-forward " \t")
935 (current-column)))) 903 (current-column))))
936 (t 904 ;; Statement level. Is it a continuation or a new statement?
937 ;; Statement level. Is it a continuation or a new statement? 905 ((setq prev-char (perl-continuation-line-p))
938 (if (perl-continuation-line-p containing-sexp) 906 ;; This line is continuation of preceding line's statement;
939 ;; This line is continuation of preceding line's statement; 907 ;; indent perl-continued-statement-offset more than the
940 ;; indent perl-continued-statement-offset more than the 908 ;; previous line of the statement.
941 ;; previous line of the statement. 909 (perl-backward-to-start-of-continued-exp)
942 (progn 910 (+ (if (or (save-excursion
943 (perl-backward-to-start-of-continued-exp containing-sexp) 911 (perl-continuation-line-p))
944 (+ (if (save-excursion 912 (and (eq prev-char ?\,)
945 (perl-continuation-line-p containing-sexp)) 913 (looking-at "[[:alnum:]_]+[ \t\n]*=>")))
946 ;; If the continued line is itself a continuation 914 ;; If the continued line is itself a continuation
947 ;; line, then align, otherwise add an offset. 915 ;; line, then align, otherwise add an offset.
948 0 perl-continued-statement-offset) 916 0 perl-continued-statement-offset)
949 (current-column) 917 (current-column)
950 (if (save-excursion (goto-char indent-point) 918 (if (save-excursion (goto-char indent-point)
951 (looking-at 919 (looking-at
952 (if perl-indent-parens-as-block 920 (if perl-indent-parens-as-block
953 "[ \t]*[{(\[]" "[ \t]*{"))) 921 "[ \t]*[{(\[]" "[ \t]*{")))
954 perl-continued-brace-offset 0))) 922 perl-continued-brace-offset 0)))
955 ;; This line starts a new statement. 923 (t
956 ;; Position at last unclosed open. 924 ;; This line starts a new statement.
957 (goto-char containing-sexp) 925 ;; Position at last unclosed open.
958 (or 926 (goto-char containing-sexp)
959 ;; Is line first statement after an open-brace? 927 (or
960 ;; If no, find that first statement and indent like it. 928 ;; Is line first statement after an open-brace?
961 (save-excursion 929 ;; If no, find that first statement and indent like it.
962 (forward-char 1) 930 (save-excursion
963 ;; Skip over comments and labels following openbrace. 931 (forward-char 1)
964 (while (progn 932 ;; Skip over comments and labels following openbrace.
965 (skip-chars-forward " \t\f\n") 933 (while (progn
966 (cond ((looking-at ";?#") 934 (skip-chars-forward " \t\f\n")
967 (forward-line 1) t) 935 (cond ((looking-at ";?#")
968 ((looking-at "\\(\\w\\|\\s_\\)+:[^:]") 936 (forward-line 1) t)
969 (setq colon-line-end (line-end-position)) 937 ((looking-at "\\(\\w\\|\\s_\\)+:[^:]")
970 (search-forward ":"))))) 938 (setq colon-line-end (line-end-position))
971 ;; The first following code counts 939 (search-forward ":")))))
972 ;; if it is before the line we want to indent. 940 ;; The first following code counts
973 (and (< (point) indent-point) 941 ;; if it is before the line we want to indent.
974 (if (> colon-line-end (point)) 942 (and (< (point) indent-point)
975 (- (current-indentation) perl-label-offset) 943 (if (> colon-line-end (point))
976 (current-column)))) 944 (- (current-indentation) perl-label-offset)
977 ;; If no previous statement, 945 (current-column))))
978 ;; indent it relative to line brace is on. 946 ;; If no previous statement,
979 ;; For open paren in column zero, don't let statement 947 ;; indent it relative to line brace is on.
980 ;; start there too. If perl-indent-level is zero, 948 ;; For open paren in column zero, don't let statement
981 ;; use perl-brace-offset + perl-continued-statement-offset 949 ;; start there too. If perl-indent-level is zero,
982 ;; For open-braces not the first thing in a line, 950 ;; use perl-brace-offset + perl-continued-statement-offset
983 ;; add in perl-brace-imaginary-offset. 951 ;; For open-braces not the first thing in a line,
984 (+ (if (and (bolp) (zerop perl-indent-level)) 952 ;; add in perl-brace-imaginary-offset.
985 (+ perl-brace-offset perl-continued-statement-offset) 953 (+ (if (and (bolp) (zerop perl-indent-level))
986 perl-indent-level) 954 (+ perl-brace-offset perl-continued-statement-offset)
987 ;; Move back over whitespace before the openbrace. 955 perl-indent-level)
988 ;; If openbrace is not first nonwhite thing on the line, 956 ;; Move back over whitespace before the openbrace.
989 ;; add the perl-brace-imaginary-offset. 957 ;; If openbrace is not first nonwhite thing on the line,
990 (progn (skip-chars-backward " \t") 958 ;; add the perl-brace-imaginary-offset.
991 (if (bolp) 0 perl-brace-imaginary-offset)) 959 (progn (skip-chars-backward " \t")
992 ;; If the openbrace is preceded by a parenthesized exp, 960 (if (bolp) 0 perl-brace-imaginary-offset))
993 ;; move to the beginning of that; 961 ;; If the openbrace is preceded by a parenthesized exp,
994 ;; possibly a different line 962 ;; move to the beginning of that;
995 (progn 963 ;; possibly a different line
996 (if (eq (preceding-char) ?\)) 964 (progn
997 (forward-sexp -1)) 965 (if (eq (preceding-char) ?\))
998 ;; Get initial indentation of the line we are on. 966 (forward-sexp -1))
999 (current-indentation)))))))))) 967 ;; Get initial indentation of the line we are on.
968 (current-indentation)))))))))
1000 969
1001(defun perl-backward-to-noncomment () 970(defun perl-backward-to-noncomment ()
1002 "Move point backward to after the first non-white-space, skipping comments." 971 "Move point backward to after the first non-white-space, skipping comments."
1003 (interactive)
1004 (forward-comment (- (point-max)))) 972 (forward-comment (- (point-max))))
1005 973
1006(defun perl-backward-to-start-of-continued-exp (lim) 974(defun perl-backward-to-start-of-continued-exp ()
1007 (if (= (preceding-char) ?\)) 975 (while
1008 (forward-sexp -1)) 976 (let ((c (preceding-char)))
1009 (beginning-of-line) 977 (cond
1010 (if (<= (point) lim) 978 ((memq c '(?\; ?\{ ?\[ ?\()) (forward-comment (point-max)) nil)
1011 (goto-char (1+ lim))) 979 ((memq c '(?\) ?\] ?\} ?\"))
1012 (skip-chars-forward " \t\f")) 980 (forward-sexp -1) (forward-comment (- (point))) t)
981 ((eq ?w (char-syntax c))
982 (forward-word -1) (forward-comment (- (point))) t)
983 (t (forward-char -1) (forward-comment (- (point))) t)))))
1013 984
1014;; note: this may be slower than the c-mode version, but I can understand it. 985;; note: this may be slower than the c-mode version, but I can understand it.
1015(defalias 'indent-perl-exp 'perl-indent-exp) 986(defalias 'indent-perl-exp 'perl-indent-exp)
@@ -1034,7 +1005,7 @@ Optional argument PARSE-START should be the position of `beginning-of-defun'."
1034 (setq lsexp-mark bof-mark) 1005 (setq lsexp-mark bof-mark)
1035 (beginning-of-line) 1006 (beginning-of-line)
1036 (while (< (point) (marker-position last-mark)) 1007 (while (< (point) (marker-position last-mark))
1037 (setq delta (perl-indent-line nil (marker-position bof-mark))) 1008 (setq delta (perl-indent-line nil))
1038 (if (numberp delta) ; unquoted start-of-line? 1009 (if (numberp delta) ; unquoted start-of-line?
1039 (progn 1010 (progn
1040 (if (eolp) 1011 (if (eolp)