Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / progmodes / etags.el
index e18491a..3e01779 100644 (file)
@@ -1,8 +1,6 @@
 ;;; etags.el --- etags facility for Emacs
 
-;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1998,
-;;               2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
-;;     Free Software Foundation, Inc.
+;; Copyright (C) 1985-1986, 1988-1989, 1992-1996, 1998, 2000-2011  Free Software Foundation, Inc.
 
 ;; Author: Roland McGrath <roland@gnu.org>
 ;; Maintainer: FSF
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;;; Code:
 
+(eval-when-compile
+  (require 'cl))
 (require 'ring)
 (require 'button)
 
@@ -39,7 +37,8 @@ To switch to a new tags table, setting this variable is sufficient.
 If you set this variable, do not also set `tags-table-list'.
 Use the `etags' program to make a tags table file.")
 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
-;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
+;;;###autoload (put 'tags-file-name 'variable-interactive (purecopy "fVisit tags table: "))
+;;;###autoload (put 'tags-file-name 'safe-local-variable 'stringp)
 
 (defgroup etags nil "Tags tables."
   :group 'tools)
@@ -67,12 +66,14 @@ Use the `etags' program to make a tags table file."
   :type '(repeat file))
 
 ;;;###autoload
-(defcustom tags-compression-info-list '("" ".Z" ".bz2" ".gz" ".tgz")
+(defcustom tags-compression-info-list
+  (purecopy '("" ".Z" ".bz2" ".gz" ".xz" ".tgz"))
   "*List of extensions tried by etags when jka-compr is used.
 An empty string means search the non-compressed file.
 These extensions will be tried only if jka-compr was activated
 \(i.e. via customize of `auto-compression-mode' or by calling the function
 `auto-compression-mode')."
+  :version "24.1"                      ; added xz
   :type  '(repeat string)
   :group 'etags)
 
@@ -277,7 +278,7 @@ buffer-local and set them to nil."
 (defun tags-table-mode ()
   "Major mode for tags table file buffers."
   (interactive)
-  (setq major-mode 'tags-table-mode
+  (setq major-mode 'tags-table-mode     ;FIXME: Use define-derived-mode.
         mode-name "Tags Table"
         buffer-undo-list t)
   (initialize-new-tags-table))
@@ -301,8 +302,7 @@ file the tag was in."
                     current-prefix-arg))
   (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
   ;; Bind tags-file-name so we can control below whether the local or
-  ;; global value gets set.  Calling visit-tags-table-buffer will
-  ;; initialize a buffer for the file and set tags-file-name to the
+  ;; global value gets set.
   ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
   ;; initialize a buffer for FILE and set tags-file-name to the
   ;; fully-expanded name.
@@ -338,8 +338,7 @@ file the tag was in."
                     ;; it is initialized as a tag table buffer.
                     (save-excursion
                       (tags-verify-table (buffer-file-name table-buffer))))
-               (save-excursion
-                 (set-buffer table-buffer)
+               (with-current-buffer table-buffer
                  (if (tags-included-tables)
                      ;; Insert the included tables into the list we
                      ;; are processing.
@@ -378,8 +377,7 @@ such table and puts its included tables into the list."
              (setq computed (cons (car tables) computed)
                    table-buffer (get-file-buffer (car tables)))
              (if table-buffer
-                 (save-excursion
-                   (set-buffer table-buffer)
+                 (with-current-buffer table-buffer
                    (if (tags-included-tables)
                        ;; Insert the included tables into the list we
                        ;; are processing.
@@ -426,9 +424,9 @@ Returns non-nil if it is a valid table."
   (if (get-file-buffer file)
       ;; The file is already in a buffer.  Check for the visited file
       ;; having changed since we last used it.
-      (let (win)
+      (progn
        (set-buffer (get-file-buffer file))
-       (setq win (or verify-tags-table-function (tags-table-mode)))
+        (or verify-tags-table-function (tags-table-mode))
        (if (or (verify-visited-file-modtime (current-buffer))
                ;; Decide whether to revert the file.
                ;; revert-without-query can say to revert
@@ -448,18 +446,19 @@ Returns non-nil if it is a valid table."
                 (funcall verify-tags-table-function))
          (revert-buffer t t)
          (tags-table-mode)))
-    (and (file-exists-p file)
-        (progn
-          (set-buffer (find-file-noselect file))
-          (or (string= file buffer-file-name)
-              ;; find-file-noselect has changed the file name.
-              ;; Propagate the change to tags-file-name and tags-table-list.
-              (let ((tail (member file tags-table-list)))
-                (if tail
-                    (setcar tail buffer-file-name))
-                (if (eq file tags-file-name)
-                    (setq tags-file-name buffer-file-name))))
-          (tags-table-mode)))))
+    (when (file-exists-p file)
+      (let* ((buf (find-file-noselect file))
+             (newfile (buffer-file-name buf)))
+        (unless (string= file newfile)
+          ;; find-file-noselect has changed the file name.
+          ;; Propagate the change to tags-file-name and tags-table-list.
+          (let ((tail (member file tags-table-list)))
+            (if tail (setcar tail newfile)))
+          (if (eq file tags-file-name) (setq tags-file-name newfile)))
+        ;; Only change buffer now that we're done using potentially
+        ;; buffer-local variables.
+        (set-buffer buf)
+        (tags-table-mode)))))
 
 ;; Subroutine of visit-tags-table-buffer.  Search the current tags tables
 ;; for one that has tags for THIS-FILE (or that includes a table that
@@ -473,7 +472,7 @@ Subroutine of `visit-tags-table-buffer'.
 Looks for a tags table that has such tags or that includes a table
 that has them.  Returns the name of the first such table.
 Non-nil CORE-ONLY means check only tags tables that are already in
-buffers.  Nil CORE-ONLY is ignored."
+buffers.  If CORE-ONLY is nil, it is ignored."
   (let ((tables tags-table-computed-list)
        (found nil))
     ;; Loop over the list, looking for a table containing tags for THIS-FILE.
@@ -775,22 +774,44 @@ tags table and its (recursively) included tags tables."
                  (setq combined-table current-table))))
            (message "Making tags completion table for %s...done"
                     buffer-file-name)
-           ;; Cache the result a buffer-local variable.
+           ;; Cache the result in a buffer-local variable.
            (setq tags-completion-table combined-table))
        (quit (message "Tags completion table construction aborted.")
              (setq tags-completion-table nil)))))
 
