TeXMaker 是否能够使用为 TeXWorks 制作的 .js 脚本?如果不行,我该如何将 .js 转换为 .tms?

TeXMaker 是否能够使用为 TeXWorks 制作的 .js 脚本?如果不行,我该如何将 .js 转换为 .tms?

我正在尝试执行以下操作:

LaTeX ->(选定文本,按下键盘快捷键)-> \hyperref[LaTeX]{LaTeX}

我不能为此使用用户标签,因为它不能使用两次选择,只能使用一次。我通过编辑为 TeXWorks 提供的简单脚本之一制作了此脚本,该脚本将普通文本转换为粗体文本。

toggleHyperref.js

// TeXworksScript
// Title: Toggle Hyperlink
// Shortcut: Ctrl+Shift+H
// Description: Converts the current selection to be a hyperlink.
// Author: Robo
// Version: 0.1
// Date: 2018-01-06
// Script-Type: standalone
// Context: TeXDocument

function addOrRemove(prefix, midfix, suffix) {
  var txt = TW.target.selection;
  var len = txt.length;
  var wrapped = prefix + txt + midfix + txt + suffix;
  var pos = TW.target.selectionStart;
  if (pos >= prefix.length) {
    TW.target.selectRange(pos - prefix.length, wrapped.length);
    if (TW.target.selection == wrapped) {
      TW.target.insertText(txt);
      TW.target.selectRange(pos - prefix.length, len);
      return;
    }
    TW.target.selectRange(pos, len);
  }
  TW.target.insertText(wrapped);
  TW.target.selectRange(pos + prefix.length, len);
  return;
};

addOrRemove("\\hyperref[", "]{", "}");

作为参考,.tms 脚本(由 TeXMaker 提供)如下所示:

硬字包装_选择_80col.tms

/***************************************************************************
 *   copyright       : (C) 2003-2014 by Pascal Brachet                     *
 *   http://www.xm1math.net/texmaker/                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

function wordwrap( str, width, brk, cut ) {

    brk = brk || '\n';
    width = width || 75;
    cut = cut || false;

    if (!str) { return str; }

    var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

    return str.match( RegExp(regex, 'g') ).join( brk );

}

var textwidth=80;

var txt = TM.selection;
if (txt != "") {
  var pos = TM.selectionStart;
  var wraptext="";
  var lines = txt.split('\n');

  for (i = 0; i < lines.length; ++i) {
  var line = lines[i];
  wraptext+=wordwrap(line, textwidth, '\n')+"\n";
  }
  TM.insertText(wraptext);
  TM.selectRange(pos, wraptext.length);
}

undefined;

谢谢!

答案1

我的问题的答案如下: http://www.xm1math.net/texmaker/doc.html#SECTION45

具体来说,这是:

Remarks:
to use TexWorks scripts, the TW.target prefix must be replaced by TM.

我在查看文档时误读了此行。为了修复它,我将 TW 替换为 TM。现在我正确遵循了说明,它运行得很好。

这是我的新工作脚本的代码:

切换Hyperref.tms

// TeXworksScript
// Title: Toggle Hyperlink
// Shortcut: Ctrl+Shift+H
// Description: Converts the current selection to be a hyperlink.
// Author: Robo
// Version: 0.1
// Date: 2018-01-06
// Script-Type: standalone
// Context: TeXDocument

function addOrRemove(prefix, midfix, suffix) {
  var txt = TM.selection;
  var len = txt.length;
  var wrapped = prefix + txt + midfix + txt + suffix;
  var pos = TM.selectionStart;
  if (pos >= prefix.length) {
    TM.selectRange(pos - prefix.length, wrapped.length);
    if (TM.selection == wrapped) {
      TM.insertText(txt);
      TM.selectRange(pos - prefix.length, len);
      return;
    }
    TM.selectRange(pos, len);
  }
  TM.insertText(wrapped);
  TM.selectRange(pos + prefix.length, len);
  return;
};

addOrRemove("\\hyperref[", "]{", "}");

相关内容