Tải bản đầy đủ - 0 (trang)
Chapter 7. Simple Text Formatting and Specialized Editing

Chapter 7. Simple Text Formatting and Specialized Editing

Tải bản đầy đủ - 0trang

7.1.UsingTabs

Tabsprovideaneasywaytodosomesimpleformatting.While

wewererevisingthisbook,wefoundthatthewayEmacs

handlestabshaschangedagreatdeal.Thissectiondescribes

firsthowEmacsworksbydefaultandthendiscusseswhatyou

candotochangethedefaultbehaviortomeetyourneeds.



7.1.1HowEmacs21HandlesTabsbyDefault

Ifyouopenanewfileintextmode,tabsareseteveryeight

spacesbydefault.(Programmingmodeshavetheirown

indentationbehavior;seeChapter9fordetails.)

PressTab.



PressingTabintextmodeorfundamentalmodeinsertsatabcharacterthatmovesthe

cursorforwardeightcolumnsbydefault.



Watchwhathappenswhenwetypeasentence.Thedefaulttab

stopschangeautomatically.

Type:ItwasthebestoftimesEnterTabTab



PressingTabtwicemovesthecursorunderthewordwas,clearlylessthaneightcolumns.



EverytimeyoupressTab,Emacsmovesthecursorunderthe

nextword.Thisisthebehaviorthatmanypeopleexpectwhen

writingcode.Neatlylinedupcodeiseasiertoread.

Asweexperimentedwiththisfeature,wewouldtabacross

undereachword,andpressEnter.Whathappensnextis

surprisingifyouarenotexpectingit.Emacsconsidersthat

newlinetobetheonlycharacteryoutypedontheline,so

pressingTabonasubsequentlinebringsyounearlytotheend

oftheline.

PressTabrepeatedlytotheendofthewindow,pressEnter,thenpressTabonce.



EmacsmovesthecursortothecolumnwhereyoupressedEnter.



IfyoupressEnterbutdon'tpressTabatall,theindentation

levelmovesbacktotheleftmargin.

Changingtabstoalignwitheachwordcanbehelpful,if,for

example,you'retypingtables.However,thedefaulttab

behaviormaynotbehelpfultoyouinallsituations.Ifyouare

interestedinchangingthedefaultbehavior,readonandwe'll

describehowtogetEmacstodowhatyouwantittodo.



7.1.2ChangingTabStops

Bydefault(andiftextisnotliningupwithsomepreviouslineof

text),tabsareseteveryeightcharacters.Emacsallowsyouto

changethepositionsofthetabstops.Tochangethetabstops,

typeM-xedit-tab-stops.A*TabStops*bufferappears.

Type:M-xedit-tab-stops



Younowseeatabstopruler;colonsshowthelocationsoftabstops.



Thecolonsinthefirstlineofthedisplayshowyouwheretab

stopsarecurrentlylocated.Thenexttwolinesformarulerthat

showseachcharacterpositionontheline.Toinsertatab,use

C-ftomovetothedesiredcolumn,andthentypeacolon(:).

Todeleteatab,movetothedesiredtab,andpressSpace.The

*TabStops*bufferisinoverwritemode,sothesechanges

won'tchangethepositionofothertabs.Makesurethatyoudo

allyoureditinginthefirstlineofthedisplay.Changesmadeto

theotherlineswon'thaveanyeffect.

Whenyou'resatisfiedwiththetabstops,pressC-cC-cto

installthem.Ifyoudon'tmakeanychanges,pressC-cC-cto

exitthebuffer.Ifyoumakesomechangesandthendecideyou

don'twantthemafterall,killthebufferbytypingC-xkEnter.

Thedefaulttabstopsremainineffect.

IfyoupressC-cC-ctoinstallthem,thenewtabsettingsaffect

allbuffersthatyoucreatebutremainineffectforthisEmacs

sessiononly.

Again,itmaywellappeartoyouthatthisfeaturedoesn'twork

asyouwouldexpect.BecauseEmacs'sdefaultbehaviortriesto

