aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/vc
diff options
context:
space:
mode:
authorJuri Linkov2023-10-16 20:14:18 +0300
committerJuri Linkov2023-10-16 20:14:18 +0300
commit5827d179fb71e6fdcc63a17eb50305545ede2f37 (patch)
treeda71d5d675471b1b8e75cae35a3921cf29616082 /lisp/vc
parent484fc70a7acc5a958bfeefa4b83255680c7da175 (diff)
downloademacs-5827d179fb71e6fdcc63a17eb50305545ede2f37.tar.gz
emacs-5827d179fb71e6fdcc63a17eb50305545ede2f37.zip
Refactor 'vc-default-mode-line-string' (bug#66464)
* lisp/vc/vc-hooks.el (vc-mode-line-state): New function with code moved from 'vc-default-mode-line-string'. (vc-default-mode-line-string): Use 'vc-mode-line-state'. * lisp/vc/vc-git.el (vc-git-mode-line-string): Use 'vc-mode-line-state' instead of hacking the string returned from 'vc-default-mode-line-string'. * lisp/vc/vc-hg.el (vc-hg-mode-line-string): Use 'vc-mode-line-state' instead of duplicating code from 'vc-default-mode-line-string'.
Diffstat (limited to 'lisp/vc')
-rw-r--r--lisp/vc/vc-git.el21
-rw-r--r--lisp/vc/vc-hg.el57
-rw-r--r--lisp/vc/vc-hooks.el98
3 files changed, 81 insertions, 95 deletions
diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 5c21a5b884e..9ec45c59893 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -416,15 +416,18 @@ in the order given by `git status'."
416 416
417(defun vc-git-mode-line-string (file) 417(defun vc-git-mode-line-string (file)
418 "Return a string for `vc-mode-line' to put in the mode line for FILE." 418 "Return a string for `vc-mode-line' to put in the mode line for FILE."
419 (let* ((rev (vc-working-revision file 'Git)) 419 (pcase-let* ((backend-name "Git")
420 (disp-rev (or (vc-git--symbolic-ref file) 420 (state (vc-state file))
421 (and rev (substring rev 0 7)))) 421 (`(,state-echo ,face ,indicator)
422 (def-ml (vc-default-mode-line-string 'Git file)) 422 (vc-mode-line-state state))
423 (help-echo (get-text-property 0 'help-echo def-ml)) 423 (rev (vc-working-revision file 'Git))
424 (face (get-text-property 0 'face def-ml))) 424 (disp-rev (or (vc-git--symbolic-ref file)
425 (propertize (concat (substring def-ml 0 4) disp-rev) 425 (and rev (substring rev 0 7))))
426 'face face 426 (state-string (concat backend-name indicator disp-rev)))
427 'help-echo (concat help-echo "\nCurrent revision: " rev)))) 427 (propertize state-string 'face face 'help-echo
428 (concat state-echo " under the " backend-name
429 " version control system"
430 "\nCurrent revision: " rev))))
428 431
429(cl-defstruct (vc-git-extra-fileinfo 432(cl-defstruct (vc-git-extra-fileinfo
430 (:copier nil) 433 (:copier nil)
diff --git a/lisp/vc/vc-hg.el b/lisp/vc/vc-hg.el
index f2ee9ef35e4..89b2814a0a3 100644
--- a/lisp/vc/vc-hg.el
+++ b/lisp/vc/vc-hg.el
@@ -352,47 +352,22 @@ specific file to query."
352 352
353(defun vc-hg-mode-line-string (file) 353(defun vc-hg-mode-line-string (file)
354 "Hg-specific version of `vc-mode-line-string'." 354 "Hg-specific version of `vc-mode-line-string'."
355 (let* ((backend-name "Hg") 355 (pcase-let* ((backend-name "Hg")
356 (truename (file-truename file)) 356 (truename (file-truename file))
357 (state (vc-state truename)) 357 (state (vc-state truename))
358 (state-echo nil) 358 (`(,state-echo ,face ,indicator)
359 (face nil) 359 (vc-mode-line-state state))
360 (rev (and state 360 (rev (and state
361 (let ((default-directory 361 (let ((default-directory
362 (expand-file-name (vc-hg-root truename)))) 362 (expand-file-name (vc-hg-root truename))))
363 (vc-hg--symbolic-revision 363 (vc-hg--symbolic-revision
364 "." 364 "."
365 (and vc-hg-use-file-version-for-mode-line-version 365 (and vc-hg-use-file-version-for-mode-line-version
366 truename))))) 366 truename)))))
367 (rev (or rev "???"))) 367 (rev (or rev "???"))
368 (propertize 368 (state-string (concat backend-name indicator rev)))
369 (cond ((or (eq state 'up-to-date) 369 (propertize state-string 'face face 'help-echo
370 (eq state 'needs-update)) 370 (concat state-echo " under the " backend-name
371 (setq state-echo "Up to date file")
372 (setq face 'vc-up-to-date-state)
373 (concat backend-name "-" rev))
374 ((eq state 'added)
375 (setq state-echo "Locally added file")
376 (setq face 'vc-locally-added-state)
377 (concat backend-name "@" rev))
378 ((eq state 'conflict)
379 (setq state-echo "File contains conflicts after the last merge")
380 (setq face 'vc-conflict-state)
381 (concat backend-name "!" rev))
382 ((eq state 'removed)
383 (setq state-echo "File removed from the VC system")
384 (setq face 'vc-removed-state)
385 (concat backend-name "!" rev))
386 ((eq state 'missing)
387 (setq state-echo "File tracked by the VC system, but missing from the file system")
388 (setq face 'vc-missing-state)
389 (concat backend-name "?" rev))
390 (t
391 (setq state-echo "Locally modified file")
392 (setq face 'vc-edited-state)
393 (concat backend-name ":" rev)))
394 'face face
395 'help-echo (concat state-echo " under the " backend-name
396 " version control system")))) 371 " version control system"))))
397 372
398;;; History functions 373;;; History functions
diff --git a/lisp/vc/vc-hooks.el b/lisp/vc/vc-hooks.el
index a4de0a6e791..c16fb63b2ff 100644
--- a/lisp/vc/vc-hooks.el
+++ b/lisp/vc/vc-hooks.el
@@ -705,6 +705,50 @@ If BACKEND is passed use it as the VC backend when computing the result."
705 (force-mode-line-update) 705 (force-mode-line-update)
706 backend) 706 backend)
707 707
708(defun vc-mode-line-state (state)
709 "Return a list of data to display on the mode line.
710The argument STATE should contain the version control state returned
711from `vc-state'. The returned list includes three elements: the echo
712string, the face name, and the indicator that usually is one character."
713 (let (state-echo face indicator)
714 (cond ((or (eq state 'up-to-date)
715 (eq state 'needs-update))
716 (setq state-echo "Up to date file")
717 (setq face 'vc-up-to-date-state)
718 (setq indicator "-"))
719 ((stringp state)
720 (setq state-echo (concat "File locked by" state))
721 (setq face 'vc-locked-state)
722 (setq indicator (concat ":" state ":")))
723 ((eq state 'added)
724 (setq state-echo "Locally added file")
725 (setq face 'vc-locally-added-state)
726 (setq indicator "@"))
727 ((eq state 'conflict)
728 (setq state-echo "File contains conflicts after the last merge")
729 (setq face 'vc-conflict-state)
730 (setq indicator "!"))
731 ((eq state 'removed)
732 (setq state-echo "File removed from the VC system")
733 (setq face 'vc-removed-state)
734 (setq indicator "!"))
735 ((eq state 'missing)
736 (setq state-echo "File tracked by the VC system, but missing from the file system")
737 (setq face 'vc-missing-state)
738 (setq indicator "?"))
739 ((eq state 'ignored)
740 (setq state-echo "File tracked by the VC system, but ignored")
741 (setq face 'vc-ignored-state)
742 (setq indicator "!"))
743 (t
744 ;; Not just for the 'edited state, but also a fallback
745 ;; for all other states. Think about different symbols
746 ;; for 'needs-update and 'needs-merge.
747 (setq state-echo "Locally modified file")
748 (setq face 'vc-edited-state)
749 (setq indicator ":")))
750 (list state-echo face indicator)))
751
708(defun vc-default-mode-line-string (backend file) 752(defun vc-default-mode-line-string (backend file)
709 "Return a string for `vc-mode-line' to put in the mode line for FILE. 753 "Return a string for `vc-mode-line' to put in the mode line for FILE.
710Format: 754Format:
@@ -717,51 +761,15 @@ Format:
717 \"BACKEND?REV\" if the file is under VC, but is missing 761 \"BACKEND?REV\" if the file is under VC, but is missing
718 762
719This function assumes that the file is registered." 763This function assumes that the file is registered."
720 (let* ((backend-name (symbol-name backend)) 764 (pcase-let* ((backend-name (symbol-name backend))
721 (state (vc-state file backend)) 765 (state (vc-state file backend))
722 (state-echo nil) 766 (rev (vc-working-revision file backend))
723 (face nil) 767 (`(,state-echo ,face ,indicator)
724 (rev (vc-working-revision file backend))) 768 (vc-mode-line-state state))
725 (propertize 769 (state-string (concat backend-name indicator rev)))
726 (cond ((or (eq state 'up-to-date) 770 (propertize state-string 'face face 'help-echo
727 (eq state 'needs-update)) 771 (concat state-echo " under the " backend-name
728 (setq state-echo "Up to date file") 772 " version control system"))))
729 (setq face 'vc-up-to-date-state)
730 (concat backend-name "-" rev))
731 ((stringp state)
732 (setq state-echo (concat "File locked by" state))
733 (setq face 'vc-locked-state)
734 (concat backend-name ":" state ":" rev))
735 ((eq state 'added)
736 (setq state-echo "Locally added file")
737 (setq face 'vc-locally-added-state)
738 (concat backend-name "@" rev))
739 ((eq state 'conflict)
740 (setq state-echo "File contains conflicts after the last merge")
741 (setq face 'vc-conflict-state)
742 (concat backend-name "!" rev))
743 ((eq state 'removed)
744 (setq state-echo "File removed from the VC system")
745 (setq face 'vc-removed-state)
746 (concat backend-name "!" rev))
747 ((eq state 'missing)
748 (setq state-echo "File tracked by the VC system, but missing from the file system")
749 (setq face 'vc-missing-state)
750 (concat backend-name "?" rev))
751 ((eq state 'ignored)
752 (setq state-echo "File tracked by the VC system, but ignored")
753 (setq face 'vc-ignored-state)
754 (concat backend-name "!" rev))
755 (t
756 ;; Not just for the 'edited state, but also a fallback
757 ;; for all other states. Think about different symbols
758 ;; for 'needs-update and 'needs-merge.
759 (setq state-echo "Locally modified file")
760 (setq face 'vc-edited-state)
761 (concat backend-name ":" rev)))
762 'face face
763 'help-echo (concat state-echo " under the " backend-name
764 " version control system"))))
765 773
766(defun vc-follow-link () 774(defun vc-follow-link ()
767 "If current buffer visits a symbolic link, visit the real file. 775 "If current buffer visits a symbolic link, visit the real file.