Mercurial > emacs
comparison lisp/progmodes/hideshow.el @ 91869:3952f2dc0abc
Remove the minor-mode bookkeeping. Move
make-variable-buffer-local code after the corresponding defvar.
(hs-minor-mode-map): Define and initialize in one step.
(hs-minor-mode): Change from defun to define-minor-mode.
author | Dan Nicolaescu <dann@ics.uci.edu> |
---|---|
date | Sat, 16 Feb 2008 07:40:06 +0000 |
parents | 4c3c683cdff8 |
children | b6212a3c659c |
comparison
equal
deleted
inserted
replaced
91868:46a8e4f391fb | 91869:3952f2dc0abc |
---|---|
343 | 343 |
344 (defvar hs-minor-mode nil | 344 (defvar hs-minor-mode nil |
345 "Non-nil if using hideshow mode as a minor mode of some other mode. | 345 "Non-nil if using hideshow mode as a minor mode of some other mode. |
346 Use the command `hs-minor-mode' to toggle or set this variable.") | 346 Use the command `hs-minor-mode' to toggle or set this variable.") |
347 | 347 |
348 (defvar hs-minor-mode-map nil | 348 (defvar hs-minor-mode-map |
349 (let ((map (make-sparse-keymap))) | |
350 ;; These bindings roughly imitate those used by Outline mode. | |
351 (define-key map "\C-c@\C-h" 'hs-hide-block) | |
352 (define-key map "\C-c@\C-s" 'hs-show-block) | |
353 (define-key map "\C-c@\C-\M-h" 'hs-hide-all) | |
354 (define-key map "\C-c@\C-\M-s" 'hs-show-all) | |
355 (define-key map "\C-c@\C-l" 'hs-hide-level) | |
356 (define-key map "\C-c@\C-c" 'hs-toggle-hiding) | |
357 (define-key map [(shift mouse-2)] 'hs-mouse-toggle-hiding) | |
358 (easy-menu-define hs-minor-mode-menu map | |
359 "Menu used when hideshow minor mode is active." | |
360 '("Hide/Show" | |
361 ["Hide Block" hs-hide-block | |
362 :help "Hide the code or comment block at point"] | |
363 ["Show Block" hs-show-block | |
364 :help "Show the code or comment block at point"] | |
365 ["Hide All" hs-hide-all | |
366 :help "Hide all the blocks in the buffer"] | |
367 ["Show All" hs-show-all | |
368 :help "Show all the clocks in the buffer"] | |
369 ["Hide Level" hs-hide-level | |
370 :help "Hide all block at levels below the current block"] | |
371 ["Toggle Hiding" hs-toggle-hiding | |
372 :help "Toggle the hiding state of the current block"])) | |
373 map) | |
349 "Keymap for hideshow minor mode.") | 374 "Keymap for hideshow minor mode.") |
350 | |
351 (defvar hs-minor-mode-menu nil | |
352 "Menu for hideshow minor mode.") | |
353 | 375 |
354 (defvar hs-c-start-regexp nil | 376 (defvar hs-c-start-regexp nil |
355 "Regexp for beginning of comments. | 377 "Regexp for beginning of comments. |
356 Differs from mode-specific comment regexps in that | 378 Differs from mode-specific comment regexps in that |
357 surrounding whitespace is stripped.") | 379 surrounding whitespace is stripped.") |
380 (make-variable-buffer-local 'hs-c-start-regexp) | |
358 | 381 |
359 (defvar hs-block-start-regexp nil | 382 (defvar hs-block-start-regexp nil |
360 "Regexp for beginning of block.") | 383 "Regexp for beginning of block.") |
384 (make-variable-buffer-local 'hs-block-start-regexp) | |
361 | 385 |
362 (defvar hs-block-start-mdata-select nil | 386 (defvar hs-block-start-mdata-select nil |
363 "Element in `hs-block-start-regexp' match data to consider as block start. | 387 "Element in `hs-block-start-regexp' match data to consider as block start. |
364 The internal function `hs-forward-sexp' moves point to the beginning of this | 388 The internal function `hs-forward-sexp' moves point to the beginning of this |
365 element (using `match-beginning') before calling `hs-forward-sexp-func'.") | 389 element (using `match-beginning') before calling `hs-forward-sexp-func'.") |
390 (make-variable-buffer-local 'hs-block-start-mdata-select) | |
366 | 391 |
367 (defvar hs-block-end-regexp nil | 392 (defvar hs-block-end-regexp nil |
368 "Regexp for end of block.") | 393 "Regexp for end of block.") |
369 | 394 |
370 (defvar hs-forward-sexp-func 'forward-sexp | 395 (defvar hs-forward-sexp-func 'forward-sexp |
372 Should change for Algol-ish modes. For single-character block | 397 Should change for Algol-ish modes. For single-character block |
373 delimiters -- ie, the syntax table regexp for the character is | 398 delimiters -- ie, the syntax table regexp for the character is |
374 either `(' or `)' -- `hs-forward-sexp-func' would just be | 399 either `(' or `)' -- `hs-forward-sexp-func' would just be |
375 `forward-sexp'. For other modes such as simula, a more specialized | 400 `forward-sexp'. For other modes such as simula, a more specialized |
376 function is necessary.") | 401 function is necessary.") |
402 (make-variable-buffer-local 'hs-forward-sexp-func) | |
377 | 403 |
378 (defvar hs-adjust-block-beginning nil | 404 (defvar hs-adjust-block-beginning nil |
379 "Function used to tweak the block beginning. | 405 "Function used to tweak the block beginning. |
380 The block is hidden from the position returned by this function, | 406 The block is hidden from the position returned by this function, |
381 as opposed to hiding it from the position returned when searching | 407 as opposed to hiding it from the position returned when searching |
392 It should return the position from where we should start hiding. | 418 It should return the position from where we should start hiding. |
393 | 419 |
394 It should not move the point. | 420 It should not move the point. |
395 | 421 |
396 See `hs-c-like-adjust-block-beginning' for an example of using this.") | 422 See `hs-c-like-adjust-block-beginning' for an example of using this.") |
423 (make-variable-buffer-local 'hs-adjust-block-beginning) | |
397 | 424 |
398 (defvar hs-headline nil | 425 (defvar hs-headline nil |
399 "Text of the line where a hidden block begins, set during isearch. | 426 "Text of the line where a hidden block begins, set during isearch. |
400 You can display this in the mode line by adding the symbol `hs-headline' | 427 You can display this in the mode line by adding the symbol `hs-headline' |
401 to the variable `mode-line-format'. For example, | 428 to the variable `mode-line-format'. For example, |
871 ;; see if we have enough comment lines to hide | 898 ;; see if we have enough comment lines to hide |
872 (when (> (count-lines beg end) 1) | 899 (when (> (count-lines beg end) 1) |
873 (hs-hide-comment-region beg end))))))) | 900 (hs-hide-comment-region beg end))))))) |
874 | 901 |
875 ;;;###autoload | 902 ;;;###autoload |
876 (defun hs-minor-mode (&optional arg) | 903 (define-minor-mode hs-minor-mode |
877 "Toggle hideshow minor mode. | 904 "Minor mode to selectively hide/show code and comment blocks. |
878 With ARG, turn hideshow minor mode on if ARG is positive, off otherwise. | |
879 When hideshow minor mode is on, the menu bar is augmented with hideshow | 905 When hideshow minor mode is on, the menu bar is augmented with hideshow |
880 commands and the hideshow commands are enabled. | 906 commands and the hideshow commands are enabled. |
881 The value '(hs . t) is added to `buffer-invisibility-spec'. | 907 The value '(hs . t) is added to `buffer-invisibility-spec'. |
882 | 908 |
883 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block', | 909 The main commands are: `hs-hide-all', `hs-show-all', `hs-hide-block', |
889 | 915 |
890 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'. | 916 Lastly, the normal hook `hs-minor-mode-hook' is run using `run-hooks'. |
891 | 917 |
892 Key bindings: | 918 Key bindings: |
893 \\{hs-minor-mode-map}" | 919 \\{hs-minor-mode-map}" |
894 | 920 :group 'hideshow |
895 (interactive "P") | 921 :lighter " hs" |
896 (setq hs-headline nil | 922 :keymap hs-minor-mode-map |
897 hs-minor-mode (if (null arg) | 923 (setq hs-headline nil) |
898 (not hs-minor-mode) | |
899 (> (prefix-numeric-value arg) 0))) | |
900 (if hs-minor-mode | 924 (if hs-minor-mode |
901 (progn | 925 (progn |
902 (hs-grok-mode-type) | 926 (hs-grok-mode-type) |
903 ;; Turn off this mode if we change major modes. | 927 ;; Turn off this mode if we change major modes. |
904 (add-hook 'change-major-mode-hook | 928 (add-hook 'change-major-mode-hook |
905 'turn-off-hideshow | 929 'turn-off-hideshow |
906 nil t) | 930 nil t) |
907 (easy-menu-add hs-minor-mode-menu) | 931 (easy-menu-add hs-minor-mode-menu) |
908 (set (make-local-variable 'line-move-ignore-invisible) t) | 932 (set (make-local-variable 'line-move-ignore-invisible) t) |
909 (add-to-invisibility-spec '(hs . t))) | 933 (add-to-invisibility-spec '(hs . t))) |
910 (easy-menu-remove hs-minor-mode-menu) | |
911 (remove-from-invisibility-spec '(hs . t)) | 934 (remove-from-invisibility-spec '(hs . t)) |
912 ;; hs-show-all does nothing unless h-m-m is non-nil. | 935 ;; hs-show-all does nothing unless h-m-m is non-nil. |
913 (let ((hs-minor-mode t)) | 936 (let ((hs-minor-mode t)) |
914 (hs-show-all))) | 937 (hs-show-all)))) |
915 (run-hooks 'hs-minor-mode-hook)) | |
916 | 938 |
917 ;;;###autoload | 939 ;;;###autoload |
918 (defun turn-off-hideshow () | 940 (defun turn-off-hideshow () |
919 "Unconditionally turn off `hs-minor-mode'." | 941 "Unconditionally turn off `hs-minor-mode'." |
920 (hs-minor-mode -1)) | 942 (hs-minor-mode -1)) |
921 | 943 |
922 ;;--------------------------------------------------------------------------- | 944 ;;--------------------------------------------------------------------------- |
923 ;; load-time actions | |
924 | |
925 ;; keymaps and menus | |
926 (unless hs-minor-mode-map | |
927 (setq hs-minor-mode-map (make-sparse-keymap)) | |
928 (easy-menu-define hs-minor-mode-menu | |
929 hs-minor-mode-map | |
930 "Menu used when hideshow minor mode is active." | |
931 (cons "Hide/Show" | |
932 (mapcar | |
933 ;; Interpret each table entry as follows: first, populate keymap | |
934 ;; with elements 2 and 1; then, for easymenu, use entry directly | |
935 ;; unless element 0 is nil, in which case the entry is "omitted". | |
936 (lambda (ent) | |
937 (define-key hs-minor-mode-map (aref ent 2) (aref ent 1)) | |
938 (if (aref ent 0) ent "-----")) | |
939 ;; These bindings roughly imitate those used by Outline mode. | |
940 ;; menu entry command key | |
941 '(["Hide Block" hs-hide-block "\C-c@\C-h"] | |
942 ["Show Block" hs-show-block "\C-c@\C-s"] | |
943 ["Hide All" hs-hide-all "\C-c@\C-\M-h"] | |
944 ["Show All" hs-show-all "\C-c@\C-\M-s"] | |
945 ["Hide Level" hs-hide-level "\C-c@\C-l"] | |
946 ["Toggle Hiding" hs-toggle-hiding "\C-c@\C-c"] | |
947 [nil hs-mouse-toggle-hiding [(shift mouse-2)]] | |
948 ))))) | |
949 | |
950 ;; some housekeeping | |
951 (add-to-list 'minor-mode-map-alist (cons 'hs-minor-mode hs-minor-mode-map)) | |
952 (add-to-list 'minor-mode-alist '(hs-minor-mode " hs") t) | |
953 | |
954 ;; make some variables buffer-local | |
955 (dolist (var '(hs-minor-mode | |
956 hs-c-start-regexp | |
957 hs-block-start-regexp | |
958 hs-block-start-mdata-select | |
959 hs-block-end-regexp | |
960 hs-forward-sexp-func | |
961 hs-adjust-block-beginning)) | |
962 (make-variable-buffer-local var)) | |
963 | |
964 ;;--------------------------------------------------------------------------- | |
965 ;; that's it | 945 ;; that's it |
966 | 946 |
967 (provide 'hideshow) | 947 (provide 'hideshow) |
968 | 948 |
969 ;; arch-tag: 378b6852-e82a-466a-aee8-d9c73859a65e | 949 ;; arch-tag: 378b6852-e82a-466a-aee8-d9c73859a65e |