diakonos 0.8.11 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +439 -0
  3. data/LICENCE.md +675 -0
  4. data/bin/diakonos +6 -0
  5. data/diakonos-256-colour.conf +220 -0
  6. data/diakonos.conf +1802 -0
  7. data/help/about-help.dhf +31 -0
  8. data/help/clipboard.dhf +45 -0
  9. data/help/close-file.dhf +6 -0
  10. data/help/code-block-navigation.dhf +16 -0
  11. data/help/column-markers.dhf +15 -0
  12. data/help/config.dhf +69 -0
  13. data/help/cursor-stack.dhf +19 -0
  14. data/help/delete.dhf +41 -0
  15. data/help/extensions.dhf +125 -0
  16. data/help/file-type.dhf +24 -0
  17. data/help/key-mapping.dhf +127 -0
  18. data/help/line-numbers.dhf +22 -0
  19. data/help/macros.dhf +27 -0
  20. data/help/new-file.dhf +6 -0
  21. data/help/open-file.dhf +21 -0
  22. data/help/quit.dhf +7 -0
  23. data/help/resizing.dhf +19 -0
  24. data/help/ruby.dhf +17 -0
  25. data/help/save-file.dhf +10 -0
  26. data/help/scripting.dhf +92 -0
  27. data/help/search.dhf +103 -0
  28. data/help/shell.dhf +60 -0
  29. data/help/speed.dhf +23 -0
  30. data/help/support.dhf +15 -0
  31. data/help/switch-buffers.dhf +15 -0
  32. data/help/tabs.dhf +36 -0
  33. data/help/undo.dhf +9 -0
  34. data/help/uninstall.dhf +18 -0
  35. data/help/welcome.dhf +32 -0
  36. data/help/word-wrap.dhf +17 -0
  37. data/lib/diakonos/about.rb +70 -0
  38. data/lib/diakonos/bookmark.rb +46 -0
  39. data/lib/diakonos/buffer/bookmarking.rb +47 -0
  40. data/lib/diakonos/buffer/cursor.rb +335 -0
  41. data/lib/diakonos/buffer/delete.rb +170 -0
  42. data/lib/diakonos/buffer/display.rb +362 -0
  43. data/lib/diakonos/buffer/file.rb +157 -0
  44. data/lib/diakonos/buffer/indentation.rb +175 -0
  45. data/lib/diakonos/buffer/searching.rb +552 -0
  46. data/lib/diakonos/buffer/selection.rb +360 -0
  47. data/lib/diakonos/buffer/undo.rb +73 -0
  48. data/lib/diakonos/buffer-hash.rb +60 -0
  49. data/lib/diakonos/buffer-management.rb +59 -0
  50. data/lib/diakonos/buffer.rb +698 -0
  51. data/lib/diakonos/clipboard-klipper-dbus.rb +62 -0
  52. data/lib/diakonos/clipboard-klipper.rb +62 -0
  53. data/lib/diakonos/clipboard-osx.rb +59 -0
  54. data/lib/diakonos/clipboard-xclip.rb +60 -0
  55. data/lib/diakonos/clipboard.rb +47 -0
  56. data/lib/diakonos/config-file.rb +67 -0
  57. data/lib/diakonos/config.rb +382 -0
  58. data/lib/diakonos/core-ext/enumerable.rb +15 -0
  59. data/lib/diakonos/core-ext/hash.rb +60 -0
  60. data/lib/diakonos/core-ext/object.rb +6 -0
  61. data/lib/diakonos/core-ext/regexp.rb +6 -0
  62. data/lib/diakonos/core-ext/string.rb +122 -0
  63. data/lib/diakonos/ctag.rb +28 -0
  64. data/lib/diakonos/cursor.rb +27 -0
  65. data/lib/diakonos/display/format.rb +75 -0
  66. data/lib/diakonos/display.rb +336 -0
  67. data/lib/diakonos/extension-set.rb +49 -0
  68. data/lib/diakonos/extension.rb +34 -0
  69. data/lib/diakonos/finding.rb +40 -0
  70. data/lib/diakonos/functions/basics.rb +34 -0
  71. data/lib/diakonos/functions/bookmarking.rb +61 -0
  72. data/lib/diakonos/functions/buffers.rb +491 -0
  73. data/lib/diakonos/functions/clipboard.rb +70 -0
  74. data/lib/diakonos/functions/cursor.rb +264 -0
  75. data/lib/diakonos/functions/grepping.rb +83 -0
  76. data/lib/diakonos/functions/indentation.rb +71 -0
  77. data/lib/diakonos/functions/readline.rb +93 -0
  78. data/lib/diakonos/functions/search.rb +179 -0
  79. data/lib/diakonos/functions/selection.rb +98 -0
  80. data/lib/diakonos/functions/sessions.rb +78 -0
  81. data/lib/diakonos/functions/shell.rb +250 -0
  82. data/lib/diakonos/functions/tags.rb +65 -0
  83. data/lib/diakonos/functions/text-manipulation.rb +196 -0
  84. data/lib/diakonos/functions-deprecated.rb +77 -0
  85. data/lib/diakonos/functions.rb +292 -0
  86. data/lib/diakonos/grep.rb +98 -0
  87. data/lib/diakonos/help.rb +47 -0
  88. data/lib/diakonos/hooks.rb +13 -0
  89. data/lib/diakonos/installation.rb +19 -0
  90. data/lib/diakonos/interaction-handler.rb +216 -0
  91. data/lib/diakonos/interaction.rb +52 -0
  92. data/lib/diakonos/key-map.rb +62 -0
  93. data/lib/diakonos/keying.rb +442 -0
  94. data/lib/diakonos/line-mover.rb +42 -0
  95. data/lib/diakonos/list.rb +59 -0
  96. data/lib/diakonos/logging.rb +27 -0
  97. data/lib/diakonos/mode.rb +17 -0
  98. data/lib/diakonos/mouse.rb +18 -0
  99. data/lib/diakonos/number-fitter.rb +11 -0
  100. data/lib/diakonos/range.rb +31 -0
  101. data/lib/diakonos/readline/functions.rb +82 -0
  102. data/lib/diakonos/readline.rb +223 -0
  103. data/lib/diakonos/search.rb +58 -0
  104. data/lib/diakonos/sessions.rb +257 -0
  105. data/lib/diakonos/sized-array.rb +48 -0
  106. data/lib/diakonos/text-mark.rb +19 -0
  107. data/lib/diakonos/vendor/fuzzy_file_finder.rb +386 -0
  108. data/lib/diakonos/version.rb +25 -0
  109. data/lib/diakonos/window.rb +43 -0
  110. data/lib/diakonos.rb +592 -0
  111. metadata +160 -68
