Sublime text manual localization and plug-in development and submission (2)-plug-in development

Source: Internet
Author: User
Tags sublime text

(Reprinted must indicate the original address and the author "bihai Qing Tian Zhao Liang" and blog address http://blog.csdn.net/theforever)

In the previous section, we talked about the "sublime text hand-written method" (http://blog.csdn.net/theforever/article/details/8962727 ). This section describes the storage location and editing method of the extension package. It is a necessary prerequisite for this section. We will not repeat it here. Please refer to it by yourself.

The following describes the development method based on a self-written st plug-in. This plug-in is simple and suitable for beginners. The function is to delete all blank rows in the current clipboard (including rows containing only spaces and tabs but without other valid content) and paste them to the document currently edited by St. This is useful when you use a browser to view the web page and want to collect text content, because even if the web page is a continuous row, you may find any blank lines in the middle after copying and pasting, needless to say, some content obviously has multiple unnecessary blank lines. When a file contains different content on multiple different pages, these extra blank lines will make the content appear scattered and difficult to distinguish.
Although the replacement of the St regular expression can eliminate empty rows, it is a little troublesome to input, and generally You must select "Replace text in the area ", otherwise, the empty lines to be retained in the previously processed text will be deleted.
However, using the unique feature of St will eliminate the same content line as the separator line, similar to other necessary repeated rows (for example, a ghost story often shows several rows in different positions that contain only a single image. What do you think if it is unique ). "Uniqueness" may be useful in other cases, but it is obviously not suitable in this case.
The delete blank line plug-in of st2 has to select the pasted content and then manually press the button. It is still troublesome to press it several times, especially when there are many pasted content, when it is not easy to see all the results, it will make the user feel more difficult.
In addition, using this method to process the clipboard has the advantage that the weight loss content can be pasted elsewhere. For example, if you see a personal question in the forum, you want to reply and record the question and your reply in a document. When you reply, you may want to reference his questions, but if there are unnecessary blank lines in his questions, it will occupy unnecessary space before your answers. In this case, after you copy the problem, press the hotkey CTRL + ALT + Shift + L bound to the plug-in St (although there are many keys, it is quite convenient to press it, it is easy to understand: shift in the meaning of the function key usually means to remove or reverse, L indicates empty lines of lines), it will be pasted into your document in the form of removing Extra blank lines, then you can go back to the browser's reply box and press Ctrl + V to paste the content that has been lost.

At least this plug-in is quite useful for what I often do, so that I have written it for several browsers and other editors that support plug-ins. I am not keen on this, but if I do not use this function, it will be a little troublesome and annoying.

Now, we have officially written the following code:

1. Although there is a "new plug-in" menu in the Tools menu of St, but I think the quicker way is to select a ready-made Plug-In directory in the packages folder at the same level as the installed packages folder (it is best to have a complete set of content if you are interested in doing this, you can create a template -- of course, you 'd better place it elsewhere -- or even write a visual operation software to generate these files), copy and paste it, change its name, and enter it, select the file you do not want to delete, and then modify the remaining file.

2. Introduce the functions and writing of some main files in the plug-in folder:
Main. sublime-menu: Main Menu of the plug-in. The content is added to the ST system menu.

This part of my plug-in is:

[{"ID": "edit", "children": [{"caption": "Clear empty clipboard rows and paste them", "command": "paste_without_blk_lines"}]

The first line is the JSON structure identifier. If you do not understand it, you only need to keep the structure in the format of the image.
The second line indicates adding a project to the menu ("Edit/edit" menu) with the menu ID "edit. It should be clear that the ID and caption are different. The ID is used to identify the real identity, and the caption is only used as the display content in the menu.

Note: The ID here is not limited to the top menu ID, but any menu item with an ID, select based on your plug-in purpose (but it is best not to add sub-menus to projects with IDs and caption is "-". Although it can be added, it looks so strange)
The third line is the display name of the added command menu and the ID of the command to be executed. The command ID must be self-initiated. To avoid duplication with other existing command IDs, you 'd better name your plug-in purpose.

To add multiple command menus, separate them with commas. Note: Do not add a comma after the last item.
In addition, if a guy writes his plug-in like this:

[{    "id": "edit",    "caption":"Edit(E)",    "children":[        {"caption": "abc","command": "abc"}    ]}]

This will make the "Edit (E)" menu you have finished Chinese still display the English "Edit (E)", so when you see this phenomenon in the menu, you need to find the plug-in to do it. You don't have to worry about it. Let's look at which item is in English under this menu (even if there are more than one item, you can check it one by one). Remember the English content of the menu after finding it, go to the three directories where plug-ins are stored in the previous tutorial, and find plug-ins with similar names, use the editing method described in the previous tutorial to open the menu file for editing and save it. Then it will work again.

Okay. After finishing the menu file, we will add a keyboard hotkey that can be quickly called in St for our plug-in. This is through the default (Windows) below ). the sublime-keymap file implements the following content:

[  { "keys": ["ctrl+alt+shift+l"], "command": "paste_without_blank_lines" }]

The command must correspond to the command ID in the menu file.
In addition, there are two files: default (Linux). sublime-keymap and default (OSX). sublime-keymap. If your plug-ins can and want to support them, you can also add them.

We can also use the default. sublime-commands file to add the commands contained in the plug-in to the "Ctrl + Shift + P" Command Panel for calling. Although it will slightly increase the system overhead, it is a good habit. The Chinese name can also be searched and called in the Command Panel.

[{"Caption": "Clear blank clipboard rows and paste", "command": "paste_without_blk_lines"}]

Pastewithoutblanklines. py is the implementation code of the plug-in command. All plug-ins of ST are written in Python, And the python program extension is. py. When a plug-in has only one py file, the file name and plug-in name can be different, and St will automatically call this unique py program. However, when there are multiple py files in the plug-in, you must either put the remaining py files in the sub-folder for calling, or you must use the name of the plug-in folder as the name of the main program file, in this way, St selects the program with the same name as the plug-in folder in several py files and runs the program first.
I have never read the python syntax before writing this tutorial. However, the python program is easier to read and understand, combined with the Code Completion prompt function of St, it is enough to write a simple plug-in. Computer technology is endless, and it is impossible to explore it all. But it is also good to learn something through a practical task.

The source code is pasted below, and detailed comments have been written:

Import sublime, sublime_plugin # The plug-in main program must reference these two base class libraries. Import re # The regular expression class library required by this plug-in. Class pastewithoutblklinescommand (sublime_plugin.textcommand): "" each menu command corresponds to a class. Note that the class name is written by removing the underline Of the menu command, changing it to the camper style, and adding command at the end. In the brackets, sublime_plugin.textcommand is the parent class of this class, indicating that this class is the actual behavior class of a command menu. If it is a command generated by clicking the panel in the Operation Panel provided by package control, sublime_plugin.windowcommand is used as the parent class, indicating that it is a window command class. "Def run (self, edit): # The st plug-in mechanism automatically calls the run method of the command class. Therefore, you must reload this method for execution. Def indicates defining a method. S = sublime. get_clipboard () # obtain the clipboard content # Read the default row Terminator category from the St File View configuration (represented by the operating system) line_ending = self. view. settings (). get ('default _ line_ending ') # perform different replacement operations based on different systems. If line_ending = 'windows': S = Re. compile ('\ n \ R '). sub (', S) S = Re. compile ('\ r \ n \ s * \ r \ n '). sub ('\ r \ n', S) Elif line_ending = 'mac': S = Re. compile ('\ r \ R '). sub ('\ R', S) S = Re. compile ('\ r \ s * \ R '). sub ('\ R', S) else: # UNIX/System S = Re. compile ('\ n \ n '). sub ('\ n', S) S = Re. compile ('\ n \ s * \ n '). sub ('\ n', S) sublime. set_clipboard (s) # modify the clipboard content. This method can enable self to be used for clipboard content that has been lost elsewhere. view. run_command ('paste ') # Call the paste command

Sublime. get_clipboard, self. view. settings, sublime. set_clipboard and so on are all APIs of st. To get more information and instructions to write your complicated plug-ins, go to the official website of St to view the API help documentation.

The packages folder at the same level as the installed packages folder can be said to be designed for debugging plug-ins. Changes to the menu or the program implemented by the command will be instantly reflected to the st. After writing the package, you can find your plug-in through the "Create a package" project in the package control (PC) panel. The PC will automatically package your plug-in and generate it to the system desktop. If it cannot be generated correctly, you can manually compress and rename it using the method described in the previous section of the tutorial.

Now, you can use the plug-in by yourself, package the plug-in to friends, or put it on a URL that can be downloaded.

If you want to see your own plug-ins in the installation plug-ins list on the PC panel for use by friends all over the world, you need to see the third section "St plug-in release" http://blog.csdn.net/theforever/article/details/8967.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.