m-x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* m-x.c -- Meta-x minibuffer reader.
  2. $Id$
  3. Copyright 1993, 1997, 1998, 2001, 2002, 2004, 2007, 2008, 2011, 2013,
  4. 2014, 2017 Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Originally written by Brian Fox. */
  16. #include "info.h"
  17. #include "display.h"
  18. #include "session.h"
  19. #include "echo-area.h"
  20. #include "funs.h"
  21. /* **************************************************************** */
  22. /* */
  23. /* Reading Named Commands */
  24. /* */
  25. /* **************************************************************** */
  26. /* Read the name of an Info function in the echo area and return the
  27. name. A return value of NULL indicates that no function name could
  28. be read. */
  29. char *
  30. read_function_name (char *prompt, WINDOW *window)
  31. {
  32. register int i;
  33. char *line;
  34. REFERENCE **array = NULL;
  35. size_t array_index = 0, array_slots = 0;
  36. /* Make an array of REFERENCE which actually contains the names of
  37. the functions available in Info. */
  38. for (i = 0; function_doc_array[i].func; i++)
  39. {
  40. REFERENCE *entry;
  41. entry = xmalloc (sizeof (REFERENCE));
  42. entry->label = xstrdup (function_doc_array[i].func_name);
  43. entry->nodename = NULL;
  44. entry->filename = NULL;
  45. add_pointer_to_array (entry, array_index, array, array_slots, 200);
  46. }
  47. line = info_read_completing_in_echo_area (prompt, array);
  48. info_free_references (array);
  49. return line;
  50. }
  51. DECLARE_INFO_COMMAND (describe_command,
  52. _("Read the name of an Info command and describe it"))
  53. {
  54. char *line;
  55. line = read_function_name (_("Describe command: "), window);
  56. if (!line)
  57. {
  58. info_abort_key (active_window, count);
  59. return;
  60. }
  61. /* Describe the function named in "LINE". */
  62. if (*line)
  63. {
  64. InfoCommand *cmd = named_function (line);
  65. if (!cmd)
  66. return;
  67. window_message_in_echo_area ("%s: %s.",
  68. line, function_documentation (cmd));
  69. }
  70. free (line);
  71. }
  72. DECLARE_INFO_COMMAND (info_execute_command,
  73. _("Read a command name in the echo area and execute it"))
  74. {
  75. char *line;
  76. char *keys;
  77. char *prompt;
  78. keys = where_is (info_keymap, InfoCmd(info_execute_command));
  79. /* If the where_is () function thinks that this command doesn't exist,
  80. there's something very wrong! */
  81. if (!keys)
  82. abort();
  83. if (info_explicit_arg || count != 1)
  84. asprintf (&prompt, "%d %s ", count, keys);
  85. else
  86. asprintf (&prompt, "%s ", keys);
  87. /* Ask the completer to read a reference for us. */
  88. line = read_function_name (prompt, window);
  89. free (prompt);
  90. /* User aborted? */
  91. if (!line)
  92. {
  93. info_abort_key (active_window, count);
  94. return;
  95. }
  96. /* User accepted "default"? (There is none.) */
  97. if (!*line)
  98. {
  99. free (line);
  100. return;
  101. }
  102. /* User wants to execute a named command. Do it. */
  103. {
  104. InfoCommand *command;
  105. if ((active_window != the_echo_area) &&
  106. (strncmp (line, "echo-area-", 10) == 0))
  107. {
  108. free (line);
  109. info_error (_("Cannot execute an 'echo-area' command here."));
  110. return;
  111. }
  112. command = named_function (line);
  113. free (line);
  114. if (command && command->func)
  115. (*command->func) (active_window, count, 0);
  116. }
  117. }
  118. /* Okay, now that we have M-x, let the user set the screen height. */
  119. DECLARE_INFO_COMMAND (set_screen_height,
  120. _("Set the height of the displayed window"))
  121. {
  122. int new_height, old_height = screenheight;
  123. if (info_explicit_arg || count != 1)
  124. new_height = count;
  125. else
  126. {
  127. char prompt[80];
  128. char *line;
  129. new_height = screenheight;
  130. sprintf (prompt, _("Set screen height to (%d): "), new_height);
  131. line = info_read_in_echo_area (prompt);
  132. /* If the user aborted, do that now. */
  133. if (!line)
  134. {
  135. info_abort_key (active_window, count);
  136. return;
  137. }
  138. /* Find out what the new height is supposed to be. */
  139. if (*line)
  140. new_height = atoi (line);
  141. free (line);
  142. }
  143. terminal_clear_screen ();
  144. display_clear_display (the_display);
  145. screenheight = new_height;
  146. #ifdef SET_SCREEN_SIZE_HELPER
  147. SET_SCREEN_SIZE_HELPER;
  148. #endif
  149. if (screenheight == old_height)
  150. {
  151. /* Display dimensions didn't actually change, so
  152. window_new_screen_size won't do anything, but we've
  153. already cleared the display above. Undo the damage. */
  154. window_mark_chain (windows, W_UpdateWindow);
  155. display_update_display ();
  156. }
  157. else
  158. {
  159. display_initialize_display (screenwidth, screenheight);
  160. window_new_screen_size (screenwidth, screenheight);
  161. }
  162. }