Merge branch 'worksheet-dedent-cell-text'

fixes #79
This commit is contained in:
Takafumi Arakaki 2012-10-09 14:23:52 +02:00
commit e04321603e
4 changed files with 66 additions and 1 deletions

View file

@ -1114,7 +1114,9 @@ This hook is run regardless the actual major mode used."
("Go to next cell" ein:worksheet-goto-next-input) ("Go to next cell" ein:worksheet-goto-next-input)
("Go to previous cell" ein:worksheet-goto-prev-input) ("Go to previous cell" ein:worksheet-goto-prev-input)
("Move cell up" ein:worksheet-move-cell-up) ("Move cell up" ein:worksheet-move-cell-up)
("Move cell down" ein:worksheet-move-cell-down)))) ("Move cell down" ein:worksheet-move-cell-down)
("Dedent text in CELL" ein:worksheet-dedent-cell-text)
)))
("Cell/Code" ("Cell/Code"
,@(ein:generate-menu ,@(ein:generate-menu
'(("Execute cell" ein:worksheet-execute-cell '(("Execute cell" ein:worksheet-execute-cell

View file

@ -278,6 +278,24 @@ Adapted from twittering-mode.el's `case-string'."
,@body))) ,@body)))
clauses))) clauses)))
;;; Text manipulation on buffer
(defun ein:find-leftmot-column (beg end)
"Return the leftmost column in region BEG to END."
(save-excursion
(let (mincol)
(goto-char beg)
(while (< (point) end)
(back-to-indentation)
(unless (= (point) (point-at-eol))
(setq mincol (if mincol
(min mincol (current-column))
(current-column))))
(unless (= (forward-line 1) 0)
(return-from ein:find-leftmot-column mincol)))
mincol)))
;;; Misc ;;; Misc

View file

@ -886,6 +886,17 @@ in the history."
(or (oref ws :dirty) (or (oref ws :dirty)
(buffer-modified-p buffer))))) (buffer-modified-p buffer)))))
;;; Utility commands
(defun ein:worksheet-dedent-cell-text (cell)
"Dedent text in CELL."
(interactive (list (ein:worksheet-get-current-cell)))
(let* ((beg (ein:cell-input-pos-min cell))
(end (ein:cell-input-pos-max cell)))
(indent-rigidly
beg end (- (ein:find-leftmot-column beg end)))))
;;; Auto-execution ;;; Auto-execution

View file

@ -82,6 +82,40 @@ def func():
")) "))
(should (equal (ein:trim-indent original) trimmed)))) (should (equal (ein:trim-indent original) trimmed))))
;;; Text manipulation on buffer
(ert-deftest ein:find-leftmot-column-simple-cases ()
(loop for (indent text) in
'(;; No indent
(0 "\
def f():
pass")
;; Indented python code
(4 "\
def f():
pass")
;; Deeper indent can come first
(4 "\
# indent = 8
# indent 4")
;; With empty lines
(4 "\
# indent = 8
# indent 4
")
)
do (with-temp-buffer
(insert text)
(should (= (ein:find-leftmot-column (point-min) (point-max))
indent)))))
;;; Misc
(ert-deftest ein:list-insert-after () (ert-deftest ein:list-insert-after ()
(should (equal (ein:list-insert-after '(a) 'a 'X) '(a X))) (should (equal (ein:list-insert-after '(a) 'a 'X) '(a X)))
(should (equal (ein:list-insert-after '(a b c) 'a 'X) '(a X b c))) (should (equal (ein:list-insert-after '(a b c) 'a 'X) '(a X b c)))