<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link href="style.css" rel="stylesheet" type="text/css">
<title>4.6.17. Modify selected text and save to file</title>
<script src="show_matches.js" type="text/javascript"></script>
</HEAD>
<BODY>
<h3>4.6.17. Modify selected text and save to file</h3>

<p><b>Question:</b></p>
<p>I know that SEL_SAVETO will save the selected text to target file, but how to modify the selected text such as add one blank line and then save to target file?</p>

<p><b>Answer, option one:</b></p>
<p>I'd say the correct answer is to change the order of these operations:</p>
<ol>
  <li>save the selected text via SEL_SAVETO;</li>
  <li>modify the saved file by some external tool.</li>
</ol>
<p>For example, sfk (Swiss File Knife) supports a lot of ways to modify a file, and, moreover, it supports commands chaining (i.e. it's possible to apply one modification, then apply another one to the result and so on), plus now it does support UTF-8 and UCS-2 files. So I'd recommend to try to use sfk for any additional file modifications.</p>
<p>Here are a few examples of how to append/insert text lines by means of sfk:</p>
<ul>
  <li>To append several lines similar to
    <blockquote>
      <pre>cmd /c echo trailing line1&gt;&gt;myfile.txt
cmd /c echo trailing line2&gt;&gt;myfile.txt</pre>
    </blockquote>
    use the following:
    <blockquote>
      <pre>sfk echo -spat &quot;trailing line1\ntrailing line2&quot; +append myfile.txt</pre>
    </blockquote>
  </li>
  <li>To insert some text before line 5:
    <blockquote>
      <pre>sfk filt -lnum myfile.txt +xed &quot;/[lstart]005 /005 text to insert\n[all]/&quot; +xed &quot;_[lstart]* __&quot; -tofile out.txt</pre>
    </blockquote>
  </li>
  <li>To insert after a specific string (e.g. insert after a line that contains a string &quot;insert something after me&quot;):
    <blockquote>
      <pre>sfk xed myfile.txt &quot;/[lstart]*insert something after me*[eol]/[all]the new text\n/&quot; -tofile out.txt</pre>
    </blockquote>
  </li>
</ul>

<p><b>Answer, option two:</b></p>
<p>It's possible to modify the original text by means of <code>SCI_</code> commands, save the modified text and then undo the changes.</p>
<p>For example, we can append a string in the following way (this example works with the entire document rather than with the selected text):</p>

<blockquote>
  <pre>set local str ~ strfromhex 0D 00 0A 00 &quot;Some string&quot; // first 4 bytes are \r\n
set local str_len ~ strlensci $(str)
sci_sendmsg SCI_APPENDTEXT $(str_len) &quot;$(str)&quot; // append a string to the end of document
text_saveto $(SYS.TEMP)\out.txt // save to the output file
sci_sendmsg SCI_UNDO // undo</pre>
</blockquote>

<p>Here is an example that appends a string to the existing selection, saves it to a file and then reverts the text (and selection) to the previous state:</p>

<blockquote>
  <pre>// getting the current selection
sci_sendmsg SCI_GETCURRENTPOS
set local pos = $(MSG_RESULT)
sci_sendmsg SCI_GETANCHOR
set local anchor = $(MSG_RESULT)

if $(pos) &lt; $(anchor) then
  set local pos1 = $(pos)
  set local pos2 = $(anchor)
else
  set local pos1 = $(anchor)
  set local pos2 = $(pos)
endif

// appending a string to the selection 
set local str ~ strfromhex 0D 00 0A 00 &quot;Some string&quot; // first 4 bytes are \r\n
set local str_len ~ strlensci $(str)
sci_sendmsg SCI_INSERTTEXT $(pos2) &quot;$(str)&quot; // append a string to the end of selection
set local pos2 ~ $(pos2) + $(str_len)
sci_sendmsg SCI_SETSEL $(pos1) $(pos2) // update the selection
sel_saveto $(SYS.TEMP)\out.txt // save to the output file
sci_sendmsg SCI_UNDO // undo

// restoring the original selection
sci_sendmsg SCI_SETSEL $(anchor) $(pos)</pre>
</blockquote>

<blockquote>
  <pre> </pre>
</blockquote>

<p>See also: Applying external tool to selected text <a href="4.6.1.html">[4.6.1]</a>; Processing &amp; replacing the selection <a href="4.6.3.html">[4.6.3]</a>.</p>

<blockquote>
  <pre> </pre>
</blockquote>

</BODY>
</HTML>
