Merge pull request #884 from cclauss/patch-2
[git-cola.git] / cola / models / prefs.py
bloba7283c9a56fcb4c446a74327cbafb09aeb3a6834
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 def default_blame_viewer():
41 return 'git gui blame'
44 def blame_viewer(context):
45 default = default_blame_viewer()
46 return context.cfg.get(BLAME_VIEWER, default=default)
49 def bold_headers(context):
50 return context.cfg.get(BOLD_HEADERS, default=False)
53 def check_conflicts(context):
54 return context.cfg.get(CHECKCONFLICTS, default=True)
57 def display_untracked(context):
58 return context.cfg.get(DISPLAY_UNTRACKED, default=True)
61 def editor(context):
62 app = context.cfg.get(EDITOR, default='gvim')
63 return _remap_editor(app)
66 def background_editor(context):
67 app = context.cfg.get(BACKGROUND_EDITOR, default=editor(context))
68 return _remap_editor(app)
71 def _remap_editor(app):
72 return {'vim': 'gvim -f'}.get(app, app)
75 def comment_char(context):
76 return context.cfg.get(COMMENT_CHAR, default='#')
79 def default_history_browser():
80 if utils.is_win32():
81 # On Windows, a sensible default is "python git-cola dag"
82 # which is different than `gitk` below, but is preferred
83 # because we don't have to guess paths.
84 git_cola = sys.argv[0].replace('\\', '/')
85 python = sys.executable.replace('\\', '/')
86 cwd = core.getcwd().replace('\\', '/')
87 argv = [python, git_cola, 'dag', '--repo', cwd]
88 argv = core.prep_for_subprocess(argv)
89 default = core.list2cmdline(argv)
90 else:
91 # The `gitk` script can be launched as-is on unix
92 default = 'gitk'
93 return default
96 def history_browser(context):
97 default = default_history_browser()
98 return context.cfg.get(HISTORY_BROWSER, default=default)
101 def linebreak(context):
102 return context.cfg.get(LINEBREAK, default=True)
105 def maxrecent(context):
106 value = 8
107 if context:
108 value = context.cfg.get(MAXRECENT, default=value)
109 return value
112 def spellcheck(context):
113 return context.cfg.get(SPELL_CHECK, default=False)
116 def expandtab(context):
117 return context.cfg.get(EXPANDTAB, default=False)
120 def sort_bookmarks(context):
121 return context.cfg.get(SORT_BOOKMARKS, default=True)
124 def tabwidth(context):
125 return context.cfg.get(TABWIDTH, default=8)
128 def textwidth(context):
129 return context.cfg.get(TEXTWIDTH, default=72)
132 class PreferencesModel(observable.Observable):
133 message_config_updated = 'config_updated'
135 def __init__(self, context):
136 observable.Observable.__init__(self)
137 self.context = context
138 self.config = context.cfg
140 def set_config(self, source, config, value):
141 if source == 'repo':
142 self.config.set_repo(config, value)
143 else:
144 self.config.set_user(config, value)
145 message = self.message_config_updated
146 self.notify_observers(message, source, config, value)
148 def get_config(self, source, config):
149 if source == 'repo':
150 value = self.config.get_repo(config)
151 else:
152 value = self.config.get(config)
153 return value
156 class SetConfig(object):
158 def __init__(self, model, source, config, value):
159 self.source = source
160 self.config = config
161 self.value = value
162 self.old_value = None
163 self.model = model
165 def is_undoable(self):
166 return True
168 def do(self):
169 self.old_value = self.model.get_config(self.source, self.config)
170 self.model.set_config(self.source, self.config, self.value)
172 def undo(self):
173 if self.old_value is None:
174 return
175 self.model.set_config(self.source, self.config, self.old_value)