-(defun tags-complete-tag (string predicate what)
-  "Completion function for tags.
-Does normal `try-completion', but builds `tags-completion-table' on
-demand."
-  (save-excursion
-    ;; If we need to ask for the tag table, allow that.
-    (let ((enable-recursive-minibuffers t))
-      (visit-tags-table-buffer))
-    (if (eq what t)
-       (all-completions string (tags-completion-table) predicate)
-      (try-completion string (tags-completion-table) predicate))))
+(defun tags-lazy-completion-table ()
+  (lexical-let ((buf (current-buffer)))
+    (lambda (string pred action)
+      (with-current-buffer buf
+        (save-excursion
+          ;; If we need to ask for the tag table, allow that.
+          (let ((enable-recursive-minibuffers t))
+            (visit-tags-table-buffer))
+          (complete-with-action action (tags-completion-table) string pred))))))
+
+;;;###autoload (defun tags-completion-at-point-function ()
+;;;###autoload   (if (or tags-table-list tags-file-name)
+;;;###autoload       (progn
+;;;###autoload         (load "etags")
+;;;###autoload         (tags-completion-at-point-function))))
+
+(defun tags-completion-at-point-function ()
+  "Using tags, return a completion table for the text around point.
+If no tags table is loaded, do nothing and return nil."
+  (when (or tags-table-list tags-file-name)
+    (let ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
+                                     tags-case-fold-search
+                                   case-fold-search))
+         (pattern (funcall (or find-tag-default-function
+                               (get major-mode 'find-tag-default-function)
+                               'find-tag-default)))
+         beg)
+      (when pattern
+       (save-excursion
+         (search-backward pattern) ;FIXME: will fail if we're inside pattern.
+         (setq beg (point))
+         (forward-char (length pattern))
+         (list beg (point) (tags-lazy-completion-table)))))))
 \f
 (defun find-tag-tag (string)
   "Read a tag name, with defaulting and completion."
@@ -805,7 +826,7 @@ demand."
                                            (substring string 0 (string-match "[ :]+\\'" string))
                                            default)
                                  string)
