diff options
380 files changed, 30396 insertions, 18945 deletions
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS deleted file mode 100644 index 7d41c17894..0000000000 --- a/.github/CODEOWNERS +++ /dev/null @@ -1,3 +0,0 @@ -/.github/ @jamessan -/ci/ @jamessan -/scripts/ @jamessan diff --git a/.github/ISSUE_TEMPLATE/lsp_bug_report.yml b/.github/ISSUE_TEMPLATE/lsp_bug_report.yml index b5b7687bf8..0ed163c9be 100644 --- a/.github/ISSUE_TEMPLATE/lsp_bug_report.yml +++ b/.github/ISSUE_TEMPLATE/lsp_bug_report.yml @@ -29,13 +29,6 @@ body: - type: textarea attributes: - label: ':checkhealth' - description: | - Paste the results from `nvim -c ":checkhealth nvim lspconfig"` - render: markdown - - - type: textarea - attributes: label: 'Steps to reproduce using "nvim -u minimal_init.lua"' description: | - Download the minimal config with `curl -LO https://raw.githubusercontent.com/neovim/nvim-lspconfig/master/test/minimal_init.lua` and modify it to include any specific commands or servers pertaining to your issues. diff --git a/.github/labeler.yml b/.github/labeler.yml index 52998c65cf..3e19c1d501 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -59,3 +59,6 @@ - .github/workflows/**/* - .builds/* - ci/**/* + +"filetype": + - runtime/lua/vim/filetype.lua diff --git a/.github/workflows/api-docs.yml b/.github/workflows/api-docs.yml index ee72cf5f01..39d85e967d 100644 --- a/.github/workflows/api-docs.yml +++ b/.github/workflows/api-docs.yml @@ -48,6 +48,6 @@ jobs: if: ${{ steps.docs.outputs.UPDATED_DOCS != 0 }} run: | git add -u - git commit -m 'docs: regenerate' + git commit -m 'docs: regenerate [skip ci]' git push --force https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY} ${DOC_BRANCH} - gh pr create --fill --base ${GITHUB_REF#refs/heads/} --head ${DOC_BRANCH} || true + gh pr create --draft --fill --base ${GITHUB_REF#refs/heads/} --head ${DOC_BRANCH} || true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d906b949c..b650c5dd85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -171,7 +171,7 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Setup commom environment variables + - name: Setup common environment variables run: ./.github/workflows/env.sh ${{ matrix.flavor }} - name: Install apt packages diff --git a/.github/workflows/commitlint.yml b/.github/workflows/commitlint.yml index 6c74ec99d3..f190981322 100644 --- a/.github/workflows/commitlint.yml +++ b/.github/workflows/commitlint.yml @@ -1,19 +1,21 @@ name: "Commit Linter" on: - pull_request: + # Only pull_request and push honor [skip ci]. Since this workflow must pass + # to merge a PR, it can't be skipped, so use pull_request_target + pull_request_target: types: [opened, synchronize, reopened, ready_for_review] jobs: lint-commits: runs-on: ubuntu-latest if: github.event.pull_request.draft == false - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v2 with: fetch-depth: 0 ref: ${{ github.event.pull_request.head.sha }} + path: pr_nvim - uses: rhysd/action-setup-vim@v1 with: neovim: true - - run: nvim --clean -es +"lua require('scripts.lintcommit').main({trace=true})" + - run: wget https://raw.githubusercontent.com/neovim/neovim/master/scripts/lintcommit.lua + - run: nvim --clean -es +"cd pr_nvim" +"lua dofile('../lintcommit.lua').main({trace=true})" diff --git a/.github/workflows/env.sh b/.github/workflows/env.sh index d1bc412399..0964995605 100755 --- a/.github/workflows/env.sh +++ b/.github/workflows/env.sh @@ -35,13 +35,14 @@ case "$FLAVOR" in cat <<EOF >> "$GITHUB_ENV" CLANG_SANITIZER=ASAN_UBSAN SYMBOLIZER=asan_symbolize-13 -ASAN_OPTIONS=detect_leaks=1:check_initialization_order=1:log_path=$GITHUB_WORKSPACE/build/log/asan +ASAN_OPTIONS=detect_leaks=1:check_initialization_order=1:log_path=$GITHUB_WORKSPACE/build/log/asan:intercept_tls_get_addr=0 UBSAN_OPTIONS=print_stacktrace=1 log_path=$GITHUB_WORKSPACE/build/log/ubsan EOF ;; tsan) cat <<EOF >> "$GITHUB_ENV" TSAN_OPTIONS=log_path=$GITHUB_WORKSPACE/build/log/tsan +CLANG_SANITIZER=TSAN EOF ;; lint) diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 76fc8793fa..08f0ce1763 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -1,8 +1,9 @@ name: "Pull Request Labeler" on: pull_request_target: - types: opened + types: [opened] jobs: + triage: runs-on: ubuntu-latest permissions: @@ -12,8 +13,10 @@ jobs: - uses: actions/labeler@main with: repo-token: "${{ secrets.GITHUB_TOKEN }}" + type-scope: runs-on: ubuntu-latest + needs: ["triage"] permissions: contents: write pull-requests: write @@ -23,8 +26,19 @@ jobs: PR_NUMBER: ${{ github.event.pull_request.number }} PR_TITLE: ${{ github.event.pull_request.title }} steps: - # Extract type and try to add it as a label - - run: gh pr edit "$PR_NUMBER" --add-label "$(echo "$PR_TITLE" | sed -E 's|([[:alpha:]]+)(\(.*\))?!?:.*|\1|')" || true + - name: "Extract commit type and add as label" + run: gh pr edit "$PR_NUMBER" --add-label "$(echo "$PR_TITLE" | sed -E 's|([[:alpha:]]+)(\(.*\))?!?:.*|\1|')" || true + - name: "Extract commit scope and add as label" + run: gh pr edit "$PR_NUMBER" --add-label "$(echo "$PR_TITLE" | sed -E 's|[[:alpha:]]+\((.+)\)!?:.*|\1|')" || true - # Extract scope and try to add it as a label - - run: gh pr edit "$PR_NUMBER" --add-label "$(echo "$PR_TITLE" | sed -E 's|[[:alpha:]]+\((.+)\)!?:.*|\1|')" || true + upload-pr-number: + runs-on: ubuntu-latest + steps: + - name: Save PR number + run: | + mkdir -p pr + echo ${{ github.event.number }} > pr/pr_number + - uses: actions/upload-artifact@v2 + with: + name: pr_number + path: pr/ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5ad4c90eaf..b72c2ab71d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -212,6 +212,7 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NVIM_VERSION: ${{ needs.linux.outputs.version }} + DEBUG: api run: | envsubst < "$GITHUB_WORKSPACE/.github/workflows/notes.md" > "$RUNNER_TEMP/notes.md" gh release create $TAG_NAME $PRERELEASE --notes-file "$RUNNER_TEMP/notes.md" --title "$SUBJECT" --target $GITHUB_SHA nvim-macos/* nvim-linux64/* appimage/* nvim-win64/* diff --git a/.github/workflows/reviews.yml b/.github/workflows/reviews.yml new file mode 100644 index 0000000000..e2253988ba --- /dev/null +++ b/.github/workflows/reviews.yml @@ -0,0 +1,129 @@ +name: "Request reviews" +on: + pull_request_target: + types: [labeled] + workflow_run: + workflows: [Pull Request Labeler] + types: [completed] +jobs: + request-reviewer: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - if: github.event_name == 'workflow_run' + name: 'Download artifact with PR number' + uses: actions/github-script@v6 + with: + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == "pr_number" + })[0]; + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + let fs = require('fs'); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr_number.zip`, Buffer.from(download.data)); + + - if: github.event_name == 'workflow_run' + name: 'Unzip artifact' + run: unzip pr_number.zip + + - name: 'Request reviewers' + uses: actions/github-script@v6 + with: + script: | + // The number of the pull request that triggered this run. If label + // was added manually by a person the number will be stored in current + // context, otherwise the number will be stored in a text file that + // was stored as an artifact from previous workflow. + + const fs = require('fs') + const pr_number = context.issue.number || Number(fs.readFileSync('./pr_number')) + + const pr_data = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr_number + }) + const labels = pr_data.data.labels.map(e => e.name) + + const reviewers = new Set() + if (labels.includes('api')) { + reviewers.add("bfredl") + reviewers.add("gpanders") + reviewers.add("muniter") + } + + if (labels.includes('ci')) { + reviewers.add("jamessan") + } + + if (labels.includes('diagnostic')) { + reviewers.add("gpanders") + } + + if (labels.includes('distribution')) { + reviewers.add("jamessan") + } + + if (labels.includes('documentation')) { + reviewers.add("clason") + } + + if (labels.includes('extmarks')) { + reviewers.add("bfredl") + } + + if (labels.includes('filetype')) { + reviewers.add("clason") + reviewers.add("gpanders") + } + + if (labels.includes('gui')) { + reviewers.add("glacambre") + reviewers.add("smolck") + } + + if (labels.includes('lsp')) { + reviewers.add("mfussenegger") + reviewers.add("mjlbach") + } + + if (labels.includes('treesitter')) { + reviewers.add("bfredl") + reviewers.add("vigoux") + } + + if (labels.includes('typo')) { + reviewers.add("dundargoc") + } + + if (labels.includes('ui')) { + reviewers.add("bfredl") + } + + if (labels.includes('vim-patch')) { + reviewers.add("janlazo") + reviewers.add("seandewar") + reviewers.add("zeertzjq") + } + + // Remove person that opened the PR since they can't review themselves + const pr_opener = pr_data.data.user.login + reviewers.delete(pr_opener) + + github.rest.pulls.requestReviewers({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: pr_number, + reviewers: Array.from(reviewers) + }); diff --git a/.github/workflows/vim-patches.yml b/.github/workflows/vim-patches.yml index 5742b51158..453d293b0e 100644 --- a/.github/workflows/vim-patches.yml +++ b/.github/workflows/vim-patches.yml @@ -49,4 +49,4 @@ jobs: git add -u git commit -m 'version.c: update [skip ci]' git push --force https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY} ${VERSION_BRANCH} - gh pr create --fill --label vim-patch --base ${GITHUB_REF#refs/heads/} --head ${VERSION_BRANCH} || true + gh pr create --draft --fill --label vim-patch --base ${GITHUB_REF#refs/heads/} --head ${VERSION_BRANCH} || true diff --git a/.luacheckrc b/.luacheckrc index 487f5ab552..9bbd323e84 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -26,6 +26,10 @@ read_globals = { "vim", } +globals = { + "vim.g", +} + exclude_files = { 'test/functional/fixtures/lua/syntax_error.lua', } diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c43c5c494..e0f05e1205 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -408,6 +408,19 @@ main(void) if(TS_HAS_SET_MATCH_LIMIT) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNVIM_TS_HAS_SET_MATCH_LIMIT") endif() +check_c_source_compiles(" +#include <stdlib.h> +#include <tree_sitter/api.h> +int +main(void) +{ + ts_set_allocator(malloc, calloc, realloc, free); + return 0; +} +" TS_HAS_SET_ALLOCATOR) +if(TS_HAS_SET_ALLOCATOR) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNVIM_TS_HAS_SET_ALLOCATOR") +endif() # Note: The test lib requires LuaJIT; it will be skipped if LuaJIT is missing. option(PREFER_LUA "Prefer Lua over LuaJIT in the nvim executable." OFF) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b02aeb1ed1..5e04f33b15 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,11 +7,11 @@ Getting started If you want to help but don't know where to start, here are some low-risk/isolated tasks: -- [Merge a Vim patch]. - Try a [complexity:low] issue. - Fix bugs found by [Clang](#clang-scan-build), [PVS](#pvs-studio) or [Coverity](#coverity). - [Improve documentation][wiki-contribute-help] +- [Merge a Vim patch] (Familiarity with Vim is *strongly* recommended) Reporting problems ------------------ diff --git a/ci/build.ps1 b/ci/build.ps1 index a81d351bc6..ef5ba3bf2d 100644 --- a/ci/build.ps1 +++ b/ci/build.ps1 @@ -86,19 +86,10 @@ elseif ($compiler -eq 'MSVC') { } if (-not $NoTests) { - # Setup python (use AppVeyor system python) - - # Disambiguate python3, if needed - if (-not (Test-Path -Path C:\hostedtoolcache\windows\Python\3.5.4\x64\python3.exe) ) { - move C:\hostedtoolcache\windows\Python\3.5.4\x64\python.exe C:\hostedtoolcache\windows\Python\3.5.4\x64\python3.exe - } - $env:PATH = "C:\hostedtoolcache\windows\Python\2.7.18\x64;C:\hostedtoolcache\windows\Python\3.5.4\x64;$env:PATH" - + python -m ensurepip python -m pip install pynvim ; exitIfFailed - python3 -m pip install pynvim ; exitIfFailed # Sanity check python -c "import pynvim; print(str(pynvim))" ; exitIfFailed - python3 -c "import pynvim; print(str(pynvim))" ; exitIfFailed gem.cmd install --pre neovim Get-Command -CommandType Application neovim-ruby-host.bat diff --git a/ci/common/build.sh b/ci/common/build.sh index 0ee4b7493f..b8bbff0b16 100644 --- a/ci/common/build.sh +++ b/ci/common/build.sh @@ -8,8 +8,6 @@ _stat() { top_make() { printf '%78s\n' | tr ' ' '=' - # Travis has 1.5 virtual cores according to: - # http://docs.travis-ci.com/user/speeding-up-the-build/#Paralellizing-your-build-on-one-VM ninja "$@" } @@ -46,7 +44,9 @@ build_deps() { cd "${CI_BUILD_DIR}" } -prepare_build() { +build_nvim() { + check_core_dumps --delete quiet + if test -n "${CLANG_SANITIZER}" ; then CMAKE_FLAGS="${CMAKE_FLAGS} -DCLANG_${CLANG_SANITIZER}=ON" fi @@ -55,9 +55,8 @@ prepare_build() { cd "${BUILD_DIR}" echo "Configuring with '${CMAKE_FLAGS} $@'." cmake -G Ninja ${CMAKE_FLAGS} "$@" "${CI_BUILD_DIR}" -} -build_nvim() { + echo "Building nvim." if ! top_make nvim ; then exit 1 diff --git a/ci/common/submit_coverage.sh b/ci/common/submit_coverage.sh index 9c7887de0b..f781ca8e5e 100755 --- a/ci/common/submit_coverage.sh +++ b/ci/common/submit_coverage.sh @@ -4,7 +4,7 @@ # Args: # $1: Flag(s) for codecov, separated by comma. -set -ex +set -e # Change to grandparent dir (POSIXly). CDPATH='' cd -P -- "$(dirname -- "$0")/../.." || exit @@ -18,12 +18,12 @@ if ! [ -f "$codecov_sh" ]; then curl --retry 5 --silent --fail -o "$codecov_sh" https://codecov.io/bash chmod +x "$codecov_sh" - python3 -m pip install --quiet --user gcovr + python -m pip install --quiet --user gcovr fi ( cd build - python3 -m gcovr --branches --exclude-unreachable-branches --print-summary -j 2 --exclude '.*/auto/.*' --root .. --delete -o ../coverage.xml --xml + python -m gcovr --branches --exclude-unreachable-branches --print-summary -j 2 --exclude '.*/auto/.*' --root .. --delete -o ../coverage.xml --xml ) # Upload to codecov. diff --git a/ci/common/suite.sh b/ci/common/suite.sh index 561849ce2d..f33f8b89d1 100644 --- a/ci/common/suite.sh +++ b/ci/common/suite.sh @@ -29,17 +29,14 @@ ci_fold() { } enter_suite() { - set +x FAILED=0 rm -f "${END_MARKER}" local suite_name="$1" export NVIM_TEST_CURRENT_SUITE="${NVIM_TEST_CURRENT_SUITE}/$suite_name" ci_fold "start" "$suite_name" - set -x } exit_suite() { - set +x if test $FAILED -ne 0 ; then echo "Suite ${NVIM_TEST_CURRENT_SUITE} failed, summary:" echo "${FAIL_SUMMARY}" @@ -47,13 +44,7 @@ exit_suite() { ci_fold "end" "" fi export NVIM_TEST_CURRENT_SUITE="${NVIM_TEST_CURRENT_SUITE%/*}" - if test "$1" != "--continue" ; then - exit $FAILED - else - local saved_failed=$FAILED - FAILED=0 - return $saved_failed - fi + FAILED=0 } fail() { @@ -71,17 +62,6 @@ fail() { FAILED=1 } -run_test() { - local cmd="$1" - test $# -gt 0 && shift - local test_name="$1" - : ${test_name:=$cmd} - test $# -gt 0 && shift - if ! eval "$cmd" ; then - fail "${test_name}" "$@" - fi -} - ended_successfully() { if test -f "${FAIL_SUMMARY_FILE}" ; then echo 'Test failed, complete summary:' @@ -99,3 +79,13 @@ end_tests() { touch "${END_MARKER}" ended_successfully } + +run_suite() { + local command="$1" + local suite_name="$2" + + enter_suite "$suite_name" + eval "$command" || fail "$suite_name" + exit_suite +} + diff --git a/ci/common/test.sh b/ci/common/test.sh index 92c15c8ba1..f211a2e7aa 100644 --- a/ci/common/test.sh +++ b/ci/common/test.sh @@ -87,18 +87,15 @@ check_sanitizer() { } run_unittests() {( - enter_suite unittests ulimit -c unlimited || true if ! build_make unittest ; then fail 'unittests' F 'Unit tests failed' fi submit_coverage unittest check_core_dumps "$(command -v luajit)" - exit_suite )} run_functionaltests() {( - enter_suite functionaltests ulimit -c unlimited || true if ! build_make ${FUNCTIONALTEST}; then fail 'functionaltests' F 'Functional tests failed' @@ -107,11 +104,9 @@ run_functionaltests() {( check_sanitizer "${LOG_DIR}" valgrind_check "${LOG_DIR}" check_core_dumps - exit_suite )} run_oldtests() {( - enter_suite oldtests ulimit -c unlimited || true if ! make oldtest; then reset @@ -121,11 +116,9 @@ run_oldtests() {( check_sanitizer "${LOG_DIR}" valgrind_check "${LOG_DIR}" check_core_dumps - exit_suite )} check_runtime_files() {( - set +x local test_name="$1" ; shift local message="$1" ; shift local tst="$1" ; shift @@ -146,7 +139,6 @@ check_runtime_files() {( )} install_nvim() {( - enter_suite 'install_nvim' if ! build_make install ; then fail 'install' E 'make install failed' exit_suite @@ -179,11 +171,4 @@ install_nvim() {( if ! grep -q "$gpat" "${INSTALL_PREFIX}/share/nvim/runtime/$genvimsynf" ; then fail 'funcnames' F "It appears that $genvimsynf does not contain $gpat." fi - - exit_suite )} - -csi_clean() { - find "${BUILD_DIR}/bin" -name 'test-includes-*' -delete - find "${BUILD_DIR}" -name '*test-include*.o' -delete -} diff --git a/ci/install.sh b/ci/install.sh index bd42274b49..894e090de2 100755 --- a/ci/install.sh +++ b/ci/install.sh @@ -4,12 +4,8 @@ set -e set -o pipefail # Use default CC to avoid compilation problems when installing Python modules. -echo "Install neovim module for Python 3." -CC=cc python3 -m pip -q install --user --upgrade pynvim -if python2 -m pip -c True 2>&1; then - echo "Install neovim module for Python 2." - CC=cc python2 -m pip -q install --user --upgrade pynvim -fi +echo "Install neovim module for Python." +CC=cc python -m pip -q install --user --upgrade pynvim echo "Install neovim RubyGem." gem install --no-document --bindir "$HOME/.local/bin" --user-install --pre neovim diff --git a/ci/run_lint.sh b/ci/run_lint.sh index 314976edc2..607ffa233a 100755 --- a/ci/run_lint.sh +++ b/ci/run_lint.sh @@ -8,24 +8,10 @@ CI_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${CI_DIR}/common/build.sh" source "${CI_DIR}/common/suite.sh" -enter_suite 'clint' -run_test 'make clint-full' clint -exit_suite --continue - -enter_suite 'lualint' -run_test 'make lualint' lualint -exit_suite --continue - -enter_suite 'pylint' -run_test 'make pylint' pylint -exit_suite --continue - -enter_suite 'shlint' -run_test 'make shlint' shlint -exit_suite --continue - -enter_suite single-includes -run_test 'make check-single-includes' single-includes -exit_suite --continue +run_suite 'make clint-full' 'clint' +run_suite 'make lualint' 'lualint' +run_suite 'make pylint' 'pylint' +run_suite 'make shlint' 'shlint' +run_suite 'make check-single-includes' 'single-includes' end_tests diff --git a/ci/run_tests.sh b/ci/run_tests.sh index c175910da5..1baeb090a8 100755 --- a/ci/run_tests.sh +++ b/ci/run_tests.sh @@ -8,29 +8,17 @@ source "${CI_DIR}/common/build.sh" source "${CI_DIR}/common/test.sh" source "${CI_DIR}/common/suite.sh" -enter_suite build +run_suite 'build_nvim' 'build' -check_core_dumps --delete quiet - -prepare_build -build_nvim - -exit_suite --continue - -enter_suite tests - -if test "$CLANG_SANITIZER" != "TSAN" ; then +if test "$CLANG_SANITIZER" != "TSAN"; then # Additional threads are only created when the builtin UI starts, which # doesn't happen in the unit/functional tests if test "${FUNCTIONALTEST}" != "functionaltest-lua"; then - run_test run_unittests + run_suite run_unittests unittests fi - run_test run_functionaltests + run_suite run_functionaltests functionaltests fi -run_test run_oldtests - -run_test install_nvim - -exit_suite --continue +run_suite run_oldtests oldtests +run_suite install_nvim install_nvim end_tests diff --git a/runtime/autoload/dist/ft.vim b/runtime/autoload/dist/ft.vim index 9933ace8c2..aacecc521e 100644 --- a/runtime/autoload/dist/ft.vim +++ b/runtime/autoload/dist/ft.vim @@ -1,7 +1,7 @@ " Vim functions for file type detection " " Maintainer: Bram Moolenaar <Bram@vim.org> -" Last Change: 2021 Dec 17 +" Last Change: 2022 Jan 31 " These functions are moved here from runtime/filetype.vim to make startup " faster. @@ -67,13 +67,32 @@ func dist#ft#FTasmsyntax() endif endfunc -" Check if one of the first five lines contains "VB_Name". In that case it is -" probably a Visual Basic file. Otherwise it's assumed to be "alt" filetype. -func dist#ft#FTVB(alt) - if getline(1).getline(2).getline(3).getline(4).getline(5) =~? 'VB_Name\|Begin VB\.\(Form\|MDIForm\|UserControl\)' +let s:ft_visual_basic_content = '\cVB_Name\|Begin VB\.\(Form\|MDIForm\|UserControl\)' + +" See FTfrm() for Visual Basic form file detection +func dist#ft#FTbas() + if exists("g:filetype_bas") + exe "setf " . g:filetype_bas + return + endif + + " most frequent FreeBASIC-specific keywords in distro files + let fb_keywords = '\c^\s*\%(extern\|var\|enum\|private\|scope\|union\|byref\|operator\|constructor\|delete\|namespace\|public\|property\|with\|destructor\|using\)\>\%(\s*[:=(]\)\@!' + let fb_preproc = '\c^\s*\%(#\a\+\|option\s\+\%(byval\|dynamic\|escape\|\%(no\)\=gosub\|nokeyword\|private\|static\)\>\)' + let fb_comment = "^\\s*/'" + " OPTION EXPLICIT, without the leading underscore, is common to many dialects + let qb64_preproc = '\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)' + + let lines = getline(1, min([line("$"), 100])) + + if match(lines, fb_preproc) > -1 || match(lines, fb_comment) > -1 || match(lines, fb_keywords) > -1 + setf freebasic + elseif match(lines, qb64_preproc) > -1 + setf qb64 + elseif match(lines, s:ft_visual_basic_content) > -1 setf vb else - exe "setf " . a:alt + setf basic endif endfunc @@ -219,6 +238,21 @@ func dist#ft#FTe() endif endfunc +func dist#ft#FTfrm() + if exists("g:filetype_frm") + exe "setf " . g:filetype_frm + return + endif + + let lines = getline(1, min([line("$"), 5])) + + if match(lines, s:ft_visual_basic_content) > -1 + setf vb + else + setf form + endif +endfunc + " Distinguish between Forth and F#. " Provided by Doug Kearns. func dist#ft#FTfs() @@ -692,7 +726,7 @@ func dist#ft#FTperl() endif let save_cursor = getpos('.') call cursor(1,1) - let has_use = search('^use\s\s*\k', 'c', 30) + let has_use = search('^use\s\s*\k', 'c', 30) > 0 call setpos('.', save_cursor) if has_use setf perl @@ -724,7 +758,8 @@ func dist#ft#FTtex() let save_cursor = getpos('.') call cursor(1,1) let firstNC = search('^\s*[^[:space:]%]', 'c', 1000) - if firstNC " Check the next thousand lines for a LaTeX or ConTeXt keyword. + if firstNC > 0 + " Check the next thousand lines for a LaTeX or ConTeXt keyword. let lpat = 'documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>' let cpat = 'start\a\+\|setup\a\+\|usemodule\|enablemode\|enableregime\|setvariables\|useencoding\|usesymbols\|stelle\a\+\|verwende\a\+\|stel\a\+\|gebruik\a\+\|usa\a\+\|imposta\a\+\|regle\a\+\|utilisemodule\>' let kwline = search('^\s*\\\%(' . lpat . '\)\|^\s*\\\(' . cpat . '\)', @@ -829,6 +864,38 @@ func dist#ft#Dep3patch() endfor endfunc +" This function checks the first 15 lines for appearance of 'FoamFile' +" and then 'object' in a following line. +" In that case, it's probably an OpenFOAM file +func dist#ft#FTfoam() + let ffile = 0 + let lnum = 1 + while lnum <= 15 + if getline(lnum) =~# '^FoamFile' + let ffile = 1 + elseif ffile == 1 && getline(lnum) =~# '^\s*object' + setf foam + return + endif + let lnum = lnum + 1 + endwhile +endfunc + +" Determine if a *.tf file is TF mud client or terraform +func dist#ft#FTtf() + let numberOfLines = line('$') + for i in range(1, numberOfLines) + let currentLine = trim(getline(i)) + let firstCharacter = currentLine[0] + if firstCharacter !=? ";" && firstCharacter !=? "/" && firstCharacter !=? "" + setf terraform + return + endif + endfor + setf tf +endfunc + + " Restore 'cpoptions' let &cpo = s:cpo_save unlet s:cpo_save diff --git a/runtime/autoload/freebasic.vim b/runtime/autoload/freebasic.vim new file mode 100644 index 0000000000..428cf1382b --- /dev/null +++ b/runtime/autoload/freebasic.vim @@ -0,0 +1,41 @@ +" Vim filetype plugin file +" Language: FreeBASIC +" Maintainer: Doug Kearns <dougkearns@gmail.com> +" Last Change: 2021 Mar 16 + +" Dialects can be one of fb, qb, fblite, or deprecated +" Precedence is forcelang > #lang > lang +function! freebasic#GetDialect() abort + if exists("g:freebasic_forcelang") + return g:freebasic_forcelang + endif + + if exists("g:freebasic_lang") + let dialect = g:freebasic_lang + else + let dialect = "fb" + endif + + " override with #lang directive or metacommand + + let skip = "has('syntax_items') && synIDattr(synID(line('.'), col('.'), 1), 'name') =~ 'Comment$'" + let pat = '\c^\s*\%(#\s*lang\s\+\|''\s*$lang\s*:\s*\)"\([^"]*\)"' + + let save_cursor = getcurpos() + call cursor(1, 1) + let lnum = search(pat, 'n', '', '', skip) + call setpos('.', save_cursor) + + if lnum + let word = matchlist(getline(lnum), pat)[1] + if word =~? '\%(fb\|deprecated\|fblite\|qb\)' + let dialect = word + else + echomsg "freebasic#GetDialect: Invalid lang, found '" .. word .. "' at line " .. lnum .. " " .. getline(lnum) + endif + endif + + return dialect +endfunction + +" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker: diff --git a/runtime/autoload/health.vim b/runtime/autoload/health.vim index 1d462ad02c..ec030adf04 100644 --- a/runtime/autoload/health.vim +++ b/runtime/autoload/health.vim @@ -21,10 +21,17 @@ function! health#check(plugin_names) abort throw 'healthcheck_not_found' endif eval type == 'v' ? call(func, []) : luaeval(func) + " in the event the healthcheck doesn't return anything + " (the plugin author should avoid this possibility) + if len(s:output) == 0 + throw 'healthcheck_no_return_value' + endif catch let s:output = [] " Clear the output if v:exception =~# 'healthcheck_not_found' call health#report_error('No healthcheck found for "'.name.'" plugin.') + elseif v:exception =~# 'healthcheck_no_return_value' + call health#report_error('The healthcheck report for "'.name.'" plugin is empty.') else call health#report_error(printf( \ "Failed to run healthcheck for \"%s\" plugin. Exception:\n%s\n%s", @@ -127,7 +134,7 @@ endfunction " }}} " From a path return a list [{name}, {func}, {type}] representing a healthcheck function! s:filepath_to_healthcheck(path) abort - if a:path =~# 'vim$' + if a:path =~# 'vim$' let name = matchstr(a:path, '\zs[^\/]*\ze\.vim$') let func = 'health#'.name.'#check' let type = 'v' diff --git a/runtime/autoload/health/nvim.vim b/runtime/autoload/health/nvim.vim index 0bb343e198..961f83d926 100644 --- a/runtime/autoload/health/nvim.vim +++ b/runtime/autoload/health/nvim.vim @@ -104,8 +104,8 @@ function! s:check_rplugin_manifest() abort if !has_key(existing_rplugins, script) let msg = printf('"%s" is not registered.', fnamemodify(path, ':t')) if python_version ==# 'pythonx' - if !has('python2') && !has('python3') - let msg .= ' (python2 and python3 not available)' + if !has('python3') + let msg .= ' (python3 not available)' endif elseif !has(python_version) let msg .= printf(' (%s not available)', python_version) @@ -148,14 +148,14 @@ endfunction function! s:get_tmux_option(option) abort let cmd = 'tmux show-option -qvg '.a:option " try global scope - let out = system(cmd) + let out = system(split(cmd)) let val = substitute(out, '\v(\s|\r|\n)', '', 'g') if v:shell_error call health#report_error('command failed: '.cmd."\n".out) return 'error' elseif empty(val) let cmd = 'tmux show-option -qvgs '.a:option " try session scope - let out = system(cmd) + let out = system(split(cmd)) let val = substitute(out, '\v(\s|\r|\n)', '', 'g') if v:shell_error call health#report_error('command failed: '.cmd."\n".out) @@ -202,11 +202,11 @@ function! s:check_tmux() abort " check default-terminal and $TERM call health#report_info('$TERM: '.$TERM) let cmd = 'tmux show-option -qvg default-terminal' - let out = system(cmd) + let out = system(split(cmd)) let tmux_default_term = substitute(out, '\v(\s|\r|\n)', '', 'g') if empty(tmux_default_term) let cmd = 'tmux show-option -qvgs default-terminal' - let out = system(cmd) + let out = system(split(cmd)) let tmux_default_term = substitute(out, '\v(\s|\r|\n)', '', 'g') endif @@ -225,7 +225,7 @@ function! s:check_tmux() abort endif " check for RGB capabilities - let info = system('tmux server-info') + let info = system(['tmux', 'server-info']) let has_tc = stridx(info, " Tc: (flag) true") != -1 let has_rgb = stridx(info, " RGB: (flag) true") != -1 if !has_tc && !has_rgb @@ -242,7 +242,7 @@ function! s:check_terminal() abort endif call health#report_start('terminal') let cmd = 'infocmp -L' - let out = system(cmd) + let out = system(split(cmd)) let kbs_entry = matchstr(out, 'key_backspace=[^,[:space:]]*') let kdch1_entry = matchstr(out, 'key_dc=[^,[:space:]]*') diff --git a/runtime/autoload/health/provider.vim b/runtime/autoload/health/provider.vim index e6523aa215..2f35179338 100644 --- a/runtime/autoload/health/provider.vim +++ b/runtime/autoload/health/provider.vim @@ -282,10 +282,10 @@ function! s:disabled_via_loaded_var(provider) abort return 0 endfunction -function! s:check_python(version) abort - call health#report_start('Python ' . a:version . ' provider (optional)') +function! s:check_python() abort + call health#report_start('Python 3 provider (optional)') - let pyname = 'python'.(a:version == 2 ? '' : '3') + let pyname = 'python3' let python_exe = '' let venv = exists('$VIRTUAL_ENV') ? resolve($VIRTUAL_ENV) : '' let host_prog_var = pyname.'_host_prog' @@ -301,7 +301,7 @@ function! s:check_python(version) abort call health#report_info(printf('Using: g:%s = "%s"', host_prog_var, get(g:, host_prog_var))) endif - let [pyname, pythonx_errors] = provider#pythonx#Detect(a:version) + let [pyname, pythonx_errors] = provider#pythonx#Detect(3) if empty(pyname) call health#report_warn('No Python executable found that can `import neovim`. ' @@ -405,7 +405,7 @@ function! s:check_python(version) abort " can import 'pynvim'. If so, that Python failed to import 'neovim' as " well, which is most probably due to a failed pip upgrade: " https://github.com/neovim/neovim/wiki/Following-HEAD#20181118 - let [pynvim_exe, errors] = provider#pythonx#DetectByModule('pynvim', a:version) + let [pynvim_exe, errors] = provider#pythonx#DetectByModule('pynvim', 3) if !empty(pynvim_exe) call health#report_error( \ 'Detected pip upgrade failure: Python executable can import "pynvim" but ' @@ -416,14 +416,14 @@ function! s:check_python(version) abort \ . pynvim_exe ." -m pip install neovim # only if needed by third-party software") endif else - let [pyversion, current, latest, status] = s:version_info(python_exe) + let [majorpyversion, current, latest, status] = s:version_info(python_exe) - if a:version != str2nr(pyversion) + if 3 != str2nr(majorpyversion) call health#report_warn('Unexpected Python version.' . \ ' This could lead to confusing error messages.') endif - call health#report_info('Python version: ' . pyversion) + call health#report_info('Python version: ' . majorpyversion) if s:is_bad_response(status) call health#report_info(printf('pynvim version: %s (%s)', current, status)) @@ -565,7 +565,7 @@ function! s:check_ruby() abort \ ['Install Ruby and verify that `ruby` and `gem` commands work.']) return endif - call health#report_info('Ruby: '. s:system('ruby -v')) + call health#report_info('Ruby: '. s:system(['ruby', '-v'])) let [host, err] = provider#ruby#Detect() if empty(host) @@ -588,11 +588,11 @@ function! s:check_ruby() abort endif let latest_gem = get(split(latest_gem, 'neovim (\|, \|)$' ), 0, 'not found') - let current_gem_cmd = host .' --version' + let current_gem_cmd = [host, '--version'] let current_gem = s:system(current_gem_cmd) if s:shell_error - call health#report_error('Failed to run: '. current_gem_cmd, - \ ['Report this issue with the output of: ', current_gem_cmd]) + call health#report_error('Failed to run: '. join(current_gem_cmd), + \ ['Report this issue with the output of: ', join(current_gem_cmd)]) return endif @@ -619,7 +619,7 @@ function! s:check_node() abort \ ['Install Node.js and verify that `node` and `npm` (or `yarn`) commands work.']) return endif - let node_v = get(split(s:system('node -v'), "\n"), 0, '') + let node_v = get(split(s:system(['node', '-v']), "\n"), 0, '') call health#report_info('Node.js: '. node_v) if s:shell_error || s:version_cmp(node_v[1:], '6.0.0') < 0 call health#report_warn('Nvim node.js host does not support '.node_v) @@ -660,8 +660,8 @@ function! s:check_node() abort let current_npm_cmd = ['node', host, '--version'] let current_npm = s:system(current_npm_cmd) if s:shell_error - call health#report_error('Failed to run: '. string(current_npm_cmd), - \ ['Report this issue with the output of: ', string(current_npm_cmd)]) + call health#report_error('Failed to run: '. join(current_npm_cmd), + \ ['Report this issue with the output of: ', join(current_npm_cmd)]) return endif @@ -734,8 +734,8 @@ function! s:check_perl() abort let current_cpan_cmd = [perl_exec, '-W', '-MNeovim::Ext', '-e', 'print $Neovim::Ext::VERSION'] let current_cpan = s:system(current_cpan_cmd) if s:shell_error - call health#report_error('Failed to run: '. string(current_cpan_cmd), - \ ['Report this issue with the output of: ', string(current_cpan_cmd)]) + call health#report_error('Failed to run: '. join(current_cpan_cmd), + \ ['Report this issue with the output of: ', join(current_cpan_cmd)]) return endif @@ -751,8 +751,7 @@ endfunction function! health#provider#check() abort call s:check_clipboard() - call s:check_python(2) - call s:check_python(3) + call s:check_python() call s:check_virtualenv() call s:check_ruby() call s:check_node() diff --git a/runtime/autoload/man.vim b/runtime/autoload/man.vim index 90d353f9de..b28170b7a1 100644 --- a/runtime/autoload/man.vim +++ b/runtime/autoload/man.vim @@ -7,7 +7,6 @@ let s:loaded_man = 1 let s:find_arg = '-w' let s:localfile_arg = v:true " Always use -l if possible. #6683 -let s:section_arg = '-S' function! man#init() abort try @@ -216,16 +215,42 @@ endfunction function! s:get_path(sect, name) abort " Some man implementations (OpenBSD) return all available paths from the - " search command, so we get() the first one. #8341 + " search command. Previously, this function would simply select the first one. + " + " However, some searches will report matches that are incorrect: + " man -w strlen may return string.3 followed by strlen.3, and therefore + " selecting the first would get us the wrong page. Thus, we must find the + " first matching one. + " + " There's yet another special case here. Consider the following: + " If you run man -w strlen and string.3 comes up first, this is a problem. We + " should search for a matching named one in the results list. + " However, if you search for man -w clock_gettime, you will *only* get + " clock_getres.2, which is the right page. Searching the resuls for + " clock_gettime will no longer work. In this case, we should just use the + " first one that was found in the correct section. + " + " Finally, we can avoid relying on -S or -s here since they are very + " inconsistently supported. Instead, call -w with a section and a name. if empty(a:sect) - return substitute(get(split(s:system(['man', s:find_arg, a:name])), 0, ''), '\n\+$', '', '') + let results = split(s:system(['man', s:find_arg, a:name])) + else + let results = split(s:system(['man', s:find_arg, a:sect, a:name])) + endif + + if empty(results) + return '' endif - " '-s' flag handles: - " - tokens like 'printf(echo)' - " - sections starting with '-' - " - 3pcap section (found on macOS) - " - commas between sections (for section priority) - return substitute(get(split(s:system(['man', s:find_arg, s:section_arg, a:sect, a:name])), 0, ''), '\n\+$', '', '') + + " find any that match the specified name + let namematches = filter(copy(results), 'fnamemodify(v:val, ":t") =~ a:name') + let sectmatches = [] + + if !empty(namematches) && !empty(a:sect) + let sectmatches = filter(copy(namematches), 'fnamemodify(v:val, ":e") == a:sect') + endif + + return substitute(get(sectmatches, 0, get(namematches, 0, results[0])), '\n\+$', '', '') endfunction " s:verify_exists attempts to find the path to a manpage @@ -243,40 +268,72 @@ endfunction " then we don't do it again in step 2. function! s:verify_exists(sect, name) abort let sect = a:sect - if empty(sect) - let sect = get(b:, 'man_default_sects', '') - endif - try - return s:get_path(sect, a:name) - catch /^command error (/ - endtry - - if !empty(get(b:, 'man_default_sects', '')) && sect !=# b:man_default_sects + if empty(sect) + " no section specified, so search with b:man_default_sects + if exists('b:man_default_sects') + let sects = split(b:man_default_sects, ',') + for sec in sects + try + let res = s:get_path(sec, a:name) + if !empty(res) + return res + endif + catch /^command error (/ + endtry + endfor + endif + else + " try with specified section try - return s:get_path(b:man_default_sects, a:name) + let res = s:get_path(sect, a:name) + if !empty(res) + return res + endif catch /^command error (/ endtry - endif - if !empty(sect) - try - return s:get_path('', a:name) - catch /^command error (/ - endtry + " try again with b:man_default_sects + if exists('b:man_default_sects') + let sects = split(b:man_default_sects, ',') + for sec in sects + try + let res = s:get_path(sec, a:name) + if !empty(res) + return res + endif + catch /^command error (/ + endtry + endfor + endif endif + " if none of the above worked, we will try with no section + try + let res = s:get_path('', a:name) + if !empty(res) + return res + endif + catch /^command error (/ + endtry + + " if that still didn't work, we will check for $MANSECT and try again with it + " unset if !empty($MANSECT) try let MANSECT = $MANSECT call setenv('MANSECT', v:null) - return s:get_path('', a:name) + let res = s:get_path('', a:name) + if !empty(res) + return res + endif catch /^command error (/ finally call setenv('MANSECT', MANSECT) endtry endif + " finally, if that didn't work, there is no hope throw 'no manual entry for ' . a:name endfunction diff --git a/runtime/autoload/provider/python.vim b/runtime/autoload/provider/python.vim deleted file mode 100644 index 8a1d162784..0000000000 --- a/runtime/autoload/provider/python.vim +++ /dev/null @@ -1,45 +0,0 @@ -" The Python provider uses a Python host to emulate an environment for running -" python-vim plugins. :help provider -" -" Associating the plugin with the Python host is the first step because plugins -" will be passed as command-line arguments - -if exists('g:loaded_python_provider') - finish -endif -let [s:prog, s:err] = provider#pythonx#Detect(2) -let g:loaded_python_provider = empty(s:prog) ? 1 : 2 - -function! provider#python#Prog() abort - return s:prog -endfunction - -function! provider#python#Error() abort - return s:err -endfunction - -" The Python provider plugin will run in a separate instance of the Python -" host. -call remote#host#RegisterClone('legacy-python-provider', 'python') -call remote#host#RegisterPlugin('legacy-python-provider', 'script_host.py', []) - -function! provider#python#Call(method, args) abort - if s:err != '' - return - endif - if !exists('s:host') - let s:rpcrequest = function('rpcrequest') - - " Ensure that we can load the Python host before bootstrapping - try - let s:host = remote#host#Require('legacy-python-provider') - catch - let s:err = v:exception - echohl WarningMsg - echomsg v:exception - echohl None - return - endtry - endif - return call(s:rpcrequest, insert(insert(a:args, 'python_'.a:method), s:host)) -endfunction diff --git a/runtime/autoload/provider/pythonx.vim b/runtime/autoload/provider/pythonx.vim index 0eeb35cba8..5b299b322c 100644 --- a/runtime/autoload/provider/pythonx.vim +++ b/runtime/autoload/provider/pythonx.vim @@ -6,10 +6,8 @@ endif let s:loaded_pythonx_provider = 1 function! provider#pythonx#Require(host) abort - let ver = (a:host.orig_name ==# 'python') ? 2 : 3 - " Python host arguments - let prog = (ver == '2' ? provider#python#Prog() : provider#python3#Prog()) + let prog = provider#python3#Prog() let args = [prog, '-c', 'import sys; sys.path = list(filter(lambda x: x != "", sys.path)); import neovim; neovim.start_host()'] @@ -23,14 +21,12 @@ function! provider#pythonx#Require(host) abort endfunction function! s:get_python_executable_from_host_var(major_version) abort - return expand(get(g:, 'python'.(a:major_version == 3 ? '3' : '').'_host_prog', ''), v:true) + return expand(get(g:, 'python'.(a:major_version == 3 ? '3' : execute("throw 'unsupported'")).'_host_prog', ''), v:true) endfunction function! s:get_python_candidates(major_version) abort return { - \ 2: ['python2', 'python2.7', 'python2.6', 'python'], - \ 3: ['python3', 'python3.10', 'python3.9', 'python3.8', 'python3.7', - \ 'python3.6', 'python'] + \ 3: ['python3', 'python3.10', 'python3.9', 'python3.8', 'python3.7', 'python'] \ }[a:major_version] endfunction @@ -82,7 +78,7 @@ function! provider#pythonx#CheckForModule(prog, module, major_version) abort return [0, a:prog . ' not found in search path or not executable.'] endif - let min_version = (a:major_version == 2) ? '2.6' : '3.3' + let min_version = '3.7' " Try to load module, and output Python version. " Exit codes: diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index e73634c632..7bae5bb94b 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -45,7 +45,7 @@ start with a TCP/IP socket instead, use |--listen| with a TCP-style address: > More endpoints can be started with |serverstart()|. Note that localhost TCP sockets are generally less secure than named pipes, -and can lead to vunerabilities like remote code execution. +and can lead to vulnerabilities like remote code execution. Connecting to the socket is the easiest way a programmer can test the API, which can be done through any msgpack-rpc client library or full-featured @@ -186,7 +186,7 @@ About the `functions` map: a type name, e.g. `nvim_buf_get_lines` is the `get_lines` method of a Buffer instance. |dev-api| - Global functions have the "method=false" flag and are prefixed with just - `nvim_`, e.g. `nvim_get_buffers`. + `nvim_`, e.g. `nvim_list_bufs`. *api-mapping* External programs (clients) can use the metadata to discover the API, using @@ -198,7 +198,7 @@ any of these approaches: 2. Start Nvim with |--api-info|. Useful for statically-compiled clients. Example (requires Python "pyyaml" and "msgpack-python" modules): > - nvim --api-info | python -c 'import msgpack, sys, yaml; print yaml.dump(msgpack.unpackb(sys.stdin.read()))' + nvim --api-info | python -c 'import msgpack, sys, yaml; yaml.dump(msgpack.unpackb(sys.stdin.buffer.read()), sys.stdout)' < 3. Use the |api_info()| Vimscript function. > :lua print(vim.inspect(vim.fn.api_info())) @@ -468,7 +468,7 @@ extmark position and enter some text, the extmark migrates forward. > f o o z|b a r line (| = cursor) 4 extmark (after typing "z") -If an extmark is on the last index of a line and you inputsa newline at that +If an extmark is on the last index of a line and you inputs a newline at that point, the extmark will accordingly migrate to the next line: > f o o z b a r| line (| = cursor) @@ -667,14 +667,17 @@ nvim_add_user_command({name}, {command}, {*opts}) {opts} Optional command attributes. See |command-attributes| for more details. To use boolean attributes (such as |:command-bang| or - |:command-bar|) set the value to "true". When - using a Lua function for {command} you can also - provide a "desc" key that will be displayed - when listing commands. In addition to the - string options listed in |:command-complete|, - the "complete" key also accepts a Lua function - which works like the "customlist" completion - mode |:command-complete-customlist|. + |:command-bar|) set the value to "true". In + addition to the string options listed in + |:command-complete|, the "complete" key also + accepts a Lua function which works like the + "customlist" completion mode + |:command-completion-customlist|. Additional + parameters: + • desc: (string) Used for listing the command + when a Lua function is used for {command}. + • force: (boolean, default true) Override any + previous definition. nvim_call_atomic({calls}) *nvim_call_atomic()* Calls many API methods atomically. @@ -706,8 +709,8 @@ nvim_chan_send({chan}, {data}) *nvim_chan_send()* Send data to channel `id` . For a job, it writes it to the stdin of the process. For the stdio channel |channel-stdio|, it writes to Nvim's stdout. For an internal terminal instance - (|nvim_open_term()|) it writes directly to terimal output. See - |channel-bytes| for more information. + (|nvim_open_term()|) it writes directly to terminal output. + See |channel-bytes| for more information. This function writes raw data, not RPC messages. If the channel was created with `rpc=true` then the channel expects @@ -1402,7 +1405,7 @@ nvim_replace_termcodes({str}, {from_part}, {do_lt}, {special}) {from_part} Legacy Vim parameter. Usually true. {do_lt} Also translate <lt>. Ignored if `special` is false. - {special} Replace |keycodes|, e.g. <CR> becomes a "\n" + {special} Replace |keycodes|, e.g. <CR> becomes a "\r" char. See also: ~ @@ -2545,6 +2548,10 @@ nvim_buf_set_extmark({buffer}, {ns_id}, {line}, {col}, {*opts}) • priority: a priority value for the highlight group. For example treesitter highlighting uses a value of 100. + • strict: boolean that indicates extmark should + not be placed if the line or column value is + past the end of the buffer or end of the line + respectively. Defaults to true. Return: ~ Id of the created/updated extmark diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt index 3df8d5ced4..ed75acf36e 100644 --- a/runtime/doc/autocmd.txt +++ b/runtime/doc/autocmd.txt @@ -40,10 +40,10 @@ effects. Be careful not to destroy your text. 2. Defining autocommands *autocmd-define* *:au* *:autocmd* -:au[tocmd] [group] {event} {pat} [++once] [++nested] {cmd} +:au[tocmd] [group] {event} {aupat} [++once] [++nested] {cmd} Add {cmd} to the list of commands that Vim will execute automatically on {event} for a file matching - {pat} |autocmd-pattern|. + {aupat} |autocmd-pattern|. Note: A quote character is seen as argument to the :autocmd and won't start a comment. Nvim always adds {cmd} after existing autocommands so @@ -119,19 +119,19 @@ prompt. When one command outputs two messages this can happen anyway. ============================================================================== 3. Removing autocommands *autocmd-remove* -:au[tocmd]! [group] {event} {pat} [++once] [++nested] {cmd} +:au[tocmd]! [group] {event} {aupat} [++once] [++nested] {cmd} Remove all autocommands associated with {event} and - {pat}, and add the command {cmd}. + {aupat}, and add the command {cmd}. See |autocmd-once| for [++once]. See |autocmd-nested| for [++nested]. -:au[tocmd]! [group] {event} {pat} +:au[tocmd]! [group] {event} {aupat} Remove all autocommands associated with {event} and - {pat}. + {aupat}. -:au[tocmd]! [group] * {pat} - Remove all autocommands associated with {pat} for all - events. +:au[tocmd]! [group] * {aupat} + Remove all autocommands associated with {aupat} for + all events. :au[tocmd]! [group] {event} Remove ALL autocommands for {event}. @@ -151,12 +151,12 @@ with ":augroup"); otherwise, Vim uses the group defined with [group]. ============================================================================== 4. Listing autocommands *autocmd-list* -:au[tocmd] [group] {event} {pat} +:au[tocmd] [group] {event} {aupat} Show the autocommands associated with {event} and - {pat}. + {aupat}. -:au[tocmd] [group] * {pat} - Show the autocommands associated with {pat} for all +:au[tocmd] [group] * {aupat} + Show the autocommands associated with {aupat} for all events. :au[tocmd] [group] {event} @@ -526,7 +526,7 @@ DirChanged After the |current-directory| was changed. "auto" to trigger on 'autochdir'. Sets these |v:event| keys: cwd: current working directory - scope: "global", "tab", "window" + scope: "global", "tabpage", "window" changed_window: v:true if we fired the event switching window (or tab) <afile> is set to the new directory name. @@ -840,6 +840,9 @@ RecordingLeave When a macro stops recording. register. |reg_recorded()| is only updated after this event. + Sets these |v:event| keys: + regcontents + regname *SessionLoadPost* SessionLoadPost After loading the session file created using the |:mksession| command. @@ -1072,16 +1075,16 @@ WinScrolled After scrolling the viewport of the current ============================================================================== -6. Patterns *autocmd-pattern* *{pat}* +6. Patterns *autocmd-pattern* *{aupat}* -The {pat} argument can be a comma separated list. This works as if the -command was given with each pattern separately. Thus this command: > +The {aupat} argument of `:autocmd` can be a comma separated list. This works +as if the command was given with each pattern separately. Thus this command: > :autocmd BufRead *.txt,*.info set et Is equivalent to: > :autocmd BufRead *.txt set et :autocmd BufRead *.info set et -The file pattern {pat} is tested for a match against the file name in one of +The file pattern {aupat} is tested for a match against the file name in one of two ways: 1. When there is no '/' in the pattern, Vim checks for a match against only the tail part of the file name (without its leading directory path). diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt new file mode 100644 index 0000000000..35a232c0c2 --- /dev/null +++ b/runtime/doc/builtin.txt @@ -0,0 +1,8962 @@ +*builtin.txt* Nvim + + + VIM REFERENCE MANUAL by Bram Moolenaar + + +Builtin functions *builtin-functions* + +1. Overview |builtin-function-list| +2. Details |builtin-function-details| +3. Matching a pattern in a String |string-match| + +============================================================================== +1. Overview *builtin-function-list* + +Use CTRL-] on the function name to jump to the full explanation. + +USAGE RESULT DESCRIPTION ~ + +abs({expr}) Float or Number absolute value of {expr} +acos({expr}) Float arc cosine of {expr} +add({object}, {item}) List/Blob append {item} to {object} +and({expr}, {expr}) Number bitwise AND +api_info() Dict api metadata +append({lnum}, {text}) Number append {text} below line {lnum} +appendbufline({expr}, {lnum}, {text}) + Number append {text} below line {lnum} + in buffer {expr} +argc([{winid}]) Number number of files in the argument list +argidx() Number current index in the argument list +arglistid([{winnr} [, {tabnr}]]) Number argument list id +argv({nr} [, {winid}]) String {nr} entry of the argument list +argv([-1, {winid}]) List the argument list +asin({expr}) Float arc sine of {expr} +assert_beeps({cmd}) Number assert {cmd} causes a beep +assert_equal({exp}, {act} [, {msg}]) + Number assert {exp} is equal to {act} +assert_equalfile({fname-one}, {fname-two} [, {msg}]) + Number assert file contents are equal +assert_exception({error} [, {msg}]) + Number assert {error} is in v:exception +assert_fails({cmd} [, {error}]) Number assert {cmd} fails +assert_false({actual} [, {msg}]) + Number assert {actual} is false +assert_inrange({lower}, {upper}, {actual} [, {msg}]) + Number assert {actual} is inside the range +assert_match({pat}, {text} [, {msg}]) + Number assert {pat} matches {text} +assert_nobeep({cmd}) Number assert {cmd} does not cause a beep +assert_notequal({exp}, {act} [, {msg}]) + Number assert {exp} is not equal {act} +assert_notmatch({pat}, {text} [, {msg}]) + Number assert {pat} not matches {text} +assert_report({msg}) Number report a test failure +assert_true({actual} [, {msg}]) Number assert {actual} is true +atan({expr}) Float arc tangent of {expr} +atan2({expr1}, {expr2}) Float arc tangent of {expr1} / {expr2} +browse({save}, {title}, {initdir}, {default}) + String put up a file requester +browsedir({title}, {initdir}) String put up a directory requester +bufadd({name}) Number add a buffer to the buffer list +bufexists({expr}) Number |TRUE| if buffer {expr} exists +buflisted({expr}) Number |TRUE| if buffer {expr} is listed +bufload({expr}) Number load buffer {expr} if not loaded yet +bufloaded({expr}) Number |TRUE| if buffer {expr} is loaded +bufname([{expr}]) String Name of the buffer {expr} +bufnr([{expr} [, {create}]]) Number Number of the buffer {expr} +bufwinid({expr}) Number |window-ID| of buffer {expr} +bufwinnr({expr}) Number window number of buffer {expr} +byte2line({byte}) Number line number at byte count {byte} +byteidx({expr}, {nr}) Number byte index of {nr}'th char in {expr} +byteidxcomp({expr}, {nr}) Number byte index of {nr}'th char in {expr} +call({func}, {arglist} [, {dict}]) + any call {func} with arguments {arglist} +ceil({expr}) Float round {expr} up +changenr() Number current change number +chanclose({id} [, {stream}]) Number Closes a channel or one of its streams +chansend({id}, {data}) Number Writes {data} to channel +char2nr({expr} [, {utf8}]) Number ASCII/UTF-8 value of first char in {expr} +charcol({expr}) Number column number of cursor or mark +charidx({string}, {idx} [, {countcc}]) + Number char index of byte {idx} in {string} +chdir({dir}) String change current working directory +cindent({lnum}) Number C indent for line {lnum} +clearmatches([{win}]) none clear all matches +col({expr}) Number column byte index of cursor or mark +complete({startcol}, {matches}) none set Insert mode completion +complete_add({expr}) Number add completion match +complete_check() Number check for key typed during completion +complete_info([{what}]) Dict get current completion information +confirm({msg} [, {choices} [, {default} [, {type}]]]) + Number number of choice picked by user +copy({expr}) any make a shallow copy of {expr} +cos({expr}) Float cosine of {expr} +cosh({expr}) Float hyperbolic cosine of {expr} +count({comp}, {expr} [, {ic} [, {start}]]) + Number count how many {expr} are in {comp} +cscope_connection([{num}, {dbpath} [, {prepend}]]) + Number checks existence of cscope connection +ctxget([{index}]) Dict return the |context| dict at {index} +ctxpop() none pop and restore |context| from the + |context-stack| +ctxpush([{types}]) none push the current |context| to the + |context-stack| +ctxset({context} [, {index}]) none set |context| at {index} +ctxsize() Number return |context-stack| size +cursor({lnum}, {col} [, {off}]) + Number move cursor to {lnum}, {col}, {off} +cursor({list}) Number move cursor to position in {list} +debugbreak({pid}) Number interrupt process being debugged +deepcopy({expr} [, {noref}]) any make a full copy of {expr} +delete({fname} [, {flags}]) Number delete the file or directory {fname} +deletebufline({buf}, {first} [, {last}]) + Number delete lines from buffer {buf} +dictwatcheradd({dict}, {pattern}, {callback}) + Start watching a dictionary +dictwatcherdel({dict}, {pattern}, {callback}) + Stop watching a dictionary +did_filetype() Number |TRUE| if FileType autocommand event used +diff_filler({lnum}) Number diff filler lines about {lnum} +diff_hlID({lnum}, {col}) Number diff highlighting at {lnum}/{col} +empty({expr}) Number |TRUE| if {expr} is empty +environ() Dict return environment variables +escape({string}, {chars}) String escape {chars} in {string} with '\' +eval({string}) any evaluate {string} into its value +eventhandler() Number |TRUE| if inside an event handler +executable({expr}) Number 1 if executable {expr} exists +execute({command}) String execute and capture output of {command} +exepath({expr}) String full path of the command {expr} +exists({expr}) Number |TRUE| if {expr} exists +extend({expr1}, {expr2} [, {expr3}]) + List/Dict insert items of {expr2} into {expr1} +exp({expr}) Float exponential of {expr} +expand({expr} [, {nosuf} [, {list}]]) + any expand special keywords in {expr} +expandcmd({expr}) String expand {expr} like with `:edit` +feedkeys({string} [, {mode}]) Number add key sequence to typeahead buffer +filereadable({file}) Number |TRUE| if {file} is a readable file +filewritable({file}) Number |TRUE| if {file} is a writable file +filter({expr1}, {expr2}) List/Dict remove items from {expr1} where + {expr2} is 0 +finddir({name} [, {path} [, {count}]]) + String find directory {name} in {path} +findfile({name} [, {path} [, {count}]]) + String find file {name} in {path} +flatten({list} [, {maxdepth}]) List flatten {list} up to {maxdepth} levels +float2nr({expr}) Number convert Float {expr} to a Number +floor({expr}) Float round {expr} down +fmod({expr1}, {expr2}) Float remainder of {expr1} / {expr2} +fnameescape({fname}) String escape special characters in {fname} +fnamemodify({fname}, {mods}) String modify file name +foldclosed({lnum}) Number first line of fold at {lnum} if closed +foldclosedend({lnum}) Number last line of fold at {lnum} if closed +foldlevel({lnum}) Number fold level at {lnum} +foldtext() String line displayed for closed fold +foldtextresult({lnum}) String text for closed fold at {lnum} +foreground() Number bring the Vim window to the foreground +fullcommand({name}) String get full command from {name} +funcref({name} [, {arglist}] [, {dict}]) + Funcref reference to function {name} +function({name} [, {arglist}] [, {dict}]) + Funcref named reference to function {name} +garbagecollect([{atexit}]) none free memory, breaking cyclic references +get({list}, {idx} [, {def}]) any get item {idx} from {list} or {def} +get({dict}, {key} [, {def}]) any get item {key} from {dict} or {def} +get({func}, {what}) any get property of funcref/partial {func} +getbufinfo([{buf}]) List information about buffers +getbufline({buf}, {lnum} [, {end}]) + List lines {lnum} to {end} of buffer {buf} +getbufvar({buf}, {varname} [, {def}]) + any variable {varname} in buffer {buf} +getchangelist([{buf}]) List list of change list items +getchar([expr]) Number or String + get one character from the user +getcharmod() Number modifiers for the last typed character +getcharpos({expr}) List position of cursor, mark, etc. +getcharsearch() Dict last character search +getcharstr([expr]) String get one character from the user +getcmdline() String return the current command-line +getcmdpos() Number return cursor position in command-line +getcmdtype() String return current command-line type +getcmdwintype() String return current command-line window type +getcompletion({pat}, {type} [, {filtered}]) + List list of cmdline completion matches +getcurpos([{winnr}]) List position of the cursor +getcursorcharpos([{winnr}]) List character position of the cursor +getcwd([{winnr} [, {tabnr}]]) String get the current working directory +getenv({name}) String return environment variable +getfontname([{name}]) String name of font being used +getfperm({fname}) String file permissions of file {fname} +getfsize({fname}) Number size in bytes of file {fname} +getftime({fname}) Number last modification time of file +getftype({fname}) String description of type of file {fname} +getjumplist([{winnr} [, {tabnr}]]) + List list of jump list items +getline({lnum}) String line {lnum} of current buffer +getline({lnum}, {end}) List lines {lnum} to {end} of current buffer +getloclist({nr}) List list of location list items +getloclist({nr}, {what}) Dict get specific location list properties +getmarklist([{buf}]) List list of global/local marks +getmatches([{win}]) List list of current matches +getmousepos() Dict last known mouse position +getpid() Number process ID of Vim +getpos({expr}) List position of cursor, mark, etc. +getqflist() List list of quickfix items +getqflist({what}) Dict get specific quickfix list properties +getreg([{regname} [, 1 [, {list}]]]) + String or List contents of a register +getreginfo([{regname}]) Dict information about a register +getregtype([{regname}]) String type of a register +gettabinfo([{expr}]) List list of tab pages +gettabvar({nr}, {varname} [, {def}]) + any variable {varname} in tab {nr} or {def} +gettabwinvar({tabnr}, {winnr}, {name} [, {def}]) + any {name} in {winnr} in tab page {tabnr} +gettagstack([{nr}]) Dict get the tag stack of window {nr} +getwininfo([{winid}]) List list of info about each window +getwinpos([{timeout}]) List X and Y coord in pixels of the Vim window +getwinposx() Number X coord in pixels of Vim window +getwinposy() Number Y coord in pixels of Vim window +getwinvar({nr}, {varname} [, {def}]) + any variable {varname} in window {nr} +glob({expr} [, {nosuf} [, {list} [, {alllinks}]]]) + any expand file wildcards in {expr} +glob2regpat({expr}) String convert a glob pat into a search pat +globpath({path}, {expr} [, {nosuf} [, {list} [, {alllinks}]]]) + String do glob({expr}) for all dirs in {path} +has({feature}) Number |TRUE| if feature {feature} supported +has_key({dict}, {key}) Number |TRUE| if {dict} has entry {key} +haslocaldir([{winnr} [, {tabnr}]]) + Number |TRUE| if the window executed |:lcd| or + the tab executed |:tcd| +hasmapto({what} [, {mode} [, {abbr}]]) + Number |TRUE| if mapping to {what} exists +histadd({history}, {item}) String add an item to a history +histdel({history} [, {item}]) String remove an item from a history +histget({history} [, {index}]) String get the item {index} from a history +histnr({history}) Number highest index of a history +hlexists({name}) Number |TRUE| if highlight group {name} exists +hlID({name}) Number syntax ID of highlight group {name} +hostname() String name of the machine Vim is running on +iconv({expr}, {from}, {to}) String convert encoding of {expr} +indent({lnum}) Number indent of line {lnum} +index({object}, {expr} [, {start} [, {ic}]]) + Number index in {object} where {expr} appears +input({prompt} [, {text} [, {completion}]]) + String get input from the user +inputlist({textlist}) Number let the user pick from a choice list +inputrestore() Number restore typeahead +inputsave() Number save and clear typeahead +inputsecret({prompt} [, {text}]) + String like input() but hiding the text +insert({object}, {item} [, {idx}]) + List insert {item} in {object} [before {idx}] +interrupt() none interrupt script execution +invert({expr}) Number bitwise invert +isdirectory({directory}) Number |TRUE| if {directory} is a directory +isinf({expr}) Number determine if {expr} is infinity value + (positive or negative) +islocked({expr}) Number |TRUE| if {expr} is locked +isnan({expr}) Number |TRUE| if {expr} is NaN +id({expr}) String identifier of the container +items({dict}) List key-value pairs in {dict} +jobpid({id}) Number Returns pid of a job. +jobresize({id}, {width}, {height}) + Number Resize pseudo terminal window of a job +jobstart({cmd} [, {opts}]) Number Spawns {cmd} as a job +jobstop({id}) Number Stops a job +jobwait({ids} [, {timeout}]) Number Wait for a set of jobs +join({list} [, {sep}]) String join {list} items into one String +json_decode({expr}) any Convert {expr} from JSON +json_encode({expr}) String Convert {expr} to JSON +keys({dict}) List keys in {dict} +len({expr}) Number the length of {expr} +libcall({lib}, {func}, {arg}) String call {func} in library {lib} with {arg} +libcallnr({lib}, {func}, {arg}) Number idem, but return a Number +line({expr} [, {winid}]) Number line nr of cursor, last line or mark +line2byte({lnum}) Number byte count of line {lnum} +lispindent({lnum}) Number Lisp indent for line {lnum} +list2str({list} [, {utf8}]) String turn numbers in {list} into a String +localtime() Number current time +log({expr}) Float natural logarithm (base e) of {expr} +log10({expr}) Float logarithm of Float {expr} to base 10 +luaeval({expr} [, {expr}]) any evaluate |Lua| expression +map({expr1}, {expr2}) List/Dict change each item in {expr1} to {expr} +maparg({name} [, {mode} [, {abbr} [, {dict}]]]) + String or Dict + rhs of mapping {name} in mode {mode} +mapcheck({name} [, {mode} [, {abbr}]]) + String check for mappings matching {name} +match({expr}, {pat} [, {start} [, {count}]]) + Number position where {pat} matches in {expr} +matchadd({group}, {pattern} [, {priority} [, {id} [, {dict}]]]) + Number highlight {pattern} with {group} +matchaddpos({group}, {pos} [, {priority} [, {id} [, {dict}]]]) + Number highlight positions with {group} +matcharg({nr}) List arguments of |:match| +matchdelete({id} [, {win}]) Number delete match identified by {id} +matchend({expr}, {pat} [, {start} [, {count}]]) + Number position where {pat} ends in {expr} +matchfuzzy({list}, {str} [, {dict}]) + List fuzzy match {str} in {list} +matchfuzzypos({list}, {str} [, {dict}]) + List fuzzy match {str} in {list} +matchlist({expr}, {pat} [, {start} [, {count}]]) + List match and submatches of {pat} in {expr} +matchstr({expr}, {pat} [, {start} [, {count}]]) + String {count}'th match of {pat} in {expr} +matchstrpos({expr}, {pat} [, {start} [, {count}]]) + List {count}'th match of {pat} in {expr} +max({expr}) Number maximum value of items in {expr} +menu_get({path} [, {modes}]) List description of |menus| matched by {path} +min({expr}) Number minimum value of items in {expr} +mkdir({name} [, {path} [, {prot}]]) + Number create directory {name} +mode([expr]) String current editing mode +msgpackdump({list} [, {type}]) List/Blob dump objects to msgpack +msgpackparse({data}) List parse msgpack to a list of objects +nextnonblank({lnum}) Number line nr of non-blank line >= {lnum} +nr2char({expr} [, {utf8}]) String single char with ASCII/UTF-8 value {expr} +nvim_...({args}...) any call nvim |api| functions +or({expr}, {expr}) Number bitwise OR +pathshorten({expr} [, {len}]) String shorten directory names in a path +perleval({expr}) any evaluate |perl| expression +pow({x}, {y}) Float {x} to the power of {y} +prevnonblank({lnum}) Number line nr of non-blank line <= {lnum} +printf({fmt}, {expr1}...) String format text +prompt_getprompt({buf}) String get prompt text +prompt_setcallback({buf}, {expr}) none set prompt callback function +prompt_setinterrupt({buf}, {text}) none set prompt interrupt function +prompt_setprompt({buf}, {text}) none set prompt text +pum_getpos() Dict position and size of pum if visible +pumvisible() Number whether popup menu is visible +pyeval({expr}) any evaluate |Python| expression +py3eval({expr}) any evaluate |python3| expression +pyxeval({expr}) any evaluate |python_x| expression +rand([{expr}]) Number get pseudo-random number +range({expr} [, {max} [, {stride}]]) + List items from {expr} to {max} +readdir({dir} [, {expr}]) List file names in {dir} selected by {expr} +readfile({fname} [, {type} [, {max}]]) + List get list of lines from file {fname} +reduce({object}, {func} [, {initial}]) + any reduce {object} using {func} +reg_executing() String get the executing register name +reg_recorded() String get the last recorded register name +reg_recording() String get the recording register name +reltime([{start} [, {end}]]) List get time value +reltimefloat({time}) Float turn the time value into a Float +reltimestr({time}) String turn time value into a String +remote_expr({server}, {string} [, {idvar} [, {timeout}]]) + String send expression +remote_foreground({server}) Number bring Vim server to the foreground +remote_peek({serverid} [, {retvar}]) + Number check for reply string +remote_read({serverid} [, {timeout}]) + String read reply string +remote_send({server}, {string} [, {idvar}]) + String send key sequence +remote_startserver({name}) none become server {name} +remove({list}, {idx} [, {end}]) any/List + remove items {idx}-{end} from {list} +remove({blob}, {idx} [, {end}]) Number/Blob + remove bytes {idx}-{end} from {blob} +remove({dict}, {key}) any remove entry {key} from {dict} +rename({from}, {to}) Number rename (move) file from {from} to {to} +repeat({expr}, {count}) String repeat {expr} {count} times +resolve({filename}) String get filename a shortcut points to +reverse({list}) List reverse {list} in-place +round({expr}) Float round off {expr} +rubyeval({expr}) any evaluate |Ruby| expression +rpcnotify({channel}, {event} [, {args}...]) + Sends an |RPC| notification to {channel} +rpcrequest({channel}, {method} [, {args}...]) + Sends an |RPC| request to {channel} +screenattr({row}, {col}) Number attribute at screen position +screenchar({row}, {col}) Number character at screen position +screenchars({row}, {col}) List List of characters at screen position +screencol() Number current cursor column +screenpos({winid}, {lnum}, {col}) Dict screen row and col of a text character +screenrow() Number current cursor row +screenstring({row}, {col}) String characters at screen position +search({pattern} [, {flags} [, {stopline} [, {timeout} [, {skip}]]]]) + Number search for {pattern} +searchcount([{options}]) Dict Get or update the last search count +searchdecl({name} [, {global} [, {thisblock}]]) + Number search for variable declaration +searchpair({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) + Number search for other end of start/end pair +searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) + List search for other end of start/end pair +searchpos({pattern} [, {flags} [, {stopline} [, {timeout} [, {skip}]]]]) + List search for {pattern} +server2client({clientid}, {string}) + Number send reply string +serverlist() String get a list of available servers +setbufline({expr}, {lnum}, {text}) + Number set line {lnum} to {text} in buffer + {expr} +setbufvar({buf}, {varname}, {val}) set {varname} in buffer {buf} to {val} +setcharpos({expr}, {list}) Number set the {expr} position to {list} +setcharsearch({dict}) Dict set character search from {dict} +setcmdpos({pos}) Number set cursor position in command-line +setcursorcharpos({list}) Number move cursor to position in {list} +setenv({name}, {val}) none set environment variable +setfperm({fname}, {mode} Number set {fname} file permissions to {mode} +setline({lnum}, {line}) Number set line {lnum} to {line} +setloclist({nr}, {list} [, {action}]) + Number modify location list using {list} +setloclist({nr}, {list}, {action}, {what}) + Number modify specific location list props +setmatches({list} [, {win}]) Number restore a list of matches +setpos({expr}, {list}) Number set the {expr} position to {list} +setqflist({list} [, {action}]) Number modify quickfix list using {list} +setqflist({list}, {action}, {what}) + Number modify specific quickfix list props +setreg({n}, {v} [, {opt}]) Number set register to value and type +settabvar({nr}, {varname}, {val}) set {varname} in tab page {nr} to {val} +settabwinvar({tabnr}, {winnr}, {varname}, {val}) set {varname} in window + {winnr} in tab page {tabnr} to {val} +settagstack({nr}, {dict} [, {action}]) + Number modify tag stack using {dict} +setwinvar({nr}, {varname}, {val}) set {varname} in window {nr} to {val} +sha256({string}) String SHA256 checksum of {string} +shellescape({string} [, {special}]) + String escape {string} for use as shell + command argument +shiftwidth([{col}]) Number effective value of 'shiftwidth' +sign_define({name} [, {dict}]) Number define or update a sign +sign_define({list}) List define or update a list of signs +sign_getdefined([{name}]) List get a list of defined signs +sign_getplaced([{buf} [, {dict}]]) + List get a list of placed signs +sign_jump({id}, {group}, {buf}) + Number jump to a sign +sign_place({id}, {group}, {name}, {buf} [, {dict}]) + Number place a sign +sign_placelist({list}) List place a list of signs +sign_undefine([{name}]) Number undefine a sign +sign_undefine({list}) List undefine a list of signs +sign_unplace({group} [, {dict}]) + Number unplace a sign +sign_unplacelist({list}) List unplace a list of signs +simplify({filename}) String simplify filename as much as possible +sin({expr}) Float sine of {expr} +sinh({expr}) Float hyperbolic sine of {expr} +sockconnect({mode}, {address} [, {opts}]) + Number Connects to socket +sort({list} [, {func} [, {dict}]]) + List sort {list}, using {func} to compare +soundfold({word}) String sound-fold {word} +spellbadword() String badly spelled word at cursor +spellsuggest({word} [, {max} [, {capital}]]) + List spelling suggestions +split({expr} [, {pat} [, {keepempty}]]) + List make |List| from {pat} separated {expr} +sqrt({expr}) Float square root of {expr} +srand([{expr}]) List get seed for |rand()| +stdioopen({dict}) Number open stdio in a headless instance. +stdpath({what}) String/List returns the standard path(s) for {what} +str2float({expr} [, {quoted}]) Float convert String to Float +str2list({expr} [, {utf8}]) List convert each character of {expr} to + ASCII/UTF-8 value +str2nr({expr} [, {base} [, {quoted}]]) + Number convert String to Number +strchars({expr} [, {skipcc}]) Number character length of the String {expr} +strcharpart({str}, {start} [, {len}]) + String {len} characters of {str} at + character {start} +strdisplaywidth({expr} [, {col}]) Number display length of the String {expr} +strftime({format} [, {time}]) String format time with a specified format +strgetchar({str}, {index}) Number get char {index} from {str} +stridx({haystack}, {needle} [, {start}]) + Number index of {needle} in {haystack} +string({expr}) String String representation of {expr} value +strlen({expr}) Number length of the String {expr} +strpart({str}, {start} [, {len} [, {chars}]]) + String {len} bytes/chars of {str} at + byte {start} +strptime({format}, {timestring}) + Number Convert {timestring} to unix timestamp +strridx({haystack}, {needle} [, {start}]) + Number last index of {needle} in {haystack} +strtrans({expr}) String translate string to make it printable +strwidth({expr}) Number display cell length of the String {expr} +submatch({nr} [, {list}]) String or List + specific match in ":s" or substitute() +substitute({expr}, {pat}, {sub}, {flags}) + String all {pat} in {expr} replaced with {sub} +swapinfo({fname}) Dict information about swap file {fname} +swapname({buf}) String swap file of buffer {buf} +synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col} +synIDattr({synID}, {what} [, {mode}]) + String attribute {what} of syntax ID {synID} +synIDtrans({synID}) Number translated syntax ID of {synID} +synconcealed({lnum}, {col}) List info about concealing +synstack({lnum}, {col}) List stack of syntax IDs at {lnum} and {col} +system({cmd} [, {input}]) String output of shell command/filter {cmd} +systemlist({cmd} [, {input}]) List output of shell command/filter {cmd} +tabpagebuflist([{arg}]) List list of buffer numbers in tab page +tabpagenr([{arg}]) Number number of current or last tab page +tabpagewinnr({tabarg} [, {arg}]) + Number number of current window in tab page +taglist({expr} [, {filename}]) List list of tags matching {expr} +tagfiles() List tags files used +tan({expr}) Float tangent of {expr} +tanh({expr}) Float hyperbolic tangent of {expr} +tempname() String name for a temporary file +test_garbagecollect_now() none free memory right now for testing +timer_info([{id}]) List information about timers +timer_pause({id}, {pause}) none pause or unpause a timer +timer_start({time}, {callback} [, {options}]) + Number create a timer +timer_stop({timer}) none stop a timer +timer_stopall() none stop all timers +tolower({expr}) String the String {expr} switched to lowercase +toupper({expr}) String the String {expr} switched to uppercase +tr({src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr} + to chars in {tostr} +trim({text} [, {mask} [, {dir}]]) + String trim characters in {mask} from {text} +trunc({expr}) Float truncate Float {expr} +type({name}) Number type of variable {name} +undofile({name}) String undo file name for {name} +undotree() List undo file tree +uniq({list} [, {func} [, {dict}]]) + List remove adjacent duplicates from a list +values({dict}) List values in {dict} +virtcol({expr}) Number screen column of cursor or mark +visualmode([expr]) String last visual mode used +wait({timeout}, {condition} [, {interval}]) + Number Wait until {condition} is satisfied +wildmenumode() Number whether 'wildmenu' mode is active +win_execute({id}, {command} [, {silent}]) + String execute {command} in window {id} +win_findbuf({bufnr}) List find windows containing {bufnr} +win_getid([{win} [, {tab}]]) Number get |window-ID| for {win} in {tab} +win_gettype([{nr}]) String type of window {nr} +win_gotoid({expr}) Number go to |window-ID| {expr} +win_id2tabwin({expr}) List get tab and window nr from |window-ID| +win_id2win({expr}) Number get window nr from |window-ID| +win_move_separator({nr}) Number move window vertical separator +win_move_statusline({nr}) Number move window status line +win_screenpos({nr}) List get screen position of window {nr} +win_splitmove({nr}, {target} [, {options}]) + Number move window {nr} to split of {target} +winbufnr({nr}) Number buffer number of window {nr} +wincol() Number window column of the cursor +windowsversion() String MS-Windows OS version +winheight({nr}) Number height of window {nr} +winlayout([{tabnr}]) List layout of windows in tab {tabnr} +winline() Number window line of the cursor +winnr([{expr}]) Number number of current window +winrestcmd() String returns command to restore window sizes +winrestview({dict}) none restore view of current window +winsaveview() Dict save view of current window +winwidth({nr}) Number width of window {nr} +wordcount() Dict get byte/char/word statistics +writefile({object}, {fname} [, {flags}]) + Number write |Blob| or |List| of lines to file +xor({expr}, {expr}) Number bitwise XOR + +============================================================================== +2. Details *builtin-function-details* + +Not all functions are here, some have been moved to a help file covering the +specific functionality. + +abs({expr}) *abs()* + Return the absolute value of {expr}. When {expr} evaluates to + a |Float| abs() returns a |Float|. When {expr} can be + converted to a |Number| abs() returns a |Number|. Otherwise + abs() gives an error message and returns -1. + Examples: > + echo abs(1.456) +< 1.456 > + echo abs(-5.456) +< 5.456 > + echo abs(-4) +< 4 + + Can also be used as a |method|: > + Compute()->abs() + +acos({expr}) *acos()* + Return the arc cosine of {expr} measured in radians, as a + |Float| in the range of [0, pi]. + {expr} must evaluate to a |Float| or a |Number| in the range + [-1, 1]. + Examples: > + :echo acos(0) +< 1.570796 > + :echo acos(-0.5) +< 2.094395 + + Can also be used as a |method|: > + Compute()->acos() + +add({object}, {expr}) *add()* + Append the item {expr} to |List| or |Blob| {object}. Returns + the resulting |List| or |Blob|. Examples: > + :let alist = add([1, 2, 3], item) + :call add(mylist, "woodstock") +< Note that when {expr} is a |List| it is appended as a single + item. Use |extend()| to concatenate |Lists|. + When {object} is a |Blob| then {expr} must be a number. + Use |insert()| to add an item at another position. + + Can also be used as a |method|: > + mylist->add(val1)->add(val2) + +and({expr}, {expr}) *and()* + Bitwise AND on the two arguments. The arguments are converted + to a number. A List, Dict or Float argument causes an error. + Example: > + :let flag = and(bits, 0x80) +< Can also be used as a |method|: > + :let flag = bits->and(0x80) + +api_info() *api_info()* + Returns Dictionary of |api-metadata|. + + View it in a nice human-readable format: > + :lua print(vim.inspect(vim.fn.api_info())) + +append({lnum}, {text}) *append()* + When {text} is a |List|: Append each item of the |List| as a + text line below line {lnum} in the current buffer. + Otherwise append {text} as one text line below line {lnum} in + the current buffer. + {lnum} can be zero to insert a line before the first one. + {lnum} is used like with |getline()|. + Returns 1 for failure ({lnum} out of range or out of memory), + 0 for success. Example: > + :let failed = append(line('$'), "# THE END") + :let failed = append(0, ["Chapter 1", "the beginning"]) + +< Can also be used as a |method| after a List: > + mylist->append(lnum) + +appendbufline({buf}, {lnum}, {text}) *appendbufline()* + Like |append()| but append the text in buffer {expr}. + + This function works only for loaded buffers. First call + |bufload()| if needed. + + For the use of {buf}, see |bufname()|. + + {lnum} is used like with |append()|. Note that using |line()| + would use the current buffer, not the one appending to. + Use "$" to append at the end of the buffer. + + On success 0 is returned, on failure 1 is returned. + + If {buf} is not a valid buffer or {lnum} is not valid, an + error message is given. Example: > + :let failed = appendbufline(13, 0, "# THE START") +< + Can also be used as a |method| after a List: > + mylist->appendbufline(buf, lnum) + +argc([{winid}]) *argc()* + The result is the number of files in the argument list. See + |arglist|. + If {winid} is not supplied, the argument list of the current + window is used. + If {winid} is -1, the global argument list is used. + Otherwise {winid} specifies the window of which the argument + list is used: either the window number or the window ID. + Returns -1 if the {winid} argument is invalid. + + *argidx()* +argidx() The result is the current index in the argument list. 0 is + the first file. argc() - 1 is the last one. See |arglist|. + + *arglistid()* +arglistid([{winnr} [, {tabnr}]]) + Return the argument list ID. This is a number which + identifies the argument list being used. Zero is used for the + global argument list. See |arglist|. + Returns -1 if the arguments are invalid. + + Without arguments use the current window. + With {winnr} only use this window in the current tab page. + With {winnr} and {tabnr} use the window in the specified tab + page. + {winnr} can be the window number or the |window-ID|. + + *argv()* +argv([{nr} [, {winid}]]) + The result is the {nr}th file in the argument list. See + |arglist|. "argv(0)" is the first one. Example: > + :let i = 0 + :while i < argc() + : let f = escape(fnameescape(argv(i)), '.') + : exe 'amenu Arg.' . f . ' :e ' . f . '<CR>' + : let i = i + 1 + :endwhile +< Without the {nr} argument, or when {nr} is -1, a |List| with + the whole |arglist| is returned. + + The {winid} argument specifies the window ID, see |argc()|. + For the Vim command line arguments see |v:argv|. + +asin({expr}) *asin()* + Return the arc sine of {expr} measured in radians, as a |Float| + in the range of [-pi/2, pi/2]. + {expr} must evaluate to a |Float| or a |Number| in the range + [-1, 1]. + Examples: > + :echo asin(0.8) +< 0.927295 > + :echo asin(-0.5) +< -0.523599 + + Can also be used as a |method|: > + Compute()->asin() + + +assert_ functions are documented here: |assert-functions-details| + + +atan({expr}) *atan()* + Return the principal value of the arc tangent of {expr}, in + the range [-pi/2, +pi/2] radians, as a |Float|. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo atan(100) +< 1.560797 > + :echo atan(-4.01) +< -1.326405 + + Can also be used as a |method|: > + Compute()->atan() + +atan2({expr1}, {expr2}) *atan2()* + Return the arc tangent of {expr1} / {expr2}, measured in + radians, as a |Float| in the range [-pi, pi]. + {expr1} and {expr2} must evaluate to a |Float| or a |Number|. + Examples: > + :echo atan2(-1, 1) +< -0.785398 > + :echo atan2(1, -1) +< 2.356194 + + Can also be used as a |method|: > + Compute()->atan2(1) + + *browse()* +browse({save}, {title}, {initdir}, {default}) + Put up a file requester. This only works when "has("browse")" + returns |TRUE| (only in some GUI versions). + The input fields are: + {save} when |TRUE|, select file to write + {title} title for the requester + {initdir} directory to start browsing in + {default} default file name + An empty string is returned when the "Cancel" button is hit, + something went wrong, or browsing is not possible. + + *browsedir()* +browsedir({title}, {initdir}) + Put up a directory requester. This only works when + "has("browse")" returns |TRUE| (only in some GUI versions). + On systems where a directory browser is not supported a file + browser is used. In that case: select a file in the directory + to be used. + The input fields are: + {title} title for the requester + {initdir} directory to start browsing in + When the "Cancel" button is hit, something went wrong, or + browsing is not possible, an empty string is returned. + +bufadd({name}) *bufadd()* + Add a buffer to the buffer list with String {name}. + If a buffer for file {name} already exists, return that buffer + number. Otherwise return the buffer number of the newly + created buffer. When {name} is an empty string then a new + buffer is always created. + The buffer will not have 'buflisted' set and not be loaded + yet. To add some text to the buffer use this: > + let bufnr = bufadd('someName') + call bufload(bufnr) + call setbufline(bufnr, 1, ['some', 'text']) +< Can also be used as a |method|: > + let bufnr = 'somename'->bufadd() + +bufexists({buf}) *bufexists()* + The result is a Number, which is |TRUE| if a buffer called + {buf} exists. + If the {buf} argument is a number, buffer numbers are used. + Number zero is the alternate buffer for the current window. + + If the {buf} argument is a string it must match a buffer name + exactly. The name can be: + - Relative to the current directory. + - A full path. + - The name of a buffer with 'buftype' set to "nofile". + - A URL name. + Unlisted buffers will be found. + Note that help files are listed by their short name in the + output of |:buffers|, but bufexists() requires using their + long name to be able to find them. + bufexists() may report a buffer exists, but to use the name + with a |:buffer| command you may need to use |expand()|. Esp + for MS-Windows 8.3 names in the form "c:\DOCUME~1" + Use "bufexists(0)" to test for the existence of an alternate + file name. + + Can also be used as a |method|: > + let exists = 'somename'->bufexists() + +buflisted({buf}) *buflisted()* + The result is a Number, which is |TRUE| if a buffer called + {buf} exists and is listed (has the 'buflisted' option set). + The {buf} argument is used like with |bufexists()|. + + Can also be used as a |method|: > + let listed = 'somename'->buflisted() + +bufload({buf}) *bufload()* + Ensure the buffer {buf} is loaded. When the buffer name + refers to an existing file then the file is read. Otherwise + the buffer will be empty. If the buffer was already loaded + then there is no change. + If there is an existing swap file for the file of the buffer, + there will be no dialog, the buffer will be loaded anyway. + The {buf} argument is used like with |bufexists()|. + + Can also be used as a |method|: > + eval 'somename'->bufload() + +bufloaded({buf}) *bufloaded()* + The result is a Number, which is |TRUE| if a buffer called + {buf} exists and is loaded (shown in a window or hidden). + The {buf} argument is used like with |bufexists()|. + + Can also be used as a |method|: > + let loaded = 'somename'->bufloaded() + +bufname([{buf}]) *bufname()* + The result is the name of a buffer. Mostly as it is displayed + by the `:ls` command, but not using special names such as + "[No Name]". + If {buf} is omitted the current buffer is used. + If {buf} is a Number, that buffer number's name is given. + Number zero is the alternate buffer for the current window. + If {buf} is a String, it is used as a |file-pattern| to match + with the buffer names. This is always done like 'magic' is + set and 'cpoptions' is empty. When there is more than one + match an empty string is returned. + "" or "%" can be used for the current buffer, "#" for the + alternate buffer. + A full match is preferred, otherwise a match at the start, end + or middle of the buffer name is accepted. If you only want a + full match then put "^" at the start and "$" at the end of the + pattern. + Listed buffers are found first. If there is a single match + with a listed buffer, that one is returned. Next unlisted + buffers are searched for. + If the {buf} is a String, but you want to use it as a buffer + number, force it to be a Number by adding zero to it: > + :echo bufname("3" + 0) +< Can also be used as a |method|: > + echo bufnr->bufname() + +< If the buffer doesn't exist, or doesn't have a name, an empty + string is returned. > + bufname("#") alternate buffer name + bufname(3) name of buffer 3 + bufname("%") name of current buffer + bufname("file2") name of buffer where "file2" matches. +< + *bufnr()* +bufnr([{buf} [, {create}]]) + The result is the number of a buffer, as it is displayed by + the `:ls` command. For the use of {buf}, see |bufname()| + above. + If the buffer doesn't exist, -1 is returned. Or, if the + {create} argument is present and TRUE, a new, unlisted, + buffer is created and its number is returned. + bufnr("$") is the last buffer: > + :let last_buffer = bufnr("$") +< The result is a Number, which is the highest buffer number + of existing buffers. Note that not all buffers with a smaller + number necessarily exist, because ":bwipeout" may have removed + them. Use bufexists() to test for the existence of a buffer. + + Can also be used as a |method|: > + echo bufref->bufnr() + +bufwinid({buf}) *bufwinid()* + The result is a Number, which is the |window-ID| of the first + window associated with buffer {buf}. For the use of {buf}, + see |bufname()| above. If buffer {buf} doesn't exist or + there is no such window, -1 is returned. Example: > + + echo "A window containing buffer 1 is " . (bufwinid(1)) +< + Only deals with the current tab page. + + Can also be used as a |method|: > + FindBuffer()->bufwinid() + +bufwinnr({buf}) *bufwinnr()* + Like |bufwinid()| but return the window number instead of the + |window-ID|. + If buffer {buf} doesn't exist or there is no such window, -1 + is returned. Example: > + + echo "A window containing buffer 1 is " . (bufwinnr(1)) + +< The number can be used with |CTRL-W_w| and ":wincmd w" + |:wincmd|. + + Can also be used as a |method|: > + FindBuffer()->bufwinnr() + +byte2line({byte}) *byte2line()* + Return the line number that contains the character at byte + count {byte} in the current buffer. This includes the + end-of-line character, depending on the 'fileformat' option + for the current buffer. The first character has byte count + one. + Also see |line2byte()|, |go| and |:goto|. + + Can also be used as a |method|: > + GetOffset()->byte2line() + +byteidx({expr}, {nr}) *byteidx()* + Return byte index of the {nr}'th character in the String + {expr}. Use zero for the first character, it then returns + zero. + If there are no multibyte characters the returned value is + equal to {nr}. + Composing characters are not counted separately, their byte + length is added to the preceding base character. See + |byteidxcomp()| below for counting composing characters + separately. + Example : > + echo matchstr(str, ".", byteidx(str, 3)) +< will display the fourth character. Another way to do the + same: > + let s = strpart(str, byteidx(str, 3)) + echo strpart(s, 0, byteidx(s, 1)) +< Also see |strgetchar()| and |strcharpart()|. + + If there are less than {nr} characters -1 is returned. + If there are exactly {nr} characters the length of the string + in bytes is returned. + + Can also be used as a |method|: > + GetName()->byteidx(idx) + +byteidxcomp({expr}, {nr}) *byteidxcomp()* + Like byteidx(), except that a composing character is counted + as a separate character. Example: > + let s = 'e' . nr2char(0x301) + echo byteidx(s, 1) + echo byteidxcomp(s, 1) + echo byteidxcomp(s, 2) +< The first and third echo result in 3 ('e' plus composing + character is 3 bytes), the second echo results in 1 ('e' is + one byte). + Only works differently from byteidx() when 'encoding' is set + to a Unicode encoding. + + Can also be used as a |method|: > + GetName()->byteidxcomp(idx) + +call({func}, {arglist} [, {dict}]) *call()* *E699* + Call function {func} with the items in |List| {arglist} as + arguments. + {func} can either be a |Funcref| or the name of a function. + a:firstline and a:lastline are set to the cursor line. + Returns the return value of the called function. + {dict} is for functions with the "dict" attribute. It will be + used to set the local variable "self". |Dictionary-function| + + Can also be used as a |method|: > + GetFunc()->call([arg, arg], dict) + +ceil({expr}) *ceil()* + Return the smallest integral value greater than or equal to + {expr} as a |Float| (round up). + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + echo ceil(1.456) +< 2.0 > + echo ceil(-5.456) +< -5.0 > + echo ceil(4.0) +< 4.0 + + Can also be used as a |method|: > + Compute()->ceil() + +changenr() *changenr()* + Return the number of the most recent change. This is the same + number as what is displayed with |:undolist| and can be used + with the |:undo| command. + When a change was made it is the number of that change. After + redo it is the number of the redone change. After undo it is + one less than the number of the undone change. + +chanclose({id} [, {stream}]) *chanclose()* + Close a channel or a specific stream associated with it. + For a job, {stream} can be one of "stdin", "stdout", + "stderr" or "rpc" (closes stdin/stdout for a job started + with `"rpc":v:true`) If {stream} is omitted, all streams + are closed. If the channel is a pty, this will then close the + pty master, sending SIGHUP to the job process. + For a socket, there is only one stream, and {stream} should be + ommited. + +chansend({id}, {data}) *chansend()* + Send data to channel {id}. For a job, it writes it to the + stdin of the process. For the stdio channel |channel-stdio|, + it writes to Nvim's stdout. Returns the number of bytes + written if the write succeeded, 0 otherwise. + See |channel-bytes| for more information. + + {data} may be a string, string convertible, |Blob|, or a list. + If {data} is a list, the items will be joined by newlines; any + newlines in an item will be sent as NUL. To send a final + newline, include a final empty string. Example: > + :call chansend(id, ["abc", "123\n456", ""]) +< will send "abc<NL>123<NUL>456<NL>". + + chansend() writes raw data, not RPC messages. If the channel + was created with `"rpc":v:true` then the channel expects RPC + messages, use |rpcnotify()| and |rpcrequest()| instead. + + +char2nr({string} [, {utf8}]) *char2nr()* + Return number value of the first char in {string}. + Examples: > + char2nr(" ") returns 32 + char2nr("ABC") returns 65 + char2nr("á") returns 225 + char2nr("á"[0]) returns 195 + char2nr("\<M-x>") returns 128 +< Non-ASCII characters are always treated as UTF-8 characters. + {utf8} is ignored, it exists only for backwards-compatibility. + A combining character is a separate character. + |nr2char()| does the opposite. + + Can also be used as a |method|: > + GetChar()->char2nr() +< + *charcol()* +charcol({expr}) Same as |col()| but returns the character index of the column + position given with {expr} instead of the byte position. + + Example: + With the cursor on '세' in line 5 with text "여보세요": > + charcol('.') returns 3 + col('.') returns 7 + +< Can also be used as a |method|: > + GetPos()->col() +< + *charidx()* +charidx({string}, {idx} [, {countcc}]) + Return the character index of the byte at {idx} in {string}. + The index of the first character is zero. + If there are no multibyte characters the returned value is + equal to {idx}. + When {countcc} is omitted or |FALSE|, then composing characters + are not counted separately, their byte length is + added to the preceding base character. + When {countcc} is |TRUE|, then composing characters are + counted as separate characters. + Returns -1 if the arguments are invalid or if {idx} is greater + than the index of the last byte in {string}. An error is + given if the first argument is not a string, the second + argument is not a number or when the third argument is present + and is not zero or one. + See |byteidx()| and |byteidxcomp()| for getting the byte index + from the character index. + Examples: > + echo charidx('áb́ć', 3) returns 1 + echo charidx('áb́ć', 6, 1) returns 4 + echo charidx('áb́ć', 16) returns -1 +< + Can also be used as a |method|: > + GetName()->charidx(idx) + +chdir({dir}) *chdir()* + Change the current working directory to {dir}. The scope of + the directory change depends on the directory of the current + window: + - If the current window has a window-local directory + (|:lcd|), then changes the window local directory. + - Otherwise, if the current tabpage has a local + directory (|:tcd|) then changes the tabpage local + directory. + - Otherwise, changes the global directory. + If successful, returns the previous working directory. Pass + this to another chdir() to restore the directory. + On failure, returns an empty string. + + Example: > + let save_dir = chdir(newdir) + if save_dir + " ... do some work + call chdir(save_dir) + endif +< +cindent({lnum}) *cindent()* + Get the amount of indent for line {lnum} according the C + indenting rules, as with 'cindent'. + The indent is counted in spaces, the value of 'tabstop' is + relevant. {lnum} is used just like in |getline()|. + When {lnum} is invalid -1 is returned. + See |C-indenting|. + + Can also be used as a |method|: > + GetLnum()->cindent() + +clearmatches([{win}]) *clearmatches()* + Clears all matches previously defined for the current window + by |matchadd()| and the |:match| commands. + If {win} is specified, use the window with this number or + window ID instead of the current window. + + Can also be used as a |method|: > + GetWin()->clearmatches() +< + *col()* +col({expr}) The result is a Number, which is the byte index of the column + position given with {expr}. The accepted positions are: + . the cursor position + $ the end of the cursor line (the result is the + number of bytes in the cursor line plus one) + 'x position of mark x (if the mark is not set, 0 is + returned) + v In Visual mode: the start of the Visual area (the + cursor is the end). When not in Visual mode + returns the cursor position. Differs from |'<| in + that it's updated right away. + Additionally {expr} can be [lnum, col]: a |List| with the line + and column number. Most useful when the column is "$", to get + the last column of a specific line. When "lnum" or "col" is + out of range then col() returns zero. + To get the line number use |line()|. To get both use + |getpos()|. + For the screen column position use |virtcol()|. For the + character position use |charcol()|. + Note that only marks in the current file can be used. + Examples: > + col(".") column of cursor + col("$") length of cursor line plus one + col("'t") column of mark t + col("'" . markname) column of mark markname +< The first column is 1. 0 is returned for an error. + For an uppercase mark the column may actually be in another + buffer. + For the cursor position, when 'virtualedit' is active, the + column is one higher if the cursor is after the end of the + line. This can be used to obtain the column in Insert mode: > + :imap <F2> <C-O>:let save_ve = &ve<CR> + \<C-O>:set ve=all<CR> + \<C-O>:echo col(".") . "\n" <Bar> + \let &ve = save_ve<CR> + +< Can also be used as a |method|: > + GetPos()->col() +< + +complete({startcol}, {matches}) *complete()* *E785* + Set the matches for Insert mode completion. + Can only be used in Insert mode. You need to use a mapping + with CTRL-R = (see |i_CTRL-R|). It does not work after CTRL-O + or with an expression mapping. + {startcol} is the byte offset in the line where the completed + text start. The text up to the cursor is the original text + that will be replaced by the matches. Use col('.') for an + empty string. "col('.') - 1" will replace one character by a + match. + {matches} must be a |List|. Each |List| item is one match. + See |complete-items| for the kind of items that are possible. + "longest" in 'completeopt' is ignored. + Note that the after calling this function you need to avoid + inserting anything that would cause completion to stop. + The match can be selected with CTRL-N and CTRL-P as usual with + Insert mode completion. The popup menu will appear if + specified, see |ins-completion-menu|. + Example: > + inoremap <F5> <C-R>=ListMonths()<CR> + + func! ListMonths() + call complete(col('.'), ['January', 'February', 'March', + \ 'April', 'May', 'June', 'July', 'August', 'September', + \ 'October', 'November', 'December']) + return '' + endfunc +< This isn't very useful, but it shows how it works. Note that + an empty string is returned to avoid a zero being inserted. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetMatches()->complete(col('.')) + +complete_add({expr}) *complete_add()* + Add {expr} to the list of matches. Only to be used by the + function specified with the 'completefunc' option. + Returns 0 for failure (empty string or out of memory), + 1 when the match was added, 2 when the match was already in + the list. + See |complete-functions| for an explanation of {expr}. It is + the same as one item in the list that 'omnifunc' would return. + + Can also be used as a |method|: > + GetMoreMatches()->complete_add() + +complete_check() *complete_check()* + Check for a key typed while looking for completion matches. + This is to be used when looking for matches takes some time. + Returns |TRUE| when searching for matches is to be aborted, + zero otherwise. + Only to be used by the function specified with the + 'completefunc' option. + + +complete_info([{what}]) *complete_info()* + Returns a |Dictionary| with information about Insert mode + completion. See |ins-completion|. + The items are: + mode Current completion mode name string. + See |complete_info_mode| for the values. + pum_visible |TRUE| if popup menu is visible. + See |pumvisible()|. + items List of completion matches. Each item is a + dictionary containing the entries "word", + "abbr", "menu", "kind", "info" and "user_data". + See |complete-items|. + selected Selected item index. First index is zero. + Index is -1 if no item is selected (showing + typed text only, or the last completion after + no item is selected when using the <Up> or + <Down> keys) + inserted Inserted string. [NOT IMPLEMENT YET] + + *complete_info_mode* + mode values are: + "" Not in completion mode + "keyword" Keyword completion |i_CTRL-X_CTRL-N| + "ctrl_x" Just pressed CTRL-X |i_CTRL-X| + "scroll" Scrolling with |i_CTRL-X_CTRL-E| or + |i_CTRL-X_CTRL-Y| + "whole_line" Whole lines |i_CTRL-X_CTRL-L| + "files" File names |i_CTRL-X_CTRL-F| + "tags" Tags |i_CTRL-X_CTRL-]| + "path_defines" Definition completion |i_CTRL-X_CTRL-D| + "path_patterns" Include completion |i_CTRL-X_CTRL-I| + "dictionary" Dictionary |i_CTRL-X_CTRL-K| + "thesaurus" Thesaurus |i_CTRL-X_CTRL-T| + "cmdline" Vim Command line |i_CTRL-X_CTRL-V| + "function" User defined completion |i_CTRL-X_CTRL-U| + "omni" Omni completion |i_CTRL-X_CTRL-O| + "spell" Spelling suggestions |i_CTRL-X_s| + "eval" |complete()| completion + "unknown" Other internal modes + + If the optional {what} list argument is supplied, then only + the items listed in {what} are returned. Unsupported items in + {what} are silently ignored. + + To get the position and size of the popup menu, see + |pum_getpos()|. It's also available in |v:event| during the + |CompleteChanged| event. + + Examples: > + " Get all items + call complete_info() + " Get only 'mode' + call complete_info(['mode']) + " Get only 'mode' and 'pum_visible' + call complete_info(['mode', 'pum_visible']) + +< Can also be used as a |method|: > + GetItems()->complete_info() +< + *confirm()* +confirm({msg} [, {choices} [, {default} [, {type}]]]) + Confirm() offers the user a dialog, from which a choice can be + made. It returns the number of the choice. For the first + choice this is 1. + + {msg} is displayed in a dialog with {choices} as the + alternatives. When {choices} is missing or empty, "&OK" is + used (and translated). + {msg} is a String, use '\n' to include a newline. Only on + some systems the string is wrapped when it doesn't fit. + + {choices} is a String, with the individual choices separated + by '\n', e.g. > + confirm("Save changes?", "&Yes\n&No\n&Cancel") +< The letter after the '&' is the shortcut key for that choice. + Thus you can type 'c' to select "Cancel". The shortcut does + not need to be the first letter: > + confirm("file has been modified", "&Save\nSave &All") +< For the console, the first letter of each choice is used as + the default shortcut key. Case is ignored. + + The optional {type} String argument gives the type of dialog. + It can be one of these values: "Error", "Question", "Info", + "Warning" or "Generic". Only the first character is relevant. + When {type} is omitted, "Generic" is used. + + The optional {type} argument gives the type of dialog. This + is only used for the icon of the Win32 GUI. It can be one of + these values: "Error", "Question", "Info", "Warning" or + "Generic". Only the first character is relevant. + When {type} is omitted, "Generic" is used. + + If the user aborts the dialog by pressing <Esc>, CTRL-C, + or another valid interrupt key, confirm() returns 0. + + An example: > + :let choice = confirm("What do you want?", "&Apples\n&Oranges\n&Bananas", 2) + :if choice == 0 + : echo "make up your mind!" + :elseif choice == 3 + : echo "tasteful" + :else + : echo "I prefer bananas myself." + :endif +< In a GUI dialog, buttons are used. The layout of the buttons + depends on the 'v' flag in 'guioptions'. If it is included, + the buttons are always put vertically. Otherwise, confirm() + tries to put the buttons in one horizontal line. If they + don't fit, a vertical layout is used anyway. For some systems + the horizontal layout is always used. + + Can also be used as a |method|in: > + BuildMessage()->confirm("&Yes\n&No") +< + *copy()* +copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't + different from using {expr} directly. + When {expr} is a |List| a shallow copy is created. This means + that the original |List| can be changed without changing the + copy, and vice versa. But the items are identical, thus + changing an item changes the contents of both |Lists|. + A |Dictionary| is copied in a similar way as a |List|. + Also see |deepcopy()|. + Can also be used as a |method|: > + mylist->copy() + +cos({expr}) *cos()* + Return the cosine of {expr}, measured in radians, as a |Float|. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo cos(100) +< 0.862319 > + :echo cos(-4.01) +< -0.646043 + + Can also be used as a |method|: > + Compute()->cos() + +cosh({expr}) *cosh()* + Return the hyperbolic cosine of {expr} as a |Float| in the range + [1, inf]. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo cosh(0.5) +< 1.127626 > + :echo cosh(-0.5) +< -1.127626 + + Can also be used as a |method|: > + Compute()->cosh() + +count({comp}, {expr} [, {ic} [, {start}]]) *count()* + Return the number of times an item with value {expr} appears + in |String|, |List| or |Dictionary| {comp}. + + If {start} is given then start with the item with this index. + {start} can only be used with a |List|. + + When {ic} is given and it's |TRUE| then case is ignored. + + When {comp} is a string then the number of not overlapping + occurrences of {expr} is returned. Zero is returned when + {expr} is an empty string. + + Can also be used as a |method|: > + mylist->count(val) +< + *cscope_connection()* +cscope_connection([{num} , {dbpath} [, {prepend}]]) + Checks for the existence of a |cscope| connection. If no + parameters are specified, then the function returns: + 0, if there are no cscope connections; + 1, if there is at least one cscope connection. + + If parameters are specified, then the value of {num} + determines how existence of a cscope connection is checked: + + {num} Description of existence check + ----- ------------------------------ + 0 Same as no parameters (e.g., "cscope_connection()"). + 1 Ignore {prepend}, and use partial string matches for + {dbpath}. + 2 Ignore {prepend}, and use exact string matches for + {dbpath}. + 3 Use {prepend}, use partial string matches for both + {dbpath} and {prepend}. + 4 Use {prepend}, use exact string matches for both + {dbpath} and {prepend}. + + Note: All string comparisons are case sensitive! + + Examples. Suppose we had the following (from ":cs show"): > + + # pid database name prepend path + 0 27664 cscope.out /usr/local +< + Invocation Return Val ~ + ---------- ---------- > + cscope_connection() 1 + cscope_connection(1, "out") 1 + cscope_connection(2, "out") 0 + cscope_connection(3, "out") 0 + cscope_connection(3, "out", "local") 1 + cscope_connection(4, "out") 0 + cscope_connection(4, "out", "local") 0 + cscope_connection(4, "cscope.out", "/usr/local") 1 +< + +ctxget([{index}]) *ctxget()* + Returns a |Dictionary| representing the |context| at {index} + from the top of the |context-stack| (see |context-dict|). + If {index} is not given, it is assumed to be 0 (i.e.: top). + +ctxpop() *ctxpop()* + Pops and restores the |context| at the top of the + |context-stack|. + +ctxpush([{types}]) *ctxpush()* + Pushes the current editor state (|context|) on the + |context-stack|. + If {types} is given and is a |List| of |String|s, it specifies + which |context-types| to include in the pushed context. + Otherwise, all context types are included. + +ctxset({context} [, {index}]) *ctxset()* + Sets the |context| at {index} from the top of the + |context-stack| to that represented by {context}. + {context} is a Dictionary with context data (|context-dict|). + If {index} is not given, it is assumed to be 0 (i.e.: top). + +ctxsize() *ctxsize()* + Returns the size of the |context-stack|. + +cursor({lnum}, {col} [, {off}]) *cursor()* +cursor({list}) + Positions the cursor at the column (byte count) {col} in the + line {lnum}. The first column is one. + + When there is one argument {list} this is used as a |List| + with two, three or four item: + [{lnum}, {col}] + [{lnum}, {col}, {off}] + [{lnum}, {col}, {off}, {curswant}] + This is like the return value of |getpos()| or |getcurpos()|, + but without the first item. + + To position the cursor using the character count, use + |setcursorcharpos()|. + + Does not change the jumplist. + If {lnum} is greater than the number of lines in the buffer, + the cursor will be positioned at the last line in the buffer. + If {lnum} is zero, the cursor will stay in the current line. + If {col} is greater than the number of bytes in the line, + the cursor will be positioned at the last character in the + line. + If {col} is zero, the cursor will stay in the current column. + If {curswant} is given it is used to set the preferred column + for vertical movement. Otherwise {col} is used. + + When 'virtualedit' is used {off} specifies the offset in + screen columns from the start of the character. E.g., a + position within a <Tab> or after the last character. + Returns 0 when the position could be set, -1 otherwise. + + Can also be used as a |method|: > + GetCursorPos()->cursor() + +deepcopy({expr} [, {noref}]) *deepcopy()* *E698* + Make a copy of {expr}. For Numbers and Strings this isn't + different from using {expr} directly. + When {expr} is a |List| a full copy is created. This means + that the original |List| can be changed without changing the + copy, and vice versa. When an item is a |List|, a copy for it + is made, recursively. Thus changing an item in the copy does + not change the contents of the original |List|. + + When {noref} is omitted or zero a contained |List| or + |Dictionary| is only copied once. All references point to + this single copy. With {noref} set to 1 every occurrence of a + |List| or |Dictionary| results in a new copy. This also means + that a cyclic reference causes deepcopy() to fail. + *E724* + Nesting is possible up to 100 levels. When there is an item + that refers back to a higher level making a deep copy with + {noref} set to 1 will fail. + Also see |copy()|. + + Can also be used as a |method|: > + GetObject()->deepcopy() + +delete({fname} [, {flags}]) *delete()* + Without {flags} or with {flags} empty: Deletes the file by the + name {fname}. This also works when {fname} is a symbolic link. + A symbolic link itself is deleted, not what it points to. + + When {flags} is "d": Deletes the directory by the name + {fname}. This fails when directory {fname} is not empty. + + When {flags} is "rf": Deletes the directory by the name + {fname} and everything in it, recursively. BE CAREFUL! + Note: on MS-Windows it is not possible to delete a directory + that is being used. + + The result is a Number, which is 0/false if the delete + operation was successful and -1/true when the deletion failed + or partly failed. + + Can also be used as a |method|: > + GetName()->delete() + +deletebufline({buf}, {first} [, {last}]) *deletebufline()* + Delete lines {first} to {last} (inclusive) from buffer {buf}. + If {last} is omitted then delete line {first} only. + On success 0 is returned, on failure 1 is returned. + + This function works only for loaded buffers. First call + |bufload()| if needed. + + For the use of {buf}, see |bufname()| above. + + {first} and {last} are used like with |getline()|. Note that + when using |line()| this refers to the current buffer. Use "$" + to refer to the last line in buffer {buf}. + + Can also be used as a |method|: > + GetBuffer()->deletebufline(1) +< +dictwatcheradd({dict}, {pattern}, {callback}) *dictwatcheradd()* + Adds a watcher to a dictionary. A dictionary watcher is + identified by three components: + + - A dictionary({dict}); + - A key pattern({pattern}). + - A function({callback}). + + After this is called, every change on {dict} and on keys + matching {pattern} will result in {callback} being invoked. + + For example, to watch all global variables: > + silent! call dictwatcherdel(g:, '*', 'OnDictChanged') + function! OnDictChanged(d,k,z) + echomsg string(a:k) string(a:z) + endfunction + call dictwatcheradd(g:, '*', 'OnDictChanged') +< + For now {pattern} only accepts very simple patterns that can + contain a '*' at the end of the string, in which case it will + match every key that begins with the substring before the '*'. + That means if '*' is not the last character of {pattern}, only + keys that are exactly equal as {pattern} will be matched. + + The {callback} receives three arguments: + + - The dictionary being watched. + - The key which changed. + - A dictionary containing the new and old values for the key. + + The type of change can be determined by examining the keys + present on the third argument: + + - If contains both `old` and `new`, the key was updated. + - If it contains only `new`, the key was added. + - If it contains only `old`, the key was deleted. + + This function can be used by plugins to implement options with + validation and parsing logic. + +dictwatcherdel({dict}, {pattern}, {callback}) *dictwatcherdel()* + Removes a watcher added with |dictwatcheradd()|. All three + arguments must match the ones passed to |dictwatcheradd()| in + order for the watcher to be successfully deleted. + + *did_filetype()* +did_filetype() Returns |TRUE| when autocommands are being executed and the + FileType event has been triggered at least once. Can be used + to avoid triggering the FileType event again in the scripts + that detect the file type. |FileType| + Returns |FALSE| when `:setf FALLBACK` was used. + When editing another file, the counter is reset, thus this + really checks if the FileType event has been triggered for the + current buffer. This allows an autocommand that starts + editing another buffer to set 'filetype' and load a syntax + file. + +diff_filler({lnum}) *diff_filler()* + Returns the number of filler lines above line {lnum}. + These are the lines that were inserted at this point in + another diff'ed window. These filler lines are shown in the + display but don't exist in the buffer. + {lnum} is used like with |getline()|. Thus "." is the current + line, "'m" mark m, etc. + Returns 0 if the current window is not in diff mode. + + Can also be used as a |method|: > + GetLnum()->diff_filler() + +diff_hlID({lnum}, {col}) *diff_hlID()* + Returns the highlight ID for diff mode at line {lnum} column + {col} (byte index). When the current line does not have a + diff change zero is returned. + {lnum} is used like with |getline()|. Thus "." is the current + line, "'m" mark m, etc. + {col} is 1 for the leftmost column, {lnum} is 1 for the first + line. + The highlight ID can be used with |synIDattr()| to obtain + syntax information about the highlighting. + + Can also be used as a |method|: > + GetLnum()->diff_hlID(col) + +empty({expr}) *empty()* + Return the Number 1 if {expr} is empty, zero otherwise. + - A |List| or |Dictionary| is empty when it does not have any + items. + - A |String| is empty when its length is zero. + - A |Number| and |Float| are empty when their value is zero. + - |v:false| and |v:null| are empty, |v:true| is not. + - A |Blob| is empty when its length is zero. + + Can also be used as a |method|: > + mylist->empty() + +environ() *environ()* + Return all of environment variables as dictionary. You can + check if an environment variable exists like this: > + :echo has_key(environ(), 'HOME') +< Note that the variable name may be CamelCase; to ignore case + use this: > + :echo index(keys(environ()), 'HOME', 0, 1) != -1 + +escape({string}, {chars}) *escape()* + Escape the characters in {chars} that occur in {string} with a + backslash. Example: > + :echo escape('c:\program files\vim', ' \') +< results in: > + c:\\program\ files\\vim +< Also see |shellescape()| and |fnameescape()|. + + Can also be used as a |method|: > + GetText()->escape(' \') +< + *eval()* +eval({string}) Evaluate {string} and return the result. Especially useful to + turn the result of |string()| back into the original value. + This works for Numbers, Floats, Strings, Blobs and composites + of them. Also works for |Funcref|s that refer to existing + functions. + + Can also be used as a |method|: > + argv->join()->eval() + +eventhandler() *eventhandler()* + Returns 1 when inside an event handler. That is that Vim got + interrupted while waiting for the user to type a character, + e.g., when dropping a file on Vim. This means interactive + commands cannot be used. Otherwise zero is returned. + +executable({expr}) *executable()* + This function checks if an executable with the name {expr} + exists. {expr} must be the name of the program without any + arguments. + executable() uses the value of $PATH and/or the normal + searchpath for programs. *PATHEXT* + On MS-Windows the ".exe", ".bat", etc. can optionally be + included. Then the extensions in $PATHEXT are tried. Thus if + "foo.exe" does not exist, "foo.exe.bat" can be found. If + $PATHEXT is not set then ".exe;.com;.bat;.cmd" is used. A dot + by itself can be used in $PATHEXT to try using the name + without an extension. When 'shell' looks like a Unix shell, + then the name is also tried without adding an extension. + On MS-Windows it only checks if the file exists and is not a + directory, not if it's really executable. + On Windows an executable in the same directory as Vim is + always found (it is added to $PATH at |startup|). + The result is a Number: + 1 exists + 0 does not exist + -1 not implemented on this system + |exepath()| can be used to get the full path of an executable. + + Can also be used as a |method|: > + GetCommand()->executable() + +execute({command} [, {silent}]) *execute()* + Execute {command} and capture its output. + If {command} is a |String|, returns {command} output. + If {command} is a |List|, returns concatenated outputs. + Examples: > + echo execute('echon "foo"') +< foo > + echo execute(['echon "foo"', 'echon "bar"']) +< foobar + + The optional {silent} argument can have these values: + "" no `:silent` used + "silent" `:silent` used + "silent!" `:silent!` used + The default is "silent". Note that with "silent!", unlike + `:redir`, error messages are dropped. + + To get a list of lines use |split()| on the result: > + split(execute('args'), "\n") + +< This function is not available in the |sandbox|. + Note: If nested, an outer execute() will not observe output of + the inner calls. + Note: Text attributes (highlights) are not captured. + To execute a command in another window than the current one + use `win_execute()`. + + Can also be used as a |method|: > + GetCommand()->execute() + +exepath({expr}) *exepath()* + Returns the full path of {expr} if it is an executable and + given as a (partial or full) path or is found in $PATH. + Returns empty string otherwise. + If {expr} starts with "./" the |current-directory| is used. + + Can also be used as a |method|: > + GetCommand()->exepath() +< + *exists()* +exists({expr}) The result is a Number, which is |TRUE| if {expr} is + defined, zero otherwise. + + For checking for a supported feature use |has()|. + For checking if a file exists use |filereadable()|. + + The {expr} argument is a string, which contains one of these: + varname internal variable (see + dict.key |internal-variables|). Also works + list[i] for |curly-braces-names|, |Dictionary| + entries, |List| items, etc. + Beware that evaluating an index may + cause an error message for an invalid + expression. E.g.: > + :let l = [1, 2, 3] + :echo exists("l[5]") +< 0 > + :echo exists("l[xx]") +< E121: Undefined variable: xx + 0 + &option-name Vim option (only checks if it exists, + not if it really works) + +option-name Vim option that works. + $ENVNAME environment variable (could also be + done by comparing with an empty + string) + *funcname built-in function (see |functions|) + or user defined function (see + |user-function|). Also works for a + variable that is a Funcref. + :cmdname Ex command: built-in command, user + command or command modifier |:command|. + Returns: + 1 for match with start of a command + 2 full match with a command + 3 matches several user commands + To check for a supported command + always check the return value to be 2. + :2match The |:2match| command. + :3match The |:3match| command. + #event autocommand defined for this event + #event#pattern autocommand defined for this event and + pattern (the pattern is taken + literally and compared to the + autocommand patterns character by + character) + #group autocommand group exists + #group#event autocommand defined for this group and + event. + #group#event#pattern + autocommand defined for this group, + event and pattern. + ##event autocommand for this event is + supported. + + Examples: > + exists("&mouse") + exists("$HOSTNAME") + exists("*strftime") + exists("*s:MyFunc") + exists("bufcount") + exists(":Make") + exists("#CursorHold") + exists("#BufReadPre#*.gz") + exists("#filetypeindent") + exists("#filetypeindent#FileType") + exists("#filetypeindent#FileType#*") + exists("##ColorScheme") +< There must be no space between the symbol (&/$/*/#) and the + name. + There must be no extra characters after the name, although in + a few cases this is ignored. That may become more strict in + the future, thus don't count on it! + Working example: > + exists(":make") +< NOT working example: > + exists(":make install") + +< Note that the argument must be a string, not the name of the + variable itself. For example: > + exists(bufcount) +< This doesn't check for existence of the "bufcount" variable, + but gets the value of "bufcount", and checks if that exists. + + Can also be used as a |method|: > + Varname()->exists() + +exp({expr}) *exp()* + Return the exponential of {expr} as a |Float| in the range + [0, inf]. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo exp(2) +< 7.389056 > + :echo exp(-1) +< 0.367879 + + Can also be used as a |method|: > + Compute()->exp() + +debugbreak({pid}) *debugbreak()* + Specifically used to interrupt a program being debugged. It + will cause process {pid} to get a SIGTRAP. Behavior for other + processes is undefined. See |terminal-debugger|. + {Sends a SIGINT to a process {pid} other than MS-Windows} + + Can also be used as a |method|: > + GetPid()->debugbreak() + +expand({string} [, {nosuf} [, {list}]]) *expand()* + Expand wildcards and the following special keywords in + {string}. 'wildignorecase' applies. + + If {list} is given and it is |TRUE|, a List will be returned. + Otherwise the result is a String and when there are several + matches, they are separated by <NL> characters. + + If the expansion fails, the result is an empty string. A name + for a non-existing file is not included, unless {string} does + not start with '%', '#' or '<', see below. + + When {string} starts with '%', '#' or '<', the expansion is + done like for the |cmdline-special| variables with their + associated modifiers. Here is a short overview: + + % current file name + # alternate file name + #n alternate file name n + <cfile> file name under the cursor + <afile> autocmd file name + <abuf> autocmd buffer number (as a String!) + <amatch> autocmd matched name + <sfile> sourced script file or function name + <slnum> sourced script line number or function + line number + <sflnum> script file line number, also when in + a function + <SID> "<SNR>123_" where "123" is the + current script ID |<SID>| + <cword> word under the cursor + <cWORD> WORD under the cursor + <client> the {clientid} of the last received + message |server2client()| + Modifiers: + :p expand to full path + :h head (last path component removed) + :t tail (last path component only) + :r root (one extension removed) + :e extension only + + Example: > + :let &tags = expand("%:p:h") . "/tags" +< Note that when expanding a string that starts with '%', '#' or + '<', any following text is ignored. This does NOT work: > + :let doesntwork = expand("%:h.bak") +< Use this: > + :let doeswork = expand("%:h") . ".bak" +< Also note that expanding "<cfile>" and others only returns the + referenced file name without further expansion. If "<cfile>" + is "~/.cshrc", you need to do another expand() to have the + "~/" expanded into the path of the home directory: > + :echo expand(expand("<cfile>")) +< + There cannot be white space between the variables and the + following modifier. The |fnamemodify()| function can be used + to modify normal file names. + + When using '%' or '#', and the current or alternate file name + is not defined, an empty string is used. Using "%:p" in a + buffer with no name, results in the current directory, with a + '/' added. + + When {string} does not start with '%', '#' or '<', it is + expanded like a file name is expanded on the command line. + 'suffixes' and 'wildignore' are used, unless the optional + {nosuf} argument is given and it is |TRUE|. + Names for non-existing files are included. The "**" item can + be used to search in a directory tree. For example, to find + all "README" files in the current directory and below: > + :echo expand("**/README") +< + expand() can also be used to expand variables and environment + variables that are only known in a shell. But this can be + slow, because a shell may be used to do the expansion. See + |expr-env-expand|. + The expanded variable is still handled like a list of file + names. When an environment variable cannot be expanded, it is + left unchanged. Thus ":echo expand('$FOOBAR')" results in + "$FOOBAR". + + See |glob()| for finding existing files. See |system()| for + getting the raw output of an external command. + + Can also be used as a |method|: > + Getpattern()->expand() + +expandcmd({string}) *expandcmd()* + Expand special items in String {string} like what is done for + an Ex command such as `:edit`. This expands special keywords, + like with |expand()|, and environment variables, anywhere in + {string}. "~user" and "~/path" are only expanded at the + start. + Returns the expanded string. Example: > + :echo expandcmd('make %<.o') + +< Can also be used as a |method|: > + GetCommand()->expandcmd() +< +extend({expr1}, {expr2} [, {expr3}]) *extend()* + {expr1} and {expr2} must be both |Lists| or both + |Dictionaries|. + + If they are |Lists|: Append {expr2} to {expr1}. + If {expr3} is given insert the items of {expr2} before the + item with index {expr3} in {expr1}. When {expr3} is zero + insert before the first item. When {expr3} is equal to + len({expr1}) then {expr2} is appended. + Examples: > + :echo sort(extend(mylist, [7, 5])) + :call extend(mylist, [2, 3], 1) +< When {expr1} is the same List as {expr2} then the number of + items copied is equal to the original length of the List. + E.g., when {expr3} is 1 you get N new copies of the first item + (where N is the original length of the List). + Use |add()| to concatenate one item to a list. To concatenate + two lists into a new list use the + operator: > + :let newlist = [1, 2, 3] + [4, 5] +< + If they are |Dictionaries|: + Add all entries from {expr2} to {expr1}. + If a key exists in both {expr1} and {expr2} then {expr3} is + used to decide what to do: + {expr3} = "keep": keep the value of {expr1} + {expr3} = "force": use the value of {expr2} + {expr3} = "error": give an error message *E737* + When {expr3} is omitted then "force" is assumed. + + {expr1} is changed when {expr2} is not empty. If necessary + make a copy of {expr1} first. + {expr2} remains unchanged. + When {expr1} is locked and {expr2} is not empty the operation + fails. + Returns {expr1}. + + Can also be used as a |method|: > + mylist->extend(otherlist) + +feedkeys({string} [, {mode}]) *feedkeys()* + Characters in {string} are queued for processing as if they + come from a mapping or were typed by the user. + + By default the string is added to the end of the typeahead + buffer, thus if a mapping is still being executed the + characters come after them. Use the 'i' flag to insert before + other characters, they will be executed next, before any + characters from a mapping. + + The function does not wait for processing of keys contained in + {string}. + + To include special keys into {string}, use double-quotes + and "\..." notation |expr-quote|. For example, + feedkeys("\<CR>") simulates pressing of the <Enter> key. But + feedkeys('\<CR>') pushes 5 characters. + The |<Ignore>| keycode may be used to exit the + wait-for-character without doing anything. + + {mode} is a String, which can contain these character flags: + 'm' Remap keys. This is default. If {mode} is absent, + keys are remapped. + 'n' Do not remap keys. + 't' Handle keys as if typed; otherwise they are handled as + if coming from a mapping. This matters for undo, + opening folds, etc. + 'i' Insert the string instead of appending (see above). + 'x' Execute commands until typeahead is empty. This is + similar to using ":normal!". You can call feedkeys() + several times without 'x' and then one time with 'x' + (possibly with an empty {string}) to execute all the + typeahead. Note that when Vim ends in Insert mode it + will behave as if <Esc> is typed, to avoid getting + stuck, waiting for a character to be typed before the + script continues. + Note that if you manage to call feedkeys() while + executing commands, thus calling it recursively, then + all typehead will be consumed by the last call. + '!' When used with 'x' will not end Insert mode. Can be + used in a test when a timer is set to exit Insert mode + a little later. Useful for testing CursorHoldI. + + Return value is always 0. + + Can also be used as a |method|: > + GetInput()->feedkeys() + +filereadable({file}) *filereadable()* + The result is a Number, which is |TRUE| when a file with the + name {file} exists, and can be read. If {file} doesn't exist, + or is a directory, the result is |FALSE|. {file} is any + expression, which is used as a String. + If you don't care about the file being readable you can use + |glob()|. + {file} is used as-is, you may want to expand wildcards first: > + echo filereadable('~/.vimrc') + 0 + echo filereadable(expand('~/.vimrc')) + 1 + +< Can also be used as a |method|: > + GetName()->filereadable() + +filewritable({file}) *filewritable()* + The result is a Number, which is 1 when a file with the + name {file} exists, and can be written. If {file} doesn't + exist, or is not writable, the result is 0. If {file} is a + directory, and we can write to it, the result is 2. + + Can also be used as a |method|: > + GetName()->filewriteable() + +filter({expr1}, {expr2}) *filter()* + {expr1} must be a |List|, |Blob|, or a |Dictionary|. + For each item in {expr1} evaluate {expr2} and when the result + is zero remove the item from the |List| or |Dictionary|. For a + |Blob| each byte is removed. + + {expr2} must be a |string| or |Funcref|. + + If {expr2} is a |string|, inside {expr2} |v:val| has the value + of the current item. For a |Dictionary| |v:key| has the key + of the current item and for a |List| |v:key| has the index of + the current item. For a |Blob| |v:key| has the index of the + current byte. + + Examples: > + call filter(mylist, 'v:val !~ "OLD"') +< Removes the items where "OLD" appears. > + call filter(mydict, 'v:key >= 8') +< Removes the items with a key below 8. > + call filter(var, 0) +< Removes all the items, thus clears the |List| or |Dictionary|. + + Note that {expr2} is the result of expression and is then + used as an expression again. Often it is good to use a + |literal-string| to avoid having to double backslashes. + + If {expr2} is a |Funcref| it must take two arguments: + 1. the key or the index of the current item. + 2. the value of the current item. + The function must return |TRUE| if the item should be kept. + Example that keeps the odd items of a list: > + func Odd(idx, val) + return a:idx % 2 == 1 + endfunc + call filter(mylist, function('Odd')) +< It is shorter when using a |lambda|: > + call filter(myList, {idx, val -> idx * val <= 42}) +< If you do not use "val" you can leave it out: > + call filter(myList, {idx -> idx % 2 == 1}) +< + The operation is done in-place. If you want a |List| or + |Dictionary| to remain unmodified make a copy first: > + :let l = filter(copy(mylist), 'v:val =~ "KEEP"') + +< Returns {expr1}, the |List|, |Blob| or |Dictionary| that was + filtered. When an error is encountered while evaluating + {expr2} no further items in {expr1} are processed. When + {expr2} is a Funcref errors inside a function are ignored, + unless it was defined with the "abort" flag. + + Can also be used as a |method|: > + mylist->filter(expr2) + +finddir({name} [, {path} [, {count}]]) *finddir()* + Find directory {name} in {path}. Supports both downwards and + upwards recursive directory searches. See |file-searching| + for the syntax of {path}. + + Returns the path of the first found match. When the found + directory is below the current directory a relative path is + returned. Otherwise a full path is returned. + If {path} is omitted or empty then 'path' is used. + + If the optional {count} is given, find {count}'s occurrence of + {name} in {path} instead of the first one. + When {count} is negative return all the matches in a |List|. + + This is quite similar to the ex-command `:find`. + + Can also be used as a |method|: > + GetName()->finddir() + +findfile({name} [, {path} [, {count}]]) *findfile()* + Just like |finddir()|, but find a file instead of a directory. + Uses 'suffixesadd'. + Example: > + :echo findfile("tags.vim", ".;") +< Searches from the directory of the current file upwards until + it finds the file "tags.vim". + + Can also be used as a |method|: > + GetName()->findfile() + +flatten({list} [, {maxdepth}]) *flatten()* + Flatten {list} up to {maxdepth} levels. Without {maxdepth} + the result is a |List| without nesting, as if {maxdepth} is + a very large number. + The {list} is changed in place, make a copy first if you do + not want that. + *E900* + {maxdepth} means how deep in nested lists changes are made. + {list} is not modified when {maxdepth} is 0. + {maxdepth} must be positive number. + + If there is an error the number zero is returned. + + Example: > + :echo flatten([1, [2, [3, 4]], 5]) +< [1, 2, 3, 4, 5] > + :echo flatten([1, [2, [3, 4]], 5], 1) +< [1, 2, [3, 4], 5] + + Can also be used as a |method|: > + mylist->flatten() +< +float2nr({expr}) *float2nr()* + Convert {expr} to a Number by omitting the part after the + decimal point. + {expr} must evaluate to a |Float| or a Number. + When the value of {expr} is out of range for a |Number| the + result is truncated to 0x7fffffff or -0x7fffffff (or when + 64-bit Number support is enabled, 0x7fffffffffffffff or + -0x7fffffffffffffff). NaN results in -0x80000000 (or when + 64-bit Number support is enabled, -0x8000000000000000). + Examples: > + echo float2nr(3.95) +< 3 > + echo float2nr(-23.45) +< -23 > + echo float2nr(1.0e100) +< 2147483647 (or 9223372036854775807) > + echo float2nr(-1.0e150) +< -2147483647 (or -9223372036854775807) > + echo float2nr(1.0e-100) +< 0 + + Can also be used as a |method|: > + Compute()->float2nr() + +floor({expr}) *floor()* + Return the largest integral value less than or equal to + {expr} as a |Float| (round down). + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + echo floor(1.856) +< 1.0 > + echo floor(-5.456) +< -6.0 > + echo floor(4.0) +< 4.0 + + Can also be used as a |method|: > + Compute()->floor() + +fmod({expr1}, {expr2}) *fmod()* + Return the remainder of {expr1} / {expr2}, even if the + division is not representable. Returns {expr1} - i * {expr2} + for some integer i such that if {expr2} is non-zero, the + result has the same sign as {expr1} and magnitude less than + the magnitude of {expr2}. If {expr2} is zero, the value + returned is zero. The value returned is a |Float|. + {expr1} and {expr2} must evaluate to a |Float| or a |Number|. + Examples: > + :echo fmod(12.33, 1.22) +< 0.13 > + :echo fmod(-12.33, 1.22) +< -0.13 + + Can also be used as a |method|: > + Compute()->fmod(1.22) + +fnameescape({string}) *fnameescape()* + Escape {string} for use as file name command argument. All + characters that have a special meaning, such as '%' and '|' + are escaped with a backslash. + For most systems the characters escaped are + " \t\n*?[{`$\\%#'\"|!<". For systems where a backslash + appears in a filename, it depends on the value of 'isfname'. + A leading '+' and '>' is also escaped (special after |:edit| + and |:write|). And a "-" by itself (special after |:cd|). + Example: > + :let fname = '+some str%nge|name' + :exe "edit " . fnameescape(fname) +< results in executing: > + edit \+some\ str\%nge\|name +< + Can also be used as a |method|: > + GetName()->fnameescape() + +fnamemodify({fname}, {mods}) *fnamemodify()* + Modify file name {fname} according to {mods}. {mods} is a + string of characters like it is used for file names on the + command line. See |filename-modifiers|. + Example: > + :echo fnamemodify("main.c", ":p:h") +< results in: > + /home/mool/vim/vim/src +< If {mods} is empty then {fname} is returned. + Note: Environment variables don't work in {fname}, use + |expand()| first then. + + Can also be used as a |method|: > + GetName()->fnamemodify(':p:h') + +foldclosed({lnum}) *foldclosed()* + The result is a Number. If the line {lnum} is in a closed + fold, the result is the number of the first line in that fold. + If the line {lnum} is not in a closed fold, -1 is returned. + {lnum} is used like with |getline()|. Thus "." is the current + line, "'m" mark m, etc. + + Can also be used as a |method|: > + GetLnum()->foldclosed() + +foldclosedend({lnum}) *foldclosedend()* + The result is a Number. If the line {lnum} is in a closed + fold, the result is the number of the last line in that fold. + If the line {lnum} is not in a closed fold, -1 is returned. + {lnum} is used like with |getline()|. Thus "." is the current + line, "'m" mark m, etc. + + Can also be used as a |method|: > + GetLnum()->foldclosedend() + +foldlevel({lnum}) *foldlevel()* + The result is a Number, which is the foldlevel of line {lnum} + in the current buffer. For nested folds the deepest level is + returned. If there is no fold at line {lnum}, zero is + returned. It doesn't matter if the folds are open or closed. + When used while updating folds (from 'foldexpr') -1 is + returned for lines where folds are still to be updated and the + foldlevel is unknown. As a special case the level of the + previous line is usually available. + {lnum} is used like with |getline()|. Thus "." is the current + line, "'m" mark m, etc. + + Can also be used as a |method|: > + GetLnum()->foldlevel() +< + *foldtext()* +foldtext() Returns a String, to be displayed for a closed fold. This is + the default function used for the 'foldtext' option and should + only be called from evaluating 'foldtext'. It uses the + |v:foldstart|, |v:foldend| and |v:folddashes| variables. + The returned string looks like this: > + +-- 45 lines: abcdef +< The number of leading dashes depends on the foldlevel. The + "45" is the number of lines in the fold. "abcdef" is the text + in the first non-blank line of the fold. Leading white space, + "//" or "/*" and the text from the 'foldmarker' and + 'commentstring' options is removed. + When used to draw the actual foldtext, the rest of the line + will be filled with the fold char from the 'fillchars' + setting. + +foldtextresult({lnum}) *foldtextresult()* + Returns the text that is displayed for the closed fold at line + {lnum}. Evaluates 'foldtext' in the appropriate context. + When there is no closed fold at {lnum} an empty string is + returned. + {lnum} is used like with |getline()|. Thus "." is the current + line, "'m" mark m, etc. + Useful when exporting folded text, e.g., to HTML. + + Can also be used as a |method|: > + GetLnum()->foldtextresult() +< + *foreground()* +foreground() Move the Vim window to the foreground. Useful when sent from + a client to a Vim server. |remote_send()| + On Win32 systems this might not work, the OS does not always + allow a window to bring itself to the foreground. Use + |remote_foreground()| instead. + {only in the Win32 GUI and console version} + +fullcommand({name}) *fullcommand()* + Get the full command name from a short abbreviated command + name; see |20.2| for details on command abbreviations. + + The string argument {name} may start with a `:` and can + include a [range], these are skipped and not returned. + Returns an empty string if a command doesn't exist or if it's + ambiguous (for user-defined commands). + + For example `fullcommand('s')`, `fullcommand('sub')`, + `fullcommand(':%substitute')` all return "substitute". + + Can also be used as a |method|: > + GetName()->fullcommand() +< + *funcref()* +funcref({name} [, {arglist}] [, {dict}]) + Just like |function()|, but the returned Funcref will lookup + the function by reference, not by name. This matters when the + function {name} is redefined later. + + Unlike |function()|, {name} must be an existing user function. + It only works for an autoloaded function if it has already + been loaded (to avoid mistakenly loading the autoload script + when only intending to use the function name, use |function()| + instead). {name} cannot be a builtin function. + + Can also be used as a |method|: > + GetFuncname()->funcref([arg]) +< + *function()* *E700* *E922* *E923* +function({name} [, {arglist}] [, {dict}]) + Return a |Funcref| variable that refers to function {name}. + {name} can be a user defined function or an internal function. + + {name} can also be a Funcref or a partial. When it is a + partial the dict stored in it will be used and the {dict} + argument is not allowed. E.g.: > + let FuncWithArg = function(dict.Func, [arg]) + let Broken = function(dict.Func, [arg], dict) +< + When using the Funcref the function will be found by {name}, + also when it was redefined later. Use |funcref()| to keep the + same function. + + When {arglist} or {dict} is present this creates a partial. + That means the argument list and/or the dictionary is stored in + the Funcref and will be used when the Funcref is called. + + The arguments are passed to the function in front of other + arguments, but after any argument from |method|. Example: > + func Callback(arg1, arg2, name) + ... + let Partial = function('Callback', ['one', 'two']) + ... + call Partial('name') +< Invokes the function as with: > + call Callback('one', 'two', 'name') + +< The Dictionary is only useful when calling a "dict" function. + In that case the {dict} is passed in as "self". Example: > + function Callback() dict + echo "called for " . self.name + endfunction + ... + let context = {"name": "example"} + let Func = function('Callback', context) + ... + call Func() " will echo: called for example + +< The argument list and the Dictionary can be combined: > + function Callback(arg1, count) dict + ... + let context = {"name": "example"} + let Func = function('Callback', ['one'], context) + ... + call Func(500) +< Invokes the function as with: > + call context.Callback('one', 500) +< + Can also be used as a |method|: > + GetFuncname()->function([arg]) + +garbagecollect([{atexit}]) *garbagecollect()* + Cleanup unused |Lists| and |Dictionaries| that have circular + references. + + There is hardly ever a need to invoke this function, as it is + automatically done when Vim runs out of memory or is waiting + for the user to press a key after 'updatetime'. Items without + circular references are always freed when they become unused. + This is useful if you have deleted a very big |List| and/or + |Dictionary| with circular references in a script that runs + for a long time. + + When the optional {atexit} argument is one, garbage + collection will also be done when exiting Vim, if it wasn't + done before. This is useful when checking for memory leaks. + + The garbage collection is not done immediately but only when + it's safe to perform. This is when waiting for the user to + type a character. + +get({list}, {idx} [, {default}]) *get()* + Get item {idx} from |List| {list}. When this item is not + available return {default}. Return zero when {default} is + omitted. + Can also be used as a |method|: > + mylist->get(idx) +get({blob}, {idx} [, {default}]) + Get byte {idx} from |Blob| {blob}. When this byte is not + available return {default}. Return -1 when {default} is + omitted. +get({dict}, {key} [, {default}]) + Get item with key {key} from |Dictionary| {dict}. When this + item is not available return {default}. Return zero when + {default} is omitted. Useful example: > + let val = get(g:, 'var_name', 'default') +< This gets the value of g:var_name if it exists, and uses + 'default' when it does not exist. +get({func}, {what}) + Get item {what} from Funcref {func}. Possible values for + {what} are: + "name" The function name + "func" The function + "dict" The dictionary + "args" The list with arguments + + *getbufinfo()* +getbufinfo([{buf}]) +getbufinfo([{dict}]) + Get information about buffers as a List of Dictionaries. + + Without an argument information about all the buffers is + returned. + + When the argument is a |Dictionary| only the buffers matching + the specified criteria are returned. The following keys can + be specified in {dict}: + buflisted include only listed buffers. + bufloaded include only loaded buffers. + bufmodified include only modified buffers. + + Otherwise, {buf} specifies a particular buffer to return + information for. For the use of {buf}, see |bufname()| + above. If the buffer is found the returned List has one item. + Otherwise the result is an empty list. + + Each returned List item is a dictionary with the following + entries: + bufnr Buffer number. + changed TRUE if the buffer is modified. + changedtick Number of changes made to the buffer. + hidden TRUE if the buffer is hidden. + lastused Timestamp in seconds, like + |localtime()|, when the buffer was + last used. + listed TRUE if the buffer is listed. + lnum Line number used for the buffer when + opened in the current window. + Only valid if the buffer has been + displayed in the window in the past. + If you want the line number of the + last known cursor position in a given + window, use |line()|: > + :echo line('.', {winid}) +< + linecount Number of lines in the buffer (only + valid when loaded) + loaded TRUE if the buffer is loaded. + name Full path to the file in the buffer. + signs List of signs placed in the buffer. + Each list item is a dictionary with + the following fields: + id sign identifier + lnum line number + name sign name + variables A reference to the dictionary with + buffer-local variables. + windows List of |window-ID|s that display this + buffer + + Examples: > + for buf in getbufinfo() + echo buf.name + endfor + for buf in getbufinfo({'buflisted':1}) + if buf.changed + .... + endif + endfor +< + To get buffer-local options use: > + getbufvar({bufnr}, '&option_name') +< + Can also be used as a |method|: > + GetBufnr()->getbufinfo() +< + *getbufline()* +getbufline({buf}, {lnum} [, {end}]) + Return a |List| with the lines starting from {lnum} to {end} + (inclusive) in the buffer {buf}. If {end} is omitted, a + |List| with only the line {lnum} is returned. + + For the use of {buf}, see |bufname()| above. + + For {lnum} and {end} "$" can be used for the last line of the + buffer. Otherwise a number must be used. + + When {lnum} is smaller than 1 or bigger than the number of + lines in the buffer, an empty |List| is returned. + + When {end} is greater than the number of lines in the buffer, + it is treated as {end} is set to the number of lines in the + buffer. When {end} is before {lnum} an empty |List| is + returned. + + This function works only for loaded buffers. For unloaded and + non-existing buffers, an empty |List| is returned. + + Example: > + :let lines = getbufline(bufnr("myfile"), 1, "$") + +< Can also be used as a |method|: > + GetBufnr()->getbufline(lnum) + +getbufvar({buf}, {varname} [, {def}]) *getbufvar()* + The result is the value of option or local buffer variable + {varname} in buffer {buf}. Note that the name without "b:" + must be used. + The {varname} argument is a string. + When {varname} is empty returns a |Dictionary| with all the + buffer-local variables. + When {varname} is equal to "&" returns a |Dictionary| with all + the buffer-local options. + Otherwise, when {varname} starts with "&" returns the value of + a buffer-local option. + This also works for a global or buffer-local option, but it + doesn't work for a global variable, window-local variable or + window-local option. + For the use of {buf}, see |bufname()| above. + When the buffer or variable doesn't exist {def} or an empty + string is returned, there is no error message. + Examples: > + :let bufmodified = getbufvar(1, "&mod") + :echo "todo myvar = " . getbufvar("todo", "myvar") + +< Can also be used as a |method|: > + GetBufnr()->getbufvar(varname) +< +getchangelist([{buf}]) *getchangelist()* + Returns the |changelist| for the buffer {buf}. For the use + of {buf}, see |bufname()| above. If buffer {buf} doesn't + exist, an empty list is returned. + + The returned list contains two entries: a list with the change + locations and the current position in the list. Each + entry in the change list is a dictionary with the following + entries: + col column number + coladd column offset for 'virtualedit' + lnum line number + If buffer {buf} is the current buffer, then the current + position refers to the position in the list. For other + buffers, it is set to the length of the list. + + Can also be used as a |method|: > + GetBufnr()->getchangelist() + +getchar([expr]) *getchar()* + Get a single character from the user or input stream. + If [expr] is omitted, wait until a character is available. + If [expr] is 0, only get a character when one is available. + Return zero otherwise. + If [expr] is 1, only check if a character is available, it is + not consumed. Return zero if no character available. + If you prefer always getting a string use |getcharstr()|. + + Without [expr] and when [expr] is 0 a whole character or + special key is returned. If it is a single character, the + result is a number. Use nr2char() to convert it to a String. + Otherwise a String is returned with the encoded character. + For a special key it's a String with a sequence of bytes + starting with 0x80 (decimal: 128). This is the same value as + the String "\<Key>", e.g., "\<Left>". The returned value is + also a String when a modifier (shift, control, alt) was used + that is not included in the character. + + When [expr] is 0 and Esc is typed, there will be a short delay + while Vim waits to see if this is the start of an escape + sequence. + + When [expr] is 1 only the first byte is returned. For a + one-byte character it is the character itself as a number. + Use nr2char() to convert it to a String. + + Use getcharmod() to obtain any additional modifiers. + + When the user clicks a mouse button, the mouse event will be + returned. The position can then be found in |v:mouse_col|, + |v:mouse_lnum|, |v:mouse_winid| and |v:mouse_win|. + |getmousepos()| can also be used. Mouse move events will be + ignored. + This example positions the mouse as it would normally happen: > + let c = getchar() + if c == "\<LeftMouse>" && v:mouse_win > 0 + exe v:mouse_win . "wincmd w" + exe v:mouse_lnum + exe "normal " . v:mouse_col . "|" + endif +< + There is no prompt, you will somehow have to make clear to the + user that a character has to be typed. The screen is not + redrawn, e.g. when resizing the window. + + There is no mapping for the character. + Key codes are replaced, thus when the user presses the <Del> + key you get the code for the <Del> key, not the raw character + sequence. Examples: > + getchar() == "\<Del>" + getchar() == "\<S-Left>" +< This example redefines "f" to ignore case: > + :nmap f :call FindChar()<CR> + :function FindChar() + : let c = nr2char(getchar()) + : while col('.') < col('$') - 1 + : normal l + : if getline('.')[col('.') - 1] ==? c + : break + : endif + : endwhile + :endfunction +< +getcharmod() *getcharmod()* + The result is a Number which is the state of the modifiers for + the last obtained character with getchar() or in another way. + These values are added together: + 2 shift + 4 control + 8 alt (meta) + 16 meta (when it's different from ALT) + 32 mouse double click + 64 mouse triple click + 96 mouse quadruple click (== 32 + 64) + 128 command (Macintosh only) + Only the modifiers that have not been included in the + character itself are obtained. Thus Shift-a results in "A" + without a modifier. + + *getcharpos()* +getcharpos({expr}) + Get the position for String {expr}. Same as |getpos()| but the + column number in the returned List is a character index + instead of a byte index. + + Example: + With the cursor on '세' in line 5 with text "여보세요": > + getcharpos('.') returns [0, 5, 3, 0] + getpos('.') returns [0, 5, 7, 0] +< + Can also be used as a |method|: > + GetMark()->getcharpos() + +getcharsearch() *getcharsearch()* + Return the current character search information as a {dict} + with the following entries: + + char character previously used for a character + search (|t|, |f|, |T|, or |F|); empty string + if no character search has been performed + forward direction of character search; 1 for forward, + 0 for backward + until type of character search; 1 for a |t| or |T| + character search, 0 for an |f| or |F| + character search + + This can be useful to always have |;| and |,| search + forward/backward regardless of the direction of the previous + character search: > + :nnoremap <expr> ; getcharsearch().forward ? ';' : ',' + :nnoremap <expr> , getcharsearch().forward ? ',' : ';' +< Also see |setcharsearch()|. + + +getcharstr([expr]) *getcharstr()* + Get a single character from the user or input stream as a + string. + If [expr] is omitted, wait until a character is available. + If [expr] is 0 or false, only get a character when one is + available. Return an empty string otherwise. + If [expr] is 1 or true, only check if a character is + available, it is not consumed. Return an empty string + if no character is available. + Otherwise this works like |getchar()|, except that a number + result is converted to a string. + + +getcmdline() *getcmdline()* + Return the current command-line. Only works when the command + line is being edited, thus requires use of |c_CTRL-\_e| or + |c_CTRL-R_=|. + Example: > + :cmap <F7> <C-\>eescape(getcmdline(), ' \')<CR> +< Also see |getcmdtype()|, |getcmdpos()| and |setcmdpos()|. + Returns an empty string when entering a password or using + |inputsecret()|. + +getcmdpos() *getcmdpos()* + Return the position of the cursor in the command line as a + byte count. The first column is 1. + Only works when editing the command line, thus requires use of + |c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping. + Returns 0 otherwise. + Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|. + +getcmdtype() *getcmdtype()* + Return the current command-line type. Possible return values + are: + : normal Ex command + > debug mode command |debug-mode| + / forward search command + ? backward search command + @ |input()| command + - |:insert| or |:append| command + = |i_CTRL-R_=| + Only works when editing the command line, thus requires use of + |c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping. + Returns an empty string otherwise. + Also see |getcmdpos()|, |setcmdpos()| and |getcmdline()|. + +getcmdwintype() *getcmdwintype()* + Return the current |command-line-window| type. Possible return + values are the same as |getcmdtype()|. Returns an empty string + when not in the command-line window. + +getcompletion({pat}, {type} [, {filtered}]) *getcompletion()* + Return a list of command-line completion matches. The String + {type} argument specifies what for. The following completion + types are supported: + + arglist file names in argument list + augroup autocmd groups + buffer buffer names + behave :behave suboptions + cmdline |cmdline-completion| result + color color schemes + command Ex command + compiler compilers + cscope |:cscope| suboptions + diff_buffer |:diffget| and |:diffput| completion + dir directory names + environment environment variable names + event autocommand events + expression Vim expression + file file and directory names + file_in_path file and directory names in |'path'| + filetype filetype names |'filetype'| + function function name + help help subjects + highlight highlight groups + history :history suboptions + locale locale names (as output of locale -a) + mapclear buffer argument + mapping mapping name + menu menus + messages |:messages| suboptions + option options + packadd optional package |pack-add| names + shellcmd Shell command + sign |:sign| suboptions + syntax syntax file names |'syntax'| + syntime |:syntime| suboptions + tag tags + tag_listfiles tags, file names + user user names + var user variables + + If {pat} is an empty string, then all the matches are + returned. Otherwise only items matching {pat} are returned. + See |wildcards| for the use of special characters in {pat}. + + If the optional {filtered} flag is set to 1, then 'wildignore' + is applied to filter the results. Otherwise all the matches + are returned. The 'wildignorecase' option always applies. + + If {type} is "cmdline", then the |cmdline-completion| result is + returned. For example, to complete the possible values after + a ":call" command: > + echo getcompletion('call ', 'cmdline') +< + If there are no matches, an empty list is returned. An + invalid value for {type} produces an error. + + Can also be used as a |method|: > + GetPattern()->getcompletion('color') +< + *getcurpos()* +getcurpos([{winid}]) + Get the position of the cursor. This is like getpos('.'), but + includes an extra "curswant" in the list: + [0, lnum, col, off, curswant] ~ + The "curswant" number is the preferred column when moving the + cursor vertically. Also see |getcursorcharpos()| and + |getpos()|. + The first "bufnum" item is always zero. The byte position of + the cursor is returned in 'col'. To get the character + position, use |getcursorcharpos()|. + + The optional {winid} argument can specify the window. It can + be the window number or the |window-ID|. The last known + cursor position is returned, this may be invalid for the + current value of the buffer if it is not the current window. + If {winid} is invalid a list with zeroes is returned. + + This can be used to save and restore the cursor position: > + let save_cursor = getcurpos() + MoveTheCursorAround + call setpos('.', save_cursor) +< Note that this only works within the window. See + |winrestview()| for restoring more state. + + Can also be used as a |method|: > + GetWinid()->getcurpos() +< + *getcursorcharpos()* +getcursorcharpos([{winid}]) + Same as |getcurpos()| but the column number in the returned + List is a character index instead of a byte index. + + Example: + With the cursor on '보' in line 3 with text "여보세요": > + getcursorcharpos() returns [0, 3, 2, 0, 3] + getcurpos() returns [0, 3, 4, 0, 3] +< + Can also be used as a |method|: > + GetWinid()->getcursorcharpos() + +getcwd([{winnr} [, {tabnr}]]) *getcwd()* + With no arguments, returns the name of the effective + |current-directory|. With {winnr} or {tabnr} the working + directory of that scope is returned, and 'autochdir' is + ignored. + Tabs and windows are identified by their respective numbers, + 0 means current tab or window. Missing tab number implies 0. + Thus the following are equivalent: > + getcwd(0) + getcwd(0, 0) +< If {winnr} is -1 it is ignored, only the tab is resolved. + {winnr} can be the window number or the |window-ID|. + If both {winnr} and {tabnr} are -1 the global working + directory is returned. + Throw error if the arguments are invalid. |E5000| |E5001| |E5002| + + Can also be used as a |method|: > + GetWinnr()->getcwd() + +getenv({name}) *getenv()* + Return the value of environment variable {name}. The {name} + argument is a string, without a leading '$'. Example: > + myHome = getenv('HOME') + +< When the variable does not exist |v:null| is returned. That + is different from a variable set to an empty string. + See also |expr-env|. + + Can also be used as a |method|: > + GetVarname()->getenv() + +getfontname([{name}]) *getfontname()* + Without an argument returns the name of the normal font being + used. Like what is used for the Normal highlight group + |hl-Normal|. + With an argument a check is done whether String {name} is a + valid font name. If not then an empty string is returned. + Otherwise the actual font name is returned, or {name} if the + GUI does not support obtaining the real name. + Only works when the GUI is running, thus not in your vimrc or + gvimrc file. Use the |GUIEnter| autocommand to use this + function just after the GUI has started. + +getfperm({fname}) *getfperm()* + The result is a String, which is the read, write, and execute + permissions of the given file {fname}. + If {fname} does not exist or its directory cannot be read, an + empty string is returned. + The result is of the form "rwxrwxrwx", where each group of + "rwx" flags represent, in turn, the permissions of the owner + of the file, the group the file belongs to, and other users. + If a user does not have a given permission the flag for this + is replaced with the string "-". Examples: > + :echo getfperm("/etc/passwd") + :echo getfperm(expand("~/.config/nvim/init.vim")) +< This will hopefully (from a security point of view) display + the string "rw-r--r--" or even "rw-------". + + Can also be used as a |method|: > + GetFilename()->getfperm() +< + For setting permissions use |setfperm()|. + +getfsize({fname}) *getfsize()* + The result is a Number, which is the size in bytes of the + given file {fname}. + If {fname} is a directory, 0 is returned. + If the file {fname} can't be found, -1 is returned. + If the size of {fname} is too big to fit in a Number then -2 + is returned. + + Can also be used as a |method|: > + GetFilename()->getfsize() + +getftime({fname}) *getftime()* + The result is a Number, which is the last modification time of + the given file {fname}. The value is measured as seconds + since 1st Jan 1970, and may be passed to strftime(). See also + |localtime()| and |strftime()|. + If the file {fname} can't be found -1 is returned. + + Can also be used as a |method|: > + GetFilename()->getftime() + +getftype({fname}) *getftype()* + The result is a String, which is a description of the kind of + file of the given file {fname}. + If {fname} does not exist an empty string is returned. + Here is a table over different kinds of files and their + results: + Normal file "file" + Directory "dir" + Symbolic link "link" + Block device "bdev" + Character device "cdev" + Socket "socket" + FIFO "fifo" + All other "other" + Example: > + getftype("/home") +< Note that a type such as "link" will only be returned on + systems that support it. On some systems only "dir" and + "file" are returned. + + Can also be used as a |method|: > + GetFilename()->getftype() + +getjumplist([{winnr} [, {tabnr}]]) *getjumplist()* + Returns the |jumplist| for the specified window. + + Without arguments use the current window. + With {winnr} only use this window in the current tab page. + {winnr} can also be a |window-ID|. + With {winnr} and {tabnr} use the window in the specified tab + page. + + The returned list contains two entries: a list with the jump + locations and the last used jump position number in the list. + Each entry in the jump location list is a dictionary with + the following entries: + bufnr buffer number + col column number + coladd column offset for 'virtualedit' + filename filename if available + lnum line number + + Can also be used as a |method|: > + GetWinnr()->getjumplist() + +< *getline()* +getline({lnum} [, {end}]) + Without {end} the result is a String, which is line {lnum} + from the current buffer. Example: > + getline(1) +< When {lnum} is a String that doesn't start with a + digit, |line()| is called to translate the String into a Number. + To get the line under the cursor: > + getline(".") +< When {lnum} is a number smaller than 1 or bigger than the + number of lines in the buffer, an empty string is returned. + + When {end} is given the result is a |List| where each item is + a line from the current buffer in the range {lnum} to {end}, + including line {end}. + {end} is used in the same way as {lnum}. + Non-existing lines are silently omitted. + When {end} is before {lnum} an empty |List| is returned. + Example: > + :let start = line('.') + :let end = search("^$") - 1 + :let lines = getline(start, end) + +< Can also be used as a |method|: > + ComputeLnum()->getline() + +< To get lines from another buffer see |getbufline()| + +getloclist({nr} [, {what}]) *getloclist()* + Returns a |List| with all the entries in the location list for + window {nr}. {nr} can be the window number or the |window-ID|. + When {nr} is zero the current window is used. + + For a location list window, the displayed location list is + returned. For an invalid window number {nr}, an empty list is + returned. Otherwise, same as |getqflist()|. + + If the optional {what} dictionary argument is supplied, then + returns the items listed in {what} as a dictionary. Refer to + |getqflist()| for the supported items in {what}. + If {what} contains 'filewinid', then returns the id of the + window used to display files from the location list. This + field is applicable only when called from a location list + window. See |location-list-file-window| for more details. + + Returns a |Dictionary| with default values if there is no + location list for the window {nr}. + Returns an empty Dictionary if window {nr} does not exist. + + Examples (See also |getqflist-examples|): > + :echo getloclist(3, {'all': 0}) + :echo getloclist(5, {'filewinid': 0}) + + +getmarklist([{buf}]) *getmarklist()* + Without the {buf} argument returns a |List| with information + about all the global marks. |mark| + + If the optional {buf} argument is specified, returns the + local marks defined in buffer {buf}. For the use of {buf}, + see |bufname()|. + + Each item in the returned List is a |Dict| with the following: + mark name of the mark prefixed by "'" + pos a |List| with the position of the mark: + [bufnum, lnum, col, off] + Refer to |getpos()| for more information. + file file name + + Refer to |getpos()| for getting information about a specific + mark. + + Can also be used as a |method|: > + GetBufnr()->getmarklist() + +getmatches([{win}]) *getmatches()* + Returns a |List| with all matches previously defined for the + current window by |matchadd()| and the |:match| commands. + |getmatches()| is useful in combination with |setmatches()|, + as |setmatches()| can restore a list of matches saved by + |getmatches()|. + If {win} is specified, use the window with this number or + window ID instead of the current window. + Example: > + :echo getmatches() +< [{'group': 'MyGroup1', 'pattern': 'TODO', + 'priority': 10, 'id': 1}, {'group': 'MyGroup2', + 'pattern': 'FIXME', 'priority': 10, 'id': 2}] > + :let m = getmatches() + :call clearmatches() + :echo getmatches() +< [] > + :call setmatches(m) + :echo getmatches() +< [{'group': 'MyGroup1', 'pattern': 'TODO', + 'priority': 10, 'id': 1}, {'group': 'MyGroup2', + 'pattern': 'FIXME', 'priority': 10, 'id': 2}] > + :unlet m +< +getmousepos() *getmousepos()* + Returns a Dictionary with the last known position of the + mouse. This can be used in a mapping for a mouse click. The + items are: + screenrow screen row + screencol screen column + winid Window ID of the click + winrow row inside "winid" + wincol column inside "winid" + line text line inside "winid" + column text column inside "winid" + All numbers are 1-based. + + If not over a window, e.g. when in the command line, then only + "screenrow" and "screencol" are valid, the others are zero. + + When on the status line below a window or the vertical + separater right of a window, the "line" and "column" values + are zero. + + When the position is after the text then "column" is the + length of the text in bytes plus one. + + If the mouse is over a focusable floating window then that + window is used. + + When using |getchar()| the Vim variables |v:mouse_lnum|, + |v:mouse_col| and |v:mouse_winid| also provide these values. + + *getpid()* +getpid() Return a Number which is the process ID of the Vim process. + This is a unique number, until Vim exits. + + *getpos()* +getpos({expr}) Get the position for String {expr}. For possible values of + {expr} see |line()|. For getting the cursor position see + |getcurpos()|. + The result is a |List| with four numbers: + [bufnum, lnum, col, off] + "bufnum" is zero, unless a mark like '0 or 'A is used, then it + is the buffer number of the mark. + "lnum" and "col" are the position in the buffer. The first + column is 1. + The "off" number is zero, unless 'virtualedit' is used. Then + it is the offset in screen columns from the start of the + character. E.g., a position within a <Tab> or after the last + character. + Note that for '< and '> Visual mode matters: when it is "V" + (visual line mode) the column of '< is zero and the column of + '> is a large number. + The column number in the returned List is the byte position + within the line. To get the character position in the line, + use |getcharpos()|. + The column number can be very large, e.g. 2147483647, in which + case it means "after the end of the line". + This can be used to save and restore the position of a mark: > + let save_a_mark = getpos("'a") + ... + call setpos("'a", save_a_mark) +< Also see |getcharpos()|, |getcurpos()| and |setpos()|. + + Can also be used as a |method|: > + GetMark()->getpos() + +getqflist([{what}]) *getqflist()* + Returns a |List| with all the current quickfix errors. Each + list item is a dictionary with these entries: + bufnr number of buffer that has the file name, use + bufname() to get the name + module module name + lnum line number in the buffer (first line is 1) + end_lnum + end of line number if the item is multiline + col column number (first column is 1) + end_col end of column number if the item has range + vcol |TRUE|: "col" is visual column + |FALSE|: "col" is byte index + nr error number + pattern search pattern used to locate the error + text description of the error + type type of the error, 'E', '1', etc. + valid |TRUE|: recognized error message + + When there is no error list or it's empty, an empty list is + returned. Quickfix list entries with a non-existing buffer + number are returned with "bufnr" set to zero (Note: some + functions accept buffer number zero for the alternate buffer, + you may need to explicitly check for zero). + + Useful application: Find pattern matches in multiple files and + do something with them: > + :vimgrep /theword/jg *.c + :for d in getqflist() + : echo bufname(d.bufnr) ':' d.lnum '=' d.text + :endfor +< + If the optional {what} dictionary argument is supplied, then + returns only the items listed in {what} as a dictionary. The + following string items are supported in {what}: + changedtick get the total number of changes made + to the list |quickfix-changedtick| + context get the |quickfix-context| + efm errorformat to use when parsing "lines". If + not present, then the 'errorformat' option + value is used. + id get information for the quickfix list with + |quickfix-ID|; zero means the id for the + current list or the list specified by "nr" + idx get information for the quickfix entry at this + index in the list specified by 'id' or 'nr'. + If set to zero, then uses the current entry. + See |quickfix-index| + items quickfix list entries + lines parse a list of lines using 'efm' and return + the resulting entries. Only a |List| type is + accepted. The current quickfix list is not + modified. See |quickfix-parse|. + nr get information for this quickfix list; zero + means the current quickfix list and "$" means + the last quickfix list + size number of entries in the quickfix list + title get the list title |quickfix-title| + winid get the quickfix |window-ID| + all all of the above quickfix properties + Non-string items in {what} are ignored. To get the value of a + particular item, set it to zero. + If "nr" is not present then the current quickfix list is used. + If both "nr" and a non-zero "id" are specified, then the list + specified by "id" is used. + To get the number of lists in the quickfix stack, set "nr" to + "$" in {what}. The "nr" value in the returned dictionary + contains the quickfix stack size. + When "lines" is specified, all the other items except "efm" + are ignored. The returned dictionary contains the entry + "items" with the list of entries. + + The returned dictionary contains the following entries: + changedtick total number of changes made to the + list |quickfix-changedtick| + context quickfix list context. See |quickfix-context| + If not present, set to "". + id quickfix list ID |quickfix-ID|. If not + present, set to 0. + idx index of the quickfix entry in the list. If not + present, set to 0. + items quickfix list entries. If not present, set to + an empty list. + nr quickfix list number. If not present, set to 0 + size number of entries in the quickfix list. If not + present, set to 0. + title quickfix list title text. If not present, set + to "". + winid quickfix |window-ID|. If not present, set to 0 + + Examples (See also |getqflist-examples|): > + :echo getqflist({'all': 1}) + :echo getqflist({'nr': 2, 'title': 1}) + :echo getqflist({'lines' : ["F1:10:L10"]}) +< +getreg([{regname} [, 1 [, {list}]]]) *getreg()* + The result is a String, which is the contents of register + {regname}. Example: > + :let cliptext = getreg('*') +< When register {regname} was not set the result is an empty + string. + The {regname} argument must be a string. + + getreg('=') returns the last evaluated value of the expression + register. (For use in maps.) + getreg('=', 1) returns the expression itself, so that it can + be restored with |setreg()|. For other registers the extra + argument is ignored, thus you can always give it. + + If {list} is present and |TRUE|, the result type is changed + to |List|. Each list item is one text line. Use it if you care + about zero bytes possibly present inside register: without + third argument both NLs and zero bytes are represented as NLs + (see |NL-used-for-Nul|). + When the register was not set an empty list is returned. + + If {regname} is not specified, |v:register| is used. + + Can also be used as a |method|: > + GetRegname()->getreg() + +getreginfo([{regname}]) *getreginfo()* + Returns detailed information about register {regname} as a + Dictionary with the following entries: + regcontents List of lines contained in register + {regname}, like + |getreg|({regname}, 1, 1). + regtype the type of register {regname}, as in + |getregtype()|. + isunnamed Boolean flag, v:true if this register + is currently pointed to by the unnamed + register. + points_to for the unnamed register, gives the + single letter name of the register + currently pointed to (see |quotequote|). + For example, after deleting a line + with `dd`, this field will be "1", + which is the register that got the + deleted text. + + The {regname} argument is a string. If {regname} is invalid + or not set, an empty Dictionary will be returned. + If {regname} is not specified, |v:register| is used. + The returned Dictionary can be passed to |setreg()|. + + Can also be used as a |method|: > + GetRegname()->getreginfo() + +getregtype([{regname}]) *getregtype()* + The result is a String, which is type of register {regname}. + The value will be one of: + "v" for |charwise| text + "V" for |linewise| text + "<CTRL-V>{width}" for |blockwise-visual| text + "" for an empty or unknown register + <CTRL-V> is one character with value 0x16. + The {regname} argument is a string. If {regname} is not + specified, |v:register| is used. + + Can also be used as a |method|: > + GetRegname()->getregtype() + +gettabinfo([{tabnr}]) *gettabinfo()* + If {tabnr} is not specified, then information about all the + tab pages is returned as a |List|. Each List item is a + |Dictionary|. Otherwise, {tabnr} specifies the tab page + number and information about that one is returned. If the tab + page does not exist an empty List is returned. + + Each List item is a |Dictionary| with the following entries: + tabnr tab page number. + variables a reference to the dictionary with + tabpage-local variables + windows List of |window-ID|s in the tab page. + + Can also be used as a |method|: > + GetTabnr()->gettabinfo() + +gettabvar({tabnr}, {varname} [, {def}]) *gettabvar()* + Get the value of a tab-local variable {varname} in tab page + {tabnr}. |t:var| + Tabs are numbered starting with one. + The {varname} argument is a string. When {varname} is empty a + dictionary with all tab-local variables is returned. + Note that the name without "t:" must be used. + When the tab or variable doesn't exist {def} or an empty + string is returned, there is no error message. + + Can also be used as a |method|: > + GetTabnr()->gettabvar(varname) + +gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()* + Get the value of window-local variable {varname} in window + {winnr} in tab page {tabnr}. + The {varname} argument is a string. When {varname} is empty a + dictionary with all window-local variables is returned. + When {varname} is equal to "&" get the values of all + window-local options in a |Dictionary|. + Otherwise, when {varname} starts with "&" get the value of a + window-local option. + Note that {varname} must be the name without "w:". + Tabs are numbered starting with one. For the current tabpage + use |getwinvar()|. + {winnr} can be the window number or the |window-ID|. + When {winnr} is zero the current window is used. + This also works for a global option, buffer-local option and + window-local option, but it doesn't work for a global variable + or buffer-local variable. + When the tab, window or variable doesn't exist {def} or an + empty string is returned, there is no error message. + Examples: > + :let list_is_on = gettabwinvar(1, 2, '&list') + :echo "myvar = " . gettabwinvar(3, 1, 'myvar') +< + To obtain all window-local variables use: > + gettabwinvar({tabnr}, {winnr}, '&') + +< Can also be used as a |method|: > + GetTabnr()->gettabwinvar(winnr, varname) + +gettagstack([{winnr}]) *gettagstack()* + The result is a Dict, which is the tag stack of window {winnr}. + {winnr} can be the window number or the |window-ID|. + When {winnr} is not specified, the current window is used. + When window {winnr} doesn't exist, an empty Dict is returned. + + The returned dictionary contains the following entries: + curidx Current index in the stack. When at + top of the stack, set to (length + 1). + Index of bottom of the stack is 1. + items List of items in the stack. Each item + is a dictionary containing the + entries described below. + length Number of entries in the stack. + + Each item in the stack is a dictionary with the following + entries: + bufnr buffer number of the current jump + from cursor position before the tag jump. + See |getpos()| for the format of the + returned list. + matchnr current matching tag number. Used when + multiple matching tags are found for a + name. + tagname name of the tag + + See |tagstack| for more information about the tag stack. + + Can also be used as a |method|: > + GetWinnr()->gettagstack() + +getwininfo([{winid}]) *getwininfo()* + Returns information about windows as a |List| with Dictionaries. + + If {winid} is given Information about the window with that ID + is returned, as a |List| with one item. If the window does not + exist the result is an empty list. + + Without {winid} information about all the windows in all the + tab pages is returned. + + Each List item is a |Dictionary| with the following entries: + botline last complete displayed buffer line + bufnr number of buffer in the window + height window height (excluding winbar) + loclist 1 if showing a location list + quickfix 1 if quickfix or location list window + terminal 1 if a terminal window + tabnr tab page number + topline first displayed buffer line + variables a reference to the dictionary with + window-local variables + width window width + winbar 1 if the window has a toolbar, 0 + otherwise + wincol leftmost screen column of the window; + "col" from |win_screenpos()| + textoff number of columns occupied by any + 'foldcolumn', 'signcolumn' and line + number in front of the text + winid |window-ID| + winnr window number + winrow topmost screen line of the window; + "row" from |win_screenpos()| + + Can also be used as a |method|: > + GetWinnr()->getwininfo() + +getwinpos([{timeout}]) *getwinpos()* + The result is a |List| with two numbers, the result of + |getwinposx()| and |getwinposy()| combined: + [x-pos, y-pos] + {timeout} can be used to specify how long to wait in msec for + a response from the terminal. When omitted 100 msec is used. + + Use a longer time for a remote terminal. + When using a value less than 10 and no response is received + within that time, a previously reported position is returned, + if available. This can be used to poll for the position and + do some work in the meantime: > + while 1 + let res = getwinpos(1) + if res[0] >= 0 + break + endif + " Do some work here + endwhile +< + Can also be used as a |method|: > + GetTimeout()->getwinpos() +< + *getwinposx()* +getwinposx() The result is a Number, which is the X coordinate in pixels of + the left hand side of the GUI Vim window. The result will be + -1 if the information is not available. + The value can be used with `:winpos`. + + *getwinposy()* +getwinposy() The result is a Number, which is the Y coordinate in pixels of + the top of the GUI Vim window. The result will be -1 if the + information is not available. + The value can be used with `:winpos`. + +getwinvar({winnr}, {varname} [, {def}]) *getwinvar()* + Like |gettabwinvar()| for the current tabpage. + Examples: > + :let list_is_on = getwinvar(2, '&list') + :echo "myvar = " . getwinvar(1, 'myvar') + +< Can also be used as a |method|: > + GetWinnr()->getwinvar(varname) +< +glob({expr} [, {nosuf} [, {list} [, {alllinks}]]]) *glob()* + Expand the file wildcards in {expr}. See |wildcards| for the + use of special characters. + + Unless the optional {nosuf} argument is given and is |TRUE|, + the 'suffixes' and 'wildignore' options apply: Names matching + one of the patterns in 'wildignore' will be skipped and + 'suffixes' affect the ordering of matches. + 'wildignorecase' always applies. + + When {list} is present and it is |TRUE| the result is a |List| + with all matching files. The advantage of using a List is, + you also get filenames containing newlines correctly. + Otherwise the result is a String and when there are several + matches, they are separated by <NL> characters. + + If the expansion fails, the result is an empty String or List. + + You can also use |readdir()| if you need to do complicated + things, such as limiting the number of matches. + + A name for a non-existing file is not included. A symbolic + link is only included if it points to an existing file. + However, when the {alllinks} argument is present and it is + |TRUE| then all symbolic links are included. + + For most systems backticks can be used to get files names from + any external command. Example: > + :let tagfiles = glob("`find . -name tags -print`") + :let &tags = substitute(tagfiles, "\n", ",", "g") +< The result of the program inside the backticks should be one + item per line. Spaces inside an item are allowed. + + See |expand()| for expanding special Vim variables. See + |system()| for getting the raw output of an external command. + + Can also be used as a |method|: > + GetExpr()->glob() + +glob2regpat({string}) *glob2regpat()* + Convert a file pattern, as used by glob(), into a search + pattern. The result can be used to match with a string that + is a file name. E.g. > + if filename =~ glob2regpat('Make*.mak') +< This is equivalent to: > + if filename =~ '^Make.*\.mak$' +< When {string} is an empty string the result is "^$", match an + empty string. + Note that the result depends on the system. On MS-Windows + a backslash usually means a path separator. + + Can also be used as a |method|: > + GetExpr()->glob2regpat() +< *globpath()* +globpath({path}, {expr} [, {nosuf} [, {list} [, {allinks}]]]) + Perform glob() for String {expr} on all directories in {path} + and concatenate the results. Example: > + :echo globpath(&rtp, "syntax/c.vim") +< + {path} is a comma-separated list of directory names. Each + directory name is prepended to {expr} and expanded like with + |glob()|. A path separator is inserted when needed. + To add a comma inside a directory name escape it with a + backslash. Note that on MS-Windows a directory may have a + trailing backslash, remove it if you put a comma after it. + If the expansion fails for one of the directories, there is no + error message. + + Unless the optional {nosuf} argument is given and is |TRUE|, + the 'suffixes' and 'wildignore' options apply: Names matching + one of the patterns in 'wildignore' will be skipped and + 'suffixes' affect the ordering of matches. + + When {list} is present and it is |TRUE| the result is a |List| + with all matching files. The advantage of using a List is, you + also get filenames containing newlines correctly. Otherwise + the result is a String and when there are several matches, + they are separated by <NL> characters. Example: > + :echo globpath(&rtp, "syntax/c.vim", 0, 1) +< + {allinks} is used as with |glob()|. + + The "**" item can be used to search in a directory tree. + For example, to find all "README.txt" files in the directories + in 'runtimepath' and below: > + :echo globpath(&rtp, "**/README.txt") +< Upwards search and limiting the depth of "**" is not + supported, thus using 'path' will not always work properly. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetExpr()->globpath(&rtp) +< + *has()* +has({feature}) Returns 1 if {feature} is supported, 0 otherwise. The + {feature} argument is a feature name like "nvim-0.2.1" or + "win32", see below. See also |exists()|. + + If the code has a syntax error, then Nvim may skip the rest + of the line and miss |:endif|. > + if has('feature') | let x = this->breaks->without->the->feature | endif +< + Put |:if| and |:endif| on separate lines to avoid the + syntax error. > + if has('feature') + let x = this->breaks->without->the->feature + endif +< + Vim's compile-time feature-names (prefixed with "+") are not + recognized because Nvim is always compiled with all possible + features. |feature-compile| + + Feature names can be: + 1. Nvim version. For example the "nvim-0.2.1" feature means + that Nvim is version 0.2.1 or later: > + :if has("nvim-0.2.1") + +< 2. Runtime condition or other pseudo-feature. For example the + "win32" feature checks if the current system is Windows: > + :if has("win32") +< *feature-list* + List of supported pseudo-feature names: + acl |ACL| support. + bsd BSD system (not macOS, use "mac" for that). + clipboard |clipboard| provider is available. + fname_case Case in file names matters (for Darwin and MS-Windows + this is not present). + iconv Can use |iconv()| for conversion. + linux Linux system. + mac MacOS system. + nvim This is Nvim. + python3 Legacy Vim |python3| interface. |has-python| + pythonx Legacy Vim |python_x| interface. |has-pythonx| + sun SunOS system. + ttyin input is a terminal (tty). + ttyout output is a terminal (tty). + unix Unix system. + *vim_starting* True during |startup|. + win32 Windows system (32 or 64 bit). + win64 Windows system (64 bit). + wsl WSL (Windows Subsystem for Linux) system. + + *has-patch* + 3. Vim patch. For example the "patch123" feature means that + Vim patch 123 at the current |v:version| was included: > + :if v:version > 602 || v:version == 602 && has("patch148") + +< 4. Vim version. For example the "patch-7.4.237" feature means + that Nvim is Vim-compatible to version 7.4.237 or later. > + :if has("patch-7.4.237") + + +has_key({dict}, {key}) *has_key()* + The result is a Number, which is TRUE if |Dictionary| {dict} + has an entry with key {key}. FALSE otherwise. The {key} + argument is a string. + + Can also be used as a |method|: > + mydict->has_key(key) + +haslocaldir([{winnr} [, {tabnr}]]) *haslocaldir()* + The result is a Number, which is 1 when the window has set a + local path via |:lcd| or when {winnr} is -1 and the tabpage + has set a local path via |:tcd|, otherwise 0. + + Tabs and windows are identified by their respective numbers, + 0 means current tab or window. Missing argument implies 0. + Thus the following are equivalent: > + haslocaldir() + haslocaldir(0) + haslocaldir(0, 0) +< With {winnr} use that window in the current tabpage. + With {winnr} and {tabnr} use the window in that tabpage. + {winnr} can be the window number or the |window-ID|. + If {winnr} is -1 it is ignored, only the tab is resolved. + Throw error if the arguments are invalid. |E5000| |E5001| |E5002| + + Can also be used as a |method|: > + GetWinnr()->haslocaldir() + +hasmapto({what} [, {mode} [, {abbr}]]) *hasmapto()* + The result is a Number, which is TRUE if there is a mapping + that contains {what} in somewhere in the rhs (what it is + mapped to) and this mapping exists in one of the modes + indicated by {mode}. + The arguments {what} and {mode} are strings. + When {abbr} is there and it is |TRUE| use abbreviations + instead of mappings. Don't forget to specify Insert and/or + Command-line mode. + Both the global mappings and the mappings local to the current + buffer are checked for a match. + If no matching mapping is found FALSE is returned. + The following characters are recognized in {mode}: + n Normal mode + v Visual and Select mode + x Visual mode + s Select mode + o Operator-pending mode + i Insert mode + l Language-Argument ("r", "f", "t", etc.) + c Command-line mode + When {mode} is omitted, "nvo" is used. + + This function is useful to check if a mapping already exists + to a function in a Vim script. Example: > + :if !hasmapto('\ABCdoit') + : map <Leader>d \ABCdoit + :endif +< This installs the mapping to "\ABCdoit" only if there isn't + already a mapping to "\ABCdoit". + + Can also be used as a |method|: > + GetRHS()->hasmapto() + +histadd({history}, {item}) *histadd()* + Add the String {item} to the history {history} which can be + one of: *hist-names* + "cmd" or ":" command line history + "search" or "/" search pattern history + "expr" or "=" typed expression history + "input" or "@" input line history + "debug" or ">" debug command history + empty the current or last used history + The {history} string does not need to be the whole name, one + character is sufficient. + If {item} does already exist in the history, it will be + shifted to become the newest entry. + The result is a Number: TRUE if the operation was successful, + otherwise FALSE is returned. + + Example: > + :call histadd("input", strftime("%Y %b %d")) + :let date=input("Enter date: ") +< This function is not available in the |sandbox|. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetHistory()->histadd('search') + +histdel({history} [, {item}]) *histdel()* + Clear {history}, i.e. delete all its entries. See |hist-names| + for the possible values of {history}. + + If the parameter {item} evaluates to a String, it is used as a + regular expression. All entries matching that expression will + be removed from the history (if there are any). + Upper/lowercase must match, unless "\c" is used |/\c|. + If {item} evaluates to a Number, it will be interpreted as + an index, see |:history-indexing|. The respective entry will + be removed if it exists. + + The result is TRUE for a successful operation, otherwise FALSE + is returned. + + Examples: + Clear expression register history: > + :call histdel("expr") +< + Remove all entries starting with "*" from the search history: > + :call histdel("/", '^\*') +< + The following three are equivalent: > + :call histdel("search", histnr("search")) + :call histdel("search", -1) + :call histdel("search", '^'.histget("search", -1).'$') +< + To delete the last search pattern and use the last-but-one for + the "n" command and 'hlsearch': > + :call histdel("search", -1) + :let @/ = histget("search", -1) +< + Can also be used as a |method|: > + GetHistory()->histdel() + +histget({history} [, {index}]) *histget()* + The result is a String, the entry with Number {index} from + {history}. See |hist-names| for the possible values of + {history}, and |:history-indexing| for {index}. If there is + no such entry, an empty String is returned. When {index} is + omitted, the most recent item from the history is used. + + Examples: + Redo the second last search from history. > + :execute '/' . histget("search", -2) + +< Define an Ex command ":H {num}" that supports re-execution of + the {num}th entry from the output of |:history|. > + :command -nargs=1 H execute histget("cmd", 0+<args>) +< + Can also be used as a |method|: > + GetHistory()->histget() + +histnr({history}) *histnr()* + The result is the Number of the current entry in {history}. + See |hist-names| for the possible values of {history}. + If an error occurred, -1 is returned. + + Example: > + :let inp_index = histnr("expr") + +< Can also be used as a |method|: > + GetHistory()->histnr() +< +hlexists({name}) *hlexists()* + The result is a Number, which is TRUE if a highlight group + called {name} exists. This is when the group has been + defined in some way. Not necessarily when highlighting has + been defined for it, it may also have been used for a syntax + item. + + Can also be used as a |method|: > + GetName()->hlexists() +< + *hlID()* +hlID({name}) The result is a Number, which is the ID of the highlight group + with name {name}. When the highlight group doesn't exist, + zero is returned. + This can be used to retrieve information about the highlight + group. For example, to get the background color of the + "Comment" group: > + :echo synIDattr(synIDtrans(hlID("Comment")), "bg") +< + Can also be used as a |method|: > + GetName()->hlID() + +hostname() *hostname()* + The result is a String, which is the name of the machine on + which Vim is currently running. Machine names greater than + 256 characters long are truncated. + +iconv({string}, {from}, {to}) *iconv()* + The result is a String, which is the text {string} converted + from encoding {from} to encoding {to}. + When the conversion completely fails an empty string is + returned. When some characters could not be converted they + are replaced with "?". + The encoding names are whatever the iconv() library function + can accept, see ":!man 3 iconv". + Note that Vim uses UTF-8 for all Unicode encodings, conversion + from/to UCS-2 is automatically changed to use UTF-8. You + cannot use UCS-2 in a string anyway, because of the NUL bytes. + + Can also be used as a |method|: > + GetText()->iconv('latin1', 'utf-8') +< + *indent()* +indent({lnum}) The result is a Number, which is indent of line {lnum} in the + current buffer. The indent is counted in spaces, the value + of 'tabstop' is relevant. {lnum} is used just like in + |getline()|. + When {lnum} is invalid -1 is returned. + + Can also be used as a |method|: > + GetLnum()->indent() + +index({object}, {expr} [, {start} [, {ic}]]) *index()* + If {object} is a |List| return the lowest index where the item + has a value equal to {expr}. There is no automatic + conversion, so the String "4" is different from the Number 4. + And the Number 4 is different from the Float 4.0. The value + of 'ignorecase' is not used here, case always matters. + + If {object} is a |Blob| return the lowest index where the byte + value is equal to {expr}. + + If {start} is given then start looking at the item with index + {start} (may be negative for an item relative to the end). + When {ic} is given and it is |TRUE|, ignore case. Otherwise + case must match. + -1 is returned when {expr} is not found in {object}. + Example: > + :let idx = index(words, "the") + :if index(numbers, 123) >= 0 + +< Can also be used as a |method|: > + GetObject()->index(what) + +input({prompt} [, {text} [, {completion}]]) *input()* +input({opts}) + The result is a String, which is whatever the user typed on + the command-line. The {prompt} argument is either a prompt + string, or a blank string (for no prompt). A '\n' can be used + in the prompt to start a new line. + + In the second form it accepts a single dictionary with the + following keys, any of which may be omitted: + + Key Default Description ~ + prompt "" Same as {prompt} in the first form. + default "" Same as {text} in the first form. + completion nothing Same as {completion} in the first form. + cancelreturn "" The value returned when the dialog is + cancelled. + highlight nothing Highlight handler: |Funcref|. + + The highlighting set with |:echohl| is used for the prompt. + The input is entered just like a command-line, with the same + editing commands and mappings. There is a separate history + for lines typed for input(). + Example: > + :if input("Coffee or beer? ") == "beer" + : echo "Cheers!" + :endif +< + If the optional {text} argument is present and not empty, this + is used for the default reply, as if the user typed this. + Example: > + :let color = input("Color? ", "white") + +< The optional {completion} argument specifies the type of + completion supported for the input. Without it completion is + not performed. The supported completion types are the same as + that can be supplied to a user-defined command using the + "-complete=" argument. Refer to |:command-completion| for + more information. Example: > + let fname = input("File: ", "", "file") + +< *input()-highlight* *E5400* *E5402* + The optional `highlight` key allows specifying function which + will be used for highlighting user input. This function + receives user input as its only argument and must return + a list of 3-tuples [hl_start_col, hl_end_col + 1, hl_group] + where + hl_start_col is the first highlighted column, + hl_end_col is the last highlighted column (+ 1!), + hl_group is |:hi| group used for highlighting. + *E5403* *E5404* *E5405* *E5406* + Both hl_start_col and hl_end_col + 1 must point to the start + of the multibyte character (highlighting must not break + multibyte characters), hl_end_col + 1 may be equal to the + input length. Start column must be in range [0, len(input)), + end column must be in range (hl_start_col, len(input)], + sections must be ordered so that next hl_start_col is greater + then or equal to previous hl_end_col. + + Example (try some input with parentheses): > + highlight RBP1 guibg=Red ctermbg=red + highlight RBP2 guibg=Yellow ctermbg=yellow + highlight RBP3 guibg=Green ctermbg=green + highlight RBP4 guibg=Blue ctermbg=blue + let g:rainbow_levels = 4 + function! RainbowParens(cmdline) + let ret = [] + let i = 0 + let lvl = 0 + while i < len(a:cmdline) + if a:cmdline[i] is# '(' + call add(ret, [i, i + 1, 'RBP' . ((lvl % g:rainbow_levels) + 1)]) + let lvl += 1 + elseif a:cmdline[i] is# ')' + let lvl -= 1 + call add(ret, [i, i + 1, 'RBP' . ((lvl % g:rainbow_levels) + 1)]) + endif + let i += 1 + endwhile + return ret + endfunction + call input({'prompt':'>','highlight':'RainbowParens'}) +< + Highlight function is called at least once for each new + displayed input string, before command-line is redrawn. It is + expected that function is pure for the duration of one input() + call, i.e. it produces the same output for the same input, so + output may be memoized. Function is run like under |:silent| + modifier. If the function causes any errors, it will be + skipped for the duration of the current input() call. + + Highlighting is disabled if command-line contains arabic + characters. + + NOTE: This function must not be used in a startup file, for + the versions that only run in GUI mode (e.g., the Win32 GUI). + Note: When input() is called from within a mapping it will + consume remaining characters from that mapping, because a + mapping is handled like the characters were typed. + Use |inputsave()| before input() and |inputrestore()| + after input() to avoid that. Another solution is to avoid + that further characters follow in the mapping, e.g., by using + |:execute| or |:normal|. + + Example with a mapping: > + :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR> + :function GetFoo() + : call inputsave() + : let g:Foo = input("enter search pattern: ") + : call inputrestore() + :endfunction + +< Can also be used as a |method|: > + GetPrompt()->input() + +inputlist({textlist}) *inputlist()* + {textlist} must be a |List| of strings. This |List| is + displayed, one string per line. The user will be prompted to + enter a number, which is returned. + The user can also select an item by clicking on it with the + mouse, if the mouse is enabled in the command line ('mouse' is + "a" or includes "c"). For the first string 0 is returned. + When clicking above the first item a negative number is + returned. When clicking on the prompt one more than the + length of {textlist} is returned. + Make sure {textlist} has less than 'lines' entries, otherwise + it won't work. It's a good idea to put the entry number at + the start of the string. And put a prompt in the first item. + Example: > + let color = inputlist(['Select color:', '1. red', + \ '2. green', '3. blue']) + +< Can also be used as a |method|: > + GetChoices()->inputlist() + +inputrestore() *inputrestore()* + Restore typeahead that was saved with a previous |inputsave()|. + Should be called the same number of times inputsave() is + called. Calling it more often is harmless though. + Returns TRUE when there is nothing to restore, FALSE otherwise. + +inputsave() *inputsave()* + Preserve typeahead (also from mappings) and clear it, so that + a following prompt gets input from the user. Should be + followed by a matching inputrestore() after the prompt. Can + be used several times, in which case there must be just as + many inputrestore() calls. + Returns TRUE when out of memory, FALSE otherwise. + +inputsecret({prompt} [, {text}]) *inputsecret()* + This function acts much like the |input()| function with but + two exceptions: + a) the user's response will be displayed as a sequence of + asterisks ("*") thereby keeping the entry secret, and + b) the user's response will not be recorded on the input + |history| stack. + The result is a String, which is whatever the user actually + typed on the command-line in response to the issued prompt. + NOTE: Command-line completion is not supported. + + Can also be used as a |method|: > + GetPrompt()->inputsecret() + +insert({object}, {item} [, {idx}]) *insert()* + When {object} is a |List| or a |Blob| insert {item} at the start + of it. + + If {idx} is specified insert {item} before the item with index + {idx}. If {idx} is zero it goes before the first item, just + like omitting {idx}. A negative {idx} is also possible, see + |list-index|. -1 inserts just before the last item. + + Returns the resulting |List| or |Blob|. Examples: > + :let mylist = insert([2, 3, 5], 1) + :call insert(mylist, 4, -1) + :call insert(mylist, 6, len(mylist)) +< The last example can be done simpler with |add()|. + Note that when {item} is a |List| it is inserted as a single + item. Use |extend()| to concatenate |Lists|. + + Can also be used as a |method|: > + mylist->insert(item) + +interrupt() *interrupt()* + Interrupt script execution. It works more or less like the + user typing CTRL-C, most commands won't execute and control + returns to the user. This is useful to abort execution + from lower down, e.g. in an autocommand. Example: > + :function s:check_typoname(file) + : if fnamemodify(a:file, ':t') == '[' + : echomsg 'Maybe typo' + : call interrupt() + : endif + :endfunction + :au BufWritePre * call s:check_typoname(expand('<amatch>')) + +invert({expr}) *invert()* + Bitwise invert. The argument is converted to a number. A + List, Dict or Float argument causes an error. Example: > + :let bits = invert(bits) +< Can also be used as a |method|: > + :let bits = bits->invert() + +isdirectory({directory}) *isdirectory()* + The result is a Number, which is |TRUE| when a directory + with the name {directory} exists. If {directory} doesn't + exist, or isn't a directory, the result is |FALSE|. {directory} + is any expression, which is used as a String. + + Can also be used as a |method|: > + GetName()->isdirectory() + +isinf({expr}) *isinf()* + Return 1 if {expr} is a positive infinity, or -1 a negative + infinity, otherwise 0. > + :echo isinf(1.0 / 0.0) +< 1 > + :echo isinf(-1.0 / 0.0) +< -1 + + Can also be used as a |method|: > + Compute()->isinf() + +islocked({expr}) *islocked()* *E786* + The result is a Number, which is |TRUE| when {expr} is the + name of a locked variable. + The string argument {expr} must be the name of a variable, + |List| item or |Dictionary| entry, not the variable itself! + Example: > + :let alist = [0, ['a', 'b'], 2, 3] + :lockvar 1 alist + :echo islocked('alist') " 1 + :echo islocked('alist[1]') " 0 + +< When {expr} is a variable that does not exist you get an error + message. Use |exists()| to check for existence. + + Can also be used as a |method|: > + GetName()->islocked() + +id({expr}) *id()* + Returns a |String| which is a unique identifier of the + container type (|List|, |Dict|, |Blob| and |Partial|). It is + guaranteed that for the mentioned types `id(v1) ==# id(v2)` + returns true iff `type(v1) == type(v2) && v1 is v2`. + Note that |v:_null_string|, |v:_null_list|, |v:_null_dict| and + |v:_null_blob| have the same `id()` with different types + because they are internally represented as NULL pointers. + `id()` returns a hexadecimal representanion of the pointers to + the containers (i.e. like `0x994a40`), same as `printf("%p", + {expr})`, but it is advised against counting on the exact + format of the return value. + + It is not guaranteed that `id(no_longer_existing_container)` + will not be equal to some other `id()`: new containers may + reuse identifiers of the garbage-collected ones. + +items({dict}) *items()* + Return a |List| with all the key-value pairs of {dict}. Each + |List| item is a list with two items: the key of a {dict} + entry and the value of this entry. The |List| is in arbitrary + order. Also see |keys()| and |values()|. + Example: > + for [key, value] in items(mydict) + echo key . ': ' . value + endfor + +< Can also be used as a |method|: > + mydict->items() + +isnan({expr}) *isnan()* + Return |TRUE| if {expr} is a float with value NaN. > + echo isnan(0.0 / 0.0) +< 1 + + Can also be used as a |method|: > + Compute()->isnan() + +jobpid({job}) *jobpid()* + Return the PID (process id) of |job-id| {job}. + +jobresize({job}, {width}, {height}) *jobresize()* + Resize the pseudo terminal window of |job-id| {job} to {width} + columns and {height} rows. + Fails if the job was not started with `"pty":v:true`. + +jobstart({cmd} [, {opts}]) *jobstart()* + Spawns {cmd} as a job. + If {cmd} is a List it runs directly (no 'shell'). + If {cmd} is a String it runs in the 'shell', like this: > + :call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}']) +< (See |shell-unquoting| for details.) + + Example: > + :call jobstart('nvim -h', {'on_stdout':{j,d,e->append(line('.'),d)}}) +< + Returns |job-id| on success, 0 on invalid arguments (or job + table is full), -1 if {cmd}[0] or 'shell' is not executable. + The returned job-id is a valid |channel-id| representing the + job's stdio streams. Use |chansend()| (or |rpcnotify()| and + |rpcrequest()| if "rpc" was enabled) to send data to stdin and + |chanclose()| to close the streams without stopping the job. + + See |job-control| and |RPC|. + + NOTE: on Windows if {cmd} is a List: + - cmd[0] must be an executable (not a "built-in"). If it is + in $PATH it can be called by name, without an extension: > + :call jobstart(['ping', 'neovim.io']) +< If it is a full or partial path, extension is required: > + :call jobstart(['System32\ping.exe', 'neovim.io']) +< - {cmd} is collapsed to a string of quoted args as expected + by CommandLineToArgvW https://msdn.microsoft.com/bb776391 + unless cmd[0] is some form of "cmd.exe". + + *jobstart-options* + {opts} is a dictionary with these keys: + clear_env: (boolean) `env` defines the job environment + exactly, instead of merging current environment. + cwd: (string, default=|current-directory|) Working + directory of the job. + detach: (boolean) Detach the job process: it will not be + killed when Nvim exits. If the process exits + before Nvim, `on_exit` will be invoked. + env: (dict) Map of environment variable name:value + pairs extending (or replacing if |clear_env|) + the current environment. + height: (number) Height of the `pty` terminal. + |on_exit|: (function) Callback invoked when the job exits. + |on_stdout|: (function) Callback invoked when the job emits + stdout data. + |on_stderr|: (function) Callback invoked when the job emits + stderr data. + overlapped: (boolean) Set FILE_FLAG_OVERLAPPED for the + standard input/output passed to the child process. + Normally you do not need to set this. + (Only available on MS-Windows, On other + platforms, this option is silently ignored.) + pty: (boolean) Connect the job to a new pseudo + terminal, and its streams to the master file + descriptor. Then `on_stderr` is ignored, + `on_stdout` receives all output. + rpc: (boolean) Use |msgpack-rpc| to communicate with + the job over stdio. Then `on_stdout` is ignored, + but `on_stderr` can still be used. + stderr_buffered: (boolean) Collect data until EOF (stream closed) + before invoking `on_stderr`. |channel-buffered| + stdout_buffered: (boolean) Collect data until EOF (stream + closed) before invoking `on_stdout`. |channel-buffered| + stdin: (string) Either "pipe" (default) to connect the + job's stdin to a channel or "null" to disconnect + stdin. + width: (number) Width of the `pty` terminal. + + {opts} is passed as |self| dictionary to the callback; the + caller may set other keys to pass application-specific data. + + Returns: + - |channel-id| on success + - 0 on invalid arguments + - -1 if {cmd}[0] is not executable. + See also |job-control|, |channel|, |msgpack-rpc|. + +jobstop({id}) *jobstop()* + Stop |job-id| {id} by sending SIGTERM to the job process. If + the process does not terminate after a timeout then SIGKILL + will be sent. When the job terminates its |on_exit| handler + (if any) will be invoked. + See |job-control|. + + Returns 1 for valid job id, 0 for invalid id, including jobs have + exited or stopped. + +jobwait({jobs} [, {timeout}]) *jobwait()* + Waits for jobs and their |on_exit| handlers to complete. + + {jobs} is a List of |job-id|s to wait for. + {timeout} is the maximum waiting time in milliseconds. If + omitted or -1, wait forever. + + Timeout of 0 can be used to check the status of a job: > + let running = jobwait([{job-id}], 0)[0] == -1 +< + During jobwait() callbacks for jobs not in the {jobs} list may + be invoked. The screen will not redraw unless |:redraw| is + invoked by a callback. + + Returns a list of len({jobs}) integers, where each integer is + the status of the corresponding job: + Exit-code, if the job exited + -1 if the timeout was exceeded + -2 if the job was interrupted (by |CTRL-C|) + -3 if the job-id is invalid + +join({list} [, {sep}]) *join()* + Join the items in {list} together into one String. + When {sep} is specified it is put in between the items. If + {sep} is omitted a single space is used. + Note that {sep} is not added at the end. You might want to + add it there too: > + let lines = join(mylist, "\n") . "\n" +< String items are used as-is. |Lists| and |Dictionaries| are + converted into a string like with |string()|. + The opposite function is |split()|. + + Can also be used as a |method|: > + mylist->join() + +json_decode({expr}) *json_decode()* + Convert {expr} from JSON object. Accepts |readfile()|-style + list as the input, as well as regular string. May output any + Vim value. In the following cases it will output + |msgpack-special-dict|: + 1. Dictionary contains duplicate key. + 2. Dictionary contains empty key. + 3. String contains NUL byte. Two special dictionaries: for + dictionary and for string will be emitted in case string + with NUL byte was a dictionary key. + + Note: function treats its input as UTF-8 always. The JSON + standard allows only a few encodings, of which UTF-8 is + recommended and the only one required to be supported. + Non-UTF-8 characters are an error. + + Can also be used as a |method|: > + ReadObject()->json_decode() + +json_encode({expr}) *json_encode()* + Convert {expr} into a JSON string. Accepts + |msgpack-special-dict| as the input. Will not convert + |Funcref|s, mappings with non-string keys (can be created as + |msgpack-special-dict|), values with self-referencing + containers, strings which contain non-UTF-8 characters, + pseudo-UTF-8 strings which contain codepoints reserved for + surrogate pairs (such strings are not valid UTF-8 strings). + Non-printable characters are converted into "\u1234" escapes + or special escapes like "\t", other are dumped as-is. + |Blob|s are converted to arrays of the individual bytes. + + Can also be used as a |method|: > + GetObject()->json_encode() + +keys({dict}) *keys()* + Return a |List| with all the keys of {dict}. The |List| is in + arbitrary order. Also see |items()| and |values()|. + + Can also be used as a |method|: > + mydict->keys() + +< *len()* *E701* +len({expr}) The result is a Number, which is the length of the argument. + When {expr} is a String or a Number the length in bytes is + used, as with |strlen()|. + When {expr} is a |List| the number of items in the |List| is + returned. + When {expr} is a |Blob| the number of bytes is returned. + When {expr} is a |Dictionary| the number of entries in the + |Dictionary| is returned. + Otherwise an error is given. + + Can also be used as a |method|: > + mylist->len() + +< *libcall()* *E364* *E368* +libcall({libname}, {funcname}, {argument}) + Call function {funcname} in the run-time library {libname} + with single argument {argument}. + This is useful to call functions in a library that you + especially made to be used with Vim. Since only one argument + is possible, calling standard library functions is rather + limited. + The result is the String returned by the function. If the + function returns NULL, this will appear as an empty string "" + to Vim. + If the function returns a number, use libcallnr()! + If {argument} is a number, it is passed to the function as an + int; if {argument} is a string, it is passed as a + null-terminated string. + + libcall() allows you to write your own 'plug-in' extensions to + Vim without having to recompile the program. It is NOT a + means to call system functions! If you try to do so Vim will + very probably crash. + + For Win32, the functions you write must be placed in a DLL + and use the normal C calling convention (NOT Pascal which is + used in Windows System DLLs). The function must take exactly + one parameter, either a character pointer or a long integer, + and must return a character pointer or NULL. The character + pointer returned must point to memory that will remain valid + after the function has returned (e.g. in static data in the + DLL). If it points to allocated memory, that memory will + leak away. Using a static buffer in the function should work, + it's then freed when the DLL is unloaded. + + WARNING: If the function returns a non-valid pointer, Vim may + crash! This also happens if the function returns a number, + because Vim thinks it's a pointer. + For Win32 systems, {libname} should be the filename of the DLL + without the ".DLL" suffix. A full path is only required if + the DLL is not in the usual places. + For Unix: When compiling your own plugins, remember that the + object code must be compiled as position-independent ('PIC'). + Examples: > + :echo libcall("libc.so", "getenv", "HOME") + +< Can also be used as a |method|, the base is passed as the + third argument: > + GetValue()->libcall("libc.so", "getenv") +< + *libcallnr()* +libcallnr({libname}, {funcname}, {argument}) + Just like |libcall()|, but used for a function that returns an + int instead of a string. + Examples: > + :echo libcallnr("/usr/lib/libc.so", "getpid", "") + :call libcallnr("libc.so", "printf", "Hello World!\n") + :call libcallnr("libc.so", "sleep", 10) +< + Can also be used as a |method|, the base is passed as the + third argument: > + GetValue()->libcallnr("libc.so", "printf") +< +line({expr} [, {winid}]) *line()* + The result is a Number, which is the line number of the file + position given with {expr}. The {expr} argument is a string. + The accepted positions are: + . the cursor position + $ the last line in the current buffer + 'x position of mark x (if the mark is not set, 0 is + returned) + w0 first line visible in current window (one if the + display isn't updated, e.g. in silent Ex mode) + w$ last line visible in current window (this is one + less than "w0" if no lines are visible) + v In Visual mode: the start of the Visual area (the + cursor is the end). When not in Visual mode + returns the cursor position. Differs from |'<| in + that it's updated right away. + Note that a mark in another file can be used. The line number + then applies to another buffer. + To get the column number use |col()|. To get both use + |getpos()|. + With the optional {winid} argument the values are obtained for + that window instead of the current window. + Examples: > + line(".") line number of the cursor + line(".", winid) idem, in window "winid" + line("'t") line number of mark t + line("'" . marker) line number of mark marker +< + To jump to the last known position when opening a file see + |last-position-jump|. + + Can also be used as a |method|: > + GetValue()->line() + +line2byte({lnum}) *line2byte()* + Return the byte count from the start of the buffer for line + {lnum}. This includes the end-of-line character, depending on + the 'fileformat' option for the current buffer. The first + line returns 1. UTF-8 encoding is used, 'fileencoding' is + ignored. This can also be used to get the byte count for the + line just below the last line: > + line2byte(line("$") + 1) +< This is the buffer size plus one. If 'fileencoding' is empty + it is the file size plus one. {lnum} is used like with + |getline()|. When {lnum} is invalid -1 is returned. + Also see |byte2line()|, |go| and |:goto|. + + Can also be used as a |method|: > + GetLnum()->line2byte() + +lispindent({lnum}) *lispindent()* + Get the amount of indent for line {lnum} according the lisp + indenting rules, as with 'lisp'. + The indent is counted in spaces, the value of 'tabstop' is + relevant. {lnum} is used just like in |getline()|. + When {lnum} is invalid, -1 is returned. + + Can also be used as a |method|: > + GetLnum()->lispindent() + +list2str({list} [, {utf8}]) *list2str()* + Convert each number in {list} to a character string can + concatenate them all. Examples: > + list2str([32]) returns " " + list2str([65, 66, 67]) returns "ABC" +< The same can be done (slowly) with: > + join(map(list, {nr, val -> nr2char(val)}), '') +< |str2list()| does the opposite. + + UTF-8 encoding is always used, {utf8} option has no effect, + and exists only for backwards-compatibility. + With UTF-8 composing characters work as expected: > + list2str([97, 769]) returns "á" +< + Can also be used as a |method|: > + GetList()->list2str() + +localtime() *localtime()* + Return the current time, measured as seconds since 1st Jan + 1970. See also |strftime()|, |strptime()| and |getftime()|. + + +log({expr}) *log()* + Return the natural logarithm (base e) of {expr} as a |Float|. + {expr} must evaluate to a |Float| or a |Number| in the range + (0, inf]. + Examples: > + :echo log(10) +< 2.302585 > + :echo log(exp(5)) +< 5.0 + + Can also be used as a |method|: > + Compute()->log() + +log10({expr}) *log10()* + Return the logarithm of Float {expr} to base 10 as a |Float|. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo log10(1000) +< 3.0 > + :echo log10(0.01) +< -2.0 + + Can also be used as a |method|: > + Compute()->log10() + +luaeval({expr} [, {expr}]) + Evaluate Lua expression {expr} and return its result converted + to Vim data structures. See |lua-eval| for more details. + + Can also be used as a |method|: > + GetExpr()->luaeval() + +map({expr1}, {expr2}) *map()* + {expr1} must be a |List|, |Blob| or |Dictionary|. + Replace each item in {expr1} with the result of evaluating + {expr2}. For a |Blob| each byte is replaced. + + {expr2} must be a |string| or |Funcref|. + + If {expr2} is a |string|, inside {expr2} |v:val| has the value + of the current item. For a |Dictionary| |v:key| has the key + of the current item and for a |List| |v:key| has the index of + the current item. For a |Blob| |v:key| has the index of the + current byte. + Example: > + :call map(mylist, '"> " . v:val . " <"') +< This puts "> " before and " <" after each item in "mylist". + + Note that {expr2} is the result of an expression and is then + used as an expression again. Often it is good to use a + |literal-string| to avoid having to double backslashes. You + still have to double ' quotes + + If {expr2} is a |Funcref| it is called with two arguments: + 1. The key or the index of the current item. + 2. the value of the current item. + The function must return the new value of the item. Example + that changes each value by "key-value": > + func KeyValue(key, val) + return a:key . '-' . a:val + endfunc + call map(myDict, function('KeyValue')) +< It is shorter when using a |lambda|: > + call map(myDict, {key, val -> key . '-' . val}) +< If you do not use "val" you can leave it out: > + call map(myDict, {key -> 'item: ' . key}) +< If you do not use "key" you can use a short name: > + call map(myDict, {_, val -> 'item: ' . val}) +< + The operation is done in-place. If you want a |List| or + |Dictionary| to remain unmodified make a copy first: > + :let tlist = map(copy(mylist), ' v:val . "\t"') + +< Returns {expr1}, the |List|, |Blob| or |Dictionary| that was + filtered. When an error is encountered while evaluating + {expr2} no further items in {expr1} are processed. When + {expr2} is a Funcref errors inside a function are ignored, + unless it was defined with the "abort" flag. + + Can also be used as a |method|: > + mylist->map(expr2) + +maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* + When {dict} is omitted or zero: Return the rhs of mapping + {name} in mode {mode}. The returned String has special + characters translated like in the output of the ":map" command + listing. + + When there is no mapping for {name}, an empty String is + returned. When the mapping for {name} is empty, then "<Nop>" + is returned. + + The {name} can have special key names, like in the ":map" + command. + + {mode} can be one of these strings: + "n" Normal + "v" Visual (including Select) + "o" Operator-pending + "i" Insert + "c" Cmd-line + "s" Select + "x" Visual + "l" langmap |language-mapping| + "t" Terminal + "" Normal, Visual and Operator-pending + When {mode} is omitted, the modes for "" are used. + + When {abbr} is there and it is |TRUE| use abbreviations + instead of mappings. + + When {dict} is there and it is |TRUE| return a dictionary + containing all the information of the mapping with the + following items: + "lhs" The {lhs} of the mapping. + "rhs" The {rhs} of the mapping as typed. + "silent" 1 for a |:map-silent| mapping, else 0. + "noremap" 1 if the {rhs} of the mapping is not remappable. + "script" 1 if mapping was defined with <script>. + "expr" 1 for an expression mapping (|:map-<expr>|). + "buffer" 1 for a buffer local mapping (|:map-local|). + "mode" Modes for which the mapping is defined. In + addition to the modes mentioned above, these + characters will be used: + " " Normal, Visual and Operator-pending + "!" Insert and Commandline mode + (|mapmode-ic|) + "sid" The script local ID, used for <sid> mappings + (|<SID>|). + "lnum" The line number in "sid", zero if unknown. + "nowait" Do not wait for other, longer mappings. + (|:map-<nowait>|). + + The mappings local to the current buffer are checked first, + then the global mappings. + This function can be used to map a key even when it's already + mapped, and have it do the original mapping too. Sketch: > + exe 'nnoremap <Tab> ==' . maparg('<Tab>', 'n') + +< Can also be used as a |method|: > + GetKey()->maparg('n') + +mapcheck({name} [, {mode} [, {abbr}]]) *mapcheck()* + Check if there is a mapping that matches with {name} in mode + {mode}. See |maparg()| for {mode} and special names in + {name}. + When {abbr} is there and it is non-zero use abbreviations + instead of mappings. + A match happens with a mapping that starts with {name} and + with a mapping which is equal to the start of {name}. + + matches mapping "a" "ab" "abc" ~ + mapcheck("a") yes yes yes + mapcheck("abc") yes yes yes + mapcheck("ax") yes no no + mapcheck("b") no no no + + The difference with maparg() is that mapcheck() finds a + mapping that matches with {name}, while maparg() only finds a + mapping for {name} exactly. + When there is no mapping that starts with {name}, an empty + String is returned. If there is one, the RHS of that mapping + is returned. If there are several mappings that start with + {name}, the RHS of one of them is returned. This will be + "<Nop>" if the RHS is empty. + The mappings local to the current buffer are checked first, + then the global mappings. + This function can be used to check if a mapping can be added + without being ambiguous. Example: > + :if mapcheck("_vv") == "" + : map _vv :set guifont=7x13<CR> + :endif +< This avoids adding the "_vv" mapping when there already is a + mapping for "_v" or for "_vvv". + + Can also be used as a |method|: > + GetKey()->mapcheck('n') + +match({expr}, {pat} [, {start} [, {count}]]) *match()* + When {expr} is a |List| then this returns the index of the + first item where {pat} matches. Each item is used as a + String, |Lists| and |Dictionaries| are used as echoed. + + Otherwise, {expr} is used as a String. The result is a + Number, which gives the index (byte offset) in {expr} where + {pat} matches. + + A match at the first character or |List| item returns zero. + If there is no match -1 is returned. + + For getting submatches see |matchlist()|. + Example: > + :echo match("testing", "ing") " results in 4 + :echo match([1, 'x'], '\a') " results in 1 +< See |string-match| for how {pat} is used. + *strpbrk()* + Vim doesn't have a strpbrk() function. But you can do: > + :let sepidx = match(line, '[.,;: \t]') +< *strcasestr()* + Vim doesn't have a strcasestr() function. But you can add + "\c" to the pattern to ignore case: > + :let idx = match(haystack, '\cneedle') +< + If {start} is given, the search starts from byte index + {start} in a String or item {start} in a |List|. + The result, however, is still the index counted from the + first character/item. Example: > + :echo match("testing", "ing", 2) +< result is again "4". > + :echo match("testing", "ing", 4) +< result is again "4". > + :echo match("testing", "t", 2) +< result is "3". + For a String, if {start} > 0 then it is like the string starts + {start} bytes later, thus "^" will match at {start}. Except + when {count} is given, then it's like matches before the + {start} byte are ignored (this is a bit complicated to keep it + backwards compatible). + For a String, if {start} < 0, it will be set to 0. For a list + the index is counted from the end. + If {start} is out of range ({start} > strlen({expr}) for a + String or {start} > len({expr}) for a |List|) -1 is returned. + + When {count} is given use the {count}'th match. When a match + is found in a String the search for the next one starts one + character further. Thus this example results in 1: > + echo match("testing", "..", 0, 2) +< In a |List| the search continues in the next item. + Note that when {count} is added the way {start} works changes, + see above. + + See |pattern| for the patterns that are accepted. + The 'ignorecase' option is used to set the ignore-caseness of + the pattern. 'smartcase' is NOT used. The matching is always + done like 'magic' is set and 'cpoptions' is empty. + Note that a match at the start is preferred, thus when the + pattern is using "*" (any number of matches) it tends to find + zero matches at the start instead of a number of matches + further down in the text. + + Can also be used as a |method|: > + GetText()->match('word') + GetList()->match('word') +< + *matchadd()* *E798* *E799* *E801* *E957* +matchadd({group}, {pattern} [, {priority} [, {id} [, {dict}]]]) + Defines a pattern to be highlighted in the current window (a + "match"). It will be highlighted with {group}. Returns an + identification number (ID), which can be used to delete the + match using |matchdelete()|. The ID is bound to the window. + Matching is case sensitive and magic, unless case sensitivity + or magicness are explicitly overridden in {pattern}. The + 'magic', 'smartcase' and 'ignorecase' options are not used. + The "Conceal" value is special, it causes the match to be + concealed. + + The optional {priority} argument assigns a priority to the + match. A match with a high priority will have its + highlighting overrule that of a match with a lower priority. + A priority is specified as an integer (negative numbers are no + exception). If the {priority} argument is not specified, the + default priority is 10. The priority of 'hlsearch' is zero, + hence all matches with a priority greater than zero will + overrule it. Syntax highlighting (see 'syntax') is a separate + mechanism, and regardless of the chosen priority a match will + always overrule syntax highlighting. + + The optional {id} argument allows the request for a specific + match ID. If a specified ID is already taken, an error + message will appear and the match will not be added. An ID + is specified as a positive integer (zero excluded). IDs 1, 2 + and 3 are reserved for |:match|, |:2match| and |:3match|, + respectively. If the {id} argument is not specified or -1, + |matchadd()| automatically chooses a free ID. + + The optional {dict} argument allows for further custom + values. Currently this is used to specify a match specific + conceal character that will be shown for |hl-Conceal| + highlighted matches. The dict can have the following members: + + conceal Special character to show instead of the + match (only for |hl-Conceal| highlighed + matches, see |:syn-cchar|) + window Instead of the current window use the + window with this number or window ID. + + The number of matches is not limited, as it is the case with + the |:match| commands. + + Example: > + :highlight MyGroup ctermbg=green guibg=green + :let m = matchadd("MyGroup", "TODO") +< Deletion of the pattern: > + :call matchdelete(m) + +< A list of matches defined by |matchadd()| and |:match| are + available from |getmatches()|. All matches can be deleted in + one operation by |clearmatches()|. + + Can also be used as a |method|: > + GetGroup()->matchadd('TODO') +< + *matchaddpos()* +matchaddpos({group}, {pos} [, {priority} [, {id} [, {dict}]]]) + Same as |matchadd()|, but requires a list of positions {pos} + instead of a pattern. This command is faster than |matchadd()| + because it does not require to handle regular expressions and + sets buffer line boundaries to redraw screen. It is supposed + to be used when fast match additions and deletions are + required, for example to highlight matching parentheses. + *E5030* *E5031* + {pos} is a list of positions. Each position can be one of + these: + - A number. This whole line will be highlighted. The first + line has number 1. + - A list with one number, e.g., [23]. The whole line with this + number will be highlighted. + - A list with two numbers, e.g., [23, 11]. The first number is + the line number, the second one is the column number (first + column is 1, the value must correspond to the byte index as + |col()| would return). The character at this position will + be highlighted. + - A list with three numbers, e.g., [23, 11, 3]. As above, but + the third number gives the length of the highlight in bytes. + + Entries with zero and negative line numbers are silently + ignored, as well as entries with negative column numbers and + lengths. + + The maximum number of positions in {pos} is 8. + + Example: > + :highlight MyGroup ctermbg=green guibg=green + :let m = matchaddpos("MyGroup", [[23, 24], 34]) +< Deletion of the pattern: > + :call matchdelete(m) + +< Matches added by |matchaddpos()| are returned by + |getmatches()|. + + Can also be used as a |method|: > + GetGroup()->matchaddpos([23, 11]) + +matcharg({nr}) *matcharg()* + Selects the {nr} match item, as set with a |:match|, + |:2match| or |:3match| command. + Return a |List| with two elements: + The name of the highlight group used + The pattern used. + When {nr} is not 1, 2 or 3 returns an empty |List|. + When there is no match item set returns ['', '']. + This is useful to save and restore a |:match|. + Highlighting matches using the |:match| commands are limited + to three matches. |matchadd()| does not have this limitation. + + Can also be used as a |method|: > + GetMatch()->matcharg() + +matchdelete({id} [, {win}]) *matchdelete()* *E802* *E803* + Deletes a match with ID {id} previously defined by |matchadd()| + or one of the |:match| commands. Returns 0 if successful, + otherwise -1. See example for |matchadd()|. All matches can + be deleted in one operation by |clearmatches()|. + If {win} is specified, use the window with this number or + window ID instead of the current window. + + Can also be used as a |method|: > + GetMatch()->matchdelete() + +matchend({expr}, {pat} [, {start} [, {count}]]) *matchend()* + Same as |match()|, but return the index of first character + after the match. Example: > + :echo matchend("testing", "ing") +< results in "7". + *strspn()* *strcspn()* + Vim doesn't have a strspn() or strcspn() function, but you can + do it with matchend(): > + :let span = matchend(line, '[a-zA-Z]') + :let span = matchend(line, '[^a-zA-Z]') +< Except that -1 is returned when there are no matches. + + The {start}, if given, has the same meaning as for |match()|. > + :echo matchend("testing", "ing", 2) +< results in "7". > + :echo matchend("testing", "ing", 5) +< result is "-1". + When {expr} is a |List| the result is equal to |match()|. + + Can also be used as a |method|: > + GetText()->matchend('word') + +matchfuzzy({list}, {str} [, {dict}]) *matchfuzzy()* + If {list} is a list of strings, then returns a |List| with all + the strings in {list} that fuzzy match {str}. The strings in + the returned list are sorted based on the matching score. + + The optional {dict} argument always supports the following + items: + matchseq When this item is present and {str} contains + multiple words separated by white space, then + returns only matches that contain the words in + the given sequence. + + If {list} is a list of dictionaries, then the optional {dict} + argument supports the following additional items: + key key of the item which is fuzzy matched against + {str}. The value of this item should be a + string. + text_cb |Funcref| that will be called for every item + in {list} to get the text for fuzzy matching. + This should accept a dictionary item as the + argument and return the text for that item to + use for fuzzy matching. + + {str} is treated as a literal string and regular expression + matching is NOT supported. The maximum supported {str} length + is 256. + + When {str} has multiple words each separated by white space, + then the list of strings that have all the words is returned. + + If there are no matching strings or there is an error, then an + empty list is returned. If length of {str} is greater than + 256, then returns an empty list. + + Refer to |fuzzy-match| for more information about fuzzy + matching strings. + + Example: > + :echo matchfuzzy(["clay", "crow"], "cay") +< results in ["clay"]. > + :echo getbufinfo()->map({_, v -> v.name})->matchfuzzy("ndl") +< results in a list of buffer names fuzzy matching "ndl". > + :echo getbufinfo()->matchfuzzy("ndl", {'key' : 'name'}) +< results in a list of buffer information dicts with buffer + names fuzzy matching "ndl". > + :echo getbufinfo()->matchfuzzy("spl", + \ {'text_cb' : {v -> v.name}}) +< results in a list of buffer information dicts with buffer + names fuzzy matching "spl". > + :echo v:oldfiles->matchfuzzy("test") +< results in a list of file names fuzzy matching "test". > + :let l = readfile("buffer.c")->matchfuzzy("str") +< results in a list of lines in "buffer.c" fuzzy matching "str". > + :echo ['one two', 'two one']->matchfuzzy('two one') +< results in ['two one', 'one two']. > + :echo ['one two', 'two one']->matchfuzzy('two one', + \ {'matchseq': 1}) +< results in ['two one']. + +matchfuzzypos({list}, {str} [, {dict}]) *matchfuzzypos()* + Same as |matchfuzzy()|, but returns the list of matched + strings, the list of character positions where characters + in {str} matches and a list of matching scores. You can + use |byteidx()| to convert a character position to a byte + position. + + If {str} matches multiple times in a string, then only the + positions for the best match is returned. + + If there are no matching strings or there is an error, then a + list with three empty list items is returned. + + Example: > + :echo matchfuzzypos(['testing'], 'tsg') +< results in [['testing'], [[0, 2, 6]], [99]] > + :echo matchfuzzypos(['clay', 'lacy'], 'la') +< results in [['lacy', 'clay'], [[0, 1], [1, 2]], [153, 133]] > + :echo [{'text': 'hello', 'id' : 10}] + \ ->matchfuzzypos('ll', {'key' : 'text'}) +< results in [[{'id': 10, 'text': 'hello'}], [[2, 3]], [127]] + +matchlist({expr}, {pat} [, {start} [, {count}]]) *matchlist()* + Same as |match()|, but return a |List|. The first item in the + list is the matched string, same as what matchstr() would + return. Following items are submatches, like "\1", "\2", etc. + in |:substitute|. When an optional submatch didn't match an + empty string is used. Example: > + echo matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)') +< Results in: ['acd', 'a', '', 'c', 'd', '', '', '', '', ''] + When there is no match an empty list is returned. + + You can pass in a List, but that is not very useful. + + Can also be used as a |method|: > + GetText()->matchlist('word') + +matchstr({expr}, {pat} [, {start} [, {count}]]) *matchstr()* + Same as |match()|, but return the matched string. Example: > + :echo matchstr("testing", "ing") +< results in "ing". + When there is no match "" is returned. + The {start}, if given, has the same meaning as for |match()|. > + :echo matchstr("testing", "ing", 2) +< results in "ing". > + :echo matchstr("testing", "ing", 5) +< result is "". + When {expr} is a |List| then the matching item is returned. + The type isn't changed, it's not necessarily a String. + + Can also be used as a |method|: > + GetText()->matchstr('word') + +matchstrpos({expr}, {pat} [, {start} [, {count}]]) *matchstrpos()* + Same as |matchstr()|, but return the matched string, the start + position and the end position of the match. Example: > + :echo matchstrpos("testing", "ing") +< results in ["ing", 4, 7]. + When there is no match ["", -1, -1] is returned. + The {start}, if given, has the same meaning as for |match()|. > + :echo matchstrpos("testing", "ing", 2) +< results in ["ing", 4, 7]. > + :echo matchstrpos("testing", "ing", 5) +< result is ["", -1, -1]. + When {expr} is a |List| then the matching item, the index + of first item where {pat} matches, the start position and the + end position of the match are returned. > + :echo matchstrpos([1, '__x'], '\a') +< result is ["x", 1, 2, 3]. + The type isn't changed, it's not necessarily a String. + + Can also be used as a |method|: > + GetText()->matchstrpos('word') +< + *max()* +max({expr}) Return the maximum value of all items in {expr}. Example: > + echo max([apples, pears, oranges]) + +< {expr} can be a |List| or a |Dictionary|. For a Dictionary, + it returns the maximum of all values in the Dictionary. + If {expr} is neither a List nor a Dictionary, or one of the + items in {expr} cannot be used as a Number this results in + an error. An empty |List| or |Dictionary| results in zero. + + Can also be used as a |method|: > + mylist->max() + +menu_get({path} [, {modes}]) *menu_get()* + Returns a |List| of |Dictionaries| describing |menus| (defined + by |:menu|, |:amenu|, …), including |hidden-menus|. + + {path} matches a menu by name, or all menus if {path} is an + empty string. Example: > + :echo menu_get('File','') + :echo menu_get('') +< + {modes} is a string of zero or more modes (see |maparg()| or + |creating-menus| for the list of modes). "a" means "all". + + Example: > + nnoremenu &Test.Test inormal + inoremenu Test.Test insert + vnoremenu Test.Test x + echo menu_get("") + +< returns something like this: > + + [ { + "hidden": 0, + "name": "Test", + "priority": 500, + "shortcut": 84, + "submenus": [ { + "hidden": 0, + "mappings": { + i": { + "enabled": 1, + "noremap": 1, + "rhs": "insert", + "sid": 1, + "silent": 0 + }, + n": { ... }, + s": { ... }, + v": { ... } + }, + "name": "Test", + "priority": 500, + "shortcut": 0 + } ] + } ] +< + + *min()* +min({expr}) Return the minimum value of all items in {expr}. Example: > + echo min([apples, pears, oranges]) + +< {expr} can be a |List| or a |Dictionary|. For a Dictionary, + it returns the minimum of all values in the Dictionary. + If {expr} is neither a List nor a Dictionary, or one of the + items in {expr} cannot be used as a Number this results in + an error. An empty |List| or |Dictionary| results in zero. + + Can also be used as a |method|: > + mylist->min() + +< *mkdir()* *E739* +mkdir({name} [, {path} [, {prot}]]) + Create directory {name}. + + If {path} is "p" then intermediate directories are created as + necessary. Otherwise it must be "". + + If {prot} is given it is used to set the protection bits of + the new directory. The default is 0o755 (rwxr-xr-x: r/w for + the user, readable for others). Use 0o700 to make it + unreadable for others. + + {prot} is applied for all parts of {name}. Thus if you create + /tmp/foo/bar then /tmp/foo will be created with 0o700. Example: > + :call mkdir($HOME . "/tmp/foo/bar", "p", 0o700) + +< This function is not available in the |sandbox|. + + If you try to create an existing directory with {path} set to + "p" mkdir() will silently exit. + + The function result is a Number, which is TRUE if the call was + successful or FALSE if the directory creation failed or partly + failed. + + Can also be used as a |method|: > + GetName()->mkdir() +< + *mode()* +mode([expr]) Return a string that indicates the current mode. + If [expr] is supplied and it evaluates to a non-zero Number or + a non-empty String (|non-zero-arg|), then the full mode is + returned, otherwise only the first letter is returned. + + n Normal + no Operator-pending + nov Operator-pending (forced charwise |o_v|) + noV Operator-pending (forced linewise |o_V|) + noCTRL-V Operator-pending (forced blockwise |o_CTRL-V|) + CTRL-V is one character + niI Normal using |i_CTRL-O| in |Insert-mode| + niR Normal using |i_CTRL-O| in |Replace-mode| + niV Normal using |i_CTRL-O| in |Virtual-Replace-mode| + nt Normal in |terminal-emulator| (insert goes to + Terminal mode) + v Visual by character + vs Visual by character using |v_CTRL-O| in Select mode + V Visual by line + Vs Visual by line using |v_CTRL-O| in Select mode + CTRL-V Visual blockwise + CTRL-Vs Visual blockwise using |v_CTRL-O| in Select mode + s Select by character + S Select by line + CTRL-S Select blockwise + i Insert + ic Insert mode completion |compl-generic| + ix Insert mode |i_CTRL-X| completion + R Replace |R| + Rc Replace mode completion |compl-generic| + Rx Replace mode |i_CTRL-X| completion + Rv Virtual Replace |gR| + Rvc Virtual Replace mode completion |compl-generic| + Rvx Virtual Replace mode |i_CTRL-X| completion + c Command-line editing + cv Vim Ex mode |gQ| + r Hit-enter prompt + rm The -- more -- prompt + r? A |:confirm| query of some sort + ! Shell or external command is executing + t Terminal mode: keys go to the job + + This is useful in the 'statusline' option or when used + with |remote_expr()| In most other places it always returns + "c" or "n". + Note that in the future more modes and more specific modes may + be added. It's better not to compare the whole string but only + the leading character(s). + Also see |visualmode()|. + + Can also be used as a |method|: > + DoFull()->mode() + +msgpackdump({list} [, {type}]) *msgpackdump()* + Convert a list of VimL objects to msgpack. Returned value is a + |readfile()|-style list. When {type} contains "B", a |Blob| is + returned instead. Example: > + call writefile(msgpackdump([{}]), 'fname.mpack', 'b') +< or, using a |Blob|: > + call writefile(msgpackdump([{}], 'B'), 'fname.mpack') +< + This will write the single 0x80 byte to a `fname.mpack` file + (dictionary with zero items is represented by 0x80 byte in + messagepack). + + Limitations: *E5004* *E5005* + 1. |Funcref|s cannot be dumped. + 2. Containers that reference themselves cannot be dumped. + 3. Dictionary keys are always dumped as STR strings. + 4. Other strings and |Blob|s are always dumped as BIN strings. + 5. Points 3. and 4. do not apply to |msgpack-special-dict|s. + +msgpackparse({data}) *msgpackparse()* + Convert a |readfile()|-style list or a |Blob| to a list of + VimL objects. + Example: > + let fname = expand('~/.config/nvim/shada/main.shada') + let mpack = readfile(fname, 'b') + let shada_objects = msgpackparse(mpack) +< This will read ~/.config/nvim/shada/main.shada file to + `shada_objects` list. + + Limitations: + 1. Mapping ordering is not preserved unless messagepack + mapping is dumped using generic mapping + (|msgpack-special-map|). + 2. Since the parser aims to preserve all data untouched + (except for 1.) some strings are parsed to + |msgpack-special-dict| format which is not convenient to + use. + *msgpack-special-dict* + Some messagepack strings may be parsed to special + dictionaries. Special dictionaries are dictionaries which + + 1. Contain exactly two keys: `_TYPE` and `_VAL`. + 2. `_TYPE` key is one of the types found in |v:msgpack_types| + variable. + 3. Value for `_VAL` has the following format (Key column + contains name of the key from |v:msgpack_types|): + + Key Value ~ + nil Zero, ignored when dumping. Not returned by + |msgpackparse()| since |v:null| was introduced. + boolean One or zero. When dumping it is only checked that + value is a |Number|. Not returned by |msgpackparse()| + since |v:true| and |v:false| were introduced. + integer |List| with four numbers: sign (-1 or 1), highest two + bits, number with bits from 62nd to 31st, lowest 31 + bits. I.e. to get actual number one will need to use + code like > + _VAL[0] * ((_VAL[1] << 62) + & (_VAL[2] << 31) + & _VAL[3]) +< Special dictionary with this type will appear in + |msgpackparse()| output under one of the following + circumstances: + 1. |Number| is 32-bit and value is either above + INT32_MAX or below INT32_MIN. + 2. |Number| is 64-bit and value is above INT64_MAX. It + cannot possibly be below INT64_MIN because msgpack + C parser does not support such values. + float |Float|. This value cannot possibly appear in + |msgpackparse()| output. + string |readfile()|-style list of strings. This value will + appear in |msgpackparse()| output if string contains + zero byte or if string is a mapping key and mapping is + being represented as special dictionary for other + reasons. + binary |String|, or |Blob| if binary string contains zero + byte. This value cannot appear in |msgpackparse()| + output since blobs were introduced. + array |List|. This value cannot appear in |msgpackparse()| + output. + *msgpack-special-map* + map |List| of |List|s with two items (key and value) each. + This value will appear in |msgpackparse()| output if + parsed mapping contains one of the following keys: + 1. Any key that is not a string (including keys which + are binary strings). + 2. String with NUL byte inside. + 3. Duplicate key. + 4. Empty key. + ext |List| with two values: first is a signed integer + representing extension type. Second is + |readfile()|-style list of strings. + +nextnonblank({lnum}) *nextnonblank()* + Return the line number of the first line at or below {lnum} + that is not blank. Example: > + if getline(nextnonblank(1)) =~ "Java" +< When {lnum} is invalid or there is no non-blank line at or + below it, zero is returned. + {lnum} is used like with |getline()|. + See also |prevnonblank()|. + + Can also be used as a |method|: > + GetLnum()->nextnonblank() + +nr2char({expr} [, {utf8}]) *nr2char()* + Return a string with a single character, which has the number + value {expr}. Examples: > + nr2char(64) returns "@" + nr2char(32) returns " " +< Example for "utf-8": > + nr2char(300) returns I with bow character +< UTF-8 encoding is always used, {utf8} option has no effect, + and exists only for backwards-compatibility. + Note that a NUL character in the file is specified with + nr2char(10), because NULs are represented with newline + characters. nr2char(0) is a real NUL and terminates the + string, thus results in an empty string. + + Can also be used as a |method|: > + GetNumber()->nr2char() + +nvim_...({...}) *E5555* *nvim_...()* *eval-api* + Call nvim |api| functions. The type checking of arguments will + be stricter than for most other builtins. For instance, + if Integer is expected, a |Number| must be passed in, a + |String| will not be autoconverted. + Buffer numbers, as returned by |bufnr()| could be used as + first argument to nvim_buf_... functions. All functions + expecting an object (buffer, window or tabpage) can + also take the numerical value 0 to indicate the current + (focused) object. + +or({expr}, {expr}) *or()* + Bitwise OR on the two arguments. The arguments are converted + to a number. A List, Dict or Float argument causes an error. + Example: > + :let bits = or(bits, 0x80) +< Can also be used as a |method|: > + :let bits = bits->or(0x80) + +pathshorten({expr} [, {len}]) *pathshorten()* + Shorten directory names in the path {path} and return the + result. The tail, the file name, is kept as-is. The other + components in the path are reduced to {len} letters in length. + If {len} is omitted or smaller than 1 then 1 is used (single + letters). Leading '~' and '.' characters are kept. Examples: > + :echo pathshorten('~/.config/nvim/autoload/file1.vim') +< ~/.c/n/a/file1.vim ~ +> + :echo pathshorten('~/.config/nvim/autoload/file2.vim', 2) +< ~/.co/nv/au/file2.vim ~ + It doesn't matter if the path exists or not. + + Can also be used as a |method|: > + GetDirectories()->pathshorten() + +perleval({expr}) *perleval()* + Evaluate |perl| expression {expr} and return its result + converted to Vim data structures. + Numbers and strings are returned as they are (strings are + copied though). + Lists are represented as Vim |List| type. + Dictionaries are represented as Vim |Dictionary| type, + non-string keys result in error. + + Note: If you want an array or hash, {expr} must return a + reference to it. + Example: > + :echo perleval('[1 .. 4]') +< [1, 2, 3, 4] + + Can also be used as a |method|: > + GetExpr()->perleval() + +pow({x}, {y}) *pow()* + Return the power of {x} to the exponent {y} as a |Float|. + {x} and {y} must evaluate to a |Float| or a |Number|. + Examples: > + :echo pow(3, 3) +< 27.0 > + :echo pow(2, 16) +< 65536.0 > + :echo pow(32, 0.20) +< 2.0 + + Can also be used as a |method|: > + Compute()->pow(3) + +prevnonblank({lnum}) *prevnonblank()* + Return the line number of the first line at or above {lnum} + that is not blank. Example: > + let ind = indent(prevnonblank(v:lnum - 1)) +< When {lnum} is invalid or there is no non-blank line at or + above it, zero is returned. + {lnum} is used like with |getline()|. + Also see |nextnonblank()|. + + Can also be used as a |method|: > + GetLnum()->prevnonblank() + +printf({fmt}, {expr1} ...) *printf()* + Return a String with {fmt}, where "%" items are replaced by + the formatted form of their respective arguments. Example: > + printf("%4d: E%d %.30s", lnum, errno, msg) +< May result in: + " 99: E42 asdfasdfasdfasdfasdfasdfasdfas" ~ + + When used as a |method| the base is passed as the second + argument: > + Compute()->printf("result: %d") + +< Often used items are: + %s string + %6S string right-aligned in 6 display cells + %6s string right-aligned in 6 bytes + %.9s string truncated to 9 bytes + %c single byte + %d decimal number + %5d decimal number padded with spaces to 5 characters + %b binary number + %08b binary number padded with zeros to at least 8 characters + %B binary number using upper case letters + %x hex number + %04x hex number padded with zeros to at least 4 characters + %X hex number using upper case letters + %o octal number + %f floating point number as 12.23, inf, -inf or nan + %F floating point number as 12.23, INF, -INF or NAN + %e floating point number as 1.23e3, inf, -inf or nan + %E floating point number as 1.23E3, INF, -INF or NAN + %g floating point number, as %f or %e depending on value + %G floating point number, as %F or %E depending on value + %% the % character itself + %p representation of the pointer to the container + + Conversion specifications start with '%' and end with the + conversion type. All other characters are copied unchanged to + the result. + + The "%" starts a conversion specification. The following + arguments appear in sequence: + + % [flags] [field-width] [.precision] type + + flags + Zero or more of the following flags: + + # The value should be converted to an "alternate + form". For c, d, and s conversions, this option + has no effect. For o conversions, the precision + of the number is increased to force the first + character of the output string to a zero (except + if a zero value is printed with an explicit + precision of zero). + For x and X conversions, a non-zero result has + the string "0x" (or "0X" for X conversions) + prepended to it. + + 0 (zero) Zero padding. For all conversions the converted + value is padded on the left with zeros rather + than blanks. If a precision is given with a + numeric conversion (d, o, x, and X), the 0 flag + is ignored. + + - A negative field width flag; the converted value + is to be left adjusted on the field boundary. + The converted value is padded on the right with + blanks, rather than on the left with blanks or + zeros. A - overrides a 0 if both are given. + + ' ' (space) A blank should be left before a positive + number produced by a signed conversion (d). + + + A sign must always be placed before a number + produced by a signed conversion. A + overrides + a space if both are used. + + field-width + An optional decimal digit string specifying a minimum + field width. If the converted value has fewer bytes + than the field width, it will be padded with spaces on + the left (or right, if the left-adjustment flag has + been given) to fill out the field width. For the S + conversion the count is in cells. + + .precision + An optional precision, in the form of a period '.' + followed by an optional digit string. If the digit + string is omitted, the precision is taken as zero. + This gives the minimum number of digits to appear for + d, o, x, and X conversions, the maximum number of + bytes to be printed from a string for s conversions, + or the maximum number of cells to be printed from a + string for S conversions. + For floating point it is the number of digits after + the decimal point. + + type + A character that specifies the type of conversion to + be applied, see below. + + A field width or precision, or both, may be indicated by an + asterisk '*' instead of a digit string. In this case, a + Number argument supplies the field width or precision. A + negative field width is treated as a left adjustment flag + followed by a positive field width; a negative precision is + treated as though it were missing. Example: > + :echo printf("%d: %.*s", nr, width, line) +< This limits the length of the text used from "line" to + "width" bytes. + + The conversion specifiers and their meanings are: + + *printf-d* *printf-b* *printf-B* *printf-o* *printf-x* *printf-X* + dbBoxX The Number argument is converted to signed decimal (d), + unsigned binary (b and B), unsigned octal (o), or + unsigned hexadecimal (x and X) notation. The letters + "abcdef" are used for x conversions; the letters + "ABCDEF" are used for X conversions. The precision, if + any, gives the minimum number of digits that must + appear; if the converted value requires fewer digits, it + is padded on the left with zeros. In no case does a + non-existent or small field width cause truncation of a + numeric field; if the result of a conversion is wider + than the field width, the field is expanded to contain + the conversion result. + The 'h' modifier indicates the argument is 16 bits. + The 'l' modifier indicates the argument is 32 bits. + The 'L' modifier indicates the argument is 64 bits. + Generally, these modifiers are not useful. They are + ignored when type is known from the argument. + + i alias for d + D alias for ld + U alias for lu + O alias for lo + + *printf-c* + c The Number argument is converted to a byte, and the + resulting character is written. + + *printf-s* + s The text of the String argument is used. If a + precision is specified, no more bytes than the number + specified are used. + If the argument is not a String type, it is + automatically converted to text with the same format + as ":echo". + *printf-S* + S The text of the String argument is used. If a + precision is specified, no more display cells than the + number specified are used. + + *printf-f* *E807* + f F The Float argument is converted into a string of the + form 123.456. The precision specifies the number of + digits after the decimal point. When the precision is + zero the decimal point is omitted. When the precision + is not specified 6 is used. A really big number + (out of range or dividing by zero) results in "inf" + or "-inf" with %f (INF or -INF with %F). + "0.0 / 0.0" results in "nan" with %f (NAN with %F). + Example: > + echo printf("%.2f", 12.115) +< 12.12 + Note that roundoff depends on the system libraries. + Use |round()| when in doubt. + + *printf-e* *printf-E* + e E The Float argument is converted into a string of the + form 1.234e+03 or 1.234E+03 when using 'E'. The + precision specifies the number of digits after the + decimal point, like with 'f'. + + *printf-g* *printf-G* + g G The Float argument is converted like with 'f' if the + value is between 0.001 (inclusive) and 10000000.0 + (exclusive). Otherwise 'e' is used for 'g' and 'E' + for 'G'. When no precision is specified superfluous + zeroes and '+' signs are removed, except for the zero + immediately after the decimal point. Thus 10000000.0 + results in 1.0e7. + + *printf-%* + % A '%' is written. No argument is converted. The + complete conversion specification is "%%". + + When a Number argument is expected a String argument is also + accepted and automatically converted. + When a Float or String argument is expected a Number argument + is also accepted and automatically converted. + Any other argument type results in an error message. + + *E766* *E767* + The number of {exprN} arguments must exactly match the number + of "%" items. If there are not sufficient or too many + arguments an error is given. Up to 18 arguments can be used. + +prompt_getprompt({buf}) *prompt_getprompt()* + Returns the effective prompt text for buffer {buf}. {buf} can + be a buffer name or number. See |prompt-buffer|. + + If the buffer doesn't exist or isn't a prompt buffer, an empty + string is returned. + + Can also be used as a |method|: > + GetBuffer()->prompt_getprompt() + +prompt_setcallback({buf}, {expr}) *prompt_setcallback()* + Set prompt callback for buffer {buf} to {expr}. When {expr} + is an empty string the callback is removed. This has only + effect if {buf} has 'buftype' set to "prompt". + + The callback is invoked when pressing Enter. The current + buffer will always be the prompt buffer. A new line for a + prompt is added before invoking the callback, thus the prompt + for which the callback was invoked will be in the last but one + line. + If the callback wants to add text to the buffer, it must + insert it above the last line, since that is where the current + prompt is. This can also be done asynchronously. + The callback is invoked with one argument, which is the text + that was entered at the prompt. This can be an empty string + if the user only typed Enter. + Example: > + call prompt_setcallback(bufnr(''), function('s:TextEntered')) + func s:TextEntered(text) + if a:text == 'exit' || a:text == 'quit' + stopinsert + close + else + call append(line('$') - 1, 'Entered: "' . a:text . '"') + " Reset 'modified' to allow the buffer to be closed. + set nomodified + endif + endfunc + +< Can also be used as a |method|: > + GetBuffer()->prompt_setcallback(callback) + +prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()* + Set a callback for buffer {buf} to {expr}. When {expr} is an + empty string the callback is removed. This has only effect if + {buf} has 'buftype' set to "prompt". + + This callback will be invoked when pressing CTRL-C in Insert + mode. Without setting a callback Vim will exit Insert mode, + as in any buffer. + + Can also be used as a |method|: > + GetBuffer()->prompt_setinterrupt(callback) + +prompt_setprompt({buf}, {text}) *prompt_setprompt()* + Set prompt for buffer {buf} to {text}. You most likely want + {text} to end in a space. + The result is only visible if {buf} has 'buftype' set to + "prompt". Example: > + call prompt_setprompt(bufnr(''), 'command: ') +< + Can also be used as a |method|: > + GetBuffer()->prompt_setprompt('command: ') + +pum_getpos() *pum_getpos()* + If the popup menu (see |ins-completion-menu|) is not visible, + returns an empty |Dictionary|, otherwise, returns a + |Dictionary| with the following keys: + height nr of items visible + width screen cells + row top screen row (0 first row) + col leftmost screen column (0 first col) + size total nr of items + scrollbar |TRUE| if scrollbar is visible + + The values are the same as in |v:event| during |CompleteChanged|. + +pumvisible() *pumvisible()* + Returns non-zero when the popup menu is visible, zero + otherwise. See |ins-completion-menu|. + This can be used to avoid some things that would remove the + popup menu. + +py3eval({expr}) *py3eval()* + Evaluate Python expression {expr} and return its result + converted to Vim data structures. + Numbers and strings are returned as they are (strings are + copied though, Unicode strings are additionally converted to + UTF-8). + Lists are represented as Vim |List| type. + Dictionaries are represented as Vim |Dictionary| type with + keys converted to strings. + + Can also be used as a |method|: > + GetExpr()->py3eval() +< + *E858* *E859* +pyeval({expr}) *pyeval()* + Evaluate Python expression {expr} and return its result + converted to Vim data structures. + Numbers and strings are returned as they are (strings are + copied though). + Lists are represented as Vim |List| type. + Dictionaries are represented as Vim |Dictionary| type, + non-string keys result in error. + + Can also be used as a |method|: > + GetExpr()->pyeval() + +pyxeval({expr}) *pyxeval()* + Evaluate Python expression {expr} and return its result + converted to Vim data structures. + Uses Python 2 or 3, see |python_x| and 'pyxversion'. + See also: |pyeval()|, |py3eval()| + + Can also be used as a |method|: > + GetExpr()->pyxeval() +< + *E726* *E727* +range({expr} [, {max} [, {stride}]]) *range()* + Returns a |List| with Numbers: + - If only {expr} is specified: [0, 1, ..., {expr} - 1] + - If {max} is specified: [{expr}, {expr} + 1, ..., {max}] + - If {stride} is specified: [{expr}, {expr} + {stride}, ..., + {max}] (increasing {expr} with {stride} each time, not + producing a value past {max}). + When the maximum is one before the start the result is an + empty list. When the maximum is more than one before the + start this is an error. + Examples: > + range(4) " [0, 1, 2, 3] + range(2, 4) " [2, 3, 4] + range(2, 9, 3) " [2, 5, 8] + range(2, -2, -1) " [2, 1, 0, -1, -2] + range(0) " [] + range(2, 0) " error! +< + Can also be used as a |method|: > + GetExpr()->range() +< +rand([{expr}]) *rand()* + Return a pseudo-random Number generated with an xoshiro128** + algorithm using seed {expr}. The returned number is 32 bits, + also on 64 bits systems, for consistency. + {expr} can be initialized by |srand()| and will be updated by + rand(). If {expr} is omitted, an internal seed value is used + and updated. + + Examples: > + :echo rand() + :let seed = srand() + :echo rand(seed) + :echo rand(seed) % 16 " random number 0 - 15 +< + Can also be used as a |method|: > + seed->rand() +< + *readdir()* +readdir({directory} [, {expr}]) + Return a list with file and directory names in {directory}. + + When {expr} is omitted all entries are included. + When {expr} is given, it is evaluated to check what to do: + If {expr} results in -1 then no further entries will + be handled. + If {expr} results in 0 then this entry will not be + added to the list. + If {expr} results in 1 then this entry will be added + to the list. + Each time {expr} is evaluated |v:val| is set to the entry name. + When {expr} is a function the name is passed as the argument. + For example, to get a list of files ending in ".txt": > + readdir(dirname, {n -> n =~ '.txt$'}) +< To skip hidden and backup files: > + readdir(dirname, {n -> n !~ '^\.\|\~$'}) + +< If you want to get a directory tree: > + function! s:tree(dir) + return {a:dir : map(readdir(a:dir), + \ {_, x -> isdirectory(x) ? + \ {x : s:tree(a:dir . '/' . x)} : x})} + endfunction + echo s:tree(".") +< + Can also be used as a |method|: > + GetDirName()->readdir() +< + *readfile()* +readfile({fname} [, {type} [, {max}]]) + Read file {fname} and return a |List|, each line of the file + as an item. Lines are broken at NL characters. Macintosh + files separated with CR will result in a single long line + (unless a NL appears somewhere). + All NUL characters are replaced with a NL character. + When {type} contains "b" binary mode is used: + - When the last line ends in a NL an extra empty list item is + added. + - No CR characters are removed. + When {type} contains "B" a |Blob| is returned with the binary + data of the file unmodified. + Otherwise: + - CR characters that appear before a NL are removed. + - Whether the last line ends in a NL or not does not matter. + - Any UTF-8 byte order mark is removed from the text. + When {max} is given this specifies the maximum number of lines + to be read. Useful if you only want to check the first ten + lines of a file: > + :for line in readfile(fname, '', 10) + : if line =~ 'Date' | echo line | endif + :endfor +< When {max} is negative -{max} lines from the end of the file + are returned, or as many as there are. + When {max} is zero the result is an empty list. + Note that without {max} the whole file is read into memory. + Also note that there is no recognition of encoding. Read a + file into a buffer if you need to. + When the file can't be opened an error message is given and + the result is an empty list. + Also see |writefile()|. + + Can also be used as a |method|: > + GetFileName()->readfile() + +reduce({object}, {func} [, {initial}]) *reduce()* *E998* + {func} is called for every item in {object}, which can be a + |List| or a |Blob|. {func} is called with two arguments: the + result so far and current item. After processing all items + the result is returned. + + {initial} is the initial result. When omitted, the first item + in {object} is used and {func} is first called for the second + item. If {initial} is not given and {object} is empty no + result can be computed, an E998 error is given. + + Examples: > + echo reduce([1, 3, 5], { acc, val -> acc + val }) + echo reduce(['x', 'y'], { acc, val -> acc .. val }, 'a') + echo reduce(0z1122, { acc, val -> 2 * acc + val }) +< + Can also be used as a |method|: > + echo mylist->reduce({ acc, val -> acc + val }, 0) + +reg_executing() *reg_executing()* + Returns the single letter name of the register being executed. + Returns an empty string when no register is being executed. + See |@|. + +reg_recorded() *reg_recorded()* + Returns the single letter name of the last recorded register. + Returns an empty string when nothing was recorded yet. + See |q| and |Q|. + +reg_recording() *reg_recording()* + Returns the single letter name of the register being recorded. + Returns an empty string when not recording. See |q|. + +reltime([{start} [, {end}]]) *reltime()* + Return an item that represents a time value. The item is a + list with items that depend on the system. + The item can be passed to |reltimestr()| to convert it to a + string or |reltimefloat()| to convert to a Float. + + Without an argument it returns the current "relative time", an + implementation-defined value meaningful only when used as an + argument to |reltime()|, |reltimestr()| and |reltimefloat()|. + + With one argument it returns the time passed since the time + specified in the argument. + With two arguments it returns the time passed between {start} + and {end}. + + The {start} and {end} arguments must be values returned by + reltime(). + + Can also be used as a |method|: > + GetStart()->reltime() +< + Note: |localtime()| returns the current (non-relative) time. + +reltimefloat({time}) *reltimefloat()* + Return a Float that represents the time value of {time}. + Unit of time is seconds. + Example: + let start = reltime() + call MyFunction() + let seconds = reltimefloat(reltime(start)) + See the note of reltimestr() about overhead. + Also see |profiling|. + If there is an error an empty string is returned + + Can also be used as a |method|: > + reltime(start)->reltimefloat() + +reltimestr({time}) *reltimestr()* + Return a String that represents the time value of {time}. + This is the number of seconds, a dot and the number of + microseconds. Example: > + let start = reltime() + call MyFunction() + echo reltimestr(reltime(start)) +< Note that overhead for the commands will be added to the time. + Leading spaces are used to make the string align nicely. You + can use split() to remove it. > + echo split(reltimestr(reltime(start)))[0] +< Also see |profiling|. + If there is an error an empty string is returned + + Can also be used as a |method|: > + reltime(start)->reltimestr() +< + *remote_expr()* *E449* +remote_expr({server}, {string} [, {idvar} [, {timeout}]]) + Send the {string} to {server}. The string is sent as an + expression and the result is returned after evaluation. + The result must be a String or a |List|. A |List| is turned + into a String by joining the items with a line break in + between (not at the end), like with join(expr, "\n"). + If {idvar} is present and not empty, it is taken as the name + of a variable and a {serverid} for later use with + |remote_read()| is stored there. + If {timeout} is given the read times out after this many + seconds. Otherwise a timeout of 600 seconds is used. + See also |clientserver| |RemoteReply|. + This function is not available in the |sandbox|. + Note: Any errors will cause a local error message to be issued + and the result will be the empty string. + + Variables will be evaluated in the global namespace, + independent of a function currently being active. Except + when in debug mode, then local function variables and + arguments can be evaluated. + + Examples: > + :echo remote_expr("gvim", "2+2") + :echo remote_expr("gvim1", "b:current_syntax") +< + +remote_foreground({server}) *remote_foreground()* + Move the Vim server with the name {server} to the foreground. + The {server} argument is a string. + This works like: > + remote_expr({server}, "foreground()") +< Except that on Win32 systems the client does the work, to work + around the problem that the OS doesn't always allow the server + to bring itself to the foreground. + Note: This does not restore the window if it was minimized, + like foreground() does. + This function is not available in the |sandbox|. + {only in the Win32 GUI and the Win32 console version} + + +remote_peek({serverid} [, {retvar}]) *remote_peek()* + Returns a positive number if there are available strings + from {serverid}. Copies any reply string into the variable + {retvar} if specified. {retvar} must be a string with the + name of a variable. + Returns zero if none are available. + Returns -1 if something is wrong. + See also |clientserver|. + This function is not available in the |sandbox|. + Examples: > + :let repl = "" + :echo "PEEK: ".remote_peek(id, "repl").": ".repl + +remote_read({serverid}, [{timeout}]) *remote_read()* + Return the oldest available reply from {serverid} and consume + it. Unless a {timeout} in seconds is given, it blocks until a + reply is available. + See also |clientserver|. + This function is not available in the |sandbox|. + Example: > + :echo remote_read(id) +< + *remote_send()* *E241* +remote_send({server}, {string} [, {idvar}]) + Send the {string} to {server}. The string is sent as input + keys and the function returns immediately. At the Vim server + the keys are not mapped |:map|. + If {idvar} is present, it is taken as the name of a variable + and a {serverid} for later use with remote_read() is stored + there. + See also |clientserver| |RemoteReply|. + This function is not available in the |sandbox|. + + Note: Any errors will be reported in the server and may mess + up the display. + Examples: > + :echo remote_send("gvim", ":DropAndReply ".file, "serverid"). + \ remote_read(serverid) + + :autocmd NONE RemoteReply * + \ echo remote_read(expand("<amatch>")) + :echo remote_send("gvim", ":sleep 10 | echo ". + \ 'server2client(expand("<client>"), "HELLO")<CR>') +< + *remote_startserver()* *E941* *E942* +remote_startserver({name}) + Become the server {name}. This fails if already running as a + server, when |v:servername| is not empty. + +remove({list}, {idx} [, {end}]) *remove()* + Without {end}: Remove the item at {idx} from |List| {list} and + return the item. + With {end}: Remove items from {idx} to {end} (inclusive) and + return a |List| with these items. When {idx} points to the same + item as {end} a list with one item is returned. When {end} + points to an item before {idx} this is an error. + See |list-index| for possible values of {idx} and {end}. + Example: > + :echo "last item: " . remove(mylist, -1) + :call remove(mylist, 0, 9) +< + Use |delete()| to remove a file. + + Can also be used as a |method|: > + mylist->remove(idx) + +remove({blob}, {idx} [, {end}]) + Without {end}: Remove the byte at {idx} from |Blob| {blob} and + return the byte. + With {end}: Remove bytes from {idx} to {end} (inclusive) and + return a |Blob| with these bytes. When {idx} points to the same + byte as {end} a |Blob| with one byte is returned. When {end} + points to a byte before {idx} this is an error. + Example: > + :echo "last byte: " . remove(myblob, -1) + :call remove(mylist, 0, 9) + +remove({dict}, {key}) + Remove the entry from {dict} with key {key} and return it. + Example: > + :echo "removed " . remove(dict, "one") +< If there is no {key} in {dict} this is an error. + +rename({from}, {to}) *rename()* + Rename the file by the name {from} to the name {to}. This + should also work to move files across file systems. The + result is a Number, which is 0 if the file was renamed + successfully, and non-zero when the renaming failed. + NOTE: If {to} exists it is overwritten without warning. + This function is not available in the |sandbox|. + + Can also be used as a |method|: > + GetOldName()->rename(newname) + +repeat({expr}, {count}) *repeat()* + Repeat {expr} {count} times and return the concatenated + result. Example: > + :let separator = repeat('-', 80) +< When {count} is zero or negative the result is empty. + When {expr} is a |List| the result is {expr} concatenated + {count} times. Example: > + :let longlist = repeat(['a', 'b'], 3) +< Results in ['a', 'b', 'a', 'b', 'a', 'b']. + + Can also be used as a |method|: > + mylist->repeat(count) + +resolve({filename}) *resolve()* *E655* + On MS-Windows, when {filename} is a shortcut (a .lnk file), + returns the path the shortcut points to in a simplified form. + On Unix, repeat resolving symbolic links in all path + components of {filename} and return the simplified result. + To cope with link cycles, resolving of symbolic links is + stopped after 100 iterations. + On other systems, return the simplified {filename}. + The simplification step is done as by |simplify()|. + resolve() keeps a leading path component specifying the + current directory (provided the result is still a relative + path name) and also keeps a trailing path separator. + + Can also be used as a |method|: > + GetName()->resolve() +< + *reverse()* +reverse({object}) + Reverse the order of items in {object} in-place. + {object} can be a |List| or a |Blob|. + Returns {object}. + If you want an object to remain unmodified make a copy first: > + :let revlist = reverse(copy(mylist)) +< Can also be used as a |method|: > + mylist->reverse() + +round({expr}) *round()* + Round off {expr} to the nearest integral value and return it + as a |Float|. If {expr} lies halfway between two integral + values, then use the larger one (away from zero). + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + echo round(0.456) +< 0.0 > + echo round(4.5) +< 5.0 > + echo round(-4.5) +< -5.0 + + Can also be used as a |method|: > + Compute()->round() + +rpcnotify({channel}, {event} [, {args}...]) *rpcnotify()* + Sends {event} to {channel} via |RPC| and returns immediately. + If {channel} is 0, the event is broadcast to all channels. + Example: > + :au VimLeave call rpcnotify(0, "leaving") + +rpcrequest({channel}, {method} [, {args}...]) *rpcrequest()* + Sends a request to {channel} to invoke {method} via + |RPC| and blocks until a response is received. + Example: > + :let result = rpcrequest(rpc_chan, "func", 1, 2, 3) + +rpcstart({prog} [, {argv}]) *rpcstart()* + Deprecated. Replace > + :let id = rpcstart('prog', ['arg1', 'arg2']) +< with > + :let id = jobstart(['prog', 'arg1', 'arg2'], {'rpc': v:true}) + +rubyeval({expr}) *rubyeval()* + Evaluate Ruby expression {expr} and return its result + converted to Vim data structures. + Numbers, floats and strings are returned as they are (strings + are copied though). + Arrays are represented as Vim |List| type. + Hashes are represented as Vim |Dictionary| type. + Other objects are represented as strings resulted from their + "Object#to_s" method. + + Can also be used as a |method|: > + GetRubyExpr()->rubyeval() + +screenattr({row}, {col}) *screenattr()* + Like |screenchar()|, but return the attribute. This is a rather + arbitrary number that can only be used to compare to the + attribute at other positions. + + Can also be used as a |method|: > + GetRow()->screenattr(col) + +screenchar({row}, {col}) *screenchar()* + The result is a Number, which is the character at position + [row, col] on the screen. This works for every possible + screen position, also status lines, window separators and the + command line. The top left position is row one, column one + The character excludes composing characters. For double-byte + encodings it may only be the first byte. + This is mainly to be used for testing. + Returns -1 when row or col is out of range. + + Can also be used as a |method|: > + GetRow()->screenchar(col) + +screenchars({row}, {col}) *screenchars()* + The result is a List of Numbers. The first number is the same + as what |screenchar()| returns. Further numbers are + composing characters on top of the base character. + This is mainly to be used for testing. + Returns an empty List when row or col is out of range. + + Can also be used as a |method|: > + GetRow()->screenchars(col) + +screencol() *screencol()* + The result is a Number, which is the current screen column of + the cursor. The leftmost column has number 1. + This function is mainly used for testing. + + Note: Always returns the current screen column, thus if used + in a command (e.g. ":echo screencol()") it will return the + column inside the command line, which is 1 when the command is + executed. To get the cursor position in the file use one of + the following mappings: > + nnoremap <expr> GG ":echom ".screencol()."\n" + nnoremap <silent> GG :echom screencol()<CR> + noremap GG <Cmd>echom screencol()<Cr> +< +screenpos({winid}, {lnum}, {col}) *screenpos()* + The result is a Dict with the screen position of the text + character in window {winid} at buffer line {lnum} and column + {col}. {col} is a one-based byte index. + The Dict has these members: + row screen row + col first screen column + endcol last screen column + curscol cursor screen column + If the specified position is not visible, all values are zero. + The "endcol" value differs from "col" when the character + occupies more than one screen cell. E.g. for a Tab "col" can + be 1 and "endcol" can be 8. + The "curscol" value is where the cursor would be placed. For + a Tab it would be the same as "endcol", while for a double + width character it would be the same as "col". + The |conceal| feature is ignored here, the column numbers are + as if 'conceallevel' is zero. You can set the cursor to the + right position and use |screencol()| to get the value with + |conceal| taken into account. + + Can also be used as a |method|: > + GetWinid()->screenpos(lnum, col) + +screenrow() *screenrow()* + The result is a Number, which is the current screen row of the + cursor. The top line has number one. + This function is mainly used for testing. + Alternatively you can use |winline()|. + + Note: Same restrictions as with |screencol()|. + +screenstring({row}, {col}) *screenstring()* + The result is a String that contains the base character and + any composing characters at position [row, col] on the screen. + This is like |screenchars()| but returning a String with the + characters. + This is mainly to be used for testing. + Returns an empty String when row or col is out of range. + + Can also be used as a |method|: > + GetRow()->screenstring(col) +< + *search()* +search({pattern} [, {flags} [, {stopline} [, {timeout} [, {skip}]]]]) + Search for regexp pattern {pattern}. The search starts at the + cursor position (you can use |cursor()| to set it). + + When a match has been found its line number is returned. + If there is no match a 0 is returned and the cursor doesn't + move. No error message is given. + + {flags} is a String, which can contain these character flags: + 'b' search Backward instead of forward + 'c' accept a match at the Cursor position + 'e' move to the End of the match + 'n' do Not move the cursor + 'p' return number of matching sub-Pattern (see below) + 's' Set the ' mark at the previous location of the cursor + 'w' Wrap around the end of the file + 'W' don't Wrap around the end of the file + 'z' start searching at the cursor column instead of Zero + If neither 'w' or 'W' is given, the 'wrapscan' option applies. + + If the 's' flag is supplied, the ' mark is set, only if the + cursor is moved. The 's' flag cannot be combined with the 'n' + flag. + + 'ignorecase', 'smartcase' and 'magic' are used. + + When the 'z' flag is not given, forward searching always + starts in column zero and then matches before the cursor are + skipped. When the 'c' flag is present in 'cpo' the next + search starts after the match. Without the 'c' flag the next + search starts one column further. This matters for + overlapping matches. + When searching backwards and the 'z' flag is given then the + search starts in column zero, thus no match in the current + line will be found (unless wrapping around the end of the + file). + + When the {stopline} argument is given then the search stops + after searching this line. This is useful to restrict the + search to a range of lines. Examples: > + let match = search('(', 'b', line("w0")) + let end = search('END', '', line("w$")) +< When {stopline} is used and it is not zero this also implies + that the search does not wrap around the end of the file. + A zero value is equal to not giving the argument. + + When the {timeout} argument is given the search stops when + more than this many milliseconds have passed. Thus when + {timeout} is 500 the search stops after half a second. + The value must not be negative. A zero value is like not + giving the argument. + + If the {skip} expression is given it is evaluated with the + cursor positioned on the start of a match. If it evaluates to + non-zero this match is skipped. This can be used, for + example, to skip a match in a comment or a string. + {skip} can be a string, which is evaluated as an expression, a + function reference or a lambda. + When {skip} is omitted or empty, every match is accepted. + When evaluating {skip} causes an error the search is aborted + and -1 returned. + *search()-sub-match* + With the 'p' flag the returned value is one more than the + first sub-match in \(\). One if none of them matched but the + whole pattern did match. + To get the column number too use |searchpos()|. + + The cursor will be positioned at the match, unless the 'n' + flag is used. + + Example (goes over all files in the argument list): > + :let n = 1 + :while n <= argc() " loop over all files in arglist + : exe "argument " . n + : " start at the last char in the file and wrap for the + : " first search to find match at start of file + : normal G$ + : let flags = "w" + : while search("foo", flags) > 0 + : s/foo/bar/g + : let flags = "W" + : endwhile + : update " write the file if modified + : let n = n + 1 + :endwhile +< + Example for using some flags: > + :echo search('\<if\|\(else\)\|\(endif\)', 'ncpe') +< This will search for the keywords "if", "else", and "endif" + under or after the cursor. Because of the 'p' flag, it + returns 1, 2, or 3 depending on which keyword is found, or 0 + if the search fails. With the cursor on the first word of the + line: + if (foo == 0) | let foo = foo + 1 | endif ~ + the function returns 1. Without the 'c' flag, the function + finds the "endif" and returns 3. The same thing happens + without the 'e' flag if the cursor is on the "f" of "if". + The 'n' flag tells the function not to move the cursor. + + Can also be used as a |method|: > + GetPattern()->search() + +searchcount([{options}]) *searchcount()* + Get or update the last search count, like what is displayed + without the "S" flag in 'shortmess'. This works even if + 'shortmess' does contain the "S" flag. + + This returns a Dictionary. The dictionary is empty if the + previous pattern was not set and "pattern" was not specified. + + key type meaning ~ + current |Number| current position of match; + 0 if the cursor position is + before the first match + exact_match |Boolean| 1 if "current" is matched on + "pos", otherwise 0 + total |Number| total count of matches found + incomplete |Number| 0: search was fully completed + 1: recomputing was timed out + 2: max count exceeded + + For {options} see further down. + + To get the last search count when |n| or |N| was pressed, call + this function with `recompute: 0` . This sometimes returns + wrong information because |n| and |N|'s maximum count is 99. + If it exceeded 99 the result must be max count + 1 (100). If + you want to get correct information, specify `recompute: 1`: > + + " result == maxcount + 1 (100) when many matches + let result = searchcount(#{recompute: 0}) + + " Below returns correct result (recompute defaults + " to 1) + let result = searchcount() +< + The function is useful to add the count to |statusline|: > + function! LastSearchCount() abort + let result = searchcount(#{recompute: 0}) + if empty(result) + return '' + endif + if result.incomplete ==# 1 " timed out + return printf(' /%s [?/??]', @/) + elseif result.incomplete ==# 2 " max count exceeded + if result.total > result.maxcount && + \ result.current > result.maxcount + return printf(' /%s [>%d/>%d]', @/, + \ result.current, result.total) + elseif result.total > result.maxcount + return printf(' /%s [%d/>%d]', @/, + \ result.current, result.total) + endif + endif + return printf(' /%s [%d/%d]', @/, + \ result.current, result.total) + endfunction + let &statusline .= '%{LastSearchCount()}' + + " Or if you want to show the count only when + " 'hlsearch' was on + " let &statusline .= + " \ '%{v:hlsearch ? LastSearchCount() : ""}' +< + You can also update the search count, which can be useful in a + |CursorMoved| or |CursorMovedI| autocommand: > + + autocmd CursorMoved,CursorMovedI * + \ let s:searchcount_timer = timer_start( + \ 200, function('s:update_searchcount')) + function! s:update_searchcount(timer) abort + if a:timer ==# s:searchcount_timer + call searchcount(#{ + \ recompute: 1, maxcount: 0, timeout: 100}) + redrawstatus + endif + endfunction +< + This can also be used to count matched texts with specified + pattern in the current buffer using "pattern": > + + " Count '\<foo\>' in this buffer + " (Note that it also updates search count) + let result = searchcount(#{pattern: '\<foo\>'}) + + " To restore old search count by old pattern, + " search again + call searchcount() +< + {options} must be a Dictionary. It can contain: + key type meaning ~ + recompute |Boolean| if |TRUE|, recompute the count + like |n| or |N| was executed. + otherwise returns the last + result by |n|, |N|, or this + function is returned. + (default: |TRUE|) + pattern |String| recompute if this was given + and different with |@/|. + this works as same as the + below command is executed + before calling this function > + let @/ = pattern +< (default: |@/|) + timeout |Number| 0 or negative number is no + timeout. timeout milliseconds + for recomputing the result + (default: 0) + maxcount |Number| 0 or negative number is no + limit. max count of matched + text while recomputing the + result. if search exceeded + total count, "total" value + becomes `maxcount + 1` + (default: 0) + pos |List| `[lnum, col, off]` value + when recomputing the result. + this changes "current" result + value. see |cursor()|, |getpos() + (default: cursor's position) + + Can also be used as a |method|: > + GetSearchOpts()->searchcount() +< +searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()* + Search for the declaration of {name}. + + With a non-zero {global} argument it works like |gD|, find + first match in the file. Otherwise it works like |gd|, find + first match in the function. + + With a non-zero {thisblock} argument matches in a {} block + that ends before the cursor position are ignored. Avoids + finding variable declarations only valid in another scope. + + Moves the cursor to the found match. + Returns zero for success, non-zero for failure. + Example: > + if searchdecl('myvar') == 0 + echo getline('.') + endif +< + Can also be used as a |method|: > + GetName()->searchdecl() +< + *searchpair()* +searchpair({start}, {middle}, {end} [, {flags} [, {skip} + [, {stopline} [, {timeout}]]]]) + Search for the match of a nested start-end pair. This can be + used to find the "endif" that matches an "if", while other + if/endif pairs in between are ignored. + The search starts at the cursor. The default is to search + forward, include 'b' in {flags} to search backward. + If a match is found, the cursor is positioned at it and the + line number is returned. If no match is found 0 or -1 is + returned and the cursor doesn't move. No error message is + given. + + {start}, {middle} and {end} are patterns, see |pattern|. They + must not contain \( \) pairs. Use of \%( \) is allowed. When + {middle} is not empty, it is found when searching from either + direction, but only when not in a nested start-end pair. A + typical use is: > + searchpair('\<if\>', '\<else\>', '\<endif\>') +< By leaving {middle} empty the "else" is skipped. + + {flags} 'b', 'c', 'n', 's', 'w' and 'W' are used like with + |search()|. Additionally: + 'r' Repeat until no more matches found; will find the + outer pair. Implies the 'W' flag. + 'm' Return number of matches instead of line number with + the match; will be > 1 when 'r' is used. + Note: it's nearly always a good idea to use the 'W' flag, to + avoid wrapping around the end of the file. + + When a match for {start}, {middle} or {end} is found, the + {skip} expression is evaluated with the cursor positioned on + the start of the match. It should return non-zero if this + match is to be skipped. E.g., because it is inside a comment + or a string. + When {skip} is omitted or empty, every match is accepted. + When evaluating {skip} causes an error the search is aborted + and -1 returned. + {skip} can be a string, a lambda, a funcref or a partial. + Anything else makes the function fail. + + For {stopline} and {timeout} see |search()|. + + The value of 'ignorecase' is used. 'magic' is ignored, the + patterns are used like it's on. + + The search starts exactly at the cursor. A match with + {start}, {middle} or {end} at the next character, in the + direction of searching, is the first one found. Example: > + if 1 + if 2 + endif 2 + endif 1 +< When starting at the "if 2", with the cursor on the "i", and + searching forwards, the "endif 2" is found. When starting on + the character just before the "if 2", the "endif 1" will be + found. That's because the "if 2" will be found first, and + then this is considered to be a nested if/endif from "if 2" to + "endif 2". + When searching backwards and {end} is more than one character, + it may be useful to put "\zs" at the end of the pattern, so + that when the cursor is inside a match with the end it finds + the matching start. + + Example, to find the "endif" command in a Vim script: > + + :echo searchpair('\<if\>', '\<el\%[seif]\>', '\<en\%[dif]\>', 'W', + \ 'getline(".") =~ "^\\s*\""') + +< The cursor must be at or after the "if" for which a match is + to be found. Note that single-quote strings are used to avoid + having to double the backslashes. The skip expression only + catches comments at the start of a line, not after a command. + Also, a word "en" or "if" halfway through a line is considered + a match. + Another example, to search for the matching "{" of a "}": > + + :echo searchpair('{', '', '}', 'bW') + +< This works when the cursor is at or before the "}" for which a + match is to be found. To reject matches that syntax + highlighting recognized as strings: > + + :echo searchpair('{', '', '}', 'bW', + \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') +< + *searchpairpos()* +searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} + [, {stopline} [, {timeout}]]]]) + Same as |searchpair()|, but returns a |List| with the line and + column position of the match. The first element of the |List| + is the line number and the second element is the byte index of + the column position of the match. If no match is found, + returns [0, 0]. > + + :let [lnum,col] = searchpairpos('{', '', '}', 'n') +< + See |match-parens| for a bigger and more useful example. + + *searchpos()* +searchpos({pattern} [, {flags} [, {stopline} [, {timeout} [, {skip}]]]]) + Same as |search()|, but returns a |List| with the line and + column position of the match. The first element of the |List| + is the line number and the second element is the byte index of + the column position of the match. If no match is found, + returns [0, 0]. + Example: > + :let [lnum, col] = searchpos('mypattern', 'n') + +< When the 'p' flag is given then there is an extra item with + the sub-pattern match number |search()-sub-match|. Example: > + :let [lnum, col, submatch] = searchpos('\(\l\)\|\(\u\)', 'np') +< In this example "submatch" is 2 when a lowercase letter is + found |/\l|, 3 when an uppercase letter is found |/\u|. + + Can also be used as a |method|: > + GetPattern()->searchpos() + +server2client({clientid}, {string}) *server2client()* + Send a reply string to {clientid}. The most recent {clientid} + that sent a string can be retrieved with expand("<client>"). + Note: + Returns zero for success, -1 for failure. + This id has to be stored before the next command can be + received. I.e. before returning from the received command and + before calling any commands that waits for input. + See also |clientserver|. + Example: > + :echo server2client(expand("<client>"), "HELLO") + +< Can also be used as a |method|: > + GetClientId()->server2client(string) +< +serverlist() *serverlist()* + Returns a list of server addresses, or empty if all servers + were stopped. |serverstart()| |serverstop()| + Example: > + :echo serverlist() + +serverstart([{address}]) *serverstart()* + Opens a socket or named pipe at {address} and listens for + |RPC| messages. Clients can send |API| commands to the address + to control Nvim. Returns the address string. + + If {address} does not contain a colon ":" it is interpreted as + a named pipe or Unix domain socket path. + + Example: > + if has('win32') + call serverstart('\\.\pipe\nvim-pipe-1234') + else + call serverstart('nvim.sock') + endif +< + If {address} contains a colon ":" it is interpreted as a TCP + address where the last ":" separates the host and port. + Assigns a random port if it is empty or 0. Supports IPv4/IPv6. + + Example: > + :call serverstart('::1:12345') +< + If no address is given, it is equivalent to: > + :call serverstart(tempname()) + +< |$NVIM_LISTEN_ADDRESS| is set to {address} if not already set. + +serverstop({address}) *serverstop()* + Closes the pipe or socket at {address}. + Returns TRUE if {address} is valid, else FALSE. + If |$NVIM_LISTEN_ADDRESS| is stopped it is unset. + If |v:servername| is stopped it is set to the next available + address returned by |serverlist()|. + +setbufline({buf}, {lnum}, {text}) *setbufline()* + Set line {lnum} to {text} in buffer {buf}. This works like + |setline()| for the specified buffer. + + This function works only for loaded buffers. First call + |bufload()| if needed. + + To insert lines use |appendbufline()|. + Any text properties in {lnum} are cleared. + + {text} can be a string to set one line, or a list of strings + to set multiple lines. If the list extends below the last + line then those lines are added. + + For the use of {buf}, see |bufname()| above. + + {lnum} is used like with |setline()|. + Use "$" to refer to the last line in buffer {buf}. + When {lnum} is just below the last line the {text} will be + added below the last line. + On success 0 is returned, on failure 1 is returned. + + If {buf} is not a valid buffer or {lnum} is not valid, an + error message is given. + + Can also be used as a |method|, the base is passed as the + third argument: > + GetText()->setbufline(buf, lnum) + +setbufvar({buf}, {varname}, {val}) *setbufvar()* + Set option or local variable {varname} in buffer {buf} to + {val}. + This also works for a global or local window option, but it + doesn't work for a global or local window variable. + For a local window option the global value is unchanged. + For the use of {buf}, see |bufname()| above. + The {varname} argument is a string. + Note that the variable name without "b:" must be used. + Examples: > + :call setbufvar(1, "&mod", 1) + :call setbufvar("todo", "myvar", "foobar") +< This function is not available in the |sandbox|. + + Can also be used as a |method|, the base is passed as the + third argument: > + GetValue()->setbufvar(buf, varname) + +setcharpos({expr}, {list}) *setcharpos()* + Same as |setpos()| but uses the specified column number as the + character index instead of the byte index in the line. + + Example: + With the text "여보세요" in line 8: > + call setcharpos('.', [0, 8, 4, 0]) +< positions the cursor on the fourth character '요'. > + call setpos('.', [0, 8, 4, 0]) +< positions the cursor on the second character '보'. + + Can also be used as a |method|: > + GetPosition()->setcharpos('.') + +setcharsearch({dict}) *setcharsearch()* + Set the current character search information to {dict}, + which contains one or more of the following entries: + + char character which will be used for a subsequent + |,| or |;| command; an empty string clears the + character search + forward direction of character search; 1 for forward, + 0 for backward + until type of character search; 1 for a |t| or |T| + character search, 0 for an |f| or |F| + character search + + This can be useful to save/restore a user's character search + from a script: > + :let prevsearch = getcharsearch() + :" Perform a command which clobbers user's search + :call setcharsearch(prevsearch) +< Also see |getcharsearch()|. + + Can also be used as a |method|: > + SavedSearch()->setcharsearch() + +setcmdpos({pos}) *setcmdpos()* + Set the cursor position in the command line to byte position + {pos}. The first position is 1. + Use |getcmdpos()| to obtain the current position. + Only works while editing the command line, thus you must use + |c_CTRL-\_e|, |c_CTRL-R_=| or |c_CTRL-R_CTRL-R| with '='. For + |c_CTRL-\_e| and |c_CTRL-R_CTRL-R| with '=' the position is + set after the command line is set to the expression. For + |c_CTRL-R_=| it is set after evaluating the expression but + before inserting the resulting text. + When the number is too big the cursor is put at the end of the + line. A number smaller than one has undefined results. + Returns FALSE when successful, TRUE when not editing the + command line. + + Can also be used as a |method|: > + GetPos()->setcmdpos() + +setcursorcharpos({lnum}, {col} [, {off}]) *setcursorcharpos()* +setcursorcharpos({list}) + Same as |cursor()| but uses the specified column number as the + character index instead of the byte index in the line. + + Example: + With the text "여보세요" in line 4: > + call setcursorcharpos(4, 3) +< positions the cursor on the third character '세'. > + call cursor(4, 3) +< positions the cursor on the first character '여'. + + Can also be used as a |method|: > + GetCursorPos()->setcursorcharpos() + +setenv({name}, {val}) *setenv()* + Set environment variable {name} to {val}. Example: > + call setenv('HOME', '/home/myhome') + +< When {val} is |v:null| the environment variable is deleted. + See also |expr-env|. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetPath()->setenv('PATH') + +setfperm({fname}, {mode}) *setfperm()* *chmod* + Set the file permissions for {fname} to {mode}. + {mode} must be a string with 9 characters. It is of the form + "rwxrwxrwx", where each group of "rwx" flags represent, in + turn, the permissions of the owner of the file, the group the + file belongs to, and other users. A '-' character means the + permission is off, any other character means on. Multi-byte + characters are not supported. + + For example "rw-r-----" means read-write for the user, + readable by the group, not accessible by others. "xx-x-----" + would do the same thing. + + Returns non-zero for success, zero for failure. + + Can also be used as a |method|: > + GetFilename()->setfperm(mode) +< + To read permissions see |getfperm()|. + +setline({lnum}, {text}) *setline()* + Set line {lnum} of the current buffer to {text}. To insert + lines use |append()|. To set lines in another buffer use + |setbufline()|. + + {lnum} is used like with |getline()|. + When {lnum} is just below the last line the {text} will be + added below the last line. + + If this succeeds, FALSE is returned. If this fails (most likely + because {lnum} is invalid) TRUE is returned. + + Example: > + :call setline(5, strftime("%c")) + +< When {text} is a |List| then line {lnum} and following lines + will be set to the items in the list. Example: > + :call setline(5, ['aaa', 'bbb', 'ccc']) +< This is equivalent to: > + :for [n, l] in [[5, 'aaa'], [6, 'bbb'], [7, 'ccc']] + : call setline(n, l) + :endfor + +< Note: The '[ and '] marks are not set. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetText()->setline(lnum) + +setloclist({nr}, {list} [, {action} [, {what}]]) *setloclist()* + Create or replace or add to the location list for window {nr}. + {nr} can be the window number or the |window-ID|. + When {nr} is zero the current window is used. + + For a location list window, the displayed location list is + modified. For an invalid window number {nr}, -1 is returned. + Otherwise, same as |setqflist()|. + Also see |location-list|. + + For {action} see |setqflist-action|. + + If the optional {what} dictionary argument is supplied, then + only the items listed in {what} are set. Refer to |setqflist()| + for the list of supported keys in {what}. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetLoclist()->setloclist(winnr) + +setmatches({list} [, {win}]) *setmatches()* + Restores a list of matches saved by |getmatches() for the + current window|. Returns 0 if successful, otherwise -1. All + current matches are cleared before the list is restored. See + example for |getmatches()|. + If {win} is specified, use the window with this number or + window ID instead of the current window. + + Can also be used as a |method|: > + GetMatches()->setmatches() +< + *setpos()* +setpos({expr}, {list}) + Set the position for String {expr}. Possible values: + . the cursor + 'x mark x + + {list} must be a |List| with four or five numbers: + [bufnum, lnum, col, off] + [bufnum, lnum, col, off, curswant] + + "bufnum" is the buffer number. Zero can be used for the + current buffer. When setting an uppercase mark "bufnum" is + used for the mark position. For other marks it specifies the + buffer to set the mark in. You can use the |bufnr()| function + to turn a file name into a buffer number. + For setting the cursor and the ' mark "bufnum" is ignored, + since these are associated with a window, not a buffer. + Does not change the jumplist. + + "lnum" and "col" are the position in the buffer. The first + column is 1. Use a zero "lnum" to delete a mark. If "col" is + smaller than 1 then 1 is used. To use the character count + instead of the byte count, use |setcharpos()|. + + The "off" number is only used when 'virtualedit' is set. Then + it is the offset in screen columns from the start of the + character. E.g., a position within a <Tab> or after the last + character. + + The "curswant" number is only used when setting the cursor + position. It sets the preferred column for when moving the + cursor vertically. When the "curswant" number is missing the + preferred column is not set. When it is present and setting a + mark position it is not used. + + Note that for '< and '> changing the line number may result in + the marks to be effectively be swapped, so that '< is always + before '>. + + Returns 0 when the position could be set, -1 otherwise. + An error message is given if {expr} is invalid. + + Also see |setcharpos()|, |getpos()| and |getcurpos()|. + + This does not restore the preferred column for moving + vertically; if you set the cursor position with this, |j| and + |k| motions will jump to previous columns! Use |cursor()| to + also set the preferred column. Also see the "curswant" key in + |winrestview()|. + + Can also be used as a |method|: > + GetPosition()->setpos('.') + +setqflist({list} [, {action} [, {what}]]) *setqflist()* + Create or replace or add to the quickfix list. + + If the optional {what} dictionary argument is supplied, then + only the items listed in {what} are set. The first {list} + argument is ignored. See below for the supported items in + {what}. + *setqflist-what* + When {what} is not present, the items in {list} are used. Each + item must be a dictionary. Non-dictionary items in {list} are + ignored. Each dictionary item can contain the following + entries: + + bufnr buffer number; must be the number of a valid + buffer + filename name of a file; only used when "bufnr" is not + present or it is invalid. + module name of a module; if given it will be used in + quickfix error window instead of the filename + lnum line number in the file + pattern search pattern used to locate the error + col column number + vcol when non-zero: "col" is visual column + when zero: "col" is byte index + nr error number + text description of the error + type single-character error type, 'E', 'W', etc. + valid recognized error message + + The "col", "vcol", "nr", "type" and "text" entries are + optional. Either "lnum" or "pattern" entry can be used to + locate a matching error line. + If the "filename" and "bufnr" entries are not present or + neither the "lnum" or "pattern" entries are present, then the + item will not be handled as an error line. + If both "pattern" and "lnum" are present then "pattern" will + be used. + If the "valid" entry is not supplied, then the valid flag is + set when "bufnr" is a valid buffer or "filename" exists. + If you supply an empty {list}, the quickfix list will be + cleared. + Note that the list is not exactly the same as what + |getqflist()| returns. + + {action} values: *setqflist-action* *E927* + 'a' The items from {list} are added to the existing + quickfix list. If there is no existing list, then a + new list is created. + + 'r' The items from the current quickfix list are replaced + with the items from {list}. This can also be used to + clear the list: > + :call setqflist([], 'r') +< + 'f' All the quickfix lists in the quickfix stack are + freed. + + If {action} is not present or is set to ' ', then a new list + is created. The new quickfix list is added after the current + quickfix list in the stack and all the following lists are + freed. To add a new quickfix list at the end of the stack, + set "nr" in {what} to "$". + + The following items can be specified in dictionary {what}: + context quickfix list context. See |quickfix-context| + efm errorformat to use when parsing text from + "lines". If this is not present, then the + 'errorformat' option value is used. + See |quickfix-parse| + id quickfix list identifier |quickfix-ID| + idx index of the current entry in the quickfix + list specified by 'id' or 'nr'. If set to '$', + then the last entry in the list is set as the + current entry. See |quickfix-index| + items list of quickfix entries. Same as the {list} + argument. + lines use 'errorformat' to parse a list of lines and + add the resulting entries to the quickfix list + {nr} or {id}. Only a |List| value is supported. + See |quickfix-parse| + nr list number in the quickfix stack; zero + means the current quickfix list and "$" means + the last quickfix list. + quickfixtextfunc + function to get the text to display in the + quickfix window. The value can be the name of + a function or a funcref or a lambda. Refer to + |quickfix-window-function| for an explanation + of how to write the function and an example. + title quickfix list title text. See |quickfix-title| + Unsupported keys in {what} are ignored. + If the "nr" item is not present, then the current quickfix list + is modified. When creating a new quickfix list, "nr" can be + set to a value one greater than the quickfix stack size. + When modifying a quickfix list, to guarantee that the correct + list is modified, "id" should be used instead of "nr" to + specify the list. + + Examples (See also |setqflist-examples|): > + :call setqflist([], 'r', {'title': 'My search'}) + :call setqflist([], 'r', {'nr': 2, 'title': 'Errors'}) + :call setqflist([], 'a', {'id':qfid, 'lines':["F1:10:L10"]}) +< + Returns zero for success, -1 for failure. + + This function can be used to create a quickfix list + independent of the 'errorformat' setting. Use a command like + `:cc 1` to jump to the first position. + + Can also be used as a |method|, the base is passed as the + second argument: > + GetErrorlist()->setqflist() +< + *setreg()* +setreg({regname}, {value} [, {options}]) + Set the register {regname} to {value}. + The {regname} argument is a string. + + {value} may be any value returned by |getreg()| or + |getreginfo()|, including a |List| or |Dict|. + If {options} contains "a" or {regname} is upper case, + then the value is appended. + + {options} can also contain a register type specification: + "c" or "v" |charwise| mode + "l" or "V" |linewise| mode + "b" or "<CTRL-V>" |blockwise-visual| mode + If a number immediately follows "b" or "<CTRL-V>" then this is + used as the width of the selection - if it is not specified + then the width of the block is set to the number of characters + in the longest line (counting a <Tab> as 1 character). + If {options} contains "u" or '"', then the unnamed register is + set to point to register {regname}. + + If {options} contains no register settings, then the default + is to use character mode unless {value} ends in a <NL> for + string {value} and linewise mode for list {value}. Blockwise + mode is never selected automatically. + Returns zero for success, non-zero for failure. + + *E883* + Note: you may not use |List| containing more than one item to + set search and expression registers. Lists containing no + items act like empty strings. + + Examples: > + :call setreg(v:register, @*) + :call setreg('*', @%, 'ac') + :call setreg('a', "1\n2\n3", 'b5') + :call setreg('"', { 'points_to': 'a'}) + +< This example shows using the functions to save and restore a + register: > + :let var_a = getreginfo() + :call setreg('a', var_a) +< or: > + :let var_a = getreg('a', 1, 1) + :let var_amode = getregtype('a') + .... + :call setreg('a', var_a, var_amode) +< Note: you may not reliably restore register value + without using the third argument to |getreg()| as without it + newlines are represented as newlines AND Nul bytes are + represented as newlines as well, see |NL-used-for-Nul|. + + You can also change the type of a register by appending + nothing: > + :call setreg('a', '', 'al') + +< Can also be used as a |method|, the base is passed as the + second argument: > + GetText()->setreg('a') + +settabvar({tabnr}, {varname}, {val}) *settabvar()* + Set tab-local variable {varname} to {val} in tab page {tabnr}. + |t:var| + The {varname} argument is a string. + Note that the variable name without "t:" must be used. + Tabs are numbered starting with one. + This function is not available in the |sandbox|. + + Can also be used as a |method|, the base is passed as the + third argument: > + GetValue()->settabvar(tab, name) + +settabwinvar({tabnr}, {winnr}, {varname}, {val}) *settabwinvar()* + Set option or local variable {varname} in window {winnr} to + {val}. + Tabs are numbered starting with one. For the current tabpage + use |setwinvar()|. + {winnr} can be the window number or the |window-ID|. + When {winnr} is zero the current window is used. + This also works for a global or local buffer option, but it + doesn't work for a global or local buffer variable. + For a local buffer option the global value is unchanged. + Note that the variable name without "w:" must be used. + Examples: > + :call settabwinvar(1, 1, "&list", 0) + :call settabwinvar(3, 2, "myvar", "foobar") +< This function is not available in the |sandbox|. + + Can also be used as a |method|, the base is passed as the + fourth argument: > + GetValue()->settabwinvar(tab, winnr, name) + +settagstack({nr}, {dict} [, {action}]) *settagstack()* + Modify the tag stack of the window {nr} using {dict}. + {nr} can be the window number or the |window-ID|. + + For a list of supported items in {dict}, refer to + |gettagstack()|. "curidx" takes effect before changing the tag + stack. + *E962* + How the tag stack is modified depends on the {action} + argument: + - If {action} is not present or is set to 'r', then the tag + stack is replaced. + - If {action} is set to 'a', then new entries from {dict} are + pushed (added) onto the tag stack. + - If {action} is set to 't', then all the entries from the + current entry in the tag stack or "curidx" in {dict} are + removed and then new entries are pushed to the stack. + + The current index is set to one after the length of the tag + stack after the modification. + + Returns zero for success, -1 for failure. + + Examples (for more examples see |tagstack-examples|): + Empty the tag stack of window 3: > + call settagstack(3, {'items' : []}) + +< Save and restore the tag stack: > + let stack = gettagstack(1003) + " do something else + call settagstack(1003, stack) + unlet stack +< + Can also be used as a |method|, the base is passed as the + second argument: > + GetStack()->settagstack(winnr) + +setwinvar({nr}, {varname}, {val}) *setwinvar()* + Like |settabwinvar()| for the current tab page. + Examples: > + :call setwinvar(1, "&list", 0) + :call setwinvar(2, "myvar", "foobar") + +< Can also be used as a |method|, the base is passed as the + third argument: > + GetValue()->setwinvar(winnr, name) + +sha256({string}) *sha256()* + Returns a String with 64 hex characters, which is the SHA256 + checksum of {string}. + + Can also be used as a |method|: > + GetText()->sha256() + +shellescape({string} [, {special}]) *shellescape()* + Escape {string} for use as a shell command argument. + + On Windows when 'shellslash' is not set, encloses {string} in + double-quotes and doubles all double-quotes within {string}. + Otherwise encloses {string} in single-quotes and replaces all + "'" with "'\''". + + If {special} is a |non-zero-arg|: + - Special items such as "!", "%", "#" and "<cword>" will be + preceded by a backslash. The backslash will be removed again + by the |:!| command. + - The <NL> character is escaped. + + If 'shell' contains "csh" in the tail: + - The "!" character will be escaped. This is because csh and + tcsh use "!" for history replacement even in single-quotes. + - The <NL> character is escaped (twice if {special} is + a |non-zero-arg|). + + If 'shell' contains "fish" in the tail, the "\" character will + be escaped because in fish it is used as an escape character + inside single quotes. + + Example of use with a |:!| command: > + :exe '!dir ' . shellescape(expand('<cfile>'), 1) +< This results in a directory listing for the file under the + cursor. Example of use with |system()|: > + :call system("chmod +w -- " . shellescape(expand("%"))) +< See also |::S|. + + Can also be used as a |method|: > + GetCommand()->shellescape() + +shiftwidth([{col}]) *shiftwidth()* + Returns the effective value of 'shiftwidth'. This is the + 'shiftwidth' value unless it is zero, in which case it is the + 'tabstop' value. To be backwards compatible in indent + plugins, use this: > + if exists('*shiftwidth') + func s:sw() + return shiftwidth() + endfunc + else + func s:sw() + return &sw + endfunc + endif +< And then use s:sw() instead of &sw. + + When there is one argument {col} this is used as column number + for which to return the 'shiftwidth' value. This matters for the + 'vartabstop' feature. If no {col} argument is given, column 1 + will be assumed. + + Can also be used as a |method|: > + GetColumn()->shiftwidth() + +sign_ functions are documented here: |sign-functions-details| + +simplify({filename}) *simplify()* + Simplify the file name as much as possible without changing + the meaning. Shortcuts (on MS-Windows) or symbolic links (on + Unix) are not resolved. If the first path component in + {filename} designates the current directory, this will be + valid for the result as well. A trailing path separator is + not removed either. On Unix "//path" is unchanged, but + "///path" is simplified to "/path" (this follows the Posix + standard). + Example: > + simplify("./dir/.././/file/") == "./file/" +< Note: The combination "dir/.." is only removed if "dir" is + a searchable directory or does not exist. On Unix, it is also + removed when "dir" is a symbolic link within the same + directory. In order to resolve all the involved symbolic + links before simplifying the path name, use |resolve()|. + + Can also be used as a |method|: > + GetName()->simplify() + +sin({expr}) *sin()* + Return the sine of {expr}, measured in radians, as a |Float|. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo sin(100) +< -0.506366 > + :echo sin(-4.01) +< 0.763301 + + Can also be used as a |method|: > + Compute()->sin() + +sinh({expr}) *sinh()* + Return the hyperbolic sine of {expr} as a |Float| in the range + [-inf, inf]. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo sinh(0.5) +< 0.521095 > + :echo sinh(-0.9) +< -1.026517 + + Can also be used as a |method|: > + Compute()->sinh() + +sockconnect({mode}, {address} [, {opts}]) *sockconnect()* + Connect a socket to an address. If {mode} is "pipe" then + {address} should be the path of a named pipe. If {mode} is + "tcp" then {address} should be of the form "host:port" where + the host should be an ip adderess or host name, and port the + port number. + + Returns a |channel| ID. Close the socket with |chanclose()|. + Use |chansend()| to send data over a bytes socket, and + |rpcrequest()| and |rpcnotify()| to communicate with a RPC + socket. + + {opts} is an optional dictionary with these keys: + |on_data| : callback invoked when data was read from socket + data_buffered : read socket data in |channel-buffered| mode. + rpc : If set, |msgpack-rpc| will be used to communicate + over the socket. + Returns: + - The channel ID on success (greater than zero) + - 0 on invalid arguments or connection failure. + +sort({list} [, {func} [, {dict}]]) *sort()* *E702* + Sort the items in {list} in-place. Returns {list}. + + If you want a list to remain unmodified make a copy first: > + :let sortedlist = sort(copy(mylist)) + +< When {func} is omitted, is empty or zero, then sort() uses the + string representation of each item to sort on. Numbers sort + after Strings, |Lists| after Numbers. For sorting text in the + current buffer use |:sort|. + + When {func} is given and it is '1' or 'i' then case is + ignored. + + When {func} is given and it is 'l' then the current collation + locale is used for ordering. Implementation details: strcoll() + is used to compare strings. See |:language| check or set the + collation locale. |v:collate| can also be used to check the + current locale. Sorting using the locale typically ignores + case. Example: > + " ö is sorted similarly to o with English locale. + :language collate en_US.UTF8 + :echo sort(['n', 'o', 'O', 'ö', 'p', 'z'], 'l') +< ['n', 'o', 'O', 'ö', 'p', 'z'] ~ +> + " ö is sorted after z with Swedish locale. + :language collate sv_SE.UTF8 + :echo sort(['n', 'o', 'O', 'ö', 'p', 'z'], 'l') +< ['n', 'o', 'O', 'p', 'z', 'ö'] ~ + This does not work properly on Mac. + + When {func} is given and it is 'n' then all items will be + sorted numerical (Implementation detail: this uses the + strtod() function to parse numbers, Strings, Lists, Dicts and + Funcrefs will be considered as being 0). + + When {func} is given and it is 'N' then all items will be + sorted numerical. This is like 'n' but a string containing + digits will be used as the number they represent. + + When {func} is given and it is 'f' then all items will be + sorted numerical. All values must be a Number or a Float. + + When {func} is a |Funcref| or a function name, this function + is called to compare items. The function is invoked with two + items as argument and must return zero if they are equal, 1 or + bigger if the first one sorts after the second one, -1 or + smaller if the first one sorts before the second one. + + {dict} is for functions with the "dict" attribute. It will be + used to set the local variable "self". |Dictionary-function| + + The sort is stable, items which compare equal (as number or as + string) will keep their relative position. E.g., when sorting + on numbers, text strings will sort next to each other, in the + same order as they were originally. + + Can also be used as a |method|: > + mylist->sort() + +< Also see |uniq()|. + + Example: > + func MyCompare(i1, i2) + return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1 + endfunc + eval mylist->sort("MyCompare") +< A shorter compare version for this specific simple case, which + ignores overflow: > + func MyCompare(i1, i2) + return a:i1 - a:i2 + endfunc +< For a simple expression you can use a lambda: > + eval mylist->sort({i1, i2 -> i1 - i2}) +< + *soundfold()* +soundfold({word}) + Return the sound-folded equivalent of {word}. Uses the first + language in 'spelllang' for the current window that supports + soundfolding. 'spell' must be set. When no sound folding is + possible the {word} is returned unmodified. + This can be used for making spelling suggestions. Note that + the method can be quite slow. + + Can also be used as a |method|: > + GetWord()->soundfold() +< + *spellbadword()* +spellbadword([{sentence}]) + Without argument: The result is the badly spelled word under + or after the cursor. The cursor is moved to the start of the + bad word. When no bad word is found in the cursor line the + result is an empty string and the cursor doesn't move. + + With argument: The result is the first word in {sentence} that + is badly spelled. If there are no spelling mistakes the + result is an empty string. + + The return value is a list with two items: + - The badly spelled word or an empty string. + - The type of the spelling error: + "bad" spelling mistake + "rare" rare word + "local" word only valid in another region + "caps" word should start with Capital + Example: > + echo spellbadword("the quik brown fox") +< ['quik', 'bad'] ~ + + The spelling information for the current window and the value + of 'spelllang' are used. + + Can also be used as a |method|: > + GetText()->spellbadword() +< + *spellsuggest()* +spellsuggest({word} [, {max} [, {capital}]]) + Return a |List| with spelling suggestions to replace {word}. + When {max} is given up to this number of suggestions are + returned. Otherwise up to 25 suggestions are returned. + + When the {capital} argument is given and it's non-zero only + suggestions with a leading capital will be given. Use this + after a match with 'spellcapcheck'. + + {word} can be a badly spelled word followed by other text. + This allows for joining two words that were split. The + suggestions also include the following text, thus you can + replace a line. + + {word} may also be a good word. Similar words will then be + returned. {word} itself is not included in the suggestions, + although it may appear capitalized. + + The spelling information for the current window is used. The + values of 'spelllang' and 'spellsuggest' are used. + + Can also be used as a |method|: > + GetWord()->spellsuggest() + +split({string} [, {pattern} [, {keepempty}]]) *split()* + Make a |List| out of {string}. When {pattern} is omitted or + empty each white-separated sequence of characters becomes an + item. + Otherwise the string is split where {pattern} matches, + removing the matched characters. 'ignorecase' is not used + here, add \c to ignore case. |/\c| + When the first or last item is empty it is omitted, unless the + {keepempty} argument is given and it's non-zero. + Other empty items are kept when {pattern} matches at least one + character or when {keepempty} is non-zero. + Example: > + :let words = split(getline('.'), '\W\+') +< To split a string in individual characters: > + :for c in split(mystring, '\zs') +< If you want to keep the separator you can also use '\zs' at + the end of the pattern: > + :echo split('abc:def:ghi', ':\zs') +< ['abc:', 'def:', 'ghi'] ~ + Splitting a table where the first element can be empty: > + :let items = split(line, ':', 1) +< The opposite function is |join()|. + + Can also be used as a |method|: > + GetString()->split() + +sqrt({expr}) *sqrt()* + Return the non-negative square root of Float {expr} as a + |Float|. + {expr} must evaluate to a |Float| or a |Number|. When {expr} + is negative the result is NaN (Not a Number). + Examples: > + :echo sqrt(100) +< 10.0 > + :echo sqrt(-4.01) +< nan + "nan" may be different, it depends on system libraries. + + Can also be used as a |method|: > + Compute()->sqrt() + +srand([{expr}]) *srand()* + Initialize seed used by |rand()|: + - If {expr} is not given, seed values are initialized by + reading from /dev/urandom, if possible, or using time(NULL) + a.k.a. epoch time otherwise; this only has second accuracy. + - If {expr} is given it must be a Number. It is used to + initialize the seed values. This is useful for testing or + when a predictable sequence is intended. + + Examples: > + :let seed = srand() + :let seed = srand(userinput) + :echo rand(seed) +< + Can also be used as a |method|: > + userinput->srand() + +stdioopen({opts}) *stdioopen()* + With |--headless| this opens stdin and stdout as a |channel|. + May be called only once. See |channel-stdio|. stderr is not + handled by this function, see |v:stderr|. + + Close the stdio handles with |chanclose()|. Use |chansend()| + to send data to stdout, and |rpcrequest()| and |rpcnotify()| + to communicate over RPC. + + {opts} is a dictionary with these keys: + |on_stdin| : callback invoked when stdin is written to. + on_print : callback invoked when Nvim needs to print a + message, with the message (whose type is string) + as sole argument. + stdin_buffered : read stdin in |channel-buffered| mode. + rpc : If set, |msgpack-rpc| will be used to communicate + over stdio + Returns: + - |channel-id| on success (value is always 1) + - 0 on invalid arguments + + +stdpath({what}) *stdpath()* *E6100* + Returns |standard-path| locations of various default files and + directories. + + {what} Type Description ~ + cache String Cache directory. Arbitrary temporary + storage for plugins, etc. + config String User configuration directory. The + |init.vim| is stored here. + config_dirs List Additional configuration directories. + data String User data directory. The |shada-file| + is stored here. + data_dirs List Additional data directories. + + Example: > + :echo stdpath("config") + + +str2float({string} [, {quoted}]) *str2float()* + Convert String {string} to a Float. This mostly works the + same as when using a floating point number in an expression, + see |floating-point-format|. But it's a bit more permissive. + E.g., "1e40" is accepted, while in an expression you need to + write "1.0e40". The hexadecimal form "0x123" is also + accepted, but not others, like binary or octal. + When {quoted} is present and non-zero then embedded single + quotes before the dot are ignored, thus "1'000.0" is a + thousand. + Text after the number is silently ignored. + The decimal point is always '.', no matter what the locale is + set to. A comma ends the number: "12,345.67" is converted to + 12.0. You can strip out thousands separators with + |substitute()|: > + let f = str2float(substitute(text, ',', '', 'g')) +< + Can also be used as a |method|: > + let f = text->substitute(',', '', 'g')->str2float() + +str2list({string} [, {utf8}]) *str2list()* + Return a list containing the number values which represent + each character in String {string}. Examples: > + str2list(" ") returns [32] + str2list("ABC") returns [65, 66, 67] +< |list2str()| does the opposite. + + UTF-8 encoding is always used, {utf8} option has no effect, + and exists only for backwards-compatibility. + With UTF-8 composing characters are handled properly: > + str2list("á") returns [97, 769] + +< Can also be used as a |method|: > + GetString()->str2list() + +str2nr({string} [, {base}]) *str2nr()* + Convert string {string} to a number. + {base} is the conversion base, it can be 2, 8, 10 or 16. + When {quoted} is present and non-zero then embedded single + quotes are ignored, thus "1'000'000" is a million. + + When {base} is omitted base 10 is used. This also means that + a leading zero doesn't cause octal conversion to be used, as + with the default String to Number conversion. Example: > + let nr = str2nr('0123') +< + When {base} is 16 a leading "0x" or "0X" is ignored. With a + different base the result will be zero. Similarly, when + {base} is 8 a leading "0", "0o" or "0O" is ignored, and when + {base} is 2 a leading "0b" or "0B" is ignored. + Text after the number is silently ignored. + + Can also be used as a |method|: > + GetText()->str2nr() + +strcharpart({src}, {start} [, {len}]) *strcharpart()* + Like |strpart()| but using character index and length instead + of byte index and length. Composing characters are counted + separately. + When a character index is used where a character does not + exist it is assumed to be one character. For example: > + strcharpart('abc', -1, 2) +< results in 'a'. + + Can also be used as a |method|: > + GetText()->strcharpart(5) + +strchars({string} [, {skipcc}]) *strchars()* + The result is a Number, which is the number of characters + in String {string}. + When {skipcc} is omitted or zero, composing characters are + counted separately. + When {skipcc} set to 1, Composing characters are ignored. + Also see |strlen()|, |strdisplaywidth()| and |strwidth()|. + + {skipcc} is only available after 7.4.755. For backward + compatibility, you can define a wrapper function: > + if has("patch-7.4.755") + function s:strchars(str, skipcc) + return strchars(a:str, a:skipcc) + endfunction + else + function s:strchars(str, skipcc) + if a:skipcc + return strlen(substitute(a:str, ".", "x", "g")) + else + return strchars(a:str) + endif + endfunction + endif +< + Can also be used as a |method|: > + GetText()->strchars() + +strdisplaywidth({string} [, {col}]) *strdisplaywidth()* + The result is a Number, which is the number of display cells + String {string} occupies on the screen when it starts at {col} + (first column is zero). When {col} is omitted zero is used. + Otherwise it is the screen column where to start. This + matters for Tab characters. + The option settings of the current window are used. This + matters for anything that's displayed differently, such as + 'tabstop' and 'display'. + When {string} contains characters with East Asian Width Class + Ambiguous, this function's return value depends on 'ambiwidth'. + Also see |strlen()|, |strwidth()| and |strchars()|. + + Can also be used as a |method|: > + GetText()->strdisplaywidth() + +strftime({format} [, {time}]) *strftime()* + The result is a String, which is a formatted date and time, as + specified by the {format} string. The given {time} is used, + or the current time if no time is given. The accepted + {format} depends on your system, thus this is not portable! + See the manual page of the C function strftime() for the + format. The maximum length of the result is 80 characters. + See also |localtime()|, |getftime()| and |strptime()|. + The language can be changed with the |:language| command. + Examples: > + :echo strftime("%c") Sun Apr 27 11:49:23 1997 + :echo strftime("%Y %b %d %X") 1997 Apr 27 11:53:25 + :echo strftime("%y%m%d %T") 970427 11:53:55 + :echo strftime("%H:%M") 11:55 + :echo strftime("%c", getftime("file.c")) + Show mod time of file.c. + +< Can also be used as a |method|: > + GetFormat()->strftime() + +strgetchar({str}, {index}) *strgetchar()* + Get character {index} from {str}. This uses a character + index, not a byte index. Composing characters are considered + separate characters here. + Also see |strcharpart()| and |strchars()|. + + Can also be used as a |method|: > + GetText()->strgetchar(5) + +stridx({haystack}, {needle} [, {start}]) *stridx()* + The result is a Number, which gives the byte index in + {haystack} of the first occurrence of the String {needle}. + If {start} is specified, the search starts at index {start}. + This can be used to find a second match: > + :let colon1 = stridx(line, ":") + :let colon2 = stridx(line, ":", colon1 + 1) +< The search is done case-sensitive. + For pattern searches use |match()|. + -1 is returned if the {needle} does not occur in {haystack}. + See also |strridx()|. + Examples: > + :echo stridx("An Example", "Example") 3 + :echo stridx("Starting point", "Start") 0 + :echo stridx("Starting point", "start") -1 +< *strstr()* *strchr()* + stridx() works similar to the C function strstr(). When used + with a single character it works similar to strchr(). + + Can also be used as a |method|: > + GetHaystack()->stridx(needle) + + *string()* +string({expr}) Return {expr} converted to a String. If {expr} is a Number, + Float, String, Blob or a composition of them, then the result + can be parsed back with |eval()|. + {expr} type result ~ + String 'string' + Number 123 + Float 123.123456 or 1.123456e8 or + `str2float('inf')` + Funcref `function('name')` + Blob 0z00112233.44556677.8899 + List [item, item] + Dictionary {key: value, key: value} + Note that in String values the ' character is doubled. + Also see |strtrans()|. + Note 2: Output format is mostly compatible with YAML, except + for infinite and NaN floating-point values representations + which use |str2float()|. Strings are also dumped literally, + only single quote is escaped, which does not allow using YAML + for parsing back binary strings. |eval()| should always work for + strings and floats though and this is the only official + method, use |msgpackdump()| or |json_encode()| if you need to + share data with other application. + + Can also be used as a |method|: > + mylist->string() + +strlen({string}) *strlen()* + The result is a Number, which is the length of the String + {string} in bytes. + If the argument is a Number it is first converted to a String. + For other types an error is given. + If you want to count the number of multibyte characters use + |strchars()|. + Also see |len()|, |strdisplaywidth()| and |strwidth()|. + + Can also be used as a |method|: > + GetString()->strlen() + +strpart({src}, {start} [, {len} [, {chars}]]) *strpart()* + The result is a String, which is part of {src}, starting from + byte {start}, with the byte length {len}. + When {chars} is present and TRUE then {len} is the number of + characters positions (composing characters are not counted + separately, thus "1" means one base character and any + following composing characters). + To count {start} as characters instead of bytes use + |strcharpart()|. + + When bytes are selected which do not exist, this doesn't + result in an error, the bytes are simply omitted. + If {len} is missing, the copy continues from {start} till the + end of the {src}. > + strpart("abcdefg", 3, 2) == "de" + strpart("abcdefg", -2, 4) == "ab" + strpart("abcdefg", 5, 4) == "fg" + strpart("abcdefg", 3) == "defg" + +< Note: To get the first character, {start} must be 0. For + example, to get the character under the cursor: > + strpart(getline("."), col(".") - 1, 1, v:true) +< + Can also be used as a |method|: > + GetText()->strpart(5) + +strptime({format}, {timestring}) *strptime()* + The result is a Number, which is a unix timestamp representing + the date and time in {timestring}, which is expected to match + the format specified in {format}. + + The accepted {format} depends on your system, thus this is not + portable! See the manual page of the C function strptime() + for the format. Especially avoid "%c". The value of $TZ also + matters. + + If the {timestring} cannot be parsed with {format} zero is + returned. If you do not know the format of {timestring} you + can try different {format} values until you get a non-zero + result. + + See also |strftime()|. + Examples: > + :echo strptime("%Y %b %d %X", "1997 Apr 27 11:49:23") +< 862156163 > + :echo strftime("%c", strptime("%y%m%d %T", "970427 11:53:55")) +< Sun Apr 27 11:53:55 1997 > + :echo strftime("%c", strptime("%Y%m%d%H%M%S", "19970427115355") + 3600) +< Sun Apr 27 12:53:55 1997 + + Can also be used as a |method|: > + GetFormat()->strptime(timestring) +< +strridx({haystack}, {needle} [, {start}]) *strridx()* + The result is a Number, which gives the byte index in + {haystack} of the last occurrence of the String {needle}. + When {start} is specified, matches beyond this index are + ignored. This can be used to find a match before a previous + match: > + :let lastcomma = strridx(line, ",") + :let comma2 = strridx(line, ",", lastcomma - 1) +< The search is done case-sensitive. + For pattern searches use |match()|. + -1 is returned if the {needle} does not occur in {haystack}. + If the {needle} is empty the length of {haystack} is returned. + See also |stridx()|. Examples: > + :echo strridx("an angry armadillo", "an") 3 +< *strrchr()* + When used with a single character it works similar to the C + function strrchr(). + + Can also be used as a |method|: > + GetHaystack()->strridx(needle) + +strtrans({string}) *strtrans()* + The result is a String, which is {string} with all unprintable + characters translated into printable characters |'isprint'|. + Like they are shown in a window. Example: > + echo strtrans(@a) +< This displays a newline in register a as "^@" instead of + starting a new line. + + Can also be used as a |method|: > + GetString()->strtrans() + +strwidth({string}) *strwidth()* + The result is a Number, which is the number of display cells + String {string} occupies. A Tab character is counted as one + cell, alternatively use |strdisplaywidth()|. + When {string} contains characters with East Asian Width Class + Ambiguous, this function's return value depends on 'ambiwidth'. + Also see |strlen()|, |strdisplaywidth()| and |strchars()|. + + Can also be used as a |method|: > + GetString()->strwidth() + +submatch({nr} [, {list}]) *submatch()* *E935* + Only for an expression in a |:substitute| command or + substitute() function. + Returns the {nr}'th submatch of the matched text. When {nr} + is 0 the whole matched text is returned. + Note that a NL in the string can stand for a line break of a + multi-line match or a NUL character in the text. + Also see |sub-replace-expression|. + + If {list} is present and non-zero then submatch() returns + a list of strings, similar to |getline()| with two arguments. + NL characters in the text represent NUL characters in the + text. + Only returns more than one item for |:substitute|, inside + |substitute()| this list will always contain one or zero + items, since there are no real line breaks. + + When substitute() is used recursively only the submatches in + the current (deepest) call can be obtained. + + Examples: > + :s/\d\+/\=submatch(0) + 1/ + :echo substitute(text, '\d\+', '\=submatch(0) + 1', '') +< This finds the first number in the line and adds one to it. + A line break is included as a newline character. + + Can also be used as a |method|: > + GetNr()->submatch() + +substitute({string}, {pat}, {sub}, {flags}) *substitute()* + The result is a String, which is a copy of {string}, in which + the first match of {pat} is replaced with {sub}. + When {flags} is "g", all matches of {pat} in {string} are + replaced. Otherwise {flags} should be "". + + This works like the ":substitute" command (without any flags). + But the matching with {pat} is always done like the 'magic' + option is set and 'cpoptions' is empty (to make scripts + portable). 'ignorecase' is still relevant, use |/\c| or |/\C| + if you want to ignore or match case and ignore 'ignorecase'. + 'smartcase' is not used. See |string-match| for how {pat} is + used. + + A "~" in {sub} is not replaced with the previous {sub}. + Note that some codes in {sub} have a special meaning + |sub-replace-special|. For example, to replace something with + "\n" (two characters), use "\\\\n" or '\\n'. + + When {pat} does not match in {string}, {string} is returned + unmodified. + + Example: > + :let &path = substitute(&path, ",\\=[^,]*$", "", "") +< This removes the last component of the 'path' option. > + :echo substitute("testing", ".*", "\\U\\0", "") +< results in "TESTING". + + When {sub} starts with "\=", the remainder is interpreted as + an expression. See |sub-replace-expression|. Example: > + :echo substitute(s, '%\(\x\x\)', + \ '\=nr2char("0x" . submatch(1))', 'g') + +< When {sub} is a Funcref that function is called, with one + optional argument. Example: > + :echo substitute(s, '%\(\x\x\)', SubNr, 'g') +< The optional argument is a list which contains the whole + matched string and up to nine submatches, like what + |submatch()| returns. Example: > + :echo substitute(s, '%\(\x\x\)', {m -> '0x' . m[1]}, 'g') + +< Can also be used as a |method|: > + GetString()->substitute(pat, sub, flags) + +swapinfo({fname}) *swapinfo()* + The result is a dictionary, which holds information about the + swapfile {fname}. The available fields are: + version Vim version + user user name + host host name + fname original file name + pid PID of the Vim process that created the swap + file + mtime last modification time in seconds + inode Optional: INODE number of the file + dirty 1 if file was modified, 0 if not + In case of failure an "error" item is added with the reason: + Cannot open file: file not found or in accessible + Cannot read file: cannot read first block + Not a swap file: does not contain correct block ID + Magic number mismatch: Info in first block is invalid + + Can also be used as a |method|: > + GetFilename()->swapinfo() + +swapname({buf}) *swapname()* + The result is the swap file path of the buffer {buf}. + For the use of {buf}, see |bufname()| above. + If buffer {buf} is the current buffer, the result is equal to + |:swapname| (unless there is no swap file). + If buffer {buf} has no swap file, returns an empty string. + + Can also be used as a |method|: > + GetBufname()->swapname() + +synID({lnum}, {col}, {trans}) *synID()* + The result is a Number, which is the syntax ID at the position + {lnum} and {col} in the current window. + The syntax ID can be used with |synIDattr()| and + |synIDtrans()| to obtain syntax information about text. + + {col} is 1 for the leftmost column, {lnum} is 1 for the first + line. 'synmaxcol' applies, in a longer line zero is returned. + Note that when the position is after the last character, + that's where the cursor can be in Insert mode, synID() returns + zero. {lnum} is used like with |getline()|. + + When {trans} is |TRUE|, transparent items are reduced to the + item that they reveal. This is useful when wanting to know + the effective color. When {trans} is |FALSE|, the transparent + item is returned. This is useful when wanting to know which + syntax item is effective (e.g. inside parens). + Warning: This function can be very slow. Best speed is + obtained by going through the file in forward direction. + + Example (echoes the name of the syntax item under the cursor): > + :echo synIDattr(synID(line("."), col("."), 1), "name") +< + +synIDattr({synID}, {what} [, {mode}]) *synIDattr()* + The result is a String, which is the {what} attribute of + syntax ID {synID}. This can be used to obtain information + about a syntax item. + {mode} can be "gui", "cterm" or "term", to get the attributes + for that mode. When {mode} is omitted, or an invalid value is + used, the attributes for the currently active highlighting are + used (GUI, cterm or term). + Use synIDtrans() to follow linked highlight groups. + {what} result + "name" the name of the syntax item + "fg" foreground color (GUI: color name used to set + the color, cterm: color number as a string, + term: empty string) + "bg" background color (as with "fg") + "font" font name (only available in the GUI) + |highlight-font| + "sp" special color (as with "fg") |highlight-guisp| + "fg#" like "fg", but for the GUI and the GUI is + running the name in "#RRGGBB" form + "bg#" like "fg#" for "bg" + "sp#" like "fg#" for "sp" + "bold" "1" if bold + "italic" "1" if italic + "reverse" "1" if reverse + "inverse" "1" if inverse (= reverse) + "standout" "1" if standout + "underline" "1" if underlined + "undercurl" "1" if undercurled + "strikethrough" "1" if struckthrough + + Example (echoes the color of the syntax item under the + cursor): > + :echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg") +< + Can also be used as a |method|: > + :echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg") + +synIDtrans({synID}) *synIDtrans()* + The result is a Number, which is the translated syntax ID of + {synID}. This is the syntax group ID of what is being used to + highlight the character. Highlight links given with + ":highlight link" are followed. + + Can also be used as a |method|: > + :echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg") + +synconcealed({lnum}, {col}) *synconcealed()* + The result is a |List| with currently three items: + 1. The first item in the list is 0 if the character at the + position {lnum} and {col} is not part of a concealable + region, 1 if it is. {lnum} is used like with |getline()|. + 2. The second item in the list is a string. If the first item + is 1, the second item contains the text which will be + displayed in place of the concealed text, depending on the + current setting of 'conceallevel' and 'listchars'. + 3. The third and final item in the list is a number + representing the specific syntax region matched in the + line. When the character is not concealed the value is + zero. This allows detection of the beginning of a new + concealable region if there are two consecutive regions + with the same replacement character. For an example, if + the text is "123456" and both "23" and "45" are concealed + and replaced by the character "X", then: + call returns ~ + synconcealed(lnum, 1) [0, '', 0] + synconcealed(lnum, 2) [1, 'X', 1] + synconcealed(lnum, 3) [1, 'X', 1] + synconcealed(lnum, 4) [1, 'X', 2] + synconcealed(lnum, 5) [1, 'X', 2] + synconcealed(lnum, 6) [0, '', 0] + + +synstack({lnum}, {col}) *synstack()* + Return a |List|, which is the stack of syntax items at the + position {lnum} and {col} in the current window. {lnum} is + used like with |getline()|. Each item in the List is an ID + like what |synID()| returns. + The first item in the List is the outer region, following are + items contained in that one. The last one is what |synID()| + returns, unless not the whole item is highlighted or it is a + transparent item. + This function is useful for debugging a syntax file. + Example that shows the syntax stack under the cursor: > + for id in synstack(line("."), col(".")) + echo synIDattr(id, "name") + endfor +< When the position specified with {lnum} and {col} is invalid + nothing is returned. The position just after the last + character in a line and the first column in an empty line are + valid positions. + +system({cmd} [, {input}]) *system()* *E677* + Gets the output of {cmd} as a |string| (|systemlist()| returns + a |List|) and sets |v:shell_error| to the error code. + {cmd} is treated as in |jobstart()|: + If {cmd} is a List it runs directly (no 'shell'). + If {cmd} is a String it runs in the 'shell', like this: > + :call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}']) + +< Not to be used for interactive commands. + + Result is a String, filtered to avoid platform-specific quirks: + - <CR><NL> is replaced with <NL> + - NUL characters are replaced with SOH (0x01) + + Example: > + :echo system(['ls', expand('%:h')]) + +< If {input} is a string it is written to a pipe and passed as + stdin to the command. The string is written as-is, line + separators are not changed. + If {input} is a |List| it is written to the pipe as + |writefile()| does with {binary} set to "b" (i.e. with + a newline between each list item, and newlines inside list + items converted to NULs). + When {input} is given and is a valid buffer id, the content of + the buffer is written to the file line by line, each line + terminated by NL (and NUL where the text has NL). + *E5677* + Note: system() cannot write to or read from backgrounded ("&") + shell commands, e.g.: > + :echo system("cat - &", "foo") +< which is equivalent to: > + $ echo foo | bash -c 'cat - &' +< The pipes are disconnected (unless overridden by shell + redirection syntax) before input can reach it. Use + |jobstart()| instead. + + Note: Use |shellescape()| or |::S| with |expand()| or + |fnamemodify()| to escape special characters in a command + argument. 'shellquote' and 'shellxquote' must be properly + configured. Example: > + :echo system('ls '..shellescape(expand('%:h'))) + :echo system('ls '..expand('%:h:S')) + +< Unlike ":!cmd" there is no automatic check for changed files. + Use |:checktime| to force a check. + + Can also be used as a |method|: > + :echo GetCmd()->system() + +systemlist({cmd} [, {input} [, {keepempty}]]) *systemlist()* + Same as |system()|, but returns a |List| with lines (parts of + output separated by NL) with NULs transformed into NLs. Output + is the same as |readfile()| will output with {binary} argument + set to "b", except that a final newline is not preserved, + unless {keepempty} is non-zero. + Note that on MS-Windows you may get trailing CR characters. + + To see the difference between "echo hello" and "echo -n hello" + use |system()| and |split()|: > + echo split(system('echo hello'), '\n', 1) +< + Returns an empty string on error. + + Can also be used as a |method|: > + :echo GetCmd()->systemlist() + +tabpagebuflist([{arg}]) *tabpagebuflist()* + The result is a |List|, where each item is the number of the + buffer associated with each window in the current tab page. + {arg} specifies the number of the tab page to be used. When + omitted the current tab page is used. + When {arg} is invalid the number zero is returned. + To get a list of all buffers in all tabs use this: > + let buflist = [] + for i in range(tabpagenr('$')) + call extend(buflist, tabpagebuflist(i + 1)) + endfor +< Note that a buffer may appear in more than one window. + + Can also be used as a |method|: > + GetTabpage()->tabpagebuflist() + +tabpagenr([{arg}]) *tabpagenr()* + The result is a Number, which is the number of the current + tab page. The first tab page has number 1. + The optional argument {arg} supports the following values: + $ the number of the last tab page (the tab page + count). + # the number of the last accessed tab page (where + |g<Tab>| goes to). If there is no previous + tab page, 0 is returned. + The number can be used with the |:tab| command. + + +tabpagewinnr({tabarg} [, {arg}]) *tabpagewinnr()* + Like |winnr()| but for tab page {tabarg}. + {tabarg} specifies the number of tab page to be used. + {arg} is used like with |winnr()|: + - When omitted the current window number is returned. This is + the window which will be used when going to this tab page. + - When "$" the number of windows is returned. + - When "#" the previous window nr is returned. + Useful examples: > + tabpagewinnr(1) " current window of tab page 1 + tabpagewinnr(4, '$') " number of windows in tab page 4 +< When {tabarg} is invalid zero is returned. + + Can also be used as a |method|: > + GetTabpage()->tabpagewinnr() +< + *tagfiles()* +tagfiles() Returns a |List| with the file names used to search for tags + for the current buffer. This is the 'tags' option expanded. + + +taglist({expr} [, {filename}]) *taglist()* + Returns a |List| of tags matching the regular expression {expr}. + + If {filename} is passed it is used to prioritize the results + in the same way that |:tselect| does. See |tag-priority|. + {filename} should be the full path of the file. + + Each list item is a dictionary with at least the following + entries: + name Name of the tag. + filename Name of the file where the tag is + defined. It is either relative to the + current directory or a full path. + cmd Ex command used to locate the tag in + the file. + kind Type of the tag. The value for this + entry depends on the language specific + kind values. Only available when + using a tags file generated by + Exuberant ctags or hdrtag. + static A file specific tag. Refer to + |static-tag| for more information. + More entries may be present, depending on the content of the + tags file: access, implementation, inherits and signature. + Refer to the ctags documentation for information about these + fields. For C code the fields "struct", "class" and "enum" + may appear, they give the name of the entity the tag is + contained in. + + The ex-command "cmd" can be either an ex search pattern, a + line number or a line number followed by a byte number. + + If there are no matching tags, then an empty list is returned. + + To get an exact tag match, the anchors '^' and '$' should be + used in {expr}. This also make the function work faster. + Refer to |tag-regexp| for more information about the tag + search regular expression pattern. + + Refer to |'tags'| for information about how the tags file is + located by Vim. Refer to |tags-file-format| for the format of + the tags file generated by the different ctags tools. + + Can also be used as a |method|: > + GetTagpattern()->taglist() + +tempname() *tempname()* *temp-file-name* + The result is a String, which is the name of a file that + doesn't exist. It can be used for a temporary file. Example: > + :let tmpfile = tempname() + :exe "redir > " . tmpfile +< For Unix, the file will be in a private directory |tempfile|. + For MS-Windows forward slashes are used when the 'shellslash' + option is set or when 'shellcmdflag' starts with '-'. + +termopen({cmd} [, {opts}]) *termopen()* + Spawns {cmd} in a new pseudo-terminal session connected + to the current buffer. {cmd} is the same as the one passed to + |jobstart()|. This function fails if the current buffer is + modified (all buffer contents are destroyed). + + The {opts} dict is similar to the one passed to |jobstart()|, + but the `pty`, `width`, `height`, and `TERM` fields are + ignored: `height`/`width` are taken from the current window + and `$TERM` is set to "xterm-256color". + Returns the same values as |jobstart()|. + + See |terminal| for more information. + +test_ functions are documented here: |test-functions-details| + +tan({expr}) *tan()* + Return the tangent of {expr}, measured in radians, as a |Float| + in the range [-inf, inf]. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo tan(10) +< 0.648361 > + :echo tan(-4.01) +< -1.181502 + + Can also be used as a |method|: > + Compute()->tan() + +tanh({expr}) *tanh()* + Return the hyperbolic tangent of {expr} as a |Float| in the + range [-1, 1]. + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + :echo tanh(0.5) +< 0.462117 > + :echo tanh(-1) +< -0.761594 + + Can also be used as a |method|: > + Compute()->tanh() +< + *timer_info()* +timer_info([{id}]) + Return a list with information about timers. + When {id} is given only information about this timer is + returned. When timer {id} does not exist an empty list is + returned. + When {id} is omitted information about all timers is returned. + + For each timer the information is stored in a |Dictionary| with + these items: + "id" the timer ID + "time" time the timer was started with + "repeat" number of times the timer will still fire; + -1 means forever + "callback" the callback + + Can also be used as a |method|: > + GetTimer()->timer_info() +< +timer_pause({timer}, {paused}) *timer_pause()* + Pause or unpause a timer. A paused timer does not invoke its + callback when its time expires. Unpausing a timer may cause + the callback to be invoked almost immediately if enough time + has passed. + + Pausing a timer is useful to avoid the callback to be called + for a short time. + + If {paused} evaluates to a non-zero Number or a non-empty + String, then the timer is paused, otherwise it is unpaused. + See |non-zero-arg|. + + Can also be used as a |method|: > + GetTimer()->timer_pause(1) +< + *timer_start()* *timer* *timers* +timer_start({time}, {callback} [, {options}]) + Create a timer and return the timer ID. + + {time} is the waiting time in milliseconds. This is the + minimum time before invoking the callback. When the system is + busy or Vim is not waiting for input the time will be longer. + + {callback} is the function to call. It can be the name of a + function or a |Funcref|. It is called with one argument, which + is the timer ID. The callback is only invoked when Vim is + waiting for input. + + {options} is a dictionary. Supported entries: + "repeat" Number of times to repeat the callback. + -1 means forever. Default is 1. + If the timer causes an error three times in a + row the repeat is cancelled. + + Example: > + func MyHandler(timer) + echo 'Handler called' + endfunc + let timer = timer_start(500, 'MyHandler', + \ {'repeat': 3}) +< This invokes MyHandler() three times at 500 msec intervals. + + Can also be used as a |method|: > + GetMsec()->timer_start(callback) + +< Not available in the |sandbox|. + +timer_stop({timer}) *timer_stop()* + Stop a timer. The timer callback will no longer be invoked. + {timer} is an ID returned by timer_start(), thus it must be a + Number. If {timer} does not exist there is no error. + + Can also be used as a |method|: > + GetTimer()->timer_stop() +< +timer_stopall() *timer_stopall()* + Stop all timers. The timer callbacks will no longer be + invoked. Useful if some timers is misbehaving. If there are + no timers there is no error. + +tolower({expr}) *tolower()* + The result is a copy of the String given, with all uppercase + characters turned into lowercase (just like applying |gu| to + the string). + + Can also be used as a |method|: > + GetText()->tolower() + +toupper({expr}) *toupper()* + The result is a copy of the String given, with all lowercase + characters turned into uppercase (just like applying |gU| to + the string). + + Can also be used as a |method|: > + GetText()->toupper() + +tr({src}, {fromstr}, {tostr}) *tr()* + The result is a copy of the {src} string with all characters + which appear in {fromstr} replaced by the character in that + position in the {tostr} string. Thus the first character in + {fromstr} is translated into the first character in {tostr} + and so on. Exactly like the unix "tr" command. + This code also deals with multibyte characters properly. + + Examples: > + echo tr("hello there", "ht", "HT") +< returns "Hello THere" > + echo tr("<blob>", "<>", "{}") +< returns "{blob}" + + Can also be used as a |method|: > + GetText()->tr(from, to) + +trim({text} [, {mask} [, {dir}]]) *trim()* + Return {text} as a String where any character in {mask} is + removed from the beginning and/or end of {text}. + If {mask} is not given, {mask} is all characters up to 0x20, + which includes Tab, space, NL and CR, plus the non-breaking + space character 0xa0. + The optional {dir} argument specifies where to remove the + characters: + 0 remove from the beginning and end of {text} + 1 remove only at the beginning of {text} + 2 remove only at the end of {text} + When omitted both ends are trimmed. + This function deals with multibyte characters properly. + Examples: > + echo trim(" some text ") +< returns "some text" > + echo trim(" \r\t\t\r RESERVE \t\n\x0B\xA0") . "_TAIL" +< returns "RESERVE_TAIL" > + echo trim("rm<Xrm<>X>rrm", "rm<>") +< returns "Xrm<>X" (characters in the middle are not removed) > + echo trim(" vim ", " ", 2) +< returns " vim" + + Can also be used as a |method|: > + GetText()->trim() + +trunc({expr}) *trunc()* + Return the largest integral value with magnitude less than or + equal to {expr} as a |Float| (truncate towards zero). + {expr} must evaluate to a |Float| or a |Number|. + Examples: > + echo trunc(1.456) +< 1.0 > + echo trunc(-5.456) +< -5.0 > + echo trunc(4.0) +< 4.0 + + Can also be used as a |method|: > + Compute()->trunc() + +type({expr}) *type()* + The result is a Number representing the type of {expr}. + Instead of using the number directly, it is better to use the + v:t_ variable that has the value: + Number: 0 (|v:t_number|) + String: 1 (|v:t_string|) + Funcref: 2 (|v:t_func|) + List: 3 (|v:t_list|) + Dictionary: 4 (|v:t_dict|) + Float: 5 (|v:t_float|) + Boolean: 6 (|v:true| and |v:false|) + Null: 7 (|v:null|) + Blob: 10 (|v:t_blob|) + For backward compatibility, this method can be used: > + :if type(myvar) == type(0) + :if type(myvar) == type("") + :if type(myvar) == type(function("tr")) + :if type(myvar) == type([]) + :if type(myvar) == type({}) + :if type(myvar) == type(0.0) + :if type(myvar) == type(v:true) +< In place of checking for |v:null| type it is better to check + for |v:null| directly as it is the only value of this type: > + :if myvar is v:null +< To check if the v:t_ variables exist use this: > + :if exists('v:t_number') + +< Can also be used as a |method|: > + mylist->type() + +undofile({name}) *undofile()* + Return the name of the undo file that would be used for a file + with name {name} when writing. This uses the 'undodir' + option, finding directories that exist. It does not check if + the undo file exists. + {name} is always expanded to the full path, since that is what + is used internally. + If {name} is empty undofile() returns an empty string, since a + buffer without a file name will not write an undo file. + Useful in combination with |:wundo| and |:rundo|. + + Can also be used as a |method|: > + GetFilename()->undofile() + +undotree() *undotree()* + Return the current state of the undo tree in a dictionary with + the following items: + "seq_last" The highest undo sequence number used. + "seq_cur" The sequence number of the current position in + the undo tree. This differs from "seq_last" + when some changes were undone. + "time_cur" Time last used for |:earlier| and related + commands. Use |strftime()| to convert to + something readable. + "save_last" Number of the last file write. Zero when no + write yet. + "save_cur" Number of the current position in the undo + tree. + "synced" Non-zero when the last undo block was synced. + This happens when waiting from input from the + user. See |undo-blocks|. + "entries" A list of dictionaries with information about + undo blocks. + + The first item in the "entries" list is the oldest undo item. + Each List item is a |Dictionary| with these items: + "seq" Undo sequence number. Same as what appears in + |:undolist|. + "time" Timestamp when the change happened. Use + |strftime()| to convert to something readable. + "newhead" Only appears in the item that is the last one + that was added. This marks the last change + and where further changes will be added. + "curhead" Only appears in the item that is the last one + that was undone. This marks the current + position in the undo tree, the block that will + be used by a redo command. When nothing was + undone after the last change this item will + not appear anywhere. + "save" Only appears on the last block before a file + write. The number is the write count. The + first write has number 1, the last one the + "save_last" mentioned above. + "alt" Alternate entry. This is again a List of undo + blocks. Each item may again have an "alt" + item. + +uniq({list} [, {func} [, {dict}]]) *uniq()* *E882* + Remove second and succeeding copies of repeated adjacent + {list} items in-place. Returns {list}. If you want a list + to remain unmodified make a copy first: > + :let newlist = uniq(copy(mylist)) +< The default compare function uses the string representation of + each item. For the use of {func} and {dict} see |sort()|. + + Can also be used as a |method|: > + mylist->uniq() + +values({dict}) *values()* + Return a |List| with all the values of {dict}. The |List| is + in arbitrary order. Also see |items()| and |keys()|. + + Can also be used as a |method|: > + mydict->values() + +virtcol({expr}) *virtcol()* + The result is a Number, which is the screen column of the file + position given with {expr}. That is, the last screen position + occupied by the character at that position, when the screen + would be of unlimited width. When there is a <Tab> at the + position, the returned Number will be the column at the end of + the <Tab>. For example, for a <Tab> in column 1, with 'ts' + set to 8, it returns 8. |conceal| is ignored. + For the byte position use |col()|. + For the use of {expr} see |col()|. + When 'virtualedit' is used {expr} can be [lnum, col, off], where + "off" is the offset in screen columns from the start of the + character. E.g., a position within a <Tab> or after the last + character. When "off" is omitted zero is used. + When Virtual editing is active in the current mode, a position + beyond the end of the line can be returned. |'virtualedit'| + The accepted positions are: + . the cursor position + $ the end of the cursor line (the result is the + number of displayed characters in the cursor line + plus one) + 'x position of mark x (if the mark is not set, 0 is + returned) + v In Visual mode: the start of the Visual area (the + cursor is the end). When not in Visual mode + returns the cursor position. Differs from |'<| in + that it's updated right away. + Note that only marks in the current file can be used. + Examples: > + virtcol(".") with text "foo^Lbar", with cursor on the "^L", returns 5 + virtcol("$") with text "foo^Lbar", returns 9 + virtcol("'t") with text " there", with 't at 'h', returns 6 +< The first column is 1. 0 is returned for an error. + A more advanced example that echoes the maximum length of + all lines: > + echo max(map(range(1, line('$')), "virtcol([v:val, '$'])")) + +< Can also be used as a |method|: > + GetPos()->virtcol() + +visualmode([{expr}]) *visualmode()* + The result is a String, which describes the last Visual mode + used in the current buffer. Initially it returns an empty + string, but once Visual mode has been used, it returns "v", + "V", or "<CTRL-V>" (a single CTRL-V character) for + character-wise, line-wise, or block-wise Visual mode + respectively. + Example: > + :exe "normal " . visualmode() +< This enters the same Visual mode as before. It is also useful + in scripts if you wish to act differently depending on the + Visual mode that was used. + If Visual mode is active, use |mode()| to get the Visual mode + (e.g., in a |:vmap|). + If {expr} is supplied and it evaluates to a non-zero Number or + a non-empty String, then the Visual mode will be cleared and + the old value is returned. See |non-zero-arg|. + +wait({timeout}, {condition} [, {interval}]) *wait()* + Waits until {condition} evaluates to |TRUE|, where {condition} + is a |Funcref| or |string| containing an expression. + + {timeout} is the maximum waiting time in milliseconds, -1 + means forever. + + Condition is evaluated on user events, internal events, and + every {interval} milliseconds (default: 200). + + Returns a status integer: + 0 if the condition was satisfied before timeout + -1 if the timeout was exceeded + -2 if the function was interrupted (by |CTRL-C|) + -3 if an error occurred + +wildmenumode() *wildmenumode()* + Returns |TRUE| when the wildmenu is active and |FALSE| + otherwise. See 'wildmenu' and 'wildmode'. + This can be used in mappings to handle the 'wildcharm' option + gracefully. (Makes only sense with |mapmode-c| mappings). + + For example to make <c-j> work like <down> in wildmode, use: > + :cnoremap <expr> <C-j> wildmenumode() ? "\<Down>\<Tab>" : "\<c-j>" +< + (Note, this needs the 'wildcharm' option set appropriately). + +win_execute({id}, {command} [, {silent}]) *win_execute()* + Like `execute()` but in the context of window {id}. + The window will temporarily be made the current window, + without triggering autocommands or changing directory. When + executing {command} autocommands will be triggered, this may + have unexpected side effects. Use |:noautocmd| if needed. + Example: > + call win_execute(winid, 'syntax enable') +< + Can also be used as a |method|, the base is passed as the + second argument: > + GetCommand()->win_execute(winid) + +win_findbuf({bufnr}) *win_findbuf()* + Returns a |List| with |window-ID|s for windows that contain + buffer {bufnr}. When there is none the list is empty. + + Can also be used as a |method|: > + GetBufnr()->win_findbuf() + +win_getid([{win} [, {tab}]]) *win_getid()* + Get the |window-ID| for the specified window. + When {win} is missing use the current window. + With {win} this is the window number. The top window has + number 1. + Without {tab} use the current tab, otherwise the tab with + number {tab}. The first tab has number one. + Return zero if the window cannot be found. + + Can also be used as a |method|: > + GetWinnr()->win_getid() + +win_gettype([{nr}]) *win_gettype()* + Return the type of the window: + "autocmd" autocommand window. Temporary window + used to execute autocommands. + "command" command-line window |cmdwin| + (empty) normal window + "loclist" |location-list-window| + "popup" popup window |popup| + "preview" preview window |preview-window| + "quickfix" |quickfix-window| + "unknown" window {nr} not found + + When {nr} is omitted return the type of the current window. + When {nr} is given return the type of this window by number or + |window-ID|. + + Also see the 'buftype' option. When running a terminal in a + popup window then 'buftype' is "terminal" and win_gettype() + returns "popup". + + Can also be used as a |method|: > + GetWinid()->win_gettype() +< +win_gotoid({expr}) *win_gotoid()* + Go to window with ID {expr}. This may also change the current + tabpage. + Return TRUE if successful, FALSE if the window cannot be found. + + Can also be used as a |method|: > + GetWinid()->win_gotoid() + +win_id2tabwin({expr}) *win_id2tabwin()* + Return a list with the tab number and window number of window + with ID {expr}: [tabnr, winnr]. + Return [0, 0] if the window cannot be found. + + Can also be used as a |method|: > + GetWinid()->win_id2tabwin() + +win_id2win({expr}) *win_id2win()* + Return the window number of window with ID {expr}. + Return 0 if the window cannot be found in the current tabpage. + + Can also be used as a |method|: > + GetWinid()->win_id2win() + +win_move_separator({nr}, {offset}) *win_move_separator()* + Move window {nr}'s vertical separator (i.e., the right border) + by {offset} columns, as if being dragged by the mouse. {nr} + can be a window number or |window-ID|. A positive {offset} + moves right and a negative {offset} moves left. Moving a + window's vertical separator will change the width of the + window and the width of other windows adjacent to the vertical + separator. The magnitude of movement may be smaller than + specified (e.g., as a consequence of maintaining + 'winminwidth'). Returns TRUE if the window can be found and + FALSE otherwise. + + Can also be used as a |method|: > + GetWinnr()->win_move_separator(offset) + +win_move_statusline({nr}, {offset}) *win_move_statusline()* + Move window {nr}'s status line (i.e., the bottom border) by + {offset} rows, as if being dragged by the mouse. {nr} can be a + window number or |window-ID|. A positive {offset} moves down + and a negative {offset} moves up. Moving a window's status + line will change the height of the window and the height of + other windows adjacent to the status line. The magnitude of + movement may be smaller than specified (e.g., as a consequence + of maintaining 'winminheight'). Returns TRUE if the window can + be found and FALSE otherwise. + + Can also be used as a |method|: > + GetWinnr()->win_move_statusline(offset) + +win_screenpos({nr}) *win_screenpos()* + Return the screen position of window {nr} as a list with two + numbers: [row, col]. The first window always has position + [1, 1], unless there is a tabline, then it is [2, 1]. + {nr} can be the window number or the |window-ID|. Use zero + for the current window. + Returns [0, 0] if the window cannot be found in the current + tabpage. + + Can also be used as a |method|: > + GetWinid()->win_screenpos() +< +win_splitmove({nr}, {target} [, {options}]) *win_splitmove()* + Move the window {nr} to a new split of the window {target}. + This is similar to moving to {target}, creating a new window + using |:split| but having the same contents as window {nr}, and + then closing {nr}. + + Both {nr} and {target} can be window numbers or |window-ID|s. + Both must be in the current tab page. + + Returns zero for success, non-zero for failure. + + {options} is a |Dictionary| with the following optional entries: + "vertical" When TRUE, the split is created vertically, + like with |:vsplit|. + "rightbelow" When TRUE, the split is made below or to the + right (if vertical). When FALSE, it is done + above or to the left (if vertical). When not + present, the values of 'splitbelow' and + 'splitright' are used. + + Can also be used as a |method|: > + GetWinid()->win_splitmove(target) +< + *winbufnr()* +winbufnr({nr}) The result is a Number, which is the number of the buffer + associated with window {nr}. {nr} can be the window number or + the |window-ID|. + When {nr} is zero, the number of the buffer in the current + window is returned. + When window {nr} doesn't exist, -1 is returned. + Example: > + :echo "The file in the current window is " . bufname(winbufnr(0)) +< + Can also be used as a |method|: > + FindWindow()->winbufnr()->bufname() +< + *wincol()* +wincol() The result is a Number, which is the virtual column of the + cursor in the window. This is counting screen cells from the + left side of the window. The leftmost column is one. + + *windowsversion()* +windowsversion() + The result is a String. For MS-Windows it indicates the OS + version. E.g, Windows 10 is "10.0", Windows 8 is "6.2", + Windows XP is "5.1". For non-MS-Windows systems the result is + an empty string. + +winheight({nr}) *winheight()* + The result is a Number, which is the height of window {nr}. + {nr} can be the window number or the |window-ID|. + When {nr} is zero, the height of the current window is + returned. When window {nr} doesn't exist, -1 is returned. + An existing window always has a height of zero or more. + This excludes any window toolbar line. + Examples: > + :echo "The current window has " . winheight(0) . " lines." + +< Can also be used as a |method|: > + GetWinid()->winheight() +< +winlayout([{tabnr}]) *winlayout()* + The result is a nested List containing the layout of windows + in a tabpage. + + Without {tabnr} use the current tabpage, otherwise the tabpage + with number {tabnr}. If the tabpage {tabnr} is not found, + returns an empty list. + + For a leaf window, it returns: + ['leaf', {winid}] + For horizontally split windows, which form a column, it + returns: + ['col', [{nested list of windows}]] + For vertically split windows, which form a row, it returns: + ['row', [{nested list of windows}]] + + Example: > + " Only one window in the tab page + :echo winlayout() + ['leaf', 1000] + " Two horizontally split windows + :echo winlayout() + ['col', [['leaf', 1000], ['leaf', 1001]]] + " The second tab page, with three horizontally split + " windows, with two vertically split windows in the + " middle window + :echo winlayout(2) + ['col', [['leaf', 1002], ['row', [['leaf', 1003], + ['leaf', 1001]]], ['leaf', 1000]]] +< + Can also be used as a |method|: > + GetTabnr()->winlayout() +< + *winline()* +winline() The result is a Number, which is the screen line of the cursor + in the window. This is counting screen lines from the top of + the window. The first line is one. + If the cursor was moved the view on the file will be updated + first, this may cause a scroll. + + *winnr()* +winnr([{arg}]) The result is a Number, which is the number of the current + window. The top window has number 1. + Returns zero for a popup window. + + The optional argument {arg} supports the following values: + $ the number of the last window (the window + count). + # the number of the last accessed window (where + |CTRL-W_p| goes to). If there is no previous + window or it is in another tab page 0 is + returned. + {N}j the number of the Nth window below the + current window (where |CTRL-W_j| goes to). + {N}k the number of the Nth window above the current + window (where |CTRL-W_k| goes to). + {N}h the number of the Nth window left of the + current window (where |CTRL-W_h| goes to). + {N}l the number of the Nth window right of the + current window (where |CTRL-W_l| goes to). + The number can be used with |CTRL-W_w| and ":wincmd w" + |:wincmd|. + Also see |tabpagewinnr()| and |win_getid()|. + Examples: > + let window_count = winnr('$') + let prev_window = winnr('#') + let wnum = winnr('3k') + +< Can also be used as a |method|: > + GetWinval()->winnr() +< + *winrestcmd()* +winrestcmd() Returns a sequence of |:resize| commands that should restore + the current window sizes. Only works properly when no windows + are opened or closed and the current window and tab page is + unchanged. + Example: > + :let cmd = winrestcmd() + :call MessWithWindowSizes() + :exe cmd +< + *winrestview()* +winrestview({dict}) + Uses the |Dictionary| returned by |winsaveview()| to restore + the view of the current window. + Note: The {dict} does not have to contain all values, that are + returned by |winsaveview()|. If values are missing, those + settings won't be restored. So you can use: > + :call winrestview({'curswant': 4}) +< + This will only set the curswant value (the column the cursor + wants to move on vertical movements) of the cursor to column 5 + (yes, that is 5), while all other settings will remain the + same. This is useful, if you set the cursor position manually. + + If you have changed the values the result is unpredictable. + If the window size changed the result won't be the same. + + Can also be used as a |method|: > + GetView()->winrestview() +< + *winsaveview()* +winsaveview() Returns a |Dictionary| that contains information to restore + the view of the current window. Use |winrestview()| to + restore the view. + This is useful if you have a mapping that jumps around in the + buffer and you want to go back to the original view. + This does not save fold information. Use the 'foldenable' + option to temporarily switch off folding, so that folds are + not opened when moving around. This may have side effects. + The return value includes: + lnum cursor line number + col cursor column (Note: the first column + zero, as opposed to what getpos() + returns) + coladd cursor column offset for 'virtualedit' + curswant column for vertical movement + topline first line in the window + topfill filler lines, only in diff mode + leftcol first column displayed; only used when + 'wrap' is off + skipcol columns skipped + Note that no option values are saved. + + +winwidth({nr}) *winwidth()* + The result is a Number, which is the width of window {nr}. + {nr} can be the window number or the |window-ID|. + When {nr} is zero, the width of the current window is + returned. When window {nr} doesn't exist, -1 is returned. + An existing window always has a width of zero or more. + Examples: > + :echo "The current window has " . winwidth(0) . " columns." + :if winwidth(0) <= 50 + : 50 wincmd | + :endif +< For getting the terminal or screen size, see the 'columns' + option. + + Can also be used as a |method|: > + GetWinid()->winwidth() + +wordcount() *wordcount()* + The result is a dictionary of byte/chars/word statistics for + the current buffer. This is the same info as provided by + |g_CTRL-G| + The return value includes: + bytes Number of bytes in the buffer + chars Number of chars in the buffer + words Number of words in the buffer + cursor_bytes Number of bytes before cursor position + (not in Visual mode) + cursor_chars Number of chars before cursor position + (not in Visual mode) + cursor_words Number of words before cursor position + (not in Visual mode) + visual_bytes Number of bytes visually selected + (only in Visual mode) + visual_chars Number of chars visually selected + (only in Visual mode) + visual_words Number of words visually selected + (only in Visual mode) + + + *writefile()* +writefile({object}, {fname} [, {flags}]) + When {object} is a |List| write it to file {fname}. Each list + item is separated with a NL. Each list item must be a String + or Number. + When {flags} contains "b" then binary mode is used: There will + not be a NL after the last list item. An empty item at the + end does cause the last line in the file to end in a NL. + + When {object} is a |Blob| write the bytes to file {fname} + unmodified. + + When {flags} contains "a" then append mode is used, lines are + appended to the file: > + :call writefile(["foo"], "event.log", "a") + :call writefile(["bar"], "event.log", "a") +< + When {flags} contains "S" fsync() call is not used, with "s" + it is used, 'fsync' option applies by default. No fsync() + means that writefile() will finish faster, but writes may be + left in OS buffers and not yet written to disk. Such changes + will disappear if system crashes before OS does writing. + + All NL characters are replaced with a NUL character. + Inserting CR characters needs to be done before passing {list} + to writefile(). + An existing file is overwritten, if possible. + When the write fails -1 is returned, otherwise 0. There is an + error message if the file can't be created or when writing + fails. + Also see |readfile()|. + To copy a file byte for byte: > + :let fl = readfile("foo", "b") + :call writefile(fl, "foocopy", "b") + +< Can also be used as a |method|: > + GetText()->writefile("thefile") + +xor({expr}, {expr}) *xor()* + Bitwise XOR on the two arguments. The arguments are converted + to a number. A List, Dict or Float argument causes an error. + Example: > + :let bits = xor(bits, 0x80) +< + Can also be used as a |method|: > + :let bits = bits->xor(0x80) +< +============================================================================== +3. Matching a pattern in a String *string-match* + +This is common between several functions. A regexp pattern as explained at +|pattern| is normally used to find a match in the buffer lines. When a +pattern is used to find a match in a String, almost everything works in the +same way. The difference is that a String is handled like it is one line. +When it contains a "\n" character, this is not seen as a line break for the +pattern. It can be matched with a "\n" in the pattern, or with ".". Example: +> + :let a = "aaaa\nxxxx" + :echo matchstr(a, "..\n..") + aa + xx + :echo matchstr(a, "a.x") + a + x + +Don't forget that "^" will only match at the first character of the String and +"$" at the last character of the string. They don't match after or before a +"\n". + + vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/runtime/doc/change.txt b/runtime/doc/change.txt index 953f097a92..b4d3304880 100644 --- a/runtime/doc/change.txt +++ b/runtime/doc/change.txt @@ -1118,10 +1118,13 @@ register. With blockwise selection it also depends on the size of the block and whether the corners are on an existing character. (Implementation detail: it actually works by first putting the register after the selection and then deleting the selection.) -The previously selected text is put in the unnamed register. If you want to -put the same text into a Visual selection several times you need to use +With 'p' the previously selected text is put in the unnamed register. This is +useful if you want to put that text somewhere else. But you cannot repeat the +same change. +With 'P' the unnamed register is not changed, you can repeat the same change. +But the deleted text cannot be used. If you do need it you can use 'p' with another register. E.g., yank the text to copy, Visually select the text to -replace and use "0p . You can repeat this as many times as you like, the +replace and use "0p . You can repeat this as many times as you like, and the unnamed register will be changed each time. When you use a blockwise Visual mode command and yank only a single line into diff --git a/runtime/doc/channel.txt b/runtime/doc/channel.txt index 5f376a600e..e14427494d 100644 --- a/runtime/doc/channel.txt +++ b/runtime/doc/channel.txt @@ -44,7 +44,7 @@ functions like |chansend()| consume channel ids. 2. Reading and writing raw bytes *channel-bytes* Channels opened by Vimscript functions operate with raw bytes by default. For -a job channel using RPC, bytes can still be read over its stderr. Similarily, +a job channel using RPC, bytes can still be read over its stderr. Similarly, only bytes can be written to Nvim's own stderr. *channel-callback* diff --git a/runtime/doc/cmdline.txt b/runtime/doc/cmdline.txt index 7716af25bd..f2f6ebb2c9 100644 --- a/runtime/doc/cmdline.txt +++ b/runtime/doc/cmdline.txt @@ -697,7 +697,8 @@ Line numbers may be specified with: *:range* *{address}* Each may be followed (several times) by '+' or '-' and an optional number. This number is added or subtracted from the preceding line number. If the -number is omitted, 1 is used. +number is omitted, 1 is used. If there is nothing before the '+' or '-' then +the current line is used. The "/" and "?" after {pattern} are required to separate the pattern from anything that follows. @@ -727,7 +728,7 @@ Some commands allow for a count after the command. This count is used as the number of lines to be used, starting with the line given in the last line specifier (the default is the cursor line). The commands that accept a count are the ones that use a range but do not have a file name argument (because -a file name can also be a number). +a file name can also be a number). The count cannot be negative. Examples: > :s/x/X/g 5 substitute 'x' by 'X' in the current line and four @@ -907,8 +908,7 @@ These modifiers can be given, in this order: directory. :. Reduce file name to be relative to current directory, if possible. File name is unmodified if it is not below the - current directory, but on MS-Windows the drive is removed if - it is the current drive. + current directory. For maximum shortness, use ":~:.". :h Head of the file name (the last component and any separators removed). Cannot be used with :e, :r or :t. diff --git a/runtime/doc/develop.txt b/runtime/doc/develop.txt index 7127c74134..178b0dc62b 100644 --- a/runtime/doc/develop.txt +++ b/runtime/doc/develop.txt @@ -105,7 +105,7 @@ in eval.c: - eval_call_provider(name, method, arguments, discard): calls provider#{name}#Call with the method and arguments. If discard is true, any - value returned by the provider will be discarded and and empty value be + value returned by the provider will be discarded and empty value will be returned. - eval_has_provider(name): Checks the `g:loaded_{name}_provider` variable which must be set to 2 by the provider script to indicate that it is diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index bb36fa46f6..19db3158be 100644 --- a/runtime/doc/diagnostic.txt +++ b/runtime/doc/diagnostic.txt @@ -334,8 +334,9 @@ config({opts}, {namespace}) *vim.diagnostic.config()* that returns any of the above. Parameters: ~ - {opts} table Configuration table with the following - keys: + {opts} table|nil When omitted or "nil", retrieve the + current configuration. Otherwise, a + configuration table with the following keys: • underline: (default true) Use underline for diagnostics. Options: • severity: Only underline diagnostics diff --git a/runtime/doc/diff.txt b/runtime/doc/diff.txt index 6115a5d235..abe99102ee 100644 --- a/runtime/doc/diff.txt +++ b/runtime/doc/diff.txt @@ -324,8 +324,9 @@ After setting this variable, reload the syntax script: > FINDING THE DIFFERENCES *diff-diffexpr* -The 'diffexpr' option can be set to use something else than the standard -"diff" program to compare two files and find the differences. *E959* +The 'diffexpr' option can be set to use something else than the internal diff +support or the standard "diff" program to compare two files and find the +differences. When 'diffexpr' is empty, Vim uses this command to find the differences between file1 and file2: > @@ -358,7 +359,7 @@ format mentioned. These variables are set to the file names used: v:fname_in original file v:fname_new new version of the same file - v:fname_out resulting diff file + v:fname_out where to write the resulting diff file Additionally, 'diffexpr' should take care of "icase" and "iwhite" in the 'diffopt' option. 'diffexpr' cannot change the value of 'lines' and diff --git a/runtime/doc/editing.txt b/runtime/doc/editing.txt index 14df41e6c8..3d0287b0cd 100644 --- a/runtime/doc/editing.txt +++ b/runtime/doc/editing.txt @@ -845,7 +845,7 @@ Note: When the 'write' option is off, you are not able to write any file. *:w* *:write* *E502* *E503* *E504* *E505* - *E512* *E514* *E667* *E796* *E949* + *E512* *E514* *E667* *E949* :w[rite] [++opt] Write the whole buffer to the current file. This is the normal way to save changes to a file. It fails when the 'readonly' option is set or when there is @@ -1331,6 +1331,7 @@ current directory for that window. Windows where the |:lcd| command has not been used stick to the global or tab-local directory. When jumping to another window the current directory is changed to the last specified local current directory. If none was specified, the global or tab-local directory is used. +When creating a new window it inherits the local directory of the current window. When changing tabs the same behaviour applies. If the current tab has no local working directory the global working directory is used. @@ -1568,6 +1569,12 @@ There are three different types of searching: /u/user_x/work/include /u/user_x/include +< Note: If your 'path' setting includes a non-existing directory, Vim will + skip the non-existing directory, but continues searching in the parent of + the non-existing directory if upwards searching is used. E.g. when + searching "../include" and that doesn't exist, and upward searching is + used, also searches in "..". + 3) Combined up/downward search: If Vim's current path is /u/user_x/work/release and you do > set path=**;/u/user_x diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index c2158f3a1e..fc788fba59 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -111,7 +111,7 @@ When mixing Number and Float the Number is converted to Float. Otherwise there is no automatic conversion of Float. You can use str2float() for String to Float, printf() for Float to String and float2nr() for Float to Number. - *E891* *E892* *E893* *E894* + *E362* *E891* *E892* *E893* *E894* *E907* When expecting a Float a Number can also be used, but nothing else. *no-type-checking* @@ -372,8 +372,8 @@ Changing the order of items in a list: > For loop ~ -The |:for| loop executes commands for each item in a |List| or |Blob|. -A variable is set to each item in the sequence. Example with a List: > +The |:for| loop executes commands for each item in a |List|, |String| or |Blob|. +A variable is set to each item in sequence. Example with a List: > :for item in mylist : call Doit(item) :endfor @@ -390,7 +390,7 @@ If all you want to do is modify each item in the list then the |map()| function will be a simpler method than a for loop. Just like the |:let| command, |:for| also accepts a list of variables. This -requires the argument to be a list of lists. > +requires the argument to be a List of Lists. > :for [lnum, col] in [[1, 3], [2, 8], [3, 0]] : call Doit(lnum, col) :endfor @@ -408,6 +408,12 @@ It is also possible to put remaining items in a List variable: > For a Blob one byte at a time is used. +For a String one character, including any composing characters, is used as a +String. Example: > + for c in text + echo 'This character is ' .. c + endfor + List functions ~ *E714* @@ -676,7 +682,7 @@ similar to -1. > :let otherblob = myblob[:] " make a copy of the Blob If the first index is beyond the last byte of the Blob or the second byte is -before the first byte, the result is an empty Blob. There is no error +before the first index, the result is an empty Blob. There is no error message. If the second index is equal to or greater than the length of the Blob the @@ -1066,7 +1072,7 @@ expr7 *expr7* For '!' |TRUE| becomes |FALSE|, |FALSE| becomes |TRUE| (one). For '-' the sign of the number is changed. -For '+' the number is unchanged. +For '+' the number is unchanged. Note: "++" has no effect. A String will be converted to a Number first. @@ -1195,6 +1201,7 @@ When expr8 is a |Funcref| type variable, invoke the function it refers to. expr8->name([args]) method call *method* *->* expr8->{lambda}([args]) + *E260* *E276* For methods that are also available as global functions this is the same as: > name(expr8 [, args]) There can also be methods specifically for the type of "expr8". @@ -1227,8 +1234,8 @@ And NOT: > number ------ number number constant *expr-number* - *hex-number* *octal-number* *binary-number* + *0x* *hex-number* *0o* *octal-number* *binary-number* Decimal, Hexadecimal (starting with 0x or 0X), Binary (starting with 0b or 0B) and Octal (starting with 0, 0o or 0O). @@ -1450,7 +1457,7 @@ the function returns: > :let Bar = Foo(4) :echo Bar(6) < 5 -Note that the variables must exist in the outer scope before the lamba is +Note that the variables must exist in the outer scope before the lambda is defined for this to work. See also |:func-closure|. Lambda and closure support can be checked with: > @@ -1485,7 +1492,7 @@ Notice how execute() is used to execute an Ex command. That's ugly though. Lambda expressions have internal names like '<lambda>42'. If you get an error for a lambda expression, you can find what it is with the following command: > - :function {'<lambda>42'} + :function <lambda>42 See also: |numbered-function| ============================================================================== @@ -2266,8654 +2273,13 @@ v:windowid Application-specific window "handle" which may be set by any ============================================================================== 4. Builtin Functions *vim-function* *functions* -The Vimscript subsystem (referred to as "eval" internally) provides the -following builtin functions. Scripts can also define |user-function|s. +The Vimscript subsystem (referred to as "eval" internally) provides builtin +functions. Scripts can also define |user-function|s. See |function-list| to browse functions by topic. -(Use CTRL-] on the function name to jump to the full explanation.) - -USAGE RESULT DESCRIPTION ~ - -abs({expr}) Float or Number absolute value of {expr} -acos({expr}) Float arc cosine of {expr} -add({object}, {item}) List/Blob append {item} to {object} -and({expr}, {expr}) Number bitwise AND -api_info() Dict api metadata -append({lnum}, {string}) Number append {string} below line {lnum} -append({lnum}, {list}) Number append lines {list} below line {lnum} -argc([{winid}]) Number number of files in the argument list -argidx() Number current index in the argument list -arglistid([{winnr} [, {tabnr}]]) Number argument list id -argv({nr} [, {winid}]) String {nr} entry of the argument list -argv([-1, {winid}]) List the argument list -asin({expr}) Float arc sine of {expr} -assert_beeps({cmd}) Number assert {cmd} causes a beep -assert_equal({exp}, {act} [, {msg}]) - Number assert {exp} is equal to {act} -assert_equalfile({fname-one}, {fname-two} [, {msg}]) - Number assert file contents are equal -assert_exception({error} [, {msg}]) - Number assert {error} is in v:exception -assert_fails({cmd} [, {error}]) Number assert {cmd} fails -assert_false({actual} [, {msg}]) - Number assert {actual} is false -assert_inrange({lower}, {upper}, {actual} [, {msg}]) - Number assert {actual} is inside the range -assert_match({pat}, {text} [, {msg}]) - Number assert {pat} matches {text} -assert_nobeep({cmd}) Number assert {cmd} does not cause a beep -assert_notequal({exp}, {act} [, {msg}]) - Number assert {exp} is not equal {act} -assert_notmatch({pat}, {text} [, {msg}]) - Number assert {pat} not matches {text} -assert_report({msg}) Number report a test failure -assert_true({actual} [, {msg}]) Number assert {actual} is true -atan({expr}) Float arc tangent of {expr} -atan2({expr}, {expr}) Float arc tangent of {expr1} / {expr2} -browse({save}, {title}, {initdir}, {default}) - String put up a file requester -browsedir({title}, {initdir}) String put up a directory requester -bufadd({name}) Number add a buffer to the buffer list -bufexists({expr}) Number |TRUE| if buffer {expr} exists -buflisted({expr}) Number |TRUE| if buffer {expr} is listed -bufload({expr}) Number load buffer {expr} if not loaded yet -bufloaded({expr}) Number |TRUE| if buffer {expr} is loaded -bufname([{expr}]) String Name of the buffer {expr} -bufnr([{expr} [, {create}]]) Number Number of the buffer {expr} -bufwinid({expr}) Number |window-ID| of buffer {expr} -bufwinnr({expr}) Number window number of buffer {expr} -byte2line({byte}) Number line number at byte count {byte} -byteidx({expr}, {nr}) Number byte index of {nr}'th char in {expr} -byteidxcomp({expr}, {nr}) Number byte index of {nr}'th char in {expr} -call({func}, {arglist} [, {dict}]) - any call {func} with arguments {arglist} -ceil({expr}) Float round {expr} up -changenr() Number current change number -chanclose({id}[, {stream}]) Number Closes a channel or one of its streams -chansend({id}, {data}) Number Writes {data} to channel -char2nr({expr}[, {utf8}]) Number ASCII/UTF-8 value of first char in {expr} -charidx({string}, {idx} [, {countcc}]) - Number char index of byte {idx} in {string} -chdir({dir}) String change current working directory -cindent({lnum}) Number C indent for line {lnum} -clearmatches([{win}]) none clear all matches -col({expr}) Number column nr of cursor or mark -complete({startcol}, {matches}) none set Insert mode completion -complete_add({expr}) Number add completion match -complete_check() Number check for key typed during completion -complete_info([{what}]) Dict get current completion information -confirm({msg} [, {choices} [, {default} [, {type}]]]) - Number number of choice picked by user -copy({expr}) any make a shallow copy of {expr} -cos({expr}) Float cosine of {expr} -cosh({expr}) Float hyperbolic cosine of {expr} -count({list}, {expr} [, {ic} [, {start}]]) - Number count how many {expr} are in {list} -cscope_connection([{num}, {dbpath} [, {prepend}]]) - Number checks existence of cscope connection -ctxget([{index}]) Dict return the |context| dict at {index} -ctxpop() none pop and restore |context| from the - |context-stack| -ctxpush([{types}]) none push the current |context| to the - |context-stack| -ctxset({context}[, {index}]) none set |context| at {index} -ctxsize() Number return |context-stack| size -cursor({lnum}, {col} [, {off}]) - Number move cursor to {lnum}, {col}, {off} -cursor({list}) Number move cursor to position in {list} -debugbreak({pid}) Number interrupt process being debugged -deepcopy({expr} [, {noref}]) any make a full copy of {expr} -delete({fname} [, {flags}]) Number delete the file or directory {fname} -deletebufline({buf}, {first}[, {last}]) - Number delete lines from buffer {buf} -dictwatcheradd({dict}, {pattern}, {callback}) - Start watching a dictionary -dictwatcherdel({dict}, {pattern}, {callback}) - Stop watching a dictionary -did_filetype() Number |TRUE| if FileType autocommand event used -diff_filler({lnum}) Number diff filler lines about {lnum} -diff_hlID({lnum}, {col}) Number diff highlighting at {lnum}/{col} -empty({expr}) Number |TRUE| if {expr} is empty -environ() Dict return environment variables -escape({string}, {chars}) String escape {chars} in {string} with '\' -eval({string}) any evaluate {string} into its value -eventhandler() Number |TRUE| if inside an event handler -executable({expr}) Number 1 if executable {expr} exists -execute({command}) String execute and capture output of {command} -exepath({expr}) String full path of the command {expr} -exists({expr}) Number |TRUE| if {expr} exists -extend({expr1}, {expr2} [, {expr3}]) - List/Dict insert items of {expr2} into {expr1} -exp({expr}) Float exponential of {expr} -expand({expr} [, {nosuf} [, {list}]]) - any expand special keywords in {expr} -expandcmd({expr}) String expand {expr} like with `:edit` -feedkeys({string} [, {mode}]) Number add key sequence to typeahead buffer -filereadable({file}) Number |TRUE| if {file} is a readable file -filewritable({file}) Number |TRUE| if {file} is a writable file -filter({expr1}, {expr2}) List/Dict remove items from {expr1} where - {expr2} is 0 -finddir({name} [, {path} [, {count}]]) - String find directory {name} in {path} -findfile({name} [, {path} [, {count}]]) - String find file {name} in {path} -flatten({list} [, {maxdepth}]) List flatten {list} up to {maxdepth} levels -float2nr({expr}) Number convert Float {expr} to a Number -floor({expr}) Float round {expr} down -fmod({expr1}, {expr2}) Float remainder of {expr1} / {expr2} -fnameescape({fname}) String escape special characters in {fname} -fnamemodify({fname}, {mods}) String modify file name -foldclosed({lnum}) Number first line of fold at {lnum} if closed -foldclosedend({lnum}) Number last line of fold at {lnum} if closed -foldlevel({lnum}) Number fold level at {lnum} -foldtext() String line displayed for closed fold -foldtextresult({lnum}) String text for closed fold at {lnum} -foreground() Number bring the Vim window to the foreground -fullcommand({name}) String get full command from {name} -funcref({name} [, {arglist}] [, {dict}]) - Funcref reference to function {name} -function({name} [, {arglist}] [, {dict}]) - Funcref named reference to function {name} -garbagecollect([{atexit}]) none free memory, breaking cyclic references -get({list}, {idx} [, {def}]) any get item {idx} from {list} or {def} -get({dict}, {key} [, {def}]) any get item {key} from {dict} or {def} -get({func}, {what}) any get property of funcref/partial {func} -getbufinfo([{buf}]) List information about buffers -getbufline({buf}, {lnum} [, {end}]) - List lines {lnum} to {end} of buffer {buf} -getbufvar({buf}, {varname} [, {def}]) - any variable {varname} in buffer {buf} -getchangelist([{buf}]) List list of change list items -getchar([expr]) Number or String - get one character from the user -getcharmod() Number modifiers for the last typed character -getcharsearch() Dict last character search -getcharstr([expr]) String get one character from the user -getcmdline() String return the current command-line -getcmdpos() Number return cursor position in command-line -getcmdtype() String return current command-line type -getcmdwintype() String return current command-line window type -getcompletion({pat}, {type} [, {filtered}]) - List list of cmdline completion matches -getcurpos() List position of the cursor -getcwd([{winnr} [, {tabnr}]]) String get the current working directory -getenv({name}) String return environment variable -getfontname([{name}]) String name of font being used -getfperm({fname}) String file permissions of file {fname} -getfsize({fname}) Number size in bytes of file {fname} -getftime({fname}) Number last modification time of file -getftype({fname}) String description of type of file {fname} -getjumplist([{winnr} [, {tabnr}]]) - List list of jump list items -getline({lnum}) String line {lnum} of current buffer -getline({lnum}, {end}) List lines {lnum} to {end} of current buffer -getloclist({nr}) List list of location list items -getloclist({nr}, {what}) Dict get specific location list properties -getmarklist([{buf}]) List list of global/local marks -getmatches([{win}]) List list of current matches -getmousepos() Dict last known mouse position -getpid() Number process ID of Vim -getpos({expr}) List position of cursor, mark, etc. -getqflist() List list of quickfix items -getqflist({what}) Dict get specific quickfix list properties -getreg([{regname} [, 1 [, {list}]]]) - String or List contents of a register -getreginfo([{regname}]) Dict information about a register -getregtype([{regname}]) String type of a register -gettabinfo([{expr}]) List list of tab pages -gettabvar({nr}, {varname} [, {def}]) - any variable {varname} in tab {nr} or {def} -gettabwinvar({tabnr}, {winnr}, {name} [, {def}]) - any {name} in {winnr} in tab page {tabnr} -gettagstack([{nr}]) Dict get the tag stack of window {nr} -getwininfo([{winid}]) List list of windows -getwinpos([{timeout}]) List X and Y coord in pixels of the Vim window -getwinposx() Number X coord in pixels of Vim window -getwinposy() Number Y coord in pixels of Vim window -getwinvar({nr}, {varname} [, {def}]) - any variable {varname} in window {nr} -glob({expr} [, {nosuf} [, {list} [, {alllinks}]]]) - any expand file wildcards in {expr} -glob2regpat({expr}) String convert a glob pat into a search pat -globpath({path}, {expr} [, {nosuf} [, {list} [, {alllinks}]]]) - String do glob({expr}) for all dirs in {path} -has({feature}) Number |TRUE| if feature {feature} supported -has_key({dict}, {key}) Number |TRUE| if {dict} has entry {key} -haslocaldir([{winnr} [, {tabnr}]]) - Number |TRUE| if the window executed |:lcd| or - the tab executed |:tcd| -hasmapto({what} [, {mode} [, {abbr}]]) - Number |TRUE| if mapping to {what} exists -histadd({history}, {item}) String add an item to a history -histdel({history} [, {item}]) String remove an item from a history -histget({history} [, {index}]) String get the item {index} from a history -histnr({history}) Number highest index of a history -hlexists({name}) Number |TRUE| if highlight group {name} exists -hlID({name}) Number syntax ID of highlight group {name} -hostname() String name of the machine Vim is running on -iconv({expr}, {from}, {to}) String convert encoding of {expr} -indent({lnum}) Number indent of line {lnum} -index({object}, {expr} [, {start} [, {ic}]]) - Number index in {object} where {expr} appears -input({prompt} [, {text} [, {completion}]]) - String get input from the user -inputlist({textlist}) Number let the user pick from a choice list -inputrestore() Number restore typeahead -inputsave() Number save and clear typeahead -inputsecret({prompt} [, {text}]) - String like input() but hiding the text -insert({object}, {item} [, {idx}]) - List insert {item} in {object} [before {idx}] -interrupt() none interrupt script execution -invert({expr}) Number bitwise invert -isdirectory({directory}) Number |TRUE| if {directory} is a directory -isinf({expr}) Number determine if {expr} is infinity value - (positive or negative) -islocked({expr}) Number |TRUE| if {expr} is locked -isnan({expr}) Number |TRUE| if {expr} is NaN -id({expr}) String identifier of the container -items({dict}) List key-value pairs in {dict} -jobpid({id}) Number Returns pid of a job. -jobresize({id}, {width}, {height}) - Number Resize pseudo terminal window of a job -jobstart({cmd}[, {opts}]) Number Spawns {cmd} as a job -jobstop({id}) Number Stops a job -jobwait({ids}[, {timeout}]) Number Wait for a set of jobs -join({list} [, {sep}]) String join {list} items into one String -json_decode({expr}) any Convert {expr} from JSON -json_encode({expr}) String Convert {expr} to JSON -keys({dict}) List keys in {dict} -len({expr}) Number the length of {expr} -libcall({lib}, {func}, {arg}) String call {func} in library {lib} with {arg} -libcallnr({lib}, {func}, {arg}) Number idem, but return a Number -line({expr} [, {winid}]) Number line nr of cursor, last line or mark -line2byte({lnum}) Number byte count of line {lnum} -lispindent({lnum}) Number Lisp indent for line {lnum} -list2str({list} [, {utf8}]) String turn numbers in {list} into a String -localtime() Number current time -log({expr}) Float natural logarithm (base e) of {expr} -log10({expr}) Float logarithm of Float {expr} to base 10 -luaeval({expr}[, {expr}]) any evaluate Lua expression -map({expr1}, {expr2}) List/Dict change each item in {expr1} to {expr} -maparg({name}[, {mode} [, {abbr} [, {dict}]]]) - String or Dict - rhs of mapping {name} in mode {mode} -mapcheck({name}[, {mode} [, {abbr}]]) - String check for mappings matching {name} -match({expr}, {pat}[, {start}[, {count}]]) - Number position where {pat} matches in {expr} -matchadd({group}, {pattern}[, {priority}[, {id}]]) - Number highlight {pattern} with {group} -matchaddpos({group}, {list}[, {priority}[, {id}]]) - Number highlight positions with {group} -matcharg({nr}) List arguments of |:match| -matchdelete({id} [, {win}]) Number delete match identified by {id} -matchend({expr}, {pat}[, {start}[, {count}]]) - Number position where {pat} ends in {expr} -matchlist({expr}, {pat}[, {start}[, {count}]]) - List match and submatches of {pat} in {expr} -matchstr({expr}, {pat}[, {start}[, {count}]]) - String {count}'th match of {pat} in {expr} -matchstrpos({expr}, {pat}[, {start}[, {count}]]) - List {count}'th match of {pat} in {expr} -max({expr}) Number maximum value of items in {expr} -menu_get({path} [, {modes}]) List description of |menus| matched by {path} -min({expr}) Number minimum value of items in {expr} -mkdir({name} [, {path} [, {prot}]]) - Number create directory {name} -mode([expr]) String current editing mode -msgpackdump({list} [, {type}]) List/Blob dump objects to msgpack -msgpackparse({data}) List parse msgpack to a list of objects -nextnonblank({lnum}) Number line nr of non-blank line >= {lnum} -nr2char({expr}[, {utf8}]) String single char with ASCII/UTF-8 value {expr} -nvim_...({args}...) any call nvim |api| functions -or({expr}, {expr}) Number bitwise OR -pathshorten({expr}) String shorten directory names in a path -perleval({expr}) any evaluate |perl| expression -pow({x}, {y}) Float {x} to the power of {y} -prevnonblank({lnum}) Number line nr of non-blank line <= {lnum} -printf({fmt}, {expr1}...) String format text -prompt_getprompt({buf}) String get prompt text -prompt_setcallback({buf}, {expr}) none set prompt callback function -prompt_setinterrupt({buf}, {text}) none set prompt interrupt function -prompt_setprompt({buf}, {text}) none set prompt text -pum_getpos() Dict position and size of pum if visible -pumvisible() Number whether popup menu is visible -pyeval({expr}) any evaluate |Python| expression -py3eval({expr}) any evaluate |python3| expression -pyxeval({expr}) any evaluate |python_x| expression -range({expr} [, {max} [, {stride}]]) - List items from {expr} to {max} -readdir({dir} [, {expr}]) List file names in {dir} selected by {expr} -readfile({fname} [, {type} [, {max}]]) - List get list of lines from file {fname} -reg_executing() String get the executing register name -reg_recorded() String get the last recorded register name -reg_recording() String get the recording register name -reltime([{start} [, {end}]]) List get time value -reltimefloat({time}) Float turn the time value into a Float -reltimestr({time}) String turn time value into a String -remote_expr({server}, {string} [, {idvar} [, {timeout}]]) - String send expression -remote_foreground({server}) Number bring Vim server to the foreground -remote_peek({serverid} [, {retvar}]) - Number check for reply string -remote_read({serverid} [, {timeout}]) - String read reply string -remote_send({server}, {string} [, {idvar}]) - String send key sequence -remote_startserver({name}) none become server {name} -remove({list}, {idx} [, {end}]) any/List - remove items {idx}-{end} from {list} -remove({blob}, {idx} [, {end}]) Number/Blob - remove bytes {idx}-{end} from {blob} -remove({dict}, {key}) any remove entry {key} from {dict} -rename({from}, {to}) Number rename (move) file from {from} to {to} -repeat({expr}, {count}) String repeat {expr} {count} times -resolve({filename}) String get filename a shortcut points to -reverse({list}) List reverse {list} in-place -round({expr}) Float round off {expr} -rubyeval({expr}) any evaluate |Ruby| expression -rpcnotify({channel}, {event}[, {args}...]) - Sends an |RPC| notification to {channel} -rpcrequest({channel}, {method}[, {args}...]) - Sends an |RPC| request to {channel} -screenattr({row}, {col}) Number attribute at screen position -screenchar({row}, {col}) Number character at screen position -screenchars({row}, {col}) List List of characters at screen position -screencol() Number current cursor column -screenpos({winid}, {lnum}, {col}) Dict screen row and col of a text character -screenrow() Number current cursor row -screenstring({row}, {col}) String characters at screen position -search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) - Number search for {pattern} -searchcount([{options}]) Dict Get or update the last search count -searchdecl({name} [, {global} [, {thisblock}]]) - Number search for variable declaration -searchpair({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) - Number search for other end of start/end pair -searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} [...]]]) - List search for other end of start/end pair -searchpos({pattern} [, {flags} [, {stopline} [, {timeout}]]]) - List search for {pattern} -server2client({clientid}, {string}) - Number send reply string -serverlist() String get a list of available servers -setbufline( {expr}, {lnum}, {line}) - Number set line {lnum} to {line} in buffer - {expr} -setbufvar({buf}, {varname}, {val}) set {varname} in buffer {buf} to {val} -setcharsearch({dict}) Dict set character search from {dict} -setcmdpos({pos}) Number set cursor position in command-line -setenv({name}, {val}) none set environment variable -setfperm({fname}, {mode} Number set {fname} file permissions to {mode} -setline({lnum}, {line}) Number set line {lnum} to {line} -setloclist({nr}, {list} [, {action}]) - Number modify location list using {list} -setloclist({nr}, {list}, {action}, {what}) - Number modify specific location list props -setmatches({list} [, {win}]) Number restore a list of matches -setpos({expr}, {list}) Number set the {expr} position to {list} -setqflist({list} [, {action}]) Number modify quickfix list using {list} -setqflist({list}, {action}, {what}) - Number modify specific quickfix list props -setreg({n}, {v}[, {opt}]) Number set register to value and type -settabvar({nr}, {varname}, {val}) set {varname} in tab page {nr} to {val} -settabwinvar({tabnr}, {winnr}, {varname}, {val}) set {varname} in window - {winnr} in tab page {tabnr} to {val} -settagstack({nr}, {dict} [, {action}]) - Number modify tag stack using {dict} -setwinvar({nr}, {varname}, {val}) set {varname} in window {nr} to {val} -sha256({string}) String SHA256 checksum of {string} -shellescape({string} [, {special}]) - String escape {string} for use as shell - command argument -shiftwidth([{col}]) Number effective value of 'shiftwidth' -sign_define({name} [, {dict}]) Number define or update a sign -sign_define({list}) List define or update a list of signs -sign_getdefined([{name}]) List get a list of defined signs -sign_getplaced([{buf} [, {dict}]]) - List get a list of placed signs -sign_jump({id}, {group}, {buf}) - Number jump to a sign -sign_place({id}, {group}, {name}, {buf} [, {dict}]) - Number place a sign -sign_placelist({list}) List place a list of signs -sign_undefine([{name}]) Number undefine a sign -sign_undefine({list}) List undefine a list of signs -sign_unplace({group} [, {dict}]) - Number unplace a sign -sign_unplacelist({list}) List unplace a list of signs -simplify({filename}) String simplify filename as much as possible -sin({expr}) Float sine of {expr} -sinh({expr}) Float hyperbolic sine of {expr} -sockconnect({mode}, {address} [, {opts}]) - Number Connects to socket -sort({list} [, {func} [, {dict}]]) - List sort {list}, using {func} to compare -soundfold({word}) String sound-fold {word} -spellbadword() String badly spelled word at cursor -spellsuggest({word} [, {max} [, {capital}]]) - List spelling suggestions -split({expr} [, {pat} [, {keepempty}]]) - List make |List| from {pat} separated {expr} -sqrt({expr}) Float square root of {expr} -stdioopen({dict}) Number open stdio in a headless instance. -stdpath({what}) String/List returns the standard path(s) for {what} -str2float({expr} [, {quoted}]) Float convert String to Float -str2list({expr} [, {utf8}]) List convert each character of {expr} to - ASCII/UTF-8 value -str2nr({expr} [, {base} [, {quoted}]]) - Number convert String to Number -strchars({expr} [, {skipcc}]) Number character length of the String {expr} -strcharpart({str}, {start} [, {len}]) - String {len} characters of {str} at - character {start} -strdisplaywidth({expr} [, {col}]) Number display length of the String {expr} -strftime({format} [, {time}]) String format time with a specified format -strgetchar({str}, {index}) Number get char {index} from {str} -stridx({haystack}, {needle} [, {start}]) - Number index of {needle} in {haystack} -string({expr}) String String representation of {expr} value -strlen({expr}) Number length of the String {expr} -strpart({str}, {start} [, {len} [, {chars}]]) - String {len} bytes/chars of {str} at - byte {start} -strptime({format}, {timestring}) - Number Convert {timestring} to unix timestamp -strridx({haystack}, {needle} [, {start}]) - Number last index of {needle} in {haystack} -strtrans({expr}) String translate string to make it printable -strwidth({expr}) Number display cell length of the String {expr} -submatch({nr} [, {list}]) String or List - specific match in ":s" or substitute() -substitute({expr}, {pat}, {sub}, {flags}) - String all {pat} in {expr} replaced with {sub} -swapinfo({fname}) Dict information about swap file {fname} -swapname({buf}) String swap file of buffer {buf} -synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col} -synIDattr({synID}, {what} [, {mode}]) - String attribute {what} of syntax ID {synID} -synIDtrans({synID}) Number translated syntax ID of {synID} -synconcealed({lnum}, {col}) List info about concealing -synstack({lnum}, {col}) List stack of syntax IDs at {lnum} and {col} -system({cmd} [, {input}]) String output of shell command/filter {cmd} -systemlist({cmd} [, {input}]) List output of shell command/filter {cmd} -tabpagebuflist([{arg}]) List list of buffer numbers in tab page -tabpagenr([{arg}]) Number number of current or last tab page -tabpagewinnr({tabarg}[, {arg}]) - Number number of current window in tab page -taglist({expr}[, {filename}]) List list of tags matching {expr} -tagfiles() List tags files used -tan({expr}) Float tangent of {expr} -tanh({expr}) Float hyperbolic tangent of {expr} -tempname() String name for a temporary file -test_garbagecollect_now() none free memory right now for testing -timer_info([{id}]) List information about timers -timer_pause({id}, {pause}) none pause or unpause a timer -timer_start({time}, {callback} [, {options}]) - Number create a timer -timer_stop({timer}) none stop a timer -timer_stopall() none stop all timers -tolower({expr}) String the String {expr} switched to lowercase -toupper({expr}) String the String {expr} switched to uppercase -tr({src}, {fromstr}, {tostr}) String translate chars of {src} in {fromstr} - to chars in {tostr} -trim({text} [, {mask} [, {dir}]]) - String trim characters in {mask} from {text} -trunc({expr}) Float truncate Float {expr} -type({name}) Number type of variable {name} -undofile({name}) String undo file name for {name} -undotree() List undo file tree -uniq({list} [, {func} [, {dict}]]) - List remove adjacent duplicates from a list -values({dict}) List values in {dict} -virtcol({expr}) Number screen column of cursor or mark -visualmode([expr]) String last visual mode used -wait({timeout}, {condition}[, {interval}]) - Number Wait until {condition} is satisfied -wildmenumode() Number whether 'wildmenu' mode is active -win_execute({id}, {command} [, {silent}]) - String execute {command} in window {id} -win_findbuf({bufnr}) List find windows containing {bufnr} -win_getid([{win} [, {tab}]]) Number get |window-ID| for {win} in {tab} -win_gettype([{nr}]) String type of window {nr} -win_gotoid({expr}) Number go to |window-ID| {expr} -win_id2tabwin({expr}) List get tab and window nr from |window-ID| -win_id2win({expr}) Number get window nr from |window-ID| -win_screenpos({nr}) List get screen position of window {nr} -win_splitmove({nr}, {target} [, {options}]) - Number move window {nr} to split of {target} -winbufnr({nr}) Number buffer number of window {nr} -wincol() Number window column of the cursor -windowsversion() String MS-Windows OS version -winheight({nr}) Number height of window {nr} -winlayout([{tabnr}]) List layout of windows in tab {tabnr} -winline() Number window line of the cursor -winnr([{expr}]) Number number of current window -winrestcmd() String returns command to restore window sizes -winrestview({dict}) none restore view of current window -winsaveview() Dict save view of current window -winwidth({nr}) Number width of window {nr} -wordcount() Dict get byte/char/word statistics -writefile({object}, {fname} [, {flags}]) - Number write |Blob| or |List| of lines to file -xor({expr}, {expr}) Number bitwise XOR - - -abs({expr}) *abs()* - Return the absolute value of {expr}. When {expr} evaluates to - a |Float| abs() returns a |Float|. When {expr} can be - converted to a |Number| abs() returns a |Number|. Otherwise - abs() gives an error message and returns -1. - Examples: > - echo abs(1.456) -< 1.456 > - echo abs(-5.456) -< 5.456 > - echo abs(-4) -< 4 - - Can also be used as a |method|: > - Compute()->abs() - -acos({expr}) *acos()* - Return the arc cosine of {expr} measured in radians, as a - |Float| in the range of [0, pi]. - {expr} must evaluate to a |Float| or a |Number| in the range - [-1, 1]. - Examples: > - :echo acos(0) -< 1.570796 > - :echo acos(-0.5) -< 2.094395 - - Can also be used as a |method|: > - Compute()->acos() - -add({object}, {expr}) *add()* - Append the item {expr} to |List| or |Blob| {object}. Returns - the resulting |List| or |Blob|. Examples: > - :let alist = add([1, 2, 3], item) - :call add(mylist, "woodstock") -< Note that when {expr} is a |List| it is appended as a single - item. Use |extend()| to concatenate |Lists|. - When {object} is a |Blob| then {expr} must be a number. - Use |insert()| to add an item at another position. - - Can also be used as a |method|: > - mylist->add(val1)->add(val2) - -and({expr}, {expr}) *and()* - Bitwise AND on the two arguments. The arguments are converted - to a number. A List, Dict or Float argument causes an error. - Example: > - :let flag = and(bits, 0x80) -< Can also be used as a |method|: > - :let flag = bits->and(0x80) - -api_info() *api_info()* - Returns Dictionary of |api-metadata|. - - View it in a nice human-readable format: > - :lua print(vim.inspect(vim.fn.api_info())) - -append({lnum}, {text}) *append()* - When {text} is a |List|: Append each item of the |List| as a - text line below line {lnum} in the current buffer. - Otherwise append {text} as one text line below line {lnum} in - the current buffer. - {lnum} can be zero to insert a line before the first one. - {lnum} is used like with |getline()|. - Returns 1 for failure ({lnum} out of range or out of memory), - 0 for success. Example: > - :let failed = append(line('$'), "# THE END") - :let failed = append(0, ["Chapter 1", "the beginning"]) - -< Can also be used as a |method| after a List: > - mylist->append(lnum) - -appendbufline({buf}, {lnum}, {text}) *appendbufline()* - Like |append()| but append the text in buffer {expr}. - - This function works only for loaded buffers. First call - |bufload()| if needed. - - For the use of {buf}, see |bufname()|. - - {lnum} is used like with |append()|. Note that using |line()| - would use the current buffer, not the one appending to. - Use "$" to append at the end of the buffer. - - On success 0 is returned, on failure 1 is returned. - - If {buf} is not a valid buffer or {lnum} is not valid, an - error message is given. Example: > - :let failed = appendbufline(13, 0, "# THE START") -< - Can also be used as a |method| after a List: > - mylist->appendbufline(buf, lnum) - -argc([{winid}]) *argc()* - The result is the number of files in the argument list. See - |arglist|. - If {winid} is not supplied, the argument list of the current - window is used. - If {winid} is -1, the global argument list is used. - Otherwise {winid} specifies the window of which the argument - list is used: either the window number or the window ID. - Returns -1 if the {winid} argument is invalid. - - *argidx()* -argidx() The result is the current index in the argument list. 0 is - the first file. argc() - 1 is the last one. See |arglist|. - - *arglistid()* -arglistid([{winnr} [, {tabnr}]]) - Return the argument list ID. This is a number which - identifies the argument list being used. Zero is used for the - global argument list. See |arglist|. - Returns -1 if the arguments are invalid. - - Without arguments use the current window. - With {winnr} only use this window in the current tab page. - With {winnr} and {tabnr} use the window in the specified tab - page. - {winnr} can be the window number or the |window-ID|. - - *argv()* -argv([{nr} [, {winid}]]) - The result is the {nr}th file in the argument list. See - |arglist|. "argv(0)" is the first one. Example: > - :let i = 0 - :while i < argc() - : let f = escape(fnameescape(argv(i)), '.') - : exe 'amenu Arg.' . f . ' :e ' . f . '<CR>' - : let i = i + 1 - :endwhile -< Without the {nr} argument, or when {nr} is -1, a |List| with - the whole |arglist| is returned. - - The {winid} argument specifies the window ID, see |argc()|. - For the Vim command line arguments see |v:argv|. - -asin({expr}) *asin()* - Return the arc sine of {expr} measured in radians, as a |Float| - in the range of [-pi/2, pi/2]. - {expr} must evaluate to a |Float| or a |Number| in the range - [-1, 1]. - Examples: > - :echo asin(0.8) -< 0.927295 > - :echo asin(-0.5) -< -0.523599 - - Can also be used as a |method|: > - Compute()->asin() - - -assert_ functions are documented here: |assert-functions-details| - - -atan({expr}) *atan()* - Return the principal value of the arc tangent of {expr}, in - the range [-pi/2, +pi/2] radians, as a |Float|. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo atan(100) -< 1.560797 > - :echo atan(-4.01) -< -1.326405 - - Can also be used as a |method|: > - Compute()->atan() - -atan2({expr1}, {expr2}) *atan2()* - Return the arc tangent of {expr1} / {expr2}, measured in - radians, as a |Float| in the range [-pi, pi]. - {expr1} and {expr2} must evaluate to a |Float| or a |Number|. - Examples: > - :echo atan2(-1, 1) -< -0.785398 > - :echo atan2(1, -1) -< 2.356194 - - Can also be used as a |method|: > - Compute()->atan2(1) - - *browse()* -browse({save}, {title}, {initdir}, {default}) - Put up a file requester. This only works when "has("browse")" - returns |TRUE| (only in some GUI versions). - The input fields are: - {save} when |TRUE|, select file to write - {title} title for the requester - {initdir} directory to start browsing in - {default} default file name - An empty string is returned when the "Cancel" button is hit, - something went wrong, or browsing is not possible. - - *browsedir()* -browsedir({title}, {initdir}) - Put up a directory requester. This only works when - "has("browse")" returns |TRUE| (only in some GUI versions). - On systems where a directory browser is not supported a file - browser is used. In that case: select a file in the directory - to be used. - The input fields are: - {title} title for the requester - {initdir} directory to start browsing in - When the "Cancel" button is hit, something went wrong, or - browsing is not possible, an empty string is returned. - -bufadd({name}) *bufadd()* - Add a buffer to the buffer list with String {name}. - If a buffer for file {name} already exists, return that buffer - number. Otherwise return the buffer number of the newly - created buffer. When {name} is an empty string then a new - buffer is always created. - The buffer will not have 'buflisted' set and not be loaded - yet. To add some text to the buffer use this: > - let bufnr = bufadd('someName') - call bufload(bufnr) - call setbufline(bufnr, 1, ['some', 'text']) -< Can also be used as a |method|: > - let bufnr = 'somename'->bufadd() - -bufexists({buf}) *bufexists()* - The result is a Number, which is |TRUE| if a buffer called - {buf} exists. - If the {buf} argument is a number, buffer numbers are used. - Number zero is the alternate buffer for the current window. - - If the {buf} argument is a string it must match a buffer name - exactly. The name can be: - - Relative to the current directory. - - A full path. - - The name of a buffer with 'buftype' set to "nofile". - - A URL name. - Unlisted buffers will be found. - Note that help files are listed by their short name in the - output of |:buffers|, but bufexists() requires using their - long name to be able to find them. - bufexists() may report a buffer exists, but to use the name - with a |:buffer| command you may need to use |expand()|. Esp - for MS-Windows 8.3 names in the form "c:\DOCUME~1" - Use "bufexists(0)" to test for the existence of an alternate - file name. - - Can also be used as a |method|: > - let exists = 'somename'->bufexists() - -buflisted({buf}) *buflisted()* - The result is a Number, which is |TRUE| if a buffer called - {buf} exists and is listed (has the 'buflisted' option set). - The {buf} argument is used like with |bufexists()|. - - Can also be used as a |method|: > - let listed = 'somename'->buflisted() - -bufload({buf}) *bufload()* - Ensure the buffer {buf} is loaded. When the buffer name - refers to an existing file then the file is read. Otherwise - the buffer will be empty. If the buffer was already loaded - then there is no change. - If there is an existing swap file for the file of the buffer, - there will be no dialog, the buffer will be loaded anyway. - The {buf} argument is used like with |bufexists()|. - - Can also be used as a |method|: > - eval 'somename'->bufload() - -bufloaded({buf}) *bufloaded()* - The result is a Number, which is |TRUE| if a buffer called - {buf} exists and is loaded (shown in a window or hidden). - The {buf} argument is used like with |bufexists()|. - - Can also be used as a |method|: > - let loaded = 'somename'->bufloaded() - -bufname([{buf}]) *bufname()* - The result is the name of a buffer. Mostly as it is displayed - by the `:ls` command, but not using special names such as - "[No Name]". - If {buf} is omitted the current buffer is used. - If {buf} is a Number, that buffer number's name is given. - Number zero is the alternate buffer for the current window. - If {buf} is a String, it is used as a |file-pattern| to match - with the buffer names. This is always done like 'magic' is - set and 'cpoptions' is empty. When there is more than one - match an empty string is returned. - "" or "%" can be used for the current buffer, "#" for the - alternate buffer. - A full match is preferred, otherwise a match at the start, end - or middle of the buffer name is accepted. If you only want a - full match then put "^" at the start and "$" at the end of the - pattern. - Listed buffers are found first. If there is a single match - with a listed buffer, that one is returned. Next unlisted - buffers are searched for. - If the {buf} is a String, but you want to use it as a buffer - number, force it to be a Number by adding zero to it: > - :echo bufname("3" + 0) -< Can also be used as a |method|: > - echo bufnr->bufname() - -< If the buffer doesn't exist, or doesn't have a name, an empty - string is returned. > - bufname("#") alternate buffer name - bufname(3) name of buffer 3 - bufname("%") name of current buffer - bufname("file2") name of buffer where "file2" matches. -< - *bufnr()* -bufnr([{buf} [, {create}]]) - The result is the number of a buffer, as it is displayed by - the `:ls` command. For the use of {buf}, see |bufname()| - above. - If the buffer doesn't exist, -1 is returned. Or, if the - {create} argument is present and TRUE, a new, unlisted, - buffer is created and its number is returned. - bufnr("$") is the last buffer: > - :let last_buffer = bufnr("$") -< The result is a Number, which is the highest buffer number - of existing buffers. Note that not all buffers with a smaller - number necessarily exist, because ":bwipeout" may have removed - them. Use bufexists() to test for the existence of a buffer. - - Can also be used as a |method|: > - echo bufref->bufnr() - -bufwinid({buf}) *bufwinid()* - The result is a Number, which is the |window-ID| of the first - window associated with buffer {buf}. For the use of {buf}, - see |bufname()| above. If buffer {buf} doesn't exist or - there is no such window, -1 is returned. Example: > - - echo "A window containing buffer 1 is " . (bufwinid(1)) -< - Only deals with the current tab page. - - Can also be used as a |method|: > - FindBuffer()->bufwinid() - -bufwinnr({buf}) *bufwinnr()* - Like |bufwinid()| but return the window number instead of the - |window-ID|. - If buffer {buf} doesn't exist or there is no such window, -1 - is returned. Example: > - - echo "A window containing buffer 1 is " . (bufwinnr(1)) - -< The number can be used with |CTRL-W_w| and ":wincmd w" - |:wincmd|. - - Can also be used as a |method|: > - FindBuffer()->bufwinnr() - -byte2line({byte}) *byte2line()* - Return the line number that contains the character at byte - count {byte} in the current buffer. This includes the - end-of-line character, depending on the 'fileformat' option - for the current buffer. The first character has byte count - one. - Also see |line2byte()|, |go| and |:goto|. - - Can also be used as a |method|: > - GetOffset()->byte2line() - -byteidx({expr}, {nr}) *byteidx()* - Return byte index of the {nr}'th character in the String - {expr}. Use zero for the first character, it then returns - zero. - If there are no multibyte characters the returned value is - equal to {nr}. - Composing characters are not counted separately, their byte - length is added to the preceding base character. See - |byteidxcomp()| below for counting composing characters - separately. - Example : > - echo matchstr(str, ".", byteidx(str, 3)) -< will display the fourth character. Another way to do the - same: > - let s = strpart(str, byteidx(str, 3)) - echo strpart(s, 0, byteidx(s, 1)) -< Also see |strgetchar()| and |strcharpart()|. - - If there are less than {nr} characters -1 is returned. - If there are exactly {nr} characters the length of the string - in bytes is returned. - - Can also be used as a |method|: > - GetName()->byteidx(idx) - -byteidxcomp({expr}, {nr}) *byteidxcomp()* - Like byteidx(), except that a composing character is counted - as a separate character. Example: > - let s = 'e' . nr2char(0x301) - echo byteidx(s, 1) - echo byteidxcomp(s, 1) - echo byteidxcomp(s, 2) -< The first and third echo result in 3 ('e' plus composing - character is 3 bytes), the second echo results in 1 ('e' is - one byte). - Only works differently from byteidx() when 'encoding' is set to - a Unicode encoding. - - Can also be used as a |method|: > - GetName()->byteidxcomp(idx) - -call({func}, {arglist} [, {dict}]) *call()* *E699* - Call function {func} with the items in |List| {arglist} as - arguments. - {func} can either be a |Funcref| or the name of a function. - a:firstline and a:lastline are set to the cursor line. - Returns the return value of the called function. - {dict} is for functions with the "dict" attribute. It will be - used to set the local variable "self". |Dictionary-function| - - Can also be used as a |method|: > - GetFunc()->call([arg, arg], dict) - -ceil({expr}) *ceil()* - Return the smallest integral value greater than or equal to - {expr} as a |Float| (round up). - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - echo ceil(1.456) -< 2.0 > - echo ceil(-5.456) -< -5.0 > - echo ceil(4.0) -< 4.0 - - Can also be used as a |method|: > - Compute()->ceil() - -changenr() *changenr()* - Return the number of the most recent change. This is the same - number as what is displayed with |:undolist| and can be used - with the |:undo| command. - When a change was made it is the number of that change. After - redo it is the number of the redone change. After undo it is - one less than the number of the undone change. - -chanclose({id}[, {stream}]) *chanclose()* - Close a channel or a specific stream associated with it. - For a job, {stream} can be one of "stdin", "stdout", - "stderr" or "rpc" (closes stdin/stdout for a job started - with `"rpc":v:true`) If {stream} is omitted, all streams - are closed. If the channel is a pty, this will then close the - pty master, sending SIGHUP to the job process. - For a socket, there is only one stream, and {stream} should be - ommited. - -chansend({id}, {data}) *chansend()* - Send data to channel {id}. For a job, it writes it to the - stdin of the process. For the stdio channel |channel-stdio|, - it writes to Nvim's stdout. Returns the number of bytes - written if the write succeeded, 0 otherwise. - See |channel-bytes| for more information. - - {data} may be a string, string convertible, |Blob|, or a list. - If {data} is a list, the items will be joined by newlines; any - newlines in an item will be sent as NUL. To send a final - newline, include a final empty string. Example: > - :call chansend(id, ["abc", "123\n456", ""]) -< will send "abc<NL>123<NUL>456<NL>". - - chansend() writes raw data, not RPC messages. If the channel - was created with `"rpc":v:true` then the channel expects RPC - messages, use |rpcnotify()| and |rpcrequest()| instead. - - -char2nr({string} [, {utf8}]) *char2nr()* - Return number value of the first char in {string}. - Examples: > - char2nr(" ") returns 32 - char2nr("ABC") returns 65 - char2nr("á") returns 225 - char2nr("á"[0]) returns 195 - char2nr("\<M-x>") returns 128 -< Non-ASCII characters are always treated as UTF-8 characters. - {utf8} is ignored, it exists only for backwards-compatibility. - A combining character is a separate character. - |nr2char()| does the opposite. - - Can also be used as a |method|: > - GetChar()->char2nr() -< - *charidx()* -charidx({string}, {idx} [, {countcc}]) - Return the character index of the byte at {idx} in {string}. - The index of the first character is zero. - If there are no multibyte characters the returned value is - equal to {idx}. - When {countcc} is omitted or |FALSE|, then composing characters - are not counted separately, their byte length is - added to the preceding base character. - When {countcc} is |TRUE|, then composing characters are - counted as separate characters. - Returns -1 if the arguments are invalid or if {idx} is greater - than the index of the last byte in {string}. An error is - given if the first argument is not a string, the second - argument is not a number or when the third argument is present - and is not zero or one. - See |byteidx()| and |byteidxcomp()| for getting the byte index - from the character index. - Examples: > - echo charidx('áb́ć', 3) returns 1 - echo charidx('áb́ć', 6, 1) returns 4 - echo charidx('áb́ć', 16) returns -1 -< - Can also be used as a |method|: > - GetName()->charidx(idx) - -chdir({dir}) *chdir()* - Change the current working directory to {dir}. The scope of - the directory change depends on the directory of the current - window: - - If the current window has a window-local directory - (|:lcd|), then changes the window local directory. - - Otherwise, if the current tabpage has a local - directory (|:tcd|) then changes the tabpage local - directory. - - Otherwise, changes the global directory. - If successful, returns the previous working directory. Pass - this to another chdir() to restore the directory. - On failure, returns an empty string. - - Example: > - let save_dir = chdir(newdir) - if save_dir - " ... do some work - call chdir(save_dir) - endif -< -cindent({lnum}) *cindent()* - Get the amount of indent for line {lnum} according the C - indenting rules, as with 'cindent'. - The indent is counted in spaces, the value of 'tabstop' is - relevant. {lnum} is used just like in |getline()|. - When {lnum} is invalid -1 is returned. - See |C-indenting|. - - Can also be used as a |method|: > - GetLnum()->cindent() - -clearmatches([{win}]) *clearmatches()* - Clears all matches previously defined for the current window - by |matchadd()| and the |:match| commands. - If {win} is specified, use the window with this number or - window ID instead of the current window. - - Can also be used as a |method|: > - GetWin()->clearmatches() -< - *col()* -col({expr}) The result is a Number, which is the byte index of the column - position given with {expr}. The accepted positions are: - . the cursor position - $ the end of the cursor line (the result is the - number of bytes in the cursor line plus one) - 'x position of mark x (if the mark is not set, 0 is - returned) - v In Visual mode: the start of the Visual area (the - cursor is the end). When not in Visual mode - returns the cursor position. Differs from |'<| in - that it's updated right away. - Additionally {expr} can be [lnum, col]: a |List| with the line - and column number. Most useful when the column is "$", to get - the last column of a specific line. When "lnum" or "col" is - out of range then col() returns zero. - To get the line number use |line()|. To get both use - |getpos()|. - For the screen column position use |virtcol()|. - Note that only marks in the current file can be used. - Examples: > - col(".") column of cursor - col("$") length of cursor line plus one - col("'t") column of mark t - col("'" . markname) column of mark markname -< The first column is 1. 0 is returned for an error. - For an uppercase mark the column may actually be in another - buffer. - For the cursor position, when 'virtualedit' is active, the - column is one higher if the cursor is after the end of the - line. This can be used to obtain the column in Insert mode: > - :imap <F2> <C-O>:let save_ve = &ve<CR> - \<C-O>:set ve=all<CR> - \<C-O>:echo col(".") . "\n" <Bar> - \let &ve = save_ve<CR> - -< Can also be used as a |method|: > - GetPos()->col() -< - -complete({startcol}, {matches}) *complete()* *E785* - Set the matches for Insert mode completion. - Can only be used in Insert mode. You need to use a mapping - with CTRL-R = (see |i_CTRL-R|). It does not work after CTRL-O - or with an expression mapping. - {startcol} is the byte offset in the line where the completed - text start. The text up to the cursor is the original text - that will be replaced by the matches. Use col('.') for an - empty string. "col('.') - 1" will replace one character by a - match. - {matches} must be a |List|. Each |List| item is one match. - See |complete-items| for the kind of items that are possible. - "longest" in 'completeopt' is ignored. - Note that the after calling this function you need to avoid - inserting anything that would cause completion to stop. - The match can be selected with CTRL-N and CTRL-P as usual with - Insert mode completion. The popup menu will appear if - specified, see |ins-completion-menu|. - Example: > - inoremap <F5> <C-R>=ListMonths()<CR> - - func! ListMonths() - call complete(col('.'), ['January', 'February', 'March', - \ 'April', 'May', 'June', 'July', 'August', 'September', - \ 'October', 'November', 'December']) - return '' - endfunc -< This isn't very useful, but it shows how it works. Note that - an empty string is returned to avoid a zero being inserted. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetMatches()->complete(col('.')) - -complete_add({expr}) *complete_add()* - Add {expr} to the list of matches. Only to be used by the - function specified with the 'completefunc' option. - Returns 0 for failure (empty string or out of memory), - 1 when the match was added, 2 when the match was already in - the list. - See |complete-functions| for an explanation of {expr}. It is - the same as one item in the list that 'omnifunc' would return. - - Can also be used as a |method|: > - GetMoreMatches()->complete_add() - -complete_check() *complete_check()* - Check for a key typed while looking for completion matches. - This is to be used when looking for matches takes some time. - Returns |TRUE| when searching for matches is to be aborted, - zero otherwise. - Only to be used by the function specified with the - 'completefunc' option. - - -complete_info([{what}]) *complete_info()* - Returns a |Dictionary| with information about Insert mode - completion. See |ins-completion|. - The items are: - mode Current completion mode name string. - See |complete_info_mode| for the values. - pum_visible |TRUE| if popup menu is visible. - See |pumvisible()|. - items List of completion matches. Each item is a - dictionary containing the entries "word", - "abbr", "menu", "kind", "info" and "user_data". - See |complete-items|. - selected Selected item index. First index is zero. - Index is -1 if no item is selected (showing - typed text only, or the last completion after - no item is selected when using the <Up> or - <Down> keys) - inserted Inserted string. [NOT IMPLEMENT YET] - - *complete_info_mode* - mode values are: - "" Not in completion mode - "keyword" Keyword completion |i_CTRL-X_CTRL-N| - "ctrl_x" Just pressed CTRL-X |i_CTRL-X| - "scroll" Scrolling with |i_CTRL-X_CTRL-E| or - |i_CTRL-X_CTRL-Y| - "whole_line" Whole lines |i_CTRL-X_CTRL-L| - "files" File names |i_CTRL-X_CTRL-F| - "tags" Tags |i_CTRL-X_CTRL-]| - "path_defines" Definition completion |i_CTRL-X_CTRL-D| - "path_patterns" Include completion |i_CTRL-X_CTRL-I| - "dictionary" Dictionary |i_CTRL-X_CTRL-K| - "thesaurus" Thesaurus |i_CTRL-X_CTRL-T| - "cmdline" Vim Command line |i_CTRL-X_CTRL-V| - "function" User defined completion |i_CTRL-X_CTRL-U| - "omni" Omni completion |i_CTRL-X_CTRL-O| - "spell" Spelling suggestions |i_CTRL-X_s| - "eval" |complete()| completion - "unknown" Other internal modes - - If the optional {what} list argument is supplied, then only - the items listed in {what} are returned. Unsupported items in - {what} are silently ignored. - - To get the position and size of the popup menu, see - |pum_getpos()|. It's also available in |v:event| during the - |CompleteChanged| event. - - Examples: > - " Get all items - call complete_info() - " Get only 'mode' - call complete_info(['mode']) - " Get only 'mode' and 'pum_visible' - call complete_info(['mode', 'pum_visible']) - -< Can also be used as a |method|: > - GetItems()->complete_info() -< - *confirm()* -confirm({msg} [, {choices} [, {default} [, {type}]]]) - Confirm() offers the user a dialog, from which a choice can be - made. It returns the number of the choice. For the first - choice this is 1. - - {msg} is displayed in a dialog with {choices} as the - alternatives. When {choices} is missing or empty, "&OK" is - used (and translated). - {msg} is a String, use '\n' to include a newline. Only on - some systems the string is wrapped when it doesn't fit. - - {choices} is a String, with the individual choices separated - by '\n', e.g. > - confirm("Save changes?", "&Yes\n&No\n&Cancel") -< The letter after the '&' is the shortcut key for that choice. - Thus you can type 'c' to select "Cancel". The shortcut does - not need to be the first letter: > - confirm("file has been modified", "&Save\nSave &All") -< For the console, the first letter of each choice is used as - the default shortcut key. Case is ignored. - - The optional {type} String argument gives the type of dialog. - It can be one of these values: "Error", "Question", "Info", - "Warning" or "Generic". Only the first character is relevant. - When {type} is omitted, "Generic" is used. - - The optional {type} argument gives the type of dialog. This - is only used for the icon of the Win32 GUI. It can be one of - these values: "Error", "Question", "Info", "Warning" or - "Generic". Only the first character is relevant. - When {type} is omitted, "Generic" is used. - - If the user aborts the dialog by pressing <Esc>, CTRL-C, - or another valid interrupt key, confirm() returns 0. - - An example: > - :let choice = confirm("What do you want?", "&Apples\n&Oranges\n&Bananas", 2) - :if choice == 0 - : echo "make up your mind!" - :elseif choice == 3 - : echo "tasteful" - :else - : echo "I prefer bananas myself." - :endif -< In a GUI dialog, buttons are used. The layout of the buttons - depends on the 'v' flag in 'guioptions'. If it is included, - the buttons are always put vertically. Otherwise, confirm() - tries to put the buttons in one horizontal line. If they - don't fit, a vertical layout is used anyway. For some systems - the horizontal layout is always used. - - Can also be used as a |method|in: > - BuildMessage()->confirm("&Yes\n&No") -< - *copy()* -copy({expr}) Make a copy of {expr}. For Numbers and Strings this isn't - different from using {expr} directly. - When {expr} is a |List| a shallow copy is created. This means - that the original |List| can be changed without changing the - copy, and vice versa. But the items are identical, thus - changing an item changes the contents of both |Lists|. - A |Dictionary| is copied in a similar way as a |List|. - Also see |deepcopy()|. - Can also be used as a |method|: > - mylist->copy() - -cos({expr}) *cos()* - Return the cosine of {expr}, measured in radians, as a |Float|. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo cos(100) -< 0.862319 > - :echo cos(-4.01) -< -0.646043 - - Can also be used as a |method|: > - Compute()->cos() - -cosh({expr}) *cosh()* - Return the hyperbolic cosine of {expr} as a |Float| in the range - [1, inf]. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo cosh(0.5) -< 1.127626 > - :echo cosh(-0.5) -< -1.127626 - - Can also be used as a |method|: > - Compute()->cosh() - -count({comp}, {expr} [, {ic} [, {start}]]) *count()* - Return the number of times an item with value {expr} appears - in |String|, |List| or |Dictionary| {comp}. - - If {start} is given then start with the item with this index. - {start} can only be used with a |List|. - - When {ic} is given and it's |TRUE| then case is ignored. - - When {comp} is a string then the number of not overlapping - occurrences of {expr} is returned. Zero is returned when - {expr} is an empty string. - - Can also be used as a |method|: > - mylist->count(val) -< - *cscope_connection()* -cscope_connection([{num} , {dbpath} [, {prepend}]]) - Checks for the existence of a |cscope| connection. If no - parameters are specified, then the function returns: - 0, if there are no cscope connections; - 1, if there is at least one cscope connection. - - If parameters are specified, then the value of {num} - determines how existence of a cscope connection is checked: - - {num} Description of existence check - ----- ------------------------------ - 0 Same as no parameters (e.g., "cscope_connection()"). - 1 Ignore {prepend}, and use partial string matches for - {dbpath}. - 2 Ignore {prepend}, and use exact string matches for - {dbpath}. - 3 Use {prepend}, use partial string matches for both - {dbpath} and {prepend}. - 4 Use {prepend}, use exact string matches for both - {dbpath} and {prepend}. - - Note: All string comparisons are case sensitive! - - Examples. Suppose we had the following (from ":cs show"): > - - # pid database name prepend path - 0 27664 cscope.out /usr/local -< - Invocation Return Val ~ - ---------- ---------- > - cscope_connection() 1 - cscope_connection(1, "out") 1 - cscope_connection(2, "out") 0 - cscope_connection(3, "out") 0 - cscope_connection(3, "out", "local") 1 - cscope_connection(4, "out") 0 - cscope_connection(4, "out", "local") 0 - cscope_connection(4, "cscope.out", "/usr/local") 1 -< - -ctxget([{index}]) *ctxget()* - Returns a |Dictionary| representing the |context| at {index} - from the top of the |context-stack| (see |context-dict|). - If {index} is not given, it is assumed to be 0 (i.e.: top). - -ctxpop() *ctxpop()* - Pops and restores the |context| at the top of the - |context-stack|. - -ctxpush([{types}]) *ctxpush()* - Pushes the current editor state (|context|) on the - |context-stack|. - If {types} is given and is a |List| of |String|s, it specifies - which |context-types| to include in the pushed context. - Otherwise, all context types are included. - -ctxset({context}[, {index}]) *ctxset()* - Sets the |context| at {index} from the top of the - |context-stack| to that represented by {context}. - {context} is a Dictionary with context data (|context-dict|). - If {index} is not given, it is assumed to be 0 (i.e.: top). - -ctxsize() *ctxsize()* - Returns the size of the |context-stack|. - -cursor({lnum}, {col} [, {off}]) *cursor()* -cursor({list}) - Positions the cursor at the column (byte count) {col} in the - line {lnum}. The first column is one. - - When there is one argument {list} this is used as a |List| - with two, three or four item: - [{lnum}, {col}] - [{lnum}, {col}, {off}] - [{lnum}, {col}, {off}, {curswant}] - This is like the return value of |getpos()| or |getcurpos()|, - but without the first item. - - Does not change the jumplist. - If {lnum} is greater than the number of lines in the buffer, - the cursor will be positioned at the last line in the buffer. - If {lnum} is zero, the cursor will stay in the current line. - If {col} is greater than the number of bytes in the line, - the cursor will be positioned at the last character in the - line. - If {col} is zero, the cursor will stay in the current column. - If {curswant} is given it is used to set the preferred column - for vertical movement. Otherwise {col} is used. - - When 'virtualedit' is used {off} specifies the offset in - screen columns from the start of the character. E.g., a - position within a <Tab> or after the last character. - Returns 0 when the position could be set, -1 otherwise. - - Can also be used as a |method|: > - GetCursorPos()->cursor() - -deepcopy({expr}[, {noref}]) *deepcopy()* *E698* - Make a copy of {expr}. For Numbers and Strings this isn't - different from using {expr} directly. - When {expr} is a |List| a full copy is created. This means - that the original |List| can be changed without changing the - copy, and vice versa. When an item is a |List|, a copy for it - is made, recursively. Thus changing an item in the copy does - not change the contents of the original |List|. - - When {noref} is omitted or zero a contained |List| or - |Dictionary| is only copied once. All references point to - this single copy. With {noref} set to 1 every occurrence of a - |List| or |Dictionary| results in a new copy. This also means - that a cyclic reference causes deepcopy() to fail. - *E724* - Nesting is possible up to 100 levels. When there is an item - that refers back to a higher level making a deep copy with - {noref} set to 1 will fail. - Also see |copy()|. - - Can also be used as a |method|: > - GetObject()->deepcopy() - -delete({fname} [, {flags}]) *delete()* - Without {flags} or with {flags} empty: Deletes the file by the - name {fname}. This also works when {fname} is a symbolic link. - A symbolic link itself is deleted, not what it points to. - - When {flags} is "d": Deletes the directory by the name - {fname}. This fails when directory {fname} is not empty. - - When {flags} is "rf": Deletes the directory by the name - {fname} and everything in it, recursively. BE CAREFUL! - Note: on MS-Windows it is not possible to delete a directory - that is being used. - - The result is a Number, which is 0/false if the delete - operation was successful and -1/true when the deletion failed - or partly failed. - - Can also be used as a |method|: > - GetName()->delete() - -deletebufline({buf}, {first}[, {last}]) *deletebufline()* - Delete lines {first} to {last} (inclusive) from buffer {buf}. - If {last} is omitted then delete line {first} only. - On success 0 is returned, on failure 1 is returned. - - This function works only for loaded buffers. First call - |bufload()| if needed. - - For the use of {buf}, see |bufname()| above. - - {first} and {last} are used like with |setline()|. Note that - when using |line()| this refers to the current buffer. Use "$" - to refer to the last line in buffer {buf}. - - Can also be used as a |method|: > - GetBuffer()->deletebufline(1) -< -dictwatcheradd({dict}, {pattern}, {callback}) *dictwatcheradd()* - Adds a watcher to a dictionary. A dictionary watcher is - identified by three components: - - - A dictionary({dict}); - - A key pattern({pattern}). - - A function({callback}). - - After this is called, every change on {dict} and on keys - matching {pattern} will result in {callback} being invoked. - - For example, to watch all global variables: > - silent! call dictwatcherdel(g:, '*', 'OnDictChanged') - function! OnDictChanged(d,k,z) - echomsg string(a:k) string(a:z) - endfunction - call dictwatcheradd(g:, '*', 'OnDictChanged') -< - For now {pattern} only accepts very simple patterns that can - contain a '*' at the end of the string, in which case it will - match every key that begins with the substring before the '*'. - That means if '*' is not the last character of {pattern}, only - keys that are exactly equal as {pattern} will be matched. - - The {callback} receives three arguments: - - - The dictionary being watched. - - The key which changed. - - A dictionary containing the new and old values for the key. - - The type of change can be determined by examining the keys - present on the third argument: - - - If contains both `old` and `new`, the key was updated. - - If it contains only `new`, the key was added. - - If it contains only `old`, the key was deleted. - - This function can be used by plugins to implement options with - validation and parsing logic. - -dictwatcherdel({dict}, {pattern}, {callback}) *dictwatcherdel()* - Removes a watcher added with |dictwatcheradd()|. All three - arguments must match the ones passed to |dictwatcheradd()| in - order for the watcher to be successfully deleted. - - *did_filetype()* -did_filetype() Returns |TRUE| when autocommands are being executed and the - FileType event has been triggered at least once. Can be used - to avoid triggering the FileType event again in the scripts - that detect the file type. |FileType| - Returns |FALSE| when `:setf FALLBACK` was used. - When editing another file, the counter is reset, thus this - really checks if the FileType event has been triggered for the - current buffer. This allows an autocommand that starts - editing another buffer to set 'filetype' and load a syntax - file. - -diff_filler({lnum}) *diff_filler()* - Returns the number of filler lines above line {lnum}. - These are the lines that were inserted at this point in - another diff'ed window. These filler lines are shown in the - display but don't exist in the buffer. - {lnum} is used like with |getline()|. Thus "." is the current - line, "'m" mark m, etc. - Returns 0 if the current window is not in diff mode. - - Can also be used as a |method|: > - GetLnum()->diff_filler() - -diff_hlID({lnum}, {col}) *diff_hlID()* - Returns the highlight ID for diff mode at line {lnum} column - {col} (byte index). When the current line does not have a - diff change zero is returned. - {lnum} is used like with |getline()|. Thus "." is the current - line, "'m" mark m, etc. - {col} is 1 for the leftmost column, {lnum} is 1 for the first - line. - The highlight ID can be used with |synIDattr()| to obtain - syntax information about the highlighting. - - Can also be used as a |method|: > - GetLnum()->diff_hlID(col) - -empty({expr}) *empty()* - Return the Number 1 if {expr} is empty, zero otherwise. - - A |List| or |Dictionary| is empty when it does not have any - items. - - A |String| is empty when its length is zero. - - A |Number| and |Float| are empty when their value is zero. - - |v:false| and |v:null| are empty, |v:true| is not. - - A |Blob| is empty when its length is zero. - - Can also be used as a |method|: > - mylist->empty() - -environ() *environ()* - Return all of environment variables as dictionary. You can - check if an environment variable exists like this: > - :echo has_key(environ(), 'HOME') -< Note that the variable name may be CamelCase; to ignore case - use this: > - :echo index(keys(environ()), 'HOME', 0, 1) != -1 - -escape({string}, {chars}) *escape()* - Escape the characters in {chars} that occur in {string} with a - backslash. Example: > - :echo escape('c:\program files\vim', ' \') -< results in: > - c:\\program\ files\\vim -< Also see |shellescape()| and |fnameescape()|. - - Can also be used as a |method|: > - GetText()->escape(' \') -< - *eval()* -eval({string}) Evaluate {string} and return the result. Especially useful to - turn the result of |string()| back into the original value. - This works for Numbers, Floats, Strings, Blobs and composites - of them. Also works for |Funcref|s that refer to existing - functions. - - Can also be used as a |method|: > - argv->join()->eval() - -eventhandler() *eventhandler()* - Returns 1 when inside an event handler. That is that Vim got - interrupted while waiting for the user to type a character, - e.g., when dropping a file on Vim. This means interactive - commands cannot be used. Otherwise zero is returned. - -executable({expr}) *executable()* - This function checks if an executable with the name {expr} - exists. {expr} must be the name of the program without any - arguments. - executable() uses the value of $PATH and/or the normal - searchpath for programs. *PATHEXT* - On MS-Windows the ".exe", ".bat", etc. can optionally be - included. Then the extensions in $PATHEXT are tried. Thus if - "foo.exe" does not exist, "foo.exe.bat" can be found. If - $PATHEXT is not set then ".exe;.com;.bat;.cmd" is used. A dot - by itself can be used in $PATHEXT to try using the name - without an extension. When 'shell' looks like a Unix shell, - then the name is also tried without adding an extension. - On MS-Windows it only checks if the file exists and is not a - directory, not if it's really executable. - On Windows an executable in the same directory as Vim is - always found (it is added to $PATH at |startup|). - The result is a Number: - 1 exists - 0 does not exist - -1 not implemented on this system - |exepath()| can be used to get the full path of an executable. - - Can also be used as a |method|: > - GetCommand()->executable() - -execute({command} [, {silent}]) *execute()* - Execute {command} and capture its output. - If {command} is a |String|, returns {command} output. - If {command} is a |List|, returns concatenated outputs. - Examples: > - echo execute('echon "foo"') -< foo > - echo execute(['echon "foo"', 'echon "bar"']) -< foobar - - The optional {silent} argument can have these values: - "" no `:silent` used - "silent" `:silent` used - "silent!" `:silent!` used - The default is "silent". Note that with "silent!", unlike - `:redir`, error messages are dropped. - - To get a list of lines use |split()| on the result: > - split(execute('args'), "\n") - -< This function is not available in the |sandbox|. - Note: If nested, an outer execute() will not observe output of - the inner calls. - Note: Text attributes (highlights) are not captured. - To execute a command in another window than the current one - use `win_execute()`. - - Can also be used as a |method|: > - GetCommand()->execute() - -exepath({expr}) *exepath()* - Returns the full path of {expr} if it is an executable and - given as a (partial or full) path or is found in $PATH. - Returns empty string otherwise. - If {expr} starts with "./" the |current-directory| is used. - - Can also be used as a |method|: > - GetCommand()->exepath() -< - *exists()* -exists({expr}) The result is a Number, which is |TRUE| if {expr} is - defined, zero otherwise. - - For checking for a supported feature use |has()|. - For checking if a file exists use |filereadable()|. - - The {expr} argument is a string, which contains one of these: - &option-name Vim option (only checks if it exists, - not if it really works) - +option-name Vim option that works. - $ENVNAME environment variable (could also be - done by comparing with an empty - string) - *funcname built-in function (see |functions|) - or user defined function (see - |user-function|). Also works for a - variable that is a Funcref. - varname internal variable (see - |internal-variables|). Also works - for |curly-braces-names|, |Dictionary| - entries, |List| items, etc. Beware - that evaluating an index may cause an - error message for an invalid - expression. E.g.: > - :let l = [1, 2, 3] - :echo exists("l[5]") -< 0 > - :echo exists("l[xx]") -< E121: Undefined variable: xx - 0 - :cmdname Ex command: built-in command, user - command or command modifier |:command|. - Returns: - 1 for match with start of a command - 2 full match with a command - 3 matches several user commands - To check for a supported command - always check the return value to be 2. - :2match The |:2match| command. - :3match The |:3match| command. - #event autocommand defined for this event - #event#pattern autocommand defined for this event and - pattern (the pattern is taken - literally and compared to the - autocommand patterns character by - character) - #group autocommand group exists - #group#event autocommand defined for this group and - event. - #group#event#pattern - autocommand defined for this group, - event and pattern. - ##event autocommand for this event is - supported. - - Examples: > - exists("&mouse") - exists("$HOSTNAME") - exists("*strftime") - exists("*s:MyFunc") - exists("bufcount") - exists(":Make") - exists("#CursorHold") - exists("#BufReadPre#*.gz") - exists("#filetypeindent") - exists("#filetypeindent#FileType") - exists("#filetypeindent#FileType#*") - exists("##ColorScheme") -< There must be no space between the symbol (&/$/*/#) and the - name. - There must be no extra characters after the name, although in - a few cases this is ignored. That may become more strict in - the future, thus don't count on it! - Working example: > - exists(":make") -< NOT working example: > - exists(":make install") - -< Note that the argument must be a string, not the name of the - variable itself. For example: > - exists(bufcount) -< This doesn't check for existence of the "bufcount" variable, - but gets the value of "bufcount", and checks if that exists. - - Can also be used as a |method|: > - Varname()->exists() - -exp({expr}) *exp()* - Return the exponential of {expr} as a |Float| in the range - [0, inf]. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo exp(2) -< 7.389056 > - :echo exp(-1) -< 0.367879 - - Can also be used as a |method|: > - Compute()->exp() - -debugbreak({pid}) *debugbreak()* - Specifically used to interrupt a program being debugged. It - will cause process {pid} to get a SIGTRAP. Behavior for other - processes is undefined. See |terminal-debugger|. - {Sends a SIGINT to a process {pid} other than MS-Windows} - - Can also be used as a |method|: > - GetPid()->debugbreak() - -expand({string} [, {nosuf} [, {list}]]) *expand()* - Expand wildcards and the following special keywords in - {string}. 'wildignorecase' applies. - - If {list} is given and it is |TRUE|, a List will be returned. - Otherwise the result is a String and when there are several - matches, they are separated by <NL> characters. - - If the expansion fails, the result is an empty string. A name - for a non-existing file is not included, unless {string} does - not start with '%', '#' or '<', see below. - - When {string} starts with '%', '#' or '<', the expansion is - done like for the |cmdline-special| variables with their - associated modifiers. Here is a short overview: - - % current file name - # alternate file name - #n alternate file name n - <cfile> file name under the cursor - <afile> autocmd file name - <abuf> autocmd buffer number (as a String!) - <amatch> autocmd matched name - <sfile> sourced script file or function name - <slnum> sourced script line number or function - line number - <sflnum> script file line number, also when in - a function - <SID> "<SNR>123_" where "123" is the - current script ID |<SID>| - <cword> word under the cursor - <cWORD> WORD under the cursor - <client> the {clientid} of the last received - message |server2client()| - Modifiers: - :p expand to full path - :h head (last path component removed) - :t tail (last path component only) - :r root (one extension removed) - :e extension only - - Example: > - :let &tags = expand("%:p:h") . "/tags" -< Note that when expanding a string that starts with '%', '#' or - '<', any following text is ignored. This does NOT work: > - :let doesntwork = expand("%:h.bak") -< Use this: > - :let doeswork = expand("%:h") . ".bak" -< Also note that expanding "<cfile>" and others only returns the - referenced file name without further expansion. If "<cfile>" - is "~/.cshrc", you need to do another expand() to have the - "~/" expanded into the path of the home directory: > - :echo expand(expand("<cfile>")) -< - There cannot be white space between the variables and the - following modifier. The |fnamemodify()| function can be used - to modify normal file names. - - When using '%' or '#', and the current or alternate file name - is not defined, an empty string is used. Using "%:p" in a - buffer with no name, results in the current directory, with a - '/' added. - - When {string} does not start with '%', '#' or '<', it is - expanded like a file name is expanded on the command line. - 'suffixes' and 'wildignore' are used, unless the optional - {nosuf} argument is given and it is |TRUE|. - Names for non-existing files are included. The "**" item can - be used to search in a directory tree. For example, to find - all "README" files in the current directory and below: > - :echo expand("**/README") -< - expand() can also be used to expand variables and environment - variables that are only known in a shell. But this can be - slow, because a shell may be used to do the expansion. See - |expr-env-expand|. - The expanded variable is still handled like a list of file - names. When an environment variable cannot be expanded, it is - left unchanged. Thus ":echo expand('$FOOBAR')" results in - "$FOOBAR". - - See |glob()| for finding existing files. See |system()| for - getting the raw output of an external command. - - Can also be used as a |method|: > - Getpattern()->expand() - -expandcmd({string}) *expandcmd()* - Expand special items in String {string} like what is done for - an Ex command such as `:edit`. This expands special keywords, - like with |expand()|, and environment variables, anywhere in - {string}. "~user" and "~/path" are only expanded at the - start. - Returns the expanded string. Example: > - :echo expandcmd('make %<.o') - -< Can also be used as a |method|: > - GetCommand()->expandcmd() -< -extend({expr1}, {expr2} [, {expr3}]) *extend()* - {expr1} and {expr2} must be both |Lists| or both - |Dictionaries|. - - If they are |Lists|: Append {expr2} to {expr1}. - If {expr3} is given insert the items of {expr2} before the - item with index {expr3} in {expr1}. When {expr3} is zero - insert before the first item. When {expr3} is equal to - len({expr1}) then {expr2} is appended. - Examples: > - :echo sort(extend(mylist, [7, 5])) - :call extend(mylist, [2, 3], 1) -< When {expr1} is the same List as {expr2} then the number of - items copied is equal to the original length of the List. - E.g., when {expr3} is 1 you get N new copies of the first item - (where N is the original length of the List). - Use |add()| to concatenate one item to a list. To concatenate - two lists into a new list use the + operator: > - :let newlist = [1, 2, 3] + [4, 5] -< - If they are |Dictionaries|: - Add all entries from {expr2} to {expr1}. - If a key exists in both {expr1} and {expr2} then {expr3} is - used to decide what to do: - {expr3} = "keep": keep the value of {expr1} - {expr3} = "force": use the value of {expr2} - {expr3} = "error": give an error message *E737* - When {expr3} is omitted then "force" is assumed. - - {expr1} is changed when {expr2} is not empty. If necessary - make a copy of {expr1} first. - {expr2} remains unchanged. - When {expr1} is locked and {expr2} is not empty the operation - fails. - Returns {expr1}. - - Can also be used as a |method|: > - mylist->extend(otherlist) - -feedkeys({string} [, {mode}]) *feedkeys()* - Characters in {string} are queued for processing as if they - come from a mapping or were typed by the user. - - By default the string is added to the end of the typeahead - buffer, thus if a mapping is still being executed the - characters come after them. Use the 'i' flag to insert before - other characters, they will be executed next, before any - characters from a mapping. - - The function does not wait for processing of keys contained in - {string}. - - To include special keys into {string}, use double-quotes - and "\..." notation |expr-quote|. For example, - feedkeys("\<CR>") simulates pressing of the <Enter> key. But - feedkeys('\<CR>') pushes 5 characters. - The |<Ignore>| keycode may be used to exit the - wait-for-character without doing anything. - - {mode} is a String, which can contain these character flags: - 'm' Remap keys. This is default. If {mode} is absent, - keys are remapped. - 'n' Do not remap keys. - 't' Handle keys as if typed; otherwise they are handled as - if coming from a mapping. This matters for undo, - opening folds, etc. - 'i' Insert the string instead of appending (see above). - 'x' Execute commands until typeahead is empty. This is - similar to using ":normal!". You can call feedkeys() - several times without 'x' and then one time with 'x' - (possibly with an empty {string}) to execute all the - typeahead. Note that when Vim ends in Insert mode it - will behave as if <Esc> is typed, to avoid getting - stuck, waiting for a character to be typed before the - script continues. - Note that if you manage to call feedkeys() while - executing commands, thus calling it recursively, then - all typehead will be consumed by the last call. - '!' When used with 'x' will not end Insert mode. Can be - used in a test when a timer is set to exit Insert mode - a little later. Useful for testing CursorHoldI. - - Return value is always 0. - - Can also be used as a |method|: > - GetInput()->feedkeys() - -filereadable({file}) *filereadable()* - The result is a Number, which is |TRUE| when a file with the - name {file} exists, and can be read. If {file} doesn't exist, - or is a directory, the result is |FALSE|. {file} is any - expression, which is used as a String. - If you don't care about the file being readable you can use - |glob()|. - {file} is used as-is, you may want to expand wildcards first: > - echo filereadable('~/.vimrc') - 0 - echo filereadable(expand('~/.vimrc')) - 1 - -< Can also be used as a |method|: > - GetName()->filereadable() - -filewritable({file}) *filewritable()* - The result is a Number, which is 1 when a file with the - name {file} exists, and can be written. If {file} doesn't - exist, or is not writable, the result is 0. If {file} is a - directory, and we can write to it, the result is 2. - - Can also be used as a |method|: > - GetName()->filewriteable() - -filter({expr1}, {expr2}) *filter()* - {expr1} must be a |List|, |Blob|, or a |Dictionary|. - For each item in {expr1} evaluate {expr2} and when the result - is zero remove the item from the |List| or |Dictionary|. For a - |Blob| each byte is removed. - - {expr2} must be a |string| or |Funcref|. - - If {expr2} is a |string|, inside {expr2} |v:val| has the value - of the current item. For a |Dictionary| |v:key| has the key - of the current item and for a |List| |v:key| has the index of - the current item. For a |Blob| |v:key| has the index of the - current byte. - - Examples: > - call filter(mylist, 'v:val !~ "OLD"') -< Removes the items where "OLD" appears. > - call filter(mydict, 'v:key >= 8') -< Removes the items with a key below 8. > - call filter(var, 0) -< Removes all the items, thus clears the |List| or |Dictionary|. - - Note that {expr2} is the result of expression and is then - used as an expression again. Often it is good to use a - |literal-string| to avoid having to double backslashes. - - If {expr2} is a |Funcref| it must take two arguments: - 1. the key or the index of the current item. - 2. the value of the current item. - The function must return |TRUE| if the item should be kept. - Example that keeps the odd items of a list: > - func Odd(idx, val) - return a:idx % 2 == 1 - endfunc - call filter(mylist, function('Odd')) -< It is shorter when using a |lambda|: > - call filter(myList, {idx, val -> idx * val <= 42}) -< If you do not use "val" you can leave it out: > - call filter(myList, {idx -> idx % 2 == 1}) -< - The operation is done in-place. If you want a |List| or - |Dictionary| to remain unmodified make a copy first: > - :let l = filter(copy(mylist), 'v:val =~ "KEEP"') - -< Returns {expr1}, the |List|, |Blob| or |Dictionary| that was - filtered. When an error is encountered while evaluating - {expr2} no further items in {expr1} are processed. When - {expr2} is a Funcref errors inside a function are ignored, - unless it was defined with the "abort" flag. - - Can also be used as a |method|: > - mylist->filter(expr2) - -finddir({name} [, {path} [, {count}]]) *finddir()* - Find directory {name} in {path}. Supports both downwards and - upwards recursive directory searches. See |file-searching| - for the syntax of {path}. - - Returns the path of the first found match. When the found - directory is below the current directory a relative path is - returned. Otherwise a full path is returned. - If {path} is omitted or empty then 'path' is used. - - If the optional {count} is given, find {count}'s occurrence of - {name} in {path} instead of the first one. - When {count} is negative return all the matches in a |List|. - - This is quite similar to the ex-command `:find`. - - Can also be used as a |method|: > - GetName()->finddir() - -findfile({name} [, {path} [, {count}]]) *findfile()* - Just like |finddir()|, but find a file instead of a directory. - Uses 'suffixesadd'. - Example: > - :echo findfile("tags.vim", ".;") -< Searches from the directory of the current file upwards until - it finds the file "tags.vim". - - Can also be used as a |method|: > - GetName()->findfile() - -flatten({list} [, {maxdepth}]) *flatten()* - Flatten {list} up to {maxdepth} levels. Without {maxdepth} - the result is a |List| without nesting, as if {maxdepth} is - a very large number. - The {list} is changed in place, make a copy first if you do - not want that. - *E900* - {maxdepth} means how deep in nested lists changes are made. - {list} is not modified when {maxdepth} is 0. - {maxdepth} must be positive number. - - If there is an error the number zero is returned. - - Example: > - :echo flatten([1, [2, [3, 4]], 5]) -< [1, 2, 3, 4, 5] > - :echo flatten([1, [2, [3, 4]], 5], 1) -< [1, 2, [3, 4], 5] - - Can also be used as a |method|: > - mylist->flatten() -< -float2nr({expr}) *float2nr()* - Convert {expr} to a Number by omitting the part after the - decimal point. - {expr} must evaluate to a |Float| or a Number. - When the value of {expr} is out of range for a |Number| the - result is truncated to 0x7fffffff or -0x7fffffff (or when - 64-bit Number support is enabled, 0x7fffffffffffffff or - -0x7fffffffffffffff). NaN results in -0x80000000 (or when - 64-bit Number support is enabled, -0x8000000000000000). - Examples: > - echo float2nr(3.95) -< 3 > - echo float2nr(-23.45) -< -23 > - echo float2nr(1.0e100) -< 2147483647 (or 9223372036854775807) > - echo float2nr(-1.0e150) -< -2147483647 (or -9223372036854775807) > - echo float2nr(1.0e-100) -< 0 - - Can also be used as a |method|: > - Compute()->float2nr() - -floor({expr}) *floor()* - Return the largest integral value less than or equal to - {expr} as a |Float| (round down). - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - echo floor(1.856) -< 1.0 > - echo floor(-5.456) -< -6.0 > - echo floor(4.0) -< 4.0 - - Can also be used as a |method|: > - Compute()->floor() - -fmod({expr1}, {expr2}) *fmod()* - Return the remainder of {expr1} / {expr2}, even if the - division is not representable. Returns {expr1} - i * {expr2} - for some integer i such that if {expr2} is non-zero, the - result has the same sign as {expr1} and magnitude less than - the magnitude of {expr2}. If {expr2} is zero, the value - returned is zero. The value returned is a |Float|. - {expr1} and {expr2} must evaluate to a |Float| or a |Number|. - Examples: > - :echo fmod(12.33, 1.22) -< 0.13 > - :echo fmod(-12.33, 1.22) -< -0.13 - - Can also be used as a |method|: > - Compute()->fmod(1.22) - -fnameescape({string}) *fnameescape()* - Escape {string} for use as file name command argument. All - characters that have a special meaning, such as '%' and '|' - are escaped with a backslash. - For most systems the characters escaped are - " \t\n*?[{`$\\%#'\"|!<". For systems where a backslash - appears in a filename, it depends on the value of 'isfname'. - A leading '+' and '>' is also escaped (special after |:edit| - and |:write|). And a "-" by itself (special after |:cd|). - Example: > - :let fname = '+some str%nge|name' - :exe "edit " . fnameescape(fname) -< results in executing: > - edit \+some\ str\%nge\|name -< - Can also be used as a |method|: > - GetName()->fnameescape() - -fnamemodify({fname}, {mods}) *fnamemodify()* - Modify file name {fname} according to {mods}. {mods} is a - string of characters like it is used for file names on the - command line. See |filename-modifiers|. - Example: > - :echo fnamemodify("main.c", ":p:h") -< results in: > - /home/mool/vim/vim/src -< If {mods} is empty then {fname} is returned. - Note: Environment variables don't work in {fname}, use - |expand()| first then. - - Can also be used as a |method|: > - GetName()->fnamemodify(':p:h') - -foldclosed({lnum}) *foldclosed()* - The result is a Number. If the line {lnum} is in a closed - fold, the result is the number of the first line in that fold. - If the line {lnum} is not in a closed fold, -1 is returned. - {lnum} is used like with |getline()|. Thus "." is the current - line, "'m" mark m, etc. - - Can also be used as a |method|: > - GetLnum()->foldclosed() - -foldclosedend({lnum}) *foldclosedend()* - The result is a Number. If the line {lnum} is in a closed - fold, the result is the number of the last line in that fold. - If the line {lnum} is not in a closed fold, -1 is returned. - {lnum} is used like with |getline()|. Thus "." is the current - line, "'m" mark m, etc. - - Can also be used as a |method|: > - GetLnum()->foldclosedend() - -foldlevel({lnum}) *foldlevel()* - The result is a Number, which is the foldlevel of line {lnum} - in the current buffer. For nested folds the deepest level is - returned. If there is no fold at line {lnum}, zero is - returned. It doesn't matter if the folds are open or closed. - When used while updating folds (from 'foldexpr') -1 is - returned for lines where folds are still to be updated and the - foldlevel is unknown. As a special case the level of the - previous line is usually available. - {lnum} is used like with |getline()|. Thus "." is the current - line, "'m" mark m, etc. - - Can also be used as a |method|: > - GetLnum()->foldlevel() -< - *foldtext()* -foldtext() Returns a String, to be displayed for a closed fold. This is - the default function used for the 'foldtext' option and should - only be called from evaluating 'foldtext'. It uses the - |v:foldstart|, |v:foldend| and |v:folddashes| variables. - The returned string looks like this: > - +-- 45 lines: abcdef -< The number of leading dashes depends on the foldlevel. The - "45" is the number of lines in the fold. "abcdef" is the text - in the first non-blank line of the fold. Leading white space, - "//" or "/*" and the text from the 'foldmarker' and - 'commentstring' options is removed. - When used to draw the actual foldtext, the rest of the line - will be filled with the fold char from the 'fillchars' - setting. - -foldtextresult({lnum}) *foldtextresult()* - Returns the text that is displayed for the closed fold at line - {lnum}. Evaluates 'foldtext' in the appropriate context. - When there is no closed fold at {lnum} an empty string is - returned. - {lnum} is used like with |getline()|. Thus "." is the current - line, "'m" mark m, etc. - Useful when exporting folded text, e.g., to HTML. - - Can also be used as a |method|: > - GetLnum()->foldtextresult() -< - *foreground()* -foreground() Move the Vim window to the foreground. Useful when sent from - a client to a Vim server. |remote_send()| - On Win32 systems this might not work, the OS does not always - allow a window to bring itself to the foreground. Use - |remote_foreground()| instead. - {only in the Win32 GUI and console version} - -fullcommand({name}) *fullcommand()* - Get the full command name from a short abbreviated command - name; see |20.2| for details on command abbreviations. - - The string argument {name} may start with a `:` and can - include a [range], these are skipped and not returned. - Returns an empty string if a command doesn't exist or if it's - ambiguous (for user-defined commands). - - For example `fullcommand('s')`, `fullcommand('sub')`, - `fullcommand(':%substitute')` all return "substitute". - - Can also be used as a |method|: > - GetName()->fullcommand() -< - *funcref()* -funcref({name} [, {arglist}] [, {dict}]) - Just like |function()|, but the returned Funcref will lookup - the function by reference, not by name. This matters when the - function {name} is redefined later. - - Unlike |function()|, {name} must be an existing user function. - Also for autoloaded functions. {name} cannot be a builtin - function. - - Can also be used as a |method|: > - GetFuncname()->funcref([arg]) -< - *function()* *E700* *E922* *E923* -function({name} [, {arglist}] [, {dict}]) - Return a |Funcref| variable that refers to function {name}. - {name} can be a user defined function or an internal function. - - {name} can also be a Funcref or a partial. When it is a - partial the dict stored in it will be used and the {dict} - argument is not allowed. E.g.: > - let FuncWithArg = function(dict.Func, [arg]) - let Broken = function(dict.Func, [arg], dict) -< - When using the Funcref the function will be found by {name}, - also when it was redefined later. Use |funcref()| to keep the - same function. - - When {arglist} or {dict} is present this creates a partial. - That means the argument list and/or the dictionary is stored in - the Funcref and will be used when the Funcref is called. - - The arguments are passed to the function in front of other - arguments, but after any argument from |method|. Example: > - func Callback(arg1, arg2, name) - ... - let Partial = function('Callback', ['one', 'two']) - ... - call Partial('name') -< Invokes the function as with: > - call Callback('one', 'two', 'name') - -< The Dictionary is only useful when calling a "dict" function. - In that case the {dict} is passed in as "self". Example: > - function Callback() dict - echo "called for " . self.name - endfunction - ... - let context = {"name": "example"} - let Func = function('Callback', context) - ... - call Func() " will echo: called for example - -< The argument list and the Dictionary can be combined: > - function Callback(arg1, count) dict - ... - let context = {"name": "example"} - let Func = function('Callback', ['one'], context) - ... - call Func(500) -< Invokes the function as with: > - call context.Callback('one', 500) -< - Can also be used as a |method|: > - GetFuncname()->function([arg]) - -garbagecollect([{atexit}]) *garbagecollect()* - Cleanup unused |Lists| and |Dictionaries| that have circular - references. - - There is hardly ever a need to invoke this function, as it is - automatically done when Vim runs out of memory or is waiting - for the user to press a key after 'updatetime'. Items without - circular references are always freed when they become unused. - This is useful if you have deleted a very big |List| and/or - |Dictionary| with circular references in a script that runs - for a long time. - - When the optional {atexit} argument is one, garbage - collection will also be done when exiting Vim, if it wasn't - done before. This is useful when checking for memory leaks. - - The garbage collection is not done immediately but only when - it's safe to perform. This is when waiting for the user to - type a character. - -get({list}, {idx} [, {default}]) *get()* - Get item {idx} from |List| {list}. When this item is not - available return {default}. Return zero when {default} is - omitted. - Can also be used as a |method|: > - mylist->get(idx) -get({blob}, {idx} [, {default}]) - Get byte {idx} from |Blob| {blob}. When this byte is not - available return {default}. Return -1 when {default} is - omitted. -get({dict}, {key} [, {default}]) - Get item with key {key} from |Dictionary| {dict}. When this - item is not available return {default}. Return zero when - {default} is omitted. Useful example: > - let val = get(g:, 'var_name', 'default') -< This gets the value of g:var_name if it exists, and uses - 'default' when it does not exist. -get({func}, {what}) - Get item {what} from Funcref {func}. Possible values for - {what} are: - "name" The function name - "func" The function - "dict" The dictionary - "args" The list with arguments - - *getbufinfo()* -getbufinfo([{buf}]) -getbufinfo([{dict}]) - Get information about buffers as a List of Dictionaries. - - Without an argument information about all the buffers is - returned. - - When the argument is a |Dictionary| only the buffers matching - the specified criteria are returned. The following keys can - be specified in {dict}: - buflisted include only listed buffers. - bufloaded include only loaded buffers. - bufmodified include only modified buffers. - - Otherwise, {buf} specifies a particular buffer to return - information for. For the use of {buf}, see |bufname()| - above. If the buffer is found the returned List has one item. - Otherwise the result is an empty list. - - Each returned List item is a dictionary with the following - entries: - bufnr Buffer number. - changed TRUE if the buffer is modified. - changedtick Number of changes made to the buffer. - hidden TRUE if the buffer is hidden. - lastused Timestamp in seconds, like - |localtime()|, when the buffer was - last used. - listed TRUE if the buffer is listed. - lnum Line number used for the buffer when - opened in the current window. - Only valid if the buffer has been - displayed in the window in the past. - If you want the line number of the - last known cursor position in a given - window, use |line()|: > - :echo line('.', {winid}) -< - linecount Number of lines in the buffer (only - valid when loaded) - loaded TRUE if the buffer is loaded. - name Full path to the file in the buffer. - signs List of signs placed in the buffer. - Each list item is a dictionary with - the following fields: - id sign identifier - lnum line number - name sign name - variables A reference to the dictionary with - buffer-local variables. - windows List of |window-ID|s that display this - buffer - - Examples: > - for buf in getbufinfo() - echo buf.name - endfor - for buf in getbufinfo({'buflisted':1}) - if buf.changed - .... - endif - endfor -< - To get buffer-local options use: > - getbufvar({bufnr}, '&option_name') -< - Can also be used as a |method|: > - GetBufnr()->getbufinfo() -< - *getbufline()* -getbufline({buf}, {lnum} [, {end}]) - Return a |List| with the lines starting from {lnum} to {end} - (inclusive) in the buffer {buf}. If {end} is omitted, a - |List| with only the line {lnum} is returned. - - For the use of {buf}, see |bufname()| above. - - For {lnum} and {end} "$" can be used for the last line of the - buffer. Otherwise a number must be used. - - When {lnum} is smaller than 1 or bigger than the number of - lines in the buffer, an empty |List| is returned. - - When {end} is greater than the number of lines in the buffer, - it is treated as {end} is set to the number of lines in the - buffer. When {end} is before {lnum} an empty |List| is - returned. - - This function works only for loaded buffers. For unloaded and - non-existing buffers, an empty |List| is returned. - - Example: > - :let lines = getbufline(bufnr("myfile"), 1, "$") - -< Can also be used as a |method|: > - GetBufnr()->getbufline(lnum) - -getbufvar({buf}, {varname} [, {def}]) *getbufvar()* - The result is the value of option or local buffer variable - {varname} in buffer {buf}. Note that the name without "b:" - must be used. - The {varname} argument is a string. - When {varname} is empty returns a |Dictionary| with all the - buffer-local variables. - When {varname} is equal to "&" returns a |Dictionary| with all - the buffer-local options. - Otherwise, when {varname} starts with "&" returns the value of - a buffer-local option. - This also works for a global or buffer-local option, but it - doesn't work for a global variable, window-local variable or - window-local option. - For the use of {buf}, see |bufname()| above. - When the buffer or variable doesn't exist {def} or an empty - string is returned, there is no error message. - Examples: > - :let bufmodified = getbufvar(1, "&mod") - :echo "todo myvar = " . getbufvar("todo", "myvar") - -< Can also be used as a |method|: > - GetBufnr()->getbufvar(varname) -< -getchangelist([{buf}]) *getchangelist()* - Returns the |changelist| for the buffer {buf}. For the use - of {buf}, see |bufname()| above. If buffer {buf} doesn't - exist, an empty list is returned. - - The returned list contains two entries: a list with the change - locations and the current position in the list. Each - entry in the change list is a dictionary with the following - entries: - col column number - coladd column offset for 'virtualedit' - lnum line number - If buffer {buf} is the current buffer, then the current - position refers to the position in the list. For other - buffers, it is set to the length of the list. - - Can also be used as a |method|: > - GetBufnr()->getchangelist() - -getchar([expr]) *getchar()* - Get a single character from the user or input stream. - If [expr] is omitted, wait until a character is available. - If [expr] is 0, only get a character when one is available. - Return zero otherwise. - If [expr] is 1, only check if a character is available, it is - not consumed. Return zero if no character available. - If you prefer always getting a string use |getcharstr()|. - - Without [expr] and when [expr] is 0 a whole character or - special key is returned. If it is a single character, the - result is a number. Use nr2char() to convert it to a String. - Otherwise a String is returned with the encoded character. - For a special key it's a String with a sequence of bytes - starting with 0x80 (decimal: 128). This is the same value as - the String "\<Key>", e.g., "\<Left>". The returned value is - also a String when a modifier (shift, control, alt) was used - that is not included in the character. - - When [expr] is 0 and Esc is typed, there will be a short delay - while Vim waits to see if this is the start of an escape - sequence. - - When [expr] is 1 only the first byte is returned. For a - one-byte character it is the character itself as a number. - Use nr2char() to convert it to a String. - - Use getcharmod() to obtain any additional modifiers. - - When the user clicks a mouse button, the mouse event will be - returned. The position can then be found in |v:mouse_col|, - |v:mouse_lnum|, |v:mouse_winid| and |v:mouse_win|. - |getmousepos()| can also be used. Mouse move events will be - ignored. - This example positions the mouse as it would normally happen: > - let c = getchar() - if c == "\<LeftMouse>" && v:mouse_win > 0 - exe v:mouse_win . "wincmd w" - exe v:mouse_lnum - exe "normal " . v:mouse_col . "|" - endif -< - There is no prompt, you will somehow have to make clear to the - user that a character has to be typed. The screen is not - redrawn, e.g. when resizing the window. - - There is no mapping for the character. - Key codes are replaced, thus when the user presses the <Del> - key you get the code for the <Del> key, not the raw character - sequence. Examples: > - getchar() == "\<Del>" - getchar() == "\<S-Left>" -< This example redefines "f" to ignore case: > - :nmap f :call FindChar()<CR> - :function FindChar() - : let c = nr2char(getchar()) - : while col('.') < col('$') - 1 - : normal l - : if getline('.')[col('.') - 1] ==? c - : break - : endif - : endwhile - :endfunction -< -getcharmod() *getcharmod()* - The result is a Number which is the state of the modifiers for - the last obtained character with getchar() or in another way. - These values are added together: - 2 shift - 4 control - 8 alt (meta) - 16 meta (when it's different from ALT) - 32 mouse double click - 64 mouse triple click - 96 mouse quadruple click (== 32 + 64) - 128 command (Macintosh only) - Only the modifiers that have not been included in the - character itself are obtained. Thus Shift-a results in "A" - without a modifier. - -getcharsearch() *getcharsearch()* - Return the current character search information as a {dict} - with the following entries: - - char character previously used for a character - search (|t|, |f|, |T|, or |F|); empty string - if no character search has been performed - forward direction of character search; 1 for forward, - 0 for backward - until type of character search; 1 for a |t| or |T| - character search, 0 for an |f| or |F| - character search - - This can be useful to always have |;| and |,| search - forward/backward regardless of the direction of the previous - character search: > - :nnoremap <expr> ; getcharsearch().forward ? ';' : ',' - :nnoremap <expr> , getcharsearch().forward ? ',' : ';' -< Also see |setcharsearch()|. - - -getcharstr([expr]) *getcharstr()* - Get a single character from the user or input stream as a - string. - If [expr] is omitted, wait until a character is available. - If [expr] is 0 or false, only get a character when one is - available. Return an empty string otherwise. - If [expr] is 1 or true, only check if a character is - available, it is not consumed. Return an empty string - if no character is available. - Otherwise this works like |getchar()|, except that a number - result is converted to a string. - - -getcmdline() *getcmdline()* - Return the current command-line. Only works when the command - line is being edited, thus requires use of |c_CTRL-\_e| or - |c_CTRL-R_=|. - Example: > - :cmap <F7> <C-\>eescape(getcmdline(), ' \')<CR> -< Also see |getcmdtype()|, |getcmdpos()| and |setcmdpos()|. - Returns an empty string when entering a password or using - |inputsecret()|. - -getcmdpos() *getcmdpos()* - Return the position of the cursor in the command line as a - byte count. The first column is 1. - Only works when editing the command line, thus requires use of - |c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping. - Returns 0 otherwise. - Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|. - -getcmdtype() *getcmdtype()* - Return the current command-line type. Possible return values - are: - : normal Ex command - > debug mode command |debug-mode| - / forward search command - ? backward search command - @ |input()| command - - |:insert| or |:append| command - = |i_CTRL-R_=| - Only works when editing the command line, thus requires use of - |c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping. - Returns an empty string otherwise. - Also see |getcmdpos()|, |setcmdpos()| and |getcmdline()|. - -getcmdwintype() *getcmdwintype()* - Return the current |command-line-window| type. Possible return - values are the same as |getcmdtype()|. Returns an empty string - when not in the command-line window. - -getcompletion({pat}, {type} [, {filtered}]) *getcompletion()* - Return a list of command-line completion matches. The String - {type} argument specifies what for. The following completion - types are supported: - - arglist file names in argument list - augroup autocmd groups - buffer buffer names - behave :behave suboptions - cmdline |cmdline-completion| result - color color schemes - command Ex command - compiler compilers - cscope |:cscope| suboptions - diff_buffer |:diffget| and |:diffput| completion - dir directory names - environment environment variable names - event autocommand events - expression Vim expression - file file and directory names - file_in_path file and directory names in |'path'| - filetype filetype names |'filetype'| - function function name - help help subjects - highlight highlight groups - history :history suboptions - locale locale names (as output of locale -a) - mapclear buffer argument - mapping mapping name - menu menus - messages |:messages| suboptions - option options - packadd optional package |pack-add| names - shellcmd Shell command - sign |:sign| suboptions - syntax syntax file names |'syntax'| - syntime |:syntime| suboptions - tag tags - tag_listfiles tags, file names - user user names - var user variables - - If {pat} is an empty string, then all the matches are - returned. Otherwise only items matching {pat} are returned. - See |wildcards| for the use of special characters in {pat}. - - If the optional {filtered} flag is set to 1, then 'wildignore' - is applied to filter the results. Otherwise all the matches - are returned. The 'wildignorecase' option always applies. - - If {type} is "cmdline", then the |cmdline-completion| result is - returned. For example, to complete the possible values after - a ":call" command: > - echo getcompletion('call ', 'cmdline') -< - If there are no matches, an empty list is returned. An - invalid value for {type} produces an error. - - Can also be used as a |method|: > - GetPattern()->getcompletion('color') -< - *getcurpos()* -getcurpos() Get the position of the cursor. This is like getpos('.'), but - includes an extra "curswant" in the list: - [0, lnum, col, off, curswant] ~ - The "curswant" number is the preferred column when moving the - cursor vertically. Also see |getpos()|. - The first "bufnum" item is always zero. - - This can be used to save and restore the cursor position: > - let save_cursor = getcurpos() - MoveTheCursorAround - call setpos('.', save_cursor) -< Note that this only works within the window. See - |winrestview()| for restoring more state. - -getcwd([{winnr}[, {tabnr}]]) *getcwd()* - With no arguments, returns the name of the effective - |current-directory|. With {winnr} or {tabnr} the working - directory of that scope is returned, and 'autochdir' is - ignored. - Tabs and windows are identified by their respective numbers, - 0 means current tab or window. Missing tab number implies 0. - Thus the following are equivalent: > - getcwd(0) - getcwd(0, 0) -< If {winnr} is -1 it is ignored, only the tab is resolved. - {winnr} can be the window number or the |window-ID|. - If both {winnr} and {tabnr} are -1 the global working - directory is returned. - Throw error if the arguments are invalid. |E5000| |E5001| |E5002| - - Can also be used as a |method|: > - GetWinnr()->getcwd() - -getenv({name}) *getenv()* - Return the value of environment variable {name}. The {name} - argument is a string, without a leading '$'. Example: > - myHome = getenv('HOME') - -< When the variable does not exist |v:null| is returned. That - is different from a variable set to an empty string. - See also |expr-env|. - - Can also be used as a |method|: > - GetVarname()->getenv() - -getfontname([{name}]) *getfontname()* - Without an argument returns the name of the normal font being - used. Like what is used for the Normal highlight group - |hl-Normal|. - With an argument a check is done whether String {name} is a - valid font name. If not then an empty string is returned. - Otherwise the actual font name is returned, or {name} if the - GUI does not support obtaining the real name. - Only works when the GUI is running, thus not in your vimrc or - gvimrc file. Use the |GUIEnter| autocommand to use this - function just after the GUI has started. - -getfperm({fname}) *getfperm()* - The result is a String, which is the read, write, and execute - permissions of the given file {fname}. - If {fname} does not exist or its directory cannot be read, an - empty string is returned. - The result is of the form "rwxrwxrwx", where each group of - "rwx" flags represent, in turn, the permissions of the owner - of the file, the group the file belongs to, and other users. - If a user does not have a given permission the flag for this - is replaced with the string "-". Examples: > - :echo getfperm("/etc/passwd") - :echo getfperm(expand("~/.config/nvim/init.vim")) -< This will hopefully (from a security point of view) display - the string "rw-r--r--" or even "rw-------". - - Can also be used as a |method|: > - GetFilename()->getfperm() -< - For setting permissions use |setfperm()|. - -getfsize({fname}) *getfsize()* - The result is a Number, which is the size in bytes of the - given file {fname}. - If {fname} is a directory, 0 is returned. - If the file {fname} can't be found, -1 is returned. - If the size of {fname} is too big to fit in a Number then -2 - is returned. - - Can also be used as a |method|: > - GetFilename()->getfsize() - -getftime({fname}) *getftime()* - The result is a Number, which is the last modification time of - the given file {fname}. The value is measured as seconds - since 1st Jan 1970, and may be passed to strftime(). See also - |localtime()| and |strftime()|. - If the file {fname} can't be found -1 is returned. - - Can also be used as a |method|: > - GetFilename()->getftime() - -getftype({fname}) *getftype()* - The result is a String, which is a description of the kind of - file of the given file {fname}. - If {fname} does not exist an empty string is returned. - Here is a table over different kinds of files and their - results: - Normal file "file" - Directory "dir" - Symbolic link "link" - Block device "bdev" - Character device "cdev" - Socket "socket" - FIFO "fifo" - All other "other" - Example: > - getftype("/home") -< Note that a type such as "link" will only be returned on - systems that support it. On some systems only "dir" and - "file" are returned. - - Can also be used as a |method|: > - GetFilename()->getftype() - -getjumplist([{winnr} [, {tabnr}]]) *getjumplist()* - Returns the |jumplist| for the specified window. - - Without arguments use the current window. - With {winnr} only use this window in the current tab page. - {winnr} can also be a |window-ID|. - With {winnr} and {tabnr} use the window in the specified tab - page. - - The returned list contains two entries: a list with the jump - locations and the last used jump position number in the list. - Each entry in the jump location list is a dictionary with - the following entries: - bufnr buffer number - col column number - coladd column offset for 'virtualedit' - filename filename if available - lnum line number - - Can also be used as a |method|: > - GetWinnr()->getjumplist() - -< *getline()* -getline({lnum} [, {end}]) - Without {end} the result is a String, which is line {lnum} - from the current buffer. Example: > - getline(1) -< When {lnum} is a String that doesn't start with a - digit, |line()| is called to translate the String into a Number. - To get the line under the cursor: > - getline(".") -< When {lnum} is a number smaller than 1 or bigger than the - number of lines in the buffer, an empty string is returned. - - When {end} is given the result is a |List| where each item is - a line from the current buffer in the range {lnum} to {end}, - including line {end}. - {end} is used in the same way as {lnum}. - Non-existing lines are silently omitted. - When {end} is before {lnum} an empty |List| is returned. - Example: > - :let start = line('.') - :let end = search("^$") - 1 - :let lines = getline(start, end) - -< Can also be used as a |method|: > - ComputeLnum()->getline() - -< To get lines from another buffer see |getbufline()| - -getloclist({nr},[, {what}]) *getloclist()* - Returns a |List| with all the entries in the location list for - window {nr}. {nr} can be the window number or the |window-ID|. - When {nr} is zero the current window is used. - - For a location list window, the displayed location list is - returned. For an invalid window number {nr}, an empty list is - returned. Otherwise, same as |getqflist()|. - - If the optional {what} dictionary argument is supplied, then - returns the items listed in {what} as a dictionary. Refer to - |getqflist()| for the supported items in {what}. - If {what} contains 'filewinid', then returns the id of the - window used to display files from the location list. This - field is applicable only when called from a location list - window. See |location-list-file-window| for more details. - - Returns a |Dictionary| with default values if there is no - location list for the window {nr}. - Returns an empty Dictionary if window {nr} does not exist. - - Examples (See also |getqflist-examples|): > - :echo getloclist(3, {'all': 0}) - :echo getloclist(5, {'filewinid': 0}) - - -getmarklist([{buf}]) *getmarklist()* - Without the {buf} argument returns a |List| with information - about all the global marks. |mark| - - If the optional {buf} argument is specified, returns the - local marks defined in buffer {buf}. For the use of {buf}, - see |bufname()|. - - Each item in the returned List is a |Dict| with the following: - mark name of the mark prefixed by "'" - pos a |List| with the position of the mark: - [bufnum, lnum, col, off] - Refer to |getpos()| for more information. - file file name - - Refer to |getpos()| for getting information about a specific - mark. - - Can also be used as a |method|: > - GetBufnr()->getmarklist() - -getmatches([{win}]) *getmatches()* - Returns a |List| with all matches previously defined for the - current window by |matchadd()| and the |:match| commands. - |getmatches()| is useful in combination with |setmatches()|, - as |setmatches()| can restore a list of matches saved by - |getmatches()|. - If {win} is specified, use the window with this number or - window ID instead of the current window. - Example: > - :echo getmatches() -< [{'group': 'MyGroup1', 'pattern': 'TODO', - 'priority': 10, 'id': 1}, {'group': 'MyGroup2', - 'pattern': 'FIXME', 'priority': 10, 'id': 2}] > - :let m = getmatches() - :call clearmatches() - :echo getmatches() -< [] > - :call setmatches(m) - :echo getmatches() -< [{'group': 'MyGroup1', 'pattern': 'TODO', - 'priority': 10, 'id': 1}, {'group': 'MyGroup2', - 'pattern': 'FIXME', 'priority': 10, 'id': 2}] > - :unlet m -< -getmousepos() *getmousepos()* - Returns a Dictionary with the last known position of the - mouse. This can be used in a mapping for a mouse click. The - items are: - screenrow screen row - screencol screen column - winid Window ID of the click - winrow row inside "winid" - wincol column inside "winid" - line text line inside "winid" - column text column inside "winid" - All numbers are 1-based. - - If not over a window, e.g. when in the command line, then only - "screenrow" and "screencol" are valid, the others are zero. - - When on the status line below a window or the vertical - separater right of a window, the "line" and "column" values - are zero. - - When the position is after the text then "column" is the - length of the text in bytes plus one. - - If the mouse is over a focusable floating window then that - window is used. - - When using |getchar()| the Vim variables |v:mouse_lnum|, - |v:mouse_col| and |v:mouse_winid| also provide these values. - - *getpid()* -getpid() Return a Number which is the process ID of the Vim process. - This is a unique number, until Vim exits. - - *getpos()* -getpos({expr}) Get the position for String {expr}. For possible values of - {expr} see |line()|. For getting the cursor position see - |getcurpos()|. - The result is a |List| with four numbers: - [bufnum, lnum, col, off] - "bufnum" is zero, unless a mark like '0 or 'A is used, then it - is the buffer number of the mark. - "lnum" and "col" are the position in the buffer. The first - column is 1. - The "off" number is zero, unless 'virtualedit' is used. Then - it is the offset in screen columns from the start of the - character. E.g., a position within a <Tab> or after the last - character. - Note that for '< and '> Visual mode matters: when it is "V" - (visual line mode) the column of '< is zero and the column of - '> is a large number. - The column number in the returned List is the byte position - within the line. - The column number can be very large, e.g. 2147483647, in which - case it means "after the end of the line". - This can be used to save and restore the position of a mark: > - let save_a_mark = getpos("'a") - ... - call setpos("'a", save_a_mark) -< Also see |getcurpos()| and |setpos()|. - - Can also be used as a |method|: > - GetMark()->getpos() - -getqflist([{what}]) *getqflist()* - Returns a |List| with all the current quickfix errors. Each - list item is a dictionary with these entries: - bufnr number of buffer that has the file name, use - bufname() to get the name - module module name - lnum line number in the buffer (first line is 1) - end_lnum - end of line number if the item is multiline - col column number (first column is 1) - end_col end of column number if the item has range - vcol |TRUE|: "col" is visual column - |FALSE|: "col" is byte index - nr error number - pattern search pattern used to locate the error - text description of the error - type type of the error, 'E', '1', etc. - valid |TRUE|: recognized error message - - When there is no error list or it's empty, an empty list is - returned. Quickfix list entries with a non-existing buffer - number are returned with "bufnr" set to zero (Note: some - functions accept buffer number zero for the alternate buffer, - you may need to explicitly check for zero). - - Useful application: Find pattern matches in multiple files and - do something with them: > - :vimgrep /theword/jg *.c - :for d in getqflist() - : echo bufname(d.bufnr) ':' d.lnum '=' d.text - :endfor -< - If the optional {what} dictionary argument is supplied, then - returns only the items listed in {what} as a dictionary. The - following string items are supported in {what}: - changedtick get the total number of changes made - to the list |quickfix-changedtick| - context get the |quickfix-context| - efm errorformat to use when parsing "lines". If - not present, then the 'errorformat' option - value is used. - id get information for the quickfix list with - |quickfix-ID|; zero means the id for the - current list or the list specified by "nr" - idx get information for the quickfix entry at this - index in the list specified by 'id' or 'nr'. - If set to zero, then uses the current entry. - See |quickfix-index| - items quickfix list entries - lines parse a list of lines using 'efm' and return - the resulting entries. Only a |List| type is - accepted. The current quickfix list is not - modified. See |quickfix-parse|. - nr get information for this quickfix list; zero - means the current quickfix list and "$" means - the last quickfix list - size number of entries in the quickfix list - title get the list title |quickfix-title| - winid get the quickfix |window-ID| - all all of the above quickfix properties - Non-string items in {what} are ignored. To get the value of a - particular item, set it to zero. - If "nr" is not present then the current quickfix list is used. - If both "nr" and a non-zero "id" are specified, then the list - specified by "id" is used. - To get the number of lists in the quickfix stack, set "nr" to - "$" in {what}. The "nr" value in the returned dictionary - contains the quickfix stack size. - When "lines" is specified, all the other items except "efm" - are ignored. The returned dictionary contains the entry - "items" with the list of entries. - - The returned dictionary contains the following entries: - changedtick total number of changes made to the - list |quickfix-changedtick| - context quickfix list context. See |quickfix-context| - If not present, set to "". - id quickfix list ID |quickfix-ID|. If not - present, set to 0. - idx index of the quickfix entry in the list. If not - present, set to 0. - items quickfix list entries. If not present, set to - an empty list. - nr quickfix list number. If not present, set to 0 - size number of entries in the quickfix list. If not - present, set to 0. - title quickfix list title text. If not present, set - to "". - winid quickfix |window-ID|. If not present, set to 0 - - Examples (See also |getqflist-examples|): > - :echo getqflist({'all': 1}) - :echo getqflist({'nr': 2, 'title': 1}) - :echo getqflist({'lines' : ["F1:10:L10"]}) -< -getreg([{regname} [, 1 [, {list}]]]) *getreg()* - The result is a String, which is the contents of register - {regname}. Example: > - :let cliptext = getreg('*') -< When register {regname} was not set the result is an empty - string. - The {regname} argument must be a string. - - getreg('=') returns the last evaluated value of the expression - register. (For use in maps.) - getreg('=', 1) returns the expression itself, so that it can - be restored with |setreg()|. For other registers the extra - argument is ignored, thus you can always give it. - - If {list} is present and |TRUE|, the result type is changed - to |List|. Each list item is one text line. Use it if you care - about zero bytes possibly present inside register: without - third argument both NLs and zero bytes are represented as NLs - (see |NL-used-for-Nul|). - When the register was not set an empty list is returned. - - If {regname} is not specified, |v:register| is used. - - Can also be used as a |method|: > - GetRegname()->getreg() - -getreginfo([{regname}]) *getreginfo()* - Returns detailed information about register {regname} as a - Dictionary with the following entries: - regcontents List of lines contained in register - {regname}, like - |getreg|({regname}, 1, 1). - regtype the type of register {regname}, as in - |getregtype()|. - isunnamed Boolean flag, v:true if this register - is currently pointed to by the unnamed - register. - points_to for the unnamed register, gives the - single letter name of the register - currently pointed to (see |quotequote|). - For example, after deleting a line - with `dd`, this field will be "1", - which is the register that got the - deleted text. - - The {regname} argument is a string. If {regname} is invalid - or not set, an empty Dictionary will be returned. - If {regname} is not specified, |v:register| is used. - The returned Dictionary can be passed to |setreg()|. - - Can also be used as a |method|: > - GetRegname()->getreginfo() - -getregtype([{regname}]) *getregtype()* - The result is a String, which is type of register {regname}. - The value will be one of: - "v" for |charwise| text - "V" for |linewise| text - "<CTRL-V>{width}" for |blockwise-visual| text - "" for an empty or unknown register - <CTRL-V> is one character with value 0x16. - The {regname} argument is a string. If {regname} is not - specified, |v:register| is used. - - Can also be used as a |method|: > - GetRegname()->getregtype() - -gettabinfo([{tabnr}]) *gettabinfo()* - If {tabnr} is not specified, then information about all the - tab pages is returned as a |List|. Each List item is a - |Dictionary|. Otherwise, {tabnr} specifies the tab page - number and information about that one is returned. If the tab - page does not exist an empty List is returned. - - Each List item is a |Dictionary| with the following entries: - tabnr tab page number. - variables a reference to the dictionary with - tabpage-local variables - windows List of |window-ID|s in the tab page. - - Can also be used as a |method|: > - GetTabnr()->gettabinfo() - -gettabvar({tabnr}, {varname} [, {def}]) *gettabvar()* - Get the value of a tab-local variable {varname} in tab page - {tabnr}. |t:var| - Tabs are numbered starting with one. - The {varname} argument is a string. When {varname} is empty a - dictionary with all tab-local variables is returned. - Note that the name without "t:" must be used. - When the tab or variable doesn't exist {def} or an empty - string is returned, there is no error message. - - Can also be used as a |method|: > - GetTabnr()->gettabvar(varname) - -gettabwinvar({tabnr}, {winnr}, {varname} [, {def}]) *gettabwinvar()* - Get the value of window-local variable {varname} in window - {winnr} in tab page {tabnr}. - The {varname} argument is a string. When {varname} is empty a - dictionary with all window-local variables is returned. - When {varname} is equal to "&" get the values of all - window-local options in a |Dictionary|. - Otherwise, when {varname} starts with "&" get the value of a - window-local option. - Note that {varname} must be the name without "w:". - Tabs are numbered starting with one. For the current tabpage - use |getwinvar()|. - {winnr} can be the window number or the |window-ID|. - When {winnr} is zero the current window is used. - This also works for a global option, buffer-local option and - window-local option, but it doesn't work for a global variable - or buffer-local variable. - When the tab, window or variable doesn't exist {def} or an - empty string is returned, there is no error message. - Examples: > - :let list_is_on = gettabwinvar(1, 2, '&list') - :echo "myvar = " . gettabwinvar(3, 1, 'myvar') -< - To obtain all window-local variables use: > - gettabwinvar({tabnr}, {winnr}, '&') - -< Can also be used as a |method|: > - GetTabnr()->gettabwinvar(winnr, varname) - -gettagstack([{winnr}]) *gettagstack()* - The result is a Dict, which is the tag stack of window {winnr}. - {winnr} can be the window number or the |window-ID|. - When {winnr} is not specified, the current window is used. - When window {winnr} doesn't exist, an empty Dict is returned. - - The returned dictionary contains the following entries: - curidx Current index in the stack. When at - top of the stack, set to (length + 1). - Index of bottom of the stack is 1. - items List of items in the stack. Each item - is a dictionary containing the - entries described below. - length Number of entries in the stack. - - Each item in the stack is a dictionary with the following - entries: - bufnr buffer number of the current jump - from cursor position before the tag jump. - See |getpos()| for the format of the - returned list. - matchnr current matching tag number. Used when - multiple matching tags are found for a - name. - tagname name of the tag - - See |tagstack| for more information about the tag stack. - - Can also be used as a |method|: > - GetWinnr()->gettagstack() - -getwininfo([{winid}]) *getwininfo()* - Returns information about windows as a |List| with Dictionaries. - - If {winid} is given Information about the window with that ID - is returned, as a |List| with one item. If the window does not - exist the result is an empty list. - - Without {winid} information about all the windows in all the - tab pages is returned. - - Each List item is a |Dictionary| with the following entries: - botline last complete displayed buffer line - bufnr number of buffer in the window - height window height (excluding winbar) - loclist 1 if showing a location list - quickfix 1 if quickfix or location list window - terminal 1 if a terminal window - tabnr tab page number - topline first displayed buffer line - variables a reference to the dictionary with - window-local variables - width window width - winbar 1 if the window has a toolbar, 0 - otherwise - wincol leftmost screen column of the window; - "col" from |win_screenpos()| - textoff number of columns occupied by any - 'foldcolumn', 'signcolumn' and line - number in front of the text - winid |window-ID| - winnr window number - winrow topmost screen line of the window; - "row" from |win_screenpos()| - - Can also be used as a |method|: > - GetWinnr()->getwininfo() - -getwinpos([{timeout}]) *getwinpos()* - The result is a |List| with two numbers, the result of - |getwinposx()| and |getwinposy()| combined: - [x-pos, y-pos] - {timeout} can be used to specify how long to wait in msec for - a response from the terminal. When omitted 100 msec is used. - - Use a longer time for a remote terminal. - When using a value less than 10 and no response is received - within that time, a previously reported position is returned, - if available. This can be used to poll for the position and - do some work in the meantime: > - while 1 - let res = getwinpos(1) - if res[0] >= 0 - break - endif - " Do some work here - endwhile -< - Can also be used as a |method|: > - GetTimeout()->getwinpos() -< - *getwinposx()* -getwinposx() The result is a Number, which is the X coordinate in pixels of - the left hand side of the GUI Vim window. The result will be - -1 if the information is not available. - The value can be used with `:winpos`. - - *getwinposy()* -getwinposy() The result is a Number, which is the Y coordinate in pixels of - the top of the GUI Vim window. The result will be -1 if the - information is not available. - The value can be used with `:winpos`. - -getwinvar({winnr}, {varname} [, {def}]) *getwinvar()* - Like |gettabwinvar()| for the current tabpage. - Examples: > - :let list_is_on = getwinvar(2, '&list') - :echo "myvar = " . getwinvar(1, 'myvar') - -< Can also be used as a |method|: > - GetWinnr()->getwinvar(varname) -< -glob({expr} [, {nosuf} [, {list} [, {alllinks}]]]) *glob()* - Expand the file wildcards in {expr}. See |wildcards| for the - use of special characters. - - Unless the optional {nosuf} argument is given and is |TRUE|, - the 'suffixes' and 'wildignore' options apply: Names matching - one of the patterns in 'wildignore' will be skipped and - 'suffixes' affect the ordering of matches. - 'wildignorecase' always applies. - - When {list} is present and it is |TRUE| the result is a |List| - with all matching files. The advantage of using a List is, - you also get filenames containing newlines correctly. - Otherwise the result is a String and when there are several - matches, they are separated by <NL> characters. - - If the expansion fails, the result is an empty String or List. - - You can also use |readdir()| if you need to do complicated - things, such as limiting the number of matches. - - A name for a non-existing file is not included. A symbolic - link is only included if it points to an existing file. - However, when the {alllinks} argument is present and it is - |TRUE| then all symbolic links are included. - - For most systems backticks can be used to get files names from - any external command. Example: > - :let tagfiles = glob("`find . -name tags -print`") - :let &tags = substitute(tagfiles, "\n", ",", "g") -< The result of the program inside the backticks should be one - item per line. Spaces inside an item are allowed. - - See |expand()| for expanding special Vim variables. See - |system()| for getting the raw output of an external command. - - Can also be used as a |method|: > - GetExpr()->glob() - -glob2regpat({string}) *glob2regpat()* - Convert a file pattern, as used by glob(), into a search - pattern. The result can be used to match with a string that - is a file name. E.g. > - if filename =~ glob2regpat('Make*.mak') -< This is equivalent to: > - if filename =~ '^Make.*\.mak$' -< When {string} is an empty string the result is "^$", match an - empty string. - Note that the result depends on the system. On MS-Windows - a backslash usually means a path separator. - - Can also be used as a |method|: > - GetExpr()->glob2regpat() -< *globpath()* -globpath({path}, {expr} [, {nosuf} [, {list} [, {allinks}]]]) - Perform glob() for String {expr} on all directories in {path} - and concatenate the results. Example: > - :echo globpath(&rtp, "syntax/c.vim") -< - {path} is a comma-separated list of directory names. Each - directory name is prepended to {expr} and expanded like with - |glob()|. A path separator is inserted when needed. - To add a comma inside a directory name escape it with a - backslash. Note that on MS-Windows a directory may have a - trailing backslash, remove it if you put a comma after it. - If the expansion fails for one of the directories, there is no - error message. - - Unless the optional {nosuf} argument is given and is |TRUE|, - the 'suffixes' and 'wildignore' options apply: Names matching - one of the patterns in 'wildignore' will be skipped and - 'suffixes' affect the ordering of matches. - - When {list} is present and it is |TRUE| the result is a |List| - with all matching files. The advantage of using a List is, you - also get filenames containing newlines correctly. Otherwise - the result is a String and when there are several matches, - they are separated by <NL> characters. Example: > - :echo globpath(&rtp, "syntax/c.vim", 0, 1) -< - {allinks} is used as with |glob()|. - - The "**" item can be used to search in a directory tree. - For example, to find all "README.txt" files in the directories - in 'runtimepath' and below: > - :echo globpath(&rtp, "**/README.txt") -< Upwards search and limiting the depth of "**" is not - supported, thus using 'path' will not always work properly. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetExpr()->globpath(&rtp) -< - *has()* -has({feature}) Returns 1 if {feature} is supported, 0 otherwise. The - {feature} argument is a feature name like "nvim-0.2.1" or - "win32", see below. See also |exists()|. - - If the code has a syntax error, then Nvim may skip the rest - of the line and miss |:endif|. > - if has('feature') | let x = this->breaks->without->the->feature | endif -< - Put |:if| and |:endif| on separate lines to avoid the - syntax error. > - if has('feature') - let x = this->breaks->without->the->feature - endif -< - Vim's compile-time feature-names (prefixed with "+") are not - recognized because Nvim is always compiled with all possible - features. |feature-compile| - - Feature names can be: - 1. Nvim version. For example the "nvim-0.2.1" feature means - that Nvim is version 0.2.1 or later: > - :if has("nvim-0.2.1") - -< 2. Runtime condition or other pseudo-feature. For example the - "win32" feature checks if the current system is Windows: > - :if has("win32") -< *feature-list* - List of supported pseudo-feature names: - acl |ACL| support - bsd BSD system (not macOS, use "mac" for that). - iconv Can use |iconv()| for conversion. - +shellslash Can use backslashes in filenames (Windows) - clipboard |clipboard| provider is available. - fname_case Case in file names matters (for Darwin and MS-Windows - this is not present). - mac MacOS system. - nvim This is Nvim. - python2 Legacy Vim |python2| interface. |has-python| - python3 Legacy Vim |python3| interface. |has-python| - pythonx Legacy Vim |python_x| interface. |has-pythonx| - ttyin input is a terminal (tty) - ttyout output is a terminal (tty) - unix Unix system. - *vim_starting* True during |startup|. - win32 Windows system (32 or 64 bit). - win64 Windows system (64 bit). - wsl WSL (Windows Subsystem for Linux) system - - *has-patch* - 3. Vim patch. For example the "patch123" feature means that - Vim patch 123 at the current |v:version| was included: > - :if v:version > 602 || v:version == 602 && has("patch148") - -< 4. Vim version. For example the "patch-7.4.237" feature means - that Nvim is Vim-compatible to version 7.4.237 or later. > - :if has("patch-7.4.237") - - -has_key({dict}, {key}) *has_key()* - The result is a Number, which is TRUE if |Dictionary| {dict} - has an entry with key {key}. FALSE otherwise. The {key} - argument is a string. - - Can also be used as a |method|: > - mydict->has_key(key) - -haslocaldir([{winnr}[, {tabnr}]]) *haslocaldir()* - The result is a Number, which is 1 when the window has set a - local path via |:lcd| or when {winnr} is -1 and the tabpage - has set a local path via |:tcd|, otherwise 0. - - Tabs and windows are identified by their respective numbers, - 0 means current tab or window. Missing argument implies 0. - Thus the following are equivalent: > - haslocaldir() - haslocaldir(0) - haslocaldir(0, 0) -< With {winnr} use that window in the current tabpage. - With {winnr} and {tabnr} use the window in that tabpage. - {winnr} can be the window number or the |window-ID|. - If {winnr} is -1 it is ignored, only the tab is resolved. - Throw error if the arguments are invalid. |E5000| |E5001| |E5002| - - Can also be used as a |method|: > - GetWinnr()->haslocaldir() - -hasmapto({what} [, {mode} [, {abbr}]]) *hasmapto()* - The result is a Number, which is TRUE if there is a mapping - that contains {what} in somewhere in the rhs (what it is - mapped to) and this mapping exists in one of the modes - indicated by {mode}. - The arguments {what} and {mode} are strings. - When {abbr} is there and it is |TRUE| use abbreviations - instead of mappings. Don't forget to specify Insert and/or - Command-line mode. - Both the global mappings and the mappings local to the current - buffer are checked for a match. - If no matching mapping is found FALSE is returned. - The following characters are recognized in {mode}: - n Normal mode - v Visual and Select mode - x Visual mode - s Select mode - o Operator-pending mode - i Insert mode - l Language-Argument ("r", "f", "t", etc.) - c Command-line mode - When {mode} is omitted, "nvo" is used. - - This function is useful to check if a mapping already exists - to a function in a Vim script. Example: > - :if !hasmapto('\ABCdoit') - : map <Leader>d \ABCdoit - :endif -< This installs the mapping to "\ABCdoit" only if there isn't - already a mapping to "\ABCdoit". - - Can also be used as a |method|: > - GetRHS()->hasmapto() - -histadd({history}, {item}) *histadd()* - Add the String {item} to the history {history} which can be - one of: *hist-names* - "cmd" or ":" command line history - "search" or "/" search pattern history - "expr" or "=" typed expression history - "input" or "@" input line history - "debug" or ">" debug command history - empty the current or last used history - The {history} string does not need to be the whole name, one - character is sufficient. - If {item} does already exist in the history, it will be - shifted to become the newest entry. - The result is a Number: TRUE if the operation was successful, - otherwise FALSE is returned. - - Example: > - :call histadd("input", strftime("%Y %b %d")) - :let date=input("Enter date: ") -< This function is not available in the |sandbox|. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetHistory()->histadd('search') - -histdel({history} [, {item}]) *histdel()* - Clear {history}, i.e. delete all its entries. See |hist-names| - for the possible values of {history}. - - If the parameter {item} evaluates to a String, it is used as a - regular expression. All entries matching that expression will - be removed from the history (if there are any). - Upper/lowercase must match, unless "\c" is used |/\c|. - If {item} evaluates to a Number, it will be interpreted as - an index, see |:history-indexing|. The respective entry will - be removed if it exists. - - The result is TRUE for a successful operation, otherwise FALSE - is returned. - - Examples: - Clear expression register history: > - :call histdel("expr") -< - Remove all entries starting with "*" from the search history: > - :call histdel("/", '^\*') -< - The following three are equivalent: > - :call histdel("search", histnr("search")) - :call histdel("search", -1) - :call histdel("search", '^'.histget("search", -1).'$') -< - To delete the last search pattern and use the last-but-one for - the "n" command and 'hlsearch': > - :call histdel("search", -1) - :let @/ = histget("search", -1) -< - Can also be used as a |method|: > - GetHistory()->histdel() - -histget({history} [, {index}]) *histget()* - The result is a String, the entry with Number {index} from - {history}. See |hist-names| for the possible values of - {history}, and |:history-indexing| for {index}. If there is - no such entry, an empty String is returned. When {index} is - omitted, the most recent item from the history is used. - - Examples: - Redo the second last search from history. > - :execute '/' . histget("search", -2) - -< Define an Ex command ":H {num}" that supports re-execution of - the {num}th entry from the output of |:history|. > - :command -nargs=1 H execute histget("cmd", 0+<args>) -< - Can also be used as a |method|: > - GetHistory()->histget() - -histnr({history}) *histnr()* - The result is the Number of the current entry in {history}. - See |hist-names| for the possible values of {history}. - If an error occurred, -1 is returned. - - Example: > - :let inp_index = histnr("expr") - -< Can also be used as a |method|: > - GetHistory()->histnr() -< -hlexists({name}) *hlexists()* - The result is a Number, which is TRUE if a highlight group - called {name} exists. This is when the group has been - defined in some way. Not necessarily when highlighting has - been defined for it, it may also have been used for a syntax - item. - - Can also be used as a |method|: > - GetName()->hlexists() -< - *hlID()* -hlID({name}) The result is a Number, which is the ID of the highlight group - with name {name}. When the highlight group doesn't exist, - zero is returned. - This can be used to retrieve information about the highlight - group. For example, to get the background color of the - "Comment" group: > - :echo synIDattr(synIDtrans(hlID("Comment")), "bg") -< - Can also be used as a |method|: > - GetName()->hlID() - -hostname() *hostname()* - The result is a String, which is the name of the machine on - which Vim is currently running. Machine names greater than - 256 characters long are truncated. - -iconv({string}, {from}, {to}) *iconv()* - The result is a String, which is the text {string} converted - from encoding {from} to encoding {to}. - When the conversion completely fails an empty string is - returned. When some characters could not be converted they - are replaced with "?". - The encoding names are whatever the iconv() library function - can accept, see ":!man 3 iconv". - Note that Vim uses UTF-8 for all Unicode encodings, conversion - from/to UCS-2 is automatically changed to use UTF-8. You - cannot use UCS-2 in a string anyway, because of the NUL bytes. - - Can also be used as a |method|: > - GetText()->iconv('latin1', 'utf-8') -< - *indent()* -indent({lnum}) The result is a Number, which is indent of line {lnum} in the - current buffer. The indent is counted in spaces, the value - of 'tabstop' is relevant. {lnum} is used just like in - |getline()|. - When {lnum} is invalid -1 is returned. - - Can also be used as a |method|: > - GetLnum()->indent() - -index({object}, {expr} [, {start} [, {ic}]]) *index()* - If {object} is a |List| return the lowest index where the item - has a value equal to {expr}. There is no automatic - conversion, so the String "4" is different from the Number 4. - And the Number 4 is different from the Float 4.0. The value - of 'ignorecase' is not used here, case always matters. - - If {object} is a |Blob| return the lowest index where the byte - value is equal to {expr}. - - If {start} is given then start looking at the item with index - {start} (may be negative for an item relative to the end). - When {ic} is given and it is |TRUE|, ignore case. Otherwise - case must match. - -1 is returned when {expr} is not found in {object}. - Example: > - :let idx = index(words, "the") - :if index(numbers, 123) >= 0 - -< Can also be used as a |method|: > - GetObject()->index(what) - -input({prompt} [, {text} [, {completion}]]) *input()* -input({opts}) - The result is a String, which is whatever the user typed on - the command-line. The {prompt} argument is either a prompt - string, or a blank string (for no prompt). A '\n' can be used - in the prompt to start a new line. - - In the second form it accepts a single dictionary with the - following keys, any of which may be omitted: - - Key Default Description ~ - prompt "" Same as {prompt} in the first form. - default "" Same as {text} in the first form. - completion nothing Same as {completion} in the first form. - cancelreturn "" The value returned when the dialog is - cancelled. - highlight nothing Highlight handler: |Funcref|. - - The highlighting set with |:echohl| is used for the prompt. - The input is entered just like a command-line, with the same - editing commands and mappings. There is a separate history - for lines typed for input(). - Example: > - :if input("Coffee or beer? ") == "beer" - : echo "Cheers!" - :endif -< - If the optional {text} argument is present and not empty, this - is used for the default reply, as if the user typed this. - Example: > - :let color = input("Color? ", "white") - -< The optional {completion} argument specifies the type of - completion supported for the input. Without it completion is - not performed. The supported completion types are the same as - that can be supplied to a user-defined command using the - "-complete=" argument. Refer to |:command-completion| for - more information. Example: > - let fname = input("File: ", "", "file") - -< *input()-highlight* *E5400* *E5402* - The optional `highlight` key allows specifying function which - will be used for highlighting user input. This function - receives user input as its only argument and must return - a list of 3-tuples [hl_start_col, hl_end_col + 1, hl_group] - where - hl_start_col is the first highlighted column, - hl_end_col is the last highlighted column (+ 1!), - hl_group is |:hi| group used for highlighting. - *E5403* *E5404* *E5405* *E5406* - Both hl_start_col and hl_end_col + 1 must point to the start - of the multibyte character (highlighting must not break - multibyte characters), hl_end_col + 1 may be equal to the - input length. Start column must be in range [0, len(input)), - end column must be in range (hl_start_col, len(input)], - sections must be ordered so that next hl_start_col is greater - then or equal to previous hl_end_col. - - Example (try some input with parentheses): > - highlight RBP1 guibg=Red ctermbg=red - highlight RBP2 guibg=Yellow ctermbg=yellow - highlight RBP3 guibg=Green ctermbg=green - highlight RBP4 guibg=Blue ctermbg=blue - let g:rainbow_levels = 4 - function! RainbowParens(cmdline) - let ret = [] - let i = 0 - let lvl = 0 - while i < len(a:cmdline) - if a:cmdline[i] is# '(' - call add(ret, [i, i + 1, 'RBP' . ((lvl % g:rainbow_levels) + 1)]) - let lvl += 1 - elseif a:cmdline[i] is# ')' - let lvl -= 1 - call add(ret, [i, i + 1, 'RBP' . ((lvl % g:rainbow_levels) + 1)]) - endif - let i += 1 - endwhile - return ret - endfunction - call input({'prompt':'>','highlight':'RainbowParens'}) -< - Highlight function is called at least once for each new - displayed input string, before command-line is redrawn. It is - expected that function is pure for the duration of one input() - call, i.e. it produces the same output for the same input, so - output may be memoized. Function is run like under |:silent| - modifier. If the function causes any errors, it will be - skipped for the duration of the current input() call. - - Highlighting is disabled if command-line contains arabic - characters. - - NOTE: This function must not be used in a startup file, for - the versions that only run in GUI mode (e.g., the Win32 GUI). - Note: When input() is called from within a mapping it will - consume remaining characters from that mapping, because a - mapping is handled like the characters were typed. - Use |inputsave()| before input() and |inputrestore()| - after input() to avoid that. Another solution is to avoid - that further characters follow in the mapping, e.g., by using - |:execute| or |:normal|. - - Example with a mapping: > - :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR> - :function GetFoo() - : call inputsave() - : let g:Foo = input("enter search pattern: ") - : call inputrestore() - :endfunction - -< Can also be used as a |method|: > - GetPrompt()->input() - -inputlist({textlist}) *inputlist()* - {textlist} must be a |List| of strings. This |List| is - displayed, one string per line. The user will be prompted to - enter a number, which is returned. - The user can also select an item by clicking on it with the - mouse, if the mouse is enabled in the command line ('mouse' is - "a" or includes "c"). For the first string 0 is returned. - When clicking above the first item a negative number is - returned. When clicking on the prompt one more than the - length of {textlist} is returned. - Make sure {textlist} has less than 'lines' entries, otherwise - it won't work. It's a good idea to put the entry number at - the start of the string. And put a prompt in the first item. - Example: > - let color = inputlist(['Select color:', '1. red', - \ '2. green', '3. blue']) - -< Can also be used as a |method|: > - GetChoices()->inputlist() - -inputrestore() *inputrestore()* - Restore typeahead that was saved with a previous |inputsave()|. - Should be called the same number of times inputsave() is - called. Calling it more often is harmless though. - Returns TRUE when there is nothing to restore, FALSE otherwise. - -inputsave() *inputsave()* - Preserve typeahead (also from mappings) and clear it, so that - a following prompt gets input from the user. Should be - followed by a matching inputrestore() after the prompt. Can - be used several times, in which case there must be just as - many inputrestore() calls. - Returns TRUE when out of memory, FALSE otherwise. - -inputsecret({prompt} [, {text}]) *inputsecret()* - This function acts much like the |input()| function with but - two exceptions: - a) the user's response will be displayed as a sequence of - asterisks ("*") thereby keeping the entry secret, and - b) the user's response will not be recorded on the input - |history| stack. - The result is a String, which is whatever the user actually - typed on the command-line in response to the issued prompt. - NOTE: Command-line completion is not supported. - - Can also be used as a |method|: > - GetPrompt()->inputsecret() - -insert({object}, {item} [, {idx}]) *insert()* - When {object} is a |List| or a |Blob| insert {item} at the start - of it. - - If {idx} is specified insert {item} before the item with index - {idx}. If {idx} is zero it goes before the first item, just - like omitting {idx}. A negative {idx} is also possible, see - |list-index|. -1 inserts just before the last item. - - Returns the resulting |List| or |Blob|. Examples: > - :let mylist = insert([2, 3, 5], 1) - :call insert(mylist, 4, -1) - :call insert(mylist, 6, len(mylist)) -< The last example can be done simpler with |add()|. - Note that when {item} is a |List| it is inserted as a single - item. Use |extend()| to concatenate |Lists|. - - Can also be used as a |method|: > - mylist->insert(item) - -interrupt() *interrupt()* - Interrupt script execution. It works more or less like the - user typing CTRL-C, most commands won't execute and control - returns to the user. This is useful to abort execution - from lower down, e.g. in an autocommand. Example: > - :function s:check_typoname(file) - : if fnamemodify(a:file, ':t') == '[' - : echomsg 'Maybe typo' - : call interrupt() - : endif - :endfunction - :au BufWritePre * call s:check_typoname(expand('<amatch>')) - -invert({expr}) *invert()* - Bitwise invert. The argument is converted to a number. A - List, Dict or Float argument causes an error. Example: > - :let bits = invert(bits) -< Can also be used as a |method|: > - :let bits = bits->invert() - -isdirectory({directory}) *isdirectory()* - The result is a Number, which is |TRUE| when a directory - with the name {directory} exists. If {directory} doesn't - exist, or isn't a directory, the result is |FALSE|. {directory} - is any expression, which is used as a String. - - Can also be used as a |method|: > - GetName()->isdirectory() - -isinf({expr}) *isinf()* - Return 1 if {expr} is a positive infinity, or -1 a negative - infinity, otherwise 0. > - :echo isinf(1.0 / 0.0) -< 1 > - :echo isinf(-1.0 / 0.0) -< -1 - - Can also be used as a |method|: > - Compute()->isinf() - -islocked({expr}) *islocked()* *E786* - The result is a Number, which is |TRUE| when {expr} is the - name of a locked variable. - The string argument {expr} must be the name of a variable, - |List| item or |Dictionary| entry, not the variable itself! - Example: > - :let alist = [0, ['a', 'b'], 2, 3] - :lockvar 1 alist - :echo islocked('alist') " 1 - :echo islocked('alist[1]') " 0 - -< When {expr} is a variable that does not exist you get an error - message. Use |exists()| to check for existence. - - Can also be used as a |method|: > - GetName()->islocked() - -id({expr}) *id()* - Returns a |String| which is a unique identifier of the - container type (|List|, |Dict|, |Blob| and |Partial|). It is - guaranteed that for the mentioned types `id(v1) ==# id(v2)` - returns true iff `type(v1) == type(v2) && v1 is v2`. - Note that |v:_null_string|, |v:_null_list|, |v:_null_dict| and - |v:_null_blob| have the same `id()` with different types - because they are internally represented as NULL pointers. - `id()` returns a hexadecimal representanion of the pointers to - the containers (i.e. like `0x994a40`), same as `printf("%p", - {expr})`, but it is advised against counting on the exact - format of the return value. - - It is not guaranteed that `id(no_longer_existing_container)` - will not be equal to some other `id()`: new containers may - reuse identifiers of the garbage-collected ones. - -items({dict}) *items()* - Return a |List| with all the key-value pairs of {dict}. Each - |List| item is a list with two items: the key of a {dict} - entry and the value of this entry. The |List| is in arbitrary - order. Also see |keys()| and |values()|. - Example: > - for [key, value] in items(mydict) - echo key . ': ' . value - endfor - -< Can also be used as a |method|: > - mydict->items() - -isnan({expr}) *isnan()* - Return |TRUE| if {expr} is a float with value NaN. > - echo isnan(0.0 / 0.0) -< 1 - - Can also be used as a |method|: > - Compute()->isnan() - -jobpid({job}) *jobpid()* - Return the PID (process id) of |job-id| {job}. - -jobresize({job}, {width}, {height}) *jobresize()* - Resize the pseudo terminal window of |job-id| {job} to {width} - columns and {height} rows. - Fails if the job was not started with `"pty":v:true`. - -jobstart({cmd}[, {opts}]) *jobstart()* - Spawns {cmd} as a job. - If {cmd} is a List it runs directly (no 'shell'). - If {cmd} is a String it runs in the 'shell', like this: > - :call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}']) -< (See |shell-unquoting| for details.) - - Example: > - :call jobstart('nvim -h', {'on_stdout':{j,d,e->append(line('.'),d)}}) -< - Returns |job-id| on success, 0 on invalid arguments (or job - table is full), -1 if {cmd}[0] or 'shell' is not executable. - The returned job-id is a valid |channel-id| representing the - job's stdio streams. Use |chansend()| (or |rpcnotify()| and - |rpcrequest()| if "rpc" was enabled) to send data to stdin and - |chanclose()| to close the streams without stopping the job. - - See |job-control| and |RPC|. - - NOTE: on Windows if {cmd} is a List: - - cmd[0] must be an executable (not a "built-in"). If it is - in $PATH it can be called by name, without an extension: > - :call jobstart(['ping', 'neovim.io']) -< If it is a full or partial path, extension is required: > - :call jobstart(['System32\ping.exe', 'neovim.io']) -< - {cmd} is collapsed to a string of quoted args as expected - by CommandLineToArgvW https://msdn.microsoft.com/bb776391 - unless cmd[0] is some form of "cmd.exe". - - *jobstart-options* - {opts} is a dictionary with these keys: - clear_env: (boolean) `env` defines the job environment - exactly, instead of merging current environment. - cwd: (string, default=|current-directory|) Working - directory of the job. - detach: (boolean) Detach the job process: it will not be - killed when Nvim exits. If the process exits - before Nvim, `on_exit` will be invoked. - env: (dict) Map of environment variable name:value - pairs extending (or replacing if |clear_env|) - the current environment. - height: (number) Height of the `pty` terminal. - |on_exit|: (function) Callback invoked when the job exits. - |on_stdout|: (function) Callback invoked when the job emits - stdout data. - |on_stderr|: (function) Callback invoked when the job emits - stderr data. - overlapped: (boolean) Set FILE_FLAG_OVERLAPPED for the - standard input/output passed to the child process. - Normally you do not need to set this. - (Only available on MS-Windows, On other - platforms, this option is silently ignored.) - pty: (boolean) Connect the job to a new pseudo - terminal, and its streams to the master file - descriptor. Then `on_stderr` is ignored, - `on_stdout` receives all output. - rpc: (boolean) Use |msgpack-rpc| to communicate with - the job over stdio. Then `on_stdout` is ignored, - but `on_stderr` can still be used. - stderr_buffered: (boolean) Collect data until EOF (stream closed) - before invoking `on_stderr`. |channel-buffered| - stdout_buffered: (boolean) Collect data until EOF (stream - closed) before invoking `on_stdout`. |channel-buffered| - stdin: (string) Either "pipe" (default) to connect the - job's stdin to a channel or "null" to disconnect - stdin. - width: (number) Width of the `pty` terminal. - - {opts} is passed as |self| dictionary to the callback; the - caller may set other keys to pass application-specific data. - - Returns: - - |channel-id| on success - - 0 on invalid arguments - - -1 if {cmd}[0] is not executable. - See also |job-control|, |channel|, |msgpack-rpc|. - -jobstop({id}) *jobstop()* - Stop |job-id| {id} by sending SIGTERM to the job process. If - the process does not terminate after a timeout then SIGKILL - will be sent. When the job terminates its |on_exit| handler - (if any) will be invoked. - See |job-control|. - - Returns 1 for valid job id, 0 for invalid id, including jobs have - exited or stopped. - -jobwait({jobs}[, {timeout}]) *jobwait()* - Waits for jobs and their |on_exit| handlers to complete. - - {jobs} is a List of |job-id|s to wait for. - {timeout} is the maximum waiting time in milliseconds. If - omitted or -1, wait forever. - - Timeout of 0 can be used to check the status of a job: > - let running = jobwait([{job-id}], 0)[0] == -1 -< - During jobwait() callbacks for jobs not in the {jobs} list may - be invoked. The screen will not redraw unless |:redraw| is - invoked by a callback. - - Returns a list of len({jobs}) integers, where each integer is - the status of the corresponding job: - Exit-code, if the job exited - -1 if the timeout was exceeded - -2 if the job was interrupted (by |CTRL-C|) - -3 if the job-id is invalid - -join({list} [, {sep}]) *join()* - Join the items in {list} together into one String. - When {sep} is specified it is put in between the items. If - {sep} is omitted a single space is used. - Note that {sep} is not added at the end. You might want to - add it there too: > - let lines = join(mylist, "\n") . "\n" -< String items are used as-is. |Lists| and |Dictionaries| are - converted into a string like with |string()|. - The opposite function is |split()|. - - Can also be used as a |method|: > - mylist->join() - -json_decode({expr}) *json_decode()* - Convert {expr} from JSON object. Accepts |readfile()|-style - list as the input, as well as regular string. May output any - Vim value. In the following cases it will output - |msgpack-special-dict|: - 1. Dictionary contains duplicate key. - 2. Dictionary contains empty key. - 3. String contains NUL byte. Two special dictionaries: for - dictionary and for string will be emitted in case string - with NUL byte was a dictionary key. - - Note: function treats its input as UTF-8 always. The JSON - standard allows only a few encodings, of which UTF-8 is - recommended and the only one required to be supported. - Non-UTF-8 characters are an error. - - Can also be used as a |method|: > - ReadObject()->json_decode() - -json_encode({expr}) *json_encode()* - Convert {expr} into a JSON string. Accepts - |msgpack-special-dict| as the input. Will not convert - |Funcref|s, mappings with non-string keys (can be created as - |msgpack-special-dict|), values with self-referencing - containers, strings which contain non-UTF-8 characters, - pseudo-UTF-8 strings which contain codepoints reserved for - surrogate pairs (such strings are not valid UTF-8 strings). - Non-printable characters are converted into "\u1234" escapes - or special escapes like "\t", other are dumped as-is. - |Blob|s are converted to arrays of the individual bytes. - - Can also be used as a |method|: > - GetObject()->json_encode() - -keys({dict}) *keys()* - Return a |List| with all the keys of {dict}. The |List| is in - arbitrary order. Also see |items()| and |values()|. - - Can also be used as a |method|: > - mydict->keys() - -< *len()* *E701* -len({expr}) The result is a Number, which is the length of the argument. - When {expr} is a String or a Number the length in bytes is - used, as with |strlen()|. - When {expr} is a |List| the number of items in the |List| is - returned. - When {expr} is a |Blob| the number of bytes is returned. - When {expr} is a |Dictionary| the number of entries in the - |Dictionary| is returned. - Otherwise an error is given. - - Can also be used as a |method|: > - mylist->len() - -< *libcall()* *E364* *E368* -libcall({libname}, {funcname}, {argument}) - Call function {funcname} in the run-time library {libname} - with single argument {argument}. - This is useful to call functions in a library that you - especially made to be used with Vim. Since only one argument - is possible, calling standard library functions is rather - limited. - The result is the String returned by the function. If the - function returns NULL, this will appear as an empty string "" - to Vim. - If the function returns a number, use libcallnr()! - If {argument} is a number, it is passed to the function as an - int; if {argument} is a string, it is passed as a - null-terminated string. - - libcall() allows you to write your own 'plug-in' extensions to - Vim without having to recompile the program. It is NOT a - means to call system functions! If you try to do so Vim will - very probably crash. - - For Win32, the functions you write must be placed in a DLL - and use the normal C calling convention (NOT Pascal which is - used in Windows System DLLs). The function must take exactly - one parameter, either a character pointer or a long integer, - and must return a character pointer or NULL. The character - pointer returned must point to memory that will remain valid - after the function has returned (e.g. in static data in the - DLL). If it points to allocated memory, that memory will - leak away. Using a static buffer in the function should work, - it's then freed when the DLL is unloaded. - - WARNING: If the function returns a non-valid pointer, Vim may - crash! This also happens if the function returns a number, - because Vim thinks it's a pointer. - For Win32 systems, {libname} should be the filename of the DLL - without the ".DLL" suffix. A full path is only required if - the DLL is not in the usual places. - For Unix: When compiling your own plugins, remember that the - object code must be compiled as position-independent ('PIC'). - Examples: > - :echo libcall("libc.so", "getenv", "HOME") - -< Can also be used as a |method|, the base is passed as the - third argument: > - GetValue()->libcall("libc.so", "getenv") -< - *libcallnr()* -libcallnr({libname}, {funcname}, {argument}) - Just like |libcall()|, but used for a function that returns an - int instead of a string. - Examples: > - :echo libcallnr("/usr/lib/libc.so", "getpid", "") - :call libcallnr("libc.so", "printf", "Hello World!\n") - :call libcallnr("libc.so", "sleep", 10) -< - Can also be used as a |method|, the base is passed as the - third argument: > - GetValue()->libcallnr("libc.so", "printf") -< -line({expr} [, {winid}]) *line()* - The result is a Number, which is the line number of the file - position given with {expr}. The {expr} argument is a string. - The accepted positions are: - . the cursor position - $ the last line in the current buffer - 'x position of mark x (if the mark is not set, 0 is - returned) - w0 first line visible in current window (one if the - display isn't updated, e.g. in silent Ex mode) - w$ last line visible in current window (this is one - less than "w0" if no lines are visible) - v In Visual mode: the start of the Visual area (the - cursor is the end). When not in Visual mode - returns the cursor position. Differs from |'<| in - that it's updated right away. - Note that a mark in another file can be used. The line number - then applies to another buffer. - To get the column number use |col()|. To get both use - |getpos()|. - With the optional {winid} argument the values are obtained for - that window instead of the current window. - Examples: > - line(".") line number of the cursor - line(".", winid) idem, in window "winid" - line("'t") line number of mark t - line("'" . marker) line number of mark marker -< - Can also be used as a |method|: > - GetValue()->line() - -line2byte({lnum}) *line2byte()* - Return the byte count from the start of the buffer for line - {lnum}. This includes the end-of-line character, depending on - the 'fileformat' option for the current buffer. The first - line returns 1. UTF-8 encoding is used, 'fileencoding' is - ignored. This can also be used to get the byte count for the - line just below the last line: > - line2byte(line("$") + 1) -< This is the buffer size plus one. If 'fileencoding' is empty - it is the file size plus one. {lnum} is used like with - |getline()|. When {lnum} is invalid -1 is returned. - Also see |byte2line()|, |go| and |:goto|. - - Can also be used as a |method|: > - GetLnum()->line2byte() - -lispindent({lnum}) *lispindent()* - Get the amount of indent for line {lnum} according the lisp - indenting rules, as with 'lisp'. - The indent is counted in spaces, the value of 'tabstop' is - relevant. {lnum} is used just like in |getline()|. - When {lnum} is invalid, -1 is returned. - - Can also be used as a |method|: > - GetLnum()->lispindent() - -list2str({list} [, {utf8}]) *list2str()* - Convert each number in {list} to a character string can - concatenate them all. Examples: > - list2str([32]) returns " " - list2str([65, 66, 67]) returns "ABC" -< The same can be done (slowly) with: > - join(map(list, {nr, val -> nr2char(val)}), '') -< |str2list()| does the opposite. - - UTF-8 encoding is always used, {utf8} option has no effect, - and exists only for backwards-compatibility. - With UTF-8 composing characters work as expected: > - list2str([97, 769]) returns "á" -< - Can also be used as a |method|: > - GetList()->list2str() - -localtime() *localtime()* - Return the current time, measured as seconds since 1st Jan - 1970. See also |strftime()|, |strptime()| and |getftime()|. - - -log({expr}) *log()* - Return the natural logarithm (base e) of {expr} as a |Float|. - {expr} must evaluate to a |Float| or a |Number| in the range - (0, inf]. - Examples: > - :echo log(10) -< 2.302585 > - :echo log(exp(5)) -< 5.0 - - Can also be used as a |method|: > - Compute()->log() - -log10({expr}) *log10()* - Return the logarithm of Float {expr} to base 10 as a |Float|. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo log10(1000) -< 3.0 > - :echo log10(0.01) -< -2.0 - - Can also be used as a |method|: > - Compute()->log10() - -luaeval({expr}[, {expr}]) - Evaluate Lua expression {expr} and return its result converted - to Vim data structures. See |lua-eval| for more details. - - Can also be used as a |method|: > - GetExpr()->luaeval() - -map({expr1}, {expr2}) *map()* - {expr1} must be a |List|, |Blob| or |Dictionary|. - Replace each item in {expr1} with the result of evaluating - {expr2}. For a |Blob| each byte is replaced. - - {expr2} must be a |string| or |Funcref|. - - If {expr2} is a |string|, inside {expr2} |v:val| has the value - of the current item. For a |Dictionary| |v:key| has the key - of the current item and for a |List| |v:key| has the index of - the current item. For a |Blob| |v:key| has the index of the - current byte. - Example: > - :call map(mylist, '"> " . v:val . " <"') -< This puts "> " before and " <" after each item in "mylist". - - Note that {expr2} is the result of an expression and is then - used as an expression again. Often it is good to use a - |literal-string| to avoid having to double backslashes. You - still have to double ' quotes - - If {expr2} is a |Funcref| it is called with two arguments: - 1. The key or the index of the current item. - 2. the value of the current item. - The function must return the new value of the item. Example - that changes each value by "key-value": > - func KeyValue(key, val) - return a:key . '-' . a:val - endfunc - call map(myDict, function('KeyValue')) -< It is shorter when using a |lambda|: > - call map(myDict, {key, val -> key . '-' . val}) -< If you do not use "val" you can leave it out: > - call map(myDict, {key -> 'item: ' . key}) -< If you do not use "key" you can use a short name: > - call map(myDict, {_, val -> 'item: ' . val}) -< - The operation is done in-place. If you want a |List| or - |Dictionary| to remain unmodified make a copy first: > - :let tlist = map(copy(mylist), ' v:val . "\t"') - -< Returns {expr1}, the |List|, |Blob| or |Dictionary| that was - filtered. When an error is encountered while evaluating - {expr2} no further items in {expr1} are processed. When - {expr2} is a Funcref errors inside a function are ignored, - unless it was defined with the "abort" flag. - - Can also be used as a |method|: > - mylist->map(expr2) - -maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* - When {dict} is omitted or zero: Return the rhs of mapping - {name} in mode {mode}. The returned String has special - characters translated like in the output of the ":map" command - listing. - - When there is no mapping for {name}, an empty String is - returned. When the mapping for {name} is empty, then "<Nop>" - is returned. - - The {name} can have special key names, like in the ":map" - command. - - {mode} can be one of these strings: - "n" Normal - "v" Visual (including Select) - "o" Operator-pending - "i" Insert - "c" Cmd-line - "s" Select - "x" Visual - "l" langmap |language-mapping| - "t" Terminal - "" Normal, Visual and Operator-pending - When {mode} is omitted, the modes for "" are used. - - When {abbr} is there and it is |TRUE| use abbreviations - instead of mappings. - - When {dict} is there and it is |TRUE| return a dictionary - containing all the information of the mapping with the - following items: - "lhs" The {lhs} of the mapping. - "rhs" The {rhs} of the mapping as typed. - "silent" 1 for a |:map-silent| mapping, else 0. - "noremap" 1 if the {rhs} of the mapping is not remappable. - "script" 1 if mapping was defined with <script>. - "expr" 1 for an expression mapping (|:map-<expr>|). - "buffer" 1 for a buffer local mapping (|:map-local|). - "mode" Modes for which the mapping is defined. In - addition to the modes mentioned above, these - characters will be used: - " " Normal, Visual and Operator-pending - "!" Insert and Commandline mode - (|mapmode-ic|) - "sid" The script local ID, used for <sid> mappings - (|<SID>|). - "lnum" The line number in "sid", zero if unknown. - "nowait" Do not wait for other, longer mappings. - (|:map-<nowait>|). - - The mappings local to the current buffer are checked first, - then the global mappings. - This function can be used to map a key even when it's already - mapped, and have it do the original mapping too. Sketch: > - exe 'nnoremap <Tab> ==' . maparg('<Tab>', 'n') - -< Can also be used as a |method|: > - GetKey()->maparg('n') - -mapcheck({name} [, {mode} [, {abbr}]]) *mapcheck()* - Check if there is a mapping that matches with {name} in mode - {mode}. See |maparg()| for {mode} and special names in - {name}. - When {abbr} is there and it is non-zero use abbreviations - instead of mappings. - A match happens with a mapping that starts with {name} and - with a mapping which is equal to the start of {name}. - - matches mapping "a" "ab" "abc" ~ - mapcheck("a") yes yes yes - mapcheck("abc") yes yes yes - mapcheck("ax") yes no no - mapcheck("b") no no no - - The difference with maparg() is that mapcheck() finds a - mapping that matches with {name}, while maparg() only finds a - mapping for {name} exactly. - When there is no mapping that starts with {name}, an empty - String is returned. If there is one, the RHS of that mapping - is returned. If there are several mappings that start with - {name}, the RHS of one of them is returned. This will be - "<Nop>" if the RHS is empty. - The mappings local to the current buffer are checked first, - then the global mappings. - This function can be used to check if a mapping can be added - without being ambiguous. Example: > - :if mapcheck("_vv") == "" - : map _vv :set guifont=7x13<CR> - :endif -< This avoids adding the "_vv" mapping when there already is a - mapping for "_v" or for "_vvv". - - Can also be used as a |method|: > - GetKey()->mapcheck('n') - -match({expr}, {pat} [, {start} [, {count}]]) *match()* - When {expr} is a |List| then this returns the index of the - first item where {pat} matches. Each item is used as a - String, |Lists| and |Dictionaries| are used as echoed. - - Otherwise, {expr} is used as a String. The result is a - Number, which gives the index (byte offset) in {expr} where - {pat} matches. - - A match at the first character or |List| item returns zero. - If there is no match -1 is returned. - - For getting submatches see |matchlist()|. - Example: > - :echo match("testing", "ing") " results in 4 - :echo match([1, 'x'], '\a') " results in 1 -< See |string-match| for how {pat} is used. - *strpbrk()* - Vim doesn't have a strpbrk() function. But you can do: > - :let sepidx = match(line, '[.,;: \t]') -< *strcasestr()* - Vim doesn't have a strcasestr() function. But you can add - "\c" to the pattern to ignore case: > - :let idx = match(haystack, '\cneedle') -< - If {start} is given, the search starts from byte index - {start} in a String or item {start} in a |List|. - The result, however, is still the index counted from the - first character/item. Example: > - :echo match("testing", "ing", 2) -< result is again "4". > - :echo match("testing", "ing", 4) -< result is again "4". > - :echo match("testing", "t", 2) -< result is "3". - For a String, if {start} > 0 then it is like the string starts - {start} bytes later, thus "^" will match at {start}. Except - when {count} is given, then it's like matches before the - {start} byte are ignored (this is a bit complicated to keep it - backwards compatible). - For a String, if {start} < 0, it will be set to 0. For a list - the index is counted from the end. - If {start} is out of range ({start} > strlen({expr}) for a - String or {start} > len({expr}) for a |List|) -1 is returned. - - When {count} is given use the {count}'th match. When a match - is found in a String the search for the next one starts one - character further. Thus this example results in 1: > - echo match("testing", "..", 0, 2) -< In a |List| the search continues in the next item. - Note that when {count} is added the way {start} works changes, - see above. - - See |pattern| for the patterns that are accepted. - The 'ignorecase' option is used to set the ignore-caseness of - the pattern. 'smartcase' is NOT used. The matching is always - done like 'magic' is set and 'cpoptions' is empty. - Note that a match at the start is preferred, thus when the - pattern is using "*" (any number of matches) it tends to find - zero matches at the start instead of a number of matches - further down in the text. - - Can also be used as a |method|: > - GetText()->match('word') - GetList()->match('word') -< - *matchadd()* *E798* *E799* *E801* *E957* -matchadd({group}, {pattern}[, {priority}[, {id} [, {dict}]]]) - Defines a pattern to be highlighted in the current window (a - "match"). It will be highlighted with {group}. Returns an - identification number (ID), which can be used to delete the - match using |matchdelete()|. The ID is bound to the window. - Matching is case sensitive and magic, unless case sensitivity - or magicness are explicitly overridden in {pattern}. The - 'magic', 'smartcase' and 'ignorecase' options are not used. - The "Conceal" value is special, it causes the match to be - concealed. - - The optional {priority} argument assigns a priority to the - match. A match with a high priority will have its - highlighting overrule that of a match with a lower priority. - A priority is specified as an integer (negative numbers are no - exception). If the {priority} argument is not specified, the - default priority is 10. The priority of 'hlsearch' is zero, - hence all matches with a priority greater than zero will - overrule it. Syntax highlighting (see 'syntax') is a separate - mechanism, and regardless of the chosen priority a match will - always overrule syntax highlighting. - - The optional {id} argument allows the request for a specific - match ID. If a specified ID is already taken, an error - message will appear and the match will not be added. An ID - is specified as a positive integer (zero excluded). IDs 1, 2 - and 3 are reserved for |:match|, |:2match| and |:3match|, - respectively. If the {id} argument is not specified or -1, - |matchadd()| automatically chooses a free ID. - - The optional {dict} argument allows for further custom - values. Currently this is used to specify a match specific - conceal character that will be shown for |hl-Conceal| - highlighted matches. The dict can have the following members: - - conceal Special character to show instead of the - match (only for |hl-Conceal| highlighed - matches, see |:syn-cchar|) - window Instead of the current window use the - window with this number or window ID. - - The number of matches is not limited, as it is the case with - the |:match| commands. - - Example: > - :highlight MyGroup ctermbg=green guibg=green - :let m = matchadd("MyGroup", "TODO") -< Deletion of the pattern: > - :call matchdelete(m) - -< A list of matches defined by |matchadd()| and |:match| are - available from |getmatches()|. All matches can be deleted in - one operation by |clearmatches()|. - - Can also be used as a |method|: > - GetGroup()->matchadd('TODO') -< - *matchaddpos()* -matchaddpos({group}, {pos} [, {priority} [, {id} [, {dict}]]]) - Same as |matchadd()|, but requires a list of positions {pos} - instead of a pattern. This command is faster than |matchadd()| - because it does not require to handle regular expressions and - sets buffer line boundaries to redraw screen. It is supposed - to be used when fast match additions and deletions are - required, for example to highlight matching parentheses. - *E5030* *E5031* - {pos} is a list of positions. Each position can be one of - these: - - A number. This whole line will be highlighted. The first - line has number 1. - - A list with one number, e.g., [23]. The whole line with this - number will be highlighted. - - A list with two numbers, e.g., [23, 11]. The first number is - the line number, the second one is the column number (first - column is 1, the value must correspond to the byte index as - |col()| would return). The character at this position will - be highlighted. - - A list with three numbers, e.g., [23, 11, 3]. As above, but - the third number gives the length of the highlight in bytes. - - Entries with zero and negative line numbers are silently - ignored, as well as entries with negative column numbers and - lengths. - - The maximum number of positions in {pos} is 8. - - Example: > - :highlight MyGroup ctermbg=green guibg=green - :let m = matchaddpos("MyGroup", [[23, 24], 34]) -< Deletion of the pattern: > - :call matchdelete(m) - -< Matches added by |matchaddpos()| are returned by - |getmatches()|. - - Can also be used as a |method|: > - GetGroup()->matchaddpos([23, 11]) - -matcharg({nr}) *matcharg()* - Selects the {nr} match item, as set with a |:match|, - |:2match| or |:3match| command. - Return a |List| with two elements: - The name of the highlight group used - The pattern used. - When {nr} is not 1, 2 or 3 returns an empty |List|. - When there is no match item set returns ['', '']. - This is useful to save and restore a |:match|. - Highlighting matches using the |:match| commands are limited - to three matches. |matchadd()| does not have this limitation. - - Can also be used as a |method|: > - GetMatch()->matcharg() - -matchdelete({id} [, {win}]) *matchdelete()* *E802* *E803* - Deletes a match with ID {id} previously defined by |matchadd()| - or one of the |:match| commands. Returns 0 if successful, - otherwise -1. See example for |matchadd()|. All matches can - be deleted in one operation by |clearmatches()|. - If {win} is specified, use the window with this number or - window ID instead of the current window. - - Can also be used as a |method|: > - GetMatch()->matchdelete() - -matchend({expr}, {pat} [, {start} [, {count}]]) *matchend()* - Same as |match()|, but return the index of first character - after the match. Example: > - :echo matchend("testing", "ing") -< results in "7". - *strspn()* *strcspn()* - Vim doesn't have a strspn() or strcspn() function, but you can - do it with matchend(): > - :let span = matchend(line, '[a-zA-Z]') - :let span = matchend(line, '[^a-zA-Z]') -< Except that -1 is returned when there are no matches. - - The {start}, if given, has the same meaning as for |match()|. > - :echo matchend("testing", "ing", 2) -< results in "7". > - :echo matchend("testing", "ing", 5) -< result is "-1". - When {expr} is a |List| the result is equal to |match()|. - - Can also be used as a |method|: > - GetText()->matchend('word') - -matchlist({expr}, {pat} [, {start} [, {count}]]) *matchlist()* - Same as |match()|, but return a |List|. The first item in the - list is the matched string, same as what matchstr() would - return. Following items are submatches, like "\1", "\2", etc. - in |:substitute|. When an optional submatch didn't match an - empty string is used. Example: > - echo matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)') -< Results in: ['acd', 'a', '', 'c', 'd', '', '', '', '', ''] - When there is no match an empty list is returned. - - You can pass in a List, but that is not very useful. - - Can also be used as a |method|: > - GetText()->matchlist('word') - -matchstr({expr}, {pat} [, {start} [, {count}]]) *matchstr()* - Same as |match()|, but return the matched string. Example: > - :echo matchstr("testing", "ing") -< results in "ing". - When there is no match "" is returned. - The {start}, if given, has the same meaning as for |match()|. > - :echo matchstr("testing", "ing", 2) -< results in "ing". > - :echo matchstr("testing", "ing", 5) -< result is "". - When {expr} is a |List| then the matching item is returned. - The type isn't changed, it's not necessarily a String. - - Can also be used as a |method|: > - GetText()->matchstr('word') - -matchstrpos({expr}, {pat} [, {start} [, {count}]]) *matchstrpos()* - Same as |matchstr()|, but return the matched string, the start - position and the end position of the match. Example: > - :echo matchstrpos("testing", "ing") -< results in ["ing", 4, 7]. - When there is no match ["", -1, -1] is returned. - The {start}, if given, has the same meaning as for |match()|. > - :echo matchstrpos("testing", "ing", 2) -< results in ["ing", 4, 7]. > - :echo matchstrpos("testing", "ing", 5) -< result is ["", -1, -1]. - When {expr} is a |List| then the matching item, the index - of first item where {pat} matches, the start position and the - end position of the match are returned. > - :echo matchstrpos([1, '__x'], '\a') -< result is ["x", 1, 2, 3]. - The type isn't changed, it's not necessarily a String. - - Can also be used as a |method|: > - GetText()->matchstrpos('word') -< - *max()* -max({expr}) Return the maximum value of all items in {expr}. - {expr} can be a |List| or a |Dictionary|. For a Dictionary, - it returns the maximum of all values in the Dictionary. - If {expr} is neither a List nor a Dictionary, or one of the - items in {expr} cannot be used as a Number this results in - an error. An empty |List| or |Dictionary| results in zero. - - Can also be used as a |method|: > - mylist->max() - -menu_get({path} [, {modes}]) *menu_get()* - Returns a |List| of |Dictionaries| describing |menus| (defined - by |:menu|, |:amenu|, …), including |hidden-menus|. - - {path} matches a menu by name, or all menus if {path} is an - empty string. Example: > - :echo menu_get('File','') - :echo menu_get('') -< - {modes} is a string of zero or more modes (see |maparg()| or - |creating-menus| for the list of modes). "a" means "all". - - Example: > - nnoremenu &Test.Test inormal - inoremenu Test.Test insert - vnoremenu Test.Test x - echo menu_get("") - -< returns something like this: > - - [ { - "hidden": 0, - "name": "Test", - "priority": 500, - "shortcut": 84, - "submenus": [ { - "hidden": 0, - "mappings": { - i": { - "enabled": 1, - "noremap": 1, - "rhs": "insert", - "sid": 1, - "silent": 0 - }, - n": { ... }, - s": { ... }, - v": { ... } - }, - "name": "Test", - "priority": 500, - "shortcut": 0 - } ] - } ] -< - - *min()* -min({expr}) Return the minimum value of all items in {expr}. - {expr} can be a |List| or a |Dictionary|. For a Dictionary, - it returns the minimum of all values in the Dictionary. - If {expr} is neither a List nor a Dictionary, or one of the - items in {expr} cannot be used as a Number this results in - an error. An empty |List| or |Dictionary| results in zero. - - Can also be used as a |method|: > - mylist->min() - -< *mkdir()* *E739* -mkdir({name} [, {path} [, {prot}]]) - Create directory {name}. - If {path} is "p" then intermediate directories are created as - necessary. Otherwise it must be "". - If {prot} is given it is used to set the protection bits of - the new directory. The default is 0o755 (rwxr-xr-x: r/w for - the user, readable for others). Use 0o700 to make it - unreadable for others. - - {prot} is applied for all parts of {name}. Thus if you create - /tmp/foo/bar then /tmp/foo will be created with 0700. Example: > - :call mkdir($HOME . "/tmp/foo/bar", "p", 0700) -< This function is not available in the |sandbox|. - - If you try to create an existing directory with {path} set to - "p" mkdir() will silently exit. - - The function result is a Number, which is TRUE if the call was - successful or FALSE if the directory creation failed or partly - failed. - - Can also be used as a |method|: > - GetName()->mkdir() -< - *mode()* -mode([expr]) Return a string that indicates the current mode. - If [expr] is supplied and it evaluates to a non-zero Number or - a non-empty String (|non-zero-arg|), then the full mode is - returned, otherwise only the first letter is returned. - - n Normal - no Operator-pending - nov Operator-pending (forced charwise |o_v|) - noV Operator-pending (forced linewise |o_V|) - noCTRL-V Operator-pending (forced blockwise |o_CTRL-V|) - CTRL-V is one character - niI Normal using |i_CTRL-O| in |Insert-mode| - niR Normal using |i_CTRL-O| in |Replace-mode| - niV Normal using |i_CTRL-O| in |Virtual-Replace-mode| - nt Normal in |terminal-emulator| (insert goes to - Terminal mode) - v Visual by character - vs Visual by character using |v_CTRL-O| in Select mode - V Visual by line - Vs Visual by line using |v_CTRL-O| in Select mode - CTRL-V Visual blockwise - CTRL-Vs Visual blockwise using |v_CTRL-O| in Select mode - s Select by character - S Select by line - CTRL-S Select blockwise - i Insert - ic Insert mode completion |compl-generic| - ix Insert mode |i_CTRL-X| completion - R Replace |R| - Rc Replace mode completion |compl-generic| - Rx Replace mode |i_CTRL-X| completion - Rv Virtual Replace |gR| - Rvc Virtual Replace mode completion |compl-generic| - Rvx Virtual Replace mode |i_CTRL-X| completion - c Command-line editing - cv Vim Ex mode |gQ| - r Hit-enter prompt - rm The -- more -- prompt - r? A |:confirm| query of some sort - ! Shell or external command is executing - t Terminal mode: keys go to the job - - This is useful in the 'statusline' option or when used - with |remote_expr()| In most other places it always returns - "c" or "n". - Note that in the future more modes and more specific modes may - be added. It's better not to compare the whole string but only - the leading character(s). - Also see |visualmode()|. - - Can also be used as a |method|: > - DoFull()->mode() - -msgpackdump({list} [, {type}]) *msgpackdump()* - Convert a list of VimL objects to msgpack. Returned value is a - |readfile()|-style list. When {type} contains "B", a |Blob| is - returned instead. Example: > - call writefile(msgpackdump([{}]), 'fname.mpack', 'b') -< or, using a |Blob|: > - call writefile(msgpackdump([{}], 'B'), 'fname.mpack') -< - This will write the single 0x80 byte to a `fname.mpack` file - (dictionary with zero items is represented by 0x80 byte in - messagepack). - - Limitations: *E5004* *E5005* - 1. |Funcref|s cannot be dumped. - 2. Containers that reference themselves cannot be dumped. - 3. Dictionary keys are always dumped as STR strings. - 4. Other strings and |Blob|s are always dumped as BIN strings. - 5. Points 3. and 4. do not apply to |msgpack-special-dict|s. - -msgpackparse({data}) *msgpackparse()* - Convert a |readfile()|-style list or a |Blob| to a list of - VimL objects. - Example: > - let fname = expand('~/.config/nvim/shada/main.shada') - let mpack = readfile(fname, 'b') - let shada_objects = msgpackparse(mpack) -< This will read ~/.config/nvim/shada/main.shada file to - `shada_objects` list. - - Limitations: - 1. Mapping ordering is not preserved unless messagepack - mapping is dumped using generic mapping - (|msgpack-special-map|). - 2. Since the parser aims to preserve all data untouched - (except for 1.) some strings are parsed to - |msgpack-special-dict| format which is not convenient to - use. - *msgpack-special-dict* - Some messagepack strings may be parsed to special - dictionaries. Special dictionaries are dictionaries which - - 1. Contain exactly two keys: `_TYPE` and `_VAL`. - 2. `_TYPE` key is one of the types found in |v:msgpack_types| - variable. - 3. Value for `_VAL` has the following format (Key column - contains name of the key from |v:msgpack_types|): - - Key Value ~ - nil Zero, ignored when dumping. Not returned by - |msgpackparse()| since |v:null| was introduced. - boolean One or zero. When dumping it is only checked that - value is a |Number|. Not returned by |msgpackparse()| - since |v:true| and |v:false| were introduced. - integer |List| with four numbers: sign (-1 or 1), highest two - bits, number with bits from 62nd to 31st, lowest 31 - bits. I.e. to get actual number one will need to use - code like > - _VAL[0] * ((_VAL[1] << 62) - & (_VAL[2] << 31) - & _VAL[3]) -< Special dictionary with this type will appear in - |msgpackparse()| output under one of the following - circumstances: - 1. |Number| is 32-bit and value is either above - INT32_MAX or below INT32_MIN. - 2. |Number| is 64-bit and value is above INT64_MAX. It - cannot possibly be below INT64_MIN because msgpack - C parser does not support such values. - float |Float|. This value cannot possibly appear in - |msgpackparse()| output. - string |readfile()|-style list of strings. This value will - appear in |msgpackparse()| output if string contains - zero byte or if string is a mapping key and mapping is - being represented as special dictionary for other - reasons. - binary |String|, or |Blob| if binary string contains zero - byte. This value cannot appear in |msgpackparse()| - output since blobs were introduced. - array |List|. This value cannot appear in |msgpackparse()| - output. - *msgpack-special-map* - map |List| of |List|s with two items (key and value) each. - This value will appear in |msgpackparse()| output if - parsed mapping contains one of the following keys: - 1. Any key that is not a string (including keys which - are binary strings). - 2. String with NUL byte inside. - 3. Duplicate key. - 4. Empty key. - ext |List| with two values: first is a signed integer - representing extension type. Second is - |readfile()|-style list of strings. - -nextnonblank({lnum}) *nextnonblank()* - Return the line number of the first line at or below {lnum} - that is not blank. Example: > - if getline(nextnonblank(1)) =~ "Java" -< When {lnum} is invalid or there is no non-blank line at or - below it, zero is returned. - {lnum} is used like with |getline()|. - See also |prevnonblank()|. - - Can also be used as a |method|: > - GetLnum()->nextnonblank() - -nr2char({expr} [, {utf8}]) *nr2char()* - Return a string with a single character, which has the number - value {expr}. Examples: > - nr2char(64) returns "@" - nr2char(32) returns " " -< Example for "utf-8": > - nr2char(300) returns I with bow character -< UTF-8 encoding is always used, {utf8} option has no effect, - and exists only for backwards-compatibility. - Note that a NUL character in the file is specified with - nr2char(10), because NULs are represented with newline - characters. nr2char(0) is a real NUL and terminates the - string, thus results in an empty string. - - Can also be used as a |method|: > - GetNumber()->nr2char() - -nvim_...({...}) *E5555* *nvim_...()* *eval-api* - Call nvim |api| functions. The type checking of arguments will - be stricter than for most other builtins. For instance, - if Integer is expected, a |Number| must be passed in, a - |String| will not be autoconverted. - Buffer numbers, as returned by |bufnr()| could be used as - first argument to nvim_buf_... functions. All functions - expecting an object (buffer, window or tabpage) can - also take the numerical value 0 to indicate the current - (focused) object. - -or({expr}, {expr}) *or()* - Bitwise OR on the two arguments. The arguments are converted - to a number. A List, Dict or Float argument causes an error. - Example: > - :let bits = or(bits, 0x80) -< Can also be used as a |method|: > - :let bits = bits->or(0x80) - -pathshorten({path}) *pathshorten()* - Shorten directory names in the path {path} and return the - result. The tail, the file name, is kept as-is. The other - components in the path are reduced to single letters. Leading - '~' and '.' characters are kept. Example: > - :echo pathshorten('~/.config/nvim/autoload/file1.vim') -< ~/.c/n/a/file1.vim ~ - It doesn't matter if the path exists or not. - - Can also be used as a |method|: > - GetDirectories()->pathshorten() - -perleval({expr}) *perleval()* - Evaluate |perl| expression {expr} and return its result - converted to Vim data structures. - Numbers and strings are returned as they are (strings are - copied though). - Lists are represented as Vim |List| type. - Dictionaries are represented as Vim |Dictionary| type, - non-string keys result in error. - - Note: If you want an array or hash, {expr} must return a - reference to it. - Example: > - :echo perleval('[1 .. 4]') -< [1, 2, 3, 4] - - Can also be used as a |method|: > - GetExpr()->perleval() - -pow({x}, {y}) *pow()* - Return the power of {x} to the exponent {y} as a |Float|. - {x} and {y} must evaluate to a |Float| or a |Number|. - Examples: > - :echo pow(3, 3) -< 27.0 > - :echo pow(2, 16) -< 65536.0 > - :echo pow(32, 0.20) -< 2.0 - - Can also be used as a |method|: > - Compute()->pow(3) - -prevnonblank({lnum}) *prevnonblank()* - Return the line number of the first line at or above {lnum} - that is not blank. Example: > - let ind = indent(prevnonblank(v:lnum - 1)) -< When {lnum} is invalid or there is no non-blank line at or - above it, zero is returned. - {lnum} is used like with |getline()|. - Also see |nextnonblank()|. - - Can also be used as a |method|: > - GetLnum()->prevnonblank() - -printf({fmt}, {expr1} ...) *printf()* - Return a String with {fmt}, where "%" items are replaced by - the formatted form of their respective arguments. Example: > - printf("%4d: E%d %.30s", lnum, errno, msg) -< May result in: - " 99: E42 asdfasdfasdfasdfasdfasdfasdfas" ~ - - When used as a |method| the base is passed as the second - argument: > - Compute()->printf("result: %d") - -< Often used items are: - %s string - %6S string right-aligned in 6 display cells - %6s string right-aligned in 6 bytes - %.9s string truncated to 9 bytes - %c single byte - %d decimal number - %5d decimal number padded with spaces to 5 characters - %b binary number - %08b binary number padded with zeros to at least 8 characters - %B binary number using upper case letters - %x hex number - %04x hex number padded with zeros to at least 4 characters - %X hex number using upper case letters - %o octal number - %f floating point number as 12.23, inf, -inf or nan - %F floating point number as 12.23, INF, -INF or NAN - %e floating point number as 1.23e3, inf, -inf or nan - %E floating point number as 1.23E3, INF, -INF or NAN - %g floating point number, as %f or %e depending on value - %G floating point number, as %F or %E depending on value - %% the % character itself - %p representation of the pointer to the container - - Conversion specifications start with '%' and end with the - conversion type. All other characters are copied unchanged to - the result. - - The "%" starts a conversion specification. The following - arguments appear in sequence: - - % [flags] [field-width] [.precision] type - - flags - Zero or more of the following flags: - - # The value should be converted to an "alternate - form". For c, d, and s conversions, this option - has no effect. For o conversions, the precision - of the number is increased to force the first - character of the output string to a zero (except - if a zero value is printed with an explicit - precision of zero). - For x and X conversions, a non-zero result has - the string "0x" (or "0X" for X conversions) - prepended to it. - - 0 (zero) Zero padding. For all conversions the converted - value is padded on the left with zeros rather - than blanks. If a precision is given with a - numeric conversion (d, o, x, and X), the 0 flag - is ignored. - - - A negative field width flag; the converted value - is to be left adjusted on the field boundary. - The converted value is padded on the right with - blanks, rather than on the left with blanks or - zeros. A - overrides a 0 if both are given. - - ' ' (space) A blank should be left before a positive - number produced by a signed conversion (d). - - + A sign must always be placed before a number - produced by a signed conversion. A + overrides - a space if both are used. - - field-width - An optional decimal digit string specifying a minimum - field width. If the converted value has fewer bytes - than the field width, it will be padded with spaces on - the left (or right, if the left-adjustment flag has - been given) to fill out the field width. For the S - conversion the count is in cells. - - .precision - An optional precision, in the form of a period '.' - followed by an optional digit string. If the digit - string is omitted, the precision is taken as zero. - This gives the minimum number of digits to appear for - d, o, x, and X conversions, the maximum number of - bytes to be printed from a string for s conversions, - or the maximum number of cells to be printed from a - string for S conversions. - For floating point it is the number of digits after - the decimal point. - - type - A character that specifies the type of conversion to - be applied, see below. - - A field width or precision, or both, may be indicated by an - asterisk '*' instead of a digit string. In this case, a - Number argument supplies the field width or precision. A - negative field width is treated as a left adjustment flag - followed by a positive field width; a negative precision is - treated as though it were missing. Example: > - :echo printf("%d: %.*s", nr, width, line) -< This limits the length of the text used from "line" to - "width" bytes. - - The conversion specifiers and their meanings are: - - *printf-d* *printf-b* *printf-B* *printf-o* *printf-x* *printf-X* - dbBoxX The Number argument is converted to signed decimal (d), - unsigned binary (b and B), unsigned octal (o), or - unsigned hexadecimal (x and X) notation. The letters - "abcdef" are used for x conversions; the letters - "ABCDEF" are used for X conversions. The precision, if - any, gives the minimum number of digits that must - appear; if the converted value requires fewer digits, it - is padded on the left with zeros. In no case does a - non-existent or small field width cause truncation of a - numeric field; if the result of a conversion is wider - than the field width, the field is expanded to contain - the conversion result. - The 'h' modifier indicates the argument is 16 bits. - The 'l' modifier indicates the argument is 32 bits. - The 'L' modifier indicates the argument is 64 bits. - Generally, these modifiers are not useful. They are - ignored when type is known from the argument. - - i alias for d - D alias for ld - U alias for lu - O alias for lo - - *printf-c* - c The Number argument is converted to a byte, and the - resulting character is written. - - *printf-s* - s The text of the String argument is used. If a - precision is specified, no more bytes than the number - specified are used. - If the argument is not a String type, it is - automatically converted to text with the same format - as ":echo". - *printf-S* - S The text of the String argument is used. If a - precision is specified, no more display cells than the - number specified are used. - - *printf-f* *E807* - f F The Float argument is converted into a string of the - form 123.456. The precision specifies the number of - digits after the decimal point. When the precision is - zero the decimal point is omitted. When the precision - is not specified 6 is used. A really big number - (out of range or dividing by zero) results in "inf" - or "-inf" with %f (INF or -INF with %F). - "0.0 / 0.0" results in "nan" with %f (NAN with %F). - Example: > - echo printf("%.2f", 12.115) -< 12.12 - Note that roundoff depends on the system libraries. - Use |round()| when in doubt. - - *printf-e* *printf-E* - e E The Float argument is converted into a string of the - form 1.234e+03 or 1.234E+03 when using 'E'. The - precision specifies the number of digits after the - decimal point, like with 'f'. - - *printf-g* *printf-G* - g G The Float argument is converted like with 'f' if the - value is between 0.001 (inclusive) and 10000000.0 - (exclusive). Otherwise 'e' is used for 'g' and 'E' - for 'G'. When no precision is specified superfluous - zeroes and '+' signs are removed, except for the zero - immediately after the decimal point. Thus 10000000.0 - results in 1.0e7. - - *printf-%* - % A '%' is written. No argument is converted. The - complete conversion specification is "%%". - - When a Number argument is expected a String argument is also - accepted and automatically converted. - When a Float or String argument is expected a Number argument - is also accepted and automatically converted. - Any other argument type results in an error message. - - *E766* *E767* - The number of {exprN} arguments must exactly match the number - of "%" items. If there are not sufficient or too many - arguments an error is given. Up to 18 arguments can be used. - -prompt_getprompt({buf}) *prompt_getprompt()* - Returns the effective prompt text for buffer {buf}. {buf} can - be a buffer name or number. See |prompt-buffer|. - - If the buffer doesn't exist or isn't a prompt buffer, an empty - string is returned. - - Can also be used as a |method|: > - GetBuffer()->prompt_getprompt() - -prompt_setcallback({buf}, {expr}) *prompt_setcallback()* - Set prompt callback for buffer {buf} to {expr}. When {expr} - is an empty string the callback is removed. This has only - effect if {buf} has 'buftype' set to "prompt". - - The callback is invoked when pressing Enter. The current - buffer will always be the prompt buffer. A new line for a - prompt is added before invoking the callback, thus the prompt - for which the callback was invoked will be in the last but one - line. - If the callback wants to add text to the buffer, it must - insert it above the last line, since that is where the current - prompt is. This can also be done asynchronously. - The callback is invoked with one argument, which is the text - that was entered at the prompt. This can be an empty string - if the user only typed Enter. - Example: > - call prompt_setcallback(bufnr(''), function('s:TextEntered')) - func s:TextEntered(text) - if a:text == 'exit' || a:text == 'quit' - stopinsert - close - else - call append(line('$') - 1, 'Entered: "' . a:text . '"') - " Reset 'modified' to allow the buffer to be closed. - set nomodified - endif - endfunc - -< Can also be used as a |method|: > - GetBuffer()->prompt_setcallback(callback) - -prompt_setinterrupt({buf}, {expr}) *prompt_setinterrupt()* - Set a callback for buffer {buf} to {expr}. When {expr} is an - empty string the callback is removed. This has only effect if - {buf} has 'buftype' set to "prompt". - - This callback will be invoked when pressing CTRL-C in Insert - mode. Without setting a callback Vim will exit Insert mode, - as in any buffer. - - Can also be used as a |method|: > - GetBuffer()->prompt_setinterrupt(callback) - -prompt_setprompt({buf}, {text}) *prompt_setprompt()* - Set prompt for buffer {buf} to {text}. You most likely want - {text} to end in a space. - The result is only visible if {buf} has 'buftype' set to - "prompt". Example: > - call prompt_setprompt(bufnr(''), 'command: ') -< - Can also be used as a |method|: > - GetBuffer()->prompt_setprompt('command: ') - -pum_getpos() *pum_getpos()* - If the popup menu (see |ins-completion-menu|) is not visible, - returns an empty |Dictionary|, otherwise, returns a - |Dictionary| with the following keys: - height nr of items visible - width screen cells - row top screen row (0 first row) - col leftmost screen column (0 first col) - size total nr of items - scrollbar |TRUE| if scrollbar is visible - - The values are the same as in |v:event| during |CompleteChanged|. - -pumvisible() *pumvisible()* - Returns non-zero when the popup menu is visible, zero - otherwise. See |ins-completion-menu|. - This can be used to avoid some things that would remove the - popup menu. - -py3eval({expr}) *py3eval()* - Evaluate Python expression {expr} and return its result - converted to Vim data structures. - Numbers and strings are returned as they are (strings are - copied though, Unicode strings are additionally converted to - UTF-8). - Lists are represented as Vim |List| type. - Dictionaries are represented as Vim |Dictionary| type with - keys converted to strings. - - Can also be used as a |method|: > - GetExpr()->py3eval() -< - *E858* *E859* -pyeval({expr}) *pyeval()* - Evaluate Python expression {expr} and return its result - converted to Vim data structures. - Numbers and strings are returned as they are (strings are - copied though). - Lists are represented as Vim |List| type. - Dictionaries are represented as Vim |Dictionary| type, - non-string keys result in error. - - Can also be used as a |method|: > - GetExpr()->pyeval() - -pyxeval({expr}) *pyxeval()* - Evaluate Python expression {expr} and return its result - converted to Vim data structures. - Uses Python 2 or 3, see |python_x| and 'pyxversion'. - See also: |pyeval()|, |py3eval()| - - Can also be used as a |method|: > - GetExpr()->pyxeval() -< - *E726* *E727* -range({expr} [, {max} [, {stride}]]) *range()* - Returns a |List| with Numbers: - - If only {expr} is specified: [0, 1, ..., {expr} - 1] - - If {max} is specified: [{expr}, {expr} + 1, ..., {max}] - - If {stride} is specified: [{expr}, {expr} + {stride}, ..., - {max}] (increasing {expr} with {stride} each time, not - producing a value past {max}). - When the maximum is one before the start the result is an - empty list. When the maximum is more than one before the - start this is an error. - Examples: > - range(4) " [0, 1, 2, 3] - range(2, 4) " [2, 3, 4] - range(2, 9, 3) " [2, 5, 8] - range(2, -2, -1) " [2, 1, 0, -1, -2] - range(0) " [] - range(2, 0) " error! -< - Can also be used as a |method|: > - GetExpr()->range() -< - *readdir()* -readdir({directory} [, {expr}]) - Return a list with file and directory names in {directory}. - - When {expr} is omitted all entries are included. - When {expr} is given, it is evaluated to check what to do: - If {expr} results in -1 then no further entries will - be handled. - If {expr} results in 0 then this entry will not be - added to the list. - If {expr} results in 1 then this entry will be added - to the list. - Each time {expr} is evaluated |v:val| is set to the entry name. - When {expr} is a function the name is passed as the argument. - For example, to get a list of files ending in ".txt": > - readdir(dirname, {n -> n =~ '.txt$'}) -< To skip hidden and backup files: > - readdir(dirname, {n -> n !~ '^\.\|\~$'}) - -< If you want to get a directory tree: > - function! s:tree(dir) - return {a:dir : map(readdir(a:dir), - \ {_, x -> isdirectory(x) ? - \ {x : s:tree(a:dir . '/' . x)} : x})} - endfunction - echo s:tree(".") -< - Can also be used as a |method|: > - GetDirName()->readdir() -< - *readfile()* -readfile({fname} [, {type} [, {max}]]) - Read file {fname} and return a |List|, each line of the file - as an item. Lines are broken at NL characters. Macintosh - files separated with CR will result in a single long line - (unless a NL appears somewhere). - All NUL characters are replaced with a NL character. - When {type} contains "b" binary mode is used: - - When the last line ends in a NL an extra empty list item is - added. - - No CR characters are removed. - When {type} contains "B" a |Blob| is returned with the binary - data of the file unmodified. - Otherwise: - - CR characters that appear before a NL are removed. - - Whether the last line ends in a NL or not does not matter. - - Any UTF-8 byte order mark is removed from the text. - When {max} is given this specifies the maximum number of lines - to be read. Useful if you only want to check the first ten - lines of a file: > - :for line in readfile(fname, '', 10) - : if line =~ 'Date' | echo line | endif - :endfor -< When {max} is negative -{max} lines from the end of the file - are returned, or as many as there are. - When {max} is zero the result is an empty list. - Note that without {max} the whole file is read into memory. - Also note that there is no recognition of encoding. Read a - file into a buffer if you need to. - When the file can't be opened an error message is given and - the result is an empty list. - Also see |writefile()|. - - Can also be used as a |method|: > - GetFileName()->readfile() - -reg_executing() *reg_executing()* - Returns the single letter name of the register being executed. - Returns an empty string when no register is being executed. - See |@|. - -reg_recorded() *reg_recorded()* - Returns the single letter name of the last recorded register. - Returns an empty string string when nothing was recorded yet. - See |q| and |Q|. - -reg_recording() *reg_recording()* - Returns the single letter name of the register being recorded. - Returns an empty string string when not recording. See |q|. - -reltime([{start} [, {end}]]) *reltime()* - Return an item that represents a time value. The item is a - list with items that depend on the system. - The item can be passed to |reltimestr()| to convert it to a - string or |reltimefloat()| to convert to a Float. - - Without an argument it returns the current "relative time", an - implementation-defined value meaningful only when used as an - argument to |reltime()|, |reltimestr()| and |reltimefloat()|. - - With one argument it returns the time passed since the time - specified in the argument. - With two arguments it returns the time passed between {start} - and {end}. - - The {start} and {end} arguments must be values returned by - reltime(). - - Can also be used as a |method|: > - GetStart()->reltime() -< - Note: |localtime()| returns the current (non-relative) time. - -reltimefloat({time}) *reltimefloat()* - Return a Float that represents the time value of {time}. - Unit of time is seconds. - Example: - let start = reltime() - call MyFunction() - let seconds = reltimefloat(reltime(start)) - See the note of reltimestr() about overhead. - Also see |profiling|. - If there is an error an empty string is returned - - Can also be used as a |method|: > - reltime(start)->reltimefloat() - -reltimestr({time}) *reltimestr()* - Return a String that represents the time value of {time}. - This is the number of seconds, a dot and the number of - microseconds. Example: > - let start = reltime() - call MyFunction() - echo reltimestr(reltime(start)) -< Note that overhead for the commands will be added to the time. - Leading spaces are used to make the string align nicely. You - can use split() to remove it. > - echo split(reltimestr(reltime(start)))[0] -< Also see |profiling|. - If there is an error an empty string is returned - - Can also be used as a |method|: > - reltime(start)->reltimestr() -< - *remote_expr()* *E449* -remote_expr({server}, {string} [, {idvar} [, {timeout}]]) - Send the {string} to {server}. The string is sent as an - expression and the result is returned after evaluation. - The result must be a String or a |List|. A |List| is turned - into a String by joining the items with a line break in - between (not at the end), like with join(expr, "\n"). - If {idvar} is present and not empty, it is taken as the name - of a variable and a {serverid} for later use with - |remote_read()| is stored there. - If {timeout} is given the read times out after this many - seconds. Otherwise a timeout of 600 seconds is used. - See also |clientserver| |RemoteReply|. - This function is not available in the |sandbox|. - Note: Any errors will cause a local error message to be issued - and the result will be the empty string. - - Variables will be evaluated in the global namespace, - independent of a function currently being active. Except - when in debug mode, then local function variables and - arguments can be evaluated. - - Examples: > - :echo remote_expr("gvim", "2+2") - :echo remote_expr("gvim1", "b:current_syntax") -< - -remote_foreground({server}) *remote_foreground()* - Move the Vim server with the name {server} to the foreground. - The {server} argument is a string. - This works like: > - remote_expr({server}, "foreground()") -< Except that on Win32 systems the client does the work, to work - around the problem that the OS doesn't always allow the server - to bring itself to the foreground. - Note: This does not restore the window if it was minimized, - like foreground() does. - This function is not available in the |sandbox|. - {only in the Win32 GUI and the Win32 console version} - - -remote_peek({serverid} [, {retvar}]) *remote_peek()* - Returns a positive number if there are available strings - from {serverid}. Copies any reply string into the variable - {retvar} if specified. {retvar} must be a string with the - name of a variable. - Returns zero if none are available. - Returns -1 if something is wrong. - See also |clientserver|. - This function is not available in the |sandbox|. - Examples: > - :let repl = "" - :echo "PEEK: ".remote_peek(id, "repl").": ".repl - -remote_read({serverid}, [{timeout}]) *remote_read()* - Return the oldest available reply from {serverid} and consume - it. Unless a {timeout} in seconds is given, it blocks until a - reply is available. - See also |clientserver|. - This function is not available in the |sandbox|. - Example: > - :echo remote_read(id) -< - *remote_send()* *E241* -remote_send({server}, {string} [, {idvar}]) - Send the {string} to {server}. The string is sent as input - keys and the function returns immediately. At the Vim server - the keys are not mapped |:map|. - If {idvar} is present, it is taken as the name of a variable - and a {serverid} for later use with remote_read() is stored - there. - See also |clientserver| |RemoteReply|. - This function is not available in the |sandbox|. - - Note: Any errors will be reported in the server and may mess - up the display. - Examples: > - :echo remote_send("gvim", ":DropAndReply ".file, "serverid"). - \ remote_read(serverid) - - :autocmd NONE RemoteReply * - \ echo remote_read(expand("<amatch>")) - :echo remote_send("gvim", ":sleep 10 | echo ". - \ 'server2client(expand("<client>"), "HELLO")<CR>') -< - *remote_startserver()* *E941* *E942* -remote_startserver({name}) - Become the server {name}. This fails if already running as a - server, when |v:servername| is not empty. - -remove({list}, {idx} [, {end}]) *remove()* - Without {end}: Remove the item at {idx} from |List| {list} and - return the item. - With {end}: Remove items from {idx} to {end} (inclusive) and - return a |List| with these items. When {idx} points to the same - item as {end} a list with one item is returned. When {end} - points to an item before {idx} this is an error. - See |list-index| for possible values of {idx} and {end}. - Example: > - :echo "last item: " . remove(mylist, -1) - :call remove(mylist, 0, 9) - -< Can also be used as a |method|: > - mylist->remove(idx) - -remove({blob}, {idx} [, {end}]) - Without {end}: Remove the byte at {idx} from |Blob| {blob} and - return the byte. - With {end}: Remove bytes from {idx} to {end} (inclusive) and - return a |Blob| with these bytes. When {idx} points to the same - byte as {end} a |Blob| with one byte is returned. When {end} - points to a byte before {idx} this is an error. - Example: > - :echo "last byte: " . remove(myblob, -1) - :call remove(mylist, 0, 9) - -remove({dict}, {key}) - Remove the entry from {dict} with key {key} and return it. - Example: > - :echo "removed " . remove(dict, "one") -< If there is no {key} in {dict} this is an error. - - Use |delete()| to remove a file. - -rename({from}, {to}) *rename()* - Rename the file by the name {from} to the name {to}. This - should also work to move files across file systems. The - result is a Number, which is 0 if the file was renamed - successfully, and non-zero when the renaming failed. - NOTE: If {to} exists it is overwritten without warning. - This function is not available in the |sandbox|. - - Can also be used as a |method|: > - GetOldName()->rename(newname) - -repeat({expr}, {count}) *repeat()* - Repeat {expr} {count} times and return the concatenated - result. Example: > - :let separator = repeat('-', 80) -< When {count} is zero or negative the result is empty. - When {expr} is a |List| the result is {expr} concatenated - {count} times. Example: > - :let longlist = repeat(['a', 'b'], 3) -< Results in ['a', 'b', 'a', 'b', 'a', 'b']. - - Can also be used as a |method|: > - mylist->repeat(count) - -resolve({filename}) *resolve()* *E655* - On MS-Windows, when {filename} is a shortcut (a .lnk file), - returns the path the shortcut points to in a simplified form. - On Unix, repeat resolving symbolic links in all path - components of {filename} and return the simplified result. - To cope with link cycles, resolving of symbolic links is - stopped after 100 iterations. - On other systems, return the simplified {filename}. - The simplification step is done as by |simplify()|. - resolve() keeps a leading path component specifying the - current directory (provided the result is still a relative - path name) and also keeps a trailing path separator. - - Can also be used as a |method|: > - GetName()->resolve() -< - *reverse()* -reverse({object}) - Reverse the order of items in {object} in-place. - {object} can be a |List| or a |Blob|. - Returns {object}. - If you want an object to remain unmodified make a copy first: > - :let revlist = reverse(copy(mylist)) -< Can also be used as a |method|: > - mylist->reverse() - -round({expr}) *round()* - Round off {expr} to the nearest integral value and return it - as a |Float|. If {expr} lies halfway between two integral - values, then use the larger one (away from zero). - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - echo round(0.456) -< 0.0 > - echo round(4.5) -< 5.0 > - echo round(-4.5) -< -5.0 - - Can also be used as a |method|: > - Compute()->round() - -rpcnotify({channel}, {event}[, {args}...]) *rpcnotify()* - Sends {event} to {channel} via |RPC| and returns immediately. - If {channel} is 0, the event is broadcast to all channels. - Example: > - :au VimLeave call rpcnotify(0, "leaving") - -rpcrequest({channel}, {method}[, {args}...]) *rpcrequest()* - Sends a request to {channel} to invoke {method} via - |RPC| and blocks until a response is received. - Example: > - :let result = rpcrequest(rpc_chan, "func", 1, 2, 3) - -rpcstart({prog}[, {argv}]) *rpcstart()* - Deprecated. Replace > - :let id = rpcstart('prog', ['arg1', 'arg2']) -< with > - :let id = jobstart(['prog', 'arg1', 'arg2'], {'rpc': v:true}) - -rubyeval({expr}) *rubyeval()* - Evaluate Ruby expression {expr} and return its result - converted to Vim data structures. - Numbers, floats and strings are returned as they are (strings - are copied though). - Arrays are represented as Vim |List| type. - Hashes are represented as Vim |Dictionary| type. - Other objects are represented as strings resulted from their - "Object#to_s" method. - - Can also be used as a |method|: > - GetRubyExpr()->rubyeval() - -screenattr({row}, {col}) *screenattr()* - Like |screenchar()|, but return the attribute. This is a rather - arbitrary number that can only be used to compare to the - attribute at other positions. - - Can also be used as a |method|: > - GetRow()->screenattr(col) - -screenchar({row}, {col}) *screenchar()* - The result is a Number, which is the character at position - [row, col] on the screen. This works for every possible - screen position, also status lines, window separators and the - command line. The top left position is row one, column one - The character excludes composing characters. For double-byte - encodings it may only be the first byte. - This is mainly to be used for testing. - Returns -1 when row or col is out of range. - - Can also be used as a |method|: > - GetRow()->screenchar(col) - -screenchars({row}, {col}) *screenchars()* - The result is a List of Numbers. The first number is the same - as what |screenchar()| returns. Further numbers are - composing characters on top of the base character. - This is mainly to be used for testing. - Returns an empty List when row or col is out of range. - - Can also be used as a |method|: > - GetRow()->screenchars(col) - -screencol() *screencol()* - The result is a Number, which is the current screen column of - the cursor. The leftmost column has number 1. - This function is mainly used for testing. - - Note: Always returns the current screen column, thus if used - in a command (e.g. ":echo screencol()") it will return the - column inside the command line, which is 1 when the command is - executed. To get the cursor position in the file use one of - the following mappings: > - nnoremap <expr> GG ":echom ".screencol()."\n" - nnoremap <silent> GG :echom screencol()<CR> - noremap GG <Cmd>echom screencol()<Cr> -< -screenpos({winid}, {lnum}, {col}) *screenpos()* - The result is a Dict with the screen position of the text - character in window {winid} at buffer line {lnum} and column - {col}. {col} is a one-based byte index. - The Dict has these members: - row screen row - col first screen column - endcol last screen column - curscol cursor screen column - If the specified position is not visible, all values are zero. - The "endcol" value differs from "col" when the character - occupies more than one screen cell. E.g. for a Tab "col" can - be 1 and "endcol" can be 8. - The "curscol" value is where the cursor would be placed. For - a Tab it would be the same as "endcol", while for a double - width character it would be the same as "col". - The |conceal| feature is ignored here, the column numbers are - as if 'conceallevel' is zero. You can set the cursor to the - right position and use |screencol()| to get the value with - |conceal| taken into account. - - Can also be used as a |method|: > - GetWinid()->screenpos(lnum, col) - -screenrow() *screenrow()* - The result is a Number, which is the current screen row of the - cursor. The top line has number one. - This function is mainly used for testing. - Alternatively you can use |winline()|. - - Note: Same restrictions as with |screencol()|. - -screenstring({row}, {col}) *screenstring()* - The result is a String that contains the base character and - any composing characters at position [row, col] on the screen. - This is like |screenchars()| but returning a String with the - characters. - This is mainly to be used for testing. - Returns an empty String when row or col is out of range. - - Can also be used as a |method|: > - GetRow()->screenstring(col) - -search({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *search()* - Search for regexp pattern {pattern}. The search starts at the - cursor position (you can use |cursor()| to set it). - - When a match has been found its line number is returned. - If there is no match a 0 is returned and the cursor doesn't - move. No error message is given. - - {flags} is a String, which can contain these character flags: - 'b' search Backward instead of forward - 'c' accept a match at the Cursor position - 'e' move to the End of the match - 'n' do Not move the cursor - 'p' return number of matching sub-Pattern (see below) - 's' Set the ' mark at the previous location of the cursor - 'w' Wrap around the end of the file - 'W' don't Wrap around the end of the file - 'z' start searching at the cursor column instead of Zero - If neither 'w' or 'W' is given, the 'wrapscan' option applies. - - If the 's' flag is supplied, the ' mark is set, only if the - cursor is moved. The 's' flag cannot be combined with the 'n' - flag. - - 'ignorecase', 'smartcase' and 'magic' are used. - - When the 'z' flag is not given, forward searching always - starts in column zero and then matches before the cursor are - skipped. When the 'c' flag is present in 'cpo' the next - search starts after the match. Without the 'c' flag the next - search starts one column further. This matters for - overlapping matches. - When searching backwards and the 'z' flag is given then the - search starts in column zero, thus no match in the current - line will be found (unless wrapping around the end of the - file). - - When the {stopline} argument is given then the search stops - after searching this line. This is useful to restrict the - search to a range of lines. Examples: > - let match = search('(', 'b', line("w0")) - let end = search('END', '', line("w$")) -< When {stopline} is used and it is not zero this also implies - that the search does not wrap around the end of the file. - A zero value is equal to not giving the argument. - - When the {timeout} argument is given the search stops when - more than this many milliseconds have passed. Thus when - {timeout} is 500 the search stops after half a second. - The value must not be negative. A zero value is like not - giving the argument. - - *search()-sub-match* - With the 'p' flag the returned value is one more than the - first sub-match in \(\). One if none of them matched but the - whole pattern did match. - To get the column number too use |searchpos()|. - - The cursor will be positioned at the match, unless the 'n' - flag is used. - - Example (goes over all files in the argument list): > - :let n = 1 - :while n <= argc() " loop over all files in arglist - : exe "argument " . n - : " start at the last char in the file and wrap for the - : " first search to find match at start of file - : normal G$ - : let flags = "w" - : while search("foo", flags) > 0 - : s/foo/bar/g - : let flags = "W" - : endwhile - : update " write the file if modified - : let n = n + 1 - :endwhile -< - Example for using some flags: > - :echo search('\<if\|\(else\)\|\(endif\)', 'ncpe') -< This will search for the keywords "if", "else", and "endif" - under or after the cursor. Because of the 'p' flag, it - returns 1, 2, or 3 depending on which keyword is found, or 0 - if the search fails. With the cursor on the first word of the - line: - if (foo == 0) | let foo = foo + 1 | endif ~ - the function returns 1. Without the 'c' flag, the function - finds the "endif" and returns 3. The same thing happens - without the 'e' flag if the cursor is on the "f" of "if". - The 'n' flag tells the function not to move the cursor. - - Can also be used as a |method|: > - GetPattern()->search() - -searchcount([{options}]) *searchcount()* - Get or update the last search count, like what is displayed - without the "S" flag in 'shortmess'. This works even if - 'shortmess' does contain the "S" flag. - - This returns a Dictionary. The dictionary is empty if the - previous pattern was not set and "pattern" was not specified. - - key type meaning ~ - current |Number| current position of match; - 0 if the cursor position is - before the first match - exact_match |Boolean| 1 if "current" is matched on - "pos", otherwise 0 - total |Number| total count of matches found - incomplete |Number| 0: search was fully completed - 1: recomputing was timed out - 2: max count exceeded - - For {options} see further down. - - To get the last search count when |n| or |N| was pressed, call - this function with `recompute: 0` . This sometimes returns - wrong information because |n| and |N|'s maximum count is 99. - If it exceeded 99 the result must be max count + 1 (100). If - you want to get correct information, specify `recompute: 1`: > - - " result == maxcount + 1 (100) when many matches - let result = searchcount(#{recompute: 0}) - - " Below returns correct result (recompute defaults - " to 1) - let result = searchcount() -< - The function is useful to add the count to |statusline|: > - function! LastSearchCount() abort - let result = searchcount(#{recompute: 0}) - if empty(result) - return '' - endif - if result.incomplete ==# 1 " timed out - return printf(' /%s [?/??]', @/) - elseif result.incomplete ==# 2 " max count exceeded - if result.total > result.maxcount && - \ result.current > result.maxcount - return printf(' /%s [>%d/>%d]', @/, - \ result.current, result.total) - elseif result.total > result.maxcount - return printf(' /%s [%d/>%d]', @/, - \ result.current, result.total) - endif - endif - return printf(' /%s [%d/%d]', @/, - \ result.current, result.total) - endfunction - let &statusline .= '%{LastSearchCount()}' - - " Or if you want to show the count only when - " 'hlsearch' was on - " let &statusline .= - " \ '%{v:hlsearch ? LastSearchCount() : ""}' -< - You can also update the search count, which can be useful in a - |CursorMoved| or |CursorMovedI| autocommand: > - - autocmd CursorMoved,CursorMovedI * - \ let s:searchcount_timer = timer_start( - \ 200, function('s:update_searchcount')) - function! s:update_searchcount(timer) abort - if a:timer ==# s:searchcount_timer - call searchcount(#{ - \ recompute: 1, maxcount: 0, timeout: 100}) - redrawstatus - endif - endfunction -< - This can also be used to count matched texts with specified - pattern in the current buffer using "pattern": > - - " Count '\<foo\>' in this buffer - " (Note that it also updates search count) - let result = searchcount(#{pattern: '\<foo\>'}) - - " To restore old search count by old pattern, - " search again - call searchcount() -< - {options} must be a Dictionary. It can contain: - key type meaning ~ - recompute |Boolean| if |TRUE|, recompute the count - like |n| or |N| was executed. - otherwise returns the last - result by |n|, |N|, or this - function is returned. - (default: |TRUE|) - pattern |String| recompute if this was given - and different with |@/|. - this works as same as the - below command is executed - before calling this function > - let @/ = pattern -< (default: |@/|) - timeout |Number| 0 or negative number is no - timeout. timeout milliseconds - for recomputing the result - (default: 0) - maxcount |Number| 0 or negative number is no - limit. max count of matched - text while recomputing the - result. if search exceeded - total count, "total" value - becomes `maxcount + 1` - (default: 0) - pos |List| `[lnum, col, off]` value - when recomputing the result. - this changes "current" result - value. see |cursor()|, |getpos() - (default: cursor's position) - - Can also be used as a |method|: > - GetSearchOpts()->searchcount() -< -searchdecl({name} [, {global} [, {thisblock}]]) *searchdecl()* - Search for the declaration of {name}. - - With a non-zero {global} argument it works like |gD|, find - first match in the file. Otherwise it works like |gd|, find - first match in the function. - - With a non-zero {thisblock} argument matches in a {} block - that ends before the cursor position are ignored. Avoids - finding variable declarations only valid in another scope. - - Moves the cursor to the found match. - Returns zero for success, non-zero for failure. - Example: > - if searchdecl('myvar') == 0 - echo getline('.') - endif -< - Can also be used as a |method|: > - GetName()->searchdecl() -< - *searchpair()* -searchpair({start}, {middle}, {end} [, {flags} [, {skip} - [, {stopline} [, {timeout}]]]]) - Search for the match of a nested start-end pair. This can be - used to find the "endif" that matches an "if", while other - if/endif pairs in between are ignored. - The search starts at the cursor. The default is to search - forward, include 'b' in {flags} to search backward. - If a match is found, the cursor is positioned at it and the - line number is returned. If no match is found 0 or -1 is - returned and the cursor doesn't move. No error message is - given. - - {start}, {middle} and {end} are patterns, see |pattern|. They - must not contain \( \) pairs. Use of \%( \) is allowed. When - {middle} is not empty, it is found when searching from either - direction, but only when not in a nested start-end pair. A - typical use is: > - searchpair('\<if\>', '\<else\>', '\<endif\>') -< By leaving {middle} empty the "else" is skipped. - - {flags} 'b', 'c', 'n', 's', 'w' and 'W' are used like with - |search()|. Additionally: - 'r' Repeat until no more matches found; will find the - outer pair. Implies the 'W' flag. - 'm' Return number of matches instead of line number with - the match; will be > 1 when 'r' is used. - Note: it's nearly always a good idea to use the 'W' flag, to - avoid wrapping around the end of the file. - - When a match for {start}, {middle} or {end} is found, the - {skip} expression is evaluated with the cursor positioned on - the start of the match. It should return non-zero if this - match is to be skipped. E.g., because it is inside a comment - or a string. - When {skip} is omitted or empty, every match is accepted. - When evaluating {skip} causes an error the search is aborted - and -1 returned. - {skip} can be a string, a lambda, a funcref or a partial. - Anything else makes the function fail. - - For {stopline} and {timeout} see |search()|. - - The value of 'ignorecase' is used. 'magic' is ignored, the - patterns are used like it's on. - - The search starts exactly at the cursor. A match with - {start}, {middle} or {end} at the next character, in the - direction of searching, is the first one found. Example: > - if 1 - if 2 - endif 2 - endif 1 -< When starting at the "if 2", with the cursor on the "i", and - searching forwards, the "endif 2" is found. When starting on - the character just before the "if 2", the "endif 1" will be - found. That's because the "if 2" will be found first, and - then this is considered to be a nested if/endif from "if 2" to - "endif 2". - When searching backwards and {end} is more than one character, - it may be useful to put "\zs" at the end of the pattern, so - that when the cursor is inside a match with the end it finds - the matching start. - - Example, to find the "endif" command in a Vim script: > - - :echo searchpair('\<if\>', '\<el\%[seif]\>', '\<en\%[dif]\>', 'W', - \ 'getline(".") =~ "^\\s*\""') - -< The cursor must be at or after the "if" for which a match is - to be found. Note that single-quote strings are used to avoid - having to double the backslashes. The skip expression only - catches comments at the start of a line, not after a command. - Also, a word "en" or "if" halfway through a line is considered - a match. - Another example, to search for the matching "{" of a "}": > - - :echo searchpair('{', '', '}', 'bW') - -< This works when the cursor is at or before the "}" for which a - match is to be found. To reject matches that syntax - highlighting recognized as strings: > - - :echo searchpair('{', '', '}', 'bW', - \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') -< - *searchpairpos()* -searchpairpos({start}, {middle}, {end} [, {flags} [, {skip} - [, {stopline} [, {timeout}]]]]) - Same as |searchpair()|, but returns a |List| with the line and - column position of the match. The first element of the |List| - is the line number and the second element is the byte index of - the column position of the match. If no match is found, - returns [0, 0]. > - - :let [lnum,col] = searchpairpos('{', '', '}', 'n') -< - See |match-parens| for a bigger and more useful example. - -searchpos({pattern} [, {flags} [, {stopline} [, {timeout}]]]) *searchpos()* - Same as |search()|, but returns a |List| with the line and - column position of the match. The first element of the |List| - is the line number and the second element is the byte index of - the column position of the match. If no match is found, - returns [0, 0]. - Example: > - :let [lnum, col] = searchpos('mypattern', 'n') - -< When the 'p' flag is given then there is an extra item with - the sub-pattern match number |search()-sub-match|. Example: > - :let [lnum, col, submatch] = searchpos('\(\l\)\|\(\u\)', 'np') -< In this example "submatch" is 2 when a lowercase letter is - found |/\l|, 3 when an uppercase letter is found |/\u|. - - Can also be used as a |method|: > - GetPattern()->searchpos() - -server2client({clientid}, {string}) *server2client()* - Send a reply string to {clientid}. The most recent {clientid} - that sent a string can be retrieved with expand("<client>"). - Note: - Returns zero for success, -1 for failure. - This id has to be stored before the next command can be - received. I.e. before returning from the received command and - before calling any commands that waits for input. - See also |clientserver|. - Example: > - :echo server2client(expand("<client>"), "HELLO") - -< Can also be used as a |method|: > - GetClientId()->server2client(string) -< -serverlist() *serverlist()* - Returns a list of server addresses, or empty if all servers - were stopped. |serverstart()| |serverstop()| - Example: > - :echo serverlist() - -serverstart([{address}]) *serverstart()* - Opens a socket or named pipe at {address} and listens for - |RPC| messages. Clients can send |API| commands to the address - to control Nvim. Returns the address string. - - If {address} does not contain a colon ":" it is interpreted as - a named pipe or Unix domain socket path. - - Example: > - if has('win32') - call serverstart('\\.\pipe\nvim-pipe-1234') - else - call serverstart('nvim.sock') - endif -< - If {address} contains a colon ":" it is interpreted as a TCP - address where the last ":" separates the host and port. - Assigns a random port if it is empty or 0. Supports IPv4/IPv6. - - Example: > - :call serverstart('::1:12345') -< - If no address is given, it is equivalent to: > - :call serverstart(tempname()) - -< |$NVIM_LISTEN_ADDRESS| is set to {address} if not already set. - -serverstop({address}) *serverstop()* - Closes the pipe or socket at {address}. - Returns TRUE if {address} is valid, else FALSE. - If |$NVIM_LISTEN_ADDRESS| is stopped it is unset. - If |v:servername| is stopped it is set to the next available - address returned by |serverlist()|. - -setbufline({buf}, {lnum}, {text}) *setbufline()* - Set line {lnum} to {text} in buffer {buf}. This works like - |setline()| for the specified buffer. - - This function works only for loaded buffers. First call - |bufload()| if needed. - - To insert lines use |appendbufline()|. - Any text properties in {lnum} are cleared. - - {text} can be a string to set one line, or a list of strings - to set multiple lines. If the list extends below the last - line then those lines are added. - - For the use of {buf}, see |bufname()| above. - - {lnum} is used like with |setline()|. - Use "$" to refer to the last line in buffer {buf}. - When {lnum} is just below the last line the {text} will be - added below the last line. - On success 0 is returned, on failure 1 is returned. - - If {buf} is not a valid buffer or {lnum} is not valid, an - error message is given. - - Can also be used as a |method|, the base is passed as the - third argument: > - GetText()->setbufline(buf, lnum) - -setbufvar({buf}, {varname}, {val}) *setbufvar()* - Set option or local variable {varname} in buffer {buf} to - {val}. - This also works for a global or local window option, but it - doesn't work for a global or local window variable. - For a local window option the global value is unchanged. - For the use of {buf}, see |bufname()| above. - The {varname} argument is a string. - Note that the variable name without "b:" must be used. - Examples: > - :call setbufvar(1, "&mod", 1) - :call setbufvar("todo", "myvar", "foobar") -< This function is not available in the |sandbox|. - - Can also be used as a |method|, the base is passed as the - third argument: > - GetValue()->setbufvar(buf, varname) - -setcharsearch({dict}) *setcharsearch()* - Set the current character search information to {dict}, - which contains one or more of the following entries: - - char character which will be used for a subsequent - |,| or |;| command; an empty string clears the - character search - forward direction of character search; 1 for forward, - 0 for backward - until type of character search; 1 for a |t| or |T| - character search, 0 for an |f| or |F| - character search - - This can be useful to save/restore a user's character search - from a script: > - :let prevsearch = getcharsearch() - :" Perform a command which clobbers user's search - :call setcharsearch(prevsearch) -< Also see |getcharsearch()|. - - Can also be used as a |method|: > - SavedSearch()->setcharsearch() - -setcmdpos({pos}) *setcmdpos()* - Set the cursor position in the command line to byte position - {pos}. The first position is 1. - Use |getcmdpos()| to obtain the current position. - Only works while editing the command line, thus you must use - |c_CTRL-\_e|, |c_CTRL-R_=| or |c_CTRL-R_CTRL-R| with '='. For - |c_CTRL-\_e| and |c_CTRL-R_CTRL-R| with '=' the position is - set after the command line is set to the expression. For - |c_CTRL-R_=| it is set after evaluating the expression but - before inserting the resulting text. - When the number is too big the cursor is put at the end of the - line. A number smaller than one has undefined results. - Returns FALSE when successful, TRUE when not editing the - command line. - - Can also be used as a |method|: > - GetPos()->setcmdpos() - -setenv({name}, {val}) *setenv()* - Set environment variable {name} to {val}. Example: > - call setenv('HOME', '/home/myhome') - -< When {val} is |v:null| the environment variable is deleted. - See also |expr-env|. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetPath()->setenv('PATH') - -setfperm({fname}, {mode}) *setfperm()* *chmod* - Set the file permissions for {fname} to {mode}. - {mode} must be a string with 9 characters. It is of the form - "rwxrwxrwx", where each group of "rwx" flags represent, in - turn, the permissions of the owner of the file, the group the - file belongs to, and other users. A '-' character means the - permission is off, any other character means on. Multi-byte - characters are not supported. - - For example "rw-r-----" means read-write for the user, - readable by the group, not accessible by others. "xx-x-----" - would do the same thing. - - Returns non-zero for success, zero for failure. - - Can also be used as a |method|: > - GetFilename()->setfperm(mode) -< - To read permissions see |getfperm()|. - -setline({lnum}, {text}) *setline()* - Set line {lnum} of the current buffer to {text}. To insert - lines use |append()|. To set lines in another buffer use - |setbufline()|. - - {lnum} is used like with |getline()|. - When {lnum} is just below the last line the {text} will be - added below the last line. - - If this succeeds, FALSE is returned. If this fails (most likely - because {lnum} is invalid) TRUE is returned. - - Example: > - :call setline(5, strftime("%c")) - -< When {text} is a |List| then line {lnum} and following lines - will be set to the items in the list. Example: > - :call setline(5, ['aaa', 'bbb', 'ccc']) -< This is equivalent to: > - :for [n, l] in [[5, 'aaa'], [6, 'bbb'], [7, 'ccc']] - : call setline(n, l) - :endfor - -< Note: The '[ and '] marks are not set. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetText()->setline(lnum) - -setloclist({nr}, {list} [, {action} [, {what}]]) *setloclist()* - Create or replace or add to the location list for window {nr}. - {nr} can be the window number or the |window-ID|. - When {nr} is zero the current window is used. - - For a location list window, the displayed location list is - modified. For an invalid window number {nr}, -1 is returned. - Otherwise, same as |setqflist()|. - Also see |location-list|. - - For {action} see |setqflist-action|. - - If the optional {what} dictionary argument is supplied, then - only the items listed in {what} are set. Refer to |setqflist()| - for the list of supported keys in {what}. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetLoclist()->setloclist(winnr) - -setmatches({list} [, {win}]) *setmatches()* - Restores a list of matches saved by |getmatches() for the - current window|. Returns 0 if successful, otherwise -1. All - current matches are cleared before the list is restored. See - example for |getmatches()|. - If {win} is specified, use the window with this number or - window ID instead of the current window. - - Can also be used as a |method|: > - GetMatches()->setmatches() -< - *setpos()* -setpos({expr}, {list}) - Set the position for String {expr}. Possible values: - . the cursor - 'x mark x - - {list} must be a |List| with four or five numbers: - [bufnum, lnum, col, off] - [bufnum, lnum, col, off, curswant] - - "bufnum" is the buffer number. Zero can be used for the - current buffer. When setting an uppercase mark "bufnum" is - used for the mark position. For other marks it specifies the - buffer to set the mark in. You can use the |bufnr()| function - to turn a file name into a buffer number. - For setting the cursor and the ' mark "bufnum" is ignored, - since these are associated with a window, not a buffer. - Does not change the jumplist. - - "lnum" and "col" are the position in the buffer. The first - column is 1. Use a zero "lnum" to delete a mark. If "col" is - smaller than 1 then 1 is used. - - The "off" number is only used when 'virtualedit' is set. Then - it is the offset in screen columns from the start of the - character. E.g., a position within a <Tab> or after the last - character. - - The "curswant" number is only used when setting the cursor - position. It sets the preferred column for when moving the - cursor vertically. When the "curswant" number is missing the - preferred column is not set. When it is present and setting a - mark position it is not used. - - Note that for '< and '> changing the line number may result in - the marks to be effectively be swapped, so that '< is always - before '>. - - Returns 0 when the position could be set, -1 otherwise. - An error message is given if {expr} is invalid. - - Also see |getpos()| and |getcurpos()|. - - This does not restore the preferred column for moving - vertically; if you set the cursor position with this, |j| and - |k| motions will jump to previous columns! Use |cursor()| to - also set the preferred column. Also see the "curswant" key in - |winrestview()|. - - Can also be used as a |method|: > - GetPosition()->setpos('.') - -setqflist({list} [, {action} [, {what}]]) *setqflist()* - Create or replace or add to the quickfix list. - - If the optional {what} dictionary argument is supplied, then - only the items listed in {what} are set. The first {list} - argument is ignored. See below for the supported items in - {what}. - *setqflist-what* - When {what} is not present, the items in {list} are used. Each - item must be a dictionary. Non-dictionary items in {list} are - ignored. Each dictionary item can contain the following - entries: - - bufnr buffer number; must be the number of a valid - buffer - filename name of a file; only used when "bufnr" is not - present or it is invalid. - module name of a module; if given it will be used in - quickfix error window instead of the filename - lnum line number in the file - pattern search pattern used to locate the error - col column number - vcol when non-zero: "col" is visual column - when zero: "col" is byte index - nr error number - text description of the error - type single-character error type, 'E', 'W', etc. - valid recognized error message - - The "col", "vcol", "nr", "type" and "text" entries are - optional. Either "lnum" or "pattern" entry can be used to - locate a matching error line. - If the "filename" and "bufnr" entries are not present or - neither the "lnum" or "pattern" entries are present, then the - item will not be handled as an error line. - If both "pattern" and "lnum" are present then "pattern" will - be used. - If the "valid" entry is not supplied, then the valid flag is - set when "bufnr" is a valid buffer or "filename" exists. - If you supply an empty {list}, the quickfix list will be - cleared. - Note that the list is not exactly the same as what - |getqflist()| returns. - - {action} values: *setqflist-action* *E927* - 'a' The items from {list} are added to the existing - quickfix list. If there is no existing list, then a - new list is created. - - 'r' The items from the current quickfix list are replaced - with the items from {list}. This can also be used to - clear the list: > - :call setqflist([], 'r') -< - 'f' All the quickfix lists in the quickfix stack are - freed. - - If {action} is not present or is set to ' ', then a new list - is created. The new quickfix list is added after the current - quickfix list in the stack and all the following lists are - freed. To add a new quickfix list at the end of the stack, - set "nr" in {what} to "$". - - The following items can be specified in dictionary {what}: - context quickfix list context. See |quickfix-context| - efm errorformat to use when parsing text from - "lines". If this is not present, then the - 'errorformat' option value is used. - See |quickfix-parse| - id quickfix list identifier |quickfix-ID| - idx index of the current entry in the quickfix - list specified by 'id' or 'nr'. If set to '$', - then the last entry in the list is set as the - current entry. See |quickfix-index| - items list of quickfix entries. Same as the {list} - argument. - lines use 'errorformat' to parse a list of lines and - add the resulting entries to the quickfix list - {nr} or {id}. Only a |List| value is supported. - See |quickfix-parse| - nr list number in the quickfix stack; zero - means the current quickfix list and "$" means - the last quickfix list. - quickfixtextfunc - function to get the text to display in the - quickfix window. The value can be the name of - a function or a funcref or a lambda. Refer to - |quickfix-window-function| for an explanation - of how to write the function and an example. - title quickfix list title text. See |quickfix-title| - Unsupported keys in {what} are ignored. - If the "nr" item is not present, then the current quickfix list - is modified. When creating a new quickfix list, "nr" can be - set to a value one greater than the quickfix stack size. - When modifying a quickfix list, to guarantee that the correct - list is modified, "id" should be used instead of "nr" to - specify the list. - - Examples (See also |setqflist-examples|): > - :call setqflist([], 'r', {'title': 'My search'}) - :call setqflist([], 'r', {'nr': 2, 'title': 'Errors'}) - :call setqflist([], 'a', {'id':qfid, 'lines':["F1:10:L10"]}) -< - Returns zero for success, -1 for failure. - - This function can be used to create a quickfix list - independent of the 'errorformat' setting. Use a command like - `:cc 1` to jump to the first position. - - Can also be used as a |method|, the base is passed as the - second argument: > - GetErrorlist()->setqflist() -< - *setreg()* -setreg({regname}, {value} [, {options}]) - Set the register {regname} to {value}. - The {regname} argument is a string. - - {value} may be any value returned by |getreg()| or - |getreginfo()|, including a |List| or |Dict|. - If {options} contains "a" or {regname} is upper case, - then the value is appended. - - {options} can also contain a register type specification: - "c" or "v" |charwise| mode - "l" or "V" |linewise| mode - "b" or "<CTRL-V>" |blockwise-visual| mode - If a number immediately follows "b" or "<CTRL-V>" then this is - used as the width of the selection - if it is not specified - then the width of the block is set to the number of characters - in the longest line (counting a <Tab> as 1 character). - If {options} contains "u" or '"', then the unnamed register is - set to point to register {regname}. - - If {options} contains no register settings, then the default - is to use character mode unless {value} ends in a <NL> for - string {value} and linewise mode for list {value}. Blockwise - mode is never selected automatically. - Returns zero for success, non-zero for failure. - - *E883* - Note: you may not use |List| containing more than one item to - set search and expression registers. Lists containing no - items act like empty strings. - - Examples: > - :call setreg(v:register, @*) - :call setreg('*', @%, 'ac') - :call setreg('a', "1\n2\n3", 'b5') - :call setreg('"', { 'points_to': 'a'}) - -< This example shows using the functions to save and restore a - register: > - :let var_a = getreginfo() - :call setreg('a', var_a) -< or: > - :let var_a = getreg('a', 1, 1) - :let var_amode = getregtype('a') - .... - :call setreg('a', var_a, var_amode) -< Note: you may not reliably restore register value - without using the third argument to |getreg()| as without it - newlines are represented as newlines AND Nul bytes are - represented as newlines as well, see |NL-used-for-Nul|. - - You can also change the type of a register by appending - nothing: > - :call setreg('a', '', 'al') - -< Can also be used as a |method|, the base is passed as the - second argument: > - GetText()->setreg('a') - -settabvar({tabnr}, {varname}, {val}) *settabvar()* - Set tab-local variable {varname} to {val} in tab page {tabnr}. - |t:var| - The {varname} argument is a string. - Note that the variable name without "t:" must be used. - Tabs are numbered starting with one. - This function is not available in the |sandbox|. - - Can also be used as a |method|, the base is passed as the - third argument: > - GetValue()->settabvar(tab, name) - -settabwinvar({tabnr}, {winnr}, {varname}, {val}) *settabwinvar()* - Set option or local variable {varname} in window {winnr} to - {val}. - Tabs are numbered starting with one. For the current tabpage - use |setwinvar()|. - {winnr} can be the window number or the |window-ID|. - When {winnr} is zero the current window is used. - This also works for a global or local buffer option, but it - doesn't work for a global or local buffer variable. - For a local buffer option the global value is unchanged. - Note that the variable name without "w:" must be used. - Examples: > - :call settabwinvar(1, 1, "&list", 0) - :call settabwinvar(3, 2, "myvar", "foobar") -< This function is not available in the |sandbox|. - - Can also be used as a |method|, the base is passed as the - fourth argument: > - GetValue()->settabwinvar(tab, winnr, name) - -settagstack({nr}, {dict} [, {action}]) *settagstack()* - Modify the tag stack of the window {nr} using {dict}. - {nr} can be the window number or the |window-ID|. - - For a list of supported items in {dict}, refer to - |gettagstack()|. "curidx" takes effect before changing the tag - stack. - *E962* - How the tag stack is modified depends on the {action} - argument: - - If {action} is not present or is set to 'r', then the tag - stack is replaced. - - If {action} is set to 'a', then new entries from {dict} are - pushed (added) onto the tag stack. - - If {action} is set to 't', then all the entries from the - current entry in the tag stack or "curidx" in {dict} are - removed and then new entries are pushed to the stack. - - The current index is set to one after the length of the tag - stack after the modification. - - Returns zero for success, -1 for failure. - - Examples (for more examples see |tagstack-examples|): - Empty the tag stack of window 3: > - call settagstack(3, {'items' : []}) - -< Save and restore the tag stack: > - let stack = gettagstack(1003) - " do something else - call settagstack(1003, stack) - unlet stack -< - Can also be used as a |method|, the base is passed as the - second argument: > - GetStack()->settagstack(winnr) - -setwinvar({nr}, {varname}, {val}) *setwinvar()* - Like |settabwinvar()| for the current tab page. - Examples: > - :call setwinvar(1, "&list", 0) - :call setwinvar(2, "myvar", "foobar") - -< Can also be used as a |method|, the base is passed as the - third argument: > - GetValue()->setwinvar(winnr, name) - -sha256({string}) *sha256()* - Returns a String with 64 hex characters, which is the SHA256 - checksum of {string}. - - Can also be used as a |method|: > - GetText()->sha256() - -shellescape({string} [, {special}]) *shellescape()* - Escape {string} for use as a shell command argument. - - On Windows when 'shellslash' is not set, encloses {string} in - double-quotes and doubles all double-quotes within {string}. - Otherwise encloses {string} in single-quotes and replaces all - "'" with "'\''". - - If {special} is a |non-zero-arg|: - - Special items such as "!", "%", "#" and "<cword>" will be - preceded by a backslash. The backslash will be removed again - by the |:!| command. - - The <NL> character is escaped. - - If 'shell' contains "csh" in the tail: - - The "!" character will be escaped. This is because csh and - tcsh use "!" for history replacement even in single-quotes. - - The <NL> character is escaped (twice if {special} is - a |non-zero-arg|). - - If 'shell' contains "fish" in the tail, the "\" character will - be escaped because in fish it is used as an escape character - inside single quotes. - - Example of use with a |:!| command: > - :exe '!dir ' . shellescape(expand('<cfile>'), 1) -< This results in a directory listing for the file under the - cursor. Example of use with |system()|: > - :call system("chmod +w -- " . shellescape(expand("%"))) -< See also |::S|. - - Can also be used as a |method|: > - GetCommand()->shellescape() - -shiftwidth([{col}]) *shiftwidth()* - Returns the effective value of 'shiftwidth'. This is the - 'shiftwidth' value unless it is zero, in which case it is the - 'tabstop' value. To be backwards compatible in indent - plugins, use this: > - if exists('*shiftwidth') - func s:sw() - return shiftwidth() - endfunc - else - func s:sw() - return &sw - endfunc - endif -< And then use s:sw() instead of &sw. - - When there is one argument {col} this is used as column number - for which to return the 'shiftwidth' value. This matters for the - 'vartabstop' feature. If no {col} argument is given, column 1 - will be assumed. - - Can also be used as a |method|: > - GetColumn()->shiftwidth() - -sign_ functions are documented here: |sign-functions-details| - -simplify({filename}) *simplify()* - Simplify the file name as much as possible without changing - the meaning. Shortcuts (on MS-Windows) or symbolic links (on - Unix) are not resolved. If the first path component in - {filename} designates the current directory, this will be - valid for the result as well. A trailing path separator is - not removed either. On Unix "//path" is unchanged, but - "///path" is simplified to "/path" (this follows the Posix - standard). - Example: > - simplify("./dir/.././/file/") == "./file/" -< Note: The combination "dir/.." is only removed if "dir" is - a searchable directory or does not exist. On Unix, it is also - removed when "dir" is a symbolic link within the same - directory. In order to resolve all the involved symbolic - links before simplifying the path name, use |resolve()|. - - Can also be used as a |method|: > - GetName()->simplify() - -sin({expr}) *sin()* - Return the sine of {expr}, measured in radians, as a |Float|. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo sin(100) -< -0.506366 > - :echo sin(-4.01) -< 0.763301 - - Can also be used as a |method|: > - Compute()->sin() - -sinh({expr}) *sinh()* - Return the hyperbolic sine of {expr} as a |Float| in the range - [-inf, inf]. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo sinh(0.5) -< 0.521095 > - :echo sinh(-0.9) -< -1.026517 - - Can also be used as a |method|: > - Compute()->sinh() - -sockconnect({mode}, {address} [, {opts}]) *sockconnect()* - Connect a socket to an address. If {mode} is "pipe" then - {address} should be the path of a named pipe. If {mode} is - "tcp" then {address} should be of the form "host:port" where - the host should be an ip adderess or host name, and port the - port number. - - Returns a |channel| ID. Close the socket with |chanclose()|. - Use |chansend()| to send data over a bytes socket, and - |rpcrequest()| and |rpcnotify()| to communicate with a RPC - socket. - - {opts} is an optional dictionary with these keys: - |on_data| : callback invoked when data was read from socket - data_buffered : read socket data in |channel-buffered| mode. - rpc : If set, |msgpack-rpc| will be used to communicate - over the socket. - Returns: - - The channel ID on success (greater than zero) - - 0 on invalid arguments or connection failure. - -sort({list} [, {func} [, {dict}]]) *sort()* *E702* - Sort the items in {list} in-place. Returns {list}. - - If you want a list to remain unmodified make a copy first: > - :let sortedlist = sort(copy(mylist)) - -< When {func} is omitted, is empty or zero, then sort() uses the - string representation of each item to sort on. Numbers sort - after Strings, |Lists| after Numbers. For sorting text in the - current buffer use |:sort|. - - When {func} is given and it is '1' or 'i' then case is - ignored. - - When {func} is given and it is 'l' then the current collation - locale is used for ordering. Implementation details: strcoll() - is used to compare strings. See |:language| check or set the - collation locale. |v:collate| can also be used to check the - current locale. Sorting using the locale typically ignores - case. Example: > - " ö is sorted similarly to o with English locale. - :language collate en_US.UTF8 - :echo sort(['n', 'o', 'O', 'ö', 'p', 'z'], 'l') -< ['n', 'o', 'O', 'ö', 'p', 'z'] ~ -> - " ö is sorted after z with Swedish locale. - :language collate sv_SE.UTF8 - :echo sort(['n', 'o', 'O', 'ö', 'p', 'z'], 'l') -< ['n', 'o', 'O', 'p', 'z', 'ö'] ~ - This does not work properly on Mac. - - When {func} is given and it is 'n' then all items will be - sorted numerical (Implementation detail: this uses the - strtod() function to parse numbers, Strings, Lists, Dicts and - Funcrefs will be considered as being 0). - - When {func} is given and it is 'N' then all items will be - sorted numerical. This is like 'n' but a string containing - digits will be used as the number they represent. - - When {func} is given and it is 'f' then all items will be - sorted numerical. All values must be a Number or a Float. - - When {func} is a |Funcref| or a function name, this function - is called to compare items. The function is invoked with two - items as argument and must return zero if they are equal, 1 or - bigger if the first one sorts after the second one, -1 or - smaller if the first one sorts before the second one. - - {dict} is for functions with the "dict" attribute. It will be - used to set the local variable "self". |Dictionary-function| - - The sort is stable, items which compare equal (as number or as - string) will keep their relative position. E.g., when sorting - on numbers, text strings will sort next to each other, in the - same order as they were originally. - - Can also be used as a |method|: > - mylist->sort() - -< Also see |uniq()|. - - Example: > - func MyCompare(i1, i2) - return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1 - endfunc - eval mylist->sort("MyCompare") -< A shorter compare version for this specific simple case, which - ignores overflow: > - func MyCompare(i1, i2) - return a:i1 - a:i2 - endfunc -< For a simple expression you can use a lambda: > - eval mylist->sort({i1, i2 -> i1 - i2}) -< - *soundfold()* -soundfold({word}) - Return the sound-folded equivalent of {word}. Uses the first - language in 'spelllang' for the current window that supports - soundfolding. 'spell' must be set. When no sound folding is - possible the {word} is returned unmodified. - This can be used for making spelling suggestions. Note that - the method can be quite slow. - - Can also be used as a |method|: > - GetWord()->soundfold() -< - *spellbadword()* -spellbadword([{sentence}]) - Without argument: The result is the badly spelled word under - or after the cursor. The cursor is moved to the start of the - bad word. When no bad word is found in the cursor line the - result is an empty string and the cursor doesn't move. - - With argument: The result is the first word in {sentence} that - is badly spelled. If there are no spelling mistakes the - result is an empty string. - - The return value is a list with two items: - - The badly spelled word or an empty string. - - The type of the spelling error: - "bad" spelling mistake - "rare" rare word - "local" word only valid in another region - "caps" word should start with Capital - Example: > - echo spellbadword("the quik brown fox") -< ['quik', 'bad'] ~ - - The spelling information for the current window and the value - of 'spelllang' are used. - - Can also be used as a |method|: > - GetText()->spellbadword() -< - *spellsuggest()* -spellsuggest({word} [, {max} [, {capital}]]) - Return a |List| with spelling suggestions to replace {word}. - When {max} is given up to this number of suggestions are - returned. Otherwise up to 25 suggestions are returned. - - When the {capital} argument is given and it's non-zero only - suggestions with a leading capital will be given. Use this - after a match with 'spellcapcheck'. - - {word} can be a badly spelled word followed by other text. - This allows for joining two words that were split. The - suggestions also include the following text, thus you can - replace a line. - - {word} may also be a good word. Similar words will then be - returned. {word} itself is not included in the suggestions, - although it may appear capitalized. - - The spelling information for the current window is used. The - values of 'spelllang' and 'spellsuggest' are used. - - Can also be used as a |method|: > - GetWord()->spellsuggest() - -split({string} [, {pattern} [, {keepempty}]]) *split()* - Make a |List| out of {string}. When {pattern} is omitted or - empty each white-separated sequence of characters becomes an - item. - Otherwise the string is split where {pattern} matches, - removing the matched characters. 'ignorecase' is not used - here, add \c to ignore case. |/\c| - When the first or last item is empty it is omitted, unless the - {keepempty} argument is given and it's non-zero. - Other empty items are kept when {pattern} matches at least one - character or when {keepempty} is non-zero. - Example: > - :let words = split(getline('.'), '\W\+') -< To split a string in individual characters: > - :for c in split(mystring, '\zs') -< If you want to keep the separator you can also use '\zs' at - the end of the pattern: > - :echo split('abc:def:ghi', ':\zs') -< ['abc:', 'def:', 'ghi'] ~ - Splitting a table where the first element can be empty: > - :let items = split(line, ':', 1) -< The opposite function is |join()|. - - Can also be used as a |method|: > - GetString()->split() - -sqrt({expr}) *sqrt()* - Return the non-negative square root of Float {expr} as a - |Float|. - {expr} must evaluate to a |Float| or a |Number|. When {expr} - is negative the result is NaN (Not a Number). - Examples: > - :echo sqrt(100) -< 10.0 > - :echo sqrt(-4.01) -< nan - "nan" may be different, it depends on system libraries. - - Can also be used as a |method|: > - Compute()->sqrt() - -stdioopen({opts}) *stdioopen()* - With |--headless| this opens stdin and stdout as a |channel|. - May be called only once. See |channel-stdio|. stderr is not - handled by this function, see |v:stderr|. - - Close the stdio handles with |chanclose()|. Use |chansend()| - to send data to stdout, and |rpcrequest()| and |rpcnotify()| - to communicate over RPC. - - {opts} is a dictionary with these keys: - |on_stdin| : callback invoked when stdin is written to. - stdin_buffered : read stdin in |channel-buffered| mode. - rpc : If set, |msgpack-rpc| will be used to communicate - over stdio - Returns: - - |channel-id| on success (value is always 1) - - 0 on invalid arguments - - -stdpath({what}) *stdpath()* *E6100* - Returns |standard-path| locations of various default files and - directories. - - {what} Type Description ~ - cache String Cache directory. Arbitrary temporary - storage for plugins, etc. - config String User configuration directory. The - |init.vim| is stored here. - config_dirs List Additional configuration directories. - data String User data directory. The |shada-file| - is stored here. - data_dirs List Additional data directories. - - Example: > - :echo stdpath("config") - - -str2float({string} [, {quoted}]) *str2float()* - Convert String {string} to a Float. This mostly works the - same as when using a floating point number in an expression, - see |floating-point-format|. But it's a bit more permissive. - E.g., "1e40" is accepted, while in an expression you need to - write "1.0e40". The hexadecimal form "0x123" is also - accepted, but not others, like binary or octal. - When {quoted} is present and non-zero then embedded single - quotes before the dot are ignored, thus "1'000.0" is a - thousand. - Text after the number is silently ignored. - The decimal point is always '.', no matter what the locale is - set to. A comma ends the number: "12,345.67" is converted to - 12.0. You can strip out thousands separators with - |substitute()|: > - let f = str2float(substitute(text, ',', '', 'g')) -< - Can also be used as a |method|: > - let f = text->substitute(',', '', 'g')->str2float() - -str2list({string} [, {utf8}]) *str2list()* - Return a list containing the number values which represent - each character in String {string}. Examples: > - str2list(" ") returns [32] - str2list("ABC") returns [65, 66, 67] -< |list2str()| does the opposite. - - UTF-8 encoding is always used, {utf8} option has no effect, - and exists only for backwards-compatibility. - With UTF-8 composing characters are handled properly: > - str2list("á") returns [97, 769] - -< Can also be used as a |method|: > - GetString()->str2list() - -str2nr({string} [, {base}]) *str2nr()* - Convert string {string} to a number. - {base} is the conversion base, it can be 2, 8, 10 or 16. - When {quoted} is present and non-zero then embedded single - quotes are ignored, thus "1'000'000" is a million. - - When {base} is omitted base 10 is used. This also means that - a leading zero doesn't cause octal conversion to be used, as - with the default String to Number conversion. Example: > - let nr = str2nr('0123') -< - When {base} is 16 a leading "0x" or "0X" is ignored. With a - different base the result will be zero. Similarly, when - {base} is 8 a leading "0", "0o" or "0O" is ignored, and when - {base} is 2 a leading "0b" or "0B" is ignored. - Text after the number is silently ignored. - - Can also be used as a |method|: > - GetText()->str2nr() - -strcharpart({src}, {start} [, {len}]) *strcharpart()* - Like |strpart()| but using character index and length instead - of byte index and length. Composing characters are counted - separately. - When a character index is used where a character does not - exist it is assumed to be one character. For example: > - strcharpart('abc', -1, 2) -< results in 'a'. - - Can also be used as a |method|: > - GetText()->strcharpart(5) - -strchars({string} [, {skipcc}]) *strchars()* - The result is a Number, which is the number of characters - in String {string}. - When {skipcc} is omitted or zero, composing characters are - counted separately. - When {skipcc} set to 1, Composing characters are ignored. - Also see |strlen()|, |strdisplaywidth()| and |strwidth()|. - - {skipcc} is only available after 7.4.755. For backward - compatibility, you can define a wrapper function: > - if has("patch-7.4.755") - function s:strchars(str, skipcc) - return strchars(a:str, a:skipcc) - endfunction - else - function s:strchars(str, skipcc) - if a:skipcc - return strlen(substitute(a:str, ".", "x", "g")) - else - return strchars(a:str) - endif - endfunction - endif -< - Can also be used as a |method|: > - GetText()->strchars() - -strdisplaywidth({string} [, {col}]) *strdisplaywidth()* - The result is a Number, which is the number of display cells - String {string} occupies on the screen when it starts at {col} - (first column is zero). When {col} is omitted zero is used. - Otherwise it is the screen column where to start. This - matters for Tab characters. - The option settings of the current window are used. This - matters for anything that's displayed differently, such as - 'tabstop' and 'display'. - When {string} contains characters with East Asian Width Class - Ambiguous, this function's return value depends on 'ambiwidth'. - Also see |strlen()|, |strwidth()| and |strchars()|. - - Can also be used as a |method|: > - GetText()->strdisplaywidth() - -strftime({format} [, {time}]) *strftime()* - The result is a String, which is a formatted date and time, as - specified by the {format} string. The given {time} is used, - or the current time if no time is given. The accepted - {format} depends on your system, thus this is not portable! - See the manual page of the C function strftime() for the - format. The maximum length of the result is 80 characters. - See also |localtime()|, |getftime()| and |strptime()|. - The language can be changed with the |:language| command. - Examples: > - :echo strftime("%c") Sun Apr 27 11:49:23 1997 - :echo strftime("%Y %b %d %X") 1997 Apr 27 11:53:25 - :echo strftime("%y%m%d %T") 970427 11:53:55 - :echo strftime("%H:%M") 11:55 - :echo strftime("%c", getftime("file.c")) - Show mod time of file.c. - -< Can also be used as a |method|: > - GetFormat()->strftime() - -strgetchar({str}, {index}) *strgetchar()* - Get character {index} from {str}. This uses a character - index, not a byte index. Composing characters are considered - separate characters here. - Also see |strcharpart()| and |strchars()|. - - Can also be used as a |method|: > - GetText()->strgetchar(5) - -stridx({haystack}, {needle} [, {start}]) *stridx()* - The result is a Number, which gives the byte index in - {haystack} of the first occurrence of the String {needle}. - If {start} is specified, the search starts at index {start}. - This can be used to find a second match: > - :let colon1 = stridx(line, ":") - :let colon2 = stridx(line, ":", colon1 + 1) -< The search is done case-sensitive. - For pattern searches use |match()|. - -1 is returned if the {needle} does not occur in {haystack}. - See also |strridx()|. - Examples: > - :echo stridx("An Example", "Example") 3 - :echo stridx("Starting point", "Start") 0 - :echo stridx("Starting point", "start") -1 -< *strstr()* *strchr()* - stridx() works similar to the C function strstr(). When used - with a single character it works similar to strchr(). - - Can also be used as a |method|: > - GetHaystack()->stridx(needle) - - *string()* -string({expr}) Return {expr} converted to a String. If {expr} is a Number, - Float, String, Blob or a composition of them, then the result - can be parsed back with |eval()|. - {expr} type result ~ - String 'string' - Number 123 - Float 123.123456 or 1.123456e8 or - `str2float('inf')` - Funcref `function('name')` - Blob 0z00112233.44556677.8899 - List [item, item] - Dictionary {key: value, key: value} - Note that in String values the ' character is doubled. - Also see |strtrans()|. - Note 2: Output format is mostly compatible with YAML, except - for infinite and NaN floating-point values representations - which use |str2float()|. Strings are also dumped literally, - only single quote is escaped, which does not allow using YAML - for parsing back binary strings. |eval()| should always work for - strings and floats though and this is the only official - method, use |msgpackdump()| or |json_encode()| if you need to - share data with other application. - - Can also be used as a |method|: > - mylist->string() - -strlen({string}) *strlen()* - The result is a Number, which is the length of the String - {string} in bytes. - If the argument is a Number it is first converted to a String. - For other types an error is given. - If you want to count the number of multibyte characters use - |strchars()|. - Also see |len()|, |strdisplaywidth()| and |strwidth()|. - - Can also be used as a |method|: > - GetString()->strlen() - -strpart({src}, {start} [, {len} [, {chars}]]) *strpart()* - The result is a String, which is part of {src}, starting from - byte {start}, with the byte length {len}. - When {chars} is present and TRUE then {len} is the number of - characters positions (composing characters are not counted - separately, thus "1" means one base character and any - following composing characters). - To count {start} as characters instead of bytes use - |strcharpart()|. - - When bytes are selected which do not exist, this doesn't - result in an error, the bytes are simply omitted. - If {len} is missing, the copy continues from {start} till the - end of the {src}. > - strpart("abcdefg", 3, 2) == "de" - strpart("abcdefg", -2, 4) == "ab" - strpart("abcdefg", 5, 4) == "fg" - strpart("abcdefg", 3) == "defg" - -< Note: To get the first character, {start} must be 0. For - example, to get the character under the cursor: > - strpart(getline("."), col(".") - 1, 1, v:true) -< - Can also be used as a |method|: > - GetText()->strpart(5) - -strptime({format}, {timestring}) *strptime()* - The result is a Number, which is a unix timestamp representing - the date and time in {timestring}, which is expected to match - the format specified in {format}. - - The accepted {format} depends on your system, thus this is not - portable! See the manual page of the C function strptime() - for the format. Especially avoid "%c". The value of $TZ also - matters. - - If the {timestring} cannot be parsed with {format} zero is - returned. If you do not know the format of {timestring} you - can try different {format} values until you get a non-zero - result. - - See also |strftime()|. - Examples: > - :echo strptime("%Y %b %d %X", "1997 Apr 27 11:49:23") -< 862156163 > - :echo strftime("%c", strptime("%y%m%d %T", "970427 11:53:55")) -< Sun Apr 27 11:53:55 1997 > - :echo strftime("%c", strptime("%Y%m%d%H%M%S", "19970427115355") + 3600) -< Sun Apr 27 12:53:55 1997 - - Can also be used as a |method|: > - GetFormat()->strptime(timestring) -< -strridx({haystack}, {needle} [, {start}]) *strridx()* - The result is a Number, which gives the byte index in - {haystack} of the last occurrence of the String {needle}. - When {start} is specified, matches beyond this index are - ignored. This can be used to find a match before a previous - match: > - :let lastcomma = strridx(line, ",") - :let comma2 = strridx(line, ",", lastcomma - 1) -< The search is done case-sensitive. - For pattern searches use |match()|. - -1 is returned if the {needle} does not occur in {haystack}. - If the {needle} is empty the length of {haystack} is returned. - See also |stridx()|. Examples: > - :echo strridx("an angry armadillo", "an") 3 -< *strrchr()* - When used with a single character it works similar to the C - function strrchr(). - - Can also be used as a |method|: > - GetHaystack()->strridx(needle) - -strtrans({string}) *strtrans()* - The result is a String, which is {string} with all unprintable - characters translated into printable characters |'isprint'|. - Like they are shown in a window. Example: > - echo strtrans(@a) -< This displays a newline in register a as "^@" instead of - starting a new line. - - Can also be used as a |method|: > - GetString()->strtrans() - -strwidth({string}) *strwidth()* - The result is a Number, which is the number of display cells - String {string} occupies. A Tab character is counted as one - cell, alternatively use |strdisplaywidth()|. - When {string} contains characters with East Asian Width Class - Ambiguous, this function's return value depends on 'ambiwidth'. - Also see |strlen()|, |strdisplaywidth()| and |strchars()|. - - Can also be used as a |method|: > - GetString()->strwidth() - -submatch({nr} [, {list}]) *submatch()* *E935* - Only for an expression in a |:substitute| command or - substitute() function. - Returns the {nr}'th submatch of the matched text. When {nr} - is 0 the whole matched text is returned. - Note that a NL in the string can stand for a line break of a - multi-line match or a NUL character in the text. - Also see |sub-replace-expression|. - - If {list} is present and non-zero then submatch() returns - a list of strings, similar to |getline()| with two arguments. - NL characters in the text represent NUL characters in the - text. - Only returns more than one item for |:substitute|, inside - |substitute()| this list will always contain one or zero - items, since there are no real line breaks. - - When substitute() is used recursively only the submatches in - the current (deepest) call can be obtained. - - Examples: > - :s/\d\+/\=submatch(0) + 1/ - :echo substitute(text, '\d\+', '\=submatch(0) + 1', '') -< This finds the first number in the line and adds one to it. - A line break is included as a newline character. - - Can also be used as a |method|: > - GetNr()->submatch() - -substitute({string}, {pat}, {sub}, {flags}) *substitute()* - The result is a String, which is a copy of {string}, in which - the first match of {pat} is replaced with {sub}. - When {flags} is "g", all matches of {pat} in {string} are - replaced. Otherwise {flags} should be "". - - This works like the ":substitute" command (without any flags). - But the matching with {pat} is always done like the 'magic' - option is set and 'cpoptions' is empty (to make scripts - portable). 'ignorecase' is still relevant, use |/\c| or |/\C| - if you want to ignore or match case and ignore 'ignorecase'. - 'smartcase' is not used. See |string-match| for how {pat} is - used. - - A "~" in {sub} is not replaced with the previous {sub}. - Note that some codes in {sub} have a special meaning - |sub-replace-special|. For example, to replace something with - "\n" (two characters), use "\\\\n" or '\\n'. - - When {pat} does not match in {string}, {string} is returned - unmodified. - - Example: > - :let &path = substitute(&path, ",\\=[^,]*$", "", "") -< This removes the last component of the 'path' option. > - :echo substitute("testing", ".*", "\\U\\0", "") -< results in "TESTING". - - When {sub} starts with "\=", the remainder is interpreted as - an expression. See |sub-replace-expression|. Example: > - :echo substitute(s, '%\(\x\x\)', - \ '\=nr2char("0x" . submatch(1))', 'g') - -< When {sub} is a Funcref that function is called, with one - optional argument. Example: > - :echo substitute(s, '%\(\x\x\)', SubNr, 'g') -< The optional argument is a list which contains the whole - matched string and up to nine submatches, like what - |submatch()| returns. Example: > - :echo substitute(s, '%\(\x\x\)', {m -> '0x' . m[1]}, 'g') - -< Can also be used as a |method|: > - GetString()->substitute(pat, sub, flags) - -swapinfo({fname}) *swapinfo()* - The result is a dictionary, which holds information about the - swapfile {fname}. The available fields are: - version VIM version - user user name - host host name - fname original file name - pid PID of the VIM process that created the swap - file - mtime last modification time in seconds - inode Optional: INODE number of the file - dirty 1 if file was modified, 0 if not - In case of failure an "error" item is added with the reason: - Cannot open file: file not found or in accessible - Cannot read file: cannot read first block - Not a swap file: does not contain correct block ID - Magic number mismatch: Info in first block is invalid - - Can also be used as a |method|: > - GetFilename()->swapinfo() - -swapname({buf}) *swapname()* - The result is the swap file path of the buffer {buf}. - For the use of {buf}, see |bufname()| above. - If buffer {buf} is the current buffer, the result is equal to - |:swapname| (unless there is no swap file). - If buffer {buf} has no swap file, returns an empty string. - - Can also be used as a |method|: > - GetBufname()->swapname() - -synID({lnum}, {col}, {trans}) *synID()* - The result is a Number, which is the syntax ID at the position - {lnum} and {col} in the current window. - The syntax ID can be used with |synIDattr()| and - |synIDtrans()| to obtain syntax information about text. - - {col} is 1 for the leftmost column, {lnum} is 1 for the first - line. 'synmaxcol' applies, in a longer line zero is returned. - Note that when the position is after the last character, - that's where the cursor can be in Insert mode, synID() returns - zero. {lnum} is used like with |getline()|. - - When {trans} is |TRUE|, transparent items are reduced to the - item that they reveal. This is useful when wanting to know - the effective color. When {trans} is |FALSE|, the transparent - item is returned. This is useful when wanting to know which - syntax item is effective (e.g. inside parens). - Warning: This function can be very slow. Best speed is - obtained by going through the file in forward direction. - - Example (echoes the name of the syntax item under the cursor): > - :echo synIDattr(synID(line("."), col("."), 1), "name") -< - -synIDattr({synID}, {what} [, {mode}]) *synIDattr()* - The result is a String, which is the {what} attribute of - syntax ID {synID}. This can be used to obtain information - about a syntax item. - {mode} can be "gui", "cterm" or "term", to get the attributes - for that mode. When {mode} is omitted, or an invalid value is - used, the attributes for the currently active highlighting are - used (GUI, cterm or term). - Use synIDtrans() to follow linked highlight groups. - {what} result - "name" the name of the syntax item - "fg" foreground color (GUI: color name used to set - the color, cterm: color number as a string, - term: empty string) - "bg" background color (as with "fg") - "font" font name (only available in the GUI) - |highlight-font| - "sp" special color (as with "fg") |highlight-guisp| - "fg#" like "fg", but for the GUI and the GUI is - running the name in "#RRGGBB" form - "bg#" like "fg#" for "bg" - "sp#" like "fg#" for "sp" - "bold" "1" if bold - "italic" "1" if italic - "reverse" "1" if reverse - "inverse" "1" if inverse (= reverse) - "standout" "1" if standout - "underline" "1" if underlined - "undercurl" "1" if undercurled - "strikethrough" "1" if struckthrough - - Example (echoes the color of the syntax item under the - cursor): > - :echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg") -< - Can also be used as a |method|: > - :echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg") - -synIDtrans({synID}) *synIDtrans()* - The result is a Number, which is the translated syntax ID of - {synID}. This is the syntax group ID of what is being used to - highlight the character. Highlight links given with - ":highlight link" are followed. - - Can also be used as a |method|: > - :echo synID(line("."), col("."), 1)->synIDtrans()->synIDattr("fg") - -synconcealed({lnum}, {col}) *synconcealed()* - The result is a |List| with currently three items: - 1. The first item in the list is 0 if the character at the - position {lnum} and {col} is not part of a concealable - region, 1 if it is. {lnum} is used like with |getline()|. - 2. The second item in the list is a string. If the first item - is 1, the second item contains the text which will be - displayed in place of the concealed text, depending on the - current setting of 'conceallevel' and 'listchars'. - 3. The third and final item in the list is a number - representing the specific syntax region matched in the - line. When the character is not concealed the value is - zero. This allows detection of the beginning of a new - concealable region if there are two consecutive regions - with the same replacement character. For an example, if - the text is "123456" and both "23" and "45" are concealed - and replaced by the character "X", then: - call returns ~ - synconcealed(lnum, 1) [0, '', 0] - synconcealed(lnum, 2) [1, 'X', 1] - synconcealed(lnum, 3) [1, 'X', 1] - synconcealed(lnum, 4) [1, 'X', 2] - synconcealed(lnum, 5) [1, 'X', 2] - synconcealed(lnum, 6) [0, '', 0] - - -synstack({lnum}, {col}) *synstack()* - Return a |List|, which is the stack of syntax items at the - position {lnum} and {col} in the current window. {lnum} is - used like with |getline()|. Each item in the List is an ID - like what |synID()| returns. - The first item in the List is the outer region, following are - items contained in that one. The last one is what |synID()| - returns, unless not the whole item is highlighted or it is a - transparent item. - This function is useful for debugging a syntax file. - Example that shows the syntax stack under the cursor: > - for id in synstack(line("."), col(".")) - echo synIDattr(id, "name") - endfor -< When the position specified with {lnum} and {col} is invalid - nothing is returned. The position just after the last - character in a line and the first column in an empty line are - valid positions. - -system({cmd} [, {input}]) *system()* *E677* - Gets the output of {cmd} as a |string| (|systemlist()| returns - a |List|) and sets |v:shell_error| to the error code. - {cmd} is treated as in |jobstart()|: - If {cmd} is a List it runs directly (no 'shell'). - If {cmd} is a String it runs in the 'shell', like this: > - :call jobstart(split(&shell) + split(&shellcmdflag) + ['{cmd}']) - -< Not to be used for interactive commands. - - Result is a String, filtered to avoid platform-specific quirks: - - <CR><NL> is replaced with <NL> - - NUL characters are replaced with SOH (0x01) - - Example: > - :echo system(['ls', expand('%:h')]) - -< If {input} is a string it is written to a pipe and passed as - stdin to the command. The string is written as-is, line - separators are not changed. - If {input} is a |List| it is written to the pipe as - |writefile()| does with {binary} set to "b" (i.e. with - a newline between each list item, and newlines inside list - items converted to NULs). - When {input} is given and is a valid buffer id, the content of - the buffer is written to the file line by line, each line - terminated by NL (and NUL where the text has NL). - *E5677* - Note: system() cannot write to or read from backgrounded ("&") - shell commands, e.g.: > - :echo system("cat - &", "foo") -< which is equivalent to: > - $ echo foo | bash -c 'cat - &' -< The pipes are disconnected (unless overridden by shell - redirection syntax) before input can reach it. Use - |jobstart()| instead. - - Note: Use |shellescape()| or |::S| with |expand()| or - |fnamemodify()| to escape special characters in a command - argument. 'shellquote' and 'shellxquote' must be properly - configured. Example: > - :echo system('ls '..shellescape(expand('%:h'))) - :echo system('ls '..expand('%:h:S')) - -< Unlike ":!cmd" there is no automatic check for changed files. - Use |:checktime| to force a check. - - Can also be used as a |method|: > - :echo GetCmd()->system() - -systemlist({cmd} [, {input} [, {keepempty}]]) *systemlist()* - Same as |system()|, but returns a |List| with lines (parts of - output separated by NL) with NULs transformed into NLs. Output - is the same as |readfile()| will output with {binary} argument - set to "b", except that a final newline is not preserved, - unless {keepempty} is non-zero. - Note that on MS-Windows you may get trailing CR characters. - - To see the difference between "echo hello" and "echo -n hello" - use |system()| and |split()|: > - echo split(system('echo hello'), '\n', 1) -< - Returns an empty string on error. - - Can also be used as a |method|: > - :echo GetCmd()->systemlist() - -tabpagebuflist([{arg}]) *tabpagebuflist()* - The result is a |List|, where each item is the number of the - buffer associated with each window in the current tab page. - {arg} specifies the number of the tab page to be used. When - omitted the current tab page is used. - When {arg} is invalid the number zero is returned. - To get a list of all buffers in all tabs use this: > - let buflist = [] - for i in range(tabpagenr('$')) - call extend(buflist, tabpagebuflist(i + 1)) - endfor -< Note that a buffer may appear in more than one window. - - Can also be used as a |method|: > - GetTabpage()->tabpagebuflist() - -tabpagenr([{arg}]) *tabpagenr()* - The result is a Number, which is the number of the current - tab page. The first tab page has number 1. - The optional argument {arg} supports the following values: - $ the number of the last tab page (the tab page - count). - # the number of the last accessed tab page (where - |g<Tab>| goes to). If there is no previous - tab page, 0 is returned. - The number can be used with the |:tab| command. - - -tabpagewinnr({tabarg} [, {arg}]) *tabpagewinnr()* - Like |winnr()| but for tab page {tabarg}. - {tabarg} specifies the number of tab page to be used. - {arg} is used like with |winnr()|: - - When omitted the current window number is returned. This is - the window which will be used when going to this tab page. - - When "$" the number of windows is returned. - - When "#" the previous window nr is returned. - Useful examples: > - tabpagewinnr(1) " current window of tab page 1 - tabpagewinnr(4, '$') " number of windows in tab page 4 -< When {tabarg} is invalid zero is returned. - - Can also be used as a |method|: > - GetTabpage()->tabpagewinnr() -< - *tagfiles()* -tagfiles() Returns a |List| with the file names used to search for tags - for the current buffer. This is the 'tags' option expanded. - - -taglist({expr} [, {filename}]) *taglist()* - Returns a |List| of tags matching the regular expression {expr}. - - If {filename} is passed it is used to prioritize the results - in the same way that |:tselect| does. See |tag-priority|. - {filename} should be the full path of the file. - - Each list item is a dictionary with at least the following - entries: - name Name of the tag. - filename Name of the file where the tag is - defined. It is either relative to the - current directory or a full path. - cmd Ex command used to locate the tag in - the file. - kind Type of the tag. The value for this - entry depends on the language specific - kind values. Only available when - using a tags file generated by - Exuberant ctags or hdrtag. - static A file specific tag. Refer to - |static-tag| for more information. - More entries may be present, depending on the content of the - tags file: access, implementation, inherits and signature. - Refer to the ctags documentation for information about these - fields. For C code the fields "struct", "class" and "enum" - may appear, they give the name of the entity the tag is - contained in. - - The ex-command "cmd" can be either an ex search pattern, a - line number or a line number followed by a byte number. - - If there are no matching tags, then an empty list is returned. - - To get an exact tag match, the anchors '^' and '$' should be - used in {expr}. This also make the function work faster. - Refer to |tag-regexp| for more information about the tag - search regular expression pattern. - - Refer to |'tags'| for information about how the tags file is - located by Vim. Refer to |tags-file-format| for the format of - the tags file generated by the different ctags tools. - - Can also be used as a |method|: > - GetTagpattern()->taglist() - -tempname() *tempname()* *temp-file-name* - The result is a String, which is the name of a file that - doesn't exist. It can be used for a temporary file. Example: > - :let tmpfile = tempname() - :exe "redir > " . tmpfile -< For Unix, the file will be in a private directory |tempfile|. - For MS-Windows forward slashes are used when the 'shellslash' - option is set or when 'shellcmdflag' starts with '-'. - -termopen({cmd}[, {opts}]) *termopen()* - Spawns {cmd} in a new pseudo-terminal session connected - to the current buffer. {cmd} is the same as the one passed to - |jobstart()|. This function fails if the current buffer is - modified (all buffer contents are destroyed). - - The {opts} dict is similar to the one passed to |jobstart()|, - but the `pty`, `width`, `height`, and `TERM` fields are - ignored: `height`/`width` are taken from the current window - and `$TERM` is set to "xterm-256color". - Returns the same values as |jobstart()|. - - See |terminal| for more information. - -test_ functions are documented here: |test-functions-details| - -tan({expr}) *tan()* - Return the tangent of {expr}, measured in radians, as a |Float| - in the range [-inf, inf]. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo tan(10) -< 0.648361 > - :echo tan(-4.01) -< -1.181502 - - Can also be used as a |method|: > - Compute()->tan() - -tanh({expr}) *tanh()* - Return the hyperbolic tangent of {expr} as a |Float| in the - range [-1, 1]. - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - :echo tanh(0.5) -< 0.462117 > - :echo tanh(-1) -< -0.761594 - - Can also be used as a |method|: > - Compute()->tanh() -< - *timer_info()* -timer_info([{id}]) - Return a list with information about timers. - When {id} is given only information about this timer is - returned. When timer {id} does not exist an empty list is - returned. - When {id} is omitted information about all timers is returned. - - For each timer the information is stored in a |Dictionary| with - these items: - "id" the timer ID - "time" time the timer was started with - "repeat" number of times the timer will still fire; - -1 means forever - "callback" the callback - - Can also be used as a |method|: > - GetTimer()->timer_info() -< -timer_pause({timer}, {paused}) *timer_pause()* - Pause or unpause a timer. A paused timer does not invoke its - callback when its time expires. Unpausing a timer may cause - the callback to be invoked almost immediately if enough time - has passed. - - Pausing a timer is useful to avoid the callback to be called - for a short time. - - If {paused} evaluates to a non-zero Number or a non-empty - String, then the timer is paused, otherwise it is unpaused. - See |non-zero-arg|. - - Can also be used as a |method|: > - GetTimer()->timer_pause(1) -< - *timer_start()* *timer* *timers* -timer_start({time}, {callback} [, {options}]) - Create a timer and return the timer ID. - - {time} is the waiting time in milliseconds. This is the - minimum time before invoking the callback. When the system is - busy or Vim is not waiting for input the time will be longer. - - {callback} is the function to call. It can be the name of a - function or a |Funcref|. It is called with one argument, which - is the timer ID. The callback is only invoked when Vim is - waiting for input. - - {options} is a dictionary. Supported entries: - "repeat" Number of times to repeat the callback. - -1 means forever. Default is 1. - If the timer causes an error three times in a - row the repeat is cancelled. - - Example: > - func MyHandler(timer) - echo 'Handler called' - endfunc - let timer = timer_start(500, 'MyHandler', - \ {'repeat': 3}) -< This invokes MyHandler() three times at 500 msec intervals. - - Can also be used as a |method|: > - GetMsec()->timer_start(callback) - -< Not available in the |sandbox|. - -timer_stop({timer}) *timer_stop()* - Stop a timer. The timer callback will no longer be invoked. - {timer} is an ID returned by timer_start(), thus it must be a - Number. If {timer} does not exist there is no error. - - Can also be used as a |method|: > - GetTimer()->timer_stop() -< -timer_stopall() *timer_stopall()* - Stop all timers. The timer callbacks will no longer be - invoked. Useful if some timers is misbehaving. If there are - no timers there is no error. - -tolower({expr}) *tolower()* - The result is a copy of the String given, with all uppercase - characters turned into lowercase (just like applying |gu| to - the string). - - Can also be used as a |method|: > - GetText()->tolower() - -toupper({expr}) *toupper()* - The result is a copy of the String given, with all lowercase - characters turned into uppercase (just like applying |gU| to - the string). - - Can also be used as a |method|: > - GetText()->toupper() - -tr({src}, {fromstr}, {tostr}) *tr()* - The result is a copy of the {src} string with all characters - which appear in {fromstr} replaced by the character in that - position in the {tostr} string. Thus the first character in - {fromstr} is translated into the first character in {tostr} - and so on. Exactly like the unix "tr" command. - This code also deals with multibyte characters properly. - - Examples: > - echo tr("hello there", "ht", "HT") -< returns "Hello THere" > - echo tr("<blob>", "<>", "{}") -< returns "{blob}" - - Can also be used as a |method|: > - GetText()->tr(from, to) - -trim({text} [, {mask} [, {dir}]]) *trim()* - Return {text} as a String where any character in {mask} is - removed from the beginning and/or end of {text}. - If {mask} is not given, {mask} is all characters up to 0x20, - which includes Tab, space, NL and CR, plus the non-breaking - space character 0xa0. - The optional {dir} argument specifies where to remove the - characters: - 0 remove from the beginning and end of {text} - 1 remove only at the beginning of {text} - 2 remove only at the end of {text} - When omitted both ends are trimmed. - This function deals with multibyte characters properly. - Examples: > - echo trim(" some text ") -< returns "some text" > - echo trim(" \r\t\t\r RESERVE \t\n\x0B\xA0") . "_TAIL" -< returns "RESERVE_TAIL" > - echo trim("rm<Xrm<>X>rrm", "rm<>") -< returns "Xrm<>X" (characters in the middle are not removed) > - echo trim(" vim ", " ", 2) -< returns " vim" - - Can also be used as a |method|: > - GetText()->trim() - -trunc({expr}) *trunc()* - Return the largest integral value with magnitude less than or - equal to {expr} as a |Float| (truncate towards zero). - {expr} must evaluate to a |Float| or a |Number|. - Examples: > - echo trunc(1.456) -< 1.0 > - echo trunc(-5.456) -< -5.0 > - echo trunc(4.0) -< 4.0 - - Can also be used as a |method|: > - Compute()->trunc() - -type({expr}) *type()* - The result is a Number representing the type of {expr}. - Instead of using the number directly, it is better to use the - v:t_ variable that has the value: - Number: 0 (|v:t_number|) - String: 1 (|v:t_string|) - Funcref: 2 (|v:t_func|) - List: 3 (|v:t_list|) - Dictionary: 4 (|v:t_dict|) - Float: 5 (|v:t_float|) - Boolean: 6 (|v:true| and |v:false|) - Null: 7 (|v:null|) - Blob: 10 (|v:t_blob|) - For backward compatibility, this method can be used: > - :if type(myvar) == type(0) - :if type(myvar) == type("") - :if type(myvar) == type(function("tr")) - :if type(myvar) == type([]) - :if type(myvar) == type({}) - :if type(myvar) == type(0.0) - :if type(myvar) == type(v:true) -< In place of checking for |v:null| type it is better to check - for |v:null| directly as it is the only value of this type: > - :if myvar is v:null -< To check if the v:t_ variables exist use this: > - :if exists('v:t_number') - -< Can also be used as a |method|: > - mylist->type() - -undofile({name}) *undofile()* - Return the name of the undo file that would be used for a file - with name {name} when writing. This uses the 'undodir' - option, finding directories that exist. It does not check if - the undo file exists. - {name} is always expanded to the full path, since that is what - is used internally. - If {name} is empty undofile() returns an empty string, since a - buffer without a file name will not write an undo file. - Useful in combination with |:wundo| and |:rundo|. - - Can also be used as a |method|: > - GetFilename()->undofile() - -undotree() *undotree()* - Return the current state of the undo tree in a dictionary with - the following items: - "seq_last" The highest undo sequence number used. - "seq_cur" The sequence number of the current position in - the undo tree. This differs from "seq_last" - when some changes were undone. - "time_cur" Time last used for |:earlier| and related - commands. Use |strftime()| to convert to - something readable. - "save_last" Number of the last file write. Zero when no - write yet. - "save_cur" Number of the current position in the undo - tree. - "synced" Non-zero when the last undo block was synced. - This happens when waiting from input from the - user. See |undo-blocks|. - "entries" A list of dictionaries with information about - undo blocks. - - The first item in the "entries" list is the oldest undo item. - Each List item is a |Dictionary| with these items: - "seq" Undo sequence number. Same as what appears in - |:undolist|. - "time" Timestamp when the change happened. Use - |strftime()| to convert to something readable. - "newhead" Only appears in the item that is the last one - that was added. This marks the last change - and where further changes will be added. - "curhead" Only appears in the item that is the last one - that was undone. This marks the current - position in the undo tree, the block that will - be used by a redo command. When nothing was - undone after the last change this item will - not appear anywhere. - "save" Only appears on the last block before a file - write. The number is the write count. The - first write has number 1, the last one the - "save_last" mentioned above. - "alt" Alternate entry. This is again a List of undo - blocks. Each item may again have an "alt" - item. - -uniq({list} [, {func} [, {dict}]]) *uniq()* *E882* - Remove second and succeeding copies of repeated adjacent - {list} items in-place. Returns {list}. If you want a list - to remain unmodified make a copy first: > - :let newlist = uniq(copy(mylist)) -< The default compare function uses the string representation of - each item. For the use of {func} and {dict} see |sort()|. - - Can also be used as a |method|: > - mylist->uniq() - -values({dict}) *values()* - Return a |List| with all the values of {dict}. The |List| is - in arbitrary order. Also see |items()| and |keys()|. - - Can also be used as a |method|: > - mydict->values() - -virtcol({expr}) *virtcol()* - The result is a Number, which is the screen column of the file - position given with {expr}. That is, the last screen position - occupied by the character at that position, when the screen - would be of unlimited width. When there is a <Tab> at the - position, the returned Number will be the column at the end of - the <Tab>. For example, for a <Tab> in column 1, with 'ts' - set to 8, it returns 8. |conceal| is ignored. - For the byte position use |col()|. - For the use of {expr} see |col()|. - When 'virtualedit' is used {expr} can be [lnum, col, off], where - "off" is the offset in screen columns from the start of the - character. E.g., a position within a <Tab> or after the last - character. When "off" is omitted zero is used. - When Virtual editing is active in the current mode, a position - beyond the end of the line can be returned. |'virtualedit'| - The accepted positions are: - . the cursor position - $ the end of the cursor line (the result is the - number of displayed characters in the cursor line - plus one) - 'x position of mark x (if the mark is not set, 0 is - returned) - v In Visual mode: the start of the Visual area (the - cursor is the end). When not in Visual mode - returns the cursor position. Differs from |'<| in - that it's updated right away. - Note that only marks in the current file can be used. - Examples: > - virtcol(".") with text "foo^Lbar", with cursor on the "^L", returns 5 - virtcol("$") with text "foo^Lbar", returns 9 - virtcol("'t") with text " there", with 't at 'h', returns 6 -< The first column is 1. 0 is returned for an error. - A more advanced example that echoes the maximum length of - all lines: > - echo max(map(range(1, line('$')), "virtcol([v:val, '$'])")) - -< Can also be used as a |method|: > - GetPos()->virtcol() - -visualmode([{expr}]) *visualmode()* - The result is a String, which describes the last Visual mode - used in the current buffer. Initially it returns an empty - string, but once Visual mode has been used, it returns "v", - "V", or "<CTRL-V>" (a single CTRL-V character) for - character-wise, line-wise, or block-wise Visual mode - respectively. - Example: > - :exe "normal " . visualmode() -< This enters the same Visual mode as before. It is also useful - in scripts if you wish to act differently depending on the - Visual mode that was used. - If Visual mode is active, use |mode()| to get the Visual mode - (e.g., in a |:vmap|). - If {expr} is supplied and it evaluates to a non-zero Number or - a non-empty String, then the Visual mode will be cleared and - the old value is returned. See |non-zero-arg|. - -wait({timeout}, {condition}[, {interval}]) *wait()* - Waits until {condition} evaluates to |TRUE|, where {condition} - is a |Funcref| or |string| containing an expression. - - {timeout} is the maximum waiting time in milliseconds, -1 - means forever. - - Condition is evaluated on user events, internal events, and - every {interval} milliseconds (default: 200). - - Returns a status integer: - 0 if the condition was satisfied before timeout - -1 if the timeout was exceeded - -2 if the function was interrupted (by |CTRL-C|) - -3 if an error occurred - -wildmenumode() *wildmenumode()* - Returns |TRUE| when the wildmenu is active and |FALSE| - otherwise. See 'wildmenu' and 'wildmode'. - This can be used in mappings to handle the 'wildcharm' option - gracefully. (Makes only sense with |mapmode-c| mappings). - - For example to make <c-j> work like <down> in wildmode, use: > - :cnoremap <expr> <C-j> wildmenumode() ? "\<Down>\<Tab>" : "\<c-j>" -< - (Note, this needs the 'wildcharm' option set appropriately). - -win_execute({id}, {command} [, {silent}]) *win_execute()* - Like `execute()` but in the context of window {id}. - The window will temporarily be made the current window, - without triggering autocommands or changing directory. When - executing {command} autocommands will be triggered, this may - have unexpected side effects. Use |:noautocmd| if needed. - Example: > - call win_execute(winid, 'syntax enable') -< - Can also be used as a |method|, the base is passed as the - second argument: > - GetCommand()->win_execute(winid) - -win_findbuf({bufnr}) *win_findbuf()* - Returns a |List| with |window-ID|s for windows that contain - buffer {bufnr}. When there is none the list is empty. - - Can also be used as a |method|: > - GetBufnr()->win_findbuf() - -win_getid([{win} [, {tab}]]) *win_getid()* - Get the |window-ID| for the specified window. - When {win} is missing use the current window. - With {win} this is the window number. The top window has - number 1. - Without {tab} use the current tab, otherwise the tab with - number {tab}. The first tab has number one. - Return zero if the window cannot be found. - - Can also be used as a |method|: > - GetWinnr()->win_getid() - -win_gettype([{nr}]) *win_gettype()* - Return the type of the window: - "autocmd" autocommand window. Temporary window - used to execute autocommands. - "command" command-line window |cmdwin| - (empty) normal window - "loclist" |location-list-window| - "popup" popup window |popup| - "preview" preview window |preview-window| - "quickfix" |quickfix-window| - "unknown" window {nr} not found - - When {nr} is omitted return the type of the current window. - When {nr} is given return the type of this window by number or - |window-ID|. - - Also see the 'buftype' option. When running a terminal in a - popup window then 'buftype' is "terminal" and win_gettype() - returns "popup". - - Can also be used as a |method|: > - GetWinid()->win_gettype() -< -win_gotoid({expr}) *win_gotoid()* - Go to window with ID {expr}. This may also change the current - tabpage. - Return TRUE if successful, FALSE if the window cannot be found. - - Can also be used as a |method|: > - GetWinid()->win_gotoid() - -win_id2tabwin({expr}) *win_id2tabwin()* - Return a list with the tab number and window number of window - with ID {expr}: [tabnr, winnr]. - Return [0, 0] if the window cannot be found. - - Can also be used as a |method|: > - GetWinid()->win_id2tabwin() - -win_id2win({expr}) *win_id2win()* - Return the window number of window with ID {expr}. - Return 0 if the window cannot be found in the current tabpage. - - Can also be used as a |method|: > - GetWinid()->win_id2win() - -win_screenpos({nr}) *win_screenpos()* - Return the screen position of window {nr} as a list with two - numbers: [row, col]. The first window always has position - [1, 1], unless there is a tabline, then it is [2, 1]. - {nr} can be the window number or the |window-ID|. Use zero - for the current window. - Returns [0, 0] if the window cannot be found in the current - tabpage. - - Can also be used as a |method|: > - GetWinid()->win_screenpos() -< -win_splitmove({nr}, {target} [, {options}]) *win_splitmove()* - Move the window {nr} to a new split of the window {target}. - This is similar to moving to {target}, creating a new window - using |:split| but having the same contents as window {nr}, and - then closing {nr}. - - Both {nr} and {target} can be window numbers or |window-ID|s. - Both must be in the current tab page. - - Returns zero for success, non-zero for failure. - - {options} is a |Dictionary| with the following optional entries: - "vertical" When TRUE, the split is created vertically, - like with |:vsplit|. - "rightbelow" When TRUE, the split is made below or to the - right (if vertical). When FALSE, it is done - above or to the left (if vertical). When not - present, the values of 'splitbelow' and - 'splitright' are used. - - Can also be used as a |method|: > - GetWinid()->win_splitmove(target) -< - *winbufnr()* -winbufnr({nr}) The result is a Number, which is the number of the buffer - associated with window {nr}. {nr} can be the window number or - the |window-ID|. - When {nr} is zero, the number of the buffer in the current - window is returned. - When window {nr} doesn't exist, -1 is returned. - Example: > - :echo "The file in the current window is " . bufname(winbufnr(0)) -< - Can also be used as a |method|: > - FindWindow()->winbufnr()->bufname() -< - *wincol()* -wincol() The result is a Number, which is the virtual column of the - cursor in the window. This is counting screen cells from the - left side of the window. The leftmost column is one. - - *windowsversion()* -windowsversion() - The result is a String. For MS-Windows it indicates the OS - version. E.g, Windows 10 is "10.0", Windows 8 is "6.2", - Windows XP is "5.1". For non-MS-Windows systems the result is - an empty string. - -winheight({nr}) *winheight()* - The result is a Number, which is the height of window {nr}. - {nr} can be the window number or the |window-ID|. - When {nr} is zero, the height of the current window is - returned. When window {nr} doesn't exist, -1 is returned. - An existing window always has a height of zero or more. - This excludes any window toolbar line. - Examples: > - :echo "The current window has " . winheight(0) . " lines." - -< Can also be used as a |method|: > - GetWinid()->winheight() -< -winlayout([{tabnr}]) *winlayout()* - The result is a nested List containing the layout of windows - in a tabpage. - - Without {tabnr} use the current tabpage, otherwise the tabpage - with number {tabnr}. If the tabpage {tabnr} is not found, - returns an empty list. - - For a leaf window, it returns: - ['leaf', {winid}] - For horizontally split windows, which form a column, it - returns: - ['col', [{nested list of windows}]] - For vertically split windows, which form a row, it returns: - ['row', [{nested list of windows}]] - - Example: > - " Only one window in the tab page - :echo winlayout() - ['leaf', 1000] - " Two horizontally split windows - :echo winlayout() - ['col', [['leaf', 1000], ['leaf', 1001]]] - " The second tab page, with three horizontally split - " windows, with two vertically split windows in the - " middle window - :echo winlayout(2) - ['col', [['leaf', 1002], ['row', [['leaf', 1003], - ['leaf', 1001]]], ['leaf', 1000]]] -< - Can also be used as a |method|: > - GetTabnr()->winlayout() -< - *winline()* -winline() The result is a Number, which is the screen line of the cursor - in the window. This is counting screen lines from the top of - the window. The first line is one. - If the cursor was moved the view on the file will be updated - first, this may cause a scroll. - - *winnr()* -winnr([{arg}]) The result is a Number, which is the number of the current - window. The top window has number 1. - Returns zero for a popup window. - - The optional argument {arg} supports the following values: - $ the number of the last window (the window - count). - # the number of the last accessed window (where - |CTRL-W_p| goes to). If there is no previous - window or it is in another tab page 0 is - returned. - {N}j the number of the Nth window below the - current window (where |CTRL-W_j| goes to). - {N}k the number of the Nth window above the current - window (where |CTRL-W_k| goes to). - {N}h the number of the Nth window left of the - current window (where |CTRL-W_h| goes to). - {N}l the number of the Nth window right of the - current window (where |CTRL-W_l| goes to). - The number can be used with |CTRL-W_w| and ":wincmd w" - |:wincmd|. - Also see |tabpagewinnr()| and |win_getid()|. - Examples: > - let window_count = winnr('$') - let prev_window = winnr('#') - let wnum = winnr('3k') - -< Can also be used as a |method|: > - GetWinval()->winnr() -< - *winrestcmd()* -winrestcmd() Returns a sequence of |:resize| commands that should restore - the current window sizes. Only works properly when no windows - are opened or closed and the current window and tab page is - unchanged. - Example: > - :let cmd = winrestcmd() - :call MessWithWindowSizes() - :exe cmd -< - *winrestview()* -winrestview({dict}) - Uses the |Dictionary| returned by |winsaveview()| to restore - the view of the current window. - Note: The {dict} does not have to contain all values, that are - returned by |winsaveview()|. If values are missing, those - settings won't be restored. So you can use: > - :call winrestview({'curswant': 4}) -< - This will only set the curswant value (the column the cursor - wants to move on vertical movements) of the cursor to column 5 - (yes, that is 5), while all other settings will remain the - same. This is useful, if you set the cursor position manually. - - If you have changed the values the result is unpredictable. - If the window size changed the result won't be the same. - - Can also be used as a |method|: > - GetView()->winrestview() -< - *winsaveview()* -winsaveview() Returns a |Dictionary| that contains information to restore - the view of the current window. Use |winrestview()| to - restore the view. - This is useful if you have a mapping that jumps around in the - buffer and you want to go back to the original view. - This does not save fold information. Use the 'foldenable' - option to temporarily switch off folding, so that folds are - not opened when moving around. This may have side effects. - The return value includes: - lnum cursor line number - col cursor column (Note: the first column - zero, as opposed to what getpos() - returns) - coladd cursor column offset for 'virtualedit' - curswant column for vertical movement - topline first line in the window - topfill filler lines, only in diff mode - leftcol first column displayed; only used when - 'wrap' is off - skipcol columns skipped - Note that no option values are saved. - - -winwidth({nr}) *winwidth()* - The result is a Number, which is the width of window {nr}. - {nr} can be the window number or the |window-ID|. - When {nr} is zero, the width of the current window is - returned. When window {nr} doesn't exist, -1 is returned. - An existing window always has a width of zero or more. - Examples: > - :echo "The current window has " . winwidth(0) . " columns." - :if winwidth(0) <= 50 - : 50 wincmd | - :endif -< For getting the terminal or screen size, see the 'columns' - option. - - Can also be used as a |method|: > - GetWinid()->winwidth() - -wordcount() *wordcount()* - The result is a dictionary of byte/chars/word statistics for - the current buffer. This is the same info as provided by - |g_CTRL-G| - The return value includes: - bytes Number of bytes in the buffer - chars Number of chars in the buffer - words Number of words in the buffer - cursor_bytes Number of bytes before cursor position - (not in Visual mode) - cursor_chars Number of chars before cursor position - (not in Visual mode) - cursor_words Number of words before cursor position - (not in Visual mode) - visual_bytes Number of bytes visually selected - (only in Visual mode) - visual_chars Number of chars visually selected - (only in Visual mode) - visual_words Number of words visually selected - (only in Visual mode) - - - *writefile()* -writefile({object}, {fname} [, {flags}]) - When {object} is a |List| write it to file {fname}. Each list - item is separated with a NL. Each list item must be a String - or Number. - When {flags} contains "b" then binary mode is used: There will - not be a NL after the last list item. An empty item at the - end does cause the last line in the file to end in a NL. - - When {object} is a |Blob| write the bytes to file {fname} - unmodified. - - When {flags} contains "a" then append mode is used, lines are - appended to the file: > - :call writefile(["foo"], "event.log", "a") - :call writefile(["bar"], "event.log", "a") -< - When {flags} contains "S" fsync() call is not used, with "s" - it is used, 'fsync' option applies by default. No fsync() - means that writefile() will finish faster, but writes may be - left in OS buffers and not yet written to disk. Such changes - will disappear if system crashes before OS does writing. - - All NL characters are replaced with a NUL character. - Inserting CR characters needs to be done before passing {list} - to writefile(). - An existing file is overwritten, if possible. - When the write fails -1 is returned, otherwise 0. There is an - error message if the file can't be created or when writing - fails. - Also see |readfile()|. - To copy a file byte for byte: > - :let fl = readfile("foo", "b") - :call writefile(fl, "foocopy", "b") - -< Can also be used as a |method|: > - GetText()->writefile("thefile") - -xor({expr}, {expr}) *xor()* - Bitwise XOR on the two arguments. The arguments are converted - to a number. A List, Dict or Float argument causes an error. - Example: > - :let bits = xor(bits, 0x80) -< - Can also be used as a |method|: > - :let bits = bits->xor(0x80) -< - - *string-match* -Matching a pattern in a String - -A regexp pattern as explained at |pattern| is normally used to find a match in -the buffer lines. When a pattern is used to find a match in a String, almost -everything works in the same way. The difference is that a String is handled -like it is one line. When it contains a "\n" character, this is not seen as a -line break for the pattern. It can be matched with a "\n" in the pattern, or -with ".". Example: > - :let a = "aaaa\nxxxx" - :echo matchstr(a, "..\n..") - aa - xx - :echo matchstr(a, "a.x") - a - x - -Don't forget that "^" will only match at the first character of the String and -"$" at the last character of the string. They don't match after or before a -"\n". +The alphabetic list of all builtin functions and details are in a separate +help file: |builtin-functions|. ============================================================================== 5. Defining functions *user-function* @@ -11063,7 +2429,7 @@ See |:verbose-cmd| for more information. command, use line breaks instead of |:bar|: > :exe "func Foo()\necho 'foo'\nendfunc" < - *:delf* *:delfunction* *E130* *E131* *E933* + *:delf* *:delfunction* *E131* *E933* :delf[unction][!] {name} Delete function {name}. {name} can also be a |Dictionary| entry that is a @@ -13153,7 +4519,7 @@ code can be used: > unlet scriptnames_output ============================================================================== -The sandbox *eval-sandbox* *sandbox* *E48* +The sandbox *eval-sandbox* *sandbox* The 'foldexpr', 'formatexpr', 'includeexpr', 'indentexpr', 'statusline' and 'foldtext' options may be evaluated in a sandbox. This means that you are @@ -13162,6 +4528,7 @@ safety for when these options are set from a modeline. It is also used when the command from a tags file is executed and for CTRL-R = in the command line. The sandbox is also used for the |:sandbox| command. + *E48* These items are not allowed in the sandbox: - changing the buffer text - defining or changing mapping, autocommands, user commands diff --git a/runtime/doc/filetype.txt b/runtime/doc/filetype.txt index 8ca763b45e..5486c87af9 100644 --- a/runtime/doc/filetype.txt +++ b/runtime/doc/filetype.txt @@ -24,12 +24,21 @@ Each time a new or existing file is edited, Vim will try to recognize the type of the file and set the 'filetype' option. This will trigger the FileType event, which can be used to set the syntax highlighting, set options, etc. -Detail: The ":filetype on" command will load this file: +Detail: The ":filetype on" command will load these files: + $VIMRUNTIME/filetype.lua $VIMRUNTIME/filetype.vim - This file is a Vim script that defines autocommands for the - BufNewFile and BufRead events. If the file type is not found by the - name, the file $VIMRUNTIME/scripts.vim is used to detect it from the - contents of the file. + filetype.lua creates an autocommand that fires for all BufNewFile and + BufRead events. It tries to detect the filetype based off of the + file's extension or name. + + filetype.vim is a Vim script that defines autocommands for the + BufNewFile and BufRead events. In contrast to filetype.lua, this + file creates separate BufNewFile and BufRead events for each filetype + pattern. + + If the file type is not found by the name, the file + $VIMRUNTIME/scripts.vim is used to detect it from the contents of the + file. When the GUI is running or will start soon, the |menu.vim| script is also sourced. See |'go-M'| about avoiding that. @@ -122,14 +131,15 @@ shell script: "#!/bin/csh". argument was used. *filetype-overrule* -When the same extension is used for two filetypes, Vim tries to guess what -kind of file it is. This doesn't always work. A number of global variables -can be used to overrule the filetype used for certain extensions: +When the same extension is used for multiple filetypes, Vim tries to guess +what kind of file it is. This doesn't always work. A number of global +variables can be used to overrule the filetype used for certain extensions: file name variable ~ *.asa g:filetype_asa |ft-aspvbs-syntax| |ft-aspperl-syntax| *.asm g:asmsyntax |ft-asm-syntax| *.asp g:filetype_asp |ft-aspvbs-syntax| |ft-aspperl-syntax| + *.bas g:filetype_bas |ft-basic-syntax| *.fs g:filetype_fs |ft-forth-syntax| *.i g:filetype_i |ft-progress-syntax| *.inc g:filetype_inc @@ -149,9 +159,10 @@ is used. The default value is set like this: > This means that the contents of compressed files are not inspected. *new-filetype* -If a file type that you want to use is not detected yet, there are four ways -to add it. In any way, it's better not to modify the $VIMRUNTIME/filetype.vim -file. It will be overwritten when installing a new version of Vim. +If a file type that you want to use is not detected yet, there are a few ways +to add it. In any way, it's better not to modify the $VIMRUNTIME/filetype.lua +or $VIMRUNTIME/filetype.vim files. They will be overwritten when installing a +new version of Nvim. A. If you want to overrule all default file type checks. This works by writing one file for each filetype. The disadvantage is that @@ -191,7 +202,7 @@ B. If you want to detect your file after the default file type checks. au BufRead,BufNewFile * if &ft == 'pascal' | set ft=mypascal | endif -C. If your file type can be detected by the file name. +C. If your file type can be detected by the file name or extension. 1. Create your user runtime directory. You would normally use the first item of the 'runtimepath' option. Example for Unix: > :!mkdir -p ~/.config/nvim @@ -206,9 +217,38 @@ C. If your file type can be detected by the file name. au! BufRead,BufNewFile *.mine setfiletype mine au! BufRead,BufNewFile *.xyz setfiletype drawing augroup END -< Write this file as "filetype.vim" in your user runtime directory. For +< + Write this file as "filetype.vim" in your user runtime directory. For example, for Unix: > :w ~/.config/nvim/filetype.vim +< + Alternatively, create a file called "filetype.lua" that adds new + filetypes. + Example: > + vim.filetype.add({ + extension = { + foo = "fooscript", + }, + filename = { + [".foorc"] = "foorc", + }, + pattern = { + [".*/etc/foo/.*%.conf"] = "foorc", + }, + }) +< + See |vim.filetype.add()|. + *g:do_filetype_lua* + For now, Lua filetype detection is opt-in. You can enable it by adding + the following to your |init.vim|: > + let g:do_filetype_lua = 1 +< *g:did_load_filetypes* + In either case, the builtin filetype detection provided by Nvim can be + disabled by setting the did_load_filetypes global variable. If this + variable exists, $VIMRUNTIME/filetype.vim will not run. + Example: > + " Disable filetype.vim + let g:did_load_filetypes = 1 < 3. To use the new filetype detection you must restart Vim. @@ -245,9 +285,9 @@ D. If your filetype can only be detected by inspecting the contents of the $VIMRUNTIME/scripts.vim. *remove-filetype* -If a file type is detected that is wrong for you, install a filetype.vim or -scripts.vim to catch it (see above). You can set 'filetype' to a non-existing -name to avoid that it will be set later anyway: > +If a file type is detected that is wrong for you, install a filetype.lua, +filetype.vim or scripts.vim to catch it (see above). You can set 'filetype' to +a non-existing name to avoid that it will be set later anyway: > :set filetype=ignored If you are setting up a system with many users, and you don't want each user diff --git a/runtime/doc/fold.txt b/runtime/doc/fold.txt index 80c934d13b..8bc47a3b10 100644 --- a/runtime/doc/fold.txt +++ b/runtime/doc/fold.txt @@ -501,7 +501,9 @@ Note the use of backslashes to avoid some characters to be interpreted by the :endfunction Evaluating 'foldtext' is done in the |sandbox|. The current window is set to -the window that displays the line. Errors are ignored. +the window that displays the line. + +Errors are ignored. For debugging set the 'debug' option to "throw". The default value is |foldtext()|. This returns a reasonable text for most types of folding. If you don't like it, you can specify your own 'foldtext' diff --git a/runtime/doc/help.txt b/runtime/doc/help.txt index 173d3c0cdf..b97c9a2e3f 100644 --- a/runtime/doc/help.txt +++ b/runtime/doc/help.txt @@ -129,6 +129,7 @@ Advanced editing ~ |diff.txt| working with two to eight versions of the same file |autocmd.txt| automatically executing commands on an event |eval.txt| expression evaluation, conditional commands +|builtin.txt| builtin functions |fold.txt| hide (fold) ranges of lines |lua.txt| Lua API |api.txt| Nvim API via RPC, Lua and VimL diff --git a/runtime/doc/helphelp.txt b/runtime/doc/helphelp.txt index 9cc7d063a8..569995d319 100644 --- a/runtime/doc/helphelp.txt +++ b/runtime/doc/helphelp.txt @@ -219,7 +219,7 @@ command: > < *:helpt* *:helptags* - *E154* *E150* *E151* *E152* *E153* *E670* *E856* + *E150* *E151* *E152* *E153* *E154* *E670* *E856* :helpt[ags] [++t] {dir} Generate the help tags file(s) for directory {dir}. When {dir} is ALL then all "doc" directories in @@ -358,7 +358,7 @@ When referring to a Vim option in the help file, place the option name between two single quotes, eg. 'statusline' When referring to any other technical term, such as a filename or function -parameter, surround it in backticks (`), eg. `~/.path/to/init.vim`. +parameter, surround it in backticks, eg. `~/.path/to/init.vim`. HIGHLIGHTING diff --git a/runtime/doc/if_cscop.txt b/runtime/doc/if_cscop.txt index f05b3bb8ed..8947aefc1b 100644 --- a/runtime/doc/if_cscop.txt +++ b/runtime/doc/if_cscop.txt @@ -40,7 +40,7 @@ See |cscope-usage| to get started. ============================================================================== Cscope commands *cscope-commands* - *:cscope* *:cs* *:scs* *:scscope* *E259* *E262* *E561* *E560* + *:cscope* *:cs* *:scs* *:scscope* *E259* *E262* *E560* *E561* All cscope commands are accessed through suboptions to the cscope commands. `:cscope` or `:cs` is the main command `:scscope` or `:scs` does the same and splits the window diff --git a/runtime/doc/if_pyth.txt b/runtime/doc/if_pyth.txt index fea47de220..afdf039aa8 100644 --- a/runtime/doc/if_pyth.txt +++ b/runtime/doc/if_pyth.txt @@ -1,10 +1,10 @@ *if_pyth.txt* Nvim - VIM REFERENCE MANUAL by Paul Moore + NVIM REFERENCE MANUAL -The Python Interface to Vim *if_pyth* *python* *Python* +The Python Interface to NVim *if_pyth* *python* *Python* See |provider-python| for more information. @@ -134,7 +134,7 @@ Instead, put the Python command in a function and call that function: Note that "EOF" must be at the start of the line. ============================================================================== -The vim module *python-vim* *python2* +The vim module *python-vim* Python code gets all of its access to vim (with one exception - see |python-output| below) via the "vim" module. The vim module implements two @@ -322,14 +322,13 @@ Output from Python *python-output* supported, and may cause the program to crash. This should probably be fixed. - *python2-directory* *python3-directory* *pythonx-directory* + *python3-directory* *pythonx-directory* Python 'runtimepath' handling *python-special-path* In python vim.VIM_SPECIAL_PATH special directory is used as a replacement for the list of paths found in 'runtimepath': with this directory in sys.path and vim.path_hooks in sys.path_hooks python will try to load module from -{rtp}/python2 (or python3) and {rtp}/pythonx (for both python versions) for -each {rtp} found in 'runtimepath'. +{rtp}/python3 and {rtp}/pythonx for each {rtp} found in 'runtimepath'. Implementation is similar to the following, but written in C: > @@ -401,8 +400,8 @@ vim._get_paths *python-_get_paths* hook. You should not rely on this method being present in future versions, but can use it for debugging. - It returns a list of {rtp}/python2 (or {rtp}/python3) and - {rtp}/pythonx directories for each {rtp} in 'runtimepath'. + It returns a list of {rtp}/python3 and {rtp}/pythonx + directories for each {rtp} in 'runtimepath'. ============================================================================== Buffer objects *python-buffer* @@ -590,6 +589,11 @@ functions to evaluate Python expressions and pass their values to Vim script. ============================================================================== Python 3 *python3* +As Python 3 is the only supported version in Nvim, "python" is synonymous +with "python3" in the current version. However, code that aims to support older +versions of Neovim, as well as Vim, should prefer to use "python3" +variants explicitly if Python 3 is required. + *:py3* *:python3* :[range]py3 {stmt} :[range]py3 << [endmarker] @@ -619,31 +623,26 @@ Raising SystemExit exception in python isn't endorsed way to quit vim, use: > :py vim.command("qall!") < *has-python* -You can test what Python version is available with: > - if has('python') - echo 'there is Python 2.x' +You can test if Python is available with: > + if has('pythonx') + echo 'there is Python' endif if has('python3') echo 'there is Python 3.x' endif +Python 2 is no longer supported. Thus `has('python')` always returns +zero for backwards compatibility reasons. + ============================================================================== Python X *python_x* *pythonx* -Because most python code can be written so that it works with Python 2.6+ and -Python 3, the pyx* functions and commands have been written. They work the -same as the Python 2 and 3 variants, but select the Python version using the -'pyxversion' setting. - -Set 'pyxversion' in your |vimrc| to prefer Python 2 or Python 3 for Python -commands. Changing this setting at runtime risks losing the state of plugins -(such as initialization). - -If you want to use a module, you can put it in the {rtp}/pythonx directory. -See |pythonx-directory|. +The "pythonx" and "pyx" prefixes were introduced for python code which +works with Python 2.6+ and Python 3. As Nvim only supports Python 3, +all these commands are now synonymous to their "python3" equivalents. *:pyx* *:pythonx* -`:pyx` and `:pythonx` work similar to `:python`. To check if `:pyx` works: > +`:pyx` and `:pythonx` work the same as `:python3`. To check if `:pyx` works: > :pyx print("Hello") To see what version of Python is being used: > @@ -651,34 +650,16 @@ To see what version of Python is being used: > :pyx print(sys.version) < *:pyxfile* *python_x-special-comments* -`:pyxfile` works similar to `:pyfile`. But you can add a "shebang" comment to -force Vim to use `:pyfile` or `:py3file`: > - #!/any string/python2 " Shebang. Must be the first line of the file. - #!/any string/python3 " Shebang. Must be the first line of the file. - # requires python 2.x " Maximum lines depend on 'modelines'. - # requires python 3.x " Maximum lines depend on 'modelines'. -Unlike normal modelines, the bottom of the file is not checked. -If none of them are found, the 'pyxversion' option is used. - *W20* *W21* -If Vim does not support the selected Python version a silent message will be -printed. Use `:messages` to read them. +`:pyxfile` works the same as `:py3file`. *:pyxdo* -`:pyxdo` works similar to `:pydo`. +`:pyxdo` works the same as `:py3do`. *has-pythonx* -To check if pyx* functions and commands are available: > +To check if `pyx*` functions and commands are available: > if has('pythonx') echo 'pyx* commands are available. (Python ' . &pyx . ')' endif -If you prefer Python 2 and want to fallback to Python 3, set 'pyxversion' -explicitly in your |.vimrc|. Example: > - if has('python') - set pyx=2 - elseif has('python3') - set pyx=3 - endif - ============================================================================== vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/runtime/doc/indent.txt b/runtime/doc/indent.txt index 1b42092616..d0d4ddad32 100644 --- a/runtime/doc/indent.txt +++ b/runtime/doc/indent.txt @@ -771,6 +771,15 @@ You can set the indent for the first line after <script> and <style> "auto" auto indent (same indent as the blocktag) "inc" auto indent + one indent step +You can set the indent for attributes after an open <tag line: > + + :let g:html_indent_attribute = 1 +< + VALUE MEANING ~ + 1 auto indent, one indent step more than <tag + 2 auto indent, two indent steps (default) + > 2 auto indent, more indent steps + Many tags increase the indent for what follows per default (see "Add Indent Tags" in the script). You can add further tags with: > @@ -872,7 +881,7 @@ For example, with N = 1, this will give: *PHP_outdentphpescape* To indent PHP escape tags as the surrounding non-PHP code (only affects the PHP escape tags): > -:let g:PHP_outdentphpescape = 0 + :let g:PHP_outdentphpescape = 0 ------------- *PHP_removeCRwhenUnix* @@ -1199,7 +1208,7 @@ comments will be indented according to the correctly indented code. VIM *ft-vim-indent* - + *g:vim_indent_cont* For indenting Vim scripts there is one variable that specifies the amount of indent for a continuation line, a line that starts with a backslash: > diff --git a/runtime/doc/index.txt b/runtime/doc/index.txt index d8689e2c65..e3bc3d5437 100644 --- a/runtime/doc/index.txt +++ b/runtime/doc/index.txt @@ -923,7 +923,9 @@ tag command note action in Visual mode ~ before the highlighted area |v_J| J 2 join the highlighted lines |v_K| K run 'keywordprg' on the highlighted area -|v_O| O Move horizontally to other corner of area. +|v_O| O move horizontally to other corner of area +|v_P| P replace highlighted area with register + contents; unnamed register is unchanged Q does not start Ex mode |v_R| R 2 delete the highlighted lines and start insert @@ -986,6 +988,8 @@ tag command note action in Visual mode ~ |v_i{| i{ same as iB |v_i}| i} same as iB |v_o| o move cursor to other corner of area +|v_p| p replace highlighted area with register + contents; deleted text in unnamed register |v_r| r 2 replace highlighted area with a character |v_s| s 2 delete highlighted area and start insert |v_u| u 2 make highlighted area lowercase diff --git a/runtime/doc/insert.txt b/runtime/doc/insert.txt index d74d7cafa8..ae2b9c4418 100644 --- a/runtime/doc/insert.txt +++ b/runtime/doc/insert.txt @@ -830,7 +830,7 @@ space is preferred). Maximum line length is 510 bytes. For an example, imagine the 'thesaurus' file has a line like this: > angry furious mad enraged -<Placing the cursor after the letters "ang" and typing CTRL-X CTRL-T would +Placing the cursor after the letters "ang" and typing CTRL-X CTRL-T would complete the word "angry"; subsequent presses would change the word to "furious", "mad" etc. @@ -842,7 +842,7 @@ https://github.com/vim/vim/issues/629#issuecomment-443293282 Unpack thesaurus_pkg.zip, put the thesaurus.txt file somewhere, e.g. ~/.vim/thesaurus/english.txt, and the 'thesaurus' option to this file name. - + Completing keywords with 'thesaurusfunc' *compl-thesaurusfunc* If the 'thesaurusfunc' option is set, then the user specified function is diff --git a/runtime/doc/intro.txt b/runtime/doc/intro.txt index 0e0156ac6b..54999fa163 100644 --- a/runtime/doc/intro.txt +++ b/runtime/doc/intro.txt @@ -322,7 +322,6 @@ notation meaning equivalent decimal value(s) ~ <Bar> vertical bar | 124 *<Bar>* <Del> delete 127 <CSI> command sequence intro ALT-Esc 155 *<CSI>* -<xCSI> CSI when typed in the GUI *<xCSI>* <EOL> end-of-line (can be <CR>, <NL> or <CR><NL>, depends on system and 'fileformat') *<EOL>* diff --git a/runtime/doc/lsp-extension.txt b/runtime/doc/lsp-extension.txt index d13303ada6..6e9ad940c7 100644 --- a/runtime/doc/lsp-extension.txt +++ b/runtime/doc/lsp-extension.txt @@ -60,7 +60,7 @@ The example will: return nil end local dir = bufname - -- Just in case our algo is buggy, don't infinite loop. + -- Just in case our algorithm is buggy, don't infinite loop. for _ = 1, 100 do local did_change dir, did_change = dirname(dir) diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index 614dd82443..f6fcbe8fb9 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -749,8 +749,8 @@ omnifunc({findstart}, {base}) *vim.lsp.omnifunc()* set_log_level({level}) *vim.lsp.set_log_level()* Sets the global log level for LSP logging. - Levels by name: "trace", "debug", "info", "warn", "error" - Level numbers begin with "trace" at 0 + Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR" + Level numbers begin with "TRACE" at 0 Use `lsp.log_levels` for reverse lookup. @@ -1008,7 +1008,7 @@ document_highlight() *vim.lsp.buf.document_highlight()* Send request to the server to resolve document highlights for the current text document position. This request can be triggered by a key mapping or by events such as `CursorHold` , - eg: + e.g.: > autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight() @@ -1024,11 +1024,12 @@ document_symbol() *vim.lsp.buf.document_symbol()* Lists all symbols in the current buffer in the quickfix window. -execute_command({command}) *vim.lsp.buf.execute_command()* +execute_command({command_params}) *vim.lsp.buf.execute_command()* Executes an LSP server command. Parameters: ~ - {command} A valid `ExecuteCommandParams` object + {command_params} table A valid `ExecuteCommandParams` + object See also: ~ https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand @@ -1331,7 +1332,7 @@ signature_help({_}, {result}, {ctx}, {config}) Lua module: vim.lsp.util *lsp-util* *vim.lsp.util.apply_text_document_edit()* -apply_text_document_edit({text_document_edit}, {index}) +apply_text_document_edit({text_document_edit}, {index}, {offset_encoding}) Applies a `TextDocumentEdit` , which is a list of changes to a single document. @@ -1351,18 +1352,19 @@ apply_text_edits({text_edits}, {bufnr}, {offset_encoding}) Parameters: ~ {text_edits} table list of `TextEdit` objects {bufnr} number Buffer id - {offset_encoding} string utf-8|utf-16|utf-32|nil defaults - to encoding of first client of `bufnr` + {offset_encoding} string utf-8|utf-16|utf-32 defaults to + encoding of first client of `bufnr` See also: ~ https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textEdit *vim.lsp.util.apply_workspace_edit()* -apply_workspace_edit({workspace_edit}) +apply_workspace_edit({workspace_edit}, {offset_encoding}) Applies a `WorkspaceEdit` . Parameters: ~ - {workspace_edit} (table) `WorkspaceEdit` + {workspace_edit} table `WorkspaceEdit` + {offset_encoding} string utf-8|utf-16|utf-32 (required) buf_clear_references({bufnr}) *vim.lsp.util.buf_clear_references()* Removes document highlights from a buffer. @@ -1379,9 +1381,7 @@ buf_highlight_references({bufnr}, {references}, {offset_encoding}) {references} table List of `DocumentHighlight` objects to highlight {offset_encoding} string One of "utf-8", "utf-16", - "utf-32", or nil. Defaults to - `offset_encoding` of first client of - `bufnr` + "utf-32". See also: ~ https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#documentHighlight @@ -1470,16 +1470,19 @@ get_effective_tabstop({bufnr}) *vim.lsp.util.get_effective_tabstop()* See also: ~ |softtabstop| -jump_to_location({location}) *vim.lsp.util.jump_to_location()* + *vim.lsp.util.jump_to_location()* +jump_to_location({location}, {offset_encoding}) Jumps to a location. Parameters: ~ - {location} ( `Location` | `LocationLink` ) + {location} table ( `Location` | `LocationLink` ) + {offset_encoding} string utf-8|utf-16|utf-32 (required) Return: ~ `true` if the jump succeeded -locations_to_items({locations}) *vim.lsp.util.locations_to_items()* + *vim.lsp.util.locations_to_items()* +locations_to_items({locations}, {offset_encoding}) Returns the items with the byte position calculated correctly and in sorted order, for display in quickfix and location lists. @@ -1488,8 +1491,10 @@ locations_to_items({locations}) *vim.lsp.util.locations_to_items()* |setqflist()| or |setloclist()|. Parameters: ~ - {locations} (table) list of `Location` s or - `LocationLink` s + {locations} table list of `Location` s or + `LocationLink` s + {offset_encoding} string offset_encoding for locations + utf-8|utf-16|utf-32 Return: ~ (table) list of items diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index eaa25f0961..77f1dad6c7 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -17,9 +17,9 @@ an idea of what lurks beneath: > :lua print(vim.inspect(package.loaded)) Nvim includes a "standard library" |lua-stdlib| for Lua. It complements the -"editor stdlib" (|functions| and Ex commands) and the |API|, all of which can -be used from Lua code. A good overview of using Lua in neovim is given by -https://github.com/nanotee/nvim-lua-guide. +"editor stdlib" (|builtin-functions| and Ex commands) and the |API|, all of +which can be used from Lua code. A good overview of using Lua in neovim is +given by https://github.com/nanotee/nvim-lua-guide. Module conflicts are resolved by "last wins". For example if both of these are on 'runtimepath': @@ -249,13 +249,15 @@ arguments separated by " " (space) instead of "\t" (tab). *:lua* :[range]lua {chunk} Executes Lua chunk {chunk}. - + if {chunk} starts with "=" the rest of the chunk is + evaluated as an expression and printed. `:lua =expr` + is equivalent to `:lua print(vim.inspect(expr))` Examples: > :lua vim.api.nvim_command('echo "Hello, Nvim!"') < To see the Lua version: > :lua print(_VERSION) < To see the LuaJIT version: > - :lua print(jit.version) + :lua =jit.version < *:lua-heredoc* :[range]lua << [endmarker] @@ -272,7 +274,7 @@ arguments separated by " " (space) instead of "\t" (tab). lua << EOF local linenr = vim.api.nvim_win_get_cursor(0)[1] local curline = vim.api.nvim_buf_get_lines( - 0, linenr, linenr + 1, false)[1] + 0, linenr - 1, linenr, false)[1] print(string.format("Current line [%d] has %d bytes", linenr, #curline)) EOF @@ -1238,19 +1240,32 @@ inspect({object}, {options}) *vim.inspect()* https://github.com/kikito/inspect.lua https://github.com/mpeterv/vinspect -notify({msg}, {log_level}, {opts}) *vim.notify()* - Notification provider +notify({msg}, {level}, {opts}) *vim.notify()* + Display a notification to the user. - Without a runtime, writes to :Messages + This function can be overridden by plugins to display + notifications using a custom provider (such as the system + notification provider). By default, writes to |:messages|. Parameters: ~ - {msg} string Content of the notification to show to - the user - {log_level} number|nil enum from vim.log.levels - {opts} table|nil additional options (timeout, etc) + {msg} string Content of the notification to show to the + user. + {level} number|nil One of the values from + |vim.log.levels|. + {opts} table|nil Optional parameters. Unused by default. - See also: ~ - :help nvim_notify +notify_once({msg}, {level}, {opts}) *vim.notify_once()* + Display a notification only one time. + + Like |vim.notify()|, but subsequent calls with the same + message will not display a notification. + + Parameters: ~ + {msg} string Content of the notification to show to the + user. + {level} number|nil One of the values from + |vim.log.levels|. + {opts} table|nil Optional parameters. Unused by default. on_key({fn}, {ns_id}) *vim.on_key()* Adds Lua function {fn} with namespace id {ns_id} as a listener @@ -1314,6 +1329,18 @@ paste({lines}, {phase}) *vim.paste()* See also: ~ |paste| +pretty_print({...}) *vim.pretty_print()* + Prints given arguments in human-readable format. Example: > + -- Print highlight group Normal and store it's contents in a variable. + local hl_normal = vim.pretty_print(vim.api.nvim_get_hl_by_name("Normal", true)) +< + + Return: ~ + given arguments. + + See also: ~ + |vim.inspect()| + region({bufnr}, {pos1}, {pos2}, {regtype}, {inclusive}) *vim.region()* Get a table of lines with start, end columns for a region marked by two points @@ -1778,4 +1805,157 @@ select({items}, {opts}, {on_choice}) *vim.ui.select()* 1-based index of `item` within `item` . `nil` if the user aborted the dialog. + +============================================================================== +Lua module: filetype *lua-filetype* + +add({filetypes}) *vim.filetype.add()* + Add new filetype mappings. + + Filetype mappings can be added either by extension or by + filename (either the "tail" or the full file path). The full + file path is checked first, followed by the file name. If a + match is not found using the filename, then the filename is + matched against the list of patterns (sorted by priority) + until a match is found. Lastly, if pattern matching does not + find a filetype, then the file extension is used. + + The filetype can be either a string (in which case it is used + as the filetype directly) or a function. If a function, it + takes the full path and buffer number of the file as arguments + (along with captures from the matched pattern, if any) and + should return a string that will be used as the buffer's + filetype. + + Filename patterns can specify an optional priority to resolve + cases when a file path matches multiple patterns. Higher + priorities are matched first. When omitted, the priority + defaults to 0. + + See $VIMRUNTIME/lua/vim/filetype.lua for more examples. + + Note that Lua filetype detection is only enabled when + |g:do_filetype_lua| is set to 1. + + Example: > + + vim.filetype.add({ + extension = { + foo = "fooscript", + bar = function(path, bufnr) + if some_condition() then + return "barscript" + end + return "bar" + end, + }, + filename = { + [".foorc"] = "toml", + ["/etc/foo/config"] = "toml", + }, + pattern = { + [".*‍/etc/foo/.*"] = "fooscript", + -- Using an optional priority + [".*‍/etc/foo/.*%.conf"] = { "dosini", { priority = 10 } }, + ["README.(%a+)$"] = function(path, bufnr, ext) + if ext == "md" then + return "markdown" + elseif ext == "rst" then + return "rst" + end + end, + }, + }) +< + + Parameters: ~ + {filetypes} table A table containing new filetype maps + (see example). + +match({name}, {bufnr}) *vim.filetype.match()* + Set the filetype for the given buffer from a file name. + + Parameters: ~ + {name} string File name (can be an absolute or relative + path) + {bufnr} number|nil The buffer to set the filetype for. + Defaults to the current buffer. + + +============================================================================== +Lua module: keymap *lua-keymap* + +del({modes}, {lhs}, {opts}) *vim.keymap.del()* + Remove an existing mapping. Examples: > + + vim.keymap.del('n', 'lhs') + + vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buffer = 5 }) +< + + Parameters: ~ + {opts} table A table of optional arguments: + • buffer: (number or boolean) Remove a mapping + from the given buffer. When "true" or 0, use the + current buffer. + + See also: ~ + |vim.keymap.set()| + +set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()* + Add a new |mapping|. Examples: > + + -- Can add mapping to Lua functions + vim.keymap.set('n', 'lhs', function() print("real lua function") end) + + -- Can use it to map multiple modes + vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, { buffer=true }) + + -- Can add mapping for specific buffer + vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", { silent = true, buffer = 5 }) + + -- Expr mappings + vim.keymap.set('i', '<Tab>', function() + return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>" + end, { expr = true }) + -- <Plug> mappings + vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)') +< + + Note that in a mapping like: > + + vim.keymap.set('n', 'asdf', require('jkl').my_fun) +< + + the require('jkl') gets evaluated during this call in order to + access the function. If you want to avoid this cost at startup + you can wrap it in a function, for example: > + + vim.keymap.set('n', 'asdf', function() return require('jkl').my_fun() end) +< + + Parameters: ~ + {mode} string|table Same mode short names as + |nvim_set_keymap()|. Can also be list of modes to + create mapping on multiple modes. + {lhs} string Left-hand side |{lhs}| of the mapping. + {rhs} string|function Right-hand side |{rhs}| of the + mapping. Can also be a Lua function. + {opts} table A table of |:map-arguments| such as + "silent". In addition to the options listed in + |nvim_set_keymap()|, this table also accepts the + following keys: + • replace_keycodes: (boolean, default true) When + both this and expr is "true", + |nvim_replace_termcodes()| is applied to the + result of Lua expr maps. + • remap: (boolean) Make the mapping recursive. + This is the inverse of the "noremap" option from + |nvim_set_keymap()|. Default `true` if `lhs` is + a string starting with `<plug>` + (case-insensitive), `false` otherwise. + + See also: ~ + |nvim_set_keymap()| + vim:tw=78:ts=8:ft=help:norl: diff --git a/runtime/doc/map.txt b/runtime/doc/map.txt index f6d9e45d64..2d2795b1ca 100644 --- a/runtime/doc/map.txt +++ b/runtime/doc/map.txt @@ -82,8 +82,7 @@ modes. map command applies. The mapping may remain defined for other modes where it applies. It also works when {lhs} matches the {rhs} of a - mapping. This is for when when an abbreviation - applied. + mapping. This is for when an abbreviation applied. Note: Trailing spaces are included in the {lhs}. This unmap does NOT work: > :map @@ foo @@ -245,7 +244,7 @@ go through the main loop (e.g. to update the display), return "\<Ignore>". This is similar to "nothing" but makes Vim return from the loop that waits for input. -Also, keep in mind that the expression may be evaluated when looking for +Keep in mind that the expression may be evaluated when looking for typeahead, before the previous command has been executed. For example: > func StoreColumn() let g:column = col('.') @@ -1218,7 +1217,7 @@ scripts. *:command-verbose* When 'verbose' is non-zero, listing a command will also display where it was -last defined. Example: > +last defined and any completion argument. Example: > :verbose command TOhtml < Name Args Range Complete Definition ~ @@ -1242,6 +1241,10 @@ See |:verbose-cmd| for more information. :delc[ommand] {cmd} *:delc* *:delcommand* *E184* Delete the user-defined command {cmd}. +:delc[ommand] -buffer {cmd} *E1237* + Delete the user-defined command {cmd} that was defined + for the current buffer. + :comc[lear] *:comc* *:comclear* Delete all user-defined commands. @@ -1333,6 +1336,8 @@ completion can be enabled: -complete=custom,{func} custom completion, defined via {func} -complete=customlist,{func} custom completion, defined via {func} +If you specify completion while there is nothing to complete (-nargs=0, the +default) then you get error *E1208* . Note: That some completion methods might expand environment variables. @@ -1435,6 +1440,9 @@ There are some special cases as well: -register The first argument to the command can be an optional register name (like :del, :put, :yank). -buffer The command will only be available in the current buffer. + -keepscript Do not use the location of where the user command was + defined for verbose messages, use the location of where + the user command was invoked. In the cases of the -count and -register attributes, if the optional argument is supplied, it is removed from the argument list and is available to the diff --git a/runtime/doc/mbyte.txt b/runtime/doc/mbyte.txt index 3bbf36c642..2aa49cee1e 100644 --- a/runtime/doc/mbyte.txt +++ b/runtime/doc/mbyte.txt @@ -489,8 +489,8 @@ Use the RPM or port for your system. window specific to the input method. -USING XIM *multibyte-input* *E284* *E286* *E287* *E288* - *E285* *E289* +USING XIM *multibyte-input* *E284* *E285* *E286* *E287* + *E288* *E289* Note that Display and Input are independent. It is possible to see your language even though you have no input method for it. But when your Display diff --git a/runtime/doc/message.txt b/runtime/doc/message.txt index 6fbd9ec922..fa1bc6f7da 100644 --- a/runtime/doc/message.txt +++ b/runtime/doc/message.txt @@ -27,8 +27,7 @@ depends on the 'shortmess' option. Clear messages, keeping only the {count} most recent ones. -The number of remembered messages is fixed at 20 for the tiny version and 200 -for other versions. +The number of remembered messages is fixed at 200. *g<* The "g<" command can be used to see the last page of previous command output. @@ -112,7 +111,8 @@ wiped out a buffer which contains a mark or is referenced in another way. *E95* > Buffer with this name already exists -You cannot have two buffers with the same name. +You cannot have two buffers with exactly the same name. This includes the +path leading to the file. *E72* > Close error on swap file @@ -513,10 +513,10 @@ If you type "gq", it will execute this mapping, which will call "gq" again. *E22* > Scripts nested too deep -Scripts can be read with the "-s" command-line argument and with the ":source" -command. The script can then again read another script. This can continue -for about 14 levels. When more nesting is done, Vim assumes that there is a -recursive loop somewhere and stops with this error message. +Scripts can be read with the "-s" command-line argument and with the +`:source!` command. The script can then again read another script. This can +continue for about 14 levels. When more nesting is done, Vim assumes that +there is a recursive loop and stops with this error message. *E300* > Swap file already exists (symlink attack?) @@ -686,6 +686,7 @@ Ex command or function was given an invalid argument. Or |jobstart()| or Trailing characters An argument was given to an Ex command that does not permit one. +Or the argument has invalid characters and has not been recognized. *E477* *E478* > No ! allowed diff --git a/runtime/doc/nvim_terminal_emulator.txt b/runtime/doc/nvim_terminal_emulator.txt index bfacbe19f5..f322764ecf 100644 --- a/runtime/doc/nvim_terminal_emulator.txt +++ b/runtime/doc/nvim_terminal_emulator.txt @@ -162,12 +162,11 @@ command name, for example: > This opens two windows: gdb window A terminal window in which "gdb vim" is executed. Here you - can directly interact with gdb. The buffer name is "!gdb". + can directly interact with gdb. program window A terminal window for the executed program. When "run" is used in gdb the program I/O will happen in this window, so - that it does not interfere with controlling gdb. The buffer - name is "gdb program". + that it does not interfere with controlling gdb. The current window is used to show the source code. When gdb pauses the source file location will be displayed, if possible. A sign is used to diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index c929178f5a..6e2bc228d0 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -20,9 +20,13 @@ achieve special effects. These options come in three forms: 1. Setting options *set-option* *E764* *:se* *:set* -:se[t] Show all options that differ from their default value. +:se[t][!] Show all options that differ from their default value. + When [!] is present every option is on a separate + line. -:se[t] all Show all options. +:se[t][!] all Show all options. + When [!] is present every option is on a separate + line. *E518* *E519* :se[t] {option}? Show value of {option}. @@ -235,7 +239,7 @@ happens when the buffer is not loaded, but they are lost when the buffer is wiped out |:bwipe|. *:setl* *:setlocal* -:setl[ocal] ... Like ":set" but set only the value local to the +:setl[ocal][!] ... Like ":set" but set only the value local to the current buffer or window. Not all options have a local value. If the option does not have a local value the global value is set. @@ -257,7 +261,7 @@ wiped out |:bwipe|. {option}, so that the global value will be used. *:setg* *:setglobal* -:setg[lobal] ... Like ":set" but set only the global value for a local +:setg[lobal][!] ... Like ":set" but set only the global value for a local option without changing the local value. When displaying an option, the global value is shown. With the "all" argument: display global values for all @@ -304,7 +308,7 @@ value to the local value, it doesn't switch back to using the global value This will make the local value of 'path' empty, so that the global value is used. Thus it does the same as: > :setlocal path= -Note: In the future more global options can be made global-local. Using +Note: In the future more global options can be made |global-local|. Using ":setlocal" on a global option might work differently then. @@ -686,10 +690,10 @@ A jump table for the options with a short description can be found at |Q_op|. Write the contents of the file, if it has been modified, on each `:next`, `:rewind`, `:last`, `:first`, `:previous`, `:stop`, `:suspend`, `:tag`, `:!`, `:make`, CTRL-] and CTRL-^ command; and when - a :buffer, CTRL-O, CTRL-I, '{A-Z0-9}, or `{A-Z0-9} command takes one + a `:buffer`, CTRL-O, CTRL-I, '{A-Z0-9}, or `{A-Z0-9} command takes one to another file. A buffer is not written if it becomes hidden, e.g. when 'bufhidden' is - set to "hide" and `:next` is used + set to "hide" and `:next` is used. Note that for some commands the 'autowrite' option is not used, see 'autowriteall' for that. Some buffers will not be written, specifically when 'buftype' is @@ -1216,8 +1220,8 @@ A jump table for the options with a short description can be found at |Q_op|. preferred, because it is much faster. 'charconvert' is not used when reading stdin |--|, because there is no file to convert from. You will have to save the text in a file first. - The expression must return zero or an empty string for success, - non-zero for failure. + The expression must return zero, false or an empty string for success, + non-zero or true for failure. See |encoding-names| for possible encoding names. Additionally, names given in 'fileencodings' and 'fileencoding' are used. @@ -2458,7 +2462,8 @@ A jump table for the options with a short description can be found at |Q_op|. < This is similar to the default, except that these characters will also be used when there is highlighting. - for "stl" and "stlnc" only single-byte values are supported. + For "stl" and "stlnc" single-byte and multibyte characters are + supported. But double-width characters are not supported. The highlighting used for these items: item highlight group ~ @@ -4633,26 +4638,11 @@ A jump table for the options with a short description can be found at |Q_op|. nudged to fit on the screen. *'pyxversion'* *'pyx'* -'pyxversion' 'pyx' number (default depends on the build) +'pyxversion' 'pyx' number (default 3) global Specifies the python version used for pyx* functions and commands - |python_x|. The default value is as follows: - - |provider| installed Default ~ - |+python| and |+python3| 0 - only |+python| 2 - only |+python3| 3 - - Available values are 0, 2 and 3. - If 'pyxversion' is 0, it is set to 2 or 3 after the first execution of - any python2/3 commands or functions. E.g. `:py` sets to 2, and `:py3` - sets to 3. `:pyx` sets it to 3 if Python 3 is available, otherwise sets - to 2 if Python 2 is available. - See also: |has-pythonx| - - If only |+python| or |+python3| are available, - 'pyxversion' has no effect. The pyx* functions and commands are - always the same as the installed version. + |python_x|. As only Python 3 is supported, this always has the value + `3`. Setting any other value is an error. This option cannot be set from a |modeline| or in the |sandbox|, for security reasons. @@ -5131,7 +5121,8 @@ A jump table for the options with a short description can be found at |Q_op|. Don't include both "curdir" and "sesdir". When neither is included filenames are stored as absolute paths. - + If you leave out "options" many things won't work well after restoring + the session. *'shada'* *'sd'* *E526* *E527* *E528* 'shada' 'sd' string (Vim default for Win32: !,'100,<50,s10,h,rA:,rB: @@ -5471,7 +5462,7 @@ A jump table for the options with a short description can be found at |Q_op|. flag meaning when present ~ f use "(3 of 5)" instead of "(file 3 of 5)" i use "[noeol]" instead of "[Incomplete last line]" - l use "999L, 888C" instead of "999 lines, 888 characters" + l use "999L, 888B" instead of "999 lines, 888 bytes" m use "[+]" instead of "[Modified]" n use "[New]" instead of "[New File]" r use "[RO]" instead of "[readonly]" @@ -5656,7 +5647,7 @@ A jump table for the options with a short description can be found at |Q_op|. Note regarding 'orphaned signs': with signcolumn numbers higher than 1, deleting lines will also remove the associated signs automatically, in contrast to the default Vim behavior of keeping and grouping them. - This is done in order for the signcolumn appearence not appear weird + This is done in order for the signcolumn appearance not appear weird during line deletion. @@ -6264,10 +6255,11 @@ A jump table for the options with a short description can be found at |Q_op|. 'tabstop' 'ts' number (default 8) local to buffer Number of spaces that a <Tab> in the file counts for. Also see - |:retab| command, and 'softtabstop' option. + the |:retab| command, and the 'softtabstop' option. Note: Setting 'tabstop' to any other value than 8 can make your file - appear wrong in many places (e.g., when printing it). + appear wrong in many places, e.g., when printing it. + The value must be more than 0 and less than 10000. There are four main ways to use tabs in Vim: 1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 @@ -6781,12 +6773,16 @@ A jump table for the options with a short description can be found at |Q_op|. *'virtualedit'* *'ve'* 'virtualedit' 've' string (default "") - global + global or local to window |global-local| A comma separated list of these words: block Allow virtual editing in Visual block mode. insert Allow virtual editing in Insert mode. all Allow virtual editing in all modes. onemore Allow the cursor to move just past the end of the line + none When used as the local value, do not allow virtual + editing even when the global value is set. When used + as the global value, "none" is the same as "". + NONE Alternative spelling of "none". Virtual editing means that the cursor can be positioned where there is no actual character. This can be halfway into a tab or beyond the end diff --git a/runtime/doc/pattern.txt b/runtime/doc/pattern.txt index dfed39dba6..42005b0d78 100644 --- a/runtime/doc/pattern.txt +++ b/runtime/doc/pattern.txt @@ -304,7 +304,7 @@ the pattern. ============================================================================== 2. The definition of a pattern *search-pattern* *pattern* *[pattern]* *regular-expression* *regexp* *Pattern* - *E76* *E383* *E476* + *E383* *E476* For starters, read chapter 27 of the user manual |usr_27.txt|. @@ -1036,6 +1036,8 @@ match ASCII characters, as indicated by the range. \(\) A pattern enclosed by escaped parentheses. */\(* */\(\)* */\)* E.g., "\(^a\)" matches 'a' at the start of a line. + There can only be ten of these. You can use "\%(" to add more, but + not counting it as a sub-expression. *E51* *E54* *E55* *E872* *E873* \1 Matches the same string that was matched by */\1* *E65* @@ -1058,7 +1060,7 @@ x A single character, with no special meaning, matches itself \x A backslash followed by a single character, with no special meaning, is reserved for future expansions -[] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection* +[] (with 'nomagic': \[]) */[]* */\[]* */\_[]* */collection* *E76* \_[] A collection. This is a sequence of characters enclosed in square brackets. It matches any single character in the collection. @@ -1419,5 +1421,38 @@ Finally, these constructs are unique to Perl: are suggested to use ":match" for manual matching and ":2match" for another plugin. +============================================================================== +11. Fuzzy matching *fuzzy-match* + +Fuzzy matching refers to matching strings using a non-exact search string. +Fuzzy matching will match a string, if all the characters in the search string +are present anywhere in the string in the same order. Case is ignored. In a +matched string, other characters can be present between two consecutive +characters in the search string. If the search string has multiple words, then +each word is matched separately. So the words in the search string can be +present in any order in a string. + +Fuzzy matching assigns a score for each matched string based on the following +criteria: + - The number of sequentially matching characters. + - The number of characters (distance) between two consecutive matching + characters. + - Matches at the beginning of a word + - Matches at a camel case character (e.g. Case in CamelCase) + - Matches after a path separator or a hyphen. + - The number of unmatched characters in a string. +The matching string with the highest score is returned first. + +For example, when you search for the "get pat" string using fuzzy matching, it +will match the strings "GetPattern", "PatternGet", "getPattern", "patGetter", +"getSomePattern", "MatchpatternGet" etc. + +The functions |matchfuzzy()| and |matchfuzzypos()| can be used to fuzzy search +a string in a List of strings. The matchfuzzy() function returns a List of +matching strings. The matchfuzzypos() functions returns the List of matches, +the matching positions and the fuzzy match scores. + +The "f" flag of `:vimgrep` enables fuzzy matching. + vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/runtime/doc/pi_msgpack.txt b/runtime/doc/pi_msgpack.txt index 1dbd268038..801c56e49f 100644 --- a/runtime/doc/pi_msgpack.txt +++ b/runtime/doc/pi_msgpack.txt @@ -88,7 +88,7 @@ msgpack#type({msgpack-value}) *msgpack#type()* Returns name of the key in |v:msgpack_types| that represents {msgpack-value} type. Never returns zero: this function returns msgpack type which will be dumped by |msgpackdump()| should it receive - a list with singe {msgpack-value} as input. + a list with single {msgpack-value} as input. msgpack#deepcopy({msgpack-value}) *msgpack#deepcopy()* Like |deepcopy()|, but works correctly with |msgpack-special-dict| diff --git a/runtime/doc/provider.txt b/runtime/doc/provider.txt index b785010699..9fd35f19c5 100644 --- a/runtime/doc/provider.txt +++ b/runtime/doc/provider.txt @@ -20,11 +20,12 @@ Run the |:checkhealth| command, and review the sections below. ============================================================================== Python integration *provider-python* -Nvim supports Python |remote-plugin|s and the Vim legacy |python2| and -|python3| interfaces (which are implemented as remote-plugins). +Nvim supports Python |remote-plugin|s and the Vim legacy |python3| and +|pythonx| interfaces (which are implemented as remote-plugins). Note: Only the Vim 7.3 legacy interface is supported, not later features such -as |python-bindeval| (Vim 7.4); use the Nvim API instead. +as |python-bindeval| (Vim 7.4); use the Nvim API instead. Python 2 is not +supported. PYTHON QUICKSTART ~ @@ -38,11 +39,6 @@ For Python 3 plugins: 2. Install the module (try "python" if "python3" is missing): > python3 -m pip install --user --upgrade pynvim -For Python 2 plugins: -1. Make sure Python 2.7 is available in your $PATH. -2. Install the module (try "python" if "python2" is missing): > - python2 -m pip install --user --upgrade pynvim - The pip `--upgrade` flag ensures that you get the latest version even if a previous version was already installed. @@ -56,22 +52,12 @@ If you run into problems, uninstall _both_ then install "pynvim" again: > PYTHON PROVIDER CONFIGURATION ~ - *g:python_host_prog* -Command to start Python 2 (executable, not directory). Setting this makes -startup faster. Useful for working with virtualenvs. Must be set before any -check for has("python2"). > - let g:python_host_prog = '/path/to/python' -< *g:python3_host_prog* Command to start Python 3 (executable, not directory). Setting this makes startup faster. Useful for working with virtualenvs. Must be set before any check for has("python3"). > let g:python3_host_prog = '/path/to/python3' < - *g:loaded_python_provider* -To disable Python 2 support: > - let g:loaded_python_provider = 0 -< *g:loaded_python3_provider* To disable Python 3 support: > let g:loaded_python3_provider = 0 @@ -81,8 +67,8 @@ PYTHON VIRTUALENVS ~ *python-virtualenv* If you plan to use per-project virtualenvs often, you should assign one virtualenv for Neovim and hard-code the interpreter path via -|g:python3_host_prog| (or |g:python_host_prog|) so that the "pynvim" package -is not required for each virtualenv. +|g:python3_host_prog| so that the "pynvim" package is not required +for each virtualenv. Example using pyenv: > pyenv install 3.4.4 diff --git a/runtime/doc/quickfix.txt b/runtime/doc/quickfix.txt index ba1da209f7..5b68da8be9 100644 --- a/runtime/doc/quickfix.txt +++ b/runtime/doc/quickfix.txt @@ -232,7 +232,7 @@ processing a quickfix or location list command, it will be aborted. ":qall!" |:qall|, except that Nvim exits non-zero or [count]. - *:cf* *:cfile* + *:cf* *:cfi* *:cfile* :cf[ile][!] [errorfile] Read the error file and jump to the first error. This is done automatically when Vim is started with the -q option. You can use this command when you @@ -989,7 +989,7 @@ commands can be combined to create a NewGrep command: > 5.1 using Vim's internal grep *:vim* *:vimgrep* *E682* *E683* -:vim[grep][!] /{pattern}/[g][j] {file} ... +:vim[grep][!] /{pattern}/[g][j][f] {file} ... Search for {pattern} in the files {file} ... and set the error list to the matches. Files matching 'wildignore' are ignored; files in 'suffixes' are @@ -1014,6 +1014,13 @@ commands can be combined to create a NewGrep command: > updated. With the [!] any changes in the current buffer are abandoned. + 'f' When the 'f' flag is specified, fuzzy string + matching is used to find matching lines. In this + case, {pattern} is treated as a literal string + instead of a regular expression. See + |fuzzy-match| for more information about fuzzy + matching strings. + |QuickFixCmdPre| and |QuickFixCmdPost| are triggered. A file that is opened for matching may use a buffer number, but it is reused if possible to avoid @@ -1042,20 +1049,20 @@ commands can be combined to create a NewGrep command: > :vimgrep Error *.c < *:lv* *:lvimgrep* -:lv[imgrep][!] /{pattern}/[g][j] {file} ... +:lv[imgrep][!] /{pattern}/[g][j][f] {file} ... :lv[imgrep][!] {pattern} {file} ... Same as ":vimgrep", except the location list for the current window is used instead of the quickfix list. *:vimgrepa* *:vimgrepadd* -:vimgrepa[dd][!] /{pattern}/[g][j] {file} ... +:vimgrepa[dd][!] /{pattern}/[g][j][f] {file} ... :vimgrepa[dd][!] {pattern} {file} ... Just like ":vimgrep", but instead of making a new list of errors the matches are appended to the current list. *:lvimgrepa* *:lvimgrepadd* -:lvimgrepa[dd][!] /{pattern}/[g][j] {file} ... +:lvimgrepa[dd][!] /{pattern}/[g][j][f] {file} ... :lvimgrepa[dd][!] {pattern} {file} ... Same as ":vimgrepadd", except the location list for the current window is used instead of the quickfix @@ -1332,12 +1339,17 @@ Basic items %f file name (finds a string) %o module name (finds a string) %l line number (finds a number) + %e end line number (finds a number) %c column number (finds a number representing character column of the error, byte index, a <tab> is 1 character column) %v virtual column number (finds a number representing screen column of the error (1 <tab> == 8 screen columns)) + %k end column number (finds a number representing + the character column of the error, byte index, or a + number representing screen end column of the error if + it's used with %v) %t error type (finds a single character): e - error message w - warning message @@ -1446,7 +1458,7 @@ error message (line numbers are not part of the actual output): 4 Traceback (most recent call last): 5 File "unittests/dbfacadeTest.py", line 89, in testFoo 6 self.assertEquals(34, dtid) - 7 File "/usr/lib/python2.2/unittest.py", line 286, in + 7 File "/usr/lib/python3.8/unittest.py", line 286, in 8 failUnlessEqual 9 raise self.failureException, \ 10 AssertionError: 34 != 33 diff --git a/runtime/doc/repeat.txt b/runtime/doc/repeat.txt index c7481ad290..a022049766 100644 --- a/runtime/doc/repeat.txt +++ b/runtime/doc/repeat.txt @@ -465,8 +465,8 @@ flag when defining the function, it is not relevant when executing it. > :set cpo-=C < *line-continuation-comment* -To add a comment in between the lines start with '\" '. Notice the space -after the double quote. Example: > +To add a comment in between the lines start with '"\ '. Notice the space +after the backslash. Example: > let array = [ "\ first entry comment \ 'first', diff --git a/runtime/doc/starting.txt b/runtime/doc/starting.txt index bb775ec884..978142a1e0 100644 --- a/runtime/doc/starting.txt +++ b/runtime/doc/starting.txt @@ -188,7 +188,7 @@ argument. changes and writing. -e *-e* *-E* --E Start Nvim in Ex mode |gQ|. +-E Start Nvim in Ex mode |gQ|, see |Ex-mode|. If stdin is not a TTY: -e reads/executes stdin as Ex commands. @@ -409,7 +409,12 @@ accordingly, proceeding as follows: 4. Setup |default-mappings| and |default-autocmds|. -5. Load user config (execute Ex commands from files, environment, …). +5. Enable filetype and indent plugins. + This does the same as the command: > + :runtime! ftplugin.vim indent.vim +< Skipped if the "-u NONE" command line argument was given. + +6. Load user config (execute Ex commands from files, environment, …). $VIMINIT environment variable is read as one Ex command line (separate multiple commands with '|' or <NL>). *config* *init.vim* *init.lua* *vimrc* *exrc* @@ -453,21 +458,19 @@ accordingly, proceeding as follows: - The file ".nvimrc" - The file ".exrc" -6. Enable filetype and indent plugins. - This does the same as the commands: > - :runtime! filetype.vim - :runtime! ftplugin.vim - :runtime! indent.vim -< Skipped if ":filetype … off" was called or if the "-u NONE" command - line argument was given. +7. Enable filetype detection. + This does the same as the command: > + :runtime! filetype.lua filetype.vim +< Skipped if ":filetype off" was called or if the "-u NONE" command line + argument was given. -7. Enable syntax highlighting. +8. Enable syntax highlighting. This does the same as the command: > :runtime! syntax/syntax.vim < Skipped if ":syntax off" was called or if the "-u NONE" command line argument was given. -8. Load the plugin scripts. *load-plugins* +9. Load the plugin scripts. *load-plugins* This does the same as the command: > :runtime! plugin/**/*.vim :runtime! plugin/**/*.lua @@ -497,21 +500,21 @@ accordingly, proceeding as follows: if packages have been found, but that should not add a directory ending in "after". -9. Set 'shellpipe' and 'shellredir' +10. Set 'shellpipe' and 'shellredir' The 'shellpipe' and 'shellredir' options are set according to the value of the 'shell' option, unless they have been set before. This means that Nvim will figure out the values of 'shellpipe' and 'shellredir' for you, unless you have set them yourself. -10. Set 'updatecount' to zero, if "-n" command argument used +11. Set 'updatecount' to zero, if "-n" command argument used -11. Set binary options if the |-b| flag was given. +12. Set binary options if the |-b| flag was given. -12. Read the |shada-file|. +13. Read the |shada-file|. -13. Read the quickfix file if the |-q| flag was given, or exit on failure. +14. Read the quickfix file if the |-q| flag was given, or exit on failure. -14. Open all windows +15. Open all windows When the |-o| flag was given, windows will be opened (but not displayed yet). When the |-p| flag was given, tab pages will be created (but not @@ -521,7 +524,7 @@ accordingly, proceeding as follows: Buffers for all windows will be loaded, without triggering |BufAdd| autocommands. -15. Execute startup commands +16. Execute startup commands If a |-t| flag was given, the tag is jumped to. Commands given with |-c| and |+cmd| are executed. If the 'insertmode' option is set, Insert mode is entered. diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 0fd481cd83..319a715e40 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -920,12 +920,16 @@ in .../after/syntax/baan.vim (see |after-directory|). Eg: > BASIC *basic.vim* *vb.vim* *ft-basic-syntax* *ft-vb-syntax* -Both Visual Basic and "normal" basic use the extension ".bas". To detect +Both Visual Basic and "normal" BASIC use the extension ".bas". To detect which one should be used, Vim checks for the string "VB_Name" in the first five lines of the file. If it is not found, filetype will be "basic", otherwise "vb". Files with the ".frm" extension will always be seen as Visual Basic. +If the automatic detection doesn't work for you or you only edit, for +example, FreeBASIC files, use this in your startup vimrc: > + :let filetype_bas = "freebasic" + C *c.vim* *ft-c-syntax* @@ -1406,7 +1410,7 @@ add the following line to your startup file: > :let g:filetype_euphoria = "euphoria4" -Elixir and Euphoria share the *.ex file extension. If the filetype is +Elixir and Euphoria share the *.ex file extension. If the filetype is specifically set as Euphoria with the g:filetype_euphoria variable, or the file is determined to be Euphoria based on keywords in the file, then the filetype will be set as Euphoria. Otherwise, the filetype will default to @@ -1437,7 +1441,7 @@ The following file extensions are auto-detected as Elixir file types: *.ex, *.exs, *.eex, *.leex, *.lock -Elixir and Euphoria share the *.ex file extension. If the filetype is +Elixir and Euphoria share the *.ex file extension. If the filetype is specifically set as Euphoria with the g:filetype_euphoria variable, or the file is determined to be Euphoria based on keywords in the file, then the filetype will be set as Euphoria. Otherwise, the filetype will default to @@ -5402,7 +5406,7 @@ To test your color setup, a file has been included in the Vim distribution. To use it, execute this command: > :runtime syntax/colortest.vim -Nvim uses 256-color and |true-color| terminal capabilities whereever possible. +Nvim uses 256-color and |true-color| terminal capabilities wherever possible. ============================================================================== 18. When syntax is slow *:syntime* diff --git a/runtime/doc/term.txt b/runtime/doc/term.txt index 935d958729..62e13285f5 100644 --- a/runtime/doc/term.txt +++ b/runtime/doc/term.txt @@ -133,7 +133,7 @@ capabilities as if they had been in the terminfo definition. If terminfo does not (yet) have this flag, Nvim will fall back to $TERM and other environment variables. It will add constructed "setrgbf" and "setrgbb" -capabilities in the case of the the "rxvt", "linux", "st", "tmux", and "iterm" +capabilities in the case of the "rxvt", "linux", "st", "tmux", and "iterm" terminal types, or when Konsole, genuine Xterm, a libvte terminal emulator version 0.36 or later, or a terminal emulator that sets the COLORTERM environment variable to "truecolor" is detected. diff --git a/runtime/doc/testing.txt b/runtime/doc/testing.txt index 8ec66d26a4..4e4a908d0f 100644 --- a/runtime/doc/testing.txt +++ b/runtime/doc/testing.txt @@ -12,7 +12,7 @@ and for testing plugins. 1. Testing Vim |testing| 2. Test functions |test-functions-details| -3. Assert funtions |assert-functions-details| +3. Assert functions |assert-functions-details| ============================================================================== 1. Testing Vim *testing* diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index 5f238fb2ff..5829dbdd6b 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -210,7 +210,7 @@ Here is a list of built-in predicates : < `match?` *ts-predicate-match?* `vim-match?` *ts-predicate-vim-match?* - This will match if the provived vim regex matches the text + This will match if the provided vim regex matches the text corresponding to a node : > ((identifier) @constant (#match? @constant "^[A-Z_]+$")) < Note: the `^` and `$` anchors will respectively match the @@ -467,8 +467,9 @@ parse_query({lang}, {query}) *parse_query()* • `info.patterns` contains information about predicates. Parameters: ~ - {lang} The language - {query} A string containing the query (s-expr syntax) + {lang} string The language + {query} string A string containing the query (s-expr + syntax) Return: ~ The query @@ -665,7 +666,7 @@ LanguageTree:invalidate({self}, {reload}) *LanguageTree:invalidate()* {self} LanguageTree:is_valid({self}) *LanguageTree:is_valid()* - Determines whether this tree is valid. If the tree is invalid, `parse()` must be called to get the an updated tree. + Determines whether this tree is valid. If the tree is invalid, `parse()` must be called to get the updated tree. Parameters: ~ {self} diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index 7e611a47f3..bf024315f6 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -549,7 +549,7 @@ A "&" character is prepended to "path", thus the argument to eval() is Vim defines many functions and provides a large amount of functionality that way. A few examples will be given in this section. You can find the whole -list here: |functions|. +list below: |function-list|. A function is called with the ":call" command. The parameters are passed in between parentheses separated by commas. Example: > @@ -588,8 +588,8 @@ after the substitute() call. FUNCTIONS *function-list* There are many functions. We will mention them here, grouped by what they are -used for. You can find an alphabetical list here: |functions|. Use CTRL-] on -the function name to jump to detailed help on it. +used for. You can find an alphabetical list here: |builtin-function-list|. +Use CTRL-] on the function name to jump to detailed help on it. String manipulation: *string-functions* nr2char() get a character by its number value @@ -608,6 +608,8 @@ String manipulation: *string-functions* toupper() turn a string to uppercase match() position where a pattern matches in a string matchend() position where a pattern match ends in a string + matchfuzzy() fuzzy matches a string in a list of strings + matchfuzzypos() fuzzy matches a string in a list of strings matchstr() match of a pattern in a string matchstrpos() match and positions of a pattern in a string matchlist() like matchstr() and also return submatches @@ -746,6 +748,11 @@ Cursor and mark position: *cursor-functions* *mark-functions* screenchar() get character code at a screen line/row screenchars() get character codes at a screen line/row screenstring() get string of characters at a screen line/row + charcol() character number of the cursor or a mark + getcharpos() get character position of cursor, mark, etc. + setcharpos() set character position of cursor, mark, etc. + getcursorcharpos() get character position of the cursor + setcursorcharpos() set character position of the cursor Working with text in the current buffer: *text-functions* getline() get a line or list of lines from the buffer @@ -837,6 +844,8 @@ Buffers, windows and the argument list: win_gotoid() go to window with ID win_id2tabwin() get tab and window nr from window ID win_id2win() get window nr from window ID + win_move_separator() move window vertical separator + win_move_statusline() move window status line getbufinfo() get a list with buffer information gettabinfo() get a list with tab page information getwininfo() get a list with window information diff --git a/runtime/doc/various.txt b/runtime/doc/various.txt index 8a4468a130..38869f8e94 100644 --- a/runtime/doc/various.txt +++ b/runtime/doc/various.txt @@ -249,7 +249,7 @@ g8 Print the hex values of the bytes used in the To enter |Terminal-mode| automatically: > autocmd TermOpen * startinsert < - *:!cmd* *:!* *E34* + *:!cmd* *:!* :!{cmd} Execute {cmd} with 'shell'. See also |:terminal|. The command runs in a non-interactive shell connected @@ -261,6 +261,7 @@ g8 Print the hex values of the bytes used in the Use |jobstart()| instead. > :call jobstart('foo', {'detach':1}) < + *E34* Any "!" in {cmd} is replaced with the previous external command (see also 'cpoptions'), unless escaped by a backslash. Example: ":!ls" followed by @@ -357,19 +358,19 @@ g8 Print the hex values of the bytes used in the :redi[r] END End redirecting messages. *:filt* *:filter* -:filt[er][!] {pat} {command} -:filt[er][!] /{pat}/ {command} +:filt[er][!] {pattern} {command} +:filt[er][!] /{pattern}/ {command} Restrict the output of {command} to lines matching - with {pat}. For example, to list only xml files: > + with {pattern}. For example, to list only xml files: > :filter /\.xml$/ oldfiles < If the [!] is given, restrict the output of {command} - to lines that do NOT match {pat}. + to lines that do NOT match {pattern}. - {pat} is a Vim search pattern. Instead of enclosing + {pattern} is a Vim search pattern. Instead of enclosing it in / any non-ID character (see |'isident'|) can be - used, so long as it does not appear in {pat}. Without - the enclosing character the pattern cannot include the - bar character. 'ignorecase' is not used. + used, so long as it does not appear in {pattern}. + Without the enclosing character the pattern cannot + include the bar character. 'ignorecase' is not used. The pattern is matched against the relevant part of the output, not necessarily the whole line. Only some @@ -387,6 +388,8 @@ g8 Print the hex values of the bytes used in the |:marks| - filter by text in the current file, or file name for other files |:oldfiles| - filter by file name + |:registers| - filter by register contents + (does not work multi-line) |:set| - filter by option name Only normal messages are filtered, error messages are diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index 4fcaf15717..7892b82137 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -23,8 +23,10 @@ centralized reference of the differences. ============================================================================== 2. Defaults *nvim-defaults* -- Syntax highlighting is enabled by default -- ":filetype plugin indent on" is enabled by default +- Filetype detection is enabled by default. This can be disabled by adding + ":filetype off" to |init.vim|. +- Syntax highlighting is enabled by default. This can be disabled by adding + ":syntax off" to |init.vim|. - 'autoindent' is enabled - 'autoread' is enabled @@ -320,6 +322,8 @@ coerced to strings. See |id()| for more details, currently it uses |c_CTRL-R| pasting a non-special register into |cmdline| omits the last <CR>. +|CursorMoved| always triggers when moving between windows. + Lua interface (|lua.txt|): - `:lua print("a\0b")` will print `a^@b`, like with `:echomsg "a\nb"` . In Vim @@ -481,7 +485,6 @@ Commands: :tearoff Compile-time features: - EBCDIC Emacs tags support X11 integration (see |x11-selection|) @@ -572,6 +575,7 @@ Test functions: test_scrollbar() test_setmouse() test_settime() + test_srand_seed() TUI: *t_xx* *termcap-options* *t_AB* *t_Sb* *t_vb* *t_SI* diff --git a/runtime/doc/visual.txt b/runtime/doc/visual.txt index 4a69fc989b..4d5366a41a 100644 --- a/runtime/doc/visual.txt +++ b/runtime/doc/visual.txt @@ -255,6 +255,7 @@ Additionally the following commands can be used: X delete (2) |v_X| Y yank (2) |v_Y| p put |v_p| + P put without unnamed register overwrite |v_P| J join (1) |v_J| U make uppercase |v_U| u make lowercase |v_u| @@ -478,6 +479,10 @@ Commands in Select mode: - ESC stops Select mode. - CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O* - CTRL-G switches to Visual mode. +- CTRL-R {register} selects the register to be used for the text that is + deleted when typing text. *v_CTRL-R* + Unless you specify the "_" (black hole) register, the unnamed register is + also overwritten. Otherwise, typed characters are handled as in Visual mode. diff --git a/runtime/doc/windows.txt b/runtime/doc/windows.txt index bb31895c96..5b91321c40 100644 --- a/runtime/doc/windows.txt +++ b/runtime/doc/windows.txt @@ -124,7 +124,7 @@ highlight group (|hl-EndOfBuffer|) can be used to change the highlighting of the filler characters. ============================================================================== -3. Opening and closing a window *opening-window* *E36* +3. Opening and closing a window *opening-window* CTRL-W s *CTRL-W_s* CTRL-W S *CTRL-W_S* @@ -223,6 +223,10 @@ CTRL-W ge *CTRL-W_ge* Note that the 'splitbelow' and 'splitright' options influence where a new window will appear. + *E36* +Creating a window will fail if there is not enough room. Every window needs +at least one screen line and column, sometimes more. Options 'winminheight' +and 'winminwidth' are relevant. *:vert* *:vertical* :vert[ical] {cmd} diff --git a/runtime/filetype.lua b/runtime/filetype.lua new file mode 100644 index 0000000000..fcfc5701f0 --- /dev/null +++ b/runtime/filetype.lua @@ -0,0 +1,26 @@ +if vim.g.did_load_filetypes and vim.g.did_load_filetypes ~= 0 then + return +end + +-- For now, make this opt-in with a global variable +if vim.g.do_filetype_lua ~= 1 then + return +end + +vim.cmd [[ +augroup filetypedetect +au BufRead,BufNewFile * call v:lua.vim.filetype.match(expand('<afile>')) + +" These *must* be sourced after the autocommand above is created +runtime! ftdetect/*.vim +runtime! ftdetect/*.lua + +" Set a marker so that the ftdetect scripts are not sourced a second time by filetype.vim +let g:did_load_ftdetect = 1 + +augroup END +]] + +if not vim.g.ft_ignore_pat then + vim.g.ft_ignore_pat = "\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$" +end diff --git a/runtime/filetype.vim b/runtime/filetype.vim index 07969b3418..35f4b25120 100644 --- a/runtime/filetype.vim +++ b/runtime/filetype.vim @@ -1,7 +1,7 @@ " Vim support file to detect file types " " Maintainer: Bram Moolenaar <Bram@vim.org> -" Last Change: 2021 Dec 27 +" Last Change: 2022 Jan 31 " Listen very carefully, I will say this only once if exists("did_load_filetypes") @@ -189,7 +189,8 @@ au BufNewFile,BufRead *.awk,*.gawk setf awk au BufNewFile,BufRead *.mch,*.ref,*.imp setf b " BASIC or Visual Basic -au BufNewFile,BufRead *.bas call dist#ft#FTVB("basic") +au BufNewFile,BufRead *.bas call dist#ft#FTbas() +au BufNewFile,BufRead *.bi,*.bm call dist#ft#FTbas() " Visual Basic Script (close to Visual Basic) or Visual Basic .NET au BufNewFile,BufRead *.vb,*.vbs,*.dsm,*.ctl setf vb @@ -198,7 +199,7 @@ au BufNewFile,BufRead *.vb,*.vbs,*.dsm,*.ctl setf vb au BufNewFile,BufRead *.iba,*.ibi setf ibasic " FreeBasic file (similar to QBasic) -au BufNewFile,BufRead *.fb,*.bi setf freebasic +au BufNewFile,BufRead *.fb setf freebasic " Batch file for MSDOS. au BufNewFile,BufRead *.bat,*.sys setf dosbatch @@ -493,7 +494,7 @@ au BufNewFile,BufRead */debian/patches/* call dist#ft#Dep3patch() " Diff files au BufNewFile,BufRead *.diff,*.rej setf diff au BufNewFile,BufRead *.patch - \ if getline(1) =~ '^From [0-9a-f]\{40\} Mon Sep 17 00:00:00 2001$' | + \ if getline(1) =~# '^From [0-9a-f]\{40,\} Mon Sep 17 00:00:00 2001$' | \ setf gitsendemail | \ else | \ setf diff | @@ -649,6 +650,9 @@ au BufNewFile,BufRead *.fsl setf framescript " FStab au BufNewFile,BufRead fstab,mtab setf fstab +" Fusion +au BufRead,BufNewFile *.fusion setf fusion + " F# or Forth au BufNewFile,BufRead *.fs call dist#ft#FTfs() @@ -661,6 +665,12 @@ au BufNewFile,BufRead .gdbinit,gdbinit setf gdb " GDMO au BufNewFile,BufRead *.mo,*.gdmo setf gdmo +" GDscript +au BufNewFile,BufRead *.gd setf gdscript + +" Godot resource +au BufRead,BufNewFile *.tscn,*.tres setf gdresource + " Gedcom au BufNewFile,BufRead *.ged,lltxxxxx.txt setf gedcom @@ -672,26 +682,28 @@ autocmd BufRead,BufNewFile *.gift setf gift " Git au BufNewFile,BufRead COMMIT_EDITMSG,MERGE_MSG,TAG_EDITMSG setf gitcommit -au BufNewFile,BufRead *.git/config,.gitconfig,/etc/gitconfig setf gitconfig +au BufNewFile,BufRead NOTES_EDITMSG,EDIT_DESCRIPTION setf gitcommit +au BufNewFile,BufRead *.git/config,.gitconfig,*/etc/gitconfig setf gitconfig au BufNewFile,BufRead */.config/git/config setf gitconfig +au BufNewFile,BufRead *.git/config.worktree setf gitconfig +au BufNewFile,BufRead *.git/worktrees/*/config.worktree setf gitconfig au BufNewFile,BufRead .gitmodules,*.git/modules/*/config setf gitconfig if !empty($XDG_CONFIG_HOME) au BufNewFile,BufRead $XDG_CONFIG_HOME/git/config setf gitconfig endif au BufNewFile,BufRead git-rebase-todo setf gitrebase au BufRead,BufNewFile .gitsendemail.msg.?????? setf gitsendemail -au BufNewFile,BufRead .msg.[0-9]* - \ if getline(1) =~ '^From.*# This line is ignored.$' | - \ setf gitsendemail | - \ endif au BufNewFile,BufRead *.git/* - \ if getline(1) =~ '^\x\{40\}\>\|^ref: ' | + \ if getline(1) =~# '^\x\{40,\}\>\|^ref: ' | \ setf git | \ endif " Gkrellmrc au BufNewFile,BufRead gkrellmrc,gkrellmrc_? setf gkrellmrc +" GLSL +au BufNewFile,BufRead *.glsl setf glsl + " GP scripts (2.0 and onward) au BufNewFile,BufRead *.gp,.gprc setf gp @@ -712,15 +724,19 @@ au BufNewFile,BufRead gitolite.conf setf gitolite au BufNewFile,BufRead {,.}gitolite.rc,example.gitolite.rc setf perl " Gnuplot scripts -au BufNewFile,BufRead *.gpi setf gnuplot +au BufNewFile,BufRead *.gpi,.gnuplot setf gnuplot " Go (Google) au BufNewFile,BufRead *.go setf go au BufNewFile,BufRead Gopkg.lock setf toml +au BufRead,BufNewFile go.work setf gowork " GrADS scripts au BufNewFile,BufRead *.gs setf grads +" GraphQL +au BufNewFile,BufRead *.graphql,*.graphqls,*.gql setf graphql + " Gretl au BufNewFile,BufRead *.gretl setf gretl @@ -736,12 +752,18 @@ au BufNewFile,BufRead */etc/group,*/etc/group-,*/etc/group.edit,*/etc/gshadow,*/ " GTK RC au BufNewFile,BufRead .gtkrc,gtkrc setf gtkrc +" Hack +au BufRead,BufNewFile *.hack,*.hackpartial setf hack + " Haml au BufNewFile,BufRead *.haml setf haml " Hamster Classic | Playground files au BufNewFile,BufRead *.hsm setf hamster +" Handlebars +au BufNewFile,BufRead *.hbs setf handlebars + " Haskell au BufNewFile,BufRead *.hs,*.hsc,*.hs-boot,*.hsig setf haskell au BufNewFile,BufRead *.lhs setf lhaskell @@ -754,12 +776,21 @@ au BufNewFile,BufRead cabal.config setf cabalconfig au BufNewFile,BufRead *.ht setf haste au BufNewFile,BufRead *.htpp setf hastepreproc +" HCL +au BufRead,BufNewFile *.hcl setf hcl + " Hercules au BufNewFile,BufRead *.vc,*.ev,*.sum,*.errsum setf hercules +" HEEx +au BufRead,BufNewFile *.heex setf heex + " HEX (Intel) au BufNewFile,BufRead *.hex,*.h32 setf hex +" Hjson +au BufNewFile,BufRead *.hjson setf hjson + " Hollywood au BufRead,BufNewFile *.hws setf hollywood @@ -884,6 +915,9 @@ au BufNewFile,BufRead *.jov,*.j73,*.jovial setf jovial " JSON au BufNewFile,BufRead *.json,*.jsonp,*.webmanifest setf json +" JSON5 +au BufNewFile,BufRead *.json5 setf json5 + " JSON Patch (RFC 6902) au BufNewFile,BufRead *.json-patch setf json @@ -935,6 +969,9 @@ au BufNewFile,BufRead *.ldif setf ldif " Ld loader au BufNewFile,BufRead *.ld setf ld +" Ledger +au BufRead,BufNewFile *.ldg,*.ledger,*.journal setf ledger + " Less au BufNewFile,BufRead *.less setf less @@ -1172,6 +1209,9 @@ au BufNewFile,BufRead *.nginx,nginx*.conf,*nginx.conf,*/etc/nginx/*,*/usr/local/ " Ninja file au BufNewFile,BufRead *.ninja setf ninja +" Nix +au BufRead,BufNewFile *.nix setf nix + " NPM RC file au BufNewFile,BufRead npmrc,.npmrc setf dosini @@ -1213,6 +1253,9 @@ au BufNewFile,BufRead *.xom,*.xin setf omnimark " OPAM au BufNewFile,BufRead opam,*.opam,*.opam.template setf opam +" OpenFOAM +au BufNewFile,BufRead [a-zA-Z0-9]*Dict\(.*\)\=,[a-zA-Z]*Properties\(.*\)\=,*Transport\(.*\),fvSchemes,fvSolution,fvConstrains,fvModels,*/constant/g,*/0\(\.orig\)\=/* call dist#ft#FTfoam() + " OpenROAD au BufNewFile,BufRead *.or setf openroad @@ -1354,6 +1397,9 @@ au BufNewFile,BufRead *printcap au BufNewFile,BufRead *termcap \ let b:ptcap_type = "term" | setf ptcap +" Prisma +au BufRead,BufNewFile *.prisma setf prisma + " PCCTS / ANTLR "au BufNewFile,BufRead *.g setf antlr au BufNewFile,BufRead *.g setf pccts @@ -1361,6 +1407,9 @@ au BufNewFile,BufRead *.g setf pccts " PPWizard au BufNewFile,BufRead *.it,*.ih setf ppwiz +" Pug +au BufRead,BufNewFile *.pug setf pug + " Puppet au BufNewFile,BufRead Puppetfile setf ruby @@ -1426,6 +1475,9 @@ au BufNewFile,BufRead *.pyx,*.pxd setf pyrex au BufNewFile,BufRead *.py,*.pyw,.pythonstartup,.pythonrc setf python au BufNewFile,BufRead *.ptl,*.pyi,SConstruct setf python +" QL +au BufRead,BufNewFile *.ql,*.qll setf ql + " Radiance au BufNewFile,BufRead *.rad,*.mat setf radiance @@ -1491,6 +1543,9 @@ au BufNewFile,BufRead *.r,*.R call dist#ft#FTr() " Remind au BufNewFile,BufRead .reminders,*.remind,*.rem setf remind +" ReScript +au BufNewFile,BufRead *.res,*.resi setf rescript + " Resolv.conf au BufNewFile,BufRead resolv.conf setf resolv @@ -1830,6 +1885,9 @@ au BufNewFile,BufRead */etc/sudoers,sudoers.tmp setf sudoers " SVG (Scalable Vector Graphics) au BufNewFile,BufRead *.svg setf svg +" Surface +au BufRead,BufNewFile *.sface setf surface + " Tads (or Nroff or Perl test file) au BufNewFile,BufRead *.t \ if !dist#ft#FTnroff() && !dist#ft#FTperl() | setf tads | endif @@ -1847,6 +1905,9 @@ au BufRead,BufNewFile *.task setf taskedit " Tcl (JACL too) au BufNewFile,BufRead *.tcl,*.tm,*.tk,*.itcl,*.itk,*.jacl,.tclshrc,.wishrc setf tcl +" Teal +au BufRead,BufNewFile *.tl setf teal + " TealInfo au BufNewFile,BufRead *.tli setf tli @@ -1864,6 +1925,9 @@ au BufRead,BufNewFile *.ttl " Terminfo au BufNewFile,BufRead *.ti setf terminfo +" Terraform +au BufRead,BufNewFile *.tfvars setf terraform + " TeX au BufNewFile,BufRead *.latex,*.sty,*.dtx,*.ltx,*.bbl setf tex au BufNewFile,BufRead *.tex call dist#ft#FTtex() @@ -1881,7 +1945,13 @@ au BufNewFile,BufRead texmf.cnf setf texmf au BufNewFile,BufRead .tidyrc,tidyrc,tidy.conf setf tidy " TF mud client -au BufNewFile,BufRead *.tf,.tfrc,tfrc setf tf +au BufNewFile,BufRead .tfrc,tfrc setf tf + +" TF mud client or terraform +au BufNewFile,BufRead *.tf call dist#ft#FTtf() + +" TLA+ +au BufNewFile,BufRead *.tla setf tla " tmux configuration au BufNewFile,BufRead {.,}tmux*.conf setf tmux @@ -1890,7 +1960,7 @@ au BufNewFile,BufRead {.,}tmux*.conf setf tmux au BufNewFile,BufRead *.toml setf toml " TPP - Text Presentation Program -au BufNewFile,BufReadPost *.tpp setf tpp +au BufNewFile,BufRead *.tpp setf tpp " Treetop au BufRead,BufNewFile *.treetop setf treetop @@ -1950,6 +2020,9 @@ au BufNewFile,BufRead */.init/*.conf,*/.init/*.override setf upstart au BufNewFile,BufRead */.config/upstart/*.conf setf upstart au BufNewFile,BufRead */.config/upstart/*.override setf upstart +" Vala +au BufNewFile,BufRead *.vala setf vala + " Vera au BufNewFile,BufRead *.vr,*.vri,*.vrh setf vera @@ -1980,7 +2053,7 @@ au BufRead,BufNewFile *.hw,*.module,*.pkg \ endif " Visual Basic (also uses *.bas) or FORM -au BufNewFile,BufRead *.frm call dist#ft#FTVB("form") +au BufNewFile,BufRead *.frm call dist#ft#FTfrm() " SaxBasic is close to Visual Basic au BufNewFile,BufRead *.sba setf vb @@ -2144,6 +2217,9 @@ au BufNewFile,BufRead *.raml setf raml " yum conf (close enough to dosini) au BufNewFile,BufRead */etc/yum.conf setf dosini +" YANG +au BufRead,BufNewFile *.yang setf yang + " Zimbu au BufNewFile,BufRead *.zu setf zimbu " Zimbu Templates @@ -2285,6 +2361,9 @@ au BufNewFile,BufRead Kconfig.* call s:StarSetf('kconfig') " Lilo: Linux loader au BufNewFile,BufRead lilo.conf* call s:StarSetf('lilo') +" Libsensors +au BufNewFile,BufRead */etc/sensors.d/[^.]* call s:StarSetf('sensors') + " Logcheck au BufNewFile,BufRead */etc/logcheck/*.d*/* call s:StarSetf('logcheck') @@ -2407,10 +2486,12 @@ au BufNewFile,BufRead *.txt \| setf text \| endif -" Use the filetype detect plugins. They may overrule any of the previously -" detected filetypes. -runtime! ftdetect/*.vim -runtime! ftdetect/*.lua +if !exists('g:did_load_ftdetect') + " Use the filetype detect plugins. They may overrule any of the previously + " detected filetypes. + runtime! ftdetect/*.vim + runtime! ftdetect/*.lua +endif " NOTE: The above command could have ended the filetypedetect autocmd group " and started another one. Let's make sure it has ended to get to a consistent diff --git a/runtime/ftplugin/basic.vim b/runtime/ftplugin/basic.vim index c6ec254dfc..a8f6b088d1 100644 --- a/runtime/ftplugin/basic.vim +++ b/runtime/ftplugin/basic.vim @@ -1,7 +1,7 @@ " Vim filetype plugin file -" Language: BASIC +" Language: BASIC (QuickBASIC 4.5) " Maintainer: Doug Kearns <dougkearns@gmail.com> -" Last Change: 2015 Jan 10 +" Last Change: 2021 Mar 16 if exists("b:did_ftplugin") finish @@ -11,17 +11,46 @@ let b:did_ftplugin = 1 let s:cpo_save = &cpo set cpo&vim -setlocal comments=:REM,:' +setlocal comments=:REM\ ,:Rem\ ,:rem\ ,:' setlocal commentstring='\ %s setlocal formatoptions-=t formatoptions+=croql +" TODO: support exit ... as middle matches? +if exists("loaded_matchit") && !exists("b:match_words") + let s:line_start = '\%(^\s*\)\@<=' + let s:not_end = '\%(end\s\+\)\@<!' + let s:not_end_or_exit = '\%(\%(end\|exit\)\s\+\)\@<!' + + let b:match_ignorecase = 1 + let b:match_words = + \ s:not_end_or_exit .. '\<def\s\+fn:\<end\s\+def\>,' .. + \ s:not_end_or_exit .. '\<function\>:\<end\s\+function\>,' .. + \ s:not_end_or_exit .. '\<sub\>:\<end\s\+sub\>,' .. + \ s:not_end .. '\<type\>:\<end\s\+type\>,' .. + \ s:not_end .. '\<select\>:\%(select\s\+\)\@<!\<case\%(\s\+\%(else\|is\)\)\=\>:\<end\s\+select\>,' .. + \ '\<do\>:\<loop\>,' .. + \ '\<for\>\%(\s\+\%(input\|output\|random\|append\|binary\)\)\@!:\<next\>,' .. + \ '\<while\>:\<wend\>,' .. + \ s:line_start .. 'if\%(.*\<then\s*\%($\|''\)\)\@=:\<\%(' .. s:line_start .. 'else\|elseif\)\>:\<end\s\+if\>,' .. + \ '\<lock\>:\<unlock\>' + + let b:match_skip = 'synIDattr(synID(line("."),col("."),1),"name") =~? "comment\\|string" || ' .. + \ 'strpart(getline("."), 0, col(".") ) =~? "\\<exit\\s\\+"' + + unlet s:line_start s:not_end s:not_end_or_exit +endif + if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter") - let b:browsefilter = "BASIC Source Files (*.bas)\t*.bas\n" . - \ "All Files (*.*)\t*.*\n" + let b:browsefilter = "BASIC Source Files (*.bas)\t*.bas\n" .. + \ "BASIC Include Files (*.bi, *.bm)\t*.bi;*.bm\n" .. + \ "All Files (*.*)\t*.*\n" endif -let b:undo_ftplugin = "setl fo< com< cms< sua<" . - \ " | unlet! b:browsefilter" +let b:undo_ftplugin = "setl fo< com< cms<" .. + \ " | unlet! b:match_ignorecase b:match_skip b:match_words" .. + \ " | unlet! b:browsefilter" let &cpo = s:cpo_save unlet s:cpo_save + +" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker: diff --git a/runtime/ftplugin/freebasic.vim b/runtime/ftplugin/freebasic.vim index a2bb459f20..58c2b4c9e2 100644 --- a/runtime/ftplugin/freebasic.vim +++ b/runtime/ftplugin/freebasic.vim @@ -1,13 +1,65 @@ " Vim filetype plugin file -" Language: FreeBasic +" Language: FreeBASIC " Maintainer: Doug Kearns <dougkearns@gmail.com> -" Last Change: 2015 Jan 10 +" Last Change: 2021 Mar 16 +" Setup {{{1 if exists("b:did_ftplugin") finish endif -let b:did_ftplugin = 1 + +let s:cpo_save = &cpo +set cpo&vim runtime! ftplugin/basic.vim -" vim: ts=8 +let s:dialect = freebasic#GetDialect() + +" Comments {{{1 +" add ''comments before 'comments +let &l:comments = "sO:*\ -,mO:*\ \ ,exO:*/,s1:/',mb:',ex:'/,:''," .. &l:comments + +" Match words {{{1 +if exists("loaded_matchit") + let s:not_end = '\%(end\s\+\)\@<!' + + let b:match_words ..= ',' + + if s:dialect == 'fb' + let b:match_words ..= s:not_end .. '\<constructor\>:\<end\s\+constructor\>,' .. + \ s:not_end .. '\<destructor\>:\<end\s\+destructor\>,' .. + \ s:not_end .. '\<property\>:\<end\s\+property\>,' .. + \ s:not_end .. '\<operator\>:\<end\s\+operator\>,' .. + \ s:not_end .. '\<extern\%(\s\+"\)\@=:\<end\s\+extern\>,' + endif + + if s:dialect == 'fb' || s:dialect == 'deprecated' + let b:match_words ..= s:not_end .. '\<scope\>:\<end\s\+scope\>,' + endif + + if s:dialect == 'qb' + let b:match_words ..= s:not_end .. '\<__asm\>:\<end\s\+__asm\>,' .. + \ s:not_end .. '\<__union\>:\<end\s\+__union\>,' .. + \ s:not_end .. '\<__with\>:\<end\s\+__with\>,' + else + let b:match_words ..= s:not_end .. '\<asm\>:\<end\s\+asm\>,' .. + \ s:not_end .. '\<namespace\>:\<end\s\+namespace\>,' .. + \ s:not_end .. '\<union\>:\<end\s\+union\>,' .. + \ s:not_end .. '\<with\>:\<end\s\+with\>,' + endif + + let b:match_words ..= s:not_end .. '\<enum\>:\<end\s\+enum\>,' .. + \ '^#\s*\%(if\|ifdef\|ifndef\)\>:^#\s*\%(else\|elseif\)\>:^#\s*endif\>,' .. + \ '^#\s*macro\>:^#\s*endmacro\>' + + " skip "function = <retval>" + let b:match_skip ..= '|| strpart(getline("."), col(".") - 1) =~? "^\\<function\\s\\+="' + + unlet s:not_end +endif + +" Cleanup {{{1 +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker: diff --git a/runtime/ftplugin/git.vim b/runtime/ftplugin/git.vim deleted file mode 100644 index 75b20f021e..0000000000 --- a/runtime/ftplugin/git.vim +++ /dev/null @@ -1,41 +0,0 @@ -" Vim filetype plugin -" Language: generic git output -" Maintainer: Tim Pope <vimNOSPAM@tpope.org> -" Last Change: 2019 Dec 05 - -" Only do this when not done yet for this buffer -if (exists("b:did_ftplugin")) - finish -endif -let b:did_ftplugin = 1 - -if !exists('b:git_dir') - if expand('%:p') =~# '[\/]\.git[\/]modules[\/]\|:[\/][\/]\|^\a\a\+:' - " Stay out of the way - elseif expand('%:p') =~# '[\/]\.git[\/]worktrees' - let b:git_dir = matchstr(expand('%:p'),'.*\.git[\/]worktrees[\/][^\/]\+\>') - elseif expand('%:p') =~# '\.git\>' - let b:git_dir = matchstr(expand('%:p'),'.*\.git\>') - elseif $GIT_DIR != '' - let b:git_dir = $GIT_DIR - endif - if (has('win32') || has('win64')) && exists('b:git_dir') - let b:git_dir = substitute(b:git_dir,'\\','/','g') - endif -endif - -if exists('*shellescape') && exists('b:git_dir') && b:git_dir != '' - if b:git_dir =~# '/\.git$' " Not a bare repository - let &l:path = escape(fnamemodify(b:git_dir,':h'),'\, ').','.&l:path - endif - let &l:path = escape(b:git_dir,'\, ').','.&l:path - let &l:keywordprg = 'git --git-dir='.shellescape(b:git_dir).' show' -else - setlocal keywordprg=git\ show -endif -if has('gui_running') - let &l:keywordprg = substitute(&l:keywordprg,'^git\>','git --no-pager','') -endif - -setlocal includeexpr=substitute(v:fname,'^[^/]\\+/','','') -let b:undo_ftplugin = "setl keywordprg< path< includeexpr<" diff --git a/runtime/ftplugin/gitcommit.vim b/runtime/ftplugin/gitcommit.vim index 9b1998acaa..9342799b56 100644 --- a/runtime/ftplugin/gitcommit.vim +++ b/runtime/ftplugin/gitcommit.vim @@ -1,66 +1,57 @@ " Vim filetype plugin " Language: git commit file " Maintainer: Tim Pope <vimNOSPAM@tpope.org> -" Last Change: 2019 Dec 05 +" Last Change: 2022 Jan 05 " Only do this when not done yet for this buffer if (exists("b:did_ftplugin")) finish endif -runtime! ftplugin/git.vim let b:did_ftplugin = 1 -setlocal comments=:# commentstring=#\ %s setlocal nomodeline tabstop=8 formatoptions+=tl textwidth=72 setlocal formatoptions-=c formatoptions-=r formatoptions-=o formatoptions-=q formatoptions+=n setlocal formatlistpat+=\\\|^\\s*[-*+]\\s\\+ +setlocal include=^+++ +setlocal includeexpr=substitute(v:fname,'^[bi]/','','') -let b:undo_ftplugin = 'setl modeline< tabstop< formatoptions< tw< com< cms< formatlistpat<' +let b:undo_ftplugin = 'setl modeline< tabstop< formatoptions< tw< com< cms< formatlistpat< inc< inex<' -if exists("g:no_gitcommit_commands") || v:version < 700 - finish -endif +let s:l = search('\C\m^[#;@!$%^&|:] -\{24,\} >8 -\{24,\}$', 'cnW', '', 100) +let &l:comments = ':' . (matchstr(getline(s:l ? s:l : '$'), '^[#;@!$%^&|:]\S\@!') . '#')[0] +let &l:commentstring = &l:comments[1] . ' %s' +unlet s:l -if !exists("b:git_dir") - let b:git_dir = expand("%:p:h") +if exists("g:no_gitcommit_commands") + finish endif -command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>) +command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0, <f-args>) let b:undo_ftplugin = b:undo_ftplugin . "|delc DiffGitCached" -function! s:diffcomplete(A,L,P) +function! s:diffcomplete(A, L, P) abort let args = "" if a:P <= match(a:L." -- "," -- ")+3 let args = args . "-p\n--stat\n--shortstat\n--summary\n--patch-with-stat\n--no-renames\n-B\n-M\n-C\n" end - if exists("b:git_dir") && a:A !~ '^-' - let tree = fnamemodify(b:git_dir,':h') - if strpart(getcwd(),0,strlen(tree)) == tree - let args = args."\n".system("git diff --cached --name-only") - endif + if a:A !~ '^-' && !empty(getftype('.git')) + let args = args."\n".system("git diff --cached --name-only") endif return args endfunction -function! s:gitdiffcached(bang,gitdir,...) - let tree = fnamemodify(a:gitdir,':h') +function! s:gitdiffcached(bang, ...) abort let name = tempname() - let git = "git" - if strpart(getcwd(),0,strlen(tree)) != tree - let git .= " --git-dir=".(exists("*shellescape") ? shellescape(a:gitdir) : '"'.a:gitdir.'"') - endif if a:0 - let extra = join(map(copy(a:000),exists("*shellescape") ? 'shellescape(v:val)' : "'\"'.v:val.'\"'")) + let extra = join(map(copy(a:000), 'shellescape(v:val)')) else let extra = "-p --stat=".&columns endif - call system(git." diff --cached --no-color --no-ext-diff ".extra." > ".(exists("*shellescape") ? shellescape(name) : name)) - exe "pedit ".(exists("*fnameescape") ? fnameescape(name) : name) + call system("git diff --cached --no-color --no-ext-diff ".extra." > ".shellescape(name)) + exe "pedit " . fnameescape(name) wincmd P - let b:git_dir = a:gitdir - command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0,b:git_dir,<f-args>) - nnoremap <buffer> <silent> q :q<CR> + command! -bang -bar -buffer -complete=custom,s:diffcomplete -nargs=* DiffGitCached :call s:gitdiffcached(<bang>0, <f-args>) setlocal buftype=nowrite nobuflisted noswapfile nomodifiable filetype=git endfunction diff --git a/runtime/ftplugin/gitrebase.vim b/runtime/ftplugin/gitrebase.vim index 2fed53c829..143f86a251 100644 --- a/runtime/ftplugin/gitrebase.vim +++ b/runtime/ftplugin/gitrebase.vim @@ -1,22 +1,20 @@ " Vim filetype plugin " Language: git rebase --interactive " Maintainer: Tim Pope <vimNOSPAM@tpope.org> -" Last Change: 2019 Dec 05 +" Last Change: 2022 Jan 05 " Only do this when not done yet for this buffer if (exists("b:did_ftplugin")) finish endif -runtime! ftplugin/git.vim let b:did_ftplugin = 1 -setlocal comments=:# commentstring=#\ %s formatoptions-=t +let &l:comments = ':' . (matchstr(getline('$'), '^[#;@!$%^&|:]\S\@!') . '#')[0] +let &l:commentstring = &l:comments[1] . ' %s' +setlocal formatoptions-=t setlocal nomodeline -if !exists("b:undo_ftplugin") - let b:undo_ftplugin = "" -endif -let b:undo_ftplugin = b:undo_ftplugin."|setl com< cms< fo< ml<" +let b:undo_ftplugin = "setl com< cms< fo< ml<" function! s:choose(word) abort s/^\(\w\+\>\)\=\(\s*\)\ze\x\{4,40\}\>/\=(strlen(submatch(1)) == 1 ? a:word[0] : a:word) . substitute(submatch(2),'^$',' ','')/e @@ -41,8 +39,7 @@ if exists("g:no_plugin_maps") || exists("g:no_gitrebase_maps") finish endif -nnoremap <buffer> <expr> K col('.') < 7 && expand('<Lt>cword>') =~ '\X' && getline('.') =~ '^\w\+\s\+\x\+\>' ? 'wK' : 'K' nnoremap <buffer> <silent> <C-A> :<C-U><C-R>=v:count1<CR>Cycle<CR> nnoremap <buffer> <silent> <C-X> :<C-U><C-R>=v:count1<CR>Cycle!<CR> -let b:undo_ftplugin = b:undo_ftplugin . "|exe 'nunmap <buffer> K'|exe 'nunmap <buffer> <C-A>'|exe 'nunmap <buffer> <C-X>'" +let b:undo_ftplugin = b:undo_ftplugin . "|exe 'nunmap <buffer> <C-A>'|exe 'nunmap <buffer> <C-X>'" diff --git a/runtime/ftplugin/qb64.vim b/runtime/ftplugin/qb64.vim new file mode 100644 index 0000000000..0fa36fc3d2 --- /dev/null +++ b/runtime/ftplugin/qb64.vim @@ -0,0 +1,26 @@ +" Vim filetype plugin file +" Language: QB64 +" Maintainer: Doug Kearns <dougkearns@gmail.com> + +if exists("b:did_ftplugin") + finish +endif + +let s:cpo_save = &cpo +set cpo&vim + +runtime! ftplugin/basic.vim + +let s:not_end = '\%(end\s\+\)\@<!' + +let b:match_words ..= ',' .. + \ s:not_end .. '\<declare\>:\<end\s\+declare\>,' .. + \ '\<select\s\+everycase\>:\%(select\s\+\)\@<!\<case\%(\s\+\%(else\|is\)\)\=\>:\<end\s\+select\>,' .. + \ '$IF\>:$\%(ELSEIF\|ELSE\)\>:$END\s*IF\>' + +unlet s:not_end + +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker: diff --git a/runtime/indent/basic.vim b/runtime/indent/basic.vim new file mode 100644 index 0000000000..7228772251 --- /dev/null +++ b/runtime/indent/basic.vim @@ -0,0 +1,11 @@ +" Vim indent file +" Language: BASIC (QuickBASIC 4.5) +" Maintainer: Doug Kearns <dougkearns@gmail.com> +" Last Change: 2022 Jan 24 + +" Only load this indent file when no other was loaded. +if exists("b:did_indent") + finish +endif + +runtime! indent/vb.vim diff --git a/runtime/indent/freebasic.vim b/runtime/indent/freebasic.vim new file mode 100644 index 0000000000..248b928635 --- /dev/null +++ b/runtime/indent/freebasic.vim @@ -0,0 +1,11 @@ +" Vim indent file +" Language: FreeBASIC +" Maintainer: Doug Kearns <dougkearns@gmail.com> +" Last Change: 2022 Jan 24 + +" Only load this indent file when no other was loaded. +if exists("b:did_indent") + finish +endif + +runtime! indent/vb.vim diff --git a/runtime/indent/html.vim b/runtime/indent/html.vim index d4b91f6421..a3c32d6342 100644 --- a/runtime/indent/html.vim +++ b/runtime/indent/html.vim @@ -1,7 +1,7 @@ " Vim indent script for HTML " Maintainer: Bram Moolenaar " Original Author: Andy Wokula <anwoku@yahoo.de> -" Last Change: 2021 Jun 13 +" Last Change: 2022 Jan 31 " Version: 1.0 "{{{ " Description: HTML indent script with cached state for faster indenting on a " range of lines. @@ -149,6 +149,15 @@ func HtmlIndent_CheckUserSettings() let b:html_indent_line_limit = 200 endif endif + + if exists('b:html_indent_attribute') + let b:hi_attr_indent = b:html_indent_attribute + elseif exists('g:html_indent_attribute') + let b:hi_attr_indent = g:html_indent_attribute + else + let b:hi_attr_indent = 2 + endif + endfunc "}}} " Init Script Vars @@ -946,11 +955,11 @@ func s:InsideTag(foundHtmlString) let idx = match(text, '<' . s:tagname . '\s\+\zs\w') endif if idx == -1 - " after just "<tag" indent two levels more + " after just "<tag" indent two levels more by default let idx = match(text, '<' . s:tagname . '$') if idx >= 0 call cursor(lnum, idx + 1) - return virtcol('.') - 1 + shiftwidth() * 2 + return virtcol('.') - 1 + shiftwidth() * b:hi_attr_indent endif endif if idx > 0 diff --git a/runtime/indent/qb64.vim b/runtime/indent/qb64.vim new file mode 100644 index 0000000000..09f815c43d --- /dev/null +++ b/runtime/indent/qb64.vim @@ -0,0 +1,11 @@ +" Vim indent file +" Language: QB64 +" Maintainer: Doug Kearns <dougkearns@gmail.com> +" Last Change: 2022 Jan 24 + +" Only load this indent file when no other was loaded. +if exists("b:did_indent") + finish +endif + +runtime! indent/vb.vim diff --git a/runtime/indent/testdir/html.in b/runtime/indent/testdir/html.in index 1acf8c0402..b62c67ddb2 100644 --- a/runtime/indent/testdir/html.in +++ b/runtime/indent/testdir/html.in @@ -1,4 +1,4 @@ -" vim: set ft=html sw=4 : +" vim: set ft=html sw=4 ts=8 : " START_INDENT @@ -41,6 +41,11 @@ dd text dt text </dt> </dl> +<div +class="test" +style="color: yellow"> +text +</div> </body> </html> @@ -50,6 +55,7 @@ dt text % START_INDENT % INDENT_EXE let g:html_indent_style1 = "inc" % INDENT_EXE let g:html_indent_script1 = "zero" +% INDENT_EXE let g:html_indent_attribute = 1 % INDENT_EXE call HtmlIndent_CheckUserSettings() <html> <body> @@ -61,6 +67,11 @@ div#d2 { color: green; } var v1 = "v1"; var v2 = "v2"; </script> +<div +class="test" +style="color: yellow"> +text +</div> </body> </html> % END_INDENT diff --git a/runtime/indent/testdir/html.ok b/runtime/indent/testdir/html.ok index c0dfc9dc72..938e965d8c 100644 --- a/runtime/indent/testdir/html.ok +++ b/runtime/indent/testdir/html.ok @@ -1,4 +1,4 @@ -" vim: set ft=html sw=4 : +" vim: set ft=html sw=4 ts=8 : " START_INDENT @@ -41,6 +41,11 @@ div#d2 { color: green; } dt text </dt> </dl> + <div + class="test" + style="color: yellow"> + text + </div> </body> </html> @@ -50,6 +55,7 @@ div#d2 { color: green; } % START_INDENT % INDENT_EXE let g:html_indent_style1 = "inc" % INDENT_EXE let g:html_indent_script1 = "zero" +% INDENT_EXE let g:html_indent_attribute = 1 % INDENT_EXE call HtmlIndent_CheckUserSettings() <html> <body> @@ -61,6 +67,11 @@ div#d2 { color: green; } var v1 = "v1"; var v2 = "v2"; </script> + <div + class="test" + style="color: yellow"> + text + </div> </body> </html> % END_INDENT diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 417b661155..b4537c2882 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -30,7 +30,7 @@ M.handlers = setmetatable({}, { __newindex = function(t, name, handler) vim.validate { handler = {handler, "t" } } rawset(t, name, handler) - if not global_diagnostic_options[name] then + if global_diagnostic_options[name] == nil then global_diagnostic_options[name] = true end end, @@ -552,7 +552,8 @@ end --- - `table`: Enable this feature with overrides. Use an empty table to use default values. --- - `function`: Function with signature (namespace, bufnr) that returns any of the above. --- ----@param opts table Configuration table with the following keys: +---@param opts table|nil When omitted or "nil", retrieve the current configuration. Otherwise, a +--- configuration table with the following keys: --- - underline: (default true) Use underline for diagnostics. Options: --- * severity: Only underline diagnostics matching the given severity --- |diagnostic-severity| @@ -599,7 +600,7 @@ end --- global diagnostic options. function M.config(opts, namespace) vim.validate { - opts = { opts, 't' }, + opts = { opts, 't', true }, namespace = { namespace, 'n', true }, } @@ -611,10 +612,13 @@ function M.config(opts, namespace) t = global_diagnostic_options end - for opt in pairs(global_diagnostic_options) do - if opts[opt] ~= nil then - t[opt] = opts[opt] - end + if not opts then + -- Return current config + return vim.deepcopy(t) + end + + for k, v in pairs(opts) do + t[k] = v end if namespace then @@ -823,6 +827,7 @@ M.handlers.signs = { } bufnr = get_bufnr(bufnr) + opts = opts or {} if opts.signs and opts.signs.severity then diagnostics = filter_by_severity(opts.signs.severity, diagnostics) @@ -890,6 +895,7 @@ M.handlers.underline = { } bufnr = get_bufnr(bufnr) + opts = opts or {} if opts.underline and opts.underline.severity then diagnostics = filter_by_severity(opts.underline.severity, diagnostics) @@ -942,6 +948,7 @@ M.handlers.virtual_text = { } bufnr = get_bufnr(bufnr) + opts = opts or {} local severity if opts.virtual_text then diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua new file mode 100644 index 0000000000..2fe4aa3d32 --- /dev/null +++ b/runtime/lua/vim/filetype.lua @@ -0,0 +1,1620 @@ +local api = vim.api + +local M = {} + +---@private +local function starsetf(ft) + return {function(path) + if not vim.g.fg_ignore_pat then + return ft + end + + local re = vim.regex(vim.g.fg_ignore_pat) + if re:match_str(path) then + return ft + end + end, { + -- Starset matches should always have lowest priority + priority = -math.huge, + }} +end + +---@private +local function getline(bufnr, lnum) + return api.nvim_buf_get_lines(bufnr, lnum-1, lnum, false)[1] +end + +-- Filetypes based on file extension +-- luacheck: push no unused args +local extension = { + -- BEGIN EXTENSION + ["8th"] = "8th", + ["a65"] = "a65", + aap = "aap", + abap = "abap", + abc = "abc", + abl = "abel", + wrm = "acedb", + ads = "ada", + ada = "ada", + gpr = "ada", + adb = "ada", + tdf = "ahdl", + aidl = "aidl", + aml = "aml", + run = "ampl", + scpt = "applescript", + ino = "arduino", + pde = "arduino", + art = "art", + asciidoc = "asciidoc", + adoc = "asciidoc", + ["asn1"] = "asn", + asn = "asn", + atl = "atlas", + as = "atlas", + ahk = "autohotkey", + ["au3"] = "autoit", + ave = "ave", + gawk = "awk", + awk = "awk", + ref = "b", + imp = "b", + mch = "b", + bc = "bc", + bdf = "bdf", + beancount = "beancount", + bib = "bib", + bl = "blank", + bsdl = "bsdl", + bst = "bst", + bzl = "bzl", + bazel = "bzl", + BUILD = "bzl", + qc = "c", + cabal = "cabal", + cdl = "cdl", + toc = "cdrtoc", + cfc = "cf", + cfm = "cf", + cfi = "cf", + cfg = "cfg", + hgrc = "cfg", + chf = "ch", + chai = "chaiscript", + chs = "chaskell", + chopro = "chordpro", + crd = "chordpro", + crdpro = "chordpro", + cho = "chordpro", + chordpro = "chordpro", + eni = "cl", + dcl = "clean", + icl = "clean", + cljx = "clojure", + clj = "clojure", + cljc = "clojure", + cljs = "clojure", + cmake = "cmake", + cmod = "cmod", + lib = "cobol", + cob = "cobol", + cbl = "cobol", + atg = "coco", + recipe = "conaryrecipe", + mklx = "context", + mkiv = "context", + mkii = "context", + mkxl = "context", + mkvi = "context", + moc = "cpp", + hh = "cpp", + tlh = "cpp", + inl = "cpp", + ipp = "cpp", + ["c++"] = "cpp", + C = "cpp", + cxx = "cpp", + H = "cpp", + tcc = "cpp", + hxx = "cpp", + hpp = "cpp", + cpp = function(path, bufnr) + if vim.g.cynlib_syntax_for_cc then + return "cynlib" + end + return "cpp" + end, + cc = function(path, bufnr) + if vim.g.cynlib_syntax_for_cc then + return "cynlib" + end + return "cpp" + end, + crm = "crm", + csx = "cs", + cs = "cs", + csc = "csc", + csdl = "csdl", + fdr = "csp", + csp = "csp", + css = "css", + con = "cterm", + feature = "cucumber", + cuh = "cuda", + cu = "cuda", + pld = "cupl", + si = "cuplsim", + cyn = "cynpp", + dart = "dart", + drt = "dart", + ds = "datascript", + dcd = "dcd", + def = "def", + desc = "desc", + directory = "desktop", + desktop = "desktop", + diff = "diff", + rej = "diff", + Dockerfile = "dockerfile", + sys = "dosbatch", + bat = "dosbatch", + wrap = "dosini", + ini = "dosini", + dot = "dot", + gv = "dot", + drac = "dracula", + drc = "dracula", + dtd = "dtd", + dts = "dts", + dtsi = "dts", + dylan = "dylan", + intr = "dylanintr", + lid = "dylanlid", + ecd = "ecd", + eex = "eelixir", + leex = "eelixir", + exs = "elixir", + elm = "elm", + epp = "epuppet", + erl = "erlang", + hrl = "erlang", + yaws = "erlang", + erb = "eruby", + rhtml = "eruby", + ec = "esqlc", + EC = "esqlc", + strl = "esterel", + exp = "expect", + factor = "factor", + fal = "falcon", + fan = "fan", + fwt = "fan", + fnl = "fennel", + ["m4gl"] = "fgl", + ["4gl"] = "fgl", + ["4gh"] = "fgl", + fish = "fish", + focexec = "focexec", + fex = "focexec", + fth = "forth", + ft = "forth", + FOR = "fortran", + ["f77"] = "fortran", + ["f03"] = "fortran", + fortran = "fortran", + ["F95"] = "fortran", + ["f90"] = "fortran", + ["F03"] = "fortran", + fpp = "fortran", + FTN = "fortran", + ftn = "fortran", + ["for"] = "fortran", + ["F90"] = "fortran", + ["F77"] = "fortran", + ["f95"] = "fortran", + FPP = "fortran", + f = "fortran", + F = "fortran", + ["F08"] = "fortran", + ["f08"] = "fortran", + fpc = "fpcmake", + fsl = "framescript", + fb = "freebasic", + fsi = "fsharp", + fsx = "fsharp", + fusion = "fusion", + gdmo = "gdmo", + mo = "gdmo", + tres = "gdresource", + tscn = "gdresource", + gd = "gdscript", + ged = "gedcom", + gmi = "gemtext", + gemini = "gemtext", + gift = "gift", + glsl = "glsl", + gpi = "gnuplot", + gnuplot = "gnuplot", + go = "go", + gp = "gp", + gs = "grads", + gql = "graphql", + graphql = "graphql", + graphqls = "graphql", + gretl = "gretl", + gradle = "groovy", + groovy = "groovy", + gsp = "gsp", + hack = "hack", + hackpartial = "hack", + haml = "haml", + hsm = "hamster", + hbs = "handlebars", + ["hs-boot"] = "haskell", + hsig = "haskell", + hsc = "haskell", + hs = "haskell", + ht = "haste", + htpp = "hastepreproc", + hb = "hb", + sum = "hercules", + errsum = "hercules", + ev = "hercules", + vc = "hercules", + hcl = "hcl", + heex = "heex", + hex = "hex", + ["h32"] = "hex", + hjson = "hjson", + hog = "hog", + hws = "hollywood", + htt = "httest", + htb = "httest", + iba = "ibasic", + ibi = "ibasic", + icn = "icon", + inf = "inform", + INF = "inform", + ii = "initng", + iss = "iss", + mst = "ist", + ist = "ist", + ijs = "j", + JAL = "jal", + jal = "jal", + jpr = "jam", + jpl = "jam", + jav = "java", + java = "java", + jj = "javacc", + jjt = "javacc", + es = "javascript", + mjs = "javascript", + javascript = "javascript", + js = "javascript", + cjs = "javascript", + jsx = "javascriptreact", + clp = "jess", + jgr = "jgraph", + ["j73"] = "jovial", + jov = "jovial", + jovial = "jovial", + properties = "jproperties", + slnf = "json", + json = "json", + jsonp = "json", + webmanifest = "json", + ipynb = "json", + ["json-patch"] = "json", + json5 = "json5", + jsonc = "jsonc", + jsp = "jsp", + jl = "julia", + kv = "kivy", + kix = "kix", + kts = "kotlin", + kt = "kotlin", + ktm = "kotlin", + ks = "kscript", + k = "kwt", + ACE = "lace", + ace = "lace", + latte = "latte", + lte = "latte", + ld = "ld", + ldif = "ldif", + journal = "ledger", + ldg = "ledger", + ledger = "ledger", + less = "less", + lex = "lex", + lxx = "lex", + ["l++"] = "lex", + l = "lex", + lhs = "lhaskell", + ll = "lifelines", + liquid = "liquid", + cl = "lisp", + L = "lisp", + lisp = "lisp", + el = "lisp", + lsp = "lisp", + asd = "lisp", + lt = "lite", + lite = "lite", + lgt = "logtalk", + lotos = "lotos", + lot = "lotos", + lout = "lout", + lou = "lout", + ulpc = "lpc", + lpc = "lpc", + sig = "lprolog", + lsl = "lsl", + lss = "lss", + nse = "lua", + rockspec = "lua", + lua = "lua", + quake = "m3quake", + at = "m4", + eml = "mail", + mk = "make", + mak = "make", + dsp = "make", + page = "mallard", + map = "map", + mws = "maple", + mpl = "maple", + mv = "maple", + mkdn = "markdown", + md = "markdown", + mdwn = "markdown", + mkd = "markdown", + markdown = "markdown", + mdown = "markdown", + mhtml = "mason", + comp = "mason", + mason = "mason", + master = "master", + mas = "master", + mel = "mel", + mf = "mf", + mgl = "mgl", + mgp = "mgp", + my = "mib", + mib = "mib", + mix = "mix", + mixal = "mix", + nb = "mma", + mmp = "mmp", + DEF = "modula2", + ["m2"] = "modula2", + MOD = "modula2", + mi = "modula2", + ssc = "monk", + monk = "monk", + tsc = "monk", + isc = "monk", + moo = "moo", + mp = "mp", + mof = "msidl", + odl = "msidl", + msql = "msql", + mu = "mupad", + mush = "mush", + mysql = "mysql", + ["n1ql"] = "n1ql", + nql = "n1ql", + nanorc = "nanorc", + ncf = "ncf", + nginx = "nginx", + ninja = "ninja", + nix = "nix", + nqc = "nqc", + roff = "nroff", + tmac = "nroff", + man = "nroff", + mom = "nroff", + nr = "nroff", + tr = "nroff", + nsi = "nsis", + nsh = "nsis", + obj = "obj", + mlt = "ocaml", + mly = "ocaml", + mll = "ocaml", + mlp = "ocaml", + mlip = "ocaml", + mli = "ocaml", + ml = "ocaml", + occ = "occam", + xom = "omnimark", + xin = "omnimark", + opam = "opam", + ["or"] = "openroad", + ora = "ora", + pxsl = "papp", + papp = "papp", + pxml = "papp", + pas = "pascal", + lpr = "pascal", + dpr = "pascal", + pbtxt = "pbtxt", + g = "pccts", + pcmk = "pcmk", + pdf = "pdf", + plx = "perl", + prisma = "prisma", + psgi = "perl", + al = "perl", + ctp = "php", + php = "php", + phtml = "php", + pike = "pike", + pmod = "pike", + rcp = "pilrc", + pli = "pli", + ["pl1"] = "pli", + ["p36"] = "plm", + plm = "plm", + pac = "plm", + plp = "plp", + pls = "plsql", + plsql = "plsql", + po = "po", + pot = "po", + pod = "pod", + pk = "poke", + ps = "postscr", + epsi = "postscr", + afm = "postscr", + epsf = "postscr", + eps = "postscr", + pfa = "postscr", + ai = "postscr", + pov = "pov", + ppd = "ppd", + it = "ppwiz", + ih = "ppwiz", + action = "privoxy", + pc = "proc", + pdb = "prolog", + pml = "promela", + proto = "proto", + ["psd1"] = "ps1", + ["psm1"] = "ps1", + ["ps1"] = "ps1", + pssc = "ps1", + ["ps1xml"] = "ps1xml", + psf = "psf", + psl = "psl", + pug = "pug", + arr = "pyret", + pxd = "pyrex", + pyx = "pyrex", + pyw = "python", + py = "python", + pyi = "python", + ptl = "python", + ql = "ql", + qll = "ql", + rad = "radiance", + mat = "radiance", + ["pod6"] = "raku", + rakudoc = "raku", + rakutest = "raku", + rakumod = "raku", + ["pm6"] = "raku", + raku = "raku", + ["t6"] = "raku", + ["p6"] = "raku", + raml = "raml", + rbs = "rbs", + rego = "rego", + rem = "remind", + remind = "remind", + res = "rescript", + resi = "rescript", + frt = "reva", + testUnit = "rexx", + rex = "rexx", + orx = "rexx", + rexx = "rexx", + jrexx = "rexx", + rxj = "rexx", + rexxj = "rexx", + testGroup = "rexx", + rxo = "rexx", + Rd = "rhelp", + rd = "rhelp", + rib = "rib", + Rmd = "rmd", + rmd = "rmd", + smd = "rmd", + Smd = "rmd", + rnc = "rnc", + rng = "rng", + rnw = "rnoweb", + snw = "rnoweb", + Rnw = "rnoweb", + Snw = "rnoweb", + rsc = "routeros", + x = "rpcgen", + rpl = "rpl", + Srst = "rrst", + srst = "rrst", + Rrst = "rrst", + rrst = "rrst", + rst = "rst", + rtf = "rtf", + rjs = "ruby", + rxml = "ruby", + rb = "ruby", + rant = "ruby", + ru = "ruby", + rbw = "ruby", + gemspec = "ruby", + builder = "ruby", + rake = "ruby", + rs = "rust", + sas = "sas", + sass = "sass", + sa = "sather", + sbt = "sbt", + scala = "scala", + sc = "scala", + scd = "scdoc", + ss = "scheme", + scm = "scheme", + sld = "scheme", + rkt = "scheme", + rktd = "scheme", + rktl = "scheme", + sce = "scilab", + sci = "scilab", + scss = "scss", + sd = "sd", + sdc = "sdc", + pr = "sdl", + sdl = "sdl", + sed = "sed", + sexp = "sexplib", + sieve = "sieve", + siv = "sieve", + sil = "sil", + sim = "simula", + ["s85"] = "sinda", + sin = "sinda", + ssm = "sisu", + sst = "sisu", + ssi = "sisu", + ["_sst"] = "sisu", + ["-sst"] = "sisu", + il = "skill", + ils = "skill", + cdf = "skill", + sl = "slang", + ice = "slice", + score = "slrnsc", + tpl = "smarty", + ihlp = "smcl", + smcl = "smcl", + hlp = "smcl", + smith = "smith", + smt = "smith", + sml = "sml", + spt = "snobol4", + sno = "snobol4", + sln = "solution", + sparql = "sparql", + rq = "sparql", + spec = "spec", + spice = "spice", + sp = "spice", + spd = "spup", + spdata = "spup", + speedup = "spup", + spi = "spyce", + spy = "spyce", + tyc = "sql", + typ = "sql", + pkb = "sql", + tyb = "sql", + pks = "sql", + sqlj = "sqlj", + sqi = "sqr", + sqr = "sqr", + nut = "squirrel", + ["s28"] = "srec", + ["s37"] = "srec", + srec = "srec", + mot = "srec", + ["s19"] = "srec", + st = "st", + imata = "stata", + ["do"] = "stata", + mata = "stata", + ado = "stata", + stp = "stp", + sface = "surface", + svelte = "svelte", + svg = "svg", + swift = "swift", + svh = "systemverilog", + sv = "systemverilog", + tak = "tak", + task = "taskedit", + tm = "tcl", + tcl = "tcl", + itk = "tcl", + itcl = "tcl", + tk = "tcl", + jacl = "tcl", + tl = "teal", + tmpl = "template", + ti = "terminfo", + dtx = "tex", + ltx = "tex", + bbl = "tex", + latex = "tex", + sty = "tex", + texi = "texinfo", + txi = "texinfo", + texinfo = "texinfo", + text = "text", + tfvars = "terraform", + tla = "tla", + tli = "tli", + toml = "toml", + tpp = "tpp", + treetop = "treetop", + slt = "tsalt", + tsscl = "tsscl", + tssgm = "tssgm", + tssop = "tssop", + tutor = "tutor", + twig = "twig", + ts = function(path, bufnr) + if getline(bufnr, 1):find("<%?xml") then + return "xml" + else + return "typescript" + end + end, + tsx = "typescriptreact", + uc = "uc", + uit = "uil", + uil = "uil", + sba = "vb", + vb = "vb", + dsm = "vb", + ctl = "vb", + vbs = "vb", + vr = "vera", + vri = "vera", + vrh = "vera", + v = "verilog", + va = "verilogams", + vams = "verilogams", + vhdl = "vhdl", + vst = "vhdl", + vhd = "vhdl", + hdl = "vhdl", + vho = "vhdl", + vbe = "vhdl", + vim = "vim", + vba = "vim", + mar = "vmasm", + cm = "voscm", + wrl = "vrml", + vroom = "vroom", + vue = "vue", + wat = "wast", + wast = "wast", + wm = "webmacro", + wbt = "winbatch", + wml = "wml", + wsml = "wsml", + ad = "xdefaults", + xhtml = "xhtml", + xht = "xhtml", + msc = "xmath", + msf = "xmath", + ["psc1"] = "xml", + tpm = "xml", + xliff = "xml", + atom = "xml", + xul = "xml", + cdxml = "xml", + mpd = "xml", + rss = "xml", + fsproj = "xml", + ui = "xml", + vbproj = "xml", + xlf = "xml", + wsdl = "xml", + csproj = "xml", + wpl = "xml", + xmi = "xml", + ["xpm2"] = "xpm2", + xqy = "xquery", + xqm = "xquery", + xquery = "xquery", + xq = "xquery", + xql = "xquery", + xs = "xs", + xsd = "xsd", + xsl = "xslt", + xslt = "xslt", + yy = "yacc", + ["y++"] = "yacc", + yxx = "yacc", + yml = "yaml", + yaml = "yaml", + yang = "yang", + ["z8a"] = "z8a", + zig = "zig", + zu = "zimbu", + zut = "zimbutempl", + zsh = "zsh", + vala = "vala", + E = function() vim.fn["dist#ft#FTe"]() end, + EU = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + EW = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + EX = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + EXU = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + EXW = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + PL = function() vim.fn["dist#ft#FTpl"]() end, + R = function() vim.fn["dist#ft#FTr"]() end, + asm = function() vim.fn["dist#ft#FTasm"]() end, + bas = function() vim.fn["dist#ft#FTbas"]() end, + bi = function() vim.fn["dist#ft#FTbas"]() end, + bm = function() vim.fn["dist#ft#FTbas"]() end, + bash = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + btm = function() vim.fn["dist#ft#FTbtm"]() end, + c = function() vim.fn["dist#ft#FTlpc"]() end, + ch = function() vim.fn["dist#ft#FTchange"]() end, + com = function() vim.fn["dist#ft#BindzoneCheck"]('dcl') end, + cpt = function() vim.fn["dist#ft#FThtml"]() end, + csh = function() vim.fn["dist#ft#CSH"]() end, + d = function() vim.fn["dist#ft#DtraceCheck"]() end, + db = function() vim.fn["dist#ft#BindzoneCheck"]('') end, + dtml = function() vim.fn["dist#ft#FThtml"]() end, + e = function() vim.fn["dist#ft#FTe"]() end, + ebuild = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + eclass = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + ent = function() vim.fn["dist#ft#FTent"]() end, + env = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + eu = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + ew = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + ex = function() vim.fn["dist#ft#ExCheck"]() end, + exu = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + exw = function() vim.fn["dist#ft#EuphoriaCheck"]() end, + frm = function() vim.fn["dist#ft#FTfrm"]() end, + fs = function() vim.fn["dist#ft#FTfs"]() end, + h = function() vim.fn["dist#ft#FTheader"]() end, + htm = function() vim.fn["dist#ft#FThtml"]() end, + html = function() vim.fn["dist#ft#FThtml"]() end, + i = function() vim.fn["dist#ft#FTprogress_asm"]() end, + idl = function() vim.fn["dist#ft#FTidl"]() end, + inc = function() vim.fn["dist#ft#FTinc"]() end, + inp = function() vim.fn["dist#ft#Check_inp"]() end, + ksh = function() vim.fn["dist#ft#SetFileTypeSH"]("ksh") end, + lst = function() vim.fn["dist#ft#FTasm"]() end, + m = function() vim.fn["dist#ft#FTm"]() end, + mac = function() vim.fn["dist#ft#FTasm"]() end, + mc = function() vim.fn["dist#ft#McSetf"]() end, + mm = function() vim.fn["dist#ft#FTmm"]() end, + mms = function() vim.fn["dist#ft#FTmms"]() end, + p = function() vim.fn["dist#ft#FTprogress_pascal"]() end, + patch = function(path, bufnr) + local firstline = getline(bufnr, 1) + if string.find(firstline, "^From " .. string.rep("%x", 40) .. "+ Mon Sep 17 00:00:00 2001$") then + return "gitsendemail" + else + return "diff" + end + end, + pl = function() vim.fn["dist#ft#FTpl"]() end, + pp = function() vim.fn["dist#ft#FTpp"]() end, + pro = function() vim.fn["dist#ft#ProtoCheck"]('idlang') end, + pt = function() vim.fn["dist#ft#FThtml"]() end, + r = function() vim.fn["dist#ft#FTr"]() end, + rdf = function() vim.fn["dist#ft#Redif"]() end, + rules = function() vim.fn["dist#ft#FTRules"]() end, + sh = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + shtml = function() vim.fn["dist#ft#FThtml"]() end, + sql = function() vim.fn["dist#ft#SQL"]() end, + stm = function() vim.fn["dist#ft#FThtml"]() end, + tcsh = function() vim.fn["dist#ft#SetFileTypeShell"]("tcsh") end, + tex = function() vim.fn["dist#ft#FTtex"]() end, + tf = function() vim.fn["dist#ft#FTtf"]() end, + w = function() vim.fn["dist#ft#FTprogress_cweb"]() end, + xml = function() vim.fn["dist#ft#FTxml"]() end, + y = function() vim.fn["dist#ft#FTy"]() end, + zsql = function() vim.fn["dist#ft#SQL"]() end, + txt = function(path, bufnr) + --helpfiles match *.txt, but should have a modeline as last line + if not getline(bufnr, -1):match("vim:.*ft=help") then + return "text" + end + end, + -- END EXTENSION +} + +local filename = { + -- BEGIN FILENAME + ["a2psrc"] = "a2ps", + ["/etc/a2ps.cfg"] = "a2ps", + [".a2psrc"] = "a2ps", + [".asoundrc"] = "alsaconf", + ["/usr/share/alsa/alsa.conf"] = "alsaconf", + ["/etc/asound.conf"] = "alsaconf", + ["build.xml"] = "ant", + [".htaccess"] = "apache", + ["apt.conf"] = "aptconf", + ["/.aptitude/config"] = "aptconf", + ["=tagging-method"] = "arch", + [".arch-inventory"] = "arch", + ["GNUmakefile.am"] = "automake", + ["named.root"] = "bindzone", + WORKSPACE = "bzl", + BUILD = "bzl", + ["cabal.config"] = "cabalconfig", + ["cabal.project"] = "cabalproject", + calendar = "calendar", + catalog = "catalog", + ["/etc/cdrdao.conf"] = "cdrdaoconf", + [".cdrdao"] = "cdrdaoconf", + ["/etc/default/cdrdao"] = "cdrdaoconf", + ["/etc/defaults/cdrdao"] = "cdrdaoconf", + ["cfengine.conf"] = "cfengine", + ["CMakeLists.txt"] = "cmake", + ["auto.master"] = "conf", + ["configure.in"] = "config", + ["configure.ac"] = "config", + [".cvsrc"] = "cvsrc", + ["/debian/changelog"] = "debchangelog", + ["changelog.dch"] = "debchangelog", + ["changelog.Debian"] = "debchangelog", + ["NEWS.dch"] = "debchangelog", + ["NEWS.Debian"] = "debchangelog", + ["/debian/control"] = "debcontrol", + ["/debian/copyright"] = "debcopyright", + ["/etc/apt/sources.list"] = "debsources", + ["denyhosts.conf"] = "denyhosts", + ["dict.conf"] = "dictconf", + [".dictrc"] = "dictconf", + ["/etc/DIR_COLORS"] = "dircolors", + [".dir_colors"] = "dircolors", + [".dircolors"] = "dircolors", + ["/etc/dnsmasq.conf"] = "dnsmasq", + Containerfile = "dockerfile", + Dockerfile = "dockerfile", + npmrc = "dosini", + ["/etc/yum.conf"] = "dosini", + ["/etc/pacman.conf"] = "dosini", + [".npmrc"] = "dosini", + [".editorconfig"] = "dosini", + dune = "dune", + jbuild = "dune", + ["dune-workspace"] = "dune", + ["dune-project"] = "dune", + ["elinks.conf"] = "elinks", + ["mix.lock"] = "elixir", + ["filter-rules"] = "elmfilt", + ["exim.conf"] = "exim", + exports = "exports", + [".fetchmailrc"] = "fetchmail", + fvSchemes = function() vim.fn["dist#ft#FTfoam"]() end, + fvSolution = function() vim.fn["dist#ft#FTfoam"]() end, + fvConstraints = function() vim.fn["dist#ft#FTfoam"]() end, + fvModels = function() vim.fn["dist#ft#FTfoam"]() end, + fstab = "fstab", + mtab = "fstab", + [".gdbinit"] = "gdb", + gdbinit = "gdb", + ["lltxxxxx.txt"] = "gedcom", + ["TAG_EDITMSG"] = "gitcommit", + ["MERGE_MSG"] = "gitcommit", + ["COMMIT_EDITMSG"] = "gitcommit", + ["NOTES_EDITMSG"] = "gitcommit", + ["EDIT_DESCRIPTION"] = "gitcommit", + [".gitconfig"] = "gitconfig", + [".gitmodules"] = "gitconfig", + ["gitolite.conf"] = "gitolite", + ["git-rebase-todo"] = "gitrebase", + gkrellmrc = "gkrellmrc", + [".gnashrc"] = "gnash", + [".gnashpluginrc"] = "gnash", + gnashpluginrc = "gnash", + gnashrc = "gnash", + ["go.work"] = "gowork", + [".gprc"] = "gp", + ["/.gnupg/gpg.conf"] = "gpg", + ["/.gnupg/options"] = "gpg", + ["/var/backups/gshadow.bak"] = "group", + ["/etc/gshadow"] = "group", + ["/etc/group-"] = "group", + ["/etc/gshadow.edit"] = "group", + ["/etc/gshadow-"] = "group", + ["/etc/group"] = "group", + ["/var/backups/group.bak"] = "group", + ["/etc/group.edit"] = "group", + ["/boot/grub/menu.lst"] = "grub", + ["/etc/grub.conf"] = "grub", + ["/boot/grub/grub.conf"] = "grub", + [".gtkrc"] = "gtkrc", + gtkrc = "gtkrc", + ["snort.conf"] = "hog", + ["vision.conf"] = "hog", + ["/etc/host.conf"] = "hostconf", + ["/etc/hosts.allow"] = "hostsaccess", + ["/etc/hosts.deny"] = "hostsaccess", + ["/i3/config"] = "i3config", + ["/sway/config"] = "i3config", + ["/.sway/config"] = "i3config", + ["/.i3/config"] = "i3config", + ["/.icewm/menu"] = "icemenu", + [".indent.pro"] = "indent", + indentrc = "indent", + inittab = "inittab", + ["ipf.conf"] = "ipfilter", + ["ipf6.conf"] = "ipfilter", + ["ipf.rules"] = "ipfilter", + [".eslintrc"] = "json", + [".babelrc"] = "json", + ["Pipfile.lock"] = "json", + [".firebaserc"] = "json", + [".prettierrc"] = "json", + Kconfig = "kconfig", + ["Kconfig.debug"] = "kconfig", + ["lftp.conf"] = "lftp", + [".lftprc"] = "lftp", + ["/.libao"] = "libao", + ["/etc/libao.conf"] = "libao", + ["lilo.conf"] = "lilo", + ["/etc/limits"] = "limits", + [".emacs"] = "lisp", + sbclrc = "lisp", + [".sbclrc"] = "lisp", + [".sawfishrc"] = "lisp", + ["/etc/login.access"] = "loginaccess", + ["/etc/login.defs"] = "logindefs", + ["lynx.cfg"] = "lynx", + ["m3overrides"] = "m3build", + ["m3makefile"] = "m3build", + ["cm3.cfg"] = "m3quake", + [".followup"] = "mail", + [".article"] = "mail", + [".letter"] = "mail", + ["/etc/aliases"] = "mailaliases", + ["/etc/mail/aliases"] = "mailaliases", + mailcap = "mailcap", + [".mailcap"] = "mailcap", + ["/etc/man.conf"] = "manconf", + ["man.config"] = "manconf", + ["meson.build"] = "meson", + ["meson_options.txt"] = "meson", + ["/etc/conf.modules"] = "modconf", + ["/etc/modules"] = "modconf", + ["/etc/modules.conf"] = "modconf", + ["/.mplayer/config"] = "mplayerconf", + ["mplayer.conf"] = "mplayerconf", + mrxvtrc = "mrxvtrc", + [".mrxvtrc"] = "mrxvtrc", + ["/etc/nanorc"] = "nanorc", + Neomuttrc = "neomuttrc", + [".netrc"] = "netrc", + [".ocamlinit"] = "ocaml", + [".octaverc"] = "octave", + octaverc = "octave", + ["octave.conf"] = "octave", + opam = "opam", + ["/etc/pam.conf"] = "pamconf", + ["pam_env.conf"] = "pamenv", + [".pam_environment"] = "pamenv", + ["/var/backups/passwd.bak"] = "passwd", + ["/var/backups/shadow.bak"] = "passwd", + ["/etc/passwd"] = "passwd", + ["/etc/passwd-"] = "passwd", + ["/etc/shadow.edit"] = "passwd", + ["/etc/shadow-"] = "passwd", + ["/etc/shadow"] = "passwd", + ["/etc/passwd.edit"] = "passwd", + ["pf.conf"] = "pf", + ["main.cf"] = "pfmain", + pinerc = "pine", + [".pinercex"] = "pine", + [".pinerc"] = "pine", + pinercex = "pine", + ["/etc/pinforc"] = "pinfo", + ["/.pinforc"] = "pinfo", + [".povrayrc"] = "povini", + [".procmailrc"] = "procmail", + [".procmail"] = "procmail", + ["/etc/protocols"] = "protocols", + [".pythonstartup"] = "python", + [".pythonrc"] = "python", + SConstruct = "python", + ratpoisonrc = "ratpoison", + [".ratpoisonrc"] = "ratpoison", + v = "rcs", + inputrc = "readline", + [".inputrc"] = "readline", + [".reminders"] = "remind", + ["resolv.conf"] = "resolv", + ["robots.txt"] = "robots", + Gemfile = "ruby", + Puppetfile = "ruby", + [".irbrc"] = "ruby", + irbrc = "ruby", + ["smb.conf"] = "samba", + screenrc = "screen", + [".screenrc"] = "screen", + ["/etc/sensors3.conf"] = "sensors", + ["/etc/sensors.conf"] = "sensors", + ["/etc/services"] = "services", + ["/etc/serial.conf"] = "setserial", + ["/etc/udev/cdsymlinks.conf"] = "sh", + ["/etc/slp.conf"] = "slpconf", + ["/etc/slp.reg"] = "slpreg", + ["/etc/slp.spi"] = "slpspi", + [".slrnrc"] = "slrnrc", + ["sendmail.cf"] = "sm", + ["squid.conf"] = "squid", + ["/.ssh/config"] = "sshconfig", + ["ssh_config"] = "sshconfig", + ["sshd_config"] = "sshdconfig", + ["/etc/sudoers"] = "sudoers", + ["sudoers.tmp"] = "sudoers", + ["/etc/sysctl.conf"] = "sysctl", + tags = "tags", + [".tclshrc"] = "tcl", + [".wishrc"] = "tcl", + ["tclsh.rc"] = "tcl", + ["texmf.cnf"] = "texmf", + COPYING = "text", + README = "text", + LICENSE = "text", + AUTHORS = "text", + tfrc = "tf", + [".tfrc"] = "tf", + ["tidy.conf"] = "tidy", + tidyrc = "tidy", + [".tidyrc"] = "tidy", + [".tmux.conf"] = "tmux", + ["/.cargo/config"] = "toml", + Pipfile = "toml", + ["Gopkg.lock"] = "toml", + ["/.cargo/credentials"] = "toml", + ["Cargo.lock"] = "toml", + ["trustees.conf"] = "trustees", + ["/etc/udev/udev.conf"] = "udevconf", + ["/etc/updatedb.conf"] = "updatedb", + ["fdrupstream.log"] = "upstreamlog", + vgrindefs = "vgrindefs", + [".exrc"] = "vim", + ["_exrc"] = "vim", + ["_viminfo"] = "viminfo", + [".viminfo"] = "viminfo", + [".wgetrc"] = "wget", + wgetrc = "wget", + [".wvdialrc"] = "wvdial", + ["wvdial.conf"] = "wvdial", + [".Xresources"] = "xdefaults", + [".Xpdefaults"] = "xdefaults", + ["xdm-config"] = "xdefaults", + [".Xdefaults"] = "xdefaults", + ["/etc/xinetd.conf"] = "xinetd", + fglrxrc = "xml", + ["/etc/blkid.tab"] = "xml", + ["/etc/blkid.tab.old"] = "xml", + ["/etc/zprofile"] = "zsh", + [".zlogin"] = "zsh", + [".zlogout"] = "zsh", + [".zshrc"] = "zsh", + [".zprofile"] = "zsh", + [".zcompdump"] = "zsh", + [".zshenv"] = "zsh", + [".zfbfmarks"] = "zsh", + [".alias"] = function() vim.fn["dist#ft#CSH"]() end, + [".bashrc"] = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + [".cshrc"] = function() vim.fn["dist#ft#CSH"]() end, + [".env"] = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + [".kshrc"] = function() vim.fn["dist#ft#SetFileTypeSH"]("ksh") end, + [".login"] = function() vim.fn["dist#ft#CSH"]() end, + [".profile"] = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + [".tcshrc"] = function() vim.fn["dist#ft#SetFileTypeShell"]("tcsh") end, + ["/etc/profile"] = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + APKBUILD = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + PKGBUILD = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + ["bash.bashrc"] = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + bashrc = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + crontab = starsetf('crontab'), + ["csh.cshrc"] = function() vim.fn["dist#ft#CSH"]() end, + ["csh.login"] = function() vim.fn["dist#ft#CSH"]() end, + ["csh.logout"] = function() vim.fn["dist#ft#CSH"]() end, + ["indent.pro"] = function() vim.fn["dist#ft#ProtoCheck"]('indent') end, + ["tcsh.login"] = function() vim.fn["dist#ft#SetFileTypeShell"]("tcsh") end, + ["tcsh.tcshrc"] = function() vim.fn["dist#ft#SetFileTypeShell"]("tcsh") end, + -- END FILENAME +} + +local pattern = { + -- BEGIN PATTERN + [".*/etc/a2ps/.*%.cfg"] = "a2ps", + [".*/etc/a2ps%.cfg"] = "a2ps", + [".*/usr/share/alsa/alsa%.conf"] = "alsaconf", + [".*/etc/asound%.conf"] = "alsaconf", + [".*/etc/apache2/sites%-.*/.*%.com"] = "apache", + [".*/etc/httpd/.*%.conf"] = "apache", + [".*/%.aptitude/config"] = "aptconf", + ["[mM]akefile%.am"] = "automake", + [".*bsd"] = "bsdl", + ["bzr_log%..*"] = "bzr", + [".*enlightenment/.*%.cfg"] = "c", + [".*/etc/defaults/cdrdao"] = "cdrdaoconf", + [".*/etc/cdrdao%.conf"] = "cdrdaoconf", + [".*/etc/default/cdrdao"] = "cdrdaoconf", + [".*hgrc"] = "cfg", + [".*%.%.ch"] = "chill", + [".*%.cmake%.in"] = "cmake", + [".*/debian/changelog"] = "debchangelog", + [".*/debian/control"] = "debcontrol", + [".*/debian/copyright"] = "debcopyright", + [".*/etc/apt/sources%.list%.d/.*%.list"] = "debsources", + [".*/etc/apt/sources%.list"] = "debsources", + ["dictd.*%.conf"] = "dictdconf", + [".*/etc/DIR_COLORS"] = "dircolors", + [".*/etc/dnsmasq%.conf"] = "dnsmasq", + ["php%.ini%-.*"] = "dosini", + [".*/etc/pacman%.conf"] = "dosini", + [".*/etc/yum%.conf"] = "dosini", + [".*lvs"] = "dracula", + [".*lpe"] = "dracula", + [".*esmtprc"] = "esmtprc", + [".*Eterm/.*%.cfg"] = "eterm", + [".*%.git/modules/.*/config"] = "gitconfig", + [".*%.git/config"] = "gitconfig", + [".*/etc/gitconfig"] = "gitconfig", + [".*/%.config/git/config"] = "gitconfig", + [".*%.git/config%.worktree"] = "gitconfig", + [".*%.git/worktrees/.*/config%.worktree"] = "gitconfig", + ["%.gitsendemail%.msg%......."] = "gitsendemail", + ["gkrellmrc_."] = "gkrellmrc", + [".*/usr/.*/gnupg/options%.skel"] = "gpg", + [".*/%.gnupg/options"] = "gpg", + [".*/%.gnupg/gpg%.conf"] = "gpg", + [".*/etc/group"] = "group", + [".*/etc/gshadow"] = "group", + [".*/etc/group%.edit"] = "group", + [".*/var/backups/gshadow%.bak"] = "group", + [".*/etc/group-"] = "group", + [".*/etc/gshadow-"] = "group", + [".*/var/backups/group%.bak"] = "group", + [".*/etc/gshadow%.edit"] = "group", + [".*/boot/grub/grub%.conf"] = "grub", + [".*/boot/grub/menu%.lst"] = "grub", + [".*/etc/grub%.conf"] = "grub", + ["hg%-editor%-.*%.txt"] = "hgcommit", + [".*/etc/host%.conf"] = "hostconf", + [".*/etc/hosts%.deny"] = "hostsaccess", + [".*/etc/hosts%.allow"] = "hostsaccess", + [".*%.html%.m4"] = "htmlm4", + [".*/%.i3/config"] = "i3config", + [".*/sway/config"] = "i3config", + [".*/i3/config"] = "i3config", + [".*/%.sway/config"] = "i3config", + [".*/%.icewm/menu"] = "icemenu", + [".*/etc/initng/.*/.*%.i"] = "initng", + [".*%.properties_.."] = "jproperties", + [".*%.properties_.._.."] = "jproperties", + [".*lftp/rc"] = "lftp", + [".*/%.libao"] = "libao", + [".*/etc/libao%.conf"] = "libao", + [".*/etc/.*limits%.conf"] = "limits", + [".*/etc/limits"] = "limits", + [".*/etc/.*limits%.d/.*%.conf"] = "limits", + [".*/LiteStep/.*/.*%.rc"] = "litestep", + [".*/etc/login%.access"] = "loginaccess", + [".*/etc/login%.defs"] = "logindefs", + [".*/etc/mail/aliases"] = "mailaliases", + [".*/etc/aliases"] = "mailaliases", + [".*[mM]akefile"] = "make", + [".*/etc/man%.conf"] = "manconf", + [".*/etc/modules%.conf"] = "modconf", + [".*/etc/conf%.modules"] = "modconf", + [".*/etc/modules"] = "modconf", + [".*%.[mi][3g]"] = "modula3", + [".*/%.mplayer/config"] = "mplayerconf", + ["rndc.*%.conf"] = "named", + ["rndc.*%.key"] = "named", + ["named.*%.conf"] = "named", + [".*/etc/nanorc"] = "nanorc", + [".*%.NS[ACGLMNPS]"] = "natural", + ["nginx.*%.conf"] = "nginx", + [".*/etc/nginx/.*"] = "nginx", + [".*nginx%.conf"] = "nginx", + [".*/nginx/.*%.conf"] = "nginx", + [".*/usr/local/nginx/conf/.*"] = "nginx", + [".*%.ml%.cppo"] = "ocaml", + [".*%.mli%.cppo"] = "ocaml", + [".*%.opam%.template"] = "opam", + [".*%.[Oo][Pp][Ll]"] = "opl", + [".*/etc/pam%.conf"] = "pamconf", + [".*/etc/passwd-"] = "passwd", + [".*/etc/shadow"] = "passwd", + [".*/etc/shadow%.edit"] = "passwd", + [".*/var/backups/shadow%.bak"] = "passwd", + [".*/var/backups/passwd%.bak"] = "passwd", + [".*/etc/passwd"] = "passwd", + [".*/etc/passwd%.edit"] = "passwd", + [".*/etc/shadow-"] = "passwd", + [".*/%.pinforc"] = "pinfo", + [".*/etc/pinforc"] = "pinfo", + [".*/etc/protocols"] = "protocols", + [".*baseq[2-3]/.*%.cfg"] = "quake", + [".*quake[1-3]/.*%.cfg"] = "quake", + [".*id1/.*%.cfg"] = "quake", + ["[rR]antfile"] = "ruby", + ["[rR]akefile"] = "ruby", + [".*/etc/sensors%.conf"] = "sensors", + [".*/etc/sensors3%.conf"] = "sensors", + [".*/etc/services"] = "services", + [".*/etc/serial%.conf"] = "setserial", + [".*/etc/udev/cdsymlinks%.conf"] = "sh", + [".*%._sst%.meta"] = "sisu", + [".*%.%-sst%.meta"] = "sisu", + [".*%.sst%.meta"] = "sisu", + [".*/etc/slp%.conf"] = "slpconf", + [".*/etc/slp%.reg"] = "slpreg", + [".*/etc/slp%.spi"] = "slpspi", + [".*/etc/ssh/ssh_config%.d/.*%.conf"] = "sshconfig", + [".*/%.ssh/config"] = "sshconfig", + [".*/etc/ssh/sshd_config%.d/.*%.conf"] = "sshdconfig", + [".*/etc/sudoers"] = "sudoers", + ["svn%-commit.*%.tmp"] = "svn", + [".*%.swift%.gyb"] = "swiftgyb", + [".*/etc/sysctl%.conf"] = "sysctl", + [".*/etc/sysctl%.d/.*%.conf"] = "sysctl", + [".*/etc/systemd/.*%.conf%.d/.*%.conf"] = "systemd", + [".*/%.config/systemd/user/.*%.d/.*%.conf"] = "systemd", + [".*/etc/systemd/system/.*%.d/.*%.conf"] = "systemd", + [".*%.t%.html"] = "tilde", + ["%.?tmux.*%.conf"] = "tmux", + ["%.?tmux.*%.conf.*"] = { "tmux", { priority = -1 } }, + [".*/%.cargo/config"] = "toml", + [".*/%.cargo/credentials"] = "toml", + [".*/etc/udev/udev%.conf"] = "udevconf", + [".*/etc/udev/permissions%.d/.*%.permissions"] = "udevperm", + [".*/etc/updatedb%.conf"] = "updatedb", + [".*/%.init/.*%.override"] = "upstart", + [".*/usr/share/upstart/.*%.conf"] = "upstart", + [".*/%.config/upstart/.*%.override"] = "upstart", + [".*/etc/init/.*%.conf"] = "upstart", + [".*/etc/init/.*%.override"] = "upstart", + [".*/%.config/upstart/.*%.conf"] = "upstart", + [".*/%.init/.*%.conf"] = "upstart", + [".*/usr/share/upstart/.*%.override"] = "upstart", + [".*%.ws[fc]"] = "wsh", + [".*/etc/xinetd%.conf"] = "xinetd", + [".*/etc/blkid%.tab"] = "xml", + [".*/etc/blkid%.tab%.old"] = "xml", + [".*%.vbproj%.user"] = "xml", + [".*%.fsproj%.user"] = "xml", + [".*%.csproj%.user"] = "xml", + [".*/etc/xdg/menus/.*%.menu"] = "xml", + [".*Xmodmap"] = "xmodmap", + [".*/etc/zprofile"] = "zsh", + ["%.bash[_-]aliases"] = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + ["%.bash[_-]logout"] = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + ["%.bash[_-]profile"] = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + ["%.cshrc.*"] = function() vim.fn["dist#ft#CSH"]() end, + ["%.gtkrc.*"] = starsetf('gtkrc'), + ["%.kshrc.*"] = function() vim.fn["dist#ft#SetFileTypeSH"]("ksh") end, + ["%.login.*"] = function() vim.fn["dist#ft#CSH"]() end, + ["%.neomuttrc.*"] = starsetf('neomuttrc'), + ["%.profile.*"] = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + ["%.reminders.*"] = starsetf('remind'), + ["%.tcshrc.*"] = function() vim.fn["dist#ft#SetFileTypeShell"]("tcsh") end, + ["%.zcompdump.*"] = starsetf('zsh'), + ["%.zlog.*"] = starsetf('zsh'), + ["%.zsh.*"] = starsetf('zsh'), + [".*%.[1-9]"] = function() vim.fn["dist#ft#FTnroff"]() end, + [".*%.[aA]"] = function() vim.fn["dist#ft#FTasm"]() end, + [".*%.[sS]"] = function() vim.fn["dist#ft#FTasm"]() end, + [".*%.properties_.._.._.*"] = starsetf('jproperties'), + [".*%.vhdl_[0-9].*"] = starsetf('vhdl'), + [".*/%.fvwm/.*"] = starsetf('fvwm'), + [".*/%.gitconfig%.d/.*"] = starsetf('gitconfig'), + [".*/%.neomutt/neomuttrc.*"] = starsetf('neomuttrc'), + [".*/Xresources/.*"] = starsetf('xdefaults'), + [".*/app%-defaults/.*"] = starsetf('xdefaults'), + [".*/bind/db%..*"] = starsetf('bindzone'), + [".*/debian/patches/.*"] = function() vim.fn["dist#ft#Dep3patch"]() end, + [".*/etc/Muttrc%.d/.*"] = starsetf('muttrc'), + [".*/etc/apache2/.*%.conf.*"] = starsetf('apache'), + [".*/etc/apache2/conf%..*/.*"] = starsetf('apache'), + [".*/etc/apache2/mods%-.*/.*"] = starsetf('apache'), + [".*/etc/apache2/sites%-.*/.*"] = starsetf('apache'), + [".*/etc/cron%.d/.*"] = starsetf('crontab'), + [".*/etc/dnsmasq%.d/.*"] = starsetf('dnsmasq'), + [".*/etc/httpd/conf%..*/.*"] = starsetf('apache'), + [".*/etc/httpd/conf%.d/.*%.conf.*"] = starsetf('apache'), + [".*/etc/httpd/mods%-.*/.*"] = starsetf('apache'), + [".*/etc/httpd/sites%-.*/.*"] = starsetf('apache'), + [".*/etc/logcheck/.*%.d.*/.*"] = starsetf('logcheck'), + [".*/etc/modprobe%..*"] = starsetf('modconf'), + [".*/etc/pam%.d/.*"] = starsetf('pamconf'), + [".*/etc/profile"] = function() vim.fn["dist#ft#SetFileTypeSH"](vim.fn.getline(1)) end, + [".*/etc/proftpd/.*%.conf.*"] = starsetf('apachestyle'), + [".*/etc/proftpd/conf%..*/.*"] = starsetf('apachestyle'), + [".*/etc/sudoers%.d/.*"] = starsetf('sudoers'), + [".*/etc/xinetd%.d/.*"] = starsetf('xinetd'), + [".*/etc/yum%.repos%.d/.*"] = starsetf('dosini'), + [".*/gitolite%-admin/conf/.*"] = starsetf('gitolite'), + [".*/named/db%..*"] = starsetf('bindzone'), + [".*/tmp/lltmp.*"] = starsetf('gedcom'), + [".*asterisk.*/.*voicemail%.conf.*"] = starsetf('asteriskvm'), + [".*asterisk/.*%.conf.*"] = starsetf('asterisk'), + [".*vimrc.*"] = starsetf('vim'), + [".*xmodmap.*"] = starsetf('xmodmap'), + ["/etc/gitconfig%.d/.*"] = starsetf('gitconfig'), + ["/etc/hostname%..*"] = starsetf('config'), + ["Containerfile%..*"] = starsetf('dockerfile'), + ["Dockerfile%..*"] = starsetf('dockerfile'), + ["JAM.*%..*"] = starsetf('jam'), + ["Kconfig%..*"] = starsetf('kconfig'), + ["Neomuttrc.*"] = starsetf('neomuttrc'), + ["Prl.*%..*"] = starsetf('jam'), + ["Xresources.*"] = starsetf('xdefaults'), + ["[mM]akefile.*"] = starsetf('make'), + ["[rR]akefile.*"] = starsetf('ruby'), + ["access%.conf.*"] = starsetf('apache'), + ["apache%.conf.*"] = starsetf('apache'), + ["apache2%.conf.*"] = starsetf('apache'), + ["bash%-fc[-%.]"] = function() vim.fn["dist#ft#SetFileTypeSH"]("bash") end, + ["cabal%.project%..*"] = starsetf('cabalproject'), + ["crontab%..*"] = starsetf('crontab'), + ["drac%..*"] = starsetf('dracula'), + ["gtkrc.*"] = starsetf('gtkrc'), + ["httpd%.conf.*"] = starsetf('apache'), + ["lilo%.conf.*"] = starsetf('lilo'), + ["neomuttrc.*"] = starsetf('neomuttrc'), + ["proftpd%.conf.*"] = starsetf('apachestyle'), + ["reportbug%-.*"] = starsetf('mail'), + ["sgml%.catalog.*"] = starsetf('catalog'), + ["srm%.conf.*"] = starsetf('apache'), + ["tmac%..*"] = starsetf('nroff'), + ["zlog.*"] = starsetf('zsh'), + ["zsh.*"] = starsetf('zsh'), + ["ae%d+%.txt"] = 'mail', + ["snd%.%d+"] = "mail", + ["%.letter%.%d+"] = "mail", + ["%.article%.%d+"] = "mail", + ["pico%.%d+"] = "mail", + ["mutt%-.*%-%w+"] = "mail", + ["neomutt%-.*%-%w+"] = "mail", + ["muttng%-.*%-%w+"] = "mail", + ["mutt" .. string.rep("[%w_-]", 6)] = "mail", + ["neomutt" .. string.rep("[%w_-]", 6)] = "mail", + ["/tmp/SLRN[0-9A-Z.]+"] = "mail", + ["[a-zA-Z0-9].*Dict"] = function() vim.fn["dist#ft#FTfoam"]() end, + ["[a-zA-Z0-9].*Dict%..*"] = function() vim.fn["dist#ft#FTfoam"]() end, + ["[a-zA-Z].*Properties"] = function() vim.fn["dist#ft#FTfoam"]() end, + ["[a-zA-Z].*Properties%..*"] = function() vim.fn["dist#ft#FTfoam"]() end, + [".*Transport%..*"] = function() vim.fn["dist#ft#FTfoam"]() end, + [".*/constant/g"] = function() vim.fn["dist#ft#FTfoam"]() end, + [".*/0/.*"] = function() vim.fn["dist#ft#FTfoam"]() end, + [".*/0%.orig/.*"] = function() vim.fn["dist#ft#FTfoam"]() end, + [".*/etc/sensors%.d/[^.].*"] = starsetf('sensors'), + [".*%.git/.*"] = function(path, bufnr) + local firstline = getline(bufnr, 1) + if firstline:find("^" .. string.rep("%x", 40) .. "+ ") or firstline:sub(1, 5) == "ref: " then + return "git" + end + end, + -- END PATTERN +} +-- luacheck: pop + +---@private +local function sort_by_priority(t) + local sorted = {} + for k, v in pairs(t) do + local ft = type(v) == "table" and v[1] or v + assert(type(ft) == "string" or type(ft) == "function", "Expected string or function for filetype") + + local opts = (type(v) == "table" and type(v[2]) == "table") and v[2] or {} + if not opts.priority then + opts.priority = 0 + end + table.insert(sorted, { [k] = { ft, opts } }) + end + table.sort(sorted, function(a, b) + return a[next(a)][2].priority > b[next(b)][2].priority + end) + return sorted +end + +local pattern_sorted = sort_by_priority(pattern) + +---@private +local function normalize_path(path) + return (path:gsub("\\", "/"):gsub("^~", vim.env.HOME)) +end + +--- Add new filetype mappings. +--- +--- Filetype mappings can be added either by extension or by filename (either +--- the "tail" or the full file path). The full file path is checked first, +--- followed by the file name. If a match is not found using the filename, then +--- the filename is matched against the list of patterns (sorted by priority) +--- until a match is found. Lastly, if pattern matching does not find a +--- filetype, then the file extension is used. +--- +--- The filetype can be either a string (in which case it is used as the +--- filetype directly) or a function. If a function, it takes the full path and +--- buffer number of the file as arguments (along with captures from the matched +--- pattern, if any) and should return a string that will be used as the +--- buffer's filetype. +--- +--- Filename patterns can specify an optional priority to resolve cases when a +--- file path matches multiple patterns. Higher priorities are matched first. +--- When omitted, the priority defaults to 0. +--- +--- See $VIMRUNTIME/lua/vim/filetype.lua for more examples. +--- +--- Note that Lua filetype detection is only enabled when |g:do_filetype_lua| is +--- set to 1. +--- +--- Example: +--- <pre> +--- vim.filetype.add({ +--- extension = { +--- foo = "fooscript", +--- bar = function(path, bufnr) +--- if some_condition() then +--- return "barscript" +--- end +--- return "bar" +--- end, +--- }, +--- filename = { +--- [".foorc"] = "toml", +--- ["/etc/foo/config"] = "toml", +--- }, +--- pattern = { +--- [".*/etc/foo/.*"] = "fooscript", +--- -- Using an optional priority +--- [".*/etc/foo/.*%.conf"] = { "dosini", { priority = 10 } }, +--- ["README.(%a+)$"] = function(path, bufnr, ext) +--- if ext == "md" then +--- return "markdown" +--- elseif ext == "rst" then +--- return "rst" +--- end +--- end, +--- }, +--- }) +--- </pre> +--- +---@param filetypes table A table containing new filetype maps (see example). +function M.add(filetypes) + for k, v in pairs(filetypes.extension or {}) do + extension[k] = v + end + + for k, v in pairs(filetypes.filename or {}) do + filename[normalize_path(k)] = v + end + + for k, v in pairs(filetypes.pattern or {}) do + pattern[normalize_path(k)] = v + end + + if filetypes.pattern then + pattern_sorted = sort_by_priority(pattern) + end +end + +---@private +local function dispatch(ft, path, bufnr, ...) + if type(ft) == "function" then + ft = ft(path, bufnr, ...) + end + + if type(ft) == "string" then + api.nvim_buf_set_option(bufnr, "filetype", ft) + return true + end + + -- Any non-falsey value (that is, anything other than 'nil' or 'false') will + -- end filetype matching. This is useful for e.g. the dist#ft functions that + -- return 0, but set the buffer's filetype themselves + return ft +end + +---@private +local function match_pattern(name, path, tail, pat) + -- If the pattern contains a / match against the full path, otherwise just the tail + local fullpat = "^" .. pat .. "$" + local matches + if pat:find("/") then + -- Similar to |autocmd-pattern|, if the pattern contains a '/' then check for a match against + -- both the short file name (as typed) and the full file name (after expanding to full path + -- and resolving symlinks) + matches = name:match(fullpat) or path:match(fullpat) + else + matches = tail:match(fullpat) + end + return matches +end + +--- Set the filetype for the given buffer from a file name. +--- +---@param name string File name (can be an absolute or relative path) +---@param bufnr number|nil The buffer to set the filetype for. Defaults to the current buffer. +function M.match(name, bufnr) + -- When fired from the main filetypedetect autocommand the {bufnr} argument is omitted, so we use + -- the current buffer. The {bufnr} argument is provided to allow extensibility in case callers + -- wish to perform filetype detection on buffers other than the current one. + bufnr = bufnr or api.nvim_get_current_buf() + + name = normalize_path(name) + + -- First check for the simple case where the full path exists as a key + local path = vim.fn.resolve(vim.fn.fnamemodify(name, ":p")) + if dispatch(filename[path], path, bufnr) then + return + end + + -- Next check against just the file name + local tail = vim.fn.fnamemodify(name, ":t") + if dispatch(filename[tail], path, bufnr) then + return + end + + -- Next, check the file path against available patterns with non-negative priority + local j = 1 + for i, v in ipairs(pattern_sorted) do + local k = next(v) + local opts = v[k][2] + if opts.priority < 0 then + j = i + break + end + + local ft = v[k][1] + local matches = match_pattern(name, path, tail, k) + if matches then + if dispatch(ft, path, bufnr, matches) then + return + end + end + end + + -- Next, check file extension + local ext = vim.fn.fnamemodify(name, ":e") + if dispatch(extension[ext], path, bufnr) then + return + end + + -- Finally, check patterns with negative priority + for i = j, #pattern_sorted do + local v = pattern_sorted[i] + local k = next(v) + + local ft = v[k][1] + local matches = match_pattern(name, path, tail, k) + if matches then + if dispatch(ft, path, bufnr, matches) then + return + end + end + end +end + +return M diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 236f3165f2..12faa0a6e1 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -25,16 +25,29 @@ end ---@param higroup highlight group to use for highlighting ---@param rtype type of range (:help setreg, default charwise) ---@param inclusive boolean indicating whether the range is end-inclusive (default false) -function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive) +---@param priority number indicating priority of highlight (default 50) +function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive, priority) rtype = rtype or 'v' inclusive = inclusive or false + priority = priority or 50 -- sanity check if start[2] < 0 or finish[1] < start[1] then return end local region = vim.region(bufnr, start, finish, rtype, inclusive) for linenr, cols in pairs(region) do - api.nvim_buf_add_highlight(bufnr, ns, higroup, linenr, cols[1], cols[2]) + local end_row + if cols[2] == -1 then + end_row = linenr + 1 + cols[2] = 0 + end + api.nvim_buf_set_extmark(bufnr, ns, linenr, cols[1], { + hl_group = higroup, + end_row = end_row, + end_col = cols[2], + priority = priority, + strict = false + }) end end @@ -82,7 +95,7 @@ function highlight.on_yank(opts) pos1 = {pos1[2] - 1, pos1[3] - 1 + pos1[4]} pos2 = {pos2[2] - 1, pos2[3] - 1 + pos2[4]} - highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive) + highlight.range(bufnr, yank_ns, higroup, pos1, pos2, event.regtype, event.inclusive, 200) vim.defer_fn( function() diff --git a/runtime/lua/vim/keymap.lua b/runtime/lua/vim/keymap.lua new file mode 100644 index 0000000000..d53b790746 --- /dev/null +++ b/runtime/lua/vim/keymap.lua @@ -0,0 +1,135 @@ +local keymap = {} + +--- Add a new |mapping|. +--- Examples: +--- <pre> +--- -- Can add mapping to Lua functions +--- vim.keymap.set('n', 'lhs', function() print("real lua function") end) +--- +--- -- Can use it to map multiple modes +--- vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, { buffer=true }) +--- +--- -- Can add mapping for specific buffer +--- vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", { silent = true, buffer = 5 }) +--- +--- -- Expr mappings +--- vim.keymap.set('i', '<Tab>', function() +--- return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>" +--- end, { expr = true }) +--- -- <Plug> mappings +--- vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)') +--- </pre> +--- +--- Note that in a mapping like: +--- <pre> +--- vim.keymap.set('n', 'asdf', require('jkl').my_fun) +--- </pre> +--- +--- the require('jkl') gets evaluated during this call in order to access the function. If you want to +--- avoid this cost at startup you can wrap it in a function, for example: +--- <pre> +--- vim.keymap.set('n', 'asdf', function() return require('jkl').my_fun() end) +--- </pre> +--- +---@param mode string|table Same mode short names as |nvim_set_keymap()|. +--- Can also be list of modes to create mapping on multiple modes. +---@param lhs string Left-hand side |{lhs}| of the mapping. +---@param rhs string|function Right-hand side |{rhs}| of the mapping. Can also be a Lua function. +-- +---@param opts table A table of |:map-arguments| such as "silent". In addition to the options +--- listed in |nvim_set_keymap()|, this table also accepts the following keys: +--- - replace_keycodes: (boolean, default true) When both this and expr is "true", +--- |nvim_replace_termcodes()| is applied to the result of Lua expr maps. +--- - remap: (boolean) Make the mapping recursive. This is the +--- inverse of the "noremap" option from |nvim_set_keymap()|. +--- Default `true` if `lhs` is a string starting with `<plug>` (case-insensitive), `false` otherwise. +---@see |nvim_set_keymap()| +function keymap.set(mode, lhs, rhs, opts) + vim.validate { + mode = {mode, {'s', 't'}}, + lhs = {lhs, 's'}, + rhs = {rhs, {'s', 'f'}}, + opts = {opts, 't', true} + } + + opts = vim.deepcopy(opts) or {} + local is_rhs_luaref = type(rhs) == "function" + mode = type(mode) == 'string' and {mode} or mode + + if is_rhs_luaref and opts.expr and opts.replace_keycodes ~= false then + local user_rhs = rhs + rhs = function () + return vim.api.nvim_replace_termcodes(user_rhs(), true, true, true) + end + end + -- clear replace_keycodes from opts table + opts.replace_keycodes = nil + + if opts.remap == nil then + -- remap by default on <plug> mappings and don't otherwise. + opts.noremap = is_rhs_luaref or rhs:lower():match("^<plug>") == nil + else + -- remaps behavior is opposite of noremap option. + opts.noremap = not opts.remap + opts.remap = nil + end + + if is_rhs_luaref then + opts.callback = rhs + rhs = '' + end + + if opts.buffer then + local bufnr = opts.buffer == true and 0 or opts.buffer + opts.buffer = nil + for _, m in ipairs(mode) do + vim.api.nvim_buf_set_keymap(bufnr, m, lhs, rhs, opts) + end + else + opts.buffer = nil + for _, m in ipairs(mode) do + vim.api.nvim_set_keymap(m, lhs, rhs, opts) + end + end +end + +--- Remove an existing mapping. +--- Examples: +--- <pre> +--- vim.keymap.del('n', 'lhs') +--- +--- vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buffer = 5 }) +--- </pre> +---@param opts table A table of optional arguments: +--- - buffer: (number or boolean) Remove a mapping from the given buffer. +--- When "true" or 0, use the current buffer. +---@see |vim.keymap.set()| +--- +function keymap.del(modes, lhs, opts) + vim.validate { + mode = {modes, {'s', 't'}}, + lhs = {lhs, 's'}, + opts = {opts, 't', true} + } + + opts = opts or {} + modes = type(modes) == 'string' and {modes} or modes + + local buffer = false + if opts.buffer ~= nil then + buffer = opts.buffer == true and 0 or opts.buffer + opts.buffer = nil + end + + if buffer == false then + for _, mode in ipairs(modes) do + vim.api.nvim_del_keymap(mode, lhs) + end + else + for _, mode in ipairs(modes) do + vim.api.nvim_buf_del_keymap(buffer, mode, lhs) + end + end +end + +return keymap diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index cfbabb12a6..8d11b4621c 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -256,7 +256,7 @@ local function validate_client_config(config) (not config.flags or not config.flags.debounce_text_changes or type(config.flags.debounce_text_changes) == 'number'), - "flags.debounce_text_changes must be nil or a number with the debounce time in milliseconds" + "flags.debounce_text_changes must be a number with the debounce time in milliseconds" ) local cmd, cmd_args = lsp._cmd_parts(config.cmd) @@ -306,70 +306,138 @@ local function once(fn) end end - local changetracking = {} do --@private --- client_id → state --- --- state + --- use_incremental_sync: bool + --- buffers: bufnr -> buffer_state + --- + --- buffer_state --- pending_change?: function that the timer starts to trigger didChange --- pending_changes: table (uri -> list of pending changeset tables)); - -- Only set if incremental_sync is used - --- use_incremental_sync: bool - --- buffers?: table (bufnr → lines); for incremental sync only + --- Only set if incremental_sync is used + --- --- timer?: uv_timer + --- lines: table local state_by_client = {} ---@private function changetracking.init(client, bufnr) + local use_incremental_sync = ( + if_nil(client.config.flags.allow_incremental_sync, true) + and client.resolved_capabilities.text_document_did_change == protocol.TextDocumentSyncKind.Incremental + ) local state = state_by_client[client.id] if not state then state = { - pending_changes = {}; - use_incremental_sync = ( - if_nil(client.config.flags.allow_incremental_sync, true) - and client.resolved_capabilities.text_document_did_change == protocol.TextDocumentSyncKind.Incremental - ); + buffers = {}; + debounce = client.config.flags.debounce_text_changes or 150, + use_incremental_sync = use_incremental_sync; } state_by_client[client.id] = state end - if not state.use_incremental_sync then - return - end - if not state.buffers then - state.buffers = {} + if not state.buffers[bufnr] then + local buf_state = {} + state.buffers[bufnr] = buf_state + if use_incremental_sync then + buf_state.lines = nvim_buf_get_lines(bufnr, 0, -1, true) + buf_state.lines_tmp = {} + buf_state.pending_changes = {} + end end - state.buffers[bufnr] = nvim_buf_get_lines(bufnr, 0, -1, true) end ---@private function changetracking.reset_buf(client, bufnr) - changetracking.flush(client) + changetracking.flush(client, bufnr) local state = state_by_client[client.id] if state and state.buffers then + local buf_state = state.buffers[bufnr] state.buffers[bufnr] = nil + if buf_state and buf_state.timer then + buf_state.timer:stop() + buf_state.timer:close() + buf_state.timer = nil + end end end ---@private function changetracking.reset(client_id) local state = state_by_client[client_id] - if state then - state_by_client[client_id] = nil - changetracking._reset_timer(state) + if not state then + return end + for _, buf_state in pairs(state.buffers) do + if buf_state.timer then + buf_state.timer:stop() + buf_state.timer:close() + buf_state.timer = nil + end + end + state.buffers = {} + end + + ---@private + -- + -- Adjust debounce time by taking time of last didChange notification into + -- consideration. If the last didChange happened more than `debounce` time ago, + -- debounce can be skipped and otherwise maybe reduced. + -- + -- This turns the debounce into a kind of client rate limiting + local function next_debounce(debounce, buf_state) + if debounce == 0 then + return 0 + end + local ns_to_ms = 0.000001 + if not buf_state.last_flush then + return debounce + end + local now = uv.hrtime() + local ms_since_last_flush = (now - buf_state.last_flush) * ns_to_ms + return math.max(debounce - ms_since_last_flush, 0) end ---@private function changetracking.prepare(bufnr, firstline, lastline, new_lastline) - local incremental_changes = function(client) - local cached_buffers = state_by_client[client.id].buffers - local curr_lines = nvim_buf_get_lines(bufnr, 0, -1, true) + local incremental_changes = function(client, buf_state) + + local prev_lines = buf_state.lines + local curr_lines = buf_state.lines_tmp + + local changed_lines = nvim_buf_get_lines(bufnr, firstline, new_lastline, true) + for i = 1, firstline do + curr_lines[i] = prev_lines[i] + end + for i = firstline + 1, new_lastline do + curr_lines[i] = changed_lines[i - firstline] + end + for i = lastline + 1, #prev_lines do + curr_lines[i - lastline + new_lastline] = prev_lines[i] + end + if tbl_isempty(curr_lines) then + -- Can happen when deleting the entire contents of a buffer, see https://github.com/neovim/neovim/issues/16259. + curr_lines[1] = '' + end + local line_ending = buf_get_line_ending(bufnr) local incremental_change = sync.compute_diff( - cached_buffers[bufnr], curr_lines, firstline, lastline, new_lastline, client.offset_encoding or 'utf-16', line_ending) - cached_buffers[bufnr] = curr_lines + buf_state.lines, curr_lines, firstline, lastline, new_lastline, client.offset_encoding or 'utf-16', line_ending) + + -- Double-buffering of lines tables is used to reduce the load on the garbage collector. + -- At this point the prev_lines table is useless, but its internal storage has already been allocated, + -- so let's keep it around for the next didChange event, in which it will become the next + -- curr_lines table. Note that setting elements to nil doesn't actually deallocate slots in the + -- internal storage - it merely marks them as free, for the GC to deallocate them. + for i in ipairs(prev_lines) do + prev_lines[i] = nil + end + buf_state.lines = curr_lines + buf_state.lines_tmp = prev_lines + return incremental_change end local full_changes = once(function() @@ -383,75 +451,68 @@ do return end local state = state_by_client[client.id] - local debounce = client.config.flags.debounce_text_changes - if not debounce then - local changes = state.use_incremental_sync and incremental_changes(client) or full_changes() - client.notify("textDocument/didChange", { - textDocument = { - uri = uri; - version = util.buf_versions[bufnr]; - }; - contentChanges = { changes, } - }) - return - end - changetracking._reset_timer(state) + local buf_state = state.buffers[bufnr] + changetracking._reset_timer(buf_state) + local debounce = next_debounce(state.debounce, buf_state) if state.use_incremental_sync then -- This must be done immediately and cannot be delayed -- The contents would further change and startline/endline may no longer fit - if not state.pending_changes[uri] then - state.pending_changes[uri] = {} - end - table.insert(state.pending_changes[uri], incremental_changes(client)) + table.insert(buf_state.pending_changes, incremental_changes(client, buf_state)) end - state.pending_change = function() - state.pending_change = nil + buf_state.pending_change = function() + buf_state.pending_change = nil + buf_state.last_flush = uv.hrtime() if client.is_stopped() or not vim.api.nvim_buf_is_valid(bufnr) then return end - if state.use_incremental_sync then - for change_uri, content_changes in pairs(state.pending_changes) do - client.notify("textDocument/didChange", { - textDocument = { - uri = change_uri; - version = util.buf_versions[vim.uri_to_bufnr(change_uri)]; - }; - contentChanges = content_changes, - }) - end - state.pending_changes = {} - else - client.notify("textDocument/didChange", { - textDocument = { - uri = uri; - version = util.buf_versions[bufnr]; - }; - contentChanges = { full_changes() }, - }) - end + local changes = state.use_incremental_sync and buf_state.pending_changes or { full_changes() } + client.notify("textDocument/didChange", { + textDocument = { + uri = uri, + version = util.buf_versions[bufnr], + }, + contentChanges = changes, + }) + buf_state.pending_changes = {} + end + if debounce == 0 then + buf_state.pending_change() + else + local timer = vim.loop.new_timer() + buf_state.timer = timer + -- Must use schedule_wrap because `full_changes()` calls nvim_buf_get_lines + timer:start(debounce, 0, vim.schedule_wrap(buf_state.pending_change)) end - state.timer = vim.loop.new_timer() - -- Must use schedule_wrap because `full_changes()` calls nvim_buf_get_lines - state.timer:start(debounce, 0, vim.schedule_wrap(state.pending_change)) end end - function changetracking._reset_timer(state) - if state.timer then - state.timer:stop() - state.timer:close() - state.timer = nil + function changetracking._reset_timer(buf_state) + if buf_state.timer then + buf_state.timer:stop() + buf_state.timer:close() + buf_state.timer = nil end end --- Flushes any outstanding change notification. ---@private - function changetracking.flush(client) + function changetracking.flush(client, bufnr) local state = state_by_client[client.id] - if state then - changetracking._reset_timer(state) - if state.pending_change then - state.pending_change() + if not state then + return + end + if bufnr then + local buf_state = state.buffers[bufnr] or {} + changetracking._reset_timer(buf_state) + if buf_state.pending_change then + buf_state.pending_change() + end + else + for _, buf_state in pairs(state.buffers) do + changetracking._reset_timer(buf_state) + if buf_state.pending_change then + buf_state.pending_change() + end end end end @@ -897,7 +958,7 @@ function lsp.start_client(config) client.initialized = true uninitialized_clients[client_id] = nil client.workspace_folders = workspace_folders - -- TODO(mjlbach): Backwards compatbility, to be removed in 0.7 + -- TODO(mjlbach): Backwards compatibility, to be removed in 0.7 client.workspaceFolders = client.workspace_folders client.server_capabilities = assert(result.capabilities, "initialize result doesn't contain capabilities") -- These are the cleaned up capabilities we use for dynamically deciding @@ -957,7 +1018,7 @@ function lsp.start_client(config) or error(string.format("not found: %q request handler for client %q.", method, client.name)) end -- Ensure pending didChange notifications are sent so that the server doesn't operate on a stale state - changetracking.flush(client) + changetracking.flush(client, bufnr) bufnr = resolve_bufnr(bufnr) local _ = log.debug() and log.debug(log_prefix, "client.request", client_id, method, params, handler, bufnr) local success, request_id = rpc.request(method, params, function(err, result) @@ -1014,14 +1075,16 @@ function lsp.start_client(config) ---@private --- Sends a notification to an LSP server. --- - ---@param method (string) LSP method name. - ---@param params (optional, table) LSP request params. - ---@param bufnr (number) Buffer handle, or 0 for current. + ---@param method string LSP method name. + ---@param params table|nil LSP request params. ---@returns {status} (bool) true if the notification was successful. ---If it is false, then it will always be false ---(the client has shutdown). - function client.notify(...) - return rpc.notify(...) + function client.notify(method, params) + if method ~= 'textDocument/didChange' then + changetracking.flush(client) + end + return rpc.notify(method, params) end ---@private @@ -1535,7 +1598,7 @@ end local function adjust_start_col(lnum, line, items, encoding) local min_start_char = nil for _, item in pairs(items) do - if item.textEdit and item.textEdit.range.start.line == lnum - 1 then + if item.filterText == nil and item.textEdit and item.textEdit.range.start.line == lnum - 1 then if min_start_char and min_start_char ~= item.textEdit.range.start.character then return nil end diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 8e3ed9b002..eb7ec579f1 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -184,7 +184,7 @@ function M.formatting_sync(options, timeout_ms) local result, err = client.request_sync('textDocument/formatting', params, timeout_ms, bufnr) if result and result.result then - util.apply_text_edits(result.result, bufnr) + util.apply_text_edits(result.result, bufnr, client.offset_encoding) elseif err then vim.notify('vim.lsp.buf.formatting_sync: ' .. err, vim.log.levels.WARN) end @@ -228,7 +228,7 @@ function M.formatting_seq_sync(options, timeout_ms, order) local params = util.make_formatting_params(options) local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, vim.api.nvim_get_current_buf()) if result and result.result then - util.apply_text_edits(result.result, bufnr) + util.apply_text_edits(result.result, bufnr, client.offset_encoding) elseif err then vim.notify(string.format("vim.lsp.buf.formatting_seq_sync: (%s) %s", client.name, err), vim.log.levels.WARN) end @@ -447,13 +447,16 @@ end ---@param query (string, optional) function M.workspace_symbol(query) query = query or npcall(vfn.input, "Query: ") + if query == nil then + return + end local params = {query = query} request('workspace/symbol', params) end --- Send request to the server to resolve document highlights for the current --- text document position. This request can be triggered by a key mapping or ---- by events such as `CursorHold`, eg: +--- by events such as `CursorHold`, e.g.: --- --- <pre> --- autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() @@ -503,7 +506,7 @@ local function on_code_action_results(results, ctx) ---@private local function apply_action(action, client) if action.edit then - util.apply_workspace_edit(action.edit) + util.apply_workspace_edit(action.edit, client.offset_encoding) end if action.command then local command = type(action.command) == 'table' and action.command or action @@ -627,14 +630,19 @@ end --- Executes an LSP server command. --- ----@param command A valid `ExecuteCommandParams` object +---@param command_params table A valid `ExecuteCommandParams` object ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand -function M.execute_command(command) +function M.execute_command(command_params) validate { - command = { command.command, 's' }, - arguments = { command.arguments, 't', true } + command = { command_params.command, 's' }, + arguments = { command_params.arguments, 't', true } + } + command_params = { + command=command_params.command, + arguments=command_params.arguments, + workDoneToken=command_params.workDoneToken, } - request('workspace/executeCommand', command) + request('workspace/executeCommand', command_params ) end return M diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index 8850d25233..68942ae11a 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -168,8 +168,8 @@ end --- }, --- -- Use a function to dynamically turn signs off --- -- and on, using buffer local variables ---- signs = function(bufnr, client_id) ---- return vim.bo[bufnr].show_signs == false +--- signs = function(namespace, bufnr) +--- return vim.b[bufnr].show_signs == true --- end, --- -- Disable a feature --- update_in_insert = false, @@ -243,7 +243,7 @@ end ---@param client_id number ---@private function M.save(diagnostics, bufnr, client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.save is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.save is deprecated. See :h deprecated', vim.log.levels.WARN) local namespace = M.get_namespace(client_id) vim.diagnostic.set(namespace, bufnr, diagnostic_lsp_to_vim(diagnostics, bufnr, client_id)) end @@ -257,7 +257,7 @@ end --- If nil, diagnostics of all clients are included. ---@return table with diagnostics grouped by bufnr (bufnr: Diagnostic[]) function M.get_all(client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_all is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_all is deprecated. See :h deprecated', vim.log.levels.WARN) local result = {} local namespace if client_id then @@ -279,7 +279,7 @@ end --- Else, return just the diagnostics associated with the client_id. ---@param predicate function|nil Optional function for filtering diagnostics function M.get(bufnr, client_id, predicate) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get is deprecated. See :h deprecated', vim.log.levels.WARN) predicate = predicate or function() return true end if client_id == nil then local all_diagnostics = {} @@ -341,7 +341,7 @@ end ---@param severity DiagnosticSeverity ---@param client_id number the client id function M.get_count(bufnr, severity, client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_count is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_count is deprecated. See :h deprecated', vim.log.levels.WARN) severity = severity_lsp_to_vim(severity) local opts = { severity = severity } if client_id ~= nil then @@ -358,7 +358,7 @@ end ---@param opts table See |vim.lsp.diagnostic.goto_next()| ---@return table Previous diagnostic function M.get_prev(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_prev is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_prev is deprecated. See :h deprecated', vim.log.levels.WARN) if opts then if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -376,7 +376,7 @@ end ---@param opts table See |vim.lsp.diagnostic.goto_next()| ---@return table Previous diagnostic position function M.get_prev_pos(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_prev_pos is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_prev_pos is deprecated. See :h deprecated', vim.log.levels.WARN) if opts then if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -393,7 +393,7 @@ end --- ---@param opts table See |vim.lsp.diagnostic.goto_next()| function M.goto_prev(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.goto_prev is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.goto_prev is deprecated. See :h deprecated', vim.log.levels.WARN) if opts then if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -411,7 +411,7 @@ end ---@param opts table See |vim.lsp.diagnostic.goto_next()| ---@return table Next diagnostic function M.get_next(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_next is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_next is deprecated. See :h deprecated', vim.log.levels.WARN) if opts then if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -429,7 +429,7 @@ end ---@param opts table See |vim.lsp.diagnostic.goto_next()| ---@return table Next diagnostic position function M.get_next_pos(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_next_pos is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_next_pos is deprecated. See :h deprecated', vim.log.levels.WARN) if opts then if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -444,7 +444,7 @@ end --- ---@deprecated Prefer |vim.diagnostic.goto_next()| function M.goto_next(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.goto_next is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.goto_next is deprecated. See :h deprecated', vim.log.levels.WARN) if opts then if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -468,7 +468,7 @@ end --- - severity_limit (DiagnosticSeverity): --- - Limit severity of diagnostics found. E.g. "Warning" means { "Error", "Warning" } will be valid. function M.set_signs(diagnostics, bufnr, client_id, _, opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.set_signs is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.set_signs is deprecated. See :h deprecated', vim.log.levels.WARN) local namespace = M.get_namespace(client_id) if opts and not opts.severity and opts.severity_limit then opts.severity = {min=severity_lsp_to_vim(opts.severity_limit)} @@ -489,7 +489,7 @@ end --- - severity_limit (DiagnosticSeverity): --- - Limit severity of diagnostics found. E.g. "Warning" means { "Error", "Warning" } will be valid. function M.set_underline(diagnostics, bufnr, client_id, _, opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.set_underline is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.set_underline is deprecated. See :h deprecated', vim.log.levels.WARN) local namespace = M.get_namespace(client_id) if opts and not opts.severity and opts.severity_limit then opts.severity = {min=severity_lsp_to_vim(opts.severity_limit)} @@ -511,7 +511,7 @@ end --- - severity_limit (DiagnosticSeverity): --- - Limit severity of diagnostics found. E.g. "Warning" means { "Error", "Warning" } will be valid. function M.set_virtual_text(diagnostics, bufnr, client_id, _, opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.set_virtual_text is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.set_virtual_text is deprecated. See :h deprecated', vim.log.levels.WARN) local namespace = M.get_namespace(client_id) if opts and not opts.severity and opts.severity_limit then opts.severity = {min=severity_lsp_to_vim(opts.severity_limit)} @@ -530,7 +530,7 @@ end ---@return an array of [text, hl_group] arrays. This can be passed directly to --- the {virt_text} option of |nvim_buf_set_extmark()|. function M.get_virtual_text_chunks_for_line(bufnr, _, line_diags, opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.get_virtual_text_chunks_for_line is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.get_virtual_text_chunks_for_line is deprecated. See :h deprecated', vim.log.levels.WARN) return vim.diagnostic._get_virt_text_chunks(diagnostic_lsp_to_vim(line_diags, bufnr), opts) end @@ -548,7 +548,7 @@ end ---@param position table|nil The (0,0)-indexed position ---@return table {popup_bufnr, win_id} function M.show_position_diagnostics(opts, buf_nr, position) - vim.api.nvim_echo({{'vim.lsp.diagnostic.show_position_diagnostics is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.show_position_diagnostics is deprecated. See :h deprecated', vim.log.levels.WARN) opts = opts or {} opts.scope = "cursor" opts.pos = position @@ -572,7 +572,7 @@ end ---@param client_id number|nil the client id ---@return table {popup_bufnr, win_id} function M.show_line_diagnostics(opts, buf_nr, line_nr, client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.show_line_diagnostics is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.show_line_diagnostics is deprecated. See :h deprecated', vim.log.levels.WARN) opts = opts or {} opts.scope = "line" opts.pos = line_nr @@ -596,7 +596,7 @@ end --- client. The default is to redraw diagnostics for all attached --- clients. function M.redraw(bufnr, client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.redraw is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.redraw is deprecated. See :h deprecated', vim.log.levels.WARN) bufnr = get_bufnr(bufnr) if not client_id then return vim.lsp.for_each_buffer_client(bufnr, function(client) @@ -624,7 +624,7 @@ end --- - {workspace}: (boolean, default true) --- - Set the list with workspace diagnostics function M.set_qflist(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.set_qflist is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.set_qflist is deprecated. See :h deprecated', vim.log.levels.WARN) opts = opts or {} if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -656,7 +656,7 @@ end --- - {workspace}: (boolean, default false) --- - Set the list with workspace diagnostics function M.set_loclist(opts) - vim.api.nvim_echo({{'vim.lsp.diagnostic.set_loclist is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.set_loclist is deprecated. See :h deprecated', vim.log.levels.WARN) opts = opts or {} if opts.severity then opts.severity = severity_lsp_to_vim(opts.severity) @@ -684,7 +684,7 @@ end -- send diagnostic information and the client will still process it. The -- diagnostics are simply not displayed to the user. function M.disable(bufnr, client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.disable is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.disable is deprecated. See :h deprecated', vim.log.levels.WARN) if not client_id then return vim.lsp.for_each_buffer_client(bufnr, function(client) M.disable(bufnr, client.id) @@ -705,7 +705,7 @@ end --- client. The default is to enable diagnostics for all attached --- clients. function M.enable(bufnr, client_id) - vim.api.nvim_echo({{'vim.lsp.diagnostic.enable is deprecated. See :h deprecated', 'WarningMsg'}}, true, {}) + vim.notify_once('vim.lsp.diagnostic.enable is deprecated. See :h deprecated', vim.log.levels.WARN) if not client_id then return vim.lsp.for_each_buffer_client(bufnr, function(client) M.enable(bufnr, client.id) diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index a1d3b2aa94..a997b887d9 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -111,13 +111,15 @@ M['client/registerCapability'] = function(_, _, ctx) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit -M['workspace/applyEdit'] = function(_, workspace_edit) +M['workspace/applyEdit'] = function(_, workspace_edit, ctx) if not workspace_edit then return end -- TODO(ashkan) Do something more with label? + local client_id = ctx.client_id + local client = vim.lsp.get_client_by_id(client_id) if workspace_edit.label then print("Workspace edit", workspace_edit.label) end - local status, result = pcall(util.apply_workspace_edit, workspace_edit.edit) + local status, result = pcall(util.apply_workspace_edit, workspace_edit.edit, client.offset_encoding) return { applied = status; failureReason = result; @@ -150,6 +152,17 @@ M['workspace/configuration'] = function(_, result, ctx) return response end +--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_workspaceFolders +M['workspace/workspaceFolders'] = function(_, _, ctx) + local client_id = ctx.client_id + local client = vim.lsp.get_client_by_id(client_id) + if not client then + err_message("LSP[id=", client_id, "] client has shut down after sending the message") + return + end + return client.workspace_folders or vim.NIL +end + M['textDocument/publishDiagnostics'] = function(...) return require('vim.lsp.diagnostic').on_publish_diagnostics(...) end @@ -159,6 +172,31 @@ M['textDocument/codeLens'] = function(...) end +--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references +M['textDocument/references'] = function(_, result, ctx, config) + if not result or vim.tbl_isempty(result) then + vim.notify('No references found') + else + local client = vim.lsp.get_client_by_id(ctx.client_id) + config = config or {} + if config.loclist then + vim.fn.setloclist(0, {}, ' ', { + title = 'References'; + items = util.locations_to_items(result, client.offset_encoding); + context = ctx; + }) + api.nvim_command("lopen") + else + vim.fn.setqflist({}, ' ', { + title = 'References'; + items = util.locations_to_items(result, client.offset_encoding); + context = ctx; + }) + api.nvim_command("botright copen") + end + end +end + ---@private --- Return a function that converts LSP responses to list items and opens the list @@ -169,23 +207,26 @@ end --- loclist: (boolean) use the location list (default is to use the quickfix list) --- ---@param map_result function `((resp, bufnr) -> list)` to convert the response ----@param entity name of the resource used in a `not found` error message -local function response_to_list(map_result, entity) - return function(_,result, ctx, config) +---@param entity string name of the resource used in a `not found` error message +---@param title_fn function Function to call to generate list title +local function response_to_list(map_result, entity, title_fn) + return function(_, result, ctx, config) if not result or vim.tbl_isempty(result) then vim.notify('No ' .. entity .. ' found') else config = config or {} if config.loclist then vim.fn.setloclist(0, {}, ' ', { - title = 'Language Server'; + title = title_fn(ctx); items = map_result(result, ctx.bufnr); + context = ctx; }) api.nvim_command("lopen") else vim.fn.setqflist({}, ' ', { - title = 'Language Server'; + title = title_fn(ctx); items = map_result(result, ctx.bufnr); + context = ctx; }) api.nvim_command("botright copen") end @@ -194,31 +235,36 @@ local function response_to_list(map_result, entity) end ---see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references -M['textDocument/references'] = response_to_list(util.locations_to_items, 'references') - --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol -M['textDocument/documentSymbol'] = response_to_list(util.symbols_to_items, 'document symbols') +M['textDocument/documentSymbol'] = response_to_list(util.symbols_to_items, 'document symbols', function(ctx) + local fname = vim.fn.fnamemodify(vim.uri_to_fname(ctx.params.textDocument.uri), ":.") + return string.format('Symbols in %s', fname) +end) --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_symbol -M['workspace/symbol'] = response_to_list(util.symbols_to_items, 'symbols') +M['workspace/symbol'] = response_to_list(util.symbols_to_items, 'symbols', function(ctx) + return string.format("Symbols matching '%s'", ctx.params.query) +end) --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename -M['textDocument/rename'] = function(_, result, _) +M['textDocument/rename'] = function(_, result, ctx, _) if not result then return end - util.apply_workspace_edit(result) + local client = vim.lsp.get_client_by_id(ctx.client_id) + util.apply_workspace_edit(result, client.offset_encoding) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rangeFormatting M['textDocument/rangeFormatting'] = function(_, result, ctx, _) if not result then return end - util.apply_text_edits(result, ctx.bufnr) + local client = vim.lsp.get_client_by_id(ctx.client_id) + util.apply_text_edits(result, ctx.bufnr, client.offset_encoding) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting M['textDocument/formatting'] = function(_, result, ctx, _) if not result then return end - util.apply_text_edits(result, ctx.bufnr) + local client = vim.lsp.get_client_by_id(ctx.client_id) + util.apply_text_edits(result, ctx.bufnr, client.offset_encoding) end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion @@ -277,19 +323,23 @@ local function location_handler(_, result, ctx, _) local _ = log.info() and log.info(ctx.method, 'No location found') return nil end + local client = vim.lsp.get_client_by_id(ctx.client_id) -- textDocument/definition can return Location or Location[] -- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition if vim.tbl_islist(result) then - util.jump_to_location(result[1]) + util.jump_to_location(result[1], client.offset_encoding) if #result > 1 then - vim.fn.setqflist({}, ' ', {title = 'LSP locations', items = util.locations_to_items(result)}) + vim.fn.setqflist({}, ' ', { + title = 'LSP locations', + items = util.locations_to_items(result, client.offset_encoding) + }) api.nvim_command("copen") end else - util.jump_to_location(result) + util.jump_to_location(result, client.offset_encoding) end end @@ -439,14 +489,20 @@ for k, fn in pairs(M) do }) if err then - local client = vim.lsp.get_client_by_id(ctx.client_id) - local client_name = client and client.name or string.format("client_id=%d", ctx.client_id) -- LSP spec: -- interface ResponseError: -- code: integer; -- message: string; -- data?: string | number | boolean | array | object | null; - return err_message(client_name .. ': ' .. tostring(err.code) .. ': ' .. err.message) + + -- Per LSP, don't show ContentModified error to the user. + if err.code ~= protocol.ErrorCodes.ContentModified then + local client = vim.lsp.get_client_by_id(ctx.client_id) + local client_name = client and client.name or string.format("client_id=%d", ctx.client_id) + + err_message(client_name .. ': ' .. tostring(err.code) .. ': ' .. err.message) + end + return end return fn(err, result, ctx, config) diff --git a/runtime/lua/vim/lsp/sync.lua b/runtime/lua/vim/lsp/sync.lua index d01f45ad8f..0f4e5b572b 100644 --- a/runtime/lua/vim/lsp/sync.lua +++ b/runtime/lua/vim/lsp/sync.lua @@ -298,7 +298,7 @@ end ---@private -- rangelength depends on the offset encoding --- bytes for utf-8 (clangd with extenion) +-- bytes for utf-8 (clangd with extension) -- codepoints for utf-16 -- codeunits for utf-32 -- Line endings count here as 2 chars for \r\n (dos), 1 char for \n (unix), and 1 char for \r (mac) diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index dcd68a3433..d22c00ae76 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -10,14 +10,6 @@ local uv = vim.loop local npcall = vim.F.npcall local split = vim.split -local _warned = {} -local warn_once = function(message) - if not _warned[message] then - vim.api.nvim_err_writeln(message) - _warned[message] = true - end -end - local M = {} local default_border = { @@ -201,6 +193,11 @@ end local function get_lines(bufnr, rows) rows = type(rows) == "table" and rows or { rows } + -- This is needed for bufload and bufloaded + if bufnr == 0 then + bufnr = vim.api.nvim_get_current_buf() + end + ---@private local function buf_lines() local lines = {} @@ -280,7 +277,7 @@ end ---@private --- Position is a https://microsoft.github.io/language-server-protocol/specifications/specification-current/#position --- Returns a zero-indexed column, since set_lines() does the conversion to ----@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to utf-16 +---@param offset_encoding string utf-8|utf-16|utf-32 --- 1-indexed local function get_line_byte_from_position(bufnr, position, offset_encoding) -- LSP's line and characters are 0-indexed @@ -289,7 +286,7 @@ local function get_line_byte_from_position(bufnr, position, offset_encoding) -- When on the first character, we can ignore the difference between byte and -- character if col > 0 then - local line = get_line(bufnr, position.line) + local line = get_line(bufnr, position.line) or '' local ok, result ok, result = pcall(_str_byteindex_enc, line, col, offset_encoding) if ok then @@ -363,15 +360,14 @@ end --- Applies a list of text edits to a buffer. ---@param text_edits table list of `TextEdit` objects ---@param bufnr number Buffer id ----@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to encoding of first client of `bufnr` +---@param offset_encoding string utf-8|utf-16|utf-32 defaults to encoding of first client of `bufnr` ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textEdit function M.apply_text_edits(text_edits, bufnr, offset_encoding) validate { text_edits = { text_edits, 't', false }; bufnr = { bufnr, 'number', false }; - offset_encoding = { offset_encoding, 'string', true }; + offset_encoding = { offset_encoding, 'string', false }; } - offset_encoding = offset_encoding or M._get_offset_encoding(bufnr) if not next(text_edits) then return end if not api.nvim_buf_is_loaded(bufnr) then vim.fn.bufload(bufnr) @@ -405,25 +401,6 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) end end) - -- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't accept it so we should fix it here. - local has_eol_text_edit = false - local max = vim.api.nvim_buf_line_count(bufnr) - local len = _str_utfindex_enc(vim.api.nvim_buf_get_lines(bufnr, -2, -1, false)[1] or '', nil, offset_encoding) - text_edits = vim.tbl_map(function(text_edit) - if max <= text_edit.range.start.line then - text_edit.range.start.line = max - 1 - text_edit.range.start.character = len - text_edit.newText = '\n' .. text_edit.newText - has_eol_text_edit = true - end - if max <= text_edit.range['end'].line then - text_edit.range['end'].line = max - 1 - text_edit.range['end'].character = len - has_eol_text_edit = true - end - return text_edit - end, text_edits) - -- Some LSP servers are depending on the VSCode behavior. -- The VSCode will re-locate the cursor position after applying TextEdit so we also do it. local is_current_buf = vim.api.nvim_get_current_buf() == bufnr @@ -443,16 +420,38 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) -- Apply text edits. local is_cursor_fixed = false + local has_eol_text_edit = false for _, text_edit in ipairs(text_edits) do + -- Normalize line ending + text_edit.newText, _ = string.gsub(text_edit.newText, '\r\n?', '\n') + + -- Convert from LSP style ranges to Neovim style ranges. local e = { start_row = text_edit.range.start.line, - start_col = get_line_byte_from_position(bufnr, text_edit.range.start), + start_col = get_line_byte_from_position(bufnr, text_edit.range.start, offset_encoding), end_row = text_edit.range['end'].line, - end_col = get_line_byte_from_position(bufnr, text_edit.range['end']), + end_col = get_line_byte_from_position(bufnr, text_edit.range['end'], offset_encoding), text = vim.split(text_edit.newText, '\n', true), } + + -- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't accept it so we should fix it here. + local max = vim.api.nvim_buf_line_count(bufnr) + if max <= e.start_row or max <= e.end_row then + local len = #(get_line(bufnr, max - 1) or '') + if max <= e.start_row then + e.start_row = max - 1 + e.start_col = len + table.insert(e.text, 1, '') + end + if max <= e.end_row then + e.end_row = max - 1 + e.end_col = len + end + has_eol_text_edit = true + end vim.api.nvim_buf_set_text(bufnr, e.start_row, e.start_col, e.end_row, e.end_col, e.text) + -- Fix cursor position. local row_count = (e.end_row - e.start_row) + 1 if e.end_row < cursor.row then cursor.row = cursor.row + (#e.text - row_count) @@ -467,10 +466,13 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) end end + local max = vim.api.nvim_buf_line_count(bufnr) + + -- Apply fixed cursor position. if is_cursor_fixed then local is_valid_cursor = true - is_valid_cursor = is_valid_cursor and cursor.row < vim.api.nvim_buf_line_count(bufnr) - is_valid_cursor = is_valid_cursor and cursor.col <= #(vim.api.nvim_buf_get_lines(bufnr, cursor.row, cursor.row + 1, false)[1] or '') + is_valid_cursor = is_valid_cursor and cursor.row < max + is_valid_cursor = is_valid_cursor and cursor.col <= #(get_line(bufnr, max - 1) or '') if is_valid_cursor then vim.api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col }) end @@ -479,7 +481,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) -- Remove final line if needed local fix_eol = has_eol_text_edit fix_eol = fix_eol and api.nvim_buf_get_option(bufnr, 'fixeol') - fix_eol = fix_eol and (vim.api.nvim_buf_get_lines(bufnr, -2, -1, false)[1] or '') == '' + fix_eol = fix_eol and get_line(bufnr, max - 1) == '' if fix_eol then vim.api.nvim_buf_set_lines(bufnr, -2, -1, false, {}) end @@ -517,9 +519,12 @@ end ---@param text_document_edit table: a `TextDocumentEdit` object ---@param index number: Optional index of the edit, if from a list of edits (or nil, if not from a list) ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentEdit -function M.apply_text_document_edit(text_document_edit, index) +function M.apply_text_document_edit(text_document_edit, index, offset_encoding) local text_document = text_document_edit.textDocument local bufnr = vim.uri_to_bufnr(text_document.uri) + if offset_encoding == nil then + vim.notify_once("apply_text_document_edit must be called with valid offset encoding", vim.log.levels.WARN) + end -- For lists of text document edits, -- do not check the version after the first edit. @@ -538,7 +543,7 @@ function M.apply_text_document_edit(text_document_edit, index) return end - M.apply_text_edits(text_document_edit.edits, bufnr) + M.apply_text_edits(text_document_edit.edits, bufnr, offset_encoding) end --- Parses snippets in a completion entry. @@ -735,9 +740,13 @@ end --- Applies a `WorkspaceEdit`. --- ----@param workspace_edit (table) `WorkspaceEdit` +---@param workspace_edit table `WorkspaceEdit` +---@param offset_encoding string utf-8|utf-16|utf-32 (required) --see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit -function M.apply_workspace_edit(workspace_edit) +function M.apply_workspace_edit(workspace_edit, offset_encoding) + if offset_encoding == nil then + vim.notify_once("apply_workspace_edit must be called with valid offset encoding", vim.log.levels.WARN) + end if workspace_edit.documentChanges then for idx, change in ipairs(workspace_edit.documentChanges) do if change.kind == "rename" then @@ -753,7 +762,7 @@ function M.apply_workspace_edit(workspace_edit) elseif change.kind then error(string.format("Unsupported change: %q", vim.inspect(change))) else - M.apply_text_document_edit(change, idx) + M.apply_text_document_edit(change, idx, offset_encoding) end end return @@ -766,7 +775,7 @@ function M.apply_workspace_edit(workspace_edit) for uri, changes in pairs(all_changes) do local bufnr = vim.uri_to_bufnr(uri) - M.apply_text_edits(changes, bufnr) + M.apply_text_edits(changes, bufnr, offset_encoding) end end @@ -842,7 +851,8 @@ function M.convert_signature_help_to_markdown_lines(signature_help, ft, triggers local active_hl local active_signature = signature_help.activeSignature or 0 -- If the activeSignature is not inside the valid range, then clip it. - if active_signature >= #signature_help.signatures then + -- In 3.15 of the protocol, activeSignature was allowed to be negative + if active_signature >= #signature_help.signatures or active_signature < 0 then active_signature = 0 end local signature = signature_help.signatures[active_signature + 1] @@ -983,12 +993,16 @@ end --- Jumps to a location. --- ----@param location (`Location`|`LocationLink`) +---@param location table (`Location`|`LocationLink`) +---@param offset_encoding string utf-8|utf-16|utf-32 (required) ---@returns `true` if the jump succeeded -function M.jump_to_location(location) +function M.jump_to_location(location, offset_encoding) -- location may be Location or LocationLink local uri = location.uri or location.targetUri if uri == nil then return end + if offset_encoding == nil then + vim.notify_once("jump_to_location must be called with valid offset encoding", vim.log.levels.WARN) + end local bufnr = vim.uri_to_bufnr(uri) -- Save position in jumplist vim.cmd "normal! m'" @@ -1003,7 +1017,7 @@ function M.jump_to_location(location) api.nvim_buf_set_option(bufnr, 'buflisted', true) local range = location.range or location.targetSelectionRange local row = range.start.line - local col = get_line_byte_from_position(bufnr, range.start) + local col = get_line_byte_from_position(bufnr, range.start, offset_encoding) api.nvim_win_set_cursor(0, {row + 1, col}) -- Open folds under the cursor vim.cmd("normal! zv") @@ -1512,11 +1526,13 @@ do --[[ References ]] --- ---@param bufnr number Buffer id ---@param references table List of `DocumentHighlight` objects to highlight - ---@param offset_encoding string One of "utf-8", "utf-16", "utf-32", or nil. Defaults to `offset_encoding` of first client of `bufnr` + ---@param offset_encoding string One of "utf-8", "utf-16", "utf-32". ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#documentHighlight function M.buf_highlight_references(bufnr, references, offset_encoding) - validate { bufnr = {bufnr, 'n', true} } - offset_encoding = offset_encoding or M._get_offset_encoding(bufnr) + validate { + bufnr = {bufnr, 'n', true}, + offset_encoding = { offset_encoding, 'string', false }; + } for _, reference in ipairs(references) do local start_line, start_char = reference["range"]["start"]["line"], reference["range"]["start"]["character"] local end_line, end_char = reference["range"]["end"]["line"], reference["range"]["end"]["character"] @@ -1534,7 +1550,10 @@ do --[[ References ]] reference_ns, document_highlight_kind[kind], { start_line, start_idx }, - { end_line, end_idx }) + { end_line, end_idx }, + nil, + false, + 40) end end end @@ -1549,9 +1568,14 @@ end) --- The result can be passed to the {list} argument of |setqflist()| or --- |setloclist()|. --- ----@param locations (table) list of `Location`s or `LocationLink`s +---@param locations table list of `Location`s or `LocationLink`s +---@param offset_encoding string offset_encoding for locations utf-8|utf-16|utf-32 ---@returns (table) list of items -function M.locations_to_items(locations) +function M.locations_to_items(locations, offset_encoding) + if offset_encoding == nil then + vim.notify_once("locations_to_items must be called with valid offset encoding", vim.log.levels.WARN) + end + local items = {} local grouped = setmetatable({}, { __index = function(t, k) @@ -1591,7 +1615,7 @@ function M.locations_to_items(locations) local pos = temp.start local row = pos.line local line = lines[row] or "" - local col = pos.character + local col = M._str_byteindex_enc(line, pos.character, offset_encoding) table.insert(items, { filename = filename, lnum = row + 1, @@ -1775,7 +1799,13 @@ function M._get_offset_encoding(bufnr) local offset_encoding for _, client in pairs(vim.lsp.buf_get_clients(bufnr)) do - local this_offset_encoding = client.offset_encoding or "utf-16" + if client.offset_encoding == nil then + vim.notify_once( + string.format("Client (id: %s) offset_encoding is nil. Do not unset offset_encoding.", client.id), + vim.log.levels.ERROR + ) + end + local this_offset_encoding = client.offset_encoding if not offset_encoding then offset_encoding = this_offset_encoding elseif offset_encoding ~= this_offset_encoding then @@ -1822,7 +1852,7 @@ function M.make_given_range_params(start_pos, end_pos, bufnr, offset_encoding) end_pos = {end_pos, 't', true}; offset_encoding = {offset_encoding, 's', true}; } - bufnr = bufnr or 0 + bufnr = bufnr or vim.api.nvim_get_current_buf() offset_encoding = offset_encoding or M._get_offset_encoding(bufnr) local A = list_extend({}, start_pos or api.nvim_buf_get_mark(bufnr, '<')) local B = list_extend({}, end_pos or api.nvim_buf_get_mark(bufnr, '>')) @@ -1904,7 +1934,9 @@ end ---@returns (number, number) `offset_encoding` index of the character in line {row} column {col} in buffer {buf} function M.character_offset(buf, row, col, offset_encoding) local line = get_line(buf, row) - offset_encoding = offset_encoding or M._get_offset_encoding(buf) + if offset_encoding == nil then + vim.notify_once("character_offset must be called with valid offset encoding", vim.log.levels.WARN) + end -- If the col is past the EOL, use the line length. if col > #line then return _str_utfindex_enc(line, nil, offset_encoding) @@ -1928,7 +1960,6 @@ function M.lookup_section(settings, section) end M._get_line_byte_from_position = get_line_byte_from_position -M._warn_once = warn_once M.buf_versions = {} diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 07f6418c0c..f9d539f028 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -11,6 +11,7 @@ local parsers = {} local M = vim.tbl_extend("error", query, language) M.language_version = vim._ts_get_language_version() +M.minimum_language_version = vim._ts_get_minimum_language_version() setmetatable(M, { __index = function (t, k) diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua index 6f347ff25f..8b106108df 100644 --- a/runtime/lua/vim/treesitter/language.lua +++ b/runtime/lua/vim/treesitter/language.lua @@ -14,7 +14,7 @@ function M.require_language(lang, path, silent) return true end if path == nil then - local fname = 'parser/' .. lang .. '.*' + local fname = 'parser/' .. vim.fn.fnameescape(lang) .. '.*' local paths = a.nvim_get_runtime_file(fname, false) if #paths == 0 then if silent then diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua index 594765761d..85fd5cd8e0 100644 --- a/runtime/lua/vim/treesitter/languagetree.lua +++ b/runtime/lua/vim/treesitter/languagetree.lua @@ -77,7 +77,7 @@ end --- Determines whether this tree is valid. --- If the tree is invalid, `parse()` must be called ---- to get the an updated tree. +--- to get the updated tree. function LanguageTree:is_valid() return self._valid end diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index ebed502c92..14940332d6 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -138,6 +138,13 @@ function M.get_query(lang, query_name) end end +local query_cache = setmetatable({}, { + __index = function(tbl, key) + rawset(tbl, key, {}) + return rawget(tbl, key) + end +}) + --- Parse {query} as a string. (If the query is in a file, the caller --- should read the contents into a string before calling). --- @@ -151,17 +158,23 @@ end --- -` info.captures` also points to `captures`. --- - `info.patterns` contains information about predicates. --- ----@param lang The language ----@param query A string containing the query (s-expr syntax) +---@param lang string The language +---@param query string A string containing the query (s-expr syntax) --- ---@returns The query function M.parse_query(lang, query) language.require_language(lang) - local self = setmetatable({}, Query) - self.query = vim._ts_parse_query(lang, query) - self.info = self.query:inspect() - self.captures = self.info.captures - return self + local cached = query_cache[lang][query] + if cached then + return cached + else + local self = setmetatable({}, Query) + self.query = vim._ts_parse_query(lang, query) + self.info = self.query:inspect() + self.captures = self.info.captures + query_cache[lang][query] = self + return self + end end --- Gets the text corresponding to a given node @@ -186,11 +199,13 @@ function M.get_node_text(node, source) lines = a.nvim_buf_get_lines(source, start_row, end_row + 1, true) end - if #lines == 1 then - lines[1] = string.sub(lines[1], start_col+1, end_col) - else - lines[1] = string.sub(lines[1], start_col+1) - lines[#lines] = string.sub(lines[#lines], 1, end_col) + if #lines > 0 then + if #lines == 1 then + lines[1] = string.sub(lines[1], start_col+1, end_col) + else + lines[1] = string.sub(lines[1], start_col+1) + lines[#lines] = string.sub(lines[#lines], 1, end_col) + end end return table.concat(lines, "\n") diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index 9568b60fd0..9d4b38f08a 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -18,6 +18,24 @@ local M = {} --- Called once the user made a choice. --- `idx` is the 1-based index of `item` within `item`. --- `nil` if the user aborted the dialog. +--- +--- +--- Example: +--- <pre> +--- vim.ui.select({ 'tabs', 'spaces' }, { +--- prompt = 'Select tabs or spaces:', +--- format_item = function(item) +--- return "I'd like to choose " .. item +--- end, +--- }, function(choice) +--- if choice == 'spaces' then +--- vim.o.expandtab = true +--- else +--- vim.o.expandtab = false +--- end +--- end) +--- </pre> + function M.select(items, opts, on_choice) vim.validate { items = { items, 'table', false }, @@ -57,6 +75,13 @@ end --- Called once the user confirms or abort the input. --- `input` is what the user typed. --- `nil` if the user aborted the dialog. +--- +--- Example: +--- <pre> +--- vim.ui.input({ prompt = 'Enter value for shiftwidth: ' }, function(input) +--- vim.o.shiftwidth = tonumber(input) +--- end) +--- </pre> function M.input(opts, on_confirm) vim.validate { on_confirm = { on_confirm, 'function', false }, diff --git a/runtime/pack/dist/opt/matchit/autoload/matchit.vim b/runtime/pack/dist/opt/matchit/autoload/matchit.vim index e8689980ae..eafb7c0551 100644 --- a/runtime/pack/dist/opt/matchit/autoload/matchit.vim +++ b/runtime/pack/dist/opt/matchit/autoload/matchit.vim @@ -763,9 +763,9 @@ fun! s:ParseSkip(str) let skip = "synIDattr(synID(line('.'),col('.'),1),'name') !~? '" .. \ strpart(skip,2) .. "'" elseif skip[0] == "r" - let skip = "strpart(getline('.'),0,col('.'))=~'" . strpart(skip,2). "'" + let skip = "strpart(getline('.'),0,col('.'))=~'" .. strpart(skip,2) .. "'" elseif skip[0] == "R" - let skip = "strpart(getline('.'),0,col('.'))!~'" . strpart(skip,2). "'" + let skip = "strpart(getline('.'),0,col('.'))!~'" .. strpart(skip,2) .. "'" endif endif return skip diff --git a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim index f9978a6b00..49d9389773 100644 --- a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim +++ b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim @@ -2,7 +2,7 @@ " " Author: Bram Moolenaar " Copyright: Vim license applies, see ":help license" -" Last Change: 2021 Dec 16 +" Last Change: 2022 Jan 17 " " WORK IN PROGRESS - Only the basics work " Note: On MS-Windows you need a recent version of gdb. The one included with @@ -153,7 +153,7 @@ func s:StartDebug_internal(dict) if &columns < g:termdebug_wide let s:save_columns = &columns let &columns = g:termdebug_wide - " If we make the Vim window wider, use the whole left halve for the debug + " If we make the Vim window wider, use the whole left half for the debug " windows. let s:allleft = 1 endif @@ -1051,10 +1051,10 @@ func s:GetEvaluationExpression(range, arg) return expr endfunc -" clean up expression that may got in because of range +" clean up expression that may get in because of range " (newlines and surrounding whitespace) " As it can also be specified via ex-command for assignments this function -" may not change the "content" parts (like replacing contained spaces +" may not change the "content" parts (like replacing contained spaces) func s:CleanupExpr(expr) " replace all embedded newlines/tabs/... let expr = substitute(a:expr, '\_s', ' ', 'g') @@ -1084,7 +1084,7 @@ func s:HandleEvaluate(msg) \ ->substitute('.*value="\(.*\)"', '\1', '') \ ->substitute('\\"', '"', 'g') \ ->substitute('\\\\', '\\', 'g') - "\ multi-byte characters arrive in octal form, replace everthing but NULL values + "\ multi-byte characters arrive in octal form, replace everything but NULL values \ ->substitute('\\000', s:NullRepl, 'g') \ ->substitute('\\\o\o\o', {-> eval('"' .. submatch(0) .. '"')}, 'g') "\ Note: GDB docs also mention hex encodings - the translations below work @@ -1344,6 +1344,16 @@ func s:HandleCursor(msg) if lnum =~ '^[0-9]*$' call s:GotoSourcewinOrCreateIt() if expand('%:p') != fnamemodify(fname, ':p') + echomsg 'different fname: "' .. expand('%:p') .. '" vs "' .. fnamemodify(fname, ':p') .. '"' + augroup Termdebug + " Always open a file read-only instead of showing the ATTENTION + " prompt, since we are unlikely to want to edit the file. + " The file may be changed but not saved, warn for that. + au SwapExists * echohl WarningMsg + \ | echo 'Warning: file is being edited elsewhere' + \ | echohl None + \ | let v:swapchoice = 'o' + augroup END if &modified " TODO: find existing window exe 'split ' . fnameescape(fname) @@ -1352,6 +1362,9 @@ func s:HandleCursor(msg) else exe 'edit ' . fnameescape(fname) endif + augroup Termdebug + au! SwapExists + augroup END endif exe lnum normal! zv diff --git a/runtime/scripts.vim b/runtime/scripts.vim index 3790b1c10f..dd47f65ba0 100644 --- a/runtime/scripts.vim +++ b/runtime/scripts.vim @@ -384,7 +384,7 @@ else set ft=scheme " Git output - elseif s:line1 =~# '^\(commit\|tree\|object\) \x\{40\}\>\|^tag \S\+$' + elseif s:line1 =~# '^\(commit\|tree\|object\) \x\{40,\}\>\|^tag \S\+$' set ft=git " Gprof (gnu profiler) @@ -406,6 +406,12 @@ else elseif s:line1 =~# '^#.*by RouterOS.*$' set ft=routeros + " Sed scripts + " #ncomment is allowed but most likely a false positive so require a space + " before any trailing comment text + elseif s:line1 =~# '^#n\%($\|\s\)' + set ft=sed + " CVS diff else let s:lnum = 1 diff --git a/runtime/syntax/basic.vim b/runtime/syntax/basic.vim index ad9450b3b8..7fe411a869 100644 --- a/runtime/syntax/basic.vim +++ b/runtime/syntax/basic.vim @@ -1,14 +1,15 @@ " Vim syntax file -" Language: BASIC +" Language: BASIC (QuickBASIC 4.5) " Maintainer: Doug Kearns <dougkearns@gmail.com> " Previous Maintainer: Allan Kelly <allan@fruitloaf.co.uk> " Contributors: Thilo Six -" Last Change: 2015 Jan 10 +" Last Change: 2021 Aug 08 " First version based on Micro$soft QBASIC circa 1989, as documented in " 'Learn BASIC Now' by Halvorson&Rygmyr. Microsoft Press 1989. -" This syntax file not a complete implementation yet. Send suggestions to the -" maintainer. +" +" Second version attempts to match Microsoft QuickBASIC 4.5 while keeping FreeBASIC +" (-lang qb) and QB64 (excluding extensions) in mind. -- DJK " Prelude {{{1 if exists("b:current_syntax") @@ -18,154 +19,357 @@ endif let s:cpo_save = &cpo set cpo&vim +syn iskeyword @,48-57,.,!,#,%,&,$ +syn case ignore + +" Whitespace Errors {{{1 +if exists("basic_space_errors") + if !exists("basic_no_trail_space_error") + syn match basicSpaceError display excludenl "\s\+$" + endif + if !exists("basic_no_tab_space_error") + syn match basicSpaceError display " \+\t"me=e-1 + endif +endif + +" Comment Errors {{{1 +if !exists("basic_no_comment_errors") + syn match basicCommentError "\<REM\>.*" +endif + +" Not Top Cluster {{{1 +syn cluster basicNotTop contains=@basicLineIdentifier,basicDataString,basicDataSeparator,basicTodo + +" Statements {{{1 + +syn cluster basicStatements contains=basicStatement,basicDataStatement,basicMetaRemStatement,basicPutStatement,basicRemStatement + +let s:statements =<< trim EOL " {{{2 + beep + bload + bsave + call + calls + case + chain + chdir + circle + clear + close + cls + color + com + common + const + declare + def + def\s\+seg + defdbl + defint + deflng + defsng + defstr + dim + do + draw + elseif + end + end\s\+\%(def\|function\|if\|select\|sub\|type\) + environ + erase + error + exit\s\+\%(def\|do\|for\|function\|sub\) + field + files + for + function + get + gosub + goto + if + input + ioctl + key + kill + let + line + line\s\+input + locate + lock + loop + lprint + lset + mkdir + name + next + on + on\s\+error + on\s\+uevent + open + open\s\+com + option + out + paint + palette + palette\s\+using + pcopy + pen + pmap + poke + preset + print + pset + randomize + read + redim + reset + restore + resume + return + rmdir + rset + run + select\s\+case + shared + shell + sleep + sound + static + stop + strig + sub + swap + system + troff + tron + type + uevent + unlock + using + view + view\s\+print + wait + wend + while + width + window + write +EOL +" }}} + +for s in s:statements + exe 'syn match basicStatement "\<' .. s .. '\>" contained' +endfor + +syn match basicStatement "\<\%(then\|else\)\>" nextgroup=@basicStatements skipwhite + +" DATA Statement +syn match basicDataSeparator "," contained +syn region basicDataStatement matchgroup=basicStatement start="\<data\>" matchgroup=basicStatementSeparator end=":\|$" contained contains=basicDataSeparator,basicDataString,basicNumber,basicFloat,basicString + +if !exists("basic_no_data_fold") + syn region basicMultilineData start="^\s*\<data\>.*\n\%(^\s*\<data\>\)\@=" end="^\s*\<data\>.*\n\%(^\s*\<data\>\)\@!" contains=basicDataStatement transparent fold keepend +endif + +" PUT File I/O and Graphics statements - needs special handling for graphics +" action verbs +syn match basicPutAction "\<\%(pset\|preset\|and\|or\|xor\)\>" contained +syn region basicPutStatement matchgroup=basicStatement start="\<put\>" matchgroup=basicStatementSeparator end=":\|$" contained contains=basicKeyword,basicPutAction,basicFilenumber + " Keywords {{{1 -syn keyword basicStatement BEEP beep Beep BLOAD bload Bload BSAVE bsave Bsave -syn keyword basicStatement CALL call Call ABSOLUTE absolute Absolute -syn keyword basicStatement CHAIN chain Chain CHDIR chdir Chdir -syn keyword basicStatement CIRCLE circle Circle CLEAR clear Clear -syn keyword basicStatement CLOSE close Close CLS cls Cls COLOR color Color -syn keyword basicStatement COM com Com COMMON common Common -syn keyword basicStatement CONST const Const DATA data Data -syn keyword basicStatement DECLARE declare Declare DEF def Def -syn keyword basicStatement DEFDBL defdbl Defdbl DEFINT defint Defint -syn keyword basicStatement DEFLNG deflng Deflng DEFSNG defsng Defsng -syn keyword basicStatement DEFSTR defstr Defstr DIM dim Dim -syn keyword basicStatement DO do Do LOOP loop Loop -syn keyword basicStatement DRAW draw Draw END end End -syn keyword basicStatement ENVIRON environ Environ ERASE erase Erase -syn keyword basicStatement ERROR error Error EXIT exit Exit -syn keyword basicStatement FIELD field Field FILES files Files -syn keyword basicStatement FOR for For NEXT next Next -syn keyword basicStatement FUNCTION function Function GET get Get -syn keyword basicStatement GOSUB gosub Gosub GOTO goto Goto -syn keyword basicStatement IF if If THEN then Then ELSE else Else -syn keyword basicStatement INPUT input Input INPUT# input# Input# -syn keyword basicStatement IOCTL ioctl Ioctl KEY key Key -syn keyword basicStatement KILL kill Kill LET let Let -syn keyword basicStatement LINE line Line LOCATE locate Locate -syn keyword basicStatement LOCK lock Lock UNLOCK unlock Unlock -syn keyword basicStatement LPRINT lprint Lprint USING using Using -syn keyword basicStatement LSET lset Lset MKDIR mkdir Mkdir -syn keyword basicStatement NAME name Name ON on On -syn keyword basicStatement ERROR error Error OPEN open Open -syn keyword basicStatement OPTION option Option BASE base Base -syn keyword basicStatement OUT out Out PAINT paint Paint -syn keyword basicStatement PALETTE palette Palette PCOPY pcopy Pcopy -syn keyword basicStatement PEN pen Pen PLAY play Play -syn keyword basicStatement PMAP pmap Pmap POKE poke Poke -syn keyword basicStatement PRESET preset Preset PRINT print Print -syn keyword basicStatement PRINT# print# Print# USING using Using -syn keyword basicStatement PSET pset Pset PUT put Put -syn keyword basicStatement RANDOMIZE randomize Randomize READ read Read -syn keyword basicStatement REDIM redim Redim RESET reset Reset -syn keyword basicStatement RESTORE restore Restore RESUME resume Resume -syn keyword basicStatement RETURN return Return RMDIR rmdir Rmdir -syn keyword basicStatement RSET rset Rset RUN run Run -syn keyword basicStatement SEEK seek Seek SELECT select Select -syn keyword basicStatement CASE case Case SHARED shared Shared -syn keyword basicStatement SHELL shell Shell SLEEP sleep Sleep -syn keyword basicStatement SOUND sound Sound STATIC static Static -syn keyword basicStatement STOP stop Stop STRIG strig Strig -syn keyword basicStatement SUB sub Sub SWAP swap Swap -syn keyword basicStatement SYSTEM system System TIMER timer Timer -syn keyword basicStatement TROFF troff Troff TRON tron Tron -syn keyword basicStatement TYPE type Type UNLOCK unlock Unlock -syn keyword basicStatement VIEW view View WAIT wait Wait -syn keyword basicStatement WHILE while While WEND wend Wend -syn keyword basicStatement WIDTH width Width WINDOW window Window -syn keyword basicStatement WRITE write Write DATE$ date$ Date$ -syn keyword basicStatement MID$ mid$ Mid$ TIME$ time$ Time$ - -syn keyword basicFunction ABS abs Abs ASC asc Asc -syn keyword basicFunction ATN atn Atn CDBL cdbl Cdbl -syn keyword basicFunction CINT cint Cint CLNG clng Clng -syn keyword basicFunction COS cos Cos CSNG csng Csng -syn keyword basicFunction CSRLIN csrlin Csrlin CVD cvd Cvd -syn keyword basicFunction CVDMBF cvdmbf Cvdmbf CVI cvi Cvi -syn keyword basicFunction CVL cvl Cvl CVS cvs Cvs -syn keyword basicFunction CVSMBF cvsmbf Cvsmbf EOF eof Eof -syn keyword basicFunction ERDEV erdev Erdev ERL erl Erl -syn keyword basicFunction ERR err Err EXP exp Exp -syn keyword basicFunction FILEATTR fileattr Fileattr FIX fix Fix -syn keyword basicFunction FRE fre Fre FREEFILE freefile Freefile -syn keyword basicFunction INP inp Inp INSTR instr Instr -syn keyword basicFunction INT int Int LBOUND lbound Lbound -syn keyword basicFunction LEN len Len LOC loc Loc -syn keyword basicFunction LOF lof Lof LOG log Log -syn keyword basicFunction LPOS lpos Lpos PEEK peek Peek -syn keyword basicFunction PEN pen Pen POINT point Point -syn keyword basicFunction POS pos Pos RND rnd Rnd -syn keyword basicFunction SADD sadd Sadd SCREEN screen Screen -syn keyword basicFunction SEEK seek Seek SETMEM setmem Setmem -syn keyword basicFunction SGN sgn Sgn SIN sin Sin -syn keyword basicFunction SPC spc Spc SQR sqr Sqr -syn keyword basicFunction STICK stick Stick STRIG strig Strig -syn keyword basicFunction TAB tab Tab TAN tan Tan -syn keyword basicFunction UBOUND ubound Ubound VAL val Val -syn keyword basicFunction VALPTR valptr Valptr VALSEG valseg Valseg -syn keyword basicFunction VARPTR varptr Varptr VARSEG varseg Varseg -syn keyword basicFunction CHR$ Chr$ chr$ COMMAND$ command$ Command$ -syn keyword basicFunction DATE$ date$ Date$ ENVIRON$ environ$ Environ$ -syn keyword basicFunction ERDEV$ erdev$ Erdev$ HEX$ hex$ Hex$ -syn keyword basicFunction INKEY$ inkey$ Inkey$ INPUT$ input$ Input$ -syn keyword basicFunction IOCTL$ ioctl$ Ioctl$ LCASES$ lcases$ Lcases$ -syn keyword basicFunction LAFT$ laft$ Laft$ LTRIM$ ltrim$ Ltrim$ -syn keyword basicFunction MID$ mid$ Mid$ MKDMBF$ mkdmbf$ Mkdmbf$ -syn keyword basicFunction MKD$ mkd$ Mkd$ MKI$ mki$ Mki$ -syn keyword basicFunction MKL$ mkl$ Mkl$ MKSMBF$ mksmbf$ Mksmbf$ -syn keyword basicFunction MKS$ mks$ Mks$ OCT$ oct$ Oct$ -syn keyword basicFunction RIGHT$ right$ Right$ RTRIM$ rtrim$ Rtrim$ -syn keyword basicFunction SPACE$ space$ Space$ STR$ str$ Str$ -syn keyword basicFunction STRING$ string$ String$ TIME$ time$ Time$ -syn keyword basicFunction UCASE$ ucase$ Ucase$ VARPTR$ varptr$ Varptr$ +let s:keywords =<< trim EOL " {{{2 + absolute + access + alias + append + as + base + binary + byval + cdecl + com + def + do + for + function + gosub + goto + input + int86old + int86xold + interrupt + interruptx + is + key + len + list + local + lock + lprint + next + off + on + output + pen + play + random + read + resume + screen + seg + shared + signal + static + step + stop + strig + sub + timer + to + until + using + while + write +EOL +" }}} + +for k in s:keywords + exe 'syn match basicKeyword "\<' .. k .. '\>"' +endfor + +" Functions {{{1 +syn keyword basicFunction abs asc atn cdbl chr$ cint clng command$ cos csng +syn keyword basicFunction csrlin cvd cvdmbf cvi cvl cvs cvsmbf environ$ eof +syn keyword basicFunction erdev erdev$ erl err exp fileattr fix fre freefile +syn keyword basicFunction hex$ inkey$ inp input$ instr int ioctl$ left$ lbound +syn keyword basicFunction lcase$ len loc lof log lpos ltrim$ mkd$ mkdmbf$ mki$ +syn keyword basicFunction mkl$ mks$ mksmbf$ oct$ peek pen point pos right$ rnd +syn keyword basicFunction rtrim$ sadd setmem sgn sin space$ spc sqr stick str$ +syn keyword basicFunction strig string$ tab tan ubound ucase$ val valptr +syn keyword basicFunction valseg varptr varptr$ varseg + +" Functions and statements (same name) {{{1 +syn match basicStatement "\<\%(date\$\|mid\$\|play\|screen\|seek\|time\$\|timer\)\>" contained +syn match basicFunction "\<\%(date\$\|mid\$\|play\|screen\|seek\|time\$\|timer\)\>" + +" Types {{{1 +syn keyword basicType integer long single double string any + +" Strings {{{1 + +" Unquoted DATA strings - anything except [:,] and leading or trailing whitespace +" Needs lower priority than numbers +syn match basicDataString "[^[:space:],:]\+\%(\s\+[^[:space:],:]\+\)*" contained + +syn region basicString start=+"+ end=+"+ oneline + +" Booleans {{{1 +if exists("basic_booleans") + syn keyword basicBoolean true false +endif " Numbers {{{1 -" Integer number, or floating point number without a dot. -syn match basicNumber "\<\d\+\>" -" Floating point number, with dot -syn match basicNumber "\<\d\+\.\d*\>" -" Floating point number, starting with a dot -syn match basicNumber "\.\d\+\>" -" String and Character constants {{{1 -syn match basicSpecial "\\\d\d\d\|\\." contained -syn region basicString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=basicSpecial +" Integers +syn match basicNumber "-\=&o\=\o\+[%&]\=\>" +syn match basicNumber "-\=&h\x\+[%&]\=\>" +syn match basicNumber "-\=\<\d\+[%&]\=\>" + +" Floats +syn match basicFloat "-\=\<\d\+\.\=\d*\%(\%([ed][+-]\=\d*\)\|[!#]\)\=\>" +syn match basicFloat "-\=\<\.\d\+\%(\%([ed][+-]\=\d*\)\|[!#]\)\=\>" -" Line numbers {{{1 -syn region basicLineNumber start="^\d" end="\s" +" Statement anchors {{{1 +syn match basicLineStart "^" nextgroup=@basicStatements,@basicLineIdentifier skipwhite +syn match basicStatementSeparator ":" nextgroup=@basicStatements skipwhite -" Data-type suffixes {{{1 -syn match basicTypeSpecifier "[a-zA-Z0-9][$%&!#]"ms=s+1 -" Used with OPEN statement -syn match basicFilenumber "#\d\+" +" Line numbers and labels {{{1 + +" QuickBASIC limits these to 65,529 and 40 chars respectively +syn match basicLineNumber "\d\+" nextgroup=@basicStatements skipwhite contained +syn match basicLineLabel "\a[[:alnum:]]*\ze\s*:" nextgroup=@basicStatements skipwhite contained + +syn cluster basicLineIdentifier contains=basicLineNumber,basicLineLabel + +" Line Continuation {{{1 +syn match basicLineContinuation "\s*\zs_\ze\s*$" + +" Type suffixes {{{1 +if exists("basic_type_suffixes") + syn match basicTypeSuffix "\a[[:alnum:].]*\zs[$%&!#]" +endif -" Mathematical operators {{{1 -" syn match basicMathsOperator "[<>+\*^/\\=-]" -syn match basicMathsOperator "-\|=\|[:<>+\*^/\\]\|AND\|OR" +" File numbers {{{1 +syn match basicFilenumber "#\d\+" +syn match basicFilenumber "#\a[[:alnum:].]*[%&!#]\=" + +" Operators {{{1 +if exists("basic_operators") + syn match basicArithmeticOperator "[-+*/\\^]" + syn match basicRelationalOperator "<>\|<=\|>=\|[><=]" +endif +syn match basicLogicalOperator "\<\%(not\|and\|or\|xor\|eqv\|imp\)\>" +syn match basicArithmeticOperator "\<mod\>" + +" Metacommands {{{1 +" Note: No trailing word boundaries. Text may be freely mixed however there +" must be only leading whitespace prior to the first metacommand +syn match basicMetacommand "$INCLUDE\s*:\s*'[^']\+'" contained containedin=@basicMetaComments +syn match basicMetacommand "$\%(DYNAMIC\|STATIC\)" contained containedin=@basicMetaComments " Comments {{{1 -syn keyword basicTodo TODO FIXME XXX NOTE contained -syn region basicComment start="^\s*\zsREM\>" start="\%(:\s*\)\@<=REM\>" end="$" contains=basicTodo -syn region basicComment start="'" end="$" contains=basicTodo +syn keyword basicTodo TODO FIXME XXX NOTE contained + +syn region basicRemStatement matchgroup=basicStatement start="REM\>" end="$" contains=basicTodo,@Spell contained +syn region basicComment start="'" end="$" contains=basicTodo,@Spell + +if !exists("basic_no_comment_fold") + syn region basicMultilineComment start="^\s*'.*\n\%(\s*'\)\@=" end="^\s*'.*\n\%(\s*'\)\@!" contains=@basicComments transparent fold keepend +endif + +" Metacommands +syn region basicMetaRemStatement matchgroup=basicStatement start="REM\>\s*\$\@=" end="$" contains=basicTodo contained +syn region basicMetaComment start="'\s*\$\@=" end="$" contains=basicTodo + +syn cluster basicMetaComments contains=basicMetaComment,basicMetaRemStatement +syn cluster basicComments contains=basicComment,basicMetaComment "syn sync ccomment basicComment " Default Highlighting {{{1 -hi def link basicLabel Label -hi def link basicConditional Conditional -hi def link basicRepeat Repeat -hi def link basicLineNumber Comment -hi def link basicNumber Number -hi def link basicError Error -hi def link basicStatement Statement -hi def link basicString String -hi def link basicComment Comment -hi def link basicSpecial Special -hi def link basicTodo Todo -hi def link basicFunction Identifier -hi def link basicTypeSpecifier Type -hi def link basicFilenumber basicTypeSpecifier -"hi basicMathsOperator term=bold cterm=bold gui=bold +hi def link basicArithmeticOperator basicOperator +hi def link basicBoolean Boolean +hi def link basicComment Comment +hi def link basicCommentError Error +hi def link basicDataString basicString +hi def link basicFilenumber basicTypeSuffix " TODO: better group +hi def link basicFloat Float +hi def link basicFunction Identifier +hi def link basicKeyword Keyword +hi def link basicLineIdentifier LineNr +hi def link basicLineContinuation Special +hi def link basicLineLabel basicLineIdentifier +hi def link basicLineNumber basicLineIdentifier +hi def link basicLogicalOperator basicOperator +hi def link basicMetacommand SpecialComment +hi def link basicMetaComment Comment +hi def link basicMetaRemStatement Comment +hi def link basicNumber Number +hi def link basicOperator Operator +hi def link basicPutAction Keyword +hi def link basicRelationalOperator basicOperator +hi def link basicRemStatement Comment +hi def link basicSpaceError Error +hi def link basicStatementSeparator Special +hi def link basicStatement Statement +hi def link basicString String +hi def link basicTodo Todo +hi def link basicType Type +hi def link basicTypeSuffix Special +if exists("basic_legacy_syntax_groups") + hi def link basicTypeSpecifier Type + hi def link basicTypeSuffix basicTypeSpecifier +endif " Postscript {{{1 let b:current_syntax = "basic" diff --git a/runtime/syntax/checkhealth.vim b/runtime/syntax/checkhealth.vim index dff880a0bc..37f1822740 100644 --- a/runtime/syntax/checkhealth.vim +++ b/runtime/syntax/checkhealth.vim @@ -12,7 +12,9 @@ unlet! b:current_syntax syn case match " We do not care about markdown syntax errors -syn clear markdownError +if hlexists('markdownError') + syn clear markdownError +endif syn keyword healthError ERROR[:] containedin=markdownCodeBlock,mkdListItemLine syn keyword healthWarning WARNING[:] containedin=markdownCodeBlock,mkdListItemLine diff --git a/runtime/syntax/git.vim b/runtime/syntax/git.vim index a8467edd43..bf013ce195 100644 --- a/runtime/syntax/git.vim +++ b/runtime/syntax/git.vim @@ -1,7 +1,7 @@ " Vim syntax file " Language: generic git output " Maintainer: Tim Pope <vimNOSPAM@tpope.org> -" Last Change: 2019 Dec 05 +" Last Change: 2022 Jan 05 if exists("b:current_syntax") finish @@ -12,12 +12,28 @@ syn sync minlines=50 syn include @gitDiff syntax/diff.vim -syn region gitHead start=/\%^/ end=/^$/ -syn region gitHead start=/\%(^commit\%( \x\{40\}\)\{1,\}\%(\s*(.*)\)\=$\)\@=/ end=/^$/ - -" For git reflog and git show ...^{tree}, avoid sync issues -syn match gitHead /^\d\{6\} \%(\w\{4} \)\=\x\{40\}\%( [0-3]\)\=\t.*/ -syn match gitHead /^\x\{40\} \x\{40}\t.*/ +syn region gitHead start=/\%^\%(tag \|tree \|object \)\@=/ end=/^$/ contains=@NoSpell +syn region gitHead start=/\%(^commit\%( \x\{4,\}\)\{1,\}\%(\s*(.*)\)\=$\)\@=/ end=/^$/ contains=@NoSpell +" git log --oneline +" minimize false positives by verifying contents of buffer +if getline(1) =~# '^\x\{7,\} ' && getline('$') =~# '^\x\{7,\} ' + syn match gitHashAbbrev /^\x\{7,\} \@=/ contains=@NoSpell +elseif getline(1) =~# '^[|\/\\_ ]\{-\}\*[|\/\\_ ]\{-\} \x\{7,\} ' + syn match gitHashAbbrev /^[|\/\\_ ]\{-\}\*[|\/\\_ ]\{-\} \zs\x\{7,\} \@=/ contains=@NoSpell +endif +" git log --graph +syn region gitGraph start=/\%(^[|\/\\_ ]*\*[|\/\\_ ]\{-\} commit\%( \x\{4,\}\)\{1,\}\%(\s*(.*)\)\=$\)\@=/ end=/^\%([|\/\\_ ]*$\)\@=/ contains=@NoSpell +" git blame --porcelain +syn region gitHead start=/\%(^\x\{40,\} \d\+ \d\+\%( \d\+\)\=$\)\@=/ end=/^\t\@=/ contains=@NoSpell +" git ls-tree +syn match gitMode /^\d\{6\}\%( \%(blob\|tree\) \x\{4,\}\t\)\@=/ nextgroup=gitType skipwhite contains=@NoSpell +" git ls-files --stage +syn match gitMode /^\d\{6\}\%( \x\{4,\} [0-3]\t\)\@=/ nextgroup=gitHashStage skipwhite contains=@NoSpell +" .git/HEAD, .git/refs/ +syn match gitKeyword /\%^ref: \@=/ nextgroup=gitReference skipwhite contains=@NoSpell +syn match gitHash /\%^\x\{40,}\%$/ skipwhite contains=@NoSpell +" .git/logs/ +syn match gitReflog /^\x\{40,\} \x\{40,\} .\{-\}\d\+\s-\d\{4\}\t.*/ skipwhite contains=@NoSpell,gitReflogOld syn region gitDiff start=/^\%(diff --git \)\@=/ end=/^\%(diff --\|$\)\@=/ contains=@gitDiff fold syn region gitDiff start=/^\%(@@ -\)\@=/ end=/^\%(diff --\%(git\|cc\|combined\) \|$\)\@=/ contains=@gitDiff @@ -25,35 +41,47 @@ syn region gitDiff start=/^\%(@@ -\)\@=/ end=/^\%(diff --\%(git\|cc\|combined\) syn region gitDiffMerge start=/^\%(diff --\%(cc\|combined\) \)\@=/ end=/^\%(diff --\|$\)\@=/ contains=@gitDiff syn region gitDiffMerge start=/^\%(@@@@* -\)\@=/ end=/^\%(diff --\|$\)\@=/ contains=@gitDiff syn match gitDiffAdded "^ \++.*" contained containedin=gitDiffMerge -syn match gitDiffAdded "{+.*+}" contained containedin=gitDiff +syn match gitDiffAdded "{+[^}]*+}" contained containedin=gitDiff syn match gitDiffRemoved "^ \+-.*" contained containedin=gitDiffMerge -syn match gitDiffRemoved "\[-.*-\]" contained containedin=gitDiff +syn match gitDiffRemoved "\[-[^]]*-\]" contained containedin=gitDiff + +syn match gitKeyword /^commit \@=/ contained containedin=gitHead nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitKeyword /^\%(object\|tree\|parent\|encoding\|gpgsig\%(-\w\+\)\=\|previous\) \@=/ contained containedin=gitHead nextgroup=gitHash skipwhite contains=@NoSpell +syn match gitKeyword /^Merge:/ contained containedin=gitHead nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitIdentityKeyword /^\%(author\|committer\|tagger\) \@=/ contained containedin=gitHead nextgroup=gitIdentity skipwhite contains=@NoSpell +syn match gitIdentityHeader /^\%(Author\|Commit\|Tagger\):/ contained containedin=gitHead nextgroup=gitIdentity skipwhite contains=@NoSpell +syn match gitDateHeader /^\%(AuthorDate\|CommitDate\|Date\):/ contained containedin=gitHead nextgroup=gitDate skipwhite contains=@NoSpell -syn match gitKeyword /^\%(object\|type\|tag\|commit\|tree\|parent\|encoding\)\>/ contained containedin=gitHead nextgroup=gitHash,gitType skipwhite -syn match gitKeyword /^\%(tag\>\|ref:\)/ contained containedin=gitHead nextgroup=gitReference skipwhite -syn match gitKeyword /^Merge:/ contained containedin=gitHead nextgroup=gitHashAbbrev skipwhite -syn match gitMode /^\d\{6\}\>/ contained containedin=gitHead nextgroup=gitType,gitHash skipwhite -syn match gitIdentityKeyword /^\%(author\|committer\|tagger\)\>/ contained containedin=gitHead nextgroup=gitIdentity skipwhite -syn match gitIdentityHeader /^\%(Author\|Commit\|Tagger\):/ contained containedin=gitHead nextgroup=gitIdentity skipwhite -syn match gitDateHeader /^\%(AuthorDate\|CommitDate\|Date\):/ contained containedin=gitHead nextgroup=gitDate skipwhite +syn match gitKeyword /^[*|\/\\_ ]\+\zscommit \@=/ contained containedin=gitGraph nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitKeyword /^[|\/\\_ ]\+\zs\%(object\|tree\|parent\|encoding\|gpgsig\%(-\w\+\)\=\|previous\) \@=/ contained containedin=gitGraph nextgroup=gitHash skipwhite contains=@NoSpell +syn match gitKeyword /^[|\/\\_ ]\+\zsMerge:/ contained containedin=gitGraph nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitIdentityKeyword /^[|\/\\_ ]\+\zs\%(author\|committer\|tagger\) \@=/ contained containedin=gitGraph nextgroup=gitIdentity skipwhite contains=@NoSpell +syn match gitIdentityHeader /^[|\/\\_ ]\+\zs\%(Author\|Commit\|Tagger\):/ contained containedin=gitGraph nextgroup=gitIdentity skipwhite contains=@NoSpell +syn match gitDateHeader /^[|\/\\_ ]\+\zs\%(AuthorDate\|CommitDate\|Date\):/ contained containedin=gitGraph nextgroup=gitDate skipwhite contains=@NoSpell -syn match gitReflogHeader /^Reflog:/ contained containedin=gitHead nextgroup=gitReflogMiddle skipwhite -syn match gitReflogHeader /^Reflog message:/ contained containedin=gitHead skipwhite -syn match gitReflogMiddle /\S\+@{\d\+} (/he=e-2 nextgroup=gitIdentity +syn match gitKeyword /^type \@=/ contained containedin=gitHead nextgroup=gitType skipwhite contains=@NoSpell +syn match gitKeyword /^\%(summary\|boundary\|filename\|\%(author\|committer\)-\%(time\|tz\)\) \@=/ contained containedin=gitHead skipwhite contains=@NoSpell +syn match gitKeyword /^tag \@=/ contained containedin=gitHead nextgroup=gitReference skipwhite contains=@NoSpell +syn match gitIdentityKeyword /^\%(author\|committer\)-mail \@=/ contained containedin=gitHead nextgroup=gitEmail skipwhite contains=@NoSpell +syn match gitReflogHeader /^Reflog:/ contained containedin=gitHead nextgroup=gitReflogMiddle skipwhite contains=@NoSpell +syn match gitReflogHeader /^Reflog message:/ contained containedin=gitHead skipwhite contains=@NoSpell +syn match gitReflogMiddle /\S\+@{\d\+} (/he=e-2 nextgroup=gitIdentity contains=@NoSpell -syn match gitDate /\<\u\l\l \u\l\l \d\=\d \d\d:\d\d:\d\d \d\d\d\d [+-]\d\d\d\d/ contained -syn match gitDate /-\=\d\+ [+-]\d\d\d\d\>/ contained -syn match gitDate /\<\d\+ \l\+ ago\>/ contained -syn match gitType /\<\%(tag\|commit\|tree\|blob\)\>/ contained nextgroup=gitHash skipwhite -syn match gitStage /\<\d\t\@=/ contained -syn match gitReference /\S\+\S\@!/ contained -syn match gitHash /\<\x\{40\}\>/ contained nextgroup=gitIdentity,gitStage,gitHash skipwhite -syn match gitHash /^\<\x\{40\}\>/ containedin=gitHead contained nextgroup=gitHash skipwhite -syn match gitHashAbbrev /\<\x\{4,40\}\>/ contained nextgroup=gitHashAbbrev skipwhite -syn match gitHashAbbrev /\<\x\{4,39\}\.\.\./he=e-3 contained nextgroup=gitHashAbbrev skipwhite +syn match gitIdentity /\S.\{-\} <[^>]*>/ contained nextgroup=gitDate skipwhite contains=@NoSpell +syn region gitEmail matchgroup=gitEmailDelimiter start=/</ end=/>/ keepend oneline contained containedin=gitIdentity contains=@NoSpell +syn match gitDate /\<\u\l\l \u\l\l \d\=\d \d\d:\d\d:\d\d \d\d\d\d [+-]\d\d\d\d/ contained contains=@NoSpell +syn match gitDate /-\=\d\+ [+-]\d\d\d\d\>/ contained contains=@NoSpell +syn match gitDate /\<\d\+ \l\+ ago\>/ contained contains=@NoSpell +syn match gitType /\<\%(tag\|commit\|tree\|blob\)\>/ contained nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitReference /\S\+\S\@!/ contained contains=@NoSpell +syn match gitHash /\<\x\{40,\}\>/ contained nextgroup=gitIdentity,gitHash skipwhite contains=@NoSpell +syn match gitReflogOld /^\x\{40,\} \@=/ contained nextgroup=gitReflogNew skipwhite contains=@NoSpell +syn match gitReflogNew /\<\x\{40,\} \@=/ contained nextgroup=gitIdentity skipwhite contains=@NoSpell +syn match gitHashAbbrev /\<\x\{4,\}\>/ contained nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitHashAbbrev /\<\x\{4,39\}\.\.\./he=e-3 contained nextgroup=gitHashAbbrev skipwhite contains=@NoSpell +syn match gitHashStage /\<\x\{4,\}\>/ contained nextgroup=gitStage skipwhite contains=@NoSpell +syn match gitStage /\<\d\t\@=/ contained contains=@NoSpell -syn match gitIdentity /\S.\{-\} <[^>]*>/ contained nextgroup=gitDate skipwhite -syn region gitEmail matchgroup=gitEmailDelimiter start=/</ end=/>/ keepend oneline contained containedin=gitIdentity syn match gitNotesHeader /^Notes:\ze\n / @@ -68,7 +96,10 @@ hi def link gitEmailDelimiter Delimiter hi def link gitEmail Special hi def link gitDate Number hi def link gitMode Number +hi def link gitHashStage gitHash hi def link gitHashAbbrev gitHash +hi def link gitReflogOld gitHash +hi def link gitReflogNew gitHash hi def link gitHash Identifier hi def link gitReflogMiddle gitReference hi def link gitReference Function diff --git a/runtime/syntax/gitcommit.vim b/runtime/syntax/gitcommit.vim index 63b1ce920f..42c8d4414f 100644 --- a/runtime/syntax/gitcommit.vim +++ b/runtime/syntax/gitcommit.vim @@ -2,71 +2,87 @@ " Language: git commit file " Maintainer: Tim Pope <vimNOSPAM@tpope.org> " Filenames: *.git/COMMIT_EDITMSG -" Last Change: 2019 Dec 05 +" Last Change: 2022 Jan 05 if exists("b:current_syntax") finish endif +scriptencoding utf-8 + syn case match syn sync minlines=50 +syn sync linebreaks=1 if has("spell") syn spell toplevel endif syn include @gitcommitDiff syntax/diff.vim -syn region gitcommitDiff start=/\%(^diff --\%(git\|cc\|combined\) \)\@=/ end=/^\%(diff --\|$\|#\)\@=/ fold contains=@gitcommitDiff +syn region gitcommitDiff start=/\%(^diff --\%(git\|cc\|combined\) \)\@=/ end=/^\%(diff --\|$\|@@\@!\|[^[:alnum:]\ +-]\S\@!\)\@=/ fold contains=@gitcommitDiff syn match gitcommitSummary "^.*\%<51v." contained containedin=gitcommitFirstLine nextgroup=gitcommitOverflow contains=@Spell syn match gitcommitOverflow ".*" contained contains=@Spell -syn match gitcommitBlank "^[^#].*" contained contains=@Spell +syn match gitcommitBlank "^.\+" contained contains=@Spell +syn match gitcommitFirstLine "\%^.*" nextgroup=gitcommitBlank,gitcommitComment skipnl + +let s:scissors = 0 +let s:l = search('^[#;@!$%^&|:] -\{24,\} >8 -\{24,\}$', 'cnW', '', 100) +if s:l == 0 + let s:l = line('$') +elseif getline(s:l)[0] !=# getline(s:l - 1)[0] + let s:scissors = 1 +endif +let s:comment = escape((matchstr(getline(s:l), '^[#;@!$%^&|:]\S\@!') . '#')[0], '^$.*[]~\"/') -if get(g:, "gitcommit_cleanup") is# "scissors" - syn match gitcommitFirstLine "\%^.*" nextgroup=gitcommitBlank skipnl - syn region gitcommitComment start=/^# -\+ >8 -\+$/ end=/\%$/ contains=gitcommitDiff +if s:scissors + let s:comment .= ' -\{24,\} >8 -\{24,\}$' + exe 'syn region gitcommitComment start="^' . s:comment . '" end="\%$" contains=gitcommitDiff' else - syn match gitcommitFirstLine "\%^[^#].*" nextgroup=gitcommitBlank skipnl - syn match gitcommitComment "^#.*" + exe 'syn match gitcommitComment "^' . s:comment . '.*"' endif +exe 'syn match gitcommitTrailers "\n\@<=\n\%([[:alnum:]-]\+\s*:.*\|(cherry picked from commit .*\)\%(\n\s.*\|\n[[:alnum:]-]\+\s*:.*\|\n(cherry picked from commit .*\)*\%(\n\n*\%(' . s:comment . '\)\|\n*\%$\)\@="' -syn match gitcommitHead "^\%(# .*\n\)\+#$" contained transparent -syn match gitcommitOnBranch "\%(^# \)\@<=On branch" contained containedin=gitcommitComment nextgroup=gitcommitBranch skipwhite -syn match gitcommitOnBranch "\%(^# \)\@<=Your branch .\{-\} '" contained containedin=gitcommitComment nextgroup=gitcommitBranch skipwhite +unlet s:l s:comment s:scissors + +syn match gitcommitTrailerToken "^[[:alnum:]-]\+\s*:" contained containedin=gitcommitTrailers + +syn match gitcommitHash "\<\x\{40,}\>" contains=@NoSpell display +syn match gitcommitOnBranch "\%(^. \)\@<=On branch" contained containedin=gitcommitComment nextgroup=gitcommitBranch skipwhite +syn match gitcommitOnBranch "\%(^. \)\@<=Your branch .\{-\} '" contained containedin=gitcommitComment nextgroup=gitcommitBranch skipwhite syn match gitcommitBranch "[^ ']\+" contained -syn match gitcommitNoBranch "\%(^# \)\@<=Not currently on any branch." contained containedin=gitcommitComment -syn match gitcommitHeader "\%(^# \)\@<=.*:$" contained containedin=gitcommitComment -syn region gitcommitAuthor matchgroup=gitCommitHeader start=/\%(^# \)\@<=\%(Author\|Committer\):/ end=/$/ keepend oneline contained containedin=gitcommitComment transparent -syn match gitcommitNoChanges "\%(^# \)\@<=No changes$" contained containedin=gitcommitComment +syn match gitcommitNoBranch "\%(^. \)\@<=Not currently on any branch." contained containedin=gitcommitComment +syn match gitcommitHeader "\%(^. \)\@<=\S.*[::]\%(\n^$\)\@!$" contained containedin=gitcommitComment +syn region gitcommitAuthor matchgroup=gitCommitHeader start=/\%(^. \)\@<=\%(Author\|Committer\|Date\):/ end=/$/ keepend oneline contained containedin=gitcommitComment transparent +syn match gitcommitHeader "\%(^. \)\@<=commit\%( \x\{40,\}$\)\@=" contained containedin=gitcommitComment nextgroup=gitcommitHash skipwhite +syn match gitcommitNoChanges "\%(^. \)\@<=No changes$" contained containedin=gitcommitComment -syn region gitcommitUntracked start=/^# Untracked files:/ end=/^#$\|^#\@!/ contains=gitcommitHeader,gitcommitHead,gitcommitUntrackedFile fold -syn match gitcommitUntrackedFile "\t\@<=.*" contained +syn match gitcommitType "\%(^.\t\)\@<=[^[:punct:][:space:]][^/::]*[^[:punct:][:space:]][::]\ze "he=e-1 contained containedin=gitcommitComment nextgroup=gitcommitFile skipwhite +syn match gitcommitFile ".\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitArrow +syn match gitcommitArrow " -> " contained nextgroup=gitcommitFile +syn match gitcommitUntrackedFile "\%(^.\t\)\@<=[^::/]*\%(/.*\)\=$" contained containedin=gitcommitComment -syn region gitcommitDiscarded start=/^# Change\%(s not staged for commit\|d but not updated\):/ end=/^#$\|^#\@!/ contains=gitcommitHeader,gitcommitHead,gitcommitDiscardedType fold -syn region gitcommitSelected start=/^# Changes to be committed:/ end=/^#$\|^#\@!/ contains=gitcommitHeader,gitcommitHead,gitcommitSelectedType fold -syn region gitcommitUnmerged start=/^# Unmerged paths:/ end=/^#$\|^#\@!/ contains=gitcommitHeader,gitcommitHead,gitcommitUnmergedType fold +syn region gitcommitUntracked start=/^\z(.\) Untracked files:$/ end=/^\z1\=$\|^\z1\@!/ contains=gitcommitHeader containedin=gitcommitComment containedin=gitcommitComment contained transparent fold +syn region gitcommitDiscarded start=/^\z(.\) Change\%(s not staged for commit\|d but not updated\):$/ end=/^\z1\=$\|^\z1\@!/ contains=gitcommitHeader,gitcommitDiscardedType containedin=gitcommitComment containedin=gitcommitComment contained transparent fold +syn region gitcommitSelected start=/^\z(.\) Changes to be committed:$/ end=/^\z1$\|^\z1\@!/ contains=gitcommitHeader,gitcommitSelectedType containedin=gitcommitComment containedin=gitcommitComment contained transparent fold +syn region gitcommitUnmerged start=/^\z(.\) Unmerged paths:$/ end=/^\z1\=$\|^\z1\@!/ contains=gitcommitHeader,gitcommitUnmergedType containedin=gitcommitComment containedin=gitcommitComment contained transparent fold +syn match gitcommitUntrackedFile "\%(^.\t\)\@<=.*" contained containedin=gitcommitUntracked -syn match gitcommitDiscardedType "\t\@<=[[:lower:]][^:]*[[:lower:]]: "he=e-2 contained containedin=gitcommitComment nextgroup=gitcommitDiscardedFile skipwhite -syn match gitcommitSelectedType "\t\@<=[[:lower:]][^:]*[[:lower:]]: "he=e-2 contained containedin=gitcommitComment nextgroup=gitcommitSelectedFile skipwhite -syn match gitcommitUnmergedType "\t\@<=[[:lower:]][^:]*[[:lower:]]: "he=e-2 contained containedin=gitcommitComment nextgroup=gitcommitUnmergedFile skipwhite -syn match gitcommitDiscardedFile ".\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitDiscardedArrow -syn match gitcommitSelectedFile ".\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitSelectedArrow -syn match gitcommitUnmergedFile ".\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitSelectedArrow +syn match gitcommitDiscardedType "\%(^.\t\)\@<=[^[:punct:][:space:]][^/::]*[^[:punct:][:space:]][::]\ze "he=e-1 contained nextgroup=gitcommitDiscardedFile skipwhite +syn match gitcommitSelectedType "\%(^.\t\)\@<=[^[:punct:][:space:]][^/::]*[^[:punct:][:space:]][::]\ze "he=e-1 contained nextgroup=gitcommitSelectedFile skipwhite +syn match gitcommitUnmergedType "\%(^.\t\)\@<=[^[:punct:][:space:]][^/::]*[^[:punct:][:space:]][::]\ze "he=e-1 contained nextgroup=gitcommitUnmergedFile skipwhite +syn match gitcommitDiscardedFile "\S.\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitDiscardedArrow +syn match gitcommitSelectedFile "\S.\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitSelectedArrow +syn match gitcommitUnmergedFile "\S.\{-\}\%($\| -> \)\@=" contained nextgroup=gitcommitUnmergedArrow syn match gitcommitDiscardedArrow " -> " contained nextgroup=gitcommitDiscardedFile syn match gitcommitSelectedArrow " -> " contained nextgroup=gitcommitSelectedFile -syn match gitcommitUnmergedArrow " -> " contained nextgroup=gitcommitSelectedFile - -syn match gitcommitWarning "\%^[^#].*: needs merge$" nextgroup=gitcommitWarning skipnl -syn match gitcommitWarning "^[^#].*: needs merge$" nextgroup=gitcommitWarning skipnl contained -syn match gitcommitWarning "^\%(no changes added to commit\|nothing \%(added \)\=to commit\)\>.*\%$" +syn match gitcommitUnmergedArrow " -> " contained nextgroup=gitcommitUnmergedFile hi def link gitcommitSummary Keyword +hi def link gitcommitTrailerToken Label hi def link gitcommitComment Comment -hi def link gitcommitUntracked gitcommitComment -hi def link gitcommitDiscarded gitcommitComment -hi def link gitcommitSelected gitcommitComment -hi def link gitcommitUnmerged gitcommitComment +hi def link gitcommitHash Identifier hi def link gitcommitOnBranch Comment hi def link gitcommitBranch Special hi def link gitcommitNoBranch gitCommitBranch diff --git a/runtime/syntax/gitrebase.vim b/runtime/syntax/gitrebase.vim index bc6f34d1a7..13f157b005 100644 --- a/runtime/syntax/gitrebase.vim +++ b/runtime/syntax/gitrebase.vim @@ -2,7 +2,7 @@ " Language: git rebase --interactive " Maintainer: Tim Pope <vimNOSPAM@tpope.org> " Filenames: git-rebase-todo -" Last Change: 2019 Dec 06 +" Last Change: 2022 Jan 05 if exists("b:current_syntax") finish @@ -10,8 +10,10 @@ endif syn case match -syn match gitrebaseHash "\v<\x{7,}>" contained -syn match gitrebaseCommit "\v<\x{7,}>" nextgroup=gitrebaseSummary skipwhite +let s:c = escape((matchstr(getline('$'), '^[#;@!$%^&|:]\S\@!') . '#')[0], '^$.*[]~\"/') + +syn match gitrebaseHash "\v<\x{7,}>" contained contains=@NoSpell +syn match gitrebaseCommit "\v<\x{7,}>" nextgroup=gitrebaseSummary skipwhite contains=@NoSpell syn match gitrebasePick "\v^p%(ick)=>" nextgroup=gitrebaseCommit skipwhite syn match gitrebaseReword "\v^r%(eword)=>" nextgroup=gitrebaseCommit skipwhite syn match gitrebaseEdit "\v^e%(dit)=>" nextgroup=gitrebaseCommit skipwhite @@ -26,12 +28,15 @@ syn match gitrebaseLabel "\v^l(abel)=>" nextgroup=gitrebaseName skipwhite syn match gitrebaseReset "\v^(t|reset)=>" nextgroup=gitrebaseName skipwhite syn match gitrebaseSummary ".*" contains=gitrebaseHash contained syn match gitrebaseCommand ".*" contained -syn match gitrebaseComment "^\s*#.*" contains=gitrebaseHash +exe 'syn match gitrebaseComment " \@<=' . s:c . ' empty$" containedin=gitrebaseSummary contained' +exe 'syn match gitrebaseComment "^\s*' . s:c . '.*" contains=gitrebaseHash' syn match gitrebaseSquashError "\v%^%(s%(quash)=>|f%(ixup)=>)" nextgroup=gitrebaseCommit skipwhite syn match gitrebaseMergeOption "\v-[Cc]>" nextgroup=gitrebaseMergeCommit skipwhite contained syn match gitrebaseMergeCommit "\v<\x{7,}>" nextgroup=gitrebaseName skipwhite contained syn match gitrebaseName "\v[^[:space:].*?i:^~/-]\S+" nextgroup=gitrebaseMergeComment skipwhite contained -syn match gitrebaseMergeComment "#" nextgroup=gitrebaseSummary skipwhite contained +exe 'syn match gitrebaseMergeComment "' . s:c . '" nextgroup=gitrebaseSummary skipwhite contained' + +unlet s:c hi def link gitrebaseCommit gitrebaseHash hi def link gitrebaseHash Identifier diff --git a/runtime/syntax/i3config.vim b/runtime/syntax/i3config.vim index a8b6637140..a2f50e50b8 100644 --- a/runtime/syntax/i3config.vim +++ b/runtime/syntax/i3config.vim @@ -1,8 +1,9 @@ " Vim syntax file " Language: i3 config file -" Maintainer: Mohamed Boughaba <mohamed dot bgb at gmail dot com> +" Original Author: Mohamed Boughaba <mohamed dot bgb at gmail dot com> +" Maintainer: Quentin Hibon (github user hiqua) " Version: 0.4 -" Last Change: 2021 Dec 14 +" Last Change: 2022 Jan 15 " References: " http://i3wm.org/docs/userguide.html#configuring @@ -174,7 +175,7 @@ syn keyword i3ConfigDrawingMarksKeyword show_marks contained syn match i3ConfigDrawingMarks /^\s*show_marks\s\+\(yes\|no\)\s\?$/ contains=i3ConfigFocusWrappingType,i3ConfigDrawingMarksKeyword " Group mode/bar -syn keyword i3ConfigBlockKeyword mode bar colors i3bar_command status_command position exec mode hidden_state modifier id position output background statusline tray_output tray_padding separator separator_symbol workspace_buttons strip_workspace_numbers binding_mode_indicator focused_workspace active_workspace inactive_workspace urgent_workspace binding_mode contained +syn keyword i3ConfigBlockKeyword mode bar colors i3bar_command status_command position exec mode hidden_state modifier id position output background statusline tray_output tray_padding separator separator_symbol workspace_min_width workspace_buttons strip_workspace_numbers binding_mode_indicator focused_workspace active_workspace inactive_workspace urgent_workspace binding_mode contained syn region i3ConfigBlock start=+.*s\?{$+ end=+^}$+ contains=i3ConfigBlockKeyword,i3ConfigString,i3ConfigBind,i3ConfigComment,i3ConfigFont,i3ConfigFocusWrappingType,i3ConfigColor,i3ConfigVariable transparent keepend extend " Line continuation diff --git a/runtime/syntax/qb64.vim b/runtime/syntax/qb64.vim new file mode 100644 index 0000000000..a777e14481 --- /dev/null +++ b/runtime/syntax/qb64.vim @@ -0,0 +1,409 @@ +" Vim syntax file +" Language: QB64 +" Maintainer: Doug Kearns <dougkearns@gmail.com> +" Last Change: 2022 Jan 21 + +" Prelude {{{1 +if exists("b:current_syntax") + finish +endif + +let s:cpo_save = &cpo +set cpo&vim + +" syn iskeyword set after sourcing of basic.vim + +syn case ignore + +let s:prefix = search('\c^\s*$NOPREFIX\>', 'n') ? '_\=' : '_' + +" Statements {{{1 + +let s:statements =<< trim EOL " {{{2 + acceptfiledrop + allowfullscreen + assert + console + consolecursor + consolefont + consoletitle + continue + copypalette + define + delay + depthbuffer + displayorder + dontblend + echo + exit\s\+\%(select\|case\) + finishdrop + freefont + freeimage + icon + keyclear + limit + maptriangle + memcopy + memfill + memfree + memput + mousehide + mousemove + mouseshow + printimage + printstring + putimage + screenclick + screenhide + screenmove + screenprint + screenshow + setalpha + sndbal + sndclose + sndlimit + sndloop + sndpause + sndplay + sndplaycopy + sndplayfile + sndraw + sndrawdone + sndsetpos + sndstop + sndvol + title +EOL +" }}} + +for s in s:statements + exe 'syn match qb64Statement "\<' .. s:prefix .. s .. '\>" contained contains=qb64Underscore' +endfor + +" Functions {{{1 + +let s:functions =<< trim EOL " {{{2 + acos + acosh + alpha + alpha32 + arccot + arccsc + arcsec + asin + asinh + atan2 + atanh + axis + backgroundcolor + blue + blue32 + button + buttonchange + ceil + cinp + commandcount + connected + connectionaddress + connectionaddress$ + consoleinput + copyimage + cot + coth + cosh + csc + csch + cv + cwd$ + d2g + d2r + defaultcolor + deflate$ + desktopheight + desktopwidth + device$ + deviceinput + devices + dir$ + direxists + droppedfile + droppedfile$ + errorline + errormessage$ + exit + fileexists + fontheight + fontwidth + freetimer + g2d + g2r + green + green32 + height + hypot + inclerrorfile$ + inclerrorline + inflate$ + instrrev + keyhit + keydown + lastaxis + lastbutton + lastwheel + loadfont + loadimage + mem + memelement + memexists + memimage + memnew + memsound + mk$ + mousebutton + mouseinput + mousemovementx + mousemovementy + mousepipeopen + mousewheel + mousex + mousey + newimage + offset + openclient + os$ + pi + pixelsize + printwidth + r2d + r2g + red + red32 + readbit + resetbit + resizeheight + resizewidth + rgb + rgb32 + rgba + rgba32 + round + sec + sech + screenexists + screenimage + screenx + screeny + setbit + shellhide + shl + shr + sinh + sndcopy + sndgetpos + sndlen + sndopen + sndopenraw + sndpaused + sndplaying + sndrate + sndrawlen + startdir$ + strcmp + stricmp + tanh + title$ + togglebit + totaldroppedfiles + trim$ + wheel + width + windowhandle + windowhasfocus +EOL +" }}} + +for f in s:functions + exe 'syn match qb64Function "\<' .. s:prefix .. f .. '\>" contains=qb64Underscore' +endfor + +" Functions and statements (same name) {{{1 + +let s:common =<< trim EOL " {{{2 + autodisplay + blend + blink + capslock + clearcolor + clipboard$ + clipboardimage + controlchr + dest + display + font + fullscreen + mapunicode + memget + numlock + palettecolor + printmode + resize + screenicon + scrolllock + source +EOL +" }}} + +for c in s:common + exe 'syn match qb64Statement "\<' .. s:prefix .. c .. '\>" contains=qb64Underscore contained' + exe 'syn match qb64Function "\<' .. s:prefix .. c .. '\>" contains=qb64Underscore' +endfor + +" Keywords {{{1 + +" Non-prefixed keywords {{{2 +" TIMER FREE +" _DEPTH_BUFFER LOCK +syn keyword qb64Keyword free lock + +let s:keywords =<< trim EOL " {{{2 + all + anticlockwise + behind + clear + clip + console + dontwait + explicit + explicitarray + fillbackground + hardware + hardware1 + hide + keepbackground + middle + none + off + only + onlybackground + ontop + openconnection + openhost + preserve + seamless + smooth + smoothshrunk + smoothstretched + software + squarepixels + stretch + toggle +EOL +" }}} + +for k in s:keywords + exe 'syn match qb64Keyword "\<' .. s:prefix .. k .. '\>" contains=qb64Underscore' +endfor + +syn match qb64Underscore "\<_" contained conceal transparent + +" Source QuickBASIC syntax {{{1 +runtime! syntax/basic.vim + +" add after the BASIC syntax file is sourced so cluster already exists +syn cluster basicStatements add=qb64Statement,qb64Metacommand,qb64IfMetacommand +syn cluster basicLineIdentifier add=qb64LineLabel +syn cluster qb64NotTop contains=@basicNotTop,qb64Metavariable + +syn iskeyword @,48-57,.,_,!,#,$,%,&,` + +" Unsupported QuickBASIC features {{{1 +" TODO: add linux only missing features +syn keyword qb64Unsupported alias any byval calls cdecl erdev erdev$ fileattr +syn keyword qb64Unsupported fre ioctl ioctl$ pen play setmem signal uevent +syn keyword qb64Unsupported tron troff +syn match qb64Unsupported "\<declare\%(\s\+\%(sub\|function\)\>\)\@=" +syn match qb64Unsupported "\<\%(date\|time\)$\ze\s*=" " statements only +syn match qb64Unsupported "\<def\zs\s\+FN" +syn match qb64Unsupported "\<\%(exit\|end\)\s\+def\>" +syn match qb64Unsupported "\<width\s\+lprint\>" + +" Types {{{1 +syn keyword qb64Type _BIT _BYTE _FLOAT _INTEGER64 _MEM _OFFSET _UNSIGNED + +" Type suffixes {{{1 +if exists("basic_type_suffixes") + " TODO: handle leading word boundary and __+ prefix + syn match qb64TypeSuffix "\%(\a[[:alnum:]._]*\)\@<=\~\=`\%(\d\+\)\=" + syn match qb64TypeSuffix "\%(\a[[:alnum:]._]*\)\@<=\~\=\%(%\|%%\|&\|&&\|%&\)" + syn match qb64TypeSuffix "\%(\a[[:alnum:]._]*\)\@<=\%(!\|##\|#\)" + syn match qb64TypeSuffix "\%(\a[[:alnum:]._]*\)\@<=$\%(\d\+\)\=" +endif + +" Numbers {{{1 + +" Integers +syn match qb64Number "-\=&b[01]\+&\>\=" + +syn match qb64Number "-\=\<[01]\~\=`\>" +syn match qb64Number "-\=\<\d\+`\d\+\>" + +syn match qb64Number "-\=\<\d\+\%(%%\|&&\|%&\)\>" +syn match qb64Number "\<\d\+\~\%(%%\|&&\|%&\)\>" + +syn match qb64Number "-\=\<&b[01]\+\%(%%\|&&\|%&\)\>" +syn match qb64Number "\<&b[01]\+\~\%(%%\|&&\|%&\)\>" + +syn match qb64Number "-\=\<&o\=\o\+\%(%%\|&&\|%&\)\>" +syn match qb64Number "\<&o\=\o\+\~\%(%%\|&&\|%&\)\>" + +syn match qb64Number "-\=\<&h\x\+\%(%%\|&&\|%&\)\>" +syn match qb64Number "\<&h\x\+\~\%(%%\|&&\|%&\)\>" + +" Floats +syn match qb64Float "-\=\<\d\+\.\=\d*##\>" +syn match qb64Float "-\=\<\.\d\+##\>" + +" Line numbers and labels {{{1 +syn match qb64LineLabel "\%(_\{2,}\)\=\a[[:alnum:]._]*[[:alnum:]]\ze\s*:" nextgroup=@basicStatements skipwhite contained + +" Metacommands {{{1 +syn match qb64Metacommand contained "$NOPREFIX\>" +syn match qb64Metacommand contained "$ASSERTS\%(:CONSOLE\)\=\>" +syn match qb64Metacommand contained "$CHECKING:\%(ON\|OFF\)\>" +syn match qb64Metacommand contained "$COLOR:\%(0\|32\)\>" +syn match qb64Metacommand contained "$CONSOLE\%(:ONLY\)\=\>" +syn match qb64Metacommand contained "$EXEICON\s*:\s*'[^']\+'" +syn match qb64Metacommand contained "$ERROR\>" +syn match qb64Metacommand contained "$LET\>" +syn match qb64Metacommand contained "$RESIZE:\%(ON\|OFF\|STRETCH\|SMOOTH\)\>" +syn match qb64Metacommand contained "$SCREEN\%(HIDE\|SHOW\)\>" +syn match qb64Metacommand contained "$VERSIONINFO\s*:.*" +syn match qb64Metacommand contained "$VIRTUALKEYBOARD:\%(ON\|OFF\)\>" + +syn region qb64IfMetacommand contained matchgroup=qb64Metacommand start="$\%(IF\|ELSEIF\)\>" end="\<THEN\>" oneline transparent contains=qb64Metavariable +syn match qb64Metacommand contained "$\%(ELSE\|END\s*IF\)\>" + +syn keyword qb64Metavariable contained defined undefined +syn keyword qb64Metavariable contained windows win linux mac maxosx +syn keyword qb64Metavariable contained 32bit 64bit version + +" Default Highlighting {{{1 +hi def link qb64Float basicFloat +hi def link qb64Function Function +hi def link qb64Keyword Keyword +hi def link qb64LineLabel basicLineLabel +hi def link qb64Metacommand PreProc +hi def link qb64Metavariable Identifier +hi def link qb64Number basicNumber +hi def link qb64Statement Statement +hi def link qb64TypeSuffix basicTypeSuffix +hi def link qb64Type Type +hi def link qb64Unsupported Error + +" Postscript {{{1 +let b:current_syntax = "qb64" + +let &cpo = s:cpo_save +unlet s:cpo_save + +" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker: diff --git a/runtime/syntax/rc.vim b/runtime/syntax/rc.vim index 4c6856bc83..d69edd00fd 100644 --- a/runtime/syntax/rc.vim +++ b/runtime/syntax/rc.vim @@ -1,7 +1,7 @@ " Vim syntax file " Language: M$ Resource files (*.rc) " Maintainer: Christian Brabandt -" Last Change: 2015-05-29 +" Last Change: 20220116 " Repository: https://github.com/chrisbra/vim-rc-syntax " License: Vim (see :h license) " Previous Maintainer: Heiko Erhardt <Heiko.Erhardt@munich.netsurf.de> @@ -173,16 +173,17 @@ hi def link rcAttribute rcCommonAttribute hi def link rcStdId rcStatement hi def link rcStatement Statement -" Default color overrides -hi def rcLanguage term=reverse ctermbg=Red ctermfg=Yellow guibg=Red guifg=Yellow -hi def rcMainObject term=underline ctermfg=Blue guifg=Blue -hi def rcSubObject ctermfg=Green guifg=Green -hi def rcCaptionParam term=underline ctermfg=DarkGreen guifg=Green -hi def rcParam ctermfg=DarkGreen guifg=DarkGreen -hi def rcStatement ctermfg=DarkGreen guifg=DarkGreen -hi def rcCommonAttribute ctermfg=Brown guifg=Brown +hi def link rcLanguage Constant +hi def link rcCaptionParam Constant +hi def link rcCommonAttribute Constant + +hi def link rcMainObject Identifier +hi def link rcSubObject Define +hi def link rcParam Constant +hi def link rcStatement Statement +" +"hi def link rcIdentifier Identifier -"hi def link rcIdentifier Identifier let b:current_syntax = "rc" diff --git a/runtime/syntax/scala.vim b/runtime/syntax/scala.vim index 16e114778d..c08e60e55a 100644 --- a/runtime/syntax/scala.vim +++ b/runtime/syntax/scala.vim @@ -3,7 +3,7 @@ " Maintainer: Derek Wyatt " URL: https://github.com/derekwyatt/vim-scala " License: Same as Vim -" Last Change: 23 August 2021 +" Last Change: 23 January 2022 " ---------------------------------------------------------------------------- if !exists('main_syntax') @@ -43,55 +43,55 @@ syn keyword scalaKeyword class trait object extends with nextgroup=scalaInstance syn keyword scalaKeyword case nextgroup=scalaKeyword,scalaCaseFollowing skipwhite syn keyword scalaKeyword val nextgroup=scalaNameDefinition,scalaQuasiQuotes skipwhite syn keyword scalaKeyword def var nextgroup=scalaNameDefinition skipwhite -hi link scalaKeyword Keyword +hi def link scalaKeyword Keyword exe 'syn region scalaBlock start=/{/ end=/}/ contains=' . s:ContainedGroup() . ' fold' syn keyword scalaAkkaSpecialWord when goto using startWith initialize onTransition stay become unbecome -hi link scalaAkkaSpecialWord PreProc +hi def link scalaAkkaSpecialWord PreProc syn keyword scalatestSpecialWord shouldBe syn match scalatestShouldDSLA /^\s\+\zsit should/ syn match scalatestShouldDSLB /\<should\>/ -hi link scalatestSpecialWord PreProc -hi link scalatestShouldDSLA PreProc -hi link scalatestShouldDSLB PreProc +hi def link scalatestSpecialWord PreProc +hi def link scalatestShouldDSLA PreProc +hi def link scalatestShouldDSLB PreProc syn match scalaSymbol /'[_A-Za-z0-9$]\+/ -hi link scalaSymbol Number +hi def link scalaSymbol Number syn match scalaChar /'.'/ syn match scalaChar /'\\[\\"'ntbrf]'/ contains=scalaEscapedChar syn match scalaChar /'\\u[A-Fa-f0-9]\{4}'/ contains=scalaUnicodeChar syn match scalaEscapedChar /\\[\\"'ntbrf]/ syn match scalaUnicodeChar /\\u[A-Fa-f0-9]\{4}/ -hi link scalaChar Character -hi link scalaEscapedChar Special -hi link scalaUnicodeChar Special +hi def link scalaChar Character +hi def link scalaEscapedChar Special +hi def link scalaUnicodeChar Special syn match scalaOperator "||" syn match scalaOperator "&&" syn match scalaOperator "|" syn match scalaOperator "&" -hi link scalaOperator Special +hi def link scalaOperator Special syn match scalaNameDefinition /\<[_A-Za-z0-9$]\+\>/ contained nextgroup=scalaPostNameDefinition,scalaVariableDeclarationList syn match scalaNameDefinition /`[^`]\+`/ contained nextgroup=scalaPostNameDefinition syn match scalaVariableDeclarationList /\s*,\s*/ contained nextgroup=scalaNameDefinition syn match scalaPostNameDefinition /\_s*:\_s*/ contained nextgroup=scalaTypeDeclaration -hi link scalaNameDefinition Function +hi def link scalaNameDefinition Function syn match scalaInstanceDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaInstanceHash syn match scalaInstanceDeclaration /`[^`]\+`/ contained syn match scalaInstanceHash /#/ contained nextgroup=scalaInstanceDeclaration -hi link scalaInstanceDeclaration Special -hi link scalaInstanceHash Type +hi def link scalaInstanceDeclaration Special +hi def link scalaInstanceHash Type syn match scalaUnimplemented /???/ -hi link scalaUnimplemented ERROR +hi def link scalaUnimplemented ERROR syn match scalaCapitalWord /\<[A-Z][A-Za-z0-9$]*\>/ -hi link scalaCapitalWord Special +hi def link scalaCapitalWord Special " Handle type declarations specially syn region scalaTypeStatement matchgroup=Keyword start=/\<type\_s\+\ze/ end=/$/ contains=scalaTypeTypeDeclaration,scalaSquareBrackets,scalaTypeTypeEquals,scalaTypeStatement @@ -105,18 +105,18 @@ syn match scalaTypeTypeEquals /=\ze[^>]/ contained nextgroup=scalaTypeTypePostDe syn match scalaTypeTypeExtension /)\?\_s*\zs\%(⇒\|=>\|<:\|:>\|=:=\|::\|#\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeTypeDeclaration skipwhite syn match scalaTypeTypePostDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeTypePostExtension skipwhite syn match scalaTypeTypePostExtension /\%(⇒\|=>\|<:\|:>\|=:=\|::\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeTypePostDeclaration skipwhite -hi link scalaTypeTypeDeclaration Type -hi link scalaTypeTypeExtension Keyword -hi link scalaTypeTypePostDeclaration Special -hi link scalaTypeTypePostExtension Keyword +hi def link scalaTypeTypeDeclaration Type +hi def link scalaTypeTypeExtension Keyword +hi def link scalaTypeTypePostDeclaration Special +hi def link scalaTypeTypePostExtension Keyword syn match scalaTypeDeclaration /(/ contained nextgroup=scalaTypeExtension contains=scalaRoundBrackets skipwhite syn match scalaTypeDeclaration /\%(⇒\|=>\)\ze/ contained nextgroup=scalaTypeDeclaration contains=scalaTypeExtension skipwhite syn match scalaTypeDeclaration /\<[_\.A-Za-z0-9$]\+\>/ contained nextgroup=scalaTypeExtension skipwhite syn match scalaTypeExtension /)\?\_s*\zs\%(⇒\|=>\|<:\|:>\|=:=\|::\|#\)/ contained contains=scalaTypeOperator nextgroup=scalaTypeDeclaration skipwhite -hi link scalaTypeDeclaration Type -hi link scalaTypeExtension Keyword -hi link scalaTypePostExtension Keyword +hi def link scalaTypeDeclaration Type +hi def link scalaTypeExtension Keyword +hi def link scalaTypePostExtension Keyword syn match scalaTypeAnnotation /\%([_a-zA-Z0-9$\s]:\_s*\)\ze[_=(\.A-Za-z0-9$]\+/ skipwhite nextgroup=scalaTypeDeclaration contains=scalaRoundBrackets syn match scalaTypeAnnotation /)\_s*:\_s*\ze[_=(\.A-Za-z0-9$]\+/ skipwhite nextgroup=scalaTypeDeclaration @@ -124,51 +124,51 @@ hi clear scalaTypeAnnotation syn match scalaCaseFollowing /\<[_\.A-Za-z0-9$]\+\>/ contained contains=scalaCapitalWord syn match scalaCaseFollowing /`[^`]\+`/ contained contains=scalaCapitalWord -hi link scalaCaseFollowing Special +hi def link scalaCaseFollowing Special syn keyword scalaKeywordModifier abstract override final lazy implicit private protected sealed null super syn keyword scalaSpecialFunction implicitly require -hi link scalaKeywordModifier Function -hi link scalaSpecialFunction Function +hi def link scalaKeywordModifier Function +hi def link scalaSpecialFunction Function syn keyword scalaSpecial this true false ne eq syn keyword scalaSpecial new nextgroup=scalaInstanceDeclaration skipwhite syn match scalaSpecial "\%(=>\|⇒\|<-\|←\|->\|→\)" syn match scalaSpecial /`[^`]\+`/ " Backtick literals -hi link scalaSpecial PreProc +hi def link scalaSpecial PreProc syn keyword scalaExternal package import -hi link scalaExternal Include +hi def link scalaExternal Include syn match scalaStringEmbeddedQuote /\\"/ contained syn region scalaString start=/"/ end=/"/ contains=scalaStringEmbeddedQuote,scalaEscapedChar,scalaUnicodeChar -hi link scalaString String -hi link scalaStringEmbeddedQuote String +hi def link scalaString String +hi def link scalaStringEmbeddedQuote String syn region scalaIString matchgroup=scalaInterpolationBrackets start=/\<[a-zA-Z][a-zA-Z0-9_]*"/ skip=/\\"/ end=/"/ contains=scalaInterpolation,scalaInterpolationB,scalaEscapedChar,scalaUnicodeChar syn region scalaTripleIString matchgroup=scalaInterpolationBrackets start=/\<[a-zA-Z][a-zA-Z0-9_]*"""/ end=/"""\ze\%([^"]\|$\)/ contains=scalaInterpolation,scalaInterpolationB,scalaEscapedChar,scalaUnicodeChar -hi link scalaIString String -hi link scalaTripleIString String +hi def link scalaIString String +hi def link scalaTripleIString String syn match scalaInterpolation /\$[a-zA-Z0-9_$]\+/ contained exe 'syn region scalaInterpolationB matchgroup=scalaInterpolationBoundary start=/\${/ end=/}/ contained contains=' . s:ContainedGroup() -hi link scalaInterpolation Function +hi def link scalaInterpolation Function hi clear scalaInterpolationB syn region scalaFString matchgroup=scalaInterpolationBrackets start=/f"/ skip=/\\"/ end=/"/ contains=scalaFInterpolation,scalaFInterpolationB,scalaEscapedChar,scalaUnicodeChar syn match scalaFInterpolation /\$[a-zA-Z0-9_$]\+\(%[-A-Za-z0-9\.]\+\)\?/ contained exe 'syn region scalaFInterpolationB matchgroup=scalaInterpolationBoundary start=/${/ end=/}\(%[-A-Za-z0-9\.]\+\)\?/ contained contains=' . s:ContainedGroup() -hi link scalaFString String -hi link scalaFInterpolation Function +hi def link scalaFString String +hi def link scalaFInterpolation Function hi clear scalaFInterpolationB syn region scalaTripleString start=/"""/ end=/"""\%([^"]\|$\)/ contains=scalaEscapedChar,scalaUnicodeChar syn region scalaTripleFString matchgroup=scalaInterpolationBrackets start=/f"""/ end=/"""\%([^"]\|$\)/ contains=scalaFInterpolation,scalaFInterpolationB,scalaEscapedChar,scalaUnicodeChar -hi link scalaTripleString String -hi link scalaTripleFString String +hi def link scalaTripleString String +hi def link scalaTripleFString String -hi link scalaInterpolationBrackets Special -hi link scalaInterpolationBoundary Function +hi def link scalaInterpolationBrackets Special +hi def link scalaInterpolationBoundary Function syn match scalaNumber /\<0[dDfFlL]\?\>/ " Just a bare 0 syn match scalaNumber /\<[1-9]\d*[dDfFlL]\?\>/ " A multi-digit number - octal numbers with leading 0's are deprecated in Scala @@ -176,16 +176,16 @@ syn match scalaNumber /\<0[xX][0-9a-fA-F]\+[dDfFlL]\?\>/ " Hex number syn match scalaNumber /\%(\<\d\+\.\d*\|\.\d\+\)\%([eE][-+]\=\d\+\)\=[fFdD]\=/ " exponential notation 1 syn match scalaNumber /\<\d\+[eE][-+]\=\d\+[fFdD]\=\>/ " exponential notation 2 syn match scalaNumber /\<\d\+\%([eE][-+]\=\d\+\)\=[fFdD]\>/ " exponential notation 3 -hi link scalaNumber Number +hi def link scalaNumber Number syn region scalaRoundBrackets start="(" end=")" skipwhite contained contains=scalaTypeDeclaration,scalaSquareBrackets,scalaRoundBrackets syn region scalaSquareBrackets matchgroup=scalaSquareBracketsBrackets start="\[" end="\]" skipwhite nextgroup=scalaTypeExtension contains=scalaTypeDeclaration,scalaSquareBrackets,scalaTypeOperator,scalaTypeAnnotationParameter syn match scalaTypeOperator /[-+=:<>]\+/ contained syn match scalaTypeAnnotationParameter /@\<[`_A-Za-z0-9$]\+\>/ contained -hi link scalaSquareBracketsBrackets Type -hi link scalaTypeOperator Keyword -hi link scalaTypeAnnotationParameter Function +hi def link scalaSquareBracketsBrackets Type +hi def link scalaTypeOperator Keyword +hi def link scalaTypeAnnotationParameter Function syn match scalaShebang "\%^#!.*" display syn region scalaMultilineComment start="/\*" end="\*/" contains=scalaMultilineComment,scalaDocLinks,scalaParameterAnnotation,scalaCommentAnnotation,scalaTodo,scalaCommentCodeBlock,@Spell keepend fold @@ -195,20 +195,20 @@ syn match scalaParamAnnotationValue /[.`_A-Za-z0-9$]\+/ contained syn region scalaDocLinks start="\[\[" end="\]\]" contained syn region scalaCommentCodeBlock matchgroup=Keyword start="{{{" end="}}}" contained syn match scalaTodo "\vTODO|FIXME|XXX" contained -hi link scalaShebang Comment -hi link scalaMultilineComment Comment -hi link scalaDocLinks Function -hi link scalaParameterAnnotation Function -hi link scalaParamAnnotationValue Keyword -hi link scalaCommentAnnotation Function -hi link scalaCommentCodeBlock String -hi link scalaTodo Todo +hi def link scalaShebang Comment +hi def link scalaMultilineComment Comment +hi def link scalaDocLinks Function +hi def link scalaParameterAnnotation Function +hi def link scalaParamAnnotationValue Keyword +hi def link scalaCommentAnnotation Function +hi def link scalaCommentCodeBlock String +hi def link scalaTodo Todo syn match scalaAnnotation /@\<[`_A-Za-z0-9$]\+\>/ -hi link scalaAnnotation PreProc +hi def link scalaAnnotation PreProc syn match scalaTrailingComment "//.*$" contains=scalaTodo,@Spell -hi link scalaTrailingComment Comment +hi def link scalaTrailingComment Comment syn match scalaAkkaFSM /goto([^)]*)\_s\+\<using\>/ contains=scalaAkkaFSMGotoUsing syn match scalaAkkaFSM /stay\_s\+using/ @@ -221,8 +221,8 @@ syn match scalaAkkaFSM /onTermination/ syn match scalaAkkaFSM /whenUnhandled/ syn match scalaAkkaFSMGotoUsing /\<using\>/ syn match scalaAkkaFSMGotoUsing /\<goto\>/ -hi link scalaAkkaFSM PreProc -hi link scalaAkkaFSMGotoUsing PreProc +hi def link scalaAkkaFSM PreProc +hi def link scalaAkkaFSMGotoUsing PreProc let b:current_syntax = 'scala' diff --git a/runtime/syntax/strace.vim b/runtime/syntax/strace.vim index 206c58919e..20516a1853 100644 --- a/runtime/syntax/strace.vim +++ b/runtime/syntax/strace.vim @@ -1,8 +1,7 @@ " Vim syntax file -" This is a GENERATED FILE. Please always refer to source file at the URI below. " Language: strace output " Maintainer: David Necas (Yeti) <yeti@physics.muni.cz> -" Last Change: 2015-01-16 +" Last Change: 2022 Jan 29 " Setup " quit when a syntax file was already loaded diff --git a/runtime/tutor/en/vim-01-beginner.tutor b/runtime/tutor/en/vim-01-beginner.tutor index 7c0c357e80..e256711e70 100644 --- a/runtime/tutor/en/vim-01-beginner.tutor +++ b/runtime/tutor/en/vim-01-beginner.tutor @@ -1,15 +1,14 @@ -# Welcome to the VIM Tutor +# Welcome to the Neovim Tutorial -Vim is a very powerful editor that has many commands, too many to explain in -a tutor such as this. This tutor is designed to describe enough of the -commands that you will be able to easily use Vim as an all-purpose editor. -It is IMPORTANT to remember that this tutor is set up to teach by use. That +Neovim is a very powerful editor that has many commands, too many to explain in +a tutorial such as this. This tutorial is designed to describe enough of the +commands that you will be able to easily use Neovim as an all-purpose editor. +It is IMPORTANT to remember that this tutorial is set up to teach by use. That means that you need to do the exercises to learn them properly. If you only read the text, you will soon forget what is most important! -For now, make sure that your Shift-Lock key is NOT depressed and press the -`j`{normal} key enough times to move the cursor so that Lesson 0 completely -fills the screen. +For now, make sure that your Caps-Lock is off and press the `j`{normal} key enough +times to move the cursor so that Lesson 0 completely fills the screen. # Lesson 0 @@ -20,12 +19,13 @@ pressing [<Esc>](<Esc>) and then [u](u) will undo the latest change. This tutorial is interactive, and there are a few things you should know. - Type [<Enter>](<Enter>) on links [like this](holy-grail ) to open the linked help section. - Or simply type [K](K) on any word to find its documentation! +- You can close this help window with `:q`{vim} - Sometimes you will be required to modify text like this here Once you have done the changes correctly, the ✗ sign at the left will change -to ✓. I imagine you can already see how neat Vim can be. ;) +to ✓. I imagine you can already see how neat Neovim can be. Other times, you'll be prompted to run a command (I'll explain this later): ~~~ cmd :help <Enter> @@ -43,9 +43,9 @@ Now, move to the next lesson (use the `j`{normal} key to scroll down). ** To move the cursor, press the `h`, `j`, `k`, `l` keys as indicated. ** - ↑ - k Hint: The `h`{normal} key is at the left and moves left. - ← h l → The `l`{normal} key is at the right and moves right. + ↑ + k Hint: The `h`{normal} key is at the left and moves left. + ← h l → The `l`{normal} key is at the right and moves right. j The `j`{normal} key looks like a down arrow. ↓ @@ -60,11 +60,11 @@ NOTE: If you are ever unsure about something you typed, press <Esc> to place you in Normal mode. Then retype the command you wanted. NOTE: The cursor keys should also work. But using hjkl you will be able to - move around much faster, once you get used to it. Really! + move around much faster, once you get used to it. -# Lesson 1.2: EXITING VIM +# Lesson 1.2: EXITING NEOVIM -!! NOTE: Before executing any of the steps below, read this entire lesson !! +!! NOTE: Before executing any of the steps below, read the entire lesson !! 1. Press the <Esc> key (to make sure you are in Normal mode). @@ -72,10 +72,10 @@ NOTE: The cursor keys should also work. But using hjkl you will be able to `:q!`{vim} `<Enter>`{normal}. - This exits the editor, DISCARDING any changes you have made. + This quits the editor, DISCARDING any changes you have made. - 3. Open vim and get back here by executing the command that got you into - this tutor. That might be: + 3. Open Neovim and get back here by executing the command that got you into + this tutorial. That might be: :Tutor <Enter> @@ -104,9 +104,9 @@ The ccow jumpedd ovverr thhe mooon. 5. Now that the line is correct, go on to Lesson 1.4. -NOTE: As you go through this tutor, do not try to memorize everything, - your Vim vocabulary will expand with usage. Consider returning to - this tutor periodically for a refresher. +NOTE: As you go through this tutorial, do not try to memorize everything, + your Neovim vocabulary will expand with usage. Consider returning to + this tutorial periodically for a refresher. # Lesson 1.4: TEXT EDITING: INSERTION @@ -151,11 +151,11 @@ There is also some text missing here. # Lesson 1.6: EDITING A FILE -** Use `:wq`{vim} to save a file and exit. ** +** Use `:wq`{vim} to write a file and quit. ** -!! NOTE: Before executing any of the steps below, read this entire lesson !! +!! NOTE: Before executing any of the steps below, read the entire lesson !! - 1. Exit this tutor as you did in Lesson 1.2: `:q!`{vim} + 1. Exit this tutorial as you did in Lesson 1.2: `:q!`{vim} Or, if you have access to another terminal, do the following there. 2. At the shell prompt type this command: @@ -167,28 +167,28 @@ There is also some text missing here. 3. Insert and delete text as you learned in the previous lessons. - 4. Save the file with changes and exit Vim with: + 4. Save the file with changes and exit Neovim with: ~~~ cmd :wq ~~~ Note you'll need to press `<Enter>` to execute the command. - 5. If you have quit vimtutor in step 1 restart the vimtutor and move down + 5. If you have quit this tutorial in step 1, restart and move down to the following summary. - 6. After reading the above steps and understanding them: do it. + 6. After reading and understanding the above steps: do them. # Lesson 1 SUMMARY 1. The cursor is moved using either the arrow keys or the hjkl keys. h (left) j (down) k (up) l (right) - 2. To start Vim from the shell prompt type: + 2. To start Neovim from the shell prompt type: ~~~ sh $ nvim FILENAME ~~~ - 3. To exit Vim type: `<Esc>`{normal} `:q!`{vim} `<Enter>`{normal} to trash all changes. - OR type: `<Esc>`{normal} `:wq`{vim} `<Enter>`{normal} to save the changes. + 3. To exit Neovim type: `<Esc>`{normal} `:q!`{vim} `<Enter>`{normal} to trash all changes. + OR type: `<Esc>`{normal} `:wq`{vim} `<Enter>`{normal} to save the changes. 4. To delete the character at the cursor type: `x`{normal} @@ -297,8 +297,11 @@ Due to the frequency of whole line deletion, the designers of Vi decided it would be easier to simply type two d's to delete a line. 1. Move the cursor to the second line in the phrase below. + 2. Type [dd](dd) to delete the line. + 3. Now move to the fourth line. + 4. Type `2dd`{normal} to delete two lines. 1) Roses are red, @@ -314,11 +317,17 @@ it would be easier to simply type two d's to delete a line. ** Press `u`{normal} to undo the last commands, `U`{normal} to fix a whole line. ** 1. Move the cursor to the line below marked ✗ and place it on the first error. + 2. Type `x`{normal} to delete the first unwanted character. + 3. Now type `u`{normal} to undo the last command executed. + 4. This time fix all the errors on the line using the `x`{normal} command. + 5. Now type a capital `U`{normal} to return the line to its original state. + 6. Now type `u`{normal} a few times to undo the `U`{normal} and preceding commands. + 7. Now type `<C-r>`{normal} (Control + R) a few times to redo the commands. Fiix the errors oon thhis line and reeplace them witth undo. @@ -328,8 +337,11 @@ Fiix the errors oon thhis line and reeplace them witth undo. # Lesson 2 SUMMARY 1. To delete from the cursor up to the next word type: `dw`{normal} + 2. To delete from the cursor to the end of a line type: `d$`{normal} + 3. To delete a whole line type: `dd`{normal} + 4. To repeat a motion prepend it with a number: `2w`{normal} 5. The format for a change command is: @@ -356,7 +368,7 @@ Fiix the errors oon thhis line and reeplace them witth undo. 1. Move the cursor to the first ✓ line below. - 2. Type `dd`{normal} to delete the line and store it in a Vim register. + 2. Type `dd`{normal} to delete the line and store it in a Neovim register. 3. Move the cursor to the c) line, ABOVE where the deleted line should go. @@ -369,6 +381,8 @@ b) Violets are blue, c) Intelligence is learned, a) Roses are red, +NOTE: You can also put the text before the cursor with `P`{normal} (capital P) + # Lesson 3.2: THE REPLACE COMMAND ** Type `rx`{normal} to replace the character at the cursor with x. ** @@ -386,7 +400,7 @@ When this line was typed in, someone pressed some wrong keys! 5. Now move on to Lesson 3.3. -NOTE: Remember that you should be learning by doing, not memorization. +NOTE: Remember that you should be learning by doing, not memorizing. # Lesson 3.3: THE CHANGE OPERATOR @@ -439,7 +453,7 @@ NOTE: You can use the Backspace key to correct mistakes while typing. 3. The [change operator](c) allows you to change from the cursor to where the motion takes you. Type `ce`{normal} to change from the cursor to the - end of the word, `c$`{normal} to change to the end of a line. + end of the word, `c$`{normal} to change to the end of a line, etc. 4. The format for change is: @@ -452,7 +466,7 @@ Now go on to the next lesson. ** Type `<C-g>`{normal} to show your location in a file and the file status. Type `G`{normal} to move to a line in the file. ** -NOTE: Read this entire lesson before executing any of the steps!! +NOTE: Read the entire lesson before executing any of these steps!! 1. Hold down the `<Ctrl>`{normal} key and press `g`{normal}. We call this `<C-g>`{normal}. A message will appear at the bottom of the page with the filename and @@ -535,8 +549,8 @@ Usually thee best time to see thee flowers is in thee spring. ~~~ cmd :#,#s/old/new/g ~~~ - where #,# are the line numbers of the range of lines where the - substitution is to be done. + where # are the line numbers of the range of lines where the + substitution is to be done (i.e., `1,3` means from line 1 to line 3, inclusive). Type ~~~ cmd @@ -551,6 +565,9 @@ Usually thee best time to see thee flowers is in thee spring. to find every occurrence in the whole file, with a prompt whether to substitute or not. +NOTE: You can also select the lines you want to substitute first using visual-mode. + This will be explained more in a future lesson. + # Lesson 4 SUMMARY 1. `<C-g>`{normal} displays your location and the file status. @@ -603,11 +620,10 @@ Usually thee best time to see thee flowers is in thee spring. This will show you a listing of your directory, just as if you were at the shell prompt. -NOTE: It is possible to execute any external command this way, also with - arguments. +NOTE: It is possible to execute any external command this way, and you + can include arguments. -NOTE: All `:`{vim} commands must be finished by hitting `<Enter>`{normal}. - From here on we will not always mention it. +NOTE: All `:`{vim} commands are executed when you press `<Enter>`{normal}. # Lesson 5.2: MORE ON WRITING FILES @@ -624,11 +640,11 @@ NOTE: All `:`{vim} commands must be finished by hitting `<Enter>`{normal}. ~~~ (where TEST is the filename you chose.) - 4. This saves the whole file (the Vim Tutor) under the name TEST. + 4. This saves the current file under the name TEST. To verify this, type `:!ls`{vim} again to see your directory. -NOTE: If you were to exit Vim and start it again with `nvim TEST`, the file - would be an exact copy of the tutor when you saved it. +NOTE: If you were to exit Neovim and start it again with `nvim TEST`, the file + would be an exact copy of the tutorial when you saved it. 5. Now remove the file by typing: ~~~ cmd @@ -659,7 +675,7 @@ NOTE: If you were to exit Vim and start it again with `nvim TEST`, the file before you press `<Enter>`{normal}. - 5. Vim will write the selected lines to the file TEST. Use `:!ls`{vim} to see it. + 5. Neovim will write the selected lines to the file TEST. Use `:!ls`{vim} to see it. Do not remove it yet! We will use it in the next lesson. NOTE: Pressing [v](v) starts [Visual selection](visual-mode). You can move the cursor around to @@ -668,7 +684,7 @@ NOTE: Pressing [v](v) starts [Visual selection](visual-mode). You can move the c # Lesson 5.4: RETRIEVING AND MERGING FILES -** To insert the contents of a file, type `:r FILENAME`{vim}. ** +** To retrieve the contents of a file, type `:r FILENAME`{vim}. ** 1. Place the cursor just above this line. @@ -683,7 +699,7 @@ NOTE: After executing Step 2 you will see text from Lesson 5.3. Then move The file you retrieve is placed below the cursor line. 3. To verify that a file was retrieved, cursor back and notice that there - are now two copies of Lesson 5.3, the original and the file version. + are now two copies of Lesson 5.3, the original and the retrieved version. NOTE: You can also read the output of an external command. For example, @@ -699,7 +715,7 @@ NOTE: You can also read the output of an external command. For example, `:!ls`{vim} - shows a directory listing `:!rm FILENAME`{vim} - removes file FILENAME - 2. [:w](:w) FILENAME writes the current Vim file to disk with + 2. [:w](:w) FILENAME writes the current Neovim file to disk with name FILENAME. 3. [v](v) motion :w FILENAME saves the Visually selected lines in file @@ -768,11 +784,11 @@ Adding 123 to xxx gives you xxx. Adding 123 to 456 gives you 579. NOTE: Replace mode is like Insert mode, but every typed character - deletes an existing character. + replaces an existing character. # Lesson 6.4: COPY AND PASTE TEXT -** Use the `y`{normal} operator to copy text and `p`{normal} to paste it. ** +** Use the `y`{normal} operator to copy text and `p`{normal} to put it. ** 1. Go to the line marked with ✓ below and place the cursor after "a)". @@ -796,9 +812,13 @@ b) NOTE: you can use `y`{normal} as an operator: `yw`{normal} yanks one word. +NOTE: you can use `P`{normal} to put before the cursor, rather than after. + # Lesson 6.5: SET OPTION -** Set an option so a search or substitute ignores case. ** +** Set an option so search and substitute commands ignore case. ** + +There are many settings in Neovim that you can configure to suit your needs. 1. Search for 'ignore' by entering: `/ignore` Repeat several times by pressing `n`{normal}. @@ -820,7 +840,7 @@ NOTE: you can use `y`{normal} as an operator: `yw`{normal} yanks one word. ~~~ cmd :set noic ~~~ - 7. To toggle the value of a setting, prepend it with "inv": + 7. To invert the value of a setting, prepend it with "inv": ~~~ cmd :set invic ~~~ @@ -858,19 +878,18 @@ NOTE: If you want to ignore case for just one search command, use [\c](/\c) ~~~ cmd :set noic ~~~ - 8. Prepend "inv" to toggle an option: + 8. Prepend "inv" to invert an option: ~~~ cmd :set invic ~~~ # Lesson 7.1: GETTING HELP -** Use the on-line help system. ** +** Use the online help system. ** -Vim has a comprehensive on-line help system. +Neovim has a comprehensive online help system. To get started, try one of these three: - - press the `<HELP>`{normal} key (if you have one) - press the `<F1>`{normal} key (if you have one) - type `:help`{vim} @@ -879,7 +898,7 @@ Type `<C-w><C-w>`{normal} to jump from one window to another. Type `:q`{vim} to close the help window. You can find help on just about any subject, by giving an argument to the -":help" command. Try these (don't forget pressing <Enter>): +":help" command. Try these (don't forget to press <Enter>): ~~~ cmd :help w :help c_CTRL-D @@ -888,12 +907,12 @@ You can find help on just about any subject, by giving an argument to the ~~~ # Lesson 7.2: CREATE A STARTUP SCRIPT -** Enable Vim features. ** +** Enable Neovim features. ** -Vim has many more features than Vi, but most of them are disabled by -default. To start using more features you have to create a "vimrc" file. +Neovim is a very configurable editor. You can customise it any way you like. +To start using more features create an "init.vim" file. - 1. Start editing the "vimrc" file. + 1. Start editing the "init.vim" file. `:call mkdir(stdpath('config'),'p')`{vim} `:exe 'edit' stdpath('config').'/init.vim'`{vim} @@ -902,24 +921,24 @@ default. To start using more features you have to create a "vimrc" file. `:w`{vim} - You can add all your preferred settings to this "vimrc" file. - For more information type `:help vimrc-intro`{vim}. + You can add all your preferred settings to this "init.vim" file. + For more information type `:help init.vim`{vim}. # Lesson 7.3: COMPLETION ** Command line completion with `<C-d>`{normal} and `<Tab>`{normal}. ** - 1. Look what files exist in the directory: `:!ls`{vim} + 1. List the contents of the current directory: `:!ls`{vim} 2. Type the start of a command: `:e`{vim} - 3. Press `<C-d>`{normal} and Vim will show a list of commands beginning with "e". + 3. Press `<C-d>`{normal} and Neovim will show a list of commands beginning with "e". - 4. Press `<Tab>`{normal} and Vim will complete the command name to ":edit". + 4. Press `<Tab>`{normal} and Neovim will complete the command name to ":edit". 5. Now add a space and the start of an existing file name: `:edit FIL`{vim} - 6. Press `<Tab>`{normal}. Vim will complete the name ("FIL" -> "FILE", if it is unique). + 6. Press `<Tab>`{normal}. Neovim will complete the name ("FIL" -> "FILE", if it is unique). NOTE: Completion works for many commands. It is especially useful for `:help`{vim}. @@ -934,19 +953,18 @@ NOTE: Completion works for many commands. It is especially useful for `:help`{vi 4. Type `:q`{vim} to close the help window - 5. Create a vimrc startup script to keep your preferred settings. + 5. Create an init.vim startup script to keep your preferred settings. 6. While in command mode, press `<C-d>`{normal} to see possible completions. Press `<Tab>`{normal} to use one completion. # CONCLUSION -This was intended to give a brief overview of the Vim editor, just enough to -allow you to use the editor fairly easily. It is far from complete as Vim has +This was intended to give a brief overview of the Neovim editor, just enough to +allow you to use it fairly easily. It is far from complete as Neovim has many many more commands. Consult the help often. - -There are many resources online to learn more about vim. Here's a bunch of -them: +There are also countless great tutorials and videos to be found online. +Here's a bunch of them: - *Learn Vim Progressively*: http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ @@ -964,7 +982,7 @@ them: https://github.com/mhinz/vim-galore If you prefer a book, *Practical Vim* by Drew Neil is recommended often -(the sequel, *Modern Vim*, includes material specific to nvim). +(the sequel, *Modern Vim*, includes material specific to Neovim). This tutorial was written by Michael C. Pierce and Robert K. Ware, Colorado School of Mines using ideas supplied by Charles Smith, Colorado State @@ -972,5 +990,6 @@ University. E-mail: bware@mines.colorado.edu. Modified for Vim by Bram Moolenaar. Modified for vim-tutor-mode by Felipe Morales. +Modified for Neovim by Rory Nesbitt. // vim: nowrap diff --git a/runtime/tutor/en/vim-01-beginner.tutor.json b/runtime/tutor/en/vim-01-beginner.tutor.json index e71ead976d..5dccb824e0 100644 --- a/runtime/tutor/en/vim-01-beginner.tutor.json +++ b/runtime/tutor/en/vim-01-beginner.tutor.json @@ -12,34 +12,34 @@ "232": "Somebody typed the end of this line twice.", "271": -1, "290": "This line of words is cleaned up.", - "304": -1, - "305": -1, - "306": -1, "307": -1, "308": -1, "309": -1, "310": -1, - "324": "Fix the errors on this line and replace them with undo.", - "367": -1, - "368": -1, - "369": -1, - "370": -1, - "384": "When this line was typed in, someone pressed some wrong keys!", - "385": "When this line was typed in, someone pressed some wrong keys!", - "405": "This line has a few words that need changing using the change operator.", - "406": "This line has a few words that need changing using the change operator.", - "426": "The end of this line needs to be corrected using the `c$` command.", - "427": "The end of this line needs to be corrected using the `c$` command.", - "490": -1, - "509": -1, - "532": "Usually the best time to see the flowers is in the spring.", - "725": -1, - "730": -1, - "746": "This line will allow you to practice appending text to a line.", - "747": "This line will allow you to practice appending text to a line.", - "767": "Adding 123 to 456 gives you 579.", - "768": "Adding 123 to 456 gives you 579.", - "794": "a) This is the first item.", - "795": "b) This is the second item." + "311": -1, + "312": -1, + "313": -1, + "333": "Fix the errors on this line and replace them with undo.", + "379": -1, + "380": -1, + "381": -1, + "382": -1, + "398": "When this line was typed in, someone pressed some wrong keys!", + "399": "When this line was typed in, someone pressed some wrong keys!", + "419": "This line has a few words that need changing using the change operator.", + "420": "This line has a few words that need changing using the change operator.", + "440": "The end of this line needs to be corrected using the `c$` command.", + "441": "The end of this line needs to be corrected using the `c$` command.", + "504": -1, + "523": -1, + "546": "Usually the best time to see the flowers is in the spring.", + "741": -1, + "746": -1, + "762": "This line will allow you to practice appending text to a line.", + "763": "This line will allow you to practice appending text to a line.", + "783": "Adding 123 to 456 gives you 579.", + "784": "Adding 123 to 456 gives you 579.", + "810": "a) This is the first item.", + "811": "b) This is the second item." } } diff --git a/scripts/gen_filetype.lua b/scripts/gen_filetype.lua new file mode 100644 index 0000000000..42478a1082 --- /dev/null +++ b/scripts/gen_filetype.lua @@ -0,0 +1,201 @@ +local do_not_run = true +if do_not_run then + print([[ + This script was used to bootstrap the filetype patterns in runtime/lua/vim/filetype.lua. It + should no longer be used except for testing purposes. New filetypes, or changes to existing + filetypes, should be ported manually as part of the vim-patch process. + ]]) + return +end + +local filetype_vim = "runtime/filetype.vim" +local filetype_lua = "runtime/lua/vim/filetype.lua" + +local keywords = { + ["for"] = true, + ["or"] = true, + ["and"] = true, + ["end"] = true, + ["do"] = true, + ["if"] = true, + ["while"] = true, + ["repeat"] = true, +} + +local sections = { + extension = { str = {}, func = {} }, + filename = { str = {}, func = {} }, + pattern = { str = {}, func = {} }, +} + +local specialchars = "%*%?\\%$%[%]%{%}" + +local function add_pattern(pat, ft) + local ok = true + + -- Patterns that start or end with { or } confuse splitting on commas and make parsing harder, so just skip those + if not string.find(pat, "^%{") and not string.find(pat, "%}$") then + for part in string.gmatch(pat, "[^,]+") do + if not string.find(part, "[" .. specialchars .. "]") then + if type(ft) == "string" then + sections.filename.str[part] = ft + else + sections.filename.func[part] = ft + end + elseif string.match(part, "^%*%.[^%./" .. specialchars .. "]+$") then + if type(ft) == "string" then + sections.extension.str[part:sub(3)] = ft + else + sections.extension.func[part:sub(3)] = ft + end + else + if string.match(part, "^%*/[^" .. specialchars .. "]+$") then + -- For patterns matching */some/pattern we want to easily match files + -- with path /some/pattern, so include those in filename detection + if type(ft) == "string" then + sections.filename.str[part:sub(2)] = ft + else + sections.filename.func[part:sub(2)] = ft + end + end + + if string.find(part, "^[%w-_.*?%[%]/]+$") then + local p = part:gsub("%.", "%%."):gsub("%*", ".*"):gsub("%?", ".") + -- Insert into array to maintain order rather than setting + -- key-value directly + if type(ft) == "string" then + sections.pattern.str[p] = ft + else + sections.pattern.func[p] = ft + end + else + ok = false + end + end + end + end + + return ok +end + +local function parse_line(line) + local pat, ft + pat, ft = line:match("^%s*au%a* Buf[%a,]+%s+(%S+)%s+setf%s+(%S+)") + if pat then + return add_pattern(pat, ft) + else + local func + pat, func = line:match("^%s*au%a* Buf[%a,]+%s+(%S+)%s+call%s+(%S+)") + if pat then + return add_pattern(pat, function() return func end) + end + end +end + +local unparsed = {} +local full_line +for line in io.lines(filetype_vim) do + local cont = string.match(line, "^%s*\\%s*(.*)$") + if cont then + full_line = full_line .. " " .. cont + else + if full_line then + if not parse_line(full_line) and string.find(full_line, "^%s*au%a* Buf") then + table.insert(unparsed, full_line) + end + end + full_line = line + end +end + +if #unparsed > 0 then + print("Failed to parse the following patterns:") + for _, v in ipairs(unparsed) do + print(v) + end +end + +local function add_item(indent, key, ft) + if type(ft) == "string" then + if string.find(key, "%A") or keywords[key] then + key = string.format("[\"%s\"]", key) + end + return string.format([[%s%s = "%s",]], indent, key, ft) + elseif type(ft) == "function" then + local func = ft() + if string.find(key, "%A") or keywords[key] then + key = string.format("[\"%s\"]", key) + end + -- Right now only a single argument is supported, which covers + -- everything in filetype.vim as of this writing + local arg = string.match(func, "%((.*)%)$") + func = string.gsub(func, "%(.*$", "") + if arg == "" then + -- Function with no arguments, call the function directly + return string.format([[%s%s = function() vim.fn["%s"]() end,]], indent, key, func) + elseif string.match(arg, [[^(["']).*%1$]]) then + -- String argument + if func == "s:StarSetf" then + return string.format([[%s%s = starsetf(%s),]], indent, key, arg) + else + return string.format([[%s%s = function() vim.fn["%s"](%s) end,]], indent, key, func, arg) + end + elseif string.find(arg, "%(") then + -- Function argument + return string.format([[%s%s = function() vim.fn["%s"](vim.fn.%s) end,]], indent, key, func, arg) + else + assert(false, arg) + end + end +end + +do + local lines = {} + local start = false + for line in io.lines(filetype_lua) do + if line:match("^%s+-- END [A-Z]+$") then + start = false + end + + if not start then + table.insert(lines, line) + end + + local indent, section = line:match("^(%s+)-- BEGIN ([A-Z]+)$") + if section then + start = true + local t = sections[string.lower(section)] + + local sorted = {} + for k, v in pairs(t.str) do + table.insert(sorted, {[k] = v}) + end + + table.sort(sorted, function(a, b) + return a[next(a)] < b[next(b)] + end) + + for _, v in ipairs(sorted) do + local k = next(v) + table.insert(lines, add_item(indent, k, v[k])) + end + + sorted = {} + for k, v in pairs(t.func) do + table.insert(sorted, {[k] = v}) + end + + table.sort(sorted, function(a, b) + return next(a) < next(b) + end) + + for _, v in ipairs(sorted) do + local k = next(v) + table.insert(lines, add_item(indent, k, v[k])) + end + end + end + local f = io.open(filetype_lua, "w") + f:write(table.concat(lines, "\n") .. "\n") + f:close() +end diff --git a/scripts/gen_vimdoc.py b/scripts/gen_vimdoc.py index 89fc14121e..36768953f9 100755 --- a/scripts/gen_vimdoc.py +++ b/scripts/gen_vimdoc.py @@ -52,7 +52,7 @@ import logging from xml.dom import minidom -MIN_PYTHON_VERSION = (3, 5) +MIN_PYTHON_VERSION = (3, 6) if sys.version_info < MIN_PYTHON_VERSION: print("requires Python {}.{}+".format(*MIN_PYTHON_VERSION)) @@ -96,7 +96,6 @@ CONFIG = { 'win_config.c', 'tabpage.c', 'ui.c', - 'extmark.c', ], # List of files/directories for doxygen to read, separated by blanks 'files': os.path.join(base_dir, 'src/nvim/api'), @@ -128,12 +127,16 @@ CONFIG = { 'shared.lua', 'uri.lua', 'ui.lua', + 'filetype.lua', + 'keymap.lua', ], 'files': ' '.join([ os.path.join(base_dir, 'src/nvim/lua/vim.lua'), os.path.join(base_dir, 'runtime/lua/vim/shared.lua'), os.path.join(base_dir, 'runtime/lua/vim/uri.lua'), os.path.join(base_dir, 'runtime/lua/vim/ui.lua'), + os.path.join(base_dir, 'runtime/lua/vim/filetype.lua'), + os.path.join(base_dir, 'runtime/lua/vim/keymap.lua'), ]), 'file_patterns': '*.lua', 'fn_name_prefix': '', @@ -148,6 +151,8 @@ CONFIG = { 'shared': 'vim', 'uri': 'vim', 'ui': 'vim.ui', + 'filetype': 'vim.filetype', + 'keymap': 'vim.keymap', }, 'append_only': [ 'shared.lua', diff --git a/scripts/lintcommit.lua b/scripts/lintcommit.lua index 0a7da4d4ef..6871858a0b 100644 --- a/scripts/lintcommit.lua +++ b/scripts/lintcommit.lua @@ -94,10 +94,24 @@ local function validate_commit(commit_message) return [[Description ends with a period (".").]] end - -- Check that description has exactly one whitespace after colon, followed by - -- a lowercase letter and then any number of letters. - if not string.match(after_colon, '^ %l%a*') then - return [[There should be one whitespace after the colon and the first letter should lowercase.]] + -- Check that description starts with a whitespace. + if after_colon:sub(1,1) ~= " " then + return [[There should be a whitespace after the colon.]] + end + + -- Check that description doesn't start with multiple whitespaces. + if after_colon:sub(1,2) == " " then + return [[There should only be one whitespace after the colon.]] + end + + -- Check that first character after space isn't uppercase. + if string.match(after_colon:sub(2,2), '%u') then + return [[First character should not be uppercase.]] + end + + -- Check that description isn't just whitespaces + if vim.trim(after_colon) == "" then + return [[Description shouldn't be empty.]] end return nil @@ -168,12 +182,14 @@ function M._test() ['ci(tui)!: message with scope and breaking change'] = true, ['vim-patch:8.2.3374: Pyret files are not recognized (#15642)'] = true, ['vim-patch:8.1.1195,8.2.{3417,3419}'] = true, + ['revert: "ci: use continue-on-error instead of "|| true""'] = true, [':no type before colon 1'] = false, [' :no type before colon 2'] = false, [' :no type before colon 3'] = false, ['ci(empty description):'] = false, - ['ci(whitespace as description): '] = false, + ['ci(only whitespace as description): '] = false, ['docs(multiple whitespaces as description): '] = false, + ['revert(multiple whitespaces and then characters as description): description'] = false, ['ci no colon after type'] = false, ['test: extra space after colon'] = false, ['ci: tab after colon'] = false, diff --git a/scripts/vim-patch.sh b/scripts/vim-patch.sh index 1c265f0f40..67a2cc96fd 100755 --- a/scripts/vim-patch.sh +++ b/scripts/vim-patch.sh @@ -576,13 +576,13 @@ show_vimpatches() { runtime_commits[$commit]=1 done - while read -r vim_commit; do + list_missing_vimpatches 1 "$@" | while read -r vim_commit; do if [[ "${runtime_commits[$vim_commit]-}" ]]; then printf ' • %s (+runtime)\n' "${vim_commit}" else printf ' • %s\n' "${vim_commit}" fi - done <<< "$(list_missing_vimpatches 1 "$@")" + done cat << EOF diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 9c4b778169..dcc20194f0 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -62,6 +62,7 @@ set(LUA_SHARED_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/shared.lua) set(LUA_INSPECT_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/inspect.lua) set(LUA_F_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/F.lua) set(LUA_META_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/_meta.lua) +set(LUA_FILETYPE_MODULE_SOURCE ${PROJECT_SOURCE_DIR}/runtime/lua/vim/filetype.lua) set(CHAR_BLOB_GENERATOR ${GENERATOR_DIR}/gen_char_blob.lua) set(LINT_SUPPRESS_FILE ${PROJECT_BINARY_DIR}/errors.json) set(LINT_SUPPRESS_URL_BASE "https://raw.githubusercontent.com/neovim/doc/gh-pages/reports/clint") @@ -334,6 +335,7 @@ add_custom_command( ${LUA_INSPECT_MODULE_SOURCE} inspect_module ${LUA_F_MODULE_SOURCE} lua_F_module ${LUA_META_MODULE_SOURCE} lua_meta_module + ${LUA_FILETYPE_MODULE_SOURCE} lua_filetype_module DEPENDS ${CHAR_BLOB_GENERATOR} ${LUA_VIM_MODULE_SOURCE} @@ -341,6 +343,7 @@ add_custom_command( ${LUA_INSPECT_MODULE_SOURCE} ${LUA_F_MODULE_SOURCE} ${LUA_META_MODULE_SOURCE} + ${LUA_FILETYPE_MODULE_SOURCE} VERBATIM ) @@ -490,6 +493,9 @@ add_executable(nvim ${NVIM_GENERATED_FOR_SOURCES} ${NVIM_GENERATED_FOR_HEADERS} ${EXTERNAL_SOURCES} ${EXTERNAL_HEADERS}) target_link_libraries(nvim ${NVIM_EXEC_LINK_LIBRARIES}) install_helper(TARGETS nvim) +if(MSVC) + install(FILES $<TARGET_PDB_FILE:nvim> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL) +endif() set_property(TARGET nvim APPEND PROPERTY INCLUDE_DIRECTORIES ${LUA_PREFERRED_INCLUDE_DIRS}) diff --git a/src/nvim/api/deprecated.c b/src/nvim/api/deprecated.c index 76b699800e..18243fec2b 100644 --- a/src/nvim/api/deprecated.c +++ b/src/nvim/api/deprecated.c @@ -130,7 +130,7 @@ Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, A return 0; } - uint64_t ns_id = src2ns(&src_id); + uint32_t ns_id = src2ns(&src_id); int width; VirtText virt_text = parse_virt_text(chunks, err, &width); @@ -148,11 +148,12 @@ Integer nvim_buf_set_virtual_text(Buffer buffer, Integer src_id, Integer line, A return src_id; } - Decoration *decor = xcalloc(1, sizeof(*decor)); - decor->virt_text = virt_text; - decor->virt_text_width = width; + Decoration decor = DECORATION_INIT; + decor.virt_text = virt_text; + decor.virt_text_width = width; + decor.priority = 0; - extmark_set(buf, ns_id, NULL, (int)line, 0, -1, -1, decor, true, + extmark_set(buf, ns_id, NULL, (int)line, 0, -1, -1, &decor, true, false, kExtmarkNoUndo); return src_id; } diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index 742b953c2a..3a968f07ab 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -85,12 +85,12 @@ const char *describe_ns(NS ns_id) } // Is the Namespace in use? -static bool ns_initialized(uint64_t ns) +static bool ns_initialized(uint32_t ns) { if (ns < 1) { return false; } - return ns < (uint64_t)next_namespace_id; + return ns < (uint32_t)next_namespace_id; } @@ -106,22 +106,55 @@ static Array extmark_to_array(ExtmarkInfo extmark, bool id, bool add_dict) if (add_dict) { Dictionary dict = ARRAY_DICT_INIT; + PUT(dict, "right_gravity", BOOLEAN_OBJ(extmark.right_gravity)); + if (extmark.end_row >= 0) { PUT(dict, "end_row", INTEGER_OBJ(extmark.end_row)); PUT(dict, "end_col", INTEGER_OBJ(extmark.end_col)); + PUT(dict, "end_right_gravity", BOOLEAN_OBJ(extmark.end_right_gravity)); } - if (extmark.decor) { - Decoration *decor = extmark.decor; - if (decor->hl_id) { - String name = cstr_to_string((const char *)syn_id2name(decor->hl_id)); - PUT(dict, "hl_group", STRING_OBJ(name)); + Decoration *decor = &extmark.decor; + if (decor->hl_id) { + String name = cstr_to_string((const char *)syn_id2name(decor->hl_id)); + PUT(dict, "hl_group", STRING_OBJ(name)); + PUT(dict, "hl_eol", BOOLEAN_OBJ(decor->hl_eol)); + } + if (decor->hl_mode) { + PUT(dict, "hl_mode", STRING_OBJ(cstr_to_string(hl_mode_str[decor->hl_mode]))); + } + + if (kv_size(decor->virt_text)) { + Array chunks = ARRAY_DICT_INIT; + for (size_t i = 0; i < decor->virt_text.size; i++) { + Array chunk = ARRAY_DICT_INIT; + VirtTextChunk *vtc = &decor->virt_text.items[i]; + ADD(chunk, STRING_OBJ(cstr_to_string(vtc->text))); + if (vtc->hl_id > 0) { + ADD(chunk, + STRING_OBJ(cstr_to_string((const char *)syn_id2name(vtc->hl_id)))); + } + ADD(chunks, ARRAY_OBJ(chunk)); } - if (kv_size(decor->virt_text)) { + PUT(dict, "virt_text", ARRAY_OBJ(chunks)); + PUT(dict, "virt_text_hide", BOOLEAN_OBJ(decor->virt_text_hide)); + if (decor->virt_text_pos == kVTWinCol) { + PUT(dict, "virt_text_win_col", INTEGER_OBJ(decor->col)); + } + PUT(dict, "virt_text_pos", + STRING_OBJ(cstr_to_string(virt_text_pos_str[decor->virt_text_pos]))); + } + + if (kv_size(decor->virt_lines)) { + Array all_chunks = ARRAY_DICT_INIT; + bool virt_lines_leftcol = false; + for (size_t i = 0; i < decor->virt_lines.size; i++) { Array chunks = ARRAY_DICT_INIT; - for (size_t i = 0; i < decor->virt_text.size; i++) { + VirtText *vt = &decor->virt_lines.items[i].line; + virt_lines_leftcol = decor->virt_lines.items[i].left_col; + for (size_t j = 0; j < vt->size; j++) { Array chunk = ARRAY_DICT_INIT; - VirtTextChunk *vtc = &decor->virt_text.items[i]; + VirtTextChunk *vtc = &vt->items[j]; ADD(chunk, STRING_OBJ(cstr_to_string(vtc->text))); if (vtc->hl_id > 0) { ADD(chunk, @@ -129,9 +162,14 @@ static Array extmark_to_array(ExtmarkInfo extmark, bool id, bool add_dict) } ADD(chunks, ARRAY_OBJ(chunk)); } - PUT(dict, "virt_text", ARRAY_OBJ(chunks)); + ADD(all_chunks, ARRAY_OBJ(chunks)); } + PUT(dict, "virt_lines", ARRAY_OBJ(all_chunks)); + PUT(dict, "virt_lines_above", BOOLEAN_OBJ(decor->virt_lines_above)); + PUT(dict, "virt_lines_leftcol", BOOLEAN_OBJ(virt_lines_leftcol)); + } + if (decor->hl_id || kv_size(decor->virt_text)) { PUT(dict, "priority", INTEGER_OBJ(decor->priority)); } @@ -166,7 +204,7 @@ ArrayOf(Integer) nvim_buf_get_extmark_by_id(Buffer buffer, Integer ns_id, return rv; } - if (!ns_initialized((uint64_t)ns_id)) { + if (!ns_initialized((uint32_t)ns_id)) { api_set_error(err, kErrorTypeValidation, "Invalid ns_id"); return rv; } @@ -191,7 +229,7 @@ ArrayOf(Integer) nvim_buf_get_extmark_by_id(Buffer buffer, Integer ns_id, } - ExtmarkInfo extmark = extmark_from_id(buf, (uint64_t)ns_id, (uint64_t)id); + ExtmarkInfo extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id); if (extmark.row < 0) { return rv; } @@ -252,7 +290,7 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e return rv; } - if (!ns_initialized((uint64_t)ns_id)) { + if (!ns_initialized((uint32_t)ns_id)) { api_set_error(err, kErrorTypeValidation, "Invalid ns_id"); return rv; } @@ -310,7 +348,7 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e } - ExtmarkInfoArray marks = extmark_get(buf, (uint64_t)ns_id, l_row, l_col, + ExtmarkInfoArray marks = extmark_get(buf, (uint32_t)ns_id, l_row, l_col, u_row, u_col, (int64_t)limit, reverse); for (size_t i = 0; i < kv_size(marks); i++) { @@ -404,6 +442,10 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e /// for left). Defaults to false. /// - priority: a priority value for the highlight group. For /// example treesitter highlighting uses a value of 100. +/// - strict: boolean that indicates extmark should not be placed +/// if the line or column value is past the end of the +/// buffer or end of the line respectively. Defaults to true. +/// /// @param[out] err Error details, if any /// @return Id of the created/updated extmark Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer col, @@ -417,14 +459,14 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer goto error; } - if (!ns_initialized((uint64_t)ns_id)) { + if (!ns_initialized((uint32_t)ns_id)) { api_set_error(err, kErrorTypeValidation, "Invalid ns_id"); goto error; } - uint64_t id = 0; + uint32_t id = 0; if (opts->id.type == kObjectTypeInteger && opts->id.data.integer > 0) { - id = (uint64_t)opts->id.data.integer; + id = (uint32_t)opts->id.data.integer; } else if (HAS_KEY(opts->id)) { api_set_error(err, kErrorTypeValidation, "id is not a positive integer"); goto error; @@ -441,9 +483,18 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer opts->end_row = opts->end_line; } +#define OPTION_TO_BOOL(target, name, val) \ + target = api_object_to_bool(opts->name, #name, val, err); \ + if (ERROR_SET(err)) { \ + goto error; \ + } + + bool strict = true; + OPTION_TO_BOOL(strict, strict, true); + if (opts->end_row.type == kObjectTypeInteger) { Integer val = opts->end_row.data.integer; - if (val < 0 || val > buf->b_ml.ml_line_count) { + if (val < 0 || (val > buf->b_ml.ml_line_count && strict)) { api_set_error(err, kErrorTypeValidation, "end_row value outside range"); goto error; } else { @@ -512,12 +563,6 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer goto error; } -#define OPTION_TO_BOOL(target, name, val) \ - target = api_object_to_bool(opts->name, #name, val, err); \ - if (ERROR_SET(err)) { \ - goto error; \ - } - OPTION_TO_BOOL(decor.virt_text_hide, virt_text_hide, false); OPTION_TO_BOOL(decor.hl_eol, hl_eol, false); @@ -596,16 +641,30 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer bool ephemeral = false; OPTION_TO_BOOL(ephemeral, ephemeral, false); - if (line < 0 || line > buf->b_ml.ml_line_count) { + if (line < 0) { api_set_error(err, kErrorTypeValidation, "line value outside range"); goto error; + } else if (line > buf->b_ml.ml_line_count) { + if (strict) { + api_set_error(err, kErrorTypeValidation, "line value outside range"); + goto error; + } else { + line = buf->b_ml.ml_line_count; + } } else if (line < buf->b_ml.ml_line_count) { len = ephemeral ? MAXCOL : STRLEN(ml_get_buf(buf, (linenr_T)line+1, false)); } if (col == -1) { col = (Integer)len; - } else if (col < -1 || col > (Integer)len) { + } else if (col > (Integer)len) { + if (strict) { + api_set_error(err, kErrorTypeValidation, "col value outside range"); + goto error; + } else { + col = (Integer)len; + } + } else if (col < -1) { api_set_error(err, kErrorTypeValidation, "col value outside range"); goto error; } @@ -621,27 +680,17 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer line2 = (int)line; } if (col2 > (Integer)len) { - api_set_error(err, kErrorTypeValidation, "end_col value outside range"); - goto error; + if (strict) { + api_set_error(err, kErrorTypeValidation, "end_col value outside range"); + goto error; + } else { + col2 = (int)len; + } } } else if (line2 >= 0) { col2 = 0; } - Decoration *d = NULL; - - if (ephemeral) { - d = &decor; - } else if (kv_size(decor.virt_text) || kv_size(decor.virt_lines) - || decor.priority != DECOR_PRIORITY_BASE - || decor.hl_eol) { - // TODO(bfredl): this is a bit sketchy. eventually we should - // have predefined decorations for both marks/ephemerals - d = xcalloc(1, sizeof(*d)); - *d = decor; - } else if (decor.hl_id) { - d = decor_hl(decor.hl_id); - } // TODO(bfredl): synergize these two branches even more if (ephemeral && decor_state.buf == buf) { @@ -652,12 +701,8 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer goto error; } - extmark_set(buf, (uint64_t)ns_id, &id, (int)line, (colnr_T)col, line2, col2, - d, right_gravity, end_right_gravity, kExtmarkNoUndo); - - if (kv_size(decor.virt_lines)) { - redraw_buf_line_later(buf, MIN(buf->b_ml.ml_line_count, line+1+(decor.virt_lines_above?0:1))); - } + extmark_set(buf, (uint32_t)ns_id, &id, (int)line, (colnr_T)col, line2, col2, + &decor, right_gravity, end_right_gravity, kExtmarkNoUndo); } return (Integer)id; @@ -682,23 +727,23 @@ Boolean nvim_buf_del_extmark(Buffer buffer, Integer ns_id, Integer id, Error *er if (!buf) { return false; } - if (!ns_initialized((uint64_t)ns_id)) { + if (!ns_initialized((uint32_t)ns_id)) { api_set_error(err, kErrorTypeValidation, "Invalid ns_id"); return false; } - return extmark_del(buf, (uint64_t)ns_id, (uint64_t)id); + return extmark_del(buf, (uint32_t)ns_id, (uint32_t)id); } -uint64_t src2ns(Integer *src_id) +uint32_t src2ns(Integer *src_id) { if (*src_id == 0) { *src_id = nvim_create_namespace((String)STRING_INIT); } if (*src_id < 0) { - return UINT64_MAX; + return (((uint32_t)1) << 31) - 1; } else { - return (uint64_t)(*src_id); + return (uint32_t)(*src_id); } } @@ -753,7 +798,7 @@ Integer nvim_buf_add_highlight(Buffer buffer, Integer ns_id, String hl_group, In col_end = MAXCOL; } - uint64_t ns = src2ns(&ns_id); + uint32_t ns = src2ns(&ns_id); if (!(line < buf->b_ml.ml_line_count)) { // safety check, we can't add marks outside the range @@ -773,10 +818,13 @@ Integer nvim_buf_add_highlight(Buffer buffer, Integer ns_id, String hl_group, In end_line++; } + Decoration decor = DECORATION_INIT; + decor.hl_id = hl_id; + extmark_set(buf, ns, NULL, (int)line, (colnr_T)col_start, end_line, (colnr_T)col_end, - decor_hl(hl_id), true, false, kExtmarkNoUndo); + &decor, true, false, kExtmarkNoUndo); return ns_id; } @@ -808,7 +856,7 @@ void nvim_buf_clear_namespace(Buffer buffer, Integer ns_id, Integer line_start, if (line_end < 0 || line_end > MAXLNUM) { line_end = MAXLNUM; } - extmark_clear(buf, (ns_id < 0 ? 0 : (uint64_t)ns_id), + extmark_clear(buf, (ns_id < 0 ? 0 : (uint32_t)ns_id), (int)line_start, 0, (int)line_end-1, MAXCOL); } diff --git a/src/nvim/api/keysets.lua b/src/nvim/api/keysets.lua index 97ee885ff6..f6dce1905e 100644 --- a/src/nvim/api/keysets.lua +++ b/src/nvim/api/keysets.lua @@ -21,6 +21,7 @@ return { "virt_lines"; "virt_lines_above"; "virt_lines_leftcol"; + "strict"; }; keymap = { "noremap"; @@ -43,6 +44,7 @@ return { "count"; "desc"; "force"; + "keepscript"; "nargs"; "range"; "register"; @@ -76,5 +78,36 @@ return { option = { "scope"; }; + highlight = { + "bold"; + "standout"; + "strikethrough"; + "underline"; + "undercurl"; + "italic"; + "reverse"; + "nocombine"; + "default"; + "global"; + "cterm"; + "foreground"; "fg"; + "background"; "bg"; + "ctermfg"; + "ctermbg"; + "special"; "sp"; + "link"; + "fallback"; + "temp"; + }; + highlight_cterm = { + "bold"; + "standout"; + "strikethrough"; + "underline"; + "undercurl"; + "italic"; + "reverse"; + "nocombine"; + }; } diff --git a/src/nvim/api/private/converter.c b/src/nvim/api/private/converter.c index 36da6c13a9..3d4ff202fe 100644 --- a/src/nvim/api/private/converter.c +++ b/src/nvim/api/private/converter.c @@ -10,6 +10,9 @@ #include "nvim/api/private/helpers.h" #include "nvim/assert.h" #include "nvim/eval/typval.h" +#include "nvim/eval/userfunc.h" +#include "nvim/lua/converter.h" +#include "nvim/lua/executor.h" /// Helper structure for vim_to_object typedef struct { @@ -228,6 +231,14 @@ static inline void typval_encode_dict_end(EncodedData *const edata) /// @return The converted value Object vim_to_object(typval_T *obj) { + if (obj->v_type == VAR_FUNC) { + ufunc_T *fp = find_func(obj->vval.v_string); + assert(fp != NULL); + if (fp->uf_cb == nlua_CFunction_func_call) { + LuaRef ref = api_new_luaref(((LuaCFunctionState *)fp->uf_cb_state)->lua_callable.func_ref); + return LUAREF_OBJ(ref); + } + } EncodedData edata; kvi_init(edata.stack); const int evo_ret = encode_vim_to_object(&edata, obj, @@ -340,6 +351,16 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err) tv->vval.v_dict = dict; break; } + + case kObjectTypeLuaRef: { + LuaCFunctionState *state = xmalloc(sizeof(LuaCFunctionState)); + state->lua_callable.func_ref = api_new_luaref(obj.data.luaref); + char_u *name = register_cfunc(&nlua_CFunction_func_call, &nlua_CFunction_func_free, state); + tv->v_type = VAR_FUNC; + tv->vval.v_string = vim_strsave(name); + break; + } + default: abort(); } diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 4884a44cea..ddcfff0097 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -139,10 +139,10 @@ bool try_end(Error *err) got_int = false; } else if (msg_list != NULL && *msg_list != NULL) { int should_free; - char *msg = (char *)get_exception_string(*msg_list, - ET_ERROR, - NULL, - &should_free); + char *msg = get_exception_string(*msg_list, + ET_ERROR, + NULL, + &should_free); api_set_error(err, kErrorTypeException, "%s", msg); free_global_msglist(); @@ -720,7 +720,6 @@ fail_and_free: xfree(parsed_args.rhs); xfree(parsed_args.orig_rhs); XFREE_CLEAR(parsed_args.desc); - return; } /// Collects `n` buffer lines into array `l`, optionally replacing newlines @@ -990,18 +989,16 @@ Object copy_object(Object obj) static void set_option_value_for(char *key, int numval, char *stringval, int opt_flags, int opt_type, void *from, Error *err) { - win_T *save_curwin = NULL; - tabpage_T *save_curtab = NULL; + switchwin_T switchwin; aco_save_T aco; try_start(); switch (opt_type) { case SREQ_WIN: - if (switch_win_noblock(&save_curwin, &save_curtab, (win_T *)from, - win_find_tabpage((win_T *)from), true) + if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true) == FAIL) { - restore_win_noblock(save_curwin, save_curtab, true); + restore_win_noblock(&switchwin, true); if (try_end(err)) { return; } @@ -1011,7 +1008,7 @@ static void set_option_value_for(char *key, int numval, char *stringval, int opt return; } set_option_value_err(key, numval, stringval, opt_flags, err); - restore_win_noblock(save_curwin, save_curtab, true); + restore_win_noblock(&switchwin, true); break; case SREQ_BUF: aucmd_prepbuf(&aco, (buf_T *)from); @@ -1141,7 +1138,7 @@ bool extmark_get_index_from_obj(buf_T *buf, Integer ns_id, Object obj, int return false; } - ExtmarkInfo extmark = extmark_from_id(buf, (uint64_t)ns_id, (uint64_t)id); + ExtmarkInfo extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id); if (extmark.row >= 0) { *row = extmark.row; *col = extmark.col; @@ -1387,6 +1384,11 @@ void add_user_command(String name, Object command, Dict(user_command) *opts, int LuaRef luaref = LUA_NOREF; LuaRef compl_luaref = LUA_NOREF; + if (mb_islower(name.data[0])) { + api_set_error(err, kErrorTypeValidation, "'name' must begin with an uppercase letter"); + goto err; + } + if (HAS_KEY(opts->range) && HAS_KEY(opts->count)) { api_set_error(err, kErrorTypeValidation, "'range' and 'count' are mutually exclusive"); goto err; @@ -1505,7 +1507,13 @@ void add_user_command(String name, Object command, Dict(user_command) *opts, int goto err; } - bool force = api_object_to_bool(opts->force, "force", false, err); + if (api_object_to_bool(opts->keepscript, "keepscript", false, err)) { + argt |= EX_KEEPSCRIPT; + } else if (ERROR_SET(err)) { + goto err; + } + + bool force = api_object_to_bool(opts->force, "force", true, err); if (ERROR_SET(err)) { goto err; } diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 693d2083e6..f4909b0801 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -19,6 +19,7 @@ #include "nvim/ascii.h" #include "nvim/buffer.h" #include "nvim/buffer_defs.h" +#include "nvim/charset.h" #include "nvim/context.h" #include "nvim/decoration.h" #include "nvim/edit.h" @@ -30,7 +31,9 @@ #include "nvim/file_search.h" #include "nvim/fileio.h" #include "nvim/getchar.h" +#include "nvim/globals.h" #include "nvim/highlight.h" +#include "nvim/highlight_defs.h" #include "nvim/lua/executor.h" #include "nvim/mark.h" #include "nvim/memline.h" @@ -121,7 +124,9 @@ Dictionary nvim__get_hl_defs(Integer ns_id, Error *err) /// Set a highlight group. /// -/// @param ns_id number of namespace for this highlight +/// @param ns_id number of namespace for this highlight. Use value 0 +/// to set a highlight group in the global (`:highlight`) +/// namespace. /// @param name highlight group name, like ErrorMsg /// @param val highlight definition map, like |nvim_get_hl_by_name|. /// in addition the following keys are also recognized: @@ -135,9 +140,8 @@ Dictionary nvim__get_hl_defs(Integer ns_id, Error *err) /// same as attributes of gui color /// @param[out] err Error details, if any /// -/// TODO: ns_id = 0, should modify :highlight namespace -/// TODO val should take update vs reset flag -void nvim_set_hl(Integer ns_id, String name, Dictionary val, Error *err) +// TODO(bfredl): val should take update vs reset flag +void nvim_set_hl(Integer ns_id, String name, Dict(highlight) *val, Error *err) FUNC_API_SINCE(7) { int hl_id = syn_check_group(name.data, (int)name.size); @@ -145,7 +149,7 @@ void nvim_set_hl(Integer ns_id, String name, Dictionary val, Error *err) HlAttrs attrs = dict2hlattrs(val, true, &link_id, err); if (!ERROR_SET(err)) { - ns_hl_def((NS)ns_id, hl_id, attrs, link_id); + ns_hl_def((NS)ns_id, hl_id, attrs, link_id, val); } } @@ -187,21 +191,23 @@ static void on_redraw_event(void **argv) /// On execution error: does not fail, but updates v:errmsg. /// /// To input sequences like <C-o> use |nvim_replace_termcodes()| (typically -/// with escape_csi=true) to replace |keycodes|, then pass the result to +/// with escape_ks=false) to replace |keycodes|, then pass the result to /// nvim_feedkeys(). /// /// Example: /// <pre> /// :let key = nvim_replace_termcodes("<C-o>", v:true, v:false, v:true) -/// :call nvim_feedkeys(key, 'n', v:true) +/// :call nvim_feedkeys(key, 'n', v:false) /// </pre> /// /// @param keys to be typed /// @param mode behavior flags, see |feedkeys()| -/// @param escape_csi If true, escape K_SPECIAL/CSI bytes in `keys` +/// @param escape_ks If true, escape K_SPECIAL bytes in `keys` +/// This should be false if you already used +/// |nvim_replace_termcodes()|, and true otherwise. /// @see feedkeys() -/// @see vim_strsave_escape_csi -void nvim_feedkeys(String keys, String mode, Boolean escape_csi) +/// @see vim_strsave_escape_ks +void nvim_feedkeys(String keys, String mode, Boolean escape_ks) FUNC_API_SINCE(1) { bool remap = true; @@ -232,10 +238,10 @@ void nvim_feedkeys(String keys, String mode, Boolean escape_csi) } char *keys_esc; - if (escape_csi) { - // Need to escape K_SPECIAL and CSI before putting the string in the + if (escape_ks) { + // Need to escape K_SPECIAL before putting the string in the // typeahead buffer. - keys_esc = (char *)vim_strsave_escape_csi((char_u *)keys.data); + keys_esc = (char *)vim_strsave_escape_ks((char_u *)keys.data); } else { keys_esc = keys.data; } @@ -245,7 +251,7 @@ void nvim_feedkeys(String keys, String mode, Boolean escape_csi) typebuf_was_filled = true; } - if (escape_csi) { + if (escape_ks) { xfree(keys_esc); } @@ -383,7 +389,7 @@ error: /// @param str String to be converted. /// @param from_part Legacy Vim parameter. Usually true. /// @param do_lt Also translate <lt>. Ignored if `special` is false. -/// @param special Replace |keycodes|, e.g. <CR> becomes a "\n" char. +/// @param special Replace |keycodes|, e.g. <CR> becomes a "\r" char. /// @see replace_termcodes /// @see cpoptions String nvim_replace_termcodes(String str, Boolean from_part, Boolean do_lt, Boolean special) @@ -540,20 +546,19 @@ void nvim_set_current_dir(String dir, Error *err) return; } - char string[MAXPATHL]; + char_u string[MAXPATHL]; memcpy(string, dir.data, dir.size); string[dir.size] = NUL; try_start(); - if (vim_chdir((char_u *)string)) { + if (!changedir_func(string, kCdScopeGlobal)) { if (!try_end(err)) { api_set_error(err, kErrorTypeException, "Failed to change directory"); } return; } - post_chdir(kCdScopeGlobal, true); try_end(err); } @@ -596,7 +601,19 @@ void nvim_del_current_line(Error *err) Object nvim_get_var(String name, Error *err) FUNC_API_SINCE(1) { - return dict_get_value(&globvardict, name, err); + dictitem_T *di = tv_dict_find(&globvardict, name.data, (ptrdiff_t)name.size); + if (di == NULL) { // try to autoload script + if (!script_autoload(name.data, name.size, false) || aborting()) { + api_set_error(err, kErrorTypeValidation, "Key not found: %s", name.data); + return (Object)OBJECT_INIT; + } + di = tv_dict_find(&globvardict, name.data, (ptrdiff_t)name.size); + } + if (di == NULL) { + api_set_error(err, kErrorTypeValidation, "Key not found: %s", name.data); + return (Object)OBJECT_INIT; + } + return vim_to_object(&di->di_tv); } /// Sets a global (g:) variable. @@ -1163,7 +1180,7 @@ static void term_close(void *data) /// Send data to channel `id`. For a job, it writes it to the /// stdin of the process. For the stdio channel |channel-stdio|, /// it writes to Nvim's stdout. For an internal terminal instance -/// (|nvim_open_term()|) it writes directly to terimal output. +/// (|nvim_open_term()|) it writes directly to terminal output. /// See |channel-bytes| for more information. /// /// This function writes raw data, not RPC messages. If the channel @@ -2234,7 +2251,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error * Dictionary result = ARRAY_DICT_INIT; int maxwidth; - char fillchar = 0; + int fillchar = 0; Window window = 0; bool use_tabline = false; bool highlights = false; @@ -2249,12 +2266,12 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error * } if (HAS_KEY(opts->fillchar)) { - if (opts->fillchar.type != kObjectTypeString || opts->fillchar.data.string.size > 1) { - api_set_error(err, kErrorTypeValidation, "fillchar must be an ASCII character"); + if (opts->fillchar.type != kObjectTypeString || opts->fillchar.data.string.size == 0 + || char2cells(fillchar = utf_ptr2char((char_u *)opts->fillchar.data.string.data)) != 1 + || (size_t)utf_char2len(fillchar) != opts->fillchar.data.string.size) { + api_set_error(err, kErrorTypeValidation, "fillchar must be a single-width character"); return result; } - - fillchar = opts->fillchar.data.string.data[0]; } if (HAS_KEY(opts->highlights)) { @@ -2281,11 +2298,16 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error * fillchar = ' '; } else { wp = find_window_by_handle(window, err); + + if (wp == NULL) { + api_set_error(err, kErrorTypeException, "unknown winid %d", window); + return result; + } ewp = wp; if (fillchar == 0) { int attr; - fillchar = (char)fillchar_status(&attr, wp); + fillchar = fillchar_status(&attr, wp); } } @@ -2313,7 +2335,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error * sizeof(buf), (char_u *)str.data, false, - (char_u)fillchar, + fillchar, maxwidth, hltab_ptr, NULL); @@ -2394,11 +2416,12 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error * /// - mods: (string) Command modifiers, if any |<mods>| /// @param opts Optional command attributes. See |command-attributes| for more details. To use /// boolean attributes (such as |:command-bang| or |:command-bar|) set the value to -/// "true". When using a Lua function for {command} you can also provide a "desc" -/// key that will be displayed when listing commands. In addition to the string -/// options listed in |:command-complete|, the "complete" key also accepts a Lua -/// function which works like the "customlist" completion mode -/// |:command-complete-customlist|. +/// "true". In addition to the string options listed in |:command-complete|, the +/// "complete" key also accepts a Lua function which works like the "customlist" +/// completion mode |:command-completion-customlist|. Additional parameters: +/// - desc: (string) Used for listing the command when a Lua function is used for +/// {command}. +/// - force: (boolean, default true) Override any previous definition. /// @param[out] err Error details, if any. void nvim_add_user_command(String name, Object command, Dict(user_command) *opts, Error *err) FUNC_API_SINCE(9) diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 907306da7b..fc7823a070 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -395,7 +395,7 @@ void nvim_win_hide(Window window, Error *err) TryState tstate; try_enter(&tstate); if (tabpage == curtab) { - win_close(win, false); + win_close(win, false, false); } else { win_close_othertab(win, false, tabpage); } @@ -455,17 +455,12 @@ Object nvim_win_call(Window window, LuaRef fun, Error *err) } tabpage_T *tabpage = win_find_tabpage(win); - win_T *save_curwin; - tabpage_T *save_curtab; - try_start(); Object res = OBJECT_INIT; - if (switch_win_noblock(&save_curwin, &save_curtab, win, tabpage, true) == - OK) { + WIN_EXECUTE(win, tabpage, { Array args = ARRAY_DICT_INIT; res = nlua_call_ref(fun, NULL, args, true, err); - } - restore_win_noblock(save_curwin, save_curtab, true); + }); try_end(err); return res; } diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 463bd5e0e6..dfba18b11d 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -285,7 +285,7 @@ void aubuflocal_remove(buf_T *buf) } // Add an autocmd group name. -// Return its ID. Returns AUGROUP_ERROR (< 0) for error. +// Return its ID. static int au_new_group(char_u *name) { int i = au_find_group(name); @@ -379,10 +379,7 @@ void do_augroup(char_u *arg, int del_group) } else if (STRICMP(arg, "end") == 0) { // ":aug end": back to group 0 current_augroup = AUGROUP_DEFAULT; } else if (*arg) { // ":aug xxx": switch to group xxx - int i = au_new_group(arg); - if (i != AUGROUP_ERROR) { - current_augroup = i; - } + current_augroup = au_new_group(arg); } else { // ":aug": list the group names msg_start(); for (int i = 0; i < augroups.ga_len; i++) { @@ -1069,8 +1066,6 @@ void ex_doautoall(exarg_T *eap) do_modelines(0); } } - - check_cursor(); // just in case lines got deleted } /// Check *argp for <nomodeline>. When it is present return false, otherwise @@ -1160,7 +1155,10 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) // Prevent chdir() call in win_enter_ext(), through do_autochdir() int save_acd = p_acd; p_acd = false; + // no redrawing and don't set the window title + RedrawingDisabled++; win_enter(aucmd_win, false); + RedrawingDisabled--; p_acd = save_acd; unblock_autocmds(); curwin = aucmd_win; @@ -1168,6 +1166,10 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) curbuf = buf; aco->new_curwin_handle = curwin->handle; set_bufref(&aco->new_curbuf, curbuf); + + // disable the Visual area, the position may be invalid in another buffer + aco->save_VIsual_active = VIsual_active; + VIsual_active = false; } /// Cleanup after executing autocommands for a (hidden) buffer. @@ -1264,6 +1266,12 @@ win_found: check_cursor(); } } + + check_cursor(); // just in case lines got deleted + VIsual_active = aco->save_VIsual_active; + if (VIsual_active) { + check_pos(curbuf, &VIsual); + } } /// Execute autocommands for "event" and file name "fname". diff --git a/src/nvim/autocmd.h b/src/nvim/autocmd.h index ac12e2acf3..63c5abd4f8 100644 --- a/src/nvim/autocmd.h +++ b/src/nvim/autocmd.h @@ -14,6 +14,7 @@ typedef struct { handle_T save_prevwin_handle; ///< ID of saved prevwin bufref_T new_curbuf; ///< new curbuf char_u *globaldir; ///< saved value of globaldir + bool save_VIsual_active; ///< saved VIsual_active } aco_save_T; typedef struct AutoCmd { diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index abd22fba26..38b045b31c 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -846,7 +846,7 @@ void goto_buffer(exarg_T *eap, int start, int dir, int count) enter_cleanup(&cs); // Quitting means closing the split window, nothing else. - win_close(curwin, true); + win_close(curwin, true, false); swap_exists_action = SEA_NONE; swap_exists_did_quit = true; @@ -1237,7 +1237,7 @@ int do_buffer(int action, int start, int dir, int count, int forceit) while (buf == curbuf && !(curwin->w_closing || curwin->w_buffer->b_locked > 0) && (!ONE_WINDOW || first_tabpage->tp_next != NULL)) { - if (win_close(curwin, false) == FAIL) { + if (win_close(curwin, false, false) == FAIL) { break; } } @@ -1454,7 +1454,10 @@ void set_curbuf(buf_T *buf, int action) } if (bufref_valid(&prevbufref) && !aborting()) { win_T *previouswin = curwin; - if (prevbuf == curbuf) { + // Do not sync when in Insert mode and the buffer is open in + // another window, might be a timer doing something in another + // window. + if (prevbuf == curbuf && ((State & INSERT) == 0 || curbuf->b_nwindows <= 1)) { u_sync(false); } close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, @@ -1494,6 +1497,11 @@ void set_curbuf(buf_T *buf, int action) */ void enter_buffer(buf_T *buf) { + // Get the buffer in the current window. + curwin->w_buffer = buf; + curbuf = buf; + curbuf->b_nwindows++; + // Copy buffer and window local option values. Not for a help buffer. buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); if (!buf->b_help) { @@ -1504,11 +1512,6 @@ void enter_buffer(buf_T *buf) } foldUpdateAll(curwin); // update folds (later). - // Get the buffer in the current window. - curwin->w_buffer = buf; - curbuf = buf; - curbuf->b_nwindows++; - if (curwin->w_p_diff) { diff_buf_add(curbuf); } @@ -1896,10 +1899,8 @@ void free_buf_options(buf_T *buf, int free_p_ff) clear_string_option(&buf->b_p_flp); clear_string_option(&buf->b_p_isk); clear_string_option(&buf->b_p_vsts); - xfree(buf->b_p_vsts_nopaste); - buf->b_p_vsts_nopaste = NULL; - xfree(buf->b_p_vsts_array); - buf->b_p_vsts_array = NULL; + XFREE_CLEAR(buf->b_p_vsts_nopaste); + XFREE_CLEAR(buf->b_p_vsts_array); clear_string_option(&buf->b_p_vts); XFREE_CLEAR(buf->b_p_vts_array); clear_string_option(&buf->b_p_keymap); @@ -3326,7 +3327,7 @@ void maketitle(void) len = (int)STRLEN(buf_p); if (len > 100) { len -= 100; - len += (*mb_tail_off)(buf_p, buf_p + len) + 1; + len += mb_tail_off(buf_p, buf_p + len) + 1; buf_p += len; } STRCPY(icon_str, buf_p); @@ -3418,7 +3419,7 @@ typedef enum { /// /// @return The final width of the statusline int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use_sandbox, - char_u fillchar, int maxwidth, stl_hlrec_t **hltab, StlClickRecord **tabtab) + int fillchar, int maxwidth, stl_hlrec_t **hltab, StlClickRecord **tabtab) { static size_t stl_items_len = 20; // Initial value, grows as needed. static stl_item_t *stl_items = NULL; @@ -3461,9 +3462,6 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use if (fillchar == 0) { fillchar = ' '; - } else if (utf_char2len(fillchar) > 1) { - // Can't handle a multi-byte fill character yet. - fillchar = '-'; } // The cursor in windows other than the current one isn't always @@ -3661,7 +3659,7 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use out_p = out_p - n + 1; // Fill up space left over by half a double-wide char. while (++group_len < stl_items[stl_groupitems[groupdepth]].minwid) { - *out_p++ = fillchar; + MB_CHAR2BYTES(fillchar, out_p); } // } @@ -3684,14 +3682,14 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use if (min_group_width < 0) { min_group_width = 0 - min_group_width; while (group_len++ < min_group_width && out_p < out_end_p) { - *out_p++ = fillchar; + MB_CHAR2BYTES(fillchar, out_p); } // If the group is right-aligned, shift everything to the right and // prepend with filler characters. } else { // { Move the group to the right - memmove(t + min_group_width - group_len, t, (size_t)(out_p - t)); - group_len = min_group_width - group_len; + group_len = (min_group_width - group_len) * utf_char2len(fillchar); + memmove(t + group_len, t, (size_t)(out_p - t)); if (out_p + group_len >= (out_end_p + 1)) { group_len = (long)(out_end_p - out_p); } @@ -3705,7 +3703,7 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use // Prepend the fill characters for (; group_len > 0; group_len--) { - *t++ = fillchar; + MB_CHAR2BYTES(fillchar, t); } } } @@ -4001,14 +3999,7 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use case STL_VIRTCOL: case STL_VIRTCOL_ALT: { - // In list mode virtcol needs to be recomputed - colnr_T virtcol = wp->w_virtcol; - if (wp->w_p_list && wp->w_p_lcs_chars.tab1 == NUL) { - wp->w_p_list = false; - getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); - wp->w_p_list = true; - } - virtcol++; + colnr_T virtcol = wp->w_virtcol + 1; // Don't display %V if it's the same as %c. if (opt == STL_VIRTCOL_ALT && (virtcol == (colnr_T)(!(State & INSERT) && empty_line @@ -4237,7 +4228,7 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use if (l + 1 == minwid && fillchar == '-' && ascii_isdigit(*t)) { *out_p++ = ' '; } else { - *out_p++ = fillchar; + MB_CHAR2BYTES(fillchar, out_p); } } minwid = 0; @@ -4248,20 +4239,21 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use } // { Copy the string text into the output buffer - while (*t && out_p < out_end_p) { - *out_p++ = *t++; + for (; *t && out_p < out_end_p; t++) { // Change a space by fillchar, unless fillchar is '-' and a // digit follows. - if (fillable && out_p[-1] == ' ' - && (!ascii_isdigit(*t) || fillchar != '-')) { - out_p[-1] = fillchar; + if (fillable && *t == ' ' + && (!ascii_isdigit(*(t + 1)) || fillchar != '-')) { + MB_CHAR2BYTES(fillchar, out_p); + } else { + *out_p++ = *t; } } // } // For left-aligned items, fill any remaining space with the fillchar for (; l < minwid && out_p < out_end_p; l++) { - *out_p++ = fillchar; + MB_CHAR2BYTES(fillchar, out_p); } // Otherwise if the item is a number, copy that to the output buffer. @@ -4351,7 +4343,7 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use // Only free the string buffer if we allocated it. // Note: This is not needed if `str` is pointing at `tmp` if (opt == STL_VIM_EXPR) { - xfree(str); + XFREE_CLEAR(str); } if (num >= 0 || (!itemisflag && str && *str)) { @@ -4454,7 +4446,7 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use // Fill up for half a double-wide character. while (++width < maxwidth) { - *trunc_p++ = fillchar; + MB_CHAR2BYTES(fillchar, trunc_p); *trunc_p = NUL; } // } @@ -4505,13 +4497,13 @@ int build_stl_str_hl(win_T *wp, char_u *out, size_t outlen, char_u *fmt, int use standard_spaces * (num_separators - 1); for (int i = 0; i < num_separators; i++) { - int dislocation = (i == (num_separators - 1)) - ? final_spaces : standard_spaces; + int dislocation = (i == (num_separators - 1)) ? final_spaces : standard_spaces; + dislocation *= utf_char2len(fillchar); char_u *start = stl_items[stl_separator_locations[i]].start; char_u *seploc = start + dislocation; STRMOVE(seploc, start); - for (char_u *s = start; s < seploc; s++) { - *s = fillchar; + for (char_u *s = start; s < seploc; ) { + MB_CHAR2BYTES(fillchar, s); } for (int item_idx = stl_separator_locations[i] + 1; @@ -4830,7 +4822,7 @@ void do_arg_all(int count, int forceit, int keep_tabs) && (first_tabpage->tp_next == NULL || !had_tab)) { use_firstwin = true; } else { - win_close(wp, !buf_hide(buf) && !bufIsChanged(buf)); + win_close(wp, !buf_hide(buf) && !bufIsChanged(buf), false); // check if autocommands removed the next window if (!win_valid(wpnext)) { // start all over... @@ -5021,7 +5013,7 @@ void ex_buffer_all(exarg_T *eap) && !ONE_WINDOW && !(wp->w_closing || wp->w_buffer->b_locked > 0)) { - win_close(wp, false); + win_close(wp, false, false); wpnext = firstwin; // just in case an autocommand does // something strange with windows tpnext = first_tabpage; // start all over... @@ -5102,7 +5094,7 @@ void ex_buffer_all(exarg_T *eap) enter_cleanup(&cs); // User selected Quit at ATTENTION prompt; close this window. - win_close(curwin, true); + win_close(curwin, true, false); open_wins--; swap_exists_action = SEA_NONE; swap_exists_did_quit = true; @@ -5144,7 +5136,7 @@ void ex_buffer_all(exarg_T *eap) // BufWrite Autocommands made the window invalid, start over wp = lastwin; } else if (r) { - win_close(wp, !buf_hide(wp->w_buffer)); + win_close(wp, !buf_hide(wp->w_buffer), false); open_wins--; wp = lastwin; } else { @@ -5463,30 +5455,43 @@ bool find_win_for_buf(buf_T *buf, win_T **wp, tabpage_T **tp) return false; } -int buf_signcols(buf_T *buf) +static int buf_signcols_inner(buf_T *buf, int maximum) { - if (!buf->b_signcols_valid) { - sign_entry_T *sign; // a sign in the sign list - int signcols = 0; - int linesum = 0; - linenr_T curline = 0; - - FOR_ALL_SIGNS_IN_BUF(buf, sign) { - if (sign->se_lnum > curline) { - if (linesum > signcols) { - signcols = linesum; + sign_entry_T *sign; // a sign in the sign list + int signcols = 0; + int linesum = 0; + linenr_T curline = 0; + + FOR_ALL_SIGNS_IN_BUF(buf, sign) { + if (sign->se_lnum > curline) { + if (linesum > signcols) { + signcols = linesum; + if (signcols >= maximum) { + return maximum; } - curline = sign->se_lnum; - linesum = 0; - } - if (sign->se_has_text_or_icon) { - linesum++; } + curline = sign->se_lnum; + linesum = 0; + } + if (sign->se_has_text_or_icon) { + linesum++; } - if (linesum > signcols) { - signcols = linesum; + } + + if (linesum > signcols) { + signcols = linesum; + if (signcols >= maximum) { + return maximum; } + } + + return signcols; +} +int buf_signcols(buf_T *buf, int maximum) +{ + if (!buf->b_signcols_valid) { + int signcols = buf_signcols_inner(buf, maximum); // Check if we need to redraw if (signcols != buf->b_signcols) { buf->b_signcols = signcols; diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 63a550c017..7b17c5b506 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -204,6 +204,10 @@ typedef struct { #define w_p_nu w_onebuf_opt.wo_nu // 'number' int wo_rnu; #define w_p_rnu w_onebuf_opt.wo_rnu // 'relativenumber' + char_u *wo_ve; +#define w_p_ve w_onebuf_opt.wo_ve // 'virtualedit' + unsigned wo_ve_flags; +#define w_ve_flags w_onebuf_opt.wo_ve_flags // flags for 'virtualedit' long wo_nuw; #define w_p_nuw w_onebuf_opt.wo_nuw // 'numberwidth' int wo_wfh; @@ -866,8 +870,7 @@ struct file_buffer { int b_mapped_ctrl_c; // modes where CTRL-C is mapped MarkTree b_marktree[1]; - Map(uint64_t, ExtmarkItem) b_extmark_index[1]; - Map(uint64_t, ExtmarkNs) b_extmark_ns[1]; // extmark namespaces + Map(uint32_t, uint32_t) b_extmark_ns[1]; // extmark namespaces size_t b_virt_line_blocks; // number of virt_line blocks // array of channel_id:s which have asked to receive updates for this diff --git a/src/nvim/change.c b/src/nvim/change.c index 1dbbfff024..6ac759d5e0 100644 --- a/src/nvim/change.c +++ b/src/nvim/change.c @@ -789,7 +789,7 @@ int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine) // fixpos is true, we don't want to end up positioned at the NUL, // unless "restart_edit" is set or 'virtualedit' contains "onemore". if (col > 0 && fixpos && restart_edit == 0 - && (ve_flags & VE_ONEMORE) == 0) { + && (get_ve_flags() & VE_ONEMORE) == 0) { curwin->w_cursor.col--; curwin->w_cursor.coladd = 0; curwin->w_cursor.col -= utf_head_off(oldp, oldp + curwin->w_cursor.col); @@ -952,11 +952,13 @@ int copy_indent(int size, char_u *src) /// /// "second_line_indent": indent for after ^^D in Insert mode or if flag /// OPENLINE_COM_LIST +/// "did_do_comment" is set to true when intentionally putting the comment +/// leader in fromt of the new line. /// /// @param dir FORWARD or BACKWARD /// /// @return true on success, false on failure -int open_line(int dir, int flags, int second_line_indent) +int open_line(int dir, int flags, int second_line_indent, bool *did_do_comment) { char_u *next_line = NULL; // copy of the next line char_u *p_extra = NULL; // what goes to next line @@ -969,6 +971,7 @@ int open_line(int dir, int flags, int second_line_indent) bool retval = false; // return value int extra_len = 0; // length of p_extra string int lead_len; // length of comment leader + int comment_start = 0; // start index of the comment leader char_u *lead_flags; // position in 'comments' for comment leader char_u *leader = NULL; // copy of comment leader char_u *allocated = NULL; // allocated memory @@ -977,6 +980,7 @@ int open_line(int dir, int flags, int second_line_indent) pos_T *pos; bool do_si = (!p_paste && curbuf->b_p_si && !curbuf->b_p_cin && *curbuf->b_p_inde == NUL); + bool do_cindent; bool no_si = false; // reset did_si afterwards int first_char = NUL; // init for GCC int vreplace_mode; @@ -1189,11 +1193,30 @@ int open_line(int dir, int flags, int second_line_indent) did_ai = true; } + // May do indenting after opening a new line. + do_cindent = !p_paste && (curbuf->b_p_cin || *curbuf->b_p_inde != NUL) + && in_cinkeys(dir == FORWARD ? KEY_OPEN_FORW : KEY_OPEN_BACK, + ' ', linewhite(curwin->w_cursor.lnum)); + // Find out if the current line starts with a comment leader. // This may then be inserted in front of the new line. end_comment_pending = NUL; if (flags & OPENLINE_DO_COM) { lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, true); + if (lead_len == 0 && do_cindent && dir == FORWARD) { + // Check for a line comment after code. + comment_start = check_linecomment(saved_line); + if (comment_start != MAXCOL) { + lead_len = get_leader_len(saved_line + comment_start, + &lead_flags, false, true); + if (lead_len != 0) { + lead_len += comment_start; + if (did_do_comment != NULL) { + *did_do_comment = true; + } + } + } + } } else { lead_len = 0; } @@ -1349,6 +1372,13 @@ int open_line(int dir, int flags, int second_line_indent) STRLCPY(leader, saved_line, lead_len + 1); + // TODO(vim): handle multi-byte and double width chars + for (int li = 0; li < comment_start; li++) { + if (!ascii_iswhite(leader[li])) { + leader[li] = ' '; + } + } + // Replace leader with lead_repl, right or left adjusted if (lead_repl != NULL) { int c = 0; @@ -1758,13 +1788,7 @@ int open_line(int dir, int flags, int second_line_indent) ai_col = (colnr_T)getwhitecols_curline(); } // May do indenting after opening a new line. - if (!p_paste - && (curbuf->b_p_cin - || *curbuf->b_p_inde != NUL - ) - && in_cinkeys(dir == FORWARD - ? KEY_OPEN_FORW - : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum))) { + if (do_cindent) { do_c_expr_indent(); ai_col = (colnr_T)getwhitecols_curline(); } diff --git a/src/nvim/channel.c b/src/nvim/channel.c index cd5134fe5f..d79c0acc4a 100644 --- a/src/nvim/channel.c +++ b/src/nvim/channel.c @@ -613,7 +613,6 @@ static void on_channel_output(Stream *stream, Channel *chan, RBuffer *buf, size_ } else { if (chan->term) { terminal_receive(chan->term, ptr, count); - terminal_flush_output(chan->term); } rbuffer_consumed(buf, count); diff --git a/src/nvim/channel.h b/src/nvim/channel.h index 81b75e2d31..50d6b3600a 100644 --- a/src/nvim/channel.h +++ b/src/nvim/channel.h @@ -96,6 +96,8 @@ struct Channel { EXTERN PMap(uint64_t) channels INIT(= MAP_INIT); +EXTERN Callback on_print INIT(= CALLBACK_INIT); + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "channel.h.generated.h" #endif diff --git a/src/nvim/charset.c b/src/nvim/charset.c index 599d662993..583a040ed1 100644 --- a/src/nvim/charset.c +++ b/src/nvim/charset.c @@ -217,9 +217,7 @@ int buf_init_chartab(buf_T *buf, int global) } } else if (i == 1) { // (re)set printable - // For double-byte we keep the cell width, so - // that we can detect it from the first byte. - if (((c < ' ') || (c > '~'))) { + if (c < ' ' || c > '~') { if (tilde) { g_chartab[c] = (uint8_t)((g_chartab[c] & ~CT_CELL_MASK) + ((dy_flags & DY_UHEX) ? 4 : 2)); @@ -539,7 +537,7 @@ char_u *transchar_buf(const buf_T *buf, int c) c = K_SECOND(c); } - if ((!chartab_initialized && (((c >= ' ') && (c <= '~')))) + if ((!chartab_initialized && (c >= ' ' && c <= '~')) || ((c <= 0xFF) && vim_isprintc_strict(c))) { // printable character transchar_charbuf[i] = (char_u)c; diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 6e2c6232d7..55f55a46b2 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -15,6 +15,7 @@ #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/move.h" +#include "nvim/option.h" #include "nvim/plines.h" #include "nvim/screen.h" #include "nvim/state.h" @@ -110,7 +111,7 @@ static int coladvance2(pos_T *pos, bool addspaces, bool finetune, colnr_T wcol_a || (State & TERM_FOCUS) || restart_edit != NUL || (VIsual_active && *p_sel != 'o') - || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL); + || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL); line = ml_get_buf(curbuf, pos->lnum, false); if (wcol >= MAXCOL) { @@ -366,6 +367,7 @@ void check_cursor_col_win(win_T *win) colnr_T len; colnr_T oldcol = win->w_cursor.col; colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd; + unsigned int cur_ve_flags = get_ve_flags(); len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, false)); if (len == 0) { @@ -377,7 +379,7 @@ void check_cursor_col_win(win_T *win) * - 'virtualedit' is set */ if ((State & INSERT) || restart_edit || (VIsual_active && *p_sel != 'o') - || (ve_flags & VE_ONEMORE) + || (cur_ve_flags & VE_ONEMORE) || virtual_active()) { win->w_cursor.col = len; } else { @@ -394,7 +396,7 @@ void check_cursor_col_win(win_T *win) // line. if (oldcol == MAXCOL) { win->w_cursor.coladd = 0; - } else if (ve_flags == VE_ALL) { + } else if (cur_ve_flags == VE_ALL) { if (oldcoladd > win->w_cursor.col) { win->w_cursor.coladd = oldcoladd - win->w_cursor.col; diff --git a/src/nvim/debugger.c b/src/nvim/debugger.c index b6e35f3047..75ebf0084e 100644 --- a/src/nvim/debugger.c +++ b/src/nvim/debugger.c @@ -268,7 +268,7 @@ void do_debug(char_u *cmd) DOCMD_VERBOSE|DOCMD_EXCRESET); debug_break_level = n; } - lines_left = (int)(Rows - 1); + lines_left = Rows - 1; } xfree(cmdline); @@ -277,7 +277,7 @@ void do_debug(char_u *cmd) redraw_all_later(NOT_VALID); need_wait_return = false; msg_scroll = save_msg_scroll; - lines_left = (int)(Rows - 1); + lines_left = Rows - 1; State = save_State; debug_mode = false; did_emsg = save_did_emsg; diff --git a/src/nvim/decoration.c b/src/nvim/decoration.c index c0f3c32f93..935b233752 100644 --- a/src/nvim/decoration.c +++ b/src/nvim/decoration.c @@ -13,8 +13,6 @@ # include "decoration.c.generated.h" #endif -static PMap(uint64_t) hl_decors; - /// Add highlighting to a buffer, bounded by two cursor positions, /// with an offset. /// @@ -33,9 +31,9 @@ void bufhl_add_hl_pos_offset(buf_T *buf, int src_id, int hl_id, lpos_T pos_start { colnr_T hl_start = 0; colnr_T hl_end = 0; - Decoration *decor = decor_hl(hl_id); + Decoration decor = DECORATION_INIT; + decor.hl_id = hl_id; - decor->priority = DECOR_PRIORITY_BASE; // TODO(bfredl): if decoration had blocky mode, we could avoid this loop for (linenr_T lnum = pos_start.lnum; lnum <= pos_end.lnum; lnum++) { int end_off = 0; @@ -59,40 +57,23 @@ void bufhl_add_hl_pos_offset(buf_T *buf, int src_id, int hl_id, lpos_T pos_start hl_start = pos_start.col + offset; hl_end = pos_end.col + offset; } - (void)extmark_set(buf, (uint64_t)src_id, NULL, - (int)lnum-1, hl_start, (int)lnum-1+end_off, hl_end, - decor, true, false, kExtmarkNoUndo); - } -} - -Decoration *decor_hl(int hl_id) -{ - assert(hl_id > 0); - Decoration **dp = (Decoration **)pmap_ref(uint64_t)(&hl_decors, - (uint64_t)hl_id, true); - if (*dp) { - return *dp; + extmark_set(buf, (uint32_t)src_id, NULL, + (int)lnum-1, hl_start, (int)lnum-1+end_off, hl_end, + &decor, true, false, kExtmarkNoUndo); } - - Decoration *decor = xcalloc(1, sizeof(*decor)); - decor->hl_id = hl_id; - decor->shared = true; - decor->priority = DECOR_PRIORITY_BASE; - *dp = decor; - return decor; } void decor_redraw(buf_T *buf, int row1, int row2, Decoration *decor) { - if (decor->hl_id && row2 >= row1) { + if ((!decor || decor->hl_id) && row2 >= row1) { redraw_buf_range_later(buf, row1+1, row2+1); } - if (kv_size(decor->virt_text)) { + if (decor && kv_size(decor->virt_text)) { redraw_buf_line_later(buf, row1+1); } - if (kv_size(decor->virt_lines)) { + if (decor && kv_size(decor->virt_lines)) { redraw_buf_line_later(buf, MIN(buf->b_ml.ml_line_count, row1+1+(decor->virt_lines_above?0:1))); } @@ -100,17 +81,17 @@ void decor_redraw(buf_T *buf, int row1, int row2, Decoration *decor) void decor_remove(buf_T *buf, int row, int row2, Decoration *decor) { - if (kv_size(decor->virt_lines)) { + decor_redraw(buf, row, row2, decor); + if (decor && kv_size(decor->virt_lines)) { assert(buf->b_virt_line_blocks > 0); buf->b_virt_line_blocks--; } - decor_redraw(buf, row, row2, decor); decor_free(decor); } void decor_free(Decoration *decor) { - if (decor && !decor->shared) { + if (decor) { clear_virttext(&decor->virt_text); for (size_t i = 0; i < kv_size(decor->virt_lines); i++) { clear_virttext(&kv_A(decor->virt_lines, i).line); @@ -134,17 +115,16 @@ Decoration *decor_find_virttext(buf_T *buf, int row, uint64_t ns_id) MarkTreeIter itr[1] = { 0 }; marktree_itr_get(buf->b_marktree, row, 0, itr); while (true) { - mtmark_t mark = marktree_itr_current(itr); - if (mark.row < 0 || mark.row > row) { + mtkey_t mark = marktree_itr_current(itr); + if (mark.pos.row < 0 || mark.pos.row > row) { break; - } else if (marktree_decor_level(mark.id) < kDecorLevelVisible) { + } else if (marktree_decor_level(mark) < kDecorLevelVisible) { goto next_mark; } - ExtmarkItem *item = map_ref(uint64_t, ExtmarkItem)(buf->b_extmark_index, - mark.id, false); - if (item && (ns_id == 0 || ns_id == item->ns_id) - && item->decor && kv_size(item->decor->virt_text)) { - return item->decor; + Decoration *decor = mark.decor_full; + if ((ns_id == 0 || ns_id == mark.ns) + && decor && kv_size(decor->virt_text)) { + return decor; } next_mark: marktree_itr_next(buf->b_marktree, itr); @@ -163,7 +143,20 @@ bool decor_redraw_reset(buf_T *buf, DecorState *state) } } kv_size(state->active) = 0; - return map_size(buf->b_extmark_index); + return buf->b_marktree->n_keys; +} + +Decoration get_decor(mtkey_t mark) +{ + if (mark.decor_full) { + return *mark.decor_full; + } else { + Decoration fake = DECORATION_INIT; + fake.hl_id = mark.hl_id; + fake.priority = mark.priority; + fake.hl_eol = (mark.flags & MT_FLAG_HL_EOL); + return fake; + } } @@ -176,42 +169,35 @@ bool decor_redraw_start(buf_T *buf, int top_row, DecorState *state) } marktree_itr_rewind(buf->b_marktree, state->itr); while (true) { - mtmark_t mark = marktree_itr_current(state->itr); - if (mark.row < 0) { // || mark.row > end_row + mtkey_t mark = marktree_itr_current(state->itr); + if (mark.pos.row < 0) { // || mark.row > end_row break; } - if ((mark.row < top_row && mark.id&MARKTREE_END_FLAG) - || marktree_decor_level(mark.id) < kDecorLevelVisible) { + if ((mark.pos.row < top_row && mt_end(mark)) + || marktree_decor_level(mark) < kDecorLevelVisible) { goto next_mark; } - uint64_t start_id = mark.id & ~MARKTREE_END_FLAG; - ExtmarkItem *item = map_ref(uint64_t, ExtmarkItem)(buf->b_extmark_index, - start_id, false); - if (!item || !item->decor) { - goto next_mark; - } - Decoration *decor = item->decor; + Decoration decor = get_decor(mark); - mtpos_t altpos = marktree_lookup(buf->b_marktree, - mark.id^MARKTREE_END_FLAG, NULL); + mtpos_t altpos = marktree_get_altpos(buf->b_marktree, mark, NULL); - if ((!(mark.id&MARKTREE_END_FLAG) && altpos.row < top_row - && !kv_size(decor->virt_text)) - || ((mark.id&MARKTREE_END_FLAG) && altpos.row >= top_row)) { + if ((!mt_end(mark) && altpos.row < top_row + && !kv_size(decor.virt_text)) + || (mt_end(mark) && altpos.row >= top_row)) { goto next_mark; } - if (mark.id&MARKTREE_END_FLAG) { - decor_add(state, altpos.row, altpos.col, mark.row, mark.col, - decor, false); + if (mt_end(mark)) { + decor_add(state, altpos.row, altpos.col, mark.pos.row, mark.pos.col, + &decor, false); } else { if (altpos.row == -1) { - altpos.row = mark.row; - altpos.col = mark.col; + altpos.row = mark.pos.row; + altpos.col = mark.pos.col; } - decor_add(state, mark.row, mark.col, altpos.row, altpos.col, - decor, false); + decor_add(state, mark.pos.row, mark.pos.col, altpos.row, altpos.col, + &decor, false); } next_mark: @@ -266,43 +252,36 @@ int decor_redraw_col(buf_T *buf, int col, int win_col, bool hidden, DecorState * while (true) { // TODO(bfredl): check duplicate entry in "intersection" // branch - mtmark_t mark = marktree_itr_current(state->itr); - if (mark.row < 0 || mark.row > state->row) { + mtkey_t mark = marktree_itr_current(state->itr); + if (mark.pos.row < 0 || mark.pos.row > state->row) { break; - } else if (mark.row == state->row && mark.col > col) { - state->col_until = mark.col-1; + } else if (mark.pos.row == state->row && mark.pos.col > col) { + state->col_until = mark.pos.col-1; break; } - if ((mark.id&MARKTREE_END_FLAG) - || marktree_decor_level(mark.id) < kDecorLevelVisible) { + if (mt_end(mark) + || marktree_decor_level(mark) < kDecorLevelVisible) { goto next_mark; } - ExtmarkItem *item = map_ref(uint64_t, ExtmarkItem)(buf->b_extmark_index, - mark.id, false); - if (!item || !item->decor) { - goto next_mark; - } - Decoration *decor = item->decor; + Decoration decor = get_decor(mark); - mtpos_t endpos = marktree_lookup(buf->b_marktree, - mark.id|MARKTREE_END_FLAG, NULL); + mtpos_t endpos = marktree_get_altpos(buf->b_marktree, mark, NULL); if (endpos.row == -1) { - endpos.row = mark.row; - endpos.col = mark.col; + endpos = mark.pos; } - if (endpos.row < mark.row - || (endpos.row == mark.row && endpos.col <= mark.col)) { - if (!kv_size(decor->virt_text)) { + if (endpos.row < mark.pos.row + || (endpos.row == mark.pos.row && endpos.col <= mark.pos.col)) { + if (!kv_size(decor.virt_text)) { goto next_mark; } } - decor_add(state, mark.row, mark.col, endpos.row, endpos.col, - decor, false); + decor_add(state, mark.pos.row, mark.pos.col, endpos.row, endpos.col, + &decor, false); next_mark: marktree_itr_next(buf->b_marktree, state->itr); @@ -452,18 +431,18 @@ int decor_virt_lines(win_T *wp, linenr_T lnum, VirtLines *lines) MarkTreeIter itr[1] = { 0 }; marktree_itr_get(buf->b_marktree, row, 0, itr); while (true) { - mtmark_t mark = marktree_itr_current(itr); - if (mark.row < 0 || mark.row >= end_row) { + mtkey_t mark = marktree_itr_current(itr); + if (mark.pos.row < 0 || mark.pos.row >= end_row) { break; - } else if (marktree_decor_level(mark.id) < kDecorLevelVirtLine) { + } else if (marktree_decor_level(mark) < kDecorLevelVirtLine) { goto next_mark; } - bool above = mark.row > (int)(lnum - 2); - ExtmarkItem *item = map_ref(uint64_t, ExtmarkItem)(buf->b_extmark_index, mark.id, false); - if (item && item->decor && item->decor->virt_lines_above == above) { - virt_lines += (int)kv_size(item->decor->virt_lines); + bool above = mark.pos.row > (int)(lnum - 2); + Decoration *decor = mark.decor_full; + if (decor && decor->virt_lines_above == above) { + virt_lines += (int)kv_size(decor->virt_lines); if (lines) { - kv_splice(*lines, item->decor->virt_lines); + kv_splice(*lines, decor->virt_lines); } } next_mark: diff --git a/src/nvim/decoration.h b/src/nvim/decoration.h index 611b4223da..02472d09e4 100644 --- a/src/nvim/decoration.h +++ b/src/nvim/decoration.h @@ -17,6 +17,8 @@ typedef enum { kVTRightAlign, } VirtTextPos; +EXTERN const char *const virt_text_pos_str[] INIT(= { "eol", "overlay", "win_col", "right_align" }); + typedef enum { kHlModeUnknown, kHlModeReplace, @@ -24,6 +26,8 @@ typedef enum { kHlModeBlend, } HlMode; +EXTERN const char *const hl_mode_str[] INIT(= { "", "replace", "combine", "blend" }); + typedef kvec_t(VirtTextChunk) VirtText; #define VIRTTEXT_EMPTY ((VirtText)KV_INITIAL_VALUE) @@ -42,7 +46,6 @@ struct Decoration { // TODO(bfredl): at some point turn this into FLAGS bool virt_text_hide; bool hl_eol; - bool shared; // shared decoration, don't free bool virt_lines_above; // TODO(bfredl): style, signs, etc DecorPriority priority; @@ -50,7 +53,7 @@ struct Decoration { int virt_text_width; // width of virt_text }; #define DECORATION_INIT { KV_INITIAL_VALUE, KV_INITIAL_VALUE, 0, kVTEndOfLine, kHlModeUnknown, \ - false, false, false, false, DECOR_PRIORITY_BASE, 0, 0 } + false, false, false, DECOR_PRIORITY_BASE, 0, 0 } typedef struct { int start_row; diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 4f4da7c2a9..233753839b 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -82,6 +82,14 @@ typedef struct { garray_T dout_ga; // used for internal diff } diffout_T; +// used for recording hunks from xdiff +typedef struct { + linenr_T lnum_orig; + long count_orig; + linenr_T lnum_new; + long count_new; +} diffhunk_T; + // two diff inputs and one result typedef struct { diffin_T dio_orig; // original file input @@ -782,9 +790,14 @@ static int diff_write(buf_T *buf, diffin_T *din) // Always use 'fileformat' set to "unix". char_u *save_ff = buf->b_p_ff; buf->b_p_ff = vim_strsave((char_u *)FF_UNIX); + const bool save_lockmarks = cmdmod.lockmarks; + // Writing the buffer is an implementation detail of performing the diff, + // so it shouldn't update the '[ and '] marks. + cmdmod.lockmarks = true; int r = buf_write(buf, din->din_fname, NULL, (linenr_T)1, buf->b_ml.ml_line_count, NULL, false, false, false, true); + cmdmod.lockmarks = save_lockmarks; free_string_option(buf->b_p_ff); buf->b_p_ff = save_ff; return r; @@ -852,7 +865,7 @@ static void diff_try_update(diffio_T *dio, int idx_orig, exarg_T *eap) } // Read the diff output and add each entry to the diff list. - diff_read(idx_orig, idx_new, &dio->dio_diff); + diff_read(idx_orig, idx_new, dio); clear_diffin(&dio->dio_new); clear_diffout(&dio->dio_diff); @@ -1078,7 +1091,7 @@ static int diff_file_internal(diffio_T *diffio) emit_cfg.ctxlen = 0; // don't need any diff_context here emit_cb.priv = &diffio->dio_diff; - emit_cb.out_line = xdiff_out; + emit_cfg.hunk_func = xdiff_out; if (xdl_diff(&diffio->dio_orig.din_mmfile, &diffio->dio_new.din_mmfile, ¶m, &emit_cfg, &emit_cb) < 0) { @@ -1519,20 +1532,20 @@ void ex_diffoff(exarg_T *eap) /// @param idx_orig idx of original file /// @param idx_new idx of new file /// @dout diff output -static void diff_read(int idx_orig, int idx_new, diffout_T *dout) +static void diff_read(int idx_orig, int idx_new, diffio_T *dio) { FILE *fd = NULL; int line_idx = 0; diff_T *dprev = NULL; diff_T *dp = curtab->tp_first_diff; diff_T *dn, *dpl; + diffout_T *dout = &dio->dio_diff; char_u linebuf[LBUFLEN]; // only need to hold the diff line char_u *line; long off; int i; - linenr_T lnum_orig, lnum_new; - long count_orig, count_new; int notset = true; // block "*dp" not set yet + diffhunk_T *hunk; enum { DIFF_ED, DIFF_UNIFIED, @@ -1549,70 +1562,79 @@ static void diff_read(int idx_orig, int idx_new, diffout_T *dout) } } + if (!dio->dio_internal) { + hunk = xmalloc(sizeof(*hunk)); + } + for (;;) { - if (fd == NULL) { + if (dio->dio_internal) { if (line_idx >= dout->dout_ga.ga_len) { break; // did last line } - line = ((char_u **)dout->dout_ga.ga_data)[line_idx++]; + hunk = ((diffhunk_T **)dout->dout_ga.ga_data)[line_idx++]; } else { - if (vim_fgets(linebuf, LBUFLEN, fd)) { - break; // end of file - } - line = linebuf; - } - - if (diffstyle == DIFF_NONE) { - // Determine diff style. - // ed like diff looks like this: - // {first}[,{last}]c{first}[,{last}] - // {first}a{first}[,{last}] - // {first}[,{last}]d{first} - // - // unified diff looks like this: - // --- file1 2018-03-20 13:23:35.783153140 +0100 - // +++ file2 2018-03-20 13:23:41.183156066 +0100 - // @@ -1,3 +1,5 @@ - if (isdigit(*line)) { - diffstyle = DIFF_ED; - } else if ((STRNCMP(line, "@@ ", 3) == 0)) { - diffstyle = DIFF_UNIFIED; - } else if ((STRNCMP(line, "--- ", 4) == 0) // -V501 - && (vim_fgets(linebuf, LBUFLEN, fd) == 0) // -V501 - && (STRNCMP(line, "+++ ", 4) == 0) - && (vim_fgets(linebuf, LBUFLEN, fd) == 0) // -V501 - && (STRNCMP(line, "@@ ", 3) == 0)) { - diffstyle = DIFF_UNIFIED; + if (fd == NULL) { + if (line_idx >= dout->dout_ga.ga_len) { + break; // did last line + } + line = ((char_u **)dout->dout_ga.ga_data)[line_idx++]; } else { - // Format not recognized yet, skip over this line. Cygwin diff - // may put a warning at the start of the file. - continue; + if (vim_fgets(linebuf, LBUFLEN, fd)) { + break; // end of file + } + line = linebuf; } - } - if (diffstyle == DIFF_ED) { - if (!isdigit(*line)) { - continue; // not the start of a diff block - } - if (parse_diff_ed(line, &lnum_orig, &count_orig, - &lnum_new, &count_new) == FAIL) { - continue; - } - } else { - assert(diffstyle == DIFF_UNIFIED); - if (STRNCMP(line, "@@ ", 3) != 0) { - continue; // not the start of a diff block + if (diffstyle == DIFF_NONE) { + // Determine diff style. + // ed like diff looks like this: + // {first}[,{last}]c{first}[,{last}] + // {first}a{first}[,{last}] + // {first}[,{last}]d{first} + // + // unified diff looks like this: + // --- file1 2018-03-20 13:23:35.783153140 +0100 + // +++ file2 2018-03-20 13:23:41.183156066 +0100 + // @@ -1,3 +1,5 @@ + if (isdigit(*line)) { + diffstyle = DIFF_ED; + } else if ((STRNCMP(line, "@@ ", 3) == 0)) { + diffstyle = DIFF_UNIFIED; + } else if ((STRNCMP(line, "--- ", 4) == 0) // -V501 + && (vim_fgets(linebuf, LBUFLEN, fd) == 0) // -V501 + && (STRNCMP(line, "+++ ", 4) == 0) + && (vim_fgets(linebuf, LBUFLEN, fd) == 0) // -V501 + && (STRNCMP(line, "@@ ", 3) == 0)) { + diffstyle = DIFF_UNIFIED; + } else { + // Format not recognized yet, skip over this line. Cygwin diff + // may put a warning at the start of the file. + continue; + } } - if (parse_diff_unified(line, &lnum_orig, &count_orig, - &lnum_new, &count_new) == FAIL) { - continue; + + if (diffstyle == DIFF_ED) { + if (!isdigit(*line)) { + continue; // not the start of a diff block + } + if (parse_diff_ed(line, hunk) == FAIL) { + continue; + } + } else { + assert(diffstyle == DIFF_UNIFIED); + if (STRNCMP(line, "@@ ", 3) != 0) { + continue; // not the start of a diff block + } + if (parse_diff_unified(line, hunk) == FAIL) { + continue; + } } } // Go over blocks before the change, for which orig and new are equal. // Copy blocks from orig to new. while (dp != NULL - && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig]) { + && hunk->lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig]) { if (notset) { diff_copy_entry(dprev, dp, idx_orig, idx_new); } @@ -1622,19 +1644,19 @@ static void diff_read(int idx_orig, int idx_new, diffout_T *dout) } if ((dp != NULL) - && (lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]) - && (lnum_orig + count_orig >= dp->df_lnum[idx_orig])) { + && (hunk->lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]) + && (hunk->lnum_orig + hunk->count_orig >= dp->df_lnum[idx_orig])) { // New block overlaps with existing block(s). // First find last block that overlaps. for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next) { - if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig]) { + if (hunk->lnum_orig + hunk->count_orig < dpl->df_next->df_lnum[idx_orig]) { break; } } // If the newly found block starts before the old one, set the // start back a number of lines. - off = dp->df_lnum[idx_orig] - lnum_orig; + off = dp->df_lnum[idx_orig] - hunk->lnum_orig; if (off > 0) { for (i = idx_orig; i < idx_new; ++i) { @@ -1642,15 +1664,15 @@ static void diff_read(int idx_orig, int idx_new, diffout_T *dout) dp->df_lnum[i] -= off; } } - dp->df_lnum[idx_new] = lnum_new; - dp->df_count[idx_new] = count_new; + dp->df_lnum[idx_new] = hunk->lnum_new; + dp->df_count[idx_new] = hunk->count_new; } else if (notset) { // new block inside existing one, adjust new block - dp->df_lnum[idx_new] = lnum_new + off; - dp->df_count[idx_new] = count_new - off; + dp->df_lnum[idx_new] = hunk->lnum_new + off; + dp->df_count[idx_new] = hunk->count_new - off; } else { // second overlap of new block with existing block - dp->df_count[idx_new] += count_new - count_orig + dp->df_count[idx_new] += hunk->count_new - hunk->count_orig + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig] - (dp->df_lnum[idx_orig] + @@ -1659,7 +1681,7 @@ static void diff_read(int idx_orig, int idx_new, diffout_T *dout) // Adjust the size of the block to include all the lines to the // end of the existing block or the new diff, whatever ends last. - off = (lnum_orig + count_orig) + off = (hunk->lnum_orig + hunk->count_orig) - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]); if (off < 0) { @@ -1691,10 +1713,10 @@ static void diff_read(int idx_orig, int idx_new, diffout_T *dout) // Allocate a new diffblock. dp = diff_alloc_new(curtab, dprev, dp); - dp->df_lnum[idx_orig] = lnum_orig; - dp->df_count[idx_orig] = count_orig; - dp->df_lnum[idx_new] = lnum_new; - dp->df_count[idx_new] = count_new; + dp->df_lnum[idx_orig] = hunk->lnum_orig; + dp->df_count[idx_orig] = hunk->count_orig; + dp->df_lnum[idx_new] = hunk->lnum_new; + dp->df_count[idx_new] = hunk->count_new; // Set values for other buffers, these must be equal to the // original buffer, otherwise there would have been a change @@ -1718,6 +1740,10 @@ static void diff_read(int idx_orig, int idx_new, diffout_T *dout) notset = true; } + if (!dio->dio_internal) { + xfree(hunk); + } + if (fd != NULL) { fclose(fd); } @@ -3026,8 +3052,7 @@ linenr_T diff_lnum_win(linenr_T lnum, win_T *wp) /// Handle an ED style diff line. /// Return FAIL if the line does not contain diff info. /// -static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, - long *count_new) +static int parse_diff_ed(char_u *line, diffhunk_T *hunk) { char_u *p; long f1, l1, f2, l2; @@ -3061,18 +3086,18 @@ static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, li } if (difftype == 'a') { - *lnum_orig = f1 + 1; - *count_orig = 0; + hunk->lnum_orig = f1 + 1; + hunk->count_orig = 0; } else { - *lnum_orig = f1; - *count_orig = l1 - f1 + 1; + hunk->lnum_orig = f1; + hunk->count_orig = l1 - f1 + 1; } if (difftype == 'd') { - *lnum_new = f2 + 1; - *count_new = 0; + hunk->lnum_new = f2 + 1; + hunk->count_new = 0; } else { - *lnum_new = f2; - *count_new = l2 - f2 + 1; + hunk->lnum_new = f2; + hunk->count_new = l2 - f2 + 1; } return OK; } @@ -3081,8 +3106,7 @@ static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, li /// Parses unified diff with zero(!) context lines. /// Return FAIL if there is no diff information in "line". /// -static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_orig, - linenr_T *lnum_new, long *count_new) +static int parse_diff_unified(char_u *line, diffhunk_T *hunk) { char_u *p; long oldline, oldcount, newline, newcount; @@ -3120,10 +3144,10 @@ static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_ori newline = 1; } - *lnum_orig = oldline; - *count_orig = oldcount; - *lnum_new = newline; - *count_new = newcount; + hunk->lnum_orig = oldline; + hunk->count_orig = oldcount; + hunk->lnum_new = newline; + hunk->count_new = newcount; return OK; } @@ -3135,25 +3159,17 @@ static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_ori /// Callback function for the xdl_diff() function. /// Stores the diff output in a grow array. /// -static int xdiff_out(void *priv, mmbuffer_t *mb, int nbuf) +static int xdiff_out(long start_a, long count_a, long start_b, long count_b, + void *priv) { diffout_T *dout = (diffout_T *)priv; - char_u *p; - - // The header line always comes by itself, text lines in at least two - // parts. We drop the text part. - if (nbuf > 1) { - return 0; - } - - // sanity check - if (STRNCMP(mb[0].ptr, "@@ ", 3) != 0) { - return 0; - } + diffhunk_T *p = xmalloc(sizeof(*p)); ga_grow(&dout->dout_ga, 1); - - p = vim_strnsave((char_u *)mb[0].ptr, mb[0].size); - ((char_u **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p; + p->lnum_orig = start_a + 1; + p->count_orig = count_a; + p->lnum_new = start_b + 1; + p->count_new = count_b; + ((diffhunk_T **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p; return 0; } diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 5eef4350b7..00ffa7cba1 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -260,7 +260,7 @@ static colnr_T Insstart_blank_vcol; // vcol for first inserted blank static bool update_Insstart_orig = true; // set Insstart_orig to Insstart static char_u *last_insert = NULL; // the text of the previous insert, - // K_SPECIAL and CSI are escaped + // K_SPECIAL is escaped static int last_insert_skip; // nr of chars in front of previous insert static int new_insert_skip; // nr of chars in front of current insert static int did_restart_edit; // "restart_edit" when calling edit() @@ -663,8 +663,12 @@ static int insert_execute(VimState *state, int key) InsertState *const s = (InsertState *)state; if (stop_insert_mode) { // Insert mode ended, possibly from a callback. + if (key != K_IGNORE && key != K_NOP) { + vungetc(key); + } s->count = 0; s->nomove = true; + ins_compl_prep(ESC); return 0; } @@ -909,7 +913,7 @@ static int insert_handle_key(InsertState *s) ins_ctrl_o(); // don't move the cursor left when 'virtualedit' has "onemore". - if (ve_flags & VE_ONEMORE) { + if (get_ve_flags() & VE_ONEMORE) { ins_at_eol = false; s->nomove = true; } @@ -1082,11 +1086,15 @@ static int insert_handle_key(InsertState *s) map_execute_lua(); check_pum: + // nvim_select_popupmenu_item() can be called from the handling of + // K_EVENT, K_COMMAND, or K_LUA. // TODO(bfredl): Not entirely sure this indirection is necessary // but doing like this ensures using nvim_select_popupmenu_item is // equivalent to selecting the item with a typed key. if (pum_want.active) { if (pum_visible()) { + // Set this to NULL so that ins_complete() will update the message. + edit_submode_extra = NULL; insert_do_complete(s); if (pum_want.finish) { // accept the item and stop completion @@ -3620,7 +3628,7 @@ static bool ins_compl_prep(int c) // Ignore end of Select mode mapping and mouse scroll buttons. if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP || c == K_MOUSELEFT || c == K_MOUSERIGHT || c == K_EVENT - || c == K_COMMAND) { + || c == K_COMMAND || c == K_LUA) { return retval; } @@ -4986,7 +4994,7 @@ void ins_compl_check_keys(int frequency, int in_compl_func) */ static int ins_compl_key2dir(int c) { - if (c == K_EVENT || c == K_COMMAND) { + if (c == K_EVENT || c == K_COMMAND || c == K_LUA) { return pum_want.item < pum_selected_item ? BACKWARD : FORWARD; } if (c == Ctrl_P || c == Ctrl_L @@ -5016,7 +5024,7 @@ static int ins_compl_key2count(int c) { int h; - if (c == K_EVENT || c == K_COMMAND) { + if (c == K_EVENT || c == K_COMMAND || c == K_LUA) { int offset = pum_want.item - pum_selected_item; return abs(offset); } @@ -5050,6 +5058,7 @@ static bool ins_compl_use_match(int c) return false; case K_EVENT: case K_COMMAND: + case K_LUA: return pum_want.active && pum_want.insert; } return true; @@ -5628,8 +5637,12 @@ int get_literal(void) i = 0; for (;;) { nc = plain_vgetc(); - if (!(State & CMDLINE) - && MB_BYTE2LEN_CHECK(nc) == 1) { + if ((mod_mask & ~MOD_MASK_SHIFT) != 0) { + // A character with non-Shift modifiers should not be a valid + // character for i_CTRL-V_digit. + break; + } + if (!(State & CMDLINE) && MB_BYTE2LEN_CHECK(nc) == 1) { add_to_showcmd(nc); } if (nc == 'x' || nc == 'X') { @@ -5695,6 +5708,8 @@ int get_literal(void) --no_mapping; if (nc) { vungetc(nc); + // A character typed with i_CTRL-V_digit cannot have modifiers. + mod_mask = 0; } got_int = false; // CTRL-C typed after CTRL-V is not an interrupt return cc; @@ -6006,6 +6021,7 @@ static void internal_format(int textwidth, int second_indent, int flags, int for char_u *saved_text = NULL; colnr_T col; colnr_T end_col; + bool did_do_comment = false; virtcol = get_nolist_virtcol() + char2cells(c != NUL ? c : gchar_cursor()); @@ -6121,8 +6137,7 @@ static void internal_format(int textwidth, int second_indent, int flags, int for if (curwin->w_cursor.col <= (colnr_T)wantcol) { break; } - } else if ((cc >= 0x100 || !utf_allow_break_before(cc)) - && fo_multibyte) { + } else if ((cc >= 0x100 || !utf_allow_break_before(cc)) && fo_multibyte) { int ncc; bool allow_break; @@ -6279,11 +6294,18 @@ static void internal_format(int textwidth, int second_indent, int flags, int for + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) + (do_comments ? OPENLINE_DO_COM : 0) + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0), - ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent)); + ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent), + &did_do_comment); if (!(flags & INSCHAR_COM_LIST)) { old_indent = 0; } + // If a comment leader was inserted, may also do this on a following + // line. + if (did_do_comment) { + no_leader = false; + } + replace_offset = 0; if (first_line) { if (!(flags & INSCHAR_COM_LIST)) { @@ -6814,7 +6836,7 @@ void free_last_insert(void) /// Add character "c" to buffer "s" /// -/// Escapes the special meaning of K_SPECIAL and CSI, handles multi-byte +/// Escapes the special meaning of K_SPECIAL, handles multi-byte /// characters. /// /// @param[in] c Character to add. @@ -6828,7 +6850,7 @@ char_u *add_char2buf(int c, char_u *s) const int len = utf_char2bytes(c, temp); for (int i = 0; i < len; i++) { c = temp[i]; - // Need to escape K_SPECIAL and CSI like in the typeahead buffer. + // Need to escape K_SPECIAL like in the typeahead buffer. if (c == K_SPECIAL) { *s++ = K_SPECIAL; *s++ = KS_SPECIAL; @@ -6902,8 +6924,7 @@ int oneright(void) // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' // contains "onemore". - if (ptr[l] == NUL - && (ve_flags & VE_ONEMORE) == 0) { + if (ptr[l] == NUL && (get_ve_flags() & VE_ONEMORE) == 0) { return FAIL; } curwin->w_cursor.col += l; @@ -7110,9 +7131,7 @@ int stuff_inserted(int c, long count, int no_esc) stuffReadbuff((const char *)ptr); // A trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^". if (last) { - stuffReadbuff((last == '0' - ? "\026\060\064\070" - : "\026^")); + stuffReadbuff(last == '0' ? "\026\060\064\070" : "\026^"); } } while (--count > 0); @@ -8025,7 +8044,7 @@ static bool ins_esc(long *count, int cmdchar, bool nomove) && !VIsual_active )) && !revins_on) { - if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) { + if (curwin->w_cursor.coladd > 0 || get_ve_flags() == VE_ALL) { oneleft(); if (restart_edit != NUL) { curwin->w_cursor.coladd++; @@ -8278,6 +8297,7 @@ static bool ins_bs(int c, int mode, int *inserted_space_p) int in_indent; int oldState; int cpc[MAX_MCO]; // composing characters + bool call_fix_indent = false; // can't delete anything in an empty file // can't backup past first character in buffer @@ -8421,6 +8441,8 @@ static bool ins_bs(int c, int mode, int *inserted_space_p) beginline(BL_WHITE); if (curwin->w_cursor.col < save_col) { mincol = curwin->w_cursor.col; + // should now fix the indent to match with the previous line + call_fix_indent = true; } curwin->w_cursor.col = save_col; } @@ -8555,6 +8577,11 @@ static bool ins_bs(int c, int mode, int *inserted_space_p) if (curwin->w_cursor.col <= 1) { did_ai = false; } + + if (call_fix_indent) { + fix_indent(); + } + // It's a little strange to put backspaces into the redo // buffer, but it makes auto-indent a lot easier to deal // with. @@ -9169,7 +9196,7 @@ static bool ins_eol(int c) AppendToRedobuff(NL_STR); bool i = open_line(FORWARD, has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : 0, - old_indent); + old_indent, NULL); old_indent = 0; can_cindent = true; // When inserting a line the cursor line must never be in a closed fold. diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 6dbdc09c3b..07f0799bc4 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -69,6 +69,7 @@ static char *e_nowhitespace static char *e_invalwindow = N_("E957: Invalid window number"); static char *e_lock_unlock = N_("E940: Cannot lock or unlock variable %s"); static char *e_write2 = N_("E80: Error while writing: %s"); +static char *e_string_list_or_blob_required = N_("E1098: String, List or Blob required"); // TODO(ZyX-I): move to eval/executor static char *e_letwrong = N_("E734: Wrong variable type for %s="); @@ -112,9 +113,11 @@ typedef struct { int fi_semicolon; // TRUE if ending in '; var]' int fi_varcount; // nr of variables in the list listwatch_T fi_lw; // keep an eye on the item used. - list_T *fi_list; // list being used + list_T *fi_list; // list being used int fi_bi; // index of blob blob_T *fi_blob; // blob being used + char_u *fi_string; // copy of string being used + int fi_byte_idx; // byte index in fi_string } forinfo_T; // values for vv_flags: @@ -763,6 +766,15 @@ static int eval1_emsg(char_u **arg, typval_T *rettv, bool evaluate) return ret; } +/// @return whether a typval is a valid expression to pass to eval_expr_typval() +/// or eval_expr_to_bool(). An empty string returns false; +bool eval_expr_valid_arg(const typval_T *const tv) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_CONST +{ + return tv->v_type != VAR_UNKNOWN + && (tv->v_type != VAR_STRING || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL)); +} + int eval_expr_typval(const typval_T *expr, typval_T *argv, int argc, typval_T *rettv) FUNC_ATTR_NONNULL_ARG(1, 2, 4) { @@ -2641,8 +2653,15 @@ void *eval_for_line(const char_u *arg, bool *errp, char_u **nextcmdp, int skip) fi->fi_blob = btv.vval.v_blob; } tv_clear(&tv); + } else if (tv.v_type == VAR_STRING) { + fi->fi_byte_idx = 0; + fi->fi_string = tv.vval.v_string; + tv.vval.v_string = NULL; + if (fi->fi_string == NULL) { + fi->fi_string = vim_strsave((char_u *)""); + } } else { - emsg(_(e_listblobreq)); + emsg(_(e_string_list_or_blob_required)); tv_clear(&tv); } } @@ -2679,6 +2698,22 @@ bool next_for_item(void *fi_void, char_u *arg) fi->fi_semicolon, fi->fi_varcount, false, NULL) == OK; } + if (fi->fi_string != NULL) { + const int len = utfc_ptr2len(fi->fi_string + fi->fi_byte_idx); + if (len == 0) { + return false; + } + typval_T tv; + tv.v_type = VAR_STRING; + tv.v_lock = VAR_FIXED; + tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len); + fi->fi_byte_idx += len; + const int result + = ex_let_vars(arg, &tv, true, fi->fi_semicolon, fi->fi_varcount, false, NULL) == OK; + xfree(tv.vval.v_string); + return result; + } + listitem_T *item = fi->fi_lw.lw_item; if (item == NULL) { return false; @@ -2698,12 +2733,16 @@ void free_for_info(void *fi_void) { forinfo_T *fi = (forinfo_T *)fi_void; - if (fi != NULL && fi->fi_list != NULL) { + if (fi == NULL) { + return; + } + if (fi->fi_list != NULL) { tv_list_watch_remove(fi->fi_list, &fi->fi_lw); tv_list_unref(fi->fi_list); - } - if (fi != NULL && fi->fi_blob != NULL) { + } else if (fi->fi_blob != NULL) { tv_blob_unref(fi->fi_blob); + } else { + xfree(fi->fi_string); } xfree(fi); } @@ -3218,9 +3257,8 @@ char_u *get_user_var_name(expand_T *xp, int idx) // b: variables // In cmdwin, the alternative buffer should be used. - hashtab_T *ht = (cmdwin_type != 0 && get_cmdline_type() == NUL) - ? &prevwin->w_buffer->b_vars->dv_hashtab - : &curbuf->b_vars->dv_hashtab; + hashtab_T *ht + = is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab : &curbuf->b_vars->dv_hashtab; if (bdone < ht->ht_used) { if (bdone++ == 0) { hi = ht->ht_array; @@ -3235,9 +3273,7 @@ char_u *get_user_var_name(expand_T *xp, int idx) // w: variables // In cmdwin, the alternative window should be used. - ht = (cmdwin_type != 0 && get_cmdline_type() == NUL) - ? &prevwin->w_vars->dv_hashtab - : &curwin->w_vars->dv_hashtab; + ht = is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab : &curwin->w_vars->dv_hashtab; if (wdone < ht->ht_used) { if (wdone++ == 0) { hi = ht->ht_array; @@ -4402,7 +4438,7 @@ static int eval_lambda(char_u **const arg, typval_T *const rettv, const bool eva rettv->v_type = VAR_UNKNOWN; int ret = get_lambda_tv(arg, rettv, evaluate); - if (ret == NOTDONE) { + if (ret != OK) { return FAIL; } else if (**arg != '(') { if (verbose) { @@ -6468,6 +6504,10 @@ void filter_map(typval_T *argvars, typval_T *rettv, int map) if (argvars[0].v_type == VAR_DICT) { vimvars[VV_KEY].vv_type = VAR_STRING; + const VarLockStatus prev_lock = d->dv_lock; + if (map && d->dv_lock == VAR_UNLOCKED) { + d->dv_lock = VAR_LOCKED; + } ht = &d->dv_hashtab; hash_lock(ht); todo = (int)ht->ht_used; @@ -6498,6 +6538,7 @@ void filter_map(typval_T *argvars, typval_T *rettv, int map) } } hash_unlock(ht); + d->dv_lock = prev_lock; } else if (argvars[0].v_type == VAR_BLOB) { vimvars[VV_KEY].vv_type = VAR_NUMBER; @@ -6530,6 +6571,10 @@ void filter_map(typval_T *argvars, typval_T *rettv, int map) assert(argvars[0].v_type == VAR_LIST); vimvars[VV_KEY].vv_type = VAR_NUMBER; + const VarLockStatus prev_lock = tv_list_locked(l); + if (map && tv_list_locked(l) == VAR_UNLOCKED) { + tv_list_set_lock(l, VAR_LOCKED); + } for (listitem_T *li = tv_list_first(l); li != NULL;) { if (map && var_check_lock(TV_LIST_ITEM_TV(li)->v_lock, arg_errmsg, @@ -6548,6 +6593,7 @@ void filter_map(typval_T *argvars, typval_T *rettv, int map) } idx++; } + tv_list_set_lock(l, prev_lock); } restore_vimvar(VV_KEY, &save_key); @@ -6956,10 +7002,9 @@ win_T *find_tabwin(typval_T *wvp, typval_T *tvp) /// @param off 1 for gettabwinvar() void getwinvar(typval_T *argvars, typval_T *rettv, int off) { - win_T *win, *oldcurwin; + win_T *win; dictitem_T *v; tabpage_T *tp = NULL; - tabpage_T *oldtabpage = NULL; bool done = false; if (off == 1) { @@ -6979,8 +7024,8 @@ void getwinvar(typval_T *argvars, typval_T *rettv, int off) // otherwise the window is not valid. Only do this when needed, // autocommands get blocked. bool need_switch_win = tp != curtab || win != curwin; - if (!need_switch_win - || switch_win(&oldcurwin, &oldtabpage, win, tp, true) == OK) { + switchwin_T switchwin; + if (!need_switch_win || switch_win(&switchwin, win, tp, true) == OK) { if (*varname == '&') { if (varname[1] == NUL) { // get all window-local options in a dict @@ -7008,7 +7053,7 @@ void getwinvar(typval_T *argvars, typval_T *rettv, int off) if (need_switch_win) { // restore previous notion of curwin - restore_win(oldcurwin, oldtabpage, true); + restore_win(&switchwin, true); } } emsg_off--; @@ -7510,11 +7555,9 @@ void setwinvar(typval_T *argvars, typval_T *rettv, int off) typval_T *varp = &argvars[off + 2]; if (win != NULL && varname != NULL && varp != NULL) { - win_T *save_curwin; - tabpage_T *save_curtab; bool need_switch_win = tp != curtab || win != curwin; - if (!need_switch_win - || switch_win(&save_curwin, &save_curtab, win, tp, true) == OK) { + switchwin_T switchwin; + if (!need_switch_win || switch_win(&switchwin, win, tp, true) == OK) { if (*varname == '&') { long numval; bool error = false; @@ -7536,7 +7579,7 @@ void setwinvar(typval_T *argvars, typval_T *rettv, int off) } } if (need_switch_win) { - restore_win(save_curwin, save_curtab, true); + restore_win(&switchwin, true); } } } @@ -8147,6 +8190,66 @@ char *save_tv_as_string(typval_T *tv, ptrdiff_t *const len, bool endnl) return ret; } +/// Convert the specified byte index of line 'lnum' in buffer 'buf' to a +/// character index. Works only for loaded buffers. Returns -1 on failure. +/// The index of the first byte and the first character is zero. +int buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx) +{ + if (buf == NULL || buf->b_ml.ml_mfp == NULL) { + return -1; + } + + if (lnum > buf->b_ml.ml_line_count) { + lnum = buf->b_ml.ml_line_count; + } + + char_u *str = ml_get_buf(buf, lnum, false); + + if (*str == NUL) { + return 0; + } + + // count the number of characters + char_u *t = str; + int count; + for (count = 0; *t != NUL && t <= str + byteidx; count++) { + t += utfc_ptr2len(t); + } + + // In insert mode, when the cursor is at the end of a non-empty line, + // byteidx points to the NUL character immediately past the end of the + // string. In this case, add one to the character count. + if (*t == NUL && byteidx != 0 && t == str + byteidx) { + count++; + } + + return count - 1; +} + +/// Convert the specified character index of line 'lnum' in buffer 'buf' to a +/// byte index. Works only for loaded buffers. Returns -1 on failure. +/// The index of the first byte and the first character is zero. +int buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx) +{ + if (buf == NULL || buf->b_ml.ml_mfp == NULL) { + return -1; + } + + if (lnum > buf->b_ml.ml_line_count) { + lnum = buf->b_ml.ml_line_count; + } + + char_u *str = ml_get_buf(buf, lnum, false); + + // Convert the character offset to a byte offset + char_u *t = str; + while (*t != NUL && --charidx > 0) { + t += utfc_ptr2len(t); + } + + return t - str; +} + /// Translate a VimL object into a position /// /// Accepts VAR_LIST and VAR_STRING objects. Does not give an error for invalid @@ -8155,9 +8258,11 @@ char *save_tv_as_string(typval_T *tv, ptrdiff_t *const len, bool endnl) /// @param[in] tv Object to translate. /// @param[in] dollar_lnum True when "$" is last line. /// @param[out] ret_fnum Set to fnum for marks. +/// @param[in] charcol True to return character column. /// /// @return Pointer to position or NULL in case of error (e.g. invalid type). -pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret_fnum) +pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret_fnum, + const bool charcol) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL { static pos_T pos; @@ -8187,7 +8292,11 @@ pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret if (error) { return NULL; } - len = (long)STRLEN(ml_get(pos.lnum)); + if (charcol) { + len = mb_charlen(ml_get(pos.lnum)); + } else { + len = STRLEN(ml_get(pos.lnum)); + } // We accept "$" for the column number: last column. li = tv_list_find(l, 1L); @@ -8218,19 +8327,31 @@ pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret return NULL; } if (name[0] == '.') { // Cursor. - return &curwin->w_cursor; + pos = curwin->w_cursor; + if (charcol) { + pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col); + } + return &pos; } if (name[0] == 'v' && name[1] == NUL) { // Visual start. if (VIsual_active) { - return &VIsual; + pos = VIsual; + } else { + pos = curwin->w_cursor; + } + if (charcol) { + pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col); } - return &curwin->w_cursor; + return &pos; } if (name[0] == '\'') { // Mark. pp = getmark_buf_fnum(curbuf, (uint8_t)name[1], false, ret_fnum); if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0) { return NULL; } + if (charcol) { + pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col); + } return pp; } @@ -8256,22 +8377,24 @@ pos_T *var2fpos(const typval_T *const tv, const bool dollar_lnum, int *const ret pos.col = 0; } else { pos.lnum = curwin->w_cursor.lnum; - pos.col = (colnr_T)STRLEN(get_cursor_line_ptr()); + if (charcol) { + pos.col = (colnr_T)mb_charlen(get_cursor_line_ptr()); + } else { + pos.col = (colnr_T)STRLEN(get_cursor_line_ptr()); + } } return &pos; } return NULL; } -/* - * Convert list in "arg" into a position and optional file number. - * When "fnump" is NULL there is no file number, only 3 items. - * Note that the column is passed on as-is, the caller may want to decrement - * it to use 1 for the first column. - * Return FAIL when conversion is not possible, doesn't check the position for - * validity. - */ -int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp) +/// Convert list in "arg" into a position and optional file number. +/// When "fnump" is NULL there is no file number, only 3 items. +/// Note that the column is passed on as-is, the caller may want to decrement +/// it to use 1 for the first column. +/// Return FAIL when conversion is not possible, doesn't check the position for +/// validity. +int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp, bool charcol) { list_T *l; long i = 0; @@ -8307,6 +8430,15 @@ int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp) if (n < 0) { return FAIL; } + // If character position is specified, then convert to byte position + if (charcol) { + // Get the text for the specified line in a loaded buffer + buf_T *buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump); + if (buf == NULL || buf->b_ml.ml_mfp == NULL) { + return FAIL; + } + n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1; + } posp->col = n; n = tv_list_find_nr(l, i, NULL); // off @@ -8946,7 +9078,7 @@ static bool tv_is_luafunc(typval_T *tv) const char *skip_luafunc_name(const char *p) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { - while (ASCII_ISALNUM(*p) || *p == '_' || *p == '.' || *p == '\'') { + while (ASCII_ISALNUM(*p) || *p == '_' || *p == '-' || *p == '.' || *p == '\'') { p++; } return p; @@ -10515,12 +10647,13 @@ int modify_fname(char_u *src, bool tilde_file, size_t *usedlen, char_u **fnamep, char_u *s, *p, *pbuf; char_u dirname[MAXPATHL]; int c; - int has_fullname = 0; + bool has_fullname = false; + bool has_homerelative = false; repeat: // ":p" - full path/file_name if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p') { - has_fullname = 1; + has_fullname = true; valid |= VALID_PATH; *usedlen += 2; @@ -10589,8 +10722,8 @@ repeat: } pbuf = NULL; // Need full path first (use expand_env() to remove a "~/") - if (!has_fullname) { - if (c == '.' && **fnamep == '~') { + if (!has_fullname && !has_homerelative) { + if (**fnamep == '~') { p = pbuf = expand_env_save(*fnamep); } else { p = pbuf = (char_u *)FullName_save((char *)*fnamep, FALSE); @@ -10599,18 +10732,33 @@ repeat: p = *fnamep; } - has_fullname = 0; + has_fullname = false; if (p != NULL) { if (c == '.') { os_dirname(dirname, MAXPATHL); - s = path_shorten_fname(p, dirname); - if (s != NULL) { - *fnamep = s; - if (pbuf != NULL) { - xfree(*bufp); // free any allocated file name - *bufp = pbuf; - pbuf = NULL; + if (has_homerelative) { + s = vim_strsave(dirname); + home_replace(NULL, s, dirname, MAXPATHL, true); + xfree(s); + } + size_t namelen = STRLEN(dirname); + + // Do not call shorten_fname() here since it removes the prefix + // even though the path does not have a prefix. + if (fnamencmp(p, dirname, namelen) == 0) { + p += namelen; + if (vim_ispathsep(*p)) { + while (*p && vim_ispathsep(*p)) { + p++; + } + *fnamep = p; + if (pbuf != NULL) { + // free any allocated file name + xfree(*bufp); + *bufp = pbuf; + pbuf = NULL; + } } } } else { @@ -10621,6 +10769,7 @@ repeat: *fnamep = s; xfree(*bufp); *bufp = s; + has_homerelative = true; } } xfree(pbuf); @@ -10997,10 +11146,7 @@ typval_T eval_call_provider(char *provider, char *method, list_T *arguments, boo bool eval_has_provider(const char *feat) { if (!strequal(feat, "clipboard") - && !strequal(feat, "python") && !strequal(feat, "python3") - && !strequal(feat, "python_compiled") - && !strequal(feat, "python_dynamic") && !strequal(feat, "python3_compiled") && !strequal(feat, "python3_dynamic") && !strequal(feat, "perl") diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index e445a08227..05e91a658f 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -71,6 +71,7 @@ return { chanclose={args={1, 2}}, chansend={args=2}, char2nr={args={1, 2}, base=1}, + charcol={args=1, base=1}, charidx={args={2, 3}, base=1}, chdir={args=1, base=1}, cindent={args=1, base=1}, @@ -144,6 +145,7 @@ return { getchangelist={args={0, 1}, base=1}, getchar={args={0, 1}}, getcharmod={}, + getcharpos={args=1, base=1}, getcharsearch={}, getcharstr={args={0, 1}}, getcmdline={}, @@ -151,7 +153,8 @@ return { getcmdtype={}, getcmdwintype={}, getcompletion={args={2, 3}, base=1}, - getcurpos={}, + getcurpos={args={0, 1}, base=1}, + getcursorcharpos={args={0, 1}, base=1}, getcwd={args={0, 2}, base=1}, getenv={args=1, base=1}, getfontname={args={0, 1}}, @@ -246,6 +249,8 @@ return { matcharg={args=1, base=1}, matchdelete={args={1, 2}, base=1}, matchend={args={2, 4}, base=1}, + matchfuzzy={args={2, 3}, base=1}, + matchfuzzypos={args={2, 3}, base=1}, matchlist={args={2, 4}, base=1}, matchstr={args={2, 4}, base=1}, matchstrpos={args={2,4}, base=1}, @@ -259,7 +264,7 @@ return { nextnonblank={args=1, base=1}, nr2char={args={1, 2}, base=1}, ['or']={args=2, base=1}, - pathshorten={args=1, base=1}, + pathshorten={args={1, 2}, base=1}, pow={args=2, base=1}, prevnonblank={args=1, base=1}, printf={args=varargs(1), base=2}, @@ -270,12 +275,14 @@ return { pum_getpos={}, pumvisible={}, py3eval={args=1, base=1}, - pyeval={args=1, base=1}, - pyxeval={args=1, base=1}, + pyeval={args=1, base=1, func="f_py3eval"}, + pyxeval={args=1, base=1, func="f_py3eval"}, perleval={args=1, base=1}, + rand={args={0, 1}, base=1}, range={args={1, 3}, base=1}, readdir={args={1, 2}, base=1}, readfile={args={1, 3}, base=1}, + reduce={args={2, 3}, base=1}, reg_executing={}, reg_recording={}, reg_recorded={}, @@ -300,19 +307,21 @@ return { screenpos={args=3, base=1}, screenrow={}, screenstring={args=2, base=1}, - search={args={1, 4}, base=1}, + search={args={1, 5}, base=1}, searchcount={args={0, 1}, base=1}, searchdecl={args={1, 3}, base=1}, searchpair={args={3, 7}}, searchpairpos={args={3, 7}}, - searchpos={args={1, 4}, base=1}, + searchpos={args={1, 5}, base=1}, serverlist={}, serverstart={args={0, 1}}, serverstop={args=1}, setbufline={args=3, base=3}, setbufvar={args=3, base=3}, + setcharpos={args=2, base=2}, setcharsearch={args=1, base=1}, setcmdpos={args=1, base=1}, + setcursorcharpos={args={1, 3}, base=1}, setenv={args=2, base=2}, setfperm={args=2, base=1}, setline={args=2, base=2}, @@ -348,6 +357,7 @@ return { spellsuggest={args={1, 3}, base=1}, split={args={1, 3}, base=1}, sqrt={args=1, base=1, func="float_op_wrapper", data="&sqrt"}, + srand={args={0, 1}, base=1}, stdpath={args=1}, str2float={args=1, base=1}, str2list={args={1, 2}, base=1}, @@ -413,6 +423,8 @@ return { win_gotoid={args=1, base=1}, win_id2tabwin={args=1, base=1}, win_id2win={args=1, base=1}, + win_move_separator={args=2, base=1}, + win_move_statusline={args=2, base=1}, win_screenpos={args=1, base=1}, win_splitmove={args={2, 3}, base=1}, winbufnr={args=1, base=1}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 7701688b49..7772d9ffc2 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -22,6 +22,7 @@ #include "nvim/eval/encode.h" #include "nvim/eval/executor.h" #include "nvim/eval/funcs.h" +#include "nvim/eval/typval.h" #include "nvim/eval/userfunc.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_docmd.h" @@ -97,9 +98,9 @@ PRAGMA_DIAG_POP #endif -static char *e_listarg = N_("E686: Argument of %s must be a List"); static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob"); static char *e_invalwindow = N_("E957: Invalid window number"); +static char *e_reduceempty = N_("E998: Reduce of an empty %s with no initial value"); /// Dummy va_list for passing to vim_snprintf /// @@ -1019,6 +1020,49 @@ static void f_char2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = utf_ptr2char((const char_u *)tv_get_string(&argvars[0])); } +/// Get the current cursor column and store it in 'rettv'. If 'charcol' is true, +/// returns the character index of the column. Otherwise, returns the byte index +/// of the column. +static void get_col(typval_T *argvars, typval_T *rettv, bool charcol) +{ + colnr_T col = 0; + pos_T *fp; + int fnum = curbuf->b_fnum; + + fp = var2fpos(&argvars[0], false, &fnum, charcol); + if (fp != NULL && fnum == curbuf->b_fnum) { + if (fp->col == MAXCOL) { + // '> can be MAXCOL, get the length of the line then + if (fp->lnum <= curbuf->b_ml.ml_line_count) { + col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1; + } else { + col = MAXCOL; + } + } else { + col = fp->col + 1; + // col(".") when the cursor is on the NUL at the end of the line + // because of "coladd" can be seen as an extra column. + if (virtual_active() && fp == &curwin->w_cursor) { + char_u *p = get_cursor_pos_ptr(); + if (curwin->w_cursor.coladd >= + (colnr_T)win_chartabsize(curwin, p, curwin->w_virtcol - curwin->w_cursor.coladd)) { + int l; + if (*p != NUL && p[(l = utfc_ptr2len(p))] == NUL) { + col += l; + } + } + } + } + } + rettv->vval.v_number = col; +} + +/// "charcol()" function +static void f_charcol(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + get_col(argvars, rettv, true); +} + // "charidx()" function static void f_charidx(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -1147,45 +1191,10 @@ static void f_clearmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } -/* - * "col(string)" function - */ +/// "col(string)" function static void f_col(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - colnr_T col = 0; - pos_T *fp; - int fnum = curbuf->b_fnum; - - fp = var2fpos(&argvars[0], FALSE, &fnum); - if (fp != NULL && fnum == curbuf->b_fnum) { - if (fp->col == MAXCOL) { - // '> can be MAXCOL, get the length of the line then - if (fp->lnum <= curbuf->b_ml.ml_line_count) { - col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1; - } else { - col = MAXCOL; - } - } else { - col = fp->col + 1; - // col(".") when the cursor is on the NUL at the end of the line - // because of "coladd" can be seen as an extra column. - if (virtual_active() && fp == &curwin->w_cursor) { - char_u *p = get_cursor_pos_ptr(); - - if (curwin->w_cursor.coladd - >= (colnr_T)win_chartabsize(curwin, p, - (curwin->w_virtcol - - curwin->w_cursor.coladd))) { - int l; - - if (*p != NUL && p[(l = utfc_ptr2len(p))] == NUL) { - col += l; - } - } - } - } - } - rettv->vval.v_number = col; + get_col(argvars, rettv, false); } /* @@ -1548,24 +1557,21 @@ static void f_ctxsize(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = ctx_size(); } -/// "cursor(lnum, col)" function, or -/// "cursor(list)" -/// -/// Moves the cursor to the specified line and column. -/// -/// @returns 0 when the position could be set, -1 otherwise. -static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// Set the cursor position. +/// If 'charcol' is true, then use the column number as a character offet. +/// Otherwise use the column number as a byte offset. +static void set_cursorpos(typval_T *argvars, typval_T *rettv, bool charcol) { long line, col; long coladd = 0; bool set_curswant = true; rettv->vval.v_number = -1; - if (argvars[1].v_type == VAR_UNKNOWN) { + if (argvars[0].v_type == VAR_LIST) { pos_T pos; colnr_T curswant = -1; - if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL) { + if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL) { emsg(_(e_invarg)); return; } @@ -1577,16 +1583,22 @@ static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr) curwin->w_curswant = curswant - 1; set_curswant = false; } - } else { + } else if ((argvars[0].v_type == VAR_NUMBER || argvars[0].v_type == VAR_STRING) + && (argvars[1].v_type == VAR_NUMBER || argvars[1].v_type == VAR_STRING)) { line = tv_get_lnum(argvars); col = (long)tv_get_number_chk(&argvars[1], NULL); + if (charcol) { + col = buf_charidx_to_byteidx(curbuf, line, col) + 1; + } if (argvars[2].v_type != VAR_UNKNOWN) { coladd = (long)tv_get_number_chk(&argvars[2], NULL); } + } else { + emsg(_(e_invarg)); + return; } - if (line < 0 || col < 0 - || coladd < 0) { - return; // type error; errmsg already given + if (line < 0 || col < 0 || coladd < 0) { + return; // type error; errmsg already given } if (line > 0) { curwin->w_cursor.lnum = line; @@ -1605,6 +1617,16 @@ static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = 0; } +/// "cursor(lnum, col)" function, or +/// "cursor(list)" +/// +/// Moves the cursor to the specified line and column. +/// Returns 0 when the position could be set, -1 otherwise. +static void f_cursor(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + set_cursorpos(argvars, rettv, false); +} + // "debugbreak()" function static void f_debugbreak(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -2176,25 +2198,12 @@ static void f_win_execute(typval_T *argvars, typval_T *rettv, FunPtr fptr) { tabpage_T *tp; win_T *wp = win_id2wp_tp(argvars, &tp); - win_T *save_curwin; - tabpage_T *save_curtab; // Return an empty string if something fails. rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; if (wp != NULL && tp != NULL) { - pos_T curpos = wp->w_cursor; - if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, true) == - OK) { - check_cursor(); - execute_common(argvars, rettv, fptr, 1); - } - restore_win_noblock(save_curwin, save_curtab, true); - - // Update the status line if the cursor moved. - if (win_valid(wp) && !equalpos(curpos, wp->w_cursor)) { - wp->w_redr_status = true; - } + WIN_EXECUTE(wp, tp, execute_common(argvars, rettv, fptr, 1)); } } @@ -3180,7 +3189,7 @@ static void getchar_common(typval_T *argvars, typval_T *rettv) if (argvars[0].v_type == VAR_UNKNOWN) { // getchar(): blocking wait. // TODO(bfredl): deduplicate shared logic with state_enter ? - if (!(char_avail() || using_script() || input_available())) { + if (!char_avail()) { (void)os_inchar(NULL, 0, -1, 0, main_loop.events); if (!multiqueue_empty(main_loop.events)) { state_handle_k_event(); @@ -3300,6 +3309,67 @@ static void f_getcharmod(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = mod_mask; } +static void getpos_both(typval_T *argvars, typval_T *rettv, bool getcurpos, bool charcol) +{ + pos_T *fp = NULL; + pos_T pos; + win_T *wp = curwin; + int fnum = -1; + + if (getcurpos) { + if (argvars[0].v_type != VAR_UNKNOWN) { + wp = find_win_by_nr_or_id(&argvars[0]); + if (wp != NULL) { + fp = &wp->w_cursor; + } + } else { + fp = &curwin->w_cursor; + } + if (fp != NULL && charcol) { + pos = *fp; + pos.col = buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col); + fp = &pos; + } + } else { + fp = var2fpos(&argvars[0], true, &fnum, charcol); + } + + list_T *const l = tv_list_alloc_ret(rettv, 4 + getcurpos); + tv_list_append_number(l, (fnum != -1) ? (varnumber_T)fnum : (varnumber_T)0); + tv_list_append_number(l, ((fp != NULL) ? (varnumber_T)fp->lnum : (varnumber_T)0)); + tv_list_append_number(l, ((fp != NULL) + ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1) + : (varnumber_T)0)); + tv_list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd : (varnumber_T)0); + if (getcurpos) { + const int save_set_curswant = curwin->w_set_curswant; + const colnr_T save_curswant = curwin->w_curswant; + const colnr_T save_virtcol = curwin->w_virtcol; + + if (wp == curwin) { + update_curswant(); + } + tv_list_append_number(l, (wp == NULL) ? 0 : ((wp->w_curswant == MAXCOL) + ? (varnumber_T)MAXCOL + : (varnumber_T)wp->w_curswant + 1)); + + // Do not change "curswant", as it is unexpected that a get + // function has a side effect. + if (wp == curwin && save_set_curswant) { + curwin->w_set_curswant = save_set_curswant; + curwin->w_curswant = save_curswant; + curwin->w_virtcol = save_virtcol; + curwin->w_valid &= ~VALID_VIRTCOL; + } + } +} + +/// "getcharpos()" function +static void f_getcharpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + getpos_both(argvars, rettv, false, true); +} + /* * "getcharsearch()" function */ @@ -3795,7 +3865,7 @@ static void f_getmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr) } // "getmousepos()" function -void f_getmousepos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +static void f_getmousepos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_T *d; win_T *wp; @@ -3855,61 +3925,21 @@ static void f_getpid(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = os_get_pid(); } -static void getpos_both(typval_T *argvars, typval_T *rettv, bool getcurpos) +/// "getcurpos(string)" function +static void f_getcurpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - pos_T *fp; - int fnum = -1; - - if (getcurpos) { - fp = &curwin->w_cursor; - } else { - fp = var2fpos(&argvars[0], true, &fnum); - } - - list_T *const l = tv_list_alloc_ret(rettv, 4 + (!!getcurpos)); - tv_list_append_number(l, (fnum != -1) ? (varnumber_T)fnum : (varnumber_T)0); - tv_list_append_number(l, ((fp != NULL) - ? (varnumber_T)fp->lnum - : (varnumber_T)0)); - tv_list_append_number(l, ((fp != NULL) - ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1) - : (varnumber_T)0)); - tv_list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd : (varnumber_T)0); - if (getcurpos) { - const int save_set_curswant = curwin->w_set_curswant; - const colnr_T save_curswant = curwin->w_curswant; - const colnr_T save_virtcol = curwin->w_virtcol; - - update_curswant(); - tv_list_append_number(l, (curwin->w_curswant == MAXCOL - ? (varnumber_T)MAXCOL - : (varnumber_T)curwin->w_curswant + 1)); - - // Do not change "curswant", as it is unexpected that a get - // function has a side effect. - if (save_set_curswant) { - curwin->w_set_curswant = save_set_curswant; - curwin->w_curswant = save_curswant; - curwin->w_virtcol = save_virtcol; - curwin->w_valid &= ~VALID_VIRTCOL; - } - } + getpos_both(argvars, rettv, true, false); } -/* - * "getcurpos(string)" function - */ -static void f_getcurpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - getpos_both(argvars, rettv, true); + getpos_both(argvars, rettv, true, true); } -/* - * "getpos(string)" function - */ +/// "getpos(string)" function static void f_getpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - getpos_both(argvars, rettv, false); + getpos_both(argvars, rettv, false, false); } /// "getqflist()" functions @@ -4031,8 +4061,6 @@ static void f_gettabinfo(typval_T *argvars, typval_T *rettv, FunPtr fptr) */ static void f_gettabvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - win_T *oldcurwin; - tabpage_T *oldtabpage; bool done = false; rettv->v_type = VAR_STRING; @@ -4046,7 +4074,8 @@ static void f_gettabvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) win_T *const window = tp == curtab || tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin; - if (switch_win(&oldcurwin, &oldtabpage, window, tp, true) == OK) { + switchwin_T switchwin; + if (switch_win(&switchwin, window, tp, true) == OK) { // look up the variable // Let gettabvar({nr}, "") return the "t:" dictionary. const dictitem_T *const v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', @@ -4059,7 +4088,7 @@ static void f_gettabvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) } // restore previous notion of curwin - restore_win(oldcurwin, oldtabpage, true); + restore_win(&switchwin, true); } if (!done && argvars[2].v_type != VAR_UNKNOWN) { @@ -4433,6 +4462,12 @@ static void f_has(typval_T *argvars, typval_T *rettv, FunPtr fptr) #if defined(BSD) && !defined(__APPLE__) "bsd", #endif +#ifdef __linux__ + "linux", +#endif +#ifdef SUN_SYSTEM + "sun", +#endif #ifdef UNIX "unix", #endif @@ -5881,22 +5916,20 @@ static void f_line(typval_T *argvars, typval_T *rettv, FunPtr fptr) if (argvars[1].v_type != VAR_UNKNOWN) { tabpage_T *tp; - win_T *save_curwin; - tabpage_T *save_curtab; // use window specified in the second argument win_T *wp = win_id2wp_tp(&argvars[1], &tp); if (wp != NULL && tp != NULL) { - if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, true) - == OK) { + switchwin_T switchwin; + if (switch_win_noblock(&switchwin, wp, tp, true) == OK) { check_cursor(); - fp = var2fpos(&argvars[0], true, &fnum); + fp = var2fpos(&argvars[0], true, &fnum, false); } - restore_win_noblock(save_curwin, save_curtab, true); + restore_win_noblock(&switchwin, true); } } else { // use current window - fp = var2fpos(&argvars[0], true, &fnum); + fp = var2fpos(&argvars[0], true, &fnum, false); } if (fp != NULL) { @@ -6813,12 +6846,23 @@ static void f_or(typval_T *argvars, typval_T *rettv, FunPtr fptr) */ static void f_pathshorten(typval_T *argvars, typval_T *rettv, FunPtr fptr) { + int trim_len = 1; + + if (argvars[1].v_type != VAR_UNKNOWN) { + trim_len = (int)tv_get_number(&argvars[1]); + if (trim_len < 1) { + trim_len = 1; + } + } + rettv->v_type = VAR_STRING; - const char *const s = tv_get_string_chk(&argvars[0]); - if (!s) { - return; + const char_u *p = (char_u *)tv_get_string_chk(&argvars[0]); + if (p == NULL) { + rettv->vval.v_string = NULL; + } else { + rettv->vval.v_string = vim_strsave(p); + shorten_dir_len(rettv->vval.v_string, trim_len); } - rettv->vval.v_string = shorten_dir((char_u *)xstrdup(s)); } /* @@ -6927,7 +6971,7 @@ static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv, FunPtr fpt } /// "prompt_getprompt({buffer})" function -void f_prompt_getprompt(typval_T *argvars, typval_T *rettv, FunPtr fptr) +static void f_prompt_getprompt(typval_T *argvars, typval_T *rettv, FunPtr fptr) FUNC_ATTR_NONNULL_ALL { // return an empty string by default, e.g. it's not a prompt buffer @@ -6982,36 +7026,169 @@ static void f_pumvisible(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } -/* - * "pyeval()" function - */ -static void f_pyeval(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// "py3eval()" and "pyxeval()" functions (always python3) +static void f_py3eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - script_host_eval("python", argvars, rettv); + script_host_eval("python3", argvars, rettv); } -/* - * "py3eval()" function - */ -static void f_py3eval(typval_T *argvars, typval_T *rettv, FunPtr fptr) +static void init_srand(uint32_t *const x) + FUNC_ATTR_NONNULL_ALL { - script_host_eval("python3", argvars, rettv); +#ifndef MSWIN + static int dev_urandom_state = NOTDONE; // FAIL or OK once tried + + if (dev_urandom_state != FAIL) { + const int fd = os_open("/dev/urandom", O_RDONLY, 0); + struct { + union { + uint32_t number; + char bytes[sizeof(uint32_t)]; + } contents; + } buf; + + // Attempt reading /dev/urandom. + if (fd == -1) { + dev_urandom_state = FAIL; + } else { + buf.contents.number = 0; + if (read(fd, buf.contents.bytes, sizeof(uint32_t)) != sizeof(uint32_t)) { + dev_urandom_state = FAIL; + } else { + dev_urandom_state = OK; + *x = buf.contents.number; + } + os_close(fd); + } + } + if (dev_urandom_state != OK) { + // Reading /dev/urandom doesn't work, fall back to time(). +#endif + *x = time(NULL); +#ifndef MSWIN + } +#endif +} + +static inline uint32_t splitmix32(uint32_t *const x) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE +{ + uint32_t z = (*x += 0x9e3779b9); + z = (z ^ (z >> 16)) * 0x85ebca6b; + z = (z ^ (z >> 13)) * 0xc2b2ae35; + return z ^ (z >> 16); +} + +static inline uint32_t shuffle_xoshiro128starstar(uint32_t *const x, uint32_t *const y, + uint32_t *const z, uint32_t *const w) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE +{ +#define ROTL(x, k) ((x << k) | (x >> (32 - k))) + const uint32_t result = ROTL(*y * 5, 7) * 9; + const uint32_t t = *y << 9; + *z ^= *x; + *w ^= *y; + *y ^= *z; + *x ^= *w; + *z ^= t; + *w = ROTL(*w, 11); +#undef ROTL + return result; +} + +/// "rand()" function +static void f_rand(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + uint32_t result; + + if (argvars[0].v_type == VAR_UNKNOWN) { + static uint32_t gx, gy, gz, gw; + static bool initialized = false; + + // When no argument is given use the global seed list. + if (!initialized) { + // Initialize the global seed list. + uint32_t x; + init_srand(&x); + + gx = splitmix32(&x); + gy = splitmix32(&x); + gz = splitmix32(&x); + gw = splitmix32(&x); + initialized = true; + } + + result = shuffle_xoshiro128starstar(&gx, &gy, &gz, &gw); + } else if (argvars[0].v_type == VAR_LIST) { + list_T *const l = argvars[0].vval.v_list; + if (tv_list_len(l) != 4) { + goto theend; + } + + typval_T *const tvx = TV_LIST_ITEM_TV(tv_list_find(l, 0L)); + typval_T *const tvy = TV_LIST_ITEM_TV(tv_list_find(l, 1L)); + typval_T *const tvz = TV_LIST_ITEM_TV(tv_list_find(l, 2L)); + typval_T *const tvw = TV_LIST_ITEM_TV(tv_list_find(l, 3L)); + if (tvx->v_type != VAR_NUMBER) { + goto theend; + } + if (tvy->v_type != VAR_NUMBER) { + goto theend; + } + if (tvz->v_type != VAR_NUMBER) { + goto theend; + } + if (tvw->v_type != VAR_NUMBER) { + goto theend; + } + uint32_t x = tvx->vval.v_number; + uint32_t y = tvy->vval.v_number; + uint32_t z = tvz->vval.v_number; + uint32_t w = tvw->vval.v_number; + + result = shuffle_xoshiro128starstar(&x, &y, &z, &w); + + tvx->vval.v_number = (varnumber_T)x; + tvy->vval.v_number = (varnumber_T)y; + tvz->vval.v_number = (varnumber_T)z; + tvw->vval.v_number = (varnumber_T)w; + } else { + goto theend; + } + + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = (varnumber_T)result; + return; + +theend: + semsg(_(e_invarg2), tv_get_string(&argvars[0])); + rettv->v_type = VAR_NUMBER; + rettv->vval.v_number = -1; } -// "pyxeval()" function -static void f_pyxeval(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// "srand()" function +static void f_srand(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - init_pyxversion(); - if (p_pyx == 2) { - f_pyeval(argvars, rettv, NULL); + uint32_t x = 0; + + tv_list_alloc_ret(rettv, 4); + if (argvars[0].v_type == VAR_UNKNOWN) { + init_srand(&x); } else { - f_py3eval(argvars, rettv, NULL); + bool error = false; + x = tv_get_number_chk(&argvars[0], &error); + if (error) { + return; + } } + + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); + tv_list_append_number(rettv->vval.v_list, (varnumber_T)splitmix32(&x)); } -/// /// "perleval()" function -/// static void f_perleval(typval_T *argvars, typval_T *rettv, FunPtr fptr) { script_host_eval("perl", argvars, rettv); @@ -7878,6 +8055,102 @@ static void f_reverse(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } +/// "reduce(list, { accumlator, element -> value } [, initial])" function +static void f_reduce(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB) { + emsg(_(e_listblobreq)); + return; + } + + const char_u *func_name; + partial_T *partial = NULL; + if (argvars[1].v_type == VAR_FUNC) { + func_name = argvars[1].vval.v_string; + } else if (argvars[1].v_type == VAR_PARTIAL) { + partial = argvars[1].vval.v_partial; + func_name = partial_name(partial); + } else { + func_name = (const char_u *)tv_get_string(&argvars[1]); + } + if (*func_name == NUL) { + return; // type error or empty name + } + + funcexe_T funcexe = FUNCEXE_INIT; + funcexe.evaluate = true; + funcexe.partial = partial; + + typval_T initial; + typval_T argv[3]; + if (argvars[0].v_type == VAR_LIST) { + list_T *const l = argvars[0].vval.v_list; + const listitem_T *li; + + if (argvars[2].v_type == VAR_UNKNOWN) { + if (tv_list_len(l) == 0) { + semsg(_(e_reduceempty), "List"); + return; + } + const listitem_T *const first = tv_list_first(l); + initial = *TV_LIST_ITEM_TV(first); + li = TV_LIST_ITEM_NEXT(l, first); + } else { + initial = argvars[2]; + li = tv_list_first(l); + } + + tv_copy(&initial, rettv); + + if (l != NULL) { + const VarLockStatus prev_locked = tv_list_locked(l); + const int called_emsg_start = called_emsg; + + tv_list_set_lock(l, VAR_FIXED); // disallow the list changing here + for (; li != NULL; li = TV_LIST_ITEM_NEXT(l, li)) { + argv[0] = *rettv; + argv[1] = *TV_LIST_ITEM_TV(li); + rettv->v_type = VAR_UNKNOWN; + const int r = call_func(func_name, -1, rettv, 2, argv, &funcexe); + tv_clear(&argv[0]); + if (r == FAIL || called_emsg != called_emsg_start) { + break; + } + } + tv_list_set_lock(l, prev_locked); + } + } else { + const blob_T *const b = argvars[0].vval.v_blob; + int i; + + if (argvars[2].v_type == VAR_UNKNOWN) { + if (tv_blob_len(b) == 0) { + semsg(_(e_reduceempty), "Blob"); + return; + } + initial.v_type = VAR_NUMBER; + initial.vval.v_number = tv_blob_get(b, 0); + i = 1; + } else if (argvars[2].v_type != VAR_NUMBER) { + emsg(_(e_number_exp)); + return; + } else { + initial = argvars[2]; + i = 0; + } + + tv_copy(&initial, rettv); + for (; i < tv_blob_len(b); i++) { + argv[0] = *rettv; + argv[1].v_type = VAR_NUMBER; + argv[1].vval.v_number = tv_blob_get(b, i); + if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL) { + return; + } + } + } +} + #define SP_NOMOVE 0x01 ///< don't move cursor #define SP_REPEAT 0x02 ///< repeat to find outer pair #define SP_RETCOUNT 0x04 ///< return matchcount @@ -7964,6 +8237,7 @@ static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp) int options = SEARCH_KEEP; int subpatnum; searchit_arg_T sia; + bool use_skip = false; const char *const pat = tv_get_string(&argvars[0]); dir = get_search_arg(&argvars[1], flagsp); // May set p_ws. @@ -7981,7 +8255,7 @@ static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp) options |= SEARCH_COL; } - // Optional arguments: line number to stop searching and timeout. + // Optional arguments: line number to stop searching, timeout and skip. if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN) { lnum_stop = tv_get_number_chk(&argvars[2], NULL); if (lnum_stop < 0) { @@ -7992,6 +8266,7 @@ static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp) if (time_limit < 0) { goto theend; } + use_skip = eval_expr_valid_arg(&argvars[4]); } } @@ -8011,11 +8286,46 @@ static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp) } pos = save_cursor = curwin->w_cursor; + pos_T firstpos = { 0 }; memset(&sia, 0, sizeof(sia)); sia.sa_stop_lnum = (linenr_T)lnum_stop; sia.sa_tm = &tm; - subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, (char_u *)pat, 1, - options, RE_SEARCH, &sia); + + // Repeat until {skip} returns false. + for (;;) { + subpatnum + = searchit(curwin, curbuf, &pos, NULL, dir, (char_u *)pat, 1, options, RE_SEARCH, &sia); + // finding the first match again means there is no match where {skip} + // evaluates to zero. + if (firstpos.lnum != 0 && equalpos(pos, firstpos)) { + subpatnum = FAIL; + } + + if (subpatnum == FAIL || !use_skip) { + // didn't find it or no skip argument + break; + } + firstpos = pos; + + // If the skip expression matches, ignore this match. + { + const pos_T save_pos = curwin->w_cursor; + + curwin->w_cursor = pos; + bool err = false; + const bool do_skip = eval_expr_to_bool(&argvars[4], &err); + curwin->w_cursor = save_pos; + if (err) { + // Evaluating {skip} caused an error, break here. + subpatnum = FAIL; + break; + } + if (!do_skip) { + break; + } + } + } + if (subpatnum != FAIL) { if (flags & SP_SUBPAT) { retval = subpatnum; @@ -8475,13 +8785,9 @@ static int searchpair_cmn(typval_T *argvars, pos_T *match_pos) || argvars[4].v_type == VAR_UNKNOWN) { skip = NULL; } else { + // Type is checked later. skip = &argvars[4]; - if (skip->v_type != VAR_FUNC - && skip->v_type != VAR_PARTIAL - && skip->v_type != VAR_STRING) { - semsg(_(e_invarg2), tv_get_string(&argvars[4])); - goto theend; // Type error. - } + if (argvars[5].v_type != VAR_UNKNOWN) { lnum_stop = tv_get_number_chk(&argvars[5], NULL); if (lnum_stop < 0) { @@ -8592,10 +8898,7 @@ long do_searchpair(const char *spat, const char *mpat, const char *epat, int dir } if (skip != NULL) { - // Empty string means to not use the skip expression. - if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC) { - use_skip = skip->vval.v_string != NULL && *skip->vval.v_string != NUL; - } + use_skip = eval_expr_valid_arg(skip); } save_cursor = curwin->w_cursor; @@ -8868,6 +9171,49 @@ static void f_setbufvar(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } +/// Set the cursor or mark position. +/// If 'charpos' is TRUE, then use the column number as a character offet. +/// Otherwise use the column number as a byte offset. +static void set_position(typval_T *argvars, typval_T *rettv, bool charpos) +{ + pos_T pos; + int fnum; + colnr_T curswant = -1; + + rettv->vval.v_number = -1; + const char *const name = tv_get_string_chk(argvars); + if (name != NULL) { + if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK) { + if (pos.col != MAXCOL && --pos.col < 0) { + pos.col = 0; + } + if (name[0] == '.' && name[1] == NUL) { + // set cursor; "fnum" is ignored + curwin->w_cursor = pos; + if (curswant >= 0) { + curwin->w_curswant = curswant - 1; + curwin->w_set_curswant = false; + } + check_cursor(); + rettv->vval.v_number = 0; + } else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL) { + // set mark + if (setmark_pos((uint8_t)name[1], &pos, fnum) == OK) { + rettv->vval.v_number = 0; + } + } else { + emsg(_(e_invarg)); + } + } + } +} + +/// "setcharpos()" function +static void f_setcharpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + set_position(argvars, rettv, true); +} + static void f_setcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr) { dict_T *d; @@ -8910,6 +9256,12 @@ static void f_setcmdpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } +/// "setcursorcharpos" function +static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + set_cursorpos(argvars, rettv, true); +} + /// "setenv()" function static void f_setenv(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -9166,41 +9518,10 @@ static void f_setmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } -/* - * "setpos()" function - */ +/// "setpos()" function static void f_setpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) { - pos_T pos; - int fnum; - colnr_T curswant = -1; - - rettv->vval.v_number = -1; - const char *const name = tv_get_string_chk(argvars); - if (name != NULL) { - if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK) { - if (pos.col != MAXCOL && --pos.col < 0) { - pos.col = 0; - } - if (name[0] == '.' && name[1] == NUL) { - // set cursor; "fnum" is ignored - curwin->w_cursor = pos; - if (curswant >= 0) { - curwin->w_curswant = curswant - 1; - curwin->w_set_curswant = false; - } - check_cursor(); - rettv->vval.v_number = 0; - } else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL) { - // set mark - if (setmark_pos((uint8_t)name[1], &pos, fnum) == OK) { - rettv->vval.v_number = 0; - } - } else { - emsg(_(e_invarg)); - } - } - } + set_position(argvars, rettv, false); } /* @@ -10215,6 +10536,10 @@ static void f_stdioopen(typval_T *argvars, typval_T *rettv, FunPtr fptr) if (!tv_dict_get_callback(opts, S_LEN("on_stdin"), &on_stdin.cb)) { return; } + if (!tv_dict_get_callback(opts, S_LEN("on_print"), &on_print)) { + return; + } + on_stdin.buffered = tv_dict_get_number(opts, "stdin_buffered"); if (on_stdin.buffered && on_stdin.cb.type == kCallbackNone) { on_stdin.self = opts; @@ -10669,7 +10994,7 @@ static void f_stridx(typval_T *argvars, typval_T *rettv, FunPtr fptr) /* * "string()" function */ -void f_string(typval_T *argvars, typval_T *rettv, FunPtr fptr) +static void f_string(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = (char_u *)encode_tv2string(&argvars[0], NULL); @@ -11879,7 +12204,7 @@ static void f_virtcol(typval_T *argvars, typval_T *rettv, FunPtr fptr) pos_T *fp; int fnum = curbuf->b_fnum; - fp = var2fpos(&argvars[0], FALSE, &fnum); + fp = var2fpos(&argvars[0], false, &fnum, false); if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count && fnum == curbuf->b_fnum) { // Limit the column to a valid value, getvvcol() doesn't check. @@ -11985,6 +12310,42 @@ static void f_win_id2win(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = win_id2win(argvars); } +/// "win_move_separator()" function +static void f_win_move_separator(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + win_T *wp; + int offset; + + rettv->vval.v_number = false; + + wp = find_win_by_nr_or_id(&argvars[0]); + if (wp == NULL || wp->w_floating) { + return; + } + + offset = (int)tv_get_number(&argvars[1]); + win_drag_vsep_line(wp, offset); + rettv->vval.v_number = true; +} + +/// "win_move_statusline()" function +static void f_win_move_statusline(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + win_T *wp; + int offset; + + rettv->vval.v_number = false; + + wp = find_win_by_nr_or_id(&argvars[0]); + if (wp == NULL || wp->w_floating) { + return; + } + + offset = (int)tv_get_number(&argvars[1]); + win_drag_status_line(wp, offset); + rettv->vval.v_number = true; +} + /// "winbufnr(nr)" function static void f_winbufnr(typval_T *argvars, typval_T *rettv, FunPtr fptr) { diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 42ac1839e6..6f8b032d41 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -3091,7 +3091,7 @@ linenr_T tv_get_lnum(const typval_T *const tv) linenr_T lnum = (linenr_T)tv_get_number_chk(tv, NULL); if (lnum == 0) { // No valid number, try using same function as line() does. int fnum; - pos_T *const fp = var2fpos(tv, true, &fnum); + pos_T *const fp = var2fpos(tv, true, &fnum, false); if (fp != NULL) { lnum = fp->lnum; } @@ -3205,8 +3205,9 @@ const char *tv_get_string_buf_chk(const typval_T *const tv, char *const buf) case VAR_BLOB: case VAR_UNKNOWN: emsg(_(str_errors[tv->v_type])); - return false; + return NULL; } + abort(); return NULL; } diff --git a/src/nvim/eval/typval.h b/src/nvim/eval/typval.h index d1275d6512..ad01c01499 100644 --- a/src/nvim/eval/typval.h +++ b/src/nvim/eval/typval.h @@ -81,7 +81,8 @@ typedef struct { } data; CallbackType type; } Callback; -#define CALLBACK_NONE ((Callback){ .type = kCallbackNone }) +#define CALLBACK_INIT { .type = kCallbackNone } +#define CALLBACK_NONE ((Callback)CALLBACK_INIT) /// Structure holding dictionary watcher typedef struct dict_watcher { diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 95390b1a70..49bf9972b1 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -814,7 +814,11 @@ void ex_retab(exarg_T *eap) // len is actual number of white characters used len = num_spaces + num_tabs; old_len = (long)STRLEN(ptr); - long new_len = old_len - col + start_col + len + 1; + const long new_len = old_len - col + start_col + len + 1; + if (new_len <= 0 || new_len >= MAXCOL) { + emsg(_(e_resulting_text_too_long)); + break; + } new_line = xmalloc(new_len); if (start_col > 0) { @@ -847,6 +851,10 @@ void ex_retab(exarg_T *eap) break; } vcol += win_chartabsize(curwin, ptr + col, (colnr_T)vcol); + if (vcol >= MAXCOL) { + emsg(_(e_resulting_text_too_long)); + break; + } col += utfc_ptr2len(ptr + col); } if (new_line == NULL) { // out of memory @@ -980,8 +988,10 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) foldMoveRange(win, &win->w_folds, line1, line2, dest); } } - curbuf->b_op_start.lnum = dest - num_lines + 1; - curbuf->b_op_end.lnum = dest; + if (!cmdmod.lockmarks) { + curbuf->b_op_start.lnum = dest - num_lines + 1; + curbuf->b_op_end.lnum = dest; + } line_off = -num_lines; byte_off = -extent_byte; } else { @@ -991,10 +1001,14 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) foldMoveRange(win, &win->w_folds, dest + 1, line1 - 1, line2); } } - curbuf->b_op_start.lnum = dest + 1; - curbuf->b_op_end.lnum = dest + num_lines; + if (!cmdmod.lockmarks) { + curbuf->b_op_start.lnum = dest + 1; + curbuf->b_op_end.lnum = dest + num_lines; + } + } + if (!cmdmod.lockmarks) { + curbuf->b_op_start.col = curbuf->b_op_end.col = 0; } - curbuf->b_op_start.col = curbuf->b_op_end.col = 0; mark_adjust_nofold(last_line - num_lines + 1, last_line, -(last_line - dest - extra), 0L, kExtmarkNOOP); @@ -1057,9 +1071,11 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n) char_u *p; count = line2 - line1 + 1; - curbuf->b_op_start.lnum = n + 1; - curbuf->b_op_end.lnum = n + count; - curbuf->b_op_start.col = curbuf->b_op_end.col = 0; + if (!cmdmod.lockmarks) { + curbuf->b_op_start.lnum = n + 1; + curbuf->b_op_end.lnum = n + count; + curbuf->b_op_start.col = curbuf->b_op_end.col = 0; + } /* * there are three situations: @@ -1099,6 +1115,9 @@ void ex_copy(linenr_T line1, linenr_T line2, linenr_T n) } appended_lines_mark(n, count); + if (VIsual_active) { + check_pos(curbuf, &VIsual); + } msgmore((long)count); } @@ -1269,12 +1288,18 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, char_u *cmd_buf; buf_T *old_curbuf = curbuf; int shell_flags = 0; + const pos_T orig_start = curbuf->b_op_start; + const pos_T orig_end = curbuf->b_op_end; const int stmp = p_stmp; if (*cmd == NUL) { // no filter command return; } + const bool save_lockmarks = cmdmod.lockmarks; + // Temporarily disable lockmarks since that's needed to propagate changed + // regions of the buffer for foldUpdate(), linecount, etc. + cmdmod.lockmarks = false; cursor_save = curwin->w_cursor; linecount = line2 - line1 + 1; @@ -1349,7 +1374,7 @@ static void do_filter(linenr_T line1, linenr_T line2, exarg_T *eap, char_u *cmd, ui_cursor_goto(Rows - 1, 0); if (do_out) { - if (u_save((line2), (linenr_T)(line2 + 1)) == FAIL) { + if (u_save(line2, (linenr_T)(line2 + 1)) == FAIL) { xfree(cmd_buf); goto error; } @@ -1455,10 +1480,15 @@ error: filterend: + cmdmod.lockmarks = save_lockmarks; if (curbuf != old_curbuf) { no_wait_return--; emsg(_("E135: *Filter* Autocommands must not change current buffer")); + } else if (cmdmod.lockmarks) { + curbuf->b_op_start = orig_start; + curbuf->b_op_end = orig_end; } + if (itmp != NULL) { os_remove((char *)itmp); } @@ -3031,14 +3061,15 @@ void ex_append(exarg_T *eap) // eap->line2 pointed to the end of the buffer and nothing was appended) // "end" is set to lnum when something has been appended, otherwise // it is the same as "start" -- Acevedo - curbuf->b_op_start.lnum = (eap->line2 < curbuf->b_ml.ml_line_count) ? - eap->line2 + 1 : curbuf->b_ml.ml_line_count; - if (eap->cmdidx != CMD_append) { - --curbuf->b_op_start.lnum; + if (!cmdmod.lockmarks) { + curbuf->b_op_start.lnum + = (eap->line2 < curbuf->b_ml.ml_line_count) ? eap->line2 + 1 : curbuf->b_ml.ml_line_count; + if (eap->cmdidx != CMD_append) { + curbuf->b_op_start.lnum--; + } + curbuf->b_op_end.lnum = (eap->line2 < lnum) ? lnum : curbuf->b_op_start.lnum; + curbuf->b_op_start.col = curbuf->b_op_end.col = 0; } - curbuf->b_op_end.lnum = (eap->line2 < lnum) - ? lnum : curbuf->b_op_start.lnum; - curbuf->b_op_start.col = curbuf->b_op_end.col = 0; curwin->w_cursor.lnum = lnum; check_cursor_lnum(); beginline(BL_SOL | BL_FIX); @@ -3629,8 +3660,14 @@ static buf_T *do_sub(exarg_T *eap, proftime_T timeout, bool do_buf_event, handle // We do it here once to avoid it to be replaced over and over again. // But don't do it when it starts with "\=", then it's an expression. assert(sub != NULL); + + bool sub_needs_free = false; if (!(sub[0] == '\\' && sub[1] == '=')) { + char_u *source = sub; sub = regtilde(sub, p_magic); + // When previewing, the new pattern allocated by regtilde() needs to be freed + // in this function because it will not be used or freed by regtilde() later. + sub_needs_free = preview && sub != source; } // Check for a match on each line. @@ -4351,10 +4388,12 @@ skip: } if (sub_nsubs > start_nsubs) { - // Set the '[ and '] marks. - curbuf->b_op_start.lnum = eap->line1; - curbuf->b_op_end.lnum = line2; - curbuf->b_op_start.col = curbuf->b_op_end.col = 0; + if (!cmdmod.lockmarks) { + // Set the '[ and '] marks. + curbuf->b_op_start.lnum = eap->line1; + curbuf->b_op_end.lnum = line2; + curbuf->b_op_start.col = curbuf->b_op_end.col = 0; + } if (!global_busy) { // when interactive leave cursor on the match @@ -4425,6 +4464,10 @@ skip: kv_destroy(preview_lines.subresults); + if (sub_needs_free) { + xfree(sub); + } + return preview_buf; #undef ADJUST_SUB_FIRSTLNUM #undef PUSH_PREVIEW_LINES @@ -5068,8 +5111,7 @@ int find_help_tags(const char_u *arg, int *num_matches, char_u ***matches, bool && ((arg[1] != NUL && arg[2] == NUL) || (vim_strchr((char_u *)"%_z@", arg[1]) != NULL && arg[2] != NUL))) { - STRCPY(d, "/\\\\"); - STRCPY(d + 3, arg + 1); + vim_snprintf((char *)d, IOSIZE, "/\\\\%s", arg + 1); // Check for "/\\_$", should be "/\\_\$" if (d[3] == '_' && d[4] == '$') { STRCPY(d + 4, "\\$"); @@ -5822,7 +5864,7 @@ void ex_helpclose(exarg_T *eap) { FOR_ALL_WINDOWS_IN_TAB(win, curtab) { if (bt_help(win->w_buffer)) { - win_close(win, false); + win_close(win, false, eap->forceit); return; } } @@ -6107,12 +6149,14 @@ char_u *skip_vimgrep_pat(char_u *p, char_u **s, int *flags) p++; // Find the flags - while (*p == 'g' || *p == 'j') { + while (*p == 'g' || *p == 'j' || *p == 'f') { if (flags != NULL) { if (*p == 'g') { *flags |= VGR_GLOBAL; - } else { + } else if (*p == 'j') { *flags |= VGR_NOJUMP; + } else { + *flags |= VGR_FUZZY; } } p++; diff --git a/src/nvim/ex_cmds.lua b/src/nvim/ex_cmds.lua index c388373ac1..c2d40c8bb7 100644 --- a/src/nvim/ex_cmds.lua +++ b/src/nvim/ex_cmds.lua @@ -2097,19 +2097,19 @@ module.cmds = { command='python', flags=bit.bor(RANGE, EXTRA, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_python', + func='ex_python3', }, { command='pydo', flags=bit.bor(RANGE, DFLALL, EXTRA, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_pydo', + func='ex_pydo3', }, { command='pyfile', flags=bit.bor(RANGE, FILE1, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_pyfile', + func='ex_py3file', }, { command='py3', @@ -2139,25 +2139,25 @@ module.cmds = { command='pyx', flags=bit.bor(RANGE, EXTRA, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_pyx', + func='ex_python3', }, { command='pyxdo', flags=bit.bor(RANGE, DFLALL, EXTRA, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_pyxdo', + func='ex_pydo3', }, { command='pythonx', flags=bit.bor(RANGE, EXTRA, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_pyx', + func='ex_python3', }, { command='pyxfile', flags=bit.bor(RANGE, FILE1, NEEDARG, CMDWIN), addr_type='ADDR_LINES', - func='ex_pyxfile', + func='ex_py3file', }, { command='quit', @@ -2413,7 +2413,7 @@ module.cmds = { }, { command='set', - flags=bit.bor(TRLBAR, EXTRA, CMDWIN, SBOXOK), + flags=bit.bor(BANG, TRLBAR, EXTRA, CMDWIN, SBOXOK), addr_type='ADDR_NONE', func='ex_set', }, @@ -2425,13 +2425,13 @@ module.cmds = { }, { command='setglobal', - flags=bit.bor(TRLBAR, EXTRA, CMDWIN, SBOXOK), + flags=bit.bor(BANG, TRLBAR, EXTRA, CMDWIN, SBOXOK), addr_type='ADDR_NONE', func='ex_set', }, { command='setlocal', - flags=bit.bor(TRLBAR, EXTRA, CMDWIN, SBOXOK), + flags=bit.bor(BANG, TRLBAR, EXTRA, CMDWIN, SBOXOK), addr_type='ADDR_NONE', func='ex_set', }, diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 2e8d39ec30..090422088a 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -12,6 +12,7 @@ #include <string.h> #include "nvim/ascii.h" +#include "nvim/globals.h" #include "nvim/vim.h" #ifdef HAVE_LOCALE_H # include <locale.h> @@ -135,21 +136,6 @@ void ex_profile(exarg_T *eap) } } -void ex_python(exarg_T *eap) -{ - script_host_execute("python", eap); -} - -void ex_pyfile(exarg_T *eap) -{ - script_host_execute_file("python", eap); -} - -void ex_pydo(exarg_T *eap) -{ - script_host_do_range("python", eap); -} - void ex_ruby(exarg_T *eap) { script_host_execute("ruby", eap); @@ -1613,7 +1599,7 @@ void ex_compiler(exarg_T *eap) if (old_cur_comp != NULL) { old_cur_comp = vim_strsave(old_cur_comp); } - do_cmdline_cmd("command -nargs=* CompilerSet setlocal <args>"); + do_cmdline_cmd("command -nargs=* -keepscript CompilerSet setlocal <args>"); } do_unlet(S_LEN("g:current_compiler"), true); do_unlet(S_LEN("b:current_compiler"), true); @@ -1660,126 +1646,6 @@ void ex_options(exarg_T *eap) cmd_source((char_u *)SYS_OPTWIN_FILE, NULL); } -// Detect Python 3 or 2, and initialize 'pyxversion'. -void init_pyxversion(void) -{ - if (p_pyx == 0) { - if (eval_has_provider("python3")) { - p_pyx = 3; - } else if (eval_has_provider("python")) { - p_pyx = 2; - } - } -} - -// Does a file contain one of the following strings at the beginning of any -// line? -// "#!(any string)python2" => returns 2 -// "#!(any string)python3" => returns 3 -// "# requires python 2.x" => returns 2 -// "# requires python 3.x" => returns 3 -// otherwise return 0. -static int requires_py_version(char_u *filename) -{ - FILE *file; - int requires_py_version = 0; - int i, lines; - - lines = (int)p_mls; - if (lines < 0) { - lines = 5; - } - - file = os_fopen((char *)filename, "r"); - if (file != NULL) { - for (i = 0; i < lines; i++) { - if (vim_fgets(IObuff, IOSIZE, file)) { - break; - } - if (i == 0 && IObuff[0] == '#' && IObuff[1] == '!') { - // Check shebang. - if (strstr((char *)IObuff + 2, "python2") != NULL) { - requires_py_version = 2; - break; - } - if (strstr((char *)IObuff + 2, "python3") != NULL) { - requires_py_version = 3; - break; - } - } - IObuff[21] = '\0'; - if (STRCMP("# requires python 2.x", IObuff) == 0) { - requires_py_version = 2; - break; - } - if (STRCMP("# requires python 3.x", IObuff) == 0) { - requires_py_version = 3; - break; - } - } - fclose(file); - } - return requires_py_version; -} - - -// Source a python file using the requested python version. -static void source_pyx_file(exarg_T *eap, char_u *fname) -{ - exarg_T ex; - long int v = requires_py_version(fname); - - init_pyxversion(); - if (v == 0) { - // user didn't choose a preference, 'pyx' is used - v = p_pyx; - } - - // now source, if required python version is not supported show - // unobtrusive message. - if (eap == NULL) { - memset(&ex, 0, sizeof(ex)); - } else { - ex = *eap; - } - ex.arg = fname; - ex.cmd = (char_u *)(v == 2 ? "pyfile" : "pyfile3"); - - if (v == 2) { - ex_pyfile(&ex); - } else { - ex_py3file(&ex); - } -} - -// ":pyxfile {fname}" -void ex_pyxfile(exarg_T *eap) -{ - source_pyx_file(eap, eap->arg); -} - -// ":pyx" -void ex_pyx(exarg_T *eap) -{ - init_pyxversion(); - if (p_pyx == 2) { - ex_python(eap); - } else { - ex_python3(eap); - } -} - -// ":pyxdo" -void ex_pyxdo(exarg_T *eap) -{ - init_pyxversion(); - if (p_pyx == 2) { - ex_pydo(eap); - } else { - ex_pydo3(eap); - } -} - /// ":source [{fname}]" void ex_source(exarg_T *eap) { @@ -2323,9 +2189,13 @@ void ex_scriptnames(exarg_T *eap) for (int i = 1; i <= script_items.ga_len && !got_int; i++) { if (SCRIPT_ITEM(i).sn_name != NULL) { - home_replace(NULL, SCRIPT_ITEM(i).sn_name, - NameBuff, MAXPATHL, true); - smsg("%3d: %s", i, NameBuff); + home_replace(NULL, SCRIPT_ITEM(i).sn_name, NameBuff, MAXPATHL, true); + vim_snprintf((char *)IObuff, IOSIZE, "%3d: %s", i, NameBuff); + if (!message_filtered(IObuff)) { + msg_putchar('\n'); + msg_outtrans(IObuff); + line_breakcheck(); + } } } } diff --git a/src/nvim/ex_cmds_defs.h b/src/nvim/ex_cmds_defs.h index e5eab61f9e..eaf5f627b6 100644 --- a/src/nvim/ex_cmds_defs.h +++ b/src/nvim/ex_cmds_defs.h @@ -61,6 +61,7 @@ // current buffer is locked #define EX_MODIFY 0x100000 // forbidden in non-'modifiable' buffer #define EX_FLAGS 0x200000 // allow flags after count in argument +#define EX_KEEPSCRIPT 0x4000000 // keep sctx of where command was invoked #define EX_FILES (EX_XFILE | EX_EXTRA) // multiple extra files allowed #define EX_FILE1 (EX_FILES | EX_NOSPC) // 1 file, defaults to current file #define EX_WORD1 (EX_EXTRA | EX_NOSPC) // one extra word allowed diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 0d82406a0a..4dba0b97ed 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -78,6 +78,10 @@ #include "nvim/vim.h" #include "nvim/window.h" +static char *e_no_such_user_defined_command_str = N_("E184: No such user-defined command: %s"); +static char *e_no_such_user_defined_command_in_current_buffer_str + = N_("E1237: No such user-defined command in current buffer: %s"); + static int quitmore = 0; static bool ex_pressedreturn = false; @@ -1758,7 +1762,9 @@ static char_u *do_one_cmd(char_u **cmdlinep, int flags, cstack_T *cstack, LineGe ea.regname = *ea.arg++; // for '=' register: accept the rest of the line as an expression if (ea.arg[-1] == '=' && ea.arg[0] != NUL) { - set_expr_line(vim_strsave(ea.arg)); + if (!ea.skip) { + set_expr_line(vim_strsave(ea.arg)); + } ea.arg += STRLEN(ea.arg); } ea.arg = skipwhite(ea.arg); @@ -2697,10 +2703,8 @@ static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int * bool amb_local = false; // Found ambiguous buffer-local command, // only full match global is accepted. - /* - * Look for buffer-local user commands first, then global ones. - */ - gap = &curbuf->b_ucmds; + // Look for buffer-local user commands first, then global ones. + gap = is_in_cmdwin() ? &prevwin->w_buffer->b_ucmds : &curbuf->b_ucmds; for (;;) { for (j = 0; j < gap->ga_len; j++) { uc = USER_CMD_GA(gap, j); @@ -2891,13 +2895,17 @@ void f_fullcommand(typval_T *argvars, typval_T *rettv, FunPtr fptr) exarg_T ea; char_u *name = argvars[0].vval.v_string; - while (name[0] != NUL && name[0] == ':') { + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + if (name == NULL) { + return; + } + + while (*name == ':') { name++; } name = skip_range(name, NULL); - rettv->v_type = VAR_STRING; - ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name; ea.cmdidx = (cmdidx_T)0; char_u *p = find_command(&ea, NULL); @@ -2906,7 +2914,7 @@ void f_fullcommand(typval_T *argvars, typval_T *rettv, FunPtr fptr) } rettv->vval.v_string = vim_strsave(IS_USER_CMDIDX(ea.cmdidx) - ? get_user_commands(NULL, ea.useridx) + ? get_user_command_name(ea.useridx, ea.cmdidx) : cmdnames[ea.cmdidx].cmd_name); } @@ -4370,7 +4378,7 @@ static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep) ++i; } len = (int)STRLEN(p); - new_cmdline = xmalloc(STRLEN(program) + i * (len - 2) + 1); + new_cmdline = xmalloc(STRLEN(program) + (size_t)i * (len - 2) + 1); ptr = new_cmdline; while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) { i = (int)(pos - program); @@ -5151,7 +5159,7 @@ static int check_more(int message, bool forceit) char_u *get_command_name(expand_T *xp, int idx) { if (idx >= CMD_SIZE) { - return get_user_command_name(idx); + return expand_user_command_name(idx); } return cmdnames[idx].cmd_name; } @@ -5343,9 +5351,7 @@ static void uc_list(char_u *name, size_t name_len) uint32_t a; // In cmdwin, the alternative buffer should be used. - garray_T *gap = (cmdwin_type != 0 && get_cmdline_type() == NUL) - ? &prevwin->w_buffer->b_ucmds - : &curbuf->b_ucmds; + garray_T *gap = is_in_cmdwin() ? &prevwin->w_buffer->b_ucmds : &curbuf->b_ucmds; for (;;) { for (i = 0; i < gap->ga_len; i++) { cmd = USER_CMD_GA(gap, i); @@ -5517,6 +5523,8 @@ static int uc_scan_attr(char_u *attr, size_t len, uint32_t *argt, long *def, int *flags |= UC_BUFFER; } else if (STRNICMP(attr, "register", len) == 0) { *argt |= EX_REGSTR; + } else if (STRNICMP(attr, "keepscript", len) == 0) { + *argt |= EX_KEEPSCRIPT; } else if (STRNICMP(attr, "bar", len) == 0) { *argt |= EX_TRLBAR; } else { @@ -5731,26 +5739,36 @@ static void ex_delcommand(exarg_T *eap) { int i = 0; ucmd_T *cmd = NULL; - int cmp = -1; + int res = -1; garray_T *gap; + const char_u *arg = eap->arg; + bool buffer_only = false; + + if (STRNCMP(arg, "-buffer", 7) == 0 && ascii_iswhite(arg[7])) { + buffer_only = true; + arg = skipwhite(arg + 7); + } gap = &curbuf->b_ucmds; for (;;) { for (i = 0; i < gap->ga_len; i++) { cmd = USER_CMD_GA(gap, i); - cmp = STRCMP(eap->arg, cmd->uc_name); - if (cmp <= 0) { + res = STRCMP(arg, cmd->uc_name); + if (res <= 0) { break; } } - if (gap == &ucmds || cmp == 0) { + if (gap == &ucmds || res == 0 || buffer_only) { break; } gap = &ucmds; } - if (cmp != 0) { - semsg(_("E184: No such user-defined command: %s"), eap->arg); + if (res != 0) { + semsg(_(buffer_only + ? e_no_such_user_defined_command_in_current_buffer_str + : e_no_such_user_defined_command_str), + arg); return; } @@ -6028,7 +6046,7 @@ static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, break; } - case ct_MODS: { + case ct_MODS: result = quote ? 2 : 0; if (buf != NULL) { if (quote) { @@ -6044,7 +6062,6 @@ static size_t uc_check_code(char_u *code, size_t len, char_u *buf, ucmd_T *cmd, *buf = '"'; } break; - } case ct_REGISTER: result = eap->regname ? 1 : 0; @@ -6205,7 +6222,6 @@ static void do_ucmd(exarg_T *eap) // K_SPECIAL has been put in the buffer as K_SPECIAL // KS_SPECIAL KE_FILLER, like for mappings, but // do_cmdline() doesn't handle that, so convert it back. - // Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. len = ksp - p; if (len > 0) { memmove(q, p, len); @@ -6258,15 +6274,19 @@ static void do_ucmd(exarg_T *eap) buf = xmalloc(totlen + 1); } - current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid; + if ((cmd->uc_argt & EX_KEEPSCRIPT) == 0) { + current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid; + } (void)do_cmdline(buf, eap->getline, eap->cookie, DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); - current_sctx = save_current_sctx; + if ((cmd->uc_argt & EX_KEEPSCRIPT) == 0) { + current_sctx = save_current_sctx; + } xfree(buf); xfree(split_buf); } -static char_u *get_user_command_name(int idx) +static char_u *expand_user_command_name(int idx) { return get_user_commands(NULL, idx - CMD_SIZE); } @@ -6285,9 +6305,7 @@ char_u *get_user_commands(expand_T *xp FUNC_ATTR_UNUSED, int idx) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { // In cmdwin, the alternative buffer should be used. - const buf_T *const buf = (cmdwin_type != 0 && get_cmdline_type() == NUL) - ? prevwin->w_buffer - : curbuf; + const buf_T *const buf = is_in_cmdwin() ? prevwin->w_buffer : curbuf; if (idx < buf->b_ucmds.ga_len) { return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name; @@ -6299,6 +6317,24 @@ char_u *get_user_commands(expand_T *xp FUNC_ATTR_UNUSED, int idx) return NULL; } +// Get the name of user command "idx". "cmdidx" can be CMD_USER or +// CMD_USER_BUF. +// Returns NULL if the command is not found. +static char_u *get_user_command_name(int idx, int cmdidx) +{ + if (cmdidx == CMD_USER && idx < ucmds.ga_len) { + return USER_CMD(idx)->uc_name; + } + if (cmdidx == CMD_USER_BUF) { + // In cmdwin, the alternative buffer should be used. + buf_T *buf = is_in_cmdwin() ? prevwin->w_buffer : curbuf; + if (idx < buf->b_ucmds.ga_len) { + return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name; + } + } + return NULL; +} + /* * Function given to ExpandGeneric() to obtain the list of user command * attributes. @@ -6307,7 +6343,7 @@ char_u *get_user_cmd_flags(expand_T *xp, int idx) { static char *user_cmd_flags[] = { "addr", "bang", "bar", "buffer", "complete", "count", - "nargs", "range", "register" }; + "nargs", "range", "register", "keepscript" }; if (idx >= (int)ARRAY_SIZE(user_cmd_flags)) { return NULL; @@ -6583,7 +6619,7 @@ static void ex_quit(exarg_T *eap) } not_exiting(); // close window; may free buffer - win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit); + win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit, eap->forceit); } } @@ -6698,7 +6734,7 @@ void ex_win_close(int forceit, win_T *win, tabpage_T *tp) // free buffer when not hiding it or when it's a scratch buffer if (tp == NULL) { - win_close(win, !need_hide && !buf_hide(buf)); + win_close(win, !need_hide && !buf_hide(buf), forceit); } else { win_close_othertab(win, !need_hide && !buf_hide(buf), tp); } @@ -6862,7 +6898,7 @@ static void ex_hide(exarg_T *eap) // ":hide" or ":hide | cmd": hide current window if (!eap->skip) { if (eap->addr_count == 0) { - win_close(curwin, false); // don't free buffer + win_close(curwin, false, eap->forceit); // don't free buffer } else { int winnr = 0; win_T *win = NULL; @@ -6877,7 +6913,7 @@ static void ex_hide(exarg_T *eap) if (win == NULL) { win = lastwin; } - win_close(win, false); + win_close(win, false, eap->forceit); } } } @@ -6935,7 +6971,7 @@ static void ex_exit(exarg_T *eap) } not_exiting(); // Quit current window, may free the buffer. - win_close(curwin, !buf_hide(curwin->w_buffer)); + win_close(curwin, !buf_hide(curwin->w_buffer), eap->forceit); } } @@ -7536,7 +7572,7 @@ void do_exedit(exarg_T *eap, win_T *old_curwin) // Reset the error/interrupt/exception state here so that // aborting() returns FALSE when closing a window. enter_cleanup(&cs); - win_close(curwin, !need_hide && !buf_hide(curbuf)); + win_close(curwin, !need_hide && !buf_hide(curbuf), false); // Restore the error/interrupt/exception state if not // discarded by a new aborting error, interrupt, or @@ -7799,14 +7835,11 @@ void post_chdir(CdScope scope, bool trigger_dirchanged) /// @return true if the directory is successfully changed. bool changedir_func(char_u *new_dir, CdScope scope) { - char_u *tofree; - char_u *pdir = NULL; - bool retval = false; - if (new_dir == NULL || allbuf_locked()) { return false; } + char_u *pdir = NULL; // ":cd -": Change to previous directory if (STRCMP(new_dir, "-") == 0) { pdir = get_prevdir(scope); @@ -7817,26 +7850,12 @@ bool changedir_func(char_u *new_dir, CdScope scope) new_dir = pdir; } - // Free the previous directory - tofree = get_prevdir(scope); - if (os_dirname(NameBuff, MAXPATHL) == OK) { pdir = vim_strsave(NameBuff); } else { pdir = NULL; } - switch (scope) { - case kCdScopeTabpage: - curtab->tp_prevdir = pdir; - break; - case kCdScopeWindow: - curwin->w_prevdir = pdir; - break; - default: - prev_dir = pdir; - } - // For UNIX ":cd" means: go to home directory. // On other systems too if 'cdhome' is set. #if defined(UNIX) @@ -7849,17 +7868,30 @@ bool changedir_func(char_u *new_dir, CdScope scope) new_dir = NameBuff; } - bool dir_differs = new_dir == NULL || pdir == NULL - || pathcmp((char *)pdir, (char *)new_dir, -1) != 0; - if (new_dir != NULL && (!dir_differs || vim_chdir(new_dir) == 0)) { - post_chdir(scope, dir_differs); - retval = true; - } else { + bool dir_differs = pdir == NULL || pathcmp((char *)pdir, (char *)new_dir, -1) != 0; + if (dir_differs && vim_chdir(new_dir) != 0) { emsg(_(e_failed)); + xfree(pdir); + return false; } - xfree(tofree); - return retval; + char_u **pp; + switch (scope) { + case kCdScopeTabpage: + pp = &curtab->tp_prevdir; + break; + case kCdScopeWindow: + pp = &curwin->w_prevdir; + break; + default: + pp = &prev_dir; + } + xfree(*pp); + *pp = pdir; + + post_chdir(scope, dir_differs); + + return true; } /// ":cd", ":tcd", ":lcd", ":chdir", "tchdir" and ":lchdir". @@ -8107,6 +8139,7 @@ static void ex_put(exarg_T *eap) eap->forceit = TRUE; } curwin->w_cursor.lnum = eap->line2; + check_cursor_col(); do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1, PUT_LINE|PUT_CURSLINE); } @@ -8628,7 +8661,7 @@ static void ex_normal(exarg_T *eap) return; } - // vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do + // vgetc() expects K_SPECIAL to have been escaped. Don't do // this for the K_SPECIAL leading byte, otherwise special keys will not // work. { @@ -8637,8 +8670,7 @@ static void ex_normal(exarg_T *eap) // Count the number of characters to be escaped. for (p = eap->arg; *p != NUL; p++) { for (l = utfc_ptr2len(p) - 1; l > 0; l--) { - if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI - ) { + if (*++p == K_SPECIAL) { // trailbyte K_SPECIAL len += 2; } } @@ -9547,23 +9579,34 @@ static void ex_filetype(exarg_T *eap) } } -/// Set all :filetype options ON if user did not explicitly set any to OFF. -void filetype_maybe_enable(void) +/// Source ftplugin.vim and indent.vim to create the necessary FileType +/// autocommands. We do this separately from filetype.vim so that these +/// autocommands will always fire first (and thus can be overriden) while still +/// allowing general filetype detection to be disabled in the user's init file. +void filetype_plugin_enable(void) { - if (filetype_detect == kNone) { - source_runtime(FILETYPE_FILE, true); - filetype_detect = kTrue; - } if (filetype_plugin == kNone) { - source_runtime(FTPLUGIN_FILE, true); + source_runtime(FTPLUGIN_FILE, DIP_ALL); filetype_plugin = kTrue; } if (filetype_indent == kNone) { - source_runtime(INDENT_FILE, true); + source_runtime(INDENT_FILE, DIP_ALL); filetype_indent = kTrue; } } +/// Enable filetype detection if the user did not explicitly disable it. +void filetype_maybe_enable(void) +{ + if (filetype_detect == kNone) { + // Normally .vim files are sourced before .lua files when both are + // supported, but we reverse the order here because we want the Lua + // autocommand to be defined first so that it runs first + source_runtime(FILETYPE_FILE, DIP_ALL); + filetype_detect = kTrue; + } +} + /// ":setfiletype [FALLBACK] {name}" static void ex_setfiletype(exarg_T *eap) { @@ -9590,18 +9633,6 @@ static void ex_digraphs(exarg_T *eap) } } -static void ex_set(exarg_T *eap) -{ - int flags = 0; - - if (eap->cmdidx == CMD_setlocal) { - flags = OPT_LOCAL; - } else if (eap->cmdidx == CMD_setglobal) { - flags = OPT_GLOBAL; - } - (void)do_set(eap->arg, flags); -} - void set_no_hlsearch(bool flag) { no_hlsearch = flag; @@ -9836,6 +9867,7 @@ Dictionary commands_array(buf_T *buf) PUT(d, "bang", BOOLEAN_OBJ(!!(cmd->uc_argt & EX_BANG))); PUT(d, "bar", BOOLEAN_OBJ(!!(cmd->uc_argt & EX_TRLBAR))); PUT(d, "register", BOOLEAN_OBJ(!!(cmd->uc_argt & EX_REGSTR))); + PUT(d, "keepscript", BOOLEAN_OBJ(!!(cmd->uc_argt & EX_KEEPSCRIPT))); switch (cmd->uc_argt & (EX_EXTRA | EX_NOSPC | EX_NEEDARG)) { case 0: diff --git a/src/nvim/ex_eval.c b/src/nvim/ex_eval.c index b1c59a607c..851828afcf 100644 --- a/src/nvim/ex_eval.c +++ b/src/nvim/ex_eval.c @@ -599,7 +599,7 @@ static void catch_exception(except_T *excp) { excp->caught = caught_stack; caught_stack = excp; - set_vim_var_string(VV_EXCEPTION, (char *)excp->value, -1); + set_vim_var_string(VV_EXCEPTION, excp->value, -1); if (*excp->throw_name != NUL) { if (excp->throw_lnum != 0) { vim_snprintf((char *)IObuff, IOSIZE, _("%s, line %" PRId64), @@ -650,7 +650,7 @@ static void finish_exception(except_T *excp) } caught_stack = caught_stack->caught; if (caught_stack != NULL) { - set_vim_var_string(VV_EXCEPTION, (char *)caught_stack->value, -1); + set_vim_var_string(VV_EXCEPTION, caught_stack->value, -1); if (*caught_stack->throw_name != NUL) { if (caught_stack->throw_lnum != 0) { vim_snprintf((char *)IObuff, IOSIZE, @@ -733,7 +733,7 @@ static void report_pending(int action, int pending, void *value) vim_snprintf((char *)IObuff, IOSIZE, mesg, _("Exception")); mesg = (char *)concat_str(IObuff, (char_u *)": %s"); - s = (char *)((except_T *)value)->value; + s = ((except_T *)value)->value; } else if ((pending & CSTP_ERROR) && (pending & CSTP_INTERRUPT)) { s = _("Error and interrupt"); } else if (pending & CSTP_ERROR) { @@ -1620,7 +1620,7 @@ void ex_endtry(exarg_T *eap) // the finally clause. The latter case need not be tested since then // anything pending has already been discarded. bool skip = did_emsg || got_int || current_exception - || !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE); + || !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE); if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY)) { eap->errmsg = get_end_emsg(cstack); diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 78b8e43e65..ed4475eb1a 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -311,7 +311,7 @@ static char_u *get_healthcheck_names(expand_T *xp, int idx) healthchecks.last_gen = last_prompt_id; } return idx < - (int)healthchecks.names.ga_len ? ((char_u **)(healthchecks.names.ga_data))[idx] : NULL; + healthchecks.names.ga_len ? ((char_u **)(healthchecks.names.ga_data))[idx] : NULL; } /// Transform healthcheck file path into it's name. @@ -772,7 +772,7 @@ static uint8_t *command_line_enter(int firstc, long count, int indent) ccline.cmdindent = (s->firstc > 0 ? s->indent : 0); // alloc initial ccline.cmdbuff - alloc_cmdbuff(exmode_active ? 250 : s->indent + 1); + alloc_cmdbuff(indent + 50); ccline.cmdlen = ccline.cmdpos = 0; ccline.cmdbuff[0] = NUL; @@ -6563,7 +6563,7 @@ static int open_cmdwin(void) set_bufref(&bufref, curbuf); win_goto(old_curwin); if (win_valid(wp) && wp != curwin) { - win_close(wp, true); + win_close(wp, true, false); } // win_close() may have already wiped the buffer when 'bh' is @@ -6587,6 +6587,13 @@ static int open_cmdwin(void) return cmdwin_result; } +/// @return true if in the cmdwin, not editing the command line. +bool is_in_cmdwin(void) + FUNC_ATTR_PURE +{ + return cmdwin_type != 0 && get_cmdline_type() == NUL; +} + /// Get script string /// /// Used for commands which accept either `:command script` or diff --git a/src/nvim/ex_session.c b/src/nvim/ex_session.c index a37cad9f2d..ca07174543 100644 --- a/src/nvim/ex_session.c +++ b/src/nvim/ex_session.c @@ -338,14 +338,26 @@ static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int curr // Edit the file. Skip this when ":next" already did it. if (add_edit && (!did_next || wp->w_arg_idx_invalid)) { - char *fname_esc = - ses_escape_fname(ses_get_fname(wp->w_buffer, flagp), flagp); - // - // Load the file. - // - if (wp->w_buffer->b_ffname != NULL - && (!bt_nofile(wp->w_buffer) - || wp->w_buffer->terminal)) { + char *fname_esc = ses_escape_fname(ses_get_fname(wp->w_buffer, flagp), flagp); + if (bt_help(wp->w_buffer)) { + char *curtag = ""; + + // A help buffer needs some options to be set. + // First, create a new empty buffer with "buftype=help". + // Then ":help" will re-use both the buffer and the window and set + // the options, even when "options" is not in 'sessionoptions'. + if (0 < wp->w_tagstackidx && wp->w_tagstackidx <= wp->w_tagstacklen) { + curtag = (char *)wp->w_tagstack[wp->w_tagstackidx - 1].tagname; + } + + if (put_line(fd, "enew | setl bt=help") == FAIL + || fprintf(fd, "help %s", curtag) < 0 || put_eol(fd) == FAIL) { + return FAIL; + } + } else if (wp->w_buffer->b_ffname != NULL + && (!bt_nofile(wp->w_buffer) || wp->w_buffer->terminal)) { + // Load the file. + // Editing a file in this buffer: use ":edit file". // This may have side effects! (e.g., compressed or network file). // @@ -579,6 +591,24 @@ static int makeopens(FILE *fd, char_u *dirnow) return FAIL; } + // Put all buffers into the buffer list. + // Do it very early to preserve buffer order after loading session (which + // can be disrupted by prior `edit` or `tabedit` calls). + FOR_ALL_BUFFERS(buf) { + if (!(only_save_windows && buf->b_nwindows == 0) + && !(buf->b_help && !(ssop_flags & SSOP_HELP)) + && buf->b_fname != NULL + && buf->b_p_bl) { + if (fprintf(fd, "badd +%" PRId64 " ", + buf->b_wininfo == NULL + ? (int64_t)1L + : (int64_t)buf->b_wininfo->wi_fpos.lnum) < 0 + || ses_fname(fd, buf, &ssop_flags, true) == FAIL) { + return FAIL; + } + } + } + // the global argument list if (ses_arglist(fd, "argglobal", &global_alist.al_ga, !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL) { @@ -614,7 +644,10 @@ static int makeopens(FILE *fd, char_u *dirnow) // Similar to ses_win_rec() below, populate the tab pages first so // later local options won't be copied to the new tabs. FOR_ALL_TABS(tp) { - if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL) { + // Use `bufhidden=wipe` to remove empty "placeholder" buffers once + // they are not needed. This prevents creating extra buffers (see + // cause of Vim patch 8.1.0829) + if (tp->tp_next != NULL && put_line(fd, "tabnew +setlocal\\ bufhidden=wipe") == FAIL) { return FAIL; } } @@ -792,25 +825,6 @@ static int makeopens(FILE *fd, char_u *dirnow) return FAIL; } - // Now put the remaining buffers into the buffer list. - // This is near the end, so that when 'hidden' is set we don't create extra - // buffers. If the buffer was already created with another command the - // ":badd" will have no effect. - FOR_ALL_BUFFERS(buf) { - if (!(only_save_windows && buf->b_nwindows == 0) - && !(buf->b_help && !(ssop_flags & SSOP_HELP)) - && buf->b_fname != NULL - && buf->b_p_bl) { - if (fprintf(fd, "badd +%" PRId64 " ", - buf->b_wininfo == NULL - ? (int64_t)1L - : (int64_t)buf->b_wininfo->wi_fpos.lnum) < 0 - || ses_fname(fd, buf, &ssop_flags, true) == FAIL) { - return FAIL; - } - } - } - // // Wipe out an empty unnamed buffer we started in. // diff --git a/src/nvim/extmark.c b/src/nvim/extmark.c index c4d8f75a21..48e57e20e1 100644 --- a/src/nvim/extmark.c +++ b/src/nvim/extmark.c @@ -48,28 +48,29 @@ # include "extmark.c.generated.h" #endif -static ExtmarkNs *buf_ns_ref(buf_T *buf, uint64_t ns_id, bool put) +static uint32_t *buf_ns_ref(buf_T *buf, uint32_t ns_id, bool put) { - return map_ref(uint64_t, ExtmarkNs)(buf->b_extmark_ns, ns_id, put); + return map_ref(uint32_t, uint32_t)(buf->b_extmark_ns, ns_id, put); } /// Create or update an extmark /// /// must not be used during iteration! -/// @returns the internal mark id -uint64_t extmark_set(buf_T *buf, uint64_t ns_id, uint64_t *idp, int row, colnr_T col, int end_row, - colnr_T end_col, Decoration *decor, bool right_gravity, bool end_right_gravity, - ExtmarkOp op) +void extmark_set(buf_T *buf, uint32_t ns_id, uint32_t *idp, int row, colnr_T col, int end_row, + colnr_T end_col, Decoration *decor, bool right_gravity, bool end_right_gravity, + ExtmarkOp op) { - ExtmarkNs *ns = buf_ns_ref(buf, ns_id, true); - assert(ns != NULL); - mtpos_t old_pos; - uint64_t mark = 0; - uint64_t id = idp ? *idp : 0; + uint32_t *ns = buf_ns_ref(buf, ns_id, true); + uint32_t id = idp ? *idp : 0; + bool decor_full = false; uint8_t decor_level = kDecorLevelNone; // no decor if (decor) { + if (kv_size(decor->virt_text) || kv_size(decor->virt_lines)) { + decor_full = true; + decor = xmemdup(decor, sizeof *decor); + } decor_level = kDecorLevelVisible; // decor affects redraw if (kv_size(decor->virt_lines)) { decor_level = kDecorLevelVirtLine; // decor affects horizontal size @@ -77,50 +78,64 @@ uint64_t extmark_set(buf_T *buf, uint64_t ns_id, uint64_t *idp, int row, colnr_T } if (id == 0) { - id = ns->free_id++; + id = ++*ns; } else { - uint64_t old_mark = map_get(uint64_t, uint64_t)(ns->map, id); - if (old_mark) { - if (old_mark & MARKTREE_PAIRED_FLAG || end_row > -1) { + MarkTreeIter itr[1] = { 0 }; + mtkey_t old_mark = marktree_lookup_ns(buf->b_marktree, ns_id, id, false, itr); + if (old_mark.id) { + if (mt_paired(old_mark) || end_row > -1) { extmark_del(buf, ns_id, id); } else { - MarkTreeIter itr[1] = { 0 }; - old_pos = marktree_lookup(buf->b_marktree, old_mark, itr); + // TODO(bfredl): we need to do more if "revising" a decoration mark. assert(itr->node); - if (old_pos.row == row && old_pos.col == col) { - ExtmarkItem it = map_del(uint64_t, ExtmarkItem)(buf->b_extmark_index, - old_mark); - if (it.decor) { - decor_remove(buf, row, row, it.decor); + if (old_mark.pos.row == row && old_mark.pos.col == col) { + if (marktree_decor_level(old_mark) > kDecorLevelNone) { + decor_remove(buf, row, row, old_mark.decor_full); + old_mark.decor_full = NULL; + } + old_mark.flags = 0; + if (decor_full) { + old_mark.decor_full = decor; + } else if (decor) { + old_mark.hl_id = decor->hl_id; + // Workaround: the gcc compiler of functionaltest-lua build + // apparently incapable of handling basic integer constants. + // This can be underanged as soon as we bump minimal gcc version. + old_mark.flags = (uint16_t)(old_mark.flags + | (decor->hl_eol ? (uint16_t)MT_FLAG_HL_EOL : (uint16_t)0)); + old_mark.priority = decor->priority; } - mark = marktree_revise(buf->b_marktree, itr, decor_level); + marktree_revise(buf->b_marktree, itr, decor_level, old_mark); goto revised; } marktree_del_itr(buf->b_marktree, itr, false); } } else { - ns->free_id = MAX(ns->free_id, id+1); + *ns = MAX(*ns, id); } } - if (end_row > -1) { - mark = marktree_put_pair(buf->b_marktree, - row, col, right_gravity, - end_row, end_col, end_right_gravity, decor_level); - } else { - mark = marktree_put(buf->b_marktree, row, col, right_gravity, decor_level); + mtkey_t mark = { { row, col }, ns_id, id, 0, + mt_flags(right_gravity, decor_level), 0, NULL }; + if (decor_full) { + mark.decor_full = decor; + } else if (decor) { + mark.hl_id = decor->hl_id; + // workaround: see above + mark.flags = (uint16_t)(mark.flags | (decor->hl_eol ? (uint16_t)MT_FLAG_HL_EOL : (uint16_t)0)); + mark.priority = decor->priority; } -revised: - map_put(uint64_t, ExtmarkItem)(buf->b_extmark_index, mark, - (ExtmarkItem){ ns_id, id, decor }); - map_put(uint64_t, uint64_t)(ns->map, id, mark); + marktree_put(buf->b_marktree, mark, end_row, end_col, end_right_gravity); +revised: if (op != kExtmarkNoUndo) { // TODO(bfredl): this doesn't cover all the cases and probably shouldn't // be done "prematurely". Any movement in undo history might necessitate - // adding new marks to old undo headers. - u_extmark_set(buf, mark, row, col); + // adding new marks to old undo headers. add a test case for this (doesn't + // fail extmark_spec.lua, and it should) + uint64_t mark_id = mt_lookup_id(ns_id, id, false); + u_extmark_set(buf, mark_id, row, col); } if (decor) { @@ -133,18 +148,17 @@ revised: if (idp) { *idp = id; } - return mark; } static bool extmark_setraw(buf_T *buf, uint64_t mark, int row, colnr_T col) { MarkTreeIter itr[1] = { 0 }; - mtpos_t pos = marktree_lookup(buf->b_marktree, mark, itr); - if (pos.row == -1) { + mtkey_t key = marktree_lookup(buf->b_marktree, mark, itr); + if (key.pos.row == -1) { return false; } - if (pos.row == row && pos.col == col) { + if (key.pos.row == row && key.pos.col == col) { return true; } @@ -154,45 +168,35 @@ static bool extmark_setraw(buf_T *buf, uint64_t mark, int row, colnr_T col) // Remove an extmark // Returns 0 on missing id -bool extmark_del(buf_T *buf, uint64_t ns_id, uint64_t id) +bool extmark_del(buf_T *buf, uint32_t ns_id, uint32_t id) { - ExtmarkNs *ns = buf_ns_ref(buf, ns_id, false); - if (!ns) { - return false; - } - - uint64_t mark = map_get(uint64_t, uint64_t)(ns->map, id); - if (!mark) { + MarkTreeIter itr[1] = { 0 }; + mtkey_t key = marktree_lookup_ns(buf->b_marktree, ns_id, id, false, itr); + if (!key.id) { return false; } - - MarkTreeIter itr[1] = { 0 }; - mtpos_t pos = marktree_lookup(buf->b_marktree, mark, itr); - assert(pos.row >= 0); + assert(key.pos.row >= 0); marktree_del_itr(buf->b_marktree, itr, false); - ExtmarkItem item = map_get(uint64_t, ExtmarkItem)(buf->b_extmark_index, mark); - mtpos_t pos2 = pos; - if (mark & MARKTREE_PAIRED_FLAG) { - pos2 = marktree_lookup(buf->b_marktree, mark|MARKTREE_END_FLAG, itr); - assert(pos2.row >= 0); + mtkey_t key2 = key; + + if (mt_paired(key)) { + key2 = marktree_lookup_ns(buf->b_marktree, ns_id, id, true, itr); + assert(key2.pos.row >= 0); marktree_del_itr(buf->b_marktree, itr, false); } - if (item.decor) { - decor_remove(buf, pos.row, pos2.row, item.decor); + if (marktree_decor_level(key) > kDecorLevelNone) { + decor_remove(buf, key.pos.row, key2.pos.row, key.decor_full); } - map_del(uint64_t, uint64_t)(ns->map, id); - map_del(uint64_t, ExtmarkItem)(buf->b_extmark_index, mark); - // TODO(bfredl): delete it from current undo header, opportunistically? return true; } // Free extmarks in a ns between lines // if ns = 0, it means clear all namespaces -bool extmark_clear(buf_T *buf, uint64_t ns_id, int l_row, colnr_T l_col, int u_row, colnr_T u_col) +bool extmark_clear(buf_T *buf, uint32_t ns_id, int l_row, colnr_T l_col, int u_row, colnr_T u_col) { if (!map_size(buf->b_extmark_ns)) { return false; @@ -201,68 +205,58 @@ bool extmark_clear(buf_T *buf, uint64_t ns_id, int l_row, colnr_T l_col, int u_r bool marks_cleared = false; bool all_ns = (ns_id == 0); - ExtmarkNs *ns = NULL; + uint32_t *ns = NULL; if (!all_ns) { ns = buf_ns_ref(buf, ns_id, false); if (!ns) { // nothing to do return false; } - - // TODO(bfredl): if map_size(ns->map) << buf->b_marktree.n_nodes - // it could be faster to iterate over the map instead } // the value is either zero or the lnum (row+1) if highlight was present. static Map(uint64_t, ssize_t) delete_set = MAP_INIT; - typedef struct { Decoration *decor; int row1; } DecorItem; + typedef struct { int row1; } DecorItem; static kvec_t(DecorItem) decors; MarkTreeIter itr[1] = { 0 }; marktree_itr_get(buf->b_marktree, l_row, l_col, itr); while (true) { - mtmark_t mark = marktree_itr_current(itr); - if (mark.row < 0 - || mark.row > u_row - || (mark.row == u_row && mark.col > u_col)) { + mtkey_t mark = marktree_itr_current(itr); + if (mark.pos.row < 0 + || mark.pos.row > u_row + || (mark.pos.row == u_row && mark.pos.col > u_col)) { break; } - ssize_t *del_status = map_ref(uint64_t, ssize_t)(&delete_set, mark.id, + ssize_t *del_status = map_ref(uint64_t, ssize_t)(&delete_set, mt_lookup_key(mark), false); if (del_status) { marktree_del_itr(buf->b_marktree, itr, false); if (*del_status >= 0) { // we had a decor_id DecorItem it = kv_A(decors, *del_status); - decor_remove(buf, it.row1, mark.row, it.decor); + decor_remove(buf, it.row1, mark.pos.row, mark.decor_full); } - map_del(uint64_t, ssize_t)(&delete_set, mark.id); + map_del(uint64_t, ssize_t)(&delete_set, mt_lookup_key(mark)); continue; } - uint64_t start_id = mark.id & ~MARKTREE_END_FLAG; - ExtmarkItem item = map_get(uint64_t, ExtmarkItem)(buf->b_extmark_index, - start_id); - - assert(item.ns_id > 0 && item.mark_id > 0); - if (item.mark_id > 0 && (item.ns_id == ns_id || all_ns)) { + assert(mark.ns > 0 && mark.id > 0); + if (mark.ns == ns_id || all_ns) { marks_cleared = true; - if (mark.id & MARKTREE_PAIRED_FLAG) { - uint64_t other = mark.id ^ MARKTREE_END_FLAG; + if (mt_paired(mark)) { + uint64_t other = mt_lookup_id(mark.ns, mark.id, !mt_end(mark)); ssize_t decor_id = -1; - if (item.decor) { + if (marktree_decor_level(mark) > kDecorLevelNone) { // Save the decoration and the first pos. Clear the decoration // later when we know the full range. decor_id = (ssize_t)kv_size(decors); kv_push(decors, - ((DecorItem) { .decor = item.decor, .row1 = mark.row })); + ((DecorItem) { .row1 = mark.pos.row })); } map_put(uint64_t, ssize_t)(&delete_set, other, decor_id); - } else if (item.decor) { - decor_remove(buf, mark.row, mark.row, item.decor); + } else if (mark.decor_full) { + decor_remove(buf, mark.pos.row, mark.pos.row, mark.decor_full); } - ExtmarkNs *my_ns = all_ns ? buf_ns_ref(buf, item.ns_id, false) : ns; - map_del(uint64_t, uint64_t)(my_ns->map, item.mark_id); - map_del(uint64_t, ExtmarkItem)(buf->b_extmark_index, start_id); marktree_del_itr(buf->b_marktree, itr, false); } else { marktree_itr_next(buf->b_marktree, itr); @@ -271,12 +265,12 @@ bool extmark_clear(buf_T *buf, uint64_t ns_id, int l_row, colnr_T l_col, int u_r uint64_t id; ssize_t decor_id; map_foreach(&delete_set, id, decor_id, { - mtpos_t pos = marktree_lookup(buf->b_marktree, id, itr); + mtkey_t mark = marktree_lookup(buf->b_marktree, id, itr); assert(itr->node); marktree_del_itr(buf->b_marktree, itr, false); if (decor_id >= 0) { DecorItem it = kv_A(decors, decor_id); - decor_remove(buf, it.row1, pos.row, it.decor); + decor_remove(buf, it.row1, mark.pos.row, mark.decor_full); } }); map_clear(uint64_t, ssize_t)(&delete_set); @@ -290,7 +284,7 @@ bool extmark_clear(buf_T *buf, uint64_t ns_id, int l_row, colnr_T l_col, int u_r // will be searched to the start, or end // dir can be set to control the order of the array // amount = amount of marks to find or -1 for all -ExtmarkInfoArray extmark_get(buf_T *buf, uint64_t ns_id, int l_row, colnr_T l_col, int u_row, +ExtmarkInfoArray extmark_get(buf_T *buf, uint32_t ns_id, int l_row, colnr_T l_col, int u_row, colnr_T u_col, int64_t amount, bool reverse) { ExtmarkInfoArray array = KV_INITIAL_VALUE; @@ -300,30 +294,26 @@ ExtmarkInfoArray extmark_get(buf_T *buf, uint64_t ns_id, int l_row, colnr_T l_co itr, reverse, false, NULL); int order = reverse ? -1 : 1; while ((int64_t)kv_size(array) < amount) { - mtmark_t mark = marktree_itr_current(itr); - mtpos_t endpos = { -1, -1 }; - if (mark.row < 0 - || (mark.row - u_row) * order > 0 - || (mark.row == u_row && (mark.col - u_col) * order > 0)) { + mtkey_t mark = marktree_itr_current(itr); + if (mark.pos.row < 0 + || (mark.pos.row - u_row) * order > 0 + || (mark.pos.row == u_row && (mark.pos.col - u_col) * order > 0)) { break; } - if (mark.id & MARKTREE_END_FLAG) { + if (mt_end(mark)) { goto next_mark; - } else if (mark.id & MARKTREE_PAIRED_FLAG) { - endpos = marktree_lookup(buf->b_marktree, mark.id | MARKTREE_END_FLAG, - NULL); } - - ExtmarkItem item = map_get(uint64_t, ExtmarkItem)(buf->b_extmark_index, - mark.id); - if (item.ns_id == ns_id) { - kv_push(array, ((ExtmarkInfo) { .ns_id = item.ns_id, - .mark_id = item.mark_id, - .row = mark.row, .col = mark.col, - .end_row = endpos.row, - .end_col = endpos.col, - .decor = item.decor })); + if (mark.ns == ns_id) { + mtkey_t end = marktree_get_alt(buf->b_marktree, mark, NULL); + kv_push(array, ((ExtmarkInfo) { .ns_id = mark.ns, + .mark_id = mark.id, + .row = mark.pos.row, .col = mark.pos.col, + .end_row = end.pos.row, + .end_col = end.pos.col, + .right_gravity = mt_right(mark), + .end_right_gravity = mt_right(end), + .decor = get_decor(mark) })); } next_mark: if (reverse) { @@ -336,36 +326,25 @@ next_mark: } // Lookup an extmark by id -ExtmarkInfo extmark_from_id(buf_T *buf, uint64_t ns_id, uint64_t id) +ExtmarkInfo extmark_from_id(buf_T *buf, uint32_t ns_id, uint32_t id) { - ExtmarkNs *ns = buf_ns_ref(buf, ns_id, false); - ExtmarkInfo ret = { 0, 0, -1, -1, -1, -1, NULL }; - if (!ns) { + ExtmarkInfo ret = { 0, 0, -1, -1, -1, -1, false, false, DECORATION_INIT }; + mtkey_t mark = marktree_lookup_ns(buf->b_marktree, ns_id, id, false, NULL); + if (!mark.id) { return ret; } - - uint64_t mark = map_get(uint64_t, uint64_t)(ns->map, id); - if (!mark) { - return ret; - } - - mtpos_t pos = marktree_lookup(buf->b_marktree, mark, NULL); - mtpos_t endpos = { -1, -1 }; - if (mark & MARKTREE_PAIRED_FLAG) { - endpos = marktree_lookup(buf->b_marktree, mark | MARKTREE_END_FLAG, NULL); - } - assert(pos.row >= 0); - - ExtmarkItem item = map_get(uint64_t, ExtmarkItem)(buf->b_extmark_index, - mark); + assert(mark.pos.row >= 0); + mtkey_t end = marktree_get_alt(buf->b_marktree, mark, NULL); ret.ns_id = ns_id; ret.mark_id = id; - ret.row = pos.row; - ret.col = pos.col; - ret.end_row = endpos.row; - ret.end_col = endpos.col; - ret.decor = item.decor; + ret.row = mark.pos.row; + ret.col = mark.pos.col; + ret.end_row = end.pos.row; + ret.end_col = end.pos.col; + ret.right_gravity = mt_right(mark); + ret.end_right_gravity = mt_right(end); + ret.decor = get_decor(mark); return ret; } @@ -378,25 +357,26 @@ void extmark_free_all(buf_T *buf) return; } - uint64_t id; - ExtmarkNs ns; - ExtmarkItem item; + MarkTreeIter itr[1] = { 0 }; + marktree_itr_get(buf->b_marktree, 0, 0, itr); + while (true) { + mtkey_t mark = marktree_itr_current(itr); + if (mark.pos.row < 0) { + break; + } - marktree_clear(buf->b_marktree); + // don't free mark.decor_full twice for a paired mark. + if (!(mt_paired(mark) && mt_end(mark))) { + decor_free(mark.decor_full); + } - map_foreach(buf->b_extmark_ns, id, ns, { - (void)id; - map_destroy(uint64_t, uint64_t)(ns.map); - }); - map_destroy(uint64_t, ExtmarkNs)(buf->b_extmark_ns); - map_init(uint64_t, ExtmarkNs, buf->b_extmark_ns); + marktree_itr_next(buf->b_marktree, itr); + } - map_foreach(buf->b_extmark_index, id, item, { - (void)id; - decor_free(item.decor); - }); - map_destroy(uint64_t, ExtmarkItem)(buf->b_extmark_index); - map_init(uint64_t, ExtmarkItem, buf->b_extmark_index); + marktree_clear(buf->b_marktree); + + map_destroy(uint32_t, uint32_t)(buf->b_extmark_ns); + map_init(uint32_t, uint32_t, buf->b_extmark_ns); } @@ -437,16 +417,16 @@ void u_extmark_copy(buf_T *buf, int l_row, colnr_T l_col, int u_row, colnr_T u_c MarkTreeIter itr[1] = { 0 }; marktree_itr_get(buf->b_marktree, l_row, l_col, itr); while (true) { - mtmark_t mark = marktree_itr_current(itr); - if (mark.row < 0 - || mark.row > u_row - || (mark.row == u_row && mark.col > u_col)) { + mtkey_t mark = marktree_itr_current(itr); + if (mark.pos.row < 0 + || mark.pos.row > u_row + || (mark.pos.row == u_row && mark.pos.col > u_col)) { break; } ExtmarkSavePos pos; - pos.mark = mark.id; - pos.old_row = mark.row; - pos.old_col = mark.col; + pos.mark = mt_lookup_key(mark); + pos.old_row = mark.pos.row; + pos.old_col = mark.pos.col; pos.row = -1; pos.col = -1; diff --git a/src/nvim/extmark.h b/src/nvim/extmark.h index c70db9f7aa..af9526cd43 100644 --- a/src/nvim/extmark.h +++ b/src/nvim/extmark.h @@ -3,6 +3,7 @@ #include "nvim/buffer_defs.h" #include "nvim/extmark_defs.h" +#include "nvim/decoration.h" #include "nvim/marktree.h" #include "nvim/pos.h" @@ -15,7 +16,9 @@ typedef struct { colnr_T col; int end_row; colnr_T end_col; - Decoration *decor; + bool right_gravity; + bool end_right_gravity; + Decoration decor; // TODO(bfredl): CHONKY } ExtmarkInfo; typedef kvec_t(ExtmarkInfo) ExtmarkInfoArray; diff --git a/src/nvim/extmark_defs.h b/src/nvim/extmark_defs.h index bbe8504ebf..5570b5c71e 100644 --- a/src/nvim/extmark_defs.h +++ b/src/nvim/extmark_defs.h @@ -4,23 +4,11 @@ #include "nvim/lib/kvec.h" #include "nvim/types.h" -typedef struct Decoration Decoration; - typedef struct { char *text; int hl_id; } VirtTextChunk; - -typedef struct { - uint64_t ns_id; - uint64_t mark_id; - // TODO(bfredl): a lot of small allocations. Should probably use - // kvec_t(Decoration) as an arena. Alternatively, store ns_id/mark_id - // _inline_ in MarkTree and use the map only for decorations. - Decoration *decor; -} ExtmarkItem; - typedef struct undo_object ExtmarkUndoObject; typedef kvec_t(ExtmarkUndoObject) extmark_undo_vec_t; diff --git a/src/nvim/file_search.c b/src/nvim/file_search.c index b2cd5c510b..25dbf680de 100644 --- a/src/nvim/file_search.c +++ b/src/nvim/file_search.c @@ -483,8 +483,14 @@ void *vim_findfile_init(char_u *path, char_u *filename, char_u *stopdirs, int le int len = 0; if (p > search_ctx->ffsc_fix_path) { + // do not add '..' to the path and start upwards searching len = (int)(p - search_ctx->ffsc_fix_path) - 1; - STRNCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, len); + if ((len >= 2 && STRNCMP(search_ctx->ffsc_fix_path, "..", 2) == 0) + && (len == 2 || search_ctx->ffsc_fix_path[2] == PATHSEP)) { + xfree(buf); + goto error_return; + } + STRLCAT(ff_expand_buffer, search_ctx->ffsc_fix_path, eb_len + (size_t)len + 1); add_pathsep((char *)ff_expand_buffer); } else { len = (int)STRLEN(search_ctx->ffsc_fix_path); @@ -1433,7 +1439,11 @@ char_u *find_file_in_path_option(char_u *ptr, size_t len, int options, int first rel_fname = NULL; } - if (first == TRUE) { + if (first == true) { + if (len == 0) { + return NULL; + } + // copy file name into NameBuff, expanding environment variables save_char = ptr[len]; ptr[len] = NUL; diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f8cf341836..8e1be3bbf7 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -242,6 +242,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski bool notconverted = false; // true if conversion wanted but it wasn't possible char_u conv_rest[CONV_RESTLEN]; int conv_restlen = 0; // nr of bytes in conv_rest[] + pos_T orig_start; buf_T *old_curbuf; char_u *old_b_ffname; char_u *old_b_fname; @@ -298,14 +299,10 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski fname = sfname; #endif - /* - * The BufReadCmd and FileReadCmd events intercept the reading process by - * executing the associated commands instead. - */ + // The BufReadCmd and FileReadCmd events intercept the reading process by + // executing the associated commands instead. if (!filtering && !read_stdin && !read_buffer) { - pos_T pos; - - pos = curbuf->b_op_start; + orig_start = curbuf->b_op_start; // Set '[ mark to the line above where the lines go (line 1 if zero). curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); @@ -335,7 +332,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski return aborting() ? FAIL : OK; } - curbuf->b_op_start = pos; + curbuf->b_op_start = orig_start; } if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) { @@ -576,9 +573,8 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski ++no_wait_return; // don't wait for return yet - /* - * Set '[ mark to the line above where the lines go (line 1 if zero). - */ + // Set '[ mark to the line above where the lines go (line 1 if zero). + orig_start = curbuf->b_op_start; curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); curbuf->b_op_start.col = 0; @@ -618,6 +614,7 @@ int readfile(char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_ski try_mac = (vim_strchr(p_ffs, 'm') != NULL); try_dos = (vim_strchr(p_ffs, 'd') != NULL); try_unix = (vim_strchr(p_ffs, 'x') != NULL); + curbuf->b_op_start = orig_start; if (msg_scrolled == n) { msg_scroll = m; @@ -1888,13 +1885,13 @@ failed: check_cursor_lnum(); beginline(BL_WHITE | BL_FIX); // on first non-blank - /* - * Set '[ and '] marks to the newly read lines. - */ - curbuf->b_op_start.lnum = from + 1; - curbuf->b_op_start.col = 0; - curbuf->b_op_end.lnum = from + linecnt; - curbuf->b_op_end.col = 0; + if (!cmdmod.lockmarks) { + // Set '[ and '] marks to the newly read lines. + curbuf->b_op_start.lnum = from + 1; + curbuf->b_op_start.col = 0; + curbuf->b_op_end.lnum = from + linecnt; + curbuf->b_op_end.col = 0; + } } msg_scroll = msg_save; @@ -2252,6 +2249,8 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ int write_undo_file = FALSE; context_sha256_T sha_ctx; unsigned int bkc = get_bkc_value(buf); + const pos_T orig_start = buf->b_op_start; + const pos_T orig_end = buf->b_op_end; if (fname == NULL || *fname == NUL) { // safety check return FAIL; @@ -2432,7 +2431,13 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) || did_cmd || nofile_err || aborting()) { - --no_wait_return; + if (buf != NULL && cmdmod.lockmarks) { + // restore the original '[ and '] positions + buf->b_op_start = orig_start; + buf->b_op_end = orig_end; + } + + no_wait_return--; msg_scroll = msg_save; if (nofile_err) { emsg(_("E676: No matching autocommands for acwrite buffer")); @@ -2513,6 +2518,11 @@ int buf_write(buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_ } } + if (cmdmod.lockmarks) { + // restore the original '[ and '] positions + buf->b_op_start = orig_start; + buf->b_op_end = orig_end; + } if (shortmess(SHM_OVER) && !exiting) { msg_scroll = FALSE; // overwrite previous file message @@ -3308,7 +3318,7 @@ restore_backup: if (end == 0 || (lnum == end && (write_bin || !buf->b_p_fixeol) - && (lnum == buf->b_no_eol_lnum + && ((write_bin && lnum == buf->b_no_eol_lnum) || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) { lnum++; // written the line, count it no_eol = true; @@ -3853,7 +3863,7 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) *p++ = ' '; } if (shortmess(SHM_LINES)) { - vim_snprintf((char *)p, IOSIZE - (p - IObuff), "%" PRId64 "L, %" PRId64 "C", + vim_snprintf((char *)p, IOSIZE - (p - IObuff), "%" PRId64 "L, %" PRId64 "B", (int64_t)lnum, (int64_t)nchars); } else { vim_snprintf((char *)p, IOSIZE - (p - IObuff), @@ -3861,7 +3871,7 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) (int64_t)lnum); p += STRLEN(p); vim_snprintf((char *)p, IOSIZE - (p - IObuff), - NGETTEXT("%" PRId64 " character", "%" PRId64 " characters", nchars), + NGETTEXT("%" PRId64 " byte", "%" PRId64 " bytes", nchars), (int64_t)nchars); } } diff --git a/src/nvim/generators/gen_keysets.lua b/src/nvim/generators/gen_keysets.lua index 01d8c1d357..633c5da184 100644 --- a/src/nvim/generators/gen_keysets.lua +++ b/src/nvim/generators/gen_keysets.lua @@ -27,7 +27,8 @@ local defspipe = io.open(defs_file, 'wb') local keysets = require'api.keysets' local keywords = { - register = true, + register = true; + default = true; } local function sanitize(key) diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 5565e17597..5d8a8ddbfe 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -59,25 +59,18 @@ static int curscript = 0; FileDescriptor *scriptin[NSCRIPT] = { NULL }; -/* - * These buffers are used for storing: - * - stuffed characters: A command that is translated into another command. - * - redo characters: will redo the last change. - * - recorded characters: for the "q" command. - * - * The bytes are stored like in the typeahead buffer: - * - K_SPECIAL introduces a special key (two more bytes follow). A literal - * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER. - * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE, - * otherwise switching the GUI on would make mappings invalid). - * A literal CSI is stored as CSI KS_EXTRA KE_CSI. - * These translations are also done on multi-byte characters! - * - * Escaping CSI bytes is done by the system-specific input functions, called - * by ui_inchar(). - * Escaping K_SPECIAL is done by inchar(). - * Un-escaping is done by vgetc(). - */ +// These buffers are used for storing: +// - stuffed characters: A command that is translated into another command. +// - redo characters: will redo the last change. +// - recorded characters: for the "q" command. +// +// The bytes are stored like in the typeahead buffer: +// - K_SPECIAL introduces a special key (two more bytes follow). A literal +// K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER. +// These translations are also done on multi-byte characters! +// +// Escaping K_SPECIAL is done by inchar(). +// Un-escaping is done by vgetc(). #define MINIMAL_SIZE 20 // minimal size for b_str @@ -170,10 +163,11 @@ void free_buff(buffheader_T *buf) xfree(p); } buf->bh_first.b_next = NULL; + buf->bh_curr = NULL; } /// Return the contents of a buffer as a single string. -/// K_SPECIAL and CSI in the returned string are escaped. +/// K_SPECIAL in the returned string is escaped. /// /// @param dozero count == zero is not an error static char_u *get_buffcont(buffheader_T *buffer, int dozero) @@ -202,11 +196,9 @@ static char_u *get_buffcont(buffheader_T *buffer, int dozero) return p; } -/* - * Return the contents of the record buffer as a single string - * and clear the record buffer. - * K_SPECIAL and CSI in the returned string are escaped. - */ +/// Return the contents of the record buffer as a single string +/// and clear the record buffer. +/// K_SPECIAL in the returned string is escaped. char_u *get_recorded(void) { char_u *p; @@ -236,10 +228,8 @@ char_u *get_recorded(void) return p; } -/* - * Return the contents of the redo buffer as a single string. - * K_SPECIAL and CSI in the returned string are escaped. - */ +/// Return the contents of the redo buffer as a single string. +/// K_SPECIAL in the returned string is escaped. char_u *get_inserted(void) { return get_buffcont(&redobuff, FALSE); @@ -247,7 +237,7 @@ char_u *get_inserted(void) /// Add string after the current block of the given buffer /// -/// K_SPECIAL and CSI should have been escaped already. +/// K_SPECIAL should have been escaped already. /// /// @param[out] buf Buffer to add to. /// @param[in] s String to add. @@ -295,9 +285,23 @@ static void add_buff(buffheader_T *const buf, const char *const s, ptrdiff_t sle } } -/* - * Add number "n" to buffer "buf". - */ +/// Delete "slen" bytes from the end of "buf". +/// Only works when it was just added. +static void delete_buff_tail(buffheader_T *buf, int slen) +{ + int len; + + if (buf->bh_curr == NULL) { + return; // nothing to delete + } + len = (int)STRLEN(buf->bh_curr->b_str); + if (len >= slen) { + buf->bh_curr->b_str[len - slen] = NUL; + buf->bh_space += (size_t)slen; + } +} + +/// Add number "n" to buffer "buf". static void add_num_buff(buffheader_T *buf, long n) { char number[32]; @@ -305,10 +309,8 @@ static void add_num_buff(buffheader_T *buf, long n) add_buff(buf, number, -1L); } -/* - * Add character 'c' to buffer "buf". - * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. - */ +/// Add character 'c' to buffer "buf". +/// Translates special keys, NUL, K_SPECIAL and multibyte characters. static void add_char_buff(buffheader_T *buf, int c) { uint8_t bytes[MB_MAXBYTES + 1]; @@ -340,12 +342,10 @@ static void add_char_buff(buffheader_T *buf, int c) } } -/* - * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 - * if that one is empty. - * If advance == TRUE go to the next char. - * No translation is done K_SPECIAL and CSI are escaped. - */ +/// Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 +/// if that one is empty. +/// If advance == TRUE go to the next char. +/// No translation is done K_SPECIAL is escaped. static int read_readbuffers(int advance) { int c; @@ -524,10 +524,8 @@ void restoreRedobuff(save_redo_T *save_redo) old_redobuff = save_redo->sr_old_redobuff; } -/* - * Append "s" to the redo buffer. - * K_SPECIAL and CSI should already have been escaped. - */ +/// Append "s" to the redo buffer. +/// K_SPECIAL should already have been escaped. void AppendToRedobuff(const char *s) { if (!block_redo) { @@ -536,7 +534,7 @@ void AppendToRedobuff(const char *s) } /// Append to Redo buffer literally, escaping special characters with CTRL-V. -/// K_SPECIAL and CSI are escaped as well. +/// K_SPECIAL is escaped as well. /// /// @param str String to append /// @param len Length of `str` or -1 for up to the NUL. @@ -584,10 +582,8 @@ void AppendToRedobuffLit(const char_u *str, int len) } } -/* - * Append a character to the redo buffer. - * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. - */ +/// Append a character to the redo buffer. +/// Translates special keys, NUL, K_SPECIAL and multibyte characters. void AppendCharToRedobuff(int c) { if (!block_redo) { @@ -605,17 +601,15 @@ void AppendNumberToRedobuff(long n) } } -/* - * Append string "s" to the stuff buffer. - * CSI and K_SPECIAL must already have been escaped. - */ +/// Append string "s" to the stuff buffer. +/// K_SPECIAL must already have been escaped. void stuffReadbuff(const char *s) { add_buff(&readbuf1, s, -1L); } /// Append string "s" to the redo stuff buffer. -/// @remark CSI and K_SPECIAL must already have been escaped. +/// @remark K_SPECIAL must already have been escaped. void stuffRedoReadbuff(const char *s) { add_buff(&readbuf2, s, -1L); @@ -626,11 +620,9 @@ void stuffReadbuffLen(const char *s, long len) add_buff(&readbuf1, s, len); } -/* - * Stuff "s" into the stuff buffer, leaving special key codes unmodified and - * escaping other K_SPECIAL and CSI bytes. - * Change CR, LF and ESC into a space. - */ +/// Stuff "s" into the stuff buffer, leaving special key codes unmodified and +/// escaping other K_SPECIAL bytes. +/// Change CR, LF and ESC into a space. void stuffReadbuffSpec(const char *s) { while (*s != NUL) { @@ -648,10 +640,8 @@ void stuffReadbuffSpec(const char *s) } } -/* - * Append a character to the stuff buffer. - * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. - */ +/// Append a character to the stuff buffer. +/// Translates special keys, NUL, K_SPECIAL and multibyte characters. void stuffcharReadbuff(int c) { add_char_buff(&readbuf1, c); @@ -665,12 +655,12 @@ void stuffnumReadbuff(long n) add_num_buff(&readbuf1, n); } -// Read a character from the redo buffer. Translates K_SPECIAL, CSI and -// multibyte characters. -// The redo buffer is left as it is. -// If init is true, prepare for redo, return FAIL if nothing to redo, OK -// otherwise. -// If old_redo is true, use old_redobuff instead of redobuff. +/// Read a character from the redo buffer. Translates K_SPECIAL and +/// multibyte characters. +/// The redo buffer is left as it is. +/// If init is true, prepare for redo, return FAIL if nothing to redo, OK +/// otherwise. +/// If old_redo is true, use old_redobuff instead of redobuff. static int read_redo(bool init, bool old_redo) { static buffblock_T *bp; @@ -724,9 +714,9 @@ static int read_redo(bool init, bool old_redo) return c; } -// Copy the rest of the redo buffer into the stuff buffer (in a slow way). -// If old_redo is true, use old_redobuff instead of redobuff. -// The escaped K_SPECIAL and CSI are copied without translation. +/// Copy the rest of the redo buffer into the stuff buffer (in a slow way). +/// If old_redo is true, use old_redobuff instead of redobuff. +/// The escaped K_SPECIAL is copied without translation. static void copy_redo(bool old_redo) { int c; @@ -992,36 +982,45 @@ int ins_typebuf(char_u *str, int noremap, int offset, bool nottyped, bool silent return OK; } -/* - * Put character "c" back into the typeahead buffer. - * Can be used for a character obtained by vgetc() that needs to be put back. - * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to - * the char. - */ -void ins_char_typebuf(int c) +/// Put character "c" back into the typeahead buffer. +/// Can be used for a character obtained by vgetc() that needs to be put back. +/// Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to +/// the char. +/// @return the length of what was inserted +int ins_char_typebuf(int c, int modifier) { - char_u buf[MB_MAXBYTES + 1]; - if (IS_SPECIAL(c)) { + char_u buf[MB_MAXBYTES * 3 + 4]; + int len = 0; + if (modifier != 0) { buf[0] = K_SPECIAL; - buf[1] = (char_u)K_SECOND(c); - buf[2] = (char_u)K_THIRD(c); + buf[1] = KS_MODIFIER; + buf[2] = (char_u)modifier; buf[3] = NUL; + len = 3; + } + if (IS_SPECIAL(c)) { + buf[len] = K_SPECIAL; + buf[len + 1] = (char_u)K_SECOND(c); + buf[len + 2] = (char_u)K_THIRD(c); + buf[len + 3] = NUL; } else { - buf[utf_char2bytes(c, buf)] = NUL; - char_u *p = buf; - while (*p) { - if ((uint8_t)(*p) == CSI || (uint8_t)(*p) == K_SPECIAL) { - bool is_csi = (uint8_t)(*p) == CSI; - memmove(p + 3, p + 1, STRLEN(p + 1) + 1); + char_u *p = buf + len; + int char_len = utf_char2bytes(c, p); + len += char_len; + // If the character contains K_SPECIAL bytes they need escaping. + for (int i = char_len; --i >= 0; p++) { + if ((uint8_t)(*p) == K_SPECIAL) { + memmove(p + 3, p + 1, (size_t)i); *p++ = K_SPECIAL; - *p++ = is_csi ? KS_EXTRA : KS_SPECIAL; - *p++ = is_csi ? KE_CSI : KE_FILLER; - } else { - p++; + *p++ = KS_SPECIAL; + *p = KE_FILLER; + len += 2; } } + *p = NUL; } (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent); + return len; } /// Return TRUE if the typeahead buffer was changed (while waiting for a @@ -1181,6 +1180,18 @@ static void gotchars(const char_u *chars, size_t len) maptick++; } +/// Undo the last gotchars() for "len" bytes. To be used when putting a typed +/// character back into the typeahead buffer, thus gotchars() will be called +/// again. +/// Only affects recorded characters. +void ungetchars(int len) +{ + if (reg_recording != 0) { + delete_buff_tail(&recordbuff, len); + last_recorded_len -= (size_t)len; + } +} + /* * Sync undo. Called when typed characters are obtained from the typeahead * buffer, or when a menu is used. @@ -1427,15 +1438,13 @@ static void updatescript(int c) } } -/* - * Get the next input character. - * Can return a special key or a multi-byte character. - * Can return NUL when called recursively, use safe_vgetc() if that's not - * wanted. - * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte. - * Collects the bytes of a multibyte character into the whole character. - * Returns the modifiers in the global "mod_mask". - */ +/// Get the next input character. +/// Can return a special key or a multi-byte character. +/// Can return NUL when called recursively, use safe_vgetc() if that's not +/// wanted. +/// This translates escaped K_SPECIAL bytes to a K_SPECIAL byte. +/// Collects the bytes of a multibyte character into the whole character. +/// Returns the modifiers in the global "mod_mask". int vgetc(void) { int c, c2; @@ -1461,8 +1470,9 @@ int vgetc(void) mouse_row = old_mouse_row; mouse_col = old_mouse_col; } else { - mod_mask = 0x0; + mod_mask = 0; last_recorded_len = 0; + for (;;) { // this is done twice if there are modifiers bool did_inc = false; if (mod_mask) { // no mapping after modifier has been read @@ -1572,14 +1582,9 @@ int vgetc(void) buf[i] = (char_u)vgetorpeek(true); if (buf[i] == K_SPECIAL) { // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence, - // which represents a K_SPECIAL (0x80), - // or a CSI - KS_EXTRA - KE_CSI sequence, which represents - // a CSI (0x9B), - // of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. - c = vgetorpeek(true); - if (vgetorpeek(true) == KE_CSI && c == KS_EXTRA) { - buf[i] = CSI; - } + // which represents a K_SPECIAL (0x80). + (void)vgetorpeek(true); // skip KS_SPECIAL + (void)vgetorpeek(true); // skip KE_FILLER } } no_mapping--; @@ -1593,8 +1598,9 @@ int vgetc(void) if (!no_mapping && KeyTyped && !(State & TERM_FOCUS) && (mod_mask == MOD_MASK_ALT || mod_mask == MOD_MASK_META)) { mod_mask = 0; - ins_char_typebuf(c); - ins_char_typebuf(ESC); + int len = ins_char_typebuf(c, 0); + (void)ins_char_typebuf(ESC, 0); + ungetchars(len + 3); // The ALT/META modifier takes three more bytes continue; } @@ -2043,7 +2049,7 @@ void vungetc(int c) /// /// When `no_mapping` (global) is zero, checks for mappings in the current mode. /// Only returns one byte (of a multi-byte character). -/// K_SPECIAL and CSI may be escaped, need to get two more bytes then. +/// K_SPECIAL may be escaped, need to get two more bytes then. static int vgetorpeek(bool advance) { int c, c1; @@ -2298,6 +2304,10 @@ static int vgetorpeek(bool advance) c = ESC; } tc = c; + + // no chars to block abbreviations for + typebuf.tb_no_abbr_cnt = 0; + break; } @@ -2573,7 +2583,7 @@ int fix_input_buffer(char_u *buf, int len) FUNC_ATTR_NONNULL_ALL { if (!using_script()) { - // Should not escape K_SPECIAL/CSI reading input from the user because vim + // Should not escape K_SPECIAL reading input from the user because vim // key codes keys are processed in input.c/input_enqueue. buf[len] = NUL; return len; @@ -2584,9 +2594,8 @@ int fix_input_buffer(char_u *buf, int len) char_u *p = buf; // Two characters are special: NUL and K_SPECIAL. - // Replace NUL by K_SPECIAL KS_ZERO KE_FILLER + // Replace NUL by K_SPECIAL KS_ZERO KE_FILLER // Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER - // Replace CSI by K_SPECIAL KS_EXTRA KE_CSI for (i = len; --i >= 0; ++p) { if (p[0] == NUL || (p[0] == K_SPECIAL @@ -2665,9 +2674,10 @@ void set_maparg_lhs_rhs(const char_u *orig_lhs, const size_t orig_lhs_len, } } else { char tmp_buf[64]; + // orig_rhs is not used for Lua mappings, but still needs to be a string. + mapargs->orig_rhs = xcalloc(1, sizeof(char_u)); + mapargs->orig_rhs_len = 0; // stores <lua>ref_no<cr> in map_str - mapargs->orig_rhs_len = (size_t)vim_snprintf(S_LEN(tmp_buf), "<LUA>%d<CR>", rhs_lua); - mapargs->orig_rhs = vim_strsave((char_u *)tmp_buf); mapargs->rhs_len = (size_t)vim_snprintf(S_LEN(tmp_buf), "%c%c%c%d\r", K_SPECIAL, (char_u)KEY2TERMCAP0(K_LUA), KEY2TERMCAP1(K_LUA), rhs_lua); @@ -3477,10 +3487,10 @@ static void showmap(mapblock_T *mp, bool local) } else if (mp->m_str == NULL) { msg_puts_attr("<Nop>", HL_ATTR(HLF_8)); } else { - // Remove escaping of CSI, because "m_str" is in a format to be used + // Remove escaping of K_SPECIAL, because "m_str" is in a format to be used // as typeahead. char_u *s = vim_strsave(mp->m_str); - vim_unescape_csi(s); + vim_unescape_ks(s); msg_outtrans_special(s, false, 0); xfree(s); } @@ -3860,9 +3870,9 @@ bool check_abbr(int c, char_u *ptr, int col, int mincol) int match; if (strchr((const char *)mp->m_keys, K_SPECIAL) != NULL) { - // Might have CSI escaped mp->m_keys. + // Might have K_SPECIAL escaped mp->m_keys. q = vim_strsave(mp->m_keys); - vim_unescape_csi(q); + vim_unescape_ks(q); qlen = (int)STRLEN(q); } // find entries with right mode and keys @@ -3908,7 +3918,7 @@ bool check_abbr(int c, char_u *ptr, int col, int mincol) int newlen = utf_char2bytes(c, tb + j); tb[j + newlen] = NUL; // Need to escape K_SPECIAL. - char_u *escaped = vim_strsave_escape_csi(tb + j); + char_u *escaped = vim_strsave_escape_ks(tb + j); if (escaped != NULL) { newlen = (int)STRLEN(escaped); memmove(tb + j, escaped, (size_t)newlen); @@ -3961,11 +3971,11 @@ static char_u *eval_map_expr(mapblock_T *mp, int c) int save_msg_col; int save_msg_row; - /* Remove escaping of CSI, because "str" is in a format to be used as - * typeahead. */ + // Remove escaping of K_SPECIAL, because "str" is in a format to be used as + // typeahead. if (mp->m_luaref == LUA_NOREF) { expr = vim_strsave(mp->m_str); - vim_unescape_csi(expr); + vim_unescape_ks(expr); } save_cmd = save_cmdline_alloc(); @@ -4005,18 +4015,16 @@ static char_u *eval_map_expr(mapblock_T *mp, int c) if (p == NULL) { return NULL; } - // Escape CSI in the result to be able to use the string as typeahead. - res = vim_strsave_escape_csi(p); + // Escape K_SPECIAL in the result to be able to use the string as typeahead. + res = vim_strsave_escape_ks(p); xfree(p); return res; } -/* - * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result - * can be put in the typeahead buffer. - */ -char_u *vim_strsave_escape_csi(char_u *p) +/// Copy "p" to allocated memory, escaping K_SPECIAL so that the result +/// can be put in the typeahead buffer. +char_u *vim_strsave_escape_ks(char_u *p) { // Need a buffer to hold up to three times as much. Four in case of an // illegal utf-8 byte: @@ -4031,7 +4039,7 @@ char_u *vim_strsave_escape_csi(char_u *p) *d++ = *s++; } else { // Add character, possibly multi-byte to destination, escaping - // CSI and K_SPECIAL. Be careful, it can be an illegal byte! + // K_SPECIAL. Be careful, it can be an illegal byte! d = add_char2buf(utf_ptr2char(s), d); s += utf_ptr2len(s); } @@ -4041,11 +4049,9 @@ char_u *vim_strsave_escape_csi(char_u *p) return res; } -/* - * Remove escaping from CSI and K_SPECIAL characters. Reverse of - * vim_strsave_escape_csi(). Works in-place. - */ -void vim_unescape_csi(char_u *p) +/// Remove escaping from K_SPECIAL characters. Reverse of +/// vim_strsave_escape_ks(). Works in-place. +void vim_unescape_ks(char_u *p) { char_u *s = p, *d = p; @@ -4053,10 +4059,6 @@ void vim_unescape_csi(char_u *p) if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER) { *d++ = K_SPECIAL; s += 3; - } else if ((s[0] == K_SPECIAL || s[0] == CSI) - && s[1] == KS_EXTRA && s[2] == (int)KE_CSI) { - *d++ = CSI; - s += 3; } else { *d++ = *s++; } @@ -4299,7 +4301,7 @@ int put_escstr(FILE *fd, char_u *strstart, int what) for (; *str != NUL; str++) { // Check for a multi-byte character, which may contain escaped - // K_SPECIAL and CSI bytes. + // K_SPECIAL bytes. const char *p = mb_unescape((const char **)&str); if (p != NULL) { while (*p != NUL) { diff --git a/src/nvim/getchar.h b/src/nvim/getchar.h index be10e150e5..f24a4e7c7c 100644 --- a/src/nvim/getchar.h +++ b/src/nvim/getchar.h @@ -55,7 +55,7 @@ struct map_arguments { char_u *orig_rhs; /// The original text of the {rhs}. size_t orig_rhs_len; - char *desc; /// map escription + char *desc; /// map description }; typedef struct map_arguments MapArguments; #define MAP_ARGUMENTS_INIT { false, false, false, false, false, false, false, \ diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 697d4b11a7..98a38c5fe2 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -28,7 +28,7 @@ #endif #ifndef FILETYPE_FILE -# define FILETYPE_FILE "filetype.vim" +# define FILETYPE_FILE "filetype.lua filetype.vim" #endif #ifndef FTPLUGIN_FILE @@ -127,7 +127,7 @@ typedef off_t off_T; // When vgetc() is called, it sets mod_mask to the set of modifiers that are // held down based on the MOD_MASK_* symbols that are read first. -EXTERN int mod_mask INIT(= 0x0); // current key modifiers +EXTERN int mod_mask INIT(= 0); // current key modifiers // Cmdline_row is the row where the command line starts, just below the @@ -524,6 +524,8 @@ EXTERN pos_T VIsual; EXTERN int VIsual_active INIT(= false); /// Whether Select mode is active. EXTERN int VIsual_select INIT(= false); +/// Register name for Select mode +EXTERN int VIsual_select_reg INIT(= 0); /// Restart Select mode when next cmd finished EXTERN int restart_VIsual_select INIT(= 0); /// Whether to restart the selection after a Select-mode mapping or menu. @@ -977,6 +979,7 @@ EXTERN char e_invalidreg[] INIT(= N_("E850: Invalid register name")); EXTERN char e_dirnotf[] INIT(= N_("E919: Directory not found in '%s': \"%s\"")); EXTERN char e_au_recursive[] INIT(= N_("E952: Autocommand caused recursive behavior")); EXTERN char e_autocmd_close[] INIT(= N_("E813: Cannot close autocmd window")); +EXTERN char e_listarg[] INIT(= N_("E686: Argument of %s must be a List")); EXTERN char e_unsupportedoption[] INIT(= N_("E519: Option not supported")); EXTERN char e_fnametoolong[] INIT(= N_("E856: Filename too long")); EXTERN char e_float_as_string[] INIT(= N_("E806: using Float as a String")); @@ -997,6 +1000,8 @@ EXTERN char e_non_empty_string_required[] INIT(= N_("E1142: Non-empty string req EXTERN char e_cannot_define_autocommands_for_all_events[] INIT(= N_("E1155: Cannot define autocommands for ALL events")); +EXTERN char e_resulting_text_too_long[] INIT(= N_("E1240: Resulting text too long")); + EXTERN char e_highlight_group_name_too_long[] INIT(= N_("E1249: Highlight group name too long")); EXTERN char top_bot_msg[] INIT(= N_("search hit TOP, continuing at BOTTOM")); diff --git a/src/nvim/highlight.c b/src/nvim/highlight.c index 3050ca02de..fcd91cdf16 100644 --- a/src/nvim/highlight.c +++ b/src/nvim/highlight.c @@ -144,13 +144,19 @@ int hl_get_syn_attr(int ns_id, int idx, HlAttrs at_en) } } -void ns_hl_def(NS ns_id, int hl_id, HlAttrs attrs, int link_id) +void ns_hl_def(NS ns_id, int hl_id, HlAttrs attrs, int link_id, Dict(highlight) *dict) { - DecorProvider *p = get_decor_provider(ns_id, true); if ((attrs.rgb_ae_attr & HL_DEFAULT) && map_has(ColorKey, ColorItem)(&ns_hl, ColorKey(ns_id, hl_id))) { return; } + if (ns_id == 0) { + assert(dict); + // set in global (':highlight') namespace + set_hl_group(hl_id, attrs, dict, link_id); + return; + } + DecorProvider *p = get_decor_provider(ns_id, true); int attr_id = link_id > 0 ? -1 : hl_get_syn_attr(ns_id, hl_id, attrs); ColorItem it = { .attr_id = attr_id, .link_id = link_id, @@ -192,23 +198,17 @@ int ns_get_hl(NS ns_id, int hl_id, bool link, bool nodefault) int tmp = false; HlAttrs attrs = HLATTRS_INIT; if (ret.type == kObjectTypeDictionary) { - Dictionary dict = ret.data.dictionary; fallback = false; - attrs = dict2hlattrs(dict, true, &it.link_id, &err); - for (size_t i = 0; i < dict.size; i++) { - char *key = dict.items[i].key.data; - Object val = dict.items[i].value; - bool truthy = api_object_to_bool(val, key, false, &err); - - if (strequal(key, "fallback")) { - fallback = truthy; - } else if (strequal(key, "temp")) { - tmp = truthy; + Dict(highlight) dict = { 0 }; + if (api_dict_to_keydict(&dict, KeyDict_highlight_get_field, + ret.data.dictionary, &err)) { + attrs = dict2hlattrs(&dict, true, &it.link_id, &err); + fallback = api_object_to_bool(dict.fallback, "fallback", true, &err); + tmp = api_object_to_bool(dict.fallback, "tmp", false, &err); + if (it.link_id >= 0) { + fallback = true; } } - if (it.link_id >= 0) { - fallback = true; - } } it.attr_id = fallback ? -1 : hl_get_syn_attr((int)ns_id, hl_id, attrs); @@ -796,112 +796,102 @@ Dictionary hlattrs2dict(HlAttrs ae, bool use_rgb) return hl; } -HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err) +HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *err) { HlAttrs hlattrs = HLATTRS_INIT; - int32_t fg = -1, bg = -1, ctermfg = -1, ctermbg = -1, sp = -1; int16_t mask = 0; int16_t cterm_mask = 0; bool cterm_mask_provided = false; - for (size_t i = 0; i < dict.size; i++) { - char *key = dict.items[i].key.data; - Object val = dict.items[i].value; - - struct { - const char *name; - int16_t flag; - } flags[] = { - { "bold", HL_BOLD }, - { "standout", HL_STANDOUT }, - { "underline", HL_UNDERLINE }, - { "undercurl", HL_UNDERCURL }, - { "italic", HL_ITALIC }, - { "reverse", HL_INVERSE }, - { "default", HL_DEFAULT }, - { "global", HL_GLOBAL }, - { NULL, 0 }, - }; - - int j; - for (j = 0; flags[j].name; j++) { - if (strequal(flags[j].name, key)) { - if (api_object_to_bool(val, key, false, err)) { - mask = mask | flags[j].flag; - } - break; - } +#define CHECK_FLAG(d, m, name, extra, flag) \ + if (api_object_to_bool(d->name ## extra, #name, false, err)) { \ + m = m | flag; \ } - // Handle cterm attrs - if (strequal(key, "cterm") && val.type == kObjectTypeDictionary) { - cterm_mask_provided = true; - Dictionary cterm_dict = val.data.dictionary; - for (size_t l = 0; l < cterm_dict.size; l++) { - char *cterm_dict_key = cterm_dict.items[l].key.data; - Object cterm_dict_val = cterm_dict.items[l].value; - for (int m = 0; flags[m].name; m++) { - if (strequal(flags[m].name, cterm_dict_key)) { - if (api_object_to_bool(cterm_dict_val, cterm_dict_key, false, - err)) { - cterm_mask |= flags[m].flag; - } - break; - } - } + CHECK_FLAG(dict, mask, bold, , HL_BOLD); + CHECK_FLAG(dict, mask, standout, , HL_STANDOUT); + CHECK_FLAG(dict, mask, underline, , HL_UNDERLINE); + CHECK_FLAG(dict, mask, undercurl, , HL_UNDERCURL); + CHECK_FLAG(dict, mask, italic, , HL_ITALIC); + CHECK_FLAG(dict, mask, reverse, , HL_INVERSE); + CHECK_FLAG(dict, mask, strikethrough, , HL_STRIKETHROUGH); + CHECK_FLAG(dict, mask, nocombine, , HL_NOCOMBINE); + CHECK_FLAG(dict, mask, default, _, HL_DEFAULT); + CHECK_FLAG(dict, mask, global, , HL_GLOBAL); + + if (HAS_KEY(dict->fg)) { + fg = object_to_color(dict->fg, "fg", err); + } else if (HAS_KEY(dict->foreground)) { + fg = object_to_color(dict->foreground, "foreground", err); + } + if (ERROR_SET(err)) { + return hlattrs; + } + + if (HAS_KEY(dict->bg)) { + bg = object_to_color(dict->bg, "bg", err); + } else if (HAS_KEY(dict->background)) { + bg = object_to_color(dict->background, "background", err); + } + if (ERROR_SET(err)) { + return hlattrs; + } + + if (HAS_KEY(dict->sp)) { + sp = object_to_color(dict->sp, "sp", err); + } else if (HAS_KEY(dict->special)) { + sp = object_to_color(dict->special, "special", err); + } + if (ERROR_SET(err)) { + return hlattrs; + } + + if (HAS_KEY(dict->link)) { + if (link_id) { + *link_id = object_to_hl_id(dict->link, "link", err); + if (ERROR_SET(err)) { + return hlattrs; } + } else { + api_set_error(err, kErrorTypeValidation, "Invalid Key: 'link'"); } + } - struct { - const char *name; - const char *shortname; - int *dest; - } colors[] = { - { "foreground", "fg", &fg }, - { "background", "bg", &bg }, - { "ctermfg", NULL, &ctermfg }, - { "ctermbg", NULL, &ctermbg }, - { "special", "sp", &sp }, - { NULL, NULL, NULL }, - }; - - int k; - for (k = 0; (!flags[j].name) && colors[k].name; k++) { - if (strequal(colors[k].name, key) || strequal(colors[k].shortname, key)) { - if (val.type == kObjectTypeInteger) { - *colors[k].dest = (int)val.data.integer; - } else if (val.type == kObjectTypeString) { - String str = val.data.string; - // TODO(bfredl): be more fancy with "bg", "fg" etc - if (str.size) { - *colors[k].dest = name_to_color(str.data); - } - } else { - api_set_error(err, kErrorTypeValidation, - "'%s' must be string or integer", key); - } - break; - } + // Handle cterm attrs + if (dict->cterm.type == kObjectTypeDictionary) { + Dict(highlight_cterm) cterm[1] = { 0 }; + if (!api_dict_to_keydict(cterm, KeyDict_highlight_cterm_get_field, + dict->cterm.data.dictionary, err)) { + return hlattrs; } - if (flags[j].name || colors[k].name) { - // handled above - } else if (link_id && strequal(key, "link")) { - if (val.type == kObjectTypeString) { - String str = val.data.string; - *link_id = syn_check_group(str.data, (int)str.size); - } else if (val.type == kObjectTypeInteger) { - // TODO(bfredl): validate range? - *link_id = (int)val.data.integer; - } else { - api_set_error(err, kErrorTypeValidation, - "'link' must be string or integer"); - } + cterm_mask_provided = true; + CHECK_FLAG(cterm, cterm_mask, bold, , HL_BOLD); + CHECK_FLAG(cterm, cterm_mask, standout, , HL_STANDOUT); + CHECK_FLAG(cterm, cterm_mask, underline, , HL_UNDERLINE); + CHECK_FLAG(cterm, cterm_mask, undercurl, , HL_UNDERCURL); + CHECK_FLAG(cterm, cterm_mask, italic, , HL_ITALIC); + CHECK_FLAG(cterm, cterm_mask, reverse, , HL_INVERSE); + CHECK_FLAG(cterm, cterm_mask, strikethrough, , HL_STRIKETHROUGH); + CHECK_FLAG(cterm, cterm_mask, nocombine, , HL_NOCOMBINE); + + } else if (HAS_KEY(dict->cterm)) { + api_set_error(err, kErrorTypeValidation, "'cterm' must be a Dictionary."); + } +#undef CHECK_FLAG + + if (HAS_KEY(dict->ctermfg)) { + ctermfg = object_to_color(dict->ctermfg, "ctermfg", err); + if (ERROR_SET(err)) { + return hlattrs; } + } + if (HAS_KEY(dict->ctermbg)) { + ctermbg = object_to_color(dict->ctermbg, "ctermbg", err); if (ERROR_SET(err)) { - return hlattrs; // error set, caller should not use retval + return hlattrs; } } @@ -927,6 +917,21 @@ HlAttrs dict2hlattrs(Dictionary dict, bool use_rgb, int *link_id, Error *err) return hlattrs; } + +int object_to_color(Object val, char *key, Error *err) +{ + if (val.type == kObjectTypeInteger) { + return (int)val.data.integer; + } else if (val.type == kObjectTypeString) { + String str = val.data.string; + // TODO(bfredl): be more fancy with "bg", "fg" etc + return str.size ? name_to_color(str.data) : 0; + } else { + api_set_error(err, kErrorTypeValidation, "'%s' must be string or integer", key); + return 0; + } +} + Array hl_inspect(int attr) { Array ret = ARRAY_DICT_INIT; diff --git a/src/nvim/if_cscope.c b/src/nvim/if_cscope.c index daef8db267..9ca01137cf 100644 --- a/src/nvim/if_cscope.c +++ b/src/nvim/if_cscope.c @@ -919,8 +919,8 @@ static int cs_find(exarg_T *eap) /// Common code for cscope find, shared by cs_find() and ex_cstag(). -static bool cs_find_common(char *opt, char *pat, int forceit, int verbose, - bool use_ll, char_u *cmdline) +static bool cs_find_common(char *opt, char *pat, int forceit, int verbose, bool use_ll, + char_u *cmdline) { char *cmd; int *nummatches; @@ -1594,7 +1594,6 @@ static char *cs_pathcomponents(char *path) char *s = path + strlen(path) - 1; for (int i = 0; i < p_cspc; i++) { while (s > path && *--s != '/') { - continue; } } if ((s > path && *s == '/')) { @@ -1813,7 +1812,6 @@ static int cs_read_prompt(size_t i) static void sig_handler(int s) { // do nothing - return; } #endif diff --git a/src/nvim/indent_c.c b/src/nvim/indent_c.c index 0f0cab33ea..7f483d02ab 100644 --- a/src/nvim/indent_c.c +++ b/src/nvim/indent_c.c @@ -41,9 +41,7 @@ static pos_T *ind_find_start_comment(void) // XXX pos_T *find_start_comment(int ind_maxcomment) // XXX { - pos_T *pos; - char_u *line; - char_u *p; + pos_T *pos; int64_t cur_maxcomment = ind_maxcomment; for (;; ) { @@ -55,11 +53,9 @@ pos_T *find_start_comment(int ind_maxcomment) // XXX * Check if the comment start we found is inside a string. * If it is then restrict the search to below this line and try again. */ - line = ml_get(pos->lnum); - for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) - p = skip_string(p); - if ((colnr_T)(p - line) <= pos->col) + if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) { break; + } cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; if (cur_maxcomment <= 0) { pos = NULL; @@ -110,8 +106,6 @@ static pos_T *ind_find_start_CORS(linenr_T *is_raw) static pos_T *find_start_rawstring(int ind_maxcomment) // XXX { pos_T *pos; - char_u *line; - char_u *p; long cur_maxcomment = ind_maxcomment; for (;;) @@ -124,11 +118,9 @@ static pos_T *find_start_rawstring(int ind_maxcomment) // XXX * Check if the raw string start we found is inside a string. * If it is then restrict the search to below this line and try again. */ - line = ml_get(pos->lnum); - for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p) - p = skip_string(p); - if ((colnr_T)(p - line) <= pos->col) - break; + if (!is_pos_in_string(ml_get(pos->lnum), pos->col)) { + break; + } cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1; if (cur_maxcomment <= 0) { @@ -143,7 +135,7 @@ static pos_T *find_start_rawstring(int ind_maxcomment) // XXX * Skip to the end of a "string" and a 'c' character. * If there is no string or character, return argument unmodified. */ -static char_u *skip_string(char_u *p) +static const char_u *skip_string(const char_u *p) { int i; @@ -152,11 +144,11 @@ static char_u *skip_string(char_u *p) */ for (;; p++) { if (p[0] == '\'') { // 'c' or '\n' or '\000' - if (!p[1]) { // ' at end of line + if (p[1] == NUL) { // ' at end of line break; } i = 2; - if (p[1] == '\\') { // '\n' or '\000' + if (p[1] == '\\' && p[2] != NUL) { // '\n' or '\000' i++; while (ascii_isdigit(p[i - 1])) { // '\000' i++; @@ -178,24 +170,24 @@ static char_u *skip_string(char_u *p) continue; // continue for another string } } else if (p[0] == 'R' && p[1] == '"') { - // Raw string: R"[delim](...)[delim]" - char_u *delim = p + 2; - char_u *paren = vim_strchr(delim, '('); - - if (paren != NULL) { - const ptrdiff_t delim_len = paren - delim; - - for (p += 3; *p; ++p) - if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 - && p[delim_len + 1] == '"') - { - p += delim_len + 1; - break; - } - if (p[0] == '"') { - continue; // continue for another string - } + // Raw string: R"[delim](...)[delim]" + const char_u *delim = p + 2; + const char_u *paren = vim_strchr(delim, '('); + + if (paren != NULL) { + const ptrdiff_t delim_len = paren - delim; + + for (p += 3; *p; p++) { + if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0 + && p[delim_len + 1] == '"') { + p += delim_len + 1; + break; + } + } + if (p[0] == '"') { + continue; // continue for another string } + } } break; // no string found } @@ -205,6 +197,16 @@ static char_u *skip_string(char_u *p) return p; } +/// @returns true if "line[col]" is inside a C string. +int is_pos_in_string(const char_u *line, colnr_T col) +{ + const char_u *p; + + for (p = line; *p && (colnr_T)(p - line) < col; p++) { + p = skip_string(p); + } + return !((colnr_T)(p - line) <= col); +} /* * Functions for C-indenting. @@ -218,7 +220,7 @@ static char_u *skip_string(char_u *p) /* * Return true if the string "line" starts with a word from 'cinwords'. */ -bool cin_is_cinword(char_u *line) +bool cin_is_cinword(const char_u *line) { bool retval = false; @@ -246,10 +248,10 @@ bool cin_is_cinword(char_u *line) * Skip over white space and C comments within the line. * Also skip over Perl/shell comments if desired. */ -static char_u *cin_skipcomment(char_u *s) +static const char_u *cin_skipcomment(const char_u *s) { while (*s) { - char_u *prev_s = s; + const char_u *prev_s = s; s = skipwhite(s); @@ -283,7 +285,7 @@ static char_u *cin_skipcomment(char_u *s) * Return TRUE if there is no code at *s. White space and comments are * not considered code. */ -static int cin_nocode(char_u *s) +static int cin_nocode(const char_u *s) { return *cin_skipcomment(s) == NUL; } @@ -312,9 +314,9 @@ static pos_T *find_line_comment(void) // XXX } /// Checks if `text` starts with "key:". -static bool cin_has_js_key(char_u *text) +static bool cin_has_js_key(const char_u *text) { - char_u *s = skipwhite(text); + const char_u *s = skipwhite(text); char_u quote = 0; if (*s == '\'' || *s == '"') { @@ -341,7 +343,7 @@ static bool cin_has_js_key(char_u *text) /// Checks if string matches "label:"; move to character after ':' if true. /// "*s" must point to the start of the label, if there is one. -static bool cin_islabel_skip(char_u **s) +static bool cin_islabel_skip(const char_u **s) FUNC_ATTR_NONNULL_ALL { if (!vim_isIDc(**s)) { // need at least one ID character @@ -361,7 +363,7 @@ static bool cin_islabel_skip(char_u **s) // Note: curwin->w_cursor must be where we are looking for the label. bool cin_islabel(void) // XXX { - char_u *s = cin_skipcomment(get_cursor_line_ptr()); + const char_u *s = cin_skipcomment(get_cursor_line_ptr()); // Exclude "default" from labels, since it should be indented // like a switch label. Same for C++ scope declarations. @@ -380,8 +382,8 @@ bool cin_islabel(void) // XXX * label. */ pos_T cursor_save; - pos_T *trypos; - char_u *line; + pos_T *trypos; + const char_u *line; cursor_save = curwin->w_cursor; while (curwin->w_cursor.lnum > 1) { @@ -424,8 +426,8 @@ bool cin_islabel(void) // XXX */ static int cin_isinit(void) { - char_u *s; - static char *skip[] = {"static", "public", "protected", "private"}; + const char_u *s; + static char *skip[] = { "static", "public", "protected", "private" }; s = cin_skipcomment(get_cursor_line_ptr()); @@ -460,7 +462,7 @@ static int cin_isinit(void) * Recognize a switch label: "case .*:" or "default:". */ bool cin_iscase( - char_u *s, + const char_u *s, bool strict // Allow relaxed check of case statement for JS ) { @@ -503,7 +505,7 @@ bool cin_iscase( /* * Recognize a "default" switch label. */ -static int cin_isdefault(char_u *s) +static int cin_isdefault(const char_u *s) { return STRNCMP(s, "default", 7) == 0 && *(s = cin_skipcomment(s + 7)) == ':' @@ -513,7 +515,7 @@ static int cin_isdefault(char_u *s) /* * Recognize a "public/private/protected" scope declaration label. */ -bool cin_isscopedecl(char_u *s) +bool cin_isscopedecl(const char_u *s) { int i; @@ -534,13 +536,18 @@ bool cin_isscopedecl(char_u *s) #define FIND_NAMESPACE_LIM 20 // Recognize a "namespace" scope declaration. -static bool cin_is_cpp_namespace(char_u *s) +static bool cin_is_cpp_namespace(const char_u *s) { - char_u *p; + const char_u *p; bool has_name = false; bool has_name_start = false; s = cin_skipcomment(s); + + if (STRNCMP(s, "inline", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) { + s = cin_skipcomment(skipwhite(s + 6)); + } + if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9]))) { p = cin_skipcomment(skipwhite(s + 9)); while (*p != NUL) { @@ -576,7 +583,7 @@ static bool cin_is_cpp_namespace(char_u *s) * case 234: a = b; * ^ */ -static char_u *after_label(char_u *l) +static const char_u *after_label(const char_u *l) { for (; *l; ++l) { if (*l == ':') { @@ -603,10 +610,10 @@ static char_u *after_label(char_u *l) */ static int get_indent_nolabel(linenr_T lnum) // XXX { - char_u *l; + const char_u *l; pos_T fp; colnr_T col; - char_u *p; + const char_u *p; l = ml_get(lnum); p = after_label(l); @@ -625,9 +632,9 @@ static int get_indent_nolabel(linenr_T lnum) // XXX * label: if (asdf && asdfasdf) * ^ */ -static int skip_label(linenr_T lnum, char_u **pp) +static int skip_label(linenr_T lnum, const char_u **pp) { - char_u *l; + const char_u *l; int amount; pos_T cursor_save; @@ -708,8 +715,8 @@ static int cin_first_id_amount(void) */ static int cin_get_equal_amount(linenr_T lnum) { - char_u *line; - char_u *s; + const char_u *line; + const char_u *s; colnr_T col; pos_T fp; @@ -747,7 +754,7 @@ static int cin_get_equal_amount(linenr_T lnum) /* * Recognize a preprocessor statement: Any line that starts with '#'. */ -static int cin_ispreproc(char_u *s) +static int cin_ispreproc(const char_u *s) { if (*skipwhite(s) == '#') return TRUE; @@ -758,9 +765,9 @@ static int cin_ispreproc(char_u *s) /// continuation line of a preprocessor statement. Decrease "*lnump" to the /// start and return the line in "*pp". /// Put the amount of indent in "*amount". -static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) +static int cin_ispreproc_cont(const char_u **pp, linenr_T *lnump, int *amount) { - char_u *line = *pp; + const char_u *line = *pp; linenr_T lnum = *lnump; int retval = false; int candidate_amount = *amount; @@ -794,7 +801,7 @@ static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount) /* * Recognize the start of a C or C++ comment. */ -static int cin_iscomment(char_u *p) +static int cin_iscomment(const char_u *p) { return p[0] == '/' && (p[1] == '*' || p[1] == '/'); } @@ -802,7 +809,7 @@ static int cin_iscomment(char_u *p) /* * Recognize the start of a "//" comment. */ -static int cin_islinecomment(char_u *p) +static int cin_islinecomment(const char_u *p) { return p[0] == '/' && p[1] == '/'; } @@ -817,8 +824,8 @@ static int cin_islinecomment(char_u *p) * both apply in order to determine initializations). */ static char_u -cin_isterminated ( - char_u *s, +cin_isterminated( + const char_u *s, int incl_open, // include '{' at the end as terminator int incl_comma // recognize a trailing comma ) @@ -867,9 +874,9 @@ cin_isterminated ( /// lines here. /// @param[in] first_lnum Where to start looking. /// @param[in] min_lnum The line before which we will not be looking. -static int cin_isfuncdecl(char_u **sp, linenr_T first_lnum, linenr_T min_lnum) +static int cin_isfuncdecl(const char_u **sp, linenr_T first_lnum, linenr_T min_lnum) { - char_u *s; + const char_u *s; linenr_T lnum = first_lnum; linenr_T save_lnum = curwin->w_cursor.lnum; int retval = false; @@ -970,12 +977,12 @@ done: return retval; } -static int cin_isif(char_u *p) +static int cin_isif(const char_u *p) { return STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]); } -static int cin_iselse(char_u *p) +static int cin_iselse(const char_u *p) { if (*p == '}') { // accept "} else" p = cin_skipcomment(p + 1); @@ -983,7 +990,7 @@ static int cin_iselse(char_u *p) return STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]); } -static int cin_isdo(char_u *p) +static int cin_isdo(const char_u *p) { return STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]); } @@ -993,7 +1000,7 @@ static int cin_isdo(char_u *p) * We only accept a "while (condition) ;", with only white space between the * ')' and ';'. The condition may be spread over several lines. */ -static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX +static int cin_iswhileofdo(const char_u *p, linenr_T lnum) // XXX { pos_T cursor_save; pos_T *trypos; @@ -1027,7 +1034,7 @@ static int cin_iswhileofdo(char_u *p, linenr_T lnum) // XXX * Otherwise return !0 and update "*poffset" to point to the place where the * string was found. */ -static int cin_is_if_for_while_before_offset(char_u *line, int *poffset) +static int cin_is_if_for_while_before_offset(const char_u *line, int *poffset) { int offset = *poffset; @@ -1071,10 +1078,10 @@ probablyFound: */ static int cin_iswhileofdo_end(int terminated) { - char_u *line; - char_u *p; - char_u *s; - pos_T *trypos; + const char_u *line; + const char_u *p; + const char_u *s; + pos_T *trypos; int i; if (terminated != ';') { // there must be a ';' at the end @@ -1114,7 +1121,7 @@ static int cin_iswhileofdo_end(int terminated) return FALSE; } -static int cin_isbreak(char_u *p) +static int cin_isbreak(const char_u *p) { return STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]); } @@ -1134,10 +1141,10 @@ static int cin_isbreak(char_u *p) */ static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached) { lpos_T *pos = &cached->lpos; // find position - char_u *s; + const char_u *s; int class_or_struct, lookfor_ctor_init, cpp_base_class; linenr_T lnum = curwin->w_cursor.lnum; - char_u *line = get_cursor_line_ptr(); + const char_u *line = get_cursor_line_ptr(); if (pos->lnum <= lnum) { return cached->found; // Use the cached result @@ -1305,10 +1312,10 @@ static int get_baseclass_amount(int col) * white space and comments. Skip strings and comments. * Ignore "ignore" after "find" if it's not NULL. */ -static int cin_ends_in(char_u *s, char_u *find, char_u *ignore) +static int cin_ends_in(const char_u *s, const char_u *find, const char_u *ignore) { - char_u *p = s; - char_u *r; + const char_u *p = s; + const char_u *r; int len = (int)STRLEN(find); while (*p != NUL) { @@ -1329,7 +1336,7 @@ static int cin_ends_in(char_u *s, char_u *find, char_u *ignore) /* * Return TRUE when "s" starts with "word" and then a non-ID character. */ -static int cin_starts_with(char_u *s, char *word) +static int cin_starts_with(const char_u *s, const char *word) { int l = (int)STRLEN(word); @@ -1337,10 +1344,10 @@ static int cin_starts_with(char_u *s, char *word) } /// Recognize a `extern "C"` or `extern "C++"` linkage specifications. -static int cin_is_cpp_extern_c(char_u *s) +static int cin_is_cpp_extern_c(const char_u *s) { - char_u *p; - int has_string_literal = false; + const char_u *p; + int has_string_literal = false; s = cin_skipcomment(s); if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6]))) { @@ -1379,9 +1386,9 @@ static int cin_is_cpp_extern_c(char_u *s) */ static int cin_skip2pos(pos_T *trypos) { - char_u *line; - char_u *p; - char_u *new_p; + const char_u *line; + const char_u *p; + const char_u *new_p; p = line = ml_get(trypos->lnum); while (*p && (colnr_T)(p - line) < trypos->col) { @@ -1411,8 +1418,8 @@ static int cin_skip2pos(pos_T *trypos) static pos_T *find_start_brace(void) // XXX { pos_T cursor_save; - pos_T *trypos; - pos_T *pos; + pos_T *trypos; + pos_T *pos; static pos_T pos_copy; cursor_save = curwin->w_cursor; @@ -1427,7 +1434,7 @@ static pos_T *find_start_brace(void) // XXX break; } if (pos != NULL) { - curwin->w_cursor.lnum = pos->lnum; + curwin->w_cursor = *pos; } } curwin->w_cursor = cursor_save; @@ -1524,7 +1531,7 @@ static int corr_ind_maxparen(pos_T *startpos) * Set w_cursor.col to the column number of the last unmatched ')' or '{' in * line "l". "l" must point to the start of the line. */ -static int find_last_paren(char_u *l, int start, int end) +static int find_last_paren(const char_u *l, int start, int end) { int i; int retval = FALSE; @@ -1796,8 +1803,8 @@ int get_c_indent(void) #define BRACE_AT_START 2 // '{' is at start of line #define BRACE_AT_END 3 // '{' is at end of line linenr_T ourscope; - char_u *l; - char_u *look; + const char_u *l; + const char_u *look; char_u terminated; int lookfor; #define LOOKFOR_INITIAL 0 @@ -1901,12 +1908,25 @@ int get_c_indent(void) * If we're inside a "//" comment and there is a "//" comment in a * previous line, lineup with that one. */ - if (cin_islinecomment(theline) - && (trypos = find_line_comment()) != NULL) { // XXX - // find how indented the line beginning the comment is - getvcol(curwin, trypos, &col, NULL, NULL); - amount = col; - goto theend; + if (cin_islinecomment(theline)) { + pos_T linecomment_pos; + + trypos = find_line_comment(); // XXX + if (trypos == NULL && curwin->w_cursor.lnum > 1) { + // There may be a statement before the comment, search from the end + // of the line for a comment start. + linecomment_pos.col = check_linecomment(ml_get(curwin->w_cursor.lnum - 1)); + if (linecomment_pos.col != MAXCOL) { + trypos = &linecomment_pos; + trypos->lnum = curwin->w_cursor.lnum - 1; + } + } + if (trypos != NULL) { + // find how indented the line beginning the comment is + getvcol(curwin, trypos, &col, NULL, NULL); + amount = col; + goto theend; + } } /* * If we're inside a comment and not looking at the start of the @@ -3597,9 +3617,9 @@ laterend: static int find_match(int lookfor, linenr_T ourscope) { - char_u *look; - pos_T *theirscope; - char_u *mightbeif; + const char_u *look; + pos_T *theirscope; + const char_u *mightbeif; int elselevel; int whilelevel; diff --git a/src/nvim/input.c b/src/nvim/input.c index 2f7c5c2c16..5fa9b8b343 100644 --- a/src/nvim/input.c +++ b/src/nvim/input.c @@ -105,7 +105,7 @@ int get_keystroke(MultiQueue *events) // terminal code to complete. n = os_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0, events); if (n > 0) { - // Replace zero and CSI by a special key code. + // Replace zero and K_SPECIAL by a special key code. n = fix_input_buffer(buf + len, n); len += n; waited = 0; diff --git a/src/nvim/keymap.c b/src/nvim/keymap.c index abf016b832..32f2158d7b 100644 --- a/src/nvim/keymap.c +++ b/src/nvim/keymap.c @@ -158,7 +158,6 @@ static const struct key_name_entry { { ESC, "Esc" }, { ESC, "Escape" }, // Alternative name { CSI, "CSI" }, - { K_CSI, "xCSI" }, { '|', "Bar" }, { '\\', "Bslash" }, { K_DEL, "Del" }, @@ -964,7 +963,6 @@ char_u *replace_termcodes(const char_u *from, const size_t from_len, char_u **bu for (i = utfc_ptr2len_len(src, (int)(end - src) + 1); i > 0; i--) { // If the character is K_SPECIAL, replace it with K_SPECIAL // KS_SPECIAL KE_FILLER. - // If compiled with the GUI replace CSI with K_CSI. if (*src == K_SPECIAL) { result[dlen++] = K_SPECIAL; result[dlen++] = KS_SPECIAL; diff --git a/src/nvim/keymap.h b/src/nvim/keymap.h index 5a74d1dc00..ae2ec7835e 100644 --- a/src/nvim/keymap.h +++ b/src/nvim/keymap.h @@ -122,8 +122,6 @@ // // Entries must be in the range 0x02-0x7f (see comment at K_SPECIAL). enum key_extra { - KE_NAME = 3, // name of this terminal entry - KE_S_UP = 4, // shift-up KE_S_DOWN = 5, // shift-down @@ -220,7 +218,7 @@ enum key_extra { KE_KINS = 79, // keypad Insert key KE_KDEL = 80, // keypad Delete key - KE_CSI = 81, // CSI typed directly + // KE_CSI = 81, // Nvim doesn't need escaping CSI KE_SNR = 82, // <SNR> KE_PLUG = 83, // <Plug> KE_CMDWIN = 84, // open command-line window from Command-line Mode @@ -435,7 +433,6 @@ enum key_extra { #define K_MOUSELEFT TERMCAP2KEY(KS_EXTRA, KE_MOUSELEFT) #define K_MOUSERIGHT TERMCAP2KEY(KS_EXTRA, KE_MOUSERIGHT) -#define K_CSI TERMCAP2KEY(KS_EXTRA, KE_CSI) #define K_SNR TERMCAP2KEY(KS_EXTRA, KE_SNR) #define K_PLUG TERMCAP2KEY(KS_EXTRA, KE_PLUG) #define K_CMDWIN TERMCAP2KEY(KS_EXTRA, KE_CMDWIN) diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 8a702ddd60..0fbd56ed53 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -617,6 +617,14 @@ bool nlua_push_typval(lua_State *lstate, typval_T *const tv, bool special) semsg(_("E1502: Lua failed to grow stack to %i"), initial_size + 4); return false; } + if (tv->v_type == VAR_FUNC) { + ufunc_T *fp = find_func(tv->vval.v_string); + assert(fp != NULL); + if (fp->uf_cb == nlua_CFunction_func_call) { + nlua_pushref(lstate, ((LuaCFunctionState *)fp->uf_cb_state)->lua_callable.func_ref); + return true; + } + } if (encode_vim_to_lua(lstate, tv, "nlua_push_typval argument") == FAIL) { return false; } diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 7d43d21d53..cbfb8364f6 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -4,6 +4,7 @@ #include <lauxlib.h> #include <lua.h> #include <lualib.h> +#include <tree_sitter/api.h> #include "luv/luv.h" #include "nvim/api/private/defs.h" @@ -434,6 +435,15 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL // [package, loaded, module] lua_setfield(lstate, -2, "vim.F"); // [package, loaded] + code = (char *)&lua_filetype_module[0]; + if (luaL_loadbuffer(lstate, code, sizeof(lua_filetype_module) - 1, "@vim/filetype.lua") + || nlua_pcall(lstate, 0, 1)) { + nlua_error(lstate, _("E5106: Error while creating vim.filetype module: %.*s")); + return 1; + } + // [package, loaded, module] + lua_setfield(lstate, -2, "vim.filetype"); // [package, loaded] + lua_pop(lstate, 2); // [] } @@ -672,7 +682,7 @@ int nlua_call(lua_State *lstate) typval_T vim_args[MAX_FUNC_ARGS + 1]; int i = 0; // also used for freeing the variables for (; i < nargs; i++) { - lua_pushvalue(lstate, (int)i+2); + lua_pushvalue(lstate, i+2); if (!nlua_pop_typval(lstate, &vim_args[i])) { api_set_error(&err, kErrorTypeException, "error converting argument %d", i+1); @@ -737,7 +747,7 @@ static int nlua_rpc(lua_State *lstate, bool request) Array args = ARRAY_DICT_INIT; for (int i = 0; i < nargs; i++) { - lua_pushvalue(lstate, (int)i+3); + lua_pushvalue(lstate, i+3); ADD(args, nlua_pop_Object(lstate, false, &err)); if (ERROR_SET(&err)) { api_free_array(args); @@ -1115,11 +1125,23 @@ void ex_lua(exarg_T *const eap) FUNC_ATTR_NONNULL_ALL { size_t len; - char *const code = script_get(eap, &len); + char *code = script_get(eap, &len); if (eap->skip) { xfree(code); return; } + // When =expr is used transform it to print(vim.inspect(expr)) + if (code[0] == '=') { + len += sizeof("vim.pretty_print()") - sizeof("="); + // code_buf needs to be 1 char larger then len for null byte in the end. + // lua nlua_typval_exec doesn't expect null terminated string so len + // needs to end before null byte. + char *code_buf = xmallocz(len); + vim_snprintf(code_buf, len+1, "vim.pretty_print(%s)", code+1); + xfree(code); + code = code_buf; + } + nlua_typval_exec(code, len, ":lua", NULL, 0, false, NULL); xfree(code); @@ -1246,6 +1268,12 @@ int tslua_get_language_version(lua_State *L) return 1; } +int tslua_get_minimum_language_version(lua_State *L) +{ + lua_pushnumber(L, TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION); + return 1; +} + static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { tslua_init(lstate); @@ -1267,6 +1295,9 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_pushcfunction(lstate, tslua_get_language_version); lua_setfield(lstate, -2, "_ts_get_language_version"); + + lua_pushcfunction(lstate, tslua_get_minimum_language_version); + lua_setfield(lstate, -2, "_ts_get_minimum_language_version"); } int nlua_expand_pat(expand_T *xp, char_u *pat, int *num_results, char_u ***results) diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 18a579ed0f..55b23cf0c8 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -25,6 +25,7 @@ #include "nvim/func_attr.h" #include "nvim/garray.h" #include "nvim/getchar.h" +#include "nvim/globals.h" #include "nvim/lua/converter.h" #include "nvim/lua/executor.h" #include "nvim/lua/stdlib.h" @@ -408,6 +409,12 @@ int nlua_getvar(lua_State *lstate) const char *name = luaL_checklstring(lstate, 3, &len); dictitem_T *di = tv_dict_find(dict, name, (ptrdiff_t)len); + if (di == NULL && dict == &globvardict) { // try to autoload script + if (!script_autoload(name, len, false) || aborting()) { + return 0; // nil + } + di = tv_dict_find(dict, name, (ptrdiff_t)len); + } if (di == NULL) { return 0; // nil } diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 60a000843f..f4067ad02f 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -129,6 +129,10 @@ void tslua_init(lua_State *L) build_meta(L, TS_META_QUERY, query_meta); build_meta(L, TS_META_QUERYCURSOR, querycursor_meta); build_meta(L, TS_META_TREECURSOR, treecursor_meta); + +#ifdef NVIM_TS_HAS_SET_ALLOCATOR + ts_set_allocator(xmalloc, xcalloc, xrealloc, xfree); +#endif } int tslua_has_language(lua_State *L) diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index f9b15d242a..f5993c3f55 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -40,6 +40,9 @@ assert(vim) vim.inspect = package.loaded['vim.inspect'] assert(vim.inspect) +vim.filetype = package.loaded['vim.filetype'] +assert(vim.filetype) + local pathtrails = {} vim._so_trails = {} for s in (package.cpath..';'):gmatch('[^;]*;') do @@ -84,7 +87,8 @@ function vim._load_package(name) return nil end -table.insert(package.loaders, 1, vim._load_package) +-- Insert vim._load_package after the preloader at position 2 +table.insert(package.loaders, 2, vim._load_package) -- These are for loading runtime modules lazily since they aren't available in -- the nvim binary as specified in executor.c @@ -112,6 +116,9 @@ setmetatable(vim, { elseif key == 'ui' then t.ui = require('vim.ui') return t.ui + elseif key == 'keymap' then + t.keymap = require('vim.keymap') + return t.keymap end end }) @@ -419,23 +426,43 @@ function vim.defer_fn(fn, timeout) end ---- Notification provider +--- Display a notification to the user. +--- +--- This function can be overridden by plugins to display notifications using a +--- custom provider (such as the system notification provider). By default, +--- writes to |:messages|. --- ---- Without a runtime, writes to :Messages ----@see :help nvim_notify ----@param msg string Content of the notification to show to the user ----@param log_level number|nil enum from |vim.log.levels| ----@param opts table|nil additional options (timeout, etc) -function vim.notify(msg, log_level, opts) -- luacheck: no unused - if log_level == vim.log.levels.ERROR then +---@param msg string Content of the notification to show to the user. +---@param level number|nil One of the values from |vim.log.levels|. +---@param opts table|nil Optional parameters. Unused by default. +function vim.notify(msg, level, opts) -- luacheck: no unused args + if level == vim.log.levels.ERROR then vim.api.nvim_err_writeln(msg) - elseif log_level == vim.log.levels.WARN then + elseif level == vim.log.levels.WARN then vim.api.nvim_echo({{msg, 'WarningMsg'}}, true, {}) else vim.api.nvim_echo({{msg}}, true, {}) end end +do + local notified = {} + + --- Display a notification only one time. + --- + --- Like |vim.notify()|, but subsequent calls with the same message will not + --- display a notification. + --- + ---@param msg string Content of the notification to show to the user. + ---@param level number|nil One of the values from |vim.log.levels|. + ---@param opts table|nil Optional parameters. Unused by default. + function vim.notify_once(msg, level, opts) -- luacheck: no unused args + if not notified[msg] then + vim.notify(msg, level, opts) + notified[msg] = true + end + end +end ---@private function vim.register_keystroke_callback() @@ -663,4 +690,23 @@ vim._expand_pat_get_parts = function(lua_string) return parts, search_index end +---Prints given arguments in human-readable format. +---Example: +---<pre> +--- -- Print highlight group Normal and store it's contents in a variable. +--- local hl_normal = vim.pretty_print(vim.api.nvim_get_hl_by_name("Normal", true)) +---</pre> +---@see |vim.inspect()| +---@return given arguments. +function vim.pretty_print(...) + local objects = {} + for i = 1, select('#', ...) do + local v = select(i, ...) + table.insert(objects, vim.inspect(v)) + end + + print(table.concat(objects, ' ')) + return ... +end + return module diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index b2e971f9f3..ea7a700e1e 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -184,11 +184,11 @@ static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg, if (strequal("myers", v->data.string.data)) { // default } else if (strequal("minimal", v->data.string.data)) { - cfg->flags |= XDF_NEED_MINIMAL; + params->flags |= XDF_NEED_MINIMAL; } else if (strequal("patience", v->data.string.data)) { - cfg->flags |= XDF_PATIENCE_DIFF; + params->flags |= XDF_PATIENCE_DIFF; } else if (strequal("histogram", v->data.string.data)) { - cfg->flags |= XDF_HISTOGRAM_DIFF; + params->flags |= XDF_HISTOGRAM_DIFF; } else { api_set_error(err, kErrorTypeValidation, "not a valid algorithm"); goto exit_1; diff --git a/src/nvim/macros.h b/src/nvim/macros.h index c2b2c89abf..d5611f587b 100644 --- a/src/nvim/macros.h +++ b/src/nvim/macros.h @@ -105,6 +105,9 @@ #define MB_PTR_BACK(s, p) \ (p -= utf_head_off((char_u *)s, (char_u *)p - 1) + 1) +// MB_CHAR2BYTES(): convert character to bytes and advance pointer to bytes +#define MB_CHAR2BYTES(c, b) ((b) += utf_char2bytes((c), (b))) + #define RESET_BINDING(wp) \ do { \ (wp)->w_p_scb = false; \ diff --git a/src/nvim/main.c b/src/nvim/main.c index cbd1f53727..8991ae7f00 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -341,9 +341,11 @@ int main(int argc, char **argv) init_default_autocmds(); TIME_MSG("init default autocommands"); + bool vimrc_none = params.use_vimrc != NULL && strequal(params.use_vimrc, "NONE"); + // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments. // Allows for setting 'loadplugins' there. - if (params.use_vimrc != NULL && strequal(params.use_vimrc, "NONE")) { + if (vimrc_none) { // When using --clean we still want to load plugins p_lpl = params.clean; } @@ -351,14 +353,23 @@ int main(int argc, char **argv) // Execute --cmd arguments. exe_pre_commands(¶ms); + if (!vimrc_none) { + // Sources ftplugin.vim and indent.vim. We do this *before* the user startup scripts to ensure + // ftplugins run before FileType autocommands defined in the init file (which allows those + // autocommands to overwrite settings from ftplugins). + filetype_plugin_enable(); + } + // Source startup scripts. source_startup_scripts(¶ms); // If using the runtime (-u is not NONE), enable syntax & filetype plugins. - if (params.use_vimrc == NULL || !strequal(params.use_vimrc, "NONE")) { - // Does ":filetype plugin indent on". + if (!vimrc_none) { + // Sources filetype.lua and filetype.vim unless the user explicitly disabled it with :filetype + // off. filetype_maybe_enable(); - // Sources syntax/syntax.vim, which calls `:filetype on`. + // Sources syntax/syntax.vim. We do this *after* the user startup scripts so that users can + // disable syntax highlighting with `:syntax off` if they wish. syn_maybe_enable(); } @@ -1549,7 +1560,7 @@ static void edit_buffers(mparm_T *parmp, char_u *cwd) // When w_arg_idx is -1 remove the window (see create_windows()). if (curwin->w_arg_idx == -1) { - win_close(curwin, true); + win_close(curwin, true, false); advance = false; } @@ -1561,7 +1572,7 @@ static void edit_buffers(mparm_T *parmp, char_u *cwd) // When w_arg_idx is -1 remove the window (see create_windows()). if (curwin->w_arg_idx == -1) { arg_idx++; - win_close(curwin, true); + win_close(curwin, true, false); advance = false; continue; } @@ -1608,7 +1619,7 @@ static void edit_buffers(mparm_T *parmp, char_u *cwd) did_emsg = FALSE; // avoid hit-enter prompt getout(1); } - win_close(curwin, true); + win_close(curwin, true, false); advance = false; } if (arg_idx == GARGCOUNT - 1) { diff --git a/src/nvim/map.c b/src/nvim/map.c index c77433df71..77ebc2a387 100644 --- a/src/nvim/map.c +++ b/src/nvim/map.c @@ -171,10 +171,7 @@ MAP_IMPL(ptr_t, ptr_t, DEFAULT_INITIALIZER) MAP_IMPL(uint64_t, ptr_t, DEFAULT_INITIALIZER) MAP_IMPL(uint64_t, ssize_t, SSIZE_INITIALIZER) MAP_IMPL(uint64_t, uint64_t, DEFAULT_INITIALIZER) -#define EXTMARK_NS_INITIALIZER { { MAP_INIT }, 1 } -MAP_IMPL(uint64_t, ExtmarkNs, EXTMARK_NS_INITIALIZER) -#define EXTMARK_ITEM_INITIALIZER { 0, 0, NULL } -MAP_IMPL(uint64_t, ExtmarkItem, EXTMARK_ITEM_INITIALIZER) +MAP_IMPL(uint32_t, uint32_t, DEFAULT_INITIALIZER) MAP_IMPL(handle_T, ptr_t, DEFAULT_INITIALIZER) #define MSGPACK_HANDLER_INITIALIZER { .fn = NULL, .fast = false } MAP_IMPL(String, MsgpackRpcRequestHandler, MSGPACK_HANDLER_INITIALIZER) diff --git a/src/nvim/map.h b/src/nvim/map.h index dbd85a4e1f..5e56f4dd65 100644 --- a/src/nvim/map.h +++ b/src/nvim/map.h @@ -40,16 +40,8 @@ MAP_DECLS(ptr_t, ptr_t) MAP_DECLS(uint64_t, ptr_t) MAP_DECLS(uint64_t, ssize_t) MAP_DECLS(uint64_t, uint64_t) +MAP_DECLS(uint32_t, uint32_t) -// NB: this is the only way to define a struct both containing and contained -// in a map... -typedef struct ExtmarkNs { // For namespacing extmarks - Map(uint64_t, uint64_t) map[1]; // For fast lookup - uint64_t free_id; // For automatically assigning id's -} ExtmarkNs; - -MAP_DECLS(uint64_t, ExtmarkNs) -MAP_DECLS(uint64_t, ExtmarkItem) MAP_DECLS(handle_T, ptr_t) MAP_DECLS(String, MsgpackRpcRequestHandler) MAP_DECLS(HlEntry, int) diff --git a/src/nvim/mark.c b/src/nvim/mark.c index 39f18b333d..8b29aa3676 100644 --- a/src/nvim/mark.c +++ b/src/nvim/mark.c @@ -217,8 +217,8 @@ void checkpcmark(void) && (equalpos(curwin->w_pcmark, curwin->w_cursor) || curwin->w_pcmark.lnum == 0)) { curwin->w_pcmark = curwin->w_prev_pcmark; - curwin->w_prev_pcmark.lnum = 0; // Show it has been checked } + curwin->w_prev_pcmark.lnum = 0; // it has been checked } /* diff --git a/src/nvim/marktree.c b/src/nvim/marktree.c index 38014ab375..918db8b76c 100644 --- a/src/nvim/marktree.c +++ b/src/nvim/marktree.c @@ -56,12 +56,6 @@ #define T MT_BRANCH_FACTOR #define ILEN (sizeof(mtnode_t)+(2 * T) * sizeof(void *)) -#define RIGHT_GRAVITY (((uint64_t)1) << 63) -#define ANTIGRAVITY(id) ((id)&(RIGHT_GRAVITY-1)) -#define IS_RIGHT(id) ((id)&RIGHT_GRAVITY) - -#define PAIRED MARKTREE_PAIRED_FLAG -#define END_FLAG MARKTREE_END_FLAG #define ID_INCR (((uint64_t)1) << 2) #define rawkey(itr) (itr->node->key[itr->i]) @@ -119,7 +113,7 @@ static int key_cmp(mtkey_t a, mtkey_t b) } // NB: keeping the events at the same pos sorted by id is actually not // necessary only make sure that START is before END etc. - return mt_generic_cmp(a.id, b.id); + return mt_generic_cmp(a.flags, b.flags); } static inline int marktree_getp_aux(const mtnode_t *x, mtkey_t k, int *r) @@ -148,7 +142,7 @@ static inline int marktree_getp_aux(const mtnode_t *x, mtkey_t k, int *r) static inline void refkey(MarkTree *b, mtnode_t *x, int i) { - pmap_put(uint64_t)(b->id2node, ANTIGRAVITY(x->key[i].id), x); + pmap_put(uint64_t)(b->id2node, mt_lookup_key(x->key[i]), x); } // put functions @@ -221,38 +215,28 @@ static inline void marktree_putp_aux(MarkTree *b, mtnode_t *x, mtkey_t k) } } -uint64_t marktree_put(MarkTree *b, int row, int col, bool right_gravity, uint8_t decor_level) +void marktree_put(MarkTree *b, mtkey_t key, int end_row, int end_col, bool end_right) { - uint64_t id = (b->next_id+=ID_INCR); - assert(decor_level < DECOR_LEVELS); - id = id | ((uint64_t)decor_level << DECOR_OFFSET); - uint64_t keyid = id; - if (right_gravity) { - // order all right gravity keys after the left ones, for effortless - // insertion (but not deletion!) - keyid |= RIGHT_GRAVITY; - } - marktree_put_key(b, row, col, keyid); - return id; -} + assert(!(key.flags & ~MT_FLAG_EXTERNAL_MASK)); + if (end_row >= 0) { + key.flags |= MT_FLAG_PAIRED; + } -uint64_t marktree_put_pair(MarkTree *b, int start_row, int start_col, bool start_right, int end_row, - int end_col, bool end_right, uint8_t decor_level) -{ - uint64_t id = (b->next_id+=ID_INCR)|PAIRED; - assert(decor_level < DECOR_LEVELS); - id = id | ((uint64_t)decor_level << DECOR_OFFSET); - uint64_t start_id = id|(start_right?RIGHT_GRAVITY:0); - uint64_t end_id = id|END_FLAG|(end_right?RIGHT_GRAVITY:0); - marktree_put_key(b, start_row, start_col, start_id); - marktree_put_key(b, end_row, end_col, end_id); - return id; + marktree_put_key(b, key); + + if (end_row >= 0) { + mtkey_t end_key = key; + end_key.flags = (uint16_t)((uint16_t)(key.flags & ~MT_FLAG_RIGHT_GRAVITY) + |(uint16_t)MT_FLAG_END + |(uint16_t)(end_right ? MT_FLAG_RIGHT_GRAVITY : 0)); + end_key.pos = (mtpos_t){ end_row, end_col }; + marktree_put_key(b, end_key); + } } -void marktree_put_key(MarkTree *b, int row, int col, uint64_t id) +void marktree_put_key(MarkTree *b, mtkey_t k) { - mtkey_t k = { .pos = { .row = row, .col = col }, .id = id }; - + k.flags |= MT_FLAG_REAL; // let's be real. if (!b->root) { b->root = (mtnode_t *)xcalloc(1, ILEN); b->n_nodes++; @@ -302,7 +286,7 @@ void marktree_del_itr(MarkTree *b, MarkTreeIter *itr, bool rev) mtnode_t *cur = itr->node; int curi = itr->i; - uint64_t id = cur->key[curi].id; + uint64_t id = mt_lookup_key(cur->key[curi]); // fprintf(stderr, "\nDELET %lu\n", id); if (itr->node->level) { @@ -364,7 +348,7 @@ void marktree_del_itr(MarkTree *b, MarkTreeIter *itr, bool rev) } b->n_keys--; - pmap_del(uint64_t)(b->id2node, ANTIGRAVITY(id)); + pmap_del(uint64_t)(b->id2node, id); // 5. bool itr_dirty = false; @@ -570,23 +554,29 @@ void marktree_free_node(mtnode_t *x) } /// NB: caller must check not pair! -uint64_t marktree_revise(MarkTree *b, MarkTreeIter *itr, uint8_t decor_level) +void marktree_revise(MarkTree *b, MarkTreeIter *itr, uint8_t decor_level, mtkey_t key) { - uint64_t old_id = rawkey(itr).id; - pmap_del(uint64_t)(b->id2node, ANTIGRAVITY(old_id)); - uint64_t new_id = (b->next_id += ID_INCR) + ((uint64_t)decor_level << DECOR_OFFSET); - rawkey(itr).id = new_id + (RIGHT_GRAVITY&old_id); - refkey(b, itr->node, itr->i); - return new_id; + // TODO(bfredl): clean up this mess and re-instantiate &= and |= forms + // once we upgrade to a non-broken version of gcc in functionaltest-lua CI + rawkey(itr).flags = (uint16_t)((uint16_t)rawkey(itr).flags & (uint16_t)~MT_FLAG_DECOR_MASK); + rawkey(itr).flags = (uint16_t)((uint16_t)rawkey(itr).flags + | (uint16_t)(decor_level << MT_FLAG_DECOR_OFFSET) + | (uint16_t)(key.flags & MT_FLAG_DECOR_MASK)); + rawkey(itr).decor_full = key.decor_full; + rawkey(itr).hl_id = key.hl_id; + rawkey(itr).priority = key.priority; } void marktree_move(MarkTree *b, MarkTreeIter *itr, int row, int col) { - uint64_t old_id = rawkey(itr).id; + mtkey_t key = rawkey(itr); // TODO(bfredl): optimize when moving a mark within a leaf without moving it // across neighbours! marktree_del_itr(b, itr, false); - marktree_put_key(b, row, col, old_id); + key.pos = (mtpos_t){ row, col }; + + + marktree_put_key(b, key); itr->node = NULL; // itr might become invalid by put } @@ -602,14 +592,15 @@ bool marktree_itr_get(MarkTree *b, int row, int col, MarkTreeIter *itr) bool marktree_itr_get_ext(MarkTree *b, mtpos_t p, MarkTreeIter *itr, bool last, bool gravity, mtpos_t *oldbase) { - mtkey_t k = { .pos = p, .id = gravity ? RIGHT_GRAVITY : 0 }; - if (last && !gravity) { - k.id = UINT64_MAX; - } if (b->n_keys == 0) { itr->node = NULL; return false; } + + mtkey_t k = { .pos = p, .flags = gravity ? MT_FLAG_RIGHT_GRAVITY : 0 }; + if (last && !gravity) { + k.flags = MT_FLAG_LAST; + } itr->pos = (mtpos_t){ 0, 0 }; itr->node = b->root; itr->lvl = 0; @@ -816,25 +807,29 @@ mtpos_t marktree_itr_pos(MarkTreeIter *itr) return pos; } -mtmark_t marktree_itr_current(MarkTreeIter *itr) +mtkey_t marktree_itr_current(MarkTreeIter *itr) { if (itr->node) { - uint64_t keyid = rawkey(itr).id; - mtpos_t pos = marktree_itr_pos(itr); - mtmark_t mark = { .row = pos.row, - .col = pos.col, - .id = ANTIGRAVITY(keyid), - .right_gravity = keyid & RIGHT_GRAVITY }; - return mark; - } - return (mtmark_t){ -1, -1, 0, false }; + mtkey_t key = rawkey(itr); + key.pos = marktree_itr_pos(itr); + return key; + } + return MT_INVALID_KEY; +} + +static bool itr_eq(MarkTreeIter *itr1, MarkTreeIter *itr2) +{ + return (&rawkey(itr1) == &rawkey(itr2)); } -static void swap_id(uint64_t *id1, uint64_t *id2) +static void itr_swap(MarkTreeIter *itr1, MarkTreeIter *itr2) { - uint64_t temp = *id1; - *id1 = *id2; - *id2 = temp; + mtkey_t key1 = rawkey(itr1); + mtkey_t key2 = rawkey(itr2); + rawkey(itr1) = key2; + rawkey(itr1).pos = key1.pos; + rawkey(itr2) = key1; + rawkey(itr2).pos = key2.pos; } bool marktree_splice(MarkTree *b, int start_line, int start_col, int old_extent_line, @@ -865,7 +860,7 @@ bool marktree_splice(MarkTree *b, int start_line, int start_col, int old_extent_ mtpos_t ipos = marktree_itr_pos(itr); if (!pos_leq(old_extent, ipos) || (old_extent.row == ipos.row && old_extent.col == ipos.col - && !IS_RIGHT(rawkey(itr).id))) { + && !mt_right(rawkey(itr)))) { marktree_itr_get_ext(b, old_extent, enditr, true, true, NULL); assert(enditr->node); // "assert" (itr <= enditr) @@ -895,13 +890,13 @@ continue_same_node: break; } - if (IS_RIGHT(rawkey(itr).id)) { - while (rawkey(itr).id != rawkey(enditr).id - && IS_RIGHT(rawkey(enditr).id)) { + if (mt_right(rawkey(itr))) { + while (!itr_eq(itr, enditr) + && mt_right(rawkey(enditr))) { marktree_itr_prev(b, enditr); } - if (!IS_RIGHT(rawkey(enditr).id)) { - swap_id(&rawkey(itr).id, &rawkey(enditr).id); + if (!mt_right(rawkey(enditr))) { + itr_swap(itr, enditr); refkey(b, itr->node, itr->i); refkey(b, enditr->node, enditr->i); } else { @@ -911,7 +906,7 @@ continue_same_node: } } - if (rawkey(itr).id == rawkey(enditr).id) { + if (itr_eq(itr, enditr)) { // actually, will be past_right after this key past_right = true; } @@ -1006,13 +1001,13 @@ void marktree_move_region(MarkTree *b, int start_row, colnr_T start_col, int ext marktree_itr_get_ext(b, start, itr, false, true, NULL); kvec_t(mtkey_t) saved = KV_INITIAL_VALUE; while (itr->node) { - mtpos_t pos = marktree_itr_pos(itr); - if (!pos_leq(pos, end) || (pos.row == end.row && pos.col == end.col - && rawkey(itr).id & RIGHT_GRAVITY)) { + mtkey_t k = marktree_itr_current(itr); + if (!pos_leq(k.pos, end) || (k.pos.row == end.row && k.pos.col == end.col + && mt_right(k))) { break; } - relative(start, &pos); - kv_push(saved, ((mtkey_t){ .pos = pos, .id = rawkey(itr).id })); + relative(start, &k.pos); + kv_push(saved, k); marktree_del_itr(b, itr, false); } @@ -1024,30 +1019,36 @@ void marktree_move_region(MarkTree *b, int start_row, colnr_T start_col, int ext for (size_t i = 0; i < kv_size(saved); i++) { mtkey_t item = kv_A(saved, i); unrelative(new, &item.pos); - marktree_put_key(b, item.pos.row, item.pos.col, item.id); + marktree_put_key(b, item); } kv_destroy(saved); } /// @param itr OPTIONAL. set itr to pos. -mtpos_t marktree_lookup(MarkTree *b, uint64_t id, MarkTreeIter *itr) +mtkey_t marktree_lookup_ns(MarkTree *b, uint32_t ns, uint32_t id, bool end, MarkTreeIter *itr) +{ + return marktree_lookup(b, mt_lookup_id(ns, id, end), itr); +} + +/// @param itr OPTIONAL. set itr to pos. +mtkey_t marktree_lookup(MarkTree *b, uint64_t id, MarkTreeIter *itr) { mtnode_t *n = pmap_get(uint64_t)(b->id2node, id); if (n == NULL) { if (itr) { itr->node = NULL; } - return (mtpos_t){ -1, -1 }; + return MT_INVALID_KEY; } int i = 0; for (i = 0; i < n->n; i++) { - if (ANTIGRAVITY(n->key[i].id) == id) { + if (mt_lookup_key(n->key[i]) == id) { goto found; } } abort(); found: {} - mtpos_t pos = n->key[i].pos; + mtkey_t key = n->key[i]; if (itr) { itr->i = i; itr->node = n; @@ -1066,14 +1067,28 @@ found_node: itr->s[b->root->level-p->level].i = i; } if (i > 0) { - unrelative(p->key[i-1].pos, &pos); + unrelative(p->key[i-1].pos, &key.pos); } n = p; } if (itr) { marktree_itr_fix_pos(b, itr); } - return pos; + return key; +} + +mtpos_t marktree_get_altpos(MarkTree *b, mtkey_t mark, MarkTreeIter *itr) +{ + return marktree_get_alt(b, mark, itr).pos; +} + +mtkey_t marktree_get_alt(MarkTree *b, mtkey_t mark, MarkTreeIter *itr) +{ + mtkey_t end = MT_INVALID_KEY; + if (mt_paired(mark)) { + end = marktree_lookup_ns(b, mark.ns, mark.id, !mt_end(mark), itr); + } + return end; } static void marktree_itr_fix_pos(MarkTree *b, MarkTreeIter *itr) @@ -1092,6 +1107,20 @@ static void marktree_itr_fix_pos(MarkTree *b, MarkTreeIter *itr) assert(x == itr->node); } +// for unit test +void marktree_put_test(MarkTree *b, uint32_t id, int row, int col, bool right_gravity) +{ + mtkey_t key = { { row, col }, UINT32_MAX, id, 0, + mt_flags(right_gravity, 0), 0, NULL }; + marktree_put(b, key, -1, -1, false); +} + +// for unit test +bool mt_right_test(mtkey_t key) +{ + return mt_right(key); +} + void marktree_check(MarkTree *b) { #ifndef NDEBUG @@ -1134,11 +1163,11 @@ static size_t check_node(MarkTree *b, mtnode_t *x, mtpos_t *last, bool *last_rig } assert(pos_leq(*last, x->key[i].pos)); if (last->row == x->key[i].pos.row && last->col == x->key[i].pos.col) { - assert(!*last_right || IS_RIGHT(x->key[i].id)); + assert(!*last_right || mt_right(x->key[i])); } - *last_right = IS_RIGHT(x->key[i].id); + *last_right = mt_right(x->key[i]); assert(x->key[i].pos.col >= 0); - assert(pmap_get(uint64_t)(b->id2node, ANTIGRAVITY(x->key[i].id)) == x); + assert(pmap_get(uint64_t)(b->id2node, mt_lookup_key(x->key[i])) == x); } if (x->level) { diff --git a/src/nvim/marktree.h b/src/nvim/marktree.h index a1dcdf5164..30f5aacebc 100644 --- a/src/nvim/marktree.h +++ b/src/nvim/marktree.h @@ -2,9 +2,12 @@ #define NVIM_MARKTREE_H #include <stdint.h> +#include <assert.h> +#include "nvim/assert.h" #include "nvim/garray.h" #include "nvim/map.h" +#include "nvim/types.h" #include "nvim/pos.h" #define MT_MAX_DEPTH 20 @@ -15,13 +18,6 @@ typedef struct { int32_t col; } mtpos_t; -typedef struct { - int32_t row; - int32_t col; - uint64_t id; - bool right_gravity; -} mtmark_t; - typedef struct mtnode_s mtnode_t; typedef struct { int oldcol; @@ -39,12 +35,75 @@ typedef struct { // Internal storage // -// NB: actual marks have id > 0, so we can use (row,col,0) pseudo-key for +// NB: actual marks have flags > 0, so we can use (row,col,0) pseudo-key for // "space before (row,col)" typedef struct { mtpos_t pos; - uint64_t id; + uint32_t ns; + uint32_t id; + int32_t hl_id; + uint16_t flags; + uint16_t priority; + Decoration *decor_full; } mtkey_t; +#define MT_INVALID_KEY (mtkey_t) { { -1, -1 }, 0, 0, 0, 0, 0, NULL } + +#define MT_FLAG_REAL (((uint16_t)1) << 0) +#define MT_FLAG_END (((uint16_t)1) << 1) +#define MT_FLAG_PAIRED (((uint16_t)1) << 2) +#define MT_FLAG_HL_EOL (((uint16_t)1) << 3) + +#define DECOR_LEVELS 4 +#define MT_FLAG_DECOR_OFFSET 4 +#define MT_FLAG_DECOR_MASK (((uint16_t)(DECOR_LEVELS-1)) << MT_FLAG_DECOR_OFFSET) + +// next flag is (((uint16_t)1) << 6) + +// These _must_ be last to preserve ordering of marks +#define MT_FLAG_RIGHT_GRAVITY (((uint16_t)1) << 14) +#define MT_FLAG_LAST (((uint16_t)1) << 15) + +#define MT_FLAG_EXTERNAL_MASK (MT_FLAG_DECOR_MASK | MT_FLAG_RIGHT_GRAVITY | MT_FLAG_HL_EOL) + +#define MARKTREE_END_FLAG (((uint64_t)1) << 63) +static inline uint64_t mt_lookup_id(uint32_t ns, uint32_t id, bool enda) +{ + return (uint64_t)ns << 32 | id | (enda?MARKTREE_END_FLAG:0); +} +#undef MARKTREE_END_FLAG + +static inline uint64_t mt_lookup_key(mtkey_t key) +{ + return mt_lookup_id(key.ns, key.id, key.flags & MT_FLAG_END); +} + +static inline bool mt_paired(mtkey_t key) +{ + return key.flags & MT_FLAG_PAIRED; +} + +static inline bool mt_end(mtkey_t key) +{ + return key.flags & MT_FLAG_END; +} + +static inline bool mt_right(mtkey_t key) +{ + return key.flags & MT_FLAG_RIGHT_GRAVITY; +} + +static inline uint8_t marktree_decor_level(mtkey_t key) +{ + return (uint8_t)((key.flags&MT_FLAG_DECOR_MASK) >> MT_FLAG_DECOR_OFFSET); +} + +static inline uint16_t mt_flags(bool right_gravity, uint8_t decor_level) +{ + assert(decor_level < DECOR_LEVELS); + return (uint16_t)((right_gravity ? MT_FLAG_RIGHT_GRAVITY : 0) + | (decor_level << MT_FLAG_DECOR_OFFSET)); +} + struct mtnode_s { int32_t n; @@ -61,7 +120,6 @@ struct mtnode_s { typedef struct { mtnode_t *root; size_t n_keys, n_nodes; - uint64_t next_id; // TODO(bfredl): the pointer to node could be part of the larger // Map(uint64_t, ExtmarkItem) essentially; PMap(uint64_t) id2node[1]; @@ -72,16 +130,4 @@ typedef struct { # include "marktree.h.generated.h" #endif -#define MARKTREE_PAIRED_FLAG (((uint64_t)1) << 1) -#define MARKTREE_END_FLAG (((uint64_t)1) << 0) - -#define DECOR_LEVELS 4 -#define DECOR_OFFSET 61 -#define DECOR_MASK (((uint64_t)(DECOR_LEVELS-1)) << DECOR_OFFSET) - -static inline uint8_t marktree_decor_level(uint64_t id) -{ - return (uint8_t)((id&DECOR_MASK) >> DECOR_OFFSET); -} - #endif // NVIM_MARKTREE_H diff --git a/src/nvim/mbyte.c b/src/nvim/mbyte.c index 5eb209a6f6..e5fa80a242 100644 --- a/src/nvim/mbyte.c +++ b/src/nvim/mbyte.c @@ -1067,7 +1067,7 @@ bool utf_printable(int c) static struct interval nonprint[] = { { 0x070f, 0x070f }, { 0x180b, 0x180e }, { 0x200b, 0x200f }, { 0x202a, 0x202e }, - { 0x206a, 0x206f }, { 0xd800, 0xdfff }, { 0xfeff, 0xfeff }, { 0xfff9, 0xfffb }, + { 0x2060, 0x206f }, { 0xd800, 0xdfff }, { 0xfeff, 0xfeff }, { 0xfff9, 0xfffb }, { 0xfffe, 0xffff } }; @@ -1821,12 +1821,10 @@ void mb_copy_char(const char_u **const fp, char_u **const tp) *fp += l; } -/* - * Return the offset from "p" to the first byte of a character. When "p" is - * at the start of a character 0 is returned, otherwise the offset to the next - * character. Can start anywhere in a stream of bytes. - */ -int mb_off_next(char_u *base, char_u *p) +/// Return the offset from "p" to the first byte of a character. When "p" is +/// at the start of a character 0 is returned, otherwise the offset to the next +/// character. Can start anywhere in a stream of bytes. +int mb_off_next(const char_u *base, const char_u *p) { int i; int j; @@ -1854,7 +1852,7 @@ int mb_off_next(char_u *base, char_u *p) /// Return the offset from "p" to the last byte of the character it points /// into. Can start anywhere in a stream of bytes. /// Composing characters are not included. -int mb_tail_off(char_u *base, char_u *p) +int mb_tail_off(const char_u *base, const char_u *p) { int i; int j; @@ -1882,12 +1880,13 @@ int mb_tail_off(char_u *base, char_u *p) /// Return the offset from "p" to the first byte of the character it points /// into. Can start anywhere in a stream of bytes. +/// Unlike utf_head_off() this doesn't include composing characters and returns a negative value. /// /// @param[in] base Pointer to start of string /// @param[in] p Pointer to byte for which to return the offset to the previous codepoint // /// @return 0 if invalid sequence, else offset to previous codepoint -int mb_head_off(char_u *base, char_u *p) +int mb_head_off(const char_u *base, const char_u *p) { int i; int j; @@ -2037,13 +2036,11 @@ char_u *mb_prevptr(char_u *line, char_u *p) return p; } -/* - * Return the character length of "str". Each multi-byte character (with - * following composing characters) counts as one. - */ -int mb_charlen(char_u *str) +/// Return the character length of "str". Each multi-byte character (with +/// following composing characters) counts as one. +int mb_charlen(const char_u *str) { - char_u *p = str; + const char_u *p = str; int count; if (p == NULL) { @@ -2057,12 +2054,10 @@ int mb_charlen(char_u *str) return count; } -/* - * Like mb_charlen() but for a string with specified length. - */ -int mb_charlen_len(char_u *str, int len) +/// Like mb_charlen() but for a string with specified length. +int mb_charlen_len(const char_u *str, int len) { - char_u *p = str; + const char_u *p = str; int count; for (count = 0; *p != NUL && p < str + len; count++) { @@ -2089,8 +2084,7 @@ const char *mb_unescape(const char **const pp) size_t buf_idx = 0; uint8_t *str = (uint8_t *)(*pp); - // Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL and CSI - // KS_EXTRA KE_CSI to CSI. + // Must translate K_SPECIAL KS_SPECIAL KE_FILLER to K_SPECIAL. // Maximum length of a utf-8 character is 4 bytes. for (size_t str_idx = 0; str[str_idx] != NUL && buf_idx < 4; str_idx++) { if (str[str_idx] == K_SPECIAL @@ -2098,11 +2092,6 @@ const char *mb_unescape(const char **const pp) && str[str_idx + 2] == KE_FILLER) { buf[buf_idx++] = (char)K_SPECIAL; str_idx += 2; - } else if ((str[str_idx] == K_SPECIAL) - && str[str_idx + 1] == KS_EXTRA - && str[str_idx + 2] == KE_CSI) { - buf[buf_idx++] = (char)CSI; - str_idx += 2; } else if (str[str_idx] == K_SPECIAL) { break; // A special key can't be a multibyte char. } else { @@ -2207,11 +2196,9 @@ char_u *enc_canonize(char_u *enc) FUNC_ATTR_NONNULL_RET return r; } -/* - * Search for an encoding alias of "name". - * Returns -1 when not found. - */ -static int enc_alias_search(char_u *name) +/// Search for an encoding alias of "name". +/// Returns -1 when not found. +static int enc_alias_search(const char_u *name) { int i; diff --git a/src/nvim/memfile.c b/src/nvim/memfile.c index 3397296b3a..2a72d1e6a0 100644 --- a/src/nvim/memfile.c +++ b/src/nvim/memfile.c @@ -247,7 +247,7 @@ bhdr_T *mf_new(memfile_T *mfp, bool negative, unsigned page_count) } else { // need to allocate memory for this block // If the number of pages matches use the bhdr_T from the free list and // allocate the data. - void *p = xmalloc(mfp->mf_page_size * page_count); + void *p = xmalloc((size_t)mfp->mf_page_size * page_count); hp = mf_rem_free(mfp); hp->bh_data = p; } @@ -269,7 +269,7 @@ bhdr_T *mf_new(memfile_T *mfp, bool negative, unsigned page_count) // Init the data to all zero, to avoid reading uninitialized data. // This also avoids that the passwd file ends up in the swap file! - (void)memset(hp->bh_data, 0, mfp->mf_page_size * page_count); + (void)memset(hp->bh_data, 0, (size_t)mfp->mf_page_size * page_count); return hp; } @@ -528,7 +528,7 @@ bool mf_release_all(void) static bhdr_T *mf_alloc_bhdr(memfile_T *mfp, unsigned page_count) { bhdr_T *hp = xmalloc(sizeof(bhdr_T)); - hp->bh_data = xmalloc(mfp->mf_page_size * page_count); + hp->bh_data = xmalloc((size_t)mfp->mf_page_size * page_count); hp->bh_page_count = page_count; return hp; } diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 08521c0dc3..9925971783 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -1032,9 +1032,9 @@ void ml_recover(bool checkext) line_count = 0; idx = 0; // start with first index in block 1 error = 0; - buf->b_ml.ml_stack_top = 0; + buf->b_ml.ml_stack_top = 0; // -V1048 buf->b_ml.ml_stack = NULL; - buf->b_ml.ml_stack_size = 0; // no stack yet + buf->b_ml.ml_stack_size = 0; // -V1048 if (curbuf->b_ffname == NULL) { cannot_open = true; @@ -4139,7 +4139,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff) || (offset != 0 && offset > size + buf->b_ml.ml_chunksize[curix].mlcs_totalsize - + ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines))) { + + (long)ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines))) { curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; size += buf->b_ml.ml_chunksize[curix].mlcs_totalsize; if (offset && ffdos) { diff --git a/src/nvim/message.c b/src/nvim/message.c index 39b023132e..93742ccbdb 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -262,7 +262,6 @@ void msg_multiline_attr(const char *s, int attr, bool check_int, bool *need_clea if (*s != NUL) { msg_outtrans_attr((char_u *)s, attr); } - return; } @@ -329,7 +328,7 @@ bool msg_attr_keep(const char *s, int attr, bool keep, bool multiline) } retval = msg_end(); - if (keep && retval && vim_strsize((char_u *)s) < (int)(Rows - cmdline_row - 1) + if (keep && retval && vim_strsize((char_u *)s) < (Rows - cmdline_row - 1) * Columns + sc_col) { set_keep_msg((char *)s, 0); } @@ -356,10 +355,10 @@ char_u *msg_strtrunc(char_u *s, int force) len = vim_strsize(s); if (msg_scrolled != 0) { // Use all the columns. - room = (int)(Rows - msg_row) * Columns - 1; + room = (Rows - msg_row) * Columns - 1; } else { // Use up to 'showcmd' column. - room = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; + room = (Rows - msg_row - 1) * Columns + sc_col - 1; } if (len > room && room > 0) { // may have up to 18 bytes per cell (6 per char, up to two @@ -873,7 +872,7 @@ char_u *msg_may_trunc(bool force, char_u *s) { int room; - room = (int)(Rows - cmdline_row - 1) * Columns + sc_col - 1; + room = (Rows - cmdline_row - 1) * Columns + sc_col - 1; if ((force || (shortmess(SHM_TRUNC) && !exmode_active)) && (int)STRLEN(s) - room > 0) { int size = vim_strsize(s); @@ -1095,6 +1094,10 @@ void wait_return(int redraw) return; } + if (headless_mode && !ui_active()) { + return; + } + /* * When inside vgetc(), we can't wait for a typed character at all. * With the global command (and some others) we only need one return at @@ -1215,7 +1218,7 @@ void wait_return(int redraw) } else if (vim_strchr((char_u *)"\r\n ", c) == NULL && c != Ctrl_C) { // Put the character back in the typeahead buffer. Don't use the // stuff buffer, because lmaps wouldn't work. - ins_char_typebuf(c); + ins_char_typebuf(c, mod_mask); do_redraw = true; // need a redraw even though there is // typeahead } @@ -2647,6 +2650,17 @@ static void msg_puts_printf(const char *str, const ptrdiff_t maxlen) char buf[7]; char *p; + if (on_print.type != kCallbackNone) { + typval_T argv[1]; + argv[0].v_type = VAR_STRING; + argv[0].v_lock = VAR_UNLOCKED; + argv[0].vval.v_string = (char_u *)str; + typval_T rettv = TV_INITIAL_VALUE; + callback_call(&on_print, 1, argv, &rettv); + tv_clear(&rettv); + return; + } + while ((maxlen < 0 || s - str < maxlen) && *s != NUL) { int len = utf_ptr2len((const char_u *)s); if (!(silent_mode && p_verbose == 0)) { @@ -3497,7 +3511,7 @@ int do_dialog(int type, char_u *title, char_u *message, char_u *buttons, int dfl } if (c == ':' && ex_cmd) { retval = dfltbutton; - ins_char_typebuf(':'); + ins_char_typebuf(':', 0); break; } diff --git a/src/nvim/move.c b/src/nvim/move.c index 67ec19903f..27cc2b341c 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -346,10 +346,10 @@ void update_topline(win_T *wp) */ void update_topline_win(win_T *win) { - win_T *save_curwin; - switch_win(&save_curwin, NULL, win, NULL, true); + switchwin_T switchwin; + switch_win(&switchwin, win, NULL, true); update_topline(curwin); - restore_win(save_curwin, NULL, true); + restore_win(&switchwin, true); } /* diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 60bf393085..21c465434a 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -164,7 +164,7 @@ static const struct nv_cmd { { Ctrl_O, nv_ctrlo, 0, 0 }, { Ctrl_P, nv_up, NV_STS, false }, { Ctrl_Q, nv_visual, 0, false }, - { Ctrl_R, nv_redo, 0, 0 }, + { Ctrl_R, nv_redo_or_register, 0, 0 }, { Ctrl_S, nv_ignore, 0, 0 }, { Ctrl_T, nv_tagpop, NV_NCW, 0 }, { Ctrl_U, nv_halfpage, 0, 0 }, @@ -961,6 +961,7 @@ normal_end: && s->oa.regname == 0) { if (restart_VIsual_select == 1) { VIsual_select = true; + VIsual_select_reg = 0; trigger_modechanged(); showmode(); restart_VIsual_select = 0; @@ -1010,7 +1011,14 @@ static int normal_execute(VimState *state, int key) // restart automatically. // Insert the typed character in the typeahead buffer, so that it can // be mapped in Insert mode. Required for ":lmap" to work. - ins_char_typebuf(s->c); + int len = ins_char_typebuf(s->c, mod_mask); + + // When recording and gotchars() was called the character will be + // recorded again, remove the previous recording. + if (KeyTyped) { + ungetchars(len); + } + if (restart_edit != 0) { s->c = 'd'; } else { @@ -1022,7 +1030,7 @@ static int normal_execute(VimState *state, int key) s->need_flushbuf = add_to_showcmd(s->c); - while (normal_get_command_count(s)) { continue; } + while (normal_get_command_count(s)) { } if (s->c == K_EVENT) { // Save the count values so that ca.opcount and ca.count0 are exactly @@ -3090,8 +3098,14 @@ static void nv_gd(oparg_T *oap, int nchar, int thisblock) if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0 || !find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)) { clearopbeep(oap); - } else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP) { - foldOpenCursor(); + } else { + if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP) { + foldOpenCursor(); + } + // clear any search statistics + if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT)) { + clear_cmdline = true; + } } } @@ -3281,7 +3295,7 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) int col_off1; // margin offset for first screen line int col_off2; // margin offset for wrapped screen line int width1; // text width for first screen line - int width2; // test width for wrapped screen line + int width2; // text width for wrapped screen line oap->motion_type = kMTCharWise; oap->inclusive = (curwin->w_curswant == MAXCOL); @@ -3405,6 +3419,13 @@ static bool nv_screengo(oparg_T *oap, int dir, long dist) virtcol -= vim_strsize(get_showbreak_value(curwin)); } + int c = utf_ptr2char(get_cursor_pos_ptr()); + if (dir == FORWARD && virtcol < curwin->w_curswant + && (curwin->w_curswant <= (colnr_T)width1) + && !vim_isprintc(c) && c > 255) { + oneright(); + } + if (virtcol > curwin->w_curswant && (curwin->w_curswant < (colnr_T)width1 ? (curwin->w_curswant > (colnr_T)width1 / 2) @@ -4078,7 +4099,7 @@ static void nv_colon(cmdarg_T *cap) if (is_lua) { cmd_result = map_execute_lua(); } else { - // get a command line and execute it + // get a command line and execute it cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL, cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0); } @@ -4437,11 +4458,7 @@ static void nv_ident(cmdarg_T *cap) // Start insert mode in terminal buffer restart_edit = 'i'; - add_map((char_u *)"<buffer> <esc> <Cmd>call jobstop(&channel)<CR>", TERM_FOCUS, true); - do_cmdline_cmd("autocmd TermClose <buffer> " - " if !v:event.status |" - " exec 'bdelete! ' .. expand('<abuf>') |" - " endif"); + add_map((char_u *)"<buffer> <esc> <Cmd>bdelete!<CR>", TERM_FOCUS, true); } } @@ -4476,8 +4493,13 @@ bool get_visual_text(cmdarg_T *cap, char_u **pp, size_t *lenp) *pp = ml_get_pos(&VIsual); *lenp = (size_t)curwin->w_cursor.col - (size_t)VIsual.col + 1; } - // Correct the length to include the whole last character. - *lenp += (size_t)(utfc_ptr2len(*pp + (*lenp - 1)) - 1); + if (**pp == NUL) { + *lenp = 0; + } + if (*lenp > 0) { + // Correct the length to include all bytes of the last character. + *lenp += (size_t)(utfc_ptr2len(*pp + (*lenp - 1)) - 1); + } } reset_VIsual_and_resel(); return true; @@ -5011,9 +5033,7 @@ static void nv_brackets(cmdarg_T *cap) * identifier "]i" "[i" "]I" "[I" "]^I" "[^I" * define "]d" "[d" "]D" "[D" "]^D" "[^D" */ - if (vim_strchr((char_u *) - "iI\011dD\004", - cap->nchar) != NULL) { + if (vim_strchr((char_u *)"iI\011dD\004", cap->nchar) != NULL) { char_u *ptr; size_t len; @@ -5967,11 +5987,8 @@ static void nv_visual(cmdarg_T *cap) * was only one -- webb */ if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1) { - curwin->w_cursor.lnum += - resel_VIsual_line_count * cap->count0 - 1; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { - curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - } + curwin->w_cursor.lnum += resel_VIsual_line_count * cap->count0 - 1; + check_cursor(); } VIsual_mode = resel_VIsual_mode; if (VIsual_mode == 'v') { @@ -6050,7 +6067,7 @@ static void n_start_visual_mode(int c) // Corner case: the 0 position in a tab may change when going into // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting. // - if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB) { + if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB) { validate_virtcol(); coladvance(curwin->w_virtcol); } @@ -6190,6 +6207,7 @@ static void nv_g_cmd(cmdarg_T *cap) // start Select mode. if (cap->arg) { VIsual_select = true; + VIsual_select_reg = 0; } else { may_start_select('c'); } @@ -6316,20 +6334,17 @@ static void nv_g_cmd(cmdarg_T *cap) curwin->w_set_curswant = true; break; - case 'M': { - const char_u *const ptr = get_cursor_line_ptr(); - + case 'M': oap->motion_type = kMTCharWise; oap->inclusive = false; - i = (int)mb_string2cells_len(ptr, STRLEN(ptr)); + i = linetabsize(get_cursor_line_ptr()); if (cap->count0 > 0 && cap->count0 <= 100) { coladvance((colnr_T)(i * cap->count0 / 100)); } else { coladvance((colnr_T)(i / 2)); } curwin->w_set_curswant = true; - } - break; + break; case '_': /* "g_": to the last non-blank character in the line or <count> lines @@ -6673,9 +6688,8 @@ static void n_opencmd(cmdarg_T *cap) (cap->cmdchar == 'o' ? 1 : 0)) ) && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD, - has_format_option(FO_OPEN_COMS) - ? OPENLINE_DO_COM : 0, - 0)) { + has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0, + 0, NULL)) { if (win_cursorline_standout(curwin)) { // force redraw of cursorline curwin->w_valid &= ~VALID_CROW; @@ -6702,11 +6716,26 @@ static void nv_dot(cmdarg_T *cap) } } -/* - * CTRL-R: undo undo - */ -static void nv_redo(cmdarg_T *cap) +// CTRL-R: undo undo or specify register in select mode +static void nv_redo_or_register(cmdarg_T *cap) { + if (VIsual_select && VIsual_active) { + int reg; + // Get register name + no_mapping++; + reg = plain_vgetc(); + LANGMAP_ADJUST(reg, true); + no_mapping--; + + if (reg == '"') { + // the unnamed register is 0 + reg = 0; + } + + VIsual_select_reg = valid_yank_reg(reg, true) ? reg : 0; + return; + } + if (!checkclearopq(cap->oap)) { u_redo((int)cap->count1); curwin->w_set_curswant = true; @@ -6955,7 +6984,7 @@ static void adjust_cursor(oparg_T *oap) if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL && (!VIsual_active || *p_sel == 'o') && !virtual_active() - && (ve_flags & VE_ONEMORE) == 0) { + && (get_ve_flags() & VE_ONEMORE) == 0) { curwin->w_cursor.col--; // prevent cursor from moving on the trail byte mb_adjust_cursor(); @@ -7027,6 +7056,7 @@ static void nv_select(cmdarg_T *cap) { if (VIsual_active) { VIsual_select = true; + VIsual_select_reg = 0; } else if (VIsual_reselect) { cap->nchar = 'v'; // fake "gv" command cap->arg = true; @@ -7161,7 +7191,7 @@ static void nv_esc(cmdarg_T *cap) void set_cursor_for_append_to_line(void) { curwin->w_set_curswant = true; - if (ve_flags == VE_ALL) { + if (get_ve_flags() == VE_ALL) { const int save_State = State; // Pretend Insert mode here to allow the cursor on the @@ -7479,9 +7509,9 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) // overwrites if the old contents is being put. was_visual = true; regname = cap->oap->regname; + bool save_unnamed = cap->cmdchar == 'P'; // '+' and '*' could be the same selection - bool clipoverwrite = (regname == '+' || regname == '*') - && (cb_flags & CB_UNNAMEDMASK); + bool clipoverwrite = (regname == '+' || regname == '*') && (cb_flags & CB_UNNAMEDMASK); if (regname == 0 || regname == '"' || clipoverwrite || ascii_isdigit(regname) || regname == '-') { // The delete might overwrite the register we want to put, save it first @@ -7494,6 +7524,10 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) // do_put(), which requires the visual selection to still be active. if (!VIsual_active || VIsual_mode == 'V' || regname != '.') { // Now delete the selected text. Avoid messages here. + yankreg_T *old_y_previous; + if (save_unnamed) { + old_y_previous = get_y_previous(); + } cap->cmdchar = 'd'; cap->nchar = NUL; cap->oap->regname = NUL; @@ -7503,6 +7537,10 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent) empty = (curbuf->b_ml.ml_flags & ML_EMPTY); msg_silent--; + if (save_unnamed) { + set_y_previous(old_y_previous); + } + // delete PUT_LINE_BACKWARD; cap->oap->regname = regname; } diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 6c2db1e9ac..f999b68236 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -135,10 +135,18 @@ static char opchars[][3] = { Ctrl_X, NUL, OPF_CHANGE }, // OP_NR_SUB }; -/* - * Translate a command name into an operator type. - * Must only be called with a valid operator name! - */ +yankreg_T *get_y_previous(void) +{ + return y_previous; +} + +void set_y_previous(yankreg_T *yreg) +{ + y_previous = yreg; +} + +/// Translate a command name into an operator type. +/// Must only be called with a valid operator name! int get_op_type(int char1, int char2) { int i; @@ -267,14 +275,14 @@ void op_shift(oparg_T *oap, int curs_top, int amount) msg_attr_keep((char *)IObuff, 0, true, false); } - /* - * Set "'[" and "']" marks. - */ - curbuf->b_op_start = oap->start; - curbuf->b_op_end.lnum = oap->end.lnum; - curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); - if (curbuf->b_op_end.col > 0) { - curbuf->b_op_end.col--; + if (!cmdmod.lockmarks) { + // Set "'[" and "']" marks. + curbuf->b_op_start = oap->start; + curbuf->b_op_end.lnum = oap->end.lnum; + curbuf->b_op_end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum)); + if (curbuf->b_op_end.col > 0) { + curbuf->b_op_end.col--; + } } changed_lines(oap->start.lnum, 0, oap->end.lnum + 1, 0L, true); @@ -381,8 +389,8 @@ static void shift_block(oparg_T *oap, int amount) } } for (; ascii_iswhite(*bd.textstart);) { - // TODO: is passing bd.textstart for start of the line OK? - incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, (bd.start_vcol)); + // TODO(fmoralesc): is passing bd.textstart for start of the line OK? + incr = lbr_chartabsize_adv(bd.textstart, &bd.textstart, bd.start_vcol); total += incr; bd.start_vcol += incr; } @@ -566,7 +574,7 @@ static void block_insert(oparg_T *oap, char_u *s, int b_insert, struct block_def if (b_insert) { off = utf_head_off(oldp, oldp + offset + spaces); } else { - off = (*mb_off_next)(oldp, oldp + offset); + off = mb_off_next(oldp, oldp + offset); offset += off; } spaces -= off; @@ -694,9 +702,11 @@ void op_reindent(oparg_T *oap, Indenter how) "%" PRId64 " lines indented ", i), (int64_t)i); } - // set '[ and '] marks - curbuf->b_op_start = oap->start; - curbuf->b_op_end = oap->end; + if (!cmdmod.lockmarks) { + // set '[ and '] marks + curbuf->b_op_start = oap->start; + curbuf->b_op_end = oap->end; + } } /* @@ -915,10 +925,29 @@ int do_record(int c) apply_autocmds(EVENT_RECORDINGENTER, NULL, NULL, false, curbuf); } } else { // stop recording - // Get the recorded key hits. K_SPECIAL and CSI will be escaped, this + save_v_event_T save_v_event; + // Set the v:event dictionary with information about the recording. + dict_T *dict = get_v_event(&save_v_event); + + // The recorded text contents. + p = get_recorded(); + if (p != NULL) { + // Remove escaping for K_SPECIAL in multi-byte chars. + vim_unescape_ks(p); + (void)tv_dict_add_str(dict, S_LEN("regcontents"), (const char *)p); + } + + // Name of requested register, or empty string for unnamed operation. + char buf[NUMBUFLEN+2]; + buf[0] = (char)regname; + buf[1] = NUL; + (void)tv_dict_add_str(dict, S_LEN("regname"), buf); + + // Get the recorded key hits. K_SPECIAL will be escaped, this // needs to be removed again to put it in a register. exec_reg then // adds the escaping back later. apply_autocmds(EVENT_RECORDINGLEAVE, NULL, NULL, false, curbuf); + restore_v_event(dict, &save_v_event); reg_recorded = reg_recording; reg_recording = 0; if (ui_has(kUIMessages)) { @@ -926,13 +955,9 @@ int do_record(int c) } else { msg(""); } - p = get_recorded(); if (p == NULL) { retval = FAIL; } else { - // Remove escaping for CSI and K_SPECIAL in multi-byte chars. - vim_unescape_csi(p); - // We don't want to change the default register here, so save and // restore the current register name. old_y_previous = y_previous; @@ -1084,7 +1109,7 @@ int do_execreg(int regname, int colon, int addcr, int silent) return FAIL; } } - escaped = vim_strsave_escape_csi(reg->y_array[i]); + escaped = vim_strsave_escape_ks(reg->y_array[i]); retval = ins_typebuf(escaped, remap, 0, true, silent); xfree(escaped); if (retval == FAIL) { @@ -1126,7 +1151,7 @@ static void put_reedit_in_typebuf(int silent) /// Insert register contents "s" into the typeahead buffer, so that it will be /// executed again. /// -/// @param esc when true then it is to be taken literally: Escape CSI +/// @param esc when true then it is to be taken literally: Escape K_SPECIAL /// characters and no remapping. /// @param colon add ':' before the line static int put_in_typebuf(char_u *s, bool esc, bool colon, int silent) @@ -1141,7 +1166,7 @@ static int put_in_typebuf(char_u *s, bool esc, bool colon, int silent) char_u *p; if (esc) { - p = vim_strsave_escape_csi(s); + p = vim_strsave_escape_ks(s); } else { p = s; } @@ -1420,6 +1445,11 @@ int op_delete(oparg_T *oap) return FAIL; } + if (VIsual_select && oap->is_VIsual) { + // Use the register given with CTRL_R, defaults to zero + oap->regname = VIsual_select_reg; + } + mb_adjust_opend(oap); /* @@ -1716,13 +1746,15 @@ int op_delete(oparg_T *oap) msgmore(curbuf->b_ml.ml_line_count - old_lcount); setmarks: - if (oap->motion_type == kMTBlockWise) { - curbuf->b_op_end.lnum = oap->end.lnum; - curbuf->b_op_end.col = oap->start.col; - } else { - curbuf->b_op_end = oap->start; + if (!cmdmod.lockmarks) { + if (oap->motion_type == kMTBlockWise) { + curbuf->b_op_end.lnum = oap->end.lnum; + curbuf->b_op_end.col = oap->start.col; + } else { + curbuf->b_op_end = oap->start; + } + curbuf->b_op_start = oap->start; } - curbuf->b_op_start = oap->start; return OK; } @@ -1987,9 +2019,11 @@ static int op_replace(oparg_T *oap, int c) check_cursor(); changed_lines(oap->start.lnum, oap->start.col, oap->end.lnum + 1, 0L, true); - // Set "'[" and "']" marks. - curbuf->b_op_start = oap->start; - curbuf->b_op_end = oap->end; + if (!cmdmod.lockmarks) { + // Set "'[" and "']" marks. + curbuf->b_op_start = oap->start; + curbuf->b_op_end = oap->end; + } return OK; } @@ -2058,11 +2092,11 @@ void op_tilde(oparg_T *oap) redraw_curbuf_later(INVERTED); } - /* - * Set '[ and '] marks. - */ - curbuf->b_op_start = oap->start; - curbuf->b_op_end = oap->end; + if (!cmdmod.lockmarks) { + // Set '[ and '] marks. + curbuf->b_op_start = oap->start; + curbuf->b_op_end = oap->end; + } if (oap->line_count > p_report) { smsg(NGETTEXT("%" PRId64 " line changed", @@ -2181,19 +2215,22 @@ void op_insert(oparg_T *oap, long count1) // doing block_prep(). When only "block" is used, virtual edit is // already disabled, but still need it when calling // coladvance_force(). + // coladvance_force() uses get_ve_flags() to get the 'virtualedit' + // state for the current window. To override that state, we need to + // set the window-local value of ve_flags rather than the global value. if (curwin->w_cursor.coladd > 0) { - unsigned old_ve_flags = ve_flags; + unsigned old_ve_flags = curwin->w_ve_flags; - ve_flags = VE_ALL; if (u_save_cursor() == FAIL) { return; } + curwin->w_ve_flags = VE_ALL; coladvance_force(oap->op_type == OP_APPEND ? oap->end_vcol + 1 : getviscol()); if (oap->op_type == OP_APPEND) { --curwin->w_cursor.col; } - ve_flags = old_ve_flags; + curwin->w_ve_flags = old_ve_flags; } // Get the info about the block before entering the text block_prep(oap, &bd, oap->start.lnum, true); @@ -2751,17 +2788,15 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) } } - /* - * Set "'[" and "']" marks. - */ - curbuf->b_op_start = oap->start; - curbuf->b_op_end = oap->end; - if (yank_type == kMTLineWise) { - curbuf->b_op_start.col = 0; - curbuf->b_op_end.col = MAXCOL; + if (!cmdmod.lockmarks) { + // Set "'[" and "']" marks. + curbuf->b_op_start = oap->start; + curbuf->b_op_end = oap->end; + if (yank_type == kMTLineWise) { + curbuf->b_op_start.col = 0; + curbuf->b_op_end.col = MAXCOL; + } } - - return; } // Copy a block range into a register. @@ -2786,7 +2821,7 @@ static void yank_copy_line(yankreg_T *reg, struct block_def *bd, size_t y_idx, if (exclude_trailing_space) { int s = bd->textlen + bd->endspaces; - while (ascii_iswhite(*(bd->textstart + s - 1)) && s > 0) { + while (s > 0 && ascii_iswhite(*(bd->textstart + s - 1))) { s = s - utf_head_off(bd->textstart, bd->textstart + s - 1) - 1; pnew--; } @@ -2891,6 +2926,9 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) char_u *insert_string = NULL; bool allocated = false; long cnt; + const pos_T orig_start = curbuf->b_op_start; + const pos_T orig_end = curbuf->b_op_end; + unsigned int cur_ve_flags = get_ve_flags(); if (flags & PUT_FIXINDENT) { orig_indent = get_indent(); @@ -2961,7 +2999,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) eol = (*(cursor_pos + utfc_ptr2len(cursor_pos)) == NUL); } - bool ve_allows = (ve_flags == VE_ALL || ve_flags == VE_ONEMORE); + bool ve_allows = (cur_ve_flags == VE_ALL || cur_ve_flags == VE_ONEMORE); bool eof = curbuf->b_ml.ml_line_count == curwin->w_cursor.lnum && one_past_line; if (ve_allows || !(eol || eof)) { @@ -3137,13 +3175,14 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) yanklen = (int)STRLEN(y_array[0]); - if (ve_flags == VE_ALL && y_type == kMTCharWise) { + if (cur_ve_flags == VE_ALL && y_type == kMTCharWise) { if (gchar_cursor() == TAB) { - /* Don't need to insert spaces when "p" on the last position of a - * tab or "P" on the first position. */ int viscol = getviscol(); + long ts = curbuf->b_p_ts; + // Don't need to insert spaces when "p" on the last position of a + // tab or "P" on the first position. if (dir == FORWARD - ? tabstop_padding(viscol, curbuf->b_p_ts, curbuf->b_p_vts_array) != 1 + ? tabstop_padding(viscol, ts, curbuf->b_p_vts_array) != 1 : curwin->w_cursor.coladd > 0) { coladvance_force(viscol); } else { @@ -3165,7 +3204,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) colnr_T endcol2 = 0; if (dir == FORWARD && c != NUL) { - if (ve_flags == VE_ALL) { + if (cur_ve_flags == VE_ALL) { getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); } else { getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); @@ -3179,9 +3218,8 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) } col += curwin->w_cursor.coladd; - if (ve_flags == VE_ALL - && (curwin->w_cursor.coladd > 0 - || endcol2 == curwin->w_cursor.col)) { + if (cur_ve_flags == VE_ALL + && (curwin->w_cursor.coladd > 0 || endcol2 == curwin->w_cursor.col)) { if (dir == FORWARD && c == NUL) { col++; } @@ -3440,6 +3478,9 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) curwin->w_cursor.col -= first_byte_off; } } else { + linenr_T new_lnum = new_cursor.lnum; + size_t len; + // Insert at least one line. When y_type is kMTCharWise, break the first // line in two. for (cnt = 1; cnt <= count; cnt++) { @@ -3456,6 +3497,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) STRCAT(newp, ptr); // insert second line ml_append(lnum, newp, (colnr_T)0, false); + new_lnum++; xfree(newp); oldp = ml_get(lnum); @@ -3471,10 +3513,11 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) } for (; i < y_size; i++) { - if ((y_type != kMTCharWise || i < y_size - 1) - && ml_append(lnum, y_array[i], (colnr_T)0, false) - == FAIL) { - goto error; + if ((y_type != kMTCharWise || i < y_size - 1)) { + if (ml_append(lnum, y_array[i], (colnr_T)0, false) == FAIL) { + goto error; + } + new_lnum++; } lnum++; ++nr_lines; @@ -3524,6 +3567,10 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) extmark_splice(curbuf, (int)new_cursor.lnum-1, col + 1, 0, 0, 0, (int)y_size+1, 0, totsize+2, kExtmarkUndo); } + + if (cnt == 1) { + new_lnum = lnum; + } } error: @@ -3551,11 +3598,12 @@ error: // Put the '] mark on the first byte of the last inserted character. // Correct the length for change in indent. - curbuf->b_op_end.lnum = lnum; - col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff; + curbuf->b_op_end.lnum = new_lnum; + len = STRLEN(y_array[y_size - 1]); + col = (colnr_T)len - lendiff; if (col > 1) { curbuf->b_op_end.col = col - 1 - utf_head_off(y_array[y_size - 1], - y_array[y_size - 1] + col - 1); + y_array[y_size - 1] + len - 1); } else { curbuf->b_op_end.col = 0; } @@ -3574,8 +3622,12 @@ error: } curwin->w_cursor.col = 0; } else { - curwin->w_cursor.lnum = lnum; + curwin->w_cursor.lnum = new_lnum; curwin->w_cursor.col = col; + curbuf->b_op_end = curwin->w_cursor; + if (col > 1) { + curbuf->b_op_end.col = col - 1; + } } } else if (y_type == kMTLineWise) { // put cursor on first non-blank in first inserted line @@ -3594,6 +3646,10 @@ error: curwin->w_set_curswant = TRUE; end: + if (cmdmod.lockmarks) { + curbuf->b_op_start = orig_start; + curbuf->b_op_end = orig_end; + } if (allocated) { xfree(insert_string); } @@ -3613,14 +3669,16 @@ end: */ void adjust_cursor_eol(void) { + unsigned int cur_ve_flags = get_ve_flags(); + if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL - && (ve_flags & VE_ONEMORE) == 0 + && (cur_ve_flags & VE_ONEMORE) == 0 && !(restart_edit || (State & INSERT))) { // Put the cursor on the last character in the line. dec_cursor(); - if (ve_flags == VE_ALL) { + if (cur_ve_flags == VE_ALL) { colnr_T scol, ecol; // Coladd is set to the width of the last character. @@ -3674,7 +3732,7 @@ void ex_display(exarg_T *eap) int name; char_u *arg = eap->arg; int clen; - char_u type[2]; + int type; if (arg != NULL && *arg == NUL) { arg = NULL; @@ -3687,11 +3745,11 @@ void ex_display(exarg_T *eap) name = get_register_name(i); switch (get_reg_type(name, NULL)) { case kMTLineWise: - type[0] = 'l'; break; + type = 'l'; break; case kMTCharWise: - type[0] = 'c'; break; + type = 'c'; break; default: - type[0] = 'b'; break; + type = 'b'; break; } if (arg != NULL && vim_strchr(arg, name) == NULL) { @@ -3718,88 +3776,87 @@ void ex_display(exarg_T *eap) } if (yb->y_array != NULL) { - msg_putchar('\n'); - msg_puts(" "); - msg_putchar(type[0]); - msg_puts(" "); - msg_putchar('"'); - msg_putchar(name); - msg_puts(" "); - - int n = Columns - 11; - for (size_t j = 0; j < yb->y_size && n > 1; j++) { - if (j) { - msg_puts_attr("^J", attr); - n -= 2; + bool do_show = false; + + for (size_t j = 0; !do_show && j < yb->y_size; j++) { + do_show = !message_filtered(yb->y_array[j]); + } + + if (do_show || yb->y_size == 0) { + msg_putchar('\n'); + msg_puts(" "); + msg_putchar(type); + msg_puts(" "); + msg_putchar('"'); + msg_putchar(name); + msg_puts(" "); + + int n = Columns - 11; + for (size_t j = 0; j < yb->y_size && n > 1; j++) { + if (j) { + msg_puts_attr("^J", attr); + n -= 2; + } + for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; p++) { + clen = utfc_ptr2len(p); + msg_outtrans_len(p, clen); + p += clen - 1; + } } - for (p = yb->y_array[j]; *p && (n -= ptr2cells(p)) >= 0; p++) { // -V1019 NOLINT(whitespace/line_length) - clen = utfc_ptr2len(p); - msg_outtrans_len(p, clen); - p += clen - 1; + if (n > 1 && yb->y_type == kMTLineWise) { + msg_puts_attr("^J", attr); } + ui_flush(); // show one line at a time } - if (n > 1 && yb->y_type == kMTLineWise) { - msg_puts_attr("^J", attr); - } - ui_flush(); // show one line at a time + os_breakcheck(); } - os_breakcheck(); } - /* - * display last inserted text - */ + // display last inserted text if ((p = get_last_insert()) != NULL - && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int) { + && (arg == NULL || vim_strchr(arg, '.') != NULL) && !got_int + && !message_filtered(p)) { msg_puts("\n c \". "); dis_msg(p, true); } - /* - * display last command line - */ + // display last command line if (last_cmdline != NULL && (arg == NULL || vim_strchr(arg, ':') != NULL) - && !got_int) { + && !got_int && !message_filtered(last_cmdline)) { msg_puts("\n c \": "); dis_msg(last_cmdline, false); } - /* - * display current file name - */ + // display current file name if (curbuf->b_fname != NULL - && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) { + && (arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int + && !message_filtered(curbuf->b_fname)) { msg_puts("\n c \"% "); dis_msg(curbuf->b_fname, false); } - /* - * display alternate file name - */ + // display alternate file name if ((arg == NULL || vim_strchr(arg, '%') != NULL) && !got_int) { char_u *fname; linenr_T dummy; - if (buflist_name_nr(0, &fname, &dummy) != FAIL) { + if (buflist_name_nr(0, &fname, &dummy) != FAIL && !message_filtered(fname)) { msg_puts("\n c \"# "); dis_msg(fname, false); } } - /* - * display last search pattern - */ + // display last search pattern if (last_search_pat() != NULL - && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int) { + && (arg == NULL || vim_strchr(arg, '/') != NULL) && !got_int + && !message_filtered(last_search_pat())) { msg_puts("\n c \"/ "); dis_msg(last_search_pat(), false); } - /* - * display last used expression - */ + // display last used expression if (expr_line != NULL && (arg == NULL || vim_strchr(arg, '=') != NULL) - && !got_int) { + && !got_int && !message_filtered(expr_line)) { msg_puts("\n c \"= "); dis_msg(expr_line, false); } @@ -3938,7 +3995,7 @@ int do_join(size_t count, int insert_space, int save_undo, int use_formatoptions // and setup the array of space strings lengths for (t = 0; t < (linenr_T)count; t++) { curr = curr_start = ml_get((linenr_T)(curwin->w_cursor.lnum + t)); - if (t == 0 && setmark) { + if (t == 0 && setmark && !cmdmod.lockmarks) { // Set the '[ mark. curwin->w_buffer->b_op_start.lnum = curwin->w_cursor.lnum; curwin->w_buffer->b_op_start.col = (colnr_T)STRLEN(curr); @@ -4059,7 +4116,7 @@ int do_join(size_t count, int insert_space, int save_undo, int use_formatoptions ml_replace(curwin->w_cursor.lnum, newp, false); - if (setmark) { + if (setmark && !cmdmod.lockmarks) { // Set the '] mark. curwin->w_buffer->b_op_end.lnum = curwin->w_cursor.lnum; curwin->w_buffer->b_op_end.col = sumsize; @@ -4197,8 +4254,10 @@ static void op_format(oparg_T *oap, int keep_cursor) redraw_curbuf_later(INVERTED); } - // Set '[ mark at the start of the formatted area - curbuf->b_op_start = oap->start; + if (!cmdmod.lockmarks) { + // Set '[ mark at the start of the formatted area + curbuf->b_op_start = oap->start; + } // For "gw" remember the cursor position and put it back below (adjusted // for joined and split lines). @@ -4220,8 +4279,10 @@ static void op_format(oparg_T *oap, int keep_cursor) old_line_count = curbuf->b_ml.ml_line_count - old_line_count; msgmore(old_line_count); - // put '] mark on the end of the formatted area - curbuf->b_op_end = curwin->w_cursor; + if (!cmdmod.lockmarks) { + // put '] mark on the end of the formatted area + curbuf->b_op_end = curwin->w_cursor; + } if (keep_cursor) { curwin->w_cursor = saved_cursor; @@ -4308,7 +4369,7 @@ void format_lines(linenr_T line_count, int avoid_fex) int leader_len = 0; // leader len of current line int next_leader_len; // leader len of next line char_u *leader_flags = NULL; // flags for leader of current line - char_u *next_leader_flags; // flags for leader of next line + char_u *next_leader_flags = NULL; // flags for leader of next line bool advance = true; int second_indent = -1; // indent for second line (comment aware) bool first_par_line = true; @@ -4425,7 +4486,14 @@ void format_lines(linenr_T line_count, int avoid_fex) leader_len, leader_flags, next_leader_len, next_leader_flags)) { - is_end_par = true; + // Special case: If the next line starts with a line comment + // and this line has a line comment after some text, the + // paragraph doesn't really end. + if (next_leader_flags == NULL + || STRNCMP(next_leader_flags, "://", 3) != 0 + || check_linecomment(get_cursor_line_ptr()) == MAXCOL) { + is_end_par = true; + } } /* @@ -4819,7 +4887,7 @@ void op_addsub(oparg_T *oap, linenr_T Prenum1, bool g_cmd) // Set '[ mark if something changed. Keep the last end // position from do_addsub(). - if (change_cnt > 0) { + if (change_cnt > 0 && !cmdmod.lockmarks) { curbuf->b_op_start = startpos; } @@ -5173,11 +5241,13 @@ int do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) } } - // set the '[ and '] marks - curbuf->b_op_start = startpos; - curbuf->b_op_end = endpos; - if (curbuf->b_op_end.col > 0) { - curbuf->b_op_end.col--; + if (!cmdmod.lockmarks) { + // set the '[ and '] marks + curbuf->b_op_start = startpos; + curbuf->b_op_end = endpos; + if (curbuf->b_op_end.col > 0) { + curbuf->b_op_end.col--; + } } theend: @@ -5990,6 +6060,8 @@ static void op_function(const oparg_T *oap) { const TriState save_virtual_op = virtual_op; const bool save_finish_op = finish_op; + const pos_T orig_start = curbuf->b_op_start; + const pos_T orig_end = curbuf->b_op_end; if (*p_opfunc == NUL) { emsg(_("E774: 'operatorfunc' is empty")); @@ -6023,6 +6095,10 @@ static void op_function(const oparg_T *oap) virtual_op = save_virtual_op; finish_op = save_finish_op; + if (cmdmod.lockmarks) { + curbuf->b_op_start = orig_start; + curbuf->b_op_end = orig_end; + } } } diff --git a/src/nvim/option.c b/src/nvim/option.c index 659965b64c..c8e50d4494 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -281,7 +281,7 @@ typedef struct vimoption { # include "options.generated.h" #endif -#define PARAM_COUNT ARRAY_SIZE(options) +#define OPTION_COUNT ARRAY_SIZE(options) static char *(p_ambw_values[]) = { "single", "double", NULL }; static char *(p_bg_values[]) = { "light", "dark", NULL }; @@ -931,6 +931,21 @@ void set_title_defaults(void) } } +void ex_set(exarg_T *eap) +{ + int flags = 0; + + if (eap->cmdidx == CMD_setlocal) { + flags = OPT_LOCAL; + } else if (eap->cmdidx == CMD_setglobal) { + flags = OPT_GLOBAL; + } + if (eap->forceit) { + flags |= OPT_ONECOLUMN; + } + (void)do_set(eap->arg, flags); +} + /// Parse 'arg' for option settings. /// /// 'arg' may be IObuff, but only when no errors can be present and option @@ -1766,7 +1781,7 @@ static char *illegal_char(char *errbuf, size_t errbuflen, int c) if (errbuf == NULL) { return ""; } - vim_snprintf((char *)errbuf, errbuflen, _("E539: Illegal character <%s>"), + vim_snprintf(errbuf, errbuflen, _("E539: Illegal character <%s>"), (char *)transchar(c)); return errbuf; } @@ -1969,10 +1984,9 @@ static void didset_options(void) (void)did_set_spell_option(true); // set cedit_key (void)check_cedit(); - briopt_check(curwin); // initialize the table for 'breakat'. fill_breakat_flags(); - fill_culopt_flags(NULL, curwin); + didset_window_options(curwin); } // More side effects of setting options. @@ -1993,9 +2007,9 @@ static void didset_options2(void) // Parse default for 'wildmode'. check_opt_wim(); xfree(curbuf->b_p_vsts_array); - tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array); + (void)tabstop_set(curbuf->b_p_vsts, &curbuf->b_p_vsts_array); xfree(curbuf->b_p_vts_array); - tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array); + (void)tabstop_set(curbuf->b_p_vts, &curbuf->b_p_vts_array); } /// Check for string options that are NULL (normally only termcap options). @@ -2269,12 +2283,12 @@ static char *set_string_option(const int opt_idx, const char *const value, const *varp = s; char *const saved_oldval = xstrdup(oldval); - char *const saved_oldval_l = (oldval_l != NULL) ? xstrdup((char *)oldval_l) : 0; - char *const saved_oldval_g = (oldval_g != NULL) ? xstrdup((char *)oldval_g) : 0; + char *const saved_oldval_l = (oldval_l != NULL) ? xstrdup(oldval_l) : 0; + char *const saved_oldval_g = (oldval_g != NULL) ? xstrdup(oldval_g) : 0; char *const saved_newval = xstrdup(s); int value_checked = false; - char *const r = did_set_string_option(opt_idx, (char_u **)varp, (int)true, + char *const r = did_set_string_option(opt_idx, (char_u **)varp, true, (char_u *)oldval, NULL, 0, opt_flags, &value_checked); if (r == NULL) { @@ -2762,7 +2776,7 @@ ambw_end: if (!ascii_isdigit(*(s - 1))) { if (errbuf != NULL) { - vim_snprintf((char *)errbuf, errbuflen, + vim_snprintf(errbuf, errbuflen, _("E526: Missing number after <%s>"), transchar_byte(*(s - 1))); errmsg = errbuf; @@ -2953,7 +2967,7 @@ ambw_end: } } else { if (errbuf != NULL) { - vim_snprintf((char *)errbuf, errbuflen, + vim_snprintf(errbuf, errbuflen, _("E535: Illegal character after <%c>"), *--s); errmsg = errbuf; @@ -3084,14 +3098,27 @@ ambw_end: if (foldmethodIsIndent(curwin)) { foldUpdateAll(curwin); } - } else if (varp == &p_ve) { // 'virtualedit' - if (opt_strings_flags(p_ve, p_ve_values, &ve_flags, true) != OK) { - errmsg = e_invarg; - } else if (STRCMP(p_ve, oldval) != 0) { - // Recompute cursor position in case the new 've' setting - // changes something. - validate_virtcol(); - coladvance(curwin->w_virtcol); + } else if (gvarp == &p_ve) { // 'virtualedit' + char_u *ve = p_ve; + unsigned int *flags = &ve_flags; + + if (opt_flags & OPT_LOCAL) { + ve = curwin->w_p_ve; + flags = &curwin->w_ve_flags; + } + + if ((opt_flags & OPT_LOCAL) && *ve == NUL) { + // make the local value empty: use the global value + *flags = 0; + } else { + if (opt_strings_flags(ve, p_ve_values, flags, true) != OK) { + errmsg = e_invarg; + } else if (STRCMP(p_ve, oldval) != 0) { + // Recompute cursor position in case the new 've' setting + // changes something. + validate_virtcol(); + coladvance(curwin->w_virtcol); + } } } else if (varp == &p_csqf) { if (p_csqf != NULL) { @@ -3150,10 +3177,7 @@ ambw_end: char_u *cp; if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1])) { - if (curbuf->b_p_vsts_array) { - xfree(curbuf->b_p_vsts_array); - curbuf->b_p_vsts_array = 0; - } + XFREE_CLEAR(curbuf->b_p_vsts_array); } else { for (cp = *varp; *cp; cp++) { if (ascii_isdigit(*cp)) { @@ -3178,10 +3202,7 @@ ambw_end: char_u *cp; if (!(*varp)[0] || ((*varp)[0] == '0' && !(*varp)[1])) { - if (curbuf->b_p_vts_array) { - xfree(curbuf->b_p_vts_array); - curbuf->b_p_vts_array = NULL; - } + XFREE_CLEAR(curbuf->b_p_vts_array); } else { for (cp = *varp; *cp; cp++) { if (ascii_isdigit(*cp)) { @@ -4321,6 +4342,12 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, } else if (value > 10000) { errmsg = e_invarg; } + } else if (pp == &p_pyx) { + if (value == 0) { + value = 3; + } else if (value != 3) { + errmsg = e_invarg; + } } else if (pp == &p_re) { if (value < 0 || value > 2) { errmsg = e_invarg; @@ -4386,6 +4413,8 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, } else if (pp == &curbuf->b_p_ts || pp == &p_ts) { if (value < 1) { errmsg = e_positive; + } else if (value > TABSTOP_MAX) { + errmsg = e_invarg; } } else if (pp == &curbuf->b_p_tw || pp == &p_tw) { if (value < 0) { @@ -4399,7 +4428,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, // Don't change the value and return early if validation failed. if (errmsg != NULL) { - return (char *)errmsg; + return errmsg; } *pp = value; @@ -4495,10 +4524,6 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, if (pum_drawn()) { pum_redraw(); } - } else if (pp == &p_pyx) { - if (p_pyx != 0 && p_pyx != 2 && p_pyx != 3) { - errmsg = e_invarg; - } } else if (pp == &p_ul || pp == &curbuf->b_p_ul) { // sync undo before 'undolevels' changes // use the old value, otherwise u_sync() may not work properly @@ -4527,7 +4552,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, // Check the (new) bounds for Rows and Columns here. if (p_lines < min_rows() && full_screen) { if (errbuf != NULL) { - vim_snprintf((char *)errbuf, errbuflen, + vim_snprintf(errbuf, errbuflen, _("E593: Need at least %d lines"), min_rows()); errmsg = errbuf; } @@ -4535,7 +4560,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, } if (p_columns < MIN_COLUMNS && full_screen) { if (errbuf != NULL) { - vim_snprintf((char *)errbuf, errbuflen, + vim_snprintf(errbuf, errbuflen, _("E594: Need at least %d columns"), MIN_COLUMNS); errmsg = errbuf; } @@ -4651,7 +4676,7 @@ static char *set_num_option(int opt_idx, char_u *varp, long value, char *errbuf, } check_redraw(options[opt_idx].flags); - return (char *)errmsg; + return errmsg; } /// Trigger the OptionSet autocommand. @@ -5184,7 +5209,7 @@ static void showoptions(int all, int opt_flags) #define INC 20 #define GAP 3 - vimoption_T **items = xmalloc(sizeof(vimoption_T *) * PARAM_COUNT); + vimoption_T **items = xmalloc(sizeof(vimoption_T *) * OPTION_COUNT); // Highlight title if (opt_flags & OPT_GLOBAL) { @@ -5198,6 +5223,7 @@ static void showoptions(int all, int opt_flags) // Do the loop two times: // 1. display the short items // 2. display the long items (only strings and numbers) + // When "opt_flags" has OPT_ONECOLUMN do everything in run 2. for (run = 1; run <= 2 && !got_int; run++) { // collect the items in items[] item_count = 0; @@ -5208,7 +5234,7 @@ static void showoptions(int all, int opt_flags) } varp = NULL; - if (opt_flags != 0) { + if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) != 0) { if (p->indir != PV_NONE) { varp = get_varp_scope(p, opt_flags); } @@ -5217,8 +5243,10 @@ static void showoptions(int all, int opt_flags) } if (varp != NULL && (all == 1 || (all == 0 && !optval_default(p, varp)))) { - if (p->flags & P_BOOL) { - len = 1; // a toggle option fits always + if (opt_flags & OPT_ONECOLUMN) { + len = Columns; + } else if (p->flags & P_BOOL) { + len = 1; // a toggle option fits always } else { option_value2string(p, opt_flags); len = (int)STRLEN(p->fullname) + vim_strsize(NameBuff) + 1; @@ -5748,6 +5776,10 @@ void unset_global_local_option(char *name, void *from) set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, true); redraw_later((win_T *)from, NOT_VALID); break; + case PV_VE: + clear_string_option(&((win_T *)from)->w_p_ve); + ((win_T *)from)->w_ve_flags = 0; + break; } } @@ -5814,6 +5846,8 @@ static char_u *get_varp_scope(vimoption_T *p, int opt_flags) return (char_u *)&(curwin->w_p_fcs); case PV_LCS: return (char_u *)&(curwin->w_p_lcs); + case PV_VE: + return (char_u *)&(curwin->w_p_ve); } return NULL; // "cannot happen" } @@ -5908,6 +5942,9 @@ static char_u *get_varp(vimoption_T *p) case PV_LCS: return *curwin->w_p_lcs != NUL ? (char_u *)&(curwin->w_p_lcs) : p->var; + case PV_VE: + return *curwin->w_p_ve != NUL + ? (char_u *)&curwin->w_p_ve : p->var; case PV_ARAB: return (char_u *)&(curwin->w_p_arab); @@ -6136,6 +6173,7 @@ void win_copy_options(win_T *wp_from, win_T *wp_to) { copy_winopt(&wp_from->w_onebuf_opt, &wp_to->w_onebuf_opt); copy_winopt(&wp_from->w_allbuf_opt, &wp_to->w_allbuf_opt); + didset_window_options(wp_to); } /// Copy the options from one winopt_T to another. @@ -6148,6 +6186,8 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_list = from->wo_list; to->wo_nu = from->wo_nu; to->wo_rnu = from->wo_rnu; + to->wo_ve = vim_strsave(from->wo_ve); + to->wo_ve_flags = from->wo_ve_flags; to->wo_nuw = from->wo_nuw; to->wo_rl = from->wo_rl; to->wo_rlc = vim_strsave(from->wo_rlc); @@ -6192,6 +6232,9 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_fcs = vim_strsave(from->wo_fcs); to->wo_lcs = vim_strsave(from->wo_lcs); to->wo_winbl = from->wo_winbl; + + // Copy the script context so that we know were the value was last set. + memmove(to->wo_script_ctx, from->wo_script_ctx, sizeof(to->wo_script_ctx)); check_winopt(to); // don't want NULL pointers } @@ -6224,6 +6267,7 @@ static void check_winopt(winopt_T *wop) check_string_option(&wop->wo_winhl); check_string_option(&wop->wo_fcs); check_string_option(&wop->wo_lcs); + check_string_option(&wop->wo_ve); } /// Free the allocated memory inside a winopt_T. @@ -6248,6 +6292,7 @@ void clear_winopt(winopt_T *wop) clear_string_option(&wop->wo_winhl); clear_string_option(&wop->wo_fcs); clear_string_option(&wop->wo_lcs); + clear_string_option(&wop->wo_ve); } void didset_window_options(win_T *wp) @@ -6262,11 +6307,30 @@ void didset_window_options(win_T *wp) wp->w_grid_alloc.blending = wp->w_p_winbl > 0; } +/// Index into the options table for a buffer-local option enum. +static int buf_opt_idx[BV_COUNT]; +#define COPY_OPT_SCTX(buf, bv) buf->b_p_script_ctx[bv] = options[buf_opt_idx[bv]].last_set + +/// Initialize buf_opt_idx[] if not done already. +static void init_buf_opt_idx(void) +{ + static int did_init_buf_opt_idx = false; + + if (did_init_buf_opt_idx) { + return; + } + did_init_buf_opt_idx = true; + for (int i = 0; options[i].fullname != NULL; i++) { + if (options[i].indir & PV_BUF) { + buf_opt_idx[options[i].indir & PV_MASK] = i; + } + } +} /// Copy global option values to local options for one buffer. /// Used when creating a new buffer and sometimes when entering a buffer. /// flags: -/// BCO_ENTER We will enter the buf buffer. +/// BCO_ENTER We will enter the buffer "buf". /// BCO_ALWAYS Always copy the options, but only set b_p_initialized when /// appropriate. /// BCO_NOHELP Don't copy the values to a help buffer. @@ -6302,11 +6366,12 @@ void buf_copy_options(buf_T *buf, int flags) } if (should_copy || (flags & BCO_ALWAYS)) { - /* Don't copy the options specific to a help buffer when - * BCO_NOHELP is given or the options were initialized already - * (jumping back to a help file with CTRL-T or CTRL-O) */ - dont_do_help = ((flags & BCO_NOHELP) && buf->b_help) - || buf->b_p_initialized; + memset(buf->b_p_script_ctx, 0, sizeof(buf->b_p_script_ctx)); + init_buf_opt_idx(); + // Don't copy the options specific to a help buffer when + // BCO_NOHELP is given or the options were initialized already + // (jumping back to a help file with CTRL-T or CTRL-O) + dont_do_help = ((flags & BCO_NOHELP) && buf->b_help) || buf->b_p_initialized; if (dont_do_help) { // don't free b_p_isk save_p_isk = buf->b_p_isk; buf->b_p_isk = NULL; @@ -6338,80 +6403,129 @@ void buf_copy_options(buf_T *buf, int flags) } buf->b_p_ai = p_ai; + COPY_OPT_SCTX(buf, BV_AI); buf->b_p_ai_nopaste = p_ai_nopaste; buf->b_p_sw = p_sw; + COPY_OPT_SCTX(buf, BV_SW); buf->b_p_scbk = p_scbk; + COPY_OPT_SCTX(buf, BV_SCBK); buf->b_p_tw = p_tw; + COPY_OPT_SCTX(buf, BV_TW); buf->b_p_tw_nopaste = p_tw_nopaste; buf->b_p_tw_nobin = p_tw_nobin; buf->b_p_wm = p_wm; + COPY_OPT_SCTX(buf, BV_WM); buf->b_p_wm_nopaste = p_wm_nopaste; buf->b_p_wm_nobin = p_wm_nobin; buf->b_p_bin = p_bin; + COPY_OPT_SCTX(buf, BV_BIN); buf->b_p_bomb = p_bomb; + COPY_OPT_SCTX(buf, BV_BOMB); buf->b_p_et = p_et; + COPY_OPT_SCTX(buf, BV_ET); buf->b_p_fixeol = p_fixeol; + COPY_OPT_SCTX(buf, BV_FIXEOL); buf->b_p_et_nobin = p_et_nobin; buf->b_p_et_nopaste = p_et_nopaste; buf->b_p_ml = p_ml; + COPY_OPT_SCTX(buf, BV_ML); buf->b_p_ml_nobin = p_ml_nobin; buf->b_p_inf = p_inf; - buf->b_p_swf = cmdmod.noswapfile ? false : p_swf; + COPY_OPT_SCTX(buf, BV_INF); + if (cmdmod.noswapfile) { + buf->b_p_swf = false; + } else { + buf->b_p_swf = p_swf; + COPY_OPT_SCTX(buf, BV_SWF); + } buf->b_p_cpt = vim_strsave(p_cpt); + COPY_OPT_SCTX(buf, BV_CPT); #ifdef BACKSLASH_IN_FILENAME buf->b_p_csl = vim_strsave(p_csl); + COPY_OPT_SCTX(buf, BV_CSL); #endif buf->b_p_cfu = vim_strsave(p_cfu); + COPY_OPT_SCTX(buf, BV_CFU); buf->b_p_ofu = vim_strsave(p_ofu); + COPY_OPT_SCTX(buf, BV_OFU); buf->b_p_tfu = vim_strsave(p_tfu); + COPY_OPT_SCTX(buf, BV_TFU); buf->b_p_sts = p_sts; + COPY_OPT_SCTX(buf, BV_STS); buf->b_p_sts_nopaste = p_sts_nopaste; buf->b_p_vsts = vim_strsave(p_vsts); + COPY_OPT_SCTX(buf, BV_VSTS); if (p_vsts && p_vsts != empty_option) { - tabstop_set(p_vsts, &buf->b_p_vsts_array); + (void)tabstop_set(p_vsts, &buf->b_p_vsts_array); } else { - buf->b_p_vsts_array = 0; + buf->b_p_vsts_array = NULL; } buf->b_p_vsts_nopaste = p_vsts_nopaste ? vim_strsave(p_vsts_nopaste) : NULL; buf->b_p_com = vim_strsave(p_com); + COPY_OPT_SCTX(buf, BV_COM); buf->b_p_cms = vim_strsave(p_cms); + COPY_OPT_SCTX(buf, BV_CMS); buf->b_p_fo = vim_strsave(p_fo); + COPY_OPT_SCTX(buf, BV_FO); buf->b_p_flp = vim_strsave(p_flp); + COPY_OPT_SCTX(buf, BV_FLP); buf->b_p_nf = vim_strsave(p_nf); + COPY_OPT_SCTX(buf, BV_NF); buf->b_p_mps = vim_strsave(p_mps); + COPY_OPT_SCTX(buf, BV_MPS); buf->b_p_si = p_si; + COPY_OPT_SCTX(buf, BV_SI); buf->b_p_channel = 0; buf->b_p_ci = p_ci; + COPY_OPT_SCTX(buf, BV_CI); buf->b_p_cin = p_cin; + COPY_OPT_SCTX(buf, BV_CIN); buf->b_p_cink = vim_strsave(p_cink); + COPY_OPT_SCTX(buf, BV_CINK); buf->b_p_cino = vim_strsave(p_cino); + COPY_OPT_SCTX(buf, BV_CINO); // Don't copy 'filetype', it must be detected buf->b_p_ft = empty_option; buf->b_p_pi = p_pi; + COPY_OPT_SCTX(buf, BV_PI); buf->b_p_cinw = vim_strsave(p_cinw); + COPY_OPT_SCTX(buf, BV_CINW); buf->b_p_lisp = p_lisp; + COPY_OPT_SCTX(buf, BV_LISP); // Don't copy 'syntax', it must be set buf->b_p_syn = empty_option; buf->b_p_smc = p_smc; + COPY_OPT_SCTX(buf, BV_SMC); buf->b_s.b_syn_isk = empty_option; buf->b_s.b_p_spc = vim_strsave(p_spc); + COPY_OPT_SCTX(buf, BV_SPC); (void)compile_cap_prog(&buf->b_s); buf->b_s.b_p_spf = vim_strsave(p_spf); + COPY_OPT_SCTX(buf, BV_SPF); buf->b_s.b_p_spl = vim_strsave(p_spl); + COPY_OPT_SCTX(buf, BV_SPL); buf->b_s.b_p_spo = vim_strsave(p_spo); + COPY_OPT_SCTX(buf, BV_SPO); buf->b_p_inde = vim_strsave(p_inde); + COPY_OPT_SCTX(buf, BV_INDE); buf->b_p_indk = vim_strsave(p_indk); + COPY_OPT_SCTX(buf, BV_INDK); buf->b_p_fp = empty_option; buf->b_p_fex = vim_strsave(p_fex); + COPY_OPT_SCTX(buf, BV_FEX); buf->b_p_sua = vim_strsave(p_sua); + COPY_OPT_SCTX(buf, BV_SUA); buf->b_p_keymap = vim_strsave(p_keymap); + COPY_OPT_SCTX(buf, BV_KMAP); buf->b_kmap_state |= KEYMAP_INIT; // This isn't really an option, but copying the langmap and IME // state from the current buffer is better than resetting it. buf->b_p_iminsert = p_iminsert; + COPY_OPT_SCTX(buf, BV_IMI); buf->b_p_imsearch = p_imsearch; + COPY_OPT_SCTX(buf, BV_IMS); // options that are normally global but also have a local value // are not copied, start using the global value @@ -6431,11 +6545,14 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_def = empty_option; buf->b_p_inc = empty_option; buf->b_p_inex = vim_strsave(p_inex); + COPY_OPT_SCTX(buf, BV_INEX); buf->b_p_dict = empty_option; buf->b_p_tsr = empty_option; buf->b_p_tsrfu = empty_option; buf->b_p_qe = vim_strsave(p_qe); + COPY_OPT_SCTX(buf, BV_QE); buf->b_p_udf = p_udf; + COPY_OPT_SCTX(buf, BV_UDF); buf->b_p_lw = empty_option; buf->b_p_menc = empty_option; @@ -6448,17 +6565,20 @@ void buf_copy_options(buf_T *buf, int flags) if (dont_do_help) { buf->b_p_isk = save_p_isk; if (p_vts && p_vts != empty_option && !buf->b_p_vts_array) { - tabstop_set(p_vts, &buf->b_p_vts_array); + (void)tabstop_set(p_vts, &buf->b_p_vts_array); } else { buf->b_p_vts_array = NULL; } } else { buf->b_p_isk = vim_strsave(p_isk); + COPY_OPT_SCTX(buf, BV_ISK); did_isk = true; buf->b_p_ts = p_ts; + COPY_OPT_SCTX(buf, BV_TS); buf->b_p_vts = vim_strsave(p_vts); + COPY_OPT_SCTX(buf, BV_VTS); if (p_vts && p_vts != empty_option && !buf->b_p_vts_array) { - tabstop_set(p_vts, &buf->b_p_vts_array); + (void)tabstop_set(p_vts, &buf->b_p_vts_array); } else { buf->b_p_vts_array = NULL; } @@ -6467,6 +6587,7 @@ void buf_copy_options(buf_T *buf, int flags) clear_string_option(&buf->b_p_bt); } buf->b_p_ma = p_ma; + COPY_OPT_SCTX(buf, BV_MA); } } @@ -7107,10 +7228,7 @@ static void paste_option_changed(void) free_string_option(buf->b_p_vsts); } buf->b_p_vsts = empty_option; - if (buf->b_p_vsts_array) { - xfree(buf->b_p_vsts_array); - } - buf->b_p_vsts_array = 0; + XFREE_CLEAR(buf->b_p_vsts_array); } // set global options @@ -7147,13 +7265,11 @@ static void paste_option_changed(void) buf->b_p_vsts = buf->b_p_vsts_nopaste ? vim_strsave(buf->b_p_vsts_nopaste) : empty_option; - if (buf->b_p_vsts_array) { - xfree(buf->b_p_vsts_array); - } + xfree(buf->b_p_vsts_array); if (buf->b_p_vsts && buf->b_p_vsts != empty_option) { - tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array); + (void)tabstop_set(buf->b_p_vsts, &buf->b_p_vsts_array); } else { - buf->b_p_vsts_array = 0; + buf->b_p_vsts_array = NULL; } } @@ -7469,6 +7585,7 @@ int check_ff_value(char_u *p) // Set the integer values corresponding to the string setting of 'vartabstop'. // "array" will be set, caller must free it if needed. +// Return false for an error. bool tabstop_set(char_u *var, long **array) { long valcount = 1; @@ -7488,7 +7605,7 @@ bool tabstop_set(char_u *var, long **array) if (cp != end) { emsg(_(e_positive)); } else { - emsg(_(e_invarg)); + semsg(_(e_invarg2), cp); } return false; } @@ -7501,7 +7618,7 @@ bool tabstop_set(char_u *var, long **array) valcount++; continue; } - emsg(_(e_invarg)); + semsg(_(e_invarg2), var); return false; } @@ -7510,7 +7627,15 @@ bool tabstop_set(char_u *var, long **array) t = 1; for (cp = var; *cp != NUL;) { - (*array)[t++] = atoi((char *)cp); + int n = atoi((char *)cp); + + // Catch negative values, overflow and ridiculous big values. + if (n <= 0 || n > TABSTOP_MAX) { + semsg(_(e_invarg2), cp); + XFREE_CLEAR(*array); + return false; + } + (*array)[t++] = n; while (*cp != NUL && *cp != ',') { cp++; } @@ -7815,6 +7940,12 @@ unsigned int get_bkc_value(buf_T *buf) return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags; } +/// Get the local or global value of the 'virtualedit' flags. +unsigned int get_ve_flags(void) +{ + return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags) & ~(VE_NONE | VE_NONEU); +} + /// Get the local or global value of 'showbreak'. /// /// @param win If not NULL, the window to get the local option from; global @@ -7994,7 +8125,6 @@ int win_signcol_count(win_T *wp) /// Return the number of requested sign columns, based on user / configuration. int win_signcol_configured(win_T *wp, int *is_fixed) { - int minimum = 0, maximum = 1, needed_signcols; const char *scl = (const char *)wp->w_p_scl; if (is_fixed) { @@ -8007,7 +8137,6 @@ int win_signcol_configured(win_T *wp, int *is_fixed) && (wp->w_p_nu || wp->w_p_rnu)))) { return 0; } - needed_signcols = buf_signcols(wp->w_buffer); // yes or yes if (!strncmp(scl, "yes:", 4)) { @@ -8023,6 +8152,8 @@ int win_signcol_configured(win_T *wp, int *is_fixed) *is_fixed = 0; } + int minimum = 0, maximum = 1; + if (!strncmp(scl, "auto:", 5)) { // Variable depending on a configuration maximum = scl[5] - '0'; @@ -8033,7 +8164,8 @@ int win_signcol_configured(win_T *wp, int *is_fixed) } } - int ret = MAX(minimum, MIN(maximum, needed_signcols)); + int needed_signcols = buf_signcols(wp->w_buffer, maximum); + int ret = MAX(minimum, needed_signcols); assert(ret <= SIGN_SHOW_MAX); return ret; } diff --git a/src/nvim/option.h b/src/nvim/option.h index f7dbaafeec..9321dd5454 100644 --- a/src/nvim/option.h +++ b/src/nvim/option.h @@ -13,16 +13,16 @@ /// When OPT_GLOBAL and OPT_LOCAL are both missing, set both local and global /// values, get local value. typedef enum { - OPT_FREE = 1, ///< Free old value if it was allocated. - OPT_GLOBAL = 2, ///< Use global value. - OPT_LOCAL = 4, ///< Use local value. - OPT_MODELINE = 8, ///< Option in modeline. - OPT_WINONLY = 16, ///< Only set window-local options. - OPT_NOWIN = 32, ///< Don’t set window-local options. - OPT_ONECOLUMN = 64, ///< list options one per line - OPT_NO_REDRAW = 128, ///< ignore redraw flags on option - OPT_SKIPRTP = 256, ///< "skiprtp" in 'sessionoptions' - OPT_CLEAR = 512, ///< Clear local value of an option. + OPT_FREE = 0x01, ///< Free old value if it was allocated. + OPT_GLOBAL = 0x02, ///< Use global value. + OPT_LOCAL = 0x04, ///< Use local value. + OPT_MODELINE = 0x08, ///< Option in modeline. + OPT_WINONLY = 0x10, ///< Only set window-local options. + OPT_NOWIN = 0x20, ///< Don’t set window-local options. + OPT_ONECOLUMN = 0x40, ///< list options one per line + OPT_NO_REDRAW = 0x80, ///< ignore redraw flags on option + OPT_SKIPRTP = 0x100, ///< "skiprtp" in 'sessionoptions' + OPT_CLEAR = 0x200, ///< Clear local value of an option. } OptionFlags; #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index 09c3bf3800..d88cd6b9b9 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -705,12 +705,14 @@ EXTERN int p_vb; ///< 'visualbell' EXTERN char_u *p_ve; ///< 'virtualedit' EXTERN unsigned ve_flags; #ifdef IN_OPTION_C -static char *(p_ve_values[]) = { "block", "insert", "all", "onemore", NULL }; +static char *(p_ve_values[]) = { "block", "insert", "all", "onemore", "none", "NONE", NULL }; #endif -#define VE_BLOCK 5 // includes "all" -#define VE_INSERT 6 // includes "all" -#define VE_ALL 4 -#define VE_ONEMORE 8 +#define VE_BLOCK 5U // includes "all" +#define VE_INSERT 6U // includes "all" +#define VE_ALL 4U +#define VE_ONEMORE 8U +#define VE_NONE 16U // "none" +#define VE_NONEU 32U // "NONE" EXTERN long p_verbose; // 'verbose' #ifdef IN_OPTION_C char_u *p_vfile = (char_u *)""; // used before options are initialized @@ -869,6 +871,7 @@ enum { WV_LBR, WV_NU, WV_RNU, + WV_VE, WV_NUW, WV_PVW, WV_RL, @@ -900,6 +903,8 @@ enum { #define SB_MAX 100000 // Maximum 'scrollback' value. +#define TABSTOP_MAX 9999 + /// Stores an identifier of a script or channel that last set an option. typedef struct { sctx_T script_ctx; /// script context where the option was last set diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 28b4eb9fe2..e665ffd346 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -411,7 +411,7 @@ return { }, { full_name='compatible', abbreviation='cp', - short_desc=N_("No description"), + short_desc=N_("No description"), type='bool', scope={'global'}, redraw={'all_windows'}, varname='p_force_off', @@ -665,14 +665,14 @@ return { }, { full_name='edcompatible', abbreviation='ed', - short_desc=N_("No description"), + short_desc=N_("No description"), type='bool', scope={'global'}, varname='p_force_off', defaults={if_true=false} }, { full_name='emoji', abbreviation='emo', - short_desc=N_("No description"), + short_desc=N_("No description"), type='bool', scope={'global'}, redraw={'all_windows', 'ui_option'}, varname='p_emoji', @@ -1184,7 +1184,7 @@ return { }, { full_name='inccommand', abbreviation='icm', - short_desc=N_("Live preview of substitution"), + short_desc=N_("Live preview of substitution"), type='string', scope={'global'}, redraw={'all_windows'}, varname='p_icm', @@ -1846,7 +1846,7 @@ return { type='number', scope={'global'}, secure=true, varname='p_pyx', - defaults={if_true=0} + defaults={if_true=3} }, { full_name='quickfixtextfunc', abbreviation='qftf', @@ -2499,7 +2499,7 @@ return { }, { full_name='termencoding', abbreviation='tenc', - short_desc=N_("Terminal encodig"), + short_desc=N_("Terminal encoding"), type='string', scope={'global'}, defaults={if_true=""} }, @@ -2622,7 +2622,7 @@ return { }, { full_name='ttyfast', abbreviation='tf', - short_desc=N_("No description"), + short_desc=N_("No description"), type='bool', scope={'global'}, no_mkrc=true, varname='p_force_on', @@ -2736,7 +2736,7 @@ return { { full_name='virtualedit', abbreviation='ve', short_desc=N_("when to use virtual editing"), - type='string', list='onecomma', scope={'global'}, + type='string', list='onecomma', scope={'global', 'window'}, deny_duplicates=true, redraw={'curswant'}, varname='p_ve', diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index e9f44d2775..e9868d6b61 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -1111,10 +1111,9 @@ size_t home_replace(const buf_T *const buf, const char_u *src, char_u *const dst *dst_p++ = '~'; } - // If it's just the home directory, add "/". - if (!vim_ispathsep(src[0]) && --dstlen > 0) { - *dst_p++ = '/'; - } + // Do not add directory separator into dst, because dst is + // expected to just return the directory name without the + // directory separator '/'. break; } if (p == homedir_env_mod) { diff --git a/src/nvim/os/input.c b/src/nvim/os/input.c index 3790eba212..54cfaee80a 100644 --- a/src/nvim/os/input.c +++ b/src/nvim/os/input.c @@ -234,9 +234,9 @@ size_t input_enqueue(String keys) while (rbuffer_space(input_buffer) >= 19 && ptr < end) { // A "<x>" form occupies at least 1 characters, and produces up // to 19 characters (1 + 5 * 3 for the char and 3 for a modifier). - // In the case of K_SPECIAL(0x80) or CSI(0x9B), 3 bytes are escaped and - // needed, but since the keys are UTF-8, so the first byte cannot be - // K_SPECIAL(0x80) or CSI(0x9B). + // In the case of K_SPECIAL(0x80), 3 bytes are escaped and needed, + // but since the keys are UTF-8, so the first byte cannot be + // K_SPECIAL(0x80). uint8_t buf[19] = { 0 }; unsigned int new_size = trans_special((const uint8_t **)&ptr, (size_t)(end - ptr), buf, true, @@ -263,12 +263,8 @@ size_t input_enqueue(String keys) continue; } - // copy the character, escaping CSI and K_SPECIAL - if ((uint8_t)*ptr == CSI) { - rbuffer_write(input_buffer, (char *)&(uint8_t){ K_SPECIAL }, 1); - rbuffer_write(input_buffer, (char *)&(uint8_t){ KS_EXTRA }, 1); - rbuffer_write(input_buffer, (char *)&(uint8_t){ KE_CSI }, 1); - } else if ((uint8_t)*ptr == K_SPECIAL) { + // copy the character, escaping K_SPECIAL + if ((uint8_t)(*ptr) == K_SPECIAL) { rbuffer_write(input_buffer, (char *)&(uint8_t){ K_SPECIAL }, 1); rbuffer_write(input_buffer, (char *)&(uint8_t){ KS_SPECIAL }, 1); rbuffer_write(input_buffer, (char *)&(uint8_t){ KE_FILLER }, 1); diff --git a/src/nvim/path.c b/src/nvim/path.c index 674d67e21a..7f7f941e26 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -269,16 +269,17 @@ int vim_ispathlistsep(int c) #endif } -/* - * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" - * It's done in-place. - */ -char_u *shorten_dir(char_u *str) +/// Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" +/// "trim_len" specifies how many characters to keep for each directory. +/// Must be 1 or more. +/// It's done in-place. +void shorten_dir_len(char_u *str, int trim_len) { char_u *tail = path_tail(str); char_u *d = str; bool skip = false; - for (char_u *s = str;; ++s) { + int dirchunk_len = 0; + for (char_u *s = str;; s++) { if (s >= tail) { // copy the whole tail *d++ = *s; if (*s == NUL) { @@ -287,10 +288,16 @@ char_u *shorten_dir(char_u *str) } else if (vim_ispathsep(*s)) { // copy '/' and next char *d++ = *s; skip = false; + dirchunk_len = 0; } else if (!skip) { *d++ = *s; // copy next char if (*s != '~' && *s != '.') { // and leading "~" and "." - skip = true; + dirchunk_len++; // only count word chars for the size + // keep copying chars until we have our preferred length (or + // until the above if/else branches move us along) + if (dirchunk_len >= trim_len) { + skip = true; + } } int l = utfc_ptr2len(s); while (--l > 0) { @@ -298,7 +305,13 @@ char_u *shorten_dir(char_u *str) } } } - return str; +} + +/// Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname" +/// It's done in-place. +void shorten_dir(char_u *str) +{ + shorten_dir_len(str, 1); } /* @@ -1508,7 +1521,7 @@ void simplify_filename(char_u *filename) p = filename; #ifdef BACKSLASH_IN_FILENAME - if (p[1] == ':') { // skip "x:" + if (p[0] != NUL && p[1] == ':') { // skip "x:" p += 2; } #endif @@ -1682,6 +1695,10 @@ char_u *find_file_name_in_path(char_u *ptr, size_t len, int options, long count, char_u *file_name; char_u *tofree = NULL; + if (len == 0) { + return NULL; + } + if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL) { tofree = (char_u *)eval_includeexpr((char *)ptr, len); if (tofree != NULL) { @@ -1743,14 +1760,32 @@ int path_is_url(const char *p) return 0; } -/// Check if "fname" starts with "name://". Return URL_SLASH if it does. +/// Check if "fname" starts with "name://" or "name:\\". /// /// @param fname is the filename to test -/// @return URL_BACKSLASH for "name:\\", zero otherwise. +/// @return URL_SLASH for "name://", URL_BACKSLASH for "name:\\", zero otherwise. int path_with_url(const char *fname) { const char *p; - for (p = fname; isalpha(*p); p++) {} + + // We accept alphabetic characters and a dash in scheme part. + // RFC 3986 allows for more, but it increases the risk of matching + // non-URL text. + + // first character must be alpha + if (!isalpha(*fname)) { + return 0; + } + + // check body: alpha or dash + for (p = fname; (isalpha(*p) || (*p == '-')); p++) {} + + // check last char is not a dash + if (p[-1] == '-') { + return 0; + } + + // "://" or ":\\" must follow return path_is_url(p); } @@ -2367,9 +2402,11 @@ static int path_to_absolute(const char_u *fname, char_u *buf, size_t len, int fo int path_is_absolute(const char_u *fname) { #ifdef WIN32 + if (*fname == NUL) { + return false; + } // A name like "d:/foo" and "//server/share" is absolute - return ((isalpha(fname[0]) && fname[1] == ':' - && vim_ispathsep_nocolon(fname[2])) + return ((isalpha(fname[0]) && fname[1] == ':' && vim_ispathsep_nocolon(fname[2])) || (vim_ispathsep_nocolon(fname[0]) && fname[0] == fname[1])); #else // UNIX: This just checks if the file name starts with '/' or '~'. diff --git a/src/nvim/po/ja.euc-jp.po b/src/nvim/po/ja.euc-jp.po index 5dda7c59f5..9633bec9f2 100644 --- a/src/nvim/po/ja.euc-jp.po +++ b/src/nvim/po/ja.euc-jp.po @@ -1644,15 +1644,6 @@ msgstr " [w]" msgid " written" msgstr " " -msgid "E205: Patchmode: can't save original file" -msgstr "E205: patchmode: ܥե¸Ǥޤ" - -msgid "E206: patchmode: can't touch empty original file" -msgstr "E206: patchmode: θܥեtouchǤޤ" - -msgid "E207: Can't delete backup file" -msgstr "E207: Хååץեäޤ" - msgid "" "\n" "WARNING: Original file may be lost or damaged\n" diff --git a/src/nvim/po/ja.po b/src/nvim/po/ja.po index a169bd3589..c363c00fa6 100644 --- a/src/nvim/po/ja.po +++ b/src/nvim/po/ja.po @@ -1644,15 +1644,6 @@ msgstr " [w]" msgid " written" msgstr " 書込み" -msgid "E205: Patchmode: can't save original file" -msgstr "E205: patchmode: 原本ファイルを保存できません" - -msgid "E206: patchmode: can't touch empty original file" -msgstr "E206: patchmode: 空の原本ファイルをtouchできません" - -msgid "E207: Can't delete backup file" -msgstr "E207: バックアップファイルを消せません" - msgid "" "\n" "WARNING: Original file may be lost or damaged\n" diff --git a/src/nvim/popupmnu.c b/src/nvim/popupmnu.c index da2ada791f..d7726409b5 100644 --- a/src/nvim/popupmnu.c +++ b/src/nvim/popupmnu.c @@ -291,7 +291,7 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i } else { assert(Columns - pum_col - pum_scrollbar >= INT_MIN && Columns - pum_col - pum_scrollbar <= INT_MAX); - pum_width = (int)(Columns - pum_col - pum_scrollbar); + pum_width = Columns - pum_col - pum_scrollbar; } if ((pum_width > max_width + pum_kind_width + pum_extra_width + 1) @@ -352,12 +352,12 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i // not enough room, will use what we have if (pum_rl) { assert(Columns - 1 >= INT_MIN); - pum_col = (int)(Columns - 1); + pum_col = Columns - 1; } else { pum_col = 0; } assert(Columns - 1 >= INT_MIN); - pum_width = (int)(Columns - 1); + pum_width = Columns - 1; } else { if (max_width > p_pw) { // truncate @@ -369,7 +369,7 @@ void pum_display(pumitem_T *array, int size, int selected, bool array_changed, i } else { assert(Columns - max_width >= INT_MIN && Columns - max_width <= INT_MAX); - pum_col = (int)(Columns - max_width); + pum_col = Columns - max_width; } pum_width = max_width - pum_scrollbar; } diff --git a/src/nvim/pos.h b/src/nvim/pos.h index d17e27906e..51991ed314 100644 --- a/src/nvim/pos.h +++ b/src/nvim/pos.h @@ -16,7 +16,9 @@ typedef int colnr_T; /// Maximal (invalid) line number enum { MAXLNUM = 0x7fffffff, }; /// Maximal column number -enum { MAXCOL = INT_MAX, }; +/// MAXCOL used to be INT_MAX, but with 64 bit ints that results in running +/// out of memory when trying to allocate a very long line. +enum { MAXCOL = 0x7fffffff, }; // Minimum line number enum { MINLNUM = 1, }; // minimum column number diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 0196e05455..7e29aed51b 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -131,7 +131,7 @@ struct qf_info_S { static qf_info_T ql_info; // global quickfix list static unsigned last_qf_id = 0; // Last Used quickfix list id -#define FMT_PATTERNS 11 // maximum number of % recognized +#define FMT_PATTERNS 13 // maximum number of % recognized // Structure used to hold the info of one part of 'errorformat' typedef struct efm_S efm_T; @@ -209,6 +209,17 @@ typedef struct { bool valid; } qffields_T; +/// :vimgrep command arguments +typedef struct vgr_args_S { + long tomatch; ///< maximum number of matches to find + char_u *spat; ///< search pattern + int flags; ///< search modifier + char_u **fnames; ///< list of files to search + int fcount; ///< number of files + regmmatch_T regmatch; ///< compiled search pattern + char_u *qf_title; ///< quickfix list title +} vgr_args_T; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "quickfix.c.generated.h" #endif @@ -321,22 +332,27 @@ int qf_init(win_T *wp, const char_u *restrict efile, char_u *restrict errorforma // Maximum number of bytes allowed per line while reading an errorfile. static const size_t LINE_MAXLEN = 4096; +/// Patterns used. Keep in sync with qf_parse_fmt[]. static struct fmtpattern { char_u convchar; char *pattern; } fmt_pat[FMT_PATTERNS] = { - { 'f', ".\\+" }, // only used when at end - { 'n', "\\d\\+" }, - { 'l', "\\d\\+" }, - { 'c', "\\d\\+" }, - { 't', "." }, - { 'm', ".\\+" }, - { 'r', ".*" }, - { 'p', "[- .]*"}, // NOLINT(whitespace/tab) - { 'v', "\\d\\+" }, - { 's', ".\\+" }, - { 'o', ".\\+" } + { 'f', ".\\+" }, // only used when at end + { 'n', "\\d\\+" }, // 1 + { 'l', "\\d\\+" }, // 2 + { 'e', "\\d\\+" }, // 3 + { 'c', "\\d\\+" }, // 4 + { 'k', "\\d\\+" }, // 5 + { 't', "." }, // 6 +#define FMT_PATTERN_M 7 + { 'm', ".\\+" }, // 7 +#define FMT_PATTERN_R 8 + { 'r', ".*" }, // 8 + { 'p', "[- \t.]*" }, // 9 + { 'v', "\\d\\+" }, // 10 + { 's', ".\\+" }, // 11 + { 'o', ".\\+" } // 12 }; /// Convert an errorformat pattern to a regular expression pattern. @@ -352,9 +368,9 @@ static char_u *efmpat_to_regpat(const char_u *efmpat, char_u *regpat, efm_T *efm semsg(_("E372: Too many %%%c in format string"), *efmpat); return NULL; } - if ((idx && idx < 6 + if ((idx && idx < FMT_PATTERN_R && vim_strchr((char_u *)"DXOPQ", efminfo->prefix) != NULL) - || (idx == 6 + || (idx == FMT_PATTERN_R && vim_strchr((char_u *)"OPQ", efminfo->prefix) == NULL)) { semsg(_("E373: Unexpected %%%c in format string"), *efmpat); return NULL; @@ -1277,7 +1293,7 @@ static int qf_parse_fmt_n(regmatch_T *rmp, int midx, qffields_T *fields) return QF_OK; } -/// Parse the match for line number (%l') pattern in regmatch. +/// Parse the match for line number ('%l') pattern in regmatch. /// Return the matched value in "fields->lnum". static int qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields) { @@ -1288,6 +1304,17 @@ static int qf_parse_fmt_l(regmatch_T *rmp, int midx, qffields_T *fields) return QF_OK; } +/// Parse the match for end line number ('%e') pattern in regmatch. +/// Return the matched value in "fields->end_lnum". +static int qf_parse_fmt_e(regmatch_T *rmp, int midx, qffields_T *fields) +{ + if (rmp->startp[midx] == NULL) { + return QF_FAIL; + } + fields->end_lnum = atol((char *)rmp->startp[midx]); + return QF_OK; +} + /// Parse the match for column number ('%c') pattern in regmatch. /// Return the matched value in "fields->col". static int qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields) @@ -1299,6 +1326,17 @@ static int qf_parse_fmt_c(regmatch_T *rmp, int midx, qffields_T *fields) return QF_OK; } +/// Parse the match for end line number ('%e') pattern in regmatch. +/// Return the matched value in "fields->end_lnum". +static int qf_parse_fmt_k(regmatch_T *rmp, int midx, qffields_T *fields) +{ + if (rmp->startp[midx] == NULL) { + return QF_FAIL; + } + fields->end_col = (int)atol((char *)rmp->startp[midx]); + return QF_OK; +} + /// Parse the match for error type ('%t') pattern in regmatch. /// Return the matched value in "fields->type". static int qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields) @@ -1431,14 +1469,17 @@ static int qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields) /// 'errorformat' format pattern parser functions. /// The '%f' and '%r' formats are parsed differently from other formats. /// See qf_parse_match() for details. +/// Keep in sync with fmt_pat[]. static int (*qf_parse_fmt[FMT_PATTERNS])(regmatch_T *, int, qffields_T *) = { - NULL, + NULL, // %f qf_parse_fmt_n, qf_parse_fmt_l, + qf_parse_fmt_e, qf_parse_fmt_c, + qf_parse_fmt_k, qf_parse_fmt_t, qf_parse_fmt_m, - NULL, + NULL, // %r qf_parse_fmt_p, qf_parse_fmt_v, qf_parse_fmt_s, @@ -1474,13 +1515,13 @@ static int qf_parse_match(char_u *linebuf, size_t linelen, efm_T *fmt_ptr, regma midx = (int)fmt_ptr->addr[i]; if (i == 0 && midx > 0) { // %f status = qf_parse_fmt_f(regmatch, midx, fields, idx); - } else if (i == 5) { + } else if (i == FMT_PATTERN_M) { if (fmt_ptr->flags == '+' && !qf_multiscan) { // %+ qf_parse_fmt_plus(linebuf, linelen, fields); } else if (midx > 0) { // %m status = qf_parse_fmt_m(regmatch, midx, fields); } - } else if (i == 6 && midx > 0) { // %r + } else if (i == FMT_PATTERN_R && midx > 0) { // %r status = qf_parse_fmt_r(regmatch, midx, tail); } else if (midx > 0) { // others status = (qf_parse_fmt[i])(regmatch, midx, fields); @@ -1625,10 +1666,16 @@ static int qf_parse_multiline_pfx(int idx, qf_list_T *qfl, qffields_T *fields) if (!qfprev->qf_lnum) { qfprev->qf_lnum = fields->lnum; } + if (!qfprev->qf_end_lnum) { + qfprev->qf_end_lnum = fields->end_lnum; + } if (!qfprev->qf_col) { qfprev->qf_col = fields->col; qfprev->qf_viscol = fields->use_viscol; } + if (!qfprev->qf_end_col) { + qfprev->qf_end_col = fields->end_col; + } if (!qfprev->qf_fnum) { qfprev->qf_fnum = qf_get_fnum(qfl, qfl->qf_directory, *fields->namebuf || qfl->qf_directory @@ -2975,7 +3022,7 @@ static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, boo if (retval != OK) { if (opened_window) { - win_close(curwin, true); // Close opened window + win_close(curwin, true, false); // Close opened window } if (qf_ptr != NULL && qf_ptr->qf_fnum != 0) { // Couldn't open file, so put index back where it was. This could @@ -3530,7 +3577,7 @@ void ex_cclose(exarg_T *eap) // Find existing quickfix window and close it. win = qf_find_win(qi); if (win != NULL) { - win_close(win, false); + win_close(win, false, false); } } @@ -3604,7 +3651,7 @@ static int qf_open_new_cwindow(qf_info_T *qi, int height) // win_split, so add a check to ensure that the win is still here if (IS_LL_STACK(qi) && !win_valid(win)) { // close the window that was supposed to be for the loclist - win_close(curwin, false); + win_close(curwin, false, false); return FAIL; } @@ -4849,11 +4896,12 @@ static qfline_T *qf_find_closest_entry(qf_list_T *qfl, int bnr, const pos_T *pos /// Get the nth quickfix entry below the specified entry. Searches forward in /// the list. If linewise is true, then treat multiple entries on a single line /// as one. -static void qf_get_nth_below_entry(qfline_T *entry, linenr_T n, bool linewise, int *errornr) +static void qf_get_nth_below_entry(qfline_T *entry_arg, linenr_T n, bool linewise, int *errornr) FUNC_ATTR_NONNULL_ALL { + qfline_T *entry = entry_arg; + while (n-- > 0 && !got_int) { - // qfline_T *first_entry = entry; int first_errornr = *errornr; if (linewise) { @@ -4864,9 +4912,6 @@ static void qf_get_nth_below_entry(qfline_T *entry, linenr_T n, bool linewise, i if (entry->qf_next == NULL || entry->qf_next->qf_fnum != entry->qf_fnum) { if (linewise) { - // If multiple entries are on the same line, then use the first - // entry - // entry = first_entry; *errornr = first_errornr; } break; @@ -5194,49 +5239,93 @@ static bool vgr_qflist_valid(win_T *wp, qf_info_T *qi, unsigned qfid, char_u *ti /// Search for a pattern in all the lines in a buffer and add the matching lines /// to a quickfix list. -static bool vgr_match_buflines(qf_list_T *qfl, char_u *fname, buf_T *buf, regmmatch_T *regmatch, - long *tomatch, int duplicate_name, int flags) - FUNC_ATTR_NONNULL_ARG(1, 3, 4, 5) +static bool vgr_match_buflines(qf_list_T *qfl, char_u *fname, buf_T *buf, char_u *spat, + regmmatch_T *regmatch, long *tomatch, int duplicate_name, int flags) + FUNC_ATTR_NONNULL_ARG(1, 3, 4, 5, 6) { bool found_match = false; for (long lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; lnum++) { colnr_T col = 0; - while (vim_regexec_multi(regmatch, curwin, buf, lnum, col, NULL, - NULL) > 0) { - // Pass the buffer number so that it gets used even for a - // dummy buffer, unless duplicate_name is set, then the - // buffer will be wiped out below. - if (qf_add_entry(qfl, - NULL, // dir - fname, - NULL, - duplicate_name ? 0 : buf->b_fnum, - ml_get_buf(buf, regmatch->startpos[0].lnum + lnum, - false), - regmatch->startpos[0].lnum + lnum, - regmatch->endpos[0].lnum + lnum, - regmatch->startpos[0].col + 1, - regmatch->endpos[0].col + 1, - false, // vis_col - NULL, // search pattern - 0, // nr - 0, // type - true) // valid - == QF_FAIL) { - got_int = true; - break; - } - found_match = true; - if (--*tomatch == 0) { - break; - } - if ((flags & VGR_GLOBAL) == 0 || regmatch->endpos[0].lnum > 0) { - break; + if (!(flags & VGR_FUZZY)) { + // Regular expression match + while (vim_regexec_multi(regmatch, curwin, buf, lnum, col, NULL, NULL) > 0) { + // Pass the buffer number so that it gets used even for a + // dummy buffer, unless duplicate_name is set, then the + // buffer will be wiped out below. + if (qf_add_entry(qfl, + NULL, // dir + fname, + NULL, + duplicate_name ? 0 : buf->b_fnum, + ml_get_buf(buf, regmatch->startpos[0].lnum + lnum, false), + regmatch->startpos[0].lnum + lnum, + regmatch->endpos[0].lnum + lnum, + regmatch->startpos[0].col + 1, + regmatch->endpos[0].col + 1, + false, // vis_col + NULL, // search pattern + 0, // nr + 0, // type + true) // valid + == QF_FAIL) { + got_int = true; + break; + } + found_match = true; + if (--*tomatch == 0) { + break; + } + if ((flags & VGR_GLOBAL) == 0 || regmatch->endpos[0].lnum > 0) { + break; + } + col = regmatch->endpos[0].col + (col == regmatch->endpos[0].col); + if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, false))) { + break; + } } - col = regmatch->endpos[0].col + (col == regmatch->endpos[0].col); - if (col > (colnr_T)STRLEN(ml_get_buf(buf, lnum, false))) { - break; + } else { + const size_t pat_len = STRLEN(spat); + char_u *const str = ml_get_buf(buf, lnum, false); + int score; + uint32_t matches[MAX_FUZZY_MATCHES]; + const size_t sz = sizeof(matches) / sizeof(matches[0]); + + // Fuzzy string match + while (fuzzy_match(str + col, spat, false, &score, matches, (int)sz) > 0) { + // Pass the buffer number so that it gets used even for a + // dummy buffer, unless duplicate_name is set, then the + // buffer will be wiped out below. + if (qf_add_entry(qfl, + NULL, // dir + fname, + NULL, + duplicate_name ? 0 : buf->b_fnum, + str, + lnum, + 0, + (colnr_T)matches[0] + col + 1, + 0, + false, // vis_col + NULL, // search pattern + 0, // nr + 0, // type + true) // valid + == QF_FAIL) { + got_int = true; + break; + } + found_match = true; + if (--*tomatch == 0) { + break; + } + if ((flags & VGR_GLOBAL) == 0) { + break; + } + col = (colnr_T)matches[pat_len - 1] + col + 1; + if (col > (colnr_T)STRLEN(str)) { + break; + } } } line_breakcheck(); @@ -5249,7 +5338,7 @@ static bool vgr_match_buflines(qf_list_T *qfl, char_u *fname, buf_T *buf, regmma } /// Jump to the first match and update the directory. -static void vgr_jump_to_match(qf_info_T *qi, int forceit, int *redraw_for_dummy, +static void vgr_jump_to_match(qf_info_T *qi, int forceit, bool *redraw_for_dummy, buf_T *first_match_buf, char_u *target_dir) { buf_T *buf = curbuf; @@ -5284,104 +5373,72 @@ static bool existing_swapfile(const buf_T *buf) return false; } -// ":vimgrep {pattern} file(s)" -// ":vimgrepadd {pattern} file(s)" -// ":lvimgrep {pattern} file(s)" -// ":lvimgrepadd {pattern} file(s)" -void ex_vimgrep(exarg_T *eap) +/// Process :vimgrep command arguments. The command syntax is: +/// +/// :{count}vimgrep /{pattern}/[g][j] {file} ... +static int vgr_process_args(exarg_T *eap, vgr_args_T *args) { - regmmatch_T regmatch; - int fcount; - char_u **fnames; - char_u *fname; - char_u *s; - char_u *p; - int fi; - qf_list_T *qfl; - win_T *wp = NULL; - buf_T *buf; - int duplicate_name = FALSE; - int using_dummy; - int redraw_for_dummy = FALSE; - int found_match; - buf_T *first_match_buf = NULL; - time_t seconds = 0; - aco_save_T aco; - int flags = 0; - long tomatch; - char_u *dirname_start = NULL; - char_u *dirname_now = NULL; - char_u *target_dir = NULL; - char_u *au_name = NULL; - - au_name = vgr_get_auname(eap->cmdidx); - if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, - curbuf->b_fname, true, curbuf)) { - if (aborting()) { - return; - } - } + memset(args, 0, sizeof(*args)); - qf_info_T *qi = qf_cmd_get_or_alloc_stack(eap, &wp); + args->regmatch.regprog = NULL; + args->qf_title = vim_strsave(qf_cmdtitle(*eap->cmdlinep)); if (eap->addr_count > 0) { - tomatch = eap->line2; + args->tomatch = eap->line2; } else { - tomatch = MAXLNUM; + args->tomatch = MAXLNUM; } // Get the search pattern: either white-separated or enclosed in // - regmatch.regprog = NULL; - char_u *title = vim_strsave(qf_cmdtitle(*eap->cmdlinep)); - p = skip_vimgrep_pat(eap->arg, &s, &flags); + char_u *p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags); if (p == NULL) { emsg(_(e_invalpat)); - goto theend; + return FAIL; } - vgr_init_regmatch(®match, s); - if (regmatch.regprog == NULL) { - goto theend; + vgr_init_regmatch(&args->regmatch, args->spat); + if (args->regmatch.regprog == NULL) { + return FAIL; } p = skipwhite(p); if (*p == NUL) { emsg(_("E683: File name missing or invalid pattern")); - goto theend; - } - - if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd - && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd) - || qf_stack_empty(qi)) { - // make place for a new list - qf_new_list(qi, title); + return FAIL; } // Parse the list of arguments, wildcards have already been expanded. - if (get_arglist_exp(p, &fcount, &fnames, true) == FAIL) { - goto theend; + if (get_arglist_exp(p, &args->fcount, &args->fnames, true) == FAIL) { + return FAIL; } - if (fcount == 0) { + if (args->fcount == 0) { emsg(_(e_nomatch)); - goto theend; + return FAIL; } - dirname_start = xmalloc(MAXPATHL); - dirname_now = xmalloc(MAXPATHL); + return OK; +} + +/// Search for a pattern in a list of files and populate the quickfix list with +/// the matches. +static int vgr_process_files(win_T *wp, qf_info_T *qi, vgr_args_T *cmd_args, + bool *redraw_for_dummy, buf_T **first_match_buf, + char_u **target_dir) +{ + int status = FAIL; + unsigned save_qfid = qf_get_curlist(qi)->qf_id; + bool duplicate_name = false; + + char_u *dirname_start = xmalloc(MAXPATHL); + char_u *dirname_now = xmalloc(MAXPATHL); // Remember the current directory, because a BufRead autocommand that does // ":lcd %:p:h" changes the meaning of short path names. os_dirname(dirname_start, MAXPATHL); - incr_quickfix_busy(); - - // Remember the current quickfix list identifier, so that we can check for - // autocommands changing the current quickfix list. - unsigned save_qfid = qf_get_curlist(qi)->qf_id; - - seconds = (time_t)0; - for (fi = 0; fi < fcount && !got_int && tomatch > 0; fi++) { - fname = path_try_shorten_fname(fnames[fi]); + time_t seconds = (time_t)0; + for (int fi = 0; fi < cmd_args->fcount && !got_int && cmd_args->tomatch > 0; fi++) { + char_u *fname = path_try_shorten_fname(cmd_args->fnames[fi]); if (time(NULL) > seconds) { // Display the file name every second or so, show the user we are // working on it. @@ -5389,13 +5446,13 @@ void ex_vimgrep(exarg_T *eap) vgr_display_fname(fname); } - buf = buflist_findname_exp(fnames[fi]); + buf_T *buf = buflist_findname_exp(cmd_args->fnames[fi]); + bool using_dummy; if (buf == NULL || buf->b_ml.ml_mfp == NULL) { // Remember that a buffer with this name already exists. duplicate_name = (buf != NULL); - using_dummy = TRUE; - redraw_for_dummy = TRUE; - + using_dummy = true; + *redraw_for_dummy = true; buf = vgr_load_dummy_buf(fname, dirname_start, dirname_now); } else { // Use existing, loaded buffer. @@ -5404,11 +5461,10 @@ void ex_vimgrep(exarg_T *eap) // Check whether the quickfix list is still valid. When loading a // buffer above, autocommands might have changed the quickfix list. - if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep)) { - FreeWild(fcount, fnames); - decr_quickfix_busy(); + if (!vgr_qflist_valid(wp, qi, save_qfid, cmd_args->qf_title)) { goto theend; } + save_qfid = qf_get_curlist(qi)->qf_id; if (buf == NULL) { @@ -5418,13 +5474,18 @@ void ex_vimgrep(exarg_T *eap) } else { // Try for a match in all lines of the buffer. // For ":1vimgrep" look for first match only. - found_match = vgr_match_buflines(qf_get_curlist(qi), - fname, buf, ®match, &tomatch, - duplicate_name, flags); + bool found_match = vgr_match_buflines(qf_get_curlist(qi), + fname, + buf, + cmd_args->spat, + &cmd_args->regmatch, + &cmd_args->tomatch, + duplicate_name, + cmd_args->flags); if (using_dummy) { - if (found_match && first_match_buf == NULL) { - first_match_buf = buf; + if (found_match && *first_match_buf == NULL) { + *first_match_buf = buf; } if (duplicate_name) { // Never keep a dummy buffer if there is another buffer @@ -5444,8 +5505,8 @@ void ex_vimgrep(exarg_T *eap) if (!found_match) { wipe_dummy_buffer(buf, dirname_start); buf = NULL; - } else if (buf != first_match_buf - || (flags & VGR_NOJUMP) + } else if (buf != *first_match_buf + || (cmd_args->flags & VGR_NOJUMP) || existing_swapfile(buf)) { unload_dummy_buffer(buf, dirname_start); // Keeping the buffer, remove the dummy flag. @@ -5460,16 +5521,17 @@ void ex_vimgrep(exarg_T *eap) // If the buffer is still loaded we need to use the // directory we jumped to below. - if (buf == first_match_buf - && target_dir == NULL + if (buf == *first_match_buf + && *target_dir == NULL && STRCMP(dirname_start, dirname_now) != 0) { - target_dir = vim_strsave(dirname_now); + *target_dir = vim_strsave(dirname_now); } // The buffer is still loaded, the Filetype autocommands // need to be done now, in that buffer. And the modelines // need to be done (again). But not the window-local // options! + aco_save_T aco; aucmd_prepbuf(&aco, buf); apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, buf->b_fname, true, buf); do_modelines(OPT_NOWIN); @@ -5479,9 +5541,58 @@ void ex_vimgrep(exarg_T *eap) } } - FreeWild(fcount, fnames); + status = OK; - qfl = qf_get_curlist(qi); +theend: + xfree(dirname_now); + xfree(dirname_start); + return status; +} + +/// ":vimgrep {pattern} file(s)" +/// ":vimgrepadd {pattern} file(s)" +/// ":lvimgrep {pattern} file(s)" +/// ":lvimgrepadd {pattern} file(s)" +void ex_vimgrep(exarg_T *eap) +{ + char_u *au_name = vgr_get_auname(eap->cmdidx); + if (au_name != NULL && apply_autocmds(EVENT_QUICKFIXCMDPRE, au_name, + curbuf->b_fname, true, curbuf)) { + if (aborting()) { + return; + } + } + + win_T *wp = NULL; + qf_info_T *qi = qf_cmd_get_or_alloc_stack(eap, &wp); + char_u *target_dir = NULL; + vgr_args_T args; + if (vgr_process_args(eap, &args) == FAIL) { + goto theend; + } + + if ((eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd + && eap->cmdidx != CMD_vimgrepadd && eap->cmdidx != CMD_lvimgrepadd) + || qf_stack_empty(qi)) { + // make place for a new list + qf_new_list(qi, args.qf_title); + } + + incr_quickfix_busy(); + + bool redraw_for_dummy = false; + buf_T *first_match_buf = NULL; + int status = vgr_process_files(wp, qi, &args, &redraw_for_dummy, &first_match_buf, &target_dir); + + if (status != OK) { + FreeWild(args.fcount, args.fnames); + decr_quickfix_busy(); + goto theend; + } + + FreeWild(args.fcount, args.fnames); + + qf_list_T *qfl = qf_get_curlist(qi); qfl->qf_nonevalid = false; qfl->qf_ptr = qfl->qf_start; qfl->qf_index = 1; @@ -5489,26 +5600,28 @@ void ex_vimgrep(exarg_T *eap) qf_update_buffer(qi, NULL); + // Remember the current quickfix list identifier, so that we can check for + // autocommands changing the current quickfix list. + unsigned save_qfid = qf_get_curlist(qi)->qf_id; + if (au_name != NULL) { apply_autocmds(EVENT_QUICKFIXCMDPOST, au_name, curbuf->b_fname, true, curbuf); } // The QuickFixCmdPost autocmd may free the quickfix list. Check the list // is still valid. - if (!qflist_valid(wp, save_qfid) - || qf_restore_list(qi, save_qfid) == FAIL) { + if (!qflist_valid(wp, save_qfid) || qf_restore_list(qi, save_qfid) == FAIL) { decr_quickfix_busy(); goto theend; } // Jump to first match. if (!qf_list_empty(qf_get_curlist(qi))) { - if ((flags & VGR_NOJUMP) == 0) { - vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy, first_match_buf, - target_dir); + if ((args.flags & VGR_NOJUMP) == 0) { + vgr_jump_to_match(qi, eap->forceit, &redraw_for_dummy, first_match_buf, target_dir); } } else { - semsg(_(e_nomatch2), s); + semsg(_(e_nomatch2), args.spat); } decr_quickfix_busy(); @@ -5520,11 +5633,9 @@ void ex_vimgrep(exarg_T *eap) } theend: - xfree(title); - xfree(dirname_now); - xfree(dirname_start); + xfree(args.qf_title); xfree(target_dir); - vim_regfree(regmatch.regprog); + vim_regfree(args.regmatch.regprog); } // Restore current working directory to "dirname_start" if they differ, taking @@ -5656,7 +5767,7 @@ static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start) if (firstwin->w_next != NULL) { for (win_T *wp = firstwin; wp != NULL; wp = wp->w_next) { if (wp->w_buffer == buf) { - if (win_close(wp, false) == OK) { + if (win_close(wp, false, false) == OK) { did_one = true; } break; diff --git a/src/nvim/quickfix.h b/src/nvim/quickfix.h index f5178e332a..0da43e436c 100644 --- a/src/nvim/quickfix.h +++ b/src/nvim/quickfix.h @@ -7,6 +7,7 @@ // flags for skip_vimgrep_pat() #define VGR_GLOBAL 1 #define VGR_NOJUMP 2 +#define VGR_FUZZY 4 #ifdef INCLUDE_GENERATED_DECLARATIONS # include "quickfix.h.generated.h" diff --git a/src/nvim/regexp.c b/src/nvim/regexp.c index 45e580dbee..6a6c915094 100644 --- a/src/nvim/regexp.c +++ b/src/nvim/regexp.c @@ -2901,18 +2901,14 @@ static int peekchr(void) { int c = regparse[1]; - if (c == NUL) - curchr = '\\'; /* trailing '\' */ - else if ( - c <= '~' && META_flags[c] - ) { - /* - * META contains everything that may be magic sometimes, - * except ^ and $ ("\^" and "\$" are only magic after - * "\V"). We now fetch the next character and toggle its - * magicness. Therefore, \ is so meta-magic that it is - * not in META. - */ + if (c == NUL) { + curchr = '\\'; // trailing '\' + } else if (c <= '~' && META_flags[c]) { + // META contains everything that may be magic sometimes, + // except ^ and $ ("\^" and "\$" are only magic after + // "\V"). We now fetch the next character and toggle its + // magicness. Therefore, \ is so meta-magic that it is + // not in META. curchr = -1; prev_at_start = at_start; at_start = false; // be able to say "/\*ptr" @@ -3232,7 +3228,7 @@ typedef struct { // The current match-position is remembered with these variables: linenr_T lnum; ///< line number, relative to first line char_u *line; ///< start of current line - char_u *input; ///< current input, points into "regline" + char_u *input; ///< current input, points into "line" int need_clear_subexpr; ///< subexpressions still need to be cleared int need_clear_zsubexpr; ///< extmatch subexpressions still need to be @@ -6538,11 +6534,16 @@ char_u *regtilde(char_u *source, int magic) } } - xfree(reg_prev_sub); - if (newsub != source) /* newsub was allocated, just keep it */ - reg_prev_sub = newsub; - else /* no ~ found, need to save newsub */ - reg_prev_sub = vim_strsave(newsub); + // Only change reg_prev_sub when not previewing. + if (!(State & CMDPREVIEW)) { + xfree(reg_prev_sub); + if (newsub != source) { // newsub was allocated, just keep it + reg_prev_sub = newsub; + } else { // no ~ found, need to save newsub + reg_prev_sub = vim_strsave(newsub); + } + } + return newsub; } diff --git a/src/nvim/regexp_nfa.c b/src/nvim/regexp_nfa.c index cafffc0319..133858f113 100644 --- a/src/nvim/regexp_nfa.c +++ b/src/nvim/regexp_nfa.c @@ -6071,8 +6071,15 @@ static int nfa_regmatch(nfa_regprog_T *prog, nfa_state_T *start, case NFA_MARK_GT: case NFA_MARK_LT: { + size_t col = rex.input - rex.line; pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, false); + // Line may have been freed, get it again. + if (REG_MULTI) { + rex.line = reg_getline(rex.lnum); + rex.input = rex.line + col; + } + // Compare the mark position to the match position, if the mark // exists and mark is set in reg_buf. if (pos != NULL && pos->lnum > 0) { diff --git a/src/nvim/screen.c b/src/nvim/screen.c index d4688f7782..15fb6901cc 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -794,7 +794,7 @@ static void win_update(win_T *wp, Providers *providers) // If we can compute a change in the automatic sizing of the sign column // under 'signcolumn=auto:X' and signs currently placed in the buffer, better // figuring it out here so we can redraw the entire screen for it. - buf_signcols(buf); + win_signcol_count(wp); type = wp->w_redr_type; @@ -1215,19 +1215,40 @@ static void win_update(win_T *wp, Providers *providers) */ if (VIsual_mode == Ctrl_V) { colnr_T fromc, toc; - int save_ve_flags = ve_flags; + unsigned int save_ve_flags = curwin->w_ve_flags; if (curwin->w_p_lbr) { - ve_flags = VE_ALL; + curwin->w_ve_flags = VE_ALL; } getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc); - ve_flags = save_ve_flags; toc++; + curwin->w_ve_flags = save_ve_flags; // Highlight to the end of the line, unless 'virtualedit' has // "block". - if (curwin->w_curswant == MAXCOL && !(ve_flags & VE_BLOCK)) { - toc = MAXCOL; + if (curwin->w_curswant == MAXCOL) { + if (get_ve_flags() & VE_BLOCK) { + pos_T pos; + int cursor_above = curwin->w_cursor.lnum < VIsual.lnum; + + // Need to find the longest line. + toc = 0; + pos.coladd = 0; + for (pos.lnum = curwin->w_cursor.lnum; + cursor_above ? pos.lnum <= VIsual.lnum : pos.lnum >= VIsual.lnum; + pos.lnum += cursor_above ? 1 : -1) { + colnr_T t; + + pos.col = STRLEN(ml_get_buf(wp->w_buffer, pos.lnum, false)); + getvvcol(wp, &pos, NULL, NULL, &t); + if (toc < t) { + toc = t; + } + } + toc++; + } else { + toc = MAXCOL; + } } if (fromc != wp->w_old_cursor_fcol @@ -1960,7 +1981,7 @@ static size_t fill_foldcolumn(char_u *p, win_T *wp, foldinfo_T foldinfo, linenr_ level = foldinfo.fi_level; // If the column is too narrow, we start at the lowest level that - // fits and use numbers to indicated the depth. + // fits and use numbers to indicate the depth. first_level = level - fdc - closed + 1; if (first_level < 1) { first_level = 1; @@ -3720,10 +3741,13 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc tab_len += n_extra - tab_len; } - // if n_extra > 0, it gives the number of chars + // If n_extra > 0, it gives the number of chars // to use for a tab, else we need to calculate the width - // for a tab + // for a tab. int len = (tab_len * utf_char2len(wp->w_p_lcs_chars.tab2)); + if (wp->w_p_lcs_chars.tab3) { + len += utf_char2len(wp->w_p_lcs_chars.tab3); + } if (n_extra > 0) { len += n_extra - tab_len; } @@ -3740,13 +3764,11 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc } int lcs = wp->w_p_lcs_chars.tab2; - // if tab3 is given, need to change the char - // for tab + // if tab3 is given, use it for the last char if (wp->w_p_lcs_chars.tab3 && i == tab_len - 1) { lcs = wp->w_p_lcs_chars.tab3; } - utf_char2bytes(lcs, p); - p += utf_char2len(lcs); + p += utf_char2bytes(lcs, p); n_extra += utf_char2len(lcs) - (saved_nextra > 0 ? 1 : 0); } p_extra = p_extra_free; @@ -4130,7 +4152,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc if (((wp->w_p_cuc && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off && (int)wp->w_virtcol < - grid->Columns * (row - startrow + 1) + v + (long)grid->Columns * (row - startrow + 1) + v && lnum != wp->w_cursor.lnum) || draw_color_col || line_attr_lowprio || line_attr || diff_hlf != (hlf_T)0 || has_virttext)) { @@ -4232,6 +4254,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc // Show "extends" character from 'listchars' if beyond the line end and // 'list' is set. if (wp->w_p_lcs_chars.ext != NUL + && draw_state == WL_LINE && wp->w_p_list && !wp->w_p_wrap && filler_todo <= 0 @@ -4427,7 +4450,8 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc */ if ((wp->w_p_rl ? (col < 0) : (col >= grid->Columns)) && foldinfo.fi_lines == 0 - && (*ptr != NUL + && (draw_state != WL_LINE + || *ptr != NUL || filler_todo > 0 || (wp->w_p_list && wp->w_p_lcs_chars.eol != NUL && p_extra != at_end_str) @@ -6762,7 +6786,7 @@ void grid_clear_line(ScreenGrid *grid, unsigned off, int width, bool valid) void grid_invalidate(ScreenGrid *grid) { - (void)memset(grid->attrs, -1, grid->Rows * grid->Columns * sizeof(sattr_T)); + (void)memset(grid->attrs, -1, sizeof(sattr_T) * grid->Rows * grid->Columns); } bool grid_invalid_row(ScreenGrid *grid, int row) @@ -6891,8 +6915,6 @@ void grid_ins_lines(ScreenGrid *grid, int row, int line_count, int end, int col, if (!grid->throttled) { ui_call_grid_scroll(grid->handle, row, end, col, col+width, -line_count, 0); } - - return; } /// delete lines on the screen and move lines up. @@ -6943,8 +6965,6 @@ void grid_del_lines(ScreenGrid *grid, int row, int line_count, int end, int col, if (!grid->throttled) { ui_call_grid_scroll(grid->handle, row, end, col, col+width, line_count, 0); } - - return; } @@ -7326,7 +7346,7 @@ void draw_tabline(void) if (room > 0) { // Get buffer name in NameBuff[] get_trans_bufname(cwp->w_buffer); - (void)shorten_dir(NameBuff); + shorten_dir(NameBuff); len = vim_strsize(NameBuff); p = NameBuff; while (len > room) { diff --git a/src/nvim/search.c b/src/nvim/search.c index 906c9a6f47..682fa417a9 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -26,6 +26,7 @@ #include "nvim/func_attr.h" #include "nvim/getchar.h" #include "nvim/indent.h" +#include "nvim/indent_c.h" #include "nvim/main.h" #include "nvim/mark.h" #include "nvim/mbyte.h" @@ -2313,12 +2314,9 @@ pos_T *findmatchlimit(oparg_T *oap, int initc, int flags, int64_t maxtravel) return (pos_T *)NULL; // never found it } -/* - * Check if line[] contains a / / comment. - * Return MAXCOL if not, otherwise return the column. - * TODO: skip strings. - */ -static int check_linecomment(const char_u *line) +/// Check if line[] contains a / / comment. +/// @returns MAXCOL if not, otherwise return the column. +int check_linecomment(const char_u *line) { const char_u *p = line; // scan from start // skip Lispish one-line comments @@ -2338,7 +2336,8 @@ static int check_linecomment(const char_u *line) in_str = true; } } else if (!in_str && ((p - line) < 2 - || (*(p - 1) != '\\' && *(p - 2) != '#'))) { + || (*(p - 1) != '\\' && *(p - 2) != '#')) + && !is_pos_in_string(line, (colnr_T)(p - line))) { break; // found! } p++; @@ -2348,9 +2347,11 @@ static int check_linecomment(const char_u *line) } } else { while ((p = vim_strchr(p, '/')) != NULL) { - // accept a double /, unless it's preceded with * and followed by *, - // because * / / * is an end and start of a C comment - if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')) { + // Accept a double /, unless it's preceded with * and followed by *, + // because * / / * is an end and start of a C comment. + // Only accept the position if it is not inside a string. + if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*') + && !is_pos_in_string(line, (colnr_T)(p - line))) { break; } ++p; @@ -4763,6 +4764,536 @@ the_end: restore_last_search_pattern(); } +/// Fuzzy string matching +/// +/// Ported from the lib_fts library authored by Forrest Smith. +/// https://github.com/forrestthewoods/lib_fts/tree/master/code +/// +/// The following blog describes the fuzzy matching algorithm: +/// https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/ +/// +/// Each matching string is assigned a score. The following factors are checked: +/// - Matched letter +/// - Unmatched letter +/// - Consecutively matched letters +/// - Proximity to start +/// - Letter following a separator (space, underscore) +/// - Uppercase letter following lowercase (aka CamelCase) +/// +/// Matched letters are good. Unmatched letters are bad. Matching near the start +/// is good. Matching the first letter in the middle of a phrase is good. +/// Matching the uppercase letters in camel case entries is good. +/// +/// The score assigned for each factor is explained below. +/// File paths are different from file names. File extensions may be ignorable. +/// Single words care about consecutive matches but not separators or camel +/// case. +/// Score starts at 100 +/// Matched letter: +0 points +/// Unmatched letter: -1 point +/// Consecutive match bonus: +15 points +/// First letter bonus: +15 points +/// Separator bonus: +30 points +/// Camel case bonus: +30 points +/// Unmatched leading letter: -5 points (max: -15) +/// +/// There is some nuance to this. Scores don’t have an intrinsic meaning. The +/// score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a +/// lower minimum score due to unmatched letter penalty. Longer search patterns +/// have a higher maximum score due to match bonuses. +/// +/// Separator and camel case bonus is worth a LOT. Consecutive matches are worth +/// quite a bit. +/// +/// There is a penalty if you DON’T match the first three letters. Which +/// effectively rewards matching near the start. However there’s no difference +/// in matching between the middle and end. +/// +/// There is not an explicit bonus for an exact match. Unmatched letters receive +/// a penalty. So shorter strings and closer matches are worth more. +typedef struct { + int idx; ///< used for stable sort + listitem_T *item; + int score; + list_T *lmatchpos; +} fuzzyItem_T; + +/// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that +/// matching a whole word is preferred. +#define SEQUENTIAL_BONUS 40 +/// bonus if match occurs after a path separator +#define PATH_SEPARATOR_BONUS 30 +/// bonus if match occurs after a word separator +#define WORD_SEPARATOR_BONUS 25 +/// bonus if match is uppercase and prev is lower +#define CAMEL_BONUS 30 +/// bonus if the first letter is matched +#define FIRST_LETTER_BONUS 15 +/// penalty applied for every letter in str before the first match +#define LEADING_LETTER_PENALTY -5 +/// maximum penalty for leading letters +#define MAX_LEADING_LETTER_PENALTY -15 +/// penalty for every letter that doesn't match +#define UNMATCHED_LETTER_PENALTY -1 +/// penalty for gap in matching positions (-2 * k) +#define GAP_PENALTY -2 +/// Score for a string that doesn't fuzzy match the pattern +#define SCORE_NONE -9999 + +#define FUZZY_MATCH_RECURSION_LIMIT 10 + +/// Compute a score for a fuzzy matched string. The matching character locations +/// are in 'matches'. +static int fuzzy_match_compute_score(const char_u *const str, const int strSz, + const uint32_t *const matches, const int numMatches) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE +{ + assert(numMatches > 0); // suppress clang "result of operation is garbage" + // Initialize score + int score = 100; + + // Apply leading letter penalty + int penalty = LEADING_LETTER_PENALTY * matches[0]; + if (penalty < MAX_LEADING_LETTER_PENALTY) { + penalty = MAX_LEADING_LETTER_PENALTY; + } + score += penalty; + + // Apply unmatched penalty + const int unmatched = strSz - numMatches; + score += UNMATCHED_LETTER_PENALTY * unmatched; + + // Apply ordering bonuses + for (int i = 0; i < numMatches; i++) { + const uint32_t currIdx = matches[i]; + + if (i > 0) { + const uint32_t prevIdx = matches[i - 1]; + + // Sequential + if (currIdx == prevIdx + 1) { + score += SEQUENTIAL_BONUS; + } else { + score += GAP_PENALTY * (currIdx - prevIdx); + } + } + + // Check for bonuses based on neighbor character value + if (currIdx > 0) { + // Camel case + const char_u *p = str; + int neighbor; + + for (uint32_t sidx = 0; sidx < currIdx; sidx++) { + neighbor = utf_ptr2char(p); + MB_PTR_ADV(p); + } + const int curr = utf_ptr2char(p); + + if (mb_islower(neighbor) && mb_isupper(curr)) { + score += CAMEL_BONUS; + } + + // Bonus if the match follows a separator character + if (neighbor == '/' || neighbor == '\\') { + score += PATH_SEPARATOR_BONUS; + } else if (neighbor == ' ' || neighbor == '_') { + score += WORD_SEPARATOR_BONUS; + } + } else { + // First letter + score += FIRST_LETTER_BONUS; + } + } + return score; +} + +/// Perform a recursive search for fuzzy matching 'fuzpat' in 'str'. +/// @return the number of matching characters. +static int fuzzy_match_recursive(const char_u *fuzpat, const char_u *str, uint32_t strIdx, + int *const outScore, const char_u *const strBegin, + const int strLen, const uint32_t *const srcMatches, + uint32_t *const matches, const int maxMatches, int nextMatch, + int *const recursionCount) + FUNC_ATTR_NONNULL_ARG(1, 2, 4, 5, 8, 11) FUNC_ATTR_WARN_UNUSED_RESULT +{ + // Recursion params + bool recursiveMatch = false; + uint32_t bestRecursiveMatches[MAX_FUZZY_MATCHES]; + int bestRecursiveScore = 0; + + // Count recursions + (*recursionCount)++; + if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT) { + return 0; + } + + // Detect end of strings + if (*fuzpat == NUL || *str == NUL) { + return 0; + } + + // Loop through fuzpat and str looking for a match + bool first_match = true; + while (*fuzpat != NUL && *str != NUL) { + const int c1 = utf_ptr2char(fuzpat); + const int c2 = utf_ptr2char(str); + + // Found match + if (mb_tolower(c1) == mb_tolower(c2)) { + // Supplied matches buffer was too short + if (nextMatch >= maxMatches) { + return 0; + } + + // "Copy-on-Write" srcMatches into matches + if (first_match && srcMatches != NULL) { + memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0])); + first_match = false; + } + + // Recursive call that "skips" this match + uint32_t recursiveMatches[MAX_FUZZY_MATCHES]; + int recursiveScore = 0; + const char_u *const next_char = str + utfc_ptr2len(str); + if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1, &recursiveScore, strBegin, strLen, + matches, recursiveMatches, + sizeof(recursiveMatches) / sizeof(recursiveMatches[0]), nextMatch, + recursionCount)) { + // Pick best recursive score + if (!recursiveMatch || recursiveScore > bestRecursiveScore) { + memcpy(bestRecursiveMatches, recursiveMatches, + MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0])); + bestRecursiveScore = recursiveScore; + } + recursiveMatch = true; + } + + // Advance + matches[nextMatch++] = strIdx; + MB_PTR_ADV(fuzpat); + } + MB_PTR_ADV(str); + strIdx++; + } + + // Determine if full fuzpat was matched + const bool matched = *fuzpat == NUL; + + // Calculate score + if (matched) { + *outScore = fuzzy_match_compute_score(strBegin, strLen, matches, nextMatch); + } + + // Return best result + if (recursiveMatch && (!matched || bestRecursiveScore > *outScore)) { + // Recursive score is better than "this" + memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0])); + *outScore = bestRecursiveScore; + return nextMatch; + } else if (matched) { + return nextMatch; // "this" score is better than recursive + } + + return 0; // no match +} + +/// fuzzy_match() +/// +/// Performs exhaustive search via recursion to find all possible matches and +/// match with highest score. +/// Scores values have no intrinsic meaning. Possible score range is not +/// normalized and varies with pattern. +/// Recursion is limited internally (default=10) to prevent degenerate cases +/// (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"). +/// Uses char_u for match indices. Therefore patterns are limited to +/// MAX_FUZZY_MATCHES characters. +/// +/// @return true if 'pat_arg' matches 'str'. Also returns the match score in +/// 'outScore' and the matching character positions in 'matches'. +bool fuzzy_match(char_u *const str, const char_u *const pat_arg, const bool matchseq, + int *const outScore, uint32_t *const matches, const int maxMatches) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + const int len = mb_charlen(str); + bool complete = false; + int numMatches = 0; + + *outScore = 0; + + char_u *const save_pat = vim_strsave(pat_arg); + char_u *pat = save_pat; + char_u *p = pat; + + // Try matching each word in 'pat_arg' in 'str' + while (true) { + if (matchseq) { + complete = true; + } else { + // Extract one word from the pattern (separated by space) + p = skipwhite(p); + if (*p == NUL) { + break; + } + pat = p; + while (*p != NUL && !ascii_iswhite(utf_ptr2char(p))) { + MB_PTR_ADV(p); + } + if (*p == NUL) { // processed all the words + complete = true; + } + *p = NUL; + } + + int score = 0; + int recursionCount = 0; + const int matchCount + = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL, matches + numMatches, + maxMatches - numMatches, 0, &recursionCount); + if (matchCount == 0) { + numMatches = 0; + break; + } + + // Accumulate the match score and the number of matches + *outScore += score; + numMatches += matchCount; + + if (complete) { + break; + } + + // try matching the next word + p++; + } + + xfree(save_pat); + return numMatches != 0; +} + +/// Sort the fuzzy matches in the descending order of the match score. +/// For items with same score, retain the order using the index (stable sort) +static int fuzzy_match_item_compare(const void *const s1, const void *const s2) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE +{ + const int v1 = ((const fuzzyItem_T *)s1)->score; + const int v2 = ((const fuzzyItem_T *)s2)->score; + const int idx1 = ((const fuzzyItem_T *)s1)->idx; + const int idx2 = ((const fuzzyItem_T *)s2)->idx; + + return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1; +} + +/// Fuzzy search the string 'str' in a list of 'items' and return the matching +/// strings in 'fmatchlist'. +/// If 'matchseq' is true, then for multi-word search strings, match all the +/// words in sequence. +/// If 'items' is a list of strings, then search for 'str' in the list. +/// If 'items' is a list of dicts, then either use 'key' to lookup the string +/// for each item or use 'item_cb' Funcref function to get the string. +/// If 'retmatchpos' is true, then return a list of positions where 'str' +/// matches for each item. +static void fuzzy_match_in_list(list_T *const items, char_u *const str, const bool matchseq, + const char_u *const key, Callback *const item_cb, + const bool retmatchpos, list_T *const fmatchlist) + FUNC_ATTR_NONNULL_ARG(2, 5, 7) +{ + const long len = tv_list_len(items); + if (len == 0) { + return; + } + + fuzzyItem_T *const ptrs = xcalloc(len, sizeof(fuzzyItem_T)); + long i = 0; + bool found_match = false; + uint32_t matches[MAX_FUZZY_MATCHES]; + + // For all the string items in items, get the fuzzy matching score + TV_LIST_ITER(items, li, { + ptrs[i].idx = i; + ptrs[i].item = li; + ptrs[i].score = SCORE_NONE; + char_u *itemstr = NULL; + typval_T rettv; + rettv.v_type = VAR_UNKNOWN; + const typval_T *const tv = TV_LIST_ITEM_TV(li); + if (tv->v_type == VAR_STRING) { // list of strings + itemstr = tv->vval.v_string; + } else if (tv->v_type == VAR_DICT && (key != NULL || item_cb->type != kCallbackNone)) { + // For a dict, either use the specified key to lookup the string or + // use the specified callback function to get the string. + if (key != NULL) { + itemstr = (char_u *)tv_dict_get_string(tv->vval.v_dict, (const char *)key, false); + } else { + typval_T argv[2]; + + // Invoke the supplied callback (if any) to get the dict item + tv->vval.v_dict->dv_refcount++; + argv[0].v_type = VAR_DICT; + argv[0].vval.v_dict = tv->vval.v_dict; + argv[1].v_type = VAR_UNKNOWN; + if (callback_call(item_cb, 1, argv, &rettv)) { + if (rettv.v_type == VAR_STRING) { + itemstr = rettv.vval.v_string; + } + } + tv_dict_unref(tv->vval.v_dict); + } + } + + int score; + if (itemstr != NULL && fuzzy_match(itemstr, str, matchseq, &score, matches, + sizeof(matches) / sizeof(matches[0]))) { + // Copy the list of matching positions in itemstr to a list, if + // 'retmatchpos' is set. + if (retmatchpos) { + ptrs[i].lmatchpos = tv_list_alloc(kListLenMayKnow); + int j = 0; + const char_u *p = str; + while (*p != NUL) { + if (!ascii_iswhite(utf_ptr2char(p))) { + tv_list_append_number(ptrs[i].lmatchpos, matches[j]); + j++; + } + MB_PTR_ADV(p); + } + } + ptrs[i].score = score; + found_match = true; + } + i++; + tv_clear(&rettv); + }); + + if (found_match) { + // Sort the list by the descending order of the match score + qsort(ptrs, len, sizeof(fuzzyItem_T), fuzzy_match_item_compare); + + // For matchfuzzy(), return a list of matched strings. + // ['str1', 'str2', 'str3'] + // For matchfuzzypos(), return a list with three items. + // The first item is a list of matched strings. The second item + // is a list of lists where each list item is a list of matched + // character positions. The third item is a list of matching scores. + // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]] + list_T *l; + if (retmatchpos) { + const listitem_T *const li = tv_list_find(fmatchlist, 0); + assert(li != NULL && TV_LIST_ITEM_TV(li)->vval.v_list != NULL); + l = TV_LIST_ITEM_TV(li)->vval.v_list; + } else { + l = fmatchlist; + } + + // Copy the matching strings with a valid score to the return list + for (i = 0; i < len; i++) { + if (ptrs[i].score == SCORE_NONE) { + break; + } + tv_list_append_tv(l, TV_LIST_ITEM_TV(ptrs[i].item)); + } + + // next copy the list of matching positions + if (retmatchpos) { + const listitem_T *li = tv_list_find(fmatchlist, -2); + assert(li != NULL && TV_LIST_ITEM_TV(li)->vval.v_list != NULL); + l = TV_LIST_ITEM_TV(li)->vval.v_list; + for (i = 0; i < len; i++) { + if (ptrs[i].score == SCORE_NONE) { + break; + } + tv_list_append_list(l, ptrs[i].lmatchpos); + } + + // copy the matching scores + li = tv_list_find(fmatchlist, -1); + assert(li != NULL && TV_LIST_ITEM_TV(li)->vval.v_list != NULL); + l = TV_LIST_ITEM_TV(li)->vval.v_list; + for (i = 0; i < len; i++) { + if (ptrs[i].score == SCORE_NONE) { + break; + } + tv_list_append_number(l, ptrs[i].score); + } + } + } + xfree(ptrs); +} + +/// Do fuzzy matching. Returns the list of matched strings in 'rettv'. +/// If 'retmatchpos' is true, also returns the matching character positions. +static void do_fuzzymatch(const typval_T *const argvars, typval_T *const rettv, + const bool retmatchpos) + FUNC_ATTR_NONNULL_ALL +{ + // validate and get the arguments + if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL) { + semsg(_(e_listarg), retmatchpos ? "matchfuzzypos()" : "matchfuzzy()"); + return; + } + if (argvars[1].v_type != VAR_STRING || argvars[1].vval.v_string == NULL) { + semsg(_(e_invarg2), tv_get_string(&argvars[1])); + return; + } + + Callback cb = CALLBACK_NONE; + const char_u *key = NULL; + bool matchseq = false; + if (argvars[2].v_type != VAR_UNKNOWN) { + if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL) { + emsg(_(e_dictreq)); + return; + } + + // To search a dict, either a callback function or a key can be + // specified. + dict_T *const d = argvars[2].vval.v_dict; + const dictitem_T *const di = tv_dict_find(d, "key", -1); + if (di != NULL) { + if (di->di_tv.v_type != VAR_STRING || di->di_tv.vval.v_string == NULL + || *di->di_tv.vval.v_string == NUL) { + semsg(_(e_invarg2), tv_get_string(&di->di_tv)); + return; + } + key = (const char_u *)tv_get_string(&di->di_tv); + } else if (!tv_dict_get_callback(d, "text_cb", -1, &cb)) { + semsg(_(e_invargval), "text_cb"); + return; + } + if (tv_dict_find(d, "matchseq", -1) != NULL) { + matchseq = true; + } + } + + // get the fuzzy matches + tv_list_alloc_ret(rettv, retmatchpos ? 3 : kListLenUnknown); + if (retmatchpos) { + // For matchfuzzypos(), a list with three items are returned. First + // item is a list of matching strings, the second item is a list of + // lists with matching positions within each string and the third item + // is the list of scores of the matches. + tv_list_append_list(rettv->vval.v_list, tv_list_alloc(kListLenUnknown)); + tv_list_append_list(rettv->vval.v_list, tv_list_alloc(kListLenUnknown)); + tv_list_append_list(rettv->vval.v_list, tv_list_alloc(kListLenUnknown)); + } + + fuzzy_match_in_list(argvars[0].vval.v_list, (char_u *)tv_get_string(&argvars[1]), matchseq, key, + &cb, retmatchpos, rettv->vval.v_list); + callback_free(&cb); +} + +/// "matchfuzzy()" function +void f_matchfuzzy(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + do_fuzzymatch(argvars, rettv, false); +} + +/// "matchfuzzypos()" function +void f_matchfuzzypos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + do_fuzzymatch(argvars, rettv, true); +} + /// Find identifiers or defines in included files. /// If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase. /// diff --git a/src/nvim/search.h b/src/nvim/search.h index 15b8d41f39..53059cc1ea 100644 --- a/src/nvim/search.h +++ b/src/nvim/search.h @@ -55,6 +55,9 @@ #define SEARCH_STAT_DEF_MAX_COUNT 99 #define SEARCH_STAT_BUF_LEN 12 +/// Maximum number of characters that can be fuzzy matched +#define MAX_FUZZY_MATCHES 256 + /// Structure containing offset definition for the last search pattern /// /// @note Only offset for the last search pattern is used, not for the last diff --git a/src/nvim/sign.c b/src/nvim/sign.c index a308df07d1..8b41781c98 100644 --- a/src/nvim/sign.c +++ b/src/nvim/sign.c @@ -1501,28 +1501,28 @@ static void sign_getinfo(sign_T *sp, dict_T *retdict) if (p == NULL) { p = "NONE"; } - tv_dict_add_str(retdict, S_LEN("linehl"), (char *)p); + tv_dict_add_str(retdict, S_LEN("linehl"), p); } if (sp->sn_text_hl > 0) { p = get_highlight_name_ext(NULL, sp->sn_text_hl - 1, false); if (p == NULL) { p = "NONE"; } - tv_dict_add_str(retdict, S_LEN("texthl"), (char *)p); + tv_dict_add_str(retdict, S_LEN("texthl"), p); } if (sp->sn_cul_hl > 0) { p = get_highlight_name_ext(NULL, sp->sn_cul_hl - 1, false); if (p == NULL) { p = "NONE"; } - tv_dict_add_str(retdict, S_LEN("culhl"), (char *)p); + tv_dict_add_str(retdict, S_LEN("culhl"), p); } if (sp->sn_num_hl > 0) { p = get_highlight_name_ext(NULL, sp->sn_num_hl - 1, false); if (p == NULL) { p = "NONE"; } - tv_dict_add_str(retdict, S_LEN("numhl"), (char *)p); + tv_dict_add_str(retdict, S_LEN("numhl"), p); } } diff --git a/src/nvim/spell.c b/src/nvim/spell.c index 9429a06e92..1296d410f6 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -4082,7 +4082,7 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, bool so // char, e.g., "thes," -> "these". p = fword + sp->ts_fidx; MB_PTR_BACK(fword, p); - if (!spell_iswordp(p, curwin)) { + if (!spell_iswordp(p, curwin) && *preword != NUL) { p = preword + STRLEN(preword); MB_PTR_BACK(preword, p); if (spell_iswordp(p, curwin)) { diff --git a/src/nvim/state.c b/src/nvim/state.c index 68bc76660d..f9a3aaab7f 100644 --- a/src/nvim/state.c +++ b/src/nvim/state.c @@ -12,6 +12,7 @@ #include "nvim/lib/kvec.h" #include "nvim/log.h" #include "nvim/main.h" +#include "nvim/option.h" #include "nvim/option_defs.h" #include "nvim/os/input.h" #include "nvim/state.h" @@ -38,10 +39,16 @@ void state_enter(VimState *s) int key; getkey: - if (char_avail() || using_script() || input_available()) { - // Don't block for events if there's a character already available for - // processing. Characters can come from mappings, scripts and other - // sources, so this scenario is very common. + // Expand mappings first by calling vpeekc() directly. + // - If vpeekc() returns non-NUL, there is a character already available for processing, so + // don't block for events. vgetc() may still block, in case of an incomplete UTF-8 sequence. + // - If vpeekc() returns NUL, vgetc() will block, and there are three cases: + // - There is no input available. + // - All of available input maps to an empty string. + // - There is an incomplete mapping. + // A blocking wait for a character should only be done in the third case, which is the only + // case of the three where typebuf.tb_len > 0 after vpeekc() returns NUL. + if (vpeekc() != NUL || typebuf.tb_len > 0) { key = safe_vgetc(); } else if (!multiqueue_empty(main_loop.events)) { // Event was made available after the last multiqueue_process_events call @@ -54,9 +61,11 @@ getkey: // mapping engine. (void)os_inchar(NULL, 0, -1, 0, main_loop.events); // If an event was put into the queue, we send K_EVENT directly. - key = !multiqueue_empty(main_loop.events) - ? K_EVENT - : safe_vgetc(); + if (!multiqueue_empty(main_loop.events)) { + key = K_EVENT; + } else { + goto getkey; + } } if (key == K_EVENT) { @@ -107,15 +116,17 @@ void state_handle_k_event(void) /// Return true if in the current mode we need to use virtual. bool virtual_active(void) { + unsigned int cur_ve_flags = get_ve_flags(); + // While an operator is being executed we return "virtual_op", because // VIsual_active has already been reset, thus we can't check for "block" // being used. if (virtual_op != kNone) { return virtual_op; } - return ve_flags == VE_ALL - || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V) - || ((ve_flags & VE_INSERT) && (State & INSERT)); + return cur_ve_flags == VE_ALL + || ((cur_ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V) + || ((cur_ve_flags & VE_INSERT) && (State & INSERT)); } /// VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to @@ -180,7 +191,7 @@ char *get_mode(void) buf[1] = 'x'; } } - } else if (State & CMDLINE) { + } else if ((State & CMDLINE) || exmode_active) { buf[0] = 'c'; if (exmode_active) { buf[1] = 'v'; diff --git a/src/nvim/strings.c b/src/nvim/strings.c index e2a8108c45..291138ef23 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -1001,22 +1001,20 @@ int vim_vsnprintf_typval(char *str, size_t str_m, const char *fmt, va_list ap, t - str_arg); } if (fmt_spec == 'S') { - if (min_field_width != 0) { - min_field_width += (strlen(str_arg) - - mb_string2cells((char_u *)str_arg)); - } - if (precision) { - char_u *p1; - size_t i = 0; - - for (p1 = (char_u *)str_arg; *p1; - p1 += utfc_ptr2len(p1)) { - i += (size_t)utf_ptr2cells(p1); - if (i > precision) { - break; - } + char_u *p1; + size_t i; + + for (i = 0, p1 = (char_u *)str_arg; *p1; p1 += utfc_ptr2len(p1)) { + size_t cell = (size_t)utf_ptr2cells(p1); + if (precision_specified && i + cell > precision) { + break; } - str_arg_l = (size_t)(p1 - (char_u *)str_arg); + i += cell; + } + + str_arg_l = (size_t)(p1 - (char_u *)str_arg); + if (min_field_width != 0) { + min_field_width += str_arg_l - i; } } break; diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index a9447165c2..119f6e811f 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -3112,9 +3112,9 @@ static void syn_cmd_conceal(exarg_T *eap, int syncing) next = skiptowhite(arg); if (*arg == NUL) { if (curwin->w_s->b_syn_conceal) { - msg(_("syntax conceal on")); + msg("syntax conceal on"); } else { - msg(_("syntax conceal off")); + msg("syntax conceal off"); } } else if (STRNICMP(arg, "on", 2) == 0 && next - arg == 2) { curwin->w_s->b_syn_conceal = true; @@ -3141,9 +3141,9 @@ static void syn_cmd_case(exarg_T *eap, int syncing) next = skiptowhite(arg); if (*arg == NUL) { if (curwin->w_s->b_syn_ic) { - msg(_("syntax case ignore")); + msg("syntax case ignore"); } else { - msg(_("syntax case match")); + msg("syntax case match"); } } else if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5) { curwin->w_s->b_syn_ic = false; @@ -3168,9 +3168,9 @@ static void syn_cmd_foldlevel(exarg_T *eap, int syncing) if (*arg == NUL) { switch (curwin->w_s->b_syn_foldlevel) { case SYNFLD_START: - msg(_("syntax foldlevel start")); break; + msg("syntax foldlevel start"); break; case SYNFLD_MINIMUM: - msg(_("syntax foldlevel minimum")); break; + msg("syntax foldlevel minimum"); break; default: break; } @@ -3209,11 +3209,11 @@ static void syn_cmd_spell(exarg_T *eap, int syncing) next = skiptowhite(arg); if (*arg == NUL) { if (curwin->w_s->b_syn_spell == SYNSPL_TOP) { - msg(_("syntax spell toplevel")); + msg("syntax spell toplevel"); } else if (curwin->w_s->b_syn_spell == SYNSPL_NOTOP) { - msg(_("syntax spell notoplevel")); + msg("syntax spell notoplevel"); } else { - msg(_("syntax spell default")); + msg("syntax spell default"); } } else if (STRNICMP(arg, "toplevel", 8) == 0 && next - arg == 8) { curwin->w_s->b_syn_spell = SYNSPL_TOP; @@ -3245,7 +3245,7 @@ static void syn_cmd_iskeyword(exarg_T *eap, int syncing) if (*arg == NUL) { msg_puts("\n"); if (curwin->w_s->b_syn_isk != empty_option) { - msg_puts(_("syntax iskeyword ")); + msg_puts("syntax iskeyword "); msg_outtrans(curwin->w_s->b_syn_isk); } else { msg_outtrans((char_u *)_("syntax iskeyword not set")); @@ -6714,6 +6714,90 @@ int lookup_color(const int idx, const bool foreground, TriState *const boldp) return color; } +void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) +{ + int idx = id - 1; // Index is ID minus one. + + bool is_default = attrs.rgb_ae_attr & HL_DEFAULT; + + // Return if "default" was used and the group already has settings + if (is_default && hl_has_settings(idx, true)) { + return; + } + + HlGroup *g = &HL_TABLE()[idx]; + + if (link_id > 0) { + g->sg_cleared = false; + g->sg_link = link_id; + g->sg_script_ctx = current_sctx; + g->sg_script_ctx.sc_lnum += sourcing_lnum; + g->sg_set |= SG_LINK; + if (is_default) { + g->sg_deflink = link_id; + g->sg_deflink_sctx = current_sctx; + g->sg_deflink_sctx.sc_lnum += sourcing_lnum; + } + return; + } + + g->sg_cleared = false; + g->sg_link = 0; + g->sg_gui = attrs.rgb_ae_attr; + + g->sg_rgb_fg = attrs.rgb_fg_color; + g->sg_rgb_bg = attrs.rgb_bg_color; + g->sg_rgb_sp = attrs.rgb_sp_color; + + struct { + char **dest; RgbValue val; Object name; + } cattrs[] = { + { &g->sg_rgb_fg_name, g->sg_rgb_fg, HAS_KEY(dict->fg) ? dict->fg : dict->foreground }, + { &g->sg_rgb_bg_name, g->sg_rgb_bg, HAS_KEY(dict->bg) ? dict->bg : dict->background }, + { &g->sg_rgb_sp_name, g->sg_rgb_sp, HAS_KEY(dict->sp) ? dict->sp : dict->special }, + { NULL, -1, NIL }, + }; + + for (int j = 0; cattrs[j].dest; j++) { + if (cattrs[j].val != -1) { + xfree(*cattrs[j].dest); + if (cattrs[j].name.type == kObjectTypeString && cattrs[j].name.data.string.size) { + *cattrs[j].dest = xstrdup(cattrs[j].name.data.string.data); + } else { + char hex_name[8]; + snprintf(hex_name, sizeof(hex_name), "#%06x", cattrs[j].val); + *cattrs[j].dest = xstrdup(hex_name); + } + } + } + + g->sg_cterm = attrs.cterm_ae_attr; + g->sg_cterm_bg = attrs.cterm_bg_color; + g->sg_cterm_fg = attrs.cterm_fg_color; + g->sg_cterm_bold = g->sg_cterm & HL_BOLD; + g->sg_blend = attrs.hl_blend; + + g->sg_script_ctx = current_sctx; + g->sg_script_ctx.sc_lnum += sourcing_lnum; + + // 'Normal' is special + if (STRCMP(g->sg_name_u, "NORMAL") == 0) { + cterm_normal_fg_color = g->sg_cterm_fg; + cterm_normal_bg_color = g->sg_cterm_bg; + normal_fg = g->sg_rgb_fg; + normal_bg = g->sg_rgb_bg; + normal_sp = g->sg_rgb_sp; + ui_default_colors_set(); + } else { + g->sg_attr = hl_get_syn_attr(0, id, attrs); + + // a cursor style uses this syn_id, make sure its attribute is updated. + if (cursor_mode_uses_syn_id(id)) { + ui_mode_info_set(); + } + } +} + /// Handle ":highlight" command /// diff --git a/src/nvim/tag.c b/src/nvim/tag.c index a10a2a0c32..32d72218c8 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -2960,7 +2960,7 @@ static int jumpto_tag(const char_u *lbuf_arg, int forceit, int keep_help) } else { RedrawingDisabled--; if (postponed_split) { // close the window - win_close(curwin, false); + win_close(curwin, false, false); postponed_split = 0; } } @@ -3408,7 +3408,7 @@ static void tagstack_push_items(win_T *wp, list_T *l) if ((di = tv_dict_find(itemdict, "from", -1)) == NULL) { continue; } - if (list2fpos(&di->di_tv, &mark, &fnum, NULL) != OK) { + if (list2fpos(&di->di_tv, &mark, &fnum, NULL, false) != OK) { continue; } if ((tagname = (char_u *)tv_dict_get_string(itemdict, "tagname", true)) diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 70a5c7aa08..1c26e46a21 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -172,6 +172,11 @@ void terminal_teardown(void) pmap_init(ptr_t, &invalidated_terminals); } +static void term_output_callback(const char *s, size_t len, void *user_data) +{ + terminal_send((Terminal *)user_data, (char *)s, len); +} + // public API {{{ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) @@ -195,6 +200,7 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) vterm_screen_set_callbacks(rv->vts, &vterm_screen_callbacks, rv); vterm_screen_set_damage_merge(rv->vts, VTERM_DAMAGE_SCROLL); vterm_screen_reset(rv->vts, 1); + vterm_output_set_callback(rv->vt, term_output_callback, rv); // force a initial refresh of the screen to ensure the buffer will always // have as many lines as screen rows when refresh_scrollback is called rv->invalid_start = 0; @@ -636,7 +642,6 @@ void terminal_paste(long count, char_u **y_array, size_t y_size) return; } vterm_keyboard_start_paste(curbuf->terminal->vt); - terminal_flush_output(curbuf->terminal); size_t buff_len = STRLEN(y_array[0]); char_u *buff = xmalloc(buff_len); for (int i = 0; i < count; i++) { // -V756 @@ -667,14 +672,6 @@ void terminal_paste(long count, char_u **y_array, size_t y_size) } xfree(buff); vterm_keyboard_end_paste(curbuf->terminal->vt); - terminal_flush_output(curbuf->terminal); -} - -void terminal_flush_output(Terminal *term) -{ - size_t len = vterm_output_read(term->vt, term->textbuf, - sizeof(term->textbuf)); - terminal_send(term, term->textbuf, len); } void terminal_send_key(Terminal *term, int c) @@ -693,8 +690,6 @@ void terminal_send_key(Terminal *term, int c) } else { vterm_keyboard_unichar(term->vt, (uint32_t)c, mod); } - - terminal_flush_output(term); } void terminal_receive(Terminal *term, char *data, size_t len) @@ -1265,9 +1260,6 @@ static bool send_mouse_event(Terminal *term, int c) } mouse_action(term, button, row, col - offset, pressed, 0); - size_t len = vterm_output_read(term->vt, term->textbuf, - sizeof(term->textbuf)); - terminal_send(term, term->textbuf, len); return false; } @@ -1299,7 +1291,7 @@ static bool send_mouse_event(Terminal *term, int c) } end: - ins_char_typebuf(c); + ins_char_typebuf(c, mod_mask); return true; } diff --git a/src/nvim/testdir/check.vim b/src/nvim/testdir/check.vim index 14bab33a2f..883f036fe1 100644 --- a/src/nvim/testdir/check.vim +++ b/src/nvim/testdir/check.vim @@ -12,9 +12,9 @@ endfunc " Command to check for the absence of a feature. command -nargs=1 CheckNotFeature call CheckNotFeature(<f-args>) func CheckNotFeature(name) - if !has(a:name, 1) - throw 'Checking for non-existent feature ' .. a:name - endif + " if !has(a:name, 1) + " throw 'Checking for non-existent feature ' .. a:name + " endif if has(a:name) throw 'Skipped: ' .. a:name .. ' feature present' endif @@ -63,6 +63,15 @@ func CheckUnix() endif endfunc +" Command to check for not running on a BSD system. +" TODO: using this checks should not be needed +command CheckNotBSD call CheckNotBSD() +func CheckNotBSD() + if has('bsd') + throw 'Skipped: does not work on BSD' + endif +endfunc + " Command to check that making screendumps is supported. " Caller must source screendump.vim command CheckScreendump call CheckScreendump() @@ -104,6 +113,14 @@ func CheckNotGui() endif endfunc +" Command to check that test is not running as root +command CheckNotRoot call CheckNotRoot() +func CheckNotRoot() + if IsRoot() + throw 'Skipped: cannot run test as root' + endif +endfunc + " Command to check that the current language is English command CheckEnglish call CheckEnglish() func CheckEnglish() diff --git a/src/nvim/testdir/shared.vim b/src/nvim/testdir/shared.vim index f456ff4250..c2809844ac 100644 --- a/src/nvim/testdir/shared.vim +++ b/src/nvim/testdir/shared.vim @@ -343,6 +343,15 @@ func RunVimPiped(before, after, arguments, pipecmd) return 1 endfunc +func IsRoot() + if !has('unix') + return v:false + elseif $USER == 'root' || system('id -un') =~ '\<root\>' + return v:true + endif + return v:false +endfunc + " Get all messages but drop the maintainer entry. func GetMessages() redir => result diff --git a/src/nvim/testdir/test_alot.vim b/src/nvim/testdir/test_alot.vim index cc767a9bcf..c0ac4393c4 100644 --- a/src/nvim/testdir/test_alot.vim +++ b/src/nvim/testdir/test_alot.vim @@ -27,6 +27,7 @@ source test_join.vim source test_jumps.vim source test_fileformat.vim source test_filetype.vim +source test_filetype_lua.vim source test_lambda.vim source test_menu.vim source test_messages.vim diff --git a/src/nvim/testdir/test_alot_utf8.vim b/src/nvim/testdir/test_alot_utf8.vim index 70f14320a6..77f5ede4c8 100644 --- a/src/nvim/testdir/test_alot_utf8.vim +++ b/src/nvim/testdir/test_alot_utf8.vim @@ -6,7 +6,6 @@ source test_charsearch_utf8.vim source test_expr_utf8.vim -source test_matchadd_conceal_utf8.vim source test_mksession_utf8.vim source test_regexp_utf8.vim source test_source_utf8.vim diff --git a/src/nvim/testdir/test_assert.vim b/src/nvim/testdir/test_assert.vim index 52f243aaea..28c5948142 100644 --- a/src/nvim/testdir/test_assert.vim +++ b/src/nvim/testdir/test_assert.vim @@ -1,5 +1,55 @@ " Test that the methods used for testing work. +func Test_assert_false() + call assert_equal(0, assert_false(0)) + call assert_equal(0, assert_false(v:false)) + call assert_equal(0, v:false->assert_false()) + + call assert_equal(1, assert_false(123)) + call assert_match("Expected False but got 123", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, 123->assert_false()) + call assert_match("Expected False but got 123", v:errors[0]) + call remove(v:errors, 0) +endfunc + +func Test_assert_true() + call assert_equal(0, assert_true(1)) + call assert_equal(0, assert_true(123)) + call assert_equal(0, assert_true(v:true)) + call assert_equal(0, v:true->assert_true()) + + call assert_equal(1, assert_true(0)) + call assert_match("Expected True but got 0", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, 0->assert_true()) + call assert_match("Expected True but got 0", v:errors[0]) + call remove(v:errors, 0) +endfunc + +func Test_assert_equal() + let s = 'foo' + call assert_equal(0, assert_equal('foo', s)) + let n = 4 + call assert_equal(0, assert_equal(4, n)) + let l = [1, 2, 3] + call assert_equal(0, assert_equal([1, 2, 3], l)) + call assert_equal(v:_null_list, v:_null_list) + call assert_equal(v:_null_list, []) + call assert_equal([], v:_null_list) + + let s = 'foo' + call assert_equal(1, assert_equal('bar', s)) + call assert_match("Expected 'bar' but got 'foo'", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal('XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX') + call assert_match("Expected 'X\\\\\\[x occurs 21 times]X' but got 'X\\\\\\[y occurs 25 times]X'", v:errors[0]) + call remove(v:errors, 0) +endfunc + func Test_assert_equalfile() call assert_equal(1, assert_equalfile('abcabc', 'xyzxyz')) call assert_match("E485: Can't read file abcabc", v:errors[0]) @@ -46,17 +96,129 @@ func Test_assert_equalfile() call delete('Xtwo') endfunc +func Test_assert_notequal() + let n = 4 + call assert_equal(0, assert_notequal('foo', n)) + let s = 'foo' + call assert_equal(0, assert_notequal([1, 2, 3], s)) + + call assert_equal(1, assert_notequal('foo', s)) + call assert_match("Expected not equal to 'foo'", v:errors[0]) + call remove(v:errors, 0) +endfunc + +func Test_assert_report() + call assert_equal(1, assert_report('something is wrong')) + call assert_match('something is wrong', v:errors[0]) + call remove(v:errors, 0) + call assert_equal(1, 'also wrong'->assert_report()) + call assert_match('also wrong', v:errors[0]) + call remove(v:errors, 0) +endfunc + +func Test_assert_exception() + try + nocommand + catch + call assert_equal(0, assert_exception('E492:')) + endtry + + try + nocommand + catch + try + " illegal argument, get NULL for error + call assert_equal(1, assert_exception([])) + catch + call assert_equal(0, assert_exception('E730:')) + endtry + endtry +endfunc + +func Test_wrong_error_type() + let save_verrors = v:errors + let v:['errors'] = {'foo': 3} + call assert_equal('yes', 'no') + let verrors = v:errors + let v:errors = save_verrors + call assert_equal(type([]), type(verrors)) +endfunc + +func Test_match() + call assert_equal(0, assert_match('^f.*b.*r$', 'foobar')) + + call assert_equal(1, assert_match('bar.*foo', 'foobar')) + call assert_match("Pattern 'bar.*foo' does not match 'foobar'", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, assert_match('bar.*foo', 'foobar', 'wrong')) + call assert_match('wrong', v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, 'foobar'->assert_match('bar.*foo', 'wrong')) + call assert_match('wrong', v:errors[0]) + call remove(v:errors, 0) +endfunc + +func Test_notmatch() + call assert_equal(0, assert_notmatch('foo', 'bar')) + call assert_equal(0, assert_notmatch('^foobar$', 'foobars')) + + call assert_equal(1, assert_notmatch('foo', 'foobar')) + call assert_match("Pattern 'foo' does match 'foobar'", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, 'foobar'->assert_notmatch('foo')) + call assert_match("Pattern 'foo' does match 'foobar'", v:errors[0]) + call remove(v:errors, 0) +endfunc + +func Test_assert_fail_fails() + call assert_equal(1, assert_fails('xxx', 'E12345')) + call assert_match("Expected 'E12345' but got 'E492:", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, assert_fails('xxx', 'E9876', 'stupid')) + call assert_match("stupid: Expected 'E9876' but got 'E492:", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, assert_fails('echo', '', 'echo command')) + call assert_match("command did not fail: echo command", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(1, 'echo'->assert_fails('', 'echo command')) + call assert_match("command did not fail: echo command", v:errors[0]) + call remove(v:errors, 0) +endfunc + func Test_assert_fails_in_try_block() try call assert_equal(0, assert_fails('throw "error"')) endtry endfunc +func Test_assert_beeps() + new + call assert_equal(0, assert_beeps('normal h')) + + call assert_equal(1, assert_beeps('normal 0')) + call assert_match("command did not beep: normal 0", v:errors[0]) + call remove(v:errors, 0) + + call assert_equal(0, 'normal h'->assert_beeps()) + call assert_equal(1, 'normal 0'->assert_beeps()) + call assert_match("command did not beep: normal 0", v:errors[0]) + call remove(v:errors, 0) + + bwipe +endfunc + func Test_assert_inrange() call assert_equal(0, assert_inrange(7, 7, 7)) call assert_equal(0, assert_inrange(5, 7, 5)) call assert_equal(0, assert_inrange(5, 7, 6)) call assert_equal(0, assert_inrange(5, 7, 7)) + call assert_equal(1, assert_inrange(5, 7, 4)) call assert_match("Expected range 5 - 7, but got 4", v:errors[0]) call remove(v:errors, 0) @@ -64,6 +226,12 @@ func Test_assert_inrange() call assert_match("Expected range 5 - 7, but got 8", v:errors[0]) call remove(v:errors, 0) + call assert_equal(0, 5->assert_inrange(5, 7)) + call assert_equal(0, 7->assert_inrange(5, 7)) + call assert_equal(1, 8->assert_inrange(5, 7)) + call assert_match("Expected range 5 - 7, but got 8", v:errors[0]) + call remove(v:errors, 0) + call assert_fails('call assert_inrange(1, 1)', 'E119:') if has('float') @@ -83,6 +251,12 @@ func Test_assert_inrange() endif endfunc +func Test_assert_with_msg() + call assert_equal('foo', 'bar', 'testing') + call assert_match("testing: Expected 'foo' but got 'bar'", v:errors[0]) + call remove(v:errors, 0) +endfunc + " Must be last. func Test_zz_quit_detected() " Verify that if a test function ends Vim the test script detects this. diff --git a/src/nvim/testdir/test_autocmd.vim b/src/nvim/testdir/test_autocmd.vim index 45285b69a1..a8d51ef598 100644 --- a/src/nvim/testdir/test_autocmd.vim +++ b/src/nvim/testdir/test_autocmd.vim @@ -4,7 +4,7 @@ source shared.vim source check.vim source term_util.vim -func! s:cleanup_buffers() abort +func s:cleanup_buffers() abort for bnr in range(1, bufnr('$')) if bufloaded(bnr) && bufnr('%') != bnr execute 'bd! ' . bnr @@ -2380,6 +2380,40 @@ func Test_autocmd_was_using_freed_memory() pclose endfunc +func Test_BufWrite_lockmarks() + edit! Xtest + call setline(1, ['a', 'b', 'c', 'd']) + + " :lockmarks preserves the marks + call SetChangeMarks(2, 3) + lockmarks write + call assert_equal([2, 3], [line("'["), line("']")]) + + " *WritePre autocmds get the correct line range, but lockmarks preserves the + " original values for the user + augroup lockmarks + au! + au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) + au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")]) + augroup END + + lockmarks write + call assert_equal([2, 3], [line("'["), line("']")]) + + if executable('cat') + lockmarks %!cat + call assert_equal([2, 3], [line("'["), line("']")]) + endif + + lockmarks 3,4write Xtest2 + call assert_equal([2, 3], [line("'["), line("']")]) + + au! lockmarks + augroup! lockmarks + call delete('Xtest') + call delete('Xtest2') +endfunc + " FileChangedShell tested in test_filechanged.vim func LogACmd() @@ -2509,6 +2543,16 @@ func Test_close_autocmd_tab() %bwipe! endfunc +func Test_Visual_doautoall_redraw() + call setline(1, ['a', 'b']) + new + wincmd p + call feedkeys("G\<C-V>", 'txn') + autocmd User Explode ++once redraw + doautoall User Explode + %bwipe! +endfunc + func Test_autocmd_closes_window() au BufNew,BufWinLeave * e %e file yyy diff --git a/src/nvim/testdir/test_breakindent.vim b/src/nvim/testdir/test_breakindent.vim index 8d592f21ea..b619f2adb6 100644 --- a/src/nvim/testdir/test_breakindent.vim +++ b/src/nvim/testdir/test_breakindent.vim @@ -20,7 +20,7 @@ func s:screen_lines2(lnums, lnume, width) abort return ScreenLines([a:lnums, a:lnume], a:width) endfunc -func! s:compare_lines(expect, actual) +func s:compare_lines(expect, actual) call assert_equal(join(a:expect, "\n"), join(a:actual, "\n")) endfunc diff --git a/src/nvim/testdir/test_buffer.vim b/src/nvim/testdir/test_buffer.vim index 40111fdf06..a31cdbb49a 100644 --- a/src/nvim/testdir/test_buffer.vim +++ b/src/nvim/testdir/test_buffer.vim @@ -1,5 +1,7 @@ " Tests for Vim buffer +source check.vim + func Test_buffer_error() new foo1 new foo2 @@ -30,4 +32,33 @@ func Test_balt() call assert_equal('OtherBuffer', bufname()) endfunc +" Test for buffer match URL(scheme) check +" scheme is alpha and inner hyphen only. +func Test_buffer_scheme() + CheckMSWindows + + set noshellslash + %bwipe! + let bufnames = [ + \ #{id: 'b0', name: 'test://xyz/foo/b0' , match: 1}, + \ #{id: 'b1', name: 'test+abc://xyz/foo/b1', match: 0}, + \ #{id: 'b2', name: 'test_abc://xyz/foo/b2', match: 0}, + \ #{id: 'b3', name: 'test-abc://xyz/foo/b3', match: 1}, + \ #{id: 'b4', name: '-test://xyz/foo/b4' , match: 0}, + \ #{id: 'b5', name: 'test-://xyz/foo/b5' , match: 0}, + \] + for buf in bufnames + new `=buf.name` + if buf.match + call assert_equal(buf.name, getbufinfo(buf.id)[0].name) + else + " slashes will have become backslashes + call assert_notequal(buf.name, getbufinfo(buf.id)[0].name) + endif + bwipe + endfor + + set shellslash& +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_cd.vim b/src/nvim/testdir/test_cd.vim index 76a2620be0..c364babd65 100644 --- a/src/nvim/testdir/test_cd.vim +++ b/src/nvim/testdir/test_cd.vim @@ -44,6 +44,15 @@ func Test_cd_minus() cd - call assert_equal(path, getcwd()) + " Test for :cd - after a failed :cd + " v8.2.1183 is not ported yet + " call assert_fails('cd /nonexistent', 'E344:') + call assert_fails('cd /nonexistent', 'E472:') + call assert_equal(path, getcwd()) + cd - + call assert_equal(path_dotdot, getcwd()) + cd - + " Test for :cd - without a previous directory let lines =<< trim [SCRIPT] call assert_fails('cd -', 'E186:') diff --git a/src/nvim/testdir/test_cdo.vim b/src/nvim/testdir/test_cdo.vim new file mode 100644 index 0000000000..aa2e4f1a8c --- /dev/null +++ b/src/nvim/testdir/test_cdo.vim @@ -0,0 +1,205 @@ +" Tests for the :cdo, :cfdo, :ldo and :lfdo commands + +if !has('quickfix') + throw 'Skipped: quickfix feature missing' +endif + +" Create the files used by the tests +function SetUp() + call writefile(["Line1", "Line2", "Line3"], 'Xtestfile1') + call writefile(["Line1", "Line2", "Line3"], 'Xtestfile2') + call writefile(["Line1", "Line2", "Line3"], 'Xtestfile3') +endfunction + +" Remove the files used by the tests +function TearDown() + call delete('Xtestfile1') + call delete('Xtestfile2') + call delete('Xtestfile3') +endfunction + +" Returns the current line in '<filename> <linenum>L <column>C' format +function GetRuler() + return expand('%') . ' ' . line('.') . 'L' . ' ' . col('.') . 'C' +endfunction + +" Tests for the :cdo and :ldo commands +function XdoTests(cchar) + enew + + " Shortcuts for calling the cdo and ldo commands + let Xdo = a:cchar . 'do' + let Xgetexpr = a:cchar . 'getexpr' + let Xprev = a:cchar. 'prev' + let XdoCmd = Xdo . ' call add(l, GetRuler())' + + " Try with an empty list + let l = [] + exe XdoCmd + call assert_equal([], l) + + " Populate the list and then try + exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:3:1:Line3']" + + let l = [] + exe XdoCmd + call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) + + " Run command only on selected error lines + let l = [] + enew + exe "2,3" . XdoCmd + call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) + + " Boundary condition tests + let l = [] + enew + exe "1,1" . XdoCmd + call assert_equal(['Xtestfile1 1L 3C'], l) + + let l = [] + enew + exe "3" . XdoCmd + call assert_equal(['Xtestfile3 3L 1C'], l) + + " Range test commands + let l = [] + enew + exe "%" . XdoCmd + call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) + + let l = [] + enew + exe "1,$" . XdoCmd + call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) + + let l = [] + enew + exe Xprev + exe "." . XdoCmd + call assert_equal(['Xtestfile2 2L 2C'], l) + + let l = [] + enew + exe "+" . XdoCmd + call assert_equal(['Xtestfile3 3L 1C'], l) + + " Invalid error lines test + let l = [] + enew + exe "silent! 27" . XdoCmd + exe "silent! 4,5" . XdoCmd + call assert_equal([], l) + + " Run commands from an unsaved buffer + let v:errmsg='' + let l = [] + enew + setlocal modified + exe "silent! 2,2" . XdoCmd + if v:errmsg !~# 'No write since last change' + call add(v:errors, 'Unsaved file change test failed') + endif + + " If the executed command fails, then the operation should be aborted + enew! + let subst_count = 0 + exe "silent!" . Xdo . " s/Line/xLine/ | let subst_count += 1" + if subst_count != 1 || getline('.') != 'xLine1' + call add(v:errors, 'Abort command on error test failed') + endif + + let l = [] + exe "2,2" . Xdo . "! call add(l, GetRuler())" + call assert_equal(['Xtestfile2 2L 2C'], l) + + " List with no valid error entries + let l = [] + edit! +2 Xtestfile1 + exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']" + exe XdoCmd + call assert_equal([], l) + exe "silent! 2" . XdoCmd + call assert_equal([], l) + let v:errmsg='' + exe "%" . XdoCmd + exe "1,$" . XdoCmd + exe "." . XdoCmd + call assert_equal('', v:errmsg) + + " List with only one valid entry + let l = [] + exe Xgetexpr . " ['Xtestfile3:3:1:Line3']" + exe XdoCmd + call assert_equal(['Xtestfile3 3L 1C'], l) + +endfunction + +" Tests for the :cfdo and :lfdo commands +function XfdoTests(cchar) + enew + + " Shortcuts for calling the cfdo and lfdo commands + let Xfdo = a:cchar . 'fdo' + let Xgetexpr = a:cchar . 'getexpr' + let XfdoCmd = Xfdo . ' call add(l, GetRuler())' + let Xpfile = a:cchar. 'pfile' + + " Clear the quickfix/location list + exe Xgetexpr . " []" + + " Try with an empty list + let l = [] + exe XfdoCmd + call assert_equal([], l) + + " Populate the list and then try + exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'Xtestfile1:2:1:Line2', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:2:3:Line2', 'Xtestfile3:3:1:Line3']" + + let l = [] + exe XfdoCmd + call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) + + " Run command only on selected error lines + let l = [] + exe "2,3" . XfdoCmd + call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) + + " Boundary condition tests + let l = [] + exe "3" . XfdoCmd + call assert_equal(['Xtestfile3 2L 3C'], l) + + " Range test commands + let l = [] + exe "%" . XfdoCmd + call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) + + let l = [] + exe "1,$" . XfdoCmd + call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) + + let l = [] + exe Xpfile + exe "." . XfdoCmd + call assert_equal(['Xtestfile2 2L 2C'], l) + + " List with only one valid entry + let l = [] + exe Xgetexpr . " ['Xtestfile2:2:5:Line2']" + exe XfdoCmd + call assert_equal(['Xtestfile2 2L 5C'], l) + +endfunction + +" Tests for cdo and cfdo +function Test_cdo() + call XdoTests('c') + call XfdoTests('c') +endfunction + +" Tests for ldo and lfdo +function Test_ldo() + call XdoTests('l') + call XfdoTests('l') +endfunction diff --git a/src/nvim/testdir/test_charsearch_utf8.vim b/src/nvim/testdir/test_charsearch_utf8.vim index 09341a90b0..82a807ac5b 100644 --- a/src/nvim/testdir/test_charsearch_utf8.vim +++ b/src/nvim/testdir/test_charsearch_utf8.vim @@ -1,7 +1,7 @@ " Tests for related f{char} and t{char} using utf-8. " Test for t,f,F,T movement commands -function! Test_search_cmds() +func Test_search_cmds() new! call setline(1, "・最初から最後まで最強のVimは最高") 1 diff --git a/src/nvim/testdir/test_cindent.vim b/src/nvim/testdir/test_cindent.vim index 562867f548..5dc54111e7 100644 --- a/src/nvim/testdir/test_cindent.vim +++ b/src/nvim/testdir/test_cindent.vim @@ -1,6 +1,5 @@ " Test for cinoptions and cindent " -" TODO: rewrite test3.in into this new style test func Test_cino_hash() " Test that curbuf->b_ind_hash_comment is correctly reset @@ -128,6 +127,5178 @@ func Test_cindent_func() bwipe! endfunc +func Test_cindent_1() + new + setl cindent ts=4 sw=4 + setl cino& sts& + + let code =<< trim [CODE] + /* start of AUTO matically checked vim: set ts=4 : */ + { + if (test) + cmd1; + cmd2; + } + + { + if (test) + cmd1; + else + cmd2; + } + + { + if (test) + { + cmd1; + cmd2; + } + } + + { + if (test) + { + cmd1; + else + } + } + + { + while (this) + if (test) + cmd1; + cmd2; + } + + { + while (this) + if (test) + cmd1; + else + cmd2; + } + + { + if (test) + { + cmd; + } + + if (test) + cmd; + } + + { + if (test) { + cmd; + } + + if (test) cmd; + } + + { + cmd1; + for (blah) + while (this) + if (test) + cmd2; + cmd3; + } + + { + cmd1; + for (blah) + while (this) + if (test) + cmd2; + cmd3; + + if (test) + { + cmd1; + cmd2; + cmd3; + } + } + + + /* Test for 'cindent' do/while mixed with if/else: */ + + { + do + if (asdf) + asdfasd; + while (cond); + + do + if (asdf) + while (asdf) + asdf; + while (asdf); + } + + /* Test for 'cindent' with two ) on a continuation line */ + { + if (asdfasdf;asldkfj asdlkfj as;ldkfj sal;d + aal;sdkjf ( ;asldfkja;sldfk + al;sdjfka ;slkdf ) sa;ldkjfsa dlk;) + line up here; + } + + + /* C++ tests: */ + + // foo() these three lines should remain in column 0 + // { + // } + + /* Test for continuation and unterminated lines: */ + { + i = 99 + 14325 + + 21345 + + 21345 + + 21345 + ( 21345 + + 21345) + + 2345 + + 1234; + c = 1; + } + + /* + testje for indent with empty line + + here */ + + { + if (testing && + not a joke || + line up here) + hay; + if (testing && + (not a joke || testing + )line up here) + hay; + if (testing && + (not a joke || testing + line up here)) + hay; + } + + + { + switch (c) + { + case xx: + do + if (asdf) + do + asdfasdf; + while (asdf); + else + asdfasdf; + while (cond); + case yy: + case xx: + case zz: + testing; + } + } + + { + if (cond) { + foo; + } + else + { + bar; + } + } + + { + if (alskdfj ;alsdkfjal;skdjf (;sadlkfsa ;dlkf j;alksdfj ;alskdjf + alsdkfj (asldk;fj + awith cino=(0 ;lf this one goes to below the paren with == + ;laksjfd ;lsakdjf ;alskdf asd) + asdfasdf;))) + asdfasdf; + } + + int + func(a, b) + int a; + int c; + { + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3) + ) + } + + { + while (asd) + { + if (asdf) + if (test) + if (that) + { + if (asdf) + do + cdasd; + while (as + df); + } + else + if (asdf) + asdf; + else + asdf; + asdf; + } + } + + { + s = "/*"; b = ';' + s = "/*"; b = ';'; + a = b; + } + + { + switch (a) + { + case a: + switch (t) + { + case 1: + cmd; + break; + case 2: + cmd; + break; + } + cmd; + break; + case b: + { + int i; + cmd; + } + break; + case c: { + int i; + cmd; + } + case d: if (cond && + test) { /* this line doesn't work right */ + int i; + cmd; + } + break; + } + } + + { + if (!(vim_strchr(p_cpo, CPO_BUFOPTGLOB) != NULL && entering) && + (bp_to->b_p_initialized || + (!entering && vim_strchr(p_cpo, CPO_BUFOPT) != NULL))) + return; + label : + asdf = asdf ? + asdf : asdf; + asdf = asdf ? + asdf: asdf; + } + + /* Special Comments : This function has the added complexity (compared */ + /* : to addtolist) of having to check for a detail */ + /* : texture and add that to the list first. */ + + char *(array[100]) = { + "testje", + "foo", + "bar", + } + + enum soppie + { + yes = 0, + no, + maybe + }; + + typedef enum soppie + { + yes = 0, + no, + maybe + }; + + static enum + { + yes = 0, + no, + maybe + } soppie; + + public static enum + { + yes = 0, + no, + maybe + } soppie; + + static private enum + { + yes = 0, + no, + maybe + } soppie; + + { + int a, + b; + } + + { + struct Type + { + int i; + char *str; + } var[] = + { + 0, "zero", + 1, "one", + 2, "two", + 3, "three" + }; + + float matrix[3][3] = + { + { + 0, + 1, + 2 + }, + { + 3, + 4, + 5 + }, + { + 6, + 7, + 8 + } + }; + } + + { + /* blah ( blah */ + /* where does this go? */ + + /* blah ( blah */ + cmd; + + func(arg1, + /* comment */ + arg2); + a; + { + b; + { + c; /* Hey, NOW it indents?! */ + } + } + + { + func(arg1, + arg2, + arg3); + /* Hey, what am I doing here? Is this coz of the ","? */ + } + } + + main () + { + if (cond) + { + a = b; + } + if (cond) { + a = c; + } + if (cond) + a = d; + return; + } + + { + case 2: if (asdf && + asdfasdf) + aasdf; + a = 9; + case 3: if (asdf) + aasdf; + a = 9; + case 4: x = 1; + y = 2; + + label: if (asdf) + here; + + label: if (asdf && + asdfasdf) + { + } + + label: if (asdf && + asdfasdf) { + there; + } + + label: if (asdf && + asdfasdf) + there; + } + + { + /* + hello with ":set comments= cino=c5" + */ + + /* + hello with ":set comments= cino=" + */ + } + + + { + if (a < b) { + a = a + 1; + } else + a = a + 2; + + if (a) + do { + testing; + } while (asdfasdf); + a = b + 1; + asdfasdf + } + + { + for ( int i = 0; + i < 10; i++ ) + { + } + i = 0; + } + + class bob + { + int foo() {return 1;} + int bar; + } + + main() + { + while(1) + if (foo) + { + bar; + } + else { + asdf; + } + misplacedline; + } + + { + if (clipboard.state == SELECT_DONE + && ((row == clipboard.start.lnum + && col >= clipboard.start.col) + || row > clipboard.start.lnum)) + } + + { + if (1) {i += 4;} + where_am_i; + return 0; + } + + { + { + } // sdf(asdf + if (asdf) + asd; + } + + { + label1: + label2: + } + + { + int fooRet = foo(pBar1, false /*fKB*/, + true /*fPTB*/, 3 /*nT*/, false /*fDF*/); + f() { + for ( i = 0; + i < m; + /* c */ i++ ) { + a = b; + } + } + } + + { + f1(/*comment*/); + f2(); + } + + { + do { + if (foo) { + } else + ; + } while (foo); + foo(); // was wrong + } + + int x; // no extra indent because of the ; + void func() + { + } + + char *tab[] = {"aaa", + "};", /* }; */ NULL} + int indented; + {} + + char *a[] = {"aaa", "bbb", + "ccc", NULL}; + // here + + char *tab[] = {"aaa", + "xx", /* xx */}; /* asdf */ + int not_indented; + + { + do { + switch (bla) + { + case 1: if (foo) + bar; + } + } while (boo); + wrong; + } + + int foo, + bar; + int foo; + + #if defined(foo) \ + && defined(bar) + char * xx = "asdf\ + foo\ + bor"; + int x; + + char *foo = "asdf\ + asdf\ + asdf", + *bar; + + void f() + { + #if defined(foo) \ + && defined(bar) + char *foo = "asdf\ + asdf\ + asdf", + *bar; + { + int i; + char *foo = "asdf\ + asdf\ + asdf", + *bar; + } + #endif + } + #endif + + int y; // comment + // comment + + // comment + + { + Constructor(int a, + int b ) : BaseClass(a) + { + } + } + + void foo() + { + char one, + two; + struct bla piet, + jan; + enum foo kees, + jannie; + static unsigned sdf, + krap; + unsigned int piet, + jan; + int + kees, + jan; + } + + { + t(int f, + int d); // ) + d(); + } + + Constructor::Constructor(int a, + int b + ) : + BaseClass(a, + b, + c), + mMember(b), + { + } + + Constructor::Constructor(int a, + int b ) : + BaseClass(a) + { + } + + Constructor::Constructor(int a, + int b ) /*x*/ : /*x*/ BaseClass(a), + member(b) + { + } + + A::A(int a, int b) + : aa(a), + bb(b), + cc(c) + { + } + + class CAbc : + public BaseClass1, + protected BaseClass2 + { + int Test() { return FALSE; } + int Test1() { return TRUE; } + + CAbc(int a, int b ) : + BaseClass(a) + { + switch(xxx) + { + case abc: + asdf(); + break; + + case 999: + baer(); + break; + } + } + + public: // <-- this was incoreectly indented before!! + void testfall(); + protected: + void testfall(); + }; + + class CAbc : public BaseClass1, + protected BaseClass2 + { + }; + + static struct + { + int a; + int b; + } variable[COUNT] = + { + { + 123, + 456 + }, + { + 123, + 456 + } + }; + + static struct + { + int a; + int b; + } variable[COUNT] = + { + { 123, 456 }, + { 123, 456 } + }; + + void asdf() /* ind_maxparen may cause trouble here */ + { + if ((0 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1)) break; + } + + foo() + { + a = cond ? foo() : asdf + + asdf; + + a = cond ? + foo() : asdf + + asdf; + } + + int main(void) + { + if (a) + if (b) + 2; + else 3; + next_line_of_code(); + } + + barry() + { + Foo::Foo (int one, + int two) + : something(4) + {} + } + + barry() + { + Foo::Foo (int one, int two) + : something(4) + {} + } + + Constructor::Constructor(int a, + int b + ) : + BaseClass(a, + b, + c), + mMember(b) + { + } + int main () + { + if (lala) + do + ++(*lolo); + while (lili + && lele); + lulu; + } + + int main () + { + switch (c) + { + case 'c': if (cond) + { + } + } + } + + main() + { + (void) MyFancyFuasdfadsfnction( + argument); + } + + main() + { + char foo[] = "/*"; + /* as + df */ + hello + } + + /* valid namespaces with normal indent */ + namespace + { + { + 111111111111; + } + } + namespace /* test */ + { + 11111111111111111; + } + namespace // test + { + 111111111111111111; + } + namespace + { + 111111111111111111; + } + namespace test + { + 111111111111111111; + } + namespace{ + 111111111111111111; + } + namespace test{ + 111111111111111111; + } + namespace { + 111111111111111111; + } + namespace test { + 111111111111111111; + namespace test2 { + 22222222222222222; + } + } + inline namespace { + 111111111111111111; + } + inline /* test */ namespace { + 111111111111111111; + } + inline/* test */namespace { + 111111111111111111; + } + + /* invalid namespaces use block indent */ + namespace test test2 { + 111111111111111111111; + } + namespace11111111111 { + 111111111111; + } + namespace() { + 1111111111111; + } + namespace() + { + 111111111111111111; + } + namespace test test2 + { + 1111111111111111111; + } + namespace111111111 + { + 111111111111111111; + } + inlinenamespace { + 111111111111111111; + } + + void getstring() { + /* Raw strings */ + const char* s = R"( + test { + # comment + field: 123 + } + )"; + } + + void getstring() { + const char* s = R"foo( + test { + # comment + field: 123 + } + )foo"; + } + + { + int a[4] = { + [0] = 0, + [1] = 1, + [2] = 2, + [3] = 3, + }; + } + + { + a = b[2] + + 3; + } + + { + if (1) + /* aaaaa + * bbbbb + */ + a = 1; + } + + void func() + { + switch (foo) + { + case (bar): + if (baz()) + quux(); + break; + case (shmoo): + if (!bar) + { + } + case (foo1): + switch (bar) + { + case baz: + baz_f(); + break; + } + break; + default: + baz(); + baz(); + break; + } + } + + /* end of AUTO */ + [CODE] + + call append(0, code) + normal gg + call search('start of AUTO') + exe "normal =/end of AUTO\<CR>" + + let expected =<< trim [CODE] + /* start of AUTO matically checked vim: set ts=4 : */ + { + if (test) + cmd1; + cmd2; + } + + { + if (test) + cmd1; + else + cmd2; + } + + { + if (test) + { + cmd1; + cmd2; + } + } + + { + if (test) + { + cmd1; + else + } + } + + { + while (this) + if (test) + cmd1; + cmd2; + } + + { + while (this) + if (test) + cmd1; + else + cmd2; + } + + { + if (test) + { + cmd; + } + + if (test) + cmd; + } + + { + if (test) { + cmd; + } + + if (test) cmd; + } + + { + cmd1; + for (blah) + while (this) + if (test) + cmd2; + cmd3; + } + + { + cmd1; + for (blah) + while (this) + if (test) + cmd2; + cmd3; + + if (test) + { + cmd1; + cmd2; + cmd3; + } + } + + + /* Test for 'cindent' do/while mixed with if/else: */ + + { + do + if (asdf) + asdfasd; + while (cond); + + do + if (asdf) + while (asdf) + asdf; + while (asdf); + } + + /* Test for 'cindent' with two ) on a continuation line */ + { + if (asdfasdf;asldkfj asdlkfj as;ldkfj sal;d + aal;sdkjf ( ;asldfkja;sldfk + al;sdjfka ;slkdf ) sa;ldkjfsa dlk;) + line up here; + } + + + /* C++ tests: */ + + // foo() these three lines should remain in column 0 + // { + // } + + /* Test for continuation and unterminated lines: */ + { + i = 99 + 14325 + + 21345 + + 21345 + + 21345 + ( 21345 + + 21345) + + 2345 + + 1234; + c = 1; + } + + /* + testje for indent with empty line + + here */ + + { + if (testing && + not a joke || + line up here) + hay; + if (testing && + (not a joke || testing + )line up here) + hay; + if (testing && + (not a joke || testing + line up here)) + hay; + } + + + { + switch (c) + { + case xx: + do + if (asdf) + do + asdfasdf; + while (asdf); + else + asdfasdf; + while (cond); + case yy: + case xx: + case zz: + testing; + } + } + + { + if (cond) { + foo; + } + else + { + bar; + } + } + + { + if (alskdfj ;alsdkfjal;skdjf (;sadlkfsa ;dlkf j;alksdfj ;alskdjf + alsdkfj (asldk;fj + awith cino=(0 ;lf this one goes to below the paren with == + ;laksjfd ;lsakdjf ;alskdf asd) + asdfasdf;))) + asdfasdf; + } + + int + func(a, b) + int a; + int c; + { + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3) + ) + } + + { + while (asd) + { + if (asdf) + if (test) + if (that) + { + if (asdf) + do + cdasd; + while (as + df); + } + else + if (asdf) + asdf; + else + asdf; + asdf; + } + } + + { + s = "/*"; b = ';' + s = "/*"; b = ';'; + a = b; + } + + { + switch (a) + { + case a: + switch (t) + { + case 1: + cmd; + break; + case 2: + cmd; + break; + } + cmd; + break; + case b: + { + int i; + cmd; + } + break; + case c: { + int i; + cmd; + } + case d: if (cond && + test) { /* this line doesn't work right */ + int i; + cmd; + } + break; + } + } + + { + if (!(vim_strchr(p_cpo, CPO_BUFOPTGLOB) != NULL && entering) && + (bp_to->b_p_initialized || + (!entering && vim_strchr(p_cpo, CPO_BUFOPT) != NULL))) + return; + label : + asdf = asdf ? + asdf : asdf; + asdf = asdf ? + asdf: asdf; + } + + /* Special Comments : This function has the added complexity (compared */ + /* : to addtolist) of having to check for a detail */ + /* : texture and add that to the list first. */ + + char *(array[100]) = { + "testje", + "foo", + "bar", + } + + enum soppie + { + yes = 0, + no, + maybe + }; + + typedef enum soppie + { + yes = 0, + no, + maybe + }; + + static enum + { + yes = 0, + no, + maybe + } soppie; + + public static enum + { + yes = 0, + no, + maybe + } soppie; + + static private enum + { + yes = 0, + no, + maybe + } soppie; + + { + int a, + b; + } + + { + struct Type + { + int i; + char *str; + } var[] = + { + 0, "zero", + 1, "one", + 2, "two", + 3, "three" + }; + + float matrix[3][3] = + { + { + 0, + 1, + 2 + }, + { + 3, + 4, + 5 + }, + { + 6, + 7, + 8 + } + }; + } + + { + /* blah ( blah */ + /* where does this go? */ + + /* blah ( blah */ + cmd; + + func(arg1, + /* comment */ + arg2); + a; + { + b; + { + c; /* Hey, NOW it indents?! */ + } + } + + { + func(arg1, + arg2, + arg3); + /* Hey, what am I doing here? Is this coz of the ","? */ + } + } + + main () + { + if (cond) + { + a = b; + } + if (cond) { + a = c; + } + if (cond) + a = d; + return; + } + + { + case 2: if (asdf && + asdfasdf) + aasdf; + a = 9; + case 3: if (asdf) + aasdf; + a = 9; + case 4: x = 1; + y = 2; + + label: if (asdf) + here; + + label: if (asdf && + asdfasdf) + { + } + + label: if (asdf && + asdfasdf) { + there; + } + + label: if (asdf && + asdfasdf) + there; + } + + { + /* + hello with ":set comments= cino=c5" + */ + + /* + hello with ":set comments= cino=" + */ + } + + + { + if (a < b) { + a = a + 1; + } else + a = a + 2; + + if (a) + do { + testing; + } while (asdfasdf); + a = b + 1; + asdfasdf + } + + { + for ( int i = 0; + i < 10; i++ ) + { + } + i = 0; + } + + class bob + { + int foo() {return 1;} + int bar; + } + + main() + { + while(1) + if (foo) + { + bar; + } + else { + asdf; + } + misplacedline; + } + + { + if (clipboard.state == SELECT_DONE + && ((row == clipboard.start.lnum + && col >= clipboard.start.col) + || row > clipboard.start.lnum)) + } + + { + if (1) {i += 4;} + where_am_i; + return 0; + } + + { + { + } // sdf(asdf + if (asdf) + asd; + } + + { + label1: + label2: + } + + { + int fooRet = foo(pBar1, false /*fKB*/, + true /*fPTB*/, 3 /*nT*/, false /*fDF*/); + f() { + for ( i = 0; + i < m; + /* c */ i++ ) { + a = b; + } + } + } + + { + f1(/*comment*/); + f2(); + } + + { + do { + if (foo) { + } else + ; + } while (foo); + foo(); // was wrong + } + + int x; // no extra indent because of the ; + void func() + { + } + + char *tab[] = {"aaa", + "};", /* }; */ NULL} + int indented; + {} + + char *a[] = {"aaa", "bbb", + "ccc", NULL}; + // here + + char *tab[] = {"aaa", + "xx", /* xx */}; /* asdf */ + int not_indented; + + { + do { + switch (bla) + { + case 1: if (foo) + bar; + } + } while (boo); + wrong; + } + + int foo, + bar; + int foo; + + #if defined(foo) \ + && defined(bar) + char * xx = "asdf\ + foo\ + bor"; + int x; + + char *foo = "asdf\ + asdf\ + asdf", + *bar; + + void f() + { + #if defined(foo) \ + && defined(bar) + char *foo = "asdf\ + asdf\ + asdf", + *bar; + { + int i; + char *foo = "asdf\ + asdf\ + asdf", + *bar; + } + #endif + } + #endif + + int y; // comment + // comment + + // comment + + { + Constructor(int a, + int b ) : BaseClass(a) + { + } + } + + void foo() + { + char one, + two; + struct bla piet, + jan; + enum foo kees, + jannie; + static unsigned sdf, + krap; + unsigned int piet, + jan; + int + kees, + jan; + } + + { + t(int f, + int d); // ) + d(); + } + + Constructor::Constructor(int a, + int b + ) : + BaseClass(a, + b, + c), + mMember(b), + { + } + + Constructor::Constructor(int a, + int b ) : + BaseClass(a) + { + } + + Constructor::Constructor(int a, + int b ) /*x*/ : /*x*/ BaseClass(a), + member(b) + { + } + + A::A(int a, int b) + : aa(a), + bb(b), + cc(c) + { + } + + class CAbc : + public BaseClass1, + protected BaseClass2 + { + int Test() { return FALSE; } + int Test1() { return TRUE; } + + CAbc(int a, int b ) : + BaseClass(a) + { + switch(xxx) + { + case abc: + asdf(); + break; + + case 999: + baer(); + break; + } + } + + public: // <-- this was incoreectly indented before!! + void testfall(); + protected: + void testfall(); + }; + + class CAbc : public BaseClass1, + protected BaseClass2 + { + }; + + static struct + { + int a; + int b; + } variable[COUNT] = + { + { + 123, + 456 + }, + { + 123, + 456 + } + }; + + static struct + { + int a; + int b; + } variable[COUNT] = + { + { 123, 456 }, + { 123, 456 } + }; + + void asdf() /* ind_maxparen may cause trouble here */ + { + if ((0 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1 + && 1)) break; + } + + foo() + { + a = cond ? foo() : asdf + + asdf; + + a = cond ? + foo() : asdf + + asdf; + } + + int main(void) + { + if (a) + if (b) + 2; + else 3; + next_line_of_code(); + } + + barry() + { + Foo::Foo (int one, + int two) + : something(4) + {} + } + + barry() + { + Foo::Foo (int one, int two) + : something(4) + {} + } + + Constructor::Constructor(int a, + int b + ) : + BaseClass(a, + b, + c), + mMember(b) + { + } + int main () + { + if (lala) + do + ++(*lolo); + while (lili + && lele); + lulu; + } + + int main () + { + switch (c) + { + case 'c': if (cond) + { + } + } + } + + main() + { + (void) MyFancyFuasdfadsfnction( + argument); + } + + main() + { + char foo[] = "/*"; + /* as + df */ + hello + } + + /* valid namespaces with normal indent */ + namespace + { + { + 111111111111; + } + } + namespace /* test */ + { + 11111111111111111; + } + namespace // test + { + 111111111111111111; + } + namespace + { + 111111111111111111; + } + namespace test + { + 111111111111111111; + } + namespace{ + 111111111111111111; + } + namespace test{ + 111111111111111111; + } + namespace { + 111111111111111111; + } + namespace test { + 111111111111111111; + namespace test2 { + 22222222222222222; + } + } + inline namespace { + 111111111111111111; + } + inline /* test */ namespace { + 111111111111111111; + } + inline/* test */namespace { + 111111111111111111; + } + + /* invalid namespaces use block indent */ + namespace test test2 { + 111111111111111111111; + } + namespace11111111111 { + 111111111111; + } + namespace() { + 1111111111111; + } + namespace() + { + 111111111111111111; + } + namespace test test2 + { + 1111111111111111111; + } + namespace111111111 + { + 111111111111111111; + } + inlinenamespace { + 111111111111111111; + } + + void getstring() { + /* Raw strings */ + const char* s = R"( + test { + # comment + field: 123 + } + )"; + } + + void getstring() { + const char* s = R"foo( + test { + # comment + field: 123 + } + )foo"; + } + + { + int a[4] = { + [0] = 0, + [1] = 1, + [2] = 2, + [3] = 3, + }; + } + + { + a = b[2] + + 3; + } + + { + if (1) + /* aaaaa + * bbbbb + */ + a = 1; + } + + void func() + { + switch (foo) + { + case (bar): + if (baz()) + quux(); + break; + case (shmoo): + if (!bar) + { + } + case (foo1): + switch (bar) + { + case baz: + baz_f(); + break; + } + break; + default: + baz(); + baz(); + break; + } + } + + /* end of AUTO */ + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_2() + new + setl cindent ts=4 sw=4 + setl tw=0 noai fo=croq + let &wm = &columns - 20 + + let code =<< trim [CODE] + { + + /* this is + * a real serious important big + * comment + */ + /* insert " about life, the universe, and the rest" after "serious" */ + } + [CODE] + + call append(0, code) + normal gg + call search('serious', 'e') + normal a about life, the universe, and the rest + + let expected =<< trim [CODE] + { + + /* this is + * a real serious + * about life, the + * universe, and the + * rest important big + * comment + */ + /* insert " about life, the universe, and the rest" after "serious" */ + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + set wm& + enew! | close +endfunc + +func Test_cindent_3() + new + setl nocindent ts=4 sw=4 + + let code =<< trim [CODE] + { + /* + * Testing for comments, without 'cin' set + */ + + /* + * what happens here? + */ + + /* + the end of the comment, try inserting a line below */ + + /* how about + this one */ + } + [CODE] + + call append(0, code) + normal gg + call search('comments') + normal joabout life + call search('happens') + normal jothere + call search('below') + normal oline + call search('this') + normal Ohello + + let expected =<< trim [CODE] + { + /* + * Testing for comments, without 'cin' set + */ + about life + + /* + * what happens here? + */ + there + + /* + the end of the comment, try inserting a line below */ + line + + /* how about + hello + this one */ + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_4() + new + setl cindent ts=4 sw=4 + + let code =<< trim [CODE] + { + var = this + that + vec[0] * vec[0] + + vec[1] * vec[1] + + vec2[2] * vec[2]; + } + [CODE] + + call append(0, code) + normal gg + call search('vec2') + normal == + + let expected =<< trim [CODE] + { + var = this + that + vec[0] * vec[0] + + vec[1] * vec[1] + + vec2[2] * vec[2]; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_5() + new + setl cindent ts=4 sw=4 + setl cino=}4 + + let code =<< trim [CODE] + { + asdf asdflkajds f; + if (tes & ting) { + asdf asdf asdf ; + asdfa sdf asdf; + } + testing1; + if (tes & ting) + { + asdf asdf asdf ; + asdfa sdf asdf; + } + testing2; + } + [CODE] + + call append(0, code) + normal gg + call search('testing1') + exe "normal k2==/testing2\<CR>" + normal k2== + + let expected =<< trim [CODE] + { + asdf asdflkajds f; + if (tes & ting) { + asdf asdf asdf ; + asdfa sdf asdf; + } + testing1; + if (tes & ting) + { + asdf asdf asdf ; + asdfa sdf asdf; + } + testing2; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_6() + new + setl cindent ts=4 sw=4 + setl cino=(0,)20 + + let code =<< trim [CODE] + main ( int first_par, /* + * Comment for + * first par + */ + int second_par /* + * Comment for + * second par + */ + ) + { + func( first_par, /* + * Comment for + * first par + */ + second_par /* + * Comment for + * second par + */ + ); + + } + [CODE] + + call append(0, code) + normal gg + call search('main') + normal =][ + + let expected =<< trim [CODE] + main ( int first_par, /* + * Comment for + * first par + */ + int second_par /* + * Comment for + * second par + */ + ) + { + func( first_par, /* + * Comment for + * first par + */ + second_par /* + * Comment for + * second par + */ + ); + + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_7() + new + setl cindent ts=4 sw=4 + setl cino=es,n0s + + let code =<< trim [CODE] + main(void) + { + /* Make sure that cino=X0s is not parsed like cino=Xs. */ + if (cond) + foo(); + else + { + bar(); + } + } + [CODE] + + call append(0, code) + normal gg + call search('main') + normal =][ + + let expected =<< trim [CODE] + main(void) + { + /* Make sure that cino=X0s is not parsed like cino=Xs. */ + if (cond) + foo(); + else + { + bar(); + } + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_8() + new + setl cindent ts=4 sw=4 + setl cino= + + let code =<< trim [CODE] + + { + do + { + if () + { + if () + asdf; + else + asdf; + } + } while (); + cmd; /* this should go under the } */ + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + + { + do + { + if () + { + if () + asdf; + else + asdf; + } + } while (); + cmd; /* this should go under the } */ + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_9() + new + setl cindent ts=4 sw=4 + + let code =<< trim [CODE] + + void f() + { + if ( k() ) { + l(); + + } else { /* Start (two words) end */ + m(); + } + + n(); + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + + void f() + { + if ( k() ) { + l(); + + } else { /* Start (two words) end */ + m(); + } + + n(); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_10() + new + setl cindent ts=4 sw=4 + setl cino={s,e-s + + let code =<< trim [CODE] + + void f() + { + if ( k() ) + { + l(); + } else { /* Start (two words) end */ + m(); + } + n(); /* should be under the if () */ + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + + void f() + { + if ( k() ) + { + l(); + } else { /* Start (two words) end */ + m(); + } + n(); /* should be under the if () */ + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_11() + new + setl cindent ts=4 sw=4 + setl cino={s,fs + + let code =<< trim [CODE] + void bar(void) + { + static array[2][2] = + { + { 1, 2 }, + { 3, 4 }, + } + + while (a) + { + foo(&a); + } + + { + int a; + { + a = a + 1; + } + } + b = a; + } + + void func(void) + { + a = 1; + { + b = 2; + } + c = 3; + d = 4; + } + /* foo */ + [CODE] + + call append(0, code) + normal gg + exe "normal ]]=/ foo\<CR>" + + let expected =<< trim [CODE] + void bar(void) + { + static array[2][2] = + { + { 1, 2 }, + { 3, 4 }, + } + + while (a) + { + foo(&a); + } + + { + int a; + { + a = a + 1; + } + } + b = a; + } + + void func(void) + { + a = 1; + { + b = 2; + } + c = 3; + d = 4; + } + /* foo */ + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_12() + new + setl cindent ts=4 sw=4 + setl cino= + + let code =<< trim [CODE] + a() + { + do { + a = a + + a; + } while ( a ); /* add text under this line */ + if ( a ) + a; + } + [CODE] + + call append(0, code) + normal gg + call search('while') + normal ohere + + let expected =<< trim [CODE] + a() + { + do { + a = a + + a; + } while ( a ); /* add text under this line */ + here + if ( a ) + a; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_13() + new + setl cindent ts=4 sw=4 + setl cino= com= + + let code =<< trim [CODE] + a() + { + label1: + /* hmm */ + // comment + } + [CODE] + + call append(0, code) + normal gg + call search('comment') + exe "normal olabel2: b();\rlabel3 /* post */:\r/* pre */ label4:\r" . + \ "f(/*com*/);\rif (/*com*/)\rcmd();" + + let expected =<< trim [CODE] + a() + { + label1: + /* hmm */ + // comment + label2: b(); + label3 /* post */: + /* pre */ label4: + f(/*com*/); + if (/*com*/) + cmd(); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_14() + new + setl cindent ts=4 sw=4 + setl comments& comments^=s:/*,m:**,ex:*/ + + let code =<< trim [CODE] + /* + * A simple comment + */ + + /* + ** A different comment + */ + [CODE] + + call append(0, code) + normal gg + call search('simple') + normal =5j + + let expected =<< trim [CODE] + /* + * A simple comment + */ + + /* + ** A different comment + */ + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_15() + new + setl cindent ts=4 sw=4 + setl cino=c0 + setl comments& comments-=s1:/* comments^=s0:/* + + let code =<< trim [CODE] + void f() + { + + /********* + A comment. + *********/ + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + + /********* + A comment. + *********/ + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_16() + new + setl cindent ts=4 sw=4 + setl cino=c0,C1 + setl comments& comments-=s1:/* comments^=s0:/* + + let code =<< trim [CODE] + void f() + { + + /********* + A comment. + *********/ + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + + /********* + A comment. + *********/ + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_17() + new + setl cindent ts=4 sw=4 + setl cino= + + let code =<< trim [CODE] + void f() + { + c = c1 && + ( + c2 || + c3 + ) && c4; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + c = c1 && + ( + c2 || + c3 + ) && c4; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_18() + new + setl cindent ts=4 sw=4 + setl cino=(s + + let code =<< trim [CODE] + void f() + { + c = c1 && + ( + c2 || + c3 + ) && c4; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + c = c1 && + ( + c2 || + c3 + ) && c4; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_19() + new + setl cindent ts=4 sw=4 + set cino=(s,U1 + + let code =<< trim [CODE] + void f() + { + c = c1 && + ( + c2 || + c3 + ) && c4; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + c = c1 && + ( + c2 || + c3 + ) && c4; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_20() + new + setl cindent ts=4 sw=4 + setl cino=(0 + + let code =<< trim [CODE] + void f() + { + if ( c1 + && ( c2 + || c3)) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + if ( c1 + && ( c2 + || c3)) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_21() + new + setl cindent ts=4 sw=4 + setl cino=(0,w1 + + let code =<< trim [CODE] + void f() + { + if ( c1 + && ( c2 + || c3)) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + if ( c1 + && ( c2 + || c3)) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_22() + new + setl cindent ts=4 sw=4 + setl cino=(s + + let code =<< trim [CODE] + void f() + { + c = c1 && ( + c2 || + c3 + ) && c4; + if ( + c1 && c2 + ) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + c = c1 && ( + c2 || + c3 + ) && c4; + if ( + c1 && c2 + ) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_23() + new + setl cindent ts=4 sw=4 + setl cino=(s,m1 + + let code =<< trim [CODE] + void f() + { + c = c1 && ( + c2 || + c3 + ) && c4; + if ( + c1 && c2 + ) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + c = c1 && ( + c2 || + c3 + ) && c4; + if ( + c1 && c2 + ) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_24() + new + setl cindent ts=4 sw=4 + setl cino=b1 + + let code =<< trim [CODE] + void f() + { + switch (x) + { + case 1: + a = b; + break; + default: + a = 0; + break; + } + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + switch (x) + { + case 1: + a = b; + break; + default: + a = 0; + break; + } + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_25() + new + setl cindent ts=4 sw=4 + setl cino=(0,W5 + + let code =<< trim [CODE] + void f() + { + invokeme( + argu, + ment); + invokeme( + argu, + ment + ); + invokeme(argu, + ment + ); + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + invokeme( + argu, + ment); + invokeme( + argu, + ment + ); + invokeme(argu, + ment + ); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_26() + new + setl cindent ts=4 sw=4 + setl cino=/6 + + let code =<< trim [CODE] + void f() + { + statement; + // comment 1 + // comment 2 + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void f() + { + statement; + // comment 1 + // comment 2 + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_27() + new + setl cindent ts=4 sw=4 + setl cino= + + let code =<< trim [CODE] + void f() + { + statement; + // comment 1 + // comment 2 + } + [CODE] + + call append(0, code) + normal gg + exe "normal ]]/comment 1/+1\<CR>==" + + let expected =<< trim [CODE] + void f() + { + statement; + // comment 1 + // comment 2 + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_28() + new + setl cindent ts=4 sw=4 + setl cino=g0 + + let code =<< trim [CODE] + class CAbc + { + int Test() { return FALSE; } + + public: // comment + void testfall(); + protected: + void testfall(); + }; + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + class CAbc + { + int Test() { return FALSE; } + + public: // comment + void testfall(); + protected: + void testfall(); + }; + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_29() + new + setl cindent ts=4 sw=4 + setl cino=(0,gs,hs + + let code =<< trim [CODE] + class Foo : public Bar + { + public: + virtual void method1(void) = 0; + virtual void method2(int arg1, + int arg2, + int arg3) = 0; + }; + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + class Foo : public Bar + { + public: + virtual void method1(void) = 0; + virtual void method2(int arg1, + int arg2, + int arg3) = 0; + }; + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_30() + new + setl cindent ts=4 sw=4 + setl cino=+20 + + let code =<< [CODE] + void +foo() +{ + if (a) + { + } else + asdf; +} +[CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< [CODE] + void +foo() +{ + if (a) + { + } else + asdf; +} + +[CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_31() + new + setl cindent ts=4 sw=4 + setl cino=(0,W2s + + let code =<< trim [CODE] + + { + averylongfunctionnamelongfunctionnameaverylongfunctionname()->asd( + asdasdf, + func(asdf, + asdfadsf), + asdfasdf + ); + + /* those are ugly, but consequent */ + + func()->asd(asdasdf, + averylongfunctionname( + abc, + dec)->averylongfunctionname( + asdfadsf, + asdfasdf, + asdfasdf, + ), + func(asdfadf, + asdfasdf + ), + asdasdf + ); + + averylongfunctionnameaverylongfunctionnameavery()->asd(fasdf( + abc, + dec)->asdfasdfasdf( + asdfadsf, + asdfasdf, + asdfasdf, + ), + func(asdfadf, + asdfasdf), + asdasdf + ); + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + + { + averylongfunctionnamelongfunctionnameaverylongfunctionname()->asd( + asdasdf, + func(asdf, + asdfadsf), + asdfasdf + ); + + /* those are ugly, but consequent */ + + func()->asd(asdasdf, + averylongfunctionname( + abc, + dec)->averylongfunctionname( + asdfadsf, + asdfasdf, + asdfasdf, + ), + func(asdfadf, + asdfasdf + ), + asdasdf + ); + + averylongfunctionnameaverylongfunctionnameavery()->asd(fasdf( + abc, + dec)->asdfasdfasdf( + asdfadsf, + asdfasdf, + asdfasdf, + ), + func(asdfadf, + asdfasdf), + asdasdf + ); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_32() + new + setl cindent ts=4 sw=4 + setl cino=M1 + + let code =<< trim [CODE] + int main () + { + if (cond1 && + cond2 + ) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + int main () + { + if (cond1 && + cond2 + ) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_33() + new + setl cindent ts=4 sw=4 + setl cino=(0,ts + + let code =<< trim [CODE] + void func(int a + #if defined(FOO) + , int b + , int c + #endif + ) + { + } + [CODE] + + call append(0, code) + normal gg + normal 2j=][ + + let expected =<< trim [CODE] + void func(int a + #if defined(FOO) + , int b + , int c + #endif + ) + { + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_34() + new + setl cindent ts=4 sw=4 + setl cino=(0 + + let code =<< trim [CODE] + + void + func(int a + #if defined(FOO) + , int b + , int c + #endif + ) + { + } + [CODE] + + call append(0, code) + normal gg + normal =][ + + let expected =<< trim [CODE] + + void + func(int a + #if defined(FOO) + , int b + , int c + #endif + ) + { + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_35() + new + setl cindent ts=4 sw=4 + setl cino& + + let code =<< trim [CODE] + void func(void) + { + if(x==y) + if(y==z) + foo=1; + else { bar=1; + baz=2; + } + printf("Foo!\n"); + } + + void func1(void) + { + char* tab[] = {"foo", "bar", + "baz", "quux", + "this line used", "to be indented incorrectly"}; + foo(); + } + + void func2(void) + { + int tab[] = + {1, 2, + 3, 4, + 5, 6}; + + printf("This line used to be indented incorrectly.\n"); + } + + int foo[] + #ifdef BAR + + = { 1, 2, 3, + 4, 5, 6 } + + #endif + ; + int baz; + + void func3(void) + { + int tab[] = { + 1, 2, + 3, 4, + 5, 6}; + + printf("Don't you dare indent this line incorrectly!\n"); + } + + void + func4(a, b, + c) + int a; + int b; + int c; + { + } + + void + func5( + int a, + int b) + { + } + + void + func6( + int a) + { + } + [CODE] + + call append(0, code) + normal gg + normal ]]=7][ + + let expected =<< trim [CODE] + void func(void) + { + if(x==y) + if(y==z) + foo=1; + else { bar=1; + baz=2; + } + printf("Foo!\n"); + } + + void func1(void) + { + char* tab[] = {"foo", "bar", + "baz", "quux", + "this line used", "to be indented incorrectly"}; + foo(); + } + + void func2(void) + { + int tab[] = + {1, 2, + 3, 4, + 5, 6}; + + printf("This line used to be indented incorrectly.\n"); + } + + int foo[] + #ifdef BAR + + = { 1, 2, 3, + 4, 5, 6 } + + #endif + ; + int baz; + + void func3(void) + { + int tab[] = { + 1, 2, + 3, 4, + 5, 6}; + + printf("Don't you dare indent this line incorrectly!\n"); + } + + void + func4(a, b, + c) + int a; + int b; + int c; + { + } + + void + func5( + int a, + int b) + { + } + + void + func6( + int a) + { + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_36() + new + setl cindent ts=4 sw=4 + setl cino& + setl cino+=l1 + + let code =<< trim [CODE] + void func(void) + { + int tab[] = + { + 1, 2, 3, + 4, 5, 6}; + + printf("Indent this line correctly!\n"); + + switch (foo) + { + case bar: + printf("bar"); + break; + case baz: { + printf("baz"); + break; + } + case quux: + printf("But don't break the indentation of this instruction\n"); + break; + } + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + int tab[] = + { + 1, 2, 3, + 4, 5, 6}; + + printf("Indent this line correctly!\n"); + + switch (foo) + { + case bar: + printf("bar"); + break; + case baz: { + printf("baz"); + break; + } + case quux: + printf("But don't break the indentation of this instruction\n"); + break; + } + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_37() + new + setl cindent ts=4 sw=4 + setl cino& + + let code =<< trim [CODE] + void func(void) + { + cout << "a" + << "b" + << ") :" + << "c"; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + cout << "a" + << "b" + << ") :" + << "c"; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_38() + new + setl cindent ts=4 sw=4 + setl com=s1:/*,m:*,ex:*/ + + let code =<< trim [CODE] + void func(void) + { + /* + * This is a comment. + */ + } + [CODE] + + call append(0, code) + normal gg + normal ]]3jofoo(); + + let expected =<< trim [CODE] + void func(void) + { + /* + * This is a comment. + */ + foo(); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_39() + new + setl cindent ts=4 sw=4 + setl cino& + + let code =<< trim [CODE] + void func(void) + { + for (int i = 0; i < 10; ++i) + if (i & 1) { + foo(1); + } else + foo(0); + baz(); + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + for (int i = 0; i < 10; ++i) + if (i & 1) { + foo(1); + } else + foo(0); + baz(); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_40() + new + setl cindent ts=4 sw=4 + setl cino=k2s,(0 + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + + if ( c1 + && ( c2 + || c3)) + foo; + func( c1 + && ( c2 + || c3)) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + + if ( c1 + && ( c2 + || c3)) + foo; + func( c1 + && ( c2 + || c3)) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_41() + new + setl cindent ts=4 sw=4 + setl cino=k2s,(s + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + + if ( c1 + && ( c2 + || c3)) + foo; + func( c1 + && ( c2 + || c3)) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + + if ( c1 + && ( c2 + || c3)) + foo; + func( c1 + && ( c2 + || c3)) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_42() + new + setl cindent ts=4 sw=4 + setl cino=k2s,(s,U1 + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + + c = c1 && + ( + c2 || + c3 + ) && c4; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + + c = c1 && + ( + c2 || + c3 + ) && c4; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_43() + new + setl cindent ts=4 sw=4 + setl cino=k2s,(0,W4 + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + + if ( c1 + && ( c2 + || c3)) + foo; + + a_long_line( + argument, + argument); + a_short_line(argument, + argument); + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + + if ( c1 + && ( c2 + || c3)) + foo; + + a_long_line( + argument, + argument); + a_short_line(argument, + argument); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_44() + new + setl cindent ts=4 sw=4 + setl cino=k2s,u2 + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_45() + new + setl cindent ts=4 sw=4 + setl cino=k2s,(0,w1 + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + + if ( c1 + && ( c2 + || c3)) + foo; + func( c1 + && ( c2 + || c3)) + foo; + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + if (c123456789 + && (c22345 + || c3)) + printf("foo\n"); + + if ( c1 + && ( c2 + || c3)) + foo; + func( c1 + && ( c2 + || c3)) + foo; + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_46() + new + setl cindent ts=4 sw=4 + setl cino=k2,(s + + let code =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + } + [CODE] + + call append(0, code) + normal gg + normal ]]=][ + + let expected =<< trim [CODE] + void func(void) + { + if (condition1 + && condition2) + action(); + function(argument1 + && argument2); + + if (c1 && (c2 || + c3)) + foo; + if (c1 && + (c2 || c3)) + { + } + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_47() + new + setl cindent ts=4 sw=4 + setl cino=N-s + + let code =<< trim [CODE] + NAMESPACESTART + /* valid namespaces with normal indent */ + namespace + { + { + 111111111111; + } + } + namespace /* test */ + { + 11111111111111111; + } + namespace // test + { + 111111111111111111; + } + namespace + { + 111111111111111111; + } + namespace test + { + 111111111111111111; + } + namespace test::cpp17 + { + 111111111111111111; + } + namespace ::incorrectcpp17 + { + 111111111111111111; + } + namespace test::incorrectcpp17:: + { + 111111111111111111; + } + namespace test:incorrectcpp17 + { + 111111111111111111; + } + namespace test:::incorrectcpp17 + { + 111111111111111111; + } + namespace{ + 111111111111111111; + } + namespace test{ + 111111111111111111; + } + namespace { + 111111111111111111; + } + namespace test { + 111111111111111111; + namespace test2 { + 22222222222222222; + } + } + inline namespace { + 111111111111111111; + } + inline /* test */ namespace { + 111111111111111111; + } + inline/* test */namespace { + 111111111111111111; + } + + /* invalid namespaces use block indent */ + namespace test test2 { + 111111111111111111111; + } + namespace11111111111 { + 111111111111; + } + namespace() { + 1111111111111; + } + namespace() + { + 111111111111111111; + } + namespace test test2 + { + 1111111111111111111; + } + namespace111111111 + { + 111111111111111111; + } + inlinenamespace { + 111111111111111111; + } + NAMESPACEEND + [CODE] + + call append(0, code) + normal gg + call search('^NAMESPACESTART') + exe "normal =/^NAMESPACEEND\n" + + let expected =<< trim [CODE] + NAMESPACESTART + /* valid namespaces with normal indent */ + namespace + { + { + 111111111111; + } + } + namespace /* test */ + { + 11111111111111111; + } + namespace // test + { + 111111111111111111; + } + namespace + { + 111111111111111111; + } + namespace test + { + 111111111111111111; + } + namespace test::cpp17 + { + 111111111111111111; + } + namespace ::incorrectcpp17 + { + 111111111111111111; + } + namespace test::incorrectcpp17:: + { + 111111111111111111; + } + namespace test:incorrectcpp17 + { + 111111111111111111; + } + namespace test:::incorrectcpp17 + { + 111111111111111111; + } + namespace{ + 111111111111111111; + } + namespace test{ + 111111111111111111; + } + namespace { + 111111111111111111; + } + namespace test { + 111111111111111111; + namespace test2 { + 22222222222222222; + } + } + inline namespace { + 111111111111111111; + } + inline /* test */ namespace { + 111111111111111111; + } + inline/* test */namespace { + 111111111111111111; + } + + /* invalid namespaces use block indent */ + namespace test test2 { + 111111111111111111111; + } + namespace11111111111 { + 111111111111; + } + namespace() { + 1111111111111; + } + namespace() + { + 111111111111111111; + } + namespace test test2 + { + 1111111111111111111; + } + namespace111111111 + { + 111111111111111111; + } + inlinenamespace { + 111111111111111111; + } + NAMESPACEEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_48() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1 + + let code =<< trim [CODE] + JSSTART + var bar = { + foo: { + that: this, + some: ok, + }, + "bar":{ + a : 2, + b: "123abc", + x: 4, + "y": 5 + } + } + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + var bar = { + foo: { + that: this, + some: ok, + }, + "bar":{ + a : 2, + b: "123abc", + x: 4, + "y": 5 + } + } + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_49() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1 + + let code =<< trim [CODE] + JSSTART + var foo = [ + 1, + 2, + 3 + ]; + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + var foo = [ + 1, + 2, + 3 + ]; + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_50() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1 + + let code =<< trim [CODE] + JSSTART + function bar() { + var foo = [ + 1, + 2, + 3 + ]; + } + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + function bar() { + var foo = [ + 1, + 2, + 3 + ]; + } + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_51() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1 + + let code =<< trim [CODE] + JSSTART + (function($){ + + if (cond && + cond) { + stmt; + } + window.something.left = + (width - 50 + offset) + "px"; + var class_name='myclass'; + + function private_method() { + } + + var public_method={ + method: function(options,args){ + private_method(); + } + } + + function init(options) { + + $(this).data(class_name+'_public',$.extend({},{ + foo: 'bar', + bar: 2, + foobar: [ + 1, + 2, + 3 + ], + callback: function(){ + return true; + } + }, options||{})); + } + + $.fn[class_name]=function() { + + var _arguments=arguments; + return this.each(function(){ + + var options=$(this).data(class_name+'_public'); + if (!options) { + init.apply(this,_arguments); + + } else { + var method=public_method[_arguments[0]]; + + if (typeof(method)!='function') { + console.log(class_name+' has no method "'+_arguments[0]+'"'); + return false; + } + _arguments[0]=options; + method.apply(this,_arguments); + } + }); + } + + })(jQuery); + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + (function($){ + + if (cond && + cond) { + stmt; + } + window.something.left = + (width - 50 + offset) + "px"; + var class_name='myclass'; + + function private_method() { + } + + var public_method={ + method: function(options,args){ + private_method(); + } + } + + function init(options) { + + $(this).data(class_name+'_public',$.extend({},{ + foo: 'bar', + bar: 2, + foobar: [ + 1, + 2, + 3 + ], + callback: function(){ + return true; + } + }, options||{})); + } + + $.fn[class_name]=function() { + + var _arguments=arguments; + return this.each(function(){ + + var options=$(this).data(class_name+'_public'); + if (!options) { + init.apply(this,_arguments); + + } else { + var method=public_method[_arguments[0]]; + + if (typeof(method)!='function') { + console.log(class_name+' has no method "'+_arguments[0]+'"'); + return false; + } + _arguments[0]=options; + method.apply(this,_arguments); + } + }); + } + + })(jQuery); + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_52() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1 + + let code =<< trim [CODE] + JSSTART + function init(options) { + $(this).data(class_name+'_public',$.extend({},{ + foo: 'bar', + bar: 2, + foobar: [ + 1, + 2, + 3 + ], + callback: function(){ + return true; + } + }, options||{})); + } + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + function init(options) { + $(this).data(class_name+'_public',$.extend({},{ + foo: 'bar', + bar: 2, + foobar: [ + 1, + 2, + 3 + ], + callback: function(){ + return true; + } + }, options||{})); + } + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_53() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1 + + let code =<< trim [CODE] + JSSTART + (function($){ + function init(options) { + $(this).data(class_name+'_public',$.extend({},{ + foo: 'bar', + bar: 2, + foobar: [ + 1, + 2, + 3 + ], + callback: function(){ + return true; + } + }, options||{})); + } + })(jQuery); + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + (function($){ + function init(options) { + $(this).data(class_name+'_public',$.extend({},{ + foo: 'bar', + bar: 2, + foobar: [ + 1, + 2, + 3 + ], + callback: function(){ + return true; + } + }, options||{})); + } + })(jQuery); + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_54() + new + setl cindent ts=4 sw=4 + setl cino=j1,J1,+2 + + let code =<< trim [CODE] + JSSTART + // Results of JavaScript indent + // 1 + (function(){ + var a = [ + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + }()) + + // 2 + (function(){ + var a = [ + 0 + + 5 * + 9 * + 'a', + 'b', + 0 + + 5 * + 9 * + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + }()) + + // 3 + (function(){ + var a = [ + 0 + + // comment 1 + 5 * + /* comment 2 */ + 9 * + 'a', + 'b', + 0 + + 5 * + 9 * + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + }()) + + // 4 + { + var a = [ + 0, + 1 + ]; + var b; + var c; + } + + // 5 + { + var a = [ + [ + 0 + ], + 2, + 3 + ]; + } + + // 6 + { + var a = [ + [ + 0, + 1 + ], + 2, + 3 + ]; + } + + // 7 + { + var a = [ + // [ + 0, + // 1 + // ], + 2, + 3 + ]; + } + + // 8 + var x = [ + (function(){ + var a, + b, + c, + d, + e, + f, + g, + h, + i; + }) + ]; + + // 9 + var a = [ + 0 + + 5 * + 9 * + 'a', + 'b', + 0 + + 5 * + 9 * + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + + // 10 + var a, + b, + c, + d, + e, + f, + g, + h, + i; + JSEND + [CODE] + + call append(0, code) + normal gg + call search('^JSSTART') + exe "normal =/^JSEND\n" + + let expected =<< trim [CODE] + JSSTART + // Results of JavaScript indent + // 1 + (function(){ + var a = [ + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + }()) + + // 2 + (function(){ + var a = [ + 0 + + 5 * + 9 * + 'a', + 'b', + 0 + + 5 * + 9 * + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + }()) + + // 3 + (function(){ + var a = [ + 0 + + // comment 1 + 5 * + /* comment 2 */ + 9 * + 'a', + 'b', + 0 + + 5 * + 9 * + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + }()) + + // 4 + { + var a = [ + 0, + 1 + ]; + var b; + var c; + } + + // 5 + { + var a = [ + [ + 0 + ], + 2, + 3 + ]; + } + + // 6 + { + var a = [ + [ + 0, + 1 + ], + 2, + 3 + ]; + } + + // 7 + { + var a = [ + // [ + 0, + // 1 + // ], + 2, + 3 + ]; + } + + // 8 + var x = [ + (function(){ + var a, + b, + c, + d, + e, + f, + g, + h, + i; + }) + ]; + + // 9 + var a = [ + 0 + + 5 * + 9 * + 'a', + 'b', + 0 + + 5 * + 9 * + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'i' + ]; + + // 10 + var a, + b, + c, + d, + e, + f, + g, + h, + i; + JSEND + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_55() + new + setl cindent ts=4 sw=4 + setl cino& + + let code =<< trim [CODE] + /* start of define */ + { + } + #define AAA \ + BBB\ + CCC + + #define CNT \ + 1 + \ + 2 + \ + 4 + /* end of define */ + [CODE] + + call append(0, code) + normal gg + call search('start of define') + exe "normal =/end of define\n" + + let expected =<< trim [CODE] + /* start of define */ + { + } + #define AAA \ + BBB\ + CCC + + #define CNT \ + 1 + \ + 2 + \ + 4 + /* end of define */ + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + +func Test_cindent_56() + new + setl cindent ts=4 sw=4 + setl cino& + + let code =<< trim [CODE] + { + a = second/*bug*/*line; + } + [CODE] + + call append(0, code) + normal gg + call search('a = second') + normal ox + + let expected =<< trim [CODE] + { + a = second/*bug*/*line; + x + } + + [CODE] + + call assert_equal(expected, getline(1, '$')) + enew! | close +endfunc + " this was going beyond the end of the line. func Test_cindent_case() new @@ -173,4 +5344,23 @@ func Test_cindent_pragma() enew! | close endfunc +func Test_backslash_at_end_of_line() + new + exe "norm v>O'\\\<C-m>-" + exe "norm \<C-q>=" + bwipe! +endfunc + +func Test_find_brace_backwards() + " this was looking beyond the end of the line + new + norm R/* + norm o0{ + norm o// + norm V{= + call assert_equal(['/*', ' 0{', '//'], getline(1, 3)) + bwipe! +endfunc + + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_cmdline.vim b/src/nvim/testdir/test_cmdline.vim index 1672b0e840..ff4cbe544c 100644 --- a/src/nvim/testdir/test_cmdline.vim +++ b/src/nvim/testdir/test_cmdline.vim @@ -500,8 +500,16 @@ func Test_fullcommand() for [in, want] in items(tests) call assert_equal(want, fullcommand(in)) endfor + call assert_equal('', fullcommand(v:_null_string)) call assert_equal('syntax', 'syn'->fullcommand()) + + command -buffer BufferLocalCommand : + command GlobalCommand : + call assert_equal('GlobalCommand', fullcommand('GlobalCom')) + call assert_equal('BufferLocalCommand', fullcommand('BufferL')) + delcommand BufferLocalCommand + delcommand GlobalCommand endfunc func Test_shellcmd_completion() diff --git a/src/nvim/testdir/test_compiler.vim b/src/nvim/testdir/test_compiler.vim index aaa2301bca..c0c572ce65 100644 --- a/src/nvim/testdir/test_compiler.vim +++ b/src/nvim/testdir/test_compiler.vim @@ -19,6 +19,9 @@ func Test_compiler() call assert_equal('perl', b:current_compiler) call assert_fails('let g:current_compiler', 'E121:') + let verbose_efm = execute('verbose set efm') + call assert_match('Last set from .*[/\\]compiler[/\\]perl.vim ', verbose_efm) + call setline(1, ['#!/usr/bin/perl -w', 'use strict;', 'my $foo=1']) w! call feedkeys(":make\<CR>\<CR>", 'tx') diff --git a/src/nvim/testdir/test_conceal.vim b/src/nvim/testdir/test_conceal.vim index 1306dbe5cf..bffc2f49d3 100644 --- a/src/nvim/testdir/test_conceal.vim +++ b/src/nvim/testdir/test_conceal.vim @@ -4,10 +4,10 @@ source check.vim CheckFeature conceal source screendump.vim -" CheckScreendump func Test_conceal_two_windows() CheckScreendump + let code =<< trim [CODE] let lines = ["one one one one one", "two |hidden| here", "three |hidden| three"] call setline(1, lines) @@ -111,6 +111,7 @@ endfunc func Test_conceal_with_cursorline() CheckScreendump + " Opens a help window, where 'conceal' is set, switches to the other window " where 'cursorline' needs to be updated when the cursor moves. let code =<< trim [CODE] @@ -139,6 +140,7 @@ endfunc func Test_conceal_resize_term() CheckScreendump + let code =<< trim [CODE] call setline(1, '`one` `two` `three` `four` `five`, the backticks should be concealed') setl cocu=n cole=3 diff --git a/src/nvim/testdir/test_cscope.vim b/src/nvim/testdir/test_cscope.vim index cc6154af69..faf37485cd 100644 --- a/src/nvim/testdir/test_cscope.vim +++ b/src/nvim/testdir/test_cscope.vim @@ -102,7 +102,7 @@ func Test_cscopeWithCscopeConnections() for cmd in ['cs find f Xmemfile_test.c', 'cs find 7 Xmemfile_test.c'] enew let a = execute(cmd) - call assert_true(a =~ '"Xmemfile_test.c" \d\+L, \d\+C') + call assert_true(a =~ '"Xmemfile_test.c" \d\+L, \d\+B') call assert_equal('Xmemfile_test.c', @%) endfor @@ -112,7 +112,7 @@ func Test_cscopeWithCscopeConnections() let a = execute(cmd) let alines = split(a, '\n', 1) call assert_equal('', alines[0]) - call assert_true(alines[1] =~ '"Xmemfile_test.c" \d\+L, \d\+C') + call assert_true(alines[1] =~ '"Xmemfile_test.c" \d\+L, \d\+B') call assert_equal('(1 of 1): <<global>> #include <assert.h>', alines[2]) call assert_equal('#include <assert.h>', getline('.')) endfor diff --git a/src/nvim/testdir/test_cursor_func.vim b/src/nvim/testdir/test_cursor_func.vim index e8c4a952ee..57825b4551 100644 --- a/src/nvim/testdir/test_cursor_func.vim +++ b/src/nvim/testdir/test_cursor_func.vim @@ -1,4 +1,4 @@ -" Tests for cursor(). +" Tests for cursor() and other functions that get/set the cursor position func Test_wrong_arguments() call assert_fails('call cursor(1. 3)', 'E474:') @@ -24,6 +24,9 @@ func Test_move_cursor() " below last line goes to last line call cursor(9, 1) call assert_equal([4, 1, 0, 1], getcurpos()[1:]) + " pass string arguments + call cursor('3', '3') + call assert_equal([3, 3, 0, 3], getcurpos()[1:]) call setline(1, ["\<TAB>"]) call cursor(1, 1, 1) @@ -72,7 +75,6 @@ func Test_curswant_with_cursorline() endfunc func Test_screenpos() - throw 'skipped: TODO: ' rightbelow new rightbelow 20vsplit call setline(1, ["\tsome text", "long wrapping line here", "next line"]) @@ -100,9 +102,10 @@ func Test_screenpos() bwipe! call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) - nmenu WinBar.TEST : - call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) - nunmenu WinBar.TEST + " Needs WinBar + " nmenu WinBar.TEST : + " call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) + " nunmenu WinBar.TEST endfunc func Test_screenpos_number() @@ -119,3 +122,247 @@ func Test_screenpos_number() close bwipe! endfunc + +" Save the visual start character position +func SaveVisualStartCharPos() + call add(g:VisualStartPos, getcharpos('v')) + return '' +endfunc + +" Save the current cursor character position in insert mode +func SaveInsertCurrentCharPos() + call add(g:InsertCurrentPos, getcharpos('.')) + return '' +endfunc + +" Test for the getcharpos() function +func Test_getcharpos() + call assert_fails('call getcharpos({})', 'E731:') + call assert_equal([0, 0, 0, 0], getcharpos(0)) + new + call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) + + " Test for '.' and '$' + normal 1G + call assert_equal([0, 1, 1, 0], getcharpos('.')) + call assert_equal([0, 4, 1, 0], getcharpos('$')) + normal 2G6l + call assert_equal([0, 2, 7, 0], getcharpos('.')) + normal 3G$ + call assert_equal([0, 3, 1, 0], getcharpos('.')) + normal 4G$ + call assert_equal([0, 4, 9, 0], getcharpos('.')) + + " Test for a mark + normal 2G7lmmgg + call assert_equal([0, 2, 8, 0], getcharpos("'m")) + delmarks m + call assert_equal([0, 0, 0, 0], getcharpos("'m")) + + " Test for the visual start column + vnoremap <expr> <F3> SaveVisualStartCharPos() + let g:VisualStartPos = [] + exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>" + call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos) + call assert_equal([0, 2, 9, 0], getcharpos('v')) + let g:VisualStartPos = [] + exe "normal 3Gv$\<F3>o\<F3>" + call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos) + let g:VisualStartPos = [] + exe "normal 1Gv$\<F3>o\<F3>" + call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos) + vunmap <F3> + + " Test for getting the position in insert mode with the cursor after the + " last character in a line + inoremap <expr> <F3> SaveInsertCurrentCharPos() + let g:InsertCurrentPos = [] + exe "normal 1GA\<F3>" + exe "normal 2GA\<F3>" + exe "normal 3GA\<F3>" + exe "normal 4GA\<F3>" + exe "normal 2G6li\<F3>" + call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0], + \ [0, 2, 7, 0]], g:InsertCurrentPos) + iunmap <F3> + + %bw! +endfunc + +" Test for the setcharpos() function +func Test_setcharpos() + call assert_equal(-1, setcharpos('.', v:_null_list)) + new + call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) + call setcharpos('.', [0, 1, 1, 0]) + call assert_equal([1, 1], [line('.'), col('.')]) + call setcharpos('.', [0, 2, 7, 0]) + call assert_equal([2, 9], [line('.'), col('.')]) + call setcharpos('.', [0, 3, 4, 0]) + call assert_equal([3, 1], [line('.'), col('.')]) + call setcharpos('.', [0, 3, 1, 0]) + call assert_equal([3, 1], [line('.'), col('.')]) + call setcharpos('.', [0, 4, 0, 0]) + call assert_equal([4, 1], [line('.'), col('.')]) + call setcharpos('.', [0, 4, 20, 0]) + call assert_equal([4, 9], [line('.'), col('.')]) + + " Test for mark + delmarks m + call setcharpos("'m", [0, 2, 9, 0]) + normal `m + call assert_equal([2, 11], [line('.'), col('.')]) + " unload the buffer and try to set the mark + let bnr = bufnr() + enew! + call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0])) + + %bw! + call assert_equal(-1, setcharpos('.', [10, 3, 1, 0])) +endfunc + +func SaveVisualStartCharCol() + call add(g:VisualStartCol, charcol('v')) + return '' +endfunc + +func SaveInsertCurrentCharCol() + call add(g:InsertCurrentCol, charcol('.')) + return '' +endfunc + +" Test for the charcol() function +func Test_charcol() + call assert_fails('call charcol({})', 'E731:') + call assert_equal(0, charcol(0)) + new + call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) + + " Test for '.' and '$' + normal 1G + call assert_equal(1, charcol('.')) + call assert_equal(1, charcol('$')) + normal 2G6l + call assert_equal(7, charcol('.')) + call assert_equal(10, charcol('$')) + normal 3G$ + call assert_equal(1, charcol('.')) + call assert_equal(2, charcol('$')) + normal 4G$ + call assert_equal(9, charcol('.')) + call assert_equal(10, charcol('$')) + + " Test for [lnum, '$'] + call assert_equal(1, charcol([1, '$'])) + call assert_equal(10, charcol([2, '$'])) + call assert_equal(2, charcol([3, '$'])) + call assert_equal(0, charcol([5, '$'])) + + " Test for a mark + normal 2G7lmmgg + call assert_equal(8, charcol("'m")) + delmarks m + call assert_equal(0, charcol("'m")) + + " Test for the visual start column + vnoremap <expr> <F3> SaveVisualStartCharCol() + let g:VisualStartCol = [] + exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>" + call assert_equal([7, 10, 5], g:VisualStartCol) + call assert_equal(9, charcol('v')) + let g:VisualStartCol = [] + exe "normal 3Gv$\<F3>o\<F3>" + call assert_equal([1, 2], g:VisualStartCol) + let g:VisualStartCol = [] + exe "normal 1Gv$\<F3>o\<F3>" + call assert_equal([1, 1], g:VisualStartCol) + vunmap <F3> + + " Test for getting the column number in insert mode with the cursor after + " the last character in a line + inoremap <expr> <F3> SaveInsertCurrentCharCol() + let g:InsertCurrentCol = [] + exe "normal 1GA\<F3>" + exe "normal 2GA\<F3>" + exe "normal 3GA\<F3>" + exe "normal 4GA\<F3>" + exe "normal 2G6li\<F3>" + call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol) + iunmap <F3> + + %bw! +endfunc + +func SaveInsertCursorCharPos() + call add(g:InsertCursorPos, getcursorcharpos('.')) + return '' +endfunc + +" Test for getcursorcharpos() +func Test_getcursorcharpos() + call assert_equal(getcursorcharpos(), getcursorcharpos(0)) + call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1)) + call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999)) + + new + call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) + normal 1G9l + call assert_equal([0, 1, 1, 0, 1], getcursorcharpos()) + normal 2G9l + call assert_equal([0, 2, 9, 0, 14], getcursorcharpos()) + normal 3G9l + call assert_equal([0, 3, 1, 0, 1], getcursorcharpos()) + normal 4G9l + call assert_equal([0, 4, 9, 0, 9], getcursorcharpos()) + + " Test for getting the cursor position in insert mode with the cursor after + " the last character in a line + inoremap <expr> <F3> SaveInsertCursorCharPos() + let g:InsertCursorPos = [] + exe "normal 1GA\<F3>" + exe "normal 2GA\<F3>" + exe "normal 3GA\<F3>" + exe "normal 4GA\<F3>" + exe "normal 2G6li\<F3>" + call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2], + \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos) + iunmap <F3> + + let winid = win_getid() + normal 2G5l + wincmd w + call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid)) + %bw! +endfunc + +" Test for setcursorcharpos() +func Test_setcursorcharpos() + call assert_fails('call setcursorcharpos(v:_null_list)', 'E474:') + call assert_fails('call setcursorcharpos([1])', 'E474:') + call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:') + new + call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) + normal G + call setcursorcharpos([1, 1]) + call assert_equal([1, 1], [line('.'), col('.')]) + call setcursorcharpos([2, 7, 0]) + call assert_equal([2, 9], [line('.'), col('.')]) + call setcursorcharpos(3, 4) + call assert_equal([3, 1], [line('.'), col('.')]) + call setcursorcharpos([3, 1]) + call assert_equal([3, 1], [line('.'), col('.')]) + call setcursorcharpos([4, 0, 0, 0]) + call assert_equal([4, 1], [line('.'), col('.')]) + call setcursorcharpos([4, 20]) + call assert_equal([4, 9], [line('.'), col('.')]) + normal 1G + call setcursorcharpos([100, 100, 100, 100]) + call assert_equal([4, 9], [line('.'), col('.')]) + normal 1G + call setcursorcharpos('$', 1) + call assert_equal([4, 1], [line('.'), col('.')]) + + %bw! +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_diffmode.vim b/src/nvim/testdir/test_diffmode.vim index 3a0c615cf6..482d39056f 100644 --- a/src/nvim/testdir/test_diffmode.vim +++ b/src/nvim/testdir/test_diffmode.vim @@ -1146,6 +1146,35 @@ func Test_diff_followwrap() bwipe! endfunc +func Test_diff_maintains_change_mark() + func DiffMaintainsChangeMark() + enew! + call setline(1, ['a', 'b', 'c', 'd']) + diffthis + new + call setline(1, ['a', 'b', 'c', 'e']) + " Set '[ and '] marks + 2,3yank + call assert_equal([2, 3], [line("'["), line("']")]) + " Verify they aren't affected by the implicit diff + diffthis + call assert_equal([2, 3], [line("'["), line("']")]) + " Verify they aren't affected by an explicit diff + diffupdate + call assert_equal([2, 3], [line("'["), line("']")]) + bwipe! + bwipe! + endfunc + + set diffopt-=internal + call DiffMaintainsChangeMark() + set diffopt+=internal + call DiffMaintainsChangeMark() + + set diffopt& + delfunc DiffMaintainsChangeMark +endfunc + func Test_diff_rnu() CheckScreendump diff --git a/src/nvim/testdir/test_display.vim b/src/nvim/testdir/test_display.vim index c2a9683f7c..9f74d0a38a 100644 --- a/src/nvim/testdir/test_display.vim +++ b/src/nvim/testdir/test_display.vim @@ -41,7 +41,7 @@ func Test_display_foldcolumn() quit! endfunc -func! Test_display_foldtext_mbyte() +func Test_display_foldtext_mbyte() CheckFeature folding call NewWindow(10, 40) diff --git a/src/nvim/testdir/test_edit.vim b/src/nvim/testdir/test_edit.vim index fc4e80f0d6..a1f6a84a99 100644 --- a/src/nvim/testdir/test_edit.vim +++ b/src/nvim/testdir/test_edit.vim @@ -213,7 +213,7 @@ func Test_edit_07() bw! endfunc -func! Test_edit_08() +func Test_edit_08() throw 'skipped: moved to test/functional/legacy/edit_spec.lua' " reset insertmode from i_ctrl-r_= let g:bufnr = bufnr('%') @@ -417,7 +417,7 @@ func Test_edit_13() bwipe! endfunc -func! Test_edit_CR() +func Test_edit_CR() " Test for <CR> in insert mode " basically only in quickfix mode ist tested, the rest " has been taken care of by other tests @@ -450,7 +450,7 @@ func! Test_edit_CR() call delete('Xqflist.txt') endfunc -func! Test_edit_CTRL_() +func Test_edit_CTRL_() " disabled for Windows builds, why? if !has("rightleft") || has("win32") return @@ -734,7 +734,7 @@ func Test_edit_CTRL_O() bw! endfunc -func! Test_edit_CTRL_R() +func Test_edit_CTRL_R() " Insert Register new " call test_override("ALL", 1) @@ -1006,16 +1006,14 @@ func Test_edit_DROP() endfunc func Test_edit_CTRL_V() - if has("ebcdic") - return - endif new call setline(1, ['abc']) call cursor(2, 1) + " force some redraws set showmode showcmd - "call test_override_char_avail(1) - " call test_override('ALL', 1) + " call test_override('char_avail', 1) + call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix') call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$')) @@ -1028,8 +1026,19 @@ func Test_edit_CTRL_V() set norl endif - " call test_override('ALL', 0) set noshowmode showcmd + " call test_override('char_avail', 0) + + " No modifiers should be applied to the char typed using i_CTRL-V_digit. + call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix') + call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2)) + + if has('osx') + " A char with a modifier should not be a valid char for i_CTRL-V_digit. + call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix') + call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3)) + endif + bw! endfunc @@ -1550,11 +1559,7 @@ endfunc func Test_edit_special_chars() new - if has("ebcdic") - let t = "o\<C-V>193\<C-V>xc2\<C-V>o303 \<C-V>90a\<C-V>xfg\<C-V>o578\<Esc>" - else - let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>" - endif + let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>" exe "normal " . t call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2)) diff --git a/src/nvim/testdir/test_eval_stuff.vim b/src/nvim/testdir/test_eval_stuff.vim index 883ba5de3d..95eccde35c 100644 --- a/src/nvim/testdir/test_eval_stuff.vim +++ b/src/nvim/testdir/test_eval_stuff.vim @@ -65,11 +65,9 @@ func Test_E963() endfunc func Test_for_invalid() - " Vim gives incorrect emsg here until v8.2.3284, but the exact emsg from that - " patch cannot be used until v8.2.2658 is ported (for loop over Strings) - call assert_fails("for x in 99", 'E897:') - call assert_fails("for x in function('winnr')", 'E897:') - call assert_fails("for x in {'a': 9}", 'E897:') + call assert_fails("for x in 99", 'E1098:') + call assert_fails("for x in function('winnr')", 'E1098:') + call assert_fails("for x in {'a': 9}", 'E1098:') if 0 /1/5/2/s/\n diff --git a/src/nvim/testdir/test_ex_mode.vim b/src/nvim/testdir/test_ex_mode.vim index 92e0559618..dcec5f7cc6 100644 --- a/src/nvim/testdir/test_ex_mode.vim +++ b/src/nvim/testdir/test_ex_mode.vim @@ -29,12 +29,11 @@ endfunc " Test editing line in Ex mode (both Q and gQ) func Test_ex_mode() - throw 'skipped: TODO: ' + throw 'Skipped: Nvim only supports Vim Ex mode' let encoding_save = &encoding set sw=2 - " for e in ['utf8', 'latin1'] - for e in ['utf8'] + for e in ['utf8', 'latin1'] exe 'set encoding=' . e call assert_equal(['bar', 'bar'], Ex("foo bar\<C-u>bar"), e) @@ -98,4 +97,14 @@ func Test_ex_mode_count_overflow() call delete('Xexmodescript') endfunc +func Test_ex_mode_large_indent() + new + set ts=500 ai + call setline(1, "\t") + exe "normal gQi\<CR>." + set ts=8 noai + bwipe! +endfunc + + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_excmd.vim b/src/nvim/testdir/test_excmd.vim index 1c053c824f..bbf8b4dfc8 100644 --- a/src/nvim/testdir/test_excmd.vim +++ b/src/nvim/testdir/test_excmd.vim @@ -400,3 +400,13 @@ func Test_winsize_cmd() call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)') " Actually changing the window size would be flaky. endfunc + +func Test_not_break_expression_register() + call setreg('=', '1+1') + if 0 + put =1 + endif + call assert_equal('1+1', getreg('=', 1)) +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_exec_while_if.vim b/src/nvim/testdir/test_exec_while_if.vim index 3da2784d77..3f13b09945 100644 --- a/src/nvim/testdir/test_exec_while_if.vim +++ b/src/nvim/testdir/test_exec_while_if.vim @@ -6,11 +6,7 @@ func Test_exec_while_if() let i = 0 while i < 12 let i = i + 1 - if has("ebcdic") - execute "normal o" . i . "\047" - else - execute "normal o" . i . "\033" - endif + execute "normal o" . i . "\033" if i % 2 normal Ax if i == 9 @@ -21,21 +17,13 @@ func Test_exec_while_if() else let j = 9 while j > 0 - if has("ebcdic") - execute "normal" j . "a" . j . "\x27" - else - execute "normal" j . "a" . j . "\x1b" - endif + execute "normal" j . "a" . j . "\x1b" let j = j - 1 endwhile endif endif if i == 9 - if has("ebcdic") - execute "normal Az\047" - else - execute "normal Az\033" - endif + execute "normal Az\033" endif endwhile unlet i j diff --git a/src/nvim/testdir/test_execute_func.vim b/src/nvim/testdir/test_execute_func.vim index 2cb6d73407..16cc20e9a7 100644 --- a/src/nvim/testdir/test_execute_func.vim +++ b/src/nvim/testdir/test_execute_func.vim @@ -147,3 +147,30 @@ func Test_win_execute_other_tab() tabclose unlet xyz endfunc + +func Test_win_execute_visual_redraw() + call setline(1, ['a', 'b', 'c']) + new + wincmd p + " start Visual in current window, redraw in other window with fewer lines + call feedkeys("G\<C-V>", 'txn') + call win_execute(winnr('#')->win_getid(), 'redraw') + call feedkeys("\<Esc>", 'txn') + bwipe! + bwipe! + + enew + new + call setline(1, ['a', 'b', 'c']) + let winid = win_getid() + wincmd p + " start Visual in current window, extend it in other window with more lines + call feedkeys("\<C-V>", 'txn') + call win_execute(winid, 'call feedkeys("G\<C-V>", ''txn'')') + redraw + + bwipe! + bwipe! +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_expand.vim b/src/nvim/testdir/test_expand.vim new file mode 100644 index 0000000000..48dce25bb3 --- /dev/null +++ b/src/nvim/testdir/test_expand.vim @@ -0,0 +1,83 @@ +" Test for expanding file names + +func Test_with_directories() + call mkdir('Xdir1') + call mkdir('Xdir2') + call mkdir('Xdir3') + cd Xdir3 + call mkdir('Xdir4') + cd .. + + split Xdir1/file + call setline(1, ['a', 'b']) + w + w Xdir3/Xdir4/file + close + + next Xdir?/*/file + call assert_equal('Xdir3/Xdir4/file', expand('%')) + if has('unix') + next! Xdir?/*/nofile + call assert_equal('Xdir?/*/nofile', expand('%')) + endif + " Edit another file, on MS-Windows the swap file would be in use and can't + " be deleted. + edit foo + + call assert_equal(0, delete('Xdir1', 'rf')) + call assert_equal(0, delete('Xdir2', 'rf')) + call assert_equal(0, delete('Xdir3', 'rf')) +endfunc + +func Test_with_tilde() + let dir = getcwd() + call mkdir('Xdir ~ dir') + call assert_true(isdirectory('Xdir ~ dir')) + cd Xdir\ ~\ dir + call assert_true(getcwd() =~ 'Xdir \~ dir') + call chdir(dir) + call delete('Xdir ~ dir', 'd') + call assert_false(isdirectory('Xdir ~ dir')) +endfunc + +func Test_expand_tilde_filename() + split ~ + call assert_equal('~', expand('%')) + call assert_notequal(expand('%:p'), expand('~/')) + call assert_match('\~', expand('%:p')) + bwipe! +endfunc + +func Test_expandcmd() + let $FOO = 'Test' + call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y')) + unlet $FOO + + new + edit Xfile1 + call assert_equal('e Xfile1', expandcmd('e %')) + edit Xfile2 + edit Xfile1 + call assert_equal('e Xfile2', 'e #'->expandcmd()) + edit Xfile2 + edit Xfile3 + edit Xfile4 + let bnum = bufnr('Xfile2') + call assert_equal('e Xfile2', expandcmd('e #' . bnum)) + call setline('.', 'Vim!@#') + call assert_equal('e Vim', expandcmd('e <cword>')) + call assert_equal('e Vim!@#', expandcmd('e <cWORD>')) + enew! + edit Xfile.java + call assert_equal('e Xfile.py', expandcmd('e %:r.py')) + call assert_equal('make abc.java', expandcmd('make abc.%:e')) + call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?')) + edit a1a2a3.rb + call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o')) + + call assert_fails('call expandcmd("make <afile>")', 'E495:') + call assert_fails('call expandcmd("make <afile>")', 'E495:') + enew + call assert_fails('call expandcmd("make %")', 'E499:') + close +endfunc diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim index 1d7fd3e385..5b10e691e5 100644 --- a/src/nvim/testdir/test_expr.vim +++ b/src/nvim/testdir/test_expr.vim @@ -283,6 +283,71 @@ function Test_printf_misc() call assert_equal('🐍', printf('%.2S', '🐍🐍')) call assert_equal('', printf('%.1S', '🐍🐍')) + call assert_equal('[ あいう]', printf('[%10.6S]', 'あいうえお')) + call assert_equal('[ あいうえ]', printf('[%10.8S]', 'あいうえお')) + call assert_equal('[あいうえお]', printf('[%10.10S]', 'あいうえお')) + call assert_equal('[あいうえお]', printf('[%10.12S]', 'あいうえお')) + + call assert_equal('あいう', printf('%S', 'あいう')) + call assert_equal('あいう', printf('%#S', 'あいう')) + + call assert_equal('あb', printf('%2S', 'あb')) + call assert_equal('あb', printf('%.4S', 'あb')) + call assert_equal('あ', printf('%.2S', 'あb')) + call assert_equal(' あb', printf('%4S', 'あb')) + call assert_equal('0あb', printf('%04S', 'あb')) + call assert_equal('あb ', printf('%-4S', 'あb')) + call assert_equal('あ ', printf('%-4.2S', 'あb')) + + call assert_equal('aい', printf('%2S', 'aい')) + call assert_equal('aい', printf('%.4S', 'aい')) + call assert_equal('a', printf('%.2S', 'aい')) + call assert_equal(' aい', printf('%4S', 'aい')) + call assert_equal('0aい', printf('%04S', 'aい')) + call assert_equal('aい ', printf('%-4S', 'aい')) + call assert_equal('a ', printf('%-4.2S', 'aい')) + + call assert_equal('[あいう]', printf('[%05S]', 'あいう')) + call assert_equal('[あいう]', printf('[%06S]', 'あいう')) + call assert_equal('[0あいう]', printf('[%07S]', 'あいう')) + + call assert_equal('[あiう]', printf('[%05S]', 'あiう')) + call assert_equal('[0あiう]', printf('[%06S]', 'あiう')) + call assert_equal('[00あiう]', printf('[%07S]', 'あiう')) + + call assert_equal('[0あい]', printf('[%05.4S]', 'あいう')) + call assert_equal('[00あい]', printf('[%06.4S]', 'あいう')) + call assert_equal('[000あい]', printf('[%07.4S]', 'あいう')) + + call assert_equal('[00あi]', printf('[%05.4S]', 'あiう')) + call assert_equal('[000あi]', printf('[%06.4S]', 'あiう')) + call assert_equal('[0000あi]', printf('[%07.4S]', 'あiう')) + + call assert_equal('[0あい]', printf('[%05.5S]', 'あいう')) + call assert_equal('[00あい]', printf('[%06.5S]', 'あいう')) + call assert_equal('[000あい]', printf('[%07.5S]', 'あいう')) + + call assert_equal('[あiう]', printf('[%05.5S]', 'あiう')) + call assert_equal('[0あiう]', printf('[%06.5S]', 'あiう')) + call assert_equal('[00あiう]', printf('[%07.5S]', 'あiう')) + + call assert_equal('[0000000000]', printf('[%010.0S]', 'あいう')) + call assert_equal('[0000000000]', printf('[%010.1S]', 'あいう')) + call assert_equal('[00000000あ]', printf('[%010.2S]', 'あいう')) + call assert_equal('[00000000あ]', printf('[%010.3S]', 'あいう')) + call assert_equal('[000000あい]', printf('[%010.4S]', 'あいう')) + call assert_equal('[000000あい]', printf('[%010.5S]', 'あいう')) + call assert_equal('[0000あいう]', printf('[%010.6S]', 'あいう')) + call assert_equal('[0000あいう]', printf('[%010.7S]', 'あいう')) + + call assert_equal('[0000000000]', printf('[%010.1S]', 'あiう')) + call assert_equal('[00000000あ]', printf('[%010.2S]', 'あiう')) + call assert_equal('[0000000あi]', printf('[%010.3S]', 'あiう')) + call assert_equal('[0000000あi]', printf('[%010.4S]', 'あiう')) + call assert_equal('[00000あiう]', printf('[%010.5S]', 'あiう')) + call assert_equal('[00000あiう]', printf('[%010.6S]', 'あiう')) + call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう')) + call assert_equal('1%', printf('%d%%', 1)) endfunc diff --git a/src/nvim/testdir/test_feedkeys.vim b/src/nvim/testdir/test_feedkeys.vim index 70500f2bb5..f343b0174c 100644 --- a/src/nvim/testdir/test_feedkeys.vim +++ b/src/nvim/testdir/test_feedkeys.vim @@ -12,3 +12,15 @@ func Test_feedkeys_x_with_empty_string() call assert_equal('foo', getline('.')) quit! endfunc + +func Test_feedkeys_with_abbreviation() + new + inoreabbrev trigger value + call feedkeys("atrigger ", 'x') + call feedkeys("atrigger ", 'x') + call assert_equal('value value ', getline(1)) + bwipe! + iunabbrev trigger +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_filechanged.vim b/src/nvim/testdir/test_filechanged.vim index b95cd5faf8..b4c32fcc16 100644 --- a/src/nvim/testdir/test_filechanged.vim +++ b/src/nvim/testdir/test_filechanged.vim @@ -91,7 +91,7 @@ func Test_FileChangedShell_reload() endfunc func Test_file_changed_dialog() - throw 'skipped: TODO: ' + throw 'Skipped: requires a UI to a active' if !has('unix') || has('gui_running') return endif diff --git a/src/nvim/testdir/test_filetype.vim b/src/nvim/testdir/test_filetype.vim index 4da68539fb..b663032c24 100644 --- a/src/nvim/testdir/test_filetype.vim +++ b/src/nvim/testdir/test_filetype.vim @@ -76,6 +76,7 @@ let s:filename_checks = { \ 'ave': ['file.ave'], \ 'awk': ['file.awk', 'file.gawk'], \ 'b': ['file.mch', 'file.ref', 'file.imp'], + \ 'basic': ['file.bas', 'file.bi', 'file.bm'], \ 'bzl': ['file.bazel', 'file.bzl', 'WORKSPACE'], \ 'bc': ['file.bc'], \ 'bdf': ['file.bdf'], @@ -182,47 +183,59 @@ let s:filename_checks = { \ 'fgl': ['file.4gl', 'file.4gh', 'file.m4gl'], \ 'fish': ['file.fish'], \ 'focexec': ['file.fex', 'file.focexec'], + \ 'form': ['file.frm'], \ 'forth': ['file.ft', 'file.fth'], \ 'fortran': ['file.f', 'file.for', 'file.fortran', 'file.fpp', 'file.ftn', 'file.f77', 'file.f90', 'file.f95', 'file.f03', 'file.f08'], \ 'fpcmake': ['file.fpc'], \ 'framescript': ['file.fsl'], - \ 'freebasic': ['file.fb', 'file.bi'], + \ 'freebasic': ['file.fb'], \ 'fsharp': ['file.fs', 'file.fsi', 'file.fsx'], \ 'fstab': ['fstab', 'mtab'], + \ 'fusion': ['file.fusion'], \ 'fvwm': ['/.fvwm/file', 'any/.fvwm/file'], \ 'gdb': ['.gdbinit', 'gdbinit'], + \ 'gdresource': ['file.tscn', 'file.tres'], + \ 'gdscript': ['file.gd'], \ 'gdmo': ['file.mo', 'file.gdmo'], \ 'gedcom': ['file.ged', 'lltxxxxx.txt', '/tmp/lltmp', '/tmp/lltmp-file', 'any/tmp/lltmp', 'any/tmp/lltmp-file'], \ 'gemtext': ['file.gmi', 'file.gemini'], \ 'gift': ['file.gift'], - \ 'gitcommit': ['COMMIT_EDITMSG', 'MERGE_MSG', 'TAG_EDITMSG'], - \ 'gitconfig': ['file.git/config', '.gitconfig', '.gitmodules', 'file.git/modules//config', '/.config/git/config', '/etc/gitconfig', '/etc/gitconfig.d/file', '/.gitconfig.d/file', 'any/.config/git/config', 'any/.gitconfig.d/file', 'some.git/config', 'some.git/modules/any/config'], + \ 'gitcommit': ['COMMIT_EDITMSG', 'MERGE_MSG', 'TAG_EDITMSG', 'NOTES_EDITMSG', 'EDIT_DESCRIPTION'], + \ 'gitconfig': ['file.git/config', 'file.git/config.worktree', 'file.git/worktrees/x/config.worktree', '.gitconfig', '.gitmodules', 'file.git/modules//config', '/.config/git/config', '/etc/gitconfig', '/usr/local/etc/gitconfig', '/etc/gitconfig.d/file', '/.gitconfig.d/file', 'any/.config/git/config', 'any/.gitconfig.d/file', 'some.git/config', 'some.git/modules/any/config'], \ 'gitolite': ['gitolite.conf', '/gitolite-admin/conf/file', 'any/gitolite-admin/conf/file'], \ 'gitrebase': ['git-rebase-todo'], \ 'gitsendemail': ['.gitsendemail.msg.xxxxxx'], \ 'gkrellmrc': ['gkrellmrc', 'gkrellmrc_x'], + \ 'glsl': ['file.glsl'], \ 'gnash': ['gnashrc', '.gnashrc', 'gnashpluginrc', '.gnashpluginrc'], - \ 'gnuplot': ['file.gpi'], + \ 'gnuplot': ['file.gpi', '.gnuplot'], \ 'go': ['file.go'], \ 'gomod': ['go.mod'], + \ 'gowork': ['go.work'], \ 'gp': ['file.gp', '.gprc'], \ 'gpg': ['/.gnupg/options', '/.gnupg/gpg.conf', '/usr/any/gnupg/options.skel', 'any/.gnupg/gpg.conf', 'any/.gnupg/options', 'any/usr/any/gnupg/options.skel'], \ 'grads': ['file.gs'], + \ 'graphql': ['file.graphql', 'file.graphqls', 'file.gql'], \ 'gretl': ['file.gretl'], \ 'groovy': ['file.gradle', 'file.groovy'], \ 'group': ['any/etc/group', 'any/etc/group-', 'any/etc/group.edit', 'any/etc/gshadow', 'any/etc/gshadow-', 'any/etc/gshadow.edit', 'any/var/backups/group.bak', 'any/var/backups/gshadow.bak', '/etc/group', '/etc/group-', '/etc/group.edit', '/etc/gshadow', '/etc/gshadow-', '/etc/gshadow.edit', '/var/backups/group.bak', '/var/backups/gshadow.bak'], \ 'grub': ['/boot/grub/menu.lst', '/boot/grub/grub.conf', '/etc/grub.conf', 'any/boot/grub/grub.conf', 'any/boot/grub/menu.lst', 'any/etc/grub.conf'], \ 'gsp': ['file.gsp'], \ 'gtkrc': ['.gtkrc', 'gtkrc', '.gtkrc-file', 'gtkrc-file'], + \ 'hack': ['file.hack', 'file.hackpartial'], \ 'haml': ['file.haml'], \ 'hamster': ['file.hsm'], + \ 'handlebars': ['file.hbs'], \ 'haskell': ['file.hs', 'file.hsc', 'file.hs-boot', 'file.hsig'], \ 'haste': ['file.ht'], \ 'hastepreproc': ['file.htpp'], \ 'hb': ['file.hb'], + \ 'hcl': ['file.hcl'], \ 'hercules': ['file.vc', 'file.ev', 'file.sum', 'file.errsum'], + \ 'heex': ['file.heex'], \ 'hex': ['file.hex', 'file.h32'], \ 'hgcommit': ['hg-editor-file.txt'], + \ 'hjson': ['file.hjson'], \ 'hog': ['file.hog', 'snort.conf', 'vision.conf'], \ 'hollywood': ['file.hws'], \ 'hostconf': ['/etc/host.conf', 'any/etc/host.conf'], @@ -264,6 +277,7 @@ let s:filename_checks = { \ 'jovial': ['file.jov', 'file.j73', 'file.jovial'], \ 'jproperties': ['file.properties', 'file.properties_xx', 'file.properties_xx_xx', 'some.properties_xx_xx_file'], \ 'json': ['file.json', 'file.jsonp', 'file.json-patch', 'file.webmanifest', 'Pipfile.lock', 'file.ipynb', '.babelrc', '.eslintrc', '.prettierrc', '.firebaserc', 'file.slnf'], + \ 'json5': ['file.json5'], \ 'jsonc': ['file.jsonc'], \ 'jsp': ['file.jsp'], \ 'julia': ['file.jl'], @@ -277,6 +291,7 @@ let s:filename_checks = { \ 'latte': ['file.latte', 'file.lte'], \ 'ld': ['file.ld'], \ 'ldif': ['file.ldif'], + \ 'ledger': ['file.ldg', 'file.ledger', 'file.journal'], \ 'less': ['file.less'], \ 'lex': ['file.lex', 'file.l', 'file.lxx', 'file.l++'], \ 'lftp': ['lftp.conf', '.lftprc', 'anylftp/rc', 'lftp/rc', 'some-lftp/rc'], @@ -350,6 +365,7 @@ let s:filename_checks = { \ 'netrc': ['.netrc'], \ 'nginx': ['file.nginx', 'nginxfile.conf', 'filenginx.conf', 'any/etc/nginx/file', 'any/usr/local/nginx/conf/file', 'any/nginx/file.conf'], \ 'ninja': ['file.ninja'], + \ 'nix': ['file.nix'], \ 'nqc': ['file.nqc'], \ 'nroff': ['file.tr', 'file.nr', 'file.roff', 'file.tmac', 'file.mom', 'tmac.file'], \ 'nsis': ['file.nsi', 'file.nsh'], @@ -392,6 +408,7 @@ let s:filename_checks = { \ 'ppd': ['file.ppd'], \ 'ppwiz': ['file.it', 'file.ih'], \ 'privoxy': ['file.action'], + \ 'prisma': ['file.prisma'], \ 'proc': ['file.pc'], \ 'procmail': ['.procmail', '.procmailrc'], \ 'prolog': ['file.pdb'], @@ -402,10 +419,12 @@ let s:filename_checks = { \ 'ps1xml': ['file.ps1xml'], \ 'psf': ['file.psf'], \ 'psl': ['file.psl'], + \ 'pug': ['file.pug'], \ 'puppet': ['file.pp'], \ 'pyret': ['file.arr'], \ 'pyrex': ['file.pyx', 'file.pxd'], \ 'python': ['file.py', 'file.pyw', '.pythonstartup', '.pythonrc', 'file.ptl', 'file.pyi', 'SConstruct'], + \ 'ql': ['file.ql', 'file.qll'], \ 'quake': ['anybaseq2/file.cfg', 'anyid1/file.cfg', 'quake3/file.cfg', 'baseq2/file.cfg', 'id1/file.cfg', 'quake1/file.cfg', 'some-baseq2/file.cfg', 'some-id1/file.cfg', 'some-quake1/file.cfg'], \ 'radiance': ['file.rad', 'file.mat'], \ 'raku': ['file.pm6', 'file.p6', 'file.t6', 'file.pod6', 'file.raku', 'file.rakumod', 'file.rakudoc', 'file.rakutest'], @@ -416,6 +435,7 @@ let s:filename_checks = { \ 'readline': ['.inputrc', 'inputrc'], \ 'remind': ['.reminders', 'file.remind', 'file.rem', '.reminders-file'], \ 'rego': ['file.rego'], + \ 'rescript': ['file.res', 'file.resi'], \ 'resolv': ['resolv.conf'], \ 'reva': ['file.frt'], \ 'rexx': ['file.rex', 'file.orx', 'file.rxo', 'file.rxj', 'file.jrexx', 'file.rexxj', 'file.rexx', 'file.testGroup', 'file.testUnit'], @@ -446,7 +466,7 @@ let s:filename_checks = { \ 'sdc': ['file.sdc'], \ 'sdl': ['file.sdl', 'file.pr'], \ 'sed': ['file.sed'], - \ 'sensors': ['/etc/sensors.conf', '/etc/sensors3.conf', 'any/etc/sensors.conf', 'any/etc/sensors3.conf'], + \ 'sensors': ['/etc/sensors.conf', '/etc/sensors3.conf', '/etc/sensors.d/file', 'any/etc/sensors.conf', 'any/etc/sensors3.conf', 'any/etc/sensors.d/file'], \ 'services': ['/etc/services', 'any/etc/services'], \ 'setserial': ['/etc/serial.conf', 'any/etc/serial.conf'], \ 'sh': ['.bashrc', 'file.bash', '/usr/share/doc/bash-completion/filter.sh','/etc/udev/cdsymlinks.conf', 'any/etc/udev/cdsymlinks.conf'], @@ -487,6 +507,7 @@ let s:filename_checks = { \ 'stata': ['file.ado', 'file.do', 'file.imata', 'file.mata'], \ 'stp': ['file.stp'], \ 'sudoers': ['any/etc/sudoers', 'sudoers.tmp', '/etc/sudoers', 'any/etc/sudoers.d/file'], + \ 'surface': ['file.sface'], \ 'svg': ['file.svg'], \ 'svn': ['svn-commitfile.tmp', 'svn-commit-file.tmp', 'svn-commit.tmp'], \ 'swift': ['file.swift'], @@ -500,8 +521,10 @@ let s:filename_checks = { \ 'taskdata': ['pending.data', 'completed.data', 'undo.data'], \ 'taskedit': ['file.task'], \ 'tcl': ['file.tcl', 'file.tm', 'file.tk', 'file.itcl', 'file.itk', 'file.jacl', '.tclshrc', 'tclsh.rc', '.wishrc'], + \ 'teal': ['file.tl'], \ 'teraterm': ['file.ttl'], \ 'terminfo': ['file.ti'], + \ 'terraform': ['file.tfvars'], \ 'tex': ['file.latex', 'file.sty', 'file.dtx', 'file.ltx', 'file.bbl'], \ 'texinfo': ['file.texinfo', 'file.texi', 'file.txi'], \ 'texmf': ['texmf.cnf'], @@ -509,6 +532,7 @@ let s:filename_checks = { \ 'tf': ['file.tf', '.tfrc', 'tfrc'], \ 'tidy': ['.tidyrc', 'tidyrc', 'tidy.conf'], \ 'tilde': ['file.t.html'], + \ 'tla': ['file.tla'], \ 'tli': ['file.tli'], \ 'tmux': ['tmuxfile.conf', '.tmuxfile.conf', '.tmux-file.conf', '.tmux.conf', 'tmux-file.conf', 'tmux.conf', 'tmux.conf.local'], \ 'toml': ['file.toml', 'Gopkg.lock', 'Pipfile', '/home/user/.cargo/config'], @@ -534,6 +558,7 @@ let s:filename_checks = { \ 'usserverlog': ['usserver.log', 'USSERVER.LOG', 'usserver.file.log', 'USSERVER.FILE.LOG', 'file.usserver.log', 'FILE.USSERVER.LOG'], \ 'usw2kagtlog': ['usw2kagt.log', 'USW2KAGT.LOG', 'usw2kagt.file.log', 'USW2KAGT.FILE.LOG', 'file.usw2kagt.log', 'FILE.USW2KAGT.LOG'], \ 'vb': ['file.sba', 'file.vb', 'file.vbs', 'file.dsm', 'file.ctl'], + \ 'vala': ['file.vala'], \ 'vera': ['file.vr', 'file.vri', 'file.vrh'], \ 'verilog': ['file.v'], \ 'verilogams': ['file.va', 'file.vams'], @@ -569,6 +594,7 @@ let s:filename_checks = { \ 'xslt': ['file.xsl', 'file.xslt'], \ 'yacc': ['file.yy', 'file.yxx', 'file.y++'], \ 'yaml': ['file.yaml', 'file.yml'], + \ 'yang': ['file.yang'], \ 'raml': ['file.raml'], \ 'z8a': ['file.z8a'], \ 'zig': ['file.zig'], @@ -653,7 +679,7 @@ let s:script_checks = { \ ['#!/path/nodejs'], \ ['#!/path/rhino']], \ 'bc': [['#!/path/bc']], - \ 'sed': [['#!/path/sed']], + \ 'sed': [['#!/path/sed'], ['#n'], ['#n comment']], \ 'ocaml': [['#!/path/ocaml']], \ 'awk': [['#!/path/awk'], \ ['#!/path/gawk']], @@ -706,83 +732,126 @@ func Test_setfiletype_completion() call assert_equal('"setfiletype java javacc javascript javascriptreact', @:) endfunc -func Test_hook_file() +""""""""""""""""""""""""""""""""""""""""""""""""" +" Tests for specific extentions and filetypes. +" Keep sorted. +""""""""""""""""""""""""""""""""""""""""""""""""" + +func Test_bas_file() filetype on - call writefile(['[Trigger]', 'this is pacman config'], 'Xfile.hook') - split Xfile.hook - call assert_equal('dosini', &filetype) + call writefile(['looks like BASIC'], 'Xfile.bas') + split Xfile.bas + call assert_equal('basic', &filetype) bwipe! - call writefile(['not pacman'], 'Xfile.hook') - split Xfile.hook - call assert_notequal('dosini', &filetype) + " Test dist#ft#FTbas() + + let g:filetype_bas = 'freebasic' + split Xfile.bas + call assert_equal('freebasic', &filetype) bwipe! + unlet g:filetype_bas - call delete('Xfile.hook') - filetype off -endfunc + " FreeBASIC -func Test_ts_file() - filetype on + call writefile(["/' FreeBASIC multiline comment '/"], 'Xfile.bas') + split Xfile.bas + call assert_equal('freebasic', &filetype) + bwipe! - call writefile(['<?xml version="1.0" encoding="utf-8"?>'], 'Xfile.ts') - split Xfile.ts - call assert_equal('xml', &filetype) + call writefile(['#define TESTING'], 'Xfile.bas') + split Xfile.bas + call assert_equal('freebasic', &filetype) bwipe! - call writefile(['// looks like Typescript'], 'Xfile.ts') - split Xfile.ts - call assert_equal('typescript', &filetype) + call writefile(['option byval'], 'Xfile.bas') + split Xfile.bas + call assert_equal('freebasic', &filetype) bwipe! - call delete('Xfile.hook') - filetype off -endfunc + call writefile(['extern "C"'], 'Xfile.bas') + split Xfile.bas + call assert_equal('freebasic', &filetype) + bwipe! -func Test_ttl_file() - filetype on + " QB64 - call writefile(['@base <http://example.org/> .'], 'Xfile.ttl') - split Xfile.ttl - call assert_equal('turtle', &filetype) + call writefile(['$LET TESTING = 1'], 'Xfile.bas') + split Xfile.bas + call assert_equal('qb64', &filetype) bwipe! - call writefile(['looks like Tera Term Language'], 'Xfile.ttl') - split Xfile.ttl - call assert_equal('teraterm', &filetype) + call writefile(['OPTION _EXPLICIT'], 'Xfile.bas') + split Xfile.bas + call assert_equal('qb64', &filetype) bwipe! - call delete('Xfile.ttl') + " Visual Basic + + call writefile(['Attribute VB_NAME = "Testing"'], 'Xfile.bas') + split Xfile.bas + call assert_equal('vb', &filetype) + bwipe! + + call delete('Xfile.bas') filetype off endfunc -func Test_pp_file() +func Test_dep3patch_file() filetype on - call writefile(['looks like puppet'], 'Xfile.pp') - split Xfile.pp - call assert_equal('puppet', &filetype) + call assert_true(mkdir('debian/patches', 'p')) + + " series files are not patches + call writefile(['Description: some awesome patch'], 'debian/patches/series') + split debian/patches/series + call assert_notequal('dep3patch', &filetype) bwipe! - let g:filetype_pp = 'pascal' - split Xfile.pp - call assert_equal('pascal', &filetype) + " diff/patch files without the right headers should still show up as ft=diff + call writefile([], 'debian/patches/foo.diff') + split debian/patches/foo.diff + call assert_equal('diff', &filetype) bwipe! - unlet g:filetype_pp - " Test dist#ft#FTpp() - call writefile(['{ pascal comment'], 'Xfile.pp') - split Xfile.pp - call assert_equal('pascal', &filetype) + " Files with the right headers are detected as dep3patch, even if they don't + " have a diff/patch extension + call writefile(['Subject: dep3patches'], 'debian/patches/bar') + split debian/patches/bar + call assert_equal('dep3patch', &filetype) bwipe! - call writefile(['procedure pascal'], 'Xfile.pp') - split Xfile.pp - call assert_equal('pascal', &filetype) + " Files in sub-directories are detected + call assert_true(mkdir('debian/patches/s390x', 'p')) + call writefile(['Subject: dep3patches'], 'debian/patches/s390x/bar') + split debian/patches/s390x/bar + call assert_equal('dep3patch', &filetype) bwipe! - call delete('Xfile.pp') + " The detection stops when seeing the "header end" marker + call writefile(['---', 'Origin: the cloud'], 'debian/patches/baz') + split debian/patches/baz + call assert_notequal('dep3patch', &filetype) + bwipe! + + call delete('debian', 'rf') +endfunc + +func Test_dsl_file() + filetype on + + call writefile([' <!doctype dsssl-spec ['], 'dslfile.dsl') + split dslfile.dsl + call assert_equal('dsl', &filetype) + bwipe! + + call writefile(['workspace {'], 'dslfile.dsl') + split dslfile.dsl + call assert_equal('structurizr', &filetype) + bwipe! + + call delete('dslfile.dsl') filetype off endfunc @@ -823,20 +892,183 @@ func Test_ex_file() filetype off endfunc -func Test_dsl_file() +func Test_foam_file() filetype on + call assert_true(mkdir('0', 'p')) + call assert_true(mkdir('0.orig', 'p')) - call writefile([' <!doctype dsssl-spec ['], 'dslfile.dsl') - split dslfile.dsl - call assert_equal('dsl', &filetype) + call writefile(['FoamFile {', ' object something;'], 'Xfile1Dict') + split Xfile1Dict + call assert_equal('foam', &filetype) bwipe! - call writefile(['workspace {'], 'dslfile.dsl') - split dslfile.dsl - call assert_equal('structurizr', &filetype) + call writefile(['FoamFile {', ' object something;'], 'Xfile1Dict.something') + split Xfile1Dict.something + call assert_equal('foam', &filetype) bwipe! - call delete('dslfile.dsl') + call writefile(['FoamFile {', ' object something;'], 'XfileProperties') + split XfileProperties + call assert_equal('foam', &filetype) + bwipe! + + call writefile(['FoamFile {', ' object something;'], 'XfileProperties.something') + split XfileProperties.something + call assert_equal('foam', &filetype) + bwipe! + + call writefile(['FoamFile {', ' object something;'], 'XfileProperties') + split XfileProperties + call assert_equal('foam', &filetype) + bwipe! + + call writefile(['FoamFile {', ' object something;'], 'XfileProperties.something') + split XfileProperties.something + call assert_equal('foam', &filetype) + bwipe! + + call writefile(['FoamFile {', ' object something;'], '0/Xfile') + split 0/Xfile + call assert_equal('foam', &filetype) + bwipe! + + call writefile(['FoamFile {', ' object something;'], '0.orig/Xfile') + split 0.orig/Xfile + call assert_equal('foam', &filetype) + bwipe! + + call delete('0', 'rf') + call delete('0.orig', 'rf') + call delete('Xfile1Dict') + call delete('Xfile1Dict.something') + call delete('XfileProperties') + call delete('XfileProperties.something') + filetype off +endfunc + +func Test_frm_file() + filetype on + + call writefile(['looks like FORM'], 'Xfile.frm') + split Xfile.frm + call assert_equal('form', &filetype) + bwipe! + + " Test dist#ft#FTfrm() + + let g:filetype_frm = 'form' + split Xfile.frm + call assert_equal('form', &filetype) + bwipe! + unlet g:filetype_frm + + " Visual Basic + + call writefile(['Begin VB.Form Form1'], 'Xfile.frm') + split Xfile.frm + call assert_equal('vb', &filetype) + bwipe! + + call delete('Xfile.frm') + filetype off +endfunc + +func Test_fs_file() + filetype on + + call writefile(['looks like F#'], 'Xfile.fs') + split Xfile.fs + call assert_equal('fsharp', &filetype) + bwipe! + + let g:filetype_fs = 'forth' + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + unlet g:filetype_fs + + " Test dist#ft#FTfs() + + " Forth (Gforth) + + call writefile(['( Forth inline comment )'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + call writefile(['.( Forth displayed inline comment )'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + call writefile(['\ Forth line comment'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + " empty line comment - no space required + call writefile(['\'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + call writefile(['\G Forth documentation comment '], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + call writefile([': squared ( n -- n^2 )', 'dup * ;'], 'Xfile.fs') + split Xfile.fs + call assert_equal('forth', &filetype) + bwipe! + + call delete('Xfile.fs') + filetype off +endfunc + +func Test_git_file() + filetype on + + call assert_true(mkdir('Xrepo.git', 'p')) + + call writefile([], 'Xrepo.git/HEAD') + split Xrepo.git/HEAD + call assert_equal('', &filetype) + bwipe! + + call writefile(['0000000000000000000000000000000000000000'], 'Xrepo.git/HEAD') + split Xrepo.git/HEAD + call assert_equal('git', &filetype) + bwipe! + + call writefile(['0000000000000000000000000000000000000000000000000000000000000000'], 'Xrepo.git/HEAD') + split Xrepo.git/HEAD + call assert_equal('git', &filetype) + bwipe! + + call writefile(['ref: refs/heads/master'], 'Xrepo.git/HEAD') + split Xrepo.git/HEAD + call assert_equal('git', &filetype) + bwipe! + + call delete('Xrepo.git', 'rf') + filetype off +endfunc + +func Test_hook_file() + filetype on + + call writefile(['[Trigger]', 'this is pacman config'], 'Xfile.hook') + split Xfile.hook + call assert_equal('dosini', &filetype) + bwipe! + + call writefile(['not pacman'], 'Xfile.hook') + split Xfile.hook + call assert_notequal('dosini', &filetype) + bwipe! + + call delete('Xfile.hook') filetype off endfunc @@ -940,109 +1172,154 @@ func Test_m_file() filetype off endfunc -func Test_xpm_file() +func Test_patch_file() filetype on - call writefile(['this is XPM2'], 'file.xpm') - split file.xpm - call assert_equal('xpm2', &filetype) + call writefile([], 'Xfile.patch') + split Xfile.patch + call assert_equal('diff', &filetype) bwipe! - call delete('file.xpm') + call writefile(['From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001'], 'Xfile.patch') + split Xfile.patch + call assert_equal('gitsendemail', &filetype) + bwipe! + + call writefile(['From 0000000000000000000000000000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001'], 'Xfile.patch') + split Xfile.patch + call assert_equal('gitsendemail', &filetype) + bwipe! + + call delete('Xfile.patch') filetype off endfunc -func Test_fs_file() +func Test_perl_file() filetype on - call writefile(['looks like F#'], 'Xfile.fs') - split Xfile.fs - call assert_equal('fsharp', &filetype) - bwipe! + " only tests one case, should do more + let lines =<< trim END - let g:filetype_fs = 'forth' - split Xfile.fs - call assert_equal('forth', &filetype) - bwipe! - unlet g:filetype_fs + use a + END + call writefile(lines, "Xfile.t") + split Xfile.t + call assert_equal('perl', &filetype) + bwipe - " Test dist#ft#FTfs() + call delete('Xfile.t') + filetype off +endfunc - " Forth (Gforth) +func Test_pp_file() + filetype on - call writefile(['( Forth inline comment )'], 'Xfile.fs') - split Xfile.fs - call assert_equal('forth', &filetype) + call writefile(['looks like puppet'], 'Xfile.pp') + split Xfile.pp + call assert_equal('puppet', &filetype) bwipe! - call writefile(['.( Forth displayed inline comment )'], 'Xfile.fs') - split Xfile.fs - call assert_equal('forth', &filetype) + let g:filetype_pp = 'pascal' + split Xfile.pp + call assert_equal('pascal', &filetype) bwipe! + unlet g:filetype_pp - call writefile(['\ Forth line comment'], 'Xfile.fs') - split Xfile.fs - call assert_equal('forth', &filetype) + " Test dist#ft#FTpp() + call writefile(['{ pascal comment'], 'Xfile.pp') + split Xfile.pp + call assert_equal('pascal', &filetype) bwipe! - " empty line comment - no space required - call writefile(['\'], 'Xfile.fs') - split Xfile.fs - call assert_equal('forth', &filetype) + call writefile(['procedure pascal'], 'Xfile.pp') + split Xfile.pp + call assert_equal('pascal', &filetype) bwipe! - call writefile(['\G Forth documentation comment '], 'Xfile.fs') - split Xfile.fs - call assert_equal('forth', &filetype) + call delete('Xfile.pp') + filetype off +endfunc + +func Test_tex_file() + filetype on + + " only tests one case, should do more + let lines =<< trim END + % This is a sentence. + + This is a sentence. + END + call writefile(lines, "Xfile.tex") + split Xfile.tex + call assert_equal('plaintex', &filetype) + bwipe + + call delete('Xfile.tex') + filetype off +endfunc + +func Test_tf_file() + filetype on + + call writefile([';;; TF MUD client is super duper cool'], 'Xfile.tf') + split Xfile.tf + call assert_equal('tf', &filetype) bwipe! - call writefile([': squared ( n -- n^2 )', 'dup * ;'], 'Xfile.fs') - split Xfile.fs - call assert_equal('forth', &filetype) + call writefile(['provider "azurerm" {'], 'Xfile.tf') + split Xfile.tf + call assert_equal('terraform', &filetype) bwipe! - call delete('Xfile.fs') + call delete('Xfile.tf') filetype off endfunc -func Test_dep3patch_file() +func Test_ts_file() filetype on - call assert_true(mkdir('debian/patches', 'p')) - - " series files are not patches - call writefile(['Description: some awesome patch'], 'debian/patches/series') - split debian/patches/series - call assert_notequal('dep3patch', &filetype) + call writefile(['<?xml version="1.0" encoding="utf-8"?>'], 'Xfile.ts') + split Xfile.ts + call assert_equal('xml', &filetype) bwipe! - " diff/patch files without the right headers should still show up as ft=diff - call writefile([], 'debian/patches/foo.diff') - split debian/patches/foo.diff - call assert_equal('diff', &filetype) + call writefile(['// looks like Typescript'], 'Xfile.ts') + split Xfile.ts + call assert_equal('typescript', &filetype) bwipe! - " Files with the right headers are detected as dep3patch, even if they don't - " have a diff/patch extension - call writefile(['Subject: dep3patches'], 'debian/patches/bar') - split debian/patches/bar - call assert_equal('dep3patch', &filetype) + call delete('Xfile.ts') + filetype off +endfunc + +func Test_ttl_file() + filetype on + + call writefile(['@base <http://example.org/> .'], 'Xfile.ttl') + split Xfile.ttl + call assert_equal('turtle', &filetype) bwipe! - " Files in sub-directories are detected - call assert_true(mkdir('debian/patches/s390x', 'p')) - call writefile(['Subject: dep3patches'], 'debian/patches/s390x/bar') - split debian/patches/s390x/bar - call assert_equal('dep3patch', &filetype) + call writefile(['looks like Tera Term Language'], 'Xfile.ttl') + split Xfile.ttl + call assert_equal('teraterm', &filetype) bwipe! - " The detection stops when seeing the "header end" marker - call writefile(['---', 'Origin: the cloud'], 'debian/patches/baz') - split debian/patches/baz - call assert_notequal('dep3patch', &filetype) + call delete('Xfile.ttl') + filetype off +endfunc + +func Test_xpm_file() + filetype on + + call writefile(['this is XPM2'], 'file.xpm') + split file.xpm + call assert_equal('xpm2', &filetype) bwipe! - call delete('debian/patches', 'rf') + call delete('file.xpm') + filetype off endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_filetype_lua.vim b/src/nvim/testdir/test_filetype_lua.vim new file mode 100644 index 0000000000..f73e4ca33f --- /dev/null +++ b/src/nvim/testdir/test_filetype_lua.vim @@ -0,0 +1,2 @@ +let g:do_filetype_lua = 1 +source test_filetype.vim diff --git a/src/nvim/testdir/test_filter_cmd.vim b/src/nvim/testdir/test_filter_cmd.vim index 0c45db049b..d465e48c7b 100644 --- a/src/nvim/testdir/test_filter_cmd.vim +++ b/src/nvim/testdir/test_filter_cmd.vim @@ -145,3 +145,38 @@ func Test_filter_commands() bwipe! file.h bwipe! file.hs endfunc + +func Test_filter_display() + edit Xdoesnotmatch + let @a = '!!willmatch' + let @b = '!!doesnotmatch' + let @c = "oneline\ntwoline\nwillmatch\n" + let @/ = '!!doesnotmatch' + call feedkeys(":echo '!!doesnotmatch:'\<CR>", 'ntx') + let lines = map(split(execute('filter /willmatch/ display'), "\n"), 'v:val[5:6]') + + call assert_true(index(lines, '"a') >= 0) + call assert_false(index(lines, '"b') >= 0) + call assert_true(index(lines, '"c') >= 0) + call assert_false(index(lines, '"/') >= 0) + call assert_false(index(lines, '":') >= 0) + call assert_false(index(lines, '"%') >= 0) + + let lines = map(split(execute('filter /doesnotmatch/ display'), "\n"), 'v:val[5:6]') + call assert_true(index(lines, '"a') < 0) + call assert_false(index(lines, '"b') < 0) + call assert_true(index(lines, '"c') < 0) + call assert_false(index(lines, '"/') < 0) + call assert_false(index(lines, '":') < 0) + call assert_false(index(lines, '"%') < 0) + + bwipe! +endfunc + +func Test_filter_scriptnames() + let lines = split(execute('filter /test_filter_cmd/ scriptnames'), "\n") + call assert_equal(1, len(lines)) + call assert_match('filter_cmd', lines[0]) +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_filter_map.vim b/src/nvim/testdir/test_filter_map.vim index a52a66ac2f..1cd3a2287b 100644 --- a/src/nvim/testdir/test_filter_map.vim +++ b/src/nvim/testdir/test_filter_map.vim @@ -88,4 +88,14 @@ func Test_map_filter_fails() call assert_fails("let l = filter('abc', '\"> \" . v:val')", 'E896:') endfunc +func Test_map_and_modify() + let l = ["abc"] + " cannot change the list halfway a map() + call assert_fails('call map(l, "remove(l, 0)[0]")', 'E741:') + + let d = #{a: 1, b: 2, c: 3} + call assert_fails('call map(d, "remove(d, v:key)[0]")', 'E741:') + call assert_fails('echo map(d, {k,v -> remove(d, k)})', 'E741:') +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_findfile.vim b/src/nvim/testdir/test_findfile.vim index 5a20475d3d..1684c5d30a 100644 --- a/src/nvim/testdir/test_findfile.vim +++ b/src/nvim/testdir/test_findfile.vim @@ -226,4 +226,26 @@ func Test_find_cmd() call assert_fails('tabfind', 'E471:') endfunc +func Test_find_non_existing_path() + new + let save_path = &path + let save_dir = getcwd() + call mkdir('dir1/dir2', 'p') + call writefile([], 'dir1/file.txt') + call writefile([], 'dir1/dir2/base.txt') + call chdir('dir1/dir2') + e base.txt + set path=../include + + call assert_fails(':find file.txt', 'E345:') + + call chdir(save_dir) + bw! + call delete('dir1/dir2/base.txt', 'rf') + call delete('dir1/dir2', 'rf') + call delete('dir1/file.txt', 'rf') + call delete('dir1', 'rf') + let &path = save_path +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_fnamemodify.vim b/src/nvim/testdir/test_fnamemodify.vim index 411f7ebbb3..5ae2a5ee17 100644 --- a/src/nvim/testdir/test_fnamemodify.vim +++ b/src/nvim/testdir/test_fnamemodify.vim @@ -3,8 +3,10 @@ func Test_fnamemodify() let save_home = $HOME let save_shell = &shell + let save_shellslash = &shellslash let $HOME = fnamemodify('.', ':p:h:h') set shell=sh + set shellslash call assert_equal('/', fnamemodify('.', ':p')[-1:]) call assert_equal('r', fnamemodify('.', ':p:h')[-1:]) @@ -27,6 +29,21 @@ func Test_fnamemodify() call assert_equal('fb2.tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e:e')) call assert_equal('fb2.tar.gz', fnamemodify('abc.fb2.tar.gz', ':e:e:e:e')) call assert_equal('tar', fnamemodify('abc.fb2.tar.gz', ':e:e:r')) + call assert_equal(getcwd(), fnamemodify('', ':p:h')) + + let cwd = getcwd() + call chdir($HOME) + call assert_equal('foobar', fnamemodify('~/foobar', ':~:.')) + call chdir(cwd) + call mkdir($HOME . '/XXXXXXXX/a', 'p') + call mkdir($HOME . '/XXXXXXXX/b', 'p') + call chdir($HOME . '/XXXXXXXX/a/') + call assert_equal('foo', fnamemodify($HOME . '/XXXXXXXX/a/foo', ':p:~:.')) + call assert_equal('~/XXXXXXXX/b/foo', fnamemodify($HOME . '/XXXXXXXX/b/foo', ':p:~:.')) + call mkdir($HOME . '/XXXXXXXX/a.ext', 'p') + call assert_equal('~/XXXXXXXX/a.ext/foo', fnamemodify($HOME . '/XXXXXXXX/a.ext/foo', ':p:~:.')) + call chdir(cwd) + call delete($HOME . '/XXXXXXXX', 'rf') call assert_equal('''abc def''', fnamemodify('abc def', ':S')) call assert_equal('''abc" "def''', fnamemodify('abc" "def', ':S')) @@ -44,6 +61,7 @@ func Test_fnamemodify() let $HOME = save_home let &shell = save_shell + let &shellslash = save_shellslash endfunc func Test_fnamemodify_er() @@ -73,6 +91,7 @@ func Test_fnamemodify_er() call assert_equal('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e')) call assert_equal('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e:e')) + call assert_equal('', fnamemodify('', ':p:t')) call assert_equal('', fnamemodify(v:_null_string, v:_null_string)) endfunc diff --git a/src/nvim/testdir/test_functions.vim b/src/nvim/testdir/test_functions.vim index 0edbeb420a..438bed51c6 100644 --- a/src/nvim/testdir/test_functions.vim +++ b/src/nvim/testdir/test_functions.vim @@ -376,6 +376,25 @@ func Test_pathshorten() call assert_equal('~.f/bar', pathshorten('~.foo/bar')) call assert_equal('.~f/bar', pathshorten('.~foo/bar')) call assert_equal('~/f/bar', pathshorten('~/foo/bar')) + call assert_fails('call pathshorten([])', 'E730:') + + " test pathshorten with optional variable to set preferred size of shortening + call assert_equal('', pathshorten('', 2)) + call assert_equal('foo', pathshorten('foo', 2)) + call assert_equal('/foo', pathshorten('/foo', 2)) + call assert_equal('fo/', pathshorten('foo/', 2)) + call assert_equal('fo/bar', pathshorten('foo/bar', 2)) + call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2)) + call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2)) + call assert_equal('.fo/bar', pathshorten('.foo/bar', 2)) + call assert_equal('~fo/bar', pathshorten('~foo/bar', 2)) + call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2)) + call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2)) + call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2)) + call assert_fails('call pathshorten([],2)', 'E730:') + call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3)) + call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3)) + call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0)) endfunc func Test_strpart() @@ -1377,6 +1396,36 @@ func Test_func_exists_on_reload() delfunc ExistingFunction endfunc +func Test_platform_name() + " The system matches at most only one name. + let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix'] + call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)'))) + + " Is Unix? + call assert_equal(has('beos'), has('beos') && has('unix')) + call assert_equal(has('bsd'), has('bsd') && has('unix')) + call assert_equal(has('hpux'), has('hpux') && has('unix')) + call assert_equal(has('linux'), has('linux') && has('unix')) + call assert_equal(has('mac'), has('mac') && has('unix')) + call assert_equal(has('qnx'), has('qnx') && has('unix')) + call assert_equal(has('sun'), has('sun') && has('unix')) + call assert_equal(has('win32'), has('win32') && !has('unix')) + call assert_equal(has('win32unix'), has('win32unix') && has('unix')) + + if has('unix') && executable('uname') + let uname = system('uname') + call assert_equal(uname =~? 'BeOS', has('beos')) + " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined + call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd')) + call assert_equal(uname =~? 'HP-UX', has('hpux')) + call assert_equal(uname =~? 'Linux', has('linux')) + call assert_equal(uname =~? 'Darwin', has('mac')) + call assert_equal(uname =~? 'QNX', has('qnx')) + call assert_equal(uname =~? 'SunOS', has('sun')) + call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix')) + endif +endfunc + sandbox function Fsandbox() normal ix endfunc @@ -1519,24 +1568,31 @@ func Test_libcall_libcallnr() let libc = 'msvcrt.dll' elseif has('mac') let libc = 'libSystem.B.dylib' - elseif system('uname -s') =~ 'SunOS' - " Set the path to libc.so according to the architecture. - let test_bits = system('file ' . GetVimProg()) - let test_arch = system('uname -p') - if test_bits =~ '64-bit' && test_arch =~ 'sparc' - let libc = '/usr/lib/sparcv9/libc.so' - elseif test_bits =~ '64-bit' && test_arch =~ 'i386' - let libc = '/usr/lib/amd64/libc.so' + elseif executable('ldd') + let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>') + endif + if get(l:, 'libc', '') ==# '' + " On Unix, libc.so can be in various places. + if has('linux') + " There is not documented but regarding the 1st argument of glibc's + " dlopen an empty string and nullptr are equivalent, so using an empty + " string for the 1st argument of libcall allows to call functions. + let libc = '' + elseif has('sun') + " Set the path to libc.so according to the architecture. + let test_bits = system('file ' . GetVimProg()) + let test_arch = system('uname -p') + if test_bits =~ '64-bit' && test_arch =~ 'sparc' + let libc = '/usr/lib/sparcv9/libc.so' + elseif test_bits =~ '64-bit' && test_arch =~ 'i386' + let libc = '/usr/lib/amd64/libc.so' + else + let libc = '/usr/lib/libc.so' + endif else - let libc = '/usr/lib/libc.so' + " Unfortunately skip this test until a good way is found. + return endif - elseif system('uname -s') =~ 'OpenBSD' - let libc = 'libc.so' - else - " On Unix, libc.so can be in various places. - " Interestingly, using an empty string for the 1st argument of libcall - " allows to call functions from libc which is not documented. - let libc = '' endif if has('win32') @@ -1675,6 +1731,33 @@ func Test_nr2char() call assert_equal("\x80\xfc\b\xfd\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x40000000) .. '>"')) endfunc +" Test for getcurpos() and setpos() +func Test_getcurpos_setpos() + new + call setline(1, ['012345678', '012345678']) + normal gg6l + let sp = getcurpos() + normal 0 + call setpos('.', sp) + normal jyl + call assert_equal('6', @") + call assert_equal(-1, setpos('.', v:_null_list)) + call assert_equal(-1, setpos('.', {})) + + let winid = win_getid() + normal G$ + let pos = getcurpos() + wincmd w + call assert_equal(pos, getcurpos(winid)) + + wincmd w + close! + + call assert_equal(getcurpos(), getcurpos(0)) + call assert_equal([0, 0, 0, 0, 0], getcurpos(-1)) + call assert_equal([0, 0, 0, 0, 0], getcurpos(1999)) +endfunc + func HasDefault(msg = 'msg') return a:msg endfunc diff --git a/src/nvim/testdir/test_gf.vim b/src/nvim/testdir/test_gf.vim index 43efd6248e..589899f532 100644 --- a/src/nvim/testdir/test_gf.vim +++ b/src/nvim/testdir/test_gf.vim @@ -19,11 +19,7 @@ func Test_gf_url() call search("^second") call search("URL") call assert_equal("URL://machine.name/tmp/vimtest2b", expand("<cfile>")) - if has("ebcdic") - set isf=@,240-249,/,.,-,_,+,,,$,:,~,\ - else - set isf=@,48-57,/,.,-,_,+,,,$,~,\ - endif + set isf=@,48-57,/,.,-,_,+,,,$,~,\ call search("^third") call search("name") call assert_equal("URL:\\\\machine.name\\vimtest2c", expand("<cfile>")) @@ -76,11 +72,7 @@ endfunc " Test for invoking 'gf' on a ${VAR} variable func Test_gf() - if has("ebcdic") - set isfname=@,240-249,/,.,-,_,+,,,$,:,~,{,} - else - set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,} - endif + set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,} call writefile(["Test for gf command"], "Xtest1") if has("unix") diff --git a/src/nvim/testdir/test_global.vim b/src/nvim/testdir/test_global.vim index 8edc9c2608..ad561baf4a 100644 --- a/src/nvim/testdir/test_global.vim +++ b/src/nvim/testdir/test_global.vim @@ -36,6 +36,36 @@ func Test_global_error() call assert_fails('g/\(/y', 'E476:') endfunc +" Test for printing lines using :g with different search patterns +func Test_global_print() + new + call setline(1, ['foo', 'bar', 'foo', 'foo']) + let @/ = 'foo' + let t = execute("g/")->trim()->split("\n") + call assert_equal(['foo', 'foo', 'foo'], t) + + " Test for Vi compatible patterns + let @/ = 'bar' + let t = execute('g\/')->trim()->split("\n") + call assert_equal(['bar'], t) + + normal gg + s/foo/foo/ + let t = execute('g\&')->trim()->split("\n") + call assert_equal(['foo', 'foo', 'foo'], t) + + let @/ = 'bar' + let t = execute('g?')->trim()->split("\n") + call assert_equal(['bar'], t) + + " Test for the 'Pattern found in every line' message + let v:statusmsg = '' + v/foo\|bar/p + call assert_notequal('', v:statusmsg) + + close! +endfunc + func Test_wrong_delimiter() call assert_fails('g x^bxd', 'E146:') endfunc diff --git a/src/nvim/testdir/test_help.vim b/src/nvim/testdir/test_help.vim index 8e59efd22d..b2d943be00 100644 --- a/src/nvim/testdir/test_help.vim +++ b/src/nvim/testdir/test_help.vim @@ -1,4 +1,3 @@ - " Tests for :help func Test_help_restore_snapshot() @@ -13,6 +12,18 @@ endfunc func Test_help_errors() call assert_fails('help doesnotexist', 'E149:') call assert_fails('help!', 'E478:') + if has('multi_lang') + call assert_fails('help help@xy', 'E661:') + endif + + let save_hf = &helpfile + set helpfile=help_missing + help + call assert_equal(1, winnr('$')) + call assert_notequal('help', &buftype) + let &helpfile = save_hf + + call assert_fails('help ' . repeat('a', 1048), 'E149:') new set keywordprg=:help @@ -101,4 +112,13 @@ func Test_helptag_cmd() call delete('Xdir', 'rf') endfunc +func Test_help_long_argument() + try + exe 'help \%' .. repeat('0', 1021) + catch + call assert_match("E149:", v:exception) + endtry +endfunc + + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_help_tagjump.vim b/src/nvim/testdir/test_help_tagjump.vim index a6494c531c..a43889b57e 100644 --- a/src/nvim/testdir/test_help_tagjump.vim +++ b/src/nvim/testdir/test_help_tagjump.vim @@ -23,6 +23,11 @@ func Test_help_tagjump() call assert_true(getline('.') =~ '\*bar\*') helpclose + help " + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*quote\*') + helpclose + help "* call assert_equal("help", &filetype) call assert_true(getline('.') =~ '\*quotestar\*') @@ -86,11 +91,40 @@ func Test_help_tagjump() call assert_true(getline('.') =~ '\*i_^_CTRL-D\*') helpclose + help i^x^y + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*i_CTRL-X_CTRL-Y\*') + helpclose + + exe "help i\<C-\>\<C-G>" + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*i_CTRL-\\_CTRL-G\*') + helpclose + exec "help \<C-V>" call assert_equal("help", &filetype) call assert_true(getline('.') =~ '\*CTRL-V\*') helpclose + help /\| + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*/\\bar\*') + helpclose + + help CTRL-\_CTRL-N + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*CTRL-\\_CTRL-N\*') + helpclose + + help `:pwd`, + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*:pwd\*') + helpclose + + help `:ls`. + call assert_equal("help", &filetype) + call assert_true(getline('.') =~ '\*:ls\*') + helpclose exec "help! ('textwidth'" call assert_equal("help", &filetype) @@ -122,6 +156,15 @@ func Test_help_tagjump() call assert_true(getline('.') =~ '\*{address}\*') helpclose + " Use special patterns in the help tag + for h in ['/\w', '/\%^', '/\%(', '/\zs', '/\@<=', '/\_$', '[++opt]', '/\{'] + exec "help! " . h + call assert_equal("help", &filetype) + let pat = '\*' . escape(h, '\$[') . '\*' + call assert_true(getline('.') =~ pat, pat) + helpclose + endfor + exusage call assert_equal("help", &filetype) call assert_true(getline('.') =~ '\*:index\*') diff --git a/src/nvim/testdir/test_highlight.vim b/src/nvim/testdir/test_highlight.vim index 899eb530ec..6971ecd357 100644 --- a/src/nvim/testdir/test_highlight.vim +++ b/src/nvim/testdir/test_highlight.vim @@ -597,6 +597,31 @@ func Test_cursorline_with_visualmode() call delete('Xtest_cursorline_with_visualmode') endfunc +func Test_colorcolumn() + CheckScreendump + + " check that setting 'colorcolumn' when entering a buffer works + let lines =<< trim END + split + edit X + call setline(1, ["1111111111","22222222222","3333333333"]) + set nomodified + set colorcolumn=3,9 + set number cursorline cursorlineopt=number + wincmd w + buf X + END + call writefile(lines, 'Xtest_colorcolumn') + let buf = RunVimInTerminal('-S Xtest_colorcolumn', {'rows': 10}) + call term_sendkeys(buf, ":\<CR>") + call term_wait(buf) + call VerifyScreenDump(buf, 'Test_colorcolumn_1', {}) + + " clean up + call StopVimInTerminal(buf) + call delete('Xtest_colorcolumn') +endfunc + func Test_colorcolumn_bri() CheckScreendump diff --git a/src/nvim/testdir/test_ins_complete.vim b/src/nvim/testdir/test_ins_complete.vim index ce75799551..186fa8871f 100644 --- a/src/nvim/testdir/test_ins_complete.vim +++ b/src/nvim/testdir/test_ins_complete.vim @@ -109,7 +109,7 @@ func s:CompleteDone_CompleteFuncNone( findstart, base ) return v:none endfunc -function! s:CompleteDone_CompleteFuncDict( findstart, base ) +func s:CompleteDone_CompleteFuncDict( findstart, base ) if a:findstart return 0 endif @@ -126,7 +126,7 @@ function! s:CompleteDone_CompleteFuncDict( findstart, base ) \ } \ ] \ } -endfunction +endfunc func s:CompleteDone_CheckCompletedItemNone() let s:called_completedone = 1 @@ -341,6 +341,14 @@ func Test_compl_feedkeys() set completeopt& endfunc +func s:ComplInCmdwin_GlobalCompletion(a, l, p) + return 'global' +endfunc + +func s:ComplInCmdwin_LocalCompletion(a, l, p) + return 'local' +endfunc + func Test_compl_in_cmdwin() set wildmenu wildchar=<Tab> com! -nargs=1 -complete=command GetInput let input = <q-args> @@ -376,6 +384,47 @@ func Test_compl_in_cmdwin() call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!') call assert_equal('b:test_', input) + + " Argument completion of buffer-local command + func s:ComplInCmdwin_GlobalCompletionList(a, l, p) + return ['global'] + endfunc + + func s:ComplInCmdwin_LocalCompletionList(a, l, p) + return ['local'] + endfunc + + func s:ComplInCmdwin_CheckCompletion(arg) + call assert_equal('local', a:arg) + endfunc + + com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion + \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) + com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion + \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) + call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!') + + com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList + \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) + com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList + \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>) + + call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!') + + func! s:ComplInCmdwin_CheckCompletion(arg) + call assert_equal('global', a:arg) + endfunc + new + call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!') + quit + + delfunc s:ComplInCmdwin_GlobalCompletion + delfunc s:ComplInCmdwin_LocalCompletion + delfunc s:ComplInCmdwin_GlobalCompletionList + delfunc s:ComplInCmdwin_LocalCompletionList + delfunc s:ComplInCmdwin_CheckCompletion + + delcom -buffer TestCommand delcom TestCommand delcom GetInput unlet w:test_winvar @@ -445,6 +494,28 @@ func Test_issue_7021() set completeslash= endfunc +func Test_pum_stopped_by_timer() + CheckScreendump + + let lines =<< trim END + call setline(1, ['hello', 'hullo', 'heeee', '']) + func StartCompl() + call timer_start(100, { -> execute('stopinsert') }) + call feedkeys("Gah\<C-N>") + endfunc + END + + call writefile(lines, 'Xpumscript') + let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12}) + call term_sendkeys(buf, ":call StartCompl()\<CR>") + call TermWait(buf, 200) + call term_sendkeys(buf, "k") + call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {}) + + call StopVimInTerminal(buf) + call delete('Xpumscript') +endfunc + func Test_pum_with_folds_two_tabs() CheckScreendump diff --git a/src/nvim/testdir/test_lambda.vim b/src/nvim/testdir/test_lambda.vim index 63bb4ae1ef..c1fe47d1c9 100644 --- a/src/nvim/testdir/test_lambda.vim +++ b/src/nvim/testdir/test_lambda.vim @@ -1,24 +1,24 @@ " Test for lambda and closure -function! Test_lambda_feature() +func Test_lambda_feature() call assert_equal(1, has('lambda')) -endfunction +endfunc -function! Test_lambda_with_filter() +func Test_lambda_with_filter() let s:x = 2 call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x})) -endfunction +endfunc -function! Test_lambda_with_map() +func Test_lambda_with_map() let s:x = 1 call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x})) -endfunction +endfunc -function! Test_lambda_with_sort() +func Test_lambda_with_sort() call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b})) -endfunction +endfunc -function! Test_lambda_with_timer() +func Test_lambda_with_timer() if !has('timers') return endif @@ -54,10 +54,10 @@ function! Test_lambda_with_timer() call assert_true(s:n > m) endfunc -function! Test_lambda_with_partial() +func Test_lambda_with_partial() let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three')) -endfunction +endfunc function Test_lambda_fails() call assert_equal(3, {a, b -> a + b}(1, 2)) @@ -70,59 +70,59 @@ func Test_not_lambda() call assert_equal('foo', x['>']) endfunc -function! Test_lambda_capture_by_reference() +func Test_lambda_capture_by_reference() let v = 1 let l:F = {x -> x + v} let v = 2 call assert_equal(12, l:F(10)) -endfunction +endfunc -function! Test_lambda_side_effect() - function! s:update_and_return(arr) +func Test_lambda_side_effect() + func! s:update_and_return(arr) let a:arr[1] = 5 return a:arr - endfunction + endfunc - function! s:foo(arr) + func! s:foo(arr) return {-> s:update_and_return(a:arr)} - endfunction + endfunc let arr = [3,2,1] call assert_equal([3, 5, 1], s:foo(arr)()) -endfunction +endfunc -function! Test_lambda_refer_local_variable_from_other_scope() - function! s:foo(X) +func Test_lambda_refer_local_variable_from_other_scope() + func! s:foo(X) return a:X() " refer l:x in s:bar() - endfunction + endfunc - function! s:bar() + func! s:bar() let x = 123 return s:foo({-> x}) - endfunction + endfunc call assert_equal(123, s:bar()) -endfunction +endfunc -function! Test_lambda_do_not_share_local_variable() - function! s:define_funcs() +func Test_lambda_do_not_share_local_variable() + func! s:define_funcs() let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]} let l:Two = {-> exists("a") ? a : "no"} return [l:One, l:Two] - endfunction + endfunc let l:F = s:define_funcs() call assert_equal('no', l:F[1]()) call assert_equal('abc', l:F[0]()) call assert_equal('no', l:F[1]()) -endfunction +endfunc -function! Test_lambda_closure_counter() - function! s:foo() +func Test_lambda_closure_counter() + func! s:foo() let x = 0 return {-> [execute("let x += 1"), x][-1]} - endfunction + endfunc let l:F = s:foo() call garbagecollect() @@ -130,52 +130,52 @@ function! Test_lambda_closure_counter() call assert_equal(2, l:F()) call assert_equal(3, l:F()) call assert_equal(4, l:F()) -endfunction +endfunc -function! Test_lambda_with_a_var() - function! s:foo() +func Test_lambda_with_a_var() + func! s:foo() let x = 2 return {... -> a:000 + [x]} - endfunction - function! s:bar() + endfunc + func! s:bar() return s:foo()(1) - endfunction + endfunc call assert_equal([1, 2], s:bar()) -endfunction +endfunc -function! Test_lambda_call_lambda_from_lambda() - function! s:foo(x) +func Test_lambda_call_lambda_from_lambda() + func! s:foo(x) let l:F1 = {-> {-> a:x}} return {-> l:F1()} - endfunction + endfunc let l:F = s:foo(1) call assert_equal(1, l:F()()) -endfunction +endfunc -function! Test_lambda_delfunc() - function! s:gen() +func Test_lambda_delfunc() + func! s:gen() let pl = l: let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))} let l:Bar = l:Foo delfunction l:Foo return l:Bar - endfunction + endfunc let l:F = s:gen() call assert_fails(':call l:F()', 'E933:') -endfunction +endfunc -function! Test_lambda_scope() - function! s:NewCounter() +func Test_lambda_scope() + func! s:NewCounter() let c = 0 return {-> [execute('let c += 1'), c][-1]} - endfunction + endfunc - function! s:NewCounter2() + func! s:NewCounter2() return {-> [execute('let c += 100'), c][-1]} - endfunction + endfunc let l:C = s:NewCounter() let l:D = s:NewCounter2() @@ -183,37 +183,37 @@ function! Test_lambda_scope() call assert_equal(1, l:C()) call assert_fails(':call l:D()', 'E121:') call assert_equal(2, l:C()) -endfunction +endfunc -function! Test_lambda_share_scope() - function! s:New() +func Test_lambda_share_scope() + func! s:New() let c = 0 let l:Inc0 = {-> [execute('let c += 1'), c][-1]} let l:Dec0 = {-> [execute('let c -= 1'), c][-1]} return [l:Inc0, l:Dec0] - endfunction + endfunc let [l:Inc, l:Dec] = s:New() call assert_equal(1, l:Inc()) call assert_equal(2, l:Inc()) call assert_equal(1, l:Dec()) -endfunction +endfunc -function! Test_lambda_circular_reference() - function! s:Foo() +func Test_lambda_circular_reference() + func! s:Foo() let d = {} let d.f = {-> d} return d.f - endfunction + endfunc call s:Foo() call garbagecollect() let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhile call garbagecollect() -endfunction +endfunc -function! Test_lambda_combination() +func Test_lambda_combination() call assert_equal(2, {x -> {x -> x}}(1)(2)) call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z})) call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0)) @@ -226,17 +226,17 @@ function! Test_lambda_combination() let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})} let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}} call assert_equal(120, Z(Fact)(5)) -endfunction +endfunc -function! Test_closure_counter() - function! s:foo() +func Test_closure_counter() + func! s:foo() let x = 0 - function! s:bar() closure + func! s:bar() closure let x += 1 return x - endfunction + endfunc return function('s:bar') - endfunction + endfunc let l:F = s:foo() call garbagecollect() @@ -244,30 +244,30 @@ function! Test_closure_counter() call assert_equal(2, l:F()) call assert_equal(3, l:F()) call assert_equal(4, l:F()) -endfunction +endfunc -function! Test_closure_unlet() - function! s:foo() +func Test_closure_unlet() + func! s:foo() let x = 1 - function! s:bar() closure + func! s:bar() closure unlet x - endfunction + endfunc call s:bar() return l: - endfunction + endfunc call assert_false(has_key(s:foo(), 'x')) call garbagecollect() -endfunction +endfunc -function! LambdaFoo() +func LambdaFoo() let x = 0 - function! LambdaBar() closure + func! LambdaBar() closure let x += 1 return x - endfunction + endfunc return function('LambdaBar') -endfunction +endfunc func Test_closure_refcount() let g:Count = LambdaFoo() @@ -303,3 +303,8 @@ func Test_lambda_with_index() let Extract = {-> function(List, ['foobar'])()[0]} call assert_equal('foobar', Extract()) endfunc + +func Test_lambda_error() + " This was causing a crash + call assert_fails('ec{@{->{d->()()', 'E15') +endfunc diff --git a/src/nvim/testdir/test_listchars.vim b/src/nvim/testdir/test_listchars.vim index 0bcbd9c4a5..c6e2ebd406 100644 --- a/src/nvim/testdir/test_listchars.vim +++ b/src/nvim/testdir/test_listchars.vim @@ -1,6 +1,8 @@ " Tests for 'listchars' display with 'list' and :list +source check.vim source view_util.vim +source screendump.vim func Test_listchars() enew! @@ -517,4 +519,34 @@ func Test_listchars_window_local() set list& listchars& endfunc +func Test_listchars_foldcolumn() + CheckScreendump + + let lines =<< trim END + call setline(1, ['aaa', '', 'a', 'aaaaaa']) + vsplit + vsplit + windo set signcolumn=yes foldcolumn=1 winminwidth=0 nowrap list listchars=extends:>,precedes:< + END + call writefile(lines, 'XTest_listchars') + + let buf = RunVimInTerminal('-S XTest_listchars', {'rows': 10, 'cols': 60}) + + call term_sendkeys(buf, "13\<C-W>>") + call VerifyScreenDump(buf, 'Test_listchars_01', {}) + call term_sendkeys(buf, "\<C-W>>") + call VerifyScreenDump(buf, 'Test_listchars_02', {}) + call term_sendkeys(buf, "\<C-W>>") + call VerifyScreenDump(buf, 'Test_listchars_03', {}) + call term_sendkeys(buf, "\<C-W>>") + call VerifyScreenDump(buf, 'Test_listchars_04', {}) + call term_sendkeys(buf, "\<C-W>>") + call VerifyScreenDump(buf, 'Test_listchars_05', {}) + + " clean up + call StopVimInTerminal(buf) + call delete('XTest_listchars') +endfunc + + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_listdict.vim b/src/nvim/testdir/test_listdict.vim index f6c404d390..aa66d86af1 100644 --- a/src/nvim/testdir/test_listdict.vim +++ b/src/nvim/testdir/test_listdict.vim @@ -516,22 +516,22 @@ func Test_dict_lock_operator() endfunc " No remove() of write-protected scope-level variable -func! Tfunc(this_is_a_long_parameter_name) +func Tfunc1(this_is_a_long_parameter_name) call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742') -endfun +endfunc func Test_dict_scope_var_remove() - call Tfunc('testval') + call Tfunc1('testval') endfunc " No extend() of write-protected scope-level variable func Test_dict_scope_var_extend() call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742') endfunc -func! Tfunc(this_is_a_long_parameter_name) +func Tfunc2(this_is_a_long_parameter_name) call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742') endfunc func Test_dict_scope_var_extend_overwrite() - call Tfunc('testval') + call Tfunc2('testval') endfunc " No :unlet of variable in locked scope @@ -620,6 +620,49 @@ func Test_reverse_sort_uniq() call assert_fails('call reverse("")', 'E899:') endfunc +" reduce a list or a blob +func Test_reduce() + call assert_equal(1, reduce([], { acc, val -> acc + val }, 1)) + call assert_equal(10, reduce([1, 3, 5], { acc, val -> acc + val }, 1)) + call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], { acc, val -> 2 * acc + val }, 1)) + call assert_equal('a x y z', ['x', 'y', 'z']->reduce({ acc, val -> acc .. ' ' .. val}, 'a')) + call assert_equal(#{ x: 1, y: 1, z: 1 }, ['x', 'y', 'z']->reduce({ acc, val -> extend(acc, { val: 1 }) }, {})) + call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0])) + + let l = ['x', 'y', 'z'] + call assert_equal(42, reduce(l, function('get'), #{ x: #{ y: #{ z: 42 } } })) + call assert_equal(['x', 'y', 'z'], l) + + call assert_equal(1, reduce([1], { acc, val -> acc + val })) + call assert_equal('x y z', reduce(['x', 'y', 'z'], { acc, val -> acc .. ' ' .. val })) + call assert_equal(120, range(1, 5)->reduce({ acc, val -> acc * val })) + call assert_fails("call reduce([], { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value') + + call assert_equal(1, reduce(0z, { acc, val -> acc + val }, 1)) + call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, { acc, val -> acc + val }, 1)) + call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce({ acc, val -> 2 * acc + val }, 1)) + + call assert_equal(0xff, reduce(0zff, { acc, val -> acc + val })) + call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, { acc, val -> 2 * acc + val })) + call assert_fails("call reduce(0z, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value') + + call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E897:') + call assert_fails("call reduce(0, { acc, val -> acc + val }, 1)", 'E897:') + call assert_fails("call reduce('', { acc, val -> acc + val }, 1)", 'E897:') + + let g:lut = [1, 2, 3, 4] + func EvilRemove() + call remove(g:lut, 1) + return 1 + endfunc + call assert_fails("call reduce(g:lut, { acc, val -> EvilRemove() }, 1)", 'E742:') + unlet g:lut + delfunc EvilRemove + + call assert_equal(42, reduce(v:_null_list, function('add'), 42)) + call assert_equal(42, reduce(v:_null_blob, function('add'), 42)) +endfunc + " splitting a string to a List func Test_str_split() call assert_equal(['aa', 'bb'], split(' aa bb ')) diff --git a/src/nvim/testdir/test_listlbr_utf8.vim b/src/nvim/testdir/test_listlbr_utf8.vim index c38e0c5f3c..1f100d6244 100644 --- a/src/nvim/testdir/test_listlbr_utf8.vim +++ b/src/nvim/testdir/test_listlbr_utf8.vim @@ -69,6 +69,16 @@ func Test_nolinebreak_with_list() call s:close_windows() endfunc +" this was causing a crash +func Test_linebreak_with_list_and_tabs() + set linebreak list listchars=tab:⇤\ ⇥ tabstop=100 + new + call setline(1, "\t\t\ttext") + redraw + bwipe! + set nolinebreak nolist listchars&vim tabstop=8 +endfunc + func Test_linebreak_with_nolist() call s:test_windows('setl nolist') call setline(1, "\t*mask = nil;") diff --git a/src/nvim/testdir/test_marks.vim b/src/nvim/testdir/test_marks.vim index b3035d73ce..00ee8f6d6a 100644 --- a/src/nvim/testdir/test_marks.vim +++ b/src/nvim/testdir/test_marks.vim @@ -1,6 +1,6 @@ " Test that a deleted mark is restored after delete-undo-redo-undo. -function! Test_Restore_DelMark() +func Test_Restore_DelMark() enew! call append(0, [" textline A", " textline B", " textline C"]) normal! 2gg @@ -11,10 +11,10 @@ function! Test_Restore_DelMark() call assert_equal(2, pos[1]) call assert_equal(1, pos[2]) enew! -endfunction +endfunc " Test that CTRL-A and CTRL-X updates last changed mark '[, ']. -function! Test_Incr_Marks() +func Test_Incr_Marks() enew! call append(0, ["123 123 123", "123 123 123", "123 123 123"]) normal! gg @@ -23,7 +23,17 @@ function! Test_Incr_Marks() call assert_equal("123 XXXXXXX", getline(2)) call assert_equal("XXX 123 123", getline(3)) enew! -endfunction +endfunc + +func Test_previous_jump_mark() + new + call setline(1, ['']->repeat(6)) + normal Ggg + call assert_equal(6, getpos("''")[1]) + normal jjjjj + call assert_equal(6, getpos("''")[1]) + bwipe! +endfunc func Test_setpos() new Xone @@ -207,6 +217,21 @@ func Test_mark_error() call assert_fails('mark _', 'E191:') endfunc +" Test for :lockmarks when pasting content +func Test_lockmarks_with_put() + new + call append(0, repeat(['sky is blue'], 4)) + normal gg + 1,2yank r + put r + normal G + lockmarks put r + call assert_equal(2, line("'[")) + call assert_equal(3, line("']")) + + bwipe! +endfunc + " Test for the getmarklist() function func Test_getmarklist() new @@ -231,3 +256,5 @@ func Test_getmarklist() call assert_equal([], {}->getmarklist()) close! endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_matchadd_conceal.vim b/src/nvim/testdir/test_matchadd_conceal.vim index 2cbaf5cb76..29a2c30b0d 100644 --- a/src/nvim/testdir/test_matchadd_conceal.vim +++ b/src/nvim/testdir/test_matchadd_conceal.vim @@ -7,7 +7,7 @@ source shared.vim source term_util.vim source view_util.vim -function! Test_simple_matchadd() +func Test_simple_matchadd() new 1put='# This is a Test' @@ -333,7 +333,7 @@ func Test_matchadd_and_syn_conceal() call assert_notequal(screenattr(1, 10) , screenattr(1, 11)) call assert_notequal(screenattr(1, 11) , screenattr(1, 12)) call assert_equal(screenattr(1, 11) , screenattr(1, 32)) -endfunction +endfunc func Test_cursor_column_in_concealed_line_after_window_scroll() CheckRunVimInTerminal diff --git a/src/nvim/testdir/test_matchadd_conceal_utf8.vim b/src/nvim/testdir/test_matchadd_conceal_utf8.vim index 7bfac13ad8..1d0c740734 100644 --- a/src/nvim/testdir/test_matchadd_conceal_utf8.vim +++ b/src/nvim/testdir/test_matchadd_conceal_utf8.vim @@ -3,19 +3,19 @@ if !has('conceal') finish endif -function! s:screenline(lnum) abort +func s:screenline(lnum) abort let line = [] for c in range(1, winwidth(0)) call add(line, nr2char(a:lnum->screenchar(c))) endfor return s:trim(join(line, '')) -endfunction +endfunc -function! s:trim(str) abort +func s:trim(str) abort return matchstr(a:str,'^\s*\zs.\{-}\ze\s*$') -endfunction +endfunc -function! Test_match_using_multibyte_conceal_char() +func Test_match_using_multibyte_conceal_char() new setlocal concealcursor=n conceallevel=1 diff --git a/src/nvim/testdir/test_matchfuzzy.vim b/src/nvim/testdir/test_matchfuzzy.vim new file mode 100644 index 0000000000..abcc9b40c1 --- /dev/null +++ b/src/nvim/testdir/test_matchfuzzy.vim @@ -0,0 +1,248 @@ +" Tests for fuzzy matching + +source shared.vim +source check.vim + +" Test for matchfuzzy() +func Test_matchfuzzy() + call assert_fails('call matchfuzzy(10, "abc")', 'E686:') + " Needs v8.2.1183; match the final error that's thrown for now + " call assert_fails('call matchfuzzy(["abc"], [])', 'E730:') + call assert_fails('call matchfuzzy(["abc"], [])', 'E475:') + call assert_fails("let x = matchfuzzy(v:_null_list, 'foo')", 'E686:') + call assert_fails('call matchfuzzy(["abc"], v:_null_string)', 'E475:') + call assert_equal([], matchfuzzy([], 'abc')) + call assert_equal([], matchfuzzy(['abc'], '')) + call assert_equal(['abc'], matchfuzzy(['abc', 10], 'ac')) + call assert_equal([], matchfuzzy([10, 20], 'ac')) + call assert_equal(['abc'], matchfuzzy(['abc'], 'abc')) + call assert_equal(['crayon', 'camera'], matchfuzzy(['camera', 'crayon'], 'cra')) + call assert_equal(['aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa', 'aba'], matchfuzzy(['aba', 'aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa'], 'aa')) + call assert_equal(['one'], matchfuzzy(['one', 'two'], 'one')) + call assert_equal(['oneTwo', 'onetwo'], matchfuzzy(['onetwo', 'oneTwo'], 'oneTwo')) + call assert_equal(['onetwo', 'one_two'], matchfuzzy(['onetwo', 'one_two'], 'oneTwo')) + call assert_equal(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], matchfuzzy(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], 'aa')) + call assert_equal(256, matchfuzzy([repeat('a', 256)], repeat('a', 256))[0]->len()) + call assert_equal([], matchfuzzy([repeat('a', 300)], repeat('a', 257))) + " matches with same score should not be reordered + let l = ['abc1', 'abc2', 'abc3'] + call assert_equal(l, l->matchfuzzy('abc')) + + " Tests for match preferences + " preference for camel case match + call assert_equal(['oneTwo', 'onetwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo')) + " preference for match after a separator (_ or space) + call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo')) + " preference for leading letter match + call assert_equal(['onetwo', 'xonetwo'], ['xonetwo', 'onetwo']->matchfuzzy('onetwo')) + " preference for sequential match + call assert_equal(['onetwo', 'oanbectdweo'], ['oanbectdweo', 'onetwo']->matchfuzzy('onetwo')) + " non-matching leading letter(s) penalty + call assert_equal(['xonetwo', 'xxonetwo'], ['xxonetwo', 'xonetwo']->matchfuzzy('onetwo')) + " total non-matching letter(s) penalty + call assert_equal(['one', 'onex', 'onexx'], ['onexx', 'one', 'onex']->matchfuzzy('one')) + " prefer complete matches over separator matches + call assert_equal(['.vim/vimrc', '.vim/vimrc_colors', '.vim/v_i_m_r_c'], ['.vim/vimrc', '.vim/vimrc_colors', '.vim/v_i_m_r_c']->matchfuzzy('vimrc')) + " gap penalty + call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab')) + " path separator vs word separator + call assert_equal(['color/setup.vim', 'color\\setup.vim', 'color setup.vim', 'color_setup.vim', 'colorsetup.vim'], matchfuzzy(['colorsetup.vim', 'color setup.vim', 'color/setup.vim', 'color_setup.vim', 'color\\setup.vim'], 'setup.vim')) + + " match multiple words (separated by space) + call assert_equal(['foo bar baz'], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('baz foo')) + call assert_equal([], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('one two')) + call assert_equal([], ['foo bar']->matchfuzzy(" \t ")) + + " test for matching a sequence of words + call assert_equal(['bar foo'], ['foo bar', 'bar foo', 'foobar', 'barfoo']->matchfuzzy('bar foo', {'matchseq' : 1})) + call assert_equal([#{text: 'two one'}], [#{text: 'one two'}, #{text: 'two one'}]->matchfuzzy('two one', #{key: 'text', matchseq: v:true})) + + %bw! + eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)}) + let l = getbufinfo()->map({_, v -> v.name})->matchfuzzy('ndl') + call assert_equal(1, len(l)) + call assert_match('needle', l[0]) + + " Test for fuzzy matching dicts + let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}] + call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}})) + call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'})) + call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}})) + call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'})) + call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E715:') + call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}})) + call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}})) + call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:') + call assert_equal([], matchfuzzy(l, 'cam')) + " Nvim's callback implementation is different, so E6000 is expected instead, + " but we need v8.2.1183 to assert it + " call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:') + " call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E6000:') + call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E475:') + " call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:') + call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E475:') + call assert_fails("let x = matchfuzzy(l, 'cam', v:_null_dict)", 'E715:') + call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : v:_null_string})", 'E475:') + " Nvim doesn't have null functions + " call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:') + " matches with same score should not be reordered + let l = [#{text: 'abc', id: 1}, #{text: 'abc', id: 2}, #{text: 'abc', id: 3}] + call assert_equal(l, l->matchfuzzy('abc', #{key: 'text'})) + + let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}] + call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:') + + " Test in latin1 encoding + let save_enc = &encoding + " Nvim supports utf-8 encoding only + " set encoding=latin1 + call assert_equal(['abc'], matchfuzzy(['abc'], 'abc')) + let &encoding = save_enc +endfunc + +" Test for the matchfuzzypos() function +func Test_matchfuzzypos() + call assert_equal([['curl', 'world'], [[2,3], [2,3]], [128, 127]], matchfuzzypos(['world', 'curl'], 'rl')) + call assert_equal([['curl', 'world'], [[2,3], [2,3]], [128, 127]], matchfuzzypos(['world', 'one', 'curl'], 'rl')) + call assert_equal([['hello', 'hello world hello world'], + \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [275, 257]], + \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello')) + call assert_equal([['aaaaaaa'], [[0, 1, 2]], [191]], matchfuzzypos(['aaaaaaa'], 'aaa')) + call assert_equal([['a b'], [[0, 3]], [219]], matchfuzzypos(['a b'], 'a b')) + call assert_equal([['a b'], [[0, 3]], [219]], matchfuzzypos(['a b'], 'a b')) + call assert_equal([['a b'], [[0]], [112]], matchfuzzypos(['a b'], ' a ')) + call assert_equal([[], [], []], matchfuzzypos(['a b'], ' ')) + call assert_equal([[], [], []], matchfuzzypos(['world', 'curl'], 'ab')) + let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256)) + call assert_equal(range(256), x[1][0]) + call assert_equal([[], [], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257))) + call assert_equal([[], [], []], matchfuzzypos([], 'abc')) + + " match in a long string + call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]], [-135]], + \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc')) + + " preference for camel case match + call assert_equal([['xabcxxaBc'], [[6, 7, 8]], [189]], matchfuzzypos(['xabcxxaBc'], 'abc')) + " preference for match after a separator (_ or space) + call assert_equal([['xabx_ab'], [[5, 6]], [145]], matchfuzzypos(['xabx_ab'], 'ab')) + " preference for leading letter match + call assert_equal([['abcxabc'], [[0, 1]], [150]], matchfuzzypos(['abcxabc'], 'ab')) + " preference for sequential match + call assert_equal([['aobncedone'], [[7, 8, 9]], [158]], matchfuzzypos(['aobncedone'], 'one')) + " best recursive match + call assert_equal([['xoone'], [[2, 3, 4]], [168]], matchfuzzypos(['xoone'], 'one')) + + " match multiple words (separated by space) + call assert_equal([['foo bar baz'], [[8, 9, 10, 0, 1, 2]], [369]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo')) + call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('one two')) + call assert_equal([[], [], []], ['foo bar']->matchfuzzypos(" \t ")) + call assert_equal([['grace'], [[1, 2, 3, 4, 2, 3, 4, 0, 1, 2, 3, 4]], [657]], ['grace']->matchfuzzypos('race ace grace')) + + let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}] + call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [192]], + \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}})) + call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [192]], + \ matchfuzzypos(l, 'cam', {'key' : 'val'})) + call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}})) + call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'key' : 'val'})) + call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E715:') + call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}})) + call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}})) + call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:') + call assert_equal([[], [], []], matchfuzzypos(l, 'cam')) + " Nvim's callback implementation is different, so E6000 is expected instead, + " but we need v8.2.1183 to assert it + " call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:') + " call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E6000:') + call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E475:') + " call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:') + call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E475:') + call assert_fails("let x = matchfuzzypos(l, 'cam', v:_null_dict)", 'E715:') + call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : v:_null_string})", 'E475:') + " Nvim doesn't have null functions + " call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:') + + let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}] + call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:') +endfunc + +" Test for matchfuzzy() with multibyte characters +func Test_matchfuzzy_mbyte() + CheckFeature multi_lang + call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ')) + " reverse the order of characters + call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ')) + call assert_equal(['αβΩxxx', 'xαxβxΩx'], + \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ')) + call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'], + \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ')) + + " match multiple words (separated by space) + call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의')) + call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘')) + + " preference for camel case match + call assert_equal(['oneĄwo', 'oneąwo'], + \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo')) + " preference for complete match then match after separator (_ or space) + call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']), + \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ')) + " preference for match after a separator (_ or space) + call assert_equal(['ㄓㄔabㄟㄠ', 'ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ'], + \ ['ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ', 'ㄓㄔabㄟㄠ']->matchfuzzy('ㄓㄔabㄟㄠ')) + " preference for leading letter match + call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'], + \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż')) + " preference for sequential match + call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'], + \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl')) + " non-matching leading letter(s) penalty + call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'], + \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl')) + " total non-matching letter(s) penalty + call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'], + \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ')) +endfunc + +" Test for matchfuzzypos() with multibyte characters +func Test_matchfuzzypos_mbyte() + CheckFeature multi_lang + call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]], [273]], + \ matchfuzzypos(['こんにちは世界'], 'こんにちは')) + call assert_equal([['ンヹㄇヺヴ'], [[1, 3]], [88]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ')) + " reverse the order of characters + call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ')) + call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]], [222, 113]], + \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ')) + call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'], + \ [[0, 1], [0, 1], [0, 1], [0, 2]], [151, 148, 145, 110]], + \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ')) + call assert_equal([['ααααααα'], [[0, 1, 2]], [191]], + \ matchfuzzypos(['ααααααα'], 'ααα')) + + call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl')) + let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256)) + call assert_equal(range(256), x[1][0]) + call assert_equal([[], [], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257))) + + " match multiple words (separated by space) + call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]], [328]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의')) + call assert_equal([[], [], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘')) + + " match in a long string + call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]], [-135]], + \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ')) + " preference for camel case match + call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]], [189]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ')) + " preference for match after a separator (_ or space) + call assert_equal([['xちだx_ちだ'], [[5, 6]], [145]], matchfuzzypos(['xちだx_ちだ'], 'ちだ')) + " preference for leading letter match + call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]], [150]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ')) + " preference for sequential match + call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]], [158]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ')) + " best recursive match + call assert_equal([['xффйд'], [[2, 3, 4]], [168]], matchfuzzypos(['xффйд'], 'фйд')) +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_messages.vim b/src/nvim/testdir/test_messages.vim index e0286548d9..9c84d77dd2 100644 --- a/src/nvim/testdir/test_messages.vim +++ b/src/nvim/testdir/test_messages.vim @@ -40,7 +40,7 @@ endfunc " indicator (e.g., "-- INSERT --") when ":stopinsert" is invoked. Message " output could then be disturbed when 'cmdheight' was greater than one. " This test ensures that the bugfix for this issue remains in place. -function! Test_stopinsert_does_not_break_message_output() +func Test_stopinsert_does_not_break_message_output() set cmdheight=2 redraw! @@ -55,7 +55,7 @@ function! Test_stopinsert_does_not_break_message_output() redraw! set cmdheight& -endfunction +endfunc func Test_message_completion() call feedkeys(":message \<C-A>\<C-B>\"\<CR>", 'tx') diff --git a/src/nvim/testdir/test_mksession.vim b/src/nvim/testdir/test_mksession.vim index 057895047d..a8e50af510 100644 --- a/src/nvim/testdir/test_mksession.vim +++ b/src/nvim/testdir/test_mksession.vim @@ -309,6 +309,31 @@ func Test_mksession_buffer_count() set nohidden endfunc +func Test_mksession_buffer_order() + %bwipe! + e Xfoo | e Xbar | e Xbaz | e Xqux + bufdo write + mksession! Xtest_mks.out + + " Verify that loading the session preserves order of buffers + %bwipe! + source Xtest_mks.out + + let s:buf_info = getbufinfo() + call assert_true(s:buf_info[0]['name'] =~# 'Xfoo$') + call assert_true(s:buf_info[1]['name'] =~# 'Xbar$') + call assert_true(s:buf_info[2]['name'] =~# 'Xbaz$') + call assert_true(s:buf_info[3]['name'] =~# 'Xqux$') + + " Clean up. + call delete('Xfoo') + call delete('Xbar') + call delete('Xbaz') + call delete('Xqux') + call delete('Xtest_mks.out') + %bwipe! +endfunc + if has('extra_search') func Test_mksession_hlsearch() @@ -696,6 +721,36 @@ func Test_mksession_foldopt() set sessionoptions& endfunc +" Test for mksession with "help" but not "options" in 'sessionoptions' +func Test_mksession_help_noopt() + set sessionoptions-=options + set sessionoptions+=help + help + let fname = expand('%') + mksession! Xtest_mks.out + bwipe + + source Xtest_mks.out + call assert_equal('help', &buftype) + call assert_equal('help', &filetype) + call assert_equal(fname, expand('%')) + call assert_false(&modifiable) + call assert_true(&readonly) + + helpclose + help index + let fname = expand('%') + mksession! Xtest_mks.out + bwipe + + source Xtest_mks.out + call assert_equal('help', &buftype) + call assert_equal(fname, expand('%')) + + call delete('Xtest_mks.out') + set sessionoptions& +endfunc + " Test for mksession with window position func Test_mksession_winpos() if !has('gui_running') diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index aff22f5d01..5b7cf6fee5 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -1812,7 +1812,15 @@ fun! Test_normal33_g_cmd2() call assert_equal(87, col('.')) call assert_equal('E', getreg(0)) + " Test for gM with Tab characters + call setline('.', "\ta\tb\tc\td\te\tf") + norm! gMyl + call assert_equal(6, col('.')) + call assert_equal("c", getreg(0)) + " Test for g Ctrl-G + call setline('.', lineC) + norm! 60gMyl set ff=unix let a=execute(":norm! g\<c-g>") call assert_match('Col 87 of 144; Line 2 of 2; Word 1 of 1; Byte 88 of 146', a) @@ -2759,4 +2767,16 @@ func Test_normal_count_after_operator() bw! endfunc +func Test_normal_gj_on_extra_wide_char() + new | 25vsp + let text='1 foooooooo ar e inszwe1 foooooooo inszwei' . + \ ' i drei vier fünf sechs sieben acht un zehn elf zwöfl' . + \ ' dreizehn v ierzehn fünfzehn' + put =text + call cursor(2,1) + norm! gj + call assert_equal([0,2,25,0], getpos('.')) + bw! +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_number.vim b/src/nvim/testdir/test_number.vim index d737ebe9f0..dfbdc0bffd 100644 --- a/src/nvim/testdir/test_number.vim +++ b/src/nvim/testdir/test_number.vim @@ -284,10 +284,10 @@ func Test_relativenumber_colors() " Default colors call VerifyScreenDump(buf, 'Test_relnr_colors_1', {}) - call term_sendkeys(buf, ":hi LineNrAbove ctermfg=blue\<CR>") + call term_sendkeys(buf, ":hi LineNrAbove ctermfg=blue\<CR>:\<CR>") call VerifyScreenDump(buf, 'Test_relnr_colors_2', {}) - call term_sendkeys(buf, ":hi LineNrBelow ctermfg=green\<CR>") + call term_sendkeys(buf, ":hi LineNrBelow ctermfg=green\<CR>:\<CR>") call VerifyScreenDump(buf, 'Test_relnr_colors_3', {}) call term_sendkeys(buf, ":hi clear LineNrAbove\<CR>") diff --git a/src/nvim/testdir/test_options.vim b/src/nvim/testdir/test_options.vim index 5946732937..8612b7013b 100644 --- a/src/nvim/testdir/test_options.vim +++ b/src/nvim/testdir/test_options.vim @@ -22,16 +22,16 @@ func Test_whichwrap() call assert_equal('h', &whichwrap) set whichwrap& -endfunction +endfunc -function! Test_isfname() +func Test_isfname() " This used to cause Vim to access uninitialized memory. set isfname= call assert_equal("~X", expand("~X")) set isfname& -endfunction +endfunc -function Test_wildchar() +func Test_wildchar() " Empty 'wildchar' used to access invalid memory. call assert_fails('set wildchar=', 'E521:') call assert_fails('set wildchar=abc', 'E521:') @@ -42,7 +42,7 @@ function Test_wildchar() let a=execute('set wildchar?') call assert_equal("\n wildchar=<Esc>", a) set wildchar& -endfunction +endfunc func Test_wildoptions() set wildoptions= @@ -51,7 +51,7 @@ func Test_wildoptions() call assert_equal('tagfile', &wildoptions) endfunc -function! Test_options() +func Test_options_command() let caught = 'ok' try options @@ -88,9 +88,9 @@ function! Test_options() " close option-window close -endfunction +endfunc -function! Test_path_keep_commas() +func Test_path_keep_commas() " Test that changing 'path' keeps two commas. set path=foo,,bar set path-=bar @@ -98,7 +98,7 @@ function! Test_path_keep_commas() call assert_equal('foo,,bar', &path) set path& -endfunction +endfunc func Test_filetype_valid() set ft=valid_name @@ -259,6 +259,8 @@ func Test_set_errors() call assert_fails('set shiftwidth=-1', 'E487:') call assert_fails('set sidescroll=-1', 'E487:') call assert_fails('set tabstop=-1', 'E487:') + call assert_fails('set tabstop=10000', 'E474:') + call assert_fails('set tabstop=5500000000', 'E474:') call assert_fails('set textwidth=-1', 'E487:') call assert_fails('set timeoutlen=-1', 'E487:') call assert_fails('set updatecount=-1', 'E487:') @@ -312,21 +314,50 @@ func Test_set_errors() set modifiable& endfunc +func CheckWasSet(name) + let verb_cm = execute('verbose set ' .. a:name .. '?') + call assert_match('Last set from.*test_options.vim', verb_cm) +endfunc +func CheckWasNotSet(name) + let verb_cm = execute('verbose set ' .. a:name .. '?') + call assert_notmatch('Last set from', verb_cm) +endfunc + " Must be executed before other tests that set 'term'. func Test_000_term_option_verbose() if has('nvim') || has('gui_running') return endif - let verb_cm = execute('verbose set t_cm') - call assert_notmatch('Last set from', verb_cm) + + call CheckWasNotSet('t_cm') let term_save = &term set term=ansi - let verb_cm = execute('verbose set t_cm') - call assert_match('Last set from.*test_options.vim', verb_cm) + call CheckWasSet('t_cm') let &term = term_save endfunc +func Test_copy_context() + setlocal list + call CheckWasSet('list') + split + call CheckWasSet('list') + quit + setlocal nolist + + set ai + call CheckWasSet('ai') + set filetype=perl + call CheckWasSet('filetype') + set fo=tcroq + call CheckWasSet('fo') + + split Xsomebuf + call CheckWasSet('ai') + call CheckWasNotSet('filetype') + call CheckWasSet('fo') +endfunc + func Test_set_ttytype() " Nvim does not support 'ttytype'. if !has('nvim') && !has('gui_running') && has('unix') @@ -368,6 +399,13 @@ func Test_set_all() set tw& iskeyword& splitbelow& endfunc +func Test_set_one_column() + let out_mult = execute('set all')->split("\n") + let out_one = execute('set! all')->split("\n") + " one column should be two to four times as many lines + call assert_inrange(len(out_mult) * 2, len(out_mult) * 4, len(out_one)) +endfunc + func Test_set_values() " The file is only generated when running "make test" in the src directory. if filereadable('opt_test.vim') @@ -647,6 +685,19 @@ func Test_buftype() close! endfunc +" Test for the 'shellquote' option +func Test_shellquote() + CheckUnix + set shellquote=# + set verbose=20 + redir => v + silent! !echo Hello + redir END + set verbose& + set shellquote& + call assert_match(': "#echo Hello#"', v) +endfunc + " Test for setting option values using v:false and v:true func Test_opt_boolean() set number& diff --git a/src/nvim/testdir/test_packadd.vim b/src/nvim/testdir/test_packadd.vim new file mode 100644 index 0000000000..fcb8b8033b --- /dev/null +++ b/src/nvim/testdir/test_packadd.vim @@ -0,0 +1,361 @@ +" Tests for 'packpath' and :packadd + + +func SetUp() + let s:topdir = getcwd() . '/Xdir' + exe 'set packpath=' . s:topdir + let s:plugdir = s:topdir . '/pack/mine/opt/mytest' +endfunc + +func TearDown() + call delete(s:topdir, 'rf') +endfunc + +func Test_packadd() + if !exists('s:plugdir') + echomsg 'when running this test manually, call SetUp() first' + return + endif + + call mkdir(s:plugdir . '/plugin/also', 'p') + call mkdir(s:plugdir . '/ftdetect', 'p') + call mkdir(s:plugdir . '/after', 'p') + set rtp& + let rtp = &rtp + filetype on + + let rtp_entries = split(rtp, ',') + for entry in rtp_entries + if entry =~? '\<after\>' + let first_after_entry = entry + break + endif + endfor + + exe 'split ' . s:plugdir . '/plugin/test.vim' + call setline(1, 'let g:plugin_works = 42') + wq + + exe 'split ' . s:plugdir . '/plugin/also/loaded.vim' + call setline(1, 'let g:plugin_also_works = 77') + wq + + exe 'split ' . s:plugdir . '/ftdetect/test.vim' + call setline(1, 'let g:ftdetect_works = 17') + wq + + packadd mytest + + call assert_equal(42, g:plugin_works) + call assert_equal(77, g:plugin_also_works) + call assert_equal(17, g:ftdetect_works) + call assert_true(len(&rtp) > len(rtp)) + call assert_match('/testdir/Xdir/pack/mine/opt/mytest\($\|,\)', &rtp) + + let new_after = match(&rtp, '/testdir/Xdir/pack/mine/opt/mytest/after,') + let forwarded = substitute(first_after_entry, '\\', '[/\\\\]', 'g') + let old_after = match(&rtp, ',' . forwarded . '\>') + call assert_true(new_after > 0, 'rtp is ' . &rtp) + call assert_true(old_after > 0, 'match ' . forwarded . ' in ' . &rtp) + call assert_true(new_after < old_after, 'rtp is ' . &rtp) + + " NOTE: '/.../opt/myte' forwardly matches with '/.../opt/mytest' + call mkdir(fnamemodify(s:plugdir, ':h') . '/myte', 'p') + let rtp = &rtp + packadd myte + + " Check the path of 'myte' is added + call assert_true(len(&rtp) > len(rtp)) + call assert_match('/testdir/Xdir/pack/mine/opt/myte\($\|,\)', &rtp) + + " Check exception + call assert_fails("packadd directorynotfound", 'E919:') + call assert_fails("packadd", 'E471:') +endfunc + +func Test_packadd_start() + let plugdir = s:topdir . '/pack/mine/start/other' + call mkdir(plugdir . '/plugin', 'p') + set rtp& + let rtp = &rtp + filetype on + + exe 'split ' . plugdir . '/plugin/test.vim' + call setline(1, 'let g:plugin_works = 24') + wq + + packadd other + + call assert_equal(24, g:plugin_works) + call assert_true(len(&rtp) > len(rtp)) + call assert_match('/testdir/Xdir/pack/mine/start/other\($\|,\)', &rtp) +endfunc + +func Test_packadd_noload() + call mkdir(s:plugdir . '/plugin', 'p') + call mkdir(s:plugdir . '/syntax', 'p') + set rtp& + let rtp = &rtp + + exe 'split ' . s:plugdir . '/plugin/test.vim' + call setline(1, 'let g:plugin_works = 42') + wq + let g:plugin_works = 0 + + packadd! mytest + + call assert_true(len(&rtp) > len(rtp)) + call assert_match('testdir/Xdir/pack/mine/opt/mytest\($\|,\)', &rtp) + call assert_equal(0, g:plugin_works) + + " check the path is not added twice + let new_rtp = &rtp + packadd! mytest + call assert_equal(new_rtp, &rtp) +endfunc + +func Test_packadd_symlink_dir() + if !has('unix') + return + endif + let top2_dir = s:topdir . '/Xdir2' + let real_dir = s:topdir . '/Xsym' + call mkdir(real_dir, 'p') + exec "silent !ln -s Xsym" top2_dir + let &rtp = top2_dir . ',' . top2_dir . '/after' + let &packpath = &rtp + + let s:plugdir = top2_dir . '/pack/mine/opt/mytest' + call mkdir(s:plugdir . '/plugin', 'p') + + exe 'split ' . s:plugdir . '/plugin/test.vim' + call setline(1, 'let g:plugin_works = 44') + wq + let g:plugin_works = 0 + + packadd mytest + + " Must have been inserted in the middle, not at the end + call assert_match('/pack/mine/opt/mytest,', &rtp) + call assert_equal(44, g:plugin_works) + + " No change when doing it again. + let rtp_before = &rtp + packadd mytest + call assert_equal(rtp_before, &rtp) + + set rtp& + let rtp = &rtp + exec "silent !rm" top2_dir +endfunc + +func Test_packadd_symlink_dir2() + if !has('unix') + return + endif + let top2_dir = s:topdir . '/Xdir2' + let real_dir = s:topdir . '/Xsym/pack' + call mkdir(top2_dir, 'p') + call mkdir(real_dir, 'p') + let &rtp = top2_dir . ',' . top2_dir . '/after' + let &packpath = &rtp + + exec "silent !ln -s ../Xsym/pack" top2_dir . '/pack' + let s:plugdir = top2_dir . '/pack/mine/opt/mytest' + call mkdir(s:plugdir . '/plugin', 'p') + + exe 'split ' . s:plugdir . '/plugin/test.vim' + call setline(1, 'let g:plugin_works = 48') + wq + let g:plugin_works = 0 + + packadd mytest + + " Must have been inserted in the middle, not at the end + call assert_match('/Xdir2/pack/mine/opt/mytest,', &rtp) + call assert_equal(48, g:plugin_works) + + " No change when doing it again. + let rtp_before = &rtp + packadd mytest + call assert_equal(rtp_before, &rtp) + + set rtp& + let rtp = &rtp + exec "silent !rm" top2_dir . '/pack' + exec "silent !rmdir" top2_dir +endfunc + +" Check command-line completion for 'packadd' +func Test_packadd_completion() + let optdir1 = &packpath . '/pack/mine/opt' + let optdir2 = &packpath . '/pack/candidate/opt' + + call mkdir(optdir1 . '/pluginA', 'p') + call mkdir(optdir1 . '/pluginC', 'p') + call mkdir(optdir2 . '/pluginB', 'p') + call mkdir(optdir2 . '/pluginC', 'p') + + let li = [] + call feedkeys(":packadd \<Tab>')\<C-B>call add(li, '\<CR>", 't') + call feedkeys(":packadd " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't') + call feedkeys(":packadd " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't') + call feedkeys(":packadd " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx') + call assert_equal("packadd pluginA", li[0]) + call assert_equal("packadd pluginB", li[1]) + call assert_equal("packadd pluginC", li[2]) + call assert_equal("packadd ", li[3]) +endfunc + +func Test_packloadall() + " plugin foo with an autoload directory + let fooplugindir = &packpath . '/pack/mine/start/foo/plugin' + call mkdir(fooplugindir, 'p') + call writefile(['let g:plugin_foo_number = 1234', + \ 'let g:plugin_foo_auto = bbb#value', + \ 'let g:plugin_extra_auto = extra#value'], fooplugindir . '/bar.vim') + let fooautodir = &packpath . '/pack/mine/start/foo/autoload' + call mkdir(fooautodir, 'p') + call writefile(['let bar#value = 77'], fooautodir . '/bar.vim') + + " plugin aaa with an autoload directory + let aaaplugindir = &packpath . '/pack/mine/start/aaa/plugin' + call mkdir(aaaplugindir, 'p') + call writefile(['let g:plugin_aaa_number = 333', + \ 'let g:plugin_aaa_auto = bar#value'], aaaplugindir . '/bbb.vim') + let aaaautodir = &packpath . '/pack/mine/start/aaa/autoload' + call mkdir(aaaautodir, 'p') + call writefile(['let bbb#value = 55'], aaaautodir . '/bbb.vim') + + " plugin extra with only an autoload directory + let extraautodir = &packpath . '/pack/mine/start/extra/autoload' + call mkdir(extraautodir, 'p') + call writefile(['let extra#value = 99'], extraautodir . '/extra.vim') + + packloadall + call assert_equal(1234, g:plugin_foo_number) + call assert_equal(55, g:plugin_foo_auto) + call assert_equal(99, g:plugin_extra_auto) + call assert_equal(333, g:plugin_aaa_number) + call assert_equal(77, g:plugin_aaa_auto) + + " only works once + call writefile(['let g:plugin_bar_number = 4321'], fooplugindir . '/bar2.vim') + packloadall + call assert_false(exists('g:plugin_bar_number')) + + " works when ! used + packloadall! + call assert_equal(4321, g:plugin_bar_number) +endfunc + +func Test_helptags() + let docdir1 = &packpath . '/pack/mine/start/foo/doc' + let docdir2 = &packpath . '/pack/mine/start/bar/doc' + call mkdir(docdir1, 'p') + call mkdir(docdir2, 'p') + call writefile(['look here: *look-here*'], docdir1 . '/bar.txt') + call writefile(['look away: *look-away*'], docdir2 . '/foo.txt') + exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar' + + helptags ALL + + let tags1 = readfile(docdir1 . '/tags') + call assert_match('look-here', tags1[0]) + let tags2 = readfile(docdir2 . '/tags') + call assert_match('look-away', tags2[0]) + + call assert_fails('helptags abcxyz', 'E150:') +endfunc + +func Test_colorscheme() + let colordirrun = &packpath . '/runtime/colors' + let colordirstart = &packpath . '/pack/mine/start/foo/colors' + let colordiropt = &packpath . '/pack/mine/opt/bar/colors' + call mkdir(colordirrun, 'p') + call mkdir(colordirstart, 'p') + call mkdir(colordiropt, 'p') + call writefile(['let g:found_one = 1'], colordirrun . '/one.vim') + call writefile(['let g:found_two = 1'], colordirstart . '/two.vim') + call writefile(['let g:found_three = 1'], colordiropt . '/three.vim') + exe 'set rtp=' . &packpath . '/runtime' + + colorscheme one + call assert_equal(1, g:found_one) + colorscheme two + call assert_equal(1, g:found_two) + colorscheme three + call assert_equal(1, g:found_three) +endfunc + +func Test_colorscheme_completion() + let colordirrun = &packpath . '/runtime/colors' + let colordirstart = &packpath . '/pack/mine/start/foo/colors' + let colordiropt = &packpath . '/pack/mine/opt/bar/colors' + call mkdir(colordirrun, 'p') + call mkdir(colordirstart, 'p') + call mkdir(colordiropt, 'p') + call writefile(['let g:found_one = 1'], colordirrun . '/one.vim') + call writefile(['let g:found_two = 1'], colordirstart . '/two.vim') + call writefile(['let g:found_three = 1'], colordiropt . '/three.vim') + exe 'set rtp=' . &packpath . '/runtime' + + let li=[] + call feedkeys(":colorscheme " . repeat("\<Tab>", 1) . "')\<C-B>call add(li, '\<CR>", 't') + call feedkeys(":colorscheme " . repeat("\<Tab>", 2) . "')\<C-B>call add(li, '\<CR>", 't') + call feedkeys(":colorscheme " . repeat("\<Tab>", 3) . "')\<C-B>call add(li, '\<CR>", 't') + call feedkeys(":colorscheme " . repeat("\<Tab>", 4) . "')\<C-B>call add(li, '\<CR>", 'tx') + call assert_equal("colorscheme one", li[0]) + call assert_equal("colorscheme three", li[1]) + call assert_equal("colorscheme two", li[2]) + call assert_equal("colorscheme ", li[3]) +endfunc + +func Test_runtime() + let rundir = &packpath . '/runtime/extra' + let startdir = &packpath . '/pack/mine/start/foo/extra' + let optdir = &packpath . '/pack/mine/opt/bar/extra' + call mkdir(rundir, 'p') + call mkdir(startdir, 'p') + call mkdir(optdir, 'p') + call writefile(['let g:sequence .= "run"'], rundir . '/bar.vim') + call writefile(['let g:sequence .= "start"'], startdir . '/bar.vim') + call writefile(['let g:sequence .= "foostart"'], startdir . '/foo.vim') + call writefile(['let g:sequence .= "opt"'], optdir . '/bar.vim') + call writefile(['let g:sequence .= "xxxopt"'], optdir . '/xxx.vim') + exe 'set rtp=' . &packpath . '/runtime' + + let g:sequence = '' + runtime extra/bar.vim + call assert_equal('run', g:sequence) + let g:sequence = '' + runtime START extra/bar.vim + call assert_equal('start', g:sequence) + let g:sequence = '' + runtime OPT extra/bar.vim + call assert_equal('opt', g:sequence) + let g:sequence = '' + runtime PACK extra/bar.vim + call assert_equal('start', g:sequence) + let g:sequence = '' + runtime! PACK extra/bar.vim + call assert_equal('startopt', g:sequence) + let g:sequence = '' + runtime PACK extra/xxx.vim + call assert_equal('xxxopt', g:sequence) + + let g:sequence = '' + runtime ALL extra/bar.vim + call assert_equal('run', g:sequence) + let g:sequence = '' + runtime ALL extra/foo.vim + call assert_equal('foostart', g:sequence) + let g:sequence = '' + runtime! ALL extra/xxx.vim + call assert_equal('xxxopt', g:sequence) + let g:sequence = '' + runtime! ALL extra/bar.vim + call assert_equal('runstartopt', g:sequence) +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_put.vim b/src/nvim/testdir/test_put.vim index 440717eaa8..ed76709a56 100644 --- a/src/nvim/testdir/test_put.vim +++ b/src/nvim/testdir/test_put.vim @@ -1,5 +1,7 @@ " Tests for put commands, e.g. ":put", "p", "gp", "P", "gP", etc. +source check.vim + func Test_put_block() new call feedkeys("i\<C-V>u2500\<CR>x\<ESC>", 'x') @@ -112,6 +114,39 @@ func Test_put_p_indent_visual() bwipe! endfunc +func Test_gp_with_count_leaves_cursor_at_end() + new + call setline(1, '<---->') + call setreg('@', "foo\nbar", 'c') + normal 1G3|3gp + call assert_equal([0, 4, 4, 0], getpos(".")) + call assert_equal(['<--foo', 'barfoo', 'barfoo', 'bar-->'], getline(1, '$')) + call assert_equal([0, 4, 3, 0], getpos("']")) + + bwipe! +endfunc + +func Test_p_with_count_leaves_mark_at_end() + new + call setline(1, '<---->') + call setreg('@', "start\nend", 'c') + normal 1G3|3p + call assert_equal([0, 1, 4, 0], getpos(".")) + call assert_equal(['<--start', 'endstart', 'endstart', 'end-->'], getline(1, '$')) + call assert_equal([0, 4, 3, 0], getpos("']")) + + bwipe! +endfunc + +func Test_put_above_first_line() + new + let @" = 'text' + silent! normal 0o00 + 0put + call assert_equal('text', getline(1)) + bwipe! +endfunc + func Test_multibyte_op_end_mark() new call setline(1, 'тест') diff --git a/src/nvim/testdir/test_python3.vim b/src/nvim/testdir/test_python3.vim index 54da3d2eba..f6a1942e24 100644 --- a/src/nvim/testdir/test_python3.vim +++ b/src/nvim/testdir/test_python3.vim @@ -69,6 +69,7 @@ func Test_vim_function() endfunc func Test_skipped_python3_command_does_not_affect_pyxversion() + throw 'skipped: Nvim hardcodes pyxversion=3' set pyxversion=0 if 0 python3 import vim diff --git a/src/nvim/testdir/test_pyx2.vim b/src/nvim/testdir/test_pyx2.vim index b6ed80f842..6a8ebf3da0 100644 --- a/src/nvim/testdir/test_pyx2.vim +++ b/src/nvim/testdir/test_pyx2.vim @@ -1,9 +1,9 @@ " Test for pyx* commands and functions with Python 2. -set pyx=2 if !has('python') finish endif +set pyx=2 let s:py2pattern = '^2\.[0-7]\.\d\+' let s:py3pattern = '^3\.\d\+\.\d\+' diff --git a/src/nvim/testdir/test_quickfix.vim b/src/nvim/testdir/test_quickfix.vim index 6db679c5f9..c4d70fb1de 100644 --- a/src/nvim/testdir/test_quickfix.vim +++ b/src/nvim/testdir/test_quickfix.vim @@ -32,7 +32,7 @@ func s:setup_commands(cchar) command! -count -nargs=* -bang Xnfile <mods><count>cnfile<bang> <args> command! -nargs=* -bang Xpfile <mods>cpfile<bang> <args> command! -nargs=* Xexpr <mods>cexpr <args> - command! -count -nargs=* Xvimgrep <mods> <count>vimgrep <args> + command! -count=999 -nargs=* Xvimgrep <mods> <count>vimgrep <args> command! -nargs=* Xvimgrepadd <mods> vimgrepadd <args> command! -nargs=* Xgrep <mods> grep <args> command! -nargs=* Xgrepadd <mods> grepadd <args> @@ -69,7 +69,7 @@ func s:setup_commands(cchar) command! -count -nargs=* -bang Xnfile <mods><count>lnfile<bang> <args> command! -nargs=* -bang Xpfile <mods>lpfile<bang> <args> command! -nargs=* Xexpr <mods>lexpr <args> - command! -count -nargs=* Xvimgrep <mods> <count>lvimgrep <args> + command! -count=999 -nargs=* Xvimgrep <mods> <count>lvimgrep <args> command! -nargs=* Xvimgrepadd <mods> lvimgrepadd <args> command! -nargs=* Xgrep <mods> lgrep <args> command! -nargs=* Xgrepadd <mods> lgrepadd <args> @@ -1384,6 +1384,29 @@ func Test_efm_error_type() let &efm = save_efm endfunc +" Test for end_lnum ('%e') and end_col ('%k') fields in 'efm' +func Test_efm_end_lnum_col() + let save_efm = &efm + + " single line + set efm=%f:%l-%e:%c-%k:%t:%m + cexpr ["Xfile1:10-20:1-2:E:msg1", "Xfile1:20-30:2-3:W:msg2",] + let output = split(execute('clist'), "\n") + call assert_equal([ + \ ' 1 Xfile1:10-20 col 1-2 error: msg1', + \ ' 2 Xfile1:20-30 col 2-3 warning: msg2'], output) + + " multiple lines + set efm=%A%n)%m,%Z%f:%l-%e:%c-%k + cexpr ["1)msg1", "Xfile1:14-24:1-2", + \ "2)msg2", "Xfile1:24-34:3-4"] + let output = split(execute('clist'), "\n") + call assert_equal([ + \ ' 1 Xfile1:14-24 col 1-2 error 1: msg1', + \ ' 2 Xfile1:24-34 col 3-4 error 2: msg2'], output) + let &efm = save_efm +endfunc + func XquickfixChangedByAutocmd(cchar) call s:setup_commands(a:cchar) if a:cchar == 'c' @@ -1914,6 +1937,7 @@ func Test_switchbuf() " If opening a file changes 'switchbuf', then the new value should be " retained. + set modeline&vim call writefile(["vim: switchbuf=split"], 'Xqftestfile1') enew | only set switchbuf&vim @@ -5027,6 +5051,52 @@ func Test_qfbuf_update() call Xqfbuf_update('l') endfunc +" Test for the :vimgrep 'f' flag (fuzzy match) +func Xvimgrep_fuzzy_match(cchar) + call s:setup_commands(a:cchar) + + Xvimgrep /three one/f Xfile* + let l = g:Xgetlist() + call assert_equal(2, len(l)) + call assert_equal(['Xfile1', 1, 9, 'one two three'], + \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text]) + call assert_equal(['Xfile2', 2, 1, 'three one two'], + \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text]) + + Xvimgrep /the/f Xfile* + let l = g:Xgetlist() + call assert_equal(3, len(l)) + call assert_equal(['Xfile1', 1, 9, 'one two three'], + \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text]) + call assert_equal(['Xfile2', 2, 1, 'three one two'], + \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text]) + call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'], + \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text]) + + Xvimgrep /aaa/fg Xfile* + let l = g:Xgetlist() + call assert_equal(4, len(l)) + call assert_equal(['Xfile1', 2, 1, 'aaaaaa'], + \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text]) + call assert_equal(['Xfile1', 2, 4, 'aaaaaa'], + \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text]) + call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'], + \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text]) + call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'], + \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text]) + + call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:') +endfunc + +func Test_vimgrep_fuzzy_match() + call writefile(['one two three', 'aaaaaa'], 'Xfile1') + call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2') + call Xvimgrep_fuzzy_match('c') + call Xvimgrep_fuzzy_match('l') + call delete('Xfile1') + call delete('Xfile2') +endfunc + " Test for getting a specific item from a quickfix list func Xtest_getqflist_by_idx(cchar) call s:setup_commands(a:cchar) diff --git a/src/nvim/testdir/test_random.vim b/src/nvim/testdir/test_random.vim new file mode 100644 index 0000000000..6d3f7dcfd9 --- /dev/null +++ b/src/nvim/testdir/test_random.vim @@ -0,0 +1,51 @@ +" Tests for srand() and rand() + +func Test_Rand() + let r = srand(123456789) + call assert_equal([1573771921, 319883699, 2742014374, 1324369493], r) + call assert_equal(4284103975, rand(r)) + call assert_equal(1001954530, rand(r)) + call assert_equal(2701803082, rand(r)) + call assert_equal(2658065534, rand(r)) + call assert_equal(3104308804, rand(r)) + + " Nvim does not support test_settime + " call test_settime(12341234) + let s = srand() + if !has('win32') && filereadable('/dev/urandom') + " using /dev/urandom + call assert_notequal(s, srand()) + " else + " " using time() + " call assert_equal(s, srand()) + " call test_settime(12341235) + " call assert_notequal(s, srand()) + endif + + " Nvim does not support test_srand_seed + " call test_srand_seed(123456789) + " call assert_equal(4284103975, rand()) + " call assert_equal(1001954530, rand()) + " call test_srand_seed() + + if has('float') + call assert_fails('echo srand(1.2)', 'E805:') + endif + call assert_fails('echo srand([1])', 'E745:') + call assert_fails('echo rand("burp")', 'E475:') + call assert_fails('echo rand([1, 2, 3])', 'E475:') + call assert_fails('echo rand([[1], 2, 3, 4])', 'E475:') + call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') + call assert_fails('echo rand([1, 2, [3], 4])', 'E475:') + call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:') + + " call test_settime(0) +endfunc + +func Test_issue_5587() + call rand() + call garbagecollect() + call rand() +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_regexp_latin.vim b/src/nvim/testdir/test_regexp_latin.vim index 712f1e6025..a92f7e1192 100644 --- a/src/nvim/testdir/test_regexp_latin.vim +++ b/src/nvim/testdir/test_regexp_latin.vim @@ -787,4 +787,12 @@ func Test_regexp_error() set re& endfunc +func Test_using_mark_position() + " this was using freed memory + new + norm O0 + call assert_fails("s/\\%')", 'E486:') + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_regexp_utf8.vim b/src/nvim/testdir/test_regexp_utf8.vim index d8d5797dcf..b640a6d043 100644 --- a/src/nvim/testdir/test_regexp_utf8.vim +++ b/src/nvim/testdir/test_regexp_utf8.vim @@ -152,9 +152,6 @@ func s:classes_test() if has('win32') let identchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz
¡¢£¤¥¦§µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' let kwordchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' - elseif has('ebcdic') - let identchars_ok = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz¬®µº¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' - let kwordchars_ok = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz¬®µº¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' else let identchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' let kwordchars_ok = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzµÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' @@ -166,8 +163,6 @@ func s:classes_test() let fnamechars_ok = '$+,-./0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' elseif has('vms') let fnamechars_ok = '#$%+,-./0123456789:;<>ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' - elseif has('ebcdic') - let fnamechars_ok = '#$%+,-./=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' else let fnamechars_ok = '#$%+,-./0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ' endif @@ -590,4 +585,12 @@ func Test_match_char_class_upper() bwipe! endfunc +func Test_match_invalid_byte() + call writefile(0z630a.765d30aa0a.2e0a.790a.4030, 'Xinvalid') + new + source Xinvalid + bwipe! + call delete('Xinvalid') +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_registers.vim b/src/nvim/testdir/test_registers.vim index 84a5aca3d5..2e92d9aa2f 100644 --- a/src/nvim/testdir/test_registers.vim +++ b/src/nvim/testdir/test_registers.vim @@ -1,6 +1,4 @@ -" " Tests for register operations -" source check.vim source view_util.vim @@ -13,6 +11,8 @@ func Test_aaa_empty_reg_test() call assert_fails('normal @!', 'E354:') call assert_fails('normal @:', 'E30:') call assert_fails('normal @.', 'E29:') + call assert_fails('put /', 'E35:') + call assert_fails('put .', 'E29:') endfunc func Test_yank_shows_register() @@ -119,6 +119,17 @@ func Test_recording_esc_sequence() endif endfunc +func Test_recording_with_select_mode() + new + call feedkeys("qacc12345\<Esc>gH98765\<Esc>q", "tx") + call assert_equal("98765", getline(1)) + call assert_equal("cc12345\<Esc>gH98765\<Esc>", @a) + call setline(1, 'asdf') + normal! @a + call assert_equal("98765", getline(1)) + bwipe! +endfunc + " Test for executing the last used register (@) func Test_last_used_exec_reg() " Test for the @: command @@ -141,6 +152,14 @@ func Test_last_used_exec_reg() normal @@ call assert_equal('EditEdit', a) + " Test for repeating the last command-line in visual mode + call append(0, 'register') + normal gg + let @r = '' + call feedkeys("v:yank R\<CR>", 'xt') + call feedkeys("v@:", 'xt') + call assert_equal("\nregister\nregister\n", @r) + enew! endfunc @@ -164,6 +183,19 @@ func Test_get_register() call assert_equal('', getregtype('!')) + " Test for inserting an invalid register content + call assert_beeps('exe "normal i\<C-R>!"') + + " Test for inserting a register with multiple lines + call deletebufline('', 1, '$') + call setreg('r', ['a', 'b']) + exe "normal i\<C-R>r" + call assert_equal(['a', 'b', ''], getline(1, '$')) + + " Test for inserting a multi-line register in the command line + call feedkeys(":\<C-R>r\<Esc>", 'xt') + call assert_equal("a\rb", histget(':', -1)) " Modified because of #6137 + enew! endfunc @@ -187,9 +219,200 @@ func Test_set_register() call setreg('=', 'b', 'a') call assert_equal('regwrite', getreg('=')) + " Test for settting a list of lines to special registers + call setreg('/', []) + call assert_equal('', @/) + call setreg('=', []) + call assert_equal('', @=) + call assert_fails("call setreg('/', ['a', 'b'])", 'E883:') + call assert_fails("call setreg('=', ['a', 'b'])", 'E883:') + call assert_equal(0, setreg('_', ['a', 'b'])) + + " Test for recording to a invalid register + call assert_beeps('normal q$') + + " Appending to a register when recording + call append(0, "text for clipboard test") + normal gg + call feedkeys('qrllq', 'xt') + call feedkeys('qRhhq', 'xt') + call assert_equal('llhh', getreg('r')) + + " Appending a list of characters to a register from different lines + let @r = '' + call append(0, ['abcdef', '123456']) + normal gg"ry3l + call cursor(2, 4) + normal "Ry3l + call assert_equal('abc456', @r) + + " Test for gP with multiple lines selected using characterwise motion + %delete + call append(0, ['vim editor', 'vim editor']) + let @r = '' + exe "normal ggwy/vim /e\<CR>gP" + call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3)) + + " Test for gP with . register + %delete + normal iabc + normal ".gp + call assert_equal('abcabc', getline(1)) + normal 0".gP + call assert_equal('abcabcabc', getline(1)) + enew! endfunc +" Test for clipboard registers (* and +) +func Test_clipboard_regs() + throw 'skipped: needs clipboard=autoselect,autoselectplus' + + CheckNotGui + CheckFeature clipboard_working + + new + call append(0, "text for clipboard test") + normal gg"*yiw + call assert_equal('text', getreg('*')) + normal gg2w"+yiw + call assert_equal('clipboard', getreg('+')) + + " Test for replacing the clipboard register contents + set clipboard=unnamed + let @* = 'food' + normal ggviw"*p + call assert_equal('text', getreg('*')) + call assert_equal('food for clipboard test', getline(1)) + normal ggviw"*p + call assert_equal('food', getreg('*')) + call assert_equal('text for clipboard test', getline(1)) + + " Test for replacing the selection register contents + set clipboard=unnamedplus + let @+ = 'food' + normal ggviw"+p + call assert_equal('text', getreg('+')) + call assert_equal('food for clipboard test', getline(1)) + normal ggviw"+p + call assert_equal('food', getreg('+')) + call assert_equal('text for clipboard test', getline(1)) + + " Test for auto copying visually selected text to clipboard register + call setline(1, "text for clipboard test") + let @* = '' + set clipboard=autoselect + normal ggwwviwy + call assert_equal('clipboard', @*) + + " Test for auto copying visually selected text to selection register + let @+ = '' + set clipboard=autoselectplus + normal ggwviwy + call assert_equal('for', @+) + + set clipboard&vim + bwipe! +endfunc + +" Test for restarting the current mode (insert or virtual replace) after +" executing the contents of a register +func Test_put_reg_restart_mode() + new + call append(0, 'editor') + normal gg + let @r = "ivim \<Esc>" + call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt') + call assert_equal('vimi editor', getline(1)) + + call setline(1, 'editor') + normal gg + call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt') + call assert_equal('vimReditor', getline(1)) + + bwipe! +endfunc + +" Test for getting register info +func Test_get_reginfo() + enew + call setline(1, ['foo', 'bar']) + + exe 'norm! "zyy' + let info = getreginfo('"') + call assert_equal('z', info.points_to) + call setreg('y', 'baz') + call assert_equal('z', getreginfo('').points_to) + call setreg('y', { 'isunnamed': v:true }) + call assert_equal('y', getreginfo('"').points_to) + + exe '$put' + call assert_equal(getreg('y'), getline(3)) + call setreg('', 'qux') + call assert_equal('0', getreginfo('').points_to) + call setreg('x', 'quux') + call assert_equal('0', getreginfo('').points_to) + + let info = getreginfo('') + call assert_equal(getreg('', 1, 1), info.regcontents) + call assert_equal(getregtype(''), info.regtype) + + exe "norm! 0\<c-v>e" .. '"zy' + let info = getreginfo('z') + call assert_equal(getreg('z', 1, 1), info.regcontents) + call assert_equal(getregtype('z'), info.regtype) + call assert_equal(1, +info.isunnamed) + + let info = getreginfo('"') + call assert_equal('z', info.points_to) + + bwipe! +endfunc + +" Test for restoring register with dict from getreginfo +func Test_set_register_dict() + enew! + + call setreg('"', #{ regcontents: ['one', 'two'], + \ regtype: 'V', points_to: 'z' }) + call assert_equal(['one', 'two'], getreg('"', 1, 1)) + let info = getreginfo('"') + call assert_equal('z', info.points_to) + call assert_equal('V', info.regtype) + call assert_equal(1, +getreginfo('z').isunnamed) + + call setreg('x', #{ regcontents: ['three', 'four'], + \ regtype: 'v', isunnamed: v:true }) + call assert_equal(['three', 'four'], getreg('"', 1, 1)) + let info = getreginfo('"') + call assert_equal('x', info.points_to) + call assert_equal('v', info.regtype) + call assert_equal(1, +getreginfo('x').isunnamed) + + call setreg('y', #{ regcontents: 'five', + \ regtype: "\<c-v>", isunnamed: v:false }) + call assert_equal("\<c-v>4", getreginfo('y').regtype) + call assert_equal(0, +getreginfo('y').isunnamed) + call assert_equal(['three', 'four'], getreg('"', 1, 1)) + call assert_equal('x', getreginfo('"').points_to) + + call setreg('"', #{ regcontents: 'six' }) + call assert_equal('0', getreginfo('"').points_to) + call assert_equal(1, +getreginfo('0').isunnamed) + call assert_equal(['six'], getreginfo('0').regcontents) + call assert_equal(['six'], getreginfo('"').regcontents) + + let @x = 'one' + call setreg('x', {}) + call assert_equal(1, len(split(execute('reg x'), '\n'))) + + call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:') + call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:') + call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:') + + bwipe! +endfunc + func Test_v_register() enew call setline(1, 'nothing') @@ -288,84 +511,25 @@ func Test_insert_small_delete() bwipe! endfunc -" Test for getting register info -func Test_get_reginfo() - enew - call setline(1, ['foo', 'bar']) - - exe 'norm! "zyy' - let info = getreginfo('"') - call assert_equal('z', info.points_to) - call setreg('y', 'baz') - call assert_equal('z', getreginfo('').points_to) - call setreg('y', { 'isunnamed': v:true }) - call assert_equal('y', getreginfo('"').points_to) - - exe '$put' - call assert_equal(getreg('y'), getline(3)) - call setreg('', 'qux') - call assert_equal('0', getreginfo('').points_to) - call setreg('x', 'quux') - call assert_equal('0', getreginfo('').points_to) - - let info = getreginfo('') - call assert_equal(getreg('', 1, 1), info.regcontents) - call assert_equal(getregtype(''), info.regtype) - - exe "norm! 0\<c-v>e" .. '"zy' - let info = getreginfo('z') - call assert_equal(getreg('z', 1, 1), info.regcontents) - call assert_equal(getregtype('z'), info.regtype) - call assert_equal(1, +info.isunnamed) - - let info = getreginfo('"') - call assert_equal('z', info.points_to) +func Test_record_in_select_mode() + new + call setline(1, 'text') + sil norm q00 + sil norm q + call assert_equal('0ext', getline(1)) + + %delete + let @r = '' + call setline(1, ['abc', 'abc', 'abc']) + smap <F2> <Right><Right>, + call feedkeys("qrgh\<F2>Dk\<Esc>q", 'xt') + call assert_equal("gh\<F2>Dk\<Esc>", @r) + norm j0@rj0@@ + call assert_equal([',Dk', ',Dk', ',Dk'], getline(1, 3)) + sunmap <F2> bwipe! endfunc -" Test for restoring register with dict from getreginfo -func Test_set_register_dict() - enew! - - call setreg('"', #{ regcontents: ['one', 'two'], - \ regtype: 'V', points_to: 'z' }) - call assert_equal(['one', 'two'], getreg('"', 1, 1)) - let info = getreginfo('"') - call assert_equal('z', info.points_to) - call assert_equal('V', info.regtype) - call assert_equal(1, +getreginfo('z').isunnamed) - - call setreg('x', #{ regcontents: ['three', 'four'], - \ regtype: 'v', isunnamed: v:true }) - call assert_equal(['three', 'four'], getreg('"', 1, 1)) - let info = getreginfo('"') - call assert_equal('x', info.points_to) - call assert_equal('v', info.regtype) - call assert_equal(1, +getreginfo('x').isunnamed) - - call setreg('y', #{ regcontents: 'five', - \ regtype: "\<c-v>", isunnamed: v:false }) - call assert_equal("\<c-v>4", getreginfo('y').regtype) - call assert_equal(0, +getreginfo('y').isunnamed) - call assert_equal(['three', 'four'], getreg('"', 1, 1)) - call assert_equal('x', getreginfo('"').points_to) - - call setreg('"', #{ regcontents: 'six' }) - call assert_equal('0', getreginfo('"').points_to) - call assert_equal(1, +getreginfo('0').isunnamed) - call assert_equal(['six'], getreginfo('0').regcontents) - call assert_equal(['six'], getreginfo('"').regcontents) - - let @x = 'one' - call setreg('x', {}) - call assert_equal(1, len(split(execute('reg x'), '\n'))) - - call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:') - call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:') - call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:') - - bwipe! -endfunc " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_rename.vim b/src/nvim/testdir/test_rename.vim index 3887fcfabf..5359b84923 100644 --- a/src/nvim/testdir/test_rename.vim +++ b/src/nvim/testdir/test_rename.vim @@ -1,5 +1,7 @@ " Test rename() +source shared.vim + func Test_rename_file_to_file() call writefile(['foo'], 'Xrename1') @@ -81,7 +83,7 @@ func Test_rename_copy() call assert_equal(0, rename('Xrenamedir/Xrenamefile', 'Xrenamefile')) - if !has('win32') + if !has('win32') && !IsRoot() " On Windows, the source file is removed despite " its directory being made not writable. call assert_equal(['foo'], readfile('Xrenamedir/Xrenamefile')) diff --git a/src/nvim/testdir/test_retab.vim b/src/nvim/testdir/test_retab.vim index f11a32bade..1650a03876 100644 --- a/src/nvim/testdir/test_retab.vim +++ b/src/nvim/testdir/test_retab.vim @@ -69,9 +69,33 @@ func Test_retab() call assert_equal(" a b c ", Retab('!', 3)) call assert_equal(" a b c ", Retab('', 5)) call assert_equal(" a b c ", Retab('!', 5)) + + set tabstop& expandtab& endfunc func Test_retab_error() call assert_fails('retab -1', 'E487:') call assert_fails('retab! -1', 'E487:') + call assert_fails('ret -1000', 'E487:') + call assert_fails('ret 10000', 'E475:') + call assert_fails('ret 80000000000000000000', 'E475:') endfunc + +func Test_retab_endless() + new + call setline(1, "\t0\t") + let caught = 'no' + try + while 1 + set ts=4000 + retab 4 + endwhile + catch /E1240/ + let caught = 'yes' + endtry + bwipe! + set tabstop& +endfunc + + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_scriptnames.vim b/src/nvim/testdir/test_scriptnames.vim index fc6c910bfa..44ec146666 100644 --- a/src/nvim/testdir/test_scriptnames.vim +++ b/src/nvim/testdir/test_scriptnames.vim @@ -23,4 +23,10 @@ func Test_scriptnames() bwipe call delete('Xscripting') + + let msgs = execute('messages') + scriptnames + call assert_equal(msgs, execute('messages')) endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_search.vim b/src/nvim/testdir/test_search.vim index c796f1f676..a3d5ca96a1 100644 --- a/src/nvim/testdir/test_search.vim +++ b/src/nvim/testdir/test_search.vim @@ -292,15 +292,84 @@ endfunc func Test_searchpair() new - call setline(1, ['other code here', '', '[', '" cursor here', ']']) + call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]']) + + 4 + call assert_equal(3, searchpair('\[', '', ']', 'bW')) + call assert_equal([0, 3, 2, 0], getpos('.')) + 4 + call assert_equal(2, searchpair('\[', '', ']', 'bWr')) + call assert_equal([0, 2, 6, 0], getpos('.')) + 4 + call assert_equal(1, searchpair('\[', '', ']', 'bWm')) + call assert_equal([0, 3, 2, 0], getpos('.')) + 4|norm ^ + call assert_equal(5, searchpair('\[', '', ']', 'Wn')) + call assert_equal([0, 4, 2, 0], getpos('.')) + 4 + call assert_equal(2, searchpair('\[', '', ']', 'bW', + \ 'getline(".") =~ "^\\s*\["')) + call assert_equal([0, 2, 6, 0], getpos('.')) + set nomagic + 4 + call assert_equal(3, searchpair('\[', '', ']', 'bW')) + call assert_equal([0, 3, 2, 0], getpos('.')) + set magic + 4|norm ^ + call assert_equal(0, searchpair('{', '', '}', 'bW')) + call assert_equal([0, 4, 2, 0], getpos('.')) + + %d + call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1']) + + /\<if 1 + call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W')) + call assert_equal([0, 5, 1, 0], getpos('.')) + /\<if 2 + call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W')) + call assert_equal([0, 3, 3, 0], getpos('.')) + + q! +endfunc + +func Test_searchpairpos() + new + call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]']) + + 4 + call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW')) + call assert_equal([0, 3, 2, 0], getpos('.')) 4 - let a = searchpair('\[','',']','bW') - call assert_equal(3, a) + call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr')) + call assert_equal([0, 2, 6, 0], getpos('.')) + 4|norm ^ + call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn')) + call assert_equal([0, 4, 2, 0], getpos('.')) + 4 + call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW', + \ 'getline(".") =~ "^\\s*\["')) + call assert_equal([0, 2, 6, 0], getpos('.')) + 4 + call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr')) + call assert_equal([0, 2, 6, 0], getpos('.')) set nomagic 4 - let a = searchpair('\[','',']','bW') - call assert_equal(3, a) + call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW')) + call assert_equal([0, 3, 2, 0], getpos('.')) set magic + 4|norm ^ + call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW')) + call assert_equal([0, 4, 2, 0], getpos('.')) + + %d + call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1']) + /\<if 1 + call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W')) + call assert_equal([0, 5, 1, 0], getpos('.')) + /\<if 2 + call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W')) + call assert_equal([0, 3, 3, 0], getpos('.')) + q! endfunc @@ -309,17 +378,29 @@ func Test_searchpair_errors() call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String') call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String') call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags') - call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0') call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99') call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100') + call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e') + call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn') +endfunc + +func Test_searchpairpos_errors() + call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String') + call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String') + call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String') + call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags') + call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99') + call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100') + call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e') + call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn') endfunc func Test_searchpair_skip() func Zero() - return 0 + return 0 endfunc func Partial(x) - return a:x + return a:x endfunc new call setline(1, ['{', 'foo', 'foo', 'foo', '}']) @@ -1192,7 +1273,7 @@ endfunc " This was causing E874. Also causes an invalid read? func Test_look_behind() new - call setline(1, '0\|\&\n\@<=') + call setline(1, '0\|\&\n\@<=') call search(getline(".")) bwipe! endfunc @@ -1236,11 +1317,11 @@ endfunc func Test_search_Ctrl_L_combining() " Make sure, that Ctrl-L works correctly with combining characters. " It uses an artificial example of an 'a' with 4 combining chars: - " 'a' U+0061 Dec:97 LATIN SMALL LETTER A a /\%u61\Z "\u0061" + " 'a' U+0061 Dec:97 LATIN SMALL LETTER A a /\%u61\Z "\u0061" " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT ̀ /\%u300\Z "\u0300" " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT ́ /\%u301\Z "\u0301" " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE ̇ /\%u307\Z "\u0307" - " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW ̣ /\%u323 "\u0323" + " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW ̣ /\%u323 "\u0323" " Those should also appear on the commandline CheckOption incsearch diff --git a/src/nvim/testdir/test_search_stat.vim b/src/nvim/testdir/test_search_stat.vim index 335a51268d..d950626615 100644 --- a/src/nvim/testdir/test_search_stat.vim +++ b/src/nvim/testdir/test_search_stat.vim @@ -1,14 +1,15 @@ " Tests for search_stats, when "S" is not in 'shortmess' -source screendump.vim source check.vim +source screendump.vim func Test_search_stat() new set shortmess-=S " Append 50 lines with text to search for, "foobar" appears 20 times call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 10)) - call nvim_win_set_cursor(0, [1, 0]) + + call cursor(1, 1) " searchcount() returns an empty dictionary when previous pattern was not set call assert_equal({}, searchcount(#{pattern: ''})) @@ -45,7 +46,6 @@ func Test_search_stat() \ searchcount(#{pattern: 'fooooobar', maxcount: 1})) " match at second line - call cursor(1, 1) let messages_before = execute('messages') let @/ = 'fo*\(bar\?\)\?' let g:a = execute(':unsilent :norm! n') @@ -73,7 +73,6 @@ func Test_search_stat() let stat = '\[2/50\]' let g:a = execute(':unsilent :norm! n') call assert_notmatch(pat .. stat, g:a) - call writefile(getline(1, '$'), 'sample.txt') " n does not update search stat call assert_equal( \ #{current: 50, exact_match: 1, total: 50, incomplete: 0, maxcount: 99}, @@ -263,6 +262,34 @@ func Test_searchcount_fails() call assert_fails('echo searchcount("boo!")', 'E715:') endfunc +func Test_searchcount_in_statusline() + CheckScreendump + + let lines =<< trim END + set shortmess-=S + call append(0, 'this is something') + function TestSearchCount() abort + let search_count = searchcount() + if !empty(search_count) + return '[' . search_count.current . '/' . search_count.total . ']' + else + return '' + endif + endfunction + set hlsearch + set laststatus=2 statusline+=%{TestSearchCount()} + END + call writefile(lines, 'Xsearchstatusline') + let buf = RunVimInTerminal('-S Xsearchstatusline', #{rows: 10}) + call TermWait(buf) + call term_sendkeys(buf, "/something") + call VerifyScreenDump(buf, 'Test_searchstat_4', {}) + + call term_sendkeys(buf, "\<Esc>") + call StopVimInTerminal(buf) + call delete('Xsearchstatusline') +endfunc + func Test_search_stat_foldopen() CheckScreendump @@ -320,30 +347,29 @@ func! Test_search_stat_screendump() call delete('Xsearchstat') endfunc -func Test_searchcount_in_statusline() +func Test_search_stat_then_gd() CheckScreendump let lines =<< trim END + call setline(1, ['int cat;', 'int dog;', 'cat = dog;']) set shortmess-=S - call append(0, 'this is something') - function TestSearchCount() abort - let search_count = searchcount() - if !empty(search_count) - return '[' . search_count.current . '/' . search_count.total . ']' - else - return '' - endif - endfunction set hlsearch - set laststatus=2 statusline+=%{TestSearchCount()} END - call writefile(lines, 'Xsearchstatusline') - let buf = RunVimInTerminal('-S Xsearchstatusline', #{rows: 10}) + call writefile(lines, 'Xsearchstatgd') + + let buf = RunVimInTerminal('-S Xsearchstatgd', #{rows: 10}) + call term_sendkeys(buf, "/dog\<CR>") call TermWait(buf) - call term_sendkeys(buf, "/something") - call VerifyScreenDump(buf, 'Test_searchstat_4', {}) + call VerifyScreenDump(buf, 'Test_searchstatgd_1', {}) + + call term_sendkeys(buf, "G0gD") + call TermWait(buf) + call VerifyScreenDump(buf, 'Test_searchstatgd_2', {}) - call term_sendkeys(buf, "\<Esc>") call StopVimInTerminal(buf) - call delete('Xsearchstatusline') + call delete('Xsearchstatgd') endfunc + + + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_selectmode.vim b/src/nvim/testdir/test_selectmode.vim new file mode 100644 index 0000000000..b483841060 --- /dev/null +++ b/src/nvim/testdir/test_selectmode.vim @@ -0,0 +1,57 @@ +" Test for Select-mode + +source shared.vim + +" Test for selecting a register with CTRL-R +func Test_selectmode_register() + new + + " Default behavior: use unnamed register + call setline(1, 'foo') + call setreg('"', 'bar') + call setreg('a', 'baz') + exe ":norm! v\<c-g>a" + call assert_equal(getline('.'), 'aoo') + call assert_equal('f', getreg('"')) + call assert_equal('baz', getreg('a')) + + " Use the black hole register + call setline(1, 'foo') + call setreg('"', 'bar') + call setreg('a', 'baz') + exe ":norm! v\<c-g>\<c-r>_a" + call assert_equal(getline('.'), 'aoo') + call assert_equal('bar', getreg('"')) + call assert_equal('baz', getreg('a')) + + " Invalid register: use unnamed register + call setline(1, 'foo') + call setreg('"', 'bar') + call setreg('a', 'baz') + exe ":norm! v\<c-g>\<c-r>?a" + call assert_equal(getline('.'), 'aoo') + call assert_equal('f', getreg('"')) + call assert_equal('baz', getreg('a')) + + " Use unnamed register + call setline(1, 'foo') + call setreg('"', 'bar') + call setreg('a', 'baz') + exe ":norm! v\<c-g>\<c-r>\"a" + call assert_equal(getline('.'), 'aoo') + call assert_equal('f', getreg('"')) + call assert_equal('baz', getreg('a')) + + " use specicifed register, unnamed register is also written + call setline(1, 'foo') + call setreg('"', 'bar') + call setreg('a', 'baz') + exe ":norm! v\<c-g>\<c-r>aa" + call assert_equal(getline('.'), 'aoo') + call assert_equal('f', getreg('"')) + call assert_equal('f', getreg('a')) + + bw! +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_source_utf8.vim b/src/nvim/testdir/test_source_utf8.vim index e93ea29dff..66fabe0442 100644 --- a/src/nvim/testdir/test_source_utf8.vim +++ b/src/nvim/testdir/test_source_utf8.vim @@ -1,4 +1,5 @@ " Test the :source! command +source check.vim func Test_source_utf8() " check that sourcing a script with 0x80 as second byte works @@ -31,24 +32,24 @@ endfunc " Test for sourcing a file with CTRL-V's at the end of the line func Test_source_ctrl_v() - call writefile(['map __1 afirst', - \ 'map __2 asecond', - \ 'map __3 athird', - \ 'map __4 afourth', - \ 'map __5 afifth', - \ "map __1 asd\<C-V>", - \ "map __2 asd\<C-V>\<C-V>", - \ "map __3 asd\<C-V>\<C-V>", - \ "map __4 asd\<C-V>\<C-V>\<C-V>", - \ "map __5 asd\<C-V>\<C-V>\<C-V>", - \ ], 'Xtestfile') + call writefile(['map __1 afirst', + \ 'map __2 asecond', + \ 'map __3 athird', + \ 'map __4 afourth', + \ 'map __5 afifth', + \ "map __1 asd\<C-V>", + \ "map __2 asd\<C-V>\<C-V>", + \ "map __3 asd\<C-V>\<C-V>", + \ "map __4 asd\<C-V>\<C-V>\<C-V>", + \ "map __5 asd\<C-V>\<C-V>\<C-V>", + \ ], 'Xtestfile') source Xtestfile enew! exe "normal __1\<Esc>\<Esc>__2\<Esc>__3\<Esc>\<Esc>__4\<Esc>__5\<Esc>" exe "%s/\<C-J>/0/g" call assert_equal(['sd', - \ "map __2 asd\<Esc>secondsd\<Esc>sd0map __5 asd0fifth"], - \ getline(1, 2)) + \ "map __2 asd\<Esc>secondsd\<Esc>sd0map __5 asd0fifth"], + \ getline(1, 2)) enew! call delete('Xtestfile') diff --git a/src/nvim/testdir/test_spell.vim b/src/nvim/testdir/test_spell.vim index cf0faeee31..1ecb5c8070 100644 --- a/src/nvim/testdir/test_spell.vim +++ b/src/nvim/testdir/test_spell.vim @@ -768,6 +768,14 @@ func Test_spell_screendump() call delete('XtestSpell') endfunc +func Test_spell_single_word() + new + silent! norm 0R00 + spell! + silent 0norm 0r$ Dvz= + bwipe! +endfunc + let g:test_data_aff1 = [ \"SET ISO8859-1", \"TRY esianrtolcdugmphbyfvkwjkqxz-\xEB\xE9\xE8\xEA\xEF\xEE\xE4\xE0\xE2\xF6\xFC\xFB'ESIANRTOLCDUGMPHBYFVKWJKQXZ", diff --git a/src/nvim/testdir/test_statusline.vim b/src/nvim/testdir/test_statusline.vim index a3e4dcdd25..fad13e3340 100644 --- a/src/nvim/testdir/test_statusline.vim +++ b/src/nvim/testdir/test_statusline.vim @@ -186,7 +186,16 @@ func Test_statusline() set virtualedit=all norm 10| call assert_match('^10,-10\s*$', s:get_statusline()) + set list + call assert_match('^10,-10\s*$', s:get_statusline()) set virtualedit& + exe "norm A\<Tab>\<Tab>a\<Esc>" + " In list mode a <Tab> is shown as "^I", which is 2-wide. + call assert_match('^9,-9\s*$', s:get_statusline()) + set list& + " Now the second <Tab> ends at the 16th screen column. + call assert_match('^17,-17\s*$', s:get_statusline()) + undo " %w: Preview window flag, text is "[Preview]". " %W: Preview window flag, text is ",PRV". @@ -498,5 +507,20 @@ func Test_statusline_after_split_vsplit() set ls& stl& endfunc +" Test using a multibyte character for 'stl' and 'stlnc' items in 'fillchars' +" with a custom 'statusline' +func Test_statusline_mbyte_fillchar() + only + set laststatus=2 + set fillchars=vert:\|,fold:-,stl:━,stlnc:═ + set statusline=a%=b + call assert_match('^a\+━\+b$', s:get_statusline()) + vnew + call assert_match('^a\+━\+b━a\+═\+b$', s:get_statusline()) + wincmd w + call assert_match('^a\+═\+b═a\+━\+b$', s:get_statusline()) + set statusline& fillchars& laststatus& + %bw! +endfunc " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_substitute.vim b/src/nvim/testdir/test_substitute.vim index 20b760ac15..dbb792d2b0 100644 --- a/src/nvim/testdir/test_substitute.vim +++ b/src/nvim/testdir/test_substitute.vim @@ -51,10 +51,12 @@ func Test_substitute_variants() \ { 'cmd': ':s/t/r/cg', 'exp': 'Tesring srring', 'prompt': 'a' }, \ { 'cmd': ':s/t/r/ci', 'exp': 'resting string', 'prompt': 'y' }, \ { 'cmd': ':s/t/r/cI', 'exp': 'Tesring string', 'prompt': 'y' }, + \ { 'cmd': ':s/t/r/c', 'exp': 'Testing string', 'prompt': 'n' }, \ { 'cmd': ':s/t/r/cn', 'exp': ln }, \ { 'cmd': ':s/t/r/cp', 'exp': 'Tesring string', 'prompt': 'y' }, \ { 'cmd': ':s/t/r/cl', 'exp': 'Tesring string', 'prompt': 'y' }, \ { 'cmd': ':s/t/r/gc', 'exp': 'Tesring srring', 'prompt': 'a' }, + \ { 'cmd': ':s/i/I/gc', 'exp': 'TestIng string', 'prompt': 'l' }, \ { 'cmd': ':s/foo/bar/ge', 'exp': ln }, \ { 'cmd': ':s/t/r/g', 'exp': 'Tesring srring' }, \ { 'cmd': ':s/t/r/gi', 'exp': 'resring srring' }, @@ -86,6 +88,7 @@ func Test_substitute_variants() \ { 'cmd': ':s//r/rp', 'exp': 'Testr string' }, \ { 'cmd': ':s//r/rl', 'exp': 'Testr string' }, \ { 'cmd': ':s//r/r', 'exp': 'Testr string' }, + \ { 'cmd': ':s/i/I/gc', 'exp': 'Testing string', 'prompt': 'q' }, \] for var in variants @@ -105,7 +108,7 @@ func Test_substitute_variants() call assert_equal(var.exp, getline('.'), msg) endfor endfor -endfunction +endfunc " Test the l, p, # flags. func Test_substitute_flags_lp() @@ -384,6 +387,10 @@ func Test_substitute_join() call assert_equal(["foo\tbarbar\<C-H>foo"], getline(1, '$')) call assert_equal('\n', histget("search", -1)) + call setline(1, ['foo', 'bar', 'baz', 'qux']) + call execute('1,2s/\n//') + call assert_equal(['foobarbaz', 'qux'], getline(1, '$')) + bwipe! endfunc @@ -398,6 +405,11 @@ func Test_substitute_count() call assert_fails('s/foo/bar/0', 'E939:') + call setline(1, ['foo foo', 'foo foo', 'foo foo', 'foo foo', 'foo foo']) + 2,4s/foo/bar/ 10 + call assert_equal(['foo foo', 'foo foo', 'foo foo', 'bar foo', 'bar foo'], + \ getline(1, '$')) + bwipe! endfunc @@ -416,6 +428,10 @@ func Test_substitute_flag_n() " No substitution should have been done. call assert_equal(lines, getline(1, '$')) + %delete _ + call setline(1, ['A', 'Bar', 'Baz']) + call assert_equal("\n1 match on 1 line", execute('s/\nB\@=//gn')) + bwipe! endfunc @@ -749,6 +765,45 @@ func Test_sub_beyond_end() bwipe! endfunc +" Test for repeating last substitution using :~ and :&r +func Test_repeat_last_sub() + new + call setline(1, ['blue green yellow orange white']) + s/blue/red/ + let @/ = 'yellow' + ~ + let @/ = 'white' + :&r + let @/ = 'green' + s//gray + call assert_equal('red gray red orange red', getline(1)) + close! +endfunc + +" Test for Vi compatible substitution: +" \/{string}/, \?{string}? and \&{string}& +func Test_sub_vi_compatibility() + new + call setline(1, ['blue green yellow orange blue']) + let @/ = 'orange' + s\/white/ + let @/ = 'blue' + s\?amber? + let @/ = 'white' + s\&green& + call assert_equal('amber green yellow white green', getline(1)) + close! +endfunc + +" Test for substitute with the new text longer than the original text +func Test_sub_expand_text() + new + call setline(1, 'abcabcabcabcabcabcabcabc') + s/b/\=repeat('B', 10)/g + call assert_equal(repeat('aBBBBBBBBBBc', 8), getline(1)) + close! +endfunc + func Test_submatch_list_concatenate() let pat = 'A\(.\)' let Rep = {-> string([submatch(0, 1)] + [[submatch(1)]])} diff --git a/src/nvim/testdir/test_swap.vim b/src/nvim/testdir/test_swap.vim index b3018b2b0d..b180f27685 100644 --- a/src/nvim/testdir/test_swap.vim +++ b/src/nvim/testdir/test_swap.vim @@ -1,6 +1,7 @@ " Tests for the swap feature source check.vim +source shared.vim func s:swapname() return trim(execute('swapname')) @@ -198,14 +199,17 @@ func Test_swapfile_delete() quit call assert_equal(fnamemodify(swapfile_name, ':t'), fnamemodify(s:swapname, ':t')) - " Write the swapfile with a modified PID, now it will be automatically - " deleted. Process one should never be Vim. - let swapfile_bytes[24:27] = 0z01000000 - call writefile(swapfile_bytes, swapfile_name) - let s:swapname = '' - split XswapfileText - quit - call assert_equal('', s:swapname) + " This test won't work as root because root can successfully run kill(1, 0) + if !IsRoot() + " Write the swapfile with a modified PID, now it will be automatically + " deleted. Process one should never be Vim. + let swapfile_bytes[24:27] = 0z01000000 + call writefile(swapfile_bytes, swapfile_name) + let s:swapname = '' + split XswapfileText + quit + call assert_equal('', s:swapname) + endif " Now set the modified flag, the swap file will not be deleted let swapfile_bytes[28 + 80 + 899] = 0x55 diff --git a/src/nvim/testdir/test_syntax.vim b/src/nvim/testdir/test_syntax.vim index 757866f5dc..b047b53b6f 100644 --- a/src/nvim/testdir/test_syntax.vim +++ b/src/nvim/testdir/test_syntax.vim @@ -728,6 +728,56 @@ func Test_syntax_foldlevel() quit! endfunc +func Test_search_syntax_skip() + new + let lines =<< trim END + + /* This is VIM */ + Another Text for VIM + let a = "VIM" + END + call setline(1, lines) + syntax on + syntax match Comment "^/\*.*\*/" + syntax match String '".*"' + + " Skip argument using string evaluation. + 1 + call search('VIM', 'w', '', 0, 'synIDattr(synID(line("."), col("."), 1), "name") =~? "comment"') + call assert_equal('Another Text for VIM', getline('.')) + 1 + call search('VIM', 'w', '', 0, 'synIDattr(synID(line("."), col("."), 1), "name") !~? "string"') + call assert_equal(' let a = "VIM"', getline('.')) + + " Skip argument using Lambda. + 1 + call search('VIM', 'w', '', 0, { -> synIDattr(synID(line("."), col("."), 1), "name") =~? "comment"}) + call assert_equal('Another Text for VIM', getline('.')) + + 1 + call search('VIM', 'w', '', 0, { -> synIDattr(synID(line("."), col("."), 1), "name") !~? "string"}) + call assert_equal(' let a = "VIM"', getline('.')) + + " Skip argument using funcref. + func InComment() + return synIDattr(synID(line("."), col("."), 1), "name") =~? "comment" + endfunc + func InString() + return synIDattr(synID(line("."), col("."), 1), "name") !~? "string" + endfunc + 1 + call search('VIM', 'w', '', 0, function('InComment')) + call assert_equal('Another Text for VIM', getline('.')) + + 1 + call search('VIM', 'w', '', 0, function('InString')) + call assert_equal(' let a = "VIM"', getline('.')) + + delfunc InComment + delfunc InString + bwipe! +endfunc + func Test_syn_include_contains_TOP() let l:case = "TOP in included syntax means its group list name" new diff --git a/src/nvim/testdir/test_system.vim b/src/nvim/testdir/test_system.vim index 1858b48807..5b8079d7b6 100644 --- a/src/nvim/testdir/test_system.vim +++ b/src/nvim/testdir/test_system.vim @@ -46,9 +46,9 @@ func Test_System() bwipe! call assert_fails('call system("wc -l", 99999)', 'E86:') -endfunction +endfunc -function! Test_system_exmode() +func Test_system_exmode() if has('unix') " echo $? only works on Unix let cmd = ' -es --headless -u NONE -c "source Xscript" +q; echo "result=$?"' " Need to put this in a script, "catch" isn't found after an unknown diff --git a/src/nvim/testdir/test_tagjump.vim b/src/nvim/testdir/test_tagjump.vim index 2aa04df42a..e0b05edf15 100644 --- a/src/nvim/testdir/test_tagjump.vim +++ b/src/nvim/testdir/test_tagjump.vim @@ -641,7 +641,7 @@ func Test_tag_envvar() endfunc " Test for :ptag -func Test_ptag() +func Test_tag_preview() call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", \ "second\tXfile1\t2", \ "third\tXfile1\t3",], @@ -655,11 +655,19 @@ func Test_ptag() call assert_equal(2, winnr('$')) call assert_equal(1, getwinvar(1, '&previewwindow')) call assert_equal(0, getwinvar(2, '&previewwindow')) - wincmd w + wincmd P call assert_equal(3, line('.')) " jump to the tag again + wincmd w ptag third + wincmd P + call assert_equal(3, line('.')) + + " jump to the newer tag + wincmd w + ptag + wincmd P call assert_equal(3, line('.')) " close the preview window @@ -829,8 +837,8 @@ func Test_tag_last_search_pat() %bwipe endfunc -" Test for jumping to a tag when the tag stack is full -func Test_tag_stack_full() +" Tag stack tests +func Test_tag_stack() let l = [] for i in range(10, 31) let l += ["var" .. i .. "\tXfoo\t/^int var" .. i .. ";$/"] @@ -844,6 +852,7 @@ func Test_tag_stack_full() endfor call writefile(l, 'Xfoo') + " Jump to a tag when the tag stack is full. Oldest entry should be removed. enew for i in range(10, 30) exe "tag var" .. i @@ -856,9 +865,15 @@ func Test_tag_stack_full() call assert_equal('var12', l.items[0].tagname) call assert_equal('var31', l.items[19].tagname) - " Jump from the top of the stack + " Use tnext with a single match + call assert_fails('tnext', 'E427:') + + " Jump to newest entry from the top of the stack call assert_fails('tag', 'E556:') + " Pop with zero count from the top of the stack + call assert_fails('0pop', 'E556:') + " Pop from an unsaved buffer enew! call append(1, "sample text") @@ -869,6 +884,13 @@ func Test_tag_stack_full() " Pop all the entries in the tag stack call assert_fails('30pop', 'E555:') + " Pop with a count when already at the bottom of the stack + call assert_fails('exe "normal 4\<C-T>"', 'E555:') + call assert_equal(1, gettagstack().curidx) + + " Jump to newest entry from the bottom of the stack with zero count + call assert_fails('0tag', 'E555:') + " Pop the tag stack when it is empty call settagstack(1, {'items' : []}) call assert_fails('pop', 'E73:') @@ -895,6 +917,7 @@ func Test_tag_multimatch() [CODE] call writefile(code, 'Xfoo') + call settagstack(1, {'items' : []}) tag first tlast call assert_equal(3, line('.')) @@ -903,6 +926,151 @@ func Test_tag_multimatch() call assert_equal(1, line('.')) call assert_fails('tprev', 'E425:') + tlast + call feedkeys("5\<CR>", 't') + tselect first + call assert_equal(2, gettagstack().curidx) + + set ignorecase + tag FIRST + tnext + call assert_equal(2, line('.')) + set ignorecase& + + call delete('Xtags') + call delete('Xfoo') + set tags& + %bwipe +endfunc + +" Test for previewing multiple matching tags +func Test_preview_tag_multimatch() + call writefile([ + \ "!_TAG_FILE_ENCODING\tutf-8\t//", + \ "first\tXfoo\t1", + \ "first\tXfoo\t2", + \ "first\tXfoo\t3"], + \ 'Xtags') + set tags=Xtags + let code =<< trim [CODE] + int first() {} + int first() {} + int first() {} + [CODE] + call writefile(code, 'Xfoo') + + enew | only + ptag first + ptlast + wincmd P + call assert_equal(3, line('.')) + wincmd w + call assert_fails('ptnext', 'E428:') + ptprev + wincmd P + call assert_equal(2, line('.')) + wincmd w + ptfirst + wincmd P + call assert_equal(1, line('.')) + wincmd w + call assert_fails('ptprev', 'E425:') + ptnext + wincmd P + call assert_equal(2, line('.')) + wincmd w + ptlast + call feedkeys("5\<CR>", 't') + ptselect first + wincmd P + call assert_equal(3, line('.')) + + pclose + + call delete('Xtags') + call delete('Xfoo') + set tags& + %bwipe +endfunc + +" Test for jumping to multiple matching tags across multiple :tags commands +func Test_tnext_multimatch() + call writefile([ + \ "!_TAG_FILE_ENCODING\tutf-8\t//", + \ "first\tXfoo1\t1", + \ "first\tXfoo2\t1", + \ "first\tXfoo3\t1"], + \ 'Xtags') + set tags=Xtags + let code =<< trim [CODE] + int first() {} + [CODE] + call writefile(code, 'Xfoo1') + call writefile(code, 'Xfoo2') + call writefile(code, 'Xfoo3') + + tag first + tag first + pop + tnext + tnext + call assert_fails('tnext', 'E428:') + + call delete('Xtags') + call delete('Xfoo1') + call delete('Xfoo2') + call delete('Xfoo3') + set tags& + %bwipe +endfunc + +" Test for jumping to multiple matching tags in non-existing files +func Test_multimatch_non_existing_files() + call writefile([ + \ "!_TAG_FILE_ENCODING\tutf-8\t//", + \ "first\tXfoo1\t1", + \ "first\tXfoo2\t1", + \ "first\tXfoo3\t1"], + \ 'Xtags') + set tags=Xtags + + call settagstack(1, {'items' : []}) + call assert_fails('tag first', 'E429:') + call assert_equal(3, gettagstack().items[0].matchnr) + + call delete('Xtags') + set tags& + %bwipe +endfunc + +func Test_tselect_listing() + call writefile([ + \ "!_TAG_FILE_ENCODING\tutf-8\t//", + \ "first\tXfoo\t1" .. ';"' .. "\tv\ttyperef:typename:int\tfile:", + \ "first\tXfoo\t2" .. ';"' .. "\tv\ttyperef:typename:char\tfile:"], + \ 'Xtags') + set tags=Xtags + + let code =<< trim [CODE] + static int first; + static char first; + [CODE] + call writefile(code, 'Xfoo') + + call feedkeys("\<CR>", "t") + let l = split(execute("tselect first"), "\n") + let expected =<< [DATA] + # pri kind tag file + 1 FS v first Xfoo + typeref:typename:int + 1 + 2 FS v first Xfoo + typeref:typename:char + 2 +Type number and <Enter> (q or empty cancels): +[DATA] + call assert_equal(expected, l) + call delete('Xtags') call delete('Xfoo') set tags& diff --git a/src/nvim/testdir/test_textformat.vim b/src/nvim/testdir/test_textformat.vim index bf0706a0c2..e9f846af7b 100644 --- a/src/nvim/testdir/test_textformat.vim +++ b/src/nvim/testdir/test_textformat.vim @@ -196,6 +196,130 @@ func Test_text_format() enew! endfunc +func Test_format_c_comment() + new + setl ai cindent tw=40 et fo=croql + let text =<< trim END + var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf + END + call setline(1, text) + normal gql + let expected =<< trim END + var = 2345; // asdf asdf asdf asdf asdf + // asdf asdf asdf asdf asdf + END + call assert_equal(expected, getline(1, '$')) + + %del + let text =<< trim END + var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf + END + call setline(1, text) + normal gql + let expected =<< trim END + var = 2345; // asdf asdf asdf asdf asdf + // asdf asdf asdf asdf asdf + // asdf asdf + END + call assert_equal(expected, getline(1, '$')) + + %del + let text =<< trim END + #if 0 // This is another long end of + // line comment that + // wraps. + END + call setline(1, text) + normal gq2j + let expected =<< trim END + #if 0 // This is another long + // end of line comment + // that wraps. + END + call assert_equal(expected, getline(1, '$')) + + " Using either "o" or "O" repeats a line comment occupying a whole line. + %del + let text =<< trim END + nop; + // This is a comment + val = val; + END + call setline(1, text) + normal 2Go + let expected =<< trim END + nop; + // This is a comment + // + val = val; + END + call assert_equal(expected, getline(1, '$')) + normal 2GO + let expected =<< trim END + nop; + // + // This is a comment + // + val = val; + END + call assert_equal(expected, getline(1, '$')) + + " Using "o" repeats a line comment after a statement, "O" does not. + %del + let text =<< trim END + nop; + val = val; // This is a comment + END + call setline(1, text) + normal 2Go + let expected =<< trim END + nop; + val = val; // This is a comment + // + END + call assert_equal(expected, getline(1, '$')) + normal 2GO + let expected =<< trim END + nop; + + val = val; // This is a comment + // + END + call assert_equal(expected, getline(1, '$')) + + " Using "o" does not repeat a comment in a string + %del + let text =<< trim END + nop; + val = " // This is not a comment"; + END + call setline(1, text) + normal 2Gox + let expected =<< trim END + nop; + val = " // This is not a comment"; + x + END + call assert_equal(expected, getline(1, '$')) + + " Using CTRL-U after "o" fixes the indent + %del + let text =<< trim END + { + val = val; // This is a comment + END + call setline(1, text) + exe "normal! 2Go\<C-U>x\<Esc>" + let expected =<< trim END + { + val = val; // This is a comment + x + END + call assert_equal(expected, getline(1, '$')) + + bwipe! +endfunc + " Tests for :right, :center and :left on text with embedded TAB. func Test_format_align() enew! @@ -433,6 +557,21 @@ func Test_format_align() call assert_equal("\t\t Vim", getline(1)) q! + " align text with 'rightleft' + if has('rightleft') + new + call setline(1, 'Vim') + setlocal rightleft + left 20 + setlocal norightleft + call assert_equal("\t\t Vim", getline(1)) + setlocal rightleft + right + setlocal norightleft + call assert_equal("Vim", getline(1)) + close! + endif + set tw& endfunc diff --git a/src/nvim/testdir/test_timers.vim b/src/nvim/testdir/test_timers.vim index aae315b2c5..09eed4e10d 100644 --- a/src/nvim/testdir/test_timers.vim +++ b/src/nvim/testdir/test_timers.vim @@ -379,4 +379,27 @@ func Test_timer_invalid_callback() call assert_fails('call timer_start(0, "0")', 'E921') endfunc +func Test_timer_using_win_execute_undo_sync() + let bufnr1 = bufnr() + new + let g:bufnr2 = bufnr() + let g:winid = win_getid() + exe "buffer " .. bufnr1 + wincmd w + call setline(1, ['test']) + autocmd InsertEnter * call timer_start(100, { -> win_execute(g:winid, 'buffer ' .. g:bufnr2) }) + call timer_start(200, { -> feedkeys("\<CR>bbbb\<Esc>") }) + call feedkeys("Oaaaa", 'x!t') + " will hang here until the second timer fires + call assert_equal(['aaaa', 'bbbb', 'test'], getline(1, '$')) + undo + call assert_equal(['test'], getline(1, '$')) + + bwipe! + bwipe! + unlet g:winid + unlet g:bufnr2 + au! InsertEnter +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_usercommands.vim b/src/nvim/testdir/test_usercommands.vim index 29e578ac6d..967ad85a64 100644 --- a/src/nvim/testdir/test_usercommands.vim +++ b/src/nvim/testdir/test_usercommands.vim @@ -269,10 +269,10 @@ endfunc func Test_CmdCompletion() call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx') - call assert_equal('"com -addr bang bar buffer complete count nargs range register', @:) + call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:) call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx') - call assert_equal('"com -nargs=0 -addr bang bar buffer complete count nargs range register', @:) + call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:) call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx') call assert_equal('"com -nargs=* + 0 1 ?', @:) @@ -350,7 +350,6 @@ func Test_use_execute_in_completion() endfunc func Test_addr_all() - throw 'skipped: requires patch v8.1.0341 to pass' command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2> %DoSomething call assert_equal(1, g:a1) @@ -551,3 +550,26 @@ func Test_command_list() call assert_equal("\nNo user-defined commands found", execute(':command Xxx')) call assert_equal("\nNo user-defined commands found", execute('command')) endfunc + +func Test_delcommand_buffer() + command Global echo 'global' + command -buffer OneBuffer echo 'one' + new + command -buffer TwoBuffer echo 'two' + call assert_equal(0, exists(':OneBuffer')) + call assert_equal(2, exists(':Global')) + call assert_equal(2, exists(':TwoBuffer')) + delcommand -buffer TwoBuffer + call assert_equal(0, exists(':TwoBuffer')) + call assert_fails('delcommand -buffer Global', 'E1237:') + call assert_fails('delcommand -buffer OneBuffer', 'E1237:') + bwipe! + call assert_equal(2, exists(':OneBuffer')) + delcommand -buffer OneBuffer + call assert_equal(0, exists(':OneBuffer')) + call assert_fails('delcommand -buffer Global', 'E1237:') + delcommand Global + call assert_equal(0, exists(':Global')) +endfunc + +" vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_utf8.vim b/src/nvim/testdir/test_utf8.vim index 0818c2e4b0..36776d5a64 100644 --- a/src/nvim/testdir/test_utf8.vim +++ b/src/nvim/testdir/test_utf8.vim @@ -1,5 +1,6 @@ " Tests for Unicode manipulations +source check.vim source view_util.vim " Visual block Insert adjusts for multi-byte char @@ -148,4 +149,55 @@ func Test_print_overlong() bwipe! endfunc +func Test_recording_with_select_mode_utf8() + call Run_test_recording_with_select_mode_utf8() +endfunc + +func Run_test_recording_with_select_mode_utf8() + new + + " No escaping + call feedkeys("qacc12345\<Esc>gH哦\<Esc>q", "tx") + call assert_equal("哦", getline(1)) + call assert_equal("cc12345\<Esc>gH哦\<Esc>", @a) + call setline(1, 'asdf') + normal! @a + call assert_equal("哦", getline(1)) + + " 固 is 0xE5 0x9B 0xBA where 0x9B is CSI + call feedkeys("qacc12345\<Esc>gH固\<Esc>q", "tx") + call assert_equal("固", getline(1)) + call assert_equal("cc12345\<Esc>gH固\<Esc>", @a) + call setline(1, 'asdf') + normal! @a + call assert_equal("固", getline(1)) + + " 四 is 0xE5 0x9B 0x9B where 0x9B is CSI + call feedkeys("qacc12345\<Esc>gH四\<Esc>q", "tx") + call assert_equal("四", getline(1)) + call assert_equal("cc12345\<Esc>gH四\<Esc>", @a) + call setline(1, 'asdf') + normal! @a + call assert_equal("四", getline(1)) + + " 倒 is 0xE5 0x80 0x92 where 0x80 is K_SPECIAL + call feedkeys("qacc12345\<Esc>gH倒\<Esc>q", "tx") + call assert_equal("倒", getline(1)) + call assert_equal("cc12345\<Esc>gH倒\<Esc>", @a) + call setline(1, 'asdf') + normal! @a + call assert_equal("倒", getline(1)) + + bwipe! +endfunc + +" This must be done as one of the last tests, because it starts the GUI, which +" cannot be undone. +func Test_zz_recording_with_select_mode_utf8_gui() + CheckCanRunGui + + gui -f + call Run_test_recording_with_select_mode_utf8() +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_utf8_comparisons.vim b/src/nvim/testdir/test_utf8_comparisons.vim index fdf9d80802..f3c86b44fb 100644 --- a/src/nvim/testdir/test_utf8_comparisons.vim +++ b/src/nvim/testdir/test_utf8_comparisons.vim @@ -1,12 +1,12 @@ " Tests for case-insensitive UTF-8 comparisons (utf_strnicmp() in mbyte.c) " Also test "g~ap". -function! Ch(a, op, b, expected) +func Ch(a, op, b, expected) call assert_equal(eval(printf('"%s" %s "%s"', a:a, a:op, a:b)), a:expected, \ printf('"%s" %s "%s" should return %d', a:a, a:op, a:b, a:expected)) -endfunction +endfunc -function! Chk(a, b, result) +func Chk(a, b, result) if a:result == 0 call Ch(a:a, '==?', a:b, 1) call Ch(a:a, '!=?', a:b, 0) @@ -86,6 +86,9 @@ endfunc " test that g~ap changes one paragraph only. func Test_gap() new - call feedkeys("iabcd\n\ndefggg0g~ap", "tx") + " setup text + call feedkeys("iabcd\<cr>\<cr>defg", "tx") + " modify only first line + call feedkeys("gg0g~ap", "tx") call assert_equal(["ABCD", "", "defg"], getline(1,3)) endfunc diff --git a/src/nvim/testdir/test_vartabs.vim b/src/nvim/testdir/test_vartabs.vim index 46e0d62313..017bb6675d 100644 --- a/src/nvim/testdir/test_vartabs.vim +++ b/src/nvim/testdir/test_vartabs.vim @@ -135,7 +135,17 @@ func Test_vartabs() bwipeout! endfunc -func! Test_vartabs_breakindent() +func Test_retab_invalid_arg() + new + call setline(1, "\ttext") + retab 0 + call assert_fails("retab -8", 'E487: Argument must be positive') + call assert_fails("retab 10000", 'E475:') + call assert_fails("retab 720575940379279360", 'E475:') + bwipe! +endfunc + +func Test_vartabs_breakindent() if !exists("+breakindent") return endif diff --git a/src/nvim/testdir/test_vimscript.vim b/src/nvim/testdir/test_vimscript.vim index 34733f127f..75a965f16d 100644 --- a/src/nvim/testdir/test_vimscript.vim +++ b/src/nvim/testdir/test_vimscript.vim @@ -25,7 +25,7 @@ com! -nargs=1 Xout call Xout(<args>) " in the variable argument list. This function is useful if similar tests are " to be made for a ":return" from a function call or a ":finish" in a script " file. -function! MakeScript(funcname, ...) +func MakeScript(funcname, ...) let script = tempname() execute "redir! >" . script execute "function" a:funcname @@ -1156,6 +1156,82 @@ func Test_type() call assert_equal(v:t_list, type(v:_null_list)) call assert_equal(v:t_dict, type(v:_null_dict)) call assert_equal(v:t_blob, type(v:_null_blob)) + + call assert_equal(0, 0 + v:false) + call assert_equal(1, 0 + v:true) + " call assert_equal(0, 0 + v:none) + call assert_equal(0, 0 + v:null) + + call assert_equal('false', '' . v:false) + call assert_equal('true', '' . v:true) + " call assert_equal('none', '' . v:none) + call assert_equal('null', '' . v:null) + + call assert_true(v:false == 0) + call assert_false(v:false != 0) + call assert_true(v:true == 1) + call assert_false(v:true != 1) + call assert_false(v:true == v:false) + call assert_true(v:true != v:false) + + call assert_true(v:null == 0) + call assert_false(v:null != 0) + " call assert_true(v:none == 0) + " call assert_false(v:none != 0) + + call assert_true(v:false is v:false) + call assert_true(v:true is v:true) + " call assert_true(v:none is v:none) + call assert_true(v:null is v:null) + + call assert_false(v:false isnot v:false) + call assert_false(v:true isnot v:true) + " call assert_false(v:none isnot v:none) + call assert_false(v:null isnot v:null) + + call assert_false(v:false is 0) + call assert_false(v:true is 1) + call assert_false(v:true is v:false) + " call assert_false(v:none is 0) + call assert_false(v:null is 0) + " call assert_false(v:null is v:none) + + call assert_true(v:false isnot 0) + call assert_true(v:true isnot 1) + call assert_true(v:true isnot v:false) + " call assert_true(v:none isnot 0) + call assert_true(v:null isnot 0) + " call assert_true(v:null isnot v:none) + + call assert_equal(v:false, eval(string(v:false))) + call assert_equal(v:true, eval(string(v:true))) + " call assert_equal(v:none, eval(string(v:none))) + call assert_equal(v:null, eval(string(v:null))) + + call assert_equal(v:false, copy(v:false)) + call assert_equal(v:true, copy(v:true)) + " call assert_equal(v:none, copy(v:none)) + call assert_equal(v:null, copy(v:null)) + + call assert_equal([v:false], deepcopy([v:false])) + call assert_equal([v:true], deepcopy([v:true])) + " call assert_equal([v:none], deepcopy([v:none])) + call assert_equal([v:null], deepcopy([v:null])) + + call assert_true(empty(v:false)) + call assert_false(empty(v:true)) + call assert_true(empty(v:null)) + " call assert_true(empty(v:none)) + + func ChangeYourMind() + try + return v:true + finally + return 'something else' + endtry + endfunc + + call ChangeYourMind() endfunc "------------------------------------------------------------------------------- @@ -1692,6 +1768,26 @@ func Test_function_defined_line() call delete('Xtest.vim') endfunc +func Test_for_over_string() + let res = '' + for c in 'aéc̀d' + let res ..= c .. '-' + endfor + call assert_equal('a-é-c̀-d-', res) + + let res = '' + for c in '' + let res ..= c .. '-' + endfor + call assert_equal('', res) + + let res = '' + for c in v:_null_string + let res ..= c .. '-' + endfor + call assert_equal('', res) +endfunc + "------------------------------------------------------------------------------- " Modelines {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker diff --git a/src/nvim/testdir/test_virtualedit.vim b/src/nvim/testdir/test_virtualedit.vim index 8f992f7501..250b896532 100644 --- a/src/nvim/testdir/test_virtualedit.vim +++ b/src/nvim/testdir/test_virtualedit.vim @@ -81,6 +81,133 @@ func Test_edit_change() normal Cx call assert_equal('x', getline(1)) bwipe! + set virtualedit= +endfunc + +" Tests for pasting at the beginning, end and middle of a tab character +" in virtual edit mode. +func Test_paste_in_tab() + new + call append(0, '') + set virtualedit=all + + " Tests for pasting a register with characterwise mode type + call setreg('"', 'xyz', 'c') + + " paste (p) unnamed register at the beginning of a tab + call setline(1, "a\tb") + call cursor(1, 2, 0) + normal p + call assert_equal('a xyz b', getline(1)) + + " paste (P) unnamed register at the beginning of a tab + call setline(1, "a\tb") + call cursor(1, 2, 0) + normal P + call assert_equal("axyz\tb", getline(1)) + + " paste (p) unnamed register at the end of a tab + call setline(1, "a\tb") + call cursor(1, 2, 6) + normal p + call assert_equal("a\txyzb", getline(1)) + + " paste (P) unnamed register at the end of a tab + call setline(1, "a\tb") + call cursor(1, 2, 6) + normal P + call assert_equal('a xyz b', getline(1)) + + " Tests for pasting a register with blockwise mode type + call setreg('"', 'xyz', 'b') + + " paste (p) unnamed register at the beginning of a tab + call setline(1, "a\tb") + call cursor(1, 2, 0) + normal p + call assert_equal('a xyz b', getline(1)) + + " paste (P) unnamed register at the beginning of a tab + call setline(1, "a\tb") + call cursor(1, 2, 0) + normal P + call assert_equal("axyz\tb", getline(1)) + + " paste (p) unnamed register at the end of a tab + call setline(1, "a\tb") + call cursor(1, 2, 6) + normal p + call assert_equal("a\txyzb", getline(1)) + + " paste (P) unnamed register at the end of a tab + call setline(1, "a\tb") + call cursor(1, 2, 6) + normal P + call assert_equal('a xyz b', getline(1)) + + " Tests for pasting with gp and gP in virtual edit mode + + " paste (gp) unnamed register at the beginning of a tab + call setline(1, "a\tb") + call cursor(1, 2, 0) + normal gp + call assert_equal('a xyz b', getline(1)) + call assert_equal([0, 1, 12, 0, 12], getcurpos()) + + " paste (gP) unnamed register at the beginning of a tab + call setline(1, "a\tb") + call cursor(1, 2, 0) + normal gP + call assert_equal("axyz\tb", getline(1)) + call assert_equal([0, 1, 5, 0, 5], getcurpos()) + + " paste (gp) unnamed register at the end of a tab + call setline(1, "a\tb") + call cursor(1, 2, 6) + normal gp + call assert_equal("a\txyzb", getline(1)) + call assert_equal([0, 1, 6, 0, 12], getcurpos()) + + " paste (gP) unnamed register at the end of a tab + call setline(1, "a\tb") + call cursor(1, 2, 6) + normal gP + call assert_equal('a xyz b', getline(1)) + call assert_equal([0, 1, 12, 0, 12], getcurpos()) + + " Tests for pasting a named register + let @r = 'xyz' + + " paste (gp) named register in the middle of a tab + call setline(1, "a\tb") + call cursor(1, 2, 2) + normal "rgp + call assert_equal('a xyz b', getline(1)) + call assert_equal([0, 1, 8, 0, 8], getcurpos()) + + " paste (gP) named register in the middle of a tab + call setline(1, "a\tb") + call cursor(1, 2, 2) + normal "rgP + call assert_equal('a xyz b', getline(1)) + call assert_equal([0, 1, 7, 0, 7], getcurpos()) + + bwipe! + set virtualedit= +endfunc + +" Test for yanking a few spaces within a tab to a register +func Test_yank_in_tab() + new + let @r = '' + call setline(1, "a\tb") + set virtualedit=all + call cursor(1, 2, 2) + normal "ry5l + call assert_equal(' ', @r) + + bwipe! + set virtualedit= endfunc " Insert "keyword keyw", ESC, C CTRL-N, shows "keyword ykeyword". @@ -216,4 +343,139 @@ func Test_yank_paste_small_del_reg() set virtualedit= endfunc +" After calling s:TryVirtualeditReplace(), line 1 will contain one of these +" two strings, depending on whether virtual editing is on or off. +let s:result_ve_on = 'a x' +let s:result_ve_off = 'x' + +" Utility function for Test_global_local_virtualedit() +func s:TryVirtualeditReplace() + call setline(1, 'a') + normal gg7l + normal rx +endfunc + +" Test for :set and :setlocal +func Test_global_local_virtualedit() + new + + " Verify that 'virtualedit' is initialized to empty, can be set globally to + " all and to empty, and can be set locally to all and to empty. + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + set ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + set ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + " Verify that :set affects multiple windows. + split + set ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + set ve= + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + bwipe! + + " Verify that :setlocal affects only the current window. + new + split + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + bwipe! + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + " Verify that the buffer 'virtualedit' state follows the global value only + " when empty and that "none" works as expected. + " + " 'virtualedit' State + " +--------+--------------------------+ + " | Local | Global | + " | | | + " +--------+--------+--------+--------+ + " | | "" | "all" | "none" | + " +--------+--------+--------+--------+ + " | "" | off | on | off | + " | "all" | on | on | on | + " | "none" | off | off | off | + " +--------+--------+--------+--------+ + new + + setglobal ve= + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=none + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + setglobal ve=all + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=none + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=NONE + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + setglobal ve=none + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=none + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + bwipe! + + " Verify that the 'virtualedit' state is copied to new windows. + new + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + split + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + split + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve= + split + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + bwipe! + + setlocal virtualedit& + set virtualedit& +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_visual.vim b/src/nvim/testdir/test_visual.vim index 8344598486..099a90643f 100644 --- a/src/nvim/testdir/test_visual.vim +++ b/src/nvim/testdir/test_visual.vim @@ -433,6 +433,19 @@ func Test_Visual_Block() close! endfunc +" Test for 'p'ut in visual block mode +func Test_visual_block_put() + enew + + call append(0, ['One', 'Two', 'Three']) + normal gg + yank + call feedkeys("jl\<C-V>ljp", 'xt') + call assert_equal(['One', 'T', 'Tee', 'One', ''], getline(1, '$')) + + enew! +endfunc + func Test_visual_put_in_block() new call setline(1, ['xxxx', 'y∞yy', 'zzzz']) @@ -1090,6 +1103,13 @@ func Test_visual_put_blockedit_zy_and_zp() bw! endfunc +func Test_visual_block_yank_zy() + new + " this was reading before the start of the line + exe "norm o\<C-T>\<Esc>\<C-V>zy" + bwipe! +endfunc + func Test_visual_block_with_virtualedit() CheckScreendump @@ -1104,11 +1124,126 @@ func Test_visual_block_with_virtualedit() call term_sendkeys(buf, "\<C-V>gg$") call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit', {}) + call term_sendkeys(buf, "\<Esc>gg\<C-V>G$") + call VerifyScreenDump(buf, 'Test_visual_block_with_virtualedit2', {}) + " clean up call term_sendkeys(buf, "\<Esc>") call StopVimInTerminal(buf) - call delete('XTest_beval') + call delete('XTest_block') +endfunc + +func Test_visual_block_ctrl_w_f() + " Emtpy block selected in new buffer should not result in an error. + au! BufNew foo sil norm f + edit foo + + au! BufNew +endfunc + +func Test_visual_reselect_with_count() + " this was causing an illegal memory access + let lines =<< trim END + + + + : + r<sfile> + exe "%norm e3\<c-v>kr\t" + : + + : + END + call writefile(lines, 'XvisualReselect') + source XvisualReselect + + bwipe! + call delete('XvisualReselect') endfunc +" this was leaving the end of the Visual area beyond the end of a line +func Test_visual_ex_copy_line() + new + call setline(1, ["aaa", "bbbbbbbbbxbb"]) + /x + exe "normal ggvjfxO" + t0 + normal gNU + bwipe! +endfunc + +" This was leaving the end of the Visual area beyond the end of a line. +" Set 'undolevels' to start a new undo block. +func Test_visual_undo_deletes_last_line() + new + call setline(1, ["aaa", "ccc", "dyd"]) + set undolevels=100 + exe "normal obbbbbbbbbxbb\<Esc>" + set undolevels=100 + /y + exe "normal ggvjfxO" + undo + normal gNU + + bwipe! +endfunc + +func Test_visual_paste() + new + + " v_p overwrites unnamed register. + call setline(1, ['xxxx']) + call setreg('"', 'foo') + call setreg('-', 'bar') + normal gg0vp + call assert_equal('x', @") + call assert_equal('x', @-) + call assert_equal('fooxxx', getline(1)) + normal $vp + call assert_equal('x', @") + call assert_equal('x', @-) + call assert_equal('fooxxx', getline(1)) + " Test with a different register as unnamed register. + call setline(2, ['baz']) + normal 2gg0"rD + call assert_equal('baz', @") + normal gg0vp + call assert_equal('f', @") + call assert_equal('f', @-) + call assert_equal('bazooxxx', getline(1)) + normal $vp + call assert_equal('x', @") + call assert_equal('x', @-) + call assert_equal('bazooxxf', getline(1)) + + if has('clipboard') + " v_P does not overwrite unnamed register. + call setline(1, ['xxxx']) + call setreg('"', 'foo') + call setreg('-', 'bar') + normal gg0vP + call assert_equal('foo', @") + call assert_equal('x', @-) + call assert_equal('fooxxx', getline(1)) + normal $vP + call assert_equal('foo', @") + call assert_equal('x', @-) + call assert_equal('fooxxfoo', getline(1)) + " Test with a different register as unnamed register. + call setline(2, ['baz']) + normal 2gg0"rD + call assert_equal('baz', @") + normal gg0vP + call assert_equal('baz', @") + call assert_equal('f', @-) + call assert_equal('bazooxxfoo', getline(1)) + normal $vP + call assert_equal('baz', @") + call assert_equal('o', @-) + call assert_equal('bazooxxfobaz', getline(1)) + endif + + bwipe! +endfunc " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_winbuf_close.vim b/src/nvim/testdir/test_winbuf_close.vim index 7f5b80e8d3..f4878c2397 100644 --- a/src/nvim/testdir/test_winbuf_close.vim +++ b/src/nvim/testdir/test_winbuf_close.vim @@ -194,3 +194,22 @@ func Test_tabwin_close() call assert_true(v:true) %bwipe! endfunc + +" Test when closing a split window (above/below) restores space to the window +" below when 'noequalalways' and 'splitright' are set. +func Test_window_close_splitright_noequalalways() + set noequalalways + set splitright + new + let w1 = win_getid() + new + let w2 = win_getid() + execute "normal \<c-w>b" + let h = winheight(0) + let w = win_getid() + new + q + call assert_equal(h, winheight(0), "Window height does not match eight before opening and closing another window") + call assert_equal(w, win_getid(), "Did not return to original window after opening and closing a window") +endfunc + diff --git a/src/nvim/testdir/test_window_cmd.vim b/src/nvim/testdir/test_window_cmd.vim index a200bf7d42..ef6dec580f 100644 --- a/src/nvim/testdir/test_window_cmd.vim +++ b/src/nvim/testdir/test_window_cmd.vim @@ -575,7 +575,7 @@ func Test_winrestcmd() only endfunc -function! Fun_RenewFile() +func Fun_RenewFile() " Need to wait a bit for the timestamp to be older. let old_ftime = getftime("tmp.txt") while getftime("tmp.txt") == old_ftime @@ -585,7 +585,7 @@ function! Fun_RenewFile() sp wincmd p edit! tmp.txt -endfunction +endfunc func Test_window_prevwin() " Can we make this work on MS-Windows? @@ -939,4 +939,124 @@ func Test_window_resize() %bwipe! endfunc +func Test_win_move_separator() + edit a + leftabove vsplit b + let w = winwidth(0) + " check win_move_separator from left window on left window + call assert_equal(1, winnr()) + for offset in range(5) + call assert_true(win_move_separator(0, offset)) + call assert_equal(w + offset, winwidth(0)) + call assert_true(0->win_move_separator(-offset)) + call assert_equal(w, winwidth(0)) + endfor + " check win_move_separator from right window on left window number + wincmd l + call assert_notequal(1, winnr()) + for offset in range(5) + call assert_true(1->win_move_separator(offset)) + call assert_equal(w + offset, winwidth(1)) + call assert_true(win_move_separator(1, -offset)) + call assert_equal(w, winwidth(1)) + endfor + " check win_move_separator from right window on left window ID + let id = win_getid(1) + for offset in range(5) + call assert_true(win_move_separator(id, offset)) + call assert_equal(w + offset, winwidth(id)) + call assert_true(id->win_move_separator(-offset)) + call assert_equal(w, winwidth(id)) + endfor + " check win_move_separator from right window on right window is no-op + let w0 = winwidth(0) + call assert_true(win_move_separator(0, 1)) + call assert_equal(w0, winwidth(0)) + call assert_true(win_move_separator(0, -1)) + call assert_equal(w0, winwidth(0)) + " check that win_move_separator doesn't error with offsets beyond moving + " possibility + call assert_true(win_move_separator(id, 5000)) + call assert_true(winwidth(id) > w) + call assert_true(win_move_separator(id, -5000)) + call assert_true(winwidth(id) < w) + " check that win_move_separator returns false for an invalid window + wincmd = + let w = winwidth(0) + call assert_false(win_move_separator(-1, 1)) + call assert_equal(w, winwidth(0)) + " check that win_move_separator returns false for a floating window + let id = nvim_open_win( + \ 0, 0, #{relative: 'editor', row: 2, col: 2, width: 5, height: 3}) + let w = winwidth(id) + call assert_false(win_move_separator(id, 1)) + call assert_equal(w, winwidth(id)) + call nvim_win_close(id, 1) + %bwipe! +endfunc + +func Test_win_move_statusline() + redraw " This test fails in Nvim without a redraw to clear messages. + edit a + leftabove split b + let h = winheight(0) + " check win_move_statusline from top window on top window + call assert_equal(1, winnr()) + for offset in range(5) + call assert_true(win_move_statusline(0, offset)) + call assert_equal(h + offset, winheight(0)) + call assert_true(0->win_move_statusline(-offset)) + call assert_equal(h, winheight(0)) + endfor + " check win_move_statusline from bottom window on top window number + wincmd j + call assert_notequal(1, winnr()) + for offset in range(5) + call assert_true(1->win_move_statusline(offset)) + call assert_equal(h + offset, winheight(1)) + call assert_true(win_move_statusline(1, -offset)) + call assert_equal(h, winheight(1)) + endfor + " check win_move_statusline from bottom window on bottom window + let h0 = winheight(0) + for offset in range(5) + call assert_true(0->win_move_statusline(-offset)) + call assert_equal(h0 - offset, winheight(0)) + call assert_equal(1 + offset, &cmdheight) + call assert_true(win_move_statusline(0, offset)) + call assert_equal(h0, winheight(0)) + call assert_equal(1, &cmdheight) + endfor + call assert_true(win_move_statusline(0, 1)) + call assert_equal(h0, winheight(0)) + call assert_equal(1, &cmdheight) + " check win_move_statusline from bottom window on top window ID + let id = win_getid(1) + for offset in range(5) + call assert_true(win_move_statusline(id, offset)) + call assert_equal(h + offset, winheight(id)) + call assert_true(id->win_move_statusline(-offset)) + call assert_equal(h, winheight(id)) + endfor + " check that win_move_statusline doesn't error with offsets beyond moving + " possibility + call assert_true(win_move_statusline(id, 5000)) + call assert_true(winheight(id) > h) + call assert_true(win_move_statusline(id, -5000)) + call assert_true(winheight(id) < h) + " check that win_move_statusline returns false for an invalid window + wincmd = + let h = winheight(0) + call assert_false(win_move_statusline(-1, 1)) + call assert_equal(h, winheight(0)) + " check that win_move_statusline returns false for a floating window + let id = nvim_open_win( + \ 0, 0, #{relative: 'editor', row: 2, col: 2, width: 5, height: 3}) + let h = winheight(id) + call assert_false(win_move_statusline(id, 1)) + call assert_equal(h, winheight(id)) + call nvim_win_close(id, 1) + %bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_writefile.vim b/src/nvim/testdir/test_writefile.vim index aa7882d129..b42665c9b5 100644 --- a/src/nvim/testdir/test_writefile.vim +++ b/src/nvim/testdir/test_writefile.vim @@ -43,7 +43,7 @@ func Test_writefile_fails_gently() endfunc func Test_writefile_fails_conversion() - if !has('iconv') || system('uname -s') =~ 'SunOS' + if !has('iconv') || has('sun') return endif " Without a backup file the write won't happen if there is a conversion @@ -169,9 +169,7 @@ endfunc " Test for ':w !<cmd>' to pipe lines from the current buffer to an external " command. func Test_write_pipe_to_cmd() - if !has('unix') - return - endif + CheckUnix new call setline(1, ['L1', 'L2', 'L3', 'L4']) 2,3w !cat > Xfile @@ -371,6 +369,154 @@ func Test_write_file_encoding() %bw! endfunc +" Test for writing and reading a file starting with a BOM. +" Byte Order Mark (BOM) character for various encodings is below: +" UTF-8 : EF BB BF +" UTF-16 (BE): FE FF +" UTF-16 (LE): FF FE +" UTF-32 (BE): 00 00 FE FF +" UTF-32 (LE): FF FE 00 00 +func Test_readwrite_file_with_bom() + let utf8_bom = "\xEF\xBB\xBF" + let utf16be_bom = "\xFE\xFF" + let utf16le_bom = "\xFF\xFE" + let utf32be_bom = "\n\n\xFE\xFF" + let utf32le_bom = "\xFF\xFE\n\n" + let save_fileencoding = &fileencoding + set cpoptions+=S + + " Check that editing a latin1 file doesn't see a BOM + call writefile(["\xFE\xFElatin-1"], 'Xtest1') + edit Xtest1 + call assert_equal('latin1', &fileencoding) + call assert_equal(0, &bomb) + set fenc=latin1 + write Xfile2 + call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xfile2', 'b')) + set bomb fenc=latin1 + write Xtest3 + call assert_equal(["\xFE\xFElatin-1", ''], readfile('Xtest3', 'b')) + set bomb& + + " Check utf-8 BOM + %bw! + call writefile([utf8_bom .. "utf-8"], 'Xtest1') + edit! Xtest1 + call assert_equal('utf-8', &fileencoding) + call assert_equal(1, &bomb) + call assert_equal('utf-8', getline(1)) + set fenc=latin1 + write! Xfile2 + call assert_equal(['utf-8', ''], readfile('Xfile2', 'b')) + set fenc=utf-8 + w! Xtest3 + call assert_equal([utf8_bom .. "utf-8", ''], readfile('Xtest3', 'b')) + + " Check utf-8 with an error (will fall back to latin-1) + %bw! + call writefile([utf8_bom .. "utf-8\x80err"], 'Xtest1') + edit! Xtest1 + call assert_equal('latin1', &fileencoding) + call assert_equal(0, &bomb) + call assert_equal("\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", getline(1)) + set fenc=latin1 + write! Xfile2 + call assert_equal([utf8_bom .. "utf-8\x80err", ''], readfile('Xfile2', 'b')) + set fenc=utf-8 + w! Xtest3 + call assert_equal(["\xC3\xAF\xC2\xBB\xC2\xBFutf-8\xC2\x80err", ''], + \ readfile('Xtest3', 'b')) + + " Check ucs-2 BOM + %bw! + call writefile([utf16be_bom .. "\nu\nc\ns\n-\n2\n"], 'Xtest1') + edit! Xtest1 + call assert_equal('utf-16', &fileencoding) + call assert_equal(1, &bomb) + call assert_equal('ucs-2', getline(1)) + set fenc=latin1 + write! Xfile2 + call assert_equal(["ucs-2", ''], readfile('Xfile2', 'b')) + set fenc=ucs-2 + w! Xtest3 + call assert_equal([utf16be_bom .. "\nu\nc\ns\n-\n2\n", ''], + \ readfile('Xtest3', 'b')) + + " Check ucs-2le BOM + %bw! + call writefile([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n"], 'Xtest1') + " Need to add a NUL byte after the NL byte + call writefile(0z00, 'Xtest1', 'a') + edit! Xtest1 + call assert_equal('utf-16le', &fileencoding) + call assert_equal(1, &bomb) + call assert_equal('ucs-2le', getline(1)) + set fenc=latin1 + write! Xfile2 + call assert_equal(["ucs-2le", ''], readfile('Xfile2', 'b')) + set fenc=ucs-2le + w! Xtest3 + call assert_equal([utf16le_bom .. "u\nc\ns\n-\n2\nl\ne\n", "\n"], + \ readfile('Xtest3', 'b')) + + " Check ucs-4 BOM + %bw! + call writefile([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n"], 'Xtest1') + edit! Xtest1 + call assert_equal('ucs-4', &fileencoding) + call assert_equal(1, &bomb) + call assert_equal('ucs-4', getline(1)) + set fenc=latin1 + write! Xfile2 + call assert_equal(["ucs-4", ''], readfile('Xfile2', 'b')) + set fenc=ucs-4 + w! Xtest3 + call assert_equal([utf32be_bom .. "\n\n\nu\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\n", ''], readfile('Xtest3', 'b')) + + " Check ucs-4le BOM + %bw! + call writefile([utf32le_bom .. "u\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\nl\n\n\ne\n\n\n"], 'Xtest1') + " Need to add three NUL bytes after the NL byte + call writefile(0z000000, 'Xtest1', 'a') + edit! Xtest1 + call assert_equal('ucs-4le', &fileencoding) + call assert_equal(1, &bomb) + call assert_equal('ucs-4le', getline(1)) + set fenc=latin1 + write! Xfile2 + call assert_equal(["ucs-4le", ''], readfile('Xfile2', 'b')) + set fenc=ucs-4le + w! Xtest3 + call assert_equal([utf32le_bom .. "u\n\n\nc\n\n\ns\n\n\n-\n\n\n4\n\n\nl\n\n\ne\n\n\n", "\n\n\n"], readfile('Xtest3', 'b')) + + set cpoptions-=S + let &fileencoding = save_fileencoding + call delete('Xtest1') + call delete('Xfile2') + call delete('Xtest3') + %bw! +endfunc + +func Test_read_write_bin() + " write file missing EOL + call writefile(['noeol'], "XNoEolSetEol", 'bS') + call assert_equal(0z6E6F656F6C, readfile('XNoEolSetEol', 'B')) + + " when file is read 'eol' is off + set nofixeol + e! ++ff=unix XNoEolSetEol + call assert_equal(0, &eol) + + " writing with 'eol' set adds the newline + setlocal eol + w + call assert_equal(0z6E6F656F6C0A, readfile('XNoEolSetEol', 'B')) + + call delete('XNoEolSetEol') + set ff& + bwipe! XNoEolSetEol +endfunc + " Check that buffer is written before triggering QuitPre func Test_wq_quitpre_autocommand() edit Xsomefile diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c index 5fec41f9a5..6b889cf97c 100644 --- a/src/nvim/tui/input.c +++ b/src/nvim/tui/input.c @@ -227,7 +227,7 @@ static void forward_modified_utf8(TermInput *input, TermKeyKey *key) && !(key->modifiers & TERMKEY_KEYMOD_SHIFT) && ASCII_ISUPPER(key->code.codepoint)) { assert(len <= 62); - // Make remove for the S- + // Make room for the S- memmove(buf + 3, buf + 1, len - 1); buf[1] = 'S'; buf[2] = '-'; @@ -315,7 +315,6 @@ static TermKeyResult tk_getkey(TermKey *tk, TermKeyKey *key, bool force) return force ? termkey_getkey_force(tk, key) : termkey_getkey(tk, key); } -static void tinput_timer_cb(TimeWatcher *watcher, void *data); static void tk_getkeys(TermInput *input, bool force) { diff --git a/src/nvim/types.h b/src/nvim/types.h index 604155c33e..73cd2204d6 100644 --- a/src/nvim/types.h +++ b/src/nvim/types.h @@ -32,4 +32,6 @@ typedef enum { kTrue = 1, } TriState; +typedef struct Decoration Decoration; + #endif // NVIM_TYPES_H diff --git a/src/nvim/undo.c b/src/nvim/undo.c index d18f35a43a..2d8df4cad8 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -2633,6 +2633,10 @@ static void u_undo_end(bool did_undo, bool absolute, bool quiet) } } + if (VIsual_active) { + check_pos(curbuf, &VIsual); + } + smsg_attr_keep(0, _("%" PRId64 " %s; %s #%" PRId64 " %s"), u_oldcount < 0 ? (int64_t)-u_oldcount : (int64_t)u_oldcount, diff --git a/src/nvim/version.c b/src/nvim/version.c index 5e2a81795a..71cca52773 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -2093,7 +2093,7 @@ void list_in_columns(char_u **items, int size, int current) // The rightmost column doesn't need a separator. // Sacrifice it to fit in one more column if possible. - int ncol = (int)(Columns + 1) / width; + int ncol = (Columns + 1) / width; int nrow = item_count / ncol + (item_count % ncol ? 1 : 0); int cur_row = 1; diff --git a/src/nvim/viml/parser/expressions.c b/src/nvim/viml/parser/expressions.c index ba6cfab98b..8a14710351 100644 --- a/src/nvim/viml/parser/expressions.c +++ b/src/nvim/viml/parser/expressions.c @@ -1536,7 +1536,7 @@ static inline void east_set_error(const ParserState *const pstate, ExprASTError /* TODO(ZyX-I): Extend syntax to allow ${expr}. This is needed to */ \ /* handle environment variables like those bash uses for */ \ /* `export -f`: their names consist not only of alphanumeric */ \ - /* characetrs. */ \ + /* characters. */ \ case kExprNodeComplexIdentifier: \ case kExprNodePlainIdentifier: \ case kExprNodeCurlyBracesIdentifier: { \ diff --git a/src/nvim/viml/parser/expressions.h b/src/nvim/viml/parser/expressions.h index fe9327b27d..9d0bc9d468 100644 --- a/src/nvim/viml/parser/expressions.h +++ b/src/nvim/viml/parser/expressions.h @@ -57,7 +57,7 @@ typedef enum { } LexExprTokenType; typedef enum { - kExprCmpEqual, ///< Equality, unequality. + kExprCmpEqual, ///< Equality, inequality. kExprCmpMatches, ///< Matches regex, not matches regex. kExprCmpGreater, ///< `>` or `<=` kExprCmpGreaterOrEqual, ///< `>=` or `<`. diff --git a/src/nvim/window.c b/src/nvim/window.c index 1e747a67e9..e09af7a7bb 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -304,7 +304,7 @@ newwindow: newtab = curtab; goto_tabpage_tp(oldtab, true, true); if (curwin == wp) { - win_close(curwin, false); + win_close(curwin, false, false); } if (valid_tabpage(newtab)) { goto_tabpage_tp(newtab, true, true); @@ -449,7 +449,7 @@ wingotofile: RESET_BINDING(curwin); if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) { // Failed to open the file, close the window opened for it. - win_close(curwin, false); + win_close(curwin, false, false); goto_tabpage_win(oldtab, oldwin); } else if (nchar == 'F' && lnum >= 0) { curwin->w_cursor.lnum = lnum; @@ -577,18 +577,19 @@ static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, int64_t Pren void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err) { - win_T *win = find_window_by_handle(window, err), *save_curwin = curwin; + win_T *win = find_window_by_handle(window, err); buf_T *buf = find_buffer_by_handle(buffer, err); - tabpage_T *tab = win_find_tabpage(win), *save_curtab = curtab; + tabpage_T *tab = win_find_tabpage(win); if (!win || !buf) { return; } - if (noautocmd) { block_autocmds(); } - if (switch_win_noblock(&save_curwin, &save_curtab, win, tab, false) == FAIL) { + + switchwin_T switchwin; + if (switch_win_noblock(&switchwin, win, tab, false) == FAIL) { api_set_error(err, kErrorTypeException, "Failed to switch to window %d", @@ -608,7 +609,7 @@ void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err) // So do it now. validate_cursor(); - restore_win_noblock(save_curwin, save_curtab, false); + restore_win_noblock(&switchwin, false); if (noautocmd) { unblock_autocmds(); } @@ -1481,8 +1482,6 @@ static void win_init(win_T *newp, win_T *oldp, int flags) copyFoldingState(oldp, newp); win_init_some(newp, oldp); - - didset_window_options(newp); } /* @@ -2289,7 +2288,7 @@ void close_windows(buf_T *buf, int keep_curwin) for (win_T *wp = firstwin; wp != NULL && !ONE_WINDOW;) { if (wp->w_buffer == buf && (!keep_curwin || wp != curwin) && !(wp->w_closing || wp->w_buffer->b_locked > 0)) { - if (win_close(wp, false) == FAIL) { + if (win_close(wp, false, false) == FAIL) { // If closing the window fails give up, to avoid looping forever. break; } @@ -2367,6 +2366,22 @@ bool last_nonfloat(win_T *wp) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT return wp != NULL && firstwin == wp && !(wp->w_next && !wp->w_floating); } +/// Check if floating windows can be closed. +/// +/// @return true if all floating windows can be closed +static bool can_close_floating_windows(tabpage_T *tab) +{ + FOR_ALL_WINDOWS_IN_TAB(wp, tab) { + buf_T *buf = wp->w_buffer; + int need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); + + if (need_hide && !buf_hide(buf)) { + return false; + } + } + return true; +} + /// Close the possibly last window in a tab page. /// /// @param win window to close @@ -2431,7 +2446,7 @@ static bool close_last_window_tabpage(win_T *win, bool free_buf, tabpage_T *prev // // Called by :quit, :close, :xit, :wq and findtag(). // Returns FAIL when the window was not closed. -int win_close(win_T *win, bool free_buf) +int win_close(win_T *win, bool free_buf, bool force) { win_T *wp; bool other_buffer = false; @@ -2461,9 +2476,18 @@ int win_close(win_T *win, bool free_buf) } if ((firstwin == win && lastwin_nofloating() == win) && lastwin->w_floating) { - // TODO(bfredl): we might close the float also instead - emsg(e_floatonly); - return FAIL; + if (force || can_close_floating_windows(curtab)) { + win_T *nextwp; + for (win_T *wpp = firstwin; wpp != NULL; wpp = nextwp) { + nextwp = wpp->w_next; + if (wpp->w_floating) { + win_close(wpp, free_buf, force); + } + } + } else { + emsg(e_floatonly); + return FAIL; + } } // When closing the last window in a tab page first go to another tab page @@ -3055,9 +3079,21 @@ static frame_T *win_altframe(win_T *win, tabpage_T *tp) return frp->fr_prev; } + // By default the next window will get the space that was abandoned by this + // window frame_T *target_fr = frp->fr_next; frame_T *other_fr = frp->fr_prev; - if (p_spr || p_sb) { + + // If this is part of a column of windows and 'splitbelow' is true then the + // previous window will get the space. + if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_COL && p_sb) { + target_fr = frp->fr_prev; + other_fr = frp->fr_next; + } + + // If this is part of a row of windows, and 'splitright' is true then the + // previous window will get the space. + if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW && p_spr) { target_fr = frp->fr_prev; other_fr = frp->fr_next; } @@ -3610,7 +3646,9 @@ void close_others(int message, int forceit) continue; } } - win_close(wp, !buf_hide(wp->w_buffer) && !bufIsChanged(wp->w_buffer)); + win_close(wp, + !buf_hide(wp->w_buffer) && !bufIsChanged(wp->w_buffer), + false); } if (message && !ONE_WINDOW) { @@ -5795,7 +5833,6 @@ void win_drag_vsep_line(win_T *dragwin, int offset) } fr = curfr; // put fr at window that grows } - assert(fr); // Not enough room if (room < offset) { @@ -5808,7 +5845,9 @@ void win_drag_vsep_line(win_T *dragwin, int offset) } if (fr == NULL) { - return; // Safety check, should not happen. + // This can happen when calling win_move_separator() on the rightmost + // window. Just don't do anything. + return; } // grow frame fr by offset lines @@ -6630,20 +6669,27 @@ static win_T *get_snapshot_focus(int idx) /// triggered, another tabpage access is limited. /// /// @return FAIL if switching to "win" failed. -int switch_win(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, - bool no_display) +int switch_win(switchwin_T *switchwin, win_T *win, tabpage_T *tp, bool no_display) { block_autocmds(); - return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display); + return switch_win_noblock(switchwin, win, tp, no_display); } // As switch_win() but without blocking autocommands. -int switch_win_noblock(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, tabpage_T *tp, - bool no_display) +int switch_win_noblock(switchwin_T *switchwin, win_T *win, tabpage_T *tp, bool no_display) { - *save_curwin = curwin; + memset(switchwin, 0, sizeof(switchwin_T)); + switchwin->sw_curwin = curwin; + if (win == curwin) { + switchwin->sw_same_win = true; + } else { + // Disable Visual selection, because redrawing may fail. + switchwin->sw_visual_active = VIsual_active; + VIsual_active = false; + } + if (tp != NULL) { - *save_curtab = curtab; + switchwin->sw_curtab = curtab; if (no_display) { curtab->tp_firstwin = firstwin; curtab->tp_lastwin = lastwin; @@ -6665,28 +6711,33 @@ int switch_win_noblock(win_T **save_curwin, tabpage_T **save_curtab, win_T *win, // Restore current tabpage and window saved by switch_win(), if still valid. // When "no_display" is true the display won't be affected, no redraw is // triggered. -void restore_win(win_T *save_curwin, tabpage_T *save_curtab, bool no_display) +void restore_win(switchwin_T *switchwin, bool no_display) { - restore_win_noblock(save_curwin, save_curtab, no_display); + restore_win_noblock(switchwin, no_display); unblock_autocmds(); } // As restore_win() but without unblocking autocommands. -void restore_win_noblock(win_T *save_curwin, tabpage_T *save_curtab, bool no_display) +void restore_win_noblock(switchwin_T *switchwin, bool no_display) { - if (save_curtab != NULL && valid_tabpage(save_curtab)) { + if (switchwin->sw_curtab != NULL && valid_tabpage(switchwin->sw_curtab)) { if (no_display) { curtab->tp_firstwin = firstwin; curtab->tp_lastwin = lastwin; - curtab = save_curtab; + curtab = switchwin->sw_curtab; firstwin = curtab->tp_firstwin; lastwin = curtab->tp_lastwin; } else { - goto_tabpage_tp(save_curtab, false, false); + goto_tabpage_tp(switchwin->sw_curtab, false, false); } } - if (win_valid(save_curwin)) { - curwin = save_curwin; + + if (!switchwin->sw_same_win) { + VIsual_active = switchwin->sw_visual_active; + } + + if (win_valid(switchwin->sw_curwin)) { + curwin = switchwin->sw_curwin; curbuf = curwin->w_buffer; } // If called by win_execute() and executing the command changed the diff --git a/src/nvim/window.h b/src/nvim/window.h index 7e465a9f08..e2fd2c515d 100644 --- a/src/nvim/window.h +++ b/src/nvim/window.h @@ -4,6 +4,7 @@ #include <stdbool.h> #include "nvim/buffer_defs.h" +#include "nvim/mark.h" // Values for file_name_in_line() #define FNAME_MESS 1 // give error message @@ -32,6 +33,38 @@ #define MIN_COLUMNS 12 // minimal columns for screen #define MIN_LINES 2 // minimal lines for screen +/// Structure used by switch_win() to pass values to restore_win() +typedef struct { + win_T *sw_curwin; + tabpage_T *sw_curtab; + bool sw_same_win; ///< VIsual_active was not reset + bool sw_visual_active; +} switchwin_T; + +/// Execute a block of code in the context of window `wp` in tabpage `tp`. +/// Ensures the status line is redrawn and cursor position is valid if it is moved. +#define WIN_EXECUTE(wp, tp, block) \ + do { \ + win_T *const wp_ = (wp); \ + const pos_T curpos_ = wp_->w_cursor; \ + switchwin_T switchwin_; \ + if (switch_win_noblock(&switchwin_, wp_, (tp), true) == OK) { \ + check_cursor(); \ + block; \ + } \ + restore_win_noblock(&switchwin_, true); \ + /* Update the status line if the cursor moved. */ \ + if (win_valid(wp_) && !equalpos(curpos_, wp_->w_cursor)) { \ + wp_->w_redr_status = true; \ + } \ + /* In case the command moved the cursor or changed the Visual area, */ \ + /* check it is valid. */ \ + check_cursor(); \ + if (VIsual_active) { \ + check_pos(curbuf, &VIsual); \ + } \ + } while (false) + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "window.h.generated.h" #endif diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua index 6c2c136edc..d64d324a88 100644 --- a/test/functional/api/command_spec.lua +++ b/test/functional/api/command_spec.lua @@ -16,8 +16,8 @@ local feed = helpers.feed local funcs = helpers.funcs describe('nvim_get_commands', function() - local cmd_dict = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='echo "Hello World"', name='Hello', nargs='1', range=NIL, register=false, script_id=0, } - local cmd_dict2 = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='pwd', name='Pwd', nargs='?', range=NIL, register=false, script_id=0, } + local cmd_dict = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='echo "Hello World"', name='Hello', nargs='1', range=NIL, register=false, keepscript=false, script_id=0, } + local cmd_dict2 = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='pwd', name='Pwd', nargs='?', range=NIL, register=false, keepscript=false, script_id=0, } before_each(clear) it('gets empty list if no commands were defined', function() @@ -59,11 +59,11 @@ describe('nvim_get_commands', function() end) it('gets various command attributes', function() - local cmd0 = { addr='arguments', bang=false, bar=false, complete='dir', complete_arg=NIL, count='10', definition='pwd <args>', name='TestCmd', nargs='1', range='10', register=false, script_id=0, } - local cmd1 = { addr=NIL, bang=false, bar=false, complete='custom', complete_arg='ListUsers', count=NIL, definition='!finger <args>', name='Finger', nargs='+', range=NIL, register=false, script_id=1, } - local cmd2 = { addr=NIL, bang=true, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253R2_foo(<q-args>)', name='Cmd2', nargs='*', range=NIL, register=false, script_id=2, } - local cmd3 = { addr=NIL, bang=false, bar=true, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253R3_ohyeah()', name='Cmd3', nargs='0', range=NIL, register=false, script_id=3, } - local cmd4 = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253R4_just_great()', name='Cmd4', nargs='0', range=NIL, register=true, script_id=4, } + local cmd0 = { addr='arguments', bang=false, bar=false, complete='dir', complete_arg=NIL, count='10', definition='pwd <args>', name='TestCmd', nargs='1', range='10', register=false, keepscript=false, script_id=0, } + local cmd1 = { addr=NIL, bang=false, bar=false, complete='custom', complete_arg='ListUsers', count=NIL, definition='!finger <args>', name='Finger', nargs='+', range=NIL, register=false, keepscript=false, script_id=1, } + local cmd2 = { addr=NIL, bang=true, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253R2_foo(<q-args>)', name='Cmd2', nargs='*', range=NIL, register=false, keepscript=false, script_id=2, } + local cmd3 = { addr=NIL, bang=false, bar=true, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253R3_ohyeah()', name='Cmd3', nargs='0', range=NIL, register=false, keepscript=false, script_id=3, } + local cmd4 = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253R4_just_great()', name='Cmd4', nargs='0', range=NIL, register=true, keepscript=false, script_id=4, } source([[ command -complete=custom,ListUsers -nargs=+ Finger !finger <args> ]]) diff --git a/test/functional/api/extmark_spec.lua b/test/functional/api/extmark_spec.lua index a8f538b951..3f96efd4ef 100644 --- a/test/functional/api/extmark_spec.lua +++ b/test/functional/api/extmark_spec.lua @@ -110,6 +110,22 @@ describe('API/extmarks', function() pcall_err(set_extmark, ns, marks[2], 0, 0, { end_col = 1, end_row = 1 })) end) + it("can end extranges past final newline when strict mode is false", function() + set_extmark(ns, marks[1], 0, 0, { + end_col = 1, + end_row = 1, + strict = false, + }) + end) + + it("can end extranges past final column when strict mode is false", function() + set_extmark(ns, marks[1], 0, 0, { + end_col = 6, + end_row = 0, + strict = false, + }) + end) + it('adds, updates and deletes marks', function() local rv = set_extmark(ns, marks[1], positions[1][1], positions[1][2]) eq(marks[1], rv) @@ -1430,7 +1446,60 @@ describe('API/extmarks', function() end_col = 0, end_line = 1 }) - eq({ {1, 0, 0, { end_col = 0, end_row = 1 }} }, get_extmarks(ns, 0, -1, {details=true})) + eq({ {1, 0, 0, { + end_col = 0, + end_row = 1, + right_gravity = true, + end_right_gravity = false, + }} }, get_extmarks(ns, 0, -1, {details=true})) + end) + + it('can get details', function() + set_extmark(ns, marks[1], 0, 0, { + end_col = 0, + end_row = 1, + right_gravity = false, + end_right_gravity = true, + priority = 0, + hl_eol = true, + hl_mode = "blend", + hl_group = "String", + virt_text = { { "text", "Statement" } }, + virt_text_pos = "right_align", + virt_text_hide = true, + virt_lines = { { { "lines", "Statement" } }}, + virt_lines_above = true, + virt_lines_leftcol = true, + }) + set_extmark(ns, marks[2], 0, 0, { + priority = 0, + virt_text = { { "text", "Statement" } }, + virt_text_win_col = 1, + }) + eq({0, 0, { + end_col = 0, + end_row = 1, + right_gravity = false, + end_right_gravity = true, + priority = 0, + hl_eol = true, + hl_mode = "blend", + hl_group = "String", + virt_text = { { "text", "Statement" } }, + virt_text_pos = "right_align", + virt_text_hide = true, + virt_lines = { { { "lines", "Statement" } }}, + virt_lines_above = true, + virt_lines_leftcol = true, + } }, get_extmark_by_id(ns, marks[1], { details = true })) + eq({0, 0, { + right_gravity = true, + priority = 0, + virt_text = { { "text", "Statement" } }, + virt_text_hide = false, + virt_text_pos = "win_col", + virt_text_win_col = 1, + } }, get_extmark_by_id(ns, marks[2], { details = true })) end) end) diff --git a/test/functional/api/highlight_spec.lua b/test/functional/api/highlight_spec.lua index 21e3094f8e..46a3798dc4 100644 --- a/test/functional/api/highlight_spec.lua +++ b/test/functional/api/highlight_spec.lua @@ -3,6 +3,7 @@ local clear, nvim = helpers.clear, helpers.nvim local Screen = require('test.functional.ui.screen') local eq, eval = helpers.eq, helpers.eval local command = helpers.command +local exec_capture = helpers.exec_capture local meths = helpers.meths local funcs = helpers.funcs local pcall_err = helpers.pcall_err @@ -194,10 +195,12 @@ describe("API: set highlight", function() reverse = true, undercurl = true, underline = true, + strikethrough = true, cterm = { italic = true, reverse = true, undercurl = true, + strikethrough = true, } } local highlight3_result_gui = { @@ -208,6 +211,7 @@ describe("API: set highlight", function() reverse = true, undercurl = true, underline = true, + strikethrough = true, } local highlight3_result_cterm = { background = highlight_color.ctermbg, @@ -215,6 +219,7 @@ describe("API: set highlight", function() italic = true, reverse = true, undercurl = true, + strikethrough = true, } local function get_ns() @@ -252,4 +257,23 @@ describe("API: set highlight", function() eq(highlight3_result_gui, meths.get_hl_by_name('Test_hl', true)) eq(highlight3_result_cterm, meths.get_hl_by_name('Test_hl', false)) end) + + it ("can set a highlight in the global namespace", function() + meths.set_hl(0, 'Test_hl', highlight2_config) + eq('Test_hl xxx cterm=underline,reverse ctermfg=8 ctermbg=15 gui=underline,reverse', + exec_capture('highlight Test_hl')) + + meths.set_hl(0, 'Test_hl', { background = highlight_color.bg }) + eq('Test_hl xxx guibg=#0032aa', + exec_capture('highlight Test_hl')) + + meths.set_hl(0, 'Test_hl2', highlight3_config) + eq('Test_hl2 xxx cterm=undercurl,italic,reverse,strikethrough ctermfg=8 ctermbg=15 gui=bold,underline,undercurl,italic,reverse,strikethrough guifg=#ff0000 guibg=#0032aa', + exec_capture('highlight Test_hl2')) + + -- Colors are stored exactly as they are defined. + meths.set_hl(0, 'Test_hl3', { bg = 'reD', fg = 'bLue'}) + eq('Test_hl3 xxx guifg=bLue guibg=reD', + exec_capture('highlight Test_hl3')) + end) end) diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua index bc2759ade7..450a76ddac 100644 --- a/test/functional/api/keymap_spec.lua +++ b/test/functional/api/keymap_spec.lua @@ -820,6 +820,16 @@ describe('nvim_set_keymap, nvim_del_keymap', function() eq(99, exec_lua[[return SomeValue]]) end) + it('does not reset pum in lua mapping', function() + eq(0, exec_lua [[ + VisibleCount = 0 + vim.api.nvim_set_keymap ('i', '<F2>', '', {callback = function() VisibleCount = VisibleCount + vim.fn.pumvisible() end}) + return VisibleCount + ]]) + feed('i<C-X><C-V><F2><F2><esc>') + eq(2, exec_lua[[return VisibleCount]]) + end) + it('can overwrite lua mappings', function() eq(0, exec_lua [[ GlobalCount = 0 diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 22201e21a2..5a387f3deb 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -333,6 +333,7 @@ describe('API', function() describe('nvim_command_output', function() it('does not induce hit-enter prompt', function() + nvim("ui_attach", 80, 20, {}) -- Induce a hit-enter prompt use nvim_input (non-blocking). nvim('command', 'set cmdheight=1') nvim('input', [[:echo "hi\nhi2"<CR>]]) @@ -535,6 +536,31 @@ describe('API', function() end) end) + describe('nvim_set_current_dir', function() + local start_dir + + before_each(function() + clear() + funcs.mkdir("Xtestdir") + start_dir = funcs.getcwd() + end) + + after_each(function() + helpers.rmdir("Xtestdir") + end) + + it('works', function() + meths.set_current_dir("Xtestdir") + eq(funcs.getcwd(), start_dir .. helpers.get_pathsep() .. "Xtestdir") + end) + + it('sets previous directory', function() + meths.set_current_dir("Xtestdir") + meths.exec('cd -', false) + eq(funcs.getcwd(), start_dir) + end) + end) + describe('nvim_exec_lua', function() it('works', function() meths.exec_lua('vim.api.nvim_set_var("test", 3)', {}) @@ -872,6 +898,19 @@ describe('API', function() command('lockvar lua') eq('Key is locked: lua', pcall_err(meths.del_var, 'lua')) eq('Key is locked: lua', pcall_err(meths.set_var, 'lua', 1)) + + -- Check if autoload works properly + local pathsep = helpers.get_pathsep() + local xconfig = 'Xhome' .. pathsep .. 'Xconfig' + local xdata = 'Xhome' .. pathsep .. 'Xdata' + local autoload_folder = table.concat({xconfig, 'nvim', 'autoload'}, pathsep) + local autoload_file = table.concat({autoload_folder , 'testload.vim'}, pathsep) + mkdir_p(autoload_folder) + write_file(autoload_file , [[let testload#value = 2]]) + + clear{ args_rm={'-u'}, env={ XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata } } + eq(2, meths.get_var('testload#value')) + rmdir('Xhome') end) it('nvim_get_vvar, nvim_set_vvar', function() @@ -1093,7 +1132,20 @@ describe('API', function() eq({mode='n', blocking=false}, nvim("get_mode")) end) + it("during press-enter prompt without UI returns blocking=false", function() + eq({mode='n', blocking=false}, nvim("get_mode")) + command("echom 'msg1'") + command("echom 'msg2'") + command("echom 'msg3'") + command("echom 'msg4'") + command("echom 'msg5'") + eq({mode='n', blocking=false}, nvim("get_mode")) + nvim("input", ":messages<CR>") + eq({mode='n', blocking=false}, nvim("get_mode")) + end) + it("during press-enter prompt returns blocking=true", function() + nvim("ui_attach", 80, 20, {}) eq({mode='n', blocking=false}, nvim("get_mode")) command("echom 'msg1'") command("echom 'msg2'") @@ -1117,6 +1169,7 @@ describe('API', function() -- TODO: bug #6247#issuecomment-286403810 it("batched with input", function() + nvim("ui_attach", 80, 20, {}) eq({mode='n', blocking=false}, nvim("get_mode")) command("echom 'msg1'") command("echom 'msg2'") @@ -1367,18 +1420,18 @@ describe('API', function() end) describe('nvim_feedkeys', function() - it('CSI escaping', function() + it('K_SPECIAL escaping', function() local function on_setup() -- notice the special char(…) \xe2\80\xa6 nvim('feedkeys', ':let x1="…"\n', '', true) -- Both nvim_replace_termcodes and nvim_feedkeys escape \x80 local inp = helpers.nvim('replace_termcodes', ':let x2="…"<CR>', true, true, true) - nvim('feedkeys', inp, '', true) -- escape_csi=true + nvim('feedkeys', inp, '', true) -- escape_ks=true - -- nvim_feedkeys with CSI escaping disabled + -- nvim_feedkeys with K_SPECIAL escaping disabled inp = helpers.nvim('replace_termcodes', ':let x3="…"<CR>', true, true, true) - nvim('feedkeys', inp, '', false) -- escape_csi=false + nvim('feedkeys', inp, '', false) -- escape_ks=false helpers.stop() end @@ -2551,6 +2604,34 @@ describe('API', function() 'Should be truncated%<', { maxwidth = 15 })) end) + it('supports ASCII fillchar', function() + eq({ str = 'a~~~b', width = 5 }, + meths.eval_statusline('a%=b', { fillchar = '~', maxwidth = 5 })) + end) + it('supports single-width multibyte fillchar', function() + eq({ str = 'a━━━b', width = 5 }, + meths.eval_statusline('a%=b', { fillchar = '━', maxwidth = 5 })) + end) + it('rejects double-width fillchar', function() + eq('fillchar must be a single-width character', + pcall_err(meths.eval_statusline, '', { fillchar = '哦' })) + end) + it('rejects control character fillchar', function() + eq('fillchar must be a single-width character', + pcall_err(meths.eval_statusline, '', { fillchar = '\a' })) + end) + it('rejects multiple-character fillchar', function() + eq('fillchar must be a single-width character', + pcall_err(meths.eval_statusline, '', { fillchar = 'aa' })) + end) + it('rejects empty string fillchar', function() + eq('fillchar must be a single-width character', + pcall_err(meths.eval_statusline, '', { fillchar = '' })) + end) + it('rejects non-string fillchar', function() + eq('fillchar must be a single-width character', + pcall_err(meths.eval_statusline, '', { fillchar = 1 })) + end) describe('highlight parsing', function() it('works', function() eq({ diff --git a/test/functional/autocmd/focus_spec.lua b/test/functional/autocmd/focus_spec.lua index 3f9a0ad09b..e3c9e1f9ee 100644 --- a/test/functional/autocmd/focus_spec.lua +++ b/test/functional/autocmd/focus_spec.lua @@ -56,7 +56,7 @@ describe('autoread TUI FocusGained/FocusLost', function() line 3 | line 4 | {5:xtest-foo }| - "xtest-foo" 4L, 28C | + "xtest-foo" 4L, 28B | {3:-- TERMINAL --} | ]]} end) diff --git a/test/functional/autocmd/recording_spec.lua b/test/functional/autocmd/recording_spec.lua index 81152758de..b9aec774f1 100644 --- a/test/functional/autocmd/recording_spec.lua +++ b/test/functional/autocmd/recording_spec.lua @@ -11,7 +11,7 @@ describe('RecordingEnter', function() source_vim [[ let g:recorded = 0 autocmd RecordingEnter * let g:recorded += 1 - execute "normal! qqyyq" + call feedkeys("qqyyq", 'xt') ]] eq(1, eval('g:recorded')) end) @@ -20,7 +20,7 @@ describe('RecordingEnter', function() source_vim [[ let g:recording = '' autocmd RecordingEnter * let g:recording = reg_recording() - execute "normal! qqyyq" + call feedkeys("qqyyq", 'xt') ]] eq('q', eval('g:recording')) end) @@ -32,7 +32,7 @@ describe('RecordingLeave', function() source_vim [[ let g:recorded = 0 autocmd RecordingLeave * let g:recorded += 1 - execute "normal! qqyyq" + call feedkeys("qqyyq", 'xt') ]] eq(1, eval('g:recorded')) end) @@ -43,10 +43,30 @@ describe('RecordingLeave', function() let g:recording = '' autocmd RecordingLeave * let g:recording = reg_recording() autocmd RecordingLeave * let g:recorded = reg_recorded() - execute "normal! qqyyq" + call feedkeys("qqyyq", 'xt') ]] eq('q', eval 'g:recording') eq('', eval 'g:recorded') eq('q', eval 'reg_recorded()') end) + + it('populates v:event', function() + source_vim [[ + let g:regname = '' + let g:regcontents = '' + autocmd RecordingLeave * let g:regname = v:event.regname + autocmd RecordingLeave * let g:regcontents = v:event.regcontents + call feedkeys("qqyyq", 'xt') + ]] + eq('q', eval 'g:regname') + eq('yy', eval 'g:regcontents') + end) + + it('resets v:event', function() + source_vim [[ + autocmd RecordingLeave * let g:event = v:event + call feedkeys("qqyyq", 'xt') + ]] + eq(0, eval 'len(v:event)') + end) end) diff --git a/test/functional/core/channels_spec.lua b/test/functional/core/channels_spec.lua index 93dec9fb35..c28300f0f4 100644 --- a/test/functional/core/channels_spec.lua +++ b/test/functional/core/channels_spec.lua @@ -100,6 +100,38 @@ describe('channels', function() eq({"notification", "exit", {3,0}}, next_msg()) end) + it('can use stdio channel and on_print callback', function() + source([[ + let g:job_opts = { + \ 'on_stdout': function('OnEvent'), + \ 'on_stderr': function('OnEvent'), + \ 'on_exit': function('OnEvent'), + \ } + ]]) + meths.set_var("nvim_prog", nvim_prog) + meths.set_var("code", [[ + function! OnStdin(id, data, event) dict + echo string([a:id, a:data, a:event]) + if a:data == [''] + quit + endif + endfunction + function! OnPrint(text) dict + call chansend(g:x, ['OnPrint:' .. a:text]) + endfunction + let g:x = stdioopen({'on_stdin': funcref('OnStdin'), 'on_print':'OnPrint'}) + call chansend(x, "hello") + ]]) + command("let g:id = jobstart([ g:nvim_prog, '-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--headless', '--cmd', g:code], g:job_opts)") + local id = eval("g:id") + ok(id > 0) + + eq({ "notification", "stdout", {id, { "hello" } } }, next_msg()) + + command("call chansend(id, 'howdy')") + eq({"notification", "stdout", {id, {"OnPrint:[1, ['howdy'], 'stdin']"}}}, next_msg()) + end) + local function expect_twoline(id, stream, line1, line2, nobr) local msg = next_msg() local joined = nobr and {line1..line2} or {line1, line2} diff --git a/test/functional/core/startup_spec.lua b/test/functional/core/startup_spec.lua index 4220d68ee1..2fa84e8313 100644 --- a/test/functional/core/startup_spec.lua +++ b/test/functional/core/startup_spec.lua @@ -224,6 +224,23 @@ describe('startup', function() end end) + it('-e sets ex mode', function() + local screen = Screen.new(25, 3) + clear('-e') + screen:attach() + -- Verify we set the proper mode both before and after :vi. + feed("put =mode(1)<CR>vi<CR>:put =mode(1)<CR>") + screen:expect([[ + cv | + ^n | + :put =mode(1) | + ]]) + + eq('cv\n', + funcs.system({nvim_prog, '-n', '-es' }, + { 'put =mode(1)', 'print', '' })) + end) + it('fails on --embed with -es/-Es', function() matches('nvim[.exe]*: %-%-embed conflicts with %-es/%-Es', funcs.system({nvim_prog, '--embed', '-es' })) @@ -651,11 +668,7 @@ describe('runtime:', function() mkdir_p(ftdetect_folder) write_file(ftdetect_file , [[vim.g.lua_ftdetect = 1]]) - -- TODO(shadmansaleh): Figure out why this test fails without - -- setting VIMRUNTIME - clear{ args_rm={'-u'}, env={XDG_CONFIG_HOME=xconfig, - XDG_DATA_HOME=xdata, - VIMRUNTIME='runtime/'}} + clear{ args_rm={'-u'}, env=xenv } eq(1, eval('g:lua_ftdetect')) rmdir(ftdetect_folder) diff --git a/test/functional/editor/K_spec.lua b/test/functional/editor/K_spec.lua index 40f36491e4..8ad81ac3d6 100644 --- a/test/functional/editor/K_spec.lua +++ b/test/functional/editor/K_spec.lua @@ -33,6 +33,29 @@ describe('K', function() feed('i'..test_file..'<ESC>K') retry(nil, nil, function() eq(1, eval('filereadable("'..test_file..'")')) end) eq({'fnord'}, eval("readfile('"..test_file.."')")) + -- Confirm that Neovim is still in terminal mode after K is pressed (#16692). + helpers.sleep(500) + eq('t', eval('mode()')) + feed('<space>') -- Any key, not just <space>, can be used here to escape. + eq('n', eval('mode()')) + end) + + it("<esc> kills the buffer for a running 'keywordprg' command", function() + helpers.source('set keywordprg=less') + eval('writefile(["hello", "world"], "' .. test_file .. '")') + feed('i' .. test_file .. '<esc>K') + eq('t', eval('mode()')) + -- Confirm that an arbitrary keypress doesn't escape (i.e., the process is + -- still running). If the process were no longer running, an arbitrary + -- keypress would escape. + helpers.sleep(500) + feed('<space>') + eq('t', eval('mode()')) + -- Confirm that <esc> kills the buffer for the running command. + local bufnr = eval('bufnr()') + feed('<esc>') + eq('n', eval('mode()')) + helpers.neq(bufnr, eval('bufnr()')) end) end) diff --git a/test/functional/editor/meta_key_spec.lua b/test/functional/editor/meta_key_spec.lua index 2280f5bb24..8efcc81616 100644 --- a/test/functional/editor/meta_key_spec.lua +++ b/test/functional/editor/meta_key_spec.lua @@ -27,6 +27,14 @@ describe('meta-keys #8226 #13042', function() command('nnoremap <A-j> Aalt-j<Esc>') feed('<A-j><M-l>') expect('llo<ESC>;<ESC>;alt-jmeta-l') + -- Unmapped ALT-chord with characters containing K_SPECIAL bytes + command('nnoremap … A…<Esc>') + feed('<A-…><M-…>') + expect('llo<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…') + command("execute 'nnoremap' nr2char(0x40000000) 'AMAX<Esc>'") + command("call nvim_input('<A-'.nr2char(0x40000000).'>')") + command("call nvim_input('<M-'.nr2char(0x40000000).'>')") + expect('llo<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…<ESC>MAX<ESC>MAX') end) it('ALT/META, visual-mode', function() @@ -36,13 +44,20 @@ describe('meta-keys #8226 #13042', function() expect('peach') -- Unmapped ALT-chord resolves isolated (non-ALT) ESC mapping. #13086 #15869 command('vnoremap <ESC> A<lt>ESC>') - feed('viw<A-;><ESC>viw<M-;><ESC>') + feed('viw<A-;><Esc>viw<M-;><Esc>') expect('peach<ESC>;<ESC>;') -- Mapped ALT-chord behaves as mapped. command('vnoremap <M-l> Ameta-l<Esc>') command('vnoremap <A-j> Aalt-j<Esc>') feed('viw<A-j>viw<M-l>') expect('peach<ESC>;<ESC>;alt-jmeta-l') + -- Unmapped ALT-chord with characters containing K_SPECIAL bytes + feed('viw<A-…><Esc>viw<M-…><Esc>') + expect('peach<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…') + command("execute 'inoremap' nr2char(0x40000000) 'MAX'") + command("call nvim_input('viw<A-'.nr2char(0x40000000).'><Esc>')") + command("call nvim_input('viw<M-'.nr2char(0x40000000).'><Esc>')") + expect('peach<ESC>;<ESC>;alt-jmeta-l<ESC>…<ESC>…<ESC>MAX<ESC>MAX') end) it('ALT/META insert-mode', function() @@ -89,4 +104,20 @@ describe('meta-keys #8226 #13042', function() eq({ 0, 2, 1, 0, }, funcs.getpos('.')) eq('nt', eval('mode(1)')) end) + + it('ALT/META when recording a macro #13235', function() + feed('ifoo<CR>bar<CR>baz<Esc>gg0') + -- <M-"> is reinterpreted as <Esc>" + feed('qrviw"ayC// This is some text: <M-">apq') + expect([[ + // This is some text: foo + bar + baz]]) + -- Should not insert an extra double quote when replaying + feed('j0@rj0@@') + expect([[ + // This is some text: foo + // This is some text: bar + // This is some text: baz]]) + end) end) diff --git a/test/functional/editor/mode_insert_spec.lua b/test/functional/editor/mode_insert_spec.lua index f03508035d..528e228121 100644 --- a/test/functional/editor/mode_insert_spec.lua +++ b/test/functional/editor/mode_insert_spec.lua @@ -75,4 +75,57 @@ describe('insert-mode', function() expect('hello oooworld') end) end) + + describe('Ctrl-V', function() + it('supports entering the decimal value of a character', function() + feed('i<C-V>076<C-V>167') + expect('L§') + end) + + it('supports entering the octal value of a character with "o"', function() + feed('i<C-V>o114<C-V>o247<Esc>') + expect('L§') + end) + + it('supports entering the octal value of a character with "O"', function() + feed('i<C-V>O114<C-V>O247<Esc>') + expect('L§') + end) + + it('supports entering the hexadecimal value of a character with "x"', function() + feed('i<C-V>x4c<C-V>xA7<Esc>') + expect('L§') + end) + + it('supports entering the hexadecimal value of a character with "X"', function() + feed('i<C-V>X4c<C-V>XA7<Esc>') + expect('L§') + end) + + it('supports entering the hexadecimal value of a character with "u"', function() + feed('i<C-V>u25ba<C-V>u25C7<Esc>') + expect('►◇') + end) + + it('supports entering the hexadecimal value of a character with "U"', function() + feed('i<C-V>U0001f600<C-V>U0001F601<Esc>') + expect('😀😁') + end) + + it('entering character by value is interrupted by invalid character', function() + feed('i<C-V>76c<C-V>76<C-F2><C-V>u3c0j<C-V>u3c0<M-F3><C-V>U1f600j<C-V>U1f600<D-F4><Esc>') + expect('LcL<C-F2>πjπ<M-F3>😀j😀<D-F4>') + end) + + it('shows o, O, u, U, x, X, and digits with modifiers', function() + feed('i<C-V><M-o><C-V><D-o><C-V><M-O><C-V><D-O><Esc>') + expect('<M-o><D-o><M-O><D-O>') + feed('cc<C-V><M-u><C-V><D-u><C-V><M-U><C-V><D-U><Esc>') + expect('<M-u><D-u><M-U><D-U>') + feed('cc<C-V><M-x><C-V><D-x><C-V><M-X><C-V><D-X><Esc>') + expect('<M-x><D-x><M-X><D-X>') + feed('cc<C-V><M-1><C-V><D-2><C-V><M-7><C-V><D-8><Esc>') + expect('<M-1><D-2><M-7><D-8>') + end) + end) end) diff --git a/test/functional/editor/put_spec.lua b/test/functional/editor/put_spec.lua index 26967ecbba..c367f8fdd0 100644 --- a/test/functional/editor/put_spec.lua +++ b/test/functional/editor/put_spec.lua @@ -64,7 +64,7 @@ describe('put command', function() -- one place to the right (unless we were at the end of the -- line when we pasted). if not (exception_table.undo_position and after_undo) then - eq(funcs.getcurpos(), init_cursorpos) + eq(init_cursorpos, funcs.getcurpos()) end end @@ -86,7 +86,7 @@ describe('put command', function() -- If we paste the ". register with a count we can't avoid -- changing this register, hence avoid this check. if not test.exception_table.dot_reg_changed then - eq(funcs.getreg('.'), orig_dotstr) + eq(orig_dotstr, funcs.getreg('.')) end -- Doing something, undoing it, and then redoing it should @@ -507,9 +507,11 @@ describe('put command', function() return function(exception_table, after_redo) test_expect(exception_table, after_redo) if selection_string then - eq(getreg('"'), selection_string) + if not conversion_table.put_backwards then + eq(selection_string, getreg('"')) + end else - eq(getreg('"'), 'test_string"') + eq('test_string"', getreg('"')) end end end @@ -714,7 +716,9 @@ describe('put command', function() expect_base, conversion_table) return function(exception_table, after_redo) test_expect(exception_table, after_redo) - eq(getreg('"'), 'Line of words 1\n') + if not conversion_table.put_backwards then + eq('Line of words 1\n', getreg('"')) + end end end local base_expect_string = [[ @@ -748,7 +752,9 @@ describe('put command', function() end, expect_base, conversion_table) return function(e,c) test_expect(e,c) - eq(getreg('"'), 'Lin\nLin') + if not conversion_table.put_backwards then + eq('Lin\nLin', getreg('"')) + end end end @@ -800,9 +806,9 @@ describe('put command', function() feed('u') -- Have to use feed('u') here to set curswant, because -- ex_undo() doesn't do that. - eq(funcs.getcurpos(), {0, 1, 1, 0, 1}) + eq({0, 1, 1, 0, 1}, funcs.getcurpos()) feed('<C-r>') - eq(funcs.getcurpos(), {0, 1, 1, 0, 1}) + eq({0, 1, 1, 0, 1}, funcs.getcurpos()) end end diff --git a/test/functional/ex_cmds/mksession_spec.lua b/test/functional/ex_cmds/mksession_spec.lua index 09eaa36686..aefcc53cab 100644 --- a/test/functional/ex_cmds/mksession_spec.lua +++ b/test/functional/ex_cmds/mksession_spec.lua @@ -5,6 +5,7 @@ local clear = helpers.clear local command = helpers.command local get_pathsep = helpers.get_pathsep local eq = helpers.eq +local neq = helpers.neq local funcs = helpers.funcs local matches = helpers.matches local pesc = helpers.pesc @@ -30,7 +31,8 @@ describe(':mksession', function() -- If the same :terminal is displayed in multiple windows, :mksession -- should restore it as such. - -- Create two windows showing the same :terminal buffer. + -- Create three windows: first two from top show same terminal, third - + -- another one (created earlier). command('terminal') command('split') command('terminal') @@ -43,8 +45,8 @@ describe(':mksession', function() -- Restore session. command('source '..session_file) - eq({2,2,4}, - {funcs.winbufnr(1), funcs.winbufnr(2), funcs.winbufnr(3)}) + eq(funcs.winbufnr(1), funcs.winbufnr(2)) + neq(funcs.winbufnr(1), funcs.winbufnr(3)) end) it('restores tab-local working directories', function() @@ -91,12 +93,7 @@ describe(':mksession', function() command('tabnext 1') eq(cwd_dir .. get_pathsep() .. tmpfile_base .. '1', funcs.expand('%:p')) command('tabnext 2') - -- :mksession stores paths using unix slashes, but Nvim doesn't adjust these - -- for absolute paths in all cases yet. Absolute paths are used in the - -- session file after :tcd, so we need to expect unix slashes here for now - -- eq(cwd_dir .. get_pathsep() .. tmpfile_base .. '2', funcs.expand('%:p')) - eq(cwd_dir:gsub([[\]], '/') .. '/' .. tmpfile_base .. '2', - funcs.expand('%:p')) + eq(cwd_dir .. get_pathsep() .. tmpfile_base .. '2', funcs.expand('%:p')) end) it('restores CWD for :terminal buffers #11288', function() diff --git a/test/functional/ex_cmds/script_spec.lua b/test/functional/ex_cmds/script_spec.lua index 0a772c559b..f249b9ba7c 100644 --- a/test/functional/ex_cmds/script_spec.lua +++ b/test/functional/ex_cmds/script_spec.lua @@ -62,10 +62,10 @@ describe('script_get-based command', function() -- Provider-based scripts test_garbage_exec('ruby', not missing_provider('ruby')) - test_garbage_exec('python', not missing_provider('python')) test_garbage_exec('python3', not missing_provider('python3')) -- Missing scripts + test_garbage_exec('python', false) test_garbage_exec('tcl', false) test_garbage_exec('mzscheme', false) test_garbage_exec('perl', false) diff --git a/test/functional/fixtures/autoload/provider/python.vim b/test/functional/fixtures/autoload/provider/python3.vim index d68360ac30..8ed4330a35 100644 --- a/test/functional/fixtures/autoload/provider/python.vim +++ b/test/functional/fixtures/autoload/provider/python3.vim @@ -1,6 +1,6 @@ " Dummy test provider, missing this required variable: " let g:loaded_brokenenabled_provider = 0 -function! provider#python#Call(method, args) +function! provider#python3#Call(method, args) return 42 endfunction diff --git a/test/functional/fixtures/lua/test_plug/submodule_empty/health.lua b/test/functional/fixtures/lua/test_plug/submodule_empty/health.lua new file mode 100644 index 0000000000..d2cf86e4f0 --- /dev/null +++ b/test/functional/fixtures/lua/test_plug/submodule_empty/health.lua @@ -0,0 +1,7 @@ +local M = {} + +M.check = function() + return {} +end + +return M diff --git a/test/functional/fixtures/pack/foo/start/bar/lua/baz-quux.lua b/test/functional/fixtures/pack/foo/start/bar/lua/baz-quux.lua new file mode 100644 index 0000000000..c1c33d787e --- /dev/null +++ b/test/functional/fixtures/pack/foo/start/bar/lua/baz-quux.lua @@ -0,0 +1 @@ +return {doit=function() return 9004 end} diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index f152a487af..1845786c4b 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -451,6 +451,7 @@ function module.new_argv(...) 'GCOV_ERROR_FILE', 'XDG_DATA_DIRS', 'TMPDIR', + 'VIMRUNTIME', }) do if not env_tbl[k] then env_tbl[k] = os.getenv(k) diff --git a/test/functional/legacy/003_cindent_spec.lua b/test/functional/legacy/003_cindent_spec.lua deleted file mode 100644 index 061904c42f..0000000000 --- a/test/functional/legacy/003_cindent_spec.lua +++ /dev/null @@ -1,4774 +0,0 @@ --- Test for 'cindent'. --- For new tests, consider putting them in test_cindent.vim. --- --- There are 50+ test command blocks (the stuff between STARTTEST and ENDTEST) --- in the original test. These have been converted to "it" test cases here. - -local helpers = require('test.functional.helpers')(after_each) -local feed, insert = helpers.feed, helpers.insert -local clear, feed_command, expect = helpers.clear, helpers.feed_command, helpers.expect - --- Inserts text as usual, and additionally positions the cursor on line 1 and --- sets 'cindent' and tab settings. (In the original "test3.in" the modeline at --- the top of the file takes care of this.) -local function insert_(content) - insert(content) - feed_command('1', 'set cin ts=4 sw=4') -end - --- luacheck: ignore 621 (Indentation) --- luacheck: ignore 613 (Trailing whitespace in a string) -describe('cindent', function() - before_each(clear) - - it('1 is working', function() - insert_([=[ - - /* start of AUTO matically checked vim: set ts=4 : */ - { - if (test) - cmd1; - cmd2; - } - - { - if (test) - cmd1; - else - cmd2; - } - - { - if (test) - { - cmd1; - cmd2; - } - } - - { - if (test) - { - cmd1; - else - } - } - - { - while (this) - if (test) - cmd1; - cmd2; - } - - { - while (this) - if (test) - cmd1; - else - cmd2; - } - - { - if (test) - { - cmd; - } - - if (test) - cmd; - } - - { - if (test) { - cmd; - } - - if (test) cmd; - } - - { - cmd1; - for (blah) - while (this) - if (test) - cmd2; - cmd3; - } - - { - cmd1; - for (blah) - while (this) - if (test) - cmd2; - cmd3; - - if (test) - { - cmd1; - cmd2; - cmd3; - } - } - - - /* Test for 'cindent' do/while mixed with if/else: */ - - { - do - if (asdf) - asdfasd; - while (cond); - - do - if (asdf) - while (asdf) - asdf; - while (asdf); - } - - /* Test for 'cindent' with two ) on a continuation line */ - { - if (asdfasdf;asldkfj asdlkfj as;ldkfj sal;d - aal;sdkjf ( ;asldfkja;sldfk - al;sdjfka ;slkdf ) sa;ldkjfsa dlk;) - line up here; - } - - - /* C++ tests: */ - - // foo() these three lines should remain in column 0 - // { - // } - - /* Test for continuation and unterminated lines: */ - { - i = 99 + 14325 + - 21345 + - 21345 + - 21345 + ( 21345 + - 21345) + - 2345 + - 1234; - c = 1; - } - - /* - testje for indent with empty line - - here */ - - { - if (testing && - not a joke || - line up here) - hay; - if (testing && - (not a joke || testing - )line up here) - hay; - if (testing && - (not a joke || testing - line up here)) - hay; - } - - - { - switch (c) - { - case xx: - do - if (asdf) - do - asdfasdf; - while (asdf); - else - asdfasdf; - while (cond); - case yy: - case xx: - case zz: - testing; - } - } - - { - if (cond) { - foo; - } - else - { - bar; - } - } - - { - if (alskdfj ;alsdkfjal;skdjf (;sadlkfsa ;dlkf j;alksdfj ;alskdjf - alsdkfj (asldk;fj - awith cino=(0 ;lf this one goes to below the paren with == - ;laksjfd ;lsakdjf ;alskdf asd) - asdfasdf;))) - asdfasdf; - } - - int - func(a, b) - int a; - int c; - { - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3) - ) - } - - { - while (asd) - { - if (asdf) - if (test) - if (that) - { - if (asdf) - do - cdasd; - while (as - df); - } - else - if (asdf) - asdf; - else - asdf; - asdf; - } - } - - { - s = "/*"; b = ';' - s = "/*"; b = ';'; - a = b; - } - - { - switch (a) - { - case a: - switch (t) - { - case 1: - cmd; - break; - case 2: - cmd; - break; - } - cmd; - break; - case b: - { - int i; - cmd; - } - break; - case c: { - int i; - cmd; - } - case d: if (cond && - test) { /* this line doesn't work right */ - int i; - cmd; - } - break; - } - } - - { - if (!(vim_strchr(p_cpo, CPO_BUFOPTGLOB) != NULL && entering) && - (bp_to->b_p_initialized || - (!entering && vim_strchr(p_cpo, CPO_BUFOPT) != NULL))) - return; - label : - asdf = asdf ? - asdf : asdf; - asdf = asdf ? - asdf: asdf; - } - - /* Special Comments : This function has the added complexity (compared */ - /* : to addtolist) of having to check for a detail */ - /* : texture and add that to the list first. */ - - char *(array[100]) = { - "testje", - "foo", - "bar", - } - - enum soppie - { - yes = 0, - no, - maybe - }; - - typedef enum soppie - { - yes = 0, - no, - maybe - }; - - static enum - { - yes = 0, - no, - maybe - } soppie; - - public static enum - { - yes = 0, - no, - maybe - } soppie; - - static private enum - { - yes = 0, - no, - maybe - } soppie; - - { - int a, - b; - } - - { - struct Type - { - int i; - char *str; - } var[] = - { - 0, "zero", - 1, "one", - 2, "two", - 3, "three" - }; - - float matrix[3][3] = - { - { - 0, - 1, - 2 - }, - { - 3, - 4, - 5 - }, - { - 6, - 7, - 8 - } - }; - } - - { - /* blah ( blah */ - /* where does this go? */ - - /* blah ( blah */ - cmd; - - func(arg1, - /* comment */ - arg2); - a; - { - b; - { - c; /* Hey, NOW it indents?! */ - } - } - - { - func(arg1, - arg2, - arg3); - /* Hey, what am I doing here? Is this coz of the ","? */ - } - } - - main () - { - if (cond) - { - a = b; - } - if (cond) { - a = c; - } - if (cond) - a = d; - return; - } - - { - case 2: if (asdf && - asdfasdf) - aasdf; - a = 9; - case 3: if (asdf) - aasdf; - a = 9; - case 4: x = 1; - y = 2; - - label: if (asdf) - here; - - label: if (asdf && - asdfasdf) - { - } - - label: if (asdf && - asdfasdf) { - there; - } - - label: if (asdf && - asdfasdf) - there; - } - - { - /* - hello with ":set comments= cino=c5" - */ - - /* - hello with ":set comments= cino=" - */ - } - - - { - if (a < b) { - a = a + 1; - } else - a = a + 2; - - if (a) - do { - testing; - } while (asdfasdf); - a = b + 1; - asdfasdf - } - - { - for ( int i = 0; - i < 10; i++ ) - { - } - i = 0; - } - - class bob - { - int foo() {return 1;} - int bar; - } - - main() - { - while(1) - if (foo) - { - bar; - } - else { - asdf; - } - misplacedline; - } - - { - if (clipboard.state == SELECT_DONE - && ((row == clipboard.start.lnum - && col >= clipboard.start.col) - || row > clipboard.start.lnum)) - } - - { - if (1) {i += 4;} - where_am_i; - return 0; - } - - { - { - } // sdf(asdf - if (asdf) - asd; - } - - { - label1: - label2: - } - - { - int fooRet = foo(pBar1, false /*fKB*/, - true /*fPTB*/, 3 /*nT*/, false /*fDF*/); - f() { - for ( i = 0; - i < m; - /* c */ i++ ) { - a = b; - } - } - } - - { - f1(/*comment*/); - f2(); - } - - { - do { - if (foo) { - } else - ; - } while (foo); - foo(); // was wrong - } - - int x; // no extra indent because of the ; - void func() - { - } - - char *tab[] = {"aaa", - "};", /* }; */ NULL} - int indented; - {} - - char *a[] = {"aaa", "bbb", - "ccc", NULL}; - // here - - char *tab[] = {"aaa", - "xx", /* xx */}; /* asdf */ - int not_indented; - - { - do { - switch (bla) - { - case 1: if (foo) - bar; - } - } while (boo); - wrong; - } - - int foo, - bar; - int foo; - - #if defined(foo) \ - && defined(bar) - char * xx = "asdf\ - foo\ - bor"; - int x; - - char *foo = "asdf\ - asdf\ - asdf", - *bar; - - void f() - { - #if defined(foo) \ - && defined(bar) - char *foo = "asdf\ - asdf\ - asdf", - *bar; - { - int i; - char *foo = "asdf\ - asdf\ - asdf", - *bar; - } - #endif - } - #endif - - int y; // comment - // comment - - // comment - - { - Constructor(int a, - int b ) : BaseClass(a) - { - } - } - - void foo() - { - char one, - two; - struct bla piet, - jan; - enum foo kees, - jannie; - static unsigned sdf, - krap; - unsigned int piet, - jan; - int - kees, - jan; - } - - { - t(int f, - int d); // ) - d(); - } - - Constructor::Constructor(int a, - int b - ) : - BaseClass(a, - b, - c), - mMember(b), - { - } - - Constructor::Constructor(int a, - int b ) : - BaseClass(a) - { - } - - Constructor::Constructor(int a, - int b ) /*x*/ : /*x*/ BaseClass(a), - member(b) - { - } - - A::A(int a, int b) - : aa(a), - bb(b), - cc(c) - { - } - - class CAbc : - public BaseClass1, - protected BaseClass2 - { - int Test() { return FALSE; } - int Test1() { return TRUE; } - - CAbc(int a, int b ) : - BaseClass(a) - { - switch(xxx) - { - case abc: - asdf(); - break; - - case 999: - baer(); - break; - } - } - - public: // <-- this was incoreectly indented before!! - void testfall(); - protected: - void testfall(); - }; - - class CAbc : public BaseClass1, - protected BaseClass2 - { - }; - - static struct - { - int a; - int b; - } variable[COUNT] = - { - { - 123, - 456 - }, - { - 123, - 456 - } - }; - - static struct - { - int a; - int b; - } variable[COUNT] = - { - { 123, 456 }, - { 123, 456 } - }; - - void asdf() /* ind_maxparen may cause trouble here */ - { - if ((0 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1)) break; - } - - foo() - { - a = cond ? foo() : asdf - + asdf; - - a = cond ? - foo() : asdf - + asdf; - } - - int main(void) - { - if (a) - if (b) - 2; - else 3; - next_line_of_code(); - } - - barry() - { - Foo::Foo (int one, - int two) - : something(4) - {} - } - - barry() - { - Foo::Foo (int one, int two) - : something(4) - {} - } - - Constructor::Constructor(int a, - int b - ) : - BaseClass(a, - b, - c), - mMember(b) - { - } - int main () - { - if (lala) - do - ++(*lolo); - while (lili - && lele); - lulu; - } - - int main () - { - switch (c) - { - case 'c': if (cond) - { - } - } - } - - main() - { - (void) MyFancyFuasdfadsfnction( - argument); - } - - main() - { - char foo[] = "/*"; - /* as - df */ - hello - } - - /* valid namespaces with normal indent */ - namespace - { - { - 111111111111; - } - } - namespace /* test */ - { - 11111111111111111; - } - namespace // test - { - 111111111111111111; - } - namespace - { - 111111111111111111; - } - namespace test - { - 111111111111111111; - } - namespace{ - 111111111111111111; - } - namespace test{ - 111111111111111111; - } - namespace { - 111111111111111111; - } - namespace test { - 111111111111111111; - namespace test2 { - 22222222222222222; - } - } - - /* invalid namespaces use block indent */ - namespace test test2 { - 111111111111111111111; - } - namespace11111111111 { - 111111111111; - } - namespace() { - 1111111111111; - } - namespace() - { - 111111111111111111; - } - namespace test test2 - { - 1111111111111111111; - } - namespace111111111 - { - 111111111111111111; - } - void getstring() { - /* Raw strings */ - const char* s = R"( - test { - # comment - field: 123 - } - )"; - } - void getstring() { - const char* s = R"foo( - test { - # comment - field: 123 - } - )foo"; - } - - { - int a[4] = { - [0] = 0, - [1] = 1, - [2] = 2, - [3] = 3, - }; - } - - { - a = b[2] - + 3; - } - - { - if (1) - /* aaaaa - * bbbbb - */ - a = 1; - } - - void func() - { - switch (foo) - { - case (bar): - if (baz()) - quux(); - break; - case (shmoo): - if (!bar) - { - } - case (foo1): - switch (bar) - { - case baz: - baz_f(); - break; - } - break; - default: - baz(); - baz(); - break; - } - } - - /* end of AUTO */ - ]=]) - - feed_command('/start of AUTO') - feed('=/end of AUTO<cr>') - - expect([=[ - - /* start of AUTO matically checked vim: set ts=4 : */ - { - if (test) - cmd1; - cmd2; - } - - { - if (test) - cmd1; - else - cmd2; - } - - { - if (test) - { - cmd1; - cmd2; - } - } - - { - if (test) - { - cmd1; - else - } - } - - { - while (this) - if (test) - cmd1; - cmd2; - } - - { - while (this) - if (test) - cmd1; - else - cmd2; - } - - { - if (test) - { - cmd; - } - - if (test) - cmd; - } - - { - if (test) { - cmd; - } - - if (test) cmd; - } - - { - cmd1; - for (blah) - while (this) - if (test) - cmd2; - cmd3; - } - - { - cmd1; - for (blah) - while (this) - if (test) - cmd2; - cmd3; - - if (test) - { - cmd1; - cmd2; - cmd3; - } - } - - - /* Test for 'cindent' do/while mixed with if/else: */ - - { - do - if (asdf) - asdfasd; - while (cond); - - do - if (asdf) - while (asdf) - asdf; - while (asdf); - } - - /* Test for 'cindent' with two ) on a continuation line */ - { - if (asdfasdf;asldkfj asdlkfj as;ldkfj sal;d - aal;sdkjf ( ;asldfkja;sldfk - al;sdjfka ;slkdf ) sa;ldkjfsa dlk;) - line up here; - } - - - /* C++ tests: */ - - // foo() these three lines should remain in column 0 - // { - // } - - /* Test for continuation and unterminated lines: */ - { - i = 99 + 14325 + - 21345 + - 21345 + - 21345 + ( 21345 + - 21345) + - 2345 + - 1234; - c = 1; - } - - /* - testje for indent with empty line - - here */ - - { - if (testing && - not a joke || - line up here) - hay; - if (testing && - (not a joke || testing - )line up here) - hay; - if (testing && - (not a joke || testing - line up here)) - hay; - } - - - { - switch (c) - { - case xx: - do - if (asdf) - do - asdfasdf; - while (asdf); - else - asdfasdf; - while (cond); - case yy: - case xx: - case zz: - testing; - } - } - - { - if (cond) { - foo; - } - else - { - bar; - } - } - - { - if (alskdfj ;alsdkfjal;skdjf (;sadlkfsa ;dlkf j;alksdfj ;alskdjf - alsdkfj (asldk;fj - awith cino=(0 ;lf this one goes to below the paren with == - ;laksjfd ;lsakdjf ;alskdf asd) - asdfasdf;))) - asdfasdf; - } - - int - func(a, b) - int a; - int c; - { - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3) - ) - } - - { - while (asd) - { - if (asdf) - if (test) - if (that) - { - if (asdf) - do - cdasd; - while (as - df); - } - else - if (asdf) - asdf; - else - asdf; - asdf; - } - } - - { - s = "/*"; b = ';' - s = "/*"; b = ';'; - a = b; - } - - { - switch (a) - { - case a: - switch (t) - { - case 1: - cmd; - break; - case 2: - cmd; - break; - } - cmd; - break; - case b: - { - int i; - cmd; - } - break; - case c: { - int i; - cmd; - } - case d: if (cond && - test) { /* this line doesn't work right */ - int i; - cmd; - } - break; - } - } - - { - if (!(vim_strchr(p_cpo, CPO_BUFOPTGLOB) != NULL && entering) && - (bp_to->b_p_initialized || - (!entering && vim_strchr(p_cpo, CPO_BUFOPT) != NULL))) - return; - label : - asdf = asdf ? - asdf : asdf; - asdf = asdf ? - asdf: asdf; - } - - /* Special Comments : This function has the added complexity (compared */ - /* : to addtolist) of having to check for a detail */ - /* : texture and add that to the list first. */ - - char *(array[100]) = { - "testje", - "foo", - "bar", - } - - enum soppie - { - yes = 0, - no, - maybe - }; - - typedef enum soppie - { - yes = 0, - no, - maybe - }; - - static enum - { - yes = 0, - no, - maybe - } soppie; - - public static enum - { - yes = 0, - no, - maybe - } soppie; - - static private enum - { - yes = 0, - no, - maybe - } soppie; - - { - int a, - b; - } - - { - struct Type - { - int i; - char *str; - } var[] = - { - 0, "zero", - 1, "one", - 2, "two", - 3, "three" - }; - - float matrix[3][3] = - { - { - 0, - 1, - 2 - }, - { - 3, - 4, - 5 - }, - { - 6, - 7, - 8 - } - }; - } - - { - /* blah ( blah */ - /* where does this go? */ - - /* blah ( blah */ - cmd; - - func(arg1, - /* comment */ - arg2); - a; - { - b; - { - c; /* Hey, NOW it indents?! */ - } - } - - { - func(arg1, - arg2, - arg3); - /* Hey, what am I doing here? Is this coz of the ","? */ - } - } - - main () - { - if (cond) - { - a = b; - } - if (cond) { - a = c; - } - if (cond) - a = d; - return; - } - - { - case 2: if (asdf && - asdfasdf) - aasdf; - a = 9; - case 3: if (asdf) - aasdf; - a = 9; - case 4: x = 1; - y = 2; - - label: if (asdf) - here; - - label: if (asdf && - asdfasdf) - { - } - - label: if (asdf && - asdfasdf) { - there; - } - - label: if (asdf && - asdfasdf) - there; - } - - { - /* - hello with ":set comments= cino=c5" - */ - - /* - hello with ":set comments= cino=" - */ - } - - - { - if (a < b) { - a = a + 1; - } else - a = a + 2; - - if (a) - do { - testing; - } while (asdfasdf); - a = b + 1; - asdfasdf - } - - { - for ( int i = 0; - i < 10; i++ ) - { - } - i = 0; - } - - class bob - { - int foo() {return 1;} - int bar; - } - - main() - { - while(1) - if (foo) - { - bar; - } - else { - asdf; - } - misplacedline; - } - - { - if (clipboard.state == SELECT_DONE - && ((row == clipboard.start.lnum - && col >= clipboard.start.col) - || row > clipboard.start.lnum)) - } - - { - if (1) {i += 4;} - where_am_i; - return 0; - } - - { - { - } // sdf(asdf - if (asdf) - asd; - } - - { - label1: - label2: - } - - { - int fooRet = foo(pBar1, false /*fKB*/, - true /*fPTB*/, 3 /*nT*/, false /*fDF*/); - f() { - for ( i = 0; - i < m; - /* c */ i++ ) { - a = b; - } - } - } - - { - f1(/*comment*/); - f2(); - } - - { - do { - if (foo) { - } else - ; - } while (foo); - foo(); // was wrong - } - - int x; // no extra indent because of the ; - void func() - { - } - - char *tab[] = {"aaa", - "};", /* }; */ NULL} - int indented; - {} - - char *a[] = {"aaa", "bbb", - "ccc", NULL}; - // here - - char *tab[] = {"aaa", - "xx", /* xx */}; /* asdf */ - int not_indented; - - { - do { - switch (bla) - { - case 1: if (foo) - bar; - } - } while (boo); - wrong; - } - - int foo, - bar; - int foo; - - #if defined(foo) \ - && defined(bar) - char * xx = "asdf\ - foo\ - bor"; - int x; - - char *foo = "asdf\ - asdf\ - asdf", - *bar; - - void f() - { - #if defined(foo) \ - && defined(bar) - char *foo = "asdf\ - asdf\ - asdf", - *bar; - { - int i; - char *foo = "asdf\ - asdf\ - asdf", - *bar; - } - #endif - } - #endif - - int y; // comment - // comment - - // comment - - { - Constructor(int a, - int b ) : BaseClass(a) - { - } - } - - void foo() - { - char one, - two; - struct bla piet, - jan; - enum foo kees, - jannie; - static unsigned sdf, - krap; - unsigned int piet, - jan; - int - kees, - jan; - } - - { - t(int f, - int d); // ) - d(); - } - - Constructor::Constructor(int a, - int b - ) : - BaseClass(a, - b, - c), - mMember(b), - { - } - - Constructor::Constructor(int a, - int b ) : - BaseClass(a) - { - } - - Constructor::Constructor(int a, - int b ) /*x*/ : /*x*/ BaseClass(a), - member(b) - { - } - - A::A(int a, int b) - : aa(a), - bb(b), - cc(c) - { - } - - class CAbc : - public BaseClass1, - protected BaseClass2 - { - int Test() { return FALSE; } - int Test1() { return TRUE; } - - CAbc(int a, int b ) : - BaseClass(a) - { - switch(xxx) - { - case abc: - asdf(); - break; - - case 999: - baer(); - break; - } - } - - public: // <-- this was incoreectly indented before!! - void testfall(); - protected: - void testfall(); - }; - - class CAbc : public BaseClass1, - protected BaseClass2 - { - }; - - static struct - { - int a; - int b; - } variable[COUNT] = - { - { - 123, - 456 - }, - { - 123, - 456 - } - }; - - static struct - { - int a; - int b; - } variable[COUNT] = - { - { 123, 456 }, - { 123, 456 } - }; - - void asdf() /* ind_maxparen may cause trouble here */ - { - if ((0 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1 - && 1)) break; - } - - foo() - { - a = cond ? foo() : asdf - + asdf; - - a = cond ? - foo() : asdf - + asdf; - } - - int main(void) - { - if (a) - if (b) - 2; - else 3; - next_line_of_code(); - } - - barry() - { - Foo::Foo (int one, - int two) - : something(4) - {} - } - - barry() - { - Foo::Foo (int one, int two) - : something(4) - {} - } - - Constructor::Constructor(int a, - int b - ) : - BaseClass(a, - b, - c), - mMember(b) - { - } - int main () - { - if (lala) - do - ++(*lolo); - while (lili - && lele); - lulu; - } - - int main () - { - switch (c) - { - case 'c': if (cond) - { - } - } - } - - main() - { - (void) MyFancyFuasdfadsfnction( - argument); - } - - main() - { - char foo[] = "/*"; - /* as - df */ - hello - } - - /* valid namespaces with normal indent */ - namespace - { - { - 111111111111; - } - } - namespace /* test */ - { - 11111111111111111; - } - namespace // test - { - 111111111111111111; - } - namespace - { - 111111111111111111; - } - namespace test - { - 111111111111111111; - } - namespace{ - 111111111111111111; - } - namespace test{ - 111111111111111111; - } - namespace { - 111111111111111111; - } - namespace test { - 111111111111111111; - namespace test2 { - 22222222222222222; - } - } - - /* invalid namespaces use block indent */ - namespace test test2 { - 111111111111111111111; - } - namespace11111111111 { - 111111111111; - } - namespace() { - 1111111111111; - } - namespace() - { - 111111111111111111; - } - namespace test test2 - { - 1111111111111111111; - } - namespace111111111 - { - 111111111111111111; - } - void getstring() { - /* Raw strings */ - const char* s = R"( - test { - # comment - field: 123 - } - )"; - } - void getstring() { - const char* s = R"foo( - test { - # comment - field: 123 - } - )foo"; - } - - { - int a[4] = { - [0] = 0, - [1] = 1, - [2] = 2, - [3] = 3, - }; - } - - { - a = b[2] - + 3; - } - - { - if (1) - /* aaaaa - * bbbbb - */ - a = 1; - } - - void func() - { - switch (foo) - { - case (bar): - if (baz()) - quux(); - break; - case (shmoo): - if (!bar) - { - } - case (foo1): - switch (bar) - { - case baz: - baz_f(); - break; - } - break; - default: - baz(); - baz(); - break; - } - } - - /* end of AUTO */ - ]=]) - end) - - it('2 is working', function() - insert_([=[ - - { - - /* this is - * a real serious important big - * comment - */ - /* insert " about life, the universe, and the rest" after "serious" */ - } - ]=]) - - feed_command('set tw=0 noai fo=croq') - feed_command('let &wm = &columns - 20') - feed_command('/serious/e') - feed('a about life, the universe, and the rest<esc>') - - expect([=[ - - { - - /* this is - * a real serious - * about life, the - * universe, and the - * rest important big - * comment - */ - /* insert " about life, the universe, and the rest" after "serious" */ - } - ]=]) - end) - - it('3 is working', function() - insert_([=[ - - { - /* - * Testing for comments, without 'cin' set - */ - - /* - * what happens here? - */ - - /* - the end of the comment, try inserting a line below */ - - /* how about - this one */ - } - ]=]) - - feed_command('set nocin') - feed_command('/comments') - feed('joabout life<esc>/happens<cr>') - feed('jothere<esc>/below<cr>') - feed('oline<esc>/this<cr>') - feed('Ohello<esc>') - - expect([=[ - - { - /* - * Testing for comments, without 'cin' set - */ - about life - - /* - * what happens here? - */ - there - - /* - the end of the comment, try inserting a line below */ - line - - /* how about - hello - this one */ - } - ]=]) - end) - - it('4 is working', function() - insert_([=[ - - { - var = this + that + vec[0] * vec[0] - + vec[1] * vec[1] - + vec2[2] * vec[2]; - } - ]=]) - feed_command('set cin') - feed_command('/vec2') - feed('==<cr>') - - expect([=[ - - { - var = this + that + vec[0] * vec[0] - + vec[1] * vec[1] - + vec2[2] * vec[2]; - } - ]=]) - end) - - it('5 is working', function() - insert_([=[ - - { - asdf asdflkajds f; - if (tes & ting) { - asdf asdf asdf ; - asdfa sdf asdf; - } - testing1; - if (tes & ting) - { - asdf asdf asdf ; - asdfa sdf asdf; - } - testing2; - } - ]=]) - - feed_command('set cin') - feed_command('set cino=}4') - feed_command('/testing1') - feed('k2==/testing2<cr>') - feed('k2==<cr>') - - expect([=[ - - { - asdf asdflkajds f; - if (tes & ting) { - asdf asdf asdf ; - asdfa sdf asdf; - } - testing1; - if (tes & ting) - { - asdf asdf asdf ; - asdfa sdf asdf; - } - testing2; - } - ]=]) - end) - - it('6 is working', function() - insert_([=[ - - main ( int first_par, /* - * Comment for - * first par - */ - int second_par /* - * Comment for - * second par - */ - ) - { - func( first_par, /* - * Comment for - * first par - */ - second_par /* - * Comment for - * second par - */ - ); - - } - ]=]) - - feed_command('set cin') - feed_command('set cino=(0,)20') - feed_command('/main') - feed('=][<cr>') - - expect([=[ - - main ( int first_par, /* - * Comment for - * first par - */ - int second_par /* - * Comment for - * second par - */ - ) - { - func( first_par, /* - * Comment for - * first par - */ - second_par /* - * Comment for - * second par - */ - ); - - } - ]=]) - end) - - it('7 is working', function() - insert_([=[ - - main(void) - { - /* Make sure that cino=X0s is not parsed like cino=Xs. */ - if (cond) - foo(); - else - { - bar(); - } - } - ]=]) - - feed_command('set cin') - feed_command('set cino=es,n0s') - feed_command('/main') - feed('=][<cr>') - - expect([=[ - - main(void) - { - /* Make sure that cino=X0s is not parsed like cino=Xs. */ - if (cond) - foo(); - else - { - bar(); - } - } - ]=]) - end) - - it('8 is working', function() - insert_([=[ - - { - do - { - if () - { - if () - asdf; - else - asdf; - } - } while (); - cmd; /* this should go under the } */ - } - ]=]) - - feed_command('set cin') - feed_command('set cino=') - feed(']]=][<cr>') - - expect([=[ - - { - do - { - if () - { - if () - asdf; - else - asdf; - } - } while (); - cmd; /* this should go under the } */ - } - ]=]) - end) - - it('9 is working', function() - insert_([=[ - - void f() - { - if ( k() ) { - l(); - - } else { /* Start (two words) end */ - m(); - } - - n(); - } - ]=]) - - feed(']]=][<cr>') - - expect([=[ - - void f() - { - if ( k() ) { - l(); - - } else { /* Start (two words) end */ - m(); - } - - n(); - } - ]=]) - end) - - it('10 is working', function() - -- This is nasty. This is the only test case where the buffer content - -- differs from the original. Why? Proper behaviour of this test depends on - -- the fact that the setup code contains an (unbalanced) opening curly - -- bracket in "set cino={s,e-s". This bracket actually affects the outcome - -- of the test: without it the curly bracket under "void f()" would not be - -- indented properly. And that's why we've had to add one explicitly. - insert_([=[ - { <= THIS IS THE CURLY BRACKET EXPLAINED IN THE COMMENT. - - void f() - { - if ( k() ) - { - l(); - } else { /* Start (two words) end */ - m(); - } - n(); /* should be under the if () */ - } - ]=]) - - feed_command('set cino={s,e-s') - feed(']]=][<cr>') - - expect([=[ - { <= THIS IS THE CURLY BRACKET EXPLAINED IN THE COMMENT. - - void f() - { - if ( k() ) - { - l(); - } else { /* Start (two words) end */ - m(); - } - n(); /* should be under the if () */ - } - ]=]) - end) - - it('11 is working', function() - insert_([=[ - - void bar(void) - { - static array[2][2] = - { - { 1, 2 }, - { 3, 4 }, - } - - while (a) - { - foo(&a); - } - - { - int a; - { - a = a + 1; - } - } - b = a; - } - - void func(void) - { - a = 1; - { - b = 2; - } - c = 3; - d = 4; - } - /* foo */ - ]=]) - - feed_command('set cino={s,fs') - feed(']]=/ foo<cr>') - - expect([=[ - - void bar(void) - { - static array[2][2] = - { - { 1, 2 }, - { 3, 4 }, - } - - while (a) - { - foo(&a); - } - - { - int a; - { - a = a + 1; - } - } - b = a; - } - - void func(void) - { - a = 1; - { - b = 2; - } - c = 3; - d = 4; - } - /* foo */ - ]=]) - end) - - it('12 is working', function() - insert_([=[ - - a() - { - do { - a = a + - a; - } while ( a ); /* add text under this line */ - if ( a ) - a; - } - ]=]) - - feed_command('set cino=') - feed_command('/while') - feed('ohere<esc>') - - expect([=[ - - a() - { - do { - a = a + - a; - } while ( a ); /* add text under this line */ - here - if ( a ) - a; - } - ]=]) - end) - - it('13 is working', function() - insert_([=[ - - a() - { - label1: - /* hmm */ - // comment - } - ]=]) - - feed_command('set cino= com=') - feed_command('/comment') - feed('olabel2: b();<cr>label3 /* post */:<cr>/* pre */ label4:<cr>f(/*com*/);<cr>if (/*com*/)<cr>cmd();<esc>') - - expect([=[ - - a() - { - label1: - /* hmm */ - // comment - label2: b(); - label3 /* post */: - /* pre */ label4: - f(/*com*/); - if (/*com*/) - cmd(); - } - ]=]) - end) - - it('14 is working', function() - insert_([=[ - - /* - * A simple comment - */ - - /* - ** A different comment - */ - ]=]) - - feed_command('set comments& comments^=s:/*,m:**,ex:*/') - feed_command('/simple') - feed('=5j<cr>') - - expect([=[ - - /* - * A simple comment - */ - - /* - ** A different comment - */ - ]=]) - end) - - it('15 is working', function() - insert_([=[ - - - void f() - { - - /********* - A comment. - *********/ - } - ]=]) - - feed_command('set cino=c0') - feed_command('set comments& comments-=s1:/* comments^=s0:/*') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - - /********* - A comment. - *********/ - } - ]=]) - end) - - it('16 is working', function() - insert_([=[ - - - void f() - { - - /********* - A comment. - *********/ - } - ]=]) - - feed_command('set cino=c0,C1') - feed_command('set comments& comments-=s1:/* comments^=s0:/*') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - - /********* - A comment. - *********/ - } - ]=]) - end) - - it('17 is working', function() - insert_([=[ - - void f() - { - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - - feed_command('set cino=') - feed(']]=][<cr>') - - expect([=[ - - void f() - { - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - end) - - it('18 is working', function() - insert_([=[ - - - void f() - { - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - - feed_command('set cino=(s') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - end) - - it('19 is working', function() - insert_([=[ - - - void f() - { - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - - feed_command('set cino=(s,U1 ') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - end) - - it('20 is working', function() - insert_([=[ - - - void f() - { - if ( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - - feed_command('set cino=(0') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - if ( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - end) - - it('21 is working', function() - insert_([=[ - - - void f() - { - if ( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - - feed_command('set cino=(0,w1 ') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - if ( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - end) - - it('22 is working', function() - insert_([=[ - - - void f() - { - c = c1 && ( - c2 || - c3 - ) && c4; - if ( - c1 && c2 - ) - foo; - } - ]=]) - - feed_command('set cino=(s') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - c = c1 && ( - c2 || - c3 - ) && c4; - if ( - c1 && c2 - ) - foo; - } - ]=]) - end) - - it('23 is working', function() - insert_([=[ - - - void f() - { - c = c1 && ( - c2 || - c3 - ) && c4; - if ( - c1 && c2 - ) - foo; - } - ]=]) - - feed_command('set cino=(s,m1 ') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - c = c1 && ( - c2 || - c3 - ) && c4; - if ( - c1 && c2 - ) - foo; - } - ]=]) - end) - - it('24 is working', function() - insert_([=[ - - - void f() - { - switch (x) - { - case 1: - a = b; - break; - default: - a = 0; - break; - } - } - ]=]) - - feed_command('set cino=b1') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - switch (x) - { - case 1: - a = b; - break; - default: - a = 0; - break; - } - } - ]=]) - end) - - it('25 is working', function() - insert_([=[ - - - void f() - { - invokeme( - argu, - ment); - invokeme( - argu, - ment - ); - invokeme(argu, - ment - ); - } - ]=]) - - feed_command('set cino=(0,W5') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - invokeme( - argu, - ment); - invokeme( - argu, - ment - ); - invokeme(argu, - ment - ); - } - ]=]) - end) - - it('26 is working', function() - insert_([=[ - - - void f() - { - statement; - // comment 1 - // comment 2 - } - ]=]) - - feed_command('set cino=/6') - feed('2kdd]]=][<cr>') - - expect([=[ - - void f() - { - statement; - // comment 1 - // comment 2 - } - ]=]) - end) - - it('27 is working', function() - insert_([=[ - - - void f() - { - statement; - // comment 1 - // comment 2 - } - ]=]) - - feed_command('set cino=') - feed('2kdd]]/comment 1/+1<cr>') - feed('==<cr>') - - expect([=[ - - void f() - { - statement; - // comment 1 - // comment 2 - } - ]=]) - end) - - it('28 is working', function() - insert_([=[ - - - class CAbc - { - int Test() { return FALSE; } - - public: // comment - void testfall(); - protected: - void testfall(); - }; - ]=]) - - feed_command('set cino=g0') - feed('2kdd]]=][<cr>') - - expect([=[ - - class CAbc - { - int Test() { return FALSE; } - - public: // comment - void testfall(); - protected: - void testfall(); - }; - ]=]) - end) - - it('29 is working', function() - insert_([=[ - - - class Foo : public Bar - { - public: - virtual void method1(void) = 0; - virtual void method2(int arg1, - int arg2, - int arg3) = 0; - }; - ]=]) - - feed_command('set cino=(0,gs,hs') - feed('2kdd]]=][<cr>') - - expect([=[ - - class Foo : public Bar - { - public: - virtual void method1(void) = 0; - virtual void method2(int arg1, - int arg2, - int arg3) = 0; - }; - ]=]) - end) - - it('30 is working', function() - insert_([=[ - - - void - foo() - { - if (a) - { - } else - asdf; - } - ]=]) - - feed_command('set cino=+20') - feed('2kdd]]=][<cr>') - - expect([=[ - - void - foo() - { - if (a) - { - } else - asdf; - } - ]=]) - end) - - it('31 is working', function() - insert_([=[ - - - { - averylongfunctionnamelongfunctionnameaverylongfunctionname()->asd( - asdasdf, - func(asdf, - asdfadsf), - asdfasdf - ); - - /* those are ugly, but consequent */ - - func()->asd(asdasdf, - averylongfunctionname( - abc, - dec)->averylongfunctionname( - asdfadsf, - asdfasdf, - asdfasdf, - ), - func(asdfadf, - asdfasdf - ), - asdasdf - ); - - averylongfunctionnameaverylongfunctionnameavery()->asd(fasdf( - abc, - dec)->asdfasdfasdf( - asdfadsf, - asdfasdf, - asdfasdf, - ), - func(asdfadf, - asdfasdf), - asdasdf - ); - } - ]=]) - - feed_command('set cino=(0,W2s') - feed('2kdd]]=][<cr>') - - expect([=[ - - { - averylongfunctionnamelongfunctionnameaverylongfunctionname()->asd( - asdasdf, - func(asdf, - asdfadsf), - asdfasdf - ); - - /* those are ugly, but consequent */ - - func()->asd(asdasdf, - averylongfunctionname( - abc, - dec)->averylongfunctionname( - asdfadsf, - asdfasdf, - asdfasdf, - ), - func(asdfadf, - asdfasdf - ), - asdasdf - ); - - averylongfunctionnameaverylongfunctionnameavery()->asd(fasdf( - abc, - dec)->asdfasdfasdf( - asdfadsf, - asdfasdf, - asdfasdf, - ), - func(asdfadf, - asdfasdf), - asdasdf - ); - } - ]=]) - end) - - it('32 is working', function() - insert_([=[ - - - int main () - { - if (cond1 && - cond2 - ) - foo; - } - ]=]) - - feed_command('set cino=M1') - feed('2kdd]]=][<cr>') - - expect([=[ - - int main () - { - if (cond1 && - cond2 - ) - foo; - } - ]=]) - end) - - it('33 is working', function() - insert_([=[ - - - void func(int a - #if defined(FOO) - , int b - , int c - #endif - ) - { - } - ]=]) - - feed_command('set cino=(0,ts') - feed('2kdd2j=][<cr>') - - expect([=[ - - void func(int a - #if defined(FOO) - , int b - , int c - #endif - ) - { - } - ]=]) - end) - - it('34 is working', function() - insert_([=[ - - - - void - func(int a - #if defined(FOO) - , int b - , int c - #endif - ) - { - } - ]=]) - - feed_command('set cino=(0') - feed('2kdd2j=][<cr>') - - expect([=[ - - - void - func(int a - #if defined(FOO) - , int b - , int c - #endif - ) - { - } - ]=]) - end) - - it('35 is working', function() - insert_([=[ - - - void func(void) - { - if(x==y) - if(y==z) - foo=1; - else { bar=1; - baz=2; - } - printf("Foo!\n"); - } - - void func1(void) - { - char* tab[] = {"foo", "bar", - "baz", "quux", - "this line used", "to be indented incorrectly"}; - foo(); - } - - void func2(void) - { - int tab[] = - {1, 2, - 3, 4, - 5, 6}; - - printf("This line used to be indented incorrectly.\n"); - } - - int foo[] - #ifdef BAR - - = { 1, 2, 3, - 4, 5, 6 } - - #endif - ; - int baz; - - void func3(void) - { - int tab[] = { - 1, 2, - 3, 4, - 5, 6}; - - printf("Don't you dare indent this line incorrectly!\n"); - } - - void - func4(a, b, - c) - int a; - int b; - int c; - { - } - - void - func5( - int a, - int b) - { - } - - void - func6( - int a) - { - } - ]=]) - - feed_command('set cino&') - feed('2kdd2j=7][<cr>') - - expect([=[ - - void func(void) - { - if(x==y) - if(y==z) - foo=1; - else { bar=1; - baz=2; - } - printf("Foo!\n"); - } - - void func1(void) - { - char* tab[] = {"foo", "bar", - "baz", "quux", - "this line used", "to be indented incorrectly"}; - foo(); - } - - void func2(void) - { - int tab[] = - {1, 2, - 3, 4, - 5, 6}; - - printf("This line used to be indented incorrectly.\n"); - } - - int foo[] - #ifdef BAR - - = { 1, 2, 3, - 4, 5, 6 } - - #endif - ; - int baz; - - void func3(void) - { - int tab[] = { - 1, 2, - 3, 4, - 5, 6}; - - printf("Don't you dare indent this line incorrectly!\n"); - } - - void - func4(a, b, - c) - int a; - int b; - int c; - { - } - - void - func5( - int a, - int b) - { - } - - void - func6( - int a) - { - } - ]=]) - end) - - it('36 is working', function() - insert_([=[ - - - void func(void) - { - int tab[] = - { - 1, 2, 3, - 4, 5, 6}; - - printf("Indent this line correctly!\n"); - - switch (foo) - { - case bar: - printf("bar"); - break; - case baz: { - printf("baz"); - break; - } - case quux: - printf("But don't break the indentation of this instruction\n"); - break; - } - } - ]=]) - - feed_command('set cino&') - feed_command('set cino+=l1') - feed('2kdd2j=][<cr>') - - expect([=[ - - void func(void) - { - int tab[] = - { - 1, 2, 3, - 4, 5, 6}; - - printf("Indent this line correctly!\n"); - - switch (foo) - { - case bar: - printf("bar"); - break; - case baz: { - printf("baz"); - break; - } - case quux: - printf("But don't break the indentation of this instruction\n"); - break; - } - } - ]=]) - end) - - it('37 is working', function() - insert_([=[ - - - void func(void) - { - cout << "a" - << "b" - << ") :" - << "c"; - } - ]=]) - - feed_command('set cino&') - feed('2kdd2j=][<cr>') - - expect([=[ - - void func(void) - { - cout << "a" - << "b" - << ") :" - << "c"; - } - ]=]) - end) - - it('38 is working', function() - insert_([=[ - - void func(void) - { - /* - * This is a comment. - */ - } - ]=]) - - feed_command('set com=s1:/*,m:*,ex:*/') - feed(']]3jofoo();<esc>') - - expect([=[ - - void func(void) - { - /* - * This is a comment. - */ - foo(); - } - ]=]) - end) - - it('39 is working', function() - insert_([=[ - - - void func(void) - { - for (int i = 0; i < 10; ++i) - if (i & 1) { - foo(1); - } else - foo(0); - baz(); - } - ]=]) - - feed_command('set cino&') - feed('2kdd2j=][<cr>') - - expect([=[ - - void func(void) - { - for (int i = 0; i < 10; ++i) - if (i & 1) { - foo(1); - } else - foo(0); - baz(); - } - ]=]) - end) - - it('40 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - - if ( c1 - && ( c2 - || c3)) - foo; - func( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - - feed_command('set cino=k2s,(0') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - - if ( c1 - && ( c2 - || c3)) - foo; - func( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - end) - - it('41 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - - if ( c1 - && ( c2 - || c3)) - foo; - func( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - - feed_command('set cino=k2s,(s') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - - if ( c1 - && ( c2 - || c3)) - foo; - func( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - end) - - it('42 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - - feed_command('set cino=k2s,(s,U1') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - - c = c1 && - ( - c2 || - c3 - ) && c4; - } - ]=]) - end) - - it('43 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - - if ( c1 - && ( c2 - || c3)) - foo; - - a_long_line( - argument, - argument); - a_short_line(argument, - argument); - } - ]=]) - - feed_command('set cino=k2s,(0,W4') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - - if ( c1 - && ( c2 - || c3)) - foo; - - a_long_line( - argument, - argument); - a_short_line(argument, - argument); - } - ]=]) - end) - - it('44 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - } - ]=]) - - feed_command('set cino=k2s,u2') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - } - ]=]) - end) - - it('45 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - - if ( c1 - && ( c2 - || c3)) - foo; - func( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - - feed_command('set cino=k2s,(0,w1') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - if (c123456789 - && (c22345 - || c3)) - printf("foo\n"); - - if ( c1 - && ( c2 - || c3)) - foo; - func( c1 - && ( c2 - || c3)) - foo; - } - ]=]) - end) - - it('46 is working', function() - insert_([=[ - - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - } - ]=]) - - feed_command('set cino=k2,(s') - feed('2kdd3j=][<cr>') - - expect([=[ - - void func(void) - { - if (condition1 - && condition2) - action(); - function(argument1 - && argument2); - - if (c1 && (c2 || - c3)) - foo; - if (c1 && - (c2 || c3)) - { - } - } - ]=]) - end) - - it('47 is working', function() - insert_([=[ - - NAMESPACESTART - /* valid namespaces with normal indent */ - namespace - { - { - 111111111111; - } - } - namespace /* test */ - { - 11111111111111111; - } - namespace // test - { - 111111111111111111; - } - namespace - { - 111111111111111111; - } - namespace test - { - 111111111111111111; - } - namespace test::cpp17 - { - 111111111111111111; - } - namespace ::incorrectcpp17 - { - 111111111111111111; - } - namespace test::incorrectcpp17:: - { - 111111111111111111; - } - namespace test:incorrectcpp17 - { - 111111111111111111; - } - namespace test:::incorrectcpp17 - { - 111111111111111111; - } - namespace{ - 111111111111111111; - } - namespace test{ - 111111111111111111; - } - namespace { - 111111111111111111; - } - namespace test { - 111111111111111111; - namespace test2 { - 22222222222222222; - } - } - - /* invalid namespaces use block indent */ - namespace test test2 { - 111111111111111111111; - } - namespace11111111111 { - 111111111111; - } - namespace() { - 1111111111111; - } - namespace() - { - 111111111111111111; - } - namespace test test2 - { - 1111111111111111111; - } - namespace111111111 - { - 111111111111111111; - } - NAMESPACEEND - ]=]) - - feed_command('set cino=N-s') - feed_command('/^NAMESPACESTART') - feed('=/^NAMESPACEEND<cr>') - - expect([=[ - - NAMESPACESTART - /* valid namespaces with normal indent */ - namespace - { - { - 111111111111; - } - } - namespace /* test */ - { - 11111111111111111; - } - namespace // test - { - 111111111111111111; - } - namespace - { - 111111111111111111; - } - namespace test - { - 111111111111111111; - } - namespace test::cpp17 - { - 111111111111111111; - } - namespace ::incorrectcpp17 - { - 111111111111111111; - } - namespace test::incorrectcpp17:: - { - 111111111111111111; - } - namespace test:incorrectcpp17 - { - 111111111111111111; - } - namespace test:::incorrectcpp17 - { - 111111111111111111; - } - namespace{ - 111111111111111111; - } - namespace test{ - 111111111111111111; - } - namespace { - 111111111111111111; - } - namespace test { - 111111111111111111; - namespace test2 { - 22222222222222222; - } - } - - /* invalid namespaces use block indent */ - namespace test test2 { - 111111111111111111111; - } - namespace11111111111 { - 111111111111; - } - namespace() { - 1111111111111; - } - namespace() - { - 111111111111111111; - } - namespace test test2 - { - 1111111111111111111; - } - namespace111111111 - { - 111111111111111111; - } - NAMESPACEEND - ]=]) - end) - - it('48 is working', function() - insert_([=[ - - JSSTART - var bar = { - foo: { - that: this, - some: ok, - }, - "bar":{ - a : 2, - b: "123abc", - x: 4, - "y": 5 - } - } - JSEND - ]=]) - - feed_command('set cino=j1,J1') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - var bar = { - foo: { - that: this, - some: ok, - }, - "bar":{ - a : 2, - b: "123abc", - x: 4, - "y": 5 - } - } - JSEND - ]=]) - end) - - it('49 is working', function() - insert_([=[ - - JSSTART - var foo = [ - 1, - 2, - 3 - ]; - JSEND - ]=]) - - feed_command('set cino=j1,J1') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - var foo = [ - 1, - 2, - 3 - ]; - JSEND - ]=]) - end) - - it('50 is working', function() - insert_([=[ - - JSSTART - function bar() { - var foo = [ - 1, - 2, - 3 - ]; - } - JSEND - ]=]) - - feed_command('set cino=j1,J1') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - function bar() { - var foo = [ - 1, - 2, - 3 - ]; - } - JSEND - ]=]) - end) - - it('51 is working', function() - insert_([=[ - - JSSTART - (function($){ - - if (cond && - cond) { - stmt; - } - window.something.left = - (width - 50 + offset) + "px"; - var class_name='myclass'; - - function private_method() { - } - - var public_method={ - method: function(options,args){ - private_method(); - } - } - - function init(options) { - - $(this).data(class_name+'_public',$.extend({},{ - foo: 'bar', - bar: 2, - foobar: [ - 1, - 2, - 3 - ], - callback: function(){ - return true; - } - }, options||{})); - } - - $.fn[class_name]=function() { - - var _arguments=arguments; - return this.each(function(){ - - var options=$(this).data(class_name+'_public'); - if (!options) { - init.apply(this,_arguments); - - } else { - var method=public_method[_arguments[0]]; - - if (typeof(method)!='function') { - console.log(class_name+' has no method "'+_arguments[0]+'"'); - return false; - } - _arguments[0]=options; - method.apply(this,_arguments); - } - }); - } - - })(jQuery); - JSEND - ]=]) - - feed_command('set cino=j1,J1') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - (function($){ - - if (cond && - cond) { - stmt; - } - window.something.left = - (width - 50 + offset) + "px"; - var class_name='myclass'; - - function private_method() { - } - - var public_method={ - method: function(options,args){ - private_method(); - } - } - - function init(options) { - - $(this).data(class_name+'_public',$.extend({},{ - foo: 'bar', - bar: 2, - foobar: [ - 1, - 2, - 3 - ], - callback: function(){ - return true; - } - }, options||{})); - } - - $.fn[class_name]=function() { - - var _arguments=arguments; - return this.each(function(){ - - var options=$(this).data(class_name+'_public'); - if (!options) { - init.apply(this,_arguments); - - } else { - var method=public_method[_arguments[0]]; - - if (typeof(method)!='function') { - console.log(class_name+' has no method "'+_arguments[0]+'"'); - return false; - } - _arguments[0]=options; - method.apply(this,_arguments); - } - }); - } - - })(jQuery); - JSEND - ]=]) - end) - - it('52 is working', function() - insert_([=[ - - JSSTART - function init(options) { - $(this).data(class_name+'_public',$.extend({},{ - foo: 'bar', - bar: 2, - foobar: [ - 1, - 2, - 3 - ], - callback: function(){ - return true; - } - }, options||{})); - } - JSEND - ]=]) - - feed_command('set cino=j1,J1') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - function init(options) { - $(this).data(class_name+'_public',$.extend({},{ - foo: 'bar', - bar: 2, - foobar: [ - 1, - 2, - 3 - ], - callback: function(){ - return true; - } - }, options||{})); - } - JSEND - ]=]) - end) - - it('53 is working', function() - insert_([=[ - - JSSTART - (function($){ - function init(options) { - $(this).data(class_name+'_public',$.extend({},{ - foo: 'bar', - bar: 2, - foobar: [ - 1, - 2, - 3 - ], - callback: function(){ - return true; - } - }, options||{})); - } - })(jQuery); - JSEND - ]=]) - - feed_command('set cino=j1,J1') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - (function($){ - function init(options) { - $(this).data(class_name+'_public',$.extend({},{ - foo: 'bar', - bar: 2, - foobar: [ - 1, - 2, - 3 - ], - callback: function(){ - return true; - } - }, options||{})); - } - })(jQuery); - JSEND - ]=]) - end) - - it('javascript indent / vim-patch 7.4.670', function() - insert_([=[ - - JSSTART - // Results of JavaScript indent - // 1 - (function(){ - var a = [ - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - }()) - - // 2 - (function(){ - var a = [ - 0 + - 5 * - 9 * - 'a', - 'b', - 0 + - 5 * - 9 * - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - }()) - - // 3 - (function(){ - var a = [ - 0 + - // comment 1 - 5 * - /* comment 2 */ - 9 * - 'a', - 'b', - 0 + - 5 * - 9 * - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - }()) - - // 4 - { - var a = [ - 0, - 1 - ]; - var b; - var c; - } - - // 5 - { - var a = [ - [ - 0 - ], - 2, - 3 - ]; - } - - // 6 - { - var a = [ - [ - 0, - 1 - ], - 2, - 3 - ]; - } - - // 7 - { - var a = [ - // [ - 0, - // 1 - // ], - 2, - 3 - ]; - } - - // 8 - var x = [ - (function(){ - var a, - b, - c, - d, - e, - f, - g, - h, - i; - }) - ]; - - // 9 - var a = [ - 0 + - 5 * - 9 * - 'a', - 'b', - 0 + - 5 * - 9 * - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - - // 10 - var a, - b, - c, - d, - e, - f, - g, - h, - i; - JSEND - ]=]) - - -- :set cino=j1,J1,+2 - feed_command('set cino=j1,J1,+2') - feed_command('/^JSSTART') - feed('=/^JSEND<cr>') - - expect([=[ - - JSSTART - // Results of JavaScript indent - // 1 - (function(){ - var a = [ - 'a', - 'b', - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - }()) - - // 2 - (function(){ - var a = [ - 0 + - 5 * - 9 * - 'a', - 'b', - 0 + - 5 * - 9 * - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - }()) - - // 3 - (function(){ - var a = [ - 0 + - // comment 1 - 5 * - /* comment 2 */ - 9 * - 'a', - 'b', - 0 + - 5 * - 9 * - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - }()) - - // 4 - { - var a = [ - 0, - 1 - ]; - var b; - var c; - } - - // 5 - { - var a = [ - [ - 0 - ], - 2, - 3 - ]; - } - - // 6 - { - var a = [ - [ - 0, - 1 - ], - 2, - 3 - ]; - } - - // 7 - { - var a = [ - // [ - 0, - // 1 - // ], - 2, - 3 - ]; - } - - // 8 - var x = [ - (function(){ - var a, - b, - c, - d, - e, - f, - g, - h, - i; - }) - ]; - - // 9 - var a = [ - 0 + - 5 * - 9 * - 'a', - 'b', - 0 + - 5 * - 9 * - 'c', - 'd', - 'e', - 'f', - 'g', - 'h', - 'i' - ]; - - // 10 - var a, - b, - c, - d, - e, - f, - g, - h, - i; - JSEND - ]=]) - end) - - it('line continuations in macros / vim-patch 8.0.0148', function() - insert_([=[ - /* start of define */ - { - } - #define AAA \ - BBB\ - CCC - - #define CNT \ - 1 + \ - 2 + \ - 4 - /* end of define */]=]) - - feed_command('set cino&') - feed_command('/start of define') - feed('=/end of define<cr>') - - expect([=[ - /* start of define */ - { - } - #define AAA \ - BBB\ - CCC - - #define CNT \ - 1 + \ - 2 + \ - 4 - /* end of define */]=]) - end) - - it('* immediately follows comment / vim-patch 8.0.1291', function() - insert_([=[ - { - a = second/*bug*/*line; - }]=]) - - feed_command('set cin cino&') - feed_command('/a = second') - feed('ox') - - expect([=[ - { - a = second/*bug*/*line; - x - }]=]) - end) -end) diff --git a/test/functional/legacy/assert_spec.lua b/test/functional/legacy/assert_spec.lua index c2b22472bf..dd6a9d0bde 100644 --- a/test/functional/legacy/assert_spec.lua +++ b/test/functional/legacy/assert_spec.lua @@ -19,36 +19,10 @@ describe('assert function:', function() clear() end) - describe('assert_beeps', function() - it('works', function() - call('assert_beeps', 'normal h') - expected_empty() - call('assert_beeps', 'normal 0') - expected_errors({'command did not beep: normal 0'}) - end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(0, 'normal h'->assert_beeps()) - call assert_equal(1, 'normal 0'->assert_beeps()) - ]] - expected_errors({tmpname .. ' line 2: command did not beep: normal 0'}) - end) - end) - -- assert_equal({expected}, {actual}, [, {msg}]) describe('assert_equal', function() it('should not change v:errors when expected is equal to actual', function() source([[ - let s = 'foo' - call assert_equal('foo', s) - let n = 4 - call assert_equal(4, n) - let l = [1, 2, 3] - call assert_equal([1, 2, 3], l) - call assert_equal(v:_null_list, v:_null_list) - call assert_equal(v:_null_list, []) - call assert_equal([], v:_null_list) fu Func() endfu let F1 = function('Func') @@ -98,30 +72,6 @@ describe('assert function:', function() eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', exc_exec('call CheckAssert()')) end) - - it('can specify a message and get a message about what failed', function() - call('assert_equal', 'foo', 'bar', 'testing') - expected_errors({"testing: Expected 'foo' but got 'bar'"}) - end) - - it('should shorten a long message', function() - call ('assert_equal', 'XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX') - expected_errors({"Expected 'X\\[x occurs 21 times]X' but got 'X\\[y occurs 25 times]X'"}) - end) - end) - - -- assert_notequal({expected}, {actual}[, {msg}]) - describe('assert_notequal', function() - it('should not change v:errors when expected differs from actual', function() - eq(0, call('assert_notequal', 'foo', 4)) - eq(0, call('assert_notequal', {1, 2, 3}, 'foo')) - expected_empty() - end) - - it('should change v:errors when expected is equal to actual', function() - eq(1, call('assert_notequal', 'foo', 'foo')) - expected_errors({"Expected not equal to 'foo'"}) - end) end) -- assert_false({actual}, [, {msg}]) @@ -141,14 +91,6 @@ describe('assert function:', function() call('assert_false', {}) expected_errors({'Expected False but got []'}) end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(0, v:false->assert_false()) - call assert_equal(1, 123->assert_false()) - ]] - expected_errors({tmpname .. ' line 2: Expected False but got 123'}) - end) end) -- assert_true({actual}, [, {msg}]) @@ -164,14 +106,6 @@ describe('assert function:', function() eq(1, call('assert_true', 1.5)) expected_errors({'Expected True but got 1.5'}) end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(0, v:true->assert_true()) - call assert_equal(1, 0->assert_true()) - ]] - expected_errors({tmpname .. ' line 2: Expected True but got 0'}) - end) end) describe('v:errors', function() @@ -217,76 +151,10 @@ describe('assert function:', function() tmpname_two .. " line 1: file two: Expected True but got ''", }) end) - - it('is reset to a list by assert functions', function() - source([[ - let save_verrors = v:errors - let v:['errors'] = {'foo': 3} - call assert_equal('yes', 'no') - let verrors = v:errors - let v:errors = save_verrors - call assert_equal(type([]), type(verrors)) - ]]) - expected_empty() - end) - end) - - -- assert_match({pat}, {text}[, {msg}]) - describe('assert_match', function() - it('should not change v:errors when pat matches text', function() - call('assert_match', '^f.*b.*r$', 'foobar') - expected_empty() - end) - - it('should change v:errors when pat does not match text', function() - call('assert_match', 'bar.*foo', 'foobar') - expected_errors({"Pattern 'bar.*foo' does not match 'foobar'"}) - end) - - it('should set v:errors to msg when given and match fails', function() - call('assert_match', 'bar.*foo', 'foobar', 'wrong') - expected_errors({"wrong: Pattern 'bar.*foo' does not match 'foobar'"}) - end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(1, 'foobar'->assert_match('bar.*foo', 'wrong')) - ]] - expected_errors({ - tmpname .. " line 1: wrong: Pattern 'bar.*foo' does not match 'foobar'" - }) - end) - end) - - -- assert_notmatch({pat}, {text}[, {msg}]) - describe('assert_notmatch', function() - it('should not change v:errors when pat does not match text', function() - call('assert_notmatch', 'foo', 'bar') - call('assert_notmatch', '^foobar$', 'foobars') - expected_empty() - end) - - it('should change v:errors when pat matches text', function() - call('assert_notmatch', 'foo', 'foobar') - expected_errors({"Pattern 'foo' does match 'foobar'"}) - end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(1, 'foobar'->assert_notmatch('foo')) - ]] - expected_errors({tmpname .. " line 1: Pattern 'foo' does match 'foobar'"}) - end) end) -- assert_fails({cmd}, [, {error}]) describe('assert_fails', function() - it('should change v:errors when error does not match v:errmsg', function() - eq(1, eval([[assert_fails('xxx', 'E12345')]])) - command([[call assert_match("Expected 'E12345' but got 'E492:", v:errors[0])]]) - expected_errors({"Expected 'E12345' but got 'E492: Not an editor command: xxx': xxx"}) - end) - it('should not change v:errors when cmd errors', function() eq(0, eval([[assert_fails('NonexistentCmd')]])) expected_empty() @@ -296,106 +164,5 @@ describe('assert function:', function() eq(1, eval([[assert_fails('call empty("")', '')]])) expected_errors({'command did not fail: call empty("")'}) end) - - it('can specify and get a message about what failed', function() - eq(1, eval([[assert_fails('xxx', 'E9876', 'stupid')]])) - command([[call assert_match("stupid: Expected 'E9876' but got 'E492:", v:errors[0])]]) - expected_errors({"stupid: Expected 'E9876' but got 'E492: Not an editor command: xxx': stupid"}) - end) - - it('can specify and get a message even when cmd succeeds', function() - eq(1, eval([[assert_fails('echo', '', 'echo command')]])) - expected_errors({'command did not fail: echo command'}) - end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(1, 'echo'->assert_fails('', 'echo command')) - ]] - expected_errors({ - tmpname .. ' line 1: command did not fail: echo command' - }) - end) - end) - - -- assert_inrange({lower}, {upper}, {actual}[, {msg}]) - describe('assert_inrange()', function() - it('should not change v:errors when actual is in range', function() - call('assert_inrange', 7, 7, 7) - call('assert_inrange', 5, 7, 5) - call('assert_inrange', 5, 7, 6) - call('assert_inrange', 5, 7, 7) - expected_empty() - end) - - it('should change v:errors when actual is not in range', function() - call('assert_inrange', 5, 7, 4) - call('assert_inrange', 5, 7, 8) - expected_errors({ - "Expected range 5 - 7, but got 4", - "Expected range 5 - 7, but got 8", - }) - end) - - it('assert_inrange(1, 1) returns E119', function() - eq('Vim(call):E119: Not enough arguments for function: assert_inrange', - exc_exec("call assert_inrange(1, 1)")) - end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(0, 5->assert_inrange(5, 7)) - call assert_equal(0, 7->assert_inrange(5, 7)) - call assert_equal(1, 8->assert_inrange(5, 7)) - ]] - expected_errors({tmpname .. ' line 3: Expected range 5 - 7, but got 8'}) - end) - end) - - -- assert_report({msg}) - describe('assert_report()', function() - it('should add a message to v:errors', function() - eq(1, call('assert_report', 'something is wrong')) - command("call assert_match('something is wrong', v:errors[0])") - command('call remove(v:errors, 0)') - expected_empty() - end) - - it('can be used as a method', function() - local tmpname = source [[ - call assert_equal(1, 'also wrong'->assert_report()) - ]] - expected_errors({tmpname .. ' line 1: also wrong'}) - end) - end) - - -- assert_exception({cmd}, [, {error}]) - describe('assert_exception()', function() - it('should assert thrown exceptions properly', function() - source([[ - try - nocommand - catch - call assert_equal(0, assert_exception('E492')) - endtry - ]]) - expected_empty() - end) - - it('should work properly when nested', function() - source([[ - try - nocommand - catch - try - " illegal argument, get NULL for error - call assert_equal(1, assert_exception([])) - catch - call assert_equal(0, assert_exception('E730')) - endtry - endtry - ]]) - expected_empty() - end) end) end) diff --git a/test/functional/legacy/cdo_spec.lua b/test/functional/legacy/cdo_spec.lua deleted file mode 100644 index 8b3216cbfd..0000000000 --- a/test/functional/legacy/cdo_spec.lua +++ /dev/null @@ -1,228 +0,0 @@ --- Tests for the :cdo, :cfdo, :ldo and :lfdo commands - -local helpers = require('test.functional.helpers')(after_each) -local nvim, clear = helpers.meths, helpers.clear -local call, feed = helpers.call, helpers.feed -local source, eq = helpers.source, helpers.eq - -local function expected_empty() - eq({}, nvim.get_vvar('errors')) -end - -describe('cdo', function() - before_each(function() - clear() - - call('writefile', {'Line1', 'Line2', 'Line3'}, 'Xtestfile1') - call('writefile', {'Line1', 'Line2', 'Line3'}, 'Xtestfile2') - call('writefile', {'Line1', 'Line2', 'Line3'}, 'Xtestfile3') - - source([=[ - " Returns the current line in '<filename> <linenum>L <column>C' format - function GetRuler() - return expand('%') . ' ' . line('.') . 'L' . ' ' . col('.') . 'C' - endfunction - - " Tests for the :cdo and :ldo commands - function XdoTests(cchar) - enew - - " Shortcuts for calling the cdo and ldo commands - let Xdo = a:cchar . 'do' - let Xgetexpr = a:cchar . 'getexpr' - let Xprev = a:cchar. 'prev' - let XdoCmd = Xdo . ' call add(l, GetRuler())' - - " Try with an empty list - let l = [] - exe XdoCmd - call assert_equal([], l) - - " Populate the list and then try - exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:3:1:Line3']" - - let l = [] - exe XdoCmd - call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) - - " Run command only on selected error lines - let l = [] - enew - exe "2,3" . XdoCmd - call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) - - " Boundary condition tests - let l = [] - enew - exe "1,1" . XdoCmd - call assert_equal(['Xtestfile1 1L 3C'], l) - - let l = [] - enew - exe "3" . XdoCmd - call assert_equal(['Xtestfile3 3L 1C'], l) - - " Range test commands - let l = [] - enew - exe "%" . XdoCmd - call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) - - let l = [] - enew - exe "1,$" . XdoCmd - call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 3L 1C'], l) - - let l = [] - enew - exe Xprev - exe "." . XdoCmd - call assert_equal(['Xtestfile2 2L 2C'], l) - - let l = [] - enew - exe "+" . XdoCmd - call assert_equal(['Xtestfile3 3L 1C'], l) - - " Invalid error lines test - let l = [] - enew - exe "silent! 27" . XdoCmd - exe "silent! 4,5" . XdoCmd - call assert_equal([], l) - - " Run commands from an unsaved buffer when 'hidden' is unset - set nohidden - let v:errmsg='' - let l = [] - enew - setlocal modified - exe "silent! 2,2" . XdoCmd - if v:errmsg !~# 'No write since last change' - call add(v:errors, 'Unsaved file change test failed') - endif - - " If the executed command fails, then the operation should be aborted - enew! - let subst_count = 0 - exe "silent!" . Xdo . " s/Line/xLine/ | let subst_count += 1" - if subst_count != 1 || getline('.') != 'xLine1' - call add(v:errors, 'Abort command on error test failed') - endif - set hidden - - let l = [] - exe "2,2" . Xdo . "! call add(l, GetRuler())" - call assert_equal(['Xtestfile2 2L 2C'], l) - - " List with no valid error entries - let l = [] - edit! +2 Xtestfile1 - exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']" - exe XdoCmd - call assert_equal([], l) - exe "silent! 2" . XdoCmd - call assert_equal([], l) - let v:errmsg='' - exe "%" . XdoCmd - exe "1,$" . XdoCmd - exe "." . XdoCmd - call assert_equal('', v:errmsg) - - " List with only one valid entry - let l = [] - exe Xgetexpr . " ['Xtestfile3:3:1:Line3']" - exe XdoCmd - call assert_equal(['Xtestfile3 3L 1C'], l) - - endfunction - - " Tests for the :cfdo and :lfdo commands - function XfdoTests(cchar) - enew - - " Shortcuts for calling the cfdo and lfdo commands - let Xfdo = a:cchar . 'fdo' - let Xgetexpr = a:cchar . 'getexpr' - let XfdoCmd = Xfdo . ' call add(l, GetRuler())' - let Xpfile = a:cchar. 'pfile' - - " Clear the quickfix/location list - exe Xgetexpr . " []" - - " Try with an empty list - let l = [] - exe XfdoCmd - call assert_equal([], l) - - " Populate the list and then try - exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', 'Xtestfile1:2:1:Line2', 'non-error 2', 'Xtestfile2:2:2:Line2', 'non-error 3', 'Xtestfile3:2:3:Line2', 'Xtestfile3:3:1:Line3']" - - let l = [] - exe XfdoCmd - call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) - - " Run command only on selected error lines - let l = [] - exe "2,3" . XfdoCmd - call assert_equal(['Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) - - " Boundary condition tests - let l = [] - exe "3" . XfdoCmd - call assert_equal(['Xtestfile3 2L 3C'], l) - - " Range test commands - let l = [] - exe "%" . XfdoCmd - call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) - - let l = [] - exe "1,$" . XfdoCmd - call assert_equal(['Xtestfile1 1L 3C', 'Xtestfile2 2L 2C', 'Xtestfile3 2L 3C'], l) - - let l = [] - exe Xpfile - exe "." . XfdoCmd - call assert_equal(['Xtestfile2 2L 2C'], l) - - " List with only one valid entry - let l = [] - exe Xgetexpr . " ['Xtestfile2:2:5:Line2']" - exe XfdoCmd - call assert_equal(['Xtestfile2 2L 5C'], l) - - endfunction - ]=]) - end) - - after_each(function() - os.remove('Xtestfile1') - os.remove('Xtestfile2') - os.remove('Xtestfile3') - end) - - it('works for :cdo', function() - -- call('XdoTests', 'c') - feed(":call XdoTests('c')<CR><C-l>") - expected_empty() - end) - - it('works for :cfdo', function() - -- call('XfdoTests', 'c') - feed(":call XfdoTests('c')<CR><C-l>") - expected_empty() - end) - - it('works for :ldo', function() - -- call('XdoTests', 'l') - feed(":call XdoTests('l')<CR><C-l>") - expected_empty() - end) - - it('works for :lfdo', function() - -- call('XfdoTests', 'l') - feed(":call XfdoTests('l')<CR><C-l>") - expected_empty() - end) -end) diff --git a/test/functional/legacy/eval_spec.lua b/test/functional/legacy/eval_spec.lua index b5de5cd232..05d853622e 100644 --- a/test/functional/legacy/eval_spec.lua +++ b/test/functional/legacy/eval_spec.lua @@ -666,7 +666,7 @@ describe('eval', function() source([[ " Vim script used in test_eval.in. Needed for script-local function. - func! s:Testje() + func s:Testje() return "foo" endfunc diff --git a/test/functional/legacy/ex_mode_spec.lua b/test/functional/legacy/ex_mode_spec.lua new file mode 100644 index 0000000000..44719027a6 --- /dev/null +++ b/test/functional/legacy/ex_mode_spec.lua @@ -0,0 +1,36 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local command = helpers.command +local eq = helpers.eq +local eval = helpers.eval +local feed = helpers.feed +local meths = helpers.meths + +before_each(clear) + +describe('Ex mode', function() + it('supports command line editing', function() + local function test_ex_edit(expected, cmd) + feed('gQ' .. cmd .. '<C-b>"<CR>') + local ret = eval('@:[1:]') -- Remove leading quote. + feed('visual<CR>') + eq(meths.replace_termcodes(expected, true, true, true), ret) + end + command('set sw=2') + test_ex_edit('bar', 'foo bar<C-u>bar') + test_ex_edit('1<C-u>2', '1<C-v><C-u>2') + test_ex_edit('213', '1<C-b>2<C-e>3') + test_ex_edit('2013', '01<Home>2<End>3') + test_ex_edit('0213', '01<Left>2<Right>3') + test_ex_edit('0342', '012<Left><Left><Insert>3<Insert>4') + test_ex_edit('foo ', 'foo bar<C-w>') + test_ex_edit('foo', 'fooba<Del><Del>') + test_ex_edit('foobar', 'foo<Tab>bar') + test_ex_edit('abbreviate', 'abbrev<Tab>') + test_ex_edit('1<C-t><C-t>', '1<C-t><C-t>') + test_ex_edit('1<C-t><C-t>', '1<C-t><C-t><C-d>') + test_ex_edit(' foo', ' foo<C-d>') + test_ex_edit(' foo0', ' foo0<C-d>') + test_ex_edit(' foo^', ' foo^<C-d>') + end) +end) diff --git a/test/functional/legacy/expand_spec.lua b/test/functional/legacy/expand_spec.lua deleted file mode 100644 index cd3713eabe..0000000000 --- a/test/functional/legacy/expand_spec.lua +++ /dev/null @@ -1,129 +0,0 @@ --- Test for expanding file names - -local helpers = require('test.functional.helpers')(after_each) -local eq = helpers.eq -local call = helpers.call -local nvim = helpers.meths -local clear = helpers.clear -local source = helpers.source - -local function expected_empty() - eq({}, nvim.get_vvar('errors')) -end - -describe('expand file name', function() - after_each(function() - helpers.rmdir('Xdir1') - helpers.rmdir('Xdir2') - helpers.rmdir('Xdir3') - helpers.rmdir('Xdir4') - end) - - before_each(function() - clear() - - source([[ - func Test_with_directories() - call mkdir('Xdir1') - call mkdir('Xdir2') - call mkdir('Xdir3') - cd Xdir3 - call mkdir('Xdir4') - cd .. - - split Xdir1/file - call setline(1, ['a', 'b']) - w - w Xdir3/Xdir4/file - close - - next Xdir?/*/file - call assert_equal('Xdir3/Xdir4/file', expand('%')) - if has('unix') - next! Xdir?/*/nofile - call assert_equal('Xdir?/*/nofile', expand('%')) - endif - " Edit another file, on MS-Windows the swap file would be in use and can't - " be deleted - edit foo - - call assert_equal(0, delete('Xdir1', 'rf')) - call assert_equal(0, delete('Xdir2', 'rf')) - call assert_equal(0, delete('Xdir3', 'rf')) - endfunc - - func Test_with_tilde() - let dir = getcwd() - call mkdir('Xdir ~ dir') - call assert_true(isdirectory('Xdir ~ dir')) - cd Xdir\ ~\ dir - call assert_true(getcwd() =~ 'Xdir \~ dir') - exe 'cd ' . fnameescape(dir) - call delete('Xdir ~ dir', 'd') - call assert_false(isdirectory('Xdir ~ dir')) - endfunc - - func Test_expand_tilde_filename() - split ~ - call assert_equal('~', expand('%')) - call assert_notequal(expand('%:p'), expand('~/')) - call assert_match('\~', expand('%:p')) - bwipe! - endfunc - - func Test_expandcmd() - let $FOO = 'Test' - call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y')) - unlet $FOO - - new - edit Xfile1 - call assert_equal('e Xfile1', expandcmd('e %')) - edit Xfile2 - edit Xfile1 - call assert_equal('e Xfile2', 'e #'->expandcmd()) - edit Xfile2 - edit Xfile3 - edit Xfile4 - let bnum = bufnr('Xfile2') - call assert_equal('e Xfile2', expandcmd('e #' . bnum)) - call setline('.', 'Vim!@#') - call assert_equal('e Vim', expandcmd('e <cword>')) - call assert_equal('e Vim!@#', expandcmd('e <cWORD>')) - enew! - edit Xfile.java - call assert_equal('e Xfile.py', expandcmd('e %:r.py')) - call assert_equal('make abc.java', expandcmd('make abc.%:e')) - call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?')) - edit a1a2a3.rb - call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o')) - - call assert_fails('call expandcmd("make <afile>")', 'E495:') - call assert_fails('call expandcmd("make <afile>")', 'E495:') - enew - call assert_fails('call expandcmd("make %")', 'E499:') - close - endfunc - ]]) - end) - - it('works with directories', function() - call('Test_with_directories') - expected_empty() - end) - - it('works with tilde', function() - call('Test_with_tilde') - expected_empty() - end) - - it('does not expand tilde if it is a filename', function() - call('Test_expand_tilde_filename') - expected_empty() - end) - - it('works with expandcmd()', function() - call('Test_expandcmd') - expected_empty() - end) -end) diff --git a/test/functional/legacy/filechanged_spec.lua b/test/functional/legacy/filechanged_spec.lua new file mode 100644 index 0000000000..fed7b27b0c --- /dev/null +++ b/test/functional/legacy/filechanged_spec.lua @@ -0,0 +1,76 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear, source = helpers.clear, helpers.source +local call, eq, meths = helpers.call, helpers.eq, helpers.meths + +local function expected_empty() + eq({}, meths.get_vvar('errors')) +end + +describe('file changed dialog', function() + before_each(function() + clear() + meths.ui_attach(80, 24, {}) + meths.set_option('autoread', false) + source([[ + func Test_file_changed_dialog() + au! FileChangedShell + + new Xchanged_d + call setline(1, 'reload this') + write + " Need to wait until the timestamp would change by at least a second. + sleep 2 + silent !echo 'extra line' >>Xchanged_d + call nvim_input('L') + checktime + call assert_match('W11:', v:warningmsg) + call assert_equal(2, line('$')) + call assert_equal('reload this', getline(1)) + call assert_equal('extra line', getline(2)) + + " delete buffer, only shows an error, no prompt + silent !rm Xchanged_d + checktime + call assert_match('E211:', v:warningmsg) + call assert_equal(2, line('$')) + call assert_equal('extra line', getline(2)) + let v:warningmsg = 'empty' + + " change buffer, recreate the file and reload + call setline(1, 'buffer is changed') + silent !echo 'new line' >Xchanged_d + call nvim_input('L') + checktime + call assert_match('W12:', v:warningmsg) + call assert_equal(1, line('$')) + call assert_equal('new line', getline(1)) + + " Only mode changed, reload + silent !chmod +x Xchanged_d + call nvim_input('L') + checktime + call assert_match('W16:', v:warningmsg) + call assert_equal(1, line('$')) + call assert_equal('new line', getline(1)) + + " Only time changed, no prompt + sleep 2 + silent !touch Xchanged_d + let v:warningmsg = '' + checktime + call assert_equal('', v:warningmsg) + call assert_equal(1, line('$')) + call assert_equal('new line', getline(1)) + + bwipe! + call delete('Xchanged_d') + endfunc + ]]) + end) + + it('works', function() + if helpers.pending_win32(pending) then return end + call('Test_file_changed_dialog') + expected_empty() + end) +end) diff --git a/test/functional/legacy/listchars_spec.lua b/test/functional/legacy/listchars_spec.lua index dc6ccd3628..7a1afa1fd6 100644 --- a/test/functional/legacy/listchars_spec.lua +++ b/test/functional/legacy/listchars_spec.lua @@ -1,7 +1,8 @@ -- Tests for 'listchars' display with 'list' and :list. local helpers = require('test.functional.helpers')(after_each) -local feed, insert, source = helpers.feed, helpers.insert, helpers.source +local Screen = require('test.functional.ui.screen') +local feed, insert, exec = helpers.feed, helpers.insert, helpers.exec local clear, feed_command, expect = helpers.clear, helpers.feed_command, helpers.expect -- luacheck: ignore 621 (Indentation) @@ -13,7 +14,7 @@ describe("'listchars'", function() -- luacheck: ignore 613 (Trailing whitespace in a string) it("works with 'list'", function() - source([[ + exec([[ function GetScreenCharsForLine(lnum) return join(map(range(1, virtcol('$')), 'nr2char(screenchar(a:lnum, v:val))'), '') endfunction @@ -98,4 +99,80 @@ describe("'listchars'", function() .....h>-$ iii<<<<><<$]]) end) + + it('"exceeds" character does not appear in foldcolumn vim-patch:8.2.3121', function() + local screen = Screen.new(60, 10) + screen:attach() + exec([[ + call setline(1, ['aaa', '', 'a', 'aaaaaa']) + vsplit + vsplit + windo set signcolumn=yes foldcolumn=1 winminwidth=0 nowrap list listchars=extends:>,precedes:< + ]]) + feed('13<C-W>>') + screen:expect([[ + aaa │ a>│ ^aaa | + │ │ | + a │ a │ a | + aaaaaa │ a>│ aaaaaa | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + [No Name] [+] <[+] [No Name] [+] | + | + ]]) + feed('<C-W>>') + screen:expect([[ + aaa │ >│ ^aaa | + │ │ | + a │ a│ a | + aaaaaa │ >│ aaaaaa | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + [No Name] [+] <+] [No Name] [+] | + | + ]]) + feed('<C-W>>') + screen:expect([[ + aaa │ │ ^aaa | + │ │ | + a │ │ a | + aaaaaa │ │ aaaaaa | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + [No Name] [+] <] [No Name] [+] | + | + ]]) + feed('<C-W>>') + screen:expect([[ + aaa │ │ ^aaa | + │ │ | + a │ │ a | + aaaaaa │ │ aaaaaa | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + ~ │~ │~ | + [No Name] [+] < [No Name] [+] | + | + ]]) + feed('<C-W>>') + screen:expect([[ + aaa │ │ ^aaa | + │ │ | + a │ │ a | + aaaaaa │ │ aaaaaa | + ~ │~│~ | + ~ │~│~ | + ~ │~│~ | + ~ │~│~ | + [No Name] [+] < [No Name] [+] | + | + ]]) + end) end) diff --git a/test/functional/legacy/memory_usage_spec.lua b/test/functional/legacy/memory_usage_spec.lua index d86caca0e9..8d25b9d927 100644 --- a/test/functional/legacy/memory_usage_spec.lua +++ b/test/functional/legacy/memory_usage_spec.lua @@ -195,10 +195,10 @@ describe('memory usage', function() local after = monitor_memory_usage(pid) source('bwipe!') poke_eventloop() - -- Allow for an increase of 5% in memory usage, which accommodates minor fluctuation, + -- Allow for an increase of 10% in memory usage, which accommodates minor fluctuation, -- but is small enough that if memory were not released (prior to PR #14884), the test -- would fail. - local upper = before.last * 1.05 + local upper = before.last * 1.10 check_result({before=before, after=after}, pcall(ok, after.last <= upper)) end) end) diff --git a/test/functional/legacy/packadd_spec.lua b/test/functional/legacy/packadd_spec.lua deleted file mode 100644 index 4f9f5a0237..0000000000 --- a/test/functional/legacy/packadd_spec.lua +++ /dev/null @@ -1,507 +0,0 @@ --- Tests for 'packpath' and :packadd - -local helpers = require('test.functional.helpers')(after_each) -local clear, source, command = helpers.clear, helpers.source, helpers.command -local call, eq, nvim = helpers.call, helpers.eq, helpers.meths -local feed = helpers.feed - -local function expected_empty() - eq({}, nvim.get_vvar('errors')) -end - -describe('packadd', function() - before_each(function() - clear() - - source([=[ - func Escape(s) - return escape(a:s, '\~') - endfunc - - func SetUp() - let s:topdir = expand(getcwd() . '/Xdir') - if isdirectory(s:topdir) - call delete(s:topdir, 'rf') - endif - exe 'set packpath=' . s:topdir - let s:plugdir = expand(s:topdir . '/pack/mine/opt/mytest') - endfunc - - func TearDown() - call delete(s:topdir, 'rf') - endfunc - - func Test_packadd() - if !exists('s:plugdir') - echomsg 'when running this test manually, call SetUp() first' - return - endif - - call mkdir(s:plugdir . '/plugin/also', 'p') - call mkdir(s:plugdir . '/ftdetect', 'p') - call mkdir(s:plugdir . '/after', 'p') - set rtp& - let rtp = &rtp - filetype on - - let rtp_entries = split(rtp, ',') - for entry in rtp_entries - if entry =~? '\<after\>' - let first_after_entry = entry - break - endif - endfor - - exe 'split ' . s:plugdir . '/plugin/test.vim' - call setline(1, 'let g:plugin_works = 42') - wq - - exe 'split ' . s:plugdir . '/plugin/also/loaded.vim' - call setline(1, 'let g:plugin_also_works = 77') - wq - - exe 'split ' . s:plugdir . '/ftdetect/test.vim' - call setline(1, 'let g:ftdetect_works = 17') - wq - - packadd mytest - - call assert_true(42, g:plugin_works) - call assert_equal(77, g:plugin_also_works) - call assert_true(17, g:ftdetect_works) - call assert_true(len(&rtp) > len(rtp)) - call assert_match(Escape(s:plugdir) . '\($\|,\)', &rtp) - - let new_after = match(&rtp, Escape(expand(s:plugdir . '/after') . ',')) - let forwarded = substitute(first_after_entry, '\\', '[/\\\\]', 'g') - let old_after = match(&rtp, ',' . escape(forwarded, '~') . '\>') - call assert_true(new_after > 0, 'rtp is ' . &rtp) - call assert_true(old_after > 0, 'match ' . forwarded . ' in ' . &rtp) - call assert_true(new_after < old_after, 'rtp is ' . &rtp) - - " NOTE: '/.../opt/myte' forwardly matches with '/.../opt/mytest' - call mkdir(fnamemodify(s:plugdir, ':h') . '/myte', 'p') - let rtp = &rtp - packadd myte - - " Check the path of 'myte' is added - call assert_true(len(&rtp) > len(rtp)) - call assert_match(Escape(s:plugdir) . '\($\|,\)', &rtp) - - " Check exception - call assert_fails("packadd directorynotfound", 'E919:') - call assert_fails("packadd", 'E471:') - endfunc - - func Test_packadd_start() - let plugdir = expand(s:topdir . '/pack/mine/start/other') - call mkdir(plugdir . '/plugin', 'p') - set rtp& - let rtp = &rtp - filetype on - - exe 'split ' . plugdir . '/plugin/test.vim' - call setline(1, 'let g:plugin_works = 24') - wq - - exe 'split ' . plugdir . '/plugin/test.lua' - call setline(1, 'vim.g.plugin_lua_works = 24') - wq - - packadd other - - call assert_equal(24, g:plugin_works) - call assert_equal(24, g:plugin_lua_works) - call assert_true(len(&rtp) > len(rtp)) - call assert_match(Escape(plugdir) . '\($\|,\)', &rtp) - endfunc - - func Test_packadd_noload() - call mkdir(s:plugdir . '/plugin', 'p') - call mkdir(s:plugdir . '/syntax', 'p') - set rtp& - let rtp = &rtp - - exe 'split ' . s:plugdir . '/plugin/test.vim' - call setline(1, 'let g:plugin_works = 42') - wq - exe 'split ' . s:plugdir . '/plugin/test.lua' - call setline(1, 'let g:plugin_lua_works = 42') - wq - let g:plugin_works = 0 - let g:plugin_lua_works = 0 - - packadd! mytest - - call assert_true(len(&rtp) > len(rtp)) - call assert_match(Escape(s:plugdir) . '\($\|,\)', &rtp) - call assert_equal(0, g:plugin_works) - call assert_equal(0, g:plugin_lua_works) - - " check the path is not added twice - let new_rtp = &rtp - packadd! mytest - call assert_equal(new_rtp, &rtp) - endfunc - - func Test_packadd_symlink_dir() - let top2_dir = expand(s:topdir . '/Xdir2') - let real_dir = expand(s:topdir . '/Xsym') - call mkdir(real_dir, 'p') - if has('win32') - exec "silent! !mklink /d" top2_dir "Xsym" - else - exec "silent! !ln -s Xsym" top2_dir - endif - let &rtp = top2_dir . ',' . expand(top2_dir . '/after') - let &packpath = &rtp - - let s:plugdir = expand(top2_dir . '/pack/mine/opt/mytest') - call mkdir(s:plugdir . '/plugin', 'p') - - exe 'split ' . s:plugdir . '/plugin/test.vim' - call setline(1, 'let g:plugin_works = 44') - wq - let g:plugin_works = 0 - - packadd mytest - - " Must have been inserted in the middle, not at the end - call assert_match(Escape(expand('/pack/mine/opt/mytest').','), &rtp) - call assert_equal(44, g:plugin_works) - - " No change when doing it again. - let rtp_before = &rtp - packadd mytest - call assert_equal(rtp_before, &rtp) - - set rtp& - let rtp = &rtp - exec "silent !" (has('win32') ? "rd /q/s" : "rm") top2_dir - endfunc - - func Test_packadd_symlink_dir2() - let top2_dir = expand(s:topdir . '/Xdir2') - let real_dir = expand(s:topdir . '/Xsym/pack') - call mkdir(top2_dir, 'p') - call mkdir(real_dir, 'p') - let &rtp = top2_dir . ',' . top2_dir . '/after' - let &packpath = &rtp - - if has('win32') - exec "silent! !mklink /d" top2_dir "Xsym" - else - exec "silent !ln -s ../Xsym/pack" top2_dir . '/pack' - endif - let s:plugdir = expand(top2_dir . '/pack/mine/opt/mytest') - call mkdir(s:plugdir . '/plugin', 'p') - - exe 'split ' . s:plugdir . '/plugin/test.vim' - call setline(1, 'let g:plugin_works = 48') - wq - let g:plugin_works = 0 - - packadd mytest - - " Must have been inserted in the middle, not at the end - call assert_match(Escape(expand('/Xdir2/pack/mine/opt/mytest').','), &rtp) - call assert_equal(48, g:plugin_works) - - " No change when doing it again. - let rtp_before = &rtp - packadd mytest - call assert_equal(rtp_before, &rtp) - - set rtp& - let rtp = &rtp - if has('win32') - exec "silent !rd /q/s" top2_dir - else - exec "silent !rm" top2_dir . '/pack' - exec "silent !rmdir" top2_dir - endif - endfunc - - func Test_packloadall() - " plugin foo with an autoload directory - let fooplugindir = &packpath . '/pack/mine/start/foo/plugin' - call mkdir(fooplugindir, 'p') - call writefile(['let g:plugin_foo_number = 1234', - \ 'let g:plugin_foo_auto = bbb#value', - \ 'let g:plugin_extra_auto = extra#value'], fooplugindir . '/bar.vim') - let fooautodir = &packpath . '/pack/mine/start/foo/autoload' - call mkdir(fooautodir, 'p') - call writefile(['let bar#value = 77'], fooautodir . '/bar.vim') - - " plugin aaa with an autoload directory - let aaaplugindir = &packpath . '/pack/mine/start/aaa/plugin' - call mkdir(aaaplugindir, 'p') - call writefile(['let g:plugin_aaa_number = 333', - \ 'let g:plugin_aaa_auto = bar#value'], aaaplugindir . '/bbb.vim') - let aaaautodir = &packpath . '/pack/mine/start/aaa/autoload' - call mkdir(aaaautodir, 'p') - call writefile(['let bbb#value = 55'], aaaautodir . '/bbb.vim') - - " plugin extra with only an autoload directory - let extraautodir = &packpath . '/pack/mine/start/extra/autoload' - call mkdir(extraautodir, 'p') - call writefile(['let extra#value = 99'], extraautodir . '/extra.vim') - - packloadall - call assert_equal(1234, g:plugin_foo_number) - call assert_equal(55, g:plugin_foo_auto) - call assert_equal(99, g:plugin_extra_auto) - call assert_equal(333, g:plugin_aaa_number) - call assert_equal(77, g:plugin_aaa_auto) - - " only works once - call writefile(['let g:plugin_bar_number = 4321'], - \ fooplugindir . '/bar2.vim') - packloadall - call assert_false(exists('g:plugin_bar_number')) - - " works when ! used - packloadall! - call assert_equal(4321, g:plugin_bar_number) - endfunc - - func Test_helptags() - let docdir1 = &packpath . '/pack/mine/start/foo/doc' - let docdir2 = &packpath . '/pack/mine/start/bar/doc' - call mkdir(docdir1, 'p') - call mkdir(docdir2, 'p') - call writefile(['look here: *look-here*'], docdir1 . '/bar.txt') - call writefile(['look away: *look-away*'], docdir2 . '/foo.txt') - exe 'set rtp=' . &packpath . '/pack/mine/start/foo,' . &packpath . '/pack/mine/start/bar' - - helptags ALL - - let tags1 = readfile(docdir1 . '/tags') - call assert_match('look-here', tags1[0]) - let tags2 = readfile(docdir2 . '/tags') - call assert_match('look-away', tags2[0]) - - call assert_fails('helptags abcxyz', 'E150:') - endfunc - - func Test_colorscheme() - let colordirrun = &packpath . '/runtime/colors' - let colordirstart = &packpath . '/pack/mine/start/foo/colors' - let colordiropt = &packpath . '/pack/mine/opt/bar/colors' - call mkdir(colordirrun, 'p') - call mkdir(colordirstart, 'p') - call mkdir(colordiropt, 'p') - call writefile(['let g:found_one = 1'], colordirrun . '/one.vim') - call writefile(['let g:found_two = 1'], colordirstart . '/two.vim') - call writefile(['let g:found_three = 1'], colordiropt . '/three.vim') - exe 'set rtp=' . &packpath . '/runtime' - - colorscheme one - call assert_equal(1, g:found_one) - colorscheme two - call assert_equal(1, g:found_two) - colorscheme three - call assert_equal(1, g:found_three) - endfunc - - func Test_runtime() - let rundir = &packpath . '/runtime/extra' - let startdir = &packpath . '/pack/mine/start/foo/extra' - let optdir = &packpath . '/pack/mine/opt/bar/extra' - call mkdir(rundir, 'p') - call mkdir(startdir, 'p') - call mkdir(optdir, 'p') - call writefile(['let g:sequence .= "run"'], rundir . '/bar.vim') - call writefile(['let g:sequence .= "start"'], startdir . '/bar.vim') - call writefile(['let g:sequence .= "foostart"'], startdir . '/foo.vim') - call writefile(['let g:sequence .= "opt"'], optdir . '/bar.vim') - call writefile(['let g:sequence .= "xxxopt"'], optdir . '/xxx.vim') - exe 'set rtp=' . &packpath . '/runtime' - - let g:sequence = '' - runtime extra/bar.vim - call assert_equal('run', g:sequence) - let g:sequence = '' - runtime START extra/bar.vim - call assert_equal('start', g:sequence) - let g:sequence = '' - runtime OPT extra/bar.vim - call assert_equal('opt', g:sequence) - let g:sequence = '' - runtime PACK extra/bar.vim - call assert_equal('start', g:sequence) - let g:sequence = '' - runtime! PACK extra/bar.vim - call assert_equal('startopt', g:sequence) - let g:sequence = '' - runtime PACK extra/xxx.vim - call assert_equal('xxxopt', g:sequence) - - let g:sequence = '' - runtime ALL extra/bar.vim - call assert_equal('run', g:sequence) - let g:sequence = '' - runtime ALL extra/foo.vim - call assert_equal('foostart', g:sequence) - let g:sequence = '' - runtime! ALL extra/xxx.vim - call assert_equal('xxxopt', g:sequence) - let g:sequence = '' - runtime! ALL extra/bar.vim - call assert_equal('runstartopt', g:sequence) - endfunc - ]=]) - call('SetUp') - end) - - after_each(function() - call('TearDown') - end) - - it('is working', function() - call('Test_packadd') - expected_empty() - end) - - it('works with packadd!', function() - call('Test_packadd_noload') - expected_empty() - end) - - it('works with symlinks', function() - call('Test_packadd_symlink_dir') - expected_empty() - end) - - it('works with :packloadall', function() - call('Test_packloadall') - expected_empty() - end) - - it('works with helptags', function() - call('Test_helptags') - expected_empty() - end) - - it('works with colorschemes', function() - call('Test_colorscheme') - expected_empty() - end) - - it('works with :runtime [what]', function() - call('Test_runtime') - expected_empty() - end) - - it('loads packages from "start" directory', function() - call('Test_packadd_start') - expected_empty() - end) - - describe('command line completion', function() - local Screen = require('test.functional.ui.screen') - local screen - - before_each(function() - screen = Screen.new(30, 5) - screen:attach() - screen:set_default_attr_ids({ - [0] = {bold=true, foreground=Screen.colors.Blue}, - [1] = { - foreground = Screen.colors.Black, - background = Screen.colors.Yellow, - }, - [2] = {bold = true, reverse = true} - }) - - command([[let optdir1 = &packpath . '/pack/mine/opt']]) - command([[let optdir2 = &packpath . '/pack/candidate/opt']]) - command([[call mkdir(optdir1 . '/pluginA', 'p')]]) - command([[call mkdir(optdir1 . '/pluginC', 'p')]]) - command([[call mkdir(optdir2 . '/pluginB', 'p')]]) - command([[call mkdir(optdir2 . '/pluginC', 'p')]]) - end) - - it('works', function() - feed(':packadd <Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {1:pluginA}{2: pluginB pluginC }| - :packadd pluginA^ | - ]=]) - feed('<Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {2:pluginA }{1:pluginB}{2: pluginC }| - :packadd pluginB^ | - ]=]) - feed('<Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {2:pluginA pluginB }{1:pluginC}{2: }| - :packadd pluginC^ | - ]=]) - feed('<Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {2:pluginA pluginB pluginC }| - :packadd ^ | - ]=]) - end) - - it('works for colorschemes', function() - source([[ - let colordirrun = &packpath . '/runtime/colors' - let colordirstart = &packpath . '/pack/mine/start/foo/colors' - let colordiropt = &packpath . '/pack/mine/opt/bar/colors' - call mkdir(colordirrun, 'p') - call mkdir(colordirstart, 'p') - call mkdir(colordiropt, 'p') - call writefile(['let g:found_one = 1'], colordirrun . '/one.vim') - call writefile(['let g:found_two = 1'], colordirstart . '/two.vim') - call writefile(['let g:found_three = 1'], colordiropt . '/three.vim') - exe 'set rtp=' . &packpath . '/runtime']]) - - feed(':colorscheme <Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {1:one}{2: three two }| - :colorscheme one^ | - ]=]) - feed('<Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {2:one }{1:three}{2: two }| - :colorscheme three^ | - ]=]) - feed('<Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {2:one three }{1:two}{2: }| - :colorscheme two^ | - ]=]) - feed('<Tab>') - screen:expect([=[ - | - {0:~ }| - {0:~ }| - {2:one three two }| - :colorscheme ^ | - ]=]) - end) - end) -end) diff --git a/test/functional/legacy/search_stat_spec.lua b/test/functional/legacy/search_stat_spec.lua new file mode 100644 index 0000000000..fdd46c0cb9 --- /dev/null +++ b/test/functional/legacy/search_stat_spec.lua @@ -0,0 +1,121 @@ +local helpers = require('test.functional.helpers')(after_each) +local Screen = require('test.functional.ui.screen') +local clear, feed, exec, command = helpers.clear, helpers.feed, helpers.exec, helpers.command +local poke_eventloop = helpers.poke_eventloop + +describe('search stat', function() + local screen + before_each(function() + clear() + screen = Screen.new(30, 10) + screen:set_default_attr_ids({ + [1] = {bold = true, foreground = Screen.colors.Blue}, -- NonText + [2] = {background = Screen.colors.Yellow}, -- Search + [3] = {foreground = Screen.colors.Blue4, background = Screen.colors.LightGrey}, -- Folded + }) + screen:attach() + end) + + it('right spacing with silent mapping vim-patch:8.1.1970', function() + exec([[ + set shortmess-=S + " Append 50 lines with text to search for, "foobar" appears 20 times + call append(0, repeat(['foobar', 'foo', 'fooooobar', 'foba', 'foobar'], 20)) + call setline(2, 'find this') + call setline(70, 'find this') + nnoremap n n + let @/ = 'find this' + call cursor(1,1) + norm n + ]]) + screen:expect([[ + foobar | + {2:^find this} | + fooooobar | + foba | + foobar | + foobar | + foo | + fooooobar | + foba | + /find this [1/2] | + ]]) + command('nnoremap <silent> n n') + feed('gg0n') + screen:expect([[ + foobar | + {2:^find this} | + fooooobar | + foba | + foobar | + foobar | + foo | + fooooobar | + foba | + [1/2] | + ]]) + end) + + it('when only match is in fold vim-patch:8.2.0840', function() + exec([[ + set shortmess-=S + setl foldenable foldmethod=indent foldopen-=search + call append(0, ['if', "\tfoo", "\tfoo", 'endif']) + let @/ = 'foo' + call cursor(1,1) + norm n + ]]) + screen:expect([[ + if | + {3:^+-- 2 lines: foo·············}| + endif | + | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + /foo [1/2] | + ]]) + feed('n') + poke_eventloop() + screen:expect_unchanged() + feed('n') + poke_eventloop() + screen:expect_unchanged() + end) + + it('is cleared by gd and gD vim-patch:8.2.3583', function() + exec([[ + call setline(1, ['int cat;', 'int dog;', 'cat = dog;']) + set shortmess-=S + set hlsearch + ]]) + feed('/dog<CR>') + screen:expect([[ + int cat; | + int {2:^dog}; | + cat = {2:dog}; | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + /dog [1/2] | + ]]) + feed('G0gD') + screen:expect([[ + int {2:^cat}; | + int dog; | + {2:cat} = dog; | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + | + ]]) + end) +end) diff --git a/test/functional/legacy/visual_mode_spec.lua b/test/functional/legacy/visual_mode_spec.lua index c8e83ed649..8b5dd0c2dc 100644 --- a/test/functional/legacy/visual_mode_spec.lua +++ b/test/functional/legacy/visual_mode_spec.lua @@ -1,5 +1,3 @@ --- Test visual line mode selection redraw after scrolling - local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') @@ -10,6 +8,7 @@ local feed_command = helpers.feed_command local funcs = helpers.funcs local meths = helpers.meths local eq = helpers.eq +local exec = helpers.exec describe('visual line mode', function() local screen @@ -40,3 +39,44 @@ describe('visual line mode', function() ]]) end) end) + +describe('visual block mode', function() + it('shows selection correctly with virtualedit=block', function() + clear() + local screen = Screen.new(30, 7) + screen:set_default_attr_ids({ + [1] = {bold = true}, -- ModeMsg + [2] = {background = Screen.colors.LightGrey}, -- Visual + [3] = {foreground = Screen.colors.Blue, bold = true} -- NonText + }) + screen:attach() + + exec([[ + call setline(1, ['aaaaaa', 'bbbb', 'cc']) + set virtualedit=block + normal G + ]]) + + feed('<C-V>gg$') + screen:expect([[ + {2:aaaaaa}^ | + {2:bbbb } | + {2:cc } | + {3:~ }| + {3:~ }| + {3:~ }| + {1:-- VISUAL BLOCK --} | + ]]) + + feed('<Esc>gg<C-V>G$') + screen:expect([[ + {2:aaaaaa } | + {2:bbbb } | + {2:cc}^ {2: } | + {3:~ }| + {3:~ }| + {3:~ }| + {1:-- VISUAL BLOCK --} | + ]]) + end) +end) diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua index 9b9ba531b0..b8346df290 100644 --- a/test/functional/lua/commands_spec.lua +++ b/test/functional/lua/commands_spec.lua @@ -141,6 +141,24 @@ describe(':lua command', function() {4:Press ENTER or type command to continue}^ | ]]} end) + + it('Can print results of =expr', function() + helpers.exec_lua("x = 5") + eq("5", helpers.exec_capture(':lua =x')) + helpers.exec_lua("function x() return 'hello' end") + eq([["hello"]], helpers.exec_capture(':lua = x()')) + helpers.exec_lua("x = {a = 1, b = 2}") + eq("{\n a = 1,\n b = 2\n}", helpers.exec_capture(':lua =x')) + helpers.exec_lua([[function x(success) + if success then + return true, "Return value" + else + return false, nil, "Error message" + end + end]]) + eq([[true "Return value"]], helpers.exec_capture(':lua =x(true)')) + eq([[false nil "Error message"]], helpers.exec_capture(':lua =x(false)')) + end) end) describe(':luado command', function() diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 7d260f2e29..b58fad1cab 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -208,10 +208,10 @@ describe('vim.diagnostic', function() eq(all_highlights, exec_lua [[ local ns_1_diags = { make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + make_warning("Warning on Server 1", 2, 1, 2, 3), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + make_warning("Warning 1", 2, 1, 2, 3), } vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) @@ -255,10 +255,10 @@ describe('vim.diagnostic', function() eq({0, 2}, exec_lua [[ local ns_1_diags = { make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + make_warning("Warning on Server 1", 2, 1, 2, 3), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + make_warning("Warning 1", 2, 1, 2, 3), } vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) @@ -599,10 +599,10 @@ describe('vim.diagnostic', function() eq(all_highlights, exec_lua [[ local ns_1_diags = { make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 2, 1, 2, 5), + make_warning("Warning on Server 1", 2, 1, 2, 3), } local ns_2_diags = { - make_warning("Warning 1", 2, 1, 2, 5), + make_warning("Warning 1", 2, 1, 2, 3), } vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, ns_1_diags) @@ -787,7 +787,7 @@ describe('vim.diagnostic', function() eq(2, exec_lua [[ vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 5), + make_warning("Warning on Server 1", 1, 1, 2, 3), }) return #vim.diagnostic.get(diagnostic_bufnr) @@ -798,9 +798,9 @@ describe('vim.diagnostic', function() eq({2, 3, 2}, exec_lua [[ vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 5), - make_info("Ignored information", 1, 1, 2, 5), - make_hint("Here's a hint", 1, 1, 2, 5), + make_warning("Warning on Server 1", 1, 1, 2, 3), + make_info("Ignored information", 1, 1, 2, 3), + make_hint("Here's a hint", 1, 1, 2, 3), }) return { @@ -820,8 +820,8 @@ describe('vim.diagnostic', function() eq(1, exec_lua [[ vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, { make_error("Error 1", 1, 1, 1, 5), - make_warning("Warning on Server 1", 1, 1, 2, 5), - make_info("Ignored information", 1, 1, 2, 5), + make_warning("Warning on Server 1", 1, 1, 2, 3), + make_info("Ignored information", 1, 1, 2, 3), make_error("Error On Other Line", 2, 1, 1, 5), }) diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua new file mode 100644 index 0000000000..729ebc601b --- /dev/null +++ b/test/functional/lua/filetype_spec.lua @@ -0,0 +1,98 @@ +local helpers = require('test.functional.helpers')(after_each) +local exec_lua = helpers.exec_lua +local eq = helpers.eq +local clear = helpers.clear +local pathroot = helpers.pathroot + +local root = pathroot() + +describe('vim.filetype', function() + before_each(function() + clear() + + exec_lua [[ + local bufnr = vim.api.nvim_create_buf(true, false) + vim.api.nvim_set_current_buf(bufnr) + ]] + end) + + it('works with extensions', function() + eq('radicalscript', exec_lua [[ + vim.filetype.add({ + extension = { + rs = 'radicalscript', + }, + }) + vim.filetype.match('main.rs') + return vim.bo.filetype + ]]) + end) + + it('prioritizes filenames over extensions', function() + eq('somethingelse', exec_lua [[ + vim.filetype.add({ + extension = { + rs = 'radicalscript', + }, + filename = { + ['main.rs'] = 'somethingelse', + }, + }) + vim.filetype.match('main.rs') + return vim.bo.filetype + ]]) + end) + + it('works with filenames', function() + eq('nim', exec_lua [[ + vim.filetype.add({ + filename = { + ['s_O_m_e_F_i_l_e'] = 'nim', + }, + }) + vim.filetype.match('s_O_m_e_F_i_l_e') + return vim.bo.filetype + ]]) + + eq('dosini', exec_lua([[ + local root = ... + vim.filetype.add({ + filename = { + ['config'] = 'toml', + [root .. '/.config/fun/config'] = 'dosini', + }, + }) + vim.filetype.match(root .. '/.config/fun/config') + return vim.bo.filetype + ]], root)) + end) + + it('works with patterns', function() + eq('markdown', exec_lua([[ + local root = ... + vim.filetype.add({ + pattern = { + ['~/blog/.*%.txt'] = 'markdown', + } + }) + vim.filetype.match('~/blog/why_neovim_is_awesome.txt') + return vim.bo.filetype + ]], root)) + end) + + it('works with functions', function() + eq('foss', exec_lua [[ + vim.filetype.add({ + pattern = { + ["relevant_to_(%a+)"] = function(path, bufnr, capture) + if capture == "me" then + return "foss" + end + end, + } + }) + vim.filetype.match('relevant_to_me') + return vim.bo.filetype + ]]) + end) +end) diff --git a/test/functional/lua/luaeval_spec.lua b/test/functional/lua/luaeval_spec.lua index c543dd1995..1322155595 100644 --- a/test/functional/lua/luaeval_spec.lua +++ b/test/functional/lua/luaeval_spec.lua @@ -532,6 +532,7 @@ describe('v:lua', function() command('set pp+=test/functional/fixtures') eq('\tbadval', eval("v:lua.require'leftpad'('badval')")) eq(9003, eval("v:lua.require'bar'.doit()")) + eq(9004, eval("v:lua.require'baz-quux'.doit()")) end) it('throw errors for invalid use', function() diff --git a/test/functional/lua/uri_spec.lua b/test/functional/lua/uri_spec.lua index fa11fdf794..4635f17557 100644 --- a/test/functional/lua/uri_spec.lua +++ b/test/functional/lua/uri_spec.lua @@ -101,7 +101,7 @@ describe('URI methods', function() eq('C:\\Foo\\Bar\\Baz.txt', exec_lua(test_case)) end) - it('file path includes only ascii charactors with encoded colon character', function() + it('file path includes only ascii characters with encoded colon character', function() local test_case = [[ local uri = 'file:///C%3A/Foo/Bar/Baz.txt' return vim.uri_to_fname(uri) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 9bf376b6fe..7ec986acdd 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -19,6 +19,9 @@ local NIL = helpers.NIL local retry = helpers.retry local next_msg = helpers.next_msg local remove_trace = helpers.remove_trace +local mkdir_p = helpers.mkdir_p +local rmdir = helpers.rmdir +local write_file = helpers.write_file before_each(clear) @@ -987,6 +990,52 @@ describe('lua stdlib', function() matches([[attempt to index .* nil value]], pcall_err(exec_lua, 'return vim.g[0].testing')) + + exec_lua [[ + local counter = 0 + vim.g.AddCounter = function() counter = counter + 1 end + vim.g.GetCounter = function() return counter end + ]] + + eq(0, eval('g:GetCounter()')) + eval('g:AddCounter()') + eq(1, eval('g:GetCounter()')) + eval('g:AddCounter()') + eq(2, eval('g:GetCounter()')) + exec_lua([[vim.g.AddCounter()]]) + eq(3, exec_lua([[return vim.g.GetCounter()]])) + exec_lua([[vim.api.nvim_get_var('AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]])) + + exec_lua [[ + local counter = 0 + vim.api.nvim_set_var('AddCounter', function() counter = counter + 1 end) + vim.api.nvim_set_var('GetCounter', function() return counter end) + ]] + + eq(0, eval('g:GetCounter()')) + eval('g:AddCounter()') + eq(1, eval('g:GetCounter()')) + eval('g:AddCounter()') + eq(2, eval('g:GetCounter()')) + exec_lua([[vim.g.AddCounter()]]) + eq(3, exec_lua([[return vim.g.GetCounter()]])) + exec_lua([[vim.api.nvim_get_var('AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]])) + + -- Check if autoload works properly + local pathsep = helpers.get_pathsep() + local xconfig = 'Xhome' .. pathsep .. 'Xconfig' + local xdata = 'Xhome' .. pathsep .. 'Xdata' + local autoload_folder = table.concat({xconfig, 'nvim', 'autoload'}, pathsep) + local autoload_file = table.concat({autoload_folder , 'testload.vim'}, pathsep) + mkdir_p(autoload_folder) + write_file(autoload_file , [[let testload#value = 2]]) + + clear{ args_rm={'-u'}, env={ XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata } } + + eq(2, exec_lua("return vim.g['testload#value']")) + rmdir('Xhome') end) it('vim.b', function() @@ -1022,6 +1071,38 @@ describe('lua stdlib', function() eq(NIL, funcs.luaeval "vim.b.to_delete") exec_lua [[ + local counter = 0 + vim.b.AddCounter = function() counter = counter + 1 end + vim.b.GetCounter = function() return counter end + ]] + + eq(0, eval('b:GetCounter()')) + eval('b:AddCounter()') + eq(1, eval('b:GetCounter()')) + eval('b:AddCounter()') + eq(2, eval('b:GetCounter()')) + exec_lua([[vim.b.AddCounter()]]) + eq(3, exec_lua([[return vim.b.GetCounter()]])) + exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]])) + + exec_lua [[ + local counter = 0 + vim.api.nvim_buf_set_var(0, 'AddCounter', function() counter = counter + 1 end) + vim.api.nvim_buf_set_var(0, 'GetCounter', function() return counter end) + ]] + + eq(0, eval('b:GetCounter()')) + eval('b:AddCounter()') + eq(1, eval('b:GetCounter()')) + eval('b:AddCounter()') + eq(2, eval('b:GetCounter()')) + exec_lua([[vim.b.AddCounter()]]) + eq(3, exec_lua([[return vim.b.GetCounter()]])) + exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]])) + + exec_lua [[ vim.cmd "vnew" ]] @@ -1059,6 +1140,38 @@ describe('lua stdlib', function() eq(NIL, funcs.luaeval "vim.w.to_delete") exec_lua [[ + local counter = 0 + vim.w.AddCounter = function() counter = counter + 1 end + vim.w.GetCounter = function() return counter end + ]] + + eq(0, eval('w:GetCounter()')) + eval('w:AddCounter()') + eq(1, eval('w:GetCounter()')) + eval('w:AddCounter()') + eq(2, eval('w:GetCounter()')) + exec_lua([[vim.w.AddCounter()]]) + eq(3, exec_lua([[return vim.w.GetCounter()]])) + exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]])) + + exec_lua [[ + local counter = 0 + vim.api.nvim_win_set_var(0, 'AddCounter', function() counter = counter + 1 end) + vim.api.nvim_win_set_var(0, 'GetCounter', function() return counter end) + ]] + + eq(0, eval('w:GetCounter()')) + eval('w:AddCounter()') + eq(1, eval('w:GetCounter()')) + eval('w:AddCounter()') + eq(2, eval('w:GetCounter()')) + exec_lua([[vim.w.AddCounter()]]) + eq(3, exec_lua([[return vim.w.GetCounter()]])) + exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]])) + + exec_lua [[ vim.cmd "vnew" ]] @@ -1091,6 +1204,38 @@ describe('lua stdlib', function() eq(NIL, funcs.luaeval "vim.t.to_delete") exec_lua [[ + local counter = 0 + vim.t.AddCounter = function() counter = counter + 1 end + vim.t.GetCounter = function() return counter end + ]] + + eq(0, eval('t:GetCounter()')) + eval('t:AddCounter()') + eq(1, eval('t:GetCounter()')) + eval('t:AddCounter()') + eq(2, eval('t:GetCounter()')) + exec_lua([[vim.t.AddCounter()]]) + eq(3, exec_lua([[return vim.t.GetCounter()]])) + exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]])) + + exec_lua [[ + local counter = 0 + vim.api.nvim_tabpage_set_var(0, 'AddCounter', function() counter = counter + 1 end) + vim.api.nvim_tabpage_set_var(0, 'GetCounter', function() return counter end) + ]] + + eq(0, eval('t:GetCounter()')) + eval('t:AddCounter()') + eq(1, eval('t:GetCounter()')) + eval('t:AddCounter()') + eq(2, eval('t:GetCounter()')) + exec_lua([[vim.t.AddCounter()]]) + eq(3, exec_lua([[return vim.t.GetCounter()]])) + exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]]) + eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]])) + + exec_lua [[ vim.cmd "tabnew" ]] @@ -2203,6 +2348,40 @@ describe('lua stdlib', function() end) end) + it('vim.notify_once', function() + local screen = Screen.new(60,5) + screen:set_default_attr_ids({ + [0] = {bold=true, foreground=Screen.colors.Blue}, + [1] = {foreground=Screen.colors.Red}, + }) + screen:attach() + screen:expect{grid=[[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]} + exec_lua [[vim.notify_once("I'll only tell you this once...", vim.log.levels.WARN)]] + screen:expect{grid=[[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {1:I'll only tell you this once...} | + ]]} + feed('<C-l>') + screen:expect{grid=[[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]} + exec_lua [[vim.notify_once("I'll only tell you this once...")]] + screen:expect_unchanged() + end) + describe('vim.schedule_wrap', function() it('preserves argument lists', function() exec_lua [[ @@ -2244,6 +2423,17 @@ describe('lua stdlib', function() eq(buf1, meths.get_current_buf()) eq(buf2, val) end) + + it('does not cause ml_get errors with invalid visual selection', function() + -- Should be fixed by vim-patch:8.2.4028. + exec_lua [[ + local a = vim.api + local t = function(s) return a.nvim_replace_termcodes(s, true, true, true) end + a.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"}) + a.nvim_feedkeys(t "G<C-V>", "txn", false) + a.nvim_buf_call(a.nvim_create_buf(false, true), function() vim.cmd "redraw" end) + ]] + end) end) describe('vim.api.nvim_win_call', function() @@ -2272,6 +2462,75 @@ describe('lua stdlib', function() eq(win1, meths.get_current_win()) eq(win2, val) end) + + it('does not cause ml_get errors with invalid visual selection', function() + -- Add lines to the current buffer and make another window looking into an empty buffer. + exec_lua [[ + _G.a = vim.api + _G.t = function(s) return a.nvim_replace_termcodes(s, true, true, true) end + _G.win_lines = a.nvim_get_current_win() + vim.cmd "new" + _G.win_empty = a.nvim_get_current_win() + a.nvim_set_current_win(win_lines) + a.nvim_buf_set_lines(0, 0, -1, true, {"a", "b", "c"}) + ]] + + -- Start Visual in current window, redraw in other window with fewer lines. + -- Should be fixed by vim-patch:8.2.4018. + exec_lua [[ + a.nvim_feedkeys(t "G<C-V>", "txn", false) + a.nvim_win_call(win_empty, function() vim.cmd "redraw" end) + ]] + + -- Start Visual in current window, extend it in other window with more lines. + -- Fixed for win_execute by vim-patch:8.2.4026, but nvim_win_call should also not be affected. + exec_lua [[ + a.nvim_feedkeys(t "<Esc>gg", "txn", false) + a.nvim_set_current_win(win_empty) + a.nvim_feedkeys(t "gg<C-V>", "txn", false) + a.nvim_win_call(win_lines, function() a.nvim_feedkeys(t "G<C-V>", "txn", false) end) + vim.cmd "redraw" + ]] + end) + + it('updates ruler if cursor moved', function() + -- Fixed for win_execute in vim-patch:8.1.2124, but should've applied to nvim_win_call too! + local screen = Screen.new(30, 5) + screen:set_default_attr_ids { + [1] = {reverse = true}, + [2] = {bold = true, reverse = true}, + } + screen:attach() + exec_lua [[ + _G.a = vim.api + vim.opt.ruler = true + local lines = {} + for i = 0, 499 do lines[#lines + 1] = tostring(i) end + a.nvim_buf_set_lines(0, 0, -1, true, lines) + a.nvim_win_set_cursor(0, {20, 0}) + vim.cmd "split" + _G.win = a.nvim_get_current_win() + vim.cmd "wincmd w | redraw" + ]] + screen:expect [[ + 19 | + {1:[No Name] [+] 20,1 3%}| + ^19 | + {2:[No Name] [+] 20,1 3%}| + | + ]] + exec_lua [[ + a.nvim_win_call(win, function() a.nvim_win_set_cursor(0, {100, 0}) end) + vim.cmd "redraw" + ]] + screen:expect [[ + 99 | + {1:[No Name] [+] 100,1 19%}| + ^19 | + {2:[No Name] [+] 20,1 3%}| + | + ]] + end) end) end) @@ -2298,3 +2557,82 @@ describe('lua: require("mod") from packages', function() eq('I am fancy_z.lua', exec_lua [[ return require'fancy_z' ]]) end) end) + +describe('vim.keymap', function() + it('can make a mapping', function() + eq(0, exec_lua [[ + GlobalCount = 0 + vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end) + return GlobalCount + ]]) + + feed('asdf\n') + + eq(1, exec_lua[[return GlobalCount]]) + end) + + it('can make an expr mapping', function() + exec_lua [[ + vim.keymap.set('n', 'aa', function() return ':lua SomeValue = 99<cr>' end, {expr = true}) + ]] + + feed('aa') + + eq(99, exec_lua[[return SomeValue]]) + end) + + it('can overwrite a mapping', function() + eq(0, exec_lua [[ + GlobalCount = 0 + vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end) + return GlobalCount + ]]) + + feed('asdf\n') + + eq(1, exec_lua[[return GlobalCount]]) + + exec_lua [[ + vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount - 1 end) + ]] + + feed('asdf\n') + + eq(0, exec_lua[[return GlobalCount]]) + end) + + it('can unmap a mapping', function() + eq(0, exec_lua [[ + GlobalCount = 0 + vim.keymap.set('n', 'asdf', function() GlobalCount = GlobalCount + 1 end) + return GlobalCount + ]]) + + feed('asdf\n') + + eq(1, exec_lua[[return GlobalCount]]) + + exec_lua [[ + vim.keymap.del('n', 'asdf') + ]] + + feed('asdf\n') + + eq(1, exec_lua[[return GlobalCount]]) + eq('\nNo mapping found', helpers.exec_capture('nmap asdf')) + end) + + it('can do <Plug> mappings', function() + eq(0, exec_lua [[ + GlobalCount = 0 + vim.keymap.set('n', '<plug>(asdf)', function() GlobalCount = GlobalCount + 1 end) + vim.keymap.set('n', 'ww', '<plug>(asdf)') + return GlobalCount + ]]) + + feed('ww\n') + + eq(1, exec_lua[[return GlobalCount]]) + end) + +end) diff --git a/test/functional/lua/xdiff_spec.lua b/test/functional/lua/xdiff_spec.lua index 4f28f84c01..d55268fc78 100644 --- a/test/functional/lua/xdiff_spec.lua +++ b/test/functional/lua/xdiff_spec.lua @@ -90,6 +90,48 @@ describe('xdiff bindings', function() exec_lua([[return vim.diff(a2, b2, {result_type = 'indices'})]])) end) + it('can run different algorithms', function() + local a = table.concat({ + '.foo1 {', + ' margin: 0;', + '}', + '', + '.bar {', + ' margin: 0;', + '}', + ''}, '\n') + + local b = table.concat({ + '.bar {', + ' margin: 0;', + '}', + '', + '.foo1 {', + ' margin: 0;', + ' color: green;', + '}', + ''}, '\n') + + eq( + table.concat({'@@ -1,4 +0,0 @@', + '-.foo1 {', + '- margin: 0;', + '-}', + '-', + '@@ -7,0 +4,5 @@', + '+', + '+.foo1 {', + '+ margin: 0;', + '+ color: green;', + '+}', + ''}, '\n'), + exec_lua([[ + local args = {...} + return vim.diff(args[1], args[2], { + algorithm = 'patience' + }) + ]], a, b)) + end) end) it('can handle bad args', function() diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index 37de5d0ce6..a9bd76ce24 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -153,6 +153,10 @@ describe('health.vim', function() ## report 2 - OK: nothing to see here + test_plug.submodule_empty: require("test_plug.submodule_empty.health").check() + ======================================================================== + - ERROR: The healthcheck report for "test_plug.submodule_empty" plugin is empty. + test_plug.submodule_failed: require("test_plug.submodule_failed.health").check() ======================================================================== - ERROR: Failed to run healthcheck for "test_plug.submodule_failed" plugin. Exception: @@ -172,6 +176,16 @@ describe('health.vim', function() ]]) end) + it("... including empty reports", function() + command("checkhealth test_plug.submodule_empty") + helpers.expect([[ + + test_plug.submodule_empty: require("test_plug.submodule_empty.health").check() + ======================================================================== + - ERROR: The healthcheck report for "test_plug.submodule_empty" plugin is empty. + ]]) + end) + it("gracefully handles broken lua healthcheck", function() command("checkhealth test_plug.submodule_failed") local buf_lines = helpers.curbuf('get_lines', 0, -1, true) @@ -230,3 +244,14 @@ describe('health.vim', function() end) end) end) + +describe(':checkhealth provider', function() + it("works correctly with a wrongly configured 'shell'", function() + clear() + command([[set shell=echo\ WRONG!!!]]) + command('let g:loaded_perl_provider = 0') + command('let g:loaded_python3_provider = 0') + command('checkhealth provider') + eq(nil, string.match(curbuf_contents(), 'WRONG!!!')) + end) +end) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 1af31c38f8..eab520948f 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -73,8 +73,11 @@ local function fake_lsp_server_setup(test_name, timeout_ms, options) on_init = function(client, result) TEST_RPC_CLIENT = client vim.rpcrequest(1, "init", result) - client.config.flags.allow_incremental_sync = options.allow_incremental_sync or false end; + flags = { + allow_incremental_sync = options.allow_incremental_sync or false; + debounce_text_changes = options.debounce_text_changes or 0; + }; on_exit = function(...) vim.rpcnotify(1, "exit", ...) end; @@ -926,7 +929,60 @@ describe('LSP', function() local client test_rpc_server { test_name = "basic_check_buffer_open_and_change_incremental"; - options = { allow_incremental_sync = true }; + options = { + allow_incremental_sync = true, + }; + on_setup = function() + exec_lua [[ + BUFFER = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(BUFFER, 0, -1, false, { + "testing"; + "123"; + }) + ]] + end; + on_init = function(_client) + client = _client + local sync_kind = exec_lua("return require'vim.lsp.protocol'.TextDocumentSyncKind.Incremental") + eq(sync_kind, client.resolved_capabilities().text_document_did_change) + eq(true, client.resolved_capabilities().text_document_open_close) + exec_lua [[ + assert(lsp.buf_attach_client(BUFFER, TEST_RPC_CLIENT_ID)) + ]] + end; + on_exit = function(code, signal) + eq(0, code, "exit code", fake_lsp_logfile) + eq(0, signal, "exit signal", fake_lsp_logfile) + end; + on_handler = function(err, result, ctx) + if ctx.method == 'start' then + exec_lua [[ + vim.api.nvim_buf_set_lines(BUFFER, 1, 2, false, { + "123boop"; + }) + ]] + client.notify('finish') + end + eq(table.remove(expected_handlers), {err, result, ctx}, "expected handler") + if ctx.method == 'finish' then + client.stop() + end + end; + } + end) + it('should check the body and didChange incremental with debounce', function() + local expected_handlers = { + {NIL, {}, {method="shutdown", client_id=1}}; + {NIL, {}, {method="finish", client_id=1}}; + {NIL, {}, {method="start", client_id=1}}; + } + local client + test_rpc_server { + test_name = "basic_check_buffer_open_and_change_incremental"; + options = { + allow_incremental_sync = true, + debounce_text_changes = 5 + }; on_setup = function() exec_lua [[ BUFFER = vim.api.nvim_create_buf(false, true) @@ -1235,7 +1291,7 @@ describe('LSP', function() make_edit(2, 0, 2, 2, {"3"}); make_edit(3, 2, 3, 4, {""}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ '123First line of text'; '2econd line of text'; @@ -1255,7 +1311,7 @@ describe('LSP', function() make_edit(3, #'', 3, #"Fourth", {"another line of text", "before this"}); make_edit(3, #'Fourth', 3, #"Fourth line of text", {"!"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ ''; '123'; @@ -1279,7 +1335,7 @@ describe('LSP', function() make_edit(3, #"Fourth", 3, #'', {"another line of text", "before this"}); make_edit(3, #"Fourth line of text", 3, #'Fourth', {"!"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ ''; '123'; @@ -1296,7 +1352,7 @@ describe('LSP', function() local edits = { make_edit(4, 3, 4, 4, {"ä"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1309,7 +1365,7 @@ describe('LSP', function() local edits = { make_edit(5, 0, 5, 0, "foobar"); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1319,6 +1375,20 @@ describe('LSP', function() 'foobar'; }, buf_lines(1)) end) + it('applies multiple text edits at the end of the document', function() + local edits = { + make_edit(4, 0, 5, 0, ""); + make_edit(5, 0, 5, 0, "foobar"); + } + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") + eq({ + 'First line of text'; + 'Second line of text'; + 'Third line of text'; + 'Fourth line of text'; + 'foobar'; + }, buf_lines(1)) + end) describe('cursor position', function() it('don\'t fix the cursor if the range contains the cursor', function() @@ -1326,7 +1396,7 @@ describe('LSP', function() local edits = { make_edit(1, 0, 1, 19, 'Second line of text') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second line of text'; @@ -1343,7 +1413,7 @@ describe('LSP', function() make_edit(1, 0, 1, 6, ''), make_edit(1, 6, 1, 19, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; ''; @@ -1360,7 +1430,7 @@ describe('LSP', function() make_edit(1, 0, 1, 6, ''), make_edit(0, 18, 5, 0, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; }, buf_lines(1)) @@ -1372,7 +1442,7 @@ describe('LSP', function() local edits = { make_edit(1, 0, 2, 0, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Third line of text'; @@ -1387,7 +1457,7 @@ describe('LSP', function() local edits = { make_edit(1, 7, 1, 11, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Second of text'; @@ -1403,7 +1473,7 @@ describe('LSP', function() local edits = { make_edit(0, 11, 1, 12, '') } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({ 'First line of text'; 'Third line of text'; @@ -1419,21 +1489,21 @@ describe('LSP', function() local edits = { make_edit(0, 0, 5, 0, {"All replaced"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({'All replaced'}, buf_lines(1)) end) it('applies edits when the end line is 2 larger than vim\'s', function() local edits = { make_edit(0, 0, 6, 0, {"All replaced"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({'All replaced'}, buf_lines(1)) end) it('applies edits with a column offset', function() local edits = { make_edit(0, 0, 5, 2, {"All replaced"}); } - exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1, "utf-16") eq({'All replaced'}, buf_lines(1)) end) end) @@ -1461,7 +1531,7 @@ describe('LSP', function() ]] end) it('correctly goes ahead with the edit if all is normal', function() - exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit(5)) + exec_lua("vim.lsp.util.apply_text_document_edit(..., nil, 'utf-16')", text_document_edit(5)) eq({ 'First ↥ 🤦 🦄 line of text'; '2nd line of 语text'; @@ -1473,7 +1543,7 @@ describe('LSP', function() local bufnr = select(1, ...) local text_edit = select(2, ...) vim.lsp.util.buf_versions[bufnr] = 10 - vim.lsp.util.apply_text_document_edit(text_edit) + vim.lsp.util.apply_text_document_edit(text_edit, nil, 'utf-16') ]], target_bufnr, text_document_edit(0)) eq({ 'First ↥ 🤦 🦄 line of text'; @@ -1486,7 +1556,7 @@ describe('LSP', function() local args = {...} local versionedBuf = args[2] vim.lsp.util.buf_versions[versionedBuf.bufnr] = versionedBuf.currentVersion - vim.lsp.util.apply_text_document_edit(args[1]) + vim.lsp.util.apply_text_document_edit(args[1], nil, 'utf-16') ]], edit, versionedBuf) end @@ -1512,17 +1582,36 @@ describe('LSP', function() describe('workspace_apply_edit', function() it('workspace/applyEdit returns ApplyWorkspaceEditResponse', function() - local expected = { - applied = true; - failureReason = nil; + local expected_handlers = { + {NIL, {}, {method="test", client_id=1}}; + } + test_rpc_server { + test_name = "basic_init"; + on_init = function(client, _) + client.stop() + end; + -- If the program timed out, then code will be nil. + on_exit = function(code, signal) + eq(0, code, "exit code", fake_lsp_logfile) + eq(0, signal, "exit signal", fake_lsp_logfile) + end; + -- Note that NIL must be used here. + -- on_handler(err, method, result, client_id) + on_handler = function(...) + local expected = { + applied = true; + failureReason = nil; + } + eq(expected, exec_lua [[ + local apply_edit = { + label = nil; + edit = {}; + } + return vim.lsp.handlers['workspace/applyEdit'](nil, apply_edit, {client_id = TEST_RPC_CLIENT_ID}) + ]]) + eq(table.remove(expected_handlers), {...}) + end; } - eq(expected, exec_lua [[ - local apply_edit = { - label = nil; - edit = {}; - } - return vim.lsp.handlers['workspace/applyEdit'](nil, apply_edit) - ]]) end) end) @@ -1596,7 +1685,7 @@ describe('LSP', function() local workspace_edits = args[1] local target_bufnr = args[2] - vim.lsp.util.apply_workspace_edit(workspace_edits) + vim.lsp.util.apply_workspace_edit(workspace_edits, 'utf-16') return vim.api.nvim_buf_get_lines(target_bufnr, 0, -1, false) ]], make_workspace_edit(edits), target_bufnr)) @@ -1618,7 +1707,7 @@ describe('LSP', function() local workspace_edits = args[1] local target_bufnr = args[2] - vim.lsp.util.apply_workspace_edit(workspace_edits) + vim.lsp.util.apply_workspace_edit(workspace_edits, 'utf-16') return vim.api.nvim_buf_get_lines(target_bufnr, 0, -1, false) ]], make_workspace_edit(edits), target_bufnr)) @@ -1635,7 +1724,7 @@ describe('LSP', function() }, } } - exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit) + exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16') eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) end) it('createFile does not touch file if it exists and ignoreIfExists is set', function() @@ -1653,7 +1742,7 @@ describe('LSP', function() }, } } - exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit) + exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16') eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq('Dummy content', read_file(tmpfile)) end) @@ -1673,7 +1762,7 @@ describe('LSP', function() }, } } - exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit) + exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16') eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq('', read_file(tmpfile)) end) @@ -1694,7 +1783,7 @@ describe('LSP', function() } } } - eq(true, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit)) + eq(true, pcall(exec_lua, 'vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16')) eq(false, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile)) eq(false, exec_lua('return vim.api.nvim_buf_is_loaded(vim.fn.bufadd(...))', tmpfile)) end) @@ -1855,7 +1944,7 @@ describe('LSP', function() } }, } - return vim.lsp.util.locations_to_items(locations) + return vim.lsp.util.locations_to_items(locations, 'utf-16') ]] eq(expected, actual) end) @@ -1885,7 +1974,7 @@ describe('LSP', function() } }, } - return vim.lsp.util.locations_to_items(locations) + return vim.lsp.util.locations_to_items(locations, 'utf-16') ]] eq(expected, actual) end) @@ -2189,7 +2278,7 @@ describe('LSP', function() end local jump = function(msg) - eq(true, exec_lua('return vim.lsp.util.jump_to_location(...)', msg)) + eq(true, exec_lua('return vim.lsp.util.jump_to_location(...)', msg, "utf-16")) eq(target_bufnr, exec_lua[[return vim.fn.bufnr('%')]]) return { line = exec_lua[[return vim.fn.line('.')]], @@ -2263,6 +2352,27 @@ describe('LSP', function() end) end) + describe('lsp.util.convert_signature_help_to_markdown_lines', function() + it('can handle negative activeSignature', function() + local result = exec_lua[[ + local signature_help = { + activeParameter = 0, + activeSignature = -1, + signatures = { + { + documentation = "", + label = "TestEntity.TestEntity()", + parameters = {} + }, + } + } + return vim.lsp.util.convert_signature_help_to_markdown_lines(signature_help, 'cs', {','}) + ]] + local expected = {'```cs', 'TestEntity.TestEntity()', '```', ''} + eq(expected, result) + end) + end) + describe('lsp.util.get_effective_tabstop', function() local function test_tabstop(tabsize, softtabstop) exec_lua(string.format([[ diff --git a/test/functional/provider/provider_spec.lua b/test/functional/provider/provider_spec.lua index 78bc4a4edb..3895b8613f 100644 --- a/test/functional/provider/provider_spec.lua +++ b/test/functional/provider/provider_spec.lua @@ -14,8 +14,8 @@ describe('providers', function() command('set loadplugins') -- Using test-fixture with broken impl: -- test/functional/fixtures/autoload/provider/python.vim - eq('Vim:provider: python: missing required variable g:loaded_python_provider', - pcall_err(eval, "has('python')")) + eq('Vim:provider: python3: missing required variable g:loaded_python3_provider', + pcall_err(eval, "has('python3')")) end) it('with g:loaded_xx_provider, missing #Call()', function() diff --git a/test/functional/provider/python3_spec.lua b/test/functional/provider/python3_spec.lua index d100db8de2..603f4d91b9 100644 --- a/test/functional/provider/python3_spec.lua +++ b/test/functional/provider/python3_spec.lua @@ -8,6 +8,7 @@ local source = helpers.source local missing_provider = helpers.missing_provider local matches = helpers.matches local pcall_err = helpers.pcall_err +local funcs = helpers.funcs do clear() @@ -93,16 +94,40 @@ describe('python3 provider', function() ghi]]) end) - it('py3eval', function() - eq({1, 2, {['key'] = 'val'}}, eval([[py3eval('[1, 2, {"key": "val"}]')]])) + describe('py3eval()', function() + it('works', function() + eq({1, 2, {['key'] = 'val'}}, funcs.py3eval('[1, 2, {"key": "val"}]')) + end) + + it('errors out when given non-string', function() + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(10)')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:_null_dict)')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:_null_list)')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(0.0)')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(function("tr"))')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:true)')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:false)')) + eq('Vim:E474: Invalid argument', pcall_err(eval, 'py3eval(v:null)')) + end) + + it('accepts NULL string', function() + matches('.*SyntaxError.*', pcall_err(eval, 'py3eval($XXX_NONEXISTENT_VAR_XXX)')) + end) end) it('pyxeval #10758', function() - eq(0, eval([[&pyxversion]])) + eq(3, eval([[&pyxversion]])) eq(3, eval([[pyxeval('sys.version_info[:3][0]')]])) eq(3, eval([[&pyxversion]])) end) + it("setting 'pyxversion'", function() + command 'set pyxversion=3' -- no error + eq('Vim(set):E474: Invalid argument: pyxversion=2', pcall_err(command, 'set pyxversion=2')) + command 'set pyxversion=0' -- allowed, but equivalent to pyxversion=3 + eq(3, eval'&pyxversion') + end) + it('RPC call to expand("<afile>") during BufDelete #5245 #5617', function() helpers.add_builddir_to_rtp() source([=[ @@ -120,3 +145,15 @@ describe('python3 provider', function() assert_alive() end) end) + +describe('python2 feature test', function() + -- python2 is not supported, so correct behaviour is to return 0 + it('works', function() + eq(0, funcs.has('python2')) + eq(0, funcs.has('python')) + eq(0, funcs.has('python_compiled')) + eq(0, funcs.has('python_dynamic')) + eq(0, funcs.has('python_dynamic_')) + eq(0, funcs.has('python_')) + end) +end) diff --git a/test/functional/provider/python_spec.lua b/test/functional/provider/python_spec.lua deleted file mode 100644 index d60d8d1001..0000000000 --- a/test/functional/provider/python_spec.lua +++ /dev/null @@ -1,123 +0,0 @@ -local helpers = require('test.functional.helpers')(after_each) - -local eq = helpers.eq -local neq = helpers.neq -local feed = helpers.feed -local clear = helpers.clear -local funcs = helpers.funcs -local meths = helpers.meths -local insert = helpers.insert -local expect = helpers.expect -local command = helpers.command -local exc_exec = helpers.exc_exec -local write_file = helpers.write_file -local curbufmeths = helpers.curbufmeths -local missing_provider = helpers.missing_provider -local matches = helpers.matches -local pcall_err = helpers.pcall_err - -do - clear() - local reason = missing_provider('python') - if reason then - it(':python reports E319 if provider is missing', function() - local expected = [[Vim%(py.*%):E319: No "python" provider found.*]] - matches(expected, pcall_err(command, 'py print("foo")')) - matches(expected, pcall_err(command, 'pyfile foo')) - end) - pending(string.format('Python 2 (or the pynvim module) is broken/missing (%s)', reason), function() end) - return - end -end - -before_each(function() - clear() - command('python import vim') -end) - -describe('python feature test', function() - it('works', function() - eq(1, funcs.has('python')) - eq(1, funcs.has('python_compiled')) - eq(1, funcs.has('python_dynamic')) - eq(0, funcs.has('python_dynamic_')) - eq(0, funcs.has('python_')) - end) -end) - -describe(':python command', function() - it('works with a line', function() - command('python vim.vars["set_by_python"] = [100, 0]') - eq({100, 0}, meths.get_var('set_by_python')) - end) - - -- TODO(ZyX-I): works with << EOF - -- TODO(ZyX-I): works with execute 'python' line1."\n".line2."\n"… - - it('supports nesting', function() - command([[python vim.command('python vim.command("python vim.command(\'let set_by_nested_python = 555\')")')]]) - eq(555, meths.get_var('set_by_nested_python')) - end) - - it('supports range', function() - insert([[ - line1 - line2 - line3 - line4]]) - feed('ggjvj:python vim.vars["range"] = vim.current.range[:]<CR>') - eq({'line2', 'line3'}, meths.get_var('range')) - end) -end) - -describe(':pyfile command', function() - it('works', function() - local fname = 'pyfile.py' - write_file(fname, 'vim.command("let set_by_pyfile = 123")') - command('pyfile pyfile.py') - eq(123, meths.get_var('set_by_pyfile')) - os.remove(fname) - end) -end) - -describe(':pydo command', function() - it('works', function() - -- :pydo 42 returns None for all lines, - -- the buffer should not be changed - command('normal :pydo 42') - eq(false, curbufmeths.get_option('modified')) - -- insert some text - insert('abc\ndef\nghi') - expect([[ - abc - def - ghi]]) - -- go to top and select and replace the first two lines - feed('ggvj:pydo return str(linenr)<CR>') - expect([[ - 1 - 2 - ghi]]) - end) -end) - -describe('pyeval()', function() - it('works', function() - eq({1, 2, {['key'] = 'val'}}, funcs.pyeval('[1, 2, {"key": "val"}]')) - end) - - it('errors out when given non-string', function() - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(10)')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:_null_dict)')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:_null_list)')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(0.0)')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(function("tr"))')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:true)')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:false)')) - eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:null)')) - end) - - it('accepts NULL string', function() - neq(0, exc_exec('call pyeval($XXX_NONEXISTENT_VAR_XXX)')) - end) -end) diff --git a/test/functional/terminal/buffer_spec.lua b/test/functional/terminal/buffer_spec.lua index 1ff451434d..beb43e0271 100644 --- a/test/functional/terminal/buffer_spec.lua +++ b/test/functional/terminal/buffer_spec.lua @@ -292,10 +292,9 @@ describe(':terminal buffer', function() command('quit') end) - it('does not segfault when pasting empty buffer #13955', function() - feed_command('terminal') + it('does not segfault when pasting empty register #13955', function() feed('<c-\\><c-n>') - feed_command('put a') -- buffer a is empty + feed_command('put a') -- register a is empty helpers.assert_alive() end) end) diff --git a/test/functional/terminal/mouse_spec.lua b/test/functional/terminal/mouse_spec.lua index 780a0b9b5a..6e2c851df7 100644 --- a/test/functional/terminal/mouse_spec.lua +++ b/test/functional/terminal/mouse_spec.lua @@ -1,7 +1,7 @@ local helpers = require('test.functional.helpers')(after_each) local thelpers = require('test.functional.terminal.helpers') local clear, eq, eval = helpers.clear, helpers.eq, helpers.eval -local feed, nvim = helpers.feed, helpers.nvim +local feed, nvim, command = helpers.feed, helpers.nvim, helpers.command local feed_data = thelpers.feed_data describe(':terminal mouse', function() @@ -10,9 +10,9 @@ describe(':terminal mouse', function() before_each(function() clear() nvim('set_option', 'statusline', '==========') - nvim('command', 'highlight StatusLine cterm=NONE') - nvim('command', 'highlight StatusLineNC cterm=NONE') - nvim('command', 'highlight VertSplit cterm=NONE') + command('highlight StatusLine cterm=NONE') + command('highlight StatusLineNC cterm=NONE') + command('highlight VertSplit cterm=NONE') screen = thelpers.screen_setup() local lines = {} for i = 1, 30 do @@ -38,6 +38,26 @@ describe(':terminal mouse', function() eq('nt', eval('mode(1)')) end) + it('will exit focus and trigger Normal mode mapping on mouse click', function() + command('let g:got_leftmouse = 0') + command('nnoremap <LeftMouse> <Cmd>let g:got_leftmouse = 1<CR>') + eq('t', eval('mode(1)')) + eq(0, eval('g:got_leftmouse')) + feed('<LeftMouse>') + eq('nt', eval('mode(1)')) + eq(1, eval('g:got_leftmouse')) + end) + + it('will exit focus and trigger Normal mode mapping on mouse click with modifier', function() + command('let g:got_ctrl_leftmouse = 0') + command('nnoremap <C-LeftMouse> <Cmd>let g:got_ctrl_leftmouse = 1<CR>') + eq('t', eval('mode(1)')) + eq(0, eval('g:got_ctrl_leftmouse')) + feed('<C-LeftMouse>') + eq('nt', eval('mode(1)')) + eq(1, eval('g:got_ctrl_leftmouse')) + end) + it('will exit focus on <C-\\> + mouse-scroll', function() eq('t', eval('mode(1)')) feed('<C-\\>') @@ -180,7 +200,7 @@ describe(':terminal mouse', function() it('will forward mouse clicks to the program with the correct even if set nu', function() if helpers.pending_win32(pending) then return end - nvim('command', 'set number') + command('set number') -- When the display area such as a number is clicked, it returns to the -- normal mode. feed('<LeftMouse><3,0>') diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua index 911fa017ab..599c74102c 100644 --- a/test/functional/treesitter/parser_spec.lua +++ b/test/functional/treesitter/parser_spec.lua @@ -167,6 +167,27 @@ void ui_refresh(void) eq('variable', ret) end) + it("supports caching queries", function() + local long_query = query:rep(100) + local first_run = exec_lua ([[ + local before = vim.loop.hrtime() + cquery = vim.treesitter.parse_query("c", ...) + local after = vim.loop.hrtime() + return after - before + ]], long_query) + + local subsequent_runs = exec_lua ([[ + local before = vim.loop.hrtime() + for i=1,100,1 do + cquery = vim.treesitter.parse_query("c", ...) + end + local after = vim.loop.hrtime() + return after - before + ]], long_query) + + assert.True(1000 * subsequent_runs < first_run) + end) + it('support query and iter by capture', function() insert(test_text) @@ -264,6 +285,25 @@ end]] eq(true, result) end) + it('support getting empty text if node range is zero width', function() + local text = [[ +```lua +{} +```]] + insert(text) + local result = exec_lua([[ + local fake_node = {} + function fake_node:start() + return 1, 0, 7 + end + function fake_node:end_() + return 1, 0, 7 + end + return vim.treesitter.get_node_text(fake_node, 0) == '' + ]]) + eq(true, result) + end) + it('can match special regex characters like \\ * + ( with `vim-match?`', function() insert('char* astring = "\\n"; (1 + 1) * 2 != 2;') @@ -645,6 +685,21 @@ int x = INT_MAX; -- READ_STRING_OK(x, y) (char_u *)read_string((x), (size_t)(y)) }, get_ranges()) end) + + it("should not inject bad languages", function() + if helpers.pending_win32(pending) then return end + exec_lua([=[ + vim.treesitter.add_directive("inject-bad!", function(match, _, _, pred, metadata) + metadata.language = "{" + metadata.combined = true + metadata.content = pred[2] + end) + + parser = vim.treesitter.get_parser(0, "c", { + injections = { + c = "(preproc_function_def value: ((preproc_arg) @_a (#inject-bad! @_a)))"}}) + ]=]) + end) end) describe("when using the offset directive", function() diff --git a/test/functional/ui/bufhl_spec.lua b/test/functional/ui/bufhl_spec.lua index 16ed3b9486..9f84b71ece 100644 --- a/test/functional/ui/bufhl_spec.lua +++ b/test/functional/ui/bufhl_spec.lua @@ -755,12 +755,26 @@ describe('Buffer highlighting', function() -- TODO: only a virtual text from the same ns curretly overrides -- an existing virtual text. We might add a prioritation system. set_virtual_text(id1, 0, s1, {}) - eq({{1, 0, 0, { priority = 0, virt_text = s1}}}, get_extmarks(id1, {0,0}, {0, -1}, {details=true})) + eq({{1, 0, 0, { + priority = 0, + virt_text = s1, + -- other details + right_gravity = true, + virt_text_pos = 'eol', + virt_text_hide = false, + }}}, get_extmarks(id1, {0,0}, {0, -1}, {details=true})) -- TODO: is this really valid? shouldn't the max be line_count()-1? local lastline = line_count() set_virtual_text(id1, line_count(), s2, {}) - eq({{3, lastline, 0, { priority = 0, virt_text = s2}}}, get_extmarks(id1, {lastline,0}, {lastline, -1}, {details=true})) + eq({{3, lastline, 0, { + priority = 0, + virt_text = s2, + -- other details + right_gravity = true, + virt_text_pos = 'eol', + virt_text_hide = false, + }}}, get_extmarks(id1, {lastline,0}, {lastline, -1}, {details=true})) eq({}, get_extmarks(id1, {lastline+9000,0}, {lastline+9000, -1}, {})) end) diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index 5f29261b17..dc26c52f1a 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -417,6 +417,28 @@ describe('float window', function() eq(winids, eval('winids')) end) + it('closed when the last non-float window is closed', function() + local tabpage = exec_lua([[ + vim.cmd('edit ./src/nvim/main.c') + vim.cmd('tabedit %') + + local buf = vim.api.nvim_create_buf(false, true) + local win = vim.api.nvim_open_win(buf, false, { + relative = 'win', + row = 1, + col = 1, + width = 10, + height = 2 + }) + + vim.cmd('quit') + + return vim.api.nvim_get_current_tabpage() + ]]) + + eq(1, tabpage) + end) + local function with_ext_multigrid(multigrid) local screen before_each(function() diff --git a/test/functional/ui/highlight_spec.lua b/test/functional/ui/highlight_spec.lua index 0983d0d4ad..12643eec1e 100644 --- a/test/functional/ui/highlight_spec.lua +++ b/test/functional/ui/highlight_spec.lua @@ -2,7 +2,7 @@ local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') local os = require('os') local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert -local command = helpers.command +local command, exec = helpers.command, helpers.exec local eval, exc_exec = helpers.eval, helpers.exc_exec local feed_command, eq = helpers.feed_command, helpers.eq local curbufmeths = helpers.curbufmeths @@ -1172,6 +1172,105 @@ describe('CursorLine highlight', function() end) end) +describe('ColorColumn highlight', function() + local screen + + before_each(function() + clear() + screen = Screen.new(40, 15) + Screen:set_default_attr_ids({ + [1] = {background = Screen.colors.LightRed}, -- ColorColumn + [2] = {background = Screen.colors.Grey90}, -- CursorLine + [3] = {foreground = Screen.colors.Brown}, -- LineNr + [4] = {foreground = Screen.colors.Brown, bold = true}, -- CursorLineNr + [5] = {foreground = Screen.colors.Blue, bold = true}, -- NonText + -- NonText and ColorColumn + [6] = {foreground = Screen.colors.Blue, background = Screen.colors.LightRed, bold = true}, + [7] = {reverse = true, bold = true}, -- StatusLine + [8] = {reverse = true}, -- StatusLineNC + }) + screen:attach() + end) + + it('when entering a buffer vim-patch:8.1.2073', function() + exec([[ + set nohidden + split + edit X + call setline(1, ["1111111111","22222222222","3333333333"]) + set nomodified + set colorcolumn=3,9 + set number cursorline cursorlineopt=number + wincmd w + buf X + ]]) + screen:expect([[ + {4: 1 }11{1:1}11111{1:1}1 | + {3: 2 }22{1:2}22222{1:2}22 | + {3: 3 }33{1:3}33333{1:3}3 | + {5:~ }| + {5:~ }| + {5:~ }| + {8:X }| + {4: 1 }^11{1:1}11111{1:1}1 | + {3: 2 }22{1:2}22222{1:2}22 | + {3: 3 }33{1:3}33333{1:3}3 | + {5:~ }| + {5:~ }| + {5:~ }| + {7:X }| + | + ]]) + end) + + it("in 'breakindent' vim-patch:8.2.1689", function() + exec([[ + call setline(1, 'The quick brown fox jumped over the lazy dogs') + set co=40 linebreak bri briopt=shift:2 cc=40,41,43 + ]]) + screen:expect([[ + ^The quick brown fox jumped over the {1: }| + {1: } {1:l}azy dogs | + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + | + ]]) + end) + + it("in 'showbreak' vim-patch:8.2.1689", function() + exec([[ + call setline(1, 'The quick brown fox jumped over the lazy dogs') + set co=40 showbreak=+++>\\ cc=40,41,43 + ]]) + screen:expect([[ + ^The quick brown fox jumped over the laz{1:y}| + {6:+}{5:+}{6:+}{5:>\} dogs | + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + {5:~ }| + | + ]]) + end) +end) describe("MsgSeparator highlight and msgsep fillchar", function() local screen diff --git a/test/functional/ui/inccommand_spec.lua b/test/functional/ui/inccommand_spec.lua index b6e2f2311f..10700d9508 100644 --- a/test/functional/ui/inccommand_spec.lua +++ b/test/functional/ui/inccommand_spec.lua @@ -293,6 +293,70 @@ describe(":substitute, 'inccommand' preserves", function() end) end + for _, case in ipairs({'', 'split', 'nosplit'}) do + it('previous substitute string ~ (inccommand='..case..') #12109', function() + local screen = Screen.new(30,10) + common_setup(screen, case, default_text) + + feed(':%s/Inc/SUB<CR>') + expect([[ + SUB substitution on + two lines + ]]) + + feed(':%s/line/') + poke_eventloop() + feed('~') + poke_eventloop() + feed('<CR>') + expect([[ + SUB substitution on + two SUBs + ]]) + + feed(':%s/sti/') + poke_eventloop() + feed('~') + poke_eventloop() + feed('B') + poke_eventloop() + feed('<CR>') + expect([[ + SUB subSUBBtution on + two SUBs + ]]) + + feed(':%s/ion/NEW<CR>') + expect([[ + SUB subSUBBtutNEW on + two SUBs + ]]) + + feed(':%s/two/') + poke_eventloop() + feed('N') + poke_eventloop() + feed('~') + poke_eventloop() + feed('<CR>') + expect([[ + SUB subSUBBtutNEW on + NNEW SUBs + ]]) + + feed(':%s/bS/') + poke_eventloop() + feed('~') + poke_eventloop() + feed('W') + poke_eventloop() + feed('<CR>') + expect([[ + SUB suNNEWWUBBtutNEW on + NNEW SUBs + ]]) + end) + end end) describe(":substitute, 'inccommand' preserves undo", function() diff --git a/test/functional/ui/input_spec.lua b/test/functional/ui/input_spec.lua index ea8968a653..7000505909 100644 --- a/test/functional/ui/input_spec.lua +++ b/test/functional/ui/input_spec.lua @@ -114,11 +114,49 @@ describe('mappings', function() end) end) -describe('input utf sequences that contain CSI/K_SPECIAL', function() +describe('input utf sequences that contain K_SPECIAL (0x80)', function() it('ok', function() feed('i…<esc>') expect('…') end) + + it('can be mapped', function() + command('inoremap … E280A6') + feed('i…<esc>') + expect('E280A6') + end) +end) + +describe('input utf sequences that contain CSI (0x9B)', function() + it('ok', function() + feed('iě<esc>') + expect('ě') + end) + + it('can be mapped', function() + command('inoremap ě C49B') + feed('iě<esc>') + expect('C49B') + end) +end) + +describe('input split utf sequences', function() + it('ok', function() + local str = '►' + feed('i' .. str:sub(1, 1)) + helpers.sleep(10) + feed(str:sub(2, 3)) + expect('►') + end) + + it('can be mapped', function() + command('inoremap ► E296BA') + local str = '►' + feed('i' .. str:sub(1, 1)) + helpers.sleep(10) + feed(str:sub(2, 3)) + expect('E296BA') + end) end) describe('input non-printable chars', function() @@ -146,7 +184,7 @@ describe('input non-printable chars', function() {1:~ }| {1:~ }| {1:~ }| - "Xtest-overwrite" [noeol] 1L, 6C | + "Xtest-overwrite" [noeol] 1L, 6B | ]]) -- The timestamp is in second resolution, wait two seconds to be sure. diff --git a/test/functional/ui/popupmenu_spec.lua b/test/functional/ui/popupmenu_spec.lua index 4fc5c389e5..c44e59cfd3 100644 --- a/test/functional/ui/popupmenu_spec.lua +++ b/test/functional/ui/popupmenu_spec.lua @@ -10,6 +10,7 @@ local funcs = helpers.funcs local get_pathsep = helpers.get_pathsep local eq = helpers.eq local pcall_err = helpers.pcall_err +local exec_lua = helpers.exec_lua describe('ui/ext_popupmenu', function() local screen @@ -25,6 +26,7 @@ describe('ui/ext_popupmenu', function() [5] = {bold = true, foreground = Screen.colors.SeaGreen}, [6] = {background = Screen.colors.WebGray}, [7] = {background = Screen.colors.LightMagenta}, + [8] = {foreground = Screen.colors.Red}, }) source([[ function! TestComplete() abort @@ -369,6 +371,111 @@ describe('ui/ext_popupmenu', function() {1:~ }| {2:-- INSERT --} | ]]) + + command('iunmap <f1>') + command('iunmap <f2>') + command('iunmap <f3>') + exec_lua([[ + vim.keymap.set('i', '<f1>', function() vim.api.nvim_select_popupmenu_item(2, true, false, {}) end) + vim.keymap.set('i', '<f2>', function() vim.api.nvim_select_popupmenu_item(-1, false, false, {}) end) + vim.keymap.set('i', '<f3>', function() vim.api.nvim_select_popupmenu_item(1, false, true, {}) end) + ]]) + feed('<C-r>=TestComplete()<CR>') + screen:expect([[ + | + foo^ | + {6:fo x the foo }{1: }| + {7:bar }{1: }| + {7:spam }{1: }| + {1:~ }| + {1:~ }| + {2:-- INSERT --} | + ]]) + + feed('<f1>') + screen:expect([[ + | + spam^ | + {7:fo x the foo }{1: }| + {7:bar }{1: }| + {6:spam }{1: }| + {1:~ }| + {1:~ }| + {2:-- INSERT --} | + ]]) + + feed('<f2>') + screen:expect([[ + | + spam^ | + {7:fo x the foo }{1: }| + {7:bar }{1: }| + {7:spam }{1: }| + {1:~ }| + {1:~ }| + {2:-- INSERT --} | + ]]) + + feed('<f3>') + screen:expect([[ + | + bar^ | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {2:-- INSERT --} | + ]]) + + feed('<esc>ddiaa bb cc<cr>') + feed('<c-x><c-n>') + screen:expect([[ + aa bb cc | + aa^ | + {6:aa }{1: }| + {7:bb }{1: }| + {7:cc }{1: }| + {1:~ }| + {1:~ }| + {2:-- Keyword Local completion (^N^P) }{5:match 1 of 3} | + ]]) + + feed('<f1>') + screen:expect([[ + aa bb cc | + cc^ | + {7:aa }{1: }| + {7:bb }{1: }| + {6:cc }{1: }| + {1:~ }| + {1:~ }| + {2:-- Keyword Local completion (^N^P) }{5:match 3 of 3} | + ]]) + + feed('<f2>') + screen:expect([[ + aa bb cc | + cc^ | + {7:aa }{1: }| + {7:bb }{1: }| + {7:cc }{1: }| + {1:~ }| + {1:~ }| + {2:-- Keyword Local completion (^N^P) }{8:Back at original} | + ]]) + + feed('<f3>') + screen:expect([[ + aa bb cc | + bb^ | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {2:-- INSERT --} | + ]]) end) local function source_complete_month() @@ -2214,6 +2321,47 @@ describe('builtin popupmenu', function() assert_alive() end) + it('is closed by :stopinsert from timer #12976', function() + screen:try_resize(32,14) + command([[call setline(1, ['hello', 'hullo', 'heeee', ''])]]) + feed('Gah<C-N>') + screen:expect([[ + hello | + hullo | + heeee | + hello^ | + {s:hello }{1: }| + {n:hullo }{1: }| + {n:heeee }{1: }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {2:-- }{5:match 1 of 3} | + ]]) + command([[call timer_start(100, { -> execute('stopinsert') })]]) + helpers.sleep(200) + feed('k') -- cursor should move up in Normal mode + screen:expect([[ + hello | + hullo | + heee^e | + hello | + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + {1:~ }| + | + ]]) + end) + it('truncates double-width character correctly when there is no scrollbar', function() screen:try_resize(32,8) command('set completeopt+=menuone,noselect') diff --git a/test/functional/ui/screen_basic_spec.lua b/test/functional/ui/screen_basic_spec.lua index 958e137f65..09bcbc2bbd 100644 --- a/test/functional/ui/screen_basic_spec.lua +++ b/test/functional/ui/screen_basic_spec.lua @@ -6,6 +6,7 @@ local insert = helpers.insert local eq = helpers.eq local eval = helpers.eval local iswin = helpers.iswin +local funcs, meths, exec_lua = helpers.funcs, helpers.meths, helpers.exec_lua describe('screen', function() local screen @@ -127,14 +128,67 @@ local function screen_tests(linegrid) end) it('has correct default title with named file', function() - local expected = (iswin() and 'myfile (C:\\mydir) - NVIM' - or 'myfile (/mydir) - NVIM') + local expected = (iswin() and 'myfile (C:\\mydir) - NVIM' or 'myfile (/mydir) - NVIM') command('set title') command(iswin() and 'file C:\\mydir\\myfile' or 'file /mydir/myfile') screen:expect(function() eq(expected, screen.title) end) end) + + describe('is not changed by', function() + local file1 = iswin() and 'C:\\mydir\\myfile1' or '/mydir/myfile1' + local file2 = iswin() and 'C:\\mydir\\myfile2' or '/mydir/myfile2' + local expected = (iswin() and 'myfile1 (C:\\mydir) - NVIM' or 'myfile1 (/mydir) - NVIM') + local buf2 + + before_each(function() + command('edit '..file1) + buf2 = funcs.bufadd(file2) + command('set title') + end) + + it('calling setbufvar() to set an option in a hidden buffer from i_CTRL-R', function() + command([[inoremap <F2> <C-R>=setbufvar(]]..buf2..[[, '&autoindent', 1) ? '' : ''<CR>]]) + feed('i<F2><Esc>') + command('redraw!') + screen:expect(function() + eq(expected, screen.title) + end) + end) + + it('an RPC call to nvim_buf_set_option in a hidden buffer', function() + meths.buf_set_option(buf2, 'autoindent', true) + command('redraw!') + screen:expect(function() + eq(expected, screen.title) + end) + end) + + it('a Lua callback calling nvim_buf_set_option in a hidden buffer', function() + exec_lua(string.format([[ + vim.schedule(function() + vim.api.nvim_buf_set_option(%d, 'autoindent', true) + end) + ]], buf2)) + command('redraw!') + screen:expect(function() + eq(expected, screen.title) + end) + end) + + it('a Lua callback calling nvim_buf_call in a hidden buffer', function() + exec_lua(string.format([[ + vim.schedule(function() + vim.api.nvim_buf_call(%d, function() end) + end) + ]], buf2)) + command('redraw!') + screen:expect(function() + eq(expected, screen.title) + end) + end) + end) end) describe(':set icon', function() diff --git a/test/functional/vimscript/eval_spec.lua b/test/functional/vimscript/eval_spec.lua index e1459ab5b8..0c2ca8de78 100644 --- a/test/functional/vimscript/eval_spec.lua +++ b/test/functional/vimscript/eval_spec.lua @@ -5,7 +5,7 @@ -- null_spec.lua -- operators_spec.lua -- --- Tests for the Vimscript |functions| library should live in: +-- Tests for the Vimscript |builtin-functions| library should live in: -- test/functional/vimscript/<funcname>_spec.lua -- test/functional/vimscript/functions_spec.lua diff --git a/test/functional/vimscript/functions_spec.lua b/test/functional/vimscript/functions_spec.lua index 0ad7fd8010..20c1400030 100644 --- a/test/functional/vimscript/functions_spec.lua +++ b/test/functional/vimscript/functions_spec.lua @@ -1,4 +1,4 @@ --- Tests for misc Vimscript |functions|. +-- Tests for misc Vimscript |builtin-functions|. -- -- If a function is non-trivial, consider moving its spec to: -- test/functional/vimscript/<funcname>_spec.lua diff --git a/test/functional/vimscript/timer_spec.lua b/test/functional/vimscript/timer_spec.lua index e45b64422f..25e46062b7 100644 --- a/test/functional/vimscript/timer_spec.lua +++ b/test/functional/vimscript/timer_spec.lua @@ -272,4 +272,12 @@ describe('timers', function() ]] eq("Vim(call):E48: Not allowed in sandbox", exc_exec("sandbox call timer_start(0, 'Scary')")) end) + + it('can be triggered after an empty string <expr> mapping', function() + local screen = Screen.new(40, 6) + screen:attach() + command([=[imap <expr> <F2> [timer_start(0, { _ -> execute("throw 'x'", "") }), ''][-1]]=]) + feed('i<F2>') + screen:expect({any='E605: Exception not caught: x'}) + end) end) diff --git a/test/helpers.lua b/test/helpers.lua index 87431e4342..522714c8be 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -815,7 +815,7 @@ end function module.read_nvim_log(logfile, ci_rename) logfile = logfile or os.getenv('NVIM_LOG_FILE') or '.nvimlog' local is_ci = module.isCI() - local keep = is_ci and 999 or 10 + local keep = is_ci and 100 or 10 local lines = module.read_file_list(logfile, -keep) or {} local log = (('-'):rep(78)..'\n' ..string.format('$NVIM_LOG_FILE: %s\n', logfile) diff --git a/test/unit/buffer_spec.lua b/test/unit/buffer_spec.lua index 3692e19379..34c88248e6 100644 --- a/test/unit/buffer_spec.lua +++ b/test/unit/buffer_spec.lua @@ -249,7 +249,7 @@ describe('buffer functions', function() -- -- @param arg Options can be placed in an optional dictionary as the last parameter -- .expected_cell_count The expected number of cells build_stl_str_hl will return - -- .expected_byte_length The expected byte length of the string + -- .expected_byte_length The expected byte length of the string (defaults to byte length of expected_stl) -- .file_name The name of the file to be tested (useful in %f type tests) -- .fillchar The character that will be used to fill any 'extra' space in the stl local function statusline_test (description, @@ -264,7 +264,7 @@ describe('buffer functions', function() local fillchar = option.fillchar or (' '):byte() local expected_cell_count = option.expected_cell_count or statusline_cell_count - local expected_byte_length = option.expected_byte_length or expected_cell_count + local expected_byte_length = option.expected_byte_length or #expected_stl itp(description, function() if option.file_name then @@ -312,12 +312,12 @@ describe('buffer functions', function() statusline_test('should put fillchar `~` in between text', 10, 'abc%=def', 'abc~~~~def', {fillchar=('~'):byte()}) + statusline_test('should put fillchar `━` in between text', 10, + 'abc%=def', 'abc━━━━def', + {fillchar=0x2501}) statusline_test('should handle zero-fillchar as a space', 10, 'abcde%=', 'abcde ', {fillchar=0}) - statusline_test('should handle multibyte-fillchar as a dash', 10, - 'abcde%=', 'abcde-----', - {fillchar=0x80}) statusline_test('should print the tail file name', 80, '%t', 'buffer_spec.lua', {file_name='test/unit/buffer_spec.lua', expected_cell_count=15}) @@ -366,70 +366,86 @@ describe('buffer functions', function() statusline_test('should ignore trailing %', 3, 'abc%', 'abc') - -- alignment testing - statusline_test('should right align when using =', 20, - 'neo%=vim', 'neo vim') - statusline_test('should, when possible, center text when using %=text%=', 20, - 'abc%=neovim%=def', 'abc neovim def') - statusline_test('should handle uneven spacing in the buffer when using %=text%=', 20, - 'abc%=neo_vim%=def', 'abc neo_vim def') - statusline_test('should have equal spaces even with non-equal sides when using =', 20, - 'foobar%=test%=baz', 'foobar test baz') - statusline_test('should have equal spaces even with longer right side when using =', 20, - 'a%=test%=longtext', 'a test longtext') - statusline_test('should handle an empty left side when using ==', 20, - '%=test%=baz', ' test baz') - statusline_test('should handle an empty right side when using ==', 20, - 'foobar%=test%=', 'foobar test ') - statusline_test('should handle consecutive empty ==', 20, - '%=%=test%=', ' test ') - statusline_test('should handle an = alone', 20, - '%=', ' ') - statusline_test('should right align text when it is alone with =', 20, - '%=foo', ' foo') - statusline_test('should left align text when it is alone with =', 20, - 'foo%=', 'foo ') - - statusline_test('should approximately center text when using %=text%=', 21, - 'abc%=neovim%=def', 'abc neovim def') - statusline_test('should completely fill the buffer when using %=text%=', 21, - 'abc%=neo_vim%=def', 'abc neo_vim def') - statusline_test('should have equal spaces even with non-equal sides when using =', 21, - 'foobar%=test%=baz', 'foobar test baz') - statusline_test('should have equal spaces even with longer right side when using =', 21, - 'a%=test%=longtext', 'a test longtext') - statusline_test('should handle an empty left side when using ==', 21, - '%=test%=baz', ' test baz') - statusline_test('should handle an empty right side when using ==', 21, - 'foobar%=test%=', 'foobar test ') - - statusline_test('should quadrant the text when using 3 %=', 40, - 'abcd%=n%=eovim%=ef', 'abcd n eovim ef') - statusline_test('should work well with %t', 40, - '%t%=right_aligned', 'buffer_spec.lua right_aligned', + -- alignment testing with fillchar + local function statusline_test_align (description, + statusline_cell_count, + input_stl, + expected_stl, + arg) + arg = arg or {} + statusline_test(description .. ' without fillchar', + statusline_cell_count, input_stl, expected_stl:gsub('%~', ' '), arg) + arg.fillchar = ('!'):byte() + statusline_test(description .. ' with fillchar `!`', + statusline_cell_count, input_stl, expected_stl:gsub('%~', '!'), arg) + arg.fillchar = 0x2501 + statusline_test(description .. ' with fillchar `━`', + statusline_cell_count, input_stl, expected_stl:gsub('%~', '━'), arg) + end + + statusline_test_align('should right align when using =', 20, + 'neo%=vim', 'neo~~~~~~~~~~~~~~vim') + statusline_test_align('should, when possible, center text when using %=text%=', 20, + 'abc%=neovim%=def', 'abc~~~~neovim~~~~def') + statusline_test_align('should handle uneven spacing in the buffer when using %=text%=', 20, + 'abc%=neo_vim%=def', 'abc~~~neo_vim~~~~def') + statusline_test_align('should have equal spaces even with non-equal sides when using =', 20, + 'foobar%=test%=baz', 'foobar~~~test~~~~baz') + statusline_test_align('should have equal spaces even with longer right side when using =', 20, + 'a%=test%=longtext', 'a~~~test~~~~longtext') + statusline_test_align('should handle an empty left side when using ==', 20, + '%=test%=baz', '~~~~~~test~~~~~~~baz') + statusline_test_align('should handle an empty right side when using ==', 20, + 'foobar%=test%=', 'foobar~~~~~test~~~~~') + statusline_test_align('should handle consecutive empty ==', 20, + '%=%=test%=', '~~~~~~~~~~test~~~~~~') + statusline_test_align('should handle an = alone', 20, + '%=', '~~~~~~~~~~~~~~~~~~~~') + statusline_test_align('should right align text when it is alone with =', 20, + '%=foo', '~~~~~~~~~~~~~~~~~foo') + statusline_test_align('should left align text when it is alone with =', 20, + 'foo%=', 'foo~~~~~~~~~~~~~~~~~') + + statusline_test_align('should approximately center text when using %=text%=', 21, + 'abc%=neovim%=def', 'abc~~~~neovim~~~~~def') + statusline_test_align('should completely fill the buffer when using %=text%=', 21, + 'abc%=neo_vim%=def', 'abc~~~~neo_vim~~~~def') + statusline_test_align('should have equal spacing even with non-equal sides when using =', 21, + 'foobar%=test%=baz', 'foobar~~~~test~~~~baz') + statusline_test_align('should have equal spacing even with longer right side when using =', 21, + 'a%=test%=longtext', 'a~~~~test~~~~longtext') + statusline_test_align('should handle an empty left side when using ==', 21, + '%=test%=baz', '~~~~~~~test~~~~~~~baz') + statusline_test_align('should handle an empty right side when using ==', 21, + 'foobar%=test%=', 'foobar~~~~~test~~~~~~') + + statusline_test_align('should quadrant the text when using 3 %=', 40, + 'abcd%=n%=eovim%=ef', 'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~ef') + statusline_test_align('should work well with %t', 40, + '%t%=right_aligned', 'buffer_spec.lua~~~~~~~~~~~~right_aligned', {file_name='test/unit/buffer_spec.lua'}) - statusline_test('should work well with %t and regular text', 40, - 'l%=m_l %t m_r%=r', 'l m_l buffer_spec.lua m_r r', + statusline_test_align('should work well with %t and regular text', 40, + 'l%=m_l %t m_r%=r', 'l~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r', {file_name='test/unit/buffer_spec.lua'}) - statusline_test('should work well with %=, %t, %L, and %l', 40, - '%t %= %L %= %l', 'buffer_spec.lua 1 0', + statusline_test_align('should work well with %=, %t, %L, and %l', 40, + '%t %= %L %= %l', 'buffer_spec.lua ~~~~~~~~~ 1 ~~~~~~~~~~ 0', {file_name='test/unit/buffer_spec.lua'}) - statusline_test('should quadrant the text when using 3 %=', 41, - 'abcd%=n%=eovim%=ef', 'abcd n eovim ef') - statusline_test('should work well with %t', 41, - '%t%=right_aligned', 'buffer_spec.lua right_aligned', + statusline_test_align('should quadrant the text when using 3 %=', 41, + 'abcd%=n%=eovim%=ef', 'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~~ef') + statusline_test_align('should work well with %t', 41, + '%t%=right_aligned', 'buffer_spec.lua~~~~~~~~~~~~~right_aligned', {file_name='test/unit/buffer_spec.lua'}) - statusline_test('should work well with %t and regular text', 41, - 'l%=m_l %t m_r%=r', 'l m_l buffer_spec.lua m_r r', + statusline_test_align('should work well with %t and regular text', 41, + 'l%=m_l %t m_r%=r', 'l~~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r', {file_name='test/unit/buffer_spec.lua'}) - statusline_test('should work well with %=, %t, %L, and %l', 41, - '%t %= %L %= %l', 'buffer_spec.lua 1 0', + statusline_test_align('should work well with %=, %t, %L, and %l', 41, + '%t %= %L %= %l', 'buffer_spec.lua ~~~~~~~~~~ 1 ~~~~~~~~~~ 0', {file_name='test/unit/buffer_spec.lua'}) - statusline_test('should work with 10 %=', 50, + statusline_test_align('should work with 10 %=', 50, 'aaaa%=b%=c%=d%=e%=fg%=hi%=jk%=lmnop%=qrstuv%=wxyz', - 'aaaa b c d e fg hi jk lmnop qrstuv wxyz') + 'aaaa~~b~~c~~d~~e~~fg~~hi~~jk~~lmnop~~qrstuv~~~wxyz') -- stl item testing local tabline = '' @@ -452,11 +468,10 @@ describe('buffer functions', function() -- multi-byte testing statusline_test('should handle multibyte characters', 10, - 'Ĉ%=x', 'Ĉ x', - {expected_byte_length=11}) + 'Ĉ%=x', 'Ĉ x') statusline_test('should handle multibyte characters and different fillchars', 10, 'Ą%=mid%=end', 'Ą@mid@@end', - {fillchar=('@'):byte(), expected_byte_length=11}) + {fillchar=('@'):byte()}) -- escaping % testing statusline_test('should handle escape of %', 4, 'abc%%', 'abc%') diff --git a/test/unit/charset/vim_str2nr_spec.lua b/test/unit/charset/vim_str2nr_spec.lua index 5fc3b83a13..caf330c378 100644 --- a/test/unit/charset/vim_str2nr_spec.lua +++ b/test/unit/charset/vim_str2nr_spec.lua @@ -463,7 +463,7 @@ describe('vim_str2nr()', function() test_vim_str2nr("1'2'3'4", flags, {len = 7, num = 1234, unum = 1234, pre = 0}, 0) -- counter-intuitive, but like Vim, strict=true should partially accept - -- these: (' and - are not alpha-numeric) + -- these: (' and - are not alphanumeric) test_vim_str2nr("7''331", flags, {len = 1, num = 7, unum = 7, pre = 0}, 0) test_vim_str2nr("123'x4", flags, {len = 3, num = 123, unum = 123, pre = 0}, 0) test_vim_str2nr("1337'", flags, {len = 4, num = 1337, unum = 1337, pre = 0}, 0) diff --git a/test/unit/garray_spec.lua b/test/unit/garray_spec.lua index 28df8a6e3f..5d41dd39ec 100644 --- a/test/unit/garray_spec.lua +++ b/test/unit/garray_spec.lua @@ -18,7 +18,7 @@ local growsize = 95 -- constructing a class wrapper around garray. It could for example associate -- ga_clear_strings to the underlying garray cdata if the garray is a string -- array. But for now I estimate that that kind of magic might make testing --- less "transparant" (i.e.: the interface would become quite different as to +-- less "transparent" (i.e.: the interface would become quite different as to -- how one would use it from C. -- accessors diff --git a/test/unit/marktree_spec.lua b/test/unit/marktree_spec.lua index 10d02d2eb4..3c96bc5f58 100644 --- a/test/unit/marktree_spec.lua +++ b/test/unit/marktree_spec.lua @@ -32,11 +32,11 @@ local function shadoworder(tree, shadow, iter, giveorder) local mark = lib.marktree_itr_current(iter) local id = tonumber(mark.id) local spos = shadow[id] - if (mark.row ~= spos[1] or mark.col ~= spos[2]) then - error("invalid pos for "..id..":("..mark.row..", "..mark.col..") instead of ("..spos[1]..", "..spos[2]..")") + if (mark.pos.row ~= spos[1] or mark.pos.col ~= spos[2]) then + error("invalid pos for "..id..":("..mark.pos.row..", "..mark.pos.col..") instead of ("..spos[1]..", "..spos[2]..")") end - if mark.right_gravity ~= spos[3] then - error("invalid gravity for "..id..":("..mark.row..", "..mark.col..")") + if lib.mt_right_test(mark) ~= spos[3] then + error("invalid gravity for "..id..":("..mark.pos.row..", "..mark.pos.col..")") end if count > 0 then if not pos_leq(last, spos) then @@ -87,7 +87,21 @@ local function dosplice(tree, shadow, start, old_extent, new_extent) shadowsplice(shadow, start, old_extent, new_extent) end +local last_id = nil + +local function put(tree, row, col, gravitate) + last_id = last_id + 1 + local my_id = last_id + + lib.marktree_put_test(tree, my_id, row, col, gravitate); + return my_id +end + describe('marktree', function() + before_each(function() + last_id = 0 + end) + itp('works', function() local tree = ffi.new("MarkTree[1]") -- zero initialized by luajit local shadow = {} @@ -97,7 +111,7 @@ describe('marktree', function() for i = 1,100 do for j = 1,100 do local gravitate = (i%2) > 0 - local id = tonumber(lib.marktree_put(tree, j, i, gravitate, 0)) + local id = put(tree, j, i, gravitate) ok(id > 0) eq(nil, shadow[id]) shadow[id] = {j,i,gravitate} @@ -115,12 +129,12 @@ describe('marktree', function() eq({}, id2pos) for i,ipos in pairs(shadow) do - local pos = lib.marktree_lookup(tree, i, iter) - eq(ipos[1], pos.row) - eq(ipos[2], pos.col) + local p = lib.marktree_lookup_ns(tree, -1, i, false, iter) + eq(ipos[1], p.pos.row) + eq(ipos[2], p.pos.col) local k = lib.marktree_itr_current(iter) - eq(ipos[1], k.row) - eq(ipos[2], k.col, ipos[1]) + eq(ipos[1], k.pos.row) + eq(ipos[2], k.pos.col, ipos[1]) lib.marktree_itr_next(tree, iter) -- TODO(bfredl): use id2pos to check neighbour? -- local k2 = lib.marktree_itr_current(iter) @@ -130,8 +144,8 @@ describe('marktree', function() lib.marktree_itr_get(tree, ipos[1], ipos[2], iter) local k = lib.marktree_itr_current(iter) eq(i, tonumber(k.id)) - eq(ipos[1], k.row) - eq(ipos[2], k.col) + eq(ipos[1], k.pos.row) + eq(ipos[2], k.pos.col) end ok(lib.marktree_itr_first(tree, iter)) @@ -146,8 +160,8 @@ describe('marktree', function() lib.marktree_itr_get(tree, i, 50+ci, iter) local k = lib.marktree_itr_current(iter) local id = tonumber(k.id) - eq(shadow[id][1], k.row) - eq(shadow[id][2], k.col) + eq(shadow[id][1], k.pos.row) + eq(shadow[id][2], k.pos.col) lib.marktree_del_itr(tree, iter, false) shadow[id] = nil end @@ -191,7 +205,7 @@ describe('marktree', function() -- https://github.com/neovim/neovim/pull/14719 lib.marktree_clear(tree) for i = 1,20 do - lib.marktree_put(tree, i, i, false, 0) + put(tree, i, i, false) end lib.marktree_itr_get(tree, 10, 10, iter) diff --git a/test/unit/os/env_spec.lua b/test/unit/os/env_spec.lua index e7cb5e5d5e..a0e02b6624 100644 --- a/test/unit/os/env_spec.lua +++ b/test/unit/os/env_spec.lua @@ -191,7 +191,7 @@ describe('env.c', function() if ffi.abi('64bit') then -- couldn't use a bigger number because it gets converted to - -- double somewere, should be big enough anyway + -- double somewhere, should be big enough anyway -- maxuint64 = ffi.new 'size_t', 18446744073709551615 local maxuint64 = ffi.new('size_t', 18446744073709000000) eq(NULL, cimp.os_getenvname_at_index(maxuint64)) diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index 7fd71cb1ae..0bb33772cd 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -1029,7 +1029,7 @@ describe('fs.c', function() itp('returns the correct blocksize of a file', function() local path = 'unit-test-directory/test.file' -- there is a bug in luafilesystem where - -- `lfs.attributes path, 'blksize'` returns the worng value: + -- `lfs.attributes path, 'blksize'` returns the wrong value: -- https://github.com/keplerproject/luafilesystem/pull/44 -- using this workaround for now: local blksize = lfs.attributes(path).blksize @@ -1038,7 +1038,7 @@ describe('fs.c', function() if blksize then eq(blksize, fs.os_fileinfo_blocksize(info)) else - -- luafs dosn't support blksize on windows + -- luafs doesn't support blksize on windows -- libuv on windows returns a constant value as blocksize -- checking for this constant value should be enough eq(2048, fs.os_fileinfo_blocksize(info)) diff --git a/test/unit/path_spec.lua b/test/unit/path_spec.lua index 15ce59747e..fb476397e6 100644 --- a/test/unit/path_spec.lua +++ b/test/unit/path_spec.lua @@ -626,4 +626,20 @@ describe('path.c', function() eq(false, path_with_extension('/some/path/file', 'lua')) end) end) + + describe('path_with_url', function() + itp('scheme is alpha and inner hyphen only', function() + local function path_with_url(fname) + return cimp.path_with_url(to_cstr(fname)) + end + eq(1, path_with_url([[test://xyz/foo/b0]])) + eq(2, path_with_url([[test:\\xyz\foo\b0]])) + eq(0, path_with_url([[test+abc://xyz/foo/b1]])) + eq(0, path_with_url([[test_abc://xyz/foo/b2]])) + eq(1, path_with_url([[test-abc://xyz/foo/b3]])) + eq(2, path_with_url([[test-abc:\\xyz\foo\b3]])) + eq(0, path_with_url([[-test://xyz/foo/b4]])) + eq(0, path_with_url([[test-://xyz/foo/b5]])) + end) + end) end) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 613b39c833..174f1cbaba 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -163,10 +163,10 @@ set(LUAROCKS_SHA256 ab6612ca9ab87c6984871d2712d05525775e8b50172701a0a1cabddf76de set(UNIBILIUM_URL https://github.com/neovim/unibilium/archive/92d929f.tar.gz) set(UNIBILIUM_SHA256 29815283c654277ef77a3adcc8840db79ddbb20a0f0b0c8f648bd8cd49a02e4b) -set(LIBTERMKEY_URL http://www.leonerd.org.uk/code/libtermkey/libtermkey-0.22.tar.gz) +set(LIBTERMKEY_URL https://www.leonerd.org.uk/code/libtermkey/libtermkey-0.22.tar.gz) set(LIBTERMKEY_SHA256 6945bd3c4aaa83da83d80a045c5563da4edd7d0374c62c0d35aec09eb3014600) -set(LIBVTERM_URL http://www.leonerd.org.uk/code/libvterm/libvterm-0.1.4.tar.gz) +set(LIBVTERM_URL https://www.leonerd.org.uk/code/libvterm/libvterm-0.1.4.tar.gz) set(LIBVTERM_SHA256 bc70349e95559c667672fc8c55b9527d9db9ada0fb80a3beda533418d782d3dd) set(LUV_VERSION 1.42.0-1) @@ -203,8 +203,8 @@ set(LIBICONV_SHA256 ccf536620a45458d26ba83887a983b96827001e92a13847b45e4925cc891 set(TREESITTER_C_URL https://github.com/tree-sitter/tree-sitter-c/archive/v0.20.1.tar.gz) set(TREESITTER_C_SHA256 ffcc2ef0eded59ad1acec9aec4f9b0c7dd209fc1a85d85f8b0e81298e3dddcc2) -set(TREESITTER_URL https://github.com/tree-sitter/tree-sitter/archive/v0.20.2.tar.gz) -set(TREESITTER_SHA256 2a0445f8172bbf83db005aedb4e893d394e2b7b33251badd3c94c2c5cc37c403) +set(TREESITTER_URL https://github.com/tree-sitter/tree-sitter/archive/v0.20.3.tar.gz) +set(TREESITTER_SHA256 ab52fe93e0c658cff656b9d10d67cdd29084247052964eba13ed6f0e9fa3bd36) if(USE_BUNDLED_UNIBILIUM) include(BuildUnibilium) |