(provide 'fill-columns)

(defun fill-columns-eol (n)
"Like fill columns, but goto eol first, and also do the right thing with the
ending tab.  The deal is that fill-columns doesn't put in the trailing tab, for
reasons having to do with how it's used in the diet functions.  All this is a
monster hack, but f it for now."
    (interactive "nTo column: ")
    (end-of-line)
    (fill-columns n)
    (backward-char 1)
    (insert "	")
)

(defun fill-to-column-with-one-trailing-tab (n)
"Fill to column N with spaces and one tab.  I.e., starting at the current
column, insert one tab, preceded by the minimum number of space characters to
get to column N."
    (interactive "nTo column: ")
    (setq c (current-column))

    ; Compute the number of spaces needed as n-p-8, noting that it will be
    ; negative if we're beyond the target column.
    (setq s (- n c 8))

    ; Do the space insertion, which will be none if s <= 0.
    (insert-char (string-to-char " ") s)

    ; Insert the tab (well we should, but things are fucked up right now).
    (insert-string "\t")

)


(defun fill-columns (n)
"NOTE: This function is currently WRONG in that it does not insert the
trailing tab.  This is due to it being used improperly in .diet-log-mode.el.
So as not to fuck things up any further there, this function is left as is.
The functions fill-to-column-with-one-trailing-tab and fill-colums-eol are
currently correct versions.

Fill to column N with spaces and one tab.  I.e., starting at the current
column, insert one tab, preceded by the minimum number of space characters to
get to column N."

    (interactive "nTo column: ")
    (setq c (current-column))

    ; Compute the number of spaces needed as n-p-8, noting that it will be
    ; negative if we're beyond the target column.
    (setq s (- n c 8))

    ; Do the space insertion, which will be none if s <= 0.
    (insert-char (string-to-char " ") s)

    ; Insert the tab (well we should, but things are fucked up right now).
    ; (insert-string "\t")

    ; End up with point after the tab.
    (forward-char 1)
)

(defun fill-columns-at-end-if-necessary (n)
"Call (fill-columns n) if n > end-of-line char pos, otherwise do nothing"
    (interactive "nTo column: ")
    (end-of-line)
    (if (> n (current-column))
	(fill-columns n)
    )
)

(defun fill-columns-up-to-next-tab (n)
"Like fill-columns, q.v., but insert just the spaces without the tab, assuming the next tab is eight spaces in front of the column where we want to be.  If tne next char after the current point is not a tab, then search forward for one.  Do nothing if there's no tab at or after the current point."
    (interactive "nTo column: ")
    (setq c (current-column))

    ; If the next char is not a tab, search forward for the next one, then move
    ; back in front of it.
    (if (not (= (following-char) ?\t))
      (progn
	(setq c (search-forward "\t" nil t))
        (backward-char 1)
      )
    )

    ; Call the little buddy if we're at a tab, otherwise do nothing.
    (if c (fill-columns n))
)

    
(defvar fill-to-column 24)
(defun fill-columns-curried-with-global ()
"Call fill-to-column fill-to-column using the current value of the global var fill-to-column."
    (interactive)
    (fill-columns fill-to-column)
)

(defun fill-columns-up-to-next-tab-curried-with-global ()
"Call fill-to-column-at-next-tab fill-to-column using the current value of the global var fill-to-column."
    (interactive)
    (fill-columns-up-to-next-tab fill-to-column)
)