prefs: add a Defaults class for holding hard-coded defaults
[git-cola.git] / cola / models / prefs.py
blob6d94d396b4d2058e497d683f738a3e75f020b63b
1 from __future__ import division, absolute_import, unicode_literals
3 import sys
5 from .. import core
6 from .. import observable
7 from .. import utils
10 BACKGROUND_EDITOR = 'cola.backgroundeditor'
11 BLAME_VIEWER = 'cola.blameviewer'
12 BOLD_HEADERS = 'cola.boldheaders'
13 CHECKCONFLICTS = 'cola.checkconflicts'
14 COMMENT_CHAR = 'core.commentchar'
15 DIFFCONTEXT = 'gui.diffcontext'
16 DIFFTOOL = 'diff.tool'
17 DISPLAY_UNTRACKED = 'gui.displayuntracked'
18 EDITOR = 'gui.editor'
19 FONTDIFF = 'cola.fontdiff'
20 HISTORY_BROWSER = 'gui.historybrowser'
21 LINEBREAK = 'cola.linebreak'
22 MAXRECENT = 'cola.maxrecent'
23 MERGE_DIFFSTAT = 'merge.diffstat'
24 MERGE_KEEPBACKUP = 'merge.keepbackup'
25 MERGE_SUMMARY = 'merge.summary'
26 MERGE_VERBOSITY = 'merge.verbosity'
27 MERGETOOL = 'merge.tool'
28 EXPANDTAB = 'cola.expandtab'
29 SAVEWINDOWSETTINGS = 'cola.savewindowsettings'
30 SORT_BOOKMARKS = 'cola.sortbookmarks'
31 TABWIDTH = 'cola.tabwidth'
32 TEXTWIDTH = 'cola.textwidth'
33 USER_EMAIL = 'user.email'
34 USER_NAME = 'user.name'
35 SAFE_MODE = 'cola.safemode'
36 SHOW_PATH = 'cola.showpath'
37 SPELL_CHECK = 'cola.spellcheck'
40 class Defaults(object):
41 """Read-only class for holding defaults that get overridden"""
42 # These should match Git's defaults for git-defined values.
43 blame_viewer = 'git gui blame'
44 bold_headers = False
45 check_conflicts = True
46 comment_char = '#'
47 display_untracked = True
48 diff_context = 5
49 difftool = 'xxdiff'
50 editor = 'gvim'
51 expandtab = False
52 history_browser = 'gitk'
53 linebreak = True
54 maxrecent = 8
55 mergetool = difftool
56 merge_diffstat = True
57 merge_keep_backup = True
58 merge_summary = True
59 merge_verbosity = 4
60 save_window_settings = True
61 safe_mode = False
62 show_path = True
63 sort_bookmarks = True
64 spellcheck = False
65 tabwidth = 8
66 textwidth = 72
69 def blame_viewer(context):
70 default = Defaults.blame_viewer
71 return context.cfg.get(BLAME_VIEWER, default=default)
74 def bold_headers(context):
75 return context.cfg.get(BOLD_HEADERS, default=Defaults.bold_headers)
78 def check_conflicts(context):
79 return context.cfg.get(CHECKCONFLICTS, default=Defaults.check_conflicts)
82 def display_untracked(context):
83 return context.cfg.get(DISPLAY_UNTRACKED,
84 default=Defaults.display_untracked)
87 def editor(context):
88 app = context.cfg.get(EDITOR, default=Defaults.editor)
89 return _remap_editor(app)
92 def background_editor(context):
93 app = context.cfg.get(BACKGROUND_EDITOR, default=editor(context))
94 return _remap_editor(app)
97 def _remap_editor(app):
98 return {'vim': 'gvim -f'}.get(app, app)
101 def comment_char(context):
102 return context.cfg.get(COMMENT_CHAR, default=Defaults.comment_char)
105 def default_history_browser():
106 if utils.is_win32():
107 # On Windows, a sensible default is "python git-cola dag"
108 # which is different than `gitk` below, but is preferred
109 # because we don't have to guess paths.
110 git_cola = sys.argv[0].replace('\\', '/')
111 python = sys.executable.replace('\\', '/')
112 cwd = core.getcwd().replace('\\', '/')
113 argv = [python, git_cola, 'dag', '--repo', cwd]
114 argv = core.prep_for_subprocess(argv)
115 default = core.list2cmdline(argv)
116 else:
117 # The `gitk` script can be launched as-is on unix
118 default = Defaults.history_browser
119 return default
122 def history_browser(context):
123 default = default_history_browser()
124 return context.cfg.get(HISTORY_BROWSER, default=default)
127 def linebreak(context):
128 return context.cfg.get(LINEBREAK, default=Defaults.linebreak)
131 def maxrecent(context):
132 value = Defaults.maxrecent
133 if context:
134 value = context.cfg.get(MAXRECENT, default=value)
135 return value
138 def spellcheck(context):
139 return context.cfg.get(SPELL_CHECK, default=Defaults.spellcheck)
142 def expandtab(context):
143 return context.cfg.get(EXPANDTAB, default=Defaults.expandtab)
146 def sort_bookmarks(context):
147 return context.cfg.get(SORT_BOOKMARKS, default=Defaults.sort_bookmarks)
150 def tabwidth(context):
151 return context.cfg.get(TABWIDTH, default=Defaults.tabwidth)
154 def textwidth(context):
155 return context.cfg.get(TEXTWIDTH, default=Defaults.textwidth)
158 class PreferencesModel(observable.Observable):
159 message_config_updated = 'config_updated'
161 def __init__(self, context):
162 observable.Observable.__init__(self)
163 self.context = context
164 self.config = context.cfg
166 def set_config(self, source, config, value):
167 if source == 'repo':
168 self.config.set_repo(config, value)
169 else:
170 self.config.set_user(config, value)
171 message = self.message_config_updated
172 self.notify_observers(message, source, config, value)
174 def get_config(self, source, config):
175 if source == 'repo':
176 value = self.config.get_repo(config)
177 else:
178 value = self.config.get(config)
179 return value
182 class SetConfig(object):
184 def __init__(self, model, source, config, value):
185 self.source = source
186 self.config = config
187 self.value = value
188 self.old_value = None
189 self.model = model
191 def is_undoable(self):
192 return True
194 def do(self):
195 self.old_value = self.model.get_config(self.source, self.config)
196 self.model.set_config(self.source, self.config, self.value)
198 def undo(self):
199 if self.old_value is None:
200 return
201 self.model.set_config(self.source, self.config, self.old_value)