3

I have a buffer with this line of text:

This is a random test

I want to convert test in T.E.S.T., like this:

This is a random T.E.S.T.

So far, I came up with this function:

(defun mu-convert-to-acronym (string)
  "Convert the given STRING in an acronym.
An acronym must be uppercase and have each letter followed by a dot."
  (s-upcase
   (s-append "."
             (s-join "."
                     (delete ""
                             (s-split "" string))))))

My question is: how can I turn this function into a command I can call interactively?

I'd like the command to behave like capitalize-word and upcase-word, meaning I don't need to select the word I need to convert but I can just issue the command on the word next to point or, given a negative prefix, the one before point.

3

My question is: how can I turn this function into a command I can call interactively?

Use the special form interactive, also see manual (elisp) Defining Commands.

Here is a command named foo which uses your mu-convert-to-acronym.

(defun foo (arg)
  "Convert word at (or next to) point to its acronym with `mu-convert-to-acronym'.
With numerical argument ARG, convert the next ARG-1 words as well.

With negative argument, convert previous words."
  (interactive "p")
  ;; With negative argument, move back point firstly
  (when (< arg 0)
    ;; If point is in a word but not at the beginning of that word,
    ;; then move to the beginning fistly
    (let ((bounds (bounds-of-thing-at-point 'word)))
      (when (and bounds (not (= (car bounds) (point))))
        (forward-word -1)))
    (forward-word arg))
  (dotimes (_ (abs arg))
    ;; Adjust point in case point isn't on any word
    (forward-word)
    (backward-word)
    (let ((bounds (bounds-of-thing-at-point 'word)))
      (when bounds
        (let* ((beg (car bounds))
               (end (cdr bounds))
               (str (buffer-substring beg end)))
          (delete-region beg end)
          (goto-char beg)
          (insert (mu-convert-to-acronym str)))))))
  • Thank you, works good. I also tested it with numerical and negative argument and both worked. – Manuel Uberti Oct 29 '15 at 11:44
0

Here it is:

(require 's)

(defun mu--convert-word-to-acronym (string)
  "Convert the given STRING in an acronym.
An acronym must be uppercase and have each letter followed by a dot."
  (insert (s-upcase
           (s-append "."
                     (s-join "."
                             (delete ""
                                     (s-split "" string)))))))

(defun mu-convert-word-to-acronym (arg)
  "Applies mu--convert-word-to-acronym to a word at point. With positive arg applies to the next word, with negative arg applies to the previous word"
  (interactive "P")

  (cond
   ((equal arg '(4))
    (forward-word)
    (forward-char)
    )
   ((equal arg '(-4))
    (forward-word -1)
    (forward-char -1)
    )
   ((equal arg '-)
    (forward-word -1)
    (forward-char -1)
    )
   (t
    )
   )

  (let (word-bounds word-start word-end)

    (setq word-bounds (bounds-of-thing-at-point 'word))
    (setq word-start (car word-bounds))
    (setq word-end (cdr word-bounds))

    (setq word (buffer-substring-no-properties word-start word-end))

    (delete-region word-start word-end)

    (mu--convert-word-to-acronym word)

    )

  )

Call M-x mu-convert-word-to-acronym.

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.