Emacs Helm: Use keywords to search, get, and execute anything.

Source: Internet
Author: User
Tags function definition goto

Helm is an Emacs software package that defines a universal framework that interactively and dynamically reduces the use of keywords to select, acquire, and execute anything. Like what:

    • Execute emacs Command
    • Open File
    • View man Documents
    • Perform the grep operation
    • Execute APT command
    • Tearful Imenu function definition
    • Toggle Buffer

The helm package itself contains two parts, the framework itself and its application. The list above is applied. Based on the framework, you can easily create new applications.

Basic principle

Three important concepts of Helm: Candidate, narrowing, action.

Candidate

Candidate is the candidate value, which is a list that holds all the items available for selection. For a command to open a file, candidate is a list of all the file names.

Narrowing

After the helm command is started, the user does not enter any keywords, it will display all the entries in the candidate, each line displays an item, you can select the current entry by moving the cursor up or down through ' c-n ', ' c-p '.

If the number of candidate is less, there is no need to enter keywords at this time, by moving the cursor up and down the selection of the line, but if the number of candidate, the target entry is not displayed on the first page, you can enter keywords, to filter the candidate entries, Only entries that match the keyword will be displayed. This is narrowing.

It is worth mentioning that this process is dynamic, that is, every input character, candidate entries will be re-filtered. Sometimes only one character is entered, the target entry is already displayed on the first page, you can stop the input, select the current item by moving the cursor, sometimes you enter a keyword, the target entry still does not appear, you can press the space, continue to enter another keyword, to make more precise filtering until the target entry appears.

The more keywords you enter, the fewer the number of candidate, and the greater the chance that the target entry will appear in the first entry position on the first page, and the more convenient it will be to choose.

Action

When a candidate is selected, when you press ENTER, an action is executed. For open files, the action corresponding to the Emacs command is ' find-file '. You can define multiple actions for an entry, such as the file entry action to open a file, to re-command a file, to delete a file, and so on. Select from multiple actions by the TAB key, and if you press ENTER directly, the first action is executed.

Define a new basic example of Helm application

To define a new Helm command, simply define a variable to specify candidate and the corresponding action, and then pass it as a parameter to helm, which is an example.

(setq some-helm-source      "HELM at the Emacs")        (Candidates. (1 2 3 4))        (Action. (Lambda (candidate)                    '%s ' : Sources ' (Some-helm-source))

The variable that defines candidate, action is called Source and is a assoc list. Candidates is a list, action is a function, and when the action function is called, the currently selected candidate is passed in as a parameter.

Define multiple action

In the above example, the action value is an anonymous function, if you want to define more than one action, you need to set the value of the action to a list,list element is a cons: (description. function). As shown below, two actions are defined.

(setq some-helm-source      "HELM at the Emacs")        (Candidates. (1 2 3 4))        (Action.                ("Display" .  (Lambda (candidate)                                 "%s " candidate)))                 ("None" . Identify))        : Sources ' ( Some-helm-source))
Separating the selected value of the candidate from the true value

Sometimes you need to select candidate with different values, you can set candidates to cons (KEY. VALUE) of the list. Where key will be used for selection and value will be the input parameter value for the action function.

(setq some-helm-source      "HELM at the Emacs")        (Candidates. ("One" . 1) 2 ("three" . 3) 4))        (Action.                ("Display" .  (Lambda (candidate)                                 "%s " candidate)))                 ("None" . Identify))        : Sources ' ( Some-helm-source))
Dynamic candidate

Sometimes the candidates needs to be dynamically computed, or the amount of static computations can be large, at which point the candidates is set to a function that will be used to calculate the values of all candidates.

(defunrandom-candidates ()  "Return a list of 4 random numbers from 0 to ten"  (loop for I below 4 collect (random 10))) (setq some-helm-source      "HELM at the Emacs")        (candidates. Random-candidates)        (Action. (Lambda (candidate)                    '%s ' : Sources ' (Some-helm-source))
Definition of Helm-org-headlines

This command is provided by default by helm, which is defined as follows:

(setqHelm-source-org-headline ' (name."ORG headline") (Headline, @ (Mapcar (Lambda(num) (format"^\\*\\{%d\\}\\(.+?\\)\\([\t]*:[[email protected]:]+:\\)? [\t]*$]num)) (number-sequence 1 8))) (condition.        (eq major-mode ' Org-mode))        (Migemo)        (subexp. 1) (Persistent-action. (Lambda(ELM)                               (Helm-action-line-goto Elm)        (org-cycle))) (Action-transformer. (Lambda(Actions candidate) ‘(("Go to Line". Helm-action-line-goto) ("refile to this headline". Helm-org-headline-refile) ("Insert link to this headline". Helm-org-headline-insert-link-to-headline))))) (Defun Helm-org-headlines()"preconfigured helm to show org headlines."(Interactive) (Helm-other-buffer ' Helm-source-org-headline"*org headlines*"))

Emacs Helm: Use keywords to search, get, and execute anything.

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.