Mientras tanto, escribí un código para implementar el hack que mencionó @glucas. Tal vez solo se adapte al propósito de ordenar encabezados en C, pero espero que alguien lo encuentre ú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)))))))))
La función swap-<-and-quote-includes
transforma cada texto como <foo>
a "foo"
, y cada "foo"
a <foo>
dentro del rango dado beg , final .
Y aquí está el código para buscar y ordenar encabezados:
(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))
)))
La función c-sort-includes
busca el primer párrafo de «#include» s, y lo ordena. Lo agregué before-save-hook
con un código para ejecutarlo solo para los modos C y C ++. Contras conocidas: α) solo se ordenará el primer párrafo con inclusiones. Esto se debe a que la búsqueda hasta el final de un archivo puede ser costosa, por ejemplo, en mi trabajo con el que encontré un .c
archivo recientemente , ¿te lo puedes imaginar? - ≈16000 líneas! La solución correcta sería un modo menor, que rastrearía dónde residen los bloques de encabezado en el archivo. β) En Emacs anteriores, a partir de ≈2015 años, la función podía bloquearse; era un error que luego se solucionó.
sort-subr
directamente. O ... como truco, podrías aconsejarsort-lines
que antes del orden intercambies"
y los<
personajes y luego después del tipo los vuelvas a intercambiar. :-)