Reformatting text (or, better, LaTeX) in 80 colums in SciTE - word-wrap

I recently dived into LaTeX, starting with the help of a WYSIWYM editor like Lix. Now I'm staring writing tex files in Sci-TE, It already has syntax higlighting and I adapted the tex.properties file to work in Windows showing a preview on Go [F5]
One pretty thing Lyx does, and it's hard to acheive with a common text editor, is to format text in 80 columns: I can write a paragraph and hit Return each time I reach near the edge column but if, after the first draft, I want to add or cut some words here and there I end up breaking the layout and having to rearrange newlines.
It would be useful to have a tool in Sci-TE so I can select a paragraph of text I added or deleted some words in and have it rearranged in 80 columns. Probably not something working on the whole document since it could probably break some intended anticipated line break.
Probably I could easily write a Python plugin for geany, I saw vim has something similar, but I'd like to know if its' possible in Sci-TE too.

I was a bit disappointed when I found no answer as I was searching for same. No helpers by Google either, so I searched for Lua examples and syntax in a hope to craft it myself. I don't know Lua so this can perhaps be made differently or efficiently but its better then nothing I hope - here is Lua function which needs to be put in SciTE start-up Lua script:
function wrap_text()
local border = 80
local t = {}
local pos = editor.SelectionStart
local sel = editor:GetSelText()
if #sel == 0 then return end
local para = {}
local function helper(line) table.insert(para, line) return "" end
helper((sel:gsub("(.-)\r?\n", helper)))
for k, v in pairs(para) do
line = ""
for token in string.gmatch(v, "[^%s]+") do
if string.len(token .. line) >= border then
t[#t + 1] = line
line = token .. " "
else
line = line .. token .. " "
end
end
t[#t + 1] = line:gsub("%s$", "")
end
editor:ReplaceSel(table.concat(t, "\n"))
editor:GotoPos(pos)
end
Usage is like any other function from start-up script, but for completness I'll paste my tool definition from SciTE properties file:
command.name.8.*=Wrap Text
command.mode.8.*=subsystem:lua,savebefore:no,groupundo
command.8.*=wrap_text
command.replace.selection.8.*=2
It does respect paragraphs, so it can be used on broader selection, not just one paragraph.

This is one way to do it in scite: first, add this to your .SciTEUser.properties (Options/Open User Options file):
# Column guide, indicates long lines (https://wiki.archlinux.org/index.php/SciTE)
# this is what they call "margin line" in gedit (at right),
# in scite, "margin" is the area on left for line numbers
edge.mode=1
edge.column=80
... and save, so you can see a line at 80 characters.
Then scale the scite window, so the text you see is wrapped at the line.
Finally, select the long line text which is to be broken into lines, and do Edit / Paragraph / Split (for me the shortcut Ctrl-K also works for that).
Unfortunately, there seems to be no "break-lines-as-you-type" facility in scite, like the "Line Breaking" facility in geany. not anymore, now there's a plugin - see this answer

Well, I was rather disappointed that there seems to be no "break-lines-as-you-type" facility in scite; and I finally managed to code a small Lua plugin/add-on/extension for that, and released it here:
lua-users wiki: Scite Line Break
Installation and usage instructions are in the script itself. Here is how SciTE may look when the extension properly installed, and toggle activated after startup:
Note that it's pretty much the same functionality as in geany - it inserts linebreaks upon typing text - but not on pressing backspace, nor upon copy/pasting.

the same but more easy, I think...
put this in the user properties:
command.name.0.*=swrap
command.0.*=fold -s $(FileNameExt) > /tmp/scite_temp ; cat /tmp/scite_temp >$(FileNameExt)
command.is.filter.0.*=1
Ciao
Pietro

Related

MigraDoc - extra and unwanted NewLines

I have a code, where I create a paragraph with 4 lines, 3 of them use tabs to separate row labels and parameter. Astonishingly, a code that worked flawlessly in another form is giving me big headache in current form, even though it is practically 100% identical. Two extra lines appear between line label and a parameter.
Dim ParPara As New Paragraph ' create new paragraph
Dim formtext As New FormattedText
formtext.Bold = True
formtext.AddText("ABC 12345")
Dim formtext2 As New FormattedText
formtext2.Bold = True
formtext2.AddText("163658468435831484")
Dim formtext3 As New FormattedText
formtext3.Bold = True
formtext3.AddText("PARAMETER 1")
ParPara.Add(formtext4)
ParPara.AddLineBreak() ' intentional line break at the end of line
ParPara.AddText("Row Header 1:")
ParPara.AddTab()
ParPara.Add(formtext)
ParPara.AddLineBreak() ' intentional line break at the end of line
ParPara.AddText("Row Header 2:")
ParPara.AddTab()
ParPara.Add(formtext2)
ParPara.AddLineBreak() ' intentional line break at the end of line
ParPara.AddText("Row Header 3:")
ParPara.AddTab()
ParPara.Add(formtext3)
...
TableRowHead0F.Cells(1).Add(ParPara)
I even compared in-memmory content of the paragraph and it's 100% identical in both forms, one working flawlessly and another one inserting extra NewLine breaks:
The in-memmory structure is correct:
The migradoc styles are set identically in both forms (working and failing) too.
Anyone has any ideas why this is happenning?
EDIT: As suggested, I compared MDDDLs and I see no differences:
I don't know, if there can be some context (cell style?) affecting this, but it's really odd. Not the first time I see something like this with MigraDoc...
EDIT 2:
The problems was in switching an order of lines of definition of one style, I had 2nd line of this code at the end:
Dim sStyle As Style = PruvDoc.Styles(StyleNames.Normal)
sStyle = PruvDoc.Styles.AddStyle("Quest", "Normal")
sStyle.ParagraphFormat.TabStops.Clear()
sStyle.ParagraphFormat.TabStops.AddTabStop("9cm", TabAlignment.Left, TabLeader.Dots)
...and that meant that tab stops were applied to the "Normal" style, instead "Quest". Against expectation, it was defined well after the part, where the problems ocured(good to know).
A useful approach to investigate such problems: You can create MDDDL files for the version that works and for the version that does not work and then compare the MDDDL files.
See here:
http://pdfsharp.net/wiki/MigraDocDDL.ashx
MDDDL files are somewhat readable and can be used to check what you actually add to your document - even if you only have a version that does not work as expected.

Paste from a file one line (or section) at a time

I am a teacher using Windows and would like to be able to paste short program snippets one after another from a file of examples I have into whatever programming environment I am teaching (e.g. the python IDLE shell or editor). During the lecture I would have IDLE open and then use Ctrl-v to paste line 1 from the file into IDLE, execute & discuss it, then use Ctrl-v to paste line 2 from the file into IDLE, execute & discuss it, then use Ctrl-V to get line 3 into IDLE, and so on ...
I suspect there is some way to do this with a clipboard manager, but haven't found it online.
Being able to paste sections of code instead of just single lines would be really useful as well. The sections of code in the file could be separated by a blank line or some kind of text string indicator.
Having this functionality would allow me to have all my examples ready in a file and then during the lecture have quick access to all the examples one at a time by using Ctrl-v.
The following AutoHotKey script will paste lines from the clipboard, one line at a time, when you press Win+Ctrl+V (on Windows).
If you haven't used AutoHotKey, I highly recommend it.
#^v::
{
originalClipboard := Clipboard
StringSplit, ClipLines, originalClipboard, `n`r
size := StrLen(ClipLines1) + 3
Clipboard = %ClipLines1%
Send ^v`n
Clipboard := SubStr(originalClipboard, size)
return
}
Caveats:
It may not robustly handle line endings--it only works for two-character \r\n endings (the Windows standard). This should be most if not all real-world usages.
AutoHotKey seems to be only for Windows.
After you paste a line, that line is removed from the clipboard so that you are ready for the next one.
It always pastes a whole line at a time, even if the source was a partial line.
When you run out of clipboard lines, it pastes blank lines till you realize it.
It adds a new line by sending a newline. Not sure if this works in all text editors, but it worked in Notepad and a few others I tried.
There may be other nuances that it doesn't handle well.
Unfortunately, I cannot comment, but the great solution by #Patrick only works for me when I add a sleep command - otherwise, the clipboard content gets overwritten before the line is pasted. So if you run into a similar issue, the following version might do it:
#^v::
{
originalClipboard := Clipboard
StringSplit, ClipLines, originalClipboard, `n`r
size := StrLen(ClipLines1) + 3
Clipboard = %ClipLines1%
Send ^v`n
sleep, 500 ;
Clipboard := SubStr(originalClipboard, size)
return
}
Install the MultiLineRun.py script from the IdleX extensions for IDLE (or the whole of IdleX). Idlex is available here: http://idlex.sourceforge.net/.
If you want to automate it:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Python 2.7.9 Shell")
# or the title of your idle shell window
for line in source.readlines():
# open your source file of examples
# better parse it into groups of commands
# and work each group in a batch
line= line.replace("(","{(}") # sendkeys escape
line= line.replace(")","{)}")
shell.SendKeys(line)
shell.SendKeys("{ENTER}") # for good measure.
"""SendKeys sends a string to the active window.
You can automate reading lines in batches linked to a button press etc
put in delays, copy per char etc
Go to town and make it a mini slide show!
"""

How to program faster, (generate code from a pattern?)

I frequently run into problems that could be solved with automating code writing, but aren't long enough to justify it as tediously entering each piece is faster.
Here is an example:
Putting lists into dictionaries and things like this. Converting A into B.
A
hotdog HD
hamburger HB
hat H
B
def symbolizeType
case self.type
when "hotdog"
return "HD"
when "hamburger"
return "HB"
when "hat"
return "H"
end
Sure I could come up with something to do this automatically, but it would only make sense if the list was 100+ items long. For a list of 10-20 items, is there a better solution than tediously typing? This is a Ruby example, but I typically run into cases like this all the time. Instead of a case statement, maybe it's a dictionary, maybe it's a list, etc.
My current solution is a python template with the streaming input and output already in place, and I just have to write the parsing and output code. This is pretty good, but is there better? I feel like this would be something VIM macro would excel at, but I'm that experienced with VIM. Can VIM do this easily?
For vim, it'd be a macro running over a list of space separated pairs of words, inserting the first 'when "' bit, the long form word 'hotdog', the ending quote, a newline and 'return "', and then the abbreviation and then final quote, then going back to the list and repeating.
Starting with a register w of:
when "
register r of:
return "
an initial list of:
hotdog HD
hamburger HB
hat H
and a starting file of:
def symbolizeType
case self.type
"newline here"
you can use the following macro at the start of the initial list:
^"ayeeeb"byeo"wp"apa"^Mrb"j
where ^M is a newline.
I do this frequently, and I use a single register and a macro, so I'll share.
Simply pick a register, record your keystrokes, and then replay your keystrokes from the register.
This is a long explanation, but the process is extremely simple and intuitive.
Here are the steps that I would take:
A. The starting text
hotdog HD
hamburger HB
hat H
B. Insert the initial, non-repetitive lines preceding the text to transform
def symbolizeType
case self.type
hotdog HD
hamburger HB
hat H
C. Transform the first line, while recording your keystrokes in a macro
This step I'll write out in detailed sub-steps.
Place the cursor on the first line to transform ("hotdog") and type qa to begin recording your keystrokes as a macro into register a.
Type ^ to move the cursor to the start of the line
Type like you normally would to transform the line to what you want, which for me comes out looking like the following macro
^i^Iwhen "^[ea"^[ldwi^M^Ireturn "^[ea"^[j
Where ^I is Tab, ^[ is Esc, and ^M is Enter.
After the line is transformed to your liking, move your cursor to the next line that you want to transform. You can see this in the macro above with the final j at the end.
This will allow you to automatically repeat the macro while it cycles through each repetitive line.
Stop recording the macro by typing q again.
You can then replay the macro from register a as many times as you like using a standard vim count prefix, in this case two consecutive times starting from the next line to transform.
2#a
This gives the following text
def symbolizeType
case self.type
when "hotdog"
return "HD"
when "hamburger"
return "HB"
when "hat"
return "H"
D. Finally, insert the ending non-repetitive text
def symbolizeType
case self.type
when "hotdog"
return "HD"
when "hamburger"
return "HB"
when "hat"
return "H"
end
Final Comments
This works very quick for any random, repetitive text, and I find it very fluent.
Simply pick a register, record your keystrokes, and then replay your keystrokes from the register.
For things like this I have a few ways of making it easier. One is to use an editor like Sublime Text that allows you to multi-edit a number of things at once, so you can throw in markup with a few keystrokes and convert that into a Hash like:
NAME_TO_CODE = {
hotdog: 'HD',
hamburger: 'HB',
hat: 'H'
}
Not really a whole lot changed there. Your function looks like:
def symbolize_type(type)
NAME_TO_CODE[type.to_sym]
end
Defining this as a data structure has the bonus of being able to manipulate it:
CODE_TO_NAME = NAME_TO_CODE.invert
Now you can do this:
def unsymbolize_type(symbol)
CODE_TO_NAME[symbol.to_s]
end
You can also get super lazy and just parse it on the fly:
NAME_TO_CODE = Hash[%w[
hotdog HD
hamburger HB
hat H
].each_slice(2).to_a]
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are three things to evaluate: First, the features of the snippet engine itself, second, the quality and breadth of snippets provided by the author or others; third, how easy it is to add new snippets.

New line or Carraige Return syntax problems

Im pretty new to coding, heres my problem.
Results->Text = "G55 > Y" + System::Convert::ToString(destY);
"Results" is a System.Windows.Forms.Textbox "multiline btw", or so says VS.
That line works fine, but i need a "new line or CR" at the end, so that i can repeat that line with different Literals and a different var in ToString.
For days now ive tried different syntax's ive read about, and i cant get it to take any of them.
Or even a complete different way to input text into Results->Text that would allow for tidy multiline use.
Sidenote: since im using ->Text and System::Convert::ToString in VC, would this code be considered just c++ or .net or CLI? to tighten my searches.
Have you tried System::Environment::NewLine? This will give you CrLf on Windows and whatever is correct for Linux/OS X on those platforms.
Being completely unfamiliar with .NET, I could be completely wrong, but surely adding a + "\n" to the end of your line would do the job? Or failing that, a + "\r\n"?

I get this window while editing Ruby Files in Vim. What is it?

I usually get this new window open up suddenly while I am editing a Ruby file in VIM. This is getting irritating because, i cant type in anything while its processing. And it usually happens arbitarily. Does any one here know which plugin could be doing this? Or is this somekind of VIM's process?
This is happening when you hit K in normal mode.
K Run a program to lookup the keyword under the
cursor. The name of the program is given with the
'keywordprg' (kp) option (default is "man"). The
keyword is formed of letters, numbers and the
characters in 'iskeyword'. The keyword under or
right of the cursor is used. The same can be done
with the command >
:!{program} {keyword}
There is an example of a program to use in the tools
directory of Vim. It is called 'ref' and does a
simple spelling check.
Special cases:
- If 'keywordprg' is empty, the ":help" command is
used. It's a good idea to include more characters
in 'iskeyword' then, to be able to find more help.
- When 'keywordprg' is equal to "man", a count before
"K" is inserted after the "man" command and before
the keyword. For example, using "2K" while the
cursor is on "mkdir", results in: >
!man 2 mkdir
- When 'keywordprg' is equal to "man -s", a count
before "K" is inserted after the "-s". If there is
no count, the "-s" is removed.
{not in Vi}
If you notice, it's running ri in the open window, which is the ruby documentation app.
In Unixy environments, the help program normally runs inline, just displacing the vim output for a minute.
Is this using gvim, or command-line vim?
In either case, you can try monkeying with 'keywordprg' to fix the popup
Or, if you can't train yourself not to type it, you can just use :nnoremap K k to change what K does (in this case, just treat it as normal k command and go up one line).
I have this same issue on my work desktop, but not my home machine. The setups are near identical.
While stalking down a possible cause, I noticed that when I leave my cursor over a Ruby symbol such as File, Vim would popup a short description of the File class. After comparing all the various vim scripts and ri-related files that I could find, I finally settled on the only solution that worked...
Open $HOME/_vimrc and add the following line:
autocmd FileType ruby,eruby set noballooneval
Previously, I commented out a block in $VIMRUNTIME/ftplugin/ruby.vim, but Brian Carper suggested a better solution of :set noballooneval. I added the autocmd line so it is only executed with Ruby files.
If anyone figures out a true solution, please contact me. :(

Resources