[comp.emacs] GNU 18.49 elisp question

jimb@drutx.ATT.COM (jim bryant) (03/03/88)

i'm trying to write a simple function that will set "fill-prefix"
before calling "fill-region". for example

#     this is the comment block
#     that i would like to fill
#     it probably would fit on 2 lines

how do i set "fill-prefix" to be the string between the beginning
of the line and the first non-whitespace character?

here's my function:

(defun fill-unix-comment-block ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (looking-at "#")
        (progn
           (skip-chars-forward " \t")
           (setq fill-prefix ???????what?????????)
           (fill-paragraph)
           (setq fill-prefix nil))
       (fill-paragraph))))

thanks!

jim bryant
ihnp4!drutx!jimb

Ram-Ashwin@cs.yale.edu (Ashwin Ram) (03/06/88)

In article <6878@drutx.ATT.COM>, jimb@drutx (jim bryant) writes:
> 
> i'm trying to write a simple function that will set "fill-prefix"
> before calling "fill-region". for example
> 
> #     this is the comment block
> #     that i would like to fill
> #     it probably would fit on 2 lines
> 
> how do i set "fill-prefix" to be the string between the beginning
> of the line and the first non-whitespace character?

Try:

(defun fill-unix-comment-paragraph ()
   "Fill current paragraph as a Unix comment paragraph."
   (interactive)
   (let ((fill-prefix (save-excursion
                         (beginning-of-line)
                         (buffer-substring (point)
                                           (progn (skip-chars-forward "# \t") (point))))))
      (fill-paragraph nil)))


-- Ashwin Ram --

ARPA:    Ram-Ashwin@cs.yale.edu
UUCP:    {decvax,ucbvax,harvard,cmcl2,...}!yale!Ram-Ashwin
BITNET:  Ram@yalecs

wolfgang@mgm.mit.edu (Wolfgang Rupprecht) (03/06/88)

In article <6878@drutx.ATT.COM> jimb@drutx.ATT.COM (jim bryant) writes:
>i'm trying to write a simple function that will set "fill-prefix"
>before calling "fill-region". for example
>#     this is the comment block
>#     that i would like to fill
>#     it probably would fit on 2 lines

Ok, I'll bite. Here is a slightly rearranged version of your code,
with the ????'s filled in.

#     this is the comment block that i would like to fill it probably
#     would fit on 2 lines

(defun fill-unix-comment-block ()
  "How about some snazzy documentation string here."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (looking-at "#")
        (progn
	  (forward-char 1)
	  (skip-chars-forward " \t")
	  (let ((fill-prefix))		;don't trash the real fill-prefix!
	    (set-fill-prefix) 
	    (fill-paragraph nil)))
      (fill-paragraph nil))))

; With a bit more work, you could make the fill-prefix detection much
; more general, and not hardwire the "^#[ \t]*" regexp into the code.

(defvar fill-comment-block-regexp "^a-zA-Z0-9"
  "*Regexp to identify the fill-prefix for the fill-comment-block function.")

(defun fill-comment-block ()
  "Fill a comment block. Uses fill-comment-block-regexp to identify the
fill-prefix. If none found, uses the default fill-prefix."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (looking-at (concat "[" fill-comment-block-regexp "]"))
        (progn
	  (skip-chars-forward fill-comment-block-regexp)
	  (let ((fill-prefix))
	    (set-fill-prefix) 
	    (fill-paragraph nil)))
      (fill-paragraph nil))))

---
Wolfgang Rupprecht	ARPA:  wolfgang@mgm.mit.edu (IP 18.82.0.114)
326 Commonwealth Ave.	UUCP:  mit-eddie!mgm.mit.edu!wolfgang
Boston, Ma. 02115	TEL:   (617) 267-4365