我正在Emacs Lisp中创建一个函数,如果没有活动区域,它将大声读取整个缓冲区。否则,它会读取活动区域。
(defun speak-my-text () "Speaks text in buffer."
(interactive)
(if (equal mark-active t)
(
(kill-ring-save (point-min) (point-max))
(start-process-shell-command "speakbuffvar" nil
"bash -c \"killall espeak;xsel --clipboard|espeak -s 290\"")
)
(
(kill-ring-save (region-beginning) (region-end))
(start-process-shell-command "speakbuffvar" nil
"bash -c \"killall espeak;xsel --clipboard|espeak -s 290\"")
)))
(global-set-key (kbd "C-z") 'speak-my-text)
我遇到了else子句的第一行问题:
(kill-ring-save (region-beginning) (region-end))
当我定义函数,绑定它并运行时,我得到“无效函数”并指向该行。我正在运行Linux Mint。
答案 0 :(得分:3)
使用C-h f progn
按顺序评估多个表达式。
您目前正在尝试调用名为(kill-ring-save (region-beginning) (region-end))
的函数并将参数传递给(start-process-shell-command ...)
当然,Emacs告诉你(kill-ring-save (region-beginning) (region-end))
不是函数的名称。
答案 1 :(得分:1)
除了phils'回答:
在这种情况下, mark-active
可能不可靠
如果一个地区没有延伸,也是非零的。
函数region-active-p
可用,但取决于a
活跃的transient-mark-mode
,可能与大多数人无关
如果它有用且足够BTW。
以下是如何检查更大的有效区域的示例
零和超越transient-mark-mode
问题:
(defun ar-region-active-p ()
"Check for and existing region of some extend.
Don't mix up that question with transient-mark-mode "
(interactive)
(and (ignore-errors (region-beginning))(region-end) (< (region-beginning)(region-end))))
基于此,您的命令可能会略有不同:
(defun my-command-on-region ()
"If a region of some extend exists, use on it.
Otherwise use whole buffer.
Doesn't require `transient-mark-mode' being active"
(interactive)
(let ((beg (or (and (ignore-errors (region-beginning))(region-end) (< (region-beginning)(region-end))(region-beginning))
(point-min)))
(end (or (and (ignore-errors (region-beginning))(region-end) (< (region-beginning)(region-end))(region-end))
(point-max))))
(DO-SOMETHING beg end)))