script to set variables based on user input

Discussion of Emacs Lisp

script to set variables based on user input

Postby rubing » Tue Dec 29, 2009 6:42 pm

I am using the mew email reader in emacs. The reader accesses your pop accounts based on lisp variables you set in its configuration file. I am trying to write an interactive function which will set these variables based on user input (e.g. type 'y' for yahoo account, 'm' for msn, etc...) When I run the function below it ends up executing the else statement that 'the email account does not exist'.

Just from doing some rudimentary debugging i think the problem is that the user-entered character is being interpreted as an integer. I tried a function to convert an integer back to a character, but the function was void (int-char).

Code: Select all
(defun popmail (account)

  "check the specified popmail account for mail"

  (interactive "cWhich account should I check? ")

  (if (char-equal account "y")
      ((setq mew-smtp-server "smtp.mail.yahoo.com")
       (setq mew-pop-user "rubinglen")
       (setq mew-pop-server "pop.mail.yahoo.com")
       (setq mew-pop-auth 'pass)
       (setq mew-pop-port nil)
       (setq mew-pop-ssl t)
       (setq mew-smtp-ssl t)
       (setq mew-smtp-port 465)
       (setq mew-pop-ssl-port 995)
       (setq mew-ssl-verify-level 0))

    (if (char-equal account "g") ; else part is another if
   ((setq mew-smtp-server "smtp.gawab.com")
    (setq mew-pop-user "glenrubin@gawab.com")
    (setq mew-pop-server "pop.gawab.com")
    (setq mew-pop-auth 'pass)
    (setq mew-pop-ssl t)
    (setq mew-pop-ssl-port 995)
    (setq mew-pop-port 110)
    (setq mew-smtp-port 25)
    (setq mew-smtp-ssl nil)
    (setq mew-ssl-verify-level nil))

      (message "email account does not exist"))));else the user entered the wrong letter
rubing
 
Posts: 9
Joined: Sun Nov 22, 2009 11:44 am

Re: script to set variables based on user input

Postby Ramarren » Tue Dec 29, 2009 11:23 pm

rubing wrote:Just from doing some rudimentary debugging i think the problem is that the user-entered character is being interpreted as an integer. I tried a function to convert an integer back to a character, but the function was void (int-char).


In Emacs Lisp characters are integers. Which is unfortunate for various reasons, but in your case the problem is that CHAR-EQUAL takes two characters, and you are giving it a string "y" which is not a character, but a string. The literal syntax for characters in Emacs Lisp would be ?y. See: Basic Char Syntax in the Emacs Lisp Reference Manual. Also, you probably want COND or possibly CASE (from cl-macs extension) rather than chained IFs.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: script to set variables based on user input

Postby rubing » Wed Dec 30, 2009 8:43 am

Great, thanks for the advice! made the changes and works fine. here's the code now in my .mew.el file in case anyone wants to use it for themselves:

;; Optional setup (Read Mail menu for Emacs 21):
(if (boundp 'read-mail-command)
(setq read-mail-command 'mew))

;; Optional setup (e.g. C-xm for sending a message):
(autoload 'mew-user-agent-compose "mew" nil t)
(if (boundp 'mail-user-agent)
(setq mail-user-agent 'mew-user-agent))
(if (fboundp 'define-mail-user-agent)
(define-mail-user-agent
'mew-user-agent
'mew-user-agent-compose
'mew-draft-send-message
'mew-draft-kill
'mew-send-hook))

(defun popmail (account)
"check the specified popmail account for mail"
(interactive "cWhich account should I check? ")

(cond ((char-equal account ?y)
(setq mew-smtp-server "smtp.mail.yahoo.com")
(setq mew-pop-user "rubinglen")
(setq mew-pop-server "pop.mail.yahoo.com")
(setq mew-pop-auth 'pass)
(setq mew-pop-port nil)
(setq mew-pop-ssl t)
(setq mew-smtp-ssl t)
(setq mew-smtp-port 465)
(setq mew-pop-ssl-port 995)
(setq mew-ssl-verify-level 0)
(mew-summary-retrieve))
((char-equal account ?g)
(setq mew-smtp-server "smtp.gawab.com")
(setq mew-pop-user "glenrubin@gawab.com")
(setq mew-pop-server "pop.gawab.com")
(setq mew-pop-auth 'pass)
(setq mew-pop-ssl nil)
(setq mew-pop-ssl-port nil)
(setq mew-pop-port 110)
(setq mew-smtp-port 25)
(setq mew-smtp-ssl nil)
(setq mew-ssl-verify-level nil)
(mew-summary-retrieve))
((char-equal account ?q)
(message "mail check aborted"))
(message "invalid mail account"))) ; next add recursive call to fcn + 'try again'
rubing
 
Posts: 9
Joined: Sun Nov 22, 2009 11:44 am


Return to Emacs Lisp

Who is online

Users browsing this forum: No registered users and 0 guests

cron