1"=============================================================================
  2"
  3" NAME
  4"
  5"     .vimrc
  6"
  7" DESCRIPTION
  8"
  9"     Settings for Vim editor loaded upon startup.
 10"
 11" NOTES
 12"
 13"     UNIX, Mac OS X:  Copy to ~/.vimrc
 14"     Windows:         Copy to C:/Program Files/vim/_vimrc
 15"
 16" AUTHOR
 17"
 18"     Bram Moolenaar             <Bram@vim.org>
 19"     Sean E. O'Connor           10 Oct 2024
 20"
 21"=============================================================================
 22
 23set fileformat=unix
 24set encoding=UTF-8
 25
 26" Use Vim settings, rather then Vi settings (much better!).
 27" This must be first, because it changes other options as a side effect.
 28set nocompatible
 29
 30" Vim can detect the file type.
 31filetype on
 32
 33" Enable plugins
 34filetype plugin on
 35
 36" Mouse and window behavior tuned for Microslime Windows.
 37behave mswin
 38
 39" Restore Control-F to page forward instead of find dialog box.
 40"unmap<C-F>
 41
 42" Allow backspacing over everything in insert mode.
 43set backspace=indent,eol,start
 44
 45" Turn on backup option.
 46set backup
 47
 48" Create the backup directory if it doesn't exist.
 49if !isdirectory($HOME.'/.vim/.backup')
 50    silent call mkdir( $HOME.'/.vim/.backup', 'p' )
 51endif
 52
 53" Store backups in the home directory ~/.vim/.backup.
 54set backupdir=~/.vim/.backup
 55
 56" Make a backup before overwriting the current buffer.
 57set writebackup
 58
 59" Overwrite the original backup file.
 60set backupcopy=yes
 61
 62" Put swap files in the home directory ~/.vim/.swp
 63if !isdirectory($HOME."/.vim/.swp")
 64    silent call mkdir( $HOME.'/.vim/.swp', 'p' )
 65endif
 66
 67set directory=~/.vim/.swp
 68
 69set history=2000     " Keep 2000 lines of command line history.
 70set ruler           " Show the cursor position all the time.
 71set showcmd         " Display incomplete commands.
 72
 73set incsearch       " Do incremental searching.
 74set hlsearch        " Searches are highlighted.
 75
 76set wildmenu        " File name completing using tabs cycles through all possibilities.
 77set wildmode=list:longest " Make similar to bash completion.
 78set wildignore=*.docx,*.jpg,*.png,*.gif,*.pyc,*.xlsx:w  " Ignore these files when doing file name completion.
 79
 80set lazyredraw      " Don't redraw screen while running macros.
 81
 82set lines=80        " Number of lines visible on the screen.
 83set columns=160     " Number of columns visible on the screen.
 84winpos 700 100      " Initial window position (MacBookPro).
 85set winwidth=240    " Initial window width.
 86set winminwidth=150 " Minimum width.
 87
 88set cursorline      " Highlight the current line.
 89set number          " Show line numbers.
 90set nosmartindent   " Don't use smart indenting.
 91
 92set tabstop=4       " Tabs are 4 spaces wide.
 93set softtabstop=4   " When editing, tabs are 4 spaces wide.
 94set shiftwidth=4    " Indent 4 spaces.
 95set expandtab       " Expand tabs into spaces.
 96set smarttab        " tab in front of a line inserts shiftwidth spaces, and backspace will delete that many spaces.
 97
 98set showmatch       " Show matching parentheses.
 99set matchtime=5     " Match time is 1/2 sec.
