「選択範囲を上下に移動」の版間の差分

提供: MeryWiki
ナビゲーションに移動 検索に移動
MSY-07 (トーク | 投稿記録)
変更履歴: 変更履歴の追加、バージョン表記の追加
MSY-07 (トーク | 投稿記録)
行番号をクリック&ドラッグで行を範囲選択した状態でマクロを実行すると移動する行が1行増える不具合を修正
 
(同じ利用者による、間の6版が非表示)
10行目: 10行目:
== ソースコード ==
== ソースコード ==
=== 選択範囲を上へ移動 ===
=== 選択範囲を上へ移動 ===
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript" copy>
#title="選択範囲を上へ移動"
#title = "選択範囲を上へ移動"


var s = Document.Selection;
var s = document.selection;
var ty = s.GetTopPointY(mePosLogical);
var ty = s.GetTopPointY(mePosLogical);
var by = s.GetBottomPointY(mePosLogical);
var by = s.GetBottomPointY(mePosLogical);
var bx = s.GetBottomPointX(mePosLogical);
// 選択範囲の末尾が行頭にあるときの調整
if (ty != by && bx == 1) {
by--;
}
if (ty > 1) {
if (ty > 1) {
  Redraw = false;
Redraw = false;
  var sx = ScrollX, sy = ScrollY;
var sx = ScrollX;
 
var sy = ScrollY;
  // ブックマークの待避
 
  var bookmarkList = GetBookmarkList(Document, ty-1, by, true);
// ブックマークの待避
 
var bookmarkList = GetBookmarkList(document, ty - 1, by, true);
  // 選択行を上へ移動
 
  SelectLine(s, ty-1, by);
// 選択行を上へ移動
  var a = s.Text.split("\n");
SelectLine(s, ty - 1, by);
  a.push(a.shift());
var a = s.Text.split("\n");
  s.Text = a.join("\n");
a.push(a.shift());
s.Text = a.join("\n");


  // ブックマークの復元
// ブックマークの復元
  for (var i=0; i<bookmarkList.length; i++) {
for (var i = 0; i < bookmarkList.length; i++) {
    var y = bookmarkList[i] - 1;
var y = bookmarkList[i] - 1;
    if (y == ty-2) {
if (y == ty - 2) {
      y = by;
y = by;
    }
}
    s.SetActivePoint(mePosLogical, 1, y);
s.SetActivePoint(mePosLogical, 1, y);
    s.SetBookmark();
s.SetBookmark();
  }
}


  // 選択の復元
// 選択の復元
  ScrollX = sx;
ScrollX = sx;
  ScrollY = sy;
ScrollY = sy;
  SelectLine(s, ty-1, by-1);
SelectLine(s, ty - 1, by - 1);
  Redraw = true;
Redraw = true;
}
}


function SelectLine(s, ty, by) {
function SelectLine(s, ty, by) {
  s.SetAnchorPoint(mePosLogical, 1, ty);
s.SetAnchorPoint(mePosLogical, 1, ty);
  s.SetActivePoint(mePosLogical, 1, by, true);
s.SetActivePoint(mePosLogical, 1, by, true);
  s.EndOfLine(true, mePosLogical);
s.EndOfLine(true, mePosLogical);
}
}


55行目: 63行目:
// キャレットおよびスクロール位置が変更されるため必要なら待避
// キャレットおよびスクロール位置が変更されるため必要なら待避
function GetBookmarkList(doc, top, bottom, isClear) {
function GetBookmarkList(doc, top, bottom, isClear) {
  var lines = doc.GetLines(0);
var lines = doc.GetLines(0);
  top = (top == null || top < 1) ? 1 : top;
top = (top == null || top < 1) ? 1 : top;
  bottom = (bottom == null || bottom > lines) ? lines : bottom;
bottom = (bottom == null || bottom > lines) ? lines : bottom;
  var s = doc.Selection;
var s = doc.selection;
  var b = [];
var b = [];
  if (lines == 1) {
if (lines == 1) {
    s.SetActivePoint(mePosLogical, 2, 1);
s.SetActivePoint(mePosLogical, 2, 1);
    if (s.PreviousBookmark()) {
if (s.PreviousBookmark()) {
      b.push(1);
b.push(1);
    }
}
    return b;
return b;
  }
}
  if (top == 1) {
if (top == 1) {
    s.SetActivePoint(mePosLogical, 1, 2);
s.SetActivePoint(mePosLogical, 1, 2);
    if (s.PreviousBookmark()) {
if (s.PreviousBookmark()) {
      b.push(1);
b.push(1);
    }
}
  }
}
  s.SetActivePoint(mePosLogical, 1, top-1);
s.SetActivePoint(mePosLogical, 1, top - 1);
  while (s.NextBookmark()) {
while (s.NextBookmark()) {
    var y = s.GetActivePointY(mePosLogical);
var y = s.GetActivePointY(mePosLogical);
    if (y > bottom) {
if (y > bottom) {
      break;
break;
    }
}
    b.push(y);
b.push(y);
    if (isClear) {
if (isClear) {
      s.ClearBookmark();
s.ClearBookmark();
    }
}
  }
}
  return b;
return b;
}
}
</syntaxhighlight>
</syntaxhighlight>


=== 選択範囲を下へ移動 ===
=== 選択範囲を下へ移動 ===
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript" copy>
#title="選択範囲を下へ移動"
#title = "選択範囲を下へ移動"


var s = Document.Selection;
var s = document.selection;
var ty = s.GetTopPointY(mePosLogical);
var ty = s.GetTopPointY(mePosLogical);
var by = s.GetBottomPointY(mePosLogical);
var by = s.GetBottomPointY(mePosLogical);
if (Document.GetLines(0) > by) {
var bx = s.GetBottomPointX(mePosLogical);
  Redraw = false;
 
  var sx = ScrollX, sy = ScrollY;
// 選択範囲の末尾が行頭にあるときの調整
 
if (ty != by && bx == 1) {
  // ブックマークの待避
by--;
  var bookmarkList = GetBookmarkList(Document, ty, by+1, true);
}
 
 
  // 選択行を下へ移動
if (document.GetLines(0) > by) {
  SelectLine(s, ty, by+1);
Redraw = false;
  var a = s.Text.split("\n");
var sx = ScrollX;
  a.unshift(a.pop());
var sy = ScrollY;
  s.Text = a.join("\n");
 
// ブックマークの待避
var bookmarkList = GetBookmarkList(document, ty, by + 1, true);
 
// 選択行を下へ移動
SelectLine(s, ty, by + 1);
var a = s.Text.split("\n");
a.unshift(a.pop());
s.Text = a.join("\n");


  // ブックマークの復元
// ブックマークの復元
  for (var i=0; i<bookmarkList.length; i++) {
for (var i = 0; i < bookmarkList.length; i++) {
    var y = bookmarkList[i] + 1;
var y = bookmarkList[i] + 1;
    if (y == by+2) {
if (y == by + 2) {
      y = ty;
y = ty;
    }
}
    s.SetActivePoint(mePosLogical, 1, y);
s.SetActivePoint(mePosLogical, 1, y);
    s.SetBookmark();
s.SetBookmark();
  }
}


  // 選択の復元
// 選択の復元
  ScrollX = sx;
ScrollX = sx;
  ScrollY = sy;
ScrollY = sy;
  SelectLine(s, ty+1, by+1);
SelectLine(s, ty + 1, by + 1);
  Redraw = true;
Redraw = true;
}
}


function SelectLine(s, ty, by) {
function SelectLine(s, ty, by) {
  s.SetAnchorPoint(mePosLogical, 1, ty);
s.SetAnchorPoint(mePosLogical, 1, ty);
  s.SetActivePoint(mePosLogical, 1, by, true);
s.SetActivePoint(mePosLogical, 1, by, true);
  s.EndOfLine(true, mePosLogical);
s.EndOfLine(true, mePosLogical);
}
}


134行目: 150行目:
// キャレットおよびスクロール位置が変更されるため必要なら待避
// キャレットおよびスクロール位置が変更されるため必要なら待避
function GetBookmarkList(doc, top, bottom, isClear) {
function GetBookmarkList(doc, top, bottom, isClear) {
  var lines = doc.GetLines(0);
var lines = doc.GetLines(0);
  top = (top == null || top < 1) ? 1 : top;
top = (top == null || top < 1) ? 1 : top;
  bottom = (bottom == null || bottom > lines) ? lines : bottom;
bottom = (bottom == null || bottom > lines) ? lines : bottom;
  var s = doc.Selection;
var s = doc.selection;
  var b = [];
var b = [];
  if (lines == 1) {
if (lines == 1) {
    s.SetActivePoint(mePosLogical, 2, 1);
s.SetActivePoint(mePosLogical, 2, 1);
    if (s.PreviousBookmark()) {
if (s.PreviousBookmark()) {
      b.push(1);
b.push(1);
    }
}
    return b;
return b;
  }
}
  if (top == 1) {
if (top == 1) {
    s.SetActivePoint(mePosLogical, 1, 2);
s.SetActivePoint(mePosLogical, 1, 2);
    if (s.PreviousBookmark()) {
if (s.PreviousBookmark()) {
      b.push(1);
b.push(1);
    }
}
  }
}
  s.SetActivePoint(mePosLogical, 1, top-1);
s.SetActivePoint(mePosLogical, 1, top - 1);
  while (s.NextBookmark()) {
while (s.NextBookmark()) {
    var y = s.GetActivePointY(mePosLogical);
var y = s.GetActivePointY(mePosLogical);
    if (y > bottom) {
if (y > bottom) {
      break;
break;
    }
}
    b.push(y);
b.push(y);
    if (isClear) {
if (isClear) {
      s.ClearBookmark();
s.ClearBookmark();
    }
}
  }
}
  return b;
return b;
}
}
</syntaxhighlight>
</syntaxhighlight>

2025年9月1日 (月) 04:42時点における最新版

概要[編集]

選択されている範囲を1行上下します。

変更履歴[編集]

  • 1.0.1 (2013-04-09)
    • ブックマークがずれるのを修正
  • 1.0.0 (2013-03-15)
    • 初版