-                               'tags-complete-tag
+                               (tags-lazy-completion-table)
                                nil nil nil nil default)))
     (if (equal spec "")
        (or default (error "There is no default tag"))
@@ -829,9 +850,8 @@ The functions using this are `find-tag-noselect',
 (defvar find-tag-history nil) ; Doc string?
 
 ;; Dynamic bondage:
-(eval-when-compile
-  (defvar etags-case-fold-search)
-  (defvar etags-syntax-table))
+(defvar etags-case-fold-search)
+(defvar etags-syntax-table)
 
 ;;;###autoload
 (defun find-tag-noselect (tagname &optional next-p regexp-p)
@@ -885,22 +905,21 @@ See documentation of variable `tags-file-name'."
        (setq last-tag tagname))
       ;; Record the location so we can pop back to it later.
       (let ((marker (make-marker)))
-       (save-excursion
-         (set-buffer
-          ;; find-tag-in-order does the real work.
-          (find-tag-in-order
-           (if (and next-p last-tag) last-tag tagname)
-           (if regexp-p
-               find-tag-regexp-search-function
-             find-tag-search-function)
-           (if regexp-p
-               find-tag-regexp-tag-order
-             find-tag-tag-order)
-           (if regexp-p
-               find-tag-regexp-next-line-after-failure-p
-             find-tag-next-line-after-failure-p)
-           (if regexp-p "matching" "containing")
-           (or (not next-p) (not last-tag))))
+       (with-current-buffer
+            ;; find-tag-in-order does the real work.
+            (find-tag-in-order
+             (if (and next-p last-tag) last-tag tagname)
+             (if regexp-p
+                 find-tag-regexp-search-function
+               find-tag-search-function)
+             (if regexp-p
+                 find-tag-regexp-tag-order
+               find-tag-tag-order)
+             (if regexp-p
+                 find-tag-regexp-next-line-after-failure-p
+               find-tag-next-line-after-failure-p)
+             (if regexp-p "matching" "containing")
+             (or (not next-p) (not last-tag)))
          (set-marker marker (point))
          (run-hooks 'local-find-tag-hook)
          (ring-insert tags-location-ring marker)
@@ -1112,9 +1131,7 @@ error message."
              ;; Naive match found.  Qualify the match.
              (and (funcall (car order) pattern)
                   ;; Make sure it is not a previous qualified match.
-                  (not (member (set-marker match-marker (save-excursion
-                                                          (beginning-of-line)
-                                                          (point)))
+                  (not (member (set-marker match-marker (point-at-bol))
                                tag-lines-already-matched))
                   (throw 'qualified-match-found nil))
              (if next-line-after-failure-p
@@ -1292,13 +1309,11 @@ buffer-local values of tags table format variables."
 
       ;; Find the end of the tag and record the whole tag text.
       (search-forward "\177")
-      (setq tag-text (buffer-substring (1- (point))
-                                      (save-excursion (beginning-of-line)
-                                                      (point))))
+      (setq tag-text (buffer-substring (1- (point)) (point-at-bol)))
       ;; If use-explicit is non nil and explicit tag is present, use it as part of
       ;; return value. Else just skip it.
       (setq explicit-start (point))
-      (when (and (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
+      (when (and (search-forward "\001" (point-at-bol 2) t)
                 use-explicit)
        (setq tag-text (buffer-substring explicit-start (1- (point)))))
 
@@ -1337,7 +1352,8 @@ hits the start of file."
        offset found pat)
     (if (eq (car tag-info) t)
        ;; Direct file tag.
-       (cond (line (goto-line line))
+       (cond (line (progn (goto-char (point-min))
+                          (forward-line (1- line))))
              (startpos (goto-char startpos))
              (t (error "etags.el BUG: bogus direct file tag")))
       ;; This constant is 1/2 the initial search window.
@@ -1355,7 +1371,8 @@ hits the start of file."
       ;; If no char pos was given, try the given line number.
       (or startpos
          (if line
-             (setq startpos (progn (goto-line line)
+             (setq startpos (progn (goto-char (point-min))
+                                   (forward-line (1- line))
                                    (point)))))
       (or startpos
          (setq startpos (point-min)))
@@ -1406,7 +1423,8 @@ hits the start of file."
                                      (tag-find-file-of-tag (button-get button 'file-path))
                                      (widen)
                                      (funcall goto-func tag-info)))
-                         'face 'tags-tag-face
+                         'follow-link t
+                         'face tags-tag-face
                          'type 'button))
       (terpri)
       (forward-line 1))
@@ -1440,6 +1458,7 @@ hits the start of file."
                                                                    (button-get button 'item)))
                                          'item sn
                                          'face tags-tag-face
+                                         'follow-link t
                                          'type 'button)
                              (terpri))))))
         (when (symbolp symbs)
@@ -1495,7 +1514,8 @@ hits the start of file."
                                              (tag-find-file-of-tag (button-get button 'file-path))
                                              (widen)
                                              (funcall goto-func tag-info)))
-                                 'face 'tags-tag-face
+                                 'follow-link t
+                                 'face tags-tag-face
                                  'type 'button)))
          (princ (format "- %s" file-label))
          (with-current-buffer standard-output
@@ -1506,9 +1526,9 @@ hits the start of file."
                                        ;; Get the local value in the tags table
                                        ;; buffer before switching buffers.
                                        (goto-char (point-min)))
-                             'face 'tags-tag-face
-                             'type 'button))
-         ))
+                             'follow-link t
+                             'face tags-tag-face
+                             'type 'button))))
       (terpri)
       (forward-line 1))
     (message nil))
