开发者

get and set line content in emacs buffer programmatically

开发者 https://www.devze.com 2023-01-08 22:49 出处:网络
I would like to translate the following function from vim script to emacs elisp (I use it to set the email recipients when writing emails).

I would like to translate the following function from vim script to emacs elisp (I use it to set the email recipients when writing emails).

My question is mainly how to get and set line contents in emacs, because with quick googling I could not find this out (probably I just did not know the right terms to google for, "getline" and "setline" in any case did show any results).

function! G_set_to()  
    let address = system('my-address-script')  "shell command to choose addresses  
    let line = getline (2)  
    if strlen(line) == 4  
        call setline(2,  line . address)  
    else  
        call setline(2,  line . "; " . address)  
    endif  
endfunction  

So开发者_StackOverflow中文版rry if the answer is obvious, I am am completely new to emacs and don't even know how to use its in-built help system.

Cheers Arian


Give this a shot, obviously customizing the variable G-address-script to suit your needs.

(require 'sendmail)
(defvar G-address-script "echo hi@google.com")
(defun G-set-to ()
  "execute script in G-address-script and populate the To: line with the results"
  (interactive)
  (save-excursion
    (mail-to)
    (unless (= (current-column) 4)
      (insert "; "))
    (insert (shell-command-to-string G-address-script))
    (when (looking-at "^$") ; when shell command has extra newline, delete it
      (delete-backward-char 1))))

As far as using the help, there's a reasonable introduction to Emacs lisp here. But the quick intro is:

  • C-h i (that's control-h then i to see the info pages, there is one for Emacs and one for Emacs Lisp.
  • M-x apropos searches for terms
  • C-h f gives you help on a function
  • C-h v gives you help on a variable
  • In the *scratch* buffer, C-j will evaluate the expression right before the point (cursor)
  • M-x edebug-defun when the point is in a emacs lisp function will turn the debugger on, and the next time a function is run you'll step through it (M-x eval-defun in the function will turn the debugger off)
  • M-x eldoc-mode will turn on automatic documentation for emacs lisp functions and variables as you type them, read more here.


I took Trey's code and changed a few things and now it seems to work:

(defvar G-address-script "goobook_dmenu_with_cache")  
  (defun G-set-to ()  
    "execute script in G-address-script and populate the To: line with the results"  
    (interactive)  
    (save-excursion  
  (goto-line 2)  
  (re-search-forward "$")  
  (unless (= (current-column) 4)  
    (insert "; "))  
  (insert (shell-command-to-string G-address-script))))

Some remaining questions:

  1. The bit where Trey's code checks if the string returned from G-address-script is empty doesn't work at the moment, what do I have to change?
  2. Is there really no function to get the content of a specified line as a string?
  3. Is there a better/more elegant way to do this? The vim script solution looks so much simpler, surely I am missing something.

Anyway, I'll mark this as solved in a day or so if there are no more answers as Trey solution basically does what I wanted. Thank you Trey!

0

精彩评论

暂无评论...
验证码 换一张
取 消