ソースコード[編集]

選択範囲を上へ移動[編集]

#title = "選択範囲を上へ移動"

var s = document.selection;
var ty = s.GetTopPointY(mePosLogical);
var by = s.GetBottomPointY(mePosLogical);
var bx = s.GetBottomPointX(mePosLogical);

// 選択範囲の末尾が行頭にあるときの調整
if (ty != by && bx == 1) {
	by--;
}

if (ty > 1) {
	Redraw = false;
	var sx = ScrollX;
	var sy = ScrollY;

	// ブックマークの待避
	var bookmarkList = GetBookmarkList(document, ty - 1, by, true);

	// 選択行を上へ移動
	SelectLine(s, ty - 1, by);
	var a = s.Text.split("\n");
	a.push(a.shift());
	s.Text = a.join("\n");

	// ブックマークの復元
	for (var i = 0; i < bookmarkList.length; i++) {
		var y = bookmarkList[i] - 1;
		if (y == ty - 2) {
			y = by;
		}
		s.SetActivePoint(mePosLogical, 1, y);
		s.SetBookmark();
	}

	// 選択の復元
	ScrollX = sx;
	ScrollY = sy;
	SelectLine(s, ty - 1, by - 1);
	Redraw = true;
}

function SelectLine(s, ty, by) {
	s.SetAnchorPoint(mePosLogical, 1, ty);
	s.SetActivePoint(mePosLogical, 1, by, true);
	s.EndOfLine(true, mePosLogical);
}

// ブックマークの一覧を論理行単位で取得
// キャレットおよびスクロール位置が変更されるため必要なら待避
function GetBookmarkList(doc, top, bottom, isClear) {
	var lines = doc.GetLines(0);
	top = (top == null || top < 1) ? 1 : top;
	bottom = (bottom == null || bottom > lines) ? lines : bottom;
	var s = doc.selection;
	var b = [];
	if (lines == 1) {
		s.SetActivePoint(mePosLogical, 2, 1);
		if (s.PreviousBookmark()) {
			b.push(1);
		}
		return b;
	}
	if (top == 1) {
		s.SetActivePoint(mePosLogical, 1, 2);
		if (s.PreviousBookmark()) {
			b.push(1);
		}
	}
	s.SetActivePoint(mePosLogical, 1, top - 1);
	while (s.NextBookmark()) {
		var y = s.GetActivePointY(mePosLogical);
		if (y > bottom) {
			break;
		}
		b.push(y);
		if (isClear) {
			s.ClearBookmark();
		}
	}
	return b;
}

選択範囲を下へ移動[編集]

#title = "選択範囲を下へ移動"

var s = document.selection;
var ty = s.GetTopPointY(mePosLogical);
var by = s.GetBottomPointY(mePosLogical);
var bx = s.GetBottomPointX(mePosLogical);

// 選択範囲の末尾が行頭にあるときの調整
if (ty != by && bx == 1) {
	by--;
}

if (document.GetLines(0) > by) {
	Redraw = false;
	var sx = ScrollX;
	var sy = ScrollY;

	// ブックマークの待避
	var bookmarkList = GetBookmarkList(document, ty, by + 1, true);

	// 選択行を下へ移動
	SelectLine(s, ty, by + 1);
	var a = s.Text.split("\n");
	a.unshift(a.pop());
	s.Text = a.join("\n");

	// ブックマークの復元
	for (var i = 0; i < bookmarkList.length; i++) {
		var y = bookmarkList[i] + 1;
		if (y == by + 2) {
			y = ty;
		}
		s.SetActivePoint(mePosLogical, 1, y);
		s.SetBookmark();
	}

	// 選択の復元
	ScrollX = sx;
	ScrollY = sy;
	SelectLine(s, ty + 1, by + 1);
	Redraw = true;
}

function SelectLine(s, ty, by) {
	s.SetAnchorPoint(mePosLogical, 1, ty);
	s.SetActivePoint(mePosLogical, 1, by, true);
	s.EndOfLine(true, mePosLogical);
}

// ブックマークの一覧を論理行単位で取得
// キャレットおよびスクロール位置が変更されるため必要なら待避
function GetBookmarkList(doc, top, bottom, isClear) {
	var lines = doc.GetLines(0);
	top = (top == null || top < 1) ? 1 : top;
	bottom = (bottom == null || bottom > lines) ? lines : bottom;
	var s = doc.selection;
	var b = [];
	if (lines == 1) {
		s.SetActivePoint(mePosLogical, 2, 1);
		if (s.PreviousBookmark()) {
			b.push(1);
		}
		return b;
	}
	if (top == 1) {
		s.SetActivePoint(mePosLogical, 1, 2);
		if (s.PreviousBookmark()) {
			b.push(1);
		}
	}
	s.SetActivePoint(mePosLogical, 1, top - 1);
	while (s.NextBookmark()) {
		var y = s.GetActivePointY(mePosLogical);
		if (y > bottom) {
			break;
		}
		b.push(y);
		if (isClear) {
			s.ClearBookmark();
		}
	}
	return b;
}
スポンサーリンク