Getting a feature like this to work 100% of the time would require the use of TeX to figure the actual heading as it could easily depend on how the file is compiled, and what the packages and other files that come before the current line do to the sectioning commands.