From 8d6e5ad9dcf62d6e3ce656b63e3a0f52d9e049b0 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Mon, 23 Nov 2015 16:43:01 +0000 Subject: [PATCH] [Fix #252] Improve roxygen prefix support --- lisp/ess-custom.el | 3 +++ lisp/ess-mode.el | 26 ++++++++++++++++++++++++-- lisp/ess-r-d.el | 8 +++++++- lisp/ess-roxy.el | 21 +++++++++++++++------ test/literate/roxy.R | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 9 deletions(-) diff --git a/lisp/ess-custom.el b/lisp/ess-custom.el index 0a6c1195f..a87c3c16c 100644 --- a/lisp/ess-custom.el +++ b/lisp/ess-custom.el @@ -1533,6 +1533,9 @@ to sweave the current noweb file and latex the result." :type '(choice (const :tag "Off" nil) (const :tag "On" t))) +(defvar ess-roxy-insert-prefix-on-newline t + "When non-nil, `ess-newline-and-indent' will make sure the new +line starts with the roxy prefix.") ; System variables diff --git a/lisp/ess-mode.el b/lisp/ess-mode.el index defc06d40..a81d3d14b 100644 --- a/lisp/ess-mode.el +++ b/lisp/ess-mode.el @@ -129,6 +129,14 @@ map) "Keymap for `ess-mode'.") +;; Redefine substituted commands +(substitute-key-definition 'newline-and-indent + 'ess-newline-and-indent + ess-mode-map global-map) + +(substitute-key-definition 'indent-new-comment-line + 'ess-indent-new-comment-line + ess-mode-map global-map) (defvar ess-eval-map (let ((map (make-sparse-keymap))) @@ -379,8 +387,8 @@ Furthermore, \\[ess-set-style] command enables you to set up predefined ess-mode indentation style. At present, predefined style are `BSD', `GNU', `K&R', `C++', `CLB' (quoted from C language style)." (setq alist (or alist - (buffer-local-value 'ess-local-customize-alist (current-buffer)) - (error "Customise alist is not specified, nor ess-local-customize-alist is set."))) + (buffer-local-value 'ess-local-customize-alist (current-buffer)) + (error "Customise alist is not specified, nor ess-local-customize-alist is set."))) (unless is-derived (kill-all-local-variables)) ;; NOTICE THIS! *** NOTICE THIS! *** NOTICE THIS! *** (ess-setq-vars-local alist) @@ -709,6 +717,20 @@ current function." (define-obsolete-function-alias 'ess-narrow-to-defun 'ess-narrow-to-defun-or-para "15.09") +(defun ess-newline-and-indent () + (interactive) + (cond ((string= ess-dialect "R") + (ess-roxy-newline-and-indent)) + (t + (newline-and-indent)))) + +(defun ess-indent-new-comment-line () + (interactive) + (cond ((string= ess-dialect "R") + (ess-roxy-indent-new-comment-line)) + (t + (indent-new-comment-line)))) + ;;*;; Loading files diff --git a/lisp/ess-r-d.el b/lisp/ess-r-d.el index e66ca3d1c..56a78079f 100644 --- a/lisp/ess-r-d.el +++ b/lisp/ess-r-d.el @@ -474,10 +474,16 @@ Executed in process buffer." (ad-activate 'fill-paragraph) (ad-activate 'move-beginning-of-line) (ad-activate 'back-to-indentation) - (ad-activate 'newline-and-indent) (ad-activate 'ess-eval-line-and-step) (if ess-roxy-hide-show-p (ad-activate 'ess-indent-command)) + (substitute-key-definition 'newline-and-indent + 'ess-newline-and-indent + ess-mode-map global-map) + + (substitute-key-definition 'indent-new-comment-line + 'ess-indent-new-comment-line + ess-mode-map global-map) (run-hooks 'R-mode-hook)) diff --git a/lisp/ess-roxy.el b/lisp/ess-roxy.el index 0d636313f..ce7f2766a 100644 --- a/lisp/ess-roxy.el +++ b/lisp/ess-roxy.el @@ -850,15 +850,23 @@ list of strings." (goto-char (match-end 0))) ad-do-it)) -(defadvice newline-and-indent (around ess-roxy-newline) +(defun ess-roxy-indent-new-comment-line () + (if (not (ess-roxy-entry-p)) + (indent-new-comment-line) + (ess-roxy-indent-on-newline))) + +(defun ess-roxy-newline-and-indent () + (if (or (not (ess-roxy-entry-p)) + (not ess-roxy-insert-prefix-on-newline)) + (newline-and-indent) + (ess-roxy-indent-on-newline))) + +(defun ess-roxy-indent-on-newline () "Insert a newline in a roxygen field." (cond - ;; Not in roxy entry; do nothing - ((not (ess-roxy-entry-p)) - ad-do-it) ;; Point at beginning of first line of entry; do nothing ((= (point) (ess-roxy-beg-of-entry)) - ad-do-it) + (newline-and-indent)) ;; Otherwise: skip over roxy comment string if necessary and then ;; newline and then inset new roxy comment string (t @@ -867,9 +875,10 @@ list of strings." (ess-back-to-roxy) (point)))) (goto-char (max (point) point-after-roxy-string))) - ad-do-it + (newline-and-indent) (insert (concat (ess-roxy-guess-str t) " "))))) + (provide 'ess-roxy) ;;; ess-roxy.el ends here diff --git a/test/literate/roxy.R b/test/literate/roxy.R index 4b633b521..f13356742 100644 --- a/test/literate/roxy.R +++ b/test/literate/roxy.R @@ -45,3 +45,36 @@ NULL ##' aliqua. Ut enim ad minim veniam, quis nostrud exercitation ##' ullamco laboris nisi ut aliquip ex ea commodo consequat. NULL + + +### 2 ---------------------------------------------------------------- + +##' ¶ + +##! (global-set-key (kbd "RET") 'newline-and-indent) +##! (substitute-key-definition 'newline-and-indent +##! 'ess-newline-and-indent +##! ess-mode-map global-map) +##! "RET" + +##' +##' ¶ + +##> "RET" + +##' +##' +##' ¶ + +##> (setq ess-roxy-insert-prefix-on-newline nil) +##> "RET" + +##' +##' +##' +¶ + +##! "M-j" + +##' +##' ¶