Fix indentation/fontification of Java enum with "implements"/generic.
[bpt/emacs.git] / lisp / progmodes / hideshow.el
index 49202ab..fc753bf 100644 (file)
@@ -1,6 +1,6 @@
-;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks
+;;; hideshow.el --- minor mode cmds to selectively display code/comment blocks -*- coding: utf-8 -*-
 
-;; Copyright (C) 1994-201 Free Software Foundation, Inc.
+;; Copyright (C) 1994-2013 Free Software Foundation, Inc.
 
 ;; Author: Thien-Thi Nguyen <ttn@gnu.org>
 ;;      Dan Nicolaescu <dann@ics.uci.edu>
@@ -52,7 +52,7 @@
 ;;
 ;; First make sure hideshow.el is in a directory in your `load-path'.
 ;; You can optionally byte-compile it using `M-x byte-compile-file'.
-;; Then, add the following to your ~/.emacs:
+;; Then, add the following to your init file:
 ;;
 ;; (load-library "hideshow")
 ;; (add-hook 'X-mode-hook               ; other modes similarly
 ;;     Unfortunately, these workarounds do not restore hideshow state.
 ;;     If someone figures out a better way, please let me know.
 
-;; * Correspondance
+;; * Correspondence
 ;;
-;; Correspondance welcome; please indicate version number.  Send bug
+;; Correspondence welcome; please indicate version number.  Send bug
 ;; reports and inquiries to <ttn@gnu.org>.
 
 ;; * Thanks
 ;;  Dean Andrews, Alf-Ivar Holm, Holger Bauer, Christoph Conrad, Dave Love,
 ;;  Dirk Herrmann, Gael Marziou, Jan Djarv, Guillaume Leray, Moody Ahmad,
 ;;  Preston F. Crow, Lars Lindberg, Reto Zimmermann, Keith Sheffield,
-;;  Chew Meng Kuan, Tony Lam, Pete Ware, François Pinard, Stefan Monnier,
+;;  Chew Meng Kuan, Tony Lam, Pete Ware, François Pinard, Stefan Monnier,
 ;;  Joseph Eydelnant, Michael Ernst, Peter Heslin
 ;;
 ;; Special thanks go to Dan Nicolaescu, who reimplemented hideshow using
   :group 'languages)
 
 (defcustom hs-hide-comments-when-hiding-all t
-  "*Hide the comments too when you do an `hs-hide-all'."
+  "Hide the comments too when you do an `hs-hide-all'."
   :type 'boolean
   :group 'hideshow)
 
 (defcustom hs-minor-mode-hook nil
-  "*Hook called when hideshow minor mode is activated or deactivated."
+  "Hook called when hideshow minor mode is activated or deactivated."
   :type 'hook
   :group 'hideshow
   :version "21.1")
 
 (defcustom hs-isearch-open 'code
-  "*What kind of hidden blocks to open when doing `isearch'.
+  "What kind of hidden blocks to open when doing `isearch'.
 One of the following symbols:
 
   code    -- open only code blocks
@@ -272,7 +272,7 @@ This has effect only if `search-invisible' is set to `open'."
     (bibtex-mode ("@\\S(*\\(\\s(\\)" 1))
     (java-mode "{" "}" "/[*/]" nil nil)
     (js-mode "{" "}" "/[*/]" nil)))
-  "*Alist for initializing the hideshow variables for different modes.
+  "Alist for initializing the hideshow variables for different modes.
 Each element has the form
   (MODE START END COMMENT-START FORWARD-SEXP-FUNC ADJUST-BEG-FUNC).
 
@@ -300,25 +300,25 @@ appropriate values.  The regexps should not contain leading or trailing
 whitespace.  Case does not matter.")
 
 (defvar hs-hide-all-non-comment-function nil
-  "*Function called if non-nil when doing `hs-hide-all' for non-comments.")
+  "Function called if non-nil when doing `hs-hide-all' for non-comments.")
 
 (defvar hs-allow-nesting nil
-  "*If non-nil, hiding remembers internal blocks.
+  "If non-nil, hiding remembers internal blocks.
 This means that when the outer block is shown again,
 any previously hidden internal blocks remain hidden.")
 
 (defvar hs-hide-hook nil
-  "*Hook called (with `run-hooks') at the end of commands to hide text.
+  "Hook called (with `run-hooks') at the end of commands to hide text.
 These commands include the toggling commands (when the result is to hide
 a block), `hs-hide-all', `hs-hide-block' and `hs-hide-level'.")
 
 (defvar hs-show-hook nil
-  "*Hook called (with `run-hooks') at the end of commands to show text.
+  "Hook called (with `run-hooks') at the end of commands to show text.
 These commands include the toggling commands (when the result is to show
 a block), `hs-show-all' and `hs-show-block'.")
 
 (defvar hs-set-up-overlay nil
-  "*Function called with one arg, OV, a newly initialized overlay.
+  "Function called with one arg, OV, a newly initialized overlay.
 Hideshow puts a unique overlay on each range of text to be hidden
 in the buffer.  Here is a simple example of how to use this variable:
 
@@ -408,6 +408,8 @@ element (using `match-beginning') before calling `hs-forward-sexp-func'.")
 
 (defvar hs-block-end-regexp nil
   "Regexp for end of block.")
+(make-variable-buffer-local 'hs-block-end-regexp)
+
 
 (defvar hs-forward-sexp-func 'forward-sexp
   "Function used to do a `forward-sexp'.
@@ -539,7 +541,7 @@ property of an overlay."
 (defun hs-looking-at-block-start-p ()
   "Return non-nil if the point is at the block start."
   (and (looking-at hs-block-start-regexp)
-       (save-match-data (not (nth 4 (syntax-ppss))))))
+       (save-match-data (not (nth 8 (syntax-ppss))))))
 
 (defun hs-forward-sexp (match-data arg)
   "Adjust point based on MATCH-DATA and call `hs-forward-sexp-func' w/ ARG.
@@ -604,9 +606,10 @@ we return a list having a nil as its car and the end of comment position
 as cdr."
   (save-excursion
     ;; the idea is to look backwards for a comment start regexp, do a
-    ;; forward comment, and see if we are inside, then extend extend
+    ;; forward comment, and see if we are inside, then extend
     ;; forward and backward as long as we have comments
     (let ((q (point)))
+      (skip-chars-forward "[:blank:]")
       (when (or (looking-at hs-c-start-regexp)
                 (re-search-backward hs-c-start-regexp (point-min) t))
         ;; first get to the beginning of this comment...
@@ -693,8 +696,8 @@ Return point, or nil if original point was not in a block."
         (point)
       ;; look backward for the start of a block that contains the cursor
       (while (and (re-search-backward hs-block-start-regexp nil t)
-                 ;; go again if in a comment
-                 (or (save-match-data (nth 4 (syntax-ppss)))
+                 ;; go again if in a comment or a string
+                 (or (save-match-data (nth 8 (syntax-ppss)))
                      (not (setq done
                                 (< here (save-excursion
                                           (hs-forward-sexp (match-data t) 1)
@@ -718,7 +721,7 @@ Return point, or nil if original point was not in a block."
            (and (< (point) maxp)
                 (re-search-forward hs-block-start-regexp maxp t)))
     (when (save-match-data
-           (not (nth 4 (syntax-ppss)))) ; not inside comments
+           (not (nth 8 (syntax-ppss)))) ; not inside comments or strings
       (if (> arg 1)
          (hs-hide-level-recursive (1- arg) minp maxp)
        (goto-char (match-beginning hs-block-start-mdata-select))
@@ -801,12 +804,15 @@ If `hs-hide-comments-when-hiding-all' is non-nil, also hide the comments."
                   (forward-comment (point-max)))
                 (re-search-forward re (point-max) t))
          (if (match-beginning 1)
-             ;; we have found a block beginning
+             ;; We have found a block beginning.
              (progn
                (goto-char (match-beginning 1))
-               (if hs-hide-all-non-comment-function
-                   (funcall hs-hide-all-non-comment-function)
-                 (hs-hide-block-at-point t)))
+              (unless (if hs-hide-all-non-comment-function
+                          (funcall hs-hide-all-non-comment-function)
+                        (hs-hide-block-at-point t))
+                ;; Go to end of matched data to prevent from getting stuck
+                ;; with an endless loop.
+                (goto-char (match-end 0))))
            ;; found a comment, probably
            (let ((c-reg (hs-inside-comment-p)))
              (when (and c-reg (car c-reg))
@@ -928,6 +934,10 @@ This can be useful if you have huge RCS logs in those comments."
 ;;;###autoload
 (define-minor-mode hs-minor-mode
   "Minor mode to selectively hide/show code and comment blocks.
+With a prefix argument ARG, enable the mode if ARG is positive,
+and disable it otherwise.  If called from Lisp, enable the mode
+if ARG is omitted or nil.
+
 When hideshow minor mode is on, the menu bar is augmented with hideshow
 commands and the hideshow commands are enabled.
 The value '(hs . t) is added to `buffer-invisibility-spec'.