Skip to content

Commit

Permalink
change code to better handle newline characters
Browse files Browse the repository at this point in the history
Various code changes were made to more robustly handle newline
characters. Most changes were made to satify these two conditions: CRLF
is used within the Edit control and clipboard; LF is used everywhere
else. The built-in conversion functions of the Edit library were used to
make the code more readable and robust.
  • Loading branch information
bhughes339 committed Sep 11, 2019
1 parent 300261a commit db78d34
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions keywords_editor.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ global templates := {}
global editWidth := 80

FileRead, tempText, *t templates\intl_keywords.txt
templates["Intl Group: Keywords"] := tempText
templates["Intl Group: Keywords"] := Edit_Convert2Unix(tempText)

FileRead, tempText, *t templates\intl_peer_review.txt
templates["Intl Group: Peer Review"] := tempText
templates["Intl Group: Peer Review"] := Edit_Convert2Unix(tempText)

global userTemplates := {}
readSettings()
Expand All @@ -44,9 +44,9 @@ Return
; =======

!+v::
fText := getFormattedText(hEdit)
fText := Edit_Convert2Unix(getFormattedText(hEdit))
SendInput {End}{Space}
Loop, Parse, fText, `n, `r
Loop, Parse, fText, `n
{
Sleep 50
line := RTrim(A_LoopField)
Expand Down Expand Up @@ -250,8 +250,8 @@ if (ErrorLevel == 0) {
}
editText := Edit_GetText(hEdit)
userTemplates[templateName] := editText
newText := RegExReplace(Edit_GetText(hEdit), "`r*`n", "\n")
IniWrite, %newText%, %settingsFile%, Templates, %templateName%
escapedText := escapeNewlines(Edit_Convert2Unix(editText))
IniWrite, %escapedText%, %settingsFile%, Templates, %templateName%
}
Gosub UpdateTemplateMenus
Return
Expand Down Expand Up @@ -325,7 +325,8 @@ readSettings() {
IniRead, dateFormat, %settingsFile%, Settings, dateformat, "dd MMM yyyy"

IniRead, outSection, %settingsFile%, Templates
Loop, Parse, outSection, "`r`n"
outSection := Edit_Convert2Unix(outSection)
Loop, Parse, outSection, "`n"
{
line := StrSplit(A_LoopField, "=", "", 2)
userTemplates[line[1]] := RegExReplace(line[2], "\\n", "`n")
Expand Down Expand Up @@ -357,7 +358,7 @@ getFormattedText(editHwnd) {
Edit_FmtLines(editHwnd, True)
; Strip whitespace from the end of the text
fText := RegExReplace(Edit_GetText(editHwnd), "D)\s+$", "")
; Format newlines to CRLF
; Format newlines to CRLF (including soft line breaks from Edit_FmtLines)
fText := RegExReplace(fText, "\r*\n", "`r`n")
; Strip whitespace from end of each line
fText := RegExReplace(fText, "m) +$", "")
Expand All @@ -378,7 +379,7 @@ loadSavedKeywords(editHwnd) {
if FileExist(savedFile) {
MsgBox, 1, Saved text found, Saved text was found from last session. Load it?
IfMsgBox, Ok
Edit_ReadFile(editHwnd, savedFile)
Edit_ReadFile(editHwnd, savedFile, "", True)
Else
Return
}
Expand All @@ -387,7 +388,7 @@ loadSavedKeywords(editHwnd) {

replaceText(editHwnd, newText) {
Gui Main:+OwnDialogs
newText := RegExReplace(newText, "\n", "`r`n")
newText := Edit_Convert2DOS(newText)
trimmedText := RegExReplace(Edit_GetText(editHwnd), "Ds)^\s*(.*)\s*$", "$1")
if (trimmedText) {
MsgBox, 1, Text present, This will replace the current text. Are you sure?
Expand All @@ -401,6 +402,13 @@ replaceText(editHwnd, newText) {
}


escapeNewlines(fText) {
fText := RegExReplace(fText, "\r", "\r")
fText := RegExReplace(fText, "\n", "\n")
return fText
}


; https://autohotkey.com/boards/viewtopic.php?p=109617&sid=a057c8ab901a3ab88f6304b71729c892#p109617
HasVal(haystack, needle) {
for index, value in haystack
Expand Down

0 comments on commit db78d34

Please sign in to comment.