" Vimball Archiver by Charles E. Campbell, Jr., Ph.D. UseVimball finish doc/sunset.txt [[[1 240 *sunset.txt* Automatically set background on local sunrise/sunset time. FRONT MATTER *sunset-front-matter* Maintainer: Alastair Touw License: Distributed under the same terms as Vim. See |license|. Version: 1.1.0 Last Change: 2012 Oct 28 INTRODUCTION AND USAGE *sunset* *sunset-introduction-usage* Sunset automatically sets 'background' when the sun rises and sets, and also when you start Vim. When the sun is up, or rises, it'll 'set background=light'. When the sun is down, or sets, it'll 'set background=dark'. So as not to interrupt you, Sunset waits for four seconds (on the |CursorHold| event) after you've pressed a key or left insert mode before changing the background. If you change your background during the day or night, it'll respect that. You must set some options in your `.vimrc` for Sunset to work, so please read on to |sunset-configuration| for details. CONTRIBUTIONS *sunset-contributions* If you feel that Sunset can be improved, pull requests and issues are appreciated and humbly requested, on Github at: http://github.com/amdt/sunset CONTENTS *sunset-contents* i. Front Matter . . . . . . . . . . . . . . . . .|sunset-front-matter| ii. Introduction & Usage . . . . . . . . . |sunset-introduction-usage| iii. Contributions . . . . . . . . . . . . . . . |sunset-contributions| iv. Contents . . . . . . . . . . . . . . . . . . . . |sunset-contents| 1. Installation . . . . . . . . . . . . . . . . |sunset-installation| a) With a Plugin Manager . . |sunset-installation-plugin-manager| b) Using Vimball . . . . . . . . . |sunset-installation-vimball| c) Manually . . . . . . . . . . . . . |sunset-installation-manual| 2. Requirements . . . . . . . . . . . . . . . . |sunset-requirements| 3. Configuration . . . . . . . . . . . . . . . |sunset-configuration| a) 'g:loaded_sunset' . . . . . . . . . . . . . |'loaded_sunset'| b) 'g:sunset_latitude' & . . . . . . . . . . |'sunset_latitude'| 'g:sunset_longitude' . . . . . . . . . . . |'sunset_longitude'| c) 'g:sunset_utc_offset' . . . . . . . . . |'sunset_utc_offset'| d) 'g:sunset_callback()' . . . . . . . . . |'sunset_callback()'| 4. A Reminder on Privacy . . . . . . . . . . . . . . |sunset-privacy| 5. Version History . . . . . . . . . . . . . |sunset-version-history| 6. Known Issues . . . . . . . . . . . . . . . . |sunset-known-issues| 7. License . . . . . . . . . . . . . . . . . . . . . |sunset-license| 8. Credits . . . . . . . . . . . . . . . . . . . . . |sunset-credits| INSTALLATION *sunset-installation* With a Plugin Manager: *sunset-installation-plugin-manager* Vundle: ~ If you don't already have a preferred manager, I recommend installing Vundle (http://github.com/gmarik/vundle). Once done, add the declaration for Sunset to your `.vimrc`: > Bundle 'amdt/sunset' < And install: > :BundleInstall < Pathogen: ~ If you're using Pathogen, simply extract the archive to `$HOME/.vim/bundle` or better yet, clone the Git repository. In a UNIX shell, for example: > cd ~/.vim/bundle git clone git://github.com/amdt/sunset.git < Using Vimball: *sunset-installation-vimball* Open the Vimball with Vim. For example, from a UNIX shell you might run: > vim sunset.vba < Once loaded in Vim, run the following two commands: > :so % :q < Manually: *sunset-installation-manual* Copy the files in the included zip archive into your 'runtimepath'. The directory tree for Sunset is as follows: /doc sunset.txt /plugin sunset.vim README.markdown Place `sunset.txt` under `$HOME/.vim/doc` and `sunset.vim` under `$HOME/.vim/plugin` where `$HOME` is the location of your `.vim` directory. You can find this by typing '|:echo| |$HOME|'. You can safely discard of `README.markdown`. REQUIREMENTS *sunset-requirements* * Sunset requires Vim 7.3. * Vim compiled with |+float| support. Use |:version| to check if this feature is available in your build. * Requires a system with |strftime()|, with the following format options: - %j returns the current day of the year. - %H returns the current hour of the day in 24-hour time. - %M returns the current minute of the hour. * A |colorscheme| with both light and dark variants, such as Solarized (http://github.com/altercation/vim-colors-solarized) or Hemisu (http://github.com/noahfrederick/Hemisu). CONFIGURATION *sunset-configuration* 'g:loaded_sunset' *'loaded_sunset'* Set to a non-0 value to disable Sunset, for example: > let g:loaded_sunset = 1 < 'g:sunset_latitude' & 'g:sunset_longitude' *'sunset_latitude'* *'sunset_longitude'* Note: If you push your dotfiles to Github, please see |sunset-privacy|. The latitude and longitude of your location in decimal. Values North and East must be positive values, those South and West must be negative. London, for example, lies at 51 degrees, 30 minutes North; and 7 minutes West. In decimal, this is 51.5 degrees North, 0.1167 degrees West. If you lived in London, you might set these options as follows: > let g:sunset_latitude = 51.5 let g:sunset_longitude = -0.1167 < If you lived in Tokyo (35 degrees, 40 minutes and 12 seconds North; 139 degrees, 46 minutes and 12 seconds East), you might set these options as follows: > let g:sunset_latitude = 35.67 let g:sunset_longitude = 139.8 < Note: Don't forget, negative values South and West. 'g:sunset_utc_offset' *'sunset_utc_offset'* The difference in hours between your timezone and Coordinated Universal Time. For example: > let g:sunset_utc_offset = 0 " London let g:sunset_utc_offset = 1 " London (British Summer Time) let g:sunset_utc_offset = 9 " Tokyo < Note: Sunset does not handle any daylight savings civil times. 'g:sunset_callback()' *'sunset_callback()'* This function is called every time Sunset checks the time. You can use it to make Sunset do more than set 'background'. In the following example, the theme used by Powerline* is changed: > let g:sunset_last_background = '' function! g:sunset_callback() if g:sunset_last_background != &bg && \ exists(':PowerlineReloadColorscheme') let g:sunset_last_background = &bg let g:Powerline_colorscheme = \ &background == 'light' ? 'solarizedLight' : 'solarizedDark' PowerlineReloadColorscheme endif endfunction < * Powerline is 'the ultimate vim statusline utility'. It's very cool; you should check it out: https://github.com/Lokaltog/vim-powerline A REMINDER ON PRIVACY *sunset-privacy* For those of us who publish our dotfiles on Github etc., please take this as a gentle reminder that out of habit you might be about to publish your whereabouts to the greater public. If this concerns you, using the location of your nearest large city might suffice; Sunset will be plenty accurate enough. VERSION HISTORY *sunset-version-history* 1.1.0 (Sun Oct 28, 2012) ~ - Added |'sunset_callback()'|. Courtesy of Github user 'delphinus35' (http://github.com/amdt/sunset/pull/1) 1.0.3 (Sat Oct 20, 2012) ~ - Improved requirements checking. - Rewrote documentation. 1.0.1 & 1.0.2 (Thu Oct 18, 2012) ~ - Corrected typos in documentation. 1.0.0 (Thu Oct 18, 2012) ~ - Initial release. KNOWN ISSUES *sunset-known-issues* For known issues (and to report your own), please see the issue tracker on Github: http://github.com/amdt/sunset/issues LICENSE *sunset-license* Sunset is distributed under the same terms as Vim itself. See |license| for details. CREDITS *sunset-credits* Sunset uses an algorithm for finding the local sunrise and sunset times published in the Almanac for Computers, 1990, by the Nautical Almanac Office of the United States Naval Observatory, as detailed here: http://williams.best.vwh.net/sunrise_sunset_algorithm.htm Special thanks: ~ * Gueorgui Tcherednitchenko --- Whose tweet (http://twitter.com/gueorgui/statuses/250765514975113216) inspired the development of this plugin. * Nick Rogers --- Whose testing helped define Sunset's requirements. * * * vim:tw=78:ft=help:norl: plugin/sunset.vim [[[1 210 " " sunset.vim - Automatically set background on local sunrise/sunset time. " " Maintainer: Alastair Touw " Website: http://github.com/amdt/sunset " License: Distributed under the same terms as Vim. See ':help license'. " Version: 1.1.0 " Last Change: 2012 Oct 28 " Usage: See 'doc/sunset.txt' or ':help sunset' if installed. if exists("g:loaded_sunset") finish endif let g:loaded_sunset = 1 if v:version < 703 echoerr "Requires Vim 7.3." finish endif if !has("float") echoerr "Requires Vim be compiled with +float support." finish endif if !exists("*strftime") echoerr "Requires a system with strftime()" finish endif let s:sunset_required_options = \ ["g:sunset_latitude", "g:sunset_longitude", "g:sunset_utc_offset"] for option in s:sunset_required_options if exists(option) call filter(s:sunset_required_options, 'v:val != option') endif endfor if !empty(s:sunset_required_options) for option in s:sunset_required_options echoerr printf("%s missing! See ':help %s' for more details.", option, option) endfor finish endif let s:save_cpo = &cpo set cpo&vim let s:PI = 3.14159265359 let s:ZENITH = 90 let s:SUNRISE = 1 let s:SUNSET = 0 lockvar s:PI s:ZENITH s:SUNRISE s:SUNSET let s:DAYTIME_CHECKED = 0 let s:NIGHTTIME_CHECKED = 0 function s:hours_and_minutes_to_minutes(hours, minutes) return (a:hours * 60) + a:minutes endfunction function s:daytimep(current_time) if a:current_time <= s:SUNRISE_TIME || a:current_time >= s:SUNSET_TIME return 0 else return 1 endif endfunction function s:calculate(sunrisep) " This algorithm for finding the local sunrise and sunset times published " in the Almanac for Computers, 1990, by the Nautical Almanac Office of the " United States Naval Observatory, as detailed " here: http://williams.best.vwh.net/sunrise_sunset_algorithm.htm function! l:degrees_to_radians(degrees) return (s:PI / 180) * a:degrees endfunction function! l:radians_to_degrees(radians) return (180 / s:PI) * a:radians endfunction function! s:minutes_from_decimal(number) return float2nr(60.0 / 100 * (a:number - floor(a:number)) * 100) endfunction " 1. First calculate the day of the year let l:day_of_year = strftime("%j") " 2. Convert the longitude to hour value and calculate an approximate time let l:longitude_hour = g:sunset_longitude / 15 let l:n = a:sunrisep ? 6 : 18 let l:approximate_time = l:day_of_year + ((l:n - l:longitude_hour) / 24) " 3. Calculate the Sun's mean anomaly let l:mean_anomaly = (0.9856 * l:approximate_time) - 3.289 " 4. Calculate the Sun's true longitude let l:true_longitude = \ l:mean_anomaly + \ (1.916 * sin(l:degrees_to_radians(l:mean_anomaly))) + \ (0.020 * sin(l:degrees_to_radians(2) * l:degrees_to_radians(l:mean_anomaly))) + \ 282.634 if l:true_longitude < 0 let l:true_longitude = l:true_longitude + 360 elseif l:true_longitude >= 360 let l:true_longitude = l:true_longitude - 360 endif " 5a. Calculate the Sun's right ascension let l:right_ascension = \ l:radians_to_degrees(atan(0.91764 * tan(l:degrees_to_radians(l:true_longitude)))) if l:right_ascension < 0 let l:right_ascension = l:right_ascension + 360 elseif l:right_ascension >= 360 let l:right_ascension = l:right_ascension - 360 endif " 5b. Right ascension value needs to be in the same quadrant as " l:true_longitude let l:true_longitude_quadrant = (floor(l:true_longitude / 90)) * 90 let l:right_ascension_quadrant = (floor(l:right_ascension / 90)) * 90 let l:right_ascension = l:right_ascension + \ (l:true_longitude_quadrant - l:right_ascension_quadrant) " 5c. Right ascension value needs to be converted into hours let l:right_ascension = l:right_ascension / 15 " 6. Calculate the Sun's declination let l:sin_declination = \ 0.39782 * \ sin(l:degrees_to_radians(l:true_longitude)) let l:cos_declination = \ cos(asin(l:degrees_to_radians(l:sin_declination))) " 7a. Calculate the Sun's local hour angle let l:cos_hour_angle = \ (cos(l:degrees_to_radians(s:ZENITH)) - (l:sin_declination * sin(l:degrees_to_radians(g:sunset_latitude)))) / \ (l:cos_declination * cos(l:degrees_to_radians(g:sunset_latitude))) if l:cos_hour_angle > 1 " the sun never rises on this location (on the specified date) elseif l:cos_hour_angle < -1 " the sun never sets on this location (on the specified date) endif " 7b. Finish calculating H and convert into hours if a:sunrisep let l:hour = 360 - l:radians_to_degrees(acos(l:cos_hour_angle)) else let l:hour = l:radians_to_degrees(acos(l:cos_hour_angle)) endif let l:hour = l:hour / 15 " 8. Calculate local mean time of rising/setting let l:mean_time = \ l:hour + \ l:right_ascension - \ (0.06571 * l:approximate_time) - \ 6.622 " 9. Adjust back to UTC let l:universal_time = l:mean_time - l:longitude_hour " 10. Convert l:universal_time value to local time zone of " latitude/longitude let l:local_time = l:universal_time + g:sunset_utc_offset if l:local_time < 0 let l:local_time = l:local_time + 24 elseif l:local_time >= 24 let l:local_time = l:local_time - 24 endif return s:hours_and_minutes_to_minutes(float2nr(l:local_time), \ s:minutes_from_decimal(l:local_time)) endfunction function s:sunset() if s:daytimep(s:hours_and_minutes_to_minutes(strftime("%H"), strftime("%M"))) if s:DAYTIME_CHECKED != 1 set background=light let s:DAYTIME_CHECKED = 1 let s:NIGHTTIME_CHECKED = 0 endif else if s:NIGHTTIME_CHECKED != 1 set background=dark let s:NIGHTTIME_CHECKED = 1 let s:DAYTIME_CHECKED = 0 endif endif if exists('*g:sunset_callback') call g:sunset_callback() endif endfunction let s:SUNRISE_TIME = s:calculate(s:SUNRISE) let s:SUNSET_TIME = s:calculate(s:SUNSET) call s:sunset() autocmd CursorHold * nested call s:sunset() let &cpo = s:save_cpo unlet s:save_cpo