Lebowtech Blog

Emacs Function to Create Docblocks

| Comments

Here is a elisp function that I wrote to quickly add doc-blocks to code.

It changes the selected region to the first line of the doc-block so you can change it immediately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(defun doc-block()
"Insert doc block"
(interactive)
(let 
    (
     (beg (point))
     (is-function (looking-at-p " *\\(\\(protected\\)\\|\\(public\\)\\|\\(protected\\)\\)? *function +\\([0-Z_-]+\\)(\\([^,)]+,?\\)*)" ))
     (is-class (looking-at-p "\s*class" ))
    )
    ( insert-string " /**
 * ")
    (if is-function (insert-string 
                     " new function
 *
 * @param string $param
 * @retrun null"))
   (if is-class (insert-string " new class
   * "))
      
    (insert-string "
 * @author ")
    (insert-string (user-real-login-name))
    (insert-string "
 */
")
    ;; re intent everything
    (forward-line 1)
    (indent-region beg (point))
    
    ;; select the description for easy edit
    (search-backward "/**")
    (forward-line 1)
    (search-forward "* ")
    (set-mark-command nil)
    (move-end-of-line nil)
    (setq deactivate-mark nil)
    )
)
(global-set-key "\C-cId" 'doc-block)

Comments