emacs - 标记区域并插入前缀

标签 emacs elisp

我最近从 vi 切换到 emacs,现在我正在将最重要的宏移植到 emacs。我最需要的是能够使用字符串为标记的文本区域添加前缀,包括页眉和页脚:

原文:

line 1
line 2
line 3
line 4

标记第二行和第三行后,我希望 emacs 询问我一个数字,比如 002,然后执行以下操作,最好记住我的选择:

line 1
*#002# Start:
*$line 2
*$line 3
*#002# End.
line 4

到目前为止,我已成功使用以下代码插入开始和结束标记:

(defun comment-region (start end)
  "Insert COBOL comments."
  (interactive "r")
  (save-excursion 
    (goto-char end) (insert "*#xxx# End.\n")
    (goto-char start) (insert "*#xxx# Start:\n")
    ))

但是,我似乎不知道如何在该区域中的所有行上添加 *$ 前缀,以及如何让 emacs 询问我一个字符串。

有什么想法吗?

最佳答案

我已经通过动态生成一个解决此类问题的方法 代码片段 yasnippet最近。

这是代码:

(require 'yasnippet)
(defun cobol-comment-region (beg end)
  "comment a region as cobol (lines 2,3 commented)

line 1
*#002# Start:
*$line 2
*$line 3
*#002# End.
line 4
"
  (interactive "*r")
  (setq beg (progn
             (goto-char beg)
             (point-at-bol 1))
       end (progn
             (goto-char end)
             (if (bolp)
                 (point)
               (forward-line 1)
               (if (bolp)
                   (point)
                 (insert "\n")
                 (point)))))
  (let* ((str (replace-regexp-in-string
               "^" "*$" (buffer-substring-no-properties beg (1- end))))
         (template (concat "*#${1:002}# Start:\n"
                           str
                           "\n*#$1# End.\n"))
         (yas-indent-line 'fixed))
    (delete-region beg end)
    (yas-expand-snippet template)))

包含视频,什么???

这是一个video它的实际应用:

关于emacs - 标记区域并插入前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15545480/

相关文章:

json - Elisp - 来自 API 响应的 json-read-from-string

emacs - 如何在 Emacs 中使用通用键绑定(bind)弹出本地和全局标记

css - 如何在 emacs 中嵌套 css 选择器?

emacs - 如何在emacs中找到TAGS文件中的文件

emacs - 在 emacs 中匹配左大括号和右大括号

Emacs 区域高亮

emacs - 重新定义 C-x C-e 以评估自定义语言表达式

Emacs -- dired-mode 测试以确定远程服务器名称/地址

emacs - Lisp:既未声明也未绑定(bind) CHAR

emacs 错误无法保存自定义