Function: Man-default-man-entry
Man-default-man-entry is a byte-compiled function defined in
man.el.gz.
Signature
(Man-default-man-entry &optional POS)
Documentation
Guess default manual entry based on the text near position POS.
POS defaults to point.
Source Code
;; Defined in /usr/src/emacs/lisp/man.el.gz
;; ======================================================================
;; default man entry: get word near point
(defun Man-default-man-entry (&optional pos)
"Guess default manual entry based on the text near position POS.
POS defaults to `point'."
(let (word start column distance)
(save-excursion
(when pos (goto-char pos))
(setq pos (point))
;; The default title is the nearest entry-like object before or
;; after POS.
(if (and (skip-chars-backward " \ta-zA-Z0-9+")
(not (zerop (skip-chars-backward "(")))
;; Try to handle the special case where POS is on a
;; section number.
(looking-at
(concat "([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
;; We skipped a valid section number backwards, look at
;; preceding text.
(or (and (skip-chars-backward ",; \t")
(not (zerop (skip-chars-backward "-a-zA-Z0-9._+:"))))
;; Not a valid entry, move POS after closing paren.
(not (setq pos (match-end 0)))))
;; We have a candidate, make `start' record its starting
;; position.
(setq start (point))
;; Otherwise look at char before POS.
(goto-char pos)
(if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
;; Our candidate is just before or around POS.
(setq start (point))
;; Otherwise record the current column and look backwards.
(setq column (current-column))
(skip-chars-backward ",; \t")
;; Record the distance traveled.
(setq distance (- column (current-column)))
(when (looking-back
(concat "([ \t]*\\(?:" Man-section-regexp "\\)[ \t]*)")
(line-beginning-position))
;; Skip section number backwards.
(goto-char (match-beginning 0))
(skip-chars-backward " \t"))
(if (not (zerop (skip-chars-backward "-a-zA-Z0-9._+:")))
(progn
;; We have a candidate before POS ...
(setq start (point))
(goto-char pos)
(if (and (skip-chars-forward ",; \t")
(< (- (current-column) column) distance)
(looking-at "[-a-zA-Z0-9._+:]"))
;; ... but the one after POS is better.
(setq start (point))
;; ... and anything after POS is worse.
(goto-char start)))
;; No candidate before POS.
(goto-char pos)
(skip-chars-forward ",; \t")
(setq start (point)))))
;; We have found a suitable starting point, try to skip at least
;; one character.
(skip-chars-forward "-a-zA-Z0-9._+:")
(setq word (buffer-substring-no-properties start (point)))
;; If there is a continuation at the end of line, check the
;; following line too, eg:
;; see this-
;; command-here(1)
;; Note: This code gets executed iff our entry is after POS.
(when (looking-at
(concat
"‐?[ \t\r\n]+\\([-a-zA-Z0-9._+:]+\\)(" Man-section-regexp ")"))
(let ((1st-part word))
(setq word (concat word (match-string-no-properties 1)))
;; If they use -Tascii, we cannot know whether a hyphen at
;; EOL is or isn't part of the referenced manpage name.
;; Heuristics: if the part of the manpage before the hyphen
;; doesn't include a hyphen, we consider the hyphen to be
;; added by troff, and remove it.
(or (not (eq (string-to-char (substring 1st-part -1)) ?-))
(string-search "-" (substring 1st-part 0 -1))
(setq word (string-replace "-" "" word))))
;; Make sure the section number gets included by the code below.
(goto-char (match-end 1)))
(when (string-match "[-._‐]+$" word)
(setq word (substring word 0 (match-beginning 0))))
;; The following was commented out since the preceding code
;; should not produce a leading "*" in the first place.
;;; ;; If looking at something like *strcat(... , remove the '*'
;;; (when (string-match "^*" word)
;;; (setq word (substring word 1)))
(concat
word
(and (not (string-equal word ""))
;; If looking at something like ioctl(2) or brc(1M),
;; include the section number in the returned value.
(looking-at
(concat "[ \t]*([ \t]*\\(" Man-section-regexp "\\)[ \t]*)"))
(format "(%s)" (match-string-no-properties 1)))))))