"Vim script for editing webpage content stored in a datatabase "By Chris Gregerson (chris@phototour.minneapolis.mn.us) "Posted 2002-08-20 " "This script must be modified to suit your own database design. "Specifically, your MySQL database, username, and password. "Also, check the SQL select and update statements; this script "assumes a table named 'pages' with fields for 'id','name', and 'content'. " "======USAGE====== "to edit a page from within Vim, type ":Edit pagename "(where pagename matches a 'name' field in the 'pages' table) "This opens the page, using a temporary file of '/tmp/pagename.txt' "to re-save, simply type: ':Save' "the webpage name will be figured out based upon the filename, "so /tmp/index.txt will be saved in the pages table as 'index' "Your version of Vim must have perl support perl <connect('DBI:mysql:database_name', 'username', 'password', { RaiseError => 1 }) || die $dbh->errstr; $string=$dbh->selectrow_array("select content from pages where name=\"$page\""); $dbh->disconnect; if($string) { open(PAGE, ">/tmp/$page.txt") || die "cannot write to /tmp/$page.txt:$!"; print PAGE $string; close PAGE; VIM::DoCommand('let $success=1'); } else { VIM::DoCommand('let $success=0'); } } sub SavePage { $page = VIM::Eval('$page'); $lines = $curbuf->Count(); @l = $curbuf->Get(0 .. $lines); $string = join("\n", @l); #This initiates the pages database connection use DBI; $dbh = DBI->connect('DBI:mysql:db', 'user', 'password', { RaiseError => 1 }); $sth=$dbh->prepare("update pages set content=? where name=?"); $result = $sth->execute($string,$page); $dbh->disconnect; VIM::Msg("$page has been saved(result:$result)") } EOF "Read an info page and display in a buffer function! Edit(t) let $page=a:t if $page =~ "^[a-z]" perl &OpenPage; if $success == 1 e /tmp/$page.txt 0 else echo "page was empty" endif else echo "there is no page named ".$page endif endfunc command! -n=1 Edit call Edit('') "cabbrev edit Edit command! Save call Save() "cabbrev save Save "Save a file function! Save() update let $file = expand("%") let $page = substitute($file, "/tmp/\\([^/]\\+\\)\.txt", '\1','') perl &SavePage endfunc