From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Moving between headers in message-mode Date: Sat, 15 Jun 2013 14:58:14 +0200 Organization: Informatimago Message-ID: <87vc5ffs49.fsf@kuiper.lan.informatimago.com> References: <87wqpwgh03.fsf@VLAN-3434.student.uu.se> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1371305563 21986 80.91.229.3 (15 Jun 2013 14:12:43 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 15 Jun 2013 14:12:43 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jun 15 16:12:45 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1UnrDe-0004vG-6R for geh-help-gnu-emacs@m.gmane.org; Sat, 15 Jun 2013 16:12:42 +0200 Original-Received: from localhost ([::1]:37519 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UnrDd-0005BT-Or for geh-help-gnu-emacs@m.gmane.org; Sat, 15 Jun 2013 10:12:41 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 113 Original-X-Trace: individual.net Eez1wxNMnypAw7K9cFd0fgr0GWTMiov3xb7R91/UG3NySRkX7q Cancel-Lock: sha1:ZjgzZjBjYjJjNWNhYWE1MWI3ZGVmNjc0MTIwMzc4MTUxZGIzZjE5OQ== sha1:hrhdrVdfjjwTqI7W7RcfKd7iLU4= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:199300 X-Mailman-Approved-At: Sat, 15 Jun 2013 10:12:10 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:91565 Archived-At: Emanuel Berg writes: > Hello all. I wrote this tonight. The purpose is defined in the > docstring. It is supposed to be used in message-mode. I haven't > done much testing but it seems to work. If you have any > suggestions how to make it better, please tell me. Cheers. > > ;; separator > (setq mail-header-separator "---") > (set-face-attribute 'message-separator nil :foreground "black") > > (defun do-nothing () "Do nothing." () ) > (defun iterate-header (back) > "Unless BACK, go to the next header input field: > either on the same line as the present point, > or the next header below. > If the field has a string, put point to the string's right; > if the field is empty, put point so that typing will set the header, > without any prior cursor movement. > If you move from a header which has data, that data is expanded if > found to be an `abbrev-mode' abbreviation (possibly setup in > ~/.mailrc), without need for a whitespace. > If there are no headers left, `message-goto-body'; > if already in the body, goto the topmost header. > If BACK, do the same, only in reverse direction." > (interactive) > (let ((position (point))) > (goto-char (point-min)) > (search-forward mail-header-separator) > (let ((separator-pos (point)) > (search-fun) > (start-pos) > (end-pos) > (chars-forward-after-hit) > (maybe-beginning-of-line)) > (if back > (progn > (setq search-fun 'search-backward) > (setq start-pos separator-pos) > (setq end-pos (point-min)) > (setq chars-forward-after-hit 2) > (setq maybe-beginning-of-line 'beginning-of-line) ) > (progn > (setq search-fun 'search-forward) > (setq start-pos (point-min)) > (setq end-pos separator-pos) > (setq chars-forward-after-hit 1) > (setq maybe-beginning-of-line 'do-nothing) )) > (if (< position separator-pos) > (progn > (goto-char position) > (expand-abbrev) ) > (goto-char start-pos) ) > (funcall maybe-beginning-of-line) > (if (not (funcall search-fun ":" end-pos t)) > (message-goto-body) > (progn > (forward-char chars-forward-after-hit) > (end-of-line) ))))) I would factorize out the body: (require 'cl) (defun iterate-header (back) "Unless BACK, go to the next header input field: either on the same line as the present point, or the next header below. If the field has a string, put point to the string's right; if the field is empty, put point so that typing will set the header, without any prior cursor movement. If you move from a header which has data, that data is expanded if found to be an `abbrev-mode' abbreviation (possibly setup in ~/.mailrc), without need for a whitespace. If there are no headers left, `message-goto-body'; if already in the body, goto the topmost header. If BACK, do the same, only in reverse direction." (interactive) (let ((position (point))) (goto-char (point-min)) (search-forward mail-header-separator) (let ((separator-pos (point))) (flet ((goto-to-header (search-fun start-pos end-pos chars-forward-after-hit maybe-beginning-of-line) (if (< position separator-pos) (progn (goto-char position) (expand-abbrev)) (goto-char start-pos)) (funcall maybe-beginning-of-line) (if (funcall search-fun ":" end-pos t) (progn (forward-char chars-forward-after-hit) (end-of-line)) (message-goto-body)))) (if back (goto-to-header 'search-backward separator-pos (point-min) 2 'beginning-of-line) (goto-to-header 'search-forward (point-min) separator-pos 1 'do-nothing)))))) Don't put spaces between openning or closing parentheses (you may put a space between a closing and an opening one). In general use paredit to edit lisp code. In general, try to avoid setq, you can only write better code if you can avoid setq or setf. Of course, setf is often needed and useful too, you have to know when ;-) But a (let (var) (setq var expr)) is almost never a good expression. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. You know you've been lisping too long when you see a recent picture of George Lucas and think "Wait, I thought John McCarthy was dead!" -- Dalek_Baldwin