emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a simple convenience function


From: Dhruva Krishnamurthy
Subject: Re: a simple convenience function
Date: Mon, 15 Nov 2004 10:31:29 +0530

Hello,


On Mon, 15 Nov 2004 03:49:57 +0200, Paul Pogonyshev <address@hidden> wrote:
> 
> 
> 
> > Though I found the behaviour suggested by Paul much more mnemonic when
> > bound to the Home key. It is also found in some other editors.
> >
> > - Lennart
> >
> > PS: Maybe I should write it as
> >
> > (defun home-or-back-to-indentation()
> >   (interactive)
> >   (if (bolp) (back-to-indentation) (beginning-of-line)))
> >
> > (define-key global-map [home] 'home-or-back-to-indentation)
> 
> Perhaps, but this way you get it the way round: it will first jump
> to the beginning of line and then to the beginning of the text.
> 

I have a slightly fancy stuff which I use: On first hit on HOME, it
finds whether it is closer to beginning of line or begining of text
and jumps to the closer. Same with end of line. An experienced elisp
geek could clean up significantly... I am learning to walk/crawl in
the elisp ground.

;;-----------------------------------------------------------------------------
;; Intelligent HOME to toggle between 1st text and ZERO column
;;-----------------------------------------------------------------------------
(defun intelli-home()
  "User method to toggle between 1st text and ZERO column"
  (interactive)
  (catch 'ret
    (let ((pos (point)) (blpos (line-beginning-position)) (btpos 0))

      ;; If at begin of line
      (if (= blpos pos)
          (progn
            (beginning-of-line-text)
            (throw 'ret t)))

      (beginning-of-line-text)
      (setq btpos (point))

      ;; If at begin of text
      (if (= btpos pos)
          (progn
            (beginning-of-line)
            (throw 'ret t)))

      ;; Cursor after first text
      (if (> pos btpos)
          (throw 'ret t))

      ;; Cursor in between begin of line and first text
      ;; Closer to first text
      (if (> (- pos blpos) (- btpos pos))
          (throw 'ret t))

      ;; Closer to begin line
      (if (or (< (- pos blpos) (- btpos pos))
              (= (- pos blpos) (- btpos pos)))
          (progn
            (beginning-of-line)
            (throw 'ret t))))))

;;-----------------------------------------------------------------------------
;; Move to end of line text (as beginning-of-line-text)
;;-----------------------------------------------------------------------------
(defun end-of-line-text()
  "User method to simulate beginning-of-line-text for end of line"
  (interactive)
  (end-of-line)
  (while (looking-at "[ \t\n]+")
    (backward-char 1)))

;;-----------------------------------------------------------------------------
;; Intelligent END to toggle between Last text and Last column
;;-----------------------------------------------------------------------------
(defun intelli-end()
  "User method to toggle between Last text and Last column"
  (interactive)
  (catch 'ret
    (let ((pos (point)) (elpos (line-end-position)) (etpos 0))

      ;; If at begin of line
      (if (= elpos pos)
          (progn
            (end-of-line-text)
            (throw 'ret t)))

      (end-of-line)
      (setq etpos (point))

      ;; If at begin of text
      (if (= etpos pos)
          (progn
            (end-of-line)
            (throw 'ret t)))

      ;; Cursor after first text
      (if (< pos etpos)
          (throw 'ret t))

      ;; Cursor in between begin of line and first text
      ;; Closer to first text
      (if (< (- pos elpos) (- etpos pos))
          (throw 'ret t))

      ;; Closer to begin line
      (if (or (> (- pos elpos) (- etpos pos))
              (= (- pos elpos) (- etpos pos)))
          (progn
            (end-of-line)
            (throw 'ret t))))))

;; Key bindings
(global-set-key [end]  'intelli-end)
(global-set-key [home] 'intelli-home)

- dhruva
-- 
Proud FSF member: #1935
http://schemer.fateback.com/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]