Follow-up to r29036: Now that the "mergeinfo" transaction file is no
[svn.git] / tools / dev / svn-dev.el
blobb8ed13bee8d1ac2bcfdeea3efe1937b54cd60071
1 ;;;; Emacs Lisp help for writing Subversion code. ;;;;
3 ;; Later on, there will be auto-detection of svn files, modeline
4 ;; status, and a whole library of routines to interface with the
5 ;; command-line client. For now, there's this, at Ben's request.
6 ;;
7 ;; All this stuff should get folded into Emacs VC mode, really.
9 (defun svn-revert ()
10 "Revert the current buffer and its file to its svn base revision."
11 (interactive)
12 (let ((obuf (current-buffer))
13 (fname (buffer-file-name))
14 (outbuf (get-buffer-create "*svn output*")))
15 (set-buffer outbuf)
16 (delete-region (point-min) (point-max))
17 (call-process "svn" nil outbuf nil "status" fname)
18 (goto-char (point-min))
19 (search-forward fname)
20 (beginning-of-line)
21 (if (looking-at "^?")
22 (error "\"%s\" is not a Subversion-controlled file" fname))
23 (call-process "svn" nil outbuf nil "revert" fname)
24 (set-buffer obuf)
25 ;; todo: make a backup~ file?
26 (save-excursion
27 (revert-buffer nil t)
28 (save-buffer))
29 (message "Reverted \"%s\"." fname)))
31 (defun svn-resolved ()
32 "Tell Subversion that conflicts in the current buffer and its file have
33 been resolved."
34 (interactive)
35 (let ((obuf (current-buffer))
36 (fname (buffer-file-name))
37 (outbuf (get-buffer-create "*svn output*")))
38 (set-buffer outbuf)
39 (delete-region (point-min) (point-max))
40 (call-process "svn" nil outbuf nil "status" fname)
41 (goto-char (point-min))
42 (search-forward fname)
43 (beginning-of-line)
44 (if (looking-at "^?")
45 (error "\"%s\" is not a Subversion-controlled file" fname))
46 (call-process "svn" nil outbuf nil "resolved" fname)
47 (set-buffer obuf)
48 ;; todo: make a backup~ file?
49 (save-excursion
50 (revert-buffer nil t)
51 (save-buffer))
52 (message "Marked \"%s\" as conflict-free." fname)))
54 (defconst svn-adm-area ".svn"
55 "The name of the Subversion administrative subdirectory.")
57 (defconst svn-adm-entries ".svn/entries"
58 "The path from cwd to the Subversion entries file.")
60 (defun svn-controlled-path-p (path)
61 "Return non-nil if PATH is under Subversion version control, else
62 return nil. If PATH does not exist, return nil.
64 In the future, this will return an Emacs Lisp reflection of PATH's
65 entry, either an explicit svn-entry-struct, or a list of the form
66 \(LAST-COMMIT-REV CURRENT-REV LAST-COMMITTER ...\), so we can display
67 svn information in the mode line. But that requires truly parsing the
68 entries file, instead of just detecting PATH among the entries."
69 (interactive "f") ; any use for interactive, other than testing?
70 (cond
71 ((not (file-exists-p path))
72 nil)
73 ((file-directory-p path)
74 (let ((adm-area (concat path "/" svn-adm-area)))
75 (if (file-directory-p adm-area)
77 nil)))
79 (let ((entries (concat (file-name-directory path) svn-adm-entries))
80 (basename (file-name-nondirectory path))
81 (found nil))
82 (save-excursion
83 (if (file-directory-p (concat (file-name-directory path) svn-adm-area))
84 (progn
85 (let ((find-file-hooks nil))
86 (set-buffer (find-file-noselect entries t)))
87 (goto-char (point-min))
88 (if (search-forward (format "name=\"%s\"" basename) nil t)
89 (setq found t)
90 (setq found nil))
91 (kill-buffer nil)))
92 found)))))
95 (defun svn-text-base-path (file)
96 "Return the path to the text base for FILE (a string).
97 If FILE is a directory or not under version control, return nil."
98 (cond
99 ((not (svn-controlled-path-p file)) nil)
100 ((file-directory-p file) nil)
102 (let* ((pdir (file-name-directory file))
103 (base (file-name-nondirectory file)))
104 (format "%s%s/text-base/%s.svn-base" (or pdir "") svn-adm-area base)))))
107 (defun svn-ediff (file)
108 "Ediff FILE against its text base."
109 (interactive "fsvn ediff: ")
110 (let ((tb (svn-text-base-path file)))
111 (if (not tb)
112 (error "No text base for %s" file)
113 (ediff-files file tb))))
116 (defun svn-find-file-hook ()
117 "Function for find-file-hooks.
118 Inhibit backup files unless `vc-make-backup-files' is non-nil."
119 (if (svn-controlled-path-p (buffer-file-name))
120 (progn
121 (if (string-match "XEMACS\\|XEmacs\\|xemacs" emacs-version)
122 (vc-load-vc-hooks)) ; for `vc-make-backup-files'
123 (unless vc-make-backup-files
124 (make-local-variable 'backup-inhibited)
125 (setq backup-inhibited t)))))
127 (add-hook 'find-file-hooks 'svn-find-file-hook)
131 ;; Helper for referring to issue numbers in a user-friendly way.
132 (defun svn-bug-url (n)
133 "Insert the url for Subversion issue number N. Interactively, prompt for N."
134 (interactive "nSubversion issue number: ")
135 (insert (format "http://subversion.tigris.org/issues/show_bug.cgi?id=%d" n)))
137 ;; Helper for referring to revisions in a browser-friendly way.
138 (defun svn-rev-url (rev &optional transform)
139 "Insert the url for Subversion revision REV, or if TRANSFORM is not
140 nil, then transform the revision at or around point into an HTML link.
142 Interactively, if at or inside a revision number, transform it into
143 full HTML link; otherwise, prompt for revision number and insert just
144 the resulting URL."
145 (interactive (let ((thing (thing-at-point 'word)))
146 (if (and thing (string-match "r[0-9]+" thing))
147 (list thing t)
148 (list (read-string "Subversion revision number: ") nil))))
149 (if (string-match "^r[0-9]+" rev)
150 (setq rev (substring rev 1)))
151 (if transform
152 (let* ((bounds (bounds-of-thing-at-point 'word))
153 (start (car bounds))
154 (end (cdr bounds)))
155 (delete-region start end)))
156 (insert (format "http://svn.collab.net/viewcvs/svn?rev=%s&view=rev" rev)))
159 ;;; Subversion C conventions
160 (if (eq major-mode 'c-mode)
161 (progn
162 (c-add-style "svn" '("gnu" (c-offsets-alist . ((inextern-lang . 0)))))
163 (c-set-style "svn")))
164 (setq indent-tabs-mode nil)
165 (setq angry-mob-with-torches-and-pitchforks t)
169 ;; Subversion Python conventions, plus some harmless helpers for
170 ;; people who don't have python mode set up by default.
171 (autoload 'python-mode "python-mode" nil t)
172 (or (assoc "\\.py$" auto-mode-alist)
173 (setq auto-mode-alist
174 (cons '("\\.py$" . python-mode) auto-mode-alist)))
176 (defun svn-python-mode-hook ()
177 "Set up the Subversion python conventions. The effect of this is
178 local to the current buffer, which is presumably visiting a file in
179 the Subversion project. Python setup in other buffers will not be
180 affected."
181 (when (string-match "/subversion/" (buffer-file-name))
182 (make-local-variable 'py-indent-offset)
183 (setq indent-tabs-mode nil)
184 (setq py-indent-offset 2)
185 (make-local-variable 'py-smart-indentation)
186 (setq py-smart-indentation nil)))
188 (add-hook 'python-mode-hook 'svn-python-mode-hook)
192 ;; Much of the APR documentation is embedded perldoc format. The
193 ;; perldoc program itself sucks, however. If you're the author of
194 ;; perldoc, I'm sorry, but what were you thinking? Don't you know
195 ;; that there are people in the world who don't work in vt100
196 ;; terminals? If I want to view a perldoc page in my Emacs shell
197 ;; buffer, I have to run the ridiculous command
199 ;; $ PAGER=cat perldoc -t target_file
201 ;; (Not that this was documented anywhere, I had to figure it out for
202 ;; myself by reading /usr/bin/perldoc).
204 ;; Non-paging behavior should be a standard command-line option. No
205 ;; program that can output text should *ever* insist on invoking the
206 ;; pager.
208 ;; Anyway, these Emacs commands will solve the problem for us.
210 ;; Acknowledgements:
211 ;; Much of this code is copied from man.el in the FSF Emacs 21.x
212 ;; sources.
214 (defcustom svn-perldoc-overstrike-face 'bold
215 "*Face to use when fontifying overstrike."
216 :type 'face
217 :group 'svn-dev)
219 (defcustom svn-perldoc-underline-face 'underline
220 "*Face to use when fontifying underlining."
221 :type 'face
222 :group 'svn-dev)
225 (defun svn-perldoc-softhyphen-to-minus ()
226 ;; \255 is some kind of dash in Latin-N. Versions of Debian man, at
227 ;; least, emit it even when not in a Latin-N locale.
228 (unless (eq t (compare-strings "latin-" 0 nil
229 current-language-environment 0 6 t))
230 (goto-char (point-min))
231 (let ((str "\255"))
232 (if enable-multibyte-characters
233 (setq str (string-as-multibyte str)))
234 (while (search-forward str nil t) (replace-match "-")))))
237 (defun svn-perldoc-fontify-buffer ()
238 "Convert overstriking and underlining to the correct fonts.
239 Same for the ANSI bold and normal escape sequences."
240 (interactive)
241 (message "Please wait, making up the page...")
242 (goto-char (point-min))
243 (while (search-forward "\e[1m" nil t)
244 (delete-backward-char 4)
245 (put-text-property (point)
246 (progn (if (search-forward "\e[0m" nil 'move)
247 (delete-backward-char 4))
248 (point))
249 'face svn-perldoc-overstrike-face))
250 (goto-char (point-min))
251 (while (search-forward "_\b" nil t)
252 (backward-delete-char 2)
253 (put-text-property (point) (1+ (point)) 'face svn-perldoc-underline-face))
254 (goto-char (point-min))
255 (while (search-forward "\b_" nil t)
256 (backward-delete-char 2)
257 (put-text-property (1- (point)) (point) 'face svn-perldoc-underline-face))
258 (goto-char (point-min))
259 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
260 (replace-match "\\1")
261 (put-text-property (1- (point)) (point) 'face svn-perldoc-overstrike-face))
262 (goto-char (point-min))
263 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t)
264 (replace-match "o")
265 (put-text-property (1- (point)) (point) 'face 'bold))
266 (goto-char (point-min))
267 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t)
268 (replace-match "+")
269 (put-text-property (1- (point)) (point) 'face 'bold))
270 (svn-perldoc-softhyphen-to-minus)
271 (message "Please wait, making up the page...done"))
274 (defun svn-perldoc-cleanup-buffer ()
275 "Remove overstriking and underlining from the current buffer."
276 (interactive)
277 (message "Please wait, cleaning up the page...")
278 (progn
279 (goto-char (point-min))
280 (while (search-forward "_\b" nil t) (backward-delete-char 2))
281 (goto-char (point-min))
282 (while (search-forward "\b_" nil t) (backward-delete-char 2))
283 (goto-char (point-min))
284 (while (re-search-forward "\\(.\\)\\(\b\\1\\)+" nil t)
285 (replace-match "\\1"))
286 (goto-char (point-min))
287 (while (re-search-forward "\e\\[[0-9]+m" nil t) (replace-match ""))
288 (goto-char (point-min))
289 (while (re-search-forward "o\b\\+\\|\\+\bo" nil t) (replace-match "o"))
290 (goto-char (point-min))
291 (while (re-search-forward "\b" nil t) (replace-match " ")))
292 (goto-char (point-min))
293 (while (re-search-forward "[-|]\\(\b[-|]\\)+" nil t) (replace-match "+"))
294 (svn-perldoc-softhyphen-to-minus)
295 (message "Please wait, cleaning up the page...done"))
298 ;; Entry point to svn-perldoc functionality.
299 (defun svn-perldoc (file)
300 "Run perldoc on FILE, display the output in a buffer."
301 (interactive "fRun perldoc on file: ")
302 (let ((outbuf (get-buffer-create
303 (format "*%s PerlDoc*" (file-name-nondirectory file))))
304 (savepg (getenv "PAGER")))
305 (setenv "PAGER" "cat") ;; for perldoc
306 (save-excursion
307 (set-buffer outbuf)
308 (delete-region (point-min) (point-max))
309 (call-process "perldoc" nil outbuf nil (expand-file-name file))
310 (svn-perldoc-fontify-buffer)
311 (svn-perldoc-cleanup-buffer)
312 ;; Clean out the inevitable leading dead space.
313 (goto-char (point-min))
314 (re-search-forward "[^ \i\n]")
315 (beginning-of-line)
316 (delete-region (point-min) (point)))
317 (setenv "PAGER" savepg)
318 (display-buffer outbuf)))
322 ;;; Help developers write log messages.
324 ;; How to use this: just run `svn-log-message'. You might want to
325 ;; bind it to a key, for example,
327 ;; (define-key "\C-cl" 'svn-log-message)
329 ;; The log message will accumulate in a file. Later, you can use
330 ;; that file when you commit:
332 ;; $ svn ci -F msg ...
334 (defun svn-log-path-derive (path)
335 "Derive a relative directory path for absolute PATH, for a log entry."
336 (save-match-data
337 (let ((base (file-name-nondirectory path))
338 (chop-spot (string-match
339 "\\(code/\\)\\|\\(src/\\)\\|\\(projects/\\)"
340 path)))
341 (if chop-spot
342 (progn
343 (setq path (substring path (match-end 0)))
344 ;; Kluge for Subversion developers.
345 (if (string-match "subversion/" path)
346 (substring path (+ (match-beginning 0) 11))
347 path))
348 (string-match (expand-file-name "~/") path)
349 (substring path (match-end 0))))))
352 (defun svn-log-message-file ()
353 "Return the name of the appropriate log message accumulation file.
354 Usually this is just the file `msg' in the current directory, but
355 certain areas are treated specially, for example, the Subversion
356 source tree."
357 (save-match-data
358 (if (string-match "subversion" default-directory)
359 (concat (substring default-directory 0 (match-end 0)) "/msg")
360 "msg")))
363 (defun svn-log-message (short-file-names)
364 "Add to an in-progress log message, based on context around point.
365 If prefix arg SHORT-FILE-NAMES is non-nil, then use basenames only in
366 log messages, otherwise use full paths. The current defun name is
367 always used.
369 If the log message already contains material about this defun, then put
370 point there, so adding to that material is easy.
372 Else if the log message already contains material about this file, put
373 point there, and push onto the kill ring the defun name with log
374 message dressing around it, plus the raw defun name, so yank and
375 yank-next are both useful.
377 Else if there is no material about this defun nor file anywhere in the
378 log message, then put point at the end of the message and insert a new
379 entry for file with defun.
381 See also the function `svn-log-message-file'."
382 (interactive "P")
383 (let ((this-file (if short-file-names
384 (file-name-nondirectory buffer-file-name)
385 (svn-log-path-derive buffer-file-name)))
386 (this-defun (or (add-log-current-defun)
387 (save-excursion
388 (save-match-data
389 (if (eq major-mode 'c-mode)
390 (progn
391 (if (fboundp 'c-beginning-of-statement-1)
392 (c-beginning-of-statement-1)
393 (c-beginning-of-statement))
394 (search-forward "(" nil t)
395 (forward-char -1)
396 (forward-sexp -1)
397 (buffer-substring
398 (point)
399 (progn (forward-sexp 1) (point)))))))))
400 (log-file (svn-log-message-file)))
401 (find-file log-file)
402 (goto-char (point-min))
403 ;; Strip text properties from strings
404 (set-text-properties 0 (length this-file) nil this-file)
405 (set-text-properties 0 (length this-defun) nil this-defun)
406 ;; If log message for defun already in progress, add to it
407 (if (and
408 this-defun ;; we have a defun to work with
409 (search-forward this-defun nil t) ;; it's in the log msg already
410 (save-excursion ;; and it's about the same file
411 (save-match-data
412 (if (re-search-backward ; Ick, I want a real filename regexp!
413 "^\\*\\s-+\\([a-zA-Z0-9-_.@=+^$/%!?(){}<>]+\\)" nil t)
414 (string-equal (match-string 1) this-file)
415 t))))
416 (if (re-search-forward ":" nil t)
417 (if (looking-at " ") (forward-char 1)))
418 ;; Else no log message for this defun in progress...
419 (goto-char (point-min))
420 ;; But if log message for file already in progress, add to it.
421 (if (search-forward this-file nil t)
422 (progn
423 (if this-defun (progn
424 (kill-new (format "(%s): " this-defun))
425 (kill-new this-defun)))
426 (search-forward ")" nil t)
427 (if (looking-at " ") (forward-char 1)))
428 ;; Found neither defun nor its file, so create new entry.
429 (goto-char (point-max))
430 (if (not (bolp)) (insert "\n"))
431 (insert (format "\n* %s (%s): " this-file (or this-defun "")))
432 ;; Finally, if no derived defun, put point where the user can
433 ;; type it themselves.
434 (if (not this-defun) (forward-char -3))))))
438 ;;; Log message helpers.
440 (defconst svn-log-msg-sep-line
441 "------------------------------------------------------------------------"
442 "The line of dashes that separates log messages in 'svn log' output.")
444 (defconst svn-log-msg-boundary-regexp
445 (concat "^" svn-log-msg-sep-line "\n" "r[0-9]+ | ")
446 "Regular expression matching the start of a log msg. The start is
447 the beginning of the separator line, not the rev/author/date line that
448 follows the separator line.")
450 (defun svn-narrow-to-log-msg ()
451 "Narrow to the current Subversion log message.
452 This meant to be used while browsing the output of 'svn log'.
453 If point is not in such output, error."
454 (interactive)
455 (let ((start nil) (end nil))
456 (save-excursion
457 (re-search-backward svn-log-msg-boundary-regexp)
458 (forward-line 1)
459 (setq start (point))
460 (end-of-line)
461 (re-search-backward "| \\([0-9]+\\) ")
462 (let ((num (match-string 1)))
463 (re-search-forward "^\n")
464 (forward-line (string-to-number num)))
465 (setq end (point)))
466 (narrow-to-region start end)))
470 (message "loaded svn-dev.el")