alignwithprecedinglines,changingtabstopsreallyaffectsonly

thefirstlineofanybuffer.

Inthisexample,wesetthefirsttabatcolumn51,pressedC-c

C-ctoinstallthetabstops,andstartedanewbuffer.Pressing

Tabatthebeginningofthebuffermovesthecursor

immediatelytocolumn51.Thatworksfine.

PressTabonce.



Cursormovestocolumn51.



NowwepressTabafewmoretimes,followedbyEnterto

movetoanewline.

WhenwepressTabonthesecondline,Emacsviewsthe

newlineastheonlyitemonthelastline.PressingTabmoves

usrighttotheendoftheline.

PressTabonthenextline.



Emacsmovestotheendoftheline.



Asyoucansee,changingtabstopsinthiswayisoflimited

efficacyifyou'regoingtoaddblanklinesbetweenrowsofyour

tableorwhateveryou'retyping.You'dhavetoworkaroundthis

byaddingblanklinesaftertypingthewholetable,perhaps

usingamacroasdescribedinChapter6.



7.1.3WhatifYouWantLiteralTabs?

Let'ssaythatallthistabfineryisgettingonyournerves.You

don'twantcontext-sensitiveindenting;youdon'tevenwantto

changetabstops.ThereisawaytomakeEmacstreattabsjust

likearegularoldtypewriterdid,movingovereightcharacters

atatime.[1]

[1]Youcan'tchangetabstopswiththismethod,butyoucanchangetabwidth.We'llcoverthis

shortly.



Toinsertrigid,typewriter-styletabs,pressC-qTab.Intheory,

thisshouldinsertatabcharacterintothefile,whichwouldlook

like^I.Inpractice,itmovesthecursorforwardrigidlyeight

columns.

Type:C-qTab



Thecursormoveseightcolumnsforwardanddoesnotalignwiththetextintheprevious

line.



C-qTabdoesinfactinsertatabcharacterinthefile.Youcan

checkthatbyerasingitwithasinglepressoftheDelkey.



7.1.4ChangingTabWidth

Oneproblemwithtabsisthatthereisnouniversaldefinitionof

whatatabmeans.Invi,thedefaulttabwidthisfourcolumns

versuseightcolumnsinEmacs.Further,Unixgenerallyfavors

eightcolumnsfortabswhilesomeoperatingsystemstendto

usefourspaces.Emacsuseseightcolumnsbydefaultno

matterwhatplatformit'srunningon.Ifyouviewanotheruser's

fileinEmacs,Emacsinterpretsthetabsaseightcolumnseach,

throwingthingsoff.Forthisreason,youmightwanttosetyour

tabdefaulttofourcolumnsbyaddingthislinetoyour.emacs

file:

(setq-defaulttab-width4)



YouhavetopressC-qTabtohavethemodifiedtabwidthtake

effect.



7.1.5TabsandSpaces

AnothercharacteristicofEmacs'sdefaultbehavioristhefact

thatitmayinsertacombinationoftabsandspaceswhenyou

pressTab.Trytoeraseafew"tabs"andyou'llseethatoftenit

isn'tonecharacter,buttheequivalentnumberofspacesora

combinationoftabsandspaces.Ofcourse,thislargelydepends



onthetabstopscomparedtosettingofthetab-widthvariable.

Ifyousettabstopsthataremultiplesofsixwhileyouhavea

tab-widthof4or8,Emacsisgoingtohavetousea

combinationoftabsandspacestoachievethedesiredtab

stops.

IfyouwantEmacstoinsertspacesforindentationratherthan

tabcharacters,addthislinetoyour.emacsfile:

(setq-defaultindent-tabs-modenil)



Withthissetting,Emacsinsertsonlyspaceswhenyoupress

Tab.PressingC-qTabinsteadinsertsaliteraltabcharacter.

It'ssafetosayyouwon'tentertabcharactersaccidentallywith

thissetting.



7.1.6ChangingTabstoSpaces(andViceVersa)

