comparison runtime/autoload/dist/man.vim @ 32837:050794aa4ef2

man.vim: Recognise hyphenated-at-eol cross-references (#12609) Commit: https://github.com/vim/vim/commit/8cfe52e6fbf44032cd40d1561e93644786b15ee7 Author: goweol <goweol@gmail.com> Date: Fri Aug 18 06:13:29 2023 +0900 man.vim: Recognise hyphenated-at-eol cross-references (https://github.com/vim/vim/issues/12609) Manual pages requested for output may undergo formatting arranged by some roff-descendant program. Lines longer than MANWIDTH or COLUMNS or real-estate width of a device (with support for horizontal scrolling considered) can be divided at either blank characters and/or at groups of word characters (syllables) according to supported hyphenation rules (although page authors are free to disable hyphenation or prevent particular words from being hyphenated). Groff?s manual describes it as follows: 5.1.2 Hyphenation Since the odds are not great for finding a set of words, for every output line, which fit nicely on a line without inserting excessive amounts of space between words, gtroff hyphenates words so that it can justify lines without inserting too much space between words. It uses an internal hyphenation algorithm (a simplified version of the algorithm used within TeX) to indicate which words can be hyphenated and how to do so. When a word is hyphenated, the first part of the word is added to the current filled line being output (with an attached hyphen), and the other portion is added to the next line to be filled. It would be expedient for autoload/dist/man.vim (along with syntax/man.vim?s highlighting and ftplugin/man.vim?s Ctrl-], \K mappings) to allow for hyphenation of cross-references to manual pages. For example, # Launch Vim [v9.0; patched: 1-1378, 1499] as follows: MANWIDTH=80 vim --not-a-term +MANPAGER '+Man man' '+/conv(1)' '+norm B' # Press Ctrl-] with cursor on _m_: "... use man? # conv(1) directly."_______________________[^] # # (Man v2.11.2) # Launch Vim as follows: MANWIDTH=80 vim --not-a-term +MANPAGER '+Man git' '+/config(1)' '+norm B' # Press Ctrl-] with cursor on _g_: "... in git- # config(1) for a more ..."_______________[^] # # (Git v2.39.2) Co-authored-by: Aliaksei Budavei <0x000c70@gmail.com>
author Christian Brabandt <cb@256bit.org>
date Thu, 17 Aug 2023 23:15:07 +0200
parents f0854888250f
children 4ae88f9389b4
comparison
equal deleted inserted replaced
32836:f0854888250f 32837:050794aa4ef2
19 endif 19 endif
20 catch /E145:/ 20 catch /E145:/
21 " Ignore the error in restricted mode 21 " Ignore the error in restricted mode
22 endtry 22 endtry
23 23
24 func s:ParseIntoPageAndSection()
25 " Accommodate a reference that terminates in a hyphen.
26 "
27 " See init_charset_table() at
28 " https://git.savannah.gnu.org/cgit/groff.git/tree/src/roff/troff/input.cpp?h=1.22.4#n6794
29 "
30 " See can_break_after() at
31 " https://git.savannah.gnu.org/cgit/groff.git/tree/src/roff/troff/charinfo.h?h=1.22.4#n140
32 "
33 " Assumptions and limitations:
34 " 1) Manual-page references (in consequence of command-related filenames)
35 " do not contain non-ASCII HYPHENs (0x2010), any terminating HYPHEN
36 " must have been introduced to mark division of a word at the end of
37 " a line and can be discarded; whereas similar references may contain
38 " ASCII HYPHEN-MINUSes (0x002d) and any terminating HYPHEN-MINUS forms
39 " a compound word in addition to marking word division.
40 " 2) Well-formed manual-page references always have a section suffix, e.g.
41 " "git-commit(1)", therefore suspended hyphenated compounds are not
42 " determined, e.g. [V] (With cursor at _git-merge-_ below...)
43 " ".................... git-merge- and git-merge-base. (See git-cherry-
44 " pick(1) and git-cherry(1).)" (... look up "git-merge-pick(1)".)
45 "
46 " Note that EM DASH (0x2014), a third stooge from init_charset_table(),
47 " neither connects nor divides parts of a word.
48 let str = expand("<cWORD>")
49
50 if str =~ '\%u2010$' " HYPHEN (-1).
51 let str = strpart(str, 0, strridx(str, "\u2010"))
52
53 " Append the leftmost WORD (or an empty string) from the line below.
54 let str .= get(split(get(getbufline(bufnr('%'), line('.') + 1), 0, '')), 0, '')
55 elseif str =~ '-$' " HYPHEN-MINUS.
56 " Append the leftmost WORD (or an empty string) from the line below.
57 let str .= get(split(get(getbufline(bufnr('%'), line('.') + 1), 0, '')), 0, '')
58 endif
59
60 " According to man(1), section name formats vary (MANSECT):
61 " 1 n l 8 3 2 3posix 3pm 3perl 3am 5 4 9 6 7
62 let parts = matchlist(str, '\(\k\+\)(\(\k\+\))')
63 return (len(parts) > 2)
64 \ ? {'page': parts[1], 'section': parts[2]}
65 \ : {'page': matchstr(str, '\k\+'), 'section': ''}
66 endfunc
67
24 func dist#man#PreGetPage(cnt) 68 func dist#man#PreGetPage(cnt)
25 if a:cnt == 0 69 if a:cnt == 0
26 let old_isk = &iskeyword 70 let what = s:ParseIntoPageAndSection()
27 if &ft == 'man' 71 let sect = what.section
28 setl iskeyword+=(,) 72 let page = what.page
29 endif
30 let str = expand("<cword>")
31 let &l:iskeyword = old_isk
32 let page = substitute(str, '(*\(\k\+\).*', '\1', '')
33 let sect = substitute(str, '\(\k\+\)(\([^()]*\)).*', '\2', '')
34 if match(sect, '^[0-9 ]\+$') == -1
35 let sect = ""
36 endif
37 if sect == page
38 let sect = ""
39 endif
40 else 73 else
74 let what = s:ParseIntoPageAndSection()
41 let sect = a:cnt 75 let sect = a:cnt
42 let page = expand("<cword>") 76 let page = what.page
43 endif 77 endif
78
44 call dist#man#GetPage('', sect, page) 79 call dist#man#GetPage('', sect, page)
45 endfunc 80 endfunc
46 81
47 func s:GetCmdArg(sect, page) 82 func s:GetCmdArg(sect, page)
48
49 if empty(a:sect) 83 if empty(a:sect)
50 return shellescape(a:page) 84 return shellescape(a:page)
51 endif 85 endif
52 86
53 return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page) 87 return s:man_sect_arg . ' ' . shellescape(a:sect) . ' ' . shellescape(a:page)
73 let page = a:1 107 let page = a:1
74 else 108 else
75 return 109 return
76 endif 110 endif
77 111
78 " To support: nmap K :Man <cword> 112 " To support: nmap K :Man <cWORD><CR>
79 if page == '<cword>' 113 if page ==? '<cword>'
80 let page = expand('<cword>') 114 let what = s:ParseIntoPageAndSection()
115 let sect = what.section
116 let page = what.page
81 endif 117 endif
82 118
83 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0) 119 if !exists('g:ft_man_no_sect_fallback') || (g:ft_man_no_sect_fallback == 0)
84 if sect != "" && s:FindPage(sect, page) == 0 120 if sect != "" && s:FindPage(sect, page) == 0
85 let sect = "" 121 let sect = ""