100
101" Error blink and bell.
102set errorbells
103set visualbell
104
105" Set file paths to my most commonly used directories (MacBookPro).
106set path="~/Applications/vim/**"
107set path+="~/Desktop/Sean/WebSite/**"
108
109" Default directory is my current active subdirectory
110" in my web page directory.
111" This is where :e . takes us.
112cd ~/Desktop/Sean/WebSite/Mathematics/AbstractAlgebra/PrimitivePolynomials/Project/SourceCode
113
114" Set paths for tags files generated by Ctags.  See :help tags
115" "./tags" means search for the file "tags" in the same directory as the current f i l e  you are editing.
116" "tags" means search for the file "tags" in the current working directory (the directory shown by the command :pwd)
117" NOTE:  this is affected if you set autochdir (see below).
118" Then search from the directory containing tags to your home directory.
119set tags=./tags,tags;~
120
121" Automatically change the current working directory to the one containing the file which was opened.  See :help autochdir
122"Note: When this option is on some plugins may not work.
123set autochdir
124
125" Color them the same as the C group-name "Type".
126highlight link xType Type
127
128" Persistent undo:  undo changes even after saving the file and exiting Vim.
129if has('persistent_undo')
130    set undofile
131    set undodir=$HOME/.vim/.backup
132endif
133
134" ===============================================================================
135" | Color scheme and font for the full gVim GUI.
136" | Use the default for launching vim from a command window.
137" ===============================================================================
138if has("gui_running")
139
140    colorscheme torte
141
142    " To verify the if condition, on the Vim command line, do
143    "    :echom has("x11")
144    " and look for 1 or 0.
145    "
146    " To find out the font type:
147    "     In the Vim command line, do
148    "         :set guifont=*
149    "     to make a menu come up.
150    "
151    "     Select the font from the menu.
152    "
153    "     Do the command
154    "         :set guifont?
155    "     to find out the name.
156    "
157    "     In the if tests below, add
158    "         set guifont=<name of the font>
159    "     Escape all spaces with backslashes.
160    "
161    if has("gui_win32")
162        set guifont=Courier_New:h13:b
163    " Test for Mac first.
164    elseif has("mac")
165        set guifont=Menlo\ Regular:h12
166    " On Ubuntu Linux:
167    elseif has("x11")
168        set guifont=DejaVu\ Sans\ Mono\ 10
169    " Otherwise
170    else
171        set guifont=*
172    endif
173endif
174
175" Switch syntax highlighting on, when the terminal has colors.
176" Also switch on highlighting the last used search pattern.
177if &t_Co > 2 || has("gui_running")
178  syntax on     " Turn on syntax highlighting.
179  set hlsearch  " Searches are highlighted.
180endif
181
182" ===============================================================================
183" |  Abbreviations
184" ===============================================================================
185
186" Abbreviations.  My name:
187:iabbrev soc Sean E. O'Connor
188
189" Right arrow.
190:iabbrev rar &#10148;
191
192" ===============================================================================
193" |  Shortcuts - HTML and CSS
194" ===============================================================================
195
196" Remapping looks like
197"     <map_mode> <what you type> <what is executed>
198" These are the map modes:
199"     nnoremap - Map keys in normal mode.
200"     inoremap - Map keys in insert mode.
201"     vnoremap - Map keys in visual mode.
202
203" Let comma be the character which starts any mapping command.
204let mapleader=","
205
206" Insert html math symbols.
207nnoremap ,th  i<em>Theorem.</em>
208nnoremap ,pf  i<em>Proof.</em>
209nnoremap ,lem i<em>Lemma.</em>
210nnoremap ,cor i<em>Corollary.</em>
211nnoremap ,qed i$\blacksquare$
212
213" Insert Blender hotkeys.
214nnoremap ,bhot a <em class="hotkey"></em><ESC>4hi
215
216" Insert html emphasis.
217nnoremap ,em a <em></em><ESC>4hi
218
219" Insert an HTML element with the indentation at the same level as the neighbors.
220"     First CR finishes the call to function HTMLParagraph()
221"     Second CR takes us out of command mode :
222"     To see the echom debug statements, type :messages.  To clear all
223"     messages type :messages clear
224function! InsertHTMLElement( prefix, suffix )
225    echom "HTMLParagraph"
226    " Scan the previous few lines until you see a line which has an indent > 0.  
227    " An indent value of -1 means the line does not exist (past the top or bottom of the file.  Zero means a blank line.
228    let current_line_num = line(".")
229    let num_lines_to_search = 10
230    let previous_nonzero_indent = 0
231    for line_num in range( current_line_num-1, current_line_num - num_lines_to_search, -1)
232        echom 'line_num = ' line_num
233        let line_indent = indent( line_num )
234        echom 'line_indent = ' line_indent
235        if line_indent > 0
236            let previous_nonzero_indent = line_indent
237            break
238        endif
239    endfor
240    echom 'previous_nonzero_indent = ' previous_nonzero_indent
241    "Create indentation with blanks.
242    let blanks = repeat( " ", previous_nonzero_indent )
243    echom 'blanks = ' '|' .. blanks .. '|'
244    " Create  scroll box lines as a list.
245    """""let scrollboxlines = [ blanks .. '<div class="scrollBox"> <div class="scrollBoxContent">', blanks, blanks .. '</div></div>', '']
246    let scrollboxlines = [ blanks .. a:prefix, blanks, blanks .. a:suffix, '']
247    echo 'scrollboxlines = ' scrollboxlines
248    " Append the scroll box lines after the current line.
249    call append( '.', scrollboxlines )
250endfunction
251
252" Insert a scroll box.
253nnoremap <leader>sb :call InsertHTMLElement( '<div class="scrollBox"> <div class="scrollBoxContent">', '</div></div>' )<CR><CR>jjA
254
255" Insert an html paragraph.
256nnoremap <leader>par :call InsertHTMLElement( '<p>', '</p>' )<CR><CR>jjA
257
258" Insert hyperlink.
259nnoremap <leader>hr :call InsertHTMLElement('<a href="', '</a>')<CR><CR>kA<ESC>jj$JJa"><ESC>hhxi
260
261" Insert an image.
262nnoremap <leader>im :call InsertHTMLElement('<img class="centeredsmaller" alt="X" src="Images/X.jpg">','')<CR><CR>jfXfXxi
263
264" Insert a nonbreaking space.
265nnoremap ,s i&nbsp;<ESC>
266
267" ===============================================================================
268" |  Shortcuts - miscellaneous
269" ===============================================================================
270
271" Make jj the same as ESCAPE in insert mode.
272inoremap jk <esc>
273
274" Reload this VIM resource file.
275nnoremap ,sou :source $MYVIMRC<CR>
276
277" Save the file.
278nnoremap ,sa   :w<CR>
279
280" ===============================================================================
281" |  Shortcuts - formatting
282" ===============================================================================
283
284" Trim blanks at end of all lines. Turn off search highlighting.
285nnoremap ,tbe :%s/\s*$//<CR><ESC>:nohlsearch<CR><ESC>
286
287" Make p in Visual mode replace the selected text with the "" register.
288vnoremap p <Esc>:let current_reg = @"<CR>gvs<C-R>=current_reg<CR><Esc>
289
290" Turn off highlighting after searches.
291nnoremap ,<SPACE> :nohlsearch<CR>
292
293" Toggle comment lines.  You can highlight the range
294" with the mouse and then type ,to
295nnoremap ,to :call ToggleComment()<CR>
296
297function! ToggleComment()
298    " Get the line under the cursor
299    " Check if it begins with |
300    if getline(".") =~ '^|'
301        let s:savedSearchPat=@/
302        s/^|//
303        let @/=s:savedSearchPat
304    else
305        let s:savedSearchPat=@/
306        s/^/|/
307        let @/=s:savedSearchPat
308    endif
309endfunction
310
311" Convert C comments on a line to C++ comments.
312nnoremap ,cc :call ConvertCCommentToCpp()<CR>
313
314function! ConvertCCommentToCpp()
315    " Get the line under the cursor
316    " Check if contains a /*
317    if getline(".") =~ '\/\*'
318        let s:savedSearchPat=@/
319        s/\/\*/\/\//
320        let @/=s:savedSearchPat
321    endif
322    if getline(".") =~ '\*\/'
323        let s:savedSearchPat=@/
324        s/\*\///
325        let @/=s:savedSearchPat
326    endif
327endfunction
328
329" Justify a paragraph.
330nnoremap ,j gqap
331
332function! HardcopyPDF()
333    " https://askubuntu.com/questions/705973/how-can-i-print-from-vim-to-pdf
334    " Vim hardcopy generates a PostScript file.
335    " Convert to PDF, delete the PS file, tell where the PDF is located.
336    hardcopy > %.ps | !ps2pdf %.ps && rm %.ps && echom 'Created: %.pdf'
337endfunction
338
339" Hardcopy to *.pdf file.
340nnoremap ,pdf :call HardcopyPDF()<CR>
341
342" Special one time mapping.  Change master_xyz to local_xyz
343nnoremap ,ll /master<CR>dt_ilocal<ESC>
344
345" ===============================================================================
346" |  Folding
347" ===============================================================================
348" zR - open all folds
349" zM - close all folds
350" zj, zk - move between folds
351" za - toggle folds;  mapped to shortcut ,,
352" :help usr_28
353set foldmethod=indent  " Folding based on indentation.
354set foldcolumn=2       " Show the status of the folds in the left column.
355nnoremap <leader>, za
356
357" Save the folds in this file upon write, and load again upon read.
358" Saved in ~/.vim/view
359augroup remember_folds
360    autocmd! | " Clear all existing autocommands
361    autocmd BufWinLeave * silent! mkview
362    autocmd BufWinEnter * silent! loadview
363augroup END