Guest

Leslie

By: a guest on Dec 28th, 2010  |  syntax: Lisp  |  size: 3.03 KB  |  hits: 56  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1.  
  2. ;; Selecione ANTES a região com as linhas a serem ordenadas
  3. ;; muito provavelmente os registros do cartão de crédito ;-)
  4.  
  5. ;; A função Ordena os registros do seguinte tipo com base na data colocada em @mm.dd
  6.  
  7. ;;--------------------------------------------------------------------------
  8. 2011-01-17      -0.60   ccred11_01, bapka| @12.21 Sorvete de Morango na Bapka
  9. 2011-01-17      -60.00  ccred11_01, carro| @12.21 Gasolina para o Carro
  10. 2011-01-17      -6.30   ccred11_01| @12.21 Carpetes e Velcro de Proteção para Árvore na Tecidos Avenida
  11. 2011-01-17      -10.50  ccred11_01, estac| @12.22 Estacionamento Almoço
  12. 2011-01-17      -36.00  ccred11_01, presente| @12.21 Camisetas
  13. ;;---------------------------------------------------------------------------
  14.  
  15.  
  16. (defun i-ordena-ccredit ()
  17. "Ordena os registros de data do cartão de crédito."
  18. (interactive)
  19. ;; discover point-min and point-max from the selected region, or use the next word
  20. (let (pos1 pos2 bds)
  21.   (if (and transient-mark-mode
  22.            mark-active)
  23.       (setq start (region-beginning) end (region-end))
  24.     (progn
  25.       (setq bds (bounds-of-thing-at-point 'symbol))
  26.       (setq start (car bds) end (cdr bds))
  27.           ))
  28. ; idiom for string replacement in region
  29. ;; restrict the atuation of the next commands to the selected region
  30. (with-output-to-temp-buffer "*registros*"
  31.   (save-restriction
  32.         (narrow-to-region start end)
  33.         (goto-char (point-min))
  34.         (while (< (point) (point-max))
  35. ;; reads the buffer line
  36.           (setq linha (buffer-substring (line-beginning-position) (line-end-position)))
  37. ;; matches the date field
  38.           (string-match "\\(@[0-9][0-9].[0-9][0-9]\\)" linha)
  39. ;; puts the date matched in the data variable
  40.           (setq data-compra (match-string 0 linha))
  41. ;; prints the line with the buy date's first
  42.           (princ (format "%s %s\n" data-compra linha))
  43.           (forward-line 1)
  44.           )
  45.         ;; repeat for other string pairs
  46.         )
  47.   )
  48. ;; creates a new buffer called *registros to manipulate the strings
  49. (let ((oldbuf (current-buffer)))
  50.   (set-buffer "*registros*")
  51. ;; let we modify the buffer
  52.   (setq buffer-read-only nil)
  53. ;; as the buffer already has the lines in the right format, we sort them all
  54. ;; @12.21 2011-01-17    -36.00  ccred11_01, presente| @12.21 Camisetas
  55.   (sort-lines nil (point-min) (point-max))
  56. ;; we need to define the region that will be killed (the @mm.dd part at the front of the line)
  57. ;; that was inserted before
  58.   (setq minr (point-min))
  59.   (goto-char (point-max))
  60.   (forward-line -1)
  61.   (re-search-forward "\\(@[0-9][0-9].[0-9][0-9] \\)")
  62.   (setq maxr (point))
  63. ;; kill the rectangle
  64.   (kill-rectangle minr maxr)
  65. ;; now we kill the whole region to put it into the kill ring
  66.   (kill-region (point-min) (point-max))
  67. ;; kill the temporary buffer
  68.   (kill-buffer (current-buffer))
  69.   (set-buffer oldbuf))
  70. ;; we'll kill the old region to put the new sorted lines
  71. (kill-region (region-beginning) (region-end))
  72. (goto-char (region-end))
  73. (forward-line)
  74. (insert (format "# ordenado\n"))
  75. ;; as we have killed the selected region, the ordered lines are in the 2nd position of the kill ring
  76. (yank 2)
  77.   )
  78. )