@@ -0,0 +1,264 @@
1
+ module Diakonos
2
+
3
+ CLEAR_STACK_POINTER = true
4
+ DONT_CLEAR_STACK_POINTER = false
5
+ DIFFERENT_FILE = true
6
+ NOT_DIFFERENT_FILE = false
7
+
8
+ module Functions
9
+
10
+ # @return [true,false] true iff the cursor changed positions
11
+ def cursor_down
12
+ buffer_current.cursor_to(
13
+ buffer_current.last_row + 1,
14
+ buffer_current.last_col,
15
+ Buffer::DO_DISPLAY,
16
+ Buffer::STOPPED_TYPING,
17
+ DONT_ADJUST_ROW
18
+ )
19
+ end
20
+
21
+ # @return [true,false] true iff the cursor changed positions
22
+ def cursor_left( stopped_typing = Buffer::STOPPED_TYPING )
23
+ buffer_current.cursor_to(
24
+ buffer_current.last_row,
25
+ buffer_current.last_col - 1,
26
+ Buffer::DO_DISPLAY,
27
+ stopped_typing
28
+ )
29
+ end
30
+
31
+ # Pops the cursor stack.
32
+ # @param [Symbol] direction
33
+ # Either :backward (default) or :forward.
34
+ # @param [Boolean] different_file
35
+ # Whether to pop just one frame (default), or many frames until a different file is reached.
36
+ # @see Diakonos::DIFFERENT_FILE
37
+ # @see Diakonos::NOT_DIFFERENT_FILE
38
+ def cursor_return( direction = :backward, different_file = NOT_DIFFERENT_FILE )
39
+ delta = 0
40
+ if @cursor_stack_pointer.nil?
41
+ push_cursor_state(
42
+ buffer_current.top_line,
43
+ buffer_current.last_row,
44
+ buffer_current.last_col,
45
+ DONT_CLEAR_STACK_POINTER
46
+ )
47
+ delta = 1
48
+ end
49
+
50
+ orig_ptr = @cursor_stack_pointer
51
+ case direction
52
+ when :backward
53
+ @cursor_stack_pointer = ( @cursor_stack_pointer || @cursor_stack.length ) - 1 - delta
54
+ while different_file && @cursor_stack[ @cursor_stack_pointer ] && @cursor_stack[ @cursor_stack_pointer ][ :buffer ] == buffer_current
55
+ @cursor_stack_pointer -= 1
56
+ end
57
+ when :forward
58
+ @cursor_stack_pointer = ( @cursor_stack_pointer || 0 ) + 1
59
+ while different_file && @cursor_stack[ @cursor_stack_pointer ] && @cursor_stack[ @cursor_stack_pointer ][ :buffer ] == buffer_current
60
+ @cursor_stack_pointer += 1
61
+ end
62
+ end
63
+ if @cursor_stack[ @cursor_stack_pointer ].nil? && orig_ptr
64
+ @cursor_stack_pointer = orig_ptr
65
+ end
66
+
67
+ return_pointer = @cursor_stack_pointer
68
+
69
+ if @cursor_stack_pointer < 0
70
+ return_pointer = @cursor_stack_pointer = 0
71
+ elsif @cursor_stack_pointer >= @cursor_stack.length
72
+ return_pointer = @cursor_stack_pointer = @cursor_stack.length - 1
73
+ else
74
+ cursor_state = @cursor_stack[ @cursor_stack_pointer ]
75
+ if cursor_state
76
+ buffer = cursor_state[ :buffer ]
77
+ switch_to buffer
78
+ buffer.pitch_view( cursor_state[ :top_line ] - buffer.top_line, Buffer::DONT_PITCH_CURSOR, Buffer::DO_DISPLAY )
79
+ buffer.cursor_to( cursor_state[ :row ], cursor_state[ :col ] )
80
+ update_status_line
81
+ end
82
+ end
83
+
84
+ set_iline "Location: #{return_pointer+1}/#{@cursor_stack.size}"
85
+ end
86
+
87
+ # @return [true,false] true iff the cursor changed positions
88
+ def cursor_right( stopped_typing = Buffer::STOPPED_TYPING, amount = 1 )
89
+ buffer_current.cursor_to(
90
+ buffer_current.last_row,
91
+ buffer_current.last_col + amount,
92
+ Buffer::DO_DISPLAY,
93
+ stopped_typing
94
+ )
95
+ end
96
+
97
+ # @return [true,false] true iff the cursor changed positions
98
+ def cursor_up
99
+ buffer_current.cursor_to(
100
+ buffer_current.last_row - 1,
101
+ buffer_current.last_col,
102
+ Buffer::DO_DISPLAY,
103
+ Buffer::STOPPED_TYPING,
104
+ DONT_ADJUST_ROW
105
+ )
106
+ end
107
+
108
+ # Moves the cursor to the beginning of the current buffer.
109
+ # @return [true,false] true iff the cursor changed positions
110
+ def cursor_bof
111
+ buffer_current.cursor_to( 0, 0, Buffer::DO_DISPLAY )
112
+ end
113
+
114
+ # Moves the cursor to the beginning of the current line.
115
+ def cursor_bol
116
+ buffer_current.cursor_to_bol
117
+ end
118
+
119
+ # Moves the cursor to the end of the current line.
120
+ def cursor_eol
121
+ buffer_current.cursor_to_eol
122
+ end
123
+
124
+ # Moves the cursor to the end of the current buffer.
125
+ def cursor_eof
126
+ buffer_current.cursor_to_eof
127
+ end
128
+
129
+ # Moves the cursor to the top of the viewport of the current buffer.
130
+ def cursor_tov
131
+ buffer_current.cursor_to_tov
132
+ end
133
+
134
+ # Moves the cursor to the bottom of the viewport of the current buffer.
135
+ def cursor_bov
136
+ buffer_current.cursor_to_bov
137
+ end
138
+
139
+ # Moves the cursor to the beginning of the parent code block.
140
+ def go_block_outer
141
+ buffer_current.go_block_outer
142
+ end
143
+ # Moves the cursor to the beginning of the first child code block.
144
+ def go_block_inner
145
+ buffer_current.go_block_inner
146
+ end
147
+ # Moves the cursor to the beginning of the next code block at the same
148
+ # indentation level as the current one.
149
+ def go_block_next
150
+ buffer_current.go_block_next
151
+ end
152
+ # Moves the cursor to the beginning of the previous code block at the same
153
+ # indentation level as the current one.
154
+ def go_block_previous
155
+ buffer_current.go_block_previous
156
+ end
157
+
158
+ # Moves the cursor to the next occurrence of the given character.
159
+ # @param [String] char The character to go to
160
+ def go_to_char( after = nil, char = nil )
161
+ char ||= @interaction_handler.get_char( "Type character to go to..." )
162
+
163
+ if char
164
+ begin
165
+ moved = buffer_current.go_to_char( char, after == :after ? AFTER_CHAR : ON_CHAR )
166
+ if ! moved
167
+ set_iline "'#{char}' not found."
168
+ end
169
+ rescue TypeError
170
+ # User pressed Esc, or Ctrl-C, or similar.
171
+ # Quietly continue.
172
+ end
173
+ end
174
+ end
175
+
176
+ # Moves the cursor to the closest previous occurrence of the given character.
177
+ # @param [String] char The character to go to
178
+ def go_to_char_previous( after = nil, char = nil )
179
+ char ||= @interaction_handler.get_char( "Type character to go to..." )
180
+
181
+ if char
182
+ begin
183
+ moved = buffer_current.go_to_char_previous( char, after == :after ? AFTER_CHAR : ON_CHAR )
184
+ if ! moved
185
+ set_iline "'#{char}' not found."
186
+ end
187
+ rescue TypeError
188
+ # User pressed Esc, or Ctrl-C, or similar.
189
+ # Quietly continue.
190
+ end
191
+ end
192
+ end
193
+
194
+ # Prompts the user for a line number or line delta, with optional column
195
+ # number. Moves the cursor there.
196
+ def go_to_line_ask
197
+ input = get_user_input( "Go to [line number|+lines][,column number]: " )
198
+ if input
199
+ row = nil
200
+ col = 0
201
+
202
+ if input =~ /([+-]\d+)/
203
+ row = buffer_current.last_row + $1.to_i
204
+ col = buffer_current.last_col
205
+ else
206
+ input = input.split( /\D+/ ).collect { |n| n.to_i }
207
+ if input.size > 0
208
+ if input[ 0 ] == 0
209
+ row = nil
210
+ else
211
+ row = input[ 0 ] - 1
212
+ end
213
+ if input[ 1 ]
214
+ col = input[ 1 ] - 1
215
+ end
216
+ end
217
+ end
218
+
219
+ if row
220
+ buffer_current.go_to_line( row, col )
221
+ end
222
+ end
223
+ end
224
+
225
+ # Pitches the current buffer's view one screenful down.
226
+ def page_down
227
+ if buffer_current.pitch_view( main_window_height, Buffer::DO_PITCH_CURSOR ) == 0
228
+ buffer_current.cursor_to_eof
229
+ end
230
+ update_status_line
231
+ update_context_line
232
+ end
233
+
234
+ # Pitches the current buffer's view one screenful up.
235
+ def page_up
236
+ if buffer_current.pitch_view( -main_window_height, Buffer::DO_PITCH_CURSOR ) == 0
237
+ cursor_bof
238
+ end
239
+ update_status_line
240
+ update_context_line
241
+ end
242
+
243
+ # Scrolls the current buffer's view down, as determined by the
244
+ # view.scroll_amount setting.
245
+ def scroll_down
246
+ buffer_current.pitch_view( @settings[ "view.scroll_amount" ] || 1 )
247
+ update_status_line
248
+ update_context_line
249
+ end
250
+
251
+ # Scrolls the current buffer's view up, as determined by the
252
+ # view.scroll_amount setting.
253
+ def scroll_up
254
+ if @settings[ "view.scroll_amount" ]
255
+ buffer_current.pitch_view( -@settings[ "view.scroll_amount" ] )
256
+ else
257
+ buffer_current.pitch_view( -1 )
258
+ end
259
+ update_status_line
260
+ update_context_line
261
+ end
262
+
263
+ end
264
+ end
@@ -0,0 +1,83 @@
1
+ module Diakonos
2
+ module Functions
3
+
4
+ def grep( regexp_source = nil )
5
+ grep_( regexp_source, buffer_current )
6
+ end
7
+
8
+ def grep_buffers( regexp_source = nil )
9
+ grep_( regexp_source, *@buffers )
10
+ end
11
+
12
+ def grep_session_dir( regexp_source = nil )
13
+ grep_dir regexp_source, @session.dir
14
+ end
15
+
16
+ def grep_dir( regexp_source = nil, dir = nil )
17
+ if dir.nil?
18
+ dir = get_user_input(
19
+ "Grep directory: ",
20
+ history: @rlh_files,
21
+ initial_text: @session.dir,
22
+ do_complete: DONT_COMPLETE,
23
+ on_dirs: :accept_dirs
24
+ )
25
+ return if dir.nil?
26
+ end
27
+ dir = File.expand_path( dir )
28
+
29
+ original_buffer = buffer_current
30
+ if buffer_current.changing_selection
31
+ selected_text = buffer_current.copy_selection[ 0 ]
32
+ end
33
+ starting_row, starting_col = buffer_current.last_row, buffer_current.last_col
34
+
35
+ selected = get_user_input(
36
+ "Grep regexp: ",
37
+ history: @rlh_search,
38
+ initial_text: regexp_source || selected_text || ""
39
+ ) { |input|
40
+ next if input.length < 2
41
+ escaped_input = input.gsub( /'/ ) { "\\047" }
42
+ matching_files = `egrep '#{escaped_input}' -rniIl #{dir}`.split( /\n/ )
43
+
44
+ grep_results = matching_files.map { |f|
45
+ ::Diakonos.grep_array(
46
+ Regexp.new( input ),
47
+ File.read( f ).split( /\n/ ),
48
+ settings[ 'grep.context' ],
49
+ "#{File.basename( f )}:",
50
+ f
51
+ )
52
+ }.flatten
53
+ if settings[ 'grep.context' ] == 0
54
+ join_str = "\n"
55
+ else
56
+ join_str = "\n---\n"
57
+ end
58
+ with_list_file do |list|
59
+ list.puts grep_results.join( join_str )
60
+ end
61
+
62
+ list_buffer = open_list_buffer
63
+ regexp = nil
64
+ begin
65
+ list_buffer.highlight_matches Regexp.new( input )
66
+ rescue RegexpError => e
67
+ # ignore
68
+ end
69
+ display_buffer list_buffer
70
+ }
71
+
72
+ if selected
73
+ spl = selected.split( "| " )
74
+ if spl.size > 1
75
+ open_file spl[ -1 ]
76
+ end
77
+ else
78
+ original_buffer.cursor_to starting_row, starting_col
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,71 @@
1
+ module Diakonos
2
+ module Functions
3
+
4
+ def indent
5
+ if ! buffer_current.changing_selection
6
+ buffer_current.indent
7
+ else
8
+ @do_display = false
9
+ mark = buffer_current.selection_mark
10
+ if mark.end_col > 0
11
+ end_row = mark.end_row
12
+ else
13
+ end_row = mark.end_row - 1
14
+ end
15
+ (mark.start_row..end_row).each do |row|
16
+ buffer_current.indent row, Buffer::DONT_DISPLAY
17
+ end
18
+ @do_display = true
19
+ display_buffer buffer_current
20
+ end
21
+ end
22
+
23
+ def insert_spaces( num_spaces )
24
+ if num_spaces > 0
25
+ buffer_current.delete_selection
26
+ buffer_current.insert_string( " " * num_spaces )
27
+ cursor_right( Buffer::STILL_TYPING, num_spaces )
28
+ end
29
+ end
30
+
31
+ def insert_tab
32
+ type_character "\t"
33
+ end
34
+
35
+ def parsed_indent
36
+ if( buffer_current.changing_selection )
37
+ @do_display = false
38
+ mark = buffer_current.selection_mark
39
+ (mark.start_row..mark.end_row).each do |row|
40
+ buffer_current.parsed_indent row: row, do_display: false
41
+ end
42
+ @do_display = true
43
+ display_buffer buffer_current
44
+ else
45
+ buffer_current.parsed_indent
46
+ end
47
+ update_status_line
48
+ update_context_line
49
+ end
50
+
51
+ def unindent
52
+ if( buffer_current.changing_selection )
53
+ @do_display = false
54
+ mark = buffer_current.selection_mark
55
+ if mark.end_col > 0
56
+ end_row = mark.end_row
57
+ else
58
+ end_row = mark.end_row - 1
59
+ end
60
+ (mark.start_row..end_row).each do |row|
61
+ buffer_current.unindent row, Buffer::DONT_DISPLAY
62
+ end
63
+ @do_display = true
64
+ display_buffer buffer_current
65
+ else
66
+ buffer_current.unindent
67
+ end
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,93 @@
1
+ module Diakonos
2
+ module Functions
3
+
4
+ def readline_abort
5
+ @readline.abort
6
+ end
7
+
8
+ def readline_accept
9
+ @readline.accept current_list_item
10
+ end
11
+
12
+ def readline_backspace
13
+ @readline.backspace
14
+ end
15
+
16
+ def readline_complete_input
17
+ @readline.complete_input
18
+ end
19
+
20
+ def readline_cursor_left
21
+ @readline.cursor_left
22
+ end
23
+
24
+ def readline_cursor_right
25
+ @readline.cursor_right
26
+ end
27
+
28
+ def readline_cursor_bol
29
+ @readline.cursor_bol
30
+ end
31
+
32
+ def readline_cursor_eol
33
+ @readline.cursor_eol
34
+ end
35
+
36
+ def readline_cursor_up
37
+ if showing_list?
38
+ if list_item_selected?
39
+ previous_list_item
40
+ end
41
+ @readline.set_input select_list_item
42
+ else
43
+ @readline.history_up
44
+ end
45
+ @readline.cursor_write_input
46
+ end
47
+
48
+ def readline_cursor_down
49
+ if showing_list?
50
+ if list_item_selected?
51
+ next_list_item
52
+ end
53
+ @readline.set_input select_list_item
54
+ else
55
+ @readline.history_down
56
+ end
57
+ @readline.cursor_write_input
58
+ end
59
+
60
+ def readline_delete
61
+ @readline.delete
62
+ end
63
+
64
+ def readline_delete_line
65
+ @readline.delete_line
66
+ end
67
+
68
+ def readline_delete_word
69
+ @readline.delete_word
70
+ end
71
+
72
+ def readline_grep_context_decrease
73
+ decrease_grep_context
74
+ @readline.call_block
75
+ end
76
+
77
+ def readline_grep_context_increase
78
+ increase_grep_context
79
+ @readline.call_block
80
+ end
81
+
82
+ def readline_page_down
83
+ page_down
84
+ @readline.list_sync select_list_item
85
+ end
86
+
87
+ def readline_page_up
88
+ page_up
89
+ @readline.list_sync select_list_item
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,179 @@
1
+ module Diakonos
2
+ module Functions
3
+
4
+ # Searches for matches of a regular expression in the current buffer.
5
+ # @param [String] regexp_source_
6
+ # The regular expression to search for.
7
+ # @param [Hash] options
8
+ # Options that alter how the search is performed
9
+ # @option options [Symbol] :direction (:down)
10
+ # The direction to search; :down or :up.
11
+ # @option options [Boolean] :case_sensitive (false)
12
+ # Whether or not the search should be case_sensitive.
13
+ # @option options [String] replacement
14
+ # If provided, do a find and replace, and replace matches with replacement.
15
+ # @option options [Boolean] :word_only (false)
16
+ # Whether or not to search with word boundaries
17
+ # @see #find_exact
18
+ # @see #find_again
19
+ # @see #find_clip
20
+ def find( regexp_source_ = nil, options = {} )
21
+ direction = options[:direction] || :down
22
+ case_sensitive = options[:case_sensitive]
23
+ replacement = options[:replacement]
24
+ word_only = options[:word_only]
25
+
26
+ if regexp_source_
27
+ regexp_source = regexp_source_
28
+ else
29
+ buffer_current.clear_search_area
30
+ m = buffer_current.selection_mark
31
+ if m
32
+ if m.start_row != m.end_row
33
+ buffer_current.set_search_area buffer_current.selection_mark
34
+ buffer_current.remove_selection
35
+ else
36
+ selected_text = buffer_current.copy_selection[ 0 ]
37
+ end
38
+ end
39
+ starting_row, starting_col = buffer_current.last_row, buffer_current.last_col
40
+
41
+ regexp_source = get_user_input(
42
+ "Search regexp: ",
43
+ history: @rlh_search,
44
+ initial_text: selected_text || ""
45
+ ) { |input|
46
+ if input.length > 1
47
+ regexp_source = word_only ? "\\b#{input}\\b" : input
48
+ find_(
49
+ direction: direction,
50
+ case_sensitive: case_sensitive,
51
+ regexp_source: regexp_source,
52
+ starting_row: starting_row,
53
+ starting_col: starting_col,
54
+ quiet: true
55
+ )
56
+ else
57
+ buffer_current.remove_selection Buffer::DONT_DISPLAY
58
+ buffer_current.clear_matches Buffer::DO_DISPLAY
59
+ end
60
+ }
61
+ end
62
+
63
+ if regexp_source
64
+ if word_only
65
+ regexp_source = "\\b#{regexp_source}\\b"
66
+ end
67
+ num_replacements = find_(
68
+ direction: direction,
69
+ case_sensitive: case_sensitive,
70
+ regexp_source: regexp_source,
71
+ replacement: replacement,
72
+ starting_row: starting_row,
73
+ starting_col: starting_col,
74
+ quiet: false
75
+ )
76
+ show_number_of_matches_found( replacement ? num_replacements : nil )
77
+ elsif starting_row && starting_col
78
+ buffer_current.clear_matches
79
+ if @settings[ 'find.return_on_abort' ]
80
+ buffer_current.cursor_to starting_row, starting_col, Buffer::DO_DISPLAY
81
+ end
82
+ end
83
+ end
84
+
85
+ # Searches for matches of the latest clipboard item in the current buffer.
86
+ # Note that the clipboard item is interpreted as a regular expression.
87
+ # Only the last line of multi-line clipboard items is used.
88
+ # @param [String] direction
89
+ # The direction to search. :down (default) or :up.
90
+ # @param [Boolean] case_sensitive
91
+ # Whether or not the search should be case_sensitive. Default is insensitive.
92
+ # @see #find
93
+ def find_clip( direction = :down, case_sensitive = CASE_INSENSITIVE )
94
+ find @clipboard.clip[-1], direction: direction, case_sensitive: case_sensitive
95
+ end
96
+
97
+ # Search again for the most recently sought search term.
98
+ # @param [String] direction
99
+ # The direction to search; :down or :up.
100
+ # @see #find
101
+ # @see #find_exact
102
+ def find_again( direction = :down )
103
+ if direction
104
+ buffer_current.find_again( @last_search_regexps, direction )
105
+ else
106
+ buffer_current.find_again( @last_search_regexps )
107
+ end
108
+ show_number_of_matches_found
109
+ end
110
+
111
+ # Search for an exact string (not a regular expression).
112
+ # @param [Symbol] direction
113
+ # The direction to search; :down (default) or :up.
114
+ # @param [String] search_term_
115
+ # The thing to search for.
116
+ # @see #find
117
+ # @see #find_again
118
+ def find_exact( direction = :down, search_term_ = nil )
119
+ buffer_current.clear_search_area
120
+ if search_term_.nil?
121
+ if buffer_current.changing_selection
122
+ selected_text = buffer_current.copy_selection[ 0 ]
123
+ end
124
+ search_term = get_user_input(
125
+ "Search for: ",
126
+ history: @rlh_search,
127
+ initial_text: selected_text || ""
128
+ )
129
+ else
130
+ search_term = search_term_
131
+ end
132
+ if search_term
133
+ regexp = [ Regexp.new( Regexp.escape( search_term ) ) ]
134
+ buffer_current.find( regexp, :direction => direction )
135
+ @last_search_regexps = regexp
136
+ end
137
+ end
138
+
139
+ # Moves the cursor to the pair match of the current character, if any.
140
+ def go_to_pair_match
141
+ buffer_current.go_to_pair_match
142
+ end
143
+
144
+ # Wrapper method for calling #find for search and replace.
145
+ # @see #find
146
+ def search_and_replace( case_sensitive = CASE_INSENSITIVE )
147
+ find nil, case_sensitive: case_sensitive, replacement: ASK_REPLACEMENT
148
+ end
149
+ alias_method :find_and_replace, :search_and_replace
150
+
151
+ # Immediately moves the cursor to the next match of a regular expression.
152
+ # The user is not prompted for any value.
153
+ # @param [String] regexp_source
154
+ # The regular expression to search for.
155
+ # @param [Symbol] direction
156
+ # The direction to search; :down (default) or :up.
157
+ def seek( regexp_source, direction = :down )
158
+ if regexp_source
159
+ regexp = Regexp.new( regexp_source )
160
+ buffer_current.seek( regexp, direction )
161
+ end
162
+ end
163
+
164
+ def show_number_of_matches_found( num_replacements = nil )
165
+ return if buffer_current.num_matches_found.nil?
166
+
167
+ num_found = buffer_current.num_matches_found
168
+ if num_found != 1
169
+ plural = 'es'
170
+ end
171
+ if num_replacements
172
+ set_iline_if_empty "#{num_replacements} out of #{num_found} match#{plural} replaced"
173
+ else
174
+ set_iline_if_empty "#{num_found} match#{plural} found"
175
+ end
176
+ end
177
+
178
+ end
179
+ end