Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extensible hook for RStudio session initialization
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 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,32 @@ | ||
# | ||
# SessionRHooks.R | ||
# | ||
# Copyright (C) 2009-14 by RStudio, Inc. | ||
# | ||
# Unless you have received this program directly from RStudio pursuant | ||
# to the terms of a commercial license agreement with RStudio, then | ||
# this program is licensed to you under the terms of version 3 of the | ||
# GNU Affero General Public License. This program is distributed WITHOUT | ||
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
# | ||
# | ||
|
||
.rs.addFunction("invokeHook", function(hookName, ...) { | ||
# ensure that the list of hooks can be iterated over as a list | ||
hooks <- getHook(hookName) | ||
if (!is.list(hooks)) | ||
hooks <- list(hooks) | ||
|
||
# execute each attached hook with the given parameters | ||
for (fun in hooks) { | ||
if (is.character(fun)) | ||
fun <- get(fun) | ||
tryCatch(fun(...), | ||
error = function(e) { | ||
# ignore errors occurring during hook execution | ||
}) | ||
} | ||
}) | ||
|
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,46 @@ | ||
/* | ||
* SessionRHooks.cpp | ||
* | ||
* Copyright (C) 2009-14 by RStudio, Inc. | ||
* | ||
* Unless you have received this program directly from RStudio pursuant | ||
* to the terms of a commercial license agreement with RStudio, then | ||
* this program is licensed to you under the terms of version 3 of the | ||
* GNU Affero General Public License. This program is distributed WITHOUT | ||
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
* | ||
*/ | ||
|
||
#include <core/Exec.hpp> | ||
|
||
#include <r/RSexp.hpp> | ||
#include <r/RExec.hpp> | ||
|
||
#include <session/SessionModuleContext.hpp> | ||
|
||
#include <boost/bind.hpp> | ||
|
||
#include "SessionRHooks.hpp" | ||
|
||
namespace session { | ||
namespace modules { | ||
namespace rhooks { | ||
|
||
using namespace core; | ||
|
||
Error initialize() | ||
{ | ||
using boost::bind; | ||
using namespace module_context; | ||
ExecBlock initBlock; | ||
initBlock.addFunctions() | ||
(bind(sourceModuleRFile, "SessionRHooks.R")); | ||
return initBlock.execute(); | ||
} | ||
|
||
} // namespace rhooks | ||
} // namespace modules | ||
} // namespace session | ||
|
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,43 @@ | ||
/* | ||
* SessionRHooks.hpp | ||
* | ||
* Copyright (C) 2009-14 by RStudio, Inc. | ||
* | ||
* Unless you have received this program directly from RStudio pursuant | ||
* to the terms of a commercial license agreement with RStudio, then | ||
* this program is licensed to you under the terms of version 3 of the | ||
* GNU Affero General Public License. This program is distributed WITHOUT | ||
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
* | ||
*/ | ||
|
||
#ifndef SESSION_RHOOKS_HPP | ||
#define SESSION_RHOOKS_HPP | ||
|
||
#define kSessionInitHook "rstudio.sessionInit" | ||
|
||
namespace core { | ||
class Error; | ||
} | ||
|
||
namespace session { | ||
namespace modules { | ||
namespace rhooks { | ||
|
||
core::Error initialize(); | ||
|
||
// thin wrapper around hook invocation | ||
template<typename ParamType> | ||
core::Error invokeHook(const std::string& hookName, const ParamType& param) | ||
{ | ||
return r::exec::RFunction(".rs.invokeHook", hookName, param).call(); | ||
} | ||
|
||
} // namespace packrat | ||
} // namespace modules | ||
} // namespace rhooks | ||
|
||
#endif // SESSION_RHOOKS_HPP | ||
|