]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/sh-script.el
(calendar-dst-check-each-year-flag): Avoid
[gnu-emacs] / lisp / progmodes / sh-script.el
index 6098c8be06703ab096b65e9d8ac41718b94f7151..83b4bdea759e7f2bfbc4ecce2e9eaf65051747b0 100644 (file)
@@ -986,7 +986,9 @@ subshells can nest."
   ;; FIXME: This can (and often does) match multiple lines, yet it makes no
   ;; effort to handle multiline cases correctly, so it ends up being
   ;; rather flakey.
-  (when (re-search-forward "\"\\(?:\\(?:.\\|\n\\)*?[^\\]\\(?:\\\\\\\\\\)*\\)??\\(\\$(\\|`\\)" limit t)
+  (when (and (re-search-forward "\"\\(?:\\(?:.\\|\n\\)*?[^\\]\\(?:\\\\\\\\\\)*\\)??\\(\\$(\\|`\\)" limit t)
+             ;; Make sure the " we matched is an opening quote.
+            (eq ?\" (nth 3 (syntax-ppss))))
     ;; bingo we have a $( or a ` inside a ""
     (let ((char (char-after (point)))
           (continue t)
@@ -1081,9 +1083,6 @@ This is used to flag quote characters in subshell constructs inside strings
     ("\\(\\\\\\)'" 1 ,sh-st-punc)
     ;; Make sure $@ and @? are correctly recognized as sexps.
     ("\\$\\([?@]\\)" 1 ,sh-st-symbol)
-    ;; highlight (possibly nested) subshells inside "" quoted regions correctly.
-    (sh-quoted-subshell
-     (1 (sh-apply-quoted-subshell) t t))
     ;; Find HEREDOC starters and add a corresponding rule for the ender.
     (sh-font-lock-here-doc
      (2 (sh-font-lock-open-heredoc
@@ -1093,7 +1092,11 @@ This is used to flag quote characters in subshell constructs inside strings
          (and (match-beginning 3) (/= (match-beginning 3) (match-end 3))))
       nil t))
     ;; Distinguish the special close-paren in `case'.
-    (")" 0 (sh-font-lock-paren (match-beginning 0)))))
+    (")" 0 (sh-font-lock-paren (match-beginning 0)))
+    ;; highlight (possibly nested) subshells inside "" quoted regions correctly.
+    ;; This should be at the very end because it uses syntax-ppss.
+    (sh-quoted-subshell
+     (1 (sh-apply-quoted-subshell) t t))))
 
 (defun sh-font-lock-syntactic-face-function (state)
   (let ((q (nth 3 state)))
@@ -2457,46 +2460,45 @@ we go to the end of the previous line and do not check for continuations."
   ;;
   (if (bolp)
       nil
-    (let (c min-point
-         (start (point)))
-      (save-restriction
-       (narrow-to-region
-       (if (sh-this-is-a-continuation)
-           (setq min-point (sh-prev-line nil))
-         (save-excursion
-           (beginning-of-line)
-           (setq min-point (point))))
-       (point))
-       (skip-chars-backward " \t;")
-       (unless (looking-at "\\s-*;;")
-       (skip-chars-backward "^)}];\"'`({[")
-       (setq c (char-before))))
-      (sh-debug "stopping at %d c is %s  start=%d min-point=%d"
-               (point) c start min-point)
-      (if (< (point) min-point)
-         (error "point %d < min-point %d" (point) min-point))
-      (cond
-       ((looking-at "\\s-*;;")
-       ;; (message "Found ;; !")
-       ";;")
-       ((or (eq c ?\n)
-           (eq c nil)
-           (eq c ?\;))
-       (save-excursion
-        ;; skip forward over white space newline and \ at eol
-        (skip-chars-forward " \t\n\\\\")
-        (sh-debug "Now at %d   start=%d" (point) start)
-        (if (>= (point) start)
-            (progn
-              (sh-debug "point: %d >= start: %d" (point) start)
-              nil)
-          (sh-get-word))
-        ))
-       (t
-       ;; c    -- return a string
-       (char-to-string c)
-       ))
-      )))
+    (let ((start (point))
+          (min-point (if (sh-this-is-a-continuation)
+                         (sh-prev-line nil)
+                       (line-beginning-position))))
+      (skip-chars-backward " \t;" min-point)
+      (if (looking-at "\\s-*;;")
+          ;; (message "Found ;; !")
+          ";;"
+        (skip-chars-backward "^)}];\"'`({[" min-point)
+        (let ((c (if (> (point) min-point) (char-before))))
+          (sh-debug "stopping at %d c is %s  start=%d min-point=%d"
+                    (point) c start min-point)
+          (if (not (memq c '(?\n nil ?\;)))
+              ;; c     -- return a string
+              (char-to-string c)
+            ;; Return the leading keyword of the "command" we supposedly
+            ;; skipped over.  Maybe we skipped too far (e.g. past a `do' or
+            ;; `then' that precedes the actual command), so check whether
+            ;; we're looking at such a keyword and if so, move back forward.
+            (let ((boundary (point))
+                  kwd next)
+              (while
+                  (progn
+                    ;; Skip forward over white space newline and \ at eol.
+                    (skip-chars-forward " \t\n\\\\" start)
+                    (if (>= (point) start)
+                        (progn
+                          (sh-debug "point: %d >= start: %d" (point) start)
+                          nil)
+                      (if next (setq boundary next))
+                      (sh-debug "Now at %d   start=%d" (point) start)
+                      (setq kwd (sh-get-word))
+                      (if (member kwd (sh-feature sh-leading-keywords))
+                          (progn
+                            (setq next (point))
+                            t)
+                        nil))))
+              (goto-char boundary)
+              kwd)))))))
 
 
 (defun sh-this-is-a-continuation ()
@@ -2515,7 +2517,7 @@ If AND-MOVE is non-nil then move to end of word."
        (goto-char where))
     (prog1
        (buffer-substring (point)
-                         (progn (skip-chars-forward "^ \t\n;&")(point)))
+                         (progn (skip-chars-forward "^ \t\n;&|()")(point)))
       (unless and-move
        (goto-char start)))))