Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Convert betwen Upper/Lower/Title Case (with auto copy/paste)

Description
This macro will rotate through upper case, lower case, and title case on the selected text.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Oct 20, 2017
Date Last Modified
Jul 10, 2020

Macro Code

using System;
using System.Text;
using System.Collections.Generic;

public static class ClipboardFusionHelper
{
    private static readonly string[] ExceptionsArray = 
    { 	
        //articles
        "a",
        "an",
        "the",
        
        //prepositions (incomplete)
        "on",
        "in",
        "at",
        "for",
        "ago",
        "to",
        "from",
        "by",
        "of",
        "with",
        
        //coordinating conjunctions			
        "and",
        "but",
        "or",
        "nor",
        "for",
        "yet",
        "so"
    };
		
    private static readonly HashSet<string> Exceptions = new HashSet<string>(ExceptionsArray);

    public static string ProcessText(string text)
    {   
        //get the highlighted text
        text = BFS.Clipboard.CopyText();
        
        //convert the text to lower case
        string lower = text.ToLowerInvariant();
        
        //if the copied text is lower case, convert it to title case
        if(lower.Equals(text, StringComparison.Ordinal))
        {
            BFS.Clipboard.PasteText(ToTitleCaseSmart(text));
            return null;
        }
        
        //convert to text to title case
        string title = ToTitleCaseSmart(text);
        
        //if the copied text is title case, convert it to upper case
        if(title.Equals(text, StringComparison.Ordinal))
        {
            BFS.Clipboard.PasteText(text.ToUpperInvariant());
            return null;
        }
               
        //otherwise, paste the lower case text
        BFS.Clipboard.PasteText(lower);
        return null;
    }
        
	private static string ToTitleCaseSmart(string text)
	{
        Queue<string> words = new Queue<string>(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(text.ToLowerInvariant()).Split(new char[] { ' ' }));

        //Always leave the first word capitalized, regardless of what it is
        StringBuilder builder = new StringBuilder(words.Dequeue());
        
        while(words.Count > 0)
        {
            string word = words.Dequeue();
            string lower = word.ToLower();
            builder.Append(" ");
            if(Exceptions.Contains(lower))
                builder.Append(lower);
            else
                builder.Append(word);
        }

	    return builder.ToString();
	}
}