burtzlaff.de

Generate code citations with line numbers

The function below creates a code citation in the following style:

sdp_request.c:1115   pos = req->buf + sizeof(sdp_pdu_hdr_t);
sdp_request.c:1116   req->len += sizeof( sdp_pdu_hdr_t);
sdp_request.c:1117 
sdp_request.c:1118   *((uint32_t*) pos) = htonl( handle );
sdp_request.c:1119   pos      += sizeof( uint32_t );
sdp_request.c:1120   req->len += sizeof( uint32_t );
sdp_request.c:1121   
sdp_request.c:1122   *((uint16_t*) pos) = htons( max_rsp_size );
sdp_request.c:1123   pos      += sizeof( uint16_t );
sdp_request.c:1124   req->len += sizeof( uint16_t );
(defun ab-code-citation (beg end)
  "Generate code citation from marked region."
  (interactive (if (use-region-p)
                   (list (region-beginning) (region-end))
                 (list nil nil)))
  (cond  ((and beg end)
          (kill-new (save-excursion
                      (let ((ab-tmp-output-string "")
                            (ab-tmp-file-name (file-name-nondirectory (buffer-file-name))))
                        (goto-char beg)
                        (while (< (point) end) 
                          (setq ab-tmp-output-string
                                (concat ab-tmp-output-string ab-tmp-file-name
                                        ":"
                                        (number-to-string (line-number-at-pos))
                                        " "
                                        (thing-at-point 'line t)  ))
                          (forward-line))
                        ab-tmp-output-string)))
          (message "Code citation added to kill ring"))
         (t (message "No region marked"))))