blob: 0c4fd541f9bfd491ae6e8f6115160dcd7920c644 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" vim: set sw=4 sts=4:
Bram Moolenaar6e649222021-10-04 21:32:54 +01002" Language: Prolog
3" Maintainer: Gergely Kontra <kgergely@mcl.hu> (Invalid email address)
4" Doug Kearns <dougkearns@gmail.com>
5" Revised on: 2002.02.18. 23:34:05
Bram Moolenaar95bafa22018-10-02 13:26:25 +02006" Last change by: Takuya Fujiwara, 2018 Sep 23
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +01007" 2022 April: b:undo_indent added by Doug Kearns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
9" TODO:
10" checking with respect to syntax highlighting
11" ignoring multiline comments
12" detecting multiline strings
13
14" Only load this indent file when no other was loaded.
15if exists("b:did_indent")
16 finish
17endif
18
19let b:did_indent = 1
20
21setlocal indentexpr=GetPrologIndent()
22setlocal indentkeys-=:,0#
23setlocal indentkeys+=0%,-,0;,>,0)
24
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010025let b:undo_indent = "setl inde< indk<"
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027" Only define the function once.
28"if exists("*GetPrologIndent")
29" finish
30"endif
31
32function! GetPrologIndent()
33 " Find a non-blank line above the current line.
34 let pnum = prevnonblank(v:lnum - 1)
35 " Hit the start of the file, use zero indent.
36 if pnum == 0
37 return 0
38 endif
39 let line = getline(v:lnum)
40 let pline = getline(pnum)
41
42 let ind = indent(pnum)
43 " Previous line was comment -> use previous line's indent
44 if pline =~ '^\s*%'
Bram Moolenaar95bafa22018-10-02 13:26:25 +020045 return ind
46 endif
47 " Previous line was the start of block comment -> +1 after '/*' comment
48 if pline =~ '^\s*/\*'
49 return ind + 1
50 endif
51 " Previous line was the end of block comment -> -1 after '*/' comment
52 if pline =~ '^\s*\*/'
53 return ind - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 endif
55 " Check for clause head on previous line
Bram Moolenaar95bafa22018-10-02 13:26:25 +020056 if pline =~ '\%(:-\|-->\)\s*\(%.*\)\?$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020057 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 " Check for end of clause on previous line
59 elseif pline =~ '\.\s*\(%.*\)\?$'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020060 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 endif
62 " Check for opening conditional on previous line
63 if pline =~ '^\s*\([(;]\|->\)'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020064 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 endif
66 " Check for closing an unclosed paren, or middle ; or ->
67 if line =~ '^\s*\([);]\|->\)'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020068 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 endif
70 return ind
71endfunction