2013-07-12 18 views
2

我想設置一個命令,在兩個§字符之間放置行的內容而不移動點(不包括包含§的行)。在Emacs中定義一個複製段命令

這裏是我當前的嘗試

(defun copy-section() 
    "Copy current section, that is lines between two §." 
    (interactive) 
    (save-excursion 
     (when (not (search-backward-regexp "§" nil t)) 
     (goto-char (point-min))) 
     (forward-line 1) 
     (when (not (search-forward-regexp "§" nil t)) 
     (goto-char (point-max))) 
     (move-beginning-of-line nil) 
     (kill-ring-save (mark) (point)))) 

它運作良好,但對周圍的標記移動是壞的風格讓我覺得taht有一個更好的方式來達到同樣的結果文檔中的言論。 將位置保存到變量中(我不知道如何去做)可以實現更清晰的功能。

上述代碼的一部分來自ergoemacs

回答

3

不「正規」的形式需要,因爲只有一個字符是尋找

(defun copy-section() 
    "Copy current section, that is lines between two §." 
    (interactive) 
    (save-excursion 
    (let* ((start (and (search-backward "§" nil t) 
         (forward-line 1) 
         (point))) 
      (end (progn (and start (search-forward "§" nil t)) 
         (forward-line -1) 
         (end-of-line) 
         (point)))) 
     (and start end (kill-new (buffer-substring-no-properties start end)))))) 
3

這個版本保存開始和臨時局部變量的部分的結束,並沒有在所有使用該商標:

(defun copy-section() 
    "Copy current page as defined by form feed characters." 
    (interactive) 
    (let (start end) 
    (save-excursion 
     (when (not (search-backward-regexp "§" nil t)) 
     (goto-char (point-min))) 
     (forward-line 1) 
     (setq start (point)) 
     (when (not (search-forward-regexp "§" nil t)) 
     (goto-char (point-max))) 
     (move-beginning-of-line nil) 
     (setq end (point)) 
     (kill-ring-save start end))))