Superfast copy / paste code snippets

    While constantly programming in C ++ / Qt, I noticed that it would be more convenient to store my code snippets somewhere and have quick access to them . Of course, I began to search and found many ready-made organizer programs and sites. I tried them, but all of this did not suit me.

    I wanted just quick access - and therefore global hotkeys. To click - entered words into the search - received a code. But only two programs had such functions, and they were paid and not cross-platform.

    Therefore, I undertook to write my program in Qt. It turned out to be so convenient that I can’t imagine my coding without it and of course decided to publish it - all of a sudden, it’s still useful? Under the cut, I’ll tell you about the problems and tasks that I had to solve, I will lay out my experience, as well as an overview of the program itself and its source code.


    Development history

    To begin with, I decided - the program should be small, convenient and smart. That means - you should put the program in the tray so that it does not interfere with the taskbar. Fortunately, Qt provides for this a convenient cross-platform solution in the form of the QSystemTrayIcon class. Also, the program must necessarily work on global hotkeys. This is where the problem arose! The fact is that Qt, unfortunately, out of the box does not support them. That is the libqxt librarywhich I have used quite successfully for almost a year under Qt 4.8. However, under Qt 5, for some reason I didn’t understand, they didn’t finish it normally (at that time) and under Win7 she didn’t want to get together, and I quit. Instead, I inserted the very bit of native code - a call to winApi of the RegisterHotKey function. This is the only native insert in the program - and I think it will be very easy to modify it under Linux or Mac.

    I decided to save the pieces of code in an XML database. This may not be the best solution, but it still copes with the task. Anyway, I got used to it.

    As a basis for the editor, I took, of course, QTextEdit. A pleasant surprise awaited me here - the code copied from QtCreator is copied as HTML with markup, and therefore with syntax highlighting. Immediately, the need to write built-in syntax highlighting for C ++ \ Qt disappeared by itself. QtCreator has very cool syntax highlighting, especially if you customize it.

    Further, when it was already possible to insert code into the window and it went to the database, and then to the xml file, the question arose about how to get it conveniently and quickly. To do this, I wrote a small search engine that looks for pieces of code where all the words that are separated by spaces are found. While it is enough. In my personal database there are already about 100 code fragments, and still, among them, I am well oriented.

    Also made keywords. Well, what without them? The search is conducted not only by the main code, but also by them. In kei you can specify the programming languages ​​to which this paste belongs, for example, “c ++, qt” or “qml, js”. If the search resulted in code fragments from several languages, you can differentiate them by adding a keyword-language.

    While finalizing the program, I drew a logo in GIMPe - curly braces. Simple and clear.

    Program Overview

    You can download the Windows distribution of the program here or compile it from the sources by downloading them from github . Just unpack them not to the desktop, as many do. The distribution, together with all the dlls, weighs 16 MB in packed form and 40 in unpacked one - Qt5 is significantly fat and even the simplest program requires dragging along a whole host of libraries. But I think that this is not a problem, especially since it does not affect the speed of the program.

    Unzip the program to any folder on the disk and run it. She will create a config.xml configuration file in her folder, and then, when saved, a clean database for your base.xml code. Actually, the main menu looks like you can see on the screen in the article title. The program interface is so far all in English so that there are no problems with the assembly on some systems - messages and signatures are in the source code for now.

    Adding a new code fragment is carried out by the global hotkey ctrl + D , and search by win + V. You can throw a stone at me for such "global" hotkeys, but experiments have shown that they are the most convenient. Yes, perhaps you are used to ctrl + D sending the code from QtCreator somewhere. If this is so critical, you can go to the constructor of the CodePaster class and change the hotkeys there to those that suit you. In the future, this can be done through the GUI.

    Let's continue. Suppose you wrote some convenient algorithm, or mastered a new language design that you don’t want to print every time. You simply select your code block in your favorite IDE (for example, in a creator) - and copy as usual through ctrl + C. It will immediately be convenient and very close - without releasing ctrl - click on D. A window will pop up over your IDE - and the code itself will jump into it! The program automatically reads the clipboard, pasting the code from it. There are 2 more small features here - a signature is inserted before the code in the form of a comment "//", and also extra indentation is removed from the code itself. Those. if you copied it from the middle of the program, then it will move to the left until it stops.

    You just click “OK” - and your code is already in the database! Moreover, it was immediately saved to the hard disk so as not to lose data in the event of any failure.

    Next - when you wanted to quickly copy this code - press win + ctrl. Why exactly him? It seems to me a convenient option and is not involved anywhere. You will see a small code search engine, and from there you can copy the fragments you need. As for the search engine itself, each piece of code has a context menu. You can delete it from the database, you can edit it, or you can see its properties.


    As you can see, code fragments are separated by "---", each has its own id, as well as small buns - the program remembers the date it was added and how many times it was copied.

    I had to work a bit to map some block of text inside QTextEdit to some Code object - when updating the search, a “map” is formed - a list of special objects, each of which stores the start and end position in QTextEdit and a pointer to a code object that corresponds to this area.

    By the way - a program can be used not only for code, but in general for any text. For example, I successfully scored my mailboxes and phone there, copying them when necessary. It turns out something like a notepad with quick access and search.

    Examples

    Here is a simple example of using the program - if you suddenly needed to throw a message box in your program for debugging (it is it, not qDebug output) - having prepared the code in advance, you simply type win + V and drive in “mes” - this is enough to find the necessary piece code and copy it. You will spend one click of hotkeys, a set of 3 characters, Enter (which can be removed later), and selection-copying. Now let’s compare - how much time will you spend to copy it from the same assistant? Even if it is on the global hotkey, you have to select the focus for the search bar through the selection or keys, enter “mess” there, select the desired class with the mouse (!), Scroll down to the place where there is an example (if there is one) and only then copy his. This is at least 2 times longer.

    What other options do you offer? Go to the website each time pastebin and look there? This is also an extra delay. Enter the same queries 100 times in Google? But most of the programmers with whom I spoke on this topic did so - they googled every time. But I found it once - you can save it in some kind of “cache” for quick access, changing it if necessary - and use it for health! This cache should be a program that is well integrated with the system for greater convenience.

    We give another example. I often sit on the prog.org.ru forumand constantly find or get many good solutions to various small problems. For example, how to use QSettings to add a program to Windows startup. Or how to dynamically load a ui-form. There are whole algorithms. Each time I found something I found once - this is not comfortable. Good and beautiful solutions are nice to keep for yourself for future use.

    Another example - I once saved the code for a bat file that adds a variable to windows environment variables. When it is needed - just copy it to the command line instead of using an inconvenient GUI.

    Of course, you can also suggest saving code fragments to files, but as soon as the question arises of quick access and search, as well as convenient organization of such files, you will see that this is not the best solution. Large chunks of code — entire libraries and classes — should certainly be stored that way, but we are talking about small fragments that we often use.

    If you work with several programming languages ​​at once, the advantages of using such a program will become even more significant. You can forget how this or that design looks in a particular language - and it’s easy to remember through a search.

    Source Code Overview

    I wrote the source code hard by the Qt Coding Style standard . At times I used C ++ 11 constructs. I think there will be no problems reading it. All windows are based on widgets, but then I plan to translate them into QML.

    In general - do not judge strictly. This is my first public project and I hope that at least someone will come in handy if not a program, then those moments that I have outlined here. If the program turns out to be really useful and you are adding it, then I can modify it. The plans are to make it even more convenient (there are ideas), syntax highlighting for other languages ​​and support for hotkeys for Linux \ Mac.

    Also, I do not exclude the possibility that somewhere in the article or in the code there are typos and / or errors. I will be glad to accept a good criticism.

    So - I'm waiting for your feedback!

    Also popular now: