Programmatically selecting a region

一曲冷凌霜 提交于 2019-12-09 08:48:16

问题


I want to perform the same action that one does by hitting C-Space + moving the arrow keys, but in elisp.

Failing to find the right function (if they just were logically grouped in namespaces or somehow tagged...). Which one is it?


回答1:


You can translate keystrokes to elisp using C-h k key.

You'll notice the elisp function for setting the mark set-mark-command, takes one non-optional argument. Emacs uses the special interactive function to allow elisp functions to be written naturally with arguments. This allows them to be generic and easy to reuse in other elisp programs, while still possible to invoke directly from a keystroke. It also has some of the C-u prefix logic built-in.

In the case of set-mark-command, its first function is (interactive "P"), which means the prefix is passed in as an argument when called from the keyboard. You can simulate this directly in elisp with:

(set-mark-command nil)

For example, to select the current line in elisp:

(defun my-select-current-line ()
  (interactive)
  (move-beginning-of-line nil)
  (set-mark-command nil)
  (move-end-of-line nil)
  (setq deactivate-mark nil))

Note you have to tell emacs to leave the mark active at the end or else the region won't remain highlighted (although the point and mark will be where you left them).




回答2:


You should use push-mark in emacs lisp code, as follows:

(defun mark-n (n)
  "Programmtically mark the next N lines"   
  (interactive "nNum lines to mark: ")
  (push-mark)  
  (next-line n))



回答3:


Just in case this additional info is of use to someone else, I've found the following:

  • As for programatic movement, see Moving Point
  • (point) and (mark) retrieve their respective positions, so one can do (set-mark (+ 5 (mark))), for instance.



回答4:


The region is the part of the buffer between point and mark.



来源:https://stackoverflow.com/questions/11689948/programmatically-selecting-a-region

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!