note 94855 added to function.readline-completion-function

From: Date: Sun, 29 Nov 2009 04:39:01 +0000
Subject: note 94855 added to function.readline-completion-function
Groups: php.notes 
Request: Send a blank email to php-notes+get-163367@lists.php.net to get a copy of this message
A little bit of info regarding useful variables when writing your callback function. There doesn't seem to be a way to set rl_basic_word_break_characters like with the pure C library, so as previous users have pointed out you'll only receive the current word in the input buffer within your callback. If for example you're typing "big bro|ther", where the bar is the position of your cursor when you hit TAB, you'll receive (string) "brother" and (int) 4 as your callback parameters. However, it is possible (easily) to get more useful information about what the user has typed into the readline buffer. readline_info() is key here. It will return an array containing: "line_buffer" => (string) the entire contents of the line buffer (+ some bugs**) "point" => (int) the current position of the cursor in the buffer "end" => (int) the position of the last character in the buffer So for the example above you'd get: * line_buffer => "big brother" * point => 7 * end => 11 From this you can easily perform multi-word matches. ** NOTE: line_buffer seems to contain spurious data at the end of the string sometime. Fortunately since $end is provided you can substr() it to get the correct value. The matches you need to return are full words that can replace $input, so your algorithm might crudely look something like: <?php function your_callback($input, $index) { // Get info about the current buffer $rl_info = readline_info(); // Figure out what the entire input is $full_input = substr($rl_info['line_buffer'], 0, $rl_info['end']); $matches = array(); // Get all matches based on the entire input buffer foreach (phrases_that_begin_with($full_input) as $phrase) { // Only add the end of the input (where this word begins) // to the matches array $matches[] = substr($phrase, $index); } return $matches; } ?> ---- Server IP: 117.104.162.229 Probable Submitter: 124.168.97.163 (proxied: 124.168.97.163) ---- Manual Page -- http://www.php.net/manual/en/function.readline-completion-function.php Edit -- https://master.php.net/note/edit/94855 Del: integrated -- https://master.php.net/note/delete/94855/integrated Del: useless -- https://master.php.net/note/delete/94855/useless Del: bad code -- https://master.php.net/note/delete/94855/bad+code Del: spam -- https://master.php.net/note/delete/94855/spam Del: non-english -- https://master.php.net/note/delete/94855/non-english Del: in docs -- https://master.php.net/note/delete/94855/in+docs Del: other reasons-- https://master.php.net/note/delete/94855 Reject -- https://master.php.net/note/reject/94855 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163367) next »