We'vejusttalkedaboutawaytomakesurethatEmacsinserts

spacesinsteadoftabs.Butwhatifyouinheritafileandithas

tabsthatyouwanttochangetospaces?

Emacsprovidesacommandtobanishtabsfromyourfiles.You

canusetabsforeditingandthenconvertallofthetabstothe

appropriatenumberofspacessothattheappearanceofyour

filedoesn'tchange.Unliketabs,aspaceisalmostalwayswell

defined.ThecommandforeliminatingtabsisM-xuntabify.

There'sacorrespondingcommandtoconvertspacesintotabs:

tabify.However,wetrustthatyou'lltakeouradviceandforget

aboutit.

Theuntabifycommandworksonaregion.Therefore,touseit,

youmustputthemarksomewhereinthebuffer(preferablyat

thebeginning),movetosomeotherplaceinthebuffer



(preferablytheend),andtypeM-xuntabifyEnter.The

commandC-xh(formark-whole-buffer)automaticallyputs

thecursoratthebeginningofthebufferandthemarkatthe

end.Itmakesuntabificationabiteasierbecauseyoucandoit

allatoncewiththesimplesequenceC-xhM-xuntabify

Enter.

Table7-1showsthetabcommandswe'vecoveredinthis

section.

Table7-1.Tabcommands

Keystrokes



Command

name



(none)



edit-tabstops



Openabuffercalled*TabStops*whereyoucanchangethetab

settings.



(none)



untabify



Changealltabsintotheequivalentnumberofspaces.



(none)



tabify



Changegroupsofthreeormorespacestotabswherepossible

withoutaffectingthetextplacement.



Action



7.2.IndentingText

Emacsprovidestheabilitytoindentparagraphs,likeablock

quoteinapaper.Italsoallowsyoutouseaparagraphstyle

thatindentsjustthefirstlineofaparagraph.Thissection

describesindentation-relatedcommands,includinghowto

changethemarginsforthecurrentsession.

Beforewestart,makesureyou'reintextmode.Lookatthe

modelineand,ifthewordTextisdisplayed,youareintext

mode.Ifnot,typeM-xtext-modeEntertoentertextmode.



7.2.1IndentingParagraphs

Let'ssayyou'rewritingapaperandwanttoincludesome

indentedblockquotes.Emacs'sdefaultbehaviormakesthisa

no-brainer.[2]Afteryoufinishyourfirstparagraph,usetabsor

spacestoindenttothedesiredlevelandstarttypingthequote.

Emacsautomaticallyfillstheparagraphandthequotecorrectly,

asshowninthefollowingscreen.

[2]Onceuponatime,youhadtoenterindentedtextmodeexplicitlytogetthebehaviorwe

describehere.Nowitisonbydefaultintextmode.



Someindentedtext:



Emacsindentsthetextproperlyandfillsitcorrectlyinauto-fillmode.



Whatifanindentedquotehasmultipleparagraphs?Youcould

justpressEnterandthenTabagainatthebeginningof

subsequentparagraphsoryoucouldpressC-j(fornewlineand-indent).PressingC-jtwicegivesyouablanklinebetween

paragraphs.



7.2.2IndentingtheFirstLineofaParagraph

Somepeoplepreferparagraphsinwhichthefirstlineis

indented.Knowingabouttheintricaciesoftabs,youmightbe

concernedthatpressingTabtoindenttheopeninglineofyour

paragraphwillinciteEmacstoindentthewholeparagraphas

youcontinuetyping.Anditwould,tobehonest.

Emacsprovidesaspecialmodeforthispurpose:paragraph

indenttextmode.It'salsoavailableasaminormode.Enter

eitherM-xparagraph-indent-text-modeorM-xparagraphident-minor-moderespectively.Ifyourunthemajormode,

EmacsdisplaysParindentonthemodeline.

WhenyoupressTabtostartaparagraph,Emacsinsertsatab's



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 7. Simple Text Formatting and Specialized Editing

Tải bản đầy đủ ngay(0 tr)

×