├── images └── usage.gif ├── Default (Linux).sublime-keymap ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── Super Calculator.sublime-settings ├── LICENSE ├── README.md ├── Main.sublime-menu └── Super Calculator.py /images/usage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pephers/Super-Calculator/HEAD/images/usage.gif -------------------------------------------------------------------------------- /Default (Linux).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "keys": ["alt+c"], "command": "super_calculator" 4 | } 5 | ] 6 | -------------------------------------------------------------------------------- /Default (OSX).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "keys": ["alt+c"], "command": "super_calculator" 4 | } 5 | ] 6 | -------------------------------------------------------------------------------- /Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "keys": ["alt+c"], "command": "super_calculator" 4 | } 5 | ] 6 | -------------------------------------------------------------------------------- /Super Calculator.sublime-settings: -------------------------------------------------------------------------------- 1 | { 2 | // Round results to the specified decimals 3 | "round_decimals": 2 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Chiel Robben 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 7 | of the Software, and to permit persons to whom the Software is furnished to do 8 | so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Super Calculator 2 | ================ 3 | * Author: Chiel Robben 4 | * License: MIT 5 | * Website: http://pephers.org 6 | 7 | About 8 | ----- 9 | Super Calculator is a plugin for Sublime Text 2 and Sublime Text 3 which lets you do inline calculations with a few keypresses. This is useful for example to subtract paddings from your widths in your CSS code. 10 | 11 | Usage 12 | ----- 13 | All you need to do is to press `Alt+C` and Super Calculator will select the mathematical expression closest to the cursor position so you can review what is going to be calculated. A second time `Alt+C` calculates the result and inserts it right away. Nice! 14 | 15 | ![](/images/usage.gif) 16 | 17 | Configuration 18 | ------------- 19 | By default, Super Calculator rounds the result down to two decimals. However, you can specify the number of decimals you would like in the user settings file for Super Calculator. The settings files can be found by navigating to `Preferences -> Package Settings -> Super Calculator`. 20 | 21 | Installation 22 | ------------ 23 | **With Package Control:** The easiest way to install Super Calculator is by using the [Package Control plugin](http://wbond.net/sublime_packages/package_control). 24 | 25 | **Without Git:** Download the latest source from [GitHub](https://github.com/Pephers/Super-Calculator) and copy the Super Calculator folder to your Sublime Text "Packages" directory. 26 | 27 | **With Git:** Clone the repository in your Sublime Text "Packages" directory: 28 | 29 | git clone https://github.com/Pephers/Super-Calculator.git 30 | 31 | The "Packages" directory can be found in the following locations: 32 | 33 | * OS X: 34 | 35 | ~/Library/Application Support/Sublime Text 2/Packages/ 36 | ~/Library/Application Support/Sublime Text 3/Packages/ 37 | 38 | * Linux: 39 | 40 | ~/.config/sublime-text-2/Packages/ 41 | ~/.config/sublime-text-3/Packages/ 42 | 43 | * Windows: 44 | 45 | %APPDATA%/Sublime Text 2/Packages/ 46 | %APPDATA%/Sublime Text 3/Packages/ 47 | 48 | -------------------------------------------------------------------------------- /Main.sublime-menu: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "preferences", 4 | "children": 5 | [ 6 | { 7 | "caption": "Package Settings", 8 | "id": "package-settings", 9 | "children": 10 | [ 11 | { 12 | "caption": "Super Calculator", 13 | "children": 14 | [ 15 | { 16 | "caption": "Settings - Default", 17 | "command": "open_file", 18 | "args": 19 | { 20 | "file": "${packages}/Super Calculator/Super Calculator.sublime-settings" 21 | } 22 | }, 23 | { 24 | "caption": "Settings - User", 25 | "command": "open_file", 26 | "args": 27 | { 28 | "file": "${packages}/User/Super Calculator.sublime-settings" 29 | } 30 | }, 31 | { 32 | "caption": "-" 33 | }, 34 | { 35 | "command": "open_file", 36 | "args": { 37 | "file": "${packages}/Super Calculator/Default (OSX).sublime-keymap", 38 | "platform": "OSX" 39 | }, 40 | "caption": "Key Bindings – Default" 41 | }, 42 | { 43 | "command": "open_file", 44 | "args": { 45 | "file": "${packages}/Super Calculator/Default (Linux).sublime-keymap", 46 | "platform": "Linux" 47 | }, 48 | "caption": "Key Bindings – Default" 49 | }, 50 | { 51 | "command": "open_file", 52 | "args": { 53 | "file": "${packages}/Super Calculator/Default (Windows).sublime-keymap", 54 | "platform": "Windows" 55 | }, 56 | "caption": "Key Bindings – Default" 57 | }, 58 | { 59 | "command": "open_file", 60 | "args": { 61 | "file": "${packages}/User/Default (OSX).sublime-keymap", 62 | "platform": "OSX" 63 | }, 64 | "caption": "Key Bindings – User" 65 | }, 66 | { 67 | "command": "open_file", 68 | "args": { 69 | "file": "${packages}/User/Default (Linux).sublime-keymap", 70 | "platform": "Linux" 71 | }, 72 | "caption": "Key Bindings – User" 73 | }, 74 | { 75 | "command": "open_file", 76 | "args": { 77 | "file": "${packages}/User/Default (Windows).sublime-keymap", 78 | "platform": "Windows" 79 | }, 80 | "caption": "Key Bindings – User" 81 | } 82 | ] 83 | } 84 | ] 85 | } 86 | ] 87 | } 88 | ] 89 | -------------------------------------------------------------------------------- /Super Calculator.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import sys 4 | import re 5 | import math 6 | import random 7 | from decimal import * 8 | 9 | import sublime 10 | import sublime_plugin 11 | 12 | 13 | class SuperCalculatorCommand(sublime_plugin.TextCommand): 14 | 15 | def __init__(self, view): 16 | self.view = view 17 | self.settings = sublime.load_settings("Super Calculator.sublime-settings") 18 | self.callables = {} 19 | self.constants = {} 20 | for lib in (random, math): 21 | for key in dir(lib): 22 | attr = getattr(lib, key) 23 | if key[0] != '_': 24 | if callable(attr): 25 | self.callables[key] = attr 26 | else: 27 | self.constants[key] = attr 28 | self.constants[key.upper()] = attr 29 | 30 | def average(nums): 31 | return sum(nums) / len(nums) 32 | 33 | self.callables['avg'] = average 34 | self.callables['average'] = average 35 | 36 | class Constant(object): 37 | def __init__(self, func): 38 | self._func = func 39 | 40 | def __call__(self, *args, **kwargs): 41 | return self._func(*args, **kwargs) 42 | 43 | def __repr__(self): 44 | return self._func() 45 | 46 | def password(length=10): 47 | pwdchrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' 48 | return ''.join(random.choice(pwdchrs) for _ in range(length)) 49 | 50 | password = Constant(password) 51 | self.callables['pwd'] = password 52 | self.callables['password'] = password 53 | self.constants['pwd'] = password 54 | self.constants['password'] = password 55 | self.constants['PWD'] = password 56 | self.constants['PASSWORD'] = password 57 | 58 | allowed = '|'.join( 59 | [r'[-+*/%%()]'] + 60 | [r'\b[-+]?(\d*\.)?\d+\b'] + 61 | [r'\b%s\b' % c for c in self.constants.keys()] + 62 | [r'\b%s\s*\(' % c for c in self.callables.keys()] 63 | ) 64 | self.regex = r'(%s)((%s|[ ])*(%s))?' % (allowed, allowed, allowed) 65 | self.dict = self.callables.copy() 66 | self.dict.update(self.constants) 67 | 68 | def run(self, edit): 69 | result_regions = [] 70 | exprs = [] 71 | for region in reversed(self.view.sel()): 72 | # Do the replacement in reverse order, so the character offsets 73 | # don't get invalidated 74 | exprs.append((region, self.view.substr(region))) 75 | for region, expr in exprs: 76 | if expr: 77 | # calculate expression and replace it with the result 78 | try: 79 | result = str(eval(expr, self.dict, {})) 80 | except Exception as e: 81 | sublime.status_message("Error: %s" % e) 82 | continue 83 | else: 84 | # round result if decimals are found 85 | if '.' in result: 86 | result = round(Decimal(result), self.settings.get("round_decimals")) 87 | result = str(result) 88 | if self.settings.get("trim_zeros") and '.' in result: 89 | result = result.strip('0').rstrip('.') 90 | if result == '': 91 | result = '0' 92 | if result != expr: 93 | self.view.replace(edit, region, result) 94 | sublime.status_message("Calculated result: " + expr + "=" + result) 95 | continue 96 | line_region = self.view.line(region) 97 | match_region = self.find_reverse(self.regex, region) 98 | if match_region: 99 | match = self.view.substr(match_region) 100 | # validate result and check if it is in the current line 101 | if re.match(self.regex, match) and line_region.begin() <= match_region.begin(): 102 | result_regions.append(match_region) 103 | sublime.status_message("Calculate: " + match + "?") 104 | if result_regions: 105 | self.view.sel().clear() 106 | for region in result_regions: 107 | self.view.sel().add(region) 108 | 109 | def find_reverse(self, string, region): 110 | new_regions = (r for r in reversed(self.view.find_all(string)) 111 | if r.begin() < region.end()) 112 | try: 113 | if sys.version_info < (3,0,0) : 114 | new_region = new_regions.next() 115 | else : 116 | new_region = next(new_regions) 117 | except StopIteration: 118 | return None 119 | else: 120 | return new_region 121 | --------------------------------------------------------------------------------