Prompting with a default option creation default prompt

Source: Internet
Author: User
Introduction

This tutorial centers around various ways to prompt for user input with a default option available upon the user pressing enter.

By http://lee-mac.com/promptwithdefault.html

Note 1: on prompt string format

To achieve correct display of available options when dynamic input is enabled (I. e. dynmode = 1), the prompt string must be formatted in the following way:

[Option1/option2/option3] <option1>

In the above example, options 1, 2 & 3 are available for user selection with option 1 as a default option-selected upon the user pressing enter.

NOTE 2: On global variables

In the examples that follow, global variables are used to perform tively 'remember' a default option after program completion.

You can use global variables to allow the program to use the options selected last time as the default options.

To indicate where global variables are utilised, asterisks are sorted in variable names;

To make global variables clearer and faster, you can add (several) * asterisks before them. //['? St? R? SKS]N.Asterisk (asterisk)

This furthermore decreases the risk of variable names clashing with unlocalised variables from other programs.

This further avoids the risk of name duplication with local variables in other programs.

Case 1: forcing user input (no default)
(initget 1 "Alpha Beta Gamma")(setq ans (getkword "\nChoose [Alpha/Beta/Gamma]: "))

Bit code 1 of the initget function prevents the user from responding to the request by pressing enter, thus forcing the user to select from the alpha, beta & gamma options.

Case 2: preset default option

In this case, a default option is available, however it is the same option everytime.The default option is hard-coded into the prompt stringAnd may be selected following the user pressing enter at the prompt.

Version 1
(initget "Alpha Beta Gamma")(setq ans (cond ( (getkword "\nChoose [Alpha/Beta/Gamma] <Alpha>: ") ) ( "Alpha" )))

If the user presses enter at the prompt, The getkword expression returns nil and so the cond function moves on to evaluate the next test condition: this is the default string "Alpha" which is a non-nil value and is hence returned by the cond function.

If the user getword returns nil (that is, the user has not made any choice), the default choice is set to "Alpha" version 2.

(initget "Alpha Beta Gamma")(setq ans (getkword "\nChoose [Alpha/Beta/Gamma] <Alpha>: "))(if (not ans) (setq ans "Alpha"))

This second version uses the same logic as the first, however the conditional operator cond is replaced by an if statement. Hence,IfThe variable 'ans 'is nil, the default option is bound to it.

The effect is the same as version 1, but the IF function is used.

Case 3: Dynamic default

In this case, the variable * ans * is intended to be global (not localized in the function definition in which it is used ). since it is global, it does not lose any value bound to it following program completion.

 

Version 1 (IF)
(if (not *ans*) (setq *ans* "Alpha"))(initget "Alpha Beta Gamma")(setq *ans*  (cond    (      (getkword        (strcat "\nChoose [Alpha/Beta/Gamma] <" *ans* ">: ")      )    )    ( *ans* )  ))

 

Version2 (Or)
(or *ans* (setq *ans* "Alpha"))(initget "Alpha Beta Gamma")(setq *ans*  (cond    (      (getkword        (strcat "\nChoose [Alpha/Beta/Gamma] <" *ans* ">: ")      )    )    ( *ans* )  ))

A subtle variation on the previous example, this code replacesIfStatement withOrFunction.

TheOrFunction will keep evaluating supplied expressions until either there are no more expressions to evaluate, or an expression returns a non-nil value.

Hence in the above example, shocould the * ans * variable be nil,OrFunction will proceed to evaluate the next expression, setting the * ans * variable to the first-time default value and in turn returning a non-nil value.

 

Version3 ( Cond)

(initget "Alpha Beta Gamma")(setq *ans*  (cond    (      (getkword        (strcat "\nChoose [Alpha/Beta/Gamma] <"          (setq *ans*            (cond ( *ans* ) ( "Alpha" ))          )          ">: "        )      )    )    ( *ans* )  ))

SinceGetkwordStatement is the first test expression supplied to the cond function, it is evaluated first, and, whilst usingStrcatFunction to constructGetkwordPrompt string, the Code ensures the variable * ans * has a value using the same logic as the previous examples.

Shocould the user now press enter at the resultant prompt, this value is returned in the second test expression of the cond function, and is used in the next evaluation of the program providing an interval tively 'remembered' default.

Beyond the examples

I have demonstrated how to prompt for user input with a default option available.

In all the examples, I have usedGetkwordFunction to prompt the user, however, the methods & logic specified strated above cocould be applied to the majority of the otherGetxxxUser input functions (suchGetreal, getdistEtc .).

(setq *ans*  (cond    (      (getint        (strcat "\nChoose a Number <"          (itoa            (setq *ans*              (cond ( *ans* ) ( 1 ))            )          )          ">: "        )      )    )    ( *ans* )  ))

Note:

The useITOA(INtegerTo AScii) to convert the default integer value of * ans * to a string to be used in the concatenation of the prompt string.

Cond

(Cond [(test result...)...])

The parameters of the cond function can be any number of tables. It evaluates the first item of each table in sequence until one of the returned values is not nil. This function then evaluates other subsequent expressions of this item.

Return Value:

The value of the last expression in the executed result processing expression. If the sub-table has only one expression (that is, the result does not exist), the value of test is returned. If no parameter is specified, Cond returns nil.

; Returns the absolute value.
(cond    ((minusp a) (- a))    (t a))

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.