emacs-devel
[Top][All Lists]
Advanced

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

Re: Patch to change just-one-space


From: Xah Lee
Subject: Re: Patch to change just-one-space
Date: Thu, 13 Aug 2009 16:12:52 -0700

i share this view.

though, in my experiment, i find that there are often situations where shrinking all whitespaces including EOL is not convenient. Here's a example:

-----------------------------
   numRep=0
   for couple in findreplace:
c    numRep += s.count(couple[0])
      outtext=s.replace(couple[0],couple[1])
      s=outtext
-----------------------------

where the c is the cursor point.
If you shrink all whitespaces including EOL, the the current line becomes joined with previous line.

what i find more ideal is this:

when the current line contain non-whitespace chars, then shrink just spaces and tabs, but if the current line does not contain non-whitespace chars (it's all white space), then shrink all all spaces, tab, EOL.

the code i've been using is this:

(defun shrink-whitespaces ()
  "Remove white spaces around cursor to just one or none.
If current line contains non-white space chars, then shrink any whitespace char surrounding cursor to just one space.
If current line does not contain non-white space chars, then remove blank lines to just one."
  (interactive)
  (let (
        cursor-point
        line-has-meat-p  ; current line contains non-white space chars
        spaceTabNeighbor-p
        whitespace-begin whitespace-end
        space-or-tab-begin space-or-tab-end
        line-begin-pos line-end-pos
        )
    (save-excursion
      ;; todo: might consider whitespace as defined by syntax table, and also consider whitespace chars in unicode if syntax table doesn't already considered it.
      (setq cursor-point (point))

      (setq spaceTabNeighbor-p (if (or (looking-at " \\|\t") (looking-back " \\|\t")) t nil) )
      (move-beginning-of-line 1) (setq line-begin-pos (point) )
      (move-end-of-line 1) (setq line-end-pos (point) )
      ;;       (re-search-backward "\n$") (setq line-begin-pos (point) )
      ;;       (re-search-forward "\n$") (setq line-end-pos (point) )
      (setq line-has-meat-p (if (< 0 (count-matches "[[:graph:]]" line-begin-pos line-end-pos)) t nil) )
      (goto-char cursor-point)

      (skip-chars-backward "\t ")
      (setq space-or-tab-begin (point))

      (skip-chars-backward "\t \n")
      (setq whitespace-begin (point))

      (goto-char cursor-point)      (skip-chars-forward "\t ")
      (setq space-or-tab-end (point))
      (skip-chars-forward "\t \n")
      (setq whitespace-end (point))
      )


    (if line-has-meat-p
        (progn 
          (when spaceTabNeighbor-p
            (delete-region space-or-tab-begin space-or-tab-end)
            (insert " "))
          )

      (progn
;;         (delete-region whitespace-begin whitespace-end)
;;         (insert "\n")
        (delete-blank-lines)
        )
      ;; todo: possibly code my own delete-blank-lines here for better efficiency, because delete-blank-lines seems complex.
      )
    )
  )

 Xah

On Thu, Aug 13, 2009 at 1:37 PM, Deniz Dogan <address@hidden> wrote:
Hi

I have long wanted to change the behavior of just-one-space to not
only delete spaces and tab characters, but newline characters as well.
Attached is the patch for this change.

I don't think that this is such a controversial modification and I
believe very few user macros will break. If I'm wrong, let me know!

--
Deniz Dogan



--
 Xah
http://xahlee.org/

reply via email to

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