<commit_before>// Time-stamp: "2018-04-28 10:58:08 phil"
// Section 0x is for common (global) settings (?)

/*
 * When debugging your rc file remember you can use the dumpln function to print
   arbitrary strings. Also remember that if you execute conkeror from a command
   line, it will show you any syntax or other errors in your rc file.
 */

dumpln("00common");

// Add .conkerorrc/modules/ to load_path before anything else
var ph_modules_directory=get_home_directory().clone();
ph_modules_directory.appendRelativePath(".conkerorrc");
ph_modules_directory.appendRelativePath("modules");
// "unshift" means "push" wtf
load_paths.unshift(make_uri(ph_modules_directory).spec);

//
// Utilities
//

// function shell_command_output (command) {
//     var error_text = "";
//     var output_text = "";
//     var exit_status = shell_command(
//         command,
//         $fds = [
//             {
//                 output:
//                 async_binary_string_writer("")
//             },
//             {
//                 input:
//                 async_binary_reader(
//                     function (s) {
//                         output_text += s || "";
//                     }
//                 )
//             },
//             {
//                 input:
//                 async_binary_reader(
//                     function (s) {
//                         error_text += s || "";
//                     }
//                 )
//             }
//         ]
//     );
//     if (exit_status != 0 ) {
//         throw new Error("exit_status: " + exit_status + ", error_text: " +
//                         error_text);
//     }
//     return output_text.trim();
// }

function wordwrap(str, int_width, str_break, cut) {
  //  discuss at: http://phpjs.org/functions/wordwrap/
  // original by: <NAME> (http://www.jsfromhell.com)
  // improved by: <NAME>
  // improved by: <NAME> (http://kevin.vanzonneveld.net)
  // improved by: Sakimori
  //  revised by: <NAME> (http://www.jsfromhell.com)
  // bugfixed by: <NAME>
  // bugfixed by: <NAME>
  //   example 1: wordwrap('<NAME>', 6, '|', true);
  //   returns 1: 'Kevin |van |Zonnev|eld'
  //   example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n');
  //   returns 2: 'The quick brown fox <br />\njumped over the lazy<br />\n dog.'
  //   example 3: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
  //   returns 3: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \ncommodo consequat.'

  var m = ((arguments.length >= 2) ? arguments[1] : 75);
  var b = ((arguments.length >= 3) ? arguments[2] : '\n');
  var c = ((arguments.length >= 4) ? arguments[3] : false);

  var i, j, l, s, r;

  str += '';

  if (m < 1) {
    return str;
  }

  for (i = -1, l = (r = str.split(/\r\n|\n|\r/))
    .length; ++i < l; r[i] += s) {
    for (s = r[i], r[i] = ''; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j))
        .length ? b : '')) {
      j = c == 2 || (j = s.slice(0, m + 1)
        .match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(
          m)
        .match(/^\S*/))[0].length;
    }
  }

  return r.join('\n');
}

function ph_where_editors_executable (executable) {
    const path_to_executable = get_home_directory().clone();
    path_to_executable.appendRelativePath("editors");
    path_to_executable.appendRelativePath(executable);
    return path_to_executable.path;
}

