comparison lisp/utils/id-select.el @ 26:441bb1e64a06 r19-15b96

Import from CVS: tag r19-15b96
author cvs
date Mon, 13 Aug 2007 08:51:32 +0200
parents b82b59fe008d
children 9b50b4588a93
comparison
equal deleted inserted replaced
25:383a494979f8 26:441bb1e64a06
1 ;;!emacs 1 ;;!emacs
2 ;; 2 ;;
3 ;; LCD-ENTRY: id-select.el|InfoDock Associates|elisp@infodock.com|Syntactical region selecting|12/02/96|1.4.3| 3 ;; LCD-ENTRY: id-select.el|InfoDock Associates|elisp@infodock.com|Syntactical region selecting|02/28/97|1.4.5|
4 ;; 4 ;;
5 ;; FILE: id-select.el 5 ;; FILE: id-select.el
6 ;; SUMMARY: Select larger and larger syntax-driven regions in a buffer. 6 ;; SUMMARY: Select larger and larger syntax-driven regions in a buffer.
7 ;; USAGE: XEmacs and Emacs Lisp Library 7 ;; USAGE: XEmacs and Emacs Lisp Library
8 ;; KEYWORDS: matching, mouse 8 ;; KEYWORDS: matching, mouse
9 ;; 9 ;;
10 ;; AUTHOR: Bob Weiner 10 ;; AUTHOR: Bob Weiner
11 ;; 11 ;;
12 ;; ORG: InfoDock Associates. We sell corporate support and development 12 ;; ORG: InfoDock Associates. We sell corporate support and
13 ;; contracts for InfoDock, Emacs and XEmacs. 13 ;; development contracts for InfoDock, Emacs and XEmacs.
14 ;; E-mail: <info@infodock.com> Web: http://www.infodock.com 14 ;; E-mail: <info@infodock.com> Web: http://www.infodock.com
15 ;; Tel: +1 408-243-3300 15 ;; Tel: +1 408-243-3300
16 ;; 16 ;;
17 ;;
17 ;; ORIG-DATE: 19-Oct-96 at 02:25:27 18 ;; ORIG-DATE: 19-Oct-96 at 02:25:27
18 ;; LAST-MOD: 2-Dec-96 at 19:45:28 by Bob Weiner 19 ;; LAST-MOD: 28-Feb-97 at 15:36:39 by Bob Weiner
19 ;; 20 ;;
20 ;; Copyright (C) 1996 InfoDock Associates 21 ;; Copyright (C) 1996, 1997 InfoDock Associates
21 ;; 22 ;;
22 ;; This file is part of InfoDock. 23 ;; This file is part of InfoDock.
23 ;; It is available for use and distribution under the terms of the GNU Public 24 ;; It is available for use and distribution under the terms of the GNU Public
24 ;; License. 25 ;; License.
25 ;; 26 ;;
60 ;; Loading this package automatically installs its functionalty on 61 ;; Loading this package automatically installs its functionalty on
61 ;; double-clicks (or higher) of the left mouse key. (See the documentation 62 ;; double-clicks (or higher) of the left mouse key. (See the documentation
62 ;; for the variable, mouse-track-click-hook, for how this is done.) A 63 ;; for the variable, mouse-track-click-hook, for how this is done.) A
63 ;; single click of the left button will remove the region and reset point. 64 ;; single click of the left button will remove the region and reset point.
64 ;; 65 ;;
65 ;; The function, id-select-thing, may be bound to a key, {C-c s}, seems to 66 ;; The function, id-select-thing, may be bound to a key to provide the same
66 ;; be a reasonable choice, to provide the same syntax-driven region 67 ;; syntax-driven region selection functionality. {C-c C-m} is a
67 ;; selection functionality. Use {C-g} to unmark the region when done. 68 ;; reasonable site-wide choice since this key is seldom used and it
68 ;; Use, id-select-thing-with-mouse, if you want to bind this to a mouse key 69 ;; mnemonically indicates marking something. {C-c s} may be preferred as a
69 ;; and thereby use single clicks instead of double clicks. 70 ;; personal binding.
71 ;;
72 ;; Use {C-g} to unmark the region when done. Use,
73 ;; id-select-thing-with-mouse, if you want to bind this to a mouse key and
74 ;; thereby use single clicks instead of double clicks.
70 ;; 75 ;;
71 ;; Three other commands are also provided: 76 ;; Three other commands are also provided:
72 ;; id-select-and-copy-thing - mark and copy the syntactical unit to the 77 ;; id-select-and-copy-thing - mark and copy the syntactical unit to the
73 ;; kill ring 78 ;; kill ring
74 ;; id-select-and-kill-thing - kill the syntactical unit at point 79 ;; id-select-and-kill-thing - kill the syntactical unit at point
139 ;; (setq sentence-end "\\([^ \t\n\r>]<\\|>\\(<[^>]*>\\)*\\|[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)\\)[ \t\n]*") 144 ;; (setq sentence-end "\\([^ \t\n\r>]<\\|>\\(<[^>]*>\\)*\\|[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)\\)[ \t\n]*")
140 ;; 145 ;;
141 ;; (define-key html-mode-map "\C-c." 'id-select-goto-matching-tag) 146 ;; (define-key html-mode-map "\C-c." 'id-select-goto-matching-tag)
142 ;; ))) 147 ;; )))
143 ;; 148 ;;
149 ;; If you are incredibly academic and you use the Miranda programming
150 ;; language with a literate programming style (where code is preceded by a
151 ;; > character in the first column, you'll want to change the line in
152 ;; mira.el that reads:
153 ;; (modify-syntax-entry ?> ".")
154 ;; to:
155 ;; (modify-syntax-entry ?> " ")
156 ;;
157 ;; in order to make this package recognize the indented expressions of the
158 ;; language. If you don't use the literate style, no changes should be
159 ;; necessary.
160 ;;
144 ;; DESCRIP-END. 161 ;; DESCRIP-END.
145 162
146 ;;; ************************************************************************ 163 ;;; ************************************************************************
147 ;;; Public variables 164 ;;; Public variables
148 ;;; ************************************************************************ 165 ;;; ************************************************************************
158 (defvar id-select-text-modes 175 (defvar id-select-text-modes
159 '(fundamental-mode kotl-mode indented-text-mode Info-mode outline-mode text-mode) 176 '(fundamental-mode kotl-mode indented-text-mode Info-mode outline-mode text-mode)
160 "*List of textual modes where paragraphs may be outdented or indented.") 177 "*List of textual modes where paragraphs may be outdented or indented.")
161 178
162 (defvar id-select-indent-modes 179 (defvar id-select-indent-modes
163 (append '(asm-mode csh-mode eiffel-mode ksh-mode python-mode pascal-mode 180 (append '(asm-mode csh-mode eiffel-mode ksh-mode miranda-mode python-mode
164 sather-mode) 181 pascal-mode sather-mode)
165 id-select-text-modes) 182 id-select-text-modes)
166 "*List of language major modes which use mostly indentation to define syntactic structure.") 183 "*List of language major modes which use mostly indentation to define syntactic structure.")
167 184
168 (defvar id-select-indent-non-end-regexp-alist 185 (defvar id-select-indent-non-end-regexp-alist
169 '((csh-mode "\\(\\|then\\|elsif\\|else\\)[ \t]*$") 186 '((csh-mode "\\(\\|then\\|elsif\\|else\\)[ \t]*$")
170 (eiffel-mode "\\(\\|then\\|else if\\|else\\)[ \t]*$") 187 (eiffel-mode "\\(\\|then\\|else if\\|else\\)[ \t]*$")
171 (ksh-mode "\\(\\|then\\|elif\\|else\\)[ \t]*$") 188 (ksh-mode "\\(\\|then\\|elif\\|else\\)[ \t]*$")
189 (miranda-mode "[ \t>]*$")
172 (pascal-mode "\\(\\|then\\|else\\)[ \t]*$") 190 (pascal-mode "\\(\\|then\\|else\\)[ \t]*$")
173 (python-mode "[ \t]*$") 191 (python-mode "[ \t]*$")
174 (sather-mode "\\(\\|then\\|else if\\|else\\)[ \t]*$") 192 (sather-mode "\\(\\|then\\|else if\\|else\\)[ \t]*$")
175 ;; 193 ;;
176 (fundamental-mode "[^ \t\n]") 194 (fundamental-mode "[^ \t\n*]")
177 (kotl-mode "[^ \t\n]") 195 (kotl-mode "[^ \t\n*]")
178 (indented-text-mode "[^ \t\n]") 196 (indented-text-mode "[^ \t\n*]")
179 (Info-mode "[^ \t\n]") 197 (Info-mode "[^ \t\n]")
180 (outline-mode "[^\\*]") 198 (outline-mode "[^*]")
181 (text-mode "[^ \t\n]") 199 (text-mode "[^ \t\n*]")
182 ) 200 )
183 "List of (major-mode . non-terminator-line-regexp) elements used to avoid early dropoff when marking indented code.") 201 "List of (major-mode . non-terminator-line-regexp) elements used to avoid early dropoff when marking indented code.")
184 202
185 (defvar id-select-indent-end-regexp-alist 203 (defvar id-select-indent-end-regexp-alist
186 '((csh-mode "end\\|while") 204 '((csh-mode "end\\|while")
534 (not (looking-at 552 (not (looking-at
535 (format ".*\\S-.*%s.*%s" 553 (format ".*\\S-.*%s.*%s"
536 (regexp-quote comment-start) 554 (regexp-quote comment-start)
537 (regexp-quote comment-end)))))))) 555 (regexp-quote comment-end))))))))
538 556
557 (defun id-select-back-to-indentation ()
558 "Move point to the first non-whitespace character on this line and return point.
559 This respects the current syntax table definition of whitespace, whereas
560 `back-to-indentation' does not. This is relevant in literate programming and
561 mail and news reply modes."
562 (goto-char (min (progn (end-of-line) (point))
563 (progn (beginning-of-line)
564 (skip-syntax-forward " ")
565 (point)))))
566
567 (defun id-select-bigger-thing ()
568 "Select a bigger object where point is."
569 (prog1
570 (id-select-thing)
571 (setq this-command 'select-thing)))
572
539 (defun id-select-region-bigger-p (old-region new-region) 573 (defun id-select-region-bigger-p (old-region new-region)
540 "Return t if OLD-REGION is smaller than NEW-REGION and NEW-REGION partially overlaps OLD-REGION, or if OLD-REGION is uninitialized." 574 "Return t if OLD-REGION is smaller than NEW-REGION and NEW-REGION partially overlaps OLD-REGION, or if OLD-REGION is uninitialized."
541 (if (null (car old-region)) 575 (if (null (car old-region))
542 t 576 t
543 (and (> (abs (- (cdr new-region) (car new-region))) 577 (and (> (abs (- (cdr new-region) (car new-region)))
549 (min (cdr old-region) (car old-region)))) 583 (min (cdr old-region) (car old-region))))
550 (and (> (min (cdr new-region) (car new-region)) 584 (and (> (min (cdr new-region) (car new-region))
551 (min (cdr old-region) (car old-region))) 585 (min (cdr old-region) (car old-region)))
552 (<= (min (cdr new-region) (car new-region)) 586 (<= (min (cdr new-region) (car new-region))
553 (max (cdr old-region) (car old-region)))))))) 587 (max (cdr old-region) (car old-region))))))))
554
555 (defun id-select-bigger-thing ()
556 "Select a bigger object where point is."
557 (prog1
558 (id-select-thing)
559 (setq this-command 'select-thing)))
560 588
561 (defun id-select-reset () 589 (defun id-select-reset ()
562 ;; Reset syntactic selection. 590 ;; Reset syntactic selection.
563 (setq id-select-prior-point (point) 591 (setq id-select-prior-point (point)
564 id-select-prior-buffer (current-buffer) 592 id-select-prior-buffer (current-buffer)
659 (error nil))))) 687 (error nil)))))
660 eod) 688 eod)
661 (if (or at-def-brace 689 (if (or at-def-brace
662 ;; At the start of a definition: 690 ;; At the start of a definition:
663 ;; Must be at the first non-whitespace character in the line. 691 ;; Must be at the first non-whitespace character in the line.
664 (and (= (point) (save-excursion (back-to-indentation) (point))) 692 (and (= (point) (save-excursion (id-select-back-to-indentation)))
665 ;; Must be on an alpha or symbol-constituent character. 693 ;; Must be on an alpha or symbol-constituent character.
666 ;; Also allow ~ for C++ destructors. 694 ;; Also allow ~ for C++ destructors.
667 (looking-at "[a-zA-z~]\\|\\s_") 695 (looking-at "[a-zA-z~]\\|\\s_")
668 ;; Previous line, if any, must be blank or a comment 696 ;; Previous line, if any, must be blank or a comment
669 ;; start or end or `defun-prompt-regexp' must be defined 697 ;; start or end or `defun-prompt-regexp' must be defined
675 (id-select-at-blank-line-or-comment)))))) 703 (id-select-at-blank-line-or-comment))))))
676 (progn 704 (progn
677 (setq id-select-previous 'brace-def-or-declaration) 705 (setq id-select-previous 'brace-def-or-declaration)
678 ;; Handle declarations and definitions embedded within classes. 706 ;; Handle declarations and definitions embedded within classes.
679 (if (and (= (following-char) ?{) 707 (if (and (= (following-char) ?{)
680 (/= (point) (save-excursion (back-to-indentation) (point)))) 708 (/= (point) (save-excursion
709 (id-select-back-to-indentation))))
681 (setq at-def-brace nil)) 710 (setq at-def-brace nil))
682 ;; 711 ;;
683 (if at-def-brace nil (beginning-of-line)) 712 (if at-def-brace nil (beginning-of-line))
684 (if (and (not at-def-brace) 713 (if (and (not at-def-brace)
685 (stringp defun-prompt-regexp) 714 (stringp defun-prompt-regexp)
739 (looking-at outline-regexp))) 768 (looking-at outline-regexp)))
740 ;; After indent in any other mode, must be on an alpha 769 ;; After indent in any other mode, must be on an alpha
741 ;; or symbol-constituent character. 770 ;; or symbol-constituent character.
742 (t (looking-at "[a-zA-z]\\|\\s_"))) 771 (t (looking-at "[a-zA-z]\\|\\s_")))
743 ;; Must be at the first non-whitespace character in the line. 772 ;; Must be at the first non-whitespace character in the line.
744 (= (point) (save-excursion (back-to-indentation) (point)))) 773 (= (point) (save-excursion (id-select-back-to-indentation))))
745 (let* ((start-col (current-column)) 774 (let* ((start-col (current-column))
746 (opoint (if (eq major-mode 'kotl-mode) 775 (opoint (if (eq major-mode 'kotl-mode)
747 (progn (kotl-mode:to-valid-position) (point)) 776 (progn (kotl-mode:to-valid-position) (point))
748 (beginning-of-line) (point)))) 777 (beginning-of-line) (point))))
749 (while 778 (while
750 (and (zerop (forward-line 1)) 779 (and (zerop (forward-line 1))
751 (bolp) 780 (bolp)
752 (or (progn (back-to-indentation) 781 (or (progn (id-select-back-to-indentation)
753 (> (current-column) start-col)) 782 (> (current-column) start-col))
754 ;; If in a text mode, allow outdenting, otherwise 783 ;; If in a text mode, allow outdenting, otherwise
755 ;; only include special lines here indented to the 784 ;; only include special lines here indented to the
756 ;; same point as the original line. 785 ;; same point as the original line.
757 (and (or (memq major-mode id-select-text-modes) 786 (and (or (memq major-mode id-select-text-modes)
844 (setq id-select-previous 'preprocessor-def) 873 (setq id-select-previous 'preprocessor-def)
845 (save-excursion 874 (save-excursion
846 (goto-char pos) 875 (goto-char pos)
847 (if (and (= (following-char) ?#) 876 (if (and (= (following-char) ?#)
848 ;; Must be at the first non-whitespace character in the line. 877 ;; Must be at the first non-whitespace character in the line.
849 (= (point) (save-excursion (back-to-indentation) (point)))) 878 (= (point) (save-excursion (id-select-back-to-indentation))))
850 (progn 879 (progn
851 ;; Skip past continuation lines that end with a backslash. 880 ;; Skip past continuation lines that end with a backslash.
852 (while (and (looking-at ".*\\\\\\s-*$") 881 (while (and (looking-at ".*\\\\\\s-*$")
853 (zerop (forward-line 1)))) 882 (zerop (forward-line 1))))
854 (forward-line 1) 883 (forward-line 1)
869 (goto-char (min (1+ pos) (point-max))) 898 (goto-char (min (1+ pos) (point-max)))
870 (if (= (char-syntax (if (eobp) (preceding-char) (char-after (point)))) 899 (if (= (char-syntax (if (eobp) (preceding-char) (char-after (point))))
871 ?\ ) 900 ?\ )
872 (id-select-set-region pos (1+ pos)) 901 (id-select-set-region pos (1+ pos))
873 (goto-char pos) 902 (goto-char pos)
874 (id-select-set-region 903 (condition-case ()
875 (save-excursion (backward-sexp) (point)) 904 (id-select-set-region
876 (progn (forward-sexp) (point))))))) 905 (save-excursion (backward-sexp) (point))
906 (progn (forward-sexp) (point)))
907 (error nil))))))
877 908
878 (defun id-select-comment (pos) 909 (defun id-select-comment (pos)
879 "Return rest of line from POS to newline." 910 "Return rest of line from POS to newline."
880 (setq id-select-previous 'comment) 911 (setq id-select-previous 'comment)
881 (save-excursion 912 (save-excursion