help: add "man.viewer" config var to use "woman" or "konqueror"
This patch makes it possible to view man pages using other tools than the "man" program. It also implements support for emacs' "woman" and konqueror with the man KIO slave to view man pages. Note that "emacsclient" is used with option "-e" to launch "woman" on emacs and this works only on versions >= 22. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Tested-by: Xavier Maillard <xma@gnu.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Mar 7, 2008 at 08:46 UTC
649499845c274dc15809fe3c9162408c334fec57
1 file changed
+80
-1
help.c
+80
-1
@@ -8,6 +8,9 @@
8
#include "exec_cmd.h"
9
#include "common-cmds.h"
10
#include "parse-options.h"
11
+#include "run-command.h"
12
+
13
+static const char *man_viewer;
14
15
enum help_format {
16
HELP_FORMAT_MAN,
@@ -50,6 +53,8 @@ static int git_help_config(const char *var, const char *value)
53
help_format = parse_help_format(value);
54
return 0;
55
}
56
+ if (!strcmp(var, "man.viewer"))
57
+ return git_config_string(&man_viewer, var, value);
58
return git_default_config(var, value);
59
}
60
@@ -345,11 +350,85 @@ static void setup_man_path(void)
350
strbuf_release(&new_path);
351
}
352
353
+static int check_emacsclient_version(void)
354
+{
355
+ struct strbuf buffer = STRBUF_INIT;
356
+ struct child_process ec_process;
357
+ const char *argv_ec[] = { "emacsclient", "--version", NULL };
358
+ int version;
359
+
360
+ /* emacsclient prints its version number on stderr */
361
+ memset(&ec_process, 0, sizeof(ec_process));
362
+ ec_process.argv = argv_ec;
363
+ ec_process.err = -1;
364
+ ec_process.stdout_to_stderr = 1;
365
+ if (start_command(&ec_process)) {
366
+ fprintf(stderr, "Failed to start emacsclient.\n");
367
+ return -1;
368
+ }
369
+ strbuf_read(&buffer, ec_process.err, 20);
370
+ close(ec_process.err);
371
+
372
+ /*
373
+ * Don't bother checking return value, because "emacsclient --version"
374
+ * seems to always exits with code 1.
375
+ */
376
+ finish_command(&ec_process);
377
+
378
+ if (prefixcmp(buffer.buf, "emacsclient")) {
379
+ fprintf(stderr, "Failed to parse emacsclient version.\n");
380
+ strbuf_release(&buffer);
381
+ return -1;
382
+ }
383
+
384
+ strbuf_remove(&buffer, 0, strlen("emacsclient"));
385
+ version = atoi(buffer.buf);
386
+
387
+ if (version < 22) {
388
+ fprintf(stderr,
389
+ "emacsclient version '%d' too old (< 22).\n",
390
+ version);
391
+ strbuf_release(&buffer);
392
+ return -1;
393
+ }
394
+
395
+ strbuf_release(&buffer);
396
+ return 0;
397
+}
398
+
399
+static void exec_woman_emacs(const char *page)
400
+{
401
+ if (!check_emacsclient_version()) {
402
+ /* This works only with emacsclient version >= 22. */
403
+ struct strbuf man_page = STRBUF_INIT;
404
+ strbuf_addf(&man_page, "(woman \"%s\")", page);
405
+ execlp("emacsclient", "emacsclient", "-e", man_page.buf, NULL);
406
+ } else
407
+ execlp("man", "man", page, NULL);
408
+}
409
+
410
+static void exec_man_konqueror(const char *page)
411
+{
412
+ const char *display = getenv("DISPLAY");
413
+ if (display && *display) {
414
+ struct strbuf man_page = STRBUF_INIT;
415
+ strbuf_addf(&man_page, "man:%s(1)", page);
416
+ execlp("kfmclient", "kfmclient", "newTab", man_page.buf, NULL);
417
+ } else
418
+ execlp("man", "man", page, NULL);
419
+}
420
+
421
static void show_man_page(const char *git_cmd)
422
{
423
const char *page = cmd_to_page(git_cmd);
424
setup_man_path();
352
- execlp("man", "man", page, NULL);
425
+ if (!man_viewer || !strcmp(man_viewer, "man"))
426
+ execlp("man", "man", page, NULL);
427
+ if (!strcmp(man_viewer, "woman"))
428
+ exec_woman_emacs(page);
429
+ if (!strcmp(man_viewer, "konqueror"))
430
+ exec_man_konqueror(page);
431
+ die("'%s': unsupported man viewer.", man_viewer);
432
}
433
434
static void show_info_page(const char *git_cmd)