Some corrections in Elisp manual
[emacs.git] / lisp / version.el
blob77188a51ee3b03e5f5895b85a2a89fe406805937
1 ;;; version.el --- record version number of Emacs
3 ;; Copyright (C) 1985, 1992, 1994-1995, 1999-2016 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
29 (defconst emacs-major-version
30 (progn (string-match "^[0-9]+" emacs-version)
31 (string-to-number (match-string 0 emacs-version)))
32 "Major version number of this version of Emacs.
33 This variable first existed in version 19.23.")
35 (defconst emacs-minor-version
36 (progn (string-match "^[0-9]+\\.\\([0-9]+\\)" emacs-version)
37 (string-to-number (match-string 1 emacs-version)))
38 "Minor version number of this version of Emacs.
39 This variable first existed in version 19.23.")
41 (defconst emacs-build-time (current-time)
42 "Time at which Emacs was dumped out.")
44 ;; I think this should be obsoleted/removed. It's just one more meaningless
45 ;; difference between different builds. It's usually not even an fqdn.
46 (defconst emacs-build-system (system-name)
47 "Name of the system on which Emacs was built.")
49 (defvar motif-version-string)
50 (defvar gtk-version-string)
51 (defvar ns-version-string)
52 (defvar cairo-version-string)
54 (defun emacs-version (&optional here)
55 "Return string describing the version of Emacs that is running.
56 If optional argument HERE is non-nil, insert string at point.
57 Don't use this function in programs to choose actions according
58 to the system configuration; look at `system-configuration' instead."
59 (interactive "P")
60 (let ((version-string
61 (format (if (not (called-interactively-p 'interactive))
62 "GNU Emacs %s (%s%s%s%s)\n of %s"
63 "GNU Emacs %s (%s%s%s%s) of %s")
64 emacs-version
65 system-configuration
66 (cond ((featurep 'motif)
67 (concat ", " (substring motif-version-string 4)))
68 ((featurep 'gtk)
69 (concat ", GTK+ Version " gtk-version-string))
70 ((featurep 'x-toolkit) ", X toolkit")
71 ((featurep 'ns)
72 (format ", NS %s" ns-version-string))
73 (t ""))
74 (if (featurep 'cairo)
75 (format ", cairo version %s" cairo-version-string)
76 "")
77 (if (and (boundp 'x-toolkit-scroll-bars)
78 (memq x-toolkit-scroll-bars '(xaw xaw3d)))
79 (format ", %s scroll bars"
80 (capitalize (symbol-name x-toolkit-scroll-bars)))
81 "")
82 (format-time-string "%Y-%m-%d" emacs-build-time))))
83 (if here
84 (insert version-string)
85 (if (called-interactively-p 'interactive)
86 (message "%s" version-string)
87 version-string))))
89 ;; We hope that this alias is easier for people to find.
90 (defalias 'version 'emacs-version)
92 ;; Set during dumping, this is a defvar so that it can be setq'd.
93 (defvar emacs-repository-version nil
94 "String giving the repository revision from which this Emacs was built.
95 Value is nil if Emacs was not built from a repository checkout,
96 or if we could not determine the revision.")
98 (define-obsolete-variable-alias 'emacs-bzr-version
99 'emacs-repository-version "24.4")
101 (define-obsolete-function-alias 'emacs-bzr-get-version
102 'emacs-repository-get-version "24.4")
104 (defun emacs-repository-version-git (dir)
105 "Ask git itself for the version information for directory DIR."
106 (message "Waiting for git...")
107 (with-temp-buffer
108 (let ((default-directory (file-name-as-directory dir)))
109 (and (eq 0
110 (with-demoted-errors "Error running git rev-parse: %S"
111 (call-process "git" nil '(t nil) nil "rev-parse" "HEAD")))
112 (progn (goto-char (point-min))
113 (looking-at "[0-9a-fA-F]\\{40\\}"))
114 (match-string 0)))))
116 (defun emacs-repository--version-git-1 (file dir)
117 "Internal subroutine of `emacs-repository-get-version'."
118 (when (file-readable-p file)
119 (with-temp-buffer
120 (insert-file-contents file)
121 (cond ((looking-at "[0-9a-fA-F]\\{40\\}")
122 (match-string 0))
123 ((looking-at "ref: \\(.*\\)")
124 (emacs-repository--version-git-1
125 (expand-file-name (match-string 1) dir)
126 dir))))))
128 (defun emacs-repository-get-version (&optional dir external)
129 "Try to return as a string the repository revision of the Emacs sources.
130 The format of the returned string is dependent on the VCS in use.
131 Value is nil if the sources do not seem to be under version
132 control, or if we could not determine the revision. Note that
133 this reports on the current state of the sources, which may not
134 correspond to the running Emacs.
136 Optional argument DIR is a directory to use instead of `source-directory'.
137 Optional argument EXTERNAL non-nil means to just ask the VCS itself,
138 if the sources appear to be under version control. Otherwise only ask
139 the VCS if we cannot find any information ourselves."
140 (or dir (setq dir source-directory))
141 (let* ((base-dir (expand-file-name ".git" dir))
142 (in-main-worktree (file-directory-p base-dir))
143 (in-linked-worktree nil)
144 sub-dir)
145 ;; If the sources are in a linked worktree, .git is a file that points to
146 ;; the location of the main worktree and the repo's administrative files.
147 (when (and (not in-main-worktree)
148 (file-regular-p base-dir)
149 (file-readable-p base-dir))
150 (with-temp-buffer
151 (insert-file-contents base-dir)
152 (when (looking-at "gitdir: \\(.*\.git\\)\\(.*\\)$")
153 (setq base-dir (match-string 1)
154 sub-dir (concat base-dir (match-string 2))
155 in-linked-worktree t))))
156 ;; We've found a worktree, either main or linked.
157 (when (or in-main-worktree in-linked-worktree)
158 (if external
159 (emacs-repository-version-git dir)
160 (or (if in-linked-worktree
161 (emacs-repository--version-git-1
162 (expand-file-name "HEAD" sub-dir) base-dir)
163 (let ((files '("HEAD" "refs/heads/master"))
164 file rev)
165 (while (and (not rev)
166 (setq file (car files)))
167 (setq file (expand-file-name file base-dir)
168 files (cdr files)
169 rev (emacs-repository--version-git-1 file base-dir)))
170 rev))
171 ;; AFAICS this doesn't work during dumping (bug#20799).
172 (emacs-repository-version-git dir))))))
174 ;; We put version info into the executable in the form that `ident' uses.
175 (purecopy (concat "\n$Id: " (subst-char-in-string ?\n ?\s (emacs-version))
176 " $\n"))
178 ;;; version.el ends here