Module: Diakonos::Functions

Included in:
Diakonos
Defined in:
lib/diakonos/functions.rb,
lib/diakonos/functions/tags.rb,
lib/diakonos/functions/shell.rb,
lib/diakonos/functions/search.rb,
lib/diakonos/functions/cursor.rb,
lib/diakonos/functions/basics.rb,
lib/diakonos/functions/buffers.rb,
lib/diakonos/functions/sessions.rb,
lib/diakonos/functions/grepping.rb,
lib/diakonos/functions/readline.rb,
lib/diakonos/functions/selection.rb,
lib/diakonos/functions/clipboard.rb,
lib/diakonos/functions-deprecated.rb,
lib/diakonos/functions/bookmarking.rb,
lib/diakonos/functions/indentation.rb,
lib/diakonos/functions/text-manipulation.rb

Overview

The Diakonos::Functions module contains all the methods that can be mapped to keys in Diakonos. New methods can be added to this module by extensions.

Instance Method Summary collapse

Instance Method Details

#aboutObject

Shows the About page, which gives information on Diakonos.



10
11
12
13
# File 'lib/diakonos/functions.rb', line 10

def about
  about_write
  open_file @about_filename
end

#add_named_bookmark(name_ = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/diakonos/functions/bookmarking.rb', line 4

def add_named_bookmark( name_ = nil )
  if name_.nil?
    name = get_user_input "Bookmark name: "
  else
    name = name_
  end

  if name
    @bookmarks[ name ] = Bookmark.new( buffer_current, buffer_current.current_row, buffer_current.current_column, name )
    set_iline "Added bookmark #{@bookmarks[ name ].to_s}."
  end
end

#addNamedBookmarkObject



8
# File 'lib/diakonos/functions-deprecated.rb', line 8

alias_method :addNamedBookmark,       :add_named_bookmark

#anchor_selectionObject Also known as: anchorSelection

Begins selecting text by anchoring (marking) the start of a selection.



5
6
7
8
# File 'lib/diakonos/functions/selection.rb', line 5

def anchor_selection
  buffer_current.anchor_selection
  update_status_line
end

#anchor_unanchored_selection(*method_and_args) ⇒ Object

Used for “shift+arrow” style selection.



11
12
13
14
15
16
17
# File 'lib/diakonos/functions/selection.rb', line 11

def anchor_unanchored_selection( *method_and_args )
  buffer_current.anchor_unanchored_selection
  if method_and_args[0]
    self.send method_and_args[0], *method_and_args[1..-1]
  end
  update_status_line
end

#backspaceObject

Move one character left, then delete one character.

See Also:



7
8
9
10
11
# File 'lib/diakonos/functions/basics.rb', line 7

def backspace
  if( buffer_current.changing_selection || cursor_left( Buffer::STILL_TYPING ) )
    delete
  end
end

#carriage_returnObject Also known as: carriageReturn

Insert a carriage return (newline) at the current cursor location. Deletes any currently selected text.



15
16
17
18
# File 'lib/diakonos/functions/basics.rb', line 15

def carriage_return
  buffer_current.carriage_return
  buffer_current.delete_selection
end

#change_session_setting(key_ = nil, value = nil, do_redraw = DONT_REDRAW) ⇒ Object Also known as: changeSessionSetting



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diakonos/functions/sessions.rb', line 8

def change_session_setting( key_ = nil, value = nil, do_redraw = DONT_REDRAW )
  if key_.nil?
    key = get_user_input( "Setting: " )
  else
    key = key_
  end

  if key
    if value.nil?
      value = get_user_input( "Value: " )
    end
    case @settings[ key ]
    when String
      value = value.to_s
    when Fixnum
      value = value.to_i
    when TrueClass, FalseClass
      value = value.to_b
    end
    @session.settings[ key ] = value
    merge_session_settings
    redraw  if do_redraw
    set_iline "#{key} = #{value}"
  end
end

#chdir(dir = nil) ⇒ Object

Change the current working directory (CWD) of the Diakonos process.

Parameters:

  • dir (String) (defaults to: nil)

    The directory to change to



6
7
8
9
10
11
# File 'lib/diakonos/functions/shell.rb', line 6

def chdir( dir = nil )
  dir ||= get_user_input( "Change to directory: ", initial_text: Dir.pwd )
  if dir
    Dir.chdir dir
  end
end

#clear_matchesObject Also known as: clearMatches

Removes the highlighting from any text that matches the most recent search.



21
22
23
# File 'lib/diakonos/functions/selection.rb', line 21

def clear_matches
  buffer_current.clear_matches Buffer::DO_DISPLAY
end

#close_buffer(buffer = buffer_current, opts = {}) ⇒ Fixnum Also known as: closeFile, close_file

Closes a buffer.

Parameters:

  • buffer (Diakonos::Buffer) (defaults to: buffer_current)

    The buffer to close. If no buffer is provided, defaults to the current buffer.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :to_all (Fixnum)

    The CHOICE to assume for the prompt.

  • :do_display (Boolean)

    Whether or not to update the display after closure

Returns:

  • (Fixnum)

    the choice the user made, or nil if the user was not prompted to choose.

See Also:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/diakonos/functions/buffers.rb', line 15

def close_buffer( buffer = buffer_current, opts = {} )
  return nil  if buffer.nil?

  to_all = opts[:to_all]
  do_display = opts.fetch( :do_display, true )

  choice = nil
  if ! @buffers.include?( buffer )
    log "No such buffer: #{buffer.name}"
    return nil
  end

  do_closure = true

  if buffer.modified? && ! buffer.read_only
    if to_all
      choice = to_all
    else
      choices = [ CHOICE_YES, CHOICE_NO, CHOICE_CANCEL ]
      if @quitting
        choices.concat [ CHOICE_YES_TO_ALL, CHOICE_NO_TO_ALL ]
      end
      choice = get_choice(
        "Save changes to #{buffer.nice_name}?",
        choices,
        CHOICE_CANCEL
      )
    end

    case choice
    when CHOICE_YES, CHOICE_YES_TO_ALL
      do_closure = true
      save_file buffer
    when CHOICE_NO, CHOICE_NO_TO_ALL
      do_closure = true
    when CHOICE_CANCEL
      do_closure = false
    end
  end

  if do_closure
    del_buffer = nil
    previous_buffer = nil
    to_switch_to = nil
    switching = false

    # Search the buffer hash for the buffer we want to delete,
    # and mark the one we will switch to after deletion.
    @buffers.each do |b|
      if switching
        to_switch_to = b
        break
      end

      if b == buffer
        del_buffer = b
        switching = true
        next
      end

      previous_buffer = b
    end

    buf = nil
    while(
      @buffer_stack.any? &&
      ! @buffers.include?( buf ) ||
      buf == del_buffer
    ) do
      buf = @buffer_stack.pop
    end
    if @buffers.include?( buf )
      to_switch_to = buf
    end

    if to_switch_to
      switch_to  to_switch_to, do_display: do_display
    elsif previous_buffer
      switch_to  previous_buffer, do_display: do_display
    else
      # No buffers left.  Open a new blank one.
      open_file
    end

    @buffer_closed = del_buffer
    @buffers.delete del_buffer
    cursor_stack_remove_buffer del_buffer
    save_session

    update_status_line
    update_context_line
  end

  choice
end

#close_codeObject



4
5
6
# File 'lib/diakonos/functions/text-manipulation.rb', line 4

def close_code
  buffer_current.close_code
end

#collapse_whitespaceObject



8
9
10
# File 'lib/diakonos/functions/text-manipulation.rb', line 8

def collapse_whitespace
  buffer_current.collapse_whitespace
end

#collapseWhitespaceObject



15
# File 'lib/diakonos/functions-deprecated.rb', line 15

alias_method :collapseWhitespace,     :collapse_whitespace

#columnize(delimiter = nil, num_spaces_padding = 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/diakonos/functions/text-manipulation.rb', line 13

def columnize( delimiter = nil, num_spaces_padding = 0 )
  delimiter ||= get_user_input(
    "Column delimiter (regexp): ",
    history: @rlh_general,
    initial_text: @settings[ "lang.#{buffer_current.original_language}.column_delimiters" ] || ''
  )
  if delimiter && num_spaces_padding
    buffer_current.columnize Regexp.new( delimiter ), num_spaces_padding
  end
end

#comment_outObject



24
25
26
# File 'lib/diakonos/functions/text-manipulation.rb', line 24

def comment_out
  buffer_current.comment_out
end

#complete_word(direction = :down) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/diakonos/functions/text-manipulation.rb', line 28

