Of course it is not very hard to do this by hand, but when doing it more
often, it would be nice to a function for it. That is why I wrote
dcbl-make-heading. I uses dcbl-heading-values, which is defined as:
(defvar dcbl-heading-values (list :char "-"
:do-upcase nil
:heading-length 50
:max-text-length 45)
"Values to use for creating heading;")
char defines the char to be used. It can also be a string if you want.
For example when you use:
(setq dcbl-heading-values
(list :char "0123456789"
:do-upcase nil
:heading-length 50
:max-text-length 45))
you get:
01234567890123456789012345678901234567890123456789
This is just a heading
01234567890123456789012345678901234567890123456789
With do-upcase you define if the heading should be in uppercase.
With heading-length you define how long the header should be.
And with max-text-length you define what the maximum size of the text is
allowed to be.
You can also do something like:
(dcbl-make-heading '(:char "#" :do-upcase 1 :heading-length 30 :max-text-length 30))
this would give:
##############################
THIS IS JUST A HEADING
##############################
The function itself is:
(defun dcbl-make-heading (&optional these-values)
"Make a heading of the current line"
(interactive)
(save-excursion
(let ((begin-of-line)
(do-upcase)
(end-of-line)
(heading-length)
(heading-string)
(heading-values)
(i)
(line-length)
(max-text-length))
(if these-values
(setq heading-values these-values)
(setq heading-values dcbl-heading-values))
(setq do-upcase (getf heading-values :do-upcase)
heading-length (getf heading-values :heading-length)
max-text-length (getf heading-values :max-text-length))
(if (> max-text-length heading-length)
(message "max-text-length is bigger as heading-length (%s, %s)"
max-text-length
heading-length)
(setq heading-string (dcbl-multiply-string
(getf heading-values :char) heading-length))
(move-end-of-line nil)
(setq end-of-line (point))
(move-beginning-of-line nil)
(setq begin-of-line (point))
(setq line-length (- end-of-line begin-of-line))
(if (> line-length max-text-length)
(message "Line is to long to make a heading (%s max %s)"
line-length
max-text-length)
(when do-upcase
(upcase-region begin-of-line end-of-line))
(insert heading-string "\n")
(move-end-of-line nil)
(insert "\n" heading-string))))))
I have a question about this. Normally I would write something like:
(when (> max-text-length heading-length)
(message "max-text-length is bigger as heading-length (%s, %s)"
max-text-length
heading-length)
(return))
Is something like this possible in Emacs Lisp?
To create the heading string I use dcbl-multiply-string. This is defined
as:
(defun dcbl-multiply-string (input-string string-length)
"Create a string from input-string with the given lenght;"
(let ((output-string input-string))
(while (> string-length (length output-string))
(setq output-string (concat output-string output-string)))
(substring output-string 0 string-length)))
I hope it is useful to someone and if there are questions or remarks: do
not hesitate to communicate them.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof