From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Wolfgang Jenkner Newsgroups: gmane.emacs.bugs Subject: bug#13160: 24.3.50; [PATCH] man page completion support beyond man-db Date: Wed, 12 Dec 2012 20:16:45 +0100 Message-ID: <856247krc6.fsf@iznogoud.viz> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1355340271 8344 80.91.229.3 (12 Dec 2012 19:24:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 12 Dec 2012 19:24:31 +0000 (UTC) Cc: Kevin Ryde To: 13160@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Dec 12 20:24:42 2012 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Tirv5-0007vY-JJ for geb-bug-gnu-emacs@m.gmane.org; Wed, 12 Dec 2012 20:24:39 +0100 Original-Received: from localhost ([::1]:41689 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tirus-0008MP-Li for geb-bug-gnu-emacs@m.gmane.org; Wed, 12 Dec 2012 14:24:26 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:60815) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tirun-0008Lu-Gs for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:24:25 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tirug-00053C-P8 for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:24:21 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:57384) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tirug-000537-LJ for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:24:14 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1TirvS-0003kN-CB for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:25:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Wolfgang Jenkner Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 12 Dec 2012 19:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 13160 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.135534027114359 (code B ref -1); Wed, 12 Dec 2012 19:25:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 12 Dec 2012 19:24:31 +0000 Original-Received: from localhost ([127.0.0.1]:39402 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1Tiruw-0003jX-Ad for submit@debbugs.gnu.org; Wed, 12 Dec 2012 14:24:31 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:49321) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1Tirus-0003jP-TO for submit@debbugs.gnu.org; Wed, 12 Dec 2012 14:24:29 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tiru0-0004wo-7o for submit@debbugs.gnu.org; Wed, 12 Dec 2012 14:23:38 -0500 Original-Received: from lists.gnu.org ([208.118.235.17]:51214) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tiru0-0004wi-4b for submit@debbugs.gnu.org; Wed, 12 Dec 2012 14:23:32 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:60625) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tirtr-0008Fu-Hu for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:23:32 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tirtg-0004sf-Ue for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:23:23 -0500 Original-Received: from mx09.lb01.inode.at ([62.99.145.9]:59476 helo=mx.inode.at) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tirtg-0004s0-Fc for bug-gnu-emacs@gnu.org; Wed, 12 Dec 2012 14:23:12 -0500 Original-Received: from [91.119.206.19] (port=11862 helo=iznogoud.viz) by smartmx-09.inode.at with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1Tirtd-0005m4-Gm; Wed, 12 Dec 2012 20:23:09 +0100 Original-Received: from wolfgang by iznogoud.viz with local (Exim 4.80.1 (FreeBSD)) (envelope-from ) id 1TirtZ-0000b2-Rg; Wed, 12 Dec 2012 20:23:05 +0100 User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.3.50 (berkeley-unix) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:68415 Archived-At: In lisp/man.el, completion of man pages and sections depends on parsing the output of `man -k'. As a bonus, this method provides short descriptions in the form of tooltips in the *Completions* buffer. However, the code only handles the `man -k' output format peculiar to the man-db package, which, even on GNU/Linux, is not the only one used for this purpose. For example, Slackware seems to use man-1.6g, whose `man -k' output can have an additional reference in square brackets between the page name and the section, so that man topic completion does work in this case, but neither sections nor descriptions are parsed. The situation is even worse for FreeBSD, where multiple comma-separated page names can be shown on the same line (which is arguably against the spirit of POSIX man(1p), but that's not quite clear and doesn't help much). The last major discussion of man completion issues seems to be bug#3717, through which I skimmed, but there doesn't seem to be anything related to this kind of portability. There is, however, some prior code in the `man-completion-cache' function in ftp://download.tuxfamily.org/user42/man-completion.el to deal with this problem. In particular, it handles the example boot-scripts (7) [boot] - General description of boot sequence where the present patch would miss the section. However, I don't know of a man program which actually produces this format (man-1.6g has the reference in square brackets /preceding/ the section). Otherwise, the following patch, which I'd like to install, should handle more formats. It mainly adds a single separate function for parsing various formats. There's also an ert test with examples for what I described above and other formats found on the web. Wolfgang === modified file 'lisp/man.el' --- lisp/man.el 2012-08-15 16:29:11 +0000 +++ lisp/man.el 2012-12-12 16:00:11 +0000 @@ -780,6 +780,57 @@ ;; but apparently that's not the case in all cases, so let's add a cache. "Cache of completion table of the form (PREFIX . TABLE).") +(defvar Man-keyword-completion-twiddle + (cond + ;; man-db-* or man-*. + ((memq system-type '(gnu gnu/linux gnu/kfreebsd)) + "^") + (t + "")) + "A string prepended to the keyword passed to `man -k' for completion. +This should usually be an extended regular expression. + +Actually, unwanted page names are weeded out anyway, so with an +empty string here we will always get correct results, but things +may take a bit longer.") + +(defun Man-parse-apropos (&optional table) + "Parse `man -k' output, prepend page names to TABLE and return that. + +The current buffer should contain the output of a command of the +form `man -k keyword', which is traditionally also available with +apropos(1). + +While POSIX man(1p) is a bit vague about what to expect here, +this function tries to parse some commonly used formats, which +can be described in the following informal way, with square brackets +indicating optional parts and whitespace being interpreted +somewhat loosely. + +foo[, bar [, ...]] [other stuff] (sec) - description +foo(sec)[, bar(sec) [, ...]] [other stuff] - description + +For more details and some regression tests, please see +test/automated/man-tests.el in the emacs bzr repository." + (goto-char (point-min)) + (while (search-forward-regexp "^\\([^ \t,\n]+\\)\\(.*?\\)\ +\\(?:[ \t]\\(([^ \t,\n]+?)\\)\\)?\\(?:[ \t]+- ?\\(.*\\)\\)?$" nil t) + (let ((section (match-string 3)) + (description (match-string 4)) + (bound (match-end 2)) + (continue (goto-char (match-end 1)))) + (while continue + ;; The first regexp grouping may already match the section + ;; tacked on to the name, which is ok since for the formats we + ;; claim to support the third (non-shy) grouping does not + ;; match in this case, i.e., section is nil. + (push (propertize (concat (match-string 1) section) + 'help-echo description) + table) + (setq continue (search-forward-regexp "\\=, *\\([^ \t,]+\\)" + bound t))))) + table) + (defun Man-completion-table (string pred action) (cond ;; This ends up returning t for pretty much any string, and hence leads to @@ -813,12 +864,9 @@ ;; error later. (ignore-errors (call-process manual-program nil '(t nil) nil - "-k" (concat "^" prefix)))) - (goto-char (point-min)) - (while (re-search-forward "^\\([^ \t\n]+\\)\\(?: ?\\((.+?)\\)\\(?:[ \t]+- \\(.*\\)\\)?\\)?" nil t) - (push (propertize (concat (match-string 1) (match-string 2)) - 'help-echo (match-string 3)) - table))) + "-k" (concat Man-keyword-completion-twiddle + prefix)))) + (setq table (Man-parse-apropos table))) ;; Cache the table for later reuse. (setq Man-completion-cache (cons prefix table))) ;; The table may contain false positives since the match is made === added file 'test/automated/man-tests.el' --- test/automated/man-tests.el 1970-01-01 00:00:00 +0000 +++ test/automated/man-tests.el 2012-12-08 17:10:24 +0000 @@ -0,0 +1,118 @@ +;;; man-tests.el --- Test suite for man. + +;; Copyright (C) 2012 Free Software Foundation, Inc. + +;; Author: Wolfgang Jenkner +;; Keywords: help, internal, unix + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Code: + +(require 'ert) +(require 'man) + +(defconst man-tests-parse-apropos-tests + '(;; GNU/Linux: man-db-2.6.1 + ("\ +sin (3) - sine function +sinf (3) - sine function +sinl (3) - sine function" + . (#("sinl(3)" 0 7 (help-echo "sine function")) #("sinf(3)" 0 7 (help-echo "sine function")) #("sin(3)" 0 6 (help-echo "sine function")))) + ;; GNU/Linux: man-1.6g + ("\ +sin (3) - sine function +sinf [sin] (3) - sine function +sinl [sin] (3) - sine function" + . (#("sinl(3)" 0 7 (help-echo "sine function")) #("sinf(3)" 0 7 (help-echo "sine function")) #("sin(3)" 0 6 (help-echo "sine function")))) + ;; FreeBSD 9 + ("\ +sin(3), sinf(3), sinl(3) - sine functions" + . (#("sinl(3)" 0 7 (help-echo "sine functions")) #("sinf(3)" 0 7 (help-echo "sine functions")) #("sin(3)" 0 6 (help-echo "sine functions")))) + ;; SunOS, Solaris + ;; http://docs.oracle.com/cd/E19455-01/805-6331/usradm-7/index.html + ;; SunOS 4 + ("\ +tset, reset (1) - establish or restore terminal characteristics" + . (#("reset(1)" 0 8 (help-echo "establish or restore terminal characteristics")) #("tset(1)" 0 7 (help-echo "establish or restore terminal characteristics")))) + ;; SunOS 5.7, Solaris + ("\ +reset tset (1b) - establish or restore terminal characteristics +tset tset (1b) - establish or restore terminal characteristics" + . (#("tset(1b)" 0 7 (help-echo "establish or restore terminal characteristics")) #("reset(1b)" 0 8 (help-echo "establish or restore terminal characteristics")))) + ;; Minix 3 + ;; http://www.minix3.org/manpages/html5/whatis.html + ("\ +cawf, nroff (1) - C version of the nroff-like, Amazingly Workable (text) Formatter +whatis (5) - database of online manual pages" + . (#("whatis(5)" 0 9 (help-echo "database of online manual pages")) #("nroff(1)" 0 8 (help-echo "C version of the nroff-like, Amazingly Workable (text) Formatter")) #("cawf(1)" 0 7 (help-echo "C version of the nroff-like, Amazingly Workable (text) Formatter")))) + ;; HP-UX + ;; http://docstore.mik.ua/manuals/hp-ux/en/B2355-60130/man.1.html + ;; Assuming that the line break in the zgrep description was + ;; introduced by the man page formatting. + ("\ +grep, egrep, fgrep (1) - search a file for a pattern +zgrep(1) - search possibly compressed files for a regular expression" + . (#("zgrep(1)" 0 8 (help-echo "search possibly compressed files for a regular expression")) #("fgrep(1)" 0 8 (help-echo "search a file for a pattern")) #("egrep(1)" 0 8 (help-echo "search a file for a pattern")) #("grep(1)" 0 7 (help-echo "search a file for a pattern")))) + ;; AIX + ;; http://pic.dhe.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.cmds/doc/aixcmds6/whatis.htm + ("\ +ls(1) -Displays the contents of a directory." + . (#("ls(1)" 0 5 (help-echo "Displays the contents of a directory.")))) + ;; https://www.ibm.com/developerworks/mydeveloperworks/blogs/cgaix/entry/catman_0703_102_usr_lbin_mkwhatis_the_error_number_is_1?lang=en + ("\ +loopmount(1) - Associate an image file to a loopback device." + . (#("loopmount(1)" 0 12 (help-echo "Associate an image file to a loopback device.")))) + ) + "List of tests for `Man-parse-apropos'. +Each element is a cons cell whose car is a string containing +man -k output. That should result in the table which is stored +in the cdr of the element.") + +(defun man-tests-name-equal-p (name description string) + (and (equal name string) + (not (next-single-property-change 0 'help-echo string)) + (equal (get-text-property 0 'help-echo string) description))) + +(defun man-tests-parse-apropos-test-case (test) + (let ((temp-buffer (get-buffer-create " *test-man*")) + (apropos-output (car test))) + (unwind-protect + (save-window-excursion + (with-current-buffer temp-buffer + (erase-buffer) + (insert apropos-output) + (let ((result (Man-parse-apropos)) + (checklist (cdr test))) + (while (and checklist result + (man-tests-name-equal-p + (car checklist) + (get-text-property 0 'help-echo + (car checklist)) + (pop result))) + (pop checklist)) + (and (null checklist) (null result))))) + (and (buffer-name temp-buffer) + (kill-buffer temp-buffer))))) + +(ert-deftest man-tests () + "Test man." + (dolist (test man-tests-parse-apropos-tests) + (should (man-tests-parse-apropos-test-case test)))) + +(provide 'man-tests) + +;;; man-tests.el ends here