function ph_shell_escape (text) {
    return new String(text).
        replace(/'/g, "′"). // PRIME ′. Not RIGHT SINGLE QUOTATION MARK ’.
        replace(/!/g, "¡"). // INVERTED EXCLAMATION MARK
        replace(/\*/g, "⋆"). // STAR OPERATOR ⋆. WHITE SMALL STAR ⭒ or
        // BLACK SMALL STAR ⭑ or SMALL ASTERISK ﹡ or ASTERISK OPERATOR ∗.
        replace(/\(/g, "﹙"). // SMALL LEFT PARENTHESIS ﹙
        replace(/\)/g, "﹚"); // SMALL RIGHT PARENTHESIS ﹚
}

// FIXME See:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter
function ph_shell_escaper(matched_text, offset, whole) {
    var m;

    if (matched_text == "\'") {
        return "’";
    } else if (matched_text == "'") {
        return "’";
    } else if ((m = matched_text.match(/(.)'/))) {
        return m[1] + "’";
    } else {
        return matched_text;
    }
}

// TODO Merge with implementation above
// function ph_shell_escape(string) {
//   return string.replace("\n", " ").replace(/.?'/g, ph_shell_escaper);
// }

// Test:
// ph_shell_escape("http://some.uri.with/pagetitle/containing'character");

function ph_encodeURIComponent (text) {
    return encodeURIComponent(ph_shell_escape(text));
}

/**
 * setenv sets the value of a named environment variable to a given string.
 */
require("env");
let (env = Cc['@mozilla.org/process/environment;1']
         .getService(Ci.nsIEnvironment)) {
    function setenv (variable, value) {
        env.set(variable, value);
    }
}

// Minibuffer message in one line
function ph_safe_message (window, message) {
    (window || get_recent_conkeror_window()).minibuffer.message(message);
}

function ph_X11_activate (window_title_regexp) {
    shell_command_blind(
        "xdotool search --onlyvisible --name '" + window_title_regexp + "'"
            + " windowactivate"
    );
} // TODO Interactive form

function blocking_pause (milliseconds) {
    const dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

// TODO Verify that there isn't a simpler/standard way to do this
function ph_count_conkeror_windows () {
    var window_count = 0;
    const en = window_watcher.getWindowEnumerator();
    while (en.hasMoreElements()) {
        const ignored = en.getNext();
        window_count++;
    }
    return window_count;
}


interactive("load-file",
           "(Re-)load an initialization file",
           function (I) {
               config_dir = get_home_directory().clone();
               config_dir.appendRelativePath(".conkerorrc");
               filename = (yield I.minibuffer.read($prompt = "Load file: ",
                                                   $initial_value = config_dir.path + "/",
                                                   $history = "load_file"));
               I.window.minibuffer.message("Loaded file "+filename);
               load(make_file(filename));
           }
          );

// The default page for new buffers.
homepage = "about:blank";

// default directory for downloads and shell commands.
cwd = get_home_directory().clone();
cwd.append("Desktop");

// Open the download manager buffer in the current frame, or in a new window
// with universal argument.
download_buffer_automatic_open_target = [OPEN_NEW_BUFFER, OPEN_NEW_WINDOW];

// save a keystroke when selecting a dom node by number.
hints_auto_exit_delay = 500;
hints_ambiguous_auto_exit_delay = 500;

// Open each externally-requested URL in a new buffer
url_remoting_fn = load_url_in_new_buffer;

// Scroll each isearch match to the frame's vertical center, not bottom<commit_msg>ph_X11_activate now uses shell_command_with_argument_blind.<commit_after>// Time-stamp: "2019-01-20 12:19:21 phil"
// Section 0x is for common (global) settings (?)

/*
 * When debugging your rc file remember you can use the dumpln function to print
   arbitrary strings. Also remember that if you execute conkeror from a command
   line, it will show you any syntax or other errors in your rc file.
 */

dumpln("00common");

// Add .conkerorrc/modules/ to load_path before anything else
var ph_modules_directory=get_home_directory().clone();
ph_modules_directory.appendRelativePath(".conkerorrc");
ph_modules_directory.appendRelativePath("modules");
// "unshift" means "push" wtf
load_paths.unshift(make_uri(ph_modules_directory).spec);

//
// Utilities
//

// function shell_command_output (command) {
//     var error_text = "";
//     var output_text = "";
//     var exit_status = shell_command(
//         command,
//         $fds = [
//             {
//                 output:
//                 async_binary_string_writer("")
//             },
//             {
//                 input:
//                 async_binary_reader(
//                     function (s) {
//                         output_text += s || "";
//                     }
//                 )
//             },
//             {
//                 input:
//                 async_binary_reader(
//                     function (s) {
//                         error_text += s || "";
//                     }
//                 )
//             }
//         ]
//     );
//     if (exit_status != 0 ) {
//         throw new Error("exit_status: " + exit_status + ", error_text: " +
//                         error_text);
//     }
//     return output_text.trim();
// }

function wordwrap(str, int_width, str_break, cut) {
  //  discuss at: http://phpjs.org/functions/wordwrap/
  // original by: <NAME> (http://www.jsfromhell.com)
  // improved by: <NAME>
  // improved by: <NAME> (http://kevin.vanzonneveld.net)
  // improved by: Sakimori
  //  revised by: <NAME> (http://www.jsfromhell.com)
  // bugfixed by: <NAME>
  // bugfixed by: <NAME>
  //   example 1: wordwrap('<NAME>', 6, '|', true);
  //   returns 1: 'Kevin |van |Zonnev|eld'
  //   example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n');
  //   returns 2: 'The quick brown fox <br />\njumped over the lazy<br />\n dog.'
  //   example 3: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
  //   returns 3: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \ncommodo consequat.'

  var m = ((arguments.length >= 2) ? arguments[1] : 75);
  var b = ((arguments.length >= 3) ? arguments[2] : '\n');
  var c = ((arguments.length >= 4) ? arguments[3] : false);

  var i, j, l, s, r;

  str += '';

  if (m < 1) {
    return str;
  }

  for (i = -1, l = (r = str.split(/\r\n|\n|\r/))
    .length; ++i < l; r[i] += s) {
    for (s = r[i], r[i] = ''; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j))
        .length ? b : '')) {
      j = c == 2 || (j = s.slice(0, m + 1)
        .match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(
          m)
        .match(/^\S*/))[0].length;
    }
  }

  return r.join('\n');
}

function ph_where_editors_executable (executable) {
    const path_to_executable = get_home_directory().clone();
    path_to_executable.appendRelativePath("editors");
    path_to_executable.appendRelativePath(executable);
    return path_to_executable.path;
}

function ph_shell_escape (text) {
    return new String(text).
        replace(/'/g, "′"). // PRIME ′. Not RIGHT SINGLE QUOTATION MARK ’.
        replace(/!/g, "¡"). // INVERTED EXCLAMATION MARK
        replace(/\*/g, "⋆"). // STAR OPERATOR ⋆. WHITE SMALL STAR ⭒ or
        // BLACK SMALL STAR ⭑ or SMALL ASTERISK ﹡ or ASTERISK OPERATOR ∗.
        replace(/\(/g, "﹙"). // SMALL LEFT PARENTHESIS ﹙
        replace(/\)/g, "﹚"); // SMALL RIGHT PARENTHESIS ﹚
}

// FIXME See:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter
function ph_shell_escaper(matched_text, offset, whole) {
    var m;

    if (matched_text == "\'") {
        return "’";
    } else if (matched_text == "'") {
        return "’";
    } else if ((m = matched_text.match(/(.)'/))) {
        return m[1] + "’";
    } else {
        return matched_text;
    }
}

// TODO Merge with implementation above
// function ph_shell_escape(string) {
//   return string.replace("\n", " ").replace(/.?'/g, ph_shell_escaper);
// }

// Test:
// ph_shell_escape("http://some.uri.with/pagetitle/containing'character");

function ph_encodeURIComponent (text) {
    return encodeURIComponent(ph_shell_escape(text));
}

/**
 * setenv sets the value of a named environment variable to a given string.
 */
require("env");
let (env = Cc['@mozilla.org/process/environment;1']
         .getService(Ci.nsIEnvironment)) {
    function setenv (variable, value) {
        env.set(variable, value);
    }
}

// Minibuffer message in one line
function ph_safe_message (window, message) {
    (window || get_recent_conkeror_window()).minibuffer.message(message);
}

function ph_X11_activate (window_title_regexp) {
    shell_command_with_argument_blind(
        "xdotool search --onlyvisible --name '{}' windowactivate",
        window_title_regexp
    );
} // TODO Interactive form

function blocking_pause (milliseconds) {
    const dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

// TODO Verify that there isn't a simpler/standard way to do this
function ph_count_conkeror_windows () {
    var window_count = 0;
    const en = window_watcher.getWindowEnumerator();
    while (en.hasMoreElements()) {
        const ignored = en.getNext();
        window_count++;
    }
    return window_count;
}


interactive("load-file",
           "(Re-)load an initialization file",
           function (I) {
               config_dir = get_home_directory().clone();
               config_dir.appendRelativePath(".conkerorrc");
               filename = (yield I.minibuffer.read($prompt = "Load file: ",
                                                   $initial_value = `${config_dir.path}/`,
                                                   $history = "load_file"));
               I.window.minibuffer.message(`Loaded file ${filename}`);
               load(make_file(filename));
           }
          );

// The default page for new buffers.
homepage = "about:blank";

// default directory for downloads and shell commands.
cwd = get_home_directory().clone();
cwd.append("Desktop");

// Open the download manager buffer in the current frame, or in a new window
// with universal argument.
download_buffer_automatic_open_target = [OPEN_NEW_BUFFER, OPEN_NEW_WINDOW];

// save a keystroke when selecting a dom node by number.
hints_auto_exit_delay = 500;
hints_ambiguous_auto_exit_delay = 500;

// Open each externally-requested URL in a new buffer
url_remoting_fn = load_url_in_new_buffer;

// Scroll each isearch match to the frame's vertical center, not bottom
{'fraction_non_alphanumeric': 0.14569225611800202, 'fraction_numerical': 0.007643312101910828, 'mean_word_length': 2.3324396782841825, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2035198', 'n_tokens_mistral': 4922, 'n_tokens_neox': 4735, 'n_words': 1240}