如何让 Emacs ess 将查询字符串(引号内)识别为代码?
背景
我有一个函数dbquery
,它简化了从R 中查询MySQL 数据库的过程。
dbquery <- function(querystring) {
dvr <- dbDriver("MySQL")
con <- dbConnect(dvr, group = "databasename")
q <- dbSendQuery(con, querystring)
data <- fetch(q, n = -1)
return(data)
}
因此我可以发送:
dbquery(querystring = "select field_1, field_2, field_3
from table_a join table_b on this = that
join table_c on that = something
where field_4 in (1,2,3);"
但是,变量querystring
必须包含在引号内。这使得 Emacs ESS 不会像在 SQL 模式下那样很好地缩进我的查询,甚至不会像没有引号而只是在 ESS-R 模式下那样很好地缩进。
问题
是否可以让 ESS 来做这件事?也许通过编写函数以便它接受不带引号的查询(并在函数内添加引号),或者可能向 .emacs 或 ess.el 添加一些内容?
Background
I have a function dbquery
that simplifies the process of querying a MySQL database from within R.
dbquery <- function(querystring) {
dvr <- dbDriver("MySQL")
con <- dbConnect(dvr, group = "databasename")
q <- dbSendQuery(con, querystring)
data <- fetch(q, n = -1)
return(data)
}
Thus I can send:
dbquery(querystring = "select field_1, field_2, field_3
from table_a join table_b on this = that
join table_c on that = something
where field_4 in (1,2,3);"
However, the variable querystring
must be contained within quotes. This makes it so that Emacs ESS will not nicely indent my queries like it would if it were in SQL mode - or even like it does if there are no quotes but just in ESS-R mode.
Question
Is it possible to get ESS to do this? Perhaps by writing the function so that it will accept the query without a quote (and add the quotes within the function), or perhaps adding something to .emacs or ess.el?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你在 MMM 模式 中想要什么。顾名思义:MultiMajorMode 模式允许在同一缓冲区的不同区域上有多种模式。
我建议您查看 http://www.emacswiki.org/emacs/HtmlModeDeluxe 中的示例因为它们可能会让您了解如何在您的情况下执行此操作(您可能希望在 sql 周围的代码中添加一些注释,以便 MMM 可以找到 sql 代码)。
我猜你必须做这样的事情(未经测试):
但是,这可能有点矫枉过正,除非你经常在 R 代码中进行复杂的 sql 查询。
I think what you want in MMM Mode. As his name suggests: MultiMajorMode Mode allows to have multiple modes on different regions of the same buffer.
I recommend that you checkout the examples in http://www.emacswiki.org/emacs/HtmlModeDeluxe as they will probably give you an idea how to do it in your case (you might want to add some comment in your code around the sql so that MMM can find the sql code).
You would have to do something like this I guess (untested):
However, this might be overkill, unless it happens a lot that you have complex sql queries in the R code.
我不知道有什么方法可以做到这一点。你似乎在问,“我可以让 Emacs 同时处于两种模式吗?(即 ESS 和 SQL)” 我认为答案是“不”,但我希望有人出现并向我们展示一个切肉刀黑客来证明我错误的!
I don't know of any way to do this. It seems like you're asking, "can I make Emacs be in two modes simultaneously? (i.e. ESS and SQL)" I think the answer is "no" but I hope that someone comes along and shows us a cleaver hack that proves me wrong!
一个简单的替代方法是使用粘贴,每行一个单独的字符串:
也许有点笨拙,但在实践中它是有效的。
A simple alternative approach would be to use paste, with each line a separate string:
Perhaps a bit clunky, but it works in practice.