简体   繁体   English

如何让Emacs ess将查询字符串(在引号内)识别为代码?

[英]How can I get Emacs ess to recognize a query string (within quotes) as code?

Background 背景

I have a function dbquery that simplifies the process of querying a MySQL database from within R. 我有一个函数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)
}    

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. 但是,变量querystring必须包含在引号内。 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. 这使得Emacs ESS不会像在SQL模式下那样很好地缩进我的查询 - 或者甚至像没有引号但只是在ESS-R模式下那样。

Question

Is it possible to get ESS to do this? 是否有可能让ESS这样做? 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? 也许通过编写函数使它接受没有引号的查询(并在函数中添加引号),或者可能在.emacs或ess.el中添加一些东西?

I think what you want in MMM Mode . 我认为你想要的MMM模式 As his name suggests: MultiMajorMode Mode allows to have multiple modes on different regions of the same buffer. 顾名思义:MultiMajorMode Mode允许在同一缓冲区的不同区域拥有多个模式。

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). 我建议您查看http://www.emacswiki.org/emacs/HtmlModeDeluxe中的示例,因为它们可能会让您知道如何在您的情况下执行此操作(您可能希望在代码中添加一些注释在sql周围这样MMM就可以找到sql代码了。

You would have to do something like this I guess (untested): 你必须做这样的事情我猜(未经测试):

(require 'mmm-mode)
(mmm-add-group
     'sql-in-ess
     '(
             (sql-query
                    :submode sql-mode
                    :face WHATEVERYOUWANT
                    :front "#SQL_QUERY>"
                    :back "#<SQL_QUERY"))
(add-to-list 'mmm-mode-ext-classes-alist '(ess-mode nil sql-in-ess))

However, this might be overkill, unless it happens a lot that you have complex sql queries in the R code. 但是,这可能是过度的,除非你在R代码中有很多复杂的SQL查询。

A simple alternative approach would be to use paste, with each line a separate string: 一种简单的替代方法是使用paste,每行都有一个单独的字符串:

dbquery(querystring = paste("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);"))

Perhaps a bit clunky, but it works in practice. 也许有点笨重,但它在实践中有效。

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? (ie 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! 看起来你似乎在问,“我可以让Emacs同时处于两种模式吗?(即ESS和SQL)”我认为答案是“不”但我希望有人出现并向我们展示一个能够证明我的切肉刀黑客错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM