0

I want to write a function which equally add "-" character on each sides of bold text. The future line should be no longer then fill-column

Input:

- <1900-01-01 Mon> *XX century* <1999-12-31 Fri>

Output:

- <1900-01-01 Mon> --------------- *XX century* --------------- <1999-12-31 Fri>
1

I'd do this with a combination of re-search-forward and replace-match on subgroups (in this case the spaces just before and after *. You can simplify the regular expression if you don't want match the timestamps.

(defun add-dashes-to-line ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (when (re-search-forward (rx "-" space
                                 "<" (1+ (not (in ">"))) ">"
                                 (group space)
                                 "*" (1+ (not (in "*"))) "*"
                                 (group space)
                                 "<" (1+ (not (in ">"))) ">")
                             (point-at-eol) t 1)
      (let* ((line-length (- fill-column (- (point-at-eol) (point-at-bol))))
            (replacement (concat " "
                                 (make-string (/ line-length 2) ?-)
                                 " ")))
        (replace-match replacement  nil t nil 1)
        (replace-match replacement  nil t nil 2)))))
0

Disclaimer: I am very much an elisp beginner. I'm sure there's a better way than this, but took the question as a bit of a personal challenge.

Here's my crack at it. I don't think the 'if' statement is working... It's meant to add an extra "-" if there's an odd number needed. I've run out of time however so can't fix it.

(defun my-pack-line ()
  (interactive)
  ; Determine the blank spaces between end of line and fill-column value
  (setq to-fill (- fill-column (progn (end-of-line) (current-column))))

  ; Put point to right of first timestamp
  (move-to-column 18)

  ; Insert half of dashes here
  (setq n-dashes (/ to-fill 2))
  (insert " ")
  (setq xx 1)
  (while (< xx n-dashes)
    (insert "-")
    (setq xx (1+ xx)))

  ; Go to start of second timestamp
  (end-of-line)
  (setq xx 1)
  (while (< xx 18)
    (left-char)
    (setq xx (1+ xx)))

  ; Insert remaining dashes
  (if (oddp to-fill)
      (setq xx 1)
      (setq xx 0))
  (insert " ")
  (setq xx 1)
  (while (< xx n-dashes)
    (insert "-")
    (setq xx (1+ xx))))
  • James thank you from your answer but I went other way :) – Hellseher Aug 7 '18 at 23:36
0

For some reason rpluim's answer not working all the time and I've noticed I need some calculation for replacement variable (see the function below).

Current working function with satisfies my needs is:

(defun equal-re-hl-line ()
  (interactive)
  (let* ((text (thing-at-point 'line t))
         (line-ln (- fill-column (- (point-at-eol) (point-at-bol))))
         (hzl-left (concat " " (make-string (/ (- line-ln 3) 2) ?-) " *"))
         (hzl-right (concat "* " (make-string (/ (- line-ln 3) 2) ?-) " ")))
    (delete-region (point-at-bol) (point-at-eol))
    (insert (with-temp-buffer
              (insert (replace-regexp-in-string "\s\\*" hzl-left (replace-regexp-in-string "\\*\s" hzl-right text)))
              (buffer-string)))))

TODO

  • how to add optional regex marker not just "*"
  • function adding an extra new line, how to avoid it?
  • Could you elaborate on what was going wrong? – rpluim Aug 20 '18 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.