mylisp/debian-doc.el
author Oleksandr Gavenko <gavenkoa@gmail.com>
Wed, 16 Jun 2021 12:50:08 +0300
changeset 1734 ae2c6a001464
parent 1666 06937ff1ec5f
permissions -rw-r--r--
Add some standard places to PATH if they are not set by login script. Rearrange the order of paths so system's are first, user's are last. For Cygwin this helps with Cygwin's paths to be situated before "C:/Windows" (Emacs is not started from a login shell on Windows!).

;;; debian-doc.el --- Debian doc-base integration with Emacs.

;;; Commentary:
;;

;;; Code:

(defvar debian-doc-dir "/usr/share/doc-base")
(defvar debian-doc-buffer-name "*debian-doc*")
(defvar debian-doc-buffer nil)

(defvar debian-doc-mode-map (make-sparse-keymap))

(defconst debian-doc-font-lock-keywords
  '(("^[^ ][^:\n]*: " . font-lock-function-name-face)
    ("^\\(Index\\|Files\\): \\(/.*\\)" 2 font-lock-doc-face)
    ("^Format: \\(.*\\)" 1 font-lock-type-face)
    ("^Document: \\(.*\\)" 1 font-lock-keyword-face)))

(defun debian-doc-mode ()
  "Debian-Doc mode for viewing doc-base information."
  (kill-all-local-variables)
  (setq major-mode 'debian-doc-mode
        mode-name "Debian-Doc")
  (use-local-map debian-doc-mode-map)
  (setq font-lock-defaults '((debian-doc-font-lock-keywords) t nil nil nil)))

(defvar debian-doc-completion-list nil)

;;;###autoload
(defun debian-doc (&optional prefix)
  "Build Debian-Doc buffer.

With PREFIX force to rebuild buffer from doc-base files."
  (interactive "P")
  (when prefix
    (kill-buffer debian-doc-buffer))
  (if (buffer-live-p debian-doc-buffer)
      (switch-to-buffer debian-doc-buffer)
    (setq debian-doc-completion-list nil)
    (setq debian-doc-buffer (get-buffer-create debian-doc-buffer-name))
    (switch-to-buffer debian-doc-buffer)
    (let ( (coding-system-for-read 'utf-8) )
      (mapc (lambda (file)
              (when (file-regular-p file)
                ;; (with-temp-buffer
                ;;   (insert-file-contents file))
                (insert "\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n\n")
                (insert-file-contents file)
                (goto-char (point-max))))
            (directory-files debian-doc-dir t)))
    (read-only-mode 1)
    (debian-doc-mode)
    (goto-char (point-min))))

;;;###autoload
(defun debian-doc-visit ()
  "Prompt for Debian package which represent docs and visit its entry in Debian-oc buffer."
  (interactive)
  (let (word start)
    (debian-doc)
    (unless debian-doc-completion-list
      (save-excursion
        (goto-char (point-min))
        (while (setq start (search-forward "Document: " nil t))
          (end-of-line)
          (push (buffer-substring-no-properties start (point)) debian-doc-completion-list))))
    (when (setq word (completing-read "Package: " debian-doc-completion-list nil t))
      (goto-char (point-min))
      (search-forward (concat "Document: " word) nil t)
      (recenter-top-bottom))))
(define-key debian-doc-mode-map (kbd "RET") 'debian-doc-visit-at-point)


(provide 'debian-doc)

;;; debian-doc.el ends here