Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functions to persist/recall Vim values from/to files
- Loading branch information
Showing
4 changed files
with
136 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
" Persist/recall Vim values from/to files. | ||
" | ||
" Author: Peter Odding <peter@peterodding.com> | ||
" Last Change: June 22, 2014 | ||
" URL: http://peterodding.com/code/vim/misc/ | ||
" | ||
" Vim's [string()][] function can be used to serialize Vim script values like | ||
" numbers, strings, lists, dictionaries and composites of them to a string | ||
" which can later be evaluated using the [eval()][] function to turn it back | ||
" into the original value. This Vim script provides functions to use these | ||
" functions to persist and recall Vim values from/to files. This is very | ||
" useful for communication between (possibly concurrent) Vim processes. | ||
|
||
function! xolox#misc#persist#load(filename, ...) " {{{1 | ||
" Read a Vim value like a number, string, list or dictionary from a file | ||
" using [readfile()][] and [eval()][]. The first argument is the filename of | ||
" the file to read (a string). The optional second argument specifies the | ||
" default value which is returned when the file can't be loaded. This | ||
" function returns the loaded value or the default value (which itself | ||
" defaults to the integer 0). | ||
" | ||
" [eval()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#eval() | ||
" [readfile()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#readfile() | ||
let default_value = exists('a:1') ? a:1 : 0 | ||
try | ||
let lines = readfile(a:filename) | ||
return eval(join(lines, "\n")) | ||
catch | ||
return default_value | ||
endtry | ||
endfunction | ||
|
||
function! xolox#misc#persist#save(filename, value) " {{{1 | ||
" Write a Vim value like a number, string, list or dictionary to a file | ||
" using [string()][] and [writefile()][]. The first argument is the filename | ||
" of the file to write (a string) and the second argument is the value to | ||
" write (any value). | ||
" | ||
" This function writes the serialized value to an intermediate file which is | ||
" then renamed into place atomically. This avoids issues with concurrent | ||
" processes where for example a producer has written a partial file which is | ||
" read by a consumer before the file is complete. In this case the consumer | ||
" would read a corrupt value. The rename trick avoids this problem. | ||
" | ||
" [string()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#string() | ||
" [writefile()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#writefile() | ||
let intermediate_file = printf('%s.tmp', a:filename) | ||
let lines = split(string(a:value), "\n") | ||
call writefile(lines, intermediate_file) | ||
call rename(intermediate_file, a:filename) | ||
endfunction | ||
|
||
" vim: ts=2 sw=2 et |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters