Enquanto isso, escrevi um código para implementar o hack que o @glucas mencionou. Talvez seja adequado apenas ao propósito de classificar cabeçalhos em C, mas espero que alguém ache útil.
(defun replace-char-after (character-number replacement)
"Replaces char in the buffer after the `character-number' with `replacement'"
(save-excursion
(goto-char character-number)
(delete-char 1)
(insert-char replacement)))
(defun replace-delimiters (old-closing-char new-opening-char new-closing-char opening-point end-point)
"Replaces delimiters between `opening-point' and the
`end-point'. Note, that the `opening-point' should point to the
opening symbol, thus the function seeks only the closing"
(block replace-delimiters
(let ((closing-point opening-point))
(setq closing-point (+ 1 opening-point))
(while (< closing-point end-point)
(if (eq (char-after closing-point) ?\n) ;;no closing delimiter
(progn
(print "Err: no closing delimiter")
(return-from replace-delimiters nil))
(when (eq (char-after closing-point) old-closing-char)
(progn
(replace-char-after opening-point new-opening-char);;opening delimiter
(replace-char-after closing-point new-closing-char);;closing delimiter
(return-from replace-delimiters (+ 1 closing-point)))))
(setq closing-point (+ closing-point 1))))))
(defun swap-<-and-quote-includes (beg end)
"Swaps in the text between `beg' and `end' the matching «<» and
«>» character to the \" quote, and vice versa. Mainly used
before sorting to swap the order of these characters, next
after the sort to restore the text."
(block swap-<-and-quote-includes
(let ((curr-point beg))
(while (< curr-point end)
(setq curr-point (+ curr-point 1))
;;first check «"»
(if (eq (char-after curr-point) ?\")
(progn
(setq curr-point (replace-delimiters ?\" ?< ?> curr-point end))
(if (eq curr-point nil)
(return-from swap-<-and-quote-includes t)))
;;else if «<»
(if (eq (char-after curr-point) ?<)
(progn
(setq curr-point (replace-delimiters ?\> ?\" ?\" curr-point end))
(if (eq curr-point nil)
(return-from swap-<-and-quote-includes t)))))))))
A função swap-<-and-quote-includes
transforma cada texto como <foo>
para "foo"
, e cada "foo"
para <foo>
dentro do intervalo dado beg , end .
E aqui está o código para encontrar e classificar cabeçalhos:
(defun sort-lines-nocase (reverse beg end)
(let ((sort-fold-case t))
(sort-lines reverse beg end)))
(defun c-sort-includes ()
"Sorts #include statements"
(interactive)
(save-excursion
(let (beg end orig-content sorted-content)
(goto-char (point-min))
(while (and (not (looking-at "#include "));;look for includes, if no then
(eq (forward-line 1) 0) ;;go one line down (if not EOF).
))
(setq beg (point))
(while (and (looking-at "#include ")
(eq (forward-line 1) 0)));;to not hang cuz of EOF
(setq end (point))
(setq orig-content (buffer-substring-no-properties beg end))
(setq sorted-content (with-temp-buffer
(insert orig-content)
(swap-<-and-quote-includes (point-min) (point-max)) ;;swap characters < and > in includes
(sort-lines-nocase (point-min) (point-max)) ;;sort
(swap-<-and-quote-includes (point-min) (point-max)) ;;swap the characters back
(buffer-string)))
(when (not (string= orig-content sorted-content))
(kill-region beg end)
(insert sorted-content))
)))
A função c-sort-includes
procura o primeiro parágrafo de «#include» s e o classifica. Eu o adicionei a before-save-hook
um código para executá-lo apenas nos modos C e C ++. Contras conhecidas: α) somente o primeiro parágrafo com inclusões será classificado. É porque a pesquisa até o final de um arquivo pode ser cara - por exemplo, no meu trabalho, recentemente encontrei um .c
arquivo - você pode imaginar ?! - 16.000 linhas! A solução correta preferiria ser um modo secundário, que rastrearia onde residem os blocos de cabeçalho no arquivo. β) No Emacs mais antigo, a partir de 2015, a função poderia travar - foi um bug que foi corrigido posteriormente.
sort-subr
diretamente. Ou .. como um hack, você pode aconselharsort-lines
para que antes do tipo você troque"
e<
caracteres e depois do tipo que você troque de volta. :-)