def complete_word( direction = :down )
  b = buffer_current
  if b.selecting?
    old_word = b.word_before_cursor
    b.delete_selection
  end
  partial = b.word_before_cursor
  return  if partial.nil?

  all_words = @buffers.find_all { |b_|
    b_.original_language == b.original_language
  }.collect { |b_|
    b_.words( /^#{Regexp.escape(partial)}./ )
  }.flatten
  if all_words.any?
    words = all_words.uniq.sort
    if old_word
      i = (
        ( direction == :up ? words.size - 1 : 1 ) +
        words.find_index { |w|
          w == old_word
        }
      ) % words.size
    else
      freq_word = words.sort_by { |word|
        all_words.find_all { |w| w == word }.size
      }[ -1 ]
      i = words.find_index { |w| w == freq_word }
    end
    word = words[ i ]
    b.insert_string word[ partial.length..-1 ]
    r, c = b.last_row, b.last_col
    b.cursor_to( b.last_row, b.last_col + word.length - partial.length )
    b.set_selection( r, c, r, c + word.length - partial.length )
    n = words.size
    middle_word = words[ i ].center( Curses::cols / 4, ' ' )
    shown_words = [
      words[ ( n+i-2 ) % n ],
      words[ ( n+i-1 ) % n ],
      middle_word,
      words[ ( n+i+1 ) % n ],
      words[ ( n+i+2 ) % n ],
    ].compact.uniq.reject { |w| w == middle_word.strip }.join( ' ' )
    mi = shown_words.index( middle_word )
    padding = " " * ( Curses::cols / 2 - mi - ( middle_word.length / 2 ) )
    set_iline padding + shown_words
  end
end

#copy_selectionObject Also known as: copySelection

Copies the currently selected text to clipboard then unselects.



5
6
7
8
# File 'lib/diakonos/functions/clipboard.rb', line 5

def copy_selection
  @clipboard.add_clip buffer_current.copy_selection
  remove_selection
end

#cursor_boftrue, false Also known as: cursorBOF

Moves the cursor to the beginning of the current buffer.

Returns:

  • (true, false)

    true iff the cursor changed positions



110
111
112
# File 'lib/diakonos/functions/cursor.rb', line 110

def cursor_bof
  buffer_current.cursor_to( 0, 0, Buffer::DO_DISPLAY )
end

#cursor_bolObject Also known as: cursorBOL

Moves the cursor to the beginning of the current line.



115
116
117
# File 'lib/diakonos/functions/cursor.rb', line 115

def cursor_bol
  buffer_current.cursor_to_bol
end

#cursor_bovObject Also known as: cursorBOV

Moves the cursor to the bottom of the viewport of the current buffer.



135
136
137
# File 'lib/diakonos/functions/cursor.rb', line 135

def cursor_bov
  buffer_current.cursor_to_bov
end

#cursor_downtrue, false Also known as: cursorDown

Returns true iff the cursor changed positions

Returns:

  • (true, false)

    true iff the cursor changed positions



11
12
13
14
15
16
17
18
19
# File 'lib/diakonos/functions/cursor.rb', line 11

def cursor_down
  buffer_current.cursor_to(
    buffer_current.last_row + 1,
    buffer_current.last_col,
    Buffer::DO_DISPLAY,
    Buffer::STOPPED_TYPING,
    DONT_ADJUST_ROW
  )
end

#cursor_eofObject Also known as: cursorEOF

Moves the cursor to the end of the current buffer.



125
126
127
# File 'lib/diakonos/functions/cursor.rb', line 125

def cursor_eof
  buffer_current.cursor_to_eof
end

#cursor_eolObject Also known as: cursorEOL

Moves the cursor to the end of the current line.



120
121
122
# File 'lib/diakonos/functions/cursor.rb', line 120

def cursor_eol
  buffer_current.cursor_to_eol
end

#cursor_left(stopped_typing = Buffer::STOPPED_TYPING) ⇒ true, false Also known as: cursorLeft

Returns true iff the cursor changed positions

Returns:

  • (true, false)

    true iff the cursor changed positions



22
23
24
25
26
27
28
29
# File 'lib/diakonos/functions/cursor.rb', line 22

def cursor_left( stopped_typing = Buffer::STOPPED_TYPING )
  buffer_current.cursor_to(
    buffer_current.last_row,
    buffer_current.last_col - 1,
    Buffer::DO_DISPLAY,
    stopped_typing
  )
end

#cursor_return(direction = :backward, different_file = NOT_DIFFERENT_FILE) ⇒ Object Also known as: cursorReturn

Pops the cursor stack.

Parameters:

  • direction (Symbol) (defaults to: :backward)

    Either :backward (default) or :forward.

  • different_file (Boolean) (defaults to: NOT_DIFFERENT_FILE)

    Whether to pop just one frame (default), or many frames until a different file is reached.

See Also:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/diakonos/functions/cursor.rb', line 38

def cursor_return( direction = :backward, different_file = NOT_DIFFERENT_FILE )
  delta = 0
  if @cursor_stack_pointer.nil?
    push_cursor_state(
      buffer_current.top_line,
      buffer_current.last_row,
      buffer_current.last_col,
      DONT_CLEAR_STACK_POINTER
    )
    delta = 1
  end

  orig_ptr = @cursor_stack_pointer
  case direction
  when :backward
    @cursor_stack_pointer = ( @cursor_stack_pointer || @cursor_stack.length ) - 1 - delta
    while different_file && @cursor_stack[ @cursor_stack_pointer ] && @cursor_stack[ @cursor_stack_pointer ][ :buffer ] == buffer_current
      @cursor_stack_pointer -= 1
    end
  when :forward
    @cursor_stack_pointer = ( @cursor_stack_pointer || 0 ) + 1
    while different_file && @cursor_stack[ @cursor_stack_pointer ] && @cursor_stack[ @cursor_stack_pointer ][ :buffer ] == buffer_current
      @cursor_stack_pointer += 1
    end
  end
  if @cursor_stack[ @cursor_stack_pointer ].nil? && orig_ptr
    @cursor_stack_pointer = orig_ptr
  end

  return_pointer = @cursor_stack_pointer

  if @cursor_stack_pointer < 0
    return_pointer = @cursor_stack_pointer = 0
  elsif @cursor_stack_pointer >= @cursor_stack.length
    return_pointer = @cursor_stack_pointer = @cursor_stack.length - 1
  else
    cursor_state = @cursor_stack[ @cursor_stack_pointer ]
    if cursor_state
      buffer = cursor_state[ :buffer ]
      switch_to buffer
      buffer.pitch_view( cursor_state[ :top_line ] - buffer.top_line, Buffer::DONT_PITCH_CURSOR, Buffer::DO_DISPLAY )
      buffer.cursor_to( cursor_state[ :row ], cursor_state[ :col ] )
      update_status_line
    end
  end

  set_iline "Location: #{return_pointer+1}/#{@cursor_stack.size}"
end

#cursor_right(stopped_typing = Buffer::STOPPED_TYPING, amount = 1) ⇒ true, false Also known as: cursorRight

Returns true iff the cursor changed positions

Returns:

  • (true, false)

    true iff the cursor changed positions



88
89
90
91
92
93
94
95
# File 'lib/diakonos/functions/cursor.rb', line 88

def cursor_right( stopped_typing = Buffer::STOPPED_TYPING, amount = 1 )
  buffer_current.cursor_to(
    buffer_current.last_row,
    buffer_current.last_col + amount,
    Buffer::DO_DISPLAY,
    stopped_typing
  )
end

#cursor_tovObject Also known as: cursorTOV

Moves the cursor to the top of the viewport of the current buffer.



130
131
132
# File 'lib/diakonos/functions/cursor.rb', line 130

def cursor_tov
  buffer_current.cursor_to_tov
end

#cursor_uptrue, false Also known as: cursorUp

Returns true iff the cursor changed positions

Returns:

  • (true, false)

    true iff the cursor changed positions



98
99
100
101
102
103
104
105
106
# File 'lib/diakonos/functions/cursor.rb', line 98

def cursor_up
  buffer_current.cursor_to(
    buffer_current.last_row - 1,
    buffer_current.last_col,
    Buffer::DO_DISPLAY,
    Buffer::STOPPED_TYPING,
    DONT_ADJUST_ROW
  )
end

#cut_selectionObject Also known as: cutSelection

Copies the currently selected text to clipboard, then deletes it.



11
12
13
14
15
# File 'lib/diakonos/functions/clipboard.rb', line 11

def cut_selection
  if @clipboard.add_clip( buffer_current.copy_selection )
    delete
  end
end

#deleteObject

Calls Buffer#delete on the current buffer.



21
22
23
# File 'lib/diakonos/functions/basics.rb', line 21

def delete
  buffer_current.delete
end

#delete_and_store_lineObject Also known as: deleteAndStoreLine

Deletes the current line, and adds it to the clipboard. If the previous command was also delete_and_store_line, append the line to the previous clip instead of making a new clip.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diakonos/functions/clipboard.rb', line 21

def delete_and_store_line
  removed_text = buffer_current.delete_line
  if removed_text
    clip = [ removed_text, "" ]
    if @functions_last[ -1 ] =~ /^delete_and_store_line/
      @clipboard.append_to_clip clip
    else
      @clipboard.add_clip clip
    end
  end
end

#delete_from(char = nil) ⇒ Object

Deletes characters starting from (but not including) a given character up to (but not including) the current cursor position. Also puts the deleted text into the clipboard.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/diakonos/functions.rb', line 36

def delete_from( char = nil )
  if char.nil?
    set_iline "Type character to delete from..."
    char = @win_main.getch
    set_iline
  end
  if char
    removed_text = buffer_current.delete_from(char)
    if removed_text
      @clipboard.add_clip removed_text
    else
      set_iline "'#{char}' not found."
    end
  end
end

#delete_lineObject Also known as: deleteLine

Deletes the current line and adds it to the clipboard.



26
27
28
29
30
31
# File 'lib/diakonos/functions/basics.rb', line 26

def delete_line
  removed_text = buffer_current.delete_line
  if removed_text
    @clipboard.add_clip( [ removed_text, "" ] )
  end
end

#delete_to(char = nil) ⇒ Object

Deletes characters up to, but not including, a given character. Also puts the deleted text into the clipboard.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diakonos/functions.rb', line 17

def delete_to( char = nil )
  if char.nil?
    set_iline "Type character to delete to..."
    char = @win_main.getch
    set_iline
  end
  if char
    removed_text = buffer_current.delete_to char
    if removed_text
      @clipboard.add_clip removed_text
    else
      set_iline "'#{char}' not found."
    end
  end
end

#delete_to_and_from(inclusive = nil, char = nil) ⇒ Object

Deletes characters between, but not including, a given pair of characters. Also puts the deleted text into the clipboard. Brace characters are intelligently matched with their opposite-side counterparts if the left-side brace is given (e.g. '[').



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/diakonos/functions.rb', line 56

def delete_to_and_from( inclusive = nil, char = nil )
  if char.nil?
    set_iline "Type character to delete to and from..."
    char = @win_main.getch
    set_iline
  end
  if char
    removed_text = buffer_current.delete_to_and_from(
      char,
      inclusive == :inclusive ? INCLUSIVE : NOT_INCLUSIVE
    )
    if removed_text
      @clipboard.add_clip( [ removed_text ] )
    else
      set_iline "'#{char}' not found."
    end
  end
end

#delete_to_eolObject Also known as: deleteToEOL

Deletes the text from the current cursor position to the end of the line, then adds the deleted text to the clipboard.



35
36
37
38
39
40
# File 'lib/diakonos/functions/clipboard.rb', line 35

def delete_to_eol
  removed_text = buffer_current.delete_to_eol
  if removed_text
    @clipboard.add_clip removed_text
  end
end

#evaluate(code_ = nil) ⇒ Object

Evaluates (executes) Ruby code.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/diakonos/functions.rb', line 76

def evaluate( code_ = nil )
  if code_.nil?
    if buffer_current.changing_selection
      selected_text = buffer_current.copy_selection[ 0 ]
    end
    code = get_user_input(
      "Ruby code: ",
      history: @rlh_general,
      initial_text: selected_text || "",
      completion_array: ::Diakonos::Functions.public_instance_methods.map { |m| m.to_s }
    )
  else
    code = code_
  end

  if code
    begin
      eval code
    rescue Exception => e
      show_exception(
        e,
        [
          "The code given to evaluate has a syntax error.",
          "The code given to evaluate refers to a Diakonos command which does not exist, or is misspelled.",
          "The code given to evaluate refers to a Diakonos command with missing arguments.",
          "The code given to evaluate refers to a variable or method which does not exist.",
        ]
      )
    end
  end
end

#execute(command_ = nil) ⇒ Object

Executes a command in a shell, and displays the exit code. Results of the shell command are discarded. Substitutes Diakonos shell variables. Interaction with Diakonos is not possible while the shell is running. For asynchronous shelling, use #spawn. The #execute function allows interaction with shell programs that accept keyboard interaction.

Parameters:

  • command_ (String) (defaults to: nil)

    The shell command to execute

See Also:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/diakonos/functions/shell.rb', line 166

def execute( command_ = nil )
  command = command_ || get_user_input( "Command: ", history: @rlh_shell )

  return  if command.nil?

  command = sub_shell_variables( command )

  Curses::close_screen

  success = system( command )
  if ! success
    result = "Could not execute: #{command}"
  else
    result = "Exit code: #{$?}"
  end

  Curses::init_screen
  refresh_all

  set_iline result
end

#find(regexp_source_ = nil, options = {}) ⇒ Object

Searches for matches of a regular expression in the current buffer.

Parameters:

  • regexp_source_ (String) (defaults to: nil)

    The regular expression to search for.

  • options (Hash) (defaults to: {})

    Options that alter how the search is performed

Options Hash (options):

  • :direction (Symbol) — default: :down

    The direction to search; :down or :up.

  • :case_sensitive (Boolean) — default: false

    Whether or not the search should be case_sensitive.

  • replacement (String)

    If provided, do a find and replace, and replace matches with replacement.

  • :word_only (Boolean) — default: false

    Whether or not to search with word boundaries

See Also:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/diakonos/functions/search.rb', line 20

def find( regexp_source_ = nil, options = {} )
  direction = options[:direction] || :down
  case_sensitive = options[:case_sensitive]
  replacement = options[:replacement]
  word_only = options[:word_only]

  if regexp_source_
    regexp_source = regexp_source_
  else
    buffer_current.search_area = nil
    m = buffer_current.selection_mark
    if m
      if m.start_row != m.end_row
        buffer_current.search_area = buffer_current.selection_mark
        buffer_current.remove_selection
      else
        selected_text = buffer_current.copy_selection[ 0 ]
      end
    end
    starting_row, starting_col = buffer_current.last_row, buffer_current.last_col

    regexp_source = get_user_input(
      "Search regexp: ",
      history: @rlh_search,
      initial_text: selected_text || ""
    ) { |input|
      if input.length > 1
        regexp_source = word_only ? "\\b#{input}\\b" : input
        find_(
          direction: direction,
          case_sensitive: case_sensitive,
          regexp_source: regexp_source,
          starting_row: starting_row,
          starting_col: starting_col,
          quiet: true
        )
      else
        buffer_current.remove_selection Buffer::DONT_DISPLAY
        buffer_current.clear_matches Buffer::DO_DISPLAY
      end
    }
  end

  if regexp_source
    if word_only
      regexp_source = "\\b#{regexp_source}\\b"
    end
    num_replacements = find_(
      direction: direction,
      case_sensitive: case_sensitive,
      regexp_source: regexp_source,
      replacement: replacement,
      starting_row: starting_row,
      starting_col: starting_col,
      quiet: false
    )
    show_number_of_matches_found( replacement ? num_replacements : nil )
  elsif starting_row && starting_col
    buffer_current.clear_matches
    if @settings[ 'find.return_on_abort' ]
      buffer_current.cursor_to starting_row, starting_col, Buffer::DO_DISPLAY
    end
  end
end

#find_again(direction = :down) ⇒ Object Also known as: findAgain

Search again for the most recently sought search term.

Parameters:

  • direction (String) (defaults to: :down)

    The direction to search; :down or :up.

See Also:



102
103
104
105
106
107
108
109
# File 'lib/diakonos/functions/search.rb', line 102

def find_again( direction = :down )
  if direction
    buffer_current.find_again( @last_search_regexps, direction )
  else
    buffer_current.find_again( @last_search_regexps )
  end
  show_number_of_matches_found
end

#find_clip(direction = :down, case_sensitive = CASE_INSENSITIVE) ⇒ Object

Searches for matches of the latest clipboard item in the current buffer. Note that the clipboard item is interpreted as a regular expression. Only the last line of multi-line clipboard items is used.

Parameters:

  • direction (String) (defaults to: :down)

    The direction to search. :down (default) or :up.

  • case_sensitive (Boolean) (defaults to: CASE_INSENSITIVE)

    Whether or not the search should be case_sensitive. Default is insensitive.

See Also:



93
94
95
# File 'lib/diakonos/functions/search.rb', line 93

def find_clip( direction = :down, case_sensitive = CASE_INSENSITIVE )
  find @clipboard.clip[-1], direction: direction, case_sensitive: case_sensitive
end

#find_exact(direction = :down, search_term_ = nil) ⇒ Object Also known as: findExact

Search for an exact string (not a regular expression).

Parameters:

  • direction (Symbol) (defaults to: :down)

    The direction to search; :down (default) or :up.

  • search_term_ (String) (defaults to: nil)

    The thing to search for.

See Also:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/diakonos/functions/search.rb', line 118

def find_exact( direction = :down, search_term_ = nil )
  buffer_current.search_area = nil
  if search_term_.nil?
    if buffer_current.changing_selection
      selected_text = buffer_current.copy_selection[ 0 ]
    end
    search_term = get_user_input(
      "Search for: ",
      history: @rlh_search,
      initial_text: selected_text || ""
    )
  else
    search_term = search_term_
  end
  if search_term
    regexp = [ Regexp.new( Regexp.escape( search_term ) ) ]
    buffer_current.find( regexp, :direction => direction )
    @last_search_regexps = regexp
  end
end

#go_block_innerObject

Moves the cursor to the beginning of the first child code block.



144
145
146
# File 'lib/diakonos/functions/cursor.rb', line 144

def go_block_inner
  buffer_current.go_block_inner
end

#go_block_nextObject

Moves the cursor to the beginning of the next code block at the same indentation level as the current one.



149
150
151
# File 'lib/diakonos/functions/cursor.rb', line 149

def go_block_next
  buffer_current.go_block_next
end

#go_block_outerObject

Moves the cursor to the beginning of the parent code block.



140
141
142
# File 'lib/diakonos/functions/cursor.rb', line 140

def go_block_outer
  buffer_current.go_block_outer
end

#go_block_previousObject

Moves the cursor to the beginning of the previous code block at the same indentation level as the current one.



154
155
156
# File 'lib/diakonos/functions/cursor.rb', line 154

def go_block_previous
  buffer_current.go_block_previous
end

#go_to_char(after = nil, char = nil) ⇒ Object

Moves the cursor to the next occurrence of the given character.

Parameters:

  • char (String) (defaults to: nil)

    The character to go to



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/diakonos/functions/cursor.rb', line 160

def go_to_char( after = nil, char = nil )
  char ||= get_char( "Type character to go to..." )

  if char
    begin
      moved = buffer_current.go_to_char( char, after == :after ? AFTER_CHAR : ON_CHAR )
      if ! moved
        set_iline "'#{char}' not found."
      end
    rescue TypeError
      # User pressed Esc, or Ctrl-C, or similar.
      # Quietly continue.
    end
  end
end

#go_to_char_previous(after = nil, char = nil) ⇒ Object

Moves the cursor to the closest previous occurrence of the given character.

Parameters:

  • char (String) (defaults to: nil)

    The character to go to



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/diakonos/functions/cursor.rb', line 178

def go_to_char_previous( after = nil, char = nil )
  char ||= get_char( "Type character to go to..." )

  if char
    begin
      moved = buffer_current.go_to_char_previous( char, after == :after ? AFTER_CHAR : ON_CHAR )
      if ! moved
        set_iline "'#{char}' not found."
      end
    rescue TypeError
      # User pressed Esc, or Ctrl-C, or similar.
      # Quietly continue.
    end
  end
end

#go_to_line_askObject Also known as: goToLineAsk

Prompts the user for a line number or line delta, with optional column number. Moves the cursor there.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/diakonos/functions/cursor.rb', line 196

def go_to_line_ask
  input = get_user_input( "Go to [line number|+lines][,column number]: " )
  if input
    row = nil
    col = 0

    if input =~ /([+-]\d+)/
      row = buffer_current.last_row + $1.to_i
      col = buffer_current.last_col
    else
      input = input.split( /\D+/ ).collect { |n| n.to_i }
      if input.size > 0
        if input[ 0 ] == 0
          row = nil
        else
          row = input[ 0 ] - 1
        end
        if input[ 1 ]
          col = input[ 1 ] - 1
        end
      end
    end

    if row
      buffer_current.go_to_line( row, col )
    end
  end
end

#go_to_named_bookmark(name_ = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/diakonos/functions/bookmarking.rb', line 17

def go_to_named_bookmark( name_ = nil )
  if name_.nil?
    name = get_user_input "Bookmark name: "
  else
    name = name_
  end

  if name
    bookmark = @bookmarks[ name ]
    if bookmark
      switch_to( bookmark.buffer )
      bookmark.buffer.cursor_to( bookmark.row, bookmark.col, Buffer::DO_DISPLAY )
    else
      set_iline "No bookmark named '#{name}'."
    end
  end
end

#go_to_next_bookmarkObject



35
36
37
# File 'lib/diakonos/functions/bookmarking.rb', line 35

def go_to_next_bookmark
  buffer_current.go_to_next_bookmark
end

#go_to_pair_matchObject

Moves the cursor to the pair match of the current character, if any.



140
141
142
# File 'lib/diakonos/functions/search.rb', line 140

def go_to_pair_match
  buffer_current.go_to_pair_match
end

#go_to_previous_bookmarkObject



39
40
41
# File 'lib/diakonos/functions/bookmarking.rb', line 39

def go_to_previous_bookmark
  buffer_current.go_to_previous_bookmark
end

#go_to_tag(tag_ = nil) ⇒ Object Also known as: goToTag



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/diakonos/functions/tags.rb', line 4

def go_to_tag( tag_ = nil )
  load_tags

  # If necessary, prompt for tag name.

  if tag_.nil?
    if buffer_current.changing_selection
      selected_text = buffer_current.copy_selection[ 0 ]
    end
    tag_name = get_user_input(
      "Tag name: ",
      history: @rlh_general,
      initial_text: selected_text || "",
      completion_array: @tags.keys
    )
  else
    tag_name = tag_
  end

  tag_array = @tags[ tag_name ]
  if tag_array && tag_array.length > 0
    if i = tag_array.index( @last_tag )
      tag = ( tag_array[ i + 1 ] || tag_array[ 0 ] )
    else
      tag = tag_array[ 0 ]
    end
    @last_tag = tag
    @tag_stack.push [ buffer_current.name, buffer_current.last_row, buffer_current.last_col ]
    if switch_to( @buffers.find { |b| b.name == tag.file } )
      #buffer_current.go_to_line( 0 )
    else
      open_file tag.file
    end
    line_number = tag.command.to_i
    if line_number > 0
      buffer_current.go_to_line( line_number - 1 )
    else
      find tag.command case_sensitive: true
    end
  elsif tag_name
    set_iline "No such tag: '#{tag_name}'"
  end
end

#go_to_tag_under_cursorObject Also known as: goToTagUnderCursor



48
49
50
# File 'lib/diakonos/functions/tags.rb', line 48

def go_to_tag_under_cursor
  go_to_tag buffer_current.word_under_cursor
end

#goToNamedBookmarkObject



36
# File 'lib/diakonos/functions-deprecated.rb', line 36

alias_method :goToNamedBookmark,      :go_to_named_bookmark

#goToNextBookmarkObject



37
# File 'lib/diakonos/functions-deprecated.rb', line 37

alias_method :goToNextBookmark,       :go_to_next_bookmark

#goToPreviousBookmarkObject



38
# File 'lib/diakonos/functions-deprecated.rb', line 38

alias_method :goToPreviousBookmark,   :go_to_previous_bookmark

#grep(regexp_source = nil) ⇒ Object



4
5
6
# File 'lib/diakonos/functions/grepping.rb', line 4

def grep( regexp_source = nil )
  grep_( regexp_source, buffer_current )
end

#grep_buffers(regexp_source = nil) ⇒ Object



8
9
10
# File 'lib/diakonos/functions/grepping.rb', line 8

def grep_buffers( regexp_source = nil )
  grep_( regexp_source, *@buffers )
end

#grep_dir(regexp_source = nil, dir = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/diakonos/functions/grepping.rb', line 16

def grep_dir( regexp_source = nil, dir = nil )
  if dir.nil?
    dir = get_user_input(
      "Grep directory: ",
      history: @rlh_files,
      initial_text: @session.dir,
      do_complete: DONT_COMPLETE,
      on_dirs: :accept_dirs
    )
    return if dir.nil?
  end
  dir = File.expand_path( dir )

  original_buffer = buffer_current
  if buffer_current.changing_selection
    selected_text = buffer_current.copy_selection[ 0 ]
  end
  starting_row, starting_col = buffer_current.last_row, buffer_current.last_col

  selected = get_user_input(
    "Grep regexp: ",
    history: @rlh_search,
    initial_text: regexp_source || selected_text || ""
  ) { |input|
    next if input.length < 2
    escaped_input = input.gsub( /'/ ) { "\\047" }
    matching_files = `egrep '#{escaped_input}' -rniIl #{dir}`.split( /\n/ )

    grep_results = matching_files.map { |f|
      ::Diakonos.grep_array(
        Regexp.new( input ),
        File.read( f ).split( /\n/ ),
        settings[ 'grep.context' ],
        "#{File.basename( f )}:",
        f
      )
    }.flatten
    if settings[ 'grep.context' ] == 0
      join_str = "\n"
    else
      join_str = "\n---\n"
    end
    with_list_file do |list|
      list.puts grep_results.join( join_str )
    end

    list_buffer = open_list_buffer
    regexp = nil
    begin
      list_buffer.highlight_matches Regexp.new( input )
    rescue RegexpError => e
      # ignore
    end
    display_buffer list_buffer
  }

  if selected
    spl = selected.split( "| " )
    if spl.size > 1
      open_file spl[ -1 ]
    end
  else
    original_buffer.cursor_to starting_row, starting_col
  end
end

#grep_session_dir(regexp_source = nil) ⇒ Object



12
13
14
# File 'lib/diakonos/functions/grepping.rb', line 12

def grep_session_dir( regexp_source = nil )
  grep_dir regexp_source, @session.dir
end

#help(prefill = '') ⇒ Object

Starts the interactive help system.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/diakonos/functions.rb', line 109

def help( prefill = '' )
  if ! File.exist?( @help_dir ) || Dir[ "#{@help_dir}/*" ].size == 0
    set_iline 'There are no help files installed.'
    return
  end

  open_help_buffer
  matching_docs = nil

  selected = get_user_input(
    "Search terms: ",
    history: @rlh_help,
    initial_text: prefill,
    completion_array: @help_tags
  ) { |input|
    next if input.length < 3 && input[ 0..0 ] != '/'

    matching_docs = matching_help_documents( input )
    with_list_file do |list|
      list.puts matching_docs.join( "\n" )
    end

    open_list_buffer
  }

  close_help_buffer

  case selected
  when /\|/
    open_help_document selected
  when nil
    # Help search aborted; do nothing
  else
    # Not a selected help document
    if matching_docs.nil? || matching_docs.empty?
      matching_docs = matching_help_documents( selected )
    end

    case matching_docs.size
    when 1
      open_help_document matching_docs[ 0 ]
    when 0
      File.open( @error_filename, 'w' ) do |f|
        f.puts "There were no help documents matching your search."
        f.puts "(#{selected.strip})"
      end
      error_file = open_file( @error_filename )

      choice = get_choice(
        "Would you like to send your search terms over the Internet to help improve Diakonos?",
        [ CHOICE_YES, CHOICE_NO ]
      )
      case choice
      when CHOICE_YES
        require 'net/http'
        require 'uri'

        res = Net::HTTP.post_form(
          URI.parse( 'http://dh.pist0s.ca/' ),
          { 'q' => selected }
        )
        # TODO: let them choose "never" and "always"
      end

      close_buffer error_file
    else
      help selected
    end
  end
end

#indentObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/diakonos/functions/indentation.rb', line 4

def indent
  if ! buffer_current.changing_selection
    buffer_current.indent
  else
    @do_display = false
    mark = buffer_current.selection_mark
    if mark.end_col > 0
      end_row = mark.end_row
    else
      end_row = mark.end_row - 1
    end
    (mark.start_row..end_row).each do |row|
      buffer_current.indent row, Buffer::DONT_DISPLAY
    end
    @do_display = true
    display_buffer buffer_current
  end
end

#insert_spaces(num_spaces) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/diakonos/functions/indentation.rb', line 23

def insert_spaces( num_spaces )
  if num_spaces > 0
    buffer_current.delete_selection
    buffer_current.insert_string( " " * num_spaces )
    cursor_right( Buffer::STILL_TYPING, num_spaces )
  end
end

#insert_tabObject



31
32
33
# File 'lib/diakonos/functions/indentation.rb', line 31

def insert_tab
  type_character "\t"
end

#insertSpacesObject



41
# File 'lib/diakonos/functions-deprecated.rb', line 41

alias_method :insertSpaces,           :insert_spaces

#insertTabObject



42
# File 'lib/diakonos/functions-deprecated.rb', line 42

alias_method :insertTab,              :insert_tab

#join_linesObject



81
82
83
# File 'lib/diakonos/functions/text-manipulation.rb', line 81

def join_lines
  buffer_current.join_lines( buffer_current.current_row, Buffer::STRIP_LINE )
end

#join_lines_upwardObject



77
78
79
# File 'lib/diakonos/functions/text-manipulation.rb', line 77

def join_lines_upward
  buffer_current.join_lines_upward( buffer_current.current_row, Buffer::STRIP_LINE )
end

#joinLinesObject



43
# File 'lib/diakonos/functions-deprecated.rb', line 43

alias_method :joinLines,              :join_lines

#list_buffersObject

Opens the special “buffer selection” buffer, and prompts the user to select a buffer. The user can select a buffer either with the arrow keys and the Enter key, or by pressing the key corresponding to an index presented in a left-hand column in the list.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/diakonos/functions/buffers.rb', line 115

def list_buffers
  bullets = ( ('0'..'9').to_a + ('a'..'z').to_a ).map { |s| "#{s}  " }
  buffers_unnamed = @buffers.find_all { |b| b.name.nil? }
  buffers_named = @buffers.find_all { |b| b.name }

  with_list_file do |f|
    if buffers_unnamed.size == 1
      bullet = bullets.shift
      f.puts "#{bullet}(unnamed buffer)"
    else
      buffers_unnamed.each_with_index do |b,i|
        bullet = bullets.shift
        f.puts "#{bullet}(unnamed buffer #{i+1})"
      end
    end

    buffers_named.collect { |b| b.name }.sort.each_with_index do |name, index|
      bullet = bullets.shift
      f.puts "#{bullet}#{name}"
    end
  end
  open_list_buffer
  filename = get_user_input( "Switch to buffer: ", numbered_list: true )
  buffer = buffers_named.find { |b| b.name == filename }
  if buffer
    switch_to buffer
  elsif filename =~ /\(unnamed buffer( \d+)?/
    switch_to( buffers_unnamed[ $1.to_i - 1 ] )
  end
end

#load_script(name_ = nil) ⇒ Object Also known as: loadScript

Loads Ruby code from file using Kernel#load.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/diakonos/functions.rb', line 181

def load_script( name_ = nil )
  if name_.nil?
    name = get_user_input( "File to load as script: ", history: @rlh_files )
  else
    name = name_
  end

  if name
    thread = Thread.new( name ) do |f|
      begin
        load( f )
      rescue Exception => e
        show_exception(
          e,
          [
            "The filename given does not exist.",
            "The filename given is not accessible or readable.",
            "The loaded script does not reference Diakonos commands as members of the global Diakonos object.  e.g. cursor_bol instead of $diakonos.cursor_bol",
            "The loaded script has syntax errors.",
            "The loaded script references objects or object members which do not exist."
          ]
        )
      end
      set_iline "Loaded script '#{name}'."
    end

    loop do
      if thread.status != "run"
        break
      else
        sleep 0.1
      end
    end
    thread.join
  end
end

#merge_session_settingsObject



4
5
6
# File 'lib/diakonos/functions/sessions.rb', line 4

def merge_session_settings
  @settings.merge! @session.settings
end

#name_sessionObject



34
35
36
37
38
39
40
# File 'lib/diakonos/functions/sessions.rb', line 34

def name_session
  name = get_user_input( 'Session name: ' )
  if name
    @session = Session.new("#{@session_dir}/#{name}")
    save_session
  end
end

#open_file(filename = nil, meta = {}) ⇒ Buffer, NilClass Also known as: new_file, newFile, openFile

Opens a file into a new Buffer.

Parameters:

  • filename (defaults to: nil)

    The file to open. If nil, an empty, unnamed buffer is opened.

  • meta (Hash) (defaults to: {})

    metadata containing additional information on how to open the file

Options Hash (meta):

  • 'cursor' (Hash) — default: nil

    A Hash containing the 'row' and 'col' to position the cursor after opening.

  • 'display' (Hash) — default: nil

    A Hash containing the 'top_line' and 'left_column' to use to position the view after opening.

  • 'read_only' (Boolean) — default: false

    Whether to open the file in read-only (unmodifiable) mode

  • 'revert' (Boolean) — default: false

    Whether to skip asking about reverting to on-disk file contents (if different)

Returns:

  • (Buffer)

    the buffer of the opened file

  • (NilClass)

    nil on failure



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/diakonos/functions/buffers.rb', line 162

def open_file( filename = nil, meta = {} )
  read_only    = !!meta[ 'read_only' ]
  force_revert = meta[ 'revert' ] || ASK_REVERT
  if meta[ 'cursor' ]
    last_row = meta[ 'cursor' ][ 'row' ]
    last_col = meta[ 'cursor' ][ 'col' ]
  end
  if meta[ 'display' ]
    top_line    = meta[ 'display' ][ 'top_line' ]
    left_column = meta[ 'display' ][ 'left_column' ]
  end

  do_open = true
  buffer = nil
  if filename
    filename, last_row_ = ::Diakonos.parse_filename_and_line_number( filename )
    last_row = last_row_ || last_row
    if filename =~ /\(unnamed buffer (\d+)\)/
      existing_buffer = @buffers.find { |b| b.object_id == $1.to_i }
      filename = nil
      do_open = false
    else
      existing_buffer = @buffers.find { |b| b.name == filename }
    end

    if filename
      if existing_buffer
        do_open = force_revert || ( filename =~ /\.diakonos/ )
        switch_to  existing_buffer, do_display: false

        if ! do_open && existing_buffer.file_different?
          show_buffer_file_diff( existing_buffer ) do
            choice = get_choice(
              "Load on-disk version of #{existing_buffer.nice_name}?",
              [ CHOICE_YES, CHOICE_NO ]
            )
            case choice
            when CHOICE_YES
              do_open = true
            when CHOICE_NO
              do_open = false
            end
          end
        end
      end

      if FileTest.exist?( filename )
        # Don't try to open non-files (i.e. directories, pipes, sockets, etc.)
        do_open &&= FileTest.file?( filename )
      end
    end
  end

  if do_open
    # Is file readable?

    # Does the "file" utility exist?
    if(
      filename &&
      @settings[ 'use_magic_file' ] &&
      FileTest.exist?( "/usr/bin/file" ) &&
      FileTest.exist?( filename ) &&
      /\blisting\.txt\b/ !~ filename
    )
      file_type = `/usr/bin/file -L #{filename}`
      if file_type !~ /text/ && file_type !~ /empty$/
        choice = get_choice(
          "#{filename} does not appear to be readable.  Try to open it anyway?",
          [ CHOICE_YES, CHOICE_NO ],
          CHOICE_NO
        )
        case choice
        when CHOICE_NO
          do_open = false
        end

      end
    end

    if do_open
      buffer = Buffer.new(
        'filepath' => filename,
        'read_only' => read_only,
        'display' => {
          'top_line' => top_line,
          'left_column' => left_column,
        },
        'cursor' => {
          'row' => last_row,
          'col' => last_col,
        }
      )
      if existing_buffer
        @buffers[ @buffers.index( existing_buffer ) ] = buffer
      else
        if @settings['open_as_first_buffer']
          @buffers.unshift buffer
        else
          @buffers << buffer
        end
      end
      run_hook_procs( :after_open, buffer )
      save_session
      if switch_to( buffer, do_display: false )
        if last_row
          buffer.cursor_to last_row, last_col || 0, Buffer::DONT_DISPLAY
        end
        display_buffer buffer
      end
    end
  elsif existing_buffer
    if switch_to( existing_buffer, do_display: false )
      if last_row
        existing_buffer.cursor_to last_row, last_col || 0, Buffer::DONT_DISPLAY
      end
      display_buffer existing_buffer
    end
  end

  buffer || existing_buffer
end

#open_file_askObject Also known as: openFileAsk

Prompts the user for a file to open, then opens it with #open_file .

See Also:



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/diakonos/functions/buffers.rb', line 287

def open_file_ask
  prefill = ''

  if buffer_current
    if buffer_current.current_line =~ %r#(/\w+)+/\w+\.\w+#
      prefill = $&
    elsif buffer_current.name
      prefill = File.expand_path( File.dirname( buffer_current.name ) ) + "/"
    end
  end

  if @settings[ 'fuzzy_file_find' ]
    prefill = ''
    finder_block = lambda { |input|
      break  if input =~ %r{^/}

      finder = FuzzyFileFinder.new(
        directories: @session.dir,
        ceiling: @settings['fuzzy_file_find.max_files'] || 8192,
        ignores: @fuzzy_ignores,
        recursive: @settings['fuzzy_file_find.recursive']
      )
      matches = finder.find(input).sort_by { |m| m[:path] }
      with_list_file do |list|
        list.puts matches.map { |m| m[ :path ] }
      end
      open_list_buffer
    }
  end

  file = get_user_input(
    "Filename: ",
    history: @rlh_files,
    initial_text: prefill,
    &finder_block
  )

  if file && ! file.empty?
    open_file file
    update_status_line
    update_context_line
  end
end

#open_matching_files(regexp = nil, search_root = nil) ⇒ Object

Opens all files within a directory whose contents match a regular expression.

Parameters:

  • regexp (String) (defaults to: nil)

    The regular expression used to match against. If nil, the user is prompted for a value.

  • search_root (String) (defaults to: nil)

    The directory under which to recursively search for matches. If nil, the user is prompted for a value.



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/diakonos/functions/buffers.rb', line 339

def open_matching_files( regexp = nil, search_root = nil )
  regexp ||= get_user_input( "Regexp: ", history: @rlh_search )
  return  if regexp.nil?

  if buffer_current.current_line =~ %r{\w*/[/\w.]+}
    prefill = $&
  else
    prefill = File.expand_path( File.dirname( buffer_current.name ) ) + "/"
  end
  search_root ||= get_user_input( "Search within: ", history: @rlh_files, initial_text: prefill )
  return  if search_root.nil?

  files = `egrep -rl '#{regexp.gsub( /'/, "'\\\\''" )}' #{search_root}/*`.split( /\n/ )
  if files.any?
    if files.size > 5
        choice = get_choice( "Open #{files.size} files?", [ CHOICE_YES, CHOICE_NO ] )
        return  if choice == CHOICE_NO
    end
    files.each do |f|
      open_file f
    end
    find regexp, direction: :down, case_sensitive: true
  end
end

#operate_on_each_line(ruby_code = get_user_input( 'Ruby code: ', history: @rlh_general, initial_text: 'line.' )) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/diakonos/functions/text-manipulation.rb', line 123

def operate_on_each_line(
  ruby_code = get_user_input(
    'Ruby code: ',
    history: @rlh_general,
    initial_text: 'line.'
  )
)
  if ruby_code
    lines = buffer_current.selected_text
    if lines && ! lines.empty?
      if lines[ -1 ].empty?
        lines.pop
        popped = true
      end
      new_lines = eval( "lines.collect { |line| #{ruby_code} }" )
      if popped
        new_lines << ''
      end
      buffer_current.paste new_lines
    end
  end
end

#operate_on_lines(ruby_code = get_user_input( 'Ruby code: ', history: @rlh_general, initial_text: 'lines.collect { |l| l }' )) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/diakonos/functions/text-manipulation.rb', line 100

def operate_on_lines(
  ruby_code = get_user_input(
    'Ruby code: ',
    history: @rlh_general,
    initial_text: 'lines.collect { |l| l }'
  )
)
  if ruby_code
    lines = buffer_current.selected_text
    if lines && ! lines.empty?
      if lines[ -1 ].empty?
        lines.pop
        popped = true
      end
      new_lines = eval( ruby_code )
      if popped
        new_lines << ''
      end
      buffer_current.paste new_lines
    end
  end
end

#operate_on_string(ruby_code = get_user_input( 'Ruby code: ', history: @rlh_general, initial_text: 'str.' )) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/diakonos/functions/text-manipulation.rb', line 85

def operate_on_string(
  ruby_code = get_user_input(
    'Ruby code: ',
    history: @rlh_general,
    initial_text: 'str.'
  )
)
  if ruby_code
    str = buffer_current.selected_string
    if str && ! str.empty?
      buffer_current.paste eval( ruby_code )
    end
  end
end

#operateOnEachLineObject



50
# File 'lib/diakonos/functions-deprecated.rb', line 50

alias_method :operateOnEachLine,      :operate_on_each_line

#operateOnLinesObject



49
# File 'lib/diakonos/functions-deprecated.rb', line 49

alias_method :operateOnLines,         :operate_on_lines

#operateOnStringObject



48
# File 'lib/diakonos/functions-deprecated.rb', line 48

alias_method :operateOnString,        :operate_on_string

#page_downObject Also known as: pageDown

Pitches the current buffer's view one screenful down.



226
227
228
229
230
231
232
# File 'lib/diakonos/functions/cursor.rb', line 226

def page_down
  if buffer_current.pitch_view( main_window_height, Buffer::DO_PITCH_CURSOR ) == 0
    buffer_current.cursor_to_eof
  end
  update_status_line
  update_context_line
end

#page_upObject Also known as: pageUp

Pitches the current buffer's view one screenful up.



235
236
237
238
239
240
241
# File 'lib/diakonos/functions/cursor.rb', line 235

def page_up
  if buffer_current.pitch_view( -main_window_height, Buffer::DO_PITCH_CURSOR ) == 0
    cursor_bof
  end
  update_status_line
  update_context_line
end

#parsed_indentObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/diakonos/functions/indentation.rb', line 35

def parsed_indent
  if( buffer_current.changing_selection )
    @do_display = false
    mark = buffer_current.selection_mark
    (mark.start_row..mark.end_row).each do |row|
      buffer_current.parsed_indent  row: row, do_display: false
    end
    @do_display = true
    display_buffer buffer_current
  else
    buffer_current.parsed_indent
  end
  update_status_line
  update_context_line
end

#parsedIndentObject



53
# File 'lib/diakonos/functions-deprecated.rb', line 53

alias_method :parsedIndent,           :parsed_indent

#pasteObject

Pastes the current clipboard item at the current cursor position.



43
44
45
# File 'lib/diakonos/functions/clipboard.rb', line 43

def paste
  buffer_current.paste @clipboard.clip
end

#paste_shell_result(command_ = nil) ⇒ Object Also known as: pasteShellResult

Executes a command in a shell, captures the results, and pastes them in the current buffer at the current cursor location. Substitutes Diakonos shell variables. Interaction with Diakonos is not possible while the shell is running. For asynchronous shelling, use #spawn.

Parameters:

  • command_ (String) (defaults to: nil)

    The shell command to execute

See Also:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/diakonos/functions/shell.rb', line 200

def paste_shell_result( command_ = nil )
  command = command_ || get_user_input( "Command: ", history: @rlh_shell )

  return  if command.nil?

  command = sub_shell_variables( command )

  Curses::close_screen

  begin
    buffer_current.paste( `#{command} 2<&1`.split( /\n/, -1 ) )
  rescue Exception => e
    debug_log e.message
    debug_log e.backtrace.join( "\n\t" )
    show_exception e
  end

  Curses::init_screen
  refresh_all
end

#play_macro(name = nil) ⇒ Object Also known as: playMacro



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/diakonos/functions.rb', line 218

def play_macro( name = nil )
  macro, input_history = @macros[ name ]
  if input_history
    @macro_input_history = input_history.deep_clone
    if macro
      @playing_macro = true
      macro.each do |command|
        eval command
      end
      @playing_macro = false
      @macro_input_history = nil
    end
  end
end

#pop_tagObject Also known as: popTag



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/diakonos/functions/tags.rb', line 52

def pop_tag
  tag = @tag_stack.pop
  if tag
    if ! switch_to( @buffers.find { |b| b.name == tag[ 0 ] } )
      open_file tag[ 0 ]
    end
    buffer_current.cursor_to( tag[ 1 ], tag[ 2 ], Buffer::DO_DISPLAY )
  else
    set_iline "Tag stack empty."
  end
end


238
239
240
241
# File 'lib/diakonos/functions.rb', line 238

def print_keychain
  @capturing_keychain = true
  set_iline "Type any chain of keystrokes or key chords, then press Enter..."
end


233
234
235
236
# File 'lib/diakonos/functions.rb', line 233

def print_mapped_function
  @capturing_mapping = true
  set_iline "Type any chain of keystrokes or key chords, or press Enter to stop."
end

#quitObject

Quits Diakonos (gracefully).



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/diakonos/functions.rb', line 244

def quit
  @quitting = true
  to_all = nil
  save_session
  @buffers.each do |buffer|
    next  if ! buffer.modified?
    switch_to buffer
    closure_choice = close_buffer( buffer, to_all: to_all )
    case closure_choice
    when CHOICE_CANCEL
      @quitting = false
      break
    when CHOICE_YES_TO_ALL, CHOICE_NO_TO_ALL
      to_all = closure_choice
    end
  end
end

#readline_abortObject



4
5
6
# File 'lib/diakonos/functions/readline.rb', line 4

def readline_abort
  @readline.abort
end

#readline_acceptObject



8
9
10
# File 'lib/diakonos/functions/readline.rb', line 8

def readline_accept
  @readline.accept current_list_item
end

#readline_backspaceObject



12
13
14
# File 'lib/diakonos/functions/readline.rb', line 12

def readline_backspace
  @readline.backspace
end

#readline_complete_inputObject



16
17
18
# File 'lib/diakonos/functions/readline.rb', line 16

def readline_complete_input
  @readline.complete_input
end

#readline_cursor_bolObject



28
29
30
# File 'lib/diakonos/functions/readline.rb', line 28

def readline_cursor_bol
  @readline.cursor_bol
end

#readline_cursor_downObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/diakonos/functions/readline.rb', line 48

def readline_cursor_down
  if showing_list?
    if list_item_selected?
      next_list_item
    end
    @readline.set_input select_list_item
  else
    @readline.history_down
  end
  @readline.cursor_write_input
end

#readline_cursor_eolObject



32
33
34
# File 'lib/diakonos/functions/readline.rb', line 32

def readline_cursor_eol
  @readline.cursor_eol
end

#readline_cursor_leftObject



20
21
22
# File 'lib/diakonos/functions/readline.rb', line 20

def readline_cursor_left
  @readline.cursor_left
end

#readline_cursor_rightObject



24
25
26
# File 'lib/diakonos/functions/readline.rb', line 24

def readline_cursor_right
  @readline.cursor_right
end

#readline_cursor_upObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/diakonos/functions/readline.rb', line 36

def readline_cursor_up
  if showing_list?
    if list_item_selected?
      previous_list_item
    end
    @readline.set_input select_list_item
  else
    @readline.history_up
  end
  @readline.cursor_write_input
end

#readline_deleteObject



60
61
62
# File 'lib/diakonos/functions/readline.rb', line 60

def readline_delete
  @readline.delete
end

#readline_delete_lineObject



64
65
66
# File 'lib/diakonos/functions/readline.rb', line 64

def readline_delete_line
  @readline.delete_line
end

#readline_delete_wordObject



68
69
70
# File 'lib/diakonos/functions/readline.rb', line 68

def readline_delete_word
  @readline.delete_word
end

#readline_grep_context_decreaseObject



72
73
74
75
# File 'lib/diakonos/functions/readline.rb', line 72

def readline_grep_context_decrease
  decrease_grep_context
  @readline.call_block
end

#readline_grep_context_increaseObject



77
78
79
80
# File 'lib/diakonos/functions/readline.rb', line 77

def readline_grep_context_increase
  increase_grep_context
  @readline.call_block
end

#readline_page_downObject



82
83
84
85
# File 'lib/diakonos/functions/readline.rb', line 82

def readline_page_down
  page_down
  @readline.list_sync select_list_item
end

#readline_page_upObject



87
88
89
90
# File 'lib/diakonos/functions/readline.rb', line 87

def readline_page_up
  page_up
  @readline.list_sync select_list_item
end

#remove_named_bookmark(name_ = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/diakonos/functions/bookmarking.rb', line 43

def remove_named_bookmark( name_ = nil )
  if name_.nil?
    name = get_user_input "Bookmark name: "
  else
    name = name_
  end

  if name
    bookmark = @bookmarks.delete name
    set_iline "Removed bookmark #{bookmark.to_s}."
  end
end

#remove_selectionObject Also known as: removeSelection

Unselects any current selection (stops selecting).



26
27
28
29
# File 'lib/diakonos/functions/selection.rb', line 26

def remove_selection
  buffer_current.remove_selection
  update_status_line
end

#removeNamedBookmarkObject



57
# File 'lib/diakonos/functions-deprecated.rb', line 57

alias_method :removeNamedBookmark,    :remove_named_bookmark

#renumber_buffer(to, from = nil) ⇒ Object

Places a buffer at a new position in the array of Buffers after shifting down (index+1) all existing Buffers from that position onwards.

Parameters:

  • to (Fixnum)

    The new 1-based position of the buffer to move

  • from (Fixnum) (defaults to: nil)

    The original 1-based position of the buffer to move. Default: current buffer



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/diakonos/functions/buffers.rb', line 368

def renumber_buffer( to, from = nil )
  if to < 1
    raise "Invalid buffer index: #{to.inspect}"
  end
  if from && from < 1
    raise "Invalid buffer index: #{from.inspect}"
  end

  from ||= buffer_to_number( buffer_current )
  from_ = from - 1
  to_   = to - 1
  b = @buffers[from_]
  @buffers.delete_at from_
  @buffers.insert( to_, b )
  @buffers.compact!

  update_status_line
end

#repeat_lastObject Also known as: repeatLast



262
263
264
# File 'lib/diakonos/functions.rb', line 262

def repeat_last
  eval @functions_last[ -1 ] if ! @functions_last.empty?
end

#revert(prompt = nil) ⇒ Object

If the prompt is non-nil, ask the user yes or no question first.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/diakonos/functions/buffers.rb', line 388

def revert( prompt = nil )
  do_revert = true

  if prompt
    show_buffer_file_diff do
      choice = get_choice(
        prompt,
        [ CHOICE_YES, CHOICE_NO ]
      )
      case choice
      when CHOICE_NO
        do_revert = false
      end
    end
  end

  if do_revert
    open_file(
      buffer_current.name,
      'read_only' => false,
      'revert' => FORCE_REVERT,
      'cursor' => {
        'row' => buffer_current.last_row,
        'col' => buffer_current.last_col
      }
    )
  end
end

#save_file(buffer = buffer_current) ⇒ Object Also known as: saveFile

Saves a buffer, then runs the :after_save hook on it.

Parameters:

  • buffer (Buffer) (defaults to: buffer_current)

    The buffer to save. If nil, defaults to the current buffer.



420
421
422
423
# File 'lib/diakonos/functions/buffers.rb', line 420

def save_file( buffer = buffer_current )
  buffer.save
  run_hook_procs( :after_save, buffer )
end

#save_file_asObject Also known as: saveFileAs



425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/diakonos/functions/buffers.rb', line 425

def save_file_as
  if buffer_current && buffer_current.name
    path = File.expand_path( File.dirname( buffer_current.name ) ) + "/"
    file = get_user_input( "Filename: ", history: @rlh_files, initial_text: path )
  else
    file = get_user_input( "Filename: ", history: @rlh_files )
  end
  if file
    old_name = buffer_current.name
    if buffer_current.save( file, PROMPT_OVERWRITE )
      save_session
    end
  end
end

#scroll_downObject Also known as: scrollDown

Scrolls the current buffer's view down, as determined by the view.scroll_amount setting.



245
246
247
248
249
# File 'lib/diakonos/functions/cursor.rb', line 245

def scroll_down
  buffer_current.pitch_view( @settings[ "view.scroll_amount" ] || 1 )
  update_status_line
  update_context_line
end

#scroll_upObject Also known as: scrollUp

Scrolls the current buffer's view up, as determined by the view.scroll_amount setting.



253
254
255
256
257
258
259
260
261
# File 'lib/diakonos/functions/cursor.rb', line 253

def scroll_up
  if @settings[ "view.scroll_amount" ]
    buffer_current.pitch_view( -@settings[ "view.scroll_amount" ] )
  else
    buffer_current.pitch_view( -1 )
  end
  update_status_line
  update_context_line
end

#search_and_replace(case_sensitive = CASE_INSENSITIVE) ⇒ Object Also known as: find_and_replace, findAndReplace, searchAndReplace

Wrapper method for calling #find for search and replace.

See Also:



146
147
148
# File 'lib/diakonos/functions/search.rb', line 146

def search_and_replace( case_sensitive = CASE_INSENSITIVE )
  find nil, case_sensitive: case_sensitive, replacement: ASK_REPLACEMENT
end

#seek(regexp_source, direction = :down) ⇒ Object

Immediately moves the cursor to the next match of a regular expression. The user is not prompted for any value.

Parameters:

  • regexp_source (String)

    The regular expression to search for.

  • direction (Symbol) (defaults to: :down)

    The direction to search; :down (default) or :up.



157
158
159
160
161
162
# File 'lib/diakonos/functions/search.rb', line 157

def seek( regexp_source, direction = :down )
  if regexp_source
    regexp = Regexp.new( regexp_source )
    buffer_current.seek( regexp, direction )
  end
end

#select_allObject

Selects the entire buffer contents.



32
33
34
# File 'lib/diakonos/functions/selection.rb', line 32

def select_all
  buffer_current.select_all
end

#select_block(beginning = nil, ending = nil, including_ending = true) ⇒ Object

Selects text between two regexps.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/diakonos/functions/selection.rb', line 37

def select_block( beginning = nil, ending = nil, including_ending = true )
  if beginning.nil?
    input = get_user_input( "Start at regexp: " )
    if input
      beginning = Regexp.new input
    end
  end
  if beginning && ending.nil?
    input = get_user_input( "End before regexp: " )
    if input
      ending = Regexp.new input
    end
  end
  if beginning && ending
    buffer_current.select( beginning, ending, including_ending )
  end
end

#select_lineObject

Selects the current line.



75
76
77
78
# File 'lib/diakonos/functions/selection.rb', line 75

def select_line
  buffer_current.select_current_line
  update_status_line
end

#select_wordObject

Selects the word at the current cursor position. If the cursor is not on a word character, the first word following the cursor is selected.



89
90
91
# File 'lib/diakonos/functions/selection.rb', line 89

def select_word
  buffer_current.select_word
end

#select_word_anotherObject



93
94
95
# File 'lib/diakonos/functions/selection.rb', line 93

def select_word_another
  buffer_current.select_word_another
end

#select_wrapping_blockObject

Selects the code block which wraps the current cursor position. Execute multiple times in succession to select increasingly outer code blocks.



82
83
84
85
# File 'lib/diakonos/functions/selection.rb', line 82

def select_wrapping_block
  buffer_current.select_wrapping_block
  update_status_line
end

#selection_mode_blockObject

Changes selection mode to block mode (rectangular selection).



56
57
58
59
# File 'lib/diakonos/functions/selection.rb', line 56

def selection_mode_block
  buffer_current.selection_mode_block
  update_status_line
end

#selection_mode_normalObject

Changes selection mode to normal mode (flow selection).



62
63
64
65
# File 'lib/diakonos/functions/selection.rb', line 62

def selection_mode_normal
  buffer_current.selection_mode_normal
  update_status_line
end

#set_buffer_type(type_ = nil) ⇒ Object Also known as: setBufferType

Sets the type (language) of the current buffer.

Parameters:

  • type_ (String) (defaults to: nil)

    The type to set the current buffer to. If nil, the user is prompted for a value.



444
445
446
447
448
449
450
451
452
453
# File 'lib/diakonos/functions/buffers.rb', line 444

def set_buffer_type( type_ = nil )
  type = type_ || get_user_input( "Content type: " )

  if type
    if buffer_current.set_type( type )
      update_status_line
      update_context_line
    end
  end
end

#set_read_only(read_only = nil) ⇒ Object Also known as: setReadOnly

If read_only is nil, the read_only state of the current buffer is toggled. Otherwise, the read_only state of the current buffer is set to read_only.



457
458
459
460
461
462
463
464
# File 'lib/diakonos/functions/buffers.rb', line 457

def set_read_only( read_only = nil )
  if read_only
    buffer_current.read_only = read_only
  else
    buffer_current.read_only = ( ! buffer_current.read_only )
  end
  update_status_line
end

#set_session_dirObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/diakonos/functions/sessions.rb', line 42

def set_session_dir
  path = get_user_input(
    "Session directory: ",
    history: @rlh_files,
    initial_text: @session.dir,
    do_complete: DONT_COMPLETE,
    on_dirs: :accept_dirs
  )
  if path
    @session.dir = File.expand_path( path )
    save_session
    set_iline "Session dir changed to: #{@session.dir}"
  else
    set_iline "(Session dir is: #{@session.dir})"
  end
end

#shell(command_ = nil, result_filename = 'shell-result.txt') ⇒ Object

Executes a command in a shell, captures the results, and displays them (if any) in a new buffer. Substitutes Diakonos shell variables. Interaction with Diakonos is not possible while the shell is running. For asynchronous shelling, use #spawn. The shell function does not allow interaction with applications run in the shell. Use #execute for interactivity.

Parameters:

  • command_ (String) (defaults to: nil)

    The shell command to execute

  • result_filename (String) (defaults to: 'shell-result.txt')

    The name of the temporary file to write the shell results to

See Also:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/diakonos/functions/shell.rb', line 93

def shell( command_ = nil, result_filename = 'shell-result.txt' )
  command = command_ || get_user_input( "Command: ", history: @rlh_shell )

  return  if command.nil?

  command = sub_shell_variables( command )

  completed = false
  result_file = "#{@diakonos_home}/#{result_filename}"
  File.open( result_file , "w" ) do |f|
    Curses::close_screen

    stdin, stdout, stderr = Open3.popen3( command )

    t1 = Thread.new do
      stdout.each_line do |line|
        f.puts line
      end
    end
    t2 = Thread.new do
      stderr.each_line do |line|
        f.puts line
      end
    end

    catch :stop do
      loop do
        begin
          Timeout::timeout( 5 ) do
            t1.join
            t2.join
            Curses::init_screen
            refresh_all
            completed = true
            throw :stop
          end
        rescue Timeout::Error => e
          choice = get_choice(
            "Keep waiting for shell results?",
            [ CHOICE_YES, CHOICE_NO ],
            CHOICE_YES
          )
          if choice != CHOICE_YES
            t1.terminate
            t2.terminate
            throw :stop
          end
        end
      end
    end

  end
  if File.size?( result_file )
    open_file result_file
    set_iline "#{completed ? '' : '(interrupted) '}Results for: #{command}"
  else
    set_iline "Empty result for: #{command}"
  end
end

#show_clipsObject Also known as: showClips

Opens a new buffer showing a list of all internal clipboard items. Only for use when no external clipboard is used.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/diakonos/functions/clipboard.rb', line 49

def show_clips
  clip_filename = @diakonos_home + "/clips.txt"
  File.open( clip_filename, "w" ) do |f|
    case @settings[ 'clipboard.external' ]
    when 'klipper'
      f.puts 'Access Klipper directly (tray icon) to get at all clips.'
    when 'xclip'
      f.puts 'xclip does not keep a history of clips.'
    else
      @clipboard.each do |clip|
        f.puts clip
        f.puts "---------------------------"
      end
    end
  end
  open_file clip_filename
end

#show_number_of_matches_found(num_replacements = nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/diakonos/functions/search.rb', line 164

def show_number_of_matches_found( num_replacements = nil )
  return  if buffer_current.num_matches_found.nil?

  num_found = buffer_current.num_matches_found
  if num_found != 1
    plural = 'es'
  end
  if num_replacements
    set_iline_if_empty "#{num_replacements} out of #{num_found} match#{plural} replaced"
  else
    set_iline_if_empty "#{num_found} match#{plural} found"
  end
end

#spawn(command_ = nil) ⇒ Object

Executes a command in a shell, captures the results, and pastes them in the current buffer at the current cursor location. Substitutes Diakonos shell variables. The shell is executed in a separate thread, so interaction with Diakonos is possible during execution.

Parameters:

  • command_ (String) (defaults to: nil)

    The shell command to execute

See Also:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/diakonos/functions/shell.rb', line 233

def spawn( command_ = nil )
  command = command_ || get_user_input( "Command: ", history: @rlh_shell )

  return  if command.nil?

  command = sub_shell_variables( command )

  Thread.new do
    if system( command )
      set_iline "Return code #{$?} from '#{command}'"
    else
      set_iline "Error code #{$?} executing '#{command}'"
    end
  end
end

#sub_shell_variables(string) ⇒ String

Substitutes Diakonos shell variables in a String.

  • $f: The current buffer's filename

  • $d: The current buffer's directory

  • $F: A space-separated list of all buffer filenames

  • $i: A string acquired from the user with a prompt

  • $c: The current clipboard text

  • $s: The currently selected text

Parameters:

  • string (String)

    The string containing variables to substitute

Returns:

  • (String)

    A new String with values substituted for all variables



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/diakonos/functions/shell.rb', line 24

def sub_shell_variables( string )
  return  if string.nil?

  retval = string.dup

  # Current buffer filename
  retval.gsub!( /\$f/, ( $1 || "" ) + File.expand_path( buffer_current.name || "" ) )
  # Current buffer dir
  retval.gsub!( /\$d/, ( $1 || "" ) + File.dirname( File.expand_path( buffer_current.name || '' ) ) )

  # space-separated list of all buffer filenames
  name_array = Array.new
  @buffers.each do |b|
    name_array.push b.name
  end
  retval.gsub!( /\$F/, ( $1 || "" ) + ( name_array.join(' ') || "" ) )

  # Get user input, sub it in
  if retval =~ /\$i/
    user_input = get_user_input(
      "Argument: ",
      history: @rlh_shell,
      initial_text: buffer_current.selected_string
    )
    retval.gsub!( /\$i/, user_input )
  end

  # Current clipboard text
  if retval =~ /\$[ck]/
    clip_filename = @diakonos_home + "/clip.txt"
    File.open( clip_filename, "w" ) do |clipfile|
      if @clipboard.clip
        clipfile.puts( @clipboard.clip.join( "\n" ) )
      end
    end
    retval.gsub!( /\$[ck]/, clip_filename )
  end

  # Currently selected text
  if retval =~ /\$s/
    text_filename = @diakonos_home + "/selected.txt"

    File.open( text_filename, "w" ) do |textfile|
      selected_text = buffer_current.selected_text
      if selected_text
        textfile.puts( selected_text.join( "\n" ) )
      end
    end
    retval.gsub!( /\$s/, text_filename )
  end

  retval
end

#surround_line(envelope = nil) ⇒ Object



146
147
148
149
# File 'lib/diakonos/functions/text-manipulation.rb', line 146

def surround_line( envelope = nil )
  buffer_current.set_selection_current_line
  surround_selection envelope
end

#surround_paragraph(envelope = nil) ⇒ Object



151
152
153
154
155
# File 'lib/diakonos/functions/text-manipulation.rb', line 151

def surround_paragraph( envelope = nil )
  ( first, _ ), ( last, length ) = buffer_current.paragraph_under_cursor_pos
  buffer_current.set_selection( first, 0, last, length+1 )
  surround_selection envelope
end

#surround_selection(parenthesis = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/diakonos/functions/text-manipulation.rb', line 157

def surround_selection( parenthesis = nil )
  if ! buffer_current.selecting?
    set_iline "Nothing selected."
    return
  end

  parenthesis ||= get_user_input( "Surround with: " )
  if parenthesis
    text = buffer_current.surround( buffer_current.selected_text, parenthesis )
    if text
      buffer_current.paste text
    end
  end
end

#surround_word(envelope = nil) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/diakonos/functions/text-manipulation.rb', line 172

def surround_word( envelope = nil )
  ( start_row, start_col ), ( end_row, end_col ) = buffer_current.word_under_cursor_pos
  if start_row && start_col && end_row && end_col
    buffer_current.set_selection( start_row, start_col, end_row, end_col )
    surround_selection envelope
  end
end

#suspendObject

Send the Diakonos job to background, as if with Ctrl-Z



267
268
269
270
271
272
# File 'lib/diakonos/functions.rb', line 267

def suspend
  Curses::close_screen
  Process.kill( "SIGSTOP", $PID )
  Curses::init_screen
  refresh_all
end

#switch_to_buffer_number(buffer_number_) ⇒ Object Also known as: switchToBufferNumber



466
467
468
469
470
471
472
473
474
# File 'lib/diakonos/functions/buffers.rb', line 466

def switch_to_buffer_number( buffer_number_ )
  buffer_number = buffer_number_.to_i
  return  if buffer_number < 1
  if @buffer_number_last && buffer_number == buffer_to_number(buffer_current)
    buffer_number = @buffer_number_last
  end
  @buffer_number_last = buffer_to_number(buffer_current)
  switch_to @buffers[ buffer_number - 1 ]
end

#switch_to_next_bufferObject Also known as: switchToNextBuffer



476
477
478
# File 'lib/diakonos/functions/buffers.rb', line 476

def switch_to_next_buffer
  switch_to_buffer_number( buffer_to_number( buffer_current ) + 1 )
end

#switch_to_previous_bufferObject Also known as: switchToPreviousBuffer



480
481
482
# File 'lib/diakonos/functions/buffers.rb', line 480

def switch_to_previous_buffer
  switch_to_buffer_number( buffer_to_number( buffer_current ) - 1 )
end

#toggle_bookmarkObject



56
57
58
# File 'lib/diakonos/functions/bookmarking.rb', line 56

def toggle_bookmark
  buffer_current.toggle_bookmark
end

#toggle_macro_recording(name = nil) ⇒ Object Also known as: toggleMacroRecording

Starts or stops macro recording.



275
276
277
278
279
280
281
# File 'lib/diakonos/functions.rb', line 275

def toggle_macro_recording( name = nil )
  if @macro_history
    stop_recording_macro
  else
    start_recording_macro name
  end
end

#toggle_selectionObject Also known as: toggleSelection

If currently selecting, stops selecting. If not currently selecting, begins selecting.



69
70
71
72
# File 'lib/diakonos/functions/selection.rb', line 69

def toggle_selection
  buffer_current.toggle_selection
  update_status_line
end

#toggle_session_setting(key_ = nil, do_redraw = DONT_REDRAW) ⇒ Object Also known as: toggleSessionSetting



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/diakonos/functions/sessions.rb', line 59

def toggle_session_setting( key_ = nil, do_redraw = DONT_REDRAW )
  key = key_ || get_user_input( "Setting: " )
  return  if key.nil?

  value = nil
  if @session.settings[ key ].class == TrueClass || @session.settings[ key ].class == FalseClass
    value = ! @session.settings[ key ]
  elsif @settings[ key ].class == TrueClass || @settings[ key ].class == FalseClass
    value = ! @settings[ key ]
  end
  if value != nil   # explicitly true or false
    @session.settings[ key ] = value
    merge_session_settings
    redraw  if do_redraw
    set_iline "#{key} = #{value}"
  end
end

#toggleBookmarkObject



73
# File 'lib/diakonos/functions-deprecated.rb', line 73

alias_method :toggleBookmark,         :toggle_bookmark

#uncommentObject



180
181
182
# File 'lib/diakonos/functions/text-manipulation.rb', line 180

def uncomment
  buffer_current.uncomment
end

#undo(buffer = buffer_current) ⇒ Object

Undoes the latest change made to the current buffer, or reopens the file that was just closed.



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/diakonos/functions.rb', line 285

def undo( buffer = buffer_current )
  if @functions_last[-1] == 'close_buffer'
    open_file(
      @buffer_closed.name,
      'cursor' => {
        'row' => @buffer_closed.last_row,
        'col' => @buffer_closed.last_col,
      },
      'display' => {
        'top_line' => @buffer_closed.top_line,
        'left_col' => @buffer_closed.left_column,
      }
    )
  else
    buffer.undo
  end
end

#unindentObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/diakonos/functions/indentation.rb', line 51

def unindent
  if( buffer_current.changing_selection )
    @do_display = false
    mark = buffer_current.selection_mark
    if mark.end_col > 0
      end_row = mark.end_row
    else
      end_row = mark.end_row - 1
    end
    (mark.start_row..end_row).each do |row|
      buffer_current.unindent row, Buffer::DONT_DISPLAY
    end
    @do_display = true
    display_buffer buffer_current
  else
    buffer_current.unindent
  end
end

#unundo(buffer = buffer_current) ⇒ Object

Redoes the latest change undone on the current buffer.



304
305
306
# File 'lib/diakonos/functions.rb', line 304

def unundo( buffer = buffer_current )
  buffer.unundo
end

#wrap_paragraphObject



184
185
186
# File 'lib/diakonos/functions/text-manipulation.rb', line 184

def wrap_paragraph
  buffer_current.wrap_paragraph
end