| 1 | # Copyright 2010 Wincent Colaiuta. All rights reserved. |
| 2 | # |
| 3 | # Redistribution and use in source and binary forms, with or without |
| 4 | # modification, are permitted provided that the following conditions are met: |
| 5 | # |
| 6 | # 1. Redistributions of source code must retain the above copyright notice, |
| 7 | # this list of conditions and the following disclaimer. |
| 8 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 9 | # this list of conditions and the following disclaimer in the documentation |
| 10 | # and/or other materials provided with the distribution. |
| 11 | # |
| 12 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 13 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 14 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 15 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 16 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 17 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 18 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 19 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 20 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 21 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 22 | # POSSIBILITY OF SUCH DAMAGE. |
| 23 | |
| 24 | module CommandT |
| 25 | # Abuse the status line as a prompt. |
| 26 | class Prompt |
| 27 | attr_accessor :abbrev |
| 28 | |
| 29 | def initialize |
| 30 | @abbrev = '' # abbreviation entered so far |
| 31 | @col = 0 # cursor position |
| 32 | @has_focus = false |
| 33 | end |
| 34 | |
| 35 | # Erase whatever is displayed in the prompt line, |
| 36 | # effectively disposing of the prompt |
| 37 | def dispose |
| 38 | ::VIM::command 'echo' |
| 39 | ::VIM::command 'redraw' |
| 40 | end |
| 41 | |
| 42 | # Clear any entered text. |
| 43 | def clear! |
| 44 | @abbrev = '' |
| 45 | @col = 0 |
| 46 | redraw |
| 47 | end |
| 48 | |
| 49 | # Insert a character at (before) the current cursor position. |
| 50 | def add! char |
| 51 | left, cursor, right = abbrev_segments |
| 52 | @abbrev = left + char + cursor + right |
| 53 | @col += 1 |
| 54 | redraw |
| 55 | end |
| 56 | |
| 57 | # Delete a character to the left of the current cursor position. |
| 58 | def backspace! |
| 59 | if @col > 0 |
| 60 | left, cursor, right = abbrev_segments |
| 61 | @abbrev = left.chop! + cursor + right |
| 62 | @col -= 1 |
| 63 | redraw |
| 64 | end |
| 65 | end |
| 66 | |
| 67 | # Delete a character at the current cursor position. |
| 68 | def delete! |
| 69 | if @col < @abbrev.length |
| 70 | left, cursor, right = abbrev_segments |
| 71 | @abbrev = left + right |
| 72 | redraw |
| 73 | end |
| 74 | end |
| 75 | |
| 76 | def cursor_left |
| 77 | if @col > 0 |
| 78 | @col -= 1 |
| 79 | redraw |
| 80 | end |
| 81 | end |
| 82 | |
| 83 | def cursor_right |
| 84 | if @col < @abbrev.length |
| 85 | @col += 1 |
| 86 | redraw |
| 87 | end |
| 88 | end |
| 89 | |
| 90 | def cursor_end |
| 91 | if @col < @abbrev.length |
| 92 | @col = @abbrev.length |
| 93 | redraw |
| 94 | end |
| 95 | end |
| 96 | |
| 97 | def cursor_start |
| 98 | if @col != 0 |
| 99 | @col = 0 |
| 100 | redraw |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | def redraw |
| 105 | if @has_focus |
| 106 | prompt_highlight = 'Comment' |
| 107 | normal_highlight = 'None' |
| 108 | cursor_highlight = 'Underlined' |
| 109 | else |
| 110 | prompt_highlight = 'NonText' |
| 111 | normal_highlight = 'NonText' |
| 112 | cursor_highlight = 'NonText' |
| 113 | end |
| 114 | left, cursor, right = abbrev_segments |
| 115 | components = [prompt_highlight, '>>', 'None', ' '] |
| 116 | components += [normal_highlight, left] unless left.empty? |
| 117 | components += [cursor_highlight, cursor] unless cursor.empty? |
| 118 | components += [normal_highlight, right] unless right.empty? |
| 119 | components += [cursor_highlight, ' '] if cursor.empty? |
| 120 | set_status *components |
| 121 | end |
| 122 | |
| 123 | def focus |
| 124 | unless @has_focus |
| 125 | @has_focus = true |
| 126 | redraw |
| 127 | end |
| 128 | end |
| 129 | |
| 130 | def unfocus |
| 131 | if @has_focus |
| 132 | @has_focus = false |
| 133 | redraw |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | private |
| 138 | |
| 139 | # Returns the @abbrev string divided up into three sections, any of |
| 140 | # which may actually be zero width, depending on the location of the |
| 141 | # cursor: |
| 142 | # - left segment (to left of cursor) |
| 143 | # - cursor segment (character at cursor) |
| 144 | # - right segment (to right of cursor) |
| 145 | def abbrev_segments |
| 146 | left = @abbrev[0, @col] |
| 147 | cursor = @abbrev[@col, 1] |
| 148 | right = @abbrev[(@col + 1)..-1] || '' |
| 149 | [left, cursor, right] |
| 150 | end |
| 151 | |
| 152 | def set_status *args |
| 153 | # see ':help :echo' for why forcing a redraw here helps |
| 154 | # prevent the status line from getting inadvertantly cleared |
| 155 | # after our echo commands |
| 156 | ::VIM::command 'redraw' |
| 157 | while (highlight = args.shift) and (text = args.shift) do |
| 158 | text = VIM::escape_for_single_quotes text |
| 159 | ::VIM::command "echohl #{highlight}" |
| 160 | ::VIM::command "echon '#{text}'" |
| 161 | end |
| 162 | ::VIM::command 'echohl None' |
| 163 | end |
| 164 | end # class Prompt |
| 165 | end # module CommandT |