Auto arg: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
(Created page with "cmd.auto_arg controls auto-completion of command arguments in PyMOL's command line. It is an array of dictionaries.When Pymol's console attempts to auto-complete the ''n''-th...")
 
(More Examples)
 
Line 1: Line 1:
cmd.[[auto_arg]] controls auto-completion of command arguments in PyMOL's command line. It is an array of dictionaries.When Pymol's console attempts to auto-complete the ''n''-th argument of command ''abc'', it will look at cmd.auto_arg[n]['abc'] (''n'' starts from 0). This is a list with three elements.
cmd.[[auto_arg]] controls auto-completion of command arguments in PyMOL's command line. It is an array of dictionaries. When Pymol's console attempts to auto-complete the ''n''-th argument of command ''abc'', it will look at <code>cmd.auto_arg[n]['abc']</code> (''n'' starts from 0). This is a list with three elements. For arguments which do not have an entry in cmd.auto_arg, the default is to auto-complete file names.


# The first element is a lambda function which returns an Shortcut object. Shortcut object wraps the list of candidate strings, given to its constructor.
# The first element is a lambda function which returns an Shortcut object. Shortcut object wraps the list of candidate strings, given to its constructor.
Line 5: Line 5:
# The third element is a string, which will be added after autocompletion (postfix).
# The third element is a string, which will be added after autocompletion (postfix).


===PYTHON EXAMPLE===
== PYTHON EXAMPLE ==


<source lang="python">
<source lang="python">
Line 19: Line 19:
If you type 'a' and press TAB again, PyMOL will complete it to "test abc, ". Note that ', ' is added.
If you type 'a' and press TAB again, PyMOL will complete it to "test abc, ". Note that ', ' is added.


===Pre-defined lambdas===
== Pre-defined lambdas ==


In most cases, you just want to complete from object names or setting lists. Then you don't have to write your own lambda function. Just use '''cmd.object_sc''' for choosing from objects.
In most cases, you just want to complete from object names or setting lists. Then you don't have to write your own lambda function. Just use '''cmd.object_sc''' for choosing from objects.


<source lang="python">cmd.auto_arg[0]['test'] = [ cmd.object_sc, 'object', '']</source>
<source lang="python">cmd.auto_arg[0]['test'] = [ cmd.object_sc, 'object', '']</source>
Or even simpler, borrow from one of the core commands:
<source lang="python">
# "extract" also uses cmd.object_sc in its first argument
cmd.auto_arg[0]['test'] = cmd.auto_arg[0]['extract']
</source>


Use '''cmd.selection_sc''' for setting names (like 'line_width'), '''cmd.map_sc''' for maps and '''cmd.repres_sc''' for representations ('sticks', 'lines', etc).
Use '''cmd.selection_sc''' for setting names (like 'line_width'), '''cmd.map_sc''' for maps and '''cmd.repres_sc''' for representations ('sticks', 'lines', etc).


===SEE ALSO===
== More Examples ==
 
The first argument of the [[save]] command auto-completes to filenames by default. If you want for example filenames '''and''' object names to auto-complete, use this (from [http://www.mail-archive.com/pymol-users@lists.sourceforge.net/msg10950.html pymol-users mailing list]):
 
<source lang="python">
import glob
names_filenames_sc = lambda: cmd.Shortcut(cmd.get_names() + glob.glob('*'))
cmd.auto_arg[0]['save'] = [names_filenames_sc, 'filename or object name', '']
</source>
 
== SEE ALSO ==
*[[extend]]
*[[extend]]
*parser.py, cmd.py, shortcut.py
*parser.py, cmd.py, shortcut.py, completing.py


[[Category:Scripting]]
[[Category:Scripting]]

Latest revision as of 02:16, 18 October 2012

cmd.auto_arg controls auto-completion of command arguments in PyMOL's command line. It is an array of dictionaries. When Pymol's console attempts to auto-complete the n-th argument of command abc, it will look at cmd.auto_arg[n]['abc'] (n starts from 0). This is a list with three elements. For arguments which do not have an entry in cmd.auto_arg, the default is to auto-complete file names.

  1. The first element is a lambda function which returns an Shortcut object. Shortcut object wraps the list of candidate strings, given to its constructor.
  2. The second element is a string, which describs the argument.
  3. The third element is a string, which will be added after autocompletion (postfix).

PYTHON EXAMPLE

cmd.auto_arg[0]['test']=[lambda: cmd.Shortcut(['abc','bcd']), '1st argument', ', ']

This code defines the auto-completion list for the first argument of command test. When you type 'test ' and press TAB, PyMOL will show you two candidates as:

parser: matching 1st argument:
     abc  bcd 

If you type 'a' and press TAB again, PyMOL will complete it to "test abc, ". Note that ', ' is added.

Pre-defined lambdas

In most cases, you just want to complete from object names or setting lists. Then you don't have to write your own lambda function. Just use cmd.object_sc for choosing from objects.

cmd.auto_arg[0]['test'] = [ cmd.object_sc, 'object', '']

Or even simpler, borrow from one of the core commands:

# "extract" also uses cmd.object_sc in its first argument
cmd.auto_arg[0]['test'] = cmd.auto_arg[0]['extract']

Use cmd.selection_sc for setting names (like 'line_width'), cmd.map_sc for maps and cmd.repres_sc for representations ('sticks', 'lines', etc).

More Examples

The first argument of the save command auto-completes to filenames by default. If you want for example filenames and object names to auto-complete, use this (from pymol-users mailing list):

import glob
names_filenames_sc = lambda: cmd.Shortcut(cmd.get_names() + glob.glob('*'))
cmd.auto_arg[0]['save'] = [names_filenames_sc, 'filename or object name', '']

SEE ALSO

  • extend
  • parser.py, cmd.py, shortcut.py, completing.py