मुझे यकीन है कि यह छोटे और बेहतर बनाया जा सकता है, लेकिन अब के लिए मैं निम्नलिखित है: ruby-goto-containing-block-start
और ruby-flip-containing-block-type
:
(defun ruby-get-containing-block()
(let ((pos (point))
(block nil))
(save-match-data
(save-excursion
(catch 'break
;; If in the middle of or at end of do, go back until at start
(while (and (not (looking-at "do"))
(string-equal (word-at-point) "do"))
(backward-char 1))
;; Keep searching for the containing block (i.e. the block that begins
;; before our point, and ends after it)
(while (not block)
(if (looking-at "do\\|{")
(let ((start (point)))
(ruby-forward-sexp)
(if (> (point) pos)
(setq block (cons start (point)))
(goto-char start))))
(if (not (search-backward-regexp "do\\|{" (point-min) t))
(throw 'break nil))))))
block))
(defun ruby-goto-containing-block-start()
(interactive)
(let ((block (ruby-get-containing-block)))
(if block
(goto-char (car block)))))
(defun ruby-flip-containing-block-type()
(interactive)
(save-excursion
(let ((block (ruby-get-containing-block)))
(goto-char (car block))
(save-match-data
(let ((strings (if (looking-at "do")
(cons
(if (= 3 (count-lines (car block) (cdr block)))
"do\\(*|[^|]+|\\)? *\n *\\(.*?\\) *\n *end"
"do\\(*|[^|]+|\\)? *\\(\\(.*\n?\\)+\\) *end")
"{\\1 \\2 }")
(cons
"{\\(*|[^|]+|\\)? *\\(\\(.*\n?\\)+\\) *}"
(if (= 1 (count-lines (car block) (cdr block)))
"do\\1\n\\2\nend"
"do\\1\\2end")))))
(when (re-search-forward (car strings) (cdr block) t)
(replace-match (cdr strings) t)
(delete-trailing-whitespace (match-beginning 0) (match-end 0))
(indent-region (match-beginning 0) (match-end 0))))))))
दो कार्य चाबियाँ करने के लिए बाध्य होने के लिए कर रहे हैं।
या तो कमांड किसी ब्लॉक के अंदर कहीं भी काम करता है, और उम्मीद है कि वे छोड़े जाने वाले ब्लॉक को छोड़ सकते हैं - हालांकि यदि आप एक छोटे ब्लॉक प्रारूप में परिवर्तित हो रहे हैं तो यह कोई मुद्दा नहीं होना चाहिए।
ruby-flip-containing-block-type
तीन पंक्तियों को ध्वस्त करता है .. एकल रेखा {} और इसके विपरीत अंत ब्लॉक। यदि ब्लॉक बिल्कुल 3 लाइनें और 1 लाइन लंबी नहीं हैं, तो उन्हें अकेले छोड़ देना चाहिए।
मैं अब अपने रूबी सेटअप पर इसका उपयोग कर रहा हूं, इसलिए मैं सुधार की सराहना करता हूं।
अच्छा। ध्यान देने योग्य एकमात्र चीज यह है कि Emacs में आपको सी-क्यू सी-जे को नई लाइनों से मेल खाने के बजाय \ n के बजाय Regex में रखना होगा :) – Gabriel
हा! धन्यवाद आदमी (मैं एक vim उपयोगकर्ता हूँ इसलिए मुझे उस सामान के बारे में निश्चित नहीं है ... मैं वाक्यविन्यास देखता हूं और अपना जवाब संशोधित करता हूं)। – Ziggy
क्या यह काम बार-बार काम करता है? मैं emacs के regex समर्थन से अपरिचित हूँ। – jtbandes