Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.3 KiB

5 years ago
  1. ;;; helm-man.el --- Man and woman UI -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2012 ~ 2019 Thierry Volpiatto <thierry.volpiatto@gmail.com>
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'cl-lib)
  15. (require 'helm)
  16. (require 'helm-help)
  17. (defvar woman-topic-all-completions)
  18. (defvar woman-manpath)
  19. (defvar woman-path)
  20. (defvar woman-expanded-directory-path)
  21. (declare-function woman-file-name "woman.el" (topic &optional re-cache))
  22. (declare-function woman-file-name-all-completions "woman.el" (topic))
  23. (declare-function Man-getpage-in-background "man.el" (topic))
  24. (declare-function woman-expand-directory-path "woman.el" (path-dirs path-regexps))
  25. (declare-function woman-topic-all-completions "woman.el" (path))
  26. (declare-function helm-generic-sort-fn "helm-utils.el" (S1 S2))
  27. (defgroup helm-man nil
  28. "Man and Woman applications for helm."
  29. :group 'helm)
  30. (defcustom helm-man-or-woman-function 'Man-getpage-in-background
  31. "Default command to display a man page."
  32. :group 'helm-man
  33. :type '(radio :tag "Preferred command to display a man page"
  34. (const :tag "Man" Man-getpage-in-background)
  35. (const :tag "Woman" woman)))
  36. (defcustom helm-man-format-switches (cl-case system-type
  37. ((darwin macos) "%s")
  38. (t "-l %s"))
  39. "Arguments to pass to the `manual-entry' function.
  40. Arguments are passed to `manual-entry' with `format.'"
  41. :group 'helm-man
  42. :type 'string)
  43. ;; Internal
  44. (defvar helm-man--pages nil
  45. "All man pages on system.
  46. Will be calculated the first time you invoke helm with this
  47. source.")
  48. (defun helm-man-default-action (candidate)
  49. "Default action for jumping to a woman or man page from helm."
  50. (let ((wfiles (mapcar #'car (woman-file-name-all-completions candidate))))
  51. (condition-case nil
  52. (let ((file (if (cdr wfiles)
  53. (helm-comp-read "ManFile: " wfiles :must-match t)
  54. (car wfiles))))
  55. (if (eq helm-man-or-woman-function 'Man-getpage-in-background)
  56. (manual-entry (format helm-man-format-switches file))
  57. (condition-case nil
  58. (woman-find-file file)
  59. ;; If woman is unable to format correctly
  60. ;; try Man instead.
  61. (error (kill-buffer)
  62. (manual-entry (format helm-man-format-switches file))))))
  63. ;; If even Man failed with file as argument, try again with Man
  64. ;; but using Topic candidate instead of the file calculated by
  65. ;; woman.
  66. (error (kill-buffer)
  67. (Man-getpage-in-background candidate)))))
  68. (defun helm-man--init ()
  69. (require 'woman)
  70. (require 'helm-utils)
  71. (unless helm-man--pages
  72. (setq woman-expanded-directory-path
  73. (woman-expand-directory-path woman-manpath woman-path))
  74. (setq woman-topic-all-completions
  75. (woman-topic-all-completions woman-expanded-directory-path))
  76. (setq helm-man--pages (mapcar 'car woman-topic-all-completions)))
  77. (helm-init-candidates-in-buffer 'global helm-man--pages))
  78. (defvar helm-source-man-pages
  79. (helm-build-in-buffer-source "Manual Pages"
  80. :init #'helm-man--init
  81. :persistent-action #'ignore
  82. :filtered-candidate-transformer
  83. (lambda (candidates _source)
  84. (sort candidates #'helm-generic-sort-fn))
  85. :action '(("Display Man page" . helm-man-default-action))
  86. :group 'helm-man))
  87. ;;;###autoload
  88. (defun helm-man-woman (arg)
  89. "Preconfigured `helm' for Man and Woman pages.
  90. With a prefix arg reinitialize the cache."
  91. (interactive "P")
  92. (when arg (setq helm-man--pages nil))
  93. (helm :sources 'helm-source-man-pages
  94. :buffer "*helm man woman*"))
  95. (provide 'helm-man)
  96. ;; Local Variables:
  97. ;; byte-compile-warnings: (not obsolete)
  98. ;; coding: utf-8
  99. ;; indent-tabs-mode: nil
  100. ;; End:
  101. ;;; helm-man.el ends here