Subversion Branch in Mode-Line

213 Views Asked by At

If a file lies in a directory backed by a Subversion repo ending with either trunk or branches/X how do I make the trunk or X show up in the mode-line as SVN-trunk or SVN-X similar to what is shown in Git-backed files typically as Git-master.

2

There are 2 best solutions below

13
On BEST ANSWER

Determining SVN status

I am not aware of any built-in way to do this, but you can write your own code for this purpose.

Based on vc-svn-repository-name and vc-backend you can write a function that returns a custom mode text for SVN, and falls back to the default for all other VC systems:

(defun lunaryorn-vc-mode-line ()
  (let ((backend (vc-backend (buffer-file-name))))
    (if (eq backend 'SVN)
        (let ((url (vc-svn-repository-hostname (buffer-file-name))))
          (cond
           ((string-match-p "/trunk/" url) "SVN-trunk")
           ((string-match "/branches/\\([^/]+\\)/" url)
            (concat "SVN-" (match-string 1 url)))
           (t vc-mode)))
      ;; Use default mode text for other backends
      vc-mode)))

If the current file is under SVN, we check its repo URL according to your convention and compile a corresponding mode text. Don't be confused by the name of vc-svn-repository-hostname, the function actually returns the complete repo URL of the given file.

For all other backends, or if the URL does not match any pattern, we fall back to the default VC status text as in vc-mode.

Adding to the mode line

You can then use this function in the mode line, by adding a corresponding mode line construct to mode-line-format:

(setq-default mode-line-format
              '(…
                (vc-mode (" " (:eval (lunaryorn-vc-mode-line))))
                …))

Since mode-line-format is buffer-local, we use setq-default to change its global value. Our new mode line construct checks whether vc-mode is active, that is, whether the file is under version control, and evaluates our function in this case, to obtain our custom status text. See Mode Line Data for more information about mode line constructs.

The ellipsis stand for the other content of the mode line, which you may also customize to your liking. I'd suggest, though, that you simply copy the default value of mode-line-format, and replace (vc-mode vc-mode) with our new construct. That will simply replace the old VC information with our custom status text, and leaves everything else as before.

2
On

You could define a function:

(defun vc-svn-mode-line-string (file)
  (format "SVN-%s" (vc-svn-current-branch-name file)))

Of course, you'd also have to define vc-svn-current-branch-name because that one doesn't exist yet. I guess it would have to do something like

(defun vc-svn-current-branch-name (file)
  (with-temp-buffer
    (setq default-directory (file-name-directory file))
    (vc-svn-command t t file "info")
    ...extract the branch name from the info in current buffer..
    ))