@@ -1562,18 +1582,10 @@ that do nothing."
 
 ;; This might be a neat idea, but it's too hairy at the moment.
 ;;(defmacro tags-with-syntax (&rest body)
-;;   `(let ((current (current-buffer))
-;;        (otable (syntax-table))
-;;        (buffer (find-file-noselect (file-of-tag)))
-;;        table)
-;;       (unwind-protect
-;;        (progn
-;;          (set-buffer buffer)
-;;          (setq table (syntax-table))
-;;          (set-buffer current)
-;;          (set-syntax-table table)
-;;            ,@body)
-;;       (set-syntax-table otable))))
+;;   `(with-syntax-table
+;;        (with-current-buffer (find-file-noselect (file-of-tag))
+;;          (syntax-table))
+;;      ,@body))
 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
 
 ;; exact file name match, i.e. searched tag must match complete file
@@ -1663,7 +1675,7 @@ Point should be just after a string that matches TAG."
   (save-excursion
     (beginning-of-line)
     (let ((bol (point)))
-      (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
+      (and (search-forward "\177" (line-end-position) t)
           (re-search-backward re bol t)))))
 \f
 (defcustom tags-loop-revert-buffers nil
@@ -1879,7 +1891,8 @@ See also the documentation of the variable `tags-file-name'."
                              ;; to the beginning of it so perform-replace
                              ;; will see it.
                              (goto-char (match-beginning 0))))
-       tags-loop-operate `(perform-replace ',from ',to t t ',delimited))
+       tags-loop-operate `(perform-replace ',from ',to t t ',delimited
+                                           nil multi-query-replace-map))
   (tags-loop-continue (or file-list-form t)))
 \f
 (defun tags-complete-tags-table-file (string predicate what) ; Doc string?
@@ -1944,6 +1957,7 @@ directory specification."
 
 (define-button-type 'tags-select-tags-table
   'action 'select-tags-table-select
+  'follow-link t
   'help-echo "RET, t or mouse-2: select tags table")
 
 ;; XXX If a file is in multiple tables, selection may get the wrong one.
@@ -2014,10 +2028,8 @@ see the doc of that variable if you want to add names to the list."
     (define-key map "q" 'select-tags-table-quit)
     map))
 
-(define-derived-mode select-tags-table-mode fundamental-mode "Select Tags Table"
-  "Major mode for choosing a current tags table among those already loaded.
-
-\\{select-tags-table-mode-map}"
+(define-derived-mode select-tags-table-mode special-mode "Select Tags Table"
+  "Major mode for choosing a current tags table among those already loaded."
   (setq buffer-read-only t))
 
 (defun select-tags-table-select (button)
@@ -2034,7 +2046,6 @@ see the doc of that variable if you want to add names to the list."
   (interactive)
   (quit-window t (selected-window)))
 \f
-;; Note, there is another definition of this function in bindings.el.
 ;;;###autoload
 (defun complete-tag ()
   "Perform tags completion on the text around point.
@@ -2047,34 +2058,10 @@ for \\[find-tag] (which see)."
       (error "%s"
             (substitute-command-keys
              "No tags table loaded; try \\[visit-tags-table]")))
-  (let ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
-                                   tags-case-fold-search
-                                 case-fold-search))
-       (pattern (funcall (or find-tag-default-function
-                             (get major-mode 'find-tag-default-function)
-                             'find-tag-default)))
-       beg
-       completion)
-    (or pattern
-       (error "Nothing to complete"))
-    (search-backward pattern)
-    (setq beg (point))
-    (forward-char (length pattern))
-    (setq completion (tags-complete-tag pattern nil nil))
-    (cond ((eq completion t))
-         ((null completion)
-          (message "Can't find completion for \"%s\"" pattern)
-          (ding))
-         ((not (string= pattern completion))
-          (delete-region beg (point))
-          (insert completion))
-         (t
-          (message "Making completion list...")
-          (with-output-to-temp-buffer "*Completions*"
-            (display-completion-list
-             (all-completions pattern 'tags-complete-tag nil)
-             pattern))
-          (message "Making completion list...%s" "done")))))
+  (let ((comp-data (tags-completion-at-point-function)))
+    (if (null comp-data)
+       (error "Nothing to complete")
+      (apply 'completion-in-region comp-data))))
 
 (dolist (x '("^No tags table in use; use .* to select one$"
             "^There is no default tag$"
@@ -2091,5 +2078,4 @@ for \\[find-tag] (which see)."
 \f
 (provide 'etags)
 
-;; arch-tag: b897c2b5-08f3-4837-b2d3-0e7d6db1b63e
 ;;; etags.el ends here