/*
   * Take a list of commands and return all possible syntaxes
   * for these commands
   */
  private List<String> getSyntaxes(List<String> commands) {
    if (commands == null) {
      return null;
    }
    ArrayList<String> syntaxes = new ArrayList<String>();
    for (String cmd : commands) {

      String cmdInt = app.getInternalCommand(cmd);

      String syntaxString;
      if (isCASInput) {
        syntaxString = app.getCommandSyntaxCAS(cmdInt);
      } else {
        syntaxString = app.getCommandSyntax(cmdInt);
      }
      if (syntaxString.endsWith(isCASInput ? app.syntaxCAS : app.syntaxStr)) {

        // command not found, check for macros
        Macro macro = isCASInput ? null : app.getKernel().getMacro(cmd);
        if (macro != null) {
          syntaxes.add(macro.toString());
        } else {
          // syntaxes.add(cmdInt + "[]");
          Application.debug("Can't find syntax for: " + cmd);
        }

        continue;
      }
      for (String syntax : syntaxString.split("\\n")) {
        syntaxes.add(syntax);
      }
    }
    return syntaxes;
  }
  public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    // we don't want to trap AltGr
    // as it is used eg for entering {[}] is some locales
    // NB e.isAltGraphDown() doesn't work
    if (e.isAltDown() && e.isControlDown()) return;

    // swallow eg ctrl-a ctrl-b ctrl-p on Mac
    if (Application.MAC_OS && e.isControlDown()) e.consume();

    ctrlC = false;

    switch (keyCode) {
      case KeyEvent.VK_Z:
      case KeyEvent.VK_Y:
        if (Application.isControlDown(e)) {
          app.getGlobalKeyDispatcher().handleGeneralKeys(e);
          e.consume();
        }
        break;

      case KeyEvent.VK_0:
      case KeyEvent.VK_1:
      case KeyEvent.VK_2:
      case KeyEvent.VK_3:
      case KeyEvent.VK_4:
      case KeyEvent.VK_5:
      case KeyEvent.VK_6:
      case KeyEvent.VK_7:
      case KeyEvent.VK_8:
      case KeyEvent.VK_9:
        if (Application.isControlDown(e) && e.isShiftDown())
          app.getGlobalKeyDispatcher().handleGeneralKeys(e);
        break;

        // process input
      case KeyEvent.VK_C:
        if (Application.isControlDown(e)) // workaround for MAC_OS
        {
          ctrlC = true;
        }

        break;

      case KeyEvent.VK_ESCAPE:
        if (!handleEscapeKey) {
          break;
        }

        Component comp = SwingUtilities.getRoot(this);
        if (comp instanceof JDialog) {
          ((JDialog) comp).setVisible(false);
          return;
        }

        setText(null);
        break;

      case KeyEvent.VK_LEFT_PARENTHESIS:
        break;

      case KeyEvent.VK_UP:
        if (!handleEscapeKey) {
          break;
        }
        if (historyPopup == null) {
          String text = getPreviousInput();
          if (text != null) setText(text);
        } else if (!historyPopup.isDownPopup()) {
          historyPopup.showPopup();
        }
        break;

      case KeyEvent.VK_DOWN:
        if (!handleEscapeKey) {
          break;
        }
        if (historyPopup != null && historyPopup.isDownPopup()) historyPopup.showPopup();
        else setText(getNextInput());
        break;

      case KeyEvent.VK_F9:
        // needed for applets
        if (app.isApplet()) app.getGlobalKeyDispatcher().handleGeneralKeys(e);
        break;

      case KeyEvent.VK_RIGHT:
        if (moveToNextArgument(false)) {
          e.consume();
        }
        break;

      case KeyEvent.VK_TAB:
        if (moveToNextArgument(true)) {
          e.consume();
        }
        break;

      case KeyEvent.VK_F1:
        if (autoComplete) {
          if (getText().equals("")) {

            Object[] options = {app.getPlain("OK"), app.getPlain("ShowOnlineHelp")};
            int n =
                JOptionPane.showOptionDialog(
                    app.getMainComponent(),
                    app.getPlain("InputFieldHelp"),
                    app.getPlain("ApplicationName") + " - " + app.getMenu("Help"),
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null, // do not use a custom Icon
                    options, // the titles of buttons
                    options[0]); // default button title

            if (n == 1) app.getGuiManager().openHelp(Application.WIKI_MANUAL);

          } else {
            int pos = getCaretPosition();
            while (pos > 0 && getText().charAt(pos - 1) == '[') {
              pos--;
            }
            String word = getWordAtPos(getText(), pos);
            String lowerCurWord = word.toLowerCase();
            String closest = dict.lookup(lowerCurWord);

            if (closest != null) // && lowerCurWord.equals(closest.toLowerCase()))		
            showCommandHelp(app.getInternalCommand(closest));
            else app.getGuiManager().openHelp(Application.WIKI_MANUAL);
          }
        } else app.getGuiManager().openHelp(Application.WIKI_MANUAL);

        e.consume();
        break;
